int ppmReadFile( char *fname, unsigned char **r, unsigned char **g, unsigned char **b, int *ncols, int *nrows) { FILE *fp; unsigned char *rpt, *gpt, *bpt; int magic, npixels, maxval, i; unsigned char *rgb, *rgbpt; /* Open file */ if ( (fp = fopen(fname, "rb")) == NULL) KLTError("(ppmWriteFileRGB) Can't open file named '%s' for readingng\n", fname); ppmReadHeader(fp, &magic, ncols, nrows, &maxval); npixels = *nrows * *ncols; if(*r == NULL) { if ((*r = (unsigned char *)malloc(npixels*3)) == NULL) { KLTError("Error allocating %d bytes for PGM file: %s\n", npixels, fname); } *g = *r + npixels; *b = *g + npixels; } rgb = (unsigned char *)malloc(npixels*3); fread(rgb, npixels*3, 1, fp); rpt = *r; gpt = *g; bpt = *b; rgbpt = rgb; for(i = 0; i < npixels; ++i) { *(rpt++)=*(rgbpt++); *(gpt++)=*(rgbpt++); *(bpt++)=*(rgbpt++); } free(rgb); fclose(fp); return 1; }
void ppmReadHeaderFile( char *fname, int *magic, int *ncols, int *nrows, int *maxval) { FILE *fp; /* Open file */ if ( (fp = fopen(fname, "rb")) == NULL) KLTError("(ppmReadHeaderFile) Can't open file named '%s' for reading\n", fname); /* Read header */ ppmReadHeader(fp, magic, ncols, nrows, maxval); /* Close file */ fclose(fp); }