#include #include #include #include "imgformat.h" #include "memory.h" PGMImage *read_pgm_file(char *fn) { FILE *ifp ; PGMImage *pgmimage ; char stringbuf[255], strbuf2[255] ; int w, h, i, j ; ifp = fopen(fn, "rb") ; if (!ifp) errmsg("Input file not found") ; fscanf(ifp, "%s\n", stringbuf) ; if (strcmp(stringbuf, PGM_RAW) != 0) { fprintf(stderr, "Input file is not a PGM raw image.\n") ; return NULL ; } fgets(stringbuf, 255, ifp) ; /* might be width and height */ /* skip the comment line */ while (stringbuf[0] == '#') fgets(stringbuf, 255, ifp) ; sscanf(stringbuf, "%d %d", &w, &h) ; fgets(stringbuf, 255, ifp) ; /* maximum gray value */ sscanf(stringbuf, "%d", &i) ; pgmimage = (PGMImage *) malloc(sizeof(PGMImage)) ; strcpy(pgmimage->MagicValue, PGM_RAW) ; pgmimage->ImageWidth = w ; pgmimage->ImageHeight = h ; pgmimage->MaxGray = i ; pgmimage->data = ucmatrix(h, w) ; for (i = 0 ; i < h ; i++) for (j = 0 ; j < w ; j++) pgmimage->data[i][j] = (unsigned char) fgetc(ifp) ; fclose(ifp) ; return pgmimage ; } int write_pgm_file(char *fn, PGMImage *pgmimage) { FILE *ofp ; int i, j ; ofp = fopen(fn, "wb") ; if (!ofp) errmsg("Output file open failed") ; fprintf(ofp, "%s\n", pgmimage->MagicValue) ; fprintf(ofp, "# %s\n", fn) ; fprintf(ofp, "%d %d\n", pgmimage->ImageWidth, pgmimage->ImageHeight) ; fprintf(ofp, "%d\n", pgmimage->MaxGray) ; for (i = 0 ; i < pgmimage->ImageHeight ; i++) for (j = 0 ; j < pgmimage->ImageWidth ; j++) fputc(pgmimage->data[i][j], ofp) ; fclose(ofp) ; return 1 ; } void free_pgm_data(PGMImage *pgmimage) { free_ucmatrix(pgmimage->data, pgmimage->ImageHeight, pgmimage->ImageWidth) ; free(pgmimage) ; } PPMImage *read_ppm_file(char *fn) { FILE *ifp ; PPMImage *ppmimage ; char stringbuf[255], strbuf2[255] ; int w, h, i, j ; ifp = fopen(fn, "rb") ; if (!ifp) errmsg("Input file not found") ; fscanf(ifp, "%s\n", stringbuf) ; if (strcmp(stringbuf, PPM_RAW) != 0) { fprintf(stderr, "Input file is not a PPM raw image.\n") ; return NULL ; } fgets(stringbuf, 255, ifp) ; /* might be width and height */ /* skip the comment line */ while (stringbuf[0] == '#') fgets(stringbuf, 255, ifp) ; sscanf(stringbuf, "%d %d", &w, &h) ; fgets(stringbuf, 255, ifp) ; /* max value per pixel */ sscanf(stringbuf, "%d", &i) ; ppmimage = (PPMImage *) malloc(sizeof(PPMImage)) ; strcpy(ppmimage->MagicValue, PPM_RAW) ; ppmimage->ImageWidth = w ; ppmimage->ImageHeight = h ; ppmimage->MaxColor = i ; ppmimage->red = ucmatrix(h, w) ; /* allocate memory for red band data */ ppmimage->green = ucmatrix(h, w) ; /* allocate memory for green band data */ ppmimage->blue = ucmatrix(h, w) ; /* allocate memory for blue band data */ for (i = 0 ; i < h ; i++) for (j = 0 ; j < w ; j++) { ppmimage->red[i][j] = (unsigned char) fgetc(ifp) ; ppmimage->green[i][j] = (unsigned char) fgetc(ifp) ; ppmimage->blue[i][j] = (unsigned char) fgetc(ifp) ; } fclose(ifp) ; return ppmimage ; } int write_ppm_file(char *fn, PPMImage *ppmimage) { FILE *ofp ; int i, j ; ofp = fopen(fn, "wb") ; if (!ofp) errmsg("Output file open failed") ; fprintf(ofp, "%s\n", ppmimage->MagicValue) ; fprintf(ofp, "# %s\n", fn) ; fprintf(ofp, "%d %d\n", ppmimage->ImageWidth, ppmimage->ImageHeight) ; fprintf(ofp, "%d\n", ppmimage->MaxColor) ; for (i = 0 ; i < ppmimage->ImageHeight ; i++) for (j = 0 ; j < ppmimage->ImageWidth ; j++) { fputc(ppmimage->red[i][j], ofp) ; fputc(ppmimage->green[i][j], ofp) ; fputc(ppmimage->blue[i][j], ofp) ; } fclose(ofp) ; return 1 ; } void free_ppm_data(PPMImage *ppmimage) { free_ucmatrix(ppmimage->red, ppmimage->ImageHeight, ppmimage->ImageWidth) ; free_ucmatrix(ppmimage->green, ppmimage->ImageHeight, ppmimage->ImageWidth) ; free_ucmatrix(ppmimage->blue, ppmimage->ImageHeight, ppmimage->ImageWidth) ; free(ppmimage) ; }