📄 ppmio.c
字号:
/************************************************************************** * ppmIO.h * These functions read and write ppm files in the format output by * cqcam and xv. * Written by Elizabeth Gordon 6-98 * Modified to read xv output images by Bruce Maxwell 7-98 *************************************************************************/#include <stdio.h>#include <stdlib.h>#include <string.h>#include "ppmIO.h"#define CPP 0// read in rgb values from the ppm file output by cqcamPixel *readPPM(int *rows, int *cols, int * colors, char *filename) { char tag[40]; Pixel *image; FILE *fp; int read, num[3], curchar; if(filename != NULL && strlen(filename)) fp = fopen(filename, "r"); else fp = stdin; if(fp) { fscanf(fp, "%s\n", tag); // Read the "magic number" at the beginning of the ppm if (strncmp(tag, "P6", 40) != 0) { fprintf(stderr, "not a ppm!\n"); exit(1); } // Read the rows, columns, and color depth output by cqcam // need to read in three numbers and skip any lines that start with a # read = 0; while(read < 3) { curchar = fgetc(fp); if((char)curchar == '#') { // skip this line while(fgetc(fp) != '\n') /* do nothing */; } else { ungetc(curchar, fp); fscanf(fp, "%d", &(num[read])); read++; } } while(fgetc(fp) != '\n') /* pass the last newline character */; *cols = num[0]; *rows = num[1]; *colors = num[2]; if(*cols > 0 && *rows > 0) {#if CPP image = new Pixel[(*rows) * (*cols)];#else image = (Pixel *)malloc(sizeof(Pixel)* (*rows) * (*cols));#endif if(image) { // Read the data fread(image, sizeof(Pixel), (*rows) * (*cols), fp); if(fp != stdin) fclose(fp); return(image); } } } return(NULL); } // end read_ppm// Write the modified image out as a ppm in the correct format to be read by // read_ppm. xv will read these properly.void writePPM(Pixel *image, int rows, int cols, int colors, char *filename){ FILE *fp; if(filename != NULL && strlen(filename)) fp = fopen(filename, "w"); else fp = stdout; if(fp) { fprintf(fp, "P6\n"); fprintf(fp, "%d %d\n%d\n", cols, rows, colors); fwrite(image, sizeof(Pixel), rows * cols, fp); } fclose(fp);} // end write_ppm // Write out a texture image in a PPM-like formatvoid writeTEX(TexImage *tex, char *filename) { FILE *fp; int i; if(filename != NULL && strlen(filename)) fp = fopen(filename, "w"); else fp = stdout; if(fp) { fprintf(fp, "T1\n"); fprintf(fp, "%ld %ld\n%d\n", tex->rows, tex->cols, tex->planes); for(i=0;i<tex->planes;i++) fwrite(tex->d[i], sizeof(float), tex->rows * tex->cols, fp); } fclose(fp);} // end writeTEX// Write the modified image out as a ppm in the correct format to be read by // read_ppm. xv will read these properly.// This write routine reverses the red and blue channelsvoid writeRPM(Pixel *image, int rows, int cols, int colors, char *filename){ FILE *fp; long i;#if CPP Pixel *tmp = new Pixel[(long)rows * (long)cols];#else Pixel *tmp = malloc(sizeof(Pixel) * (long)rows * (long)cols);#endif for(i=0;i<(long)rows * (long)cols;i++) { tmp[i].r = image[i].b; tmp[i].g = image[i].g; tmp[i].b = image[i].r; } if(filename != NULL && strlen(filename)) fp = fopen(filename, "w"); else fp = stdout; if(fp) { fprintf(fp, "P6\n"); fprintf(fp, "%d %d\n%d\n", cols, rows, colors); fwrite(tmp, sizeof(Pixel), rows * cols, fp); } fclose(fp);#if CPP delete[] tmp;#else free(tmp);#endif} // end write_rpm // Write the modified image out as a pgm in the correct formatvoid writePGM(unsigned char *image, long rows, long cols, int intensities, char *filename){ FILE *fp; if(filename != NULL && strlen(filename)) fp = fopen(filename, "w"); else fp = stdout; if(fp) { fprintf(fp, "P5\n"); fprintf(fp, "%ld %ld\n%d\n", cols, rows, intensities); fwrite(image, sizeof(unsigned char), rows * cols, fp); } fclose(fp);} // end write_pgm // read in intensity values from the pgm fileunsigned char *readPGM(int *rows, int *cols, int *intensities, char *filename) { char tag[40]; unsigned char *image; FILE *fp; int read, num[3], curchar; if(filename != NULL && strlen(filename)) fp = fopen(filename, "r"); else fp = stdin; if(fp) { fscanf(fp, "%s\n", tag); // Read the "magic number" at the beginning of the ppm if (strncmp(tag, "P5", 40) != 0) { fprintf(stderr, "not a ppm!\n"); exit(1); } // Read the rows, columns, and color depth output by cqcam // need to read in three numbers and skip any lines that start with a # read = 0; while(read < 3) { curchar = fgetc(fp); if((char)curchar == '#') { // skip this line while(fgetc(fp) != '\n') /* do nothing */; } else { ungetc(curchar, fp); fscanf(fp, "%d", &(num[read])); read++; } } while(fgetc(fp) != '\n') /* pass the last newline character */; *cols = num[0]; *rows = num[1]; *intensities = num[2]; if(*intensities != 255) { printf("Unable to read this file correctly\n"); return(NULL); } if(*cols > 0 && *rows > 0) {#if CPP image = new unsigned char[(*rows) * (*cols)];#else image = (unsigned char *)malloc(sizeof(unsigned char) * (*rows) * (*cols));#endif if(image) { // Read the data fread(image, sizeof(unsigned char), (*rows) * (*cols), fp); if(fp != stdin) fclose(fp); return(image); } } } return(NULL); } // end read_pgm// read in a Texture imageTexImage *readTEX(char *filename) { char tag[40]; FILE *fp; int read, num[3], curchar; TexImage *tex; int i; if(filename != NULL && strlen(filename)) fp = fopen(filename, "r"); else fp = stdin; if(fp) { fscanf(fp, "%s\n", tag); // Read the "magic number" at the beginning of the ppm if (strncmp(tag, "T1", 40) != 0) { fprintf(stderr, "not a TEX image!\n"); exit(1); } // Read the rows, columns, and number of planes // need to read in three numbers and skip any lines that start with a # read = 0; while(read < 3) { curchar = fgetc(fp); if((char)curchar == '#') { // skip this line while(fgetc(fp) != '\n') /* do nothing */; } else { ungetc(curchar, fp); fscanf(fp, "%d", &(num[read])); read++; } } while(fgetc(fp) != '\n') /* pass the last newline character */; tex = CreateTEX(num[0], num[1], num[2]); if(tex == NULL) { printf("Memory error (-121)\n"); return(NULL); } if((tex->cols > 0) && (tex->rows > 0)) { // Read the data for(i=0;i<tex->planes;i++) fread(tex->d[i], sizeof(float), tex->rows * tex->cols, fp); if(fp != stdin) fclose(fp); return(tex); } } return(NULL);} // end readTEXTexImage *CreateTEX(long rows, long cols, long planes) { TexImage *tex; int i, j; // allocate the structure#if CPP tex = new TexImage;#else tex = (TexImage *)malloc(sizeof(TexImage));#endif if(tex == NULL) return(NULL); tex->rows = rows; tex->cols = cols; tex->planes = planes > MAX_TEXPLANES ? MAX_TEXPLANES : planes; for(i=0;i<MAX_TEXPLANES;i++) tex->d[i] = NULL; // allocate each plane for(i=0;i<tex->planes;i++) {#if CPP tex->d[i] = new float[rows * cols];#else tex->d[i] = malloc(sizeof(float) * rows * cols);#endif // error check if(tex->d[i] == NULL) { for(j=i-1;j>=0;j--) {#if CPP delete[] tex->d[i];#else free(tex->d[i]);#endif }#if CPP delete tex;#else free(tex);#endif return(NULL); } } return(tex);}void FreeTEX(TexImage *tex) { int i; for(i=0;i<tex->planes;i++) { if(tex->d[i]) {#if CPP delete[] tex->d[i];#else free(tex->d[i]);#endif } }#if CPP delete tex;#else free(tex);#endif}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -