static void ppm_writefile(uint8_t *prgb, int width, int height, int num) { pixel *pixelrow; int i, x, y; pixelrow = ppm_allocrow(width); for (i = 0; i < num; i++) { char filename[16]; FILE *fp; sprintf(filename, PPM_FILENAME, i); fp = fopen(filename, "w"); ppm_writeppminit(fp, width, height, (pixval)255, 0); for (y = 0; y < height; y++) { for (x = 0; x < width; x++, prgb += 3) PPM_ASSIGN(pixelrow[x], prgb[0], prgb[1], prgb[2]); ppm_writeppmrow(fp, pixelrow, width, (pixval)255, 0); } fclose(fp); } ppm_freerow(pixelrow); }
static void convertPpmRaster(FILE * const ifP, int const format, xelval const maxval, unsigned int const cols, unsigned int const rows, FILE * const ofP, unsigned int const bytesPerLine, unsigned char * const data) { pixel * const pixels = ppm_allocrow(cols); unsigned int row; for (row = 0; row < rows; ++row) { unsigned char * p; unsigned int col; size_t bytesWritten; p = &data[0]; ppm_readppmrow(ifP, pixels, cols, maxval, format); for (col = 0; col < cols; ++col) { *p++ = PPM_GETR(pixels[col]); *p++ = PPM_GETG(pixels[col]); *p++ = PPM_GETB(pixels[col]); } bytesWritten = fwrite(data, 1, bytesPerLine, ofP); if (bytesWritten != bytesPerLine) pm_error("File write error on Row %u", row); } ppm_freerow(pixels); }
int main(int argc, char *argv[]) { struct cmdlineInfo cmdline; pixel * pixrow; unsigned int row; ppm_init(&argc, argv); parseCommandLine(argc, argv, &cmdline); ppm_writeppminit(stdout, cmdline.cols, cmdline.rows, cmdline.maxval, 0); pixrow = ppm_allocrow(cmdline.cols); for (row = 0; row < cmdline.rows; ++row) { unsigned int col; for (col = 0; col < cmdline.cols; ++col) pixrow[col] = cmdline.color; ppm_writeppmrow(stdout, pixrow, cmdline.cols, cmdline.maxval, 0); } ppm_freerow(pixrow); pm_close(stdout); return 0; }
int main(int argc, char *argv[]) { int format; int rows, cols; pixval maxval; int row; pixel* pixelrow; ppm_init(&argc, argv); if (argc-1 != 0) pm_error("Program takes no arguments. Input is from Standard Input"); ppm_readppminit(stdin, &cols, &rows, &maxval, &format); ppm_writeppminit(stdout, cols, rows, maxval, 0); pixelrow = ppm_allocrow(cols); for (row = 0; row < rows; row++) { ppm_readppmrow(stdin, pixelrow, cols, maxval, format); ppm_writeppmrow(stdout, pixelrow, cols, maxval, 0); } ppm_freerow(pixelrow); pm_close(stdin); exit(0); }
static void writeOutput(FILE * const imageout_file, FILE * const alpha_file, int const cols, int const rows, pixel * const colors, int * const data, int transparent) { /*---------------------------------------------------------------------------- Write the image in 'data' to open PPM file stream 'imageout_file', and the alpha mask for it to open PBM file stream 'alpha_file', except if either is NULL, skip it. 'data' is an array of cols * rows integers, each one being an index into the colormap 'colors'. Where the index 'transparent' occurs in 'data', the pixel is supposed to be transparent. If 'transparent' < 0, no pixels are transparent. -----------------------------------------------------------------------------*/ int row; pixel *pixrow; bit * alpharow; if (imageout_file) ppm_writeppminit(imageout_file, cols, rows, PPM_MAXMAXVAL, 0); if (alpha_file) pbm_writepbminit(alpha_file, cols, rows, 0); pixrow = ppm_allocrow(cols); alpharow = pbm_allocrow(cols); for (row = 0; row < rows; ++row ) { int col; int * const datarow = data+(row*cols); for (col = 0; col < cols; ++col) { pixrow[col] = colors[datarow[col]]; if (datarow[col] == transparent) alpharow[col] = PBM_BLACK; else alpharow[col] = PBM_WHITE; } if (imageout_file) ppm_writeppmrow(imageout_file, pixrow, cols, (pixval) PPM_MAXMAXVAL, 0); if (alpha_file) pbm_writepbmrow(alpha_file, alpharow, cols, 0); } ppm_freerow(pixrow); pbm_freerow(alpharow); if (imageout_file) pm_close(imageout_file); if (alpha_file) pm_close(alpha_file); }
int main(int argc, char *argv[]) { struct cmdlineInfo cmdline; FILE * ifP; /* Parameters of input image: */ int rows, cols; pixval maxval; int format; ppm_init(&argc, argv); parseCommandLine(argc, argv, &cmdline); ifP = pm_openr(cmdline.inputFilename); ppm_readppminit(ifP, &cols, &rows, &maxval, &format); pbm_writepbminit(stdout, cols, rows, 0); { pixel * const inputRow = ppm_allocrow(cols); bit * const maskRow = pbm_allocrow(cols); unsigned int numPixelsMasked; unsigned int row; for (row = 0, numPixelsMasked = 0; row < rows; ++row) { int col; ppm_readppmrow(ifP, inputRow, cols, maxval, format); for (col = 0; col < cols; ++col) { if (colorIsInSet(inputRow[col], maxval, cmdline)) { maskRow[col] = PBM_BLACK; ++numPixelsMasked; } else maskRow[col] = PBM_WHITE; } pbm_writepbmrow(stdout, maskRow, cols, 0); } if (cmdline.verbose) pm_message("%u pixels found matching %u requested colors", numPixelsMasked, cmdline.colorCount); pbm_freerow(maskRow); ppm_freerow(inputRow); } pm_close(ifP); return 0; }
static void fillPpmBins(FILE * const ifP, unsigned int const cols, unsigned int const rows, xelval const maxval, int const format, bool const colorWanted[3], bool const verbose, xelval const startval, xelval const endval, unsigned int const histWidth, unsigned int ** const hist) { /*---------------------------------------------------------------------------- For each wanted color component, given by colorWanted[], hist[color] is the histogram. Each histogram as 'histWidth' bins; we ignore color component values less than 'startval' and greater than or equal to 'endval' and spread the rest evenly across the 'histWidth' bins. We get the color component values from the PNM image on *ifP, which is positioned to the raster, whose format is described by 'cols', 'rows', 'maxval', and 'format'. -----------------------------------------------------------------------------*/ pixel * pixrow; unsigned int row; pixrow = ppm_allocrow(cols); if (verbose) pm_message("making histogram..."); for (row = 0; row < rows; ++row) { unsigned int col; ppm_readppmrow(ifP, pixrow, cols, maxval, format); for (col = 0; col < cols; ++col) { if (colorWanted[WANT_RED]) countComp(PPM_GETR(pixrow[col]), startval, endval, histWidth, hist[WANT_RED]); if (colorWanted[WANT_GRN]) countComp(PPM_GETG(pixrow[col]), startval, endval, histWidth, hist[WANT_GRN]); if (colorWanted[WANT_BLU]) countComp(PPM_GETB(pixrow[col]), startval, endval, histWidth, hist[WANT_BLU]); } } ppm_freerow(pixrow); }
int main(int argc, char *argv[]) { FILE* ifP; const char * inputFilespec; int eof; ppm_init( &argc, argv ); if (argc-1 > 1) pm_error("The only argument is the (optional) input filename"); if (argc == 2) inputFilespec = argv[1]; else inputFilespec = "-"; ifP = pm_openr(inputFilespec); eof = FALSE; /* initial assumption */ while (!eof) { ppm_nextimage(ifP, &eof); if (!eof) { int rows, cols, format; pixval maxval; pixel* inputRow; gray* outputRow; ppm_readppminit(ifP, &cols, &rows, &maxval, &format); pgm_writepgminit(stdout, cols, rows, maxval, 0); inputRow = ppm_allocrow(cols); outputRow = pgm_allocrow(cols); convertRaster(ifP, cols, rows, maxval, format, inputRow, outputRow, stdout); ppm_freerow(inputRow); pgm_freerow(outputRow); } } pm_close(ifP); pm_close(stdout); return 0; }
static void writePpm(FILE * const ifP, const xvPalette * const xvPaletteP, unsigned int const cols, unsigned int const rows, pixval const maxval, FILE * const ofP) { /*---------------------------------------------------------------------------- Write out the PPM image, from the XV-mini input file ifP, which is positioned to the raster. The raster contains indices into the palette *xvPaletteP. -----------------------------------------------------------------------------*/ pixel * pixrow; unsigned int row; pixrow = ppm_allocrow(cols); ppm_writeppminit(ofP, cols, rows, maxval, 0); for (row = 0; row < rows; ++row) { unsigned int col; for (col = 0; col < cols; ++col) { int byte; byte = fgetc(ifP); if (byte == EOF) pm_error("unexpected EOF"); else { unsigned int const paletteIndex = byte; assert(byte >= 0); PPM_ASSIGN(pixrow[col], xvPaletteP->red[paletteIndex], xvPaletteP->grn[paletteIndex], xvPaletteP->blu[paletteIndex]); } } ppm_writeppmrow(ofP, pixrow, cols, maxval, 0); } ppm_freerow(pixrow); }
void ppm_readcolordict(const char * const fileName, int const mustOpen, unsigned int * const nColorsP, const char *** const colornamesP, pixel ** const colorsP, colorhash_table * const chtP) { colorhash_table cht; const char ** colornames; pixel * colors; unsigned int nColors; cht = ppm_alloccolorhash(); MALLOCARRAY(colornames, MAXCOLORNAMES); colors = ppm_allocrow(MAXCOLORNAMES); if (colornames == NULL) pm_error("Unable to allocate space for colorname table."); readcolordict(fileName, mustOpen, &nColors, colornames, colors, cht); if (chtP) *chtP = cht; else ppm_freecolorhash(cht); if (colornamesP) *colornamesP = colornames; else ppm_freecolornames(colornames); if (colorsP) *colorsP = colors; else ppm_freerow(colors); if (nColorsP) *nColorsP = nColors; }
int main(int argc, char * argv[]) { FILE * ifP; struct cmdlineInfo cmdline; gray * grayrow; pixel * pixelrow; int rows, cols, format; gray maxval; ppm_init(&argc, argv); parseCommandLine(argc, argv, &cmdline); ifP = pm_openr(cmdline.inputFilename); pgm_readpgminit(ifP, &cols, &rows, &maxval, &format); grayrow = pgm_allocrow(cols); pixelrow = ppm_allocrow(cols); if (cmdline.map) convertWithMap(ifP, cols, rows, maxval, format, cmdline.map, stdout, grayrow, pixelrow); else convertLinear(ifP, cols, rows, maxval, format, cmdline.colorBlack, cmdline.colorWhite, stdout, grayrow, pixelrow); ppm_freerow(pixelrow); pgm_freerow(grayrow); pm_close(ifP); /* If the program failed, it previously aborted with nonzero completion code, via various function calls. */ return 0; }
int main(int argc, const char ** argv) { FILE * ifP; unsigned int i; pixel * pixelrow; unsigned int row; Pal pal; short screen[ROWS*COLS/4]; /* simulates the Atari's video RAM */ pm_proginit(&argc, argv); /* Check args. */ if ( argc > 2 ) pm_usage( "[spufile]" ); if ( argc == 2 ) ifP = pm_openr( argv[1] ); else ifP = stdin; /* Read the SPU file */ /* Read the screen data. */ for (i = 0; i < ROWS*COLS/4; ++i) pm_readbigshort(ifP, &screen[i]); readPalettes(ifP, &pal); pm_close(ifP); /* Ok, get set for writing PPM. */ ppm_writeppminit(stdout, COLS, ROWS, MAXVAL, 0); pixelrow = ppm_allocrow(COLS); /* Now do the conversion. */ for (row = 0; row < ROWS; ++row) { unsigned int col; for (col = 0; col < COLS; ++col) { /* Compute pixel value. */ unsigned int const ind = 80 * row + ((col >> 4) << 2); unsigned int const b = 0x8000 >> (col & 0xf); unsigned int c; unsigned int plane; unsigned int x1; c = 0; /* initial value */ for (plane = 0; plane < 4; ++plane) { if (b & screen[ind + plane]) c |= (1 << plane); } /* Compute palette index. */ x1 = 10 * c; if ((c & 1) != 0) x1 -= 5; else ++x1; if ((col >= x1 ) && (col < (x1 + 160))) c += 16; if (col >= (x1 + 160)) c += 32; /* Set the proper color. */ pixelrow[col] = pal.pal[row][c]; } ppm_writeppmrow(stdout, pixelrow, COLS, MAXVAL, 0); } ppm_freerow(pixelrow); pm_close(stdout); return 0; }
int main(int argc, char *argv[]) { FILE *ifd; FILE *ofd; int rows, cols; xelval maxval; int format; const char * const usage = "[-resolution x y] [pnmfile [ddiffile]]"; int i, j; char *outfile; int argn; int hor_resolution = 75; int ver_resolution = 75; imageparams ip; unsigned char *data, *p; pnm_init(&argc, argv); for (argn = 1;argn < argc && argv[argn][0] == '-';argn++) { int arglen = strlen(argv[argn]); if (!strncmp (argv[argn],"-resolution", arglen)) { if (argn + 2 < argc) { hor_resolution = atoi(argv[argn+1]); ver_resolution = atoi(argv[argn+2]); argn += 2; continue; } else { pm_usage(usage); } } else { pm_usage(usage); } } if (hor_resolution <= 0 || ver_resolution <= 0) { fprintf(stderr,"Unreasonable resolution values: %d x %d\n", hor_resolution,ver_resolution); exit(1); } if (argn == argc - 2) { ifd = pm_openr(argv[argn]); outfile = argv[argn+1]; if (!(ofd = fopen(outfile,"wb"))) { perror(outfile); exit(1); } } else if (argn == argc - 1) { ifd = pm_openr(argv[argn]); ofd = stdout; } else { ifd = stdin; ofd = stdout; } pnm_readpnminit(ifd, &cols, &rows, &maxval, &format); ip.width = cols; ip.height = rows; ip.h_res = hor_resolution; ip.v_res = ver_resolution; switch (PNM_FORMAT_TYPE(format)) { case PBM_TYPE: ip.bits_per_pixel = 1; ip.bytes_per_line = (cols + 7) / 8; ip.spectral = 2; ip.components = 1; ip.bits_per_component = 1; ip.polarity = 1; break; case PGM_TYPE: ip.bytes_per_line = cols; ip.bits_per_pixel = 8; ip.spectral = 2; ip.components = 1; ip.bits_per_component = 8; ip.polarity = 2; break; case PPM_TYPE: ip.bytes_per_line = 3 * cols; ip.bits_per_pixel = 24; ip.spectral = 5; ip.components = 3; ip.bits_per_component = 8; ip.polarity = 2; break; default: fprintf(stderr, "Unrecognized PBMPLUS format %d\n", format); exit(1); } if (!write_header(ofd,&ip)) { perror("Writing header"); exit(1); } if (!(p = data = (unsigned char*) malloc(ip.bytes_per_line))) { perror("allocating line buffer"); exit(1); } switch (PNM_FORMAT_TYPE(format)) { case PBM_TYPE: { bit *pixels; int mask; int k; pixels = pbm_allocrow(cols); for (i = 0; i < rows; i++) { pbm_readpbmrow(ifd, pixels, cols, format); mask = 0; p = data; for (j = 0, k = 0; j < cols; j++) { if (pixels[j] == PBM_BLACK) { mask |= 1 << k; } if (k == 7) { *p++ = mask; mask = 0; k = 0; } else { k++; } } if (k != 7) { /* Flush the rest of the column */ *p = mask; } if (fwrite(data,1,ip.bytes_per_line,ofd) != ip.bytes_per_line) { perror("Writing image data\n"); exit(1); } } } break; case PGM_TYPE: { gray *pixels = pgm_allocrow(cols); for (i = 0; i < rows; i++) { p = data; pgm_readpgmrow(ifd, pixels, cols, maxval, format); for (j = 0; j < cols; j++) { *p++ = (unsigned char) pixels[j]; } if (fwrite(data,1,ip.bytes_per_line,ofd) != ip.bytes_per_line) { perror("Writing image data\n"); exit(1); } } pgm_freerow(pixels); } break; case PPM_TYPE: { pixel *pixels = ppm_allocrow(cols); for (i = 0; i < rows; i++) { p = data; ppm_readppmrow(ifd, pixels, cols, maxval, format); for (j = 0; j < cols; j++) { *p++ = PPM_GETR(pixels[j]); *p++ = PPM_GETG(pixels[j]); *p++ = PPM_GETB(pixels[j]); } if (fwrite(data,1,ip.bytes_per_line,ofd) != ip.bytes_per_line) { perror("Writing image data\n"); exit(1); } } ppm_freerow(pixels); } break; } pm_close(ifd); free(data); if (!write_trailer(ofd)) { perror("Writing trailer"); exit(1); } if (fclose(ofd) == EOF) { perror("Closing output file"); exit(1); }; return(0); }