/* This is a distributed version of pgm read/write functions. pgmread(), pgmwrite(), pgmread2(), pgmwrite2() was revised by Ozy, who is the TA of CECS365 in Univ. of Missouri-Columbia. ppmwrite2() was writen by Hai Jiang, just a little change from pgmwrite2(). */ #include #include /* unsigned char* pgmread(char* filename, int* w, int* h) * * Reads a binary or ascii pgm file and returns a pointer to the * image in 2D matrix (wxh) which are the width and height * returned through the pointers that are given. The calling * function has responsibility to free the memory pointed to by * the returned value. A NULL is returned in the case of failure to * correctly read the file. * * NOTE: the image buffer returned is in column dominant format */ unsigned char** pgmread(char* filename, int* w, int* h) { FILE* file; char line[256]; int maxval; int binary; int nread; int i,j, int_tmp; unsigned char** data; unsigned char* bindata; if ((file = fopen(filename, "r")) == NULL) { printf("ERROR: file open failed\n"); *h = *w = 0; return(NULL); } fgets(line, 256, file); if (strncmp(line,"P5", 2)) { if (strncmp(line,"P2", 2)) { printf("pgm read: not a pgm file\n"); *h = *w = 0; return(NULL); } else binary = 0; } else binary = 1; fgets(line, 256, file); while (line[0] == '#') fgets(line, 256, file); sscanf(line,"%d %d", w, h); fgets(line, 256, file); sscanf(line, "%d", &maxval); if ((data = (unsigned char**)calloc((*w), sizeof(unsigned char*))) == NULL) { printf("Memory allocation error. Exit program"); exit(1); } for (j=0 ; j < (*w); j++) if ((data[j] = (unsigned char*)calloc((*h), sizeof(unsigned char))) == NULL) { printf("Memory allocation error. Exit program"); exit(1); } if (binary) { if ((bindata = (unsigned char*)calloc((*w)*(*h), sizeof(unsigned char))) == NULL) { printf("Memory allocation error on bindata. Exit program"); exit(1); } printf("Reading %s as binary.\n", filename); nread = fread((void*)bindata, sizeof(unsigned char), (*w)*(*h), file); for(i=0; i< (*w); i++) for(j=0; j< (*h); j++) data[i][j] = (unsigned char)bindata[(j*(*w))+i]; free(bindata); } else { printf("Reading %s as ascii.\n", filename); for (i = 0; i < (*h); i++) { for (j = 0; j < (*w); j++) { fscanf(file,"%d", &int_tmp); data[j][i] = (unsigned char)int_tmp; } } } fclose(file); return(data); } /* write a binary pgm or an ascii pgm file with the comment string in the header * int pgmwrite(char* filename, int w, int h, unsigned char* data, * char* comment_string, int binsave) * Writes a binary pgm file to the file name given * and returns a 0 if successful. Input parameters are the pointer to 2D matrix * with wxh dimension, w and h are the width and height respectively. * The comment string should be passed as NULL if no comments are to be included. * Setting binsave = 1 will save the file as binary pgm, else it is ascii pgm. * * NOTE: this function assumes input bufefr "data" is in column dominant format. */ int pgmwrite(char* filename, int w, int h, unsigned char** data, char* comment_string, int binsave) { FILE* file; char line[256]; int maxval; int binary; int nread; int i,j, int_tmp; unsigned char* temp; if ((file = fopen(filename, "w")) == NULL) { printf("ERROR: file open failed\n"); return(-1); } if (binsave == 1) fprintf(file,"P5\n"); else fprintf(file,"P2\n"); if (comment_string != NULL) fprintf(file,"# %s \n", comment_string); fprintf(file,"%d %d \n", w, h); maxval = 0; for (i = 0; i < w; i++) for (j=0; j < h; j++) if ((int)data[i][j] > maxval) maxval = (int)data[i][j]; fprintf(file, "%d \n", maxval); if (binsave == 1) { temp = (unsigned char*)calloc(w*h, sizeof(unsigned char)); for(i=0; i maxval) maxval = (int)data[i][j]; fprintf(file, "%d\n", maxval); if (binsave == 1) { temp = (unsigned char*)calloc(row*col, sizeof(unsigned char)); for(i=0; i maxval) maxval = (int)datar[i][j]; maxval = 255; fprintf(file, "%d\n", maxval); if (binsave == 1) { temp = (unsigned char*)calloc(3*row*col, sizeof(unsigned char)); for(i=0; i