void pnm_writepnminit(FILE * const fileP, int const cols, int const rows, xelval const maxval, int const format, int const forceplain) { bool const plainFormat = forceplain || pm_plain_output; switch (PNM_FORMAT_TYPE(format)) { case PPM_TYPE: ppm_writeppminit(fileP, cols, rows, (pixval) maxval, plainFormat); break; case PGM_TYPE: pgm_writepgminit(fileP, cols, rows, (gray) maxval, plainFormat); break; case PBM_TYPE: pbm_writepbminit(fileP, cols, rows, plainFormat); break; default: pm_error("invalid format argument received by pnm_writepnminit(): %d" "PNM_FORMAT_TYPE(format) must be %d, %d, or %d", format, PBM_TYPE, PGM_TYPE, PPM_TYPE); } }
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; }
int main(int argc, char **argv) { FILE *ifp; register bit *bitrow, *bP; int rows, cols, row, col, charcount; unsigned char *data, mask; pbm_init ( &argc, argv ); if ( argc > 2 ) pm_usage( "[raster obj]" ); if ( argc == 2 ) ifp = pm_openr( argv[1] ); else ifp = stdin; ReadATKRaster( ifp, &cols, &rows, &data ); pm_close( ifp ); pbm_writepbminit( stdout, cols, rows, 0 ); bitrow = pbm_allocrow( cols ); for ( row = 0; row < rows; ++row ) { charcount = 0; mask = 0x80; for ( col = 0, bP = bitrow; col < cols; ++col, ++bP ) { if ( charcount >= 8 ) { ++data; charcount = 0; mask = 0x80; } *bP = ( *data & mask ) ? PBM_BLACK : PBM_WHITE; ++charcount; mask >>= 1; } ++data; pbm_writepbmrow( stdout, bitrow, cols, 0 ); } pm_close( stdout ); exit( 0 ); }
int main(int argc, char * argv[]) { FILE * ifP; unsigned char * bitrow; unsigned int rows, cols, depth; unsigned int padright; unsigned int row; unsigned int itemCount; const char * inputFileName; pbm_init(&argc, argv); if (argc-1 > 1) pm_error("Too many arguments (%u). " "Only argument is optional input file", argc-1); if (argc-1 == 1) inputFileName = argv[1]; else inputFileName = "-"; ifP = pm_openr(inputFileName); readMgrHeader(ifP, &cols, &rows, &depth, &padright); if (depth != 1) pm_error("MGR file has depth of %u, must be 1", depth); pbm_writepbminit(stdout, cols, rows, 0); bitrow = pbm_allocrow_packed(cols + padright); itemCount = (cols + padright ) / 8; for (row = 0; row < rows; ++row) { /* The raster formats are nearly identical. MGR may have rows padded to 16 or 32 bit boundaries. */ size_t bytesRead; bytesRead = fread(bitrow, 1, itemCount, ifP); if (bytesRead < itemCount) pm_error("fread() failed to read mgr bitmap data"); pbm_writepbmrow_packed(stdout, bitrow, cols, 0); } pm_close(ifP); pm_close(stdout); return 0; }
int main(int argc, char** argv) { int row; int cols; int rows; int format; bit *imageRow; pm_init(argv[0], 0); pbm_readpbminit(stdin, &cols, &rows, &format); pbm_writepbminit(stdout, cols, rows, 1); imageRow = pbm_allocrow(cols); for (row = 0; row < rows; row++) { if (row > 0) usleep(500000); pbm_readpbmrow(stdin, imageRow, cols, format); pbm_writepbmrow(stdout, imageRow, cols, 1); fflush(stdout); } pgm_freerow(imageRow); return 0; }
static void writeCroppedPBM(FILE * const ifP, unsigned int const cols, unsigned int const rows, int const format, cropSet const crop, xel const backgroundColor, FILE * const ofP) { /* See comments for writeCroppedNonPBM(), which uses identical logic flow. Uses pbm functions instead of general pnm functions. */ unsigned int const foregroundCols = cols - crop.op[LEFT].removeSize - crop.op[RIGHT].removeSize; unsigned int const outputCols = foregroundCols + crop.op[LEFT].padSize + crop.op[RIGHT].padSize; unsigned int const foregroundRows = rows - crop.op[TOP].removeSize - crop.op[BOTTOM].removeSize; unsigned int const outputRows = foregroundRows + crop.op[TOP].padSize + crop.op[BOTTOM].padSize; unsigned int const foregroundLeft = MAX(crop.op[LEFT].removeSize, crop.op[LEFT].padSize); unsigned int const foregroundRight = foregroundLeft + foregroundCols; unsigned int const allocCols = foregroundRight + MAX(crop.op[RIGHT].removeSize, crop.op[RIGHT].padSize); unsigned int const backgroundBlackWhite = PNM_EQUAL(backgroundColor, pnm_whitexel(1, PBM_TYPE)) ? 0: 1; unsigned int const readOffset = foregroundLeft - crop.op[LEFT].removeSize; unsigned int const writeOffset = foregroundLeft - crop.op[LEFT].padSize; unsigned int const lastWriteChar = writeOffset/8 + (outputCols-1)/8; unsigned char * bitrow; unsigned int i; pbm_writepbminit(ofP, outputCols, outputRows, 0); bitrow = pbm_allocrow_packed(allocCols); readOffBorderPbm(crop.op[TOP].removeSize, ifP, cols, format); outputNewBorderPbm(crop.op[TOP].padSize, outputCols, backgroundBlackWhite, ofP); /* Prepare padding: left and/or right */ fillRowPBM(bitrow, allocCols, backgroundBlackWhite); /* Read and output foreground rows */ for (i = 0; i < foregroundRows; ++i) { /* Read foreground pixels */ pbm_readpbmrow_bitoffset(ifP, bitrow, cols, format, readOffset); pbm_writepbmrow_bitoffset(ofP, bitrow, outputCols, format, writeOffset); /* If there is right-side padding, repair the write buffer distorted by pbm_writepbmrow_bitoffset() (No need to mend any left-side padding) */ if (crop.op[RIGHT].padSize > 0) bitrow[lastWriteChar] = backgroundBlackWhite * 0xff; } readOffBorderPbm(crop.op[BOTTOM].removeSize, ifP, cols, format); outputNewBorderPbm(crop.op[BOTTOM].padSize, outputCols, backgroundBlackWhite, ofP); pbm_freerow_packed(bitrow); }
int main( int argc, char * argv[]) { FILE * ifp; int argn, extraskip; const char * const usage = "[-extraskip N] [macpfile]"; int outOfSync; int pixelCnt; pbm_init( &argc, argv ); argn = 1; /* initial value */ extraskip = 0; /* initial value */ /* Check for flags. */ if ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' ) { if ( pm_keymatch( argv[argn], "-extraskip", 2 ) ) { argn++; if ( argn == argc || sscanf( argv[argn], "%d", &extraskip ) != 1 ) pm_usage( usage ); } else pm_usage( usage ); argn++; } if ( argn < argc ) { ifp = pm_openr( argv[argn] ); argn++; } else ifp = stdin; if ( argn != argc ) pm_usage( usage ); if ( extraskip > 256 * 1024 ) pm_error("-extraskip value too large"); else if ( extraskip > 0 ) skipExtraBytes( ifp, extraskip); else skipHeader( ifp ); pbm_writepbminit( stdout, MACP_COLS, MACP_ROWS, 0 ); ReadMacPaintFile( ifp, &outOfSync, &pixelCnt ); /* We may not be at EOF. Macpaint files often have extra bytes after image data. */ pm_close( ifp ); if ( pixelCnt == 0 ) pm_error("No image data."); else if ( pixelCnt < MACP_BYTES ) pm_error("Compressed image data terminated prematurely."); else if ( outOfSync > 0 ) pm_message("Warning: Corrupt image data. %d rows misaligned.", outOfSync); pm_close( stdout ); exit( 0 ); }