Code:
int LoadImage(char *Name,imginfo *I)
{
FILE *img;
unsigned char *Data;
/* Data : [XSize*YSize*ZSize)], Acces=[(Y*XSize+X)(*Zsz)+(z)] */
int P,x,y,z,v;
int XSize,YSize,ZSize,Ascii;
char TBuf[100];
/* default values */
I->XSize=I->YSize=I->ZSize=0;
I->Data=NULL;
img=fopen(Name,"r");
if( img==NULL ) {
fprintf(stderr,"LoadImage: Unable to open '%s'\n",Name);return(-1);
}
/** Read header **/
if( fgets(TBuf,100,img)!=TBuf ) {
fprintf(stderr,"LoadImage: Unable to read header of '%s'\n",Name);
return(-1);
}
if( strncmp(TBuf,"P5",2)==0 ) { Ascii=0;ZSize=1; } /* pgm raw */
else if( strncmp(TBuf,"P6",2)==0 ) { Ascii=0;ZSize=3; } /* ppm raw */
else if( strncmp(TBuf,"P2",2)==0 ) { Ascii=1;ZSize=1; } /* pgm ascii */
else if( strncmp(TBuf,"P3",2)==0 ) { Ascii=1;ZSize=3; } /* ppm ascii */
else {
fprintf(stderr,"LoadImage: Unsupported PNM format for '%s'\n",Name);
return(-1);
}
/* Skip comments */
do {
if( fgets(TBuf,100,img)!=TBuf ) return(-1);
} while( TBuf[0]=='#' );
/* xres/yres */
sscanf(TBuf,"%d %d",&XSize,&YSize);
/* Number of gray , usually 255, Just skip it */
if( fgets(TBuf,100,img)!=TBuf ) return(-1);
/* print a little info about the image */
#ifdef VERBOSE
printf("Image x and y size in pixels: %d %d\n"
,XSize,YSize);
printf("Image zsize in channels: %d\n",ZSize);
printf("Image pixel min and max: %d %d\n",0,255);
#endif
/* Allocate data space , as char data... for now */
Data=(unsigned char *)malloc(XSize*YSize*ZSize);
if( Data==NULL ) { fprintf(stderr,"LoadImage: Out of mem\n");return(-1); }
if( Ascii==0 ) {
for(y=0;y<YSize;y++) {
#ifdef INVERSE_Y
P=(YSize-1-y)*XSize*ZSize;
#else
P=y*XSize*ZSize;
#endif
fread(Data+P,1,XSize*ZSize,img);
}
}else{
for(y=0;y<YSize;y++)
for(x=0;x<XSize;x++)
for(z=0;z<ZSize;z++) {
#ifdef INVERSE_Y
P=((YSize-1-y)*XSize+x)*ZSize+z;
#else
P=(y*XSize+x)*ZSize+z;
#endif
fscanf(img," %d",&v);
Data[P]=v;
}
}
I->Data=Data;
I->XSize=XSize;
I->YSize=YSize;
I->ZSize=ZSize;
fclose(img);
return(0);
}
Doesn't that mean that Data points to the pixel values in my image?