ppm
Posted: 2010年1月08日(金) 00:56
今、北海道の工業系の大学に通っているものです。Cのプログラミングをして1年の初心者です。
現在、C++でマンデルブロを描画するためにPPMを使って描画しようとしているのですが、PPM画像用のヘッダにエラーが出ます。
ppb_ppm.h(35) : error C2514: 'real_ppm::stat' : クラスにコンストラクタが定義されていません。
ppb_ppm.h(30) : 'real_ppm::stat' の宣言を確認してください。
ppb_ppm.h(36) : error C2228: '.st_size' の左側はクラス、構造体、共用体でなければなりません。
型は 'real_ppm::stat *' です。
代わりに '->' を使用しますか?
どこを直したらエラーがなくなるでしょうか。皆さんのお力を貸してください。
実行環境は、microsoft windows xp sp3、microsoft visual C++ 2008 express editionです。
以下にPPMのヘッダを書きます。
/* ppm.h */
#ifndef _PPB_PPM_H
#define _PPB_PPM_H
#define PPM_MAGIC "P6"
typedef struct _rgb_t{
unsigned char r;
unsigned char g;
unsigned char b;
} rgb_t;
typedef struct _image_t{
int width;
int height;
rgb_t *buf;
} image_t;
int real_ppm(image_t* img,char* fname){
char *token, *pc;
char *buf;
char *del=" \t\n";
int filesize;
int i,w,h,luma,pixs;
struct stat *st;
rgb_t *dot;
FILE *fp;
stat(fname,&st);
filesize=(int)st.st_size;
buf=(char *)malloc(filesize*sizeof(char));
if((fp=fopen(fname, "r"))==NULL){
printf("Failed to open file\n");
return -1;
}
fseek(fp,0,SEEK_SET);
fread(buf,filesize * sizeof(char),1,fp);
fclose(fp);
token=(char *)strtok(buf,del);
if(strncmp(token,PPM_MAGIC,2) !=0){
return -1;
}
token=(char *)strtok(NULL,del);
if(token[0]=='#'){
token=(char *)strtok(NULL,"\n");
token=(char *)strtok(NULL,del);
}
w=strtoul(token,&pc,10);
token=(char *)strtok(NULL,del);
h=strtoul(token,&pc,10);
token=(char *)strtok(NULL,del);
luma=strtoul(token,&pc,10);
token=pc+1;
pixs=w*h;
img->buf=(rgb_t *)malloc(pixs * sizeof(rgb_t));
dot=img->buf;
for(i=0;i<pixs;i++,dot++){
dot->r=*token++;
dot->g=*token++;
dot->b=*token++;
}
img->width=w;
img->height=h;
return 0;
}
int write_ppm(image_t* img, char* fname){
int i,j;
int w=img->width;
int h=img->height;
FILE *fp;
if((fp=fopen(fname,"wb+"))==NULL){
fprintf(stderr,"failed to open file %s\n",fname);
return -1;
}
fprintf(fp,"%s\n%d %d\n255\n",PPM_MAGIC,w,h);
for(i=0;i<w;i++){
for(j=0;j<h;j++){
putc((int)img->buf[i*w+j].r,fp);
putc((int)img->buf[i*w+j].g,fp);
putc((int)img->buf[i*w+j].b,fp);
}
}
fclose(fp);
return 0;
}
void new_image(image_t* img,int w, int h){
img->width=w;
img->height=h;
img->buf=(rgb_t*)malloc(w*h*sizeof(rgb_t));
}
void delete_image(image_t *img){
free(img->buf);
}
#endif
現在、C++でマンデルブロを描画するためにPPMを使って描画しようとしているのですが、PPM画像用のヘッダにエラーが出ます。
ppb_ppm.h(35) : error C2514: 'real_ppm::stat' : クラスにコンストラクタが定義されていません。
ppb_ppm.h(30) : 'real_ppm::stat' の宣言を確認してください。
ppb_ppm.h(36) : error C2228: '.st_size' の左側はクラス、構造体、共用体でなければなりません。
型は 'real_ppm::stat *' です。
代わりに '->' を使用しますか?
どこを直したらエラーがなくなるでしょうか。皆さんのお力を貸してください。
実行環境は、microsoft windows xp sp3、microsoft visual C++ 2008 express editionです。
以下にPPMのヘッダを書きます。
/* ppm.h */
#ifndef _PPB_PPM_H
#define _PPB_PPM_H
#define PPM_MAGIC "P6"
typedef struct _rgb_t{
unsigned char r;
unsigned char g;
unsigned char b;
} rgb_t;
typedef struct _image_t{
int width;
int height;
rgb_t *buf;
} image_t;
int real_ppm(image_t* img,char* fname){
char *token, *pc;
char *buf;
char *del=" \t\n";
int filesize;
int i,w,h,luma,pixs;
struct stat *st;
rgb_t *dot;
FILE *fp;
stat(fname,&st);
filesize=(int)st.st_size;
buf=(char *)malloc(filesize*sizeof(char));
if((fp=fopen(fname, "r"))==NULL){
printf("Failed to open file\n");
return -1;
}
fseek(fp,0,SEEK_SET);
fread(buf,filesize * sizeof(char),1,fp);
fclose(fp);
token=(char *)strtok(buf,del);
if(strncmp(token,PPM_MAGIC,2) !=0){
return -1;
}
token=(char *)strtok(NULL,del);
if(token[0]=='#'){
token=(char *)strtok(NULL,"\n");
token=(char *)strtok(NULL,del);
}
w=strtoul(token,&pc,10);
token=(char *)strtok(NULL,del);
h=strtoul(token,&pc,10);
token=(char *)strtok(NULL,del);
luma=strtoul(token,&pc,10);
token=pc+1;
pixs=w*h;
img->buf=(rgb_t *)malloc(pixs * sizeof(rgb_t));
dot=img->buf;
for(i=0;i<pixs;i++,dot++){
dot->r=*token++;
dot->g=*token++;
dot->b=*token++;
}
img->width=w;
img->height=h;
return 0;
}
int write_ppm(image_t* img, char* fname){
int i,j;
int w=img->width;
int h=img->height;
FILE *fp;
if((fp=fopen(fname,"wb+"))==NULL){
fprintf(stderr,"failed to open file %s\n",fname);
return -1;
}
fprintf(fp,"%s\n%d %d\n255\n",PPM_MAGIC,w,h);
for(i=0;i<w;i++){
for(j=0;j<h;j++){
putc((int)img->buf[i*w+j].r,fp);
putc((int)img->buf[i*w+j].g,fp);
putc((int)img->buf[i*w+j].b,fp);
}
}
fclose(fp);
return 0;
}
void new_image(image_t* img,int w, int h){
img->width=w;
img->height=h;
img->buf=(rgb_t*)malloc(w*h*sizeof(rgb_t));
}
void delete_image(image_t *img){
free(img->buf);
}
#endif