static void writepbmrow(FILE * const fileP, const xel * const xelrow, unsigned int const cols, bool const plainFormat) { jmp_buf jmpbuf; jmp_buf * origJmpbufP; bit * bitrow; bitrow = pbm_allocrow(cols); if (setjmp(jmpbuf) != 0) { pbm_freerow(bitrow); pm_setjmpbuf(origJmpbufP); pm_longjmp(); } else { unsigned int col; pm_setjmpbufsave(&jmpbuf, &origJmpbufP); for (col = 0; col < cols; ++col) bitrow[col] = PNM_GET1(xelrow[col]) == 0 ? PBM_BLACK : PBM_WHITE; pbm_writepbmrow(fileP, bitrow, cols, plainFormat); pm_setjmpbuf(origJmpbufP); } pbm_freerow(bitrow); }
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); }
void pnm_writepnmrow(FILE * const fileP, xel * const xelrow, int const cols, 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_writeppmrow(fileP, (pixel*) xelrow, cols, (pixval) maxval, plainFormat); break; case PGM_TYPE: { gray* grayrow; unsigned int col; grayrow = pgm_allocrow(cols); for (col = 0; col < cols; ++col) grayrow[col] = PNM_GET1(xelrow[col]); pgm_writepgmrow(fileP, grayrow, cols, (gray) maxval, plainFormat); pgm_freerow( grayrow ); } break; case PBM_TYPE: { bit* bitrow; unsigned int col; bitrow = pbm_allocrow(cols); for (col = 0; col < cols; ++col) bitrow[col] = PNM_GET1(xelrow[col]) == 0 ? PBM_BLACK : PBM_WHITE; pbm_writepbmrow(fileP, bitrow, cols, plainFormat); pbm_freerow(bitrow); } break; default: pm_error("invalid format argument received by pnm_writepnmrow(): %d" "PNM_FORMAT_TYPE(format) must be %d, %d, or %d", format, PBM_TYPE, PGM_TYPE, PPM_TYPE); } }
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 convertPbmRaster(FILE * const ifP, int const format, unsigned int const cols, unsigned int const rows, FILE * const ofP, unsigned int const bytesPerLine, unsigned char * const data) { bit * const pixels = pbm_allocrow(cols); unsigned int row; for (row = 0; row < rows; ++row) { unsigned int col; unsigned int k; unsigned int mask; unsigned char * p; size_t bytesWritten; pbm_readpbmrow(ifP, pixels, cols, format); mask = 0x00; p = &data[0]; for (col = 0, k = 0; col < cols; ++col) { if (pixels[col] == PBM_BLACK) mask |= 1 << k; if (k == 7) { *p++ = mask; mask = 0x00; k = 0; } else ++k; } if (k != 7) /* Flush the rest of the column */ *p = mask; bytesWritten = fwrite(data, 1, bytesPerLine, ofP); if (bytesWritten != bytesPerLine) pm_error("File write error on Row %u", row); } pbm_freerow(pixels); }
static void doPage(FILE * const ifP, struct cmdlineInfo const cmdline) { bit * bitrow; int rows, cols, format, row; unsigned int blankRows; bool rowIsBlank; pbm_readpbminit(ifP, &cols, &rows, &format); bitrow = pbm_allocrow(cols); allocateBuffers(cols); putinit(cmdline); blankRows = 0; prevRowBufferIndex = 0; memset(prevRowBuffer, 0, rowBufferSize); for (row = 0; row < rows; ++row) { pbm_readpbmrow(ifP, bitrow, cols, format); convertRow(bitrow, cols, cmdline.pack, cmdline.delta, &rowIsBlank); if (rowIsBlank) ++blankRows; else { printBlankRows(blankRows); blankRows = 0; printRow(); } } printBlankRows(blankRows); blankRows = 0; putrest(!cmdline.noreset); freeBuffers(); pbm_freerow(bitrow); }
int main(int argc, char * argv[]) { FILE* ifp; bit* bitrow; int rows, cols, format; int padright; int row; const char * inputFilename; const char *name; int itemsperline; int bitsperitem; int item; int firstitem; const char hexchar[] = "0123456789abcdef"; pbm_init(&argc, argv); if (argc-1 > 1) pm_error("Too many arguments (%d). The only valid argument is an " "input file name.", argc-1); else if (argc-1 == 1) inputFilename = argv[1]; else inputFilename = "-"; generateName(inputFilename, &name); ifp = pm_openr(inputFilename); pbm_readpbminit(ifp, &cols, &rows, &format); bitrow = pbm_allocrow(cols); /* Compute padding to round cols up to the nearest multiple of 8. */ overflow_add(cols, 8); padright = ((cols + 7)/8) * 8 - cols; printf("#define %s_width %d\n", name, cols); printf("#define %s_height %d\n", name, rows); printf("static char %s_bits[] = {\n", name); itemsperline = 0; bitsperitem = 0; item = 0; firstitem = 1; #define PUTITEM \ { \ if ( firstitem ) \ firstitem = 0; \ else \ putchar( ',' ); \ if ( itemsperline == 15 ) \ { \ putchar( '\n' ); \ itemsperline = 0; \ } \ if ( itemsperline == 0 ) \ putchar( ' ' ); \ ++itemsperline; \ putchar('0'); \ putchar('x'); \ putchar(hexchar[item >> 4]); \ putchar(hexchar[item & 15]); \ bitsperitem = 0; \ item = 0; \ } #define PUTBIT(b) \ { \ if ( bitsperitem == 8 ) \ PUTITEM; \ if ( (b) == PBM_BLACK ) \ item += 1 << bitsperitem; \ ++bitsperitem; \ } for (row = 0; row < rows; ++row) { int col; pbm_readpbmrow(ifp, bitrow, cols, format); for (col = 0; col < cols; ++col) PUTBIT(bitrow[col]); for (col = 0; col < padright; ++col) PUTBIT(0); } pm_close(ifp); if (bitsperitem > 0) PUTITEM; printf("};\n"); pbm_freerow(bitrow); strfree(name); exit(0); }
/* Read all pbm images from a filehandle and print them */ static void process_handle(FILE * const fh, unsigned char const graph_mode, unsigned int const passes) { int eof; while(pbm_nextimage(fh, &eof), eof == 0) { /* pbm header dats */ int cols, rows, format; /* iteration variables */ unsigned int x, y; unsigned int bitline; /* pixel line within a sigle printing line */ unsigned int pass; /* here we build the to-be-printed data */ unsigned char *output; /* for reading one row from the file */ bit *row; /* Enable printer in case it is disabled, do it only once */ if(!sent_xon) { putchar(0x11); sent_xon = TRUE; } pbm_readpbminit(fh, &cols, &rows, &format); output = malloc(sizeof(*output) * cols * passes); if(output == NULL) pm_error("Out of memory"); row = pbm_allocrow(cols); for(y = 0; y < rows; y += 8 * passes) { memset(output, 0, sizeof(*output) * cols * passes); for(bitline = 0; bitline < 8; ++bitline) for(pass = 0; pass < passes; ++pass) /* don't read beyond the end of the image if height is not a multiple of passes */ if(y + bitline * passes + pass < rows) { pbm_readpbmrow(fh, row, cols, format); for(x = 0; x < cols; ++x) if(row[x] == PBM_BLACK) output[cols * pass + x] |= 1 << (7 - bitline); } for(pass = 0; pass < passes; ++pass){ /* write graphics data */ putchar(0x1b); putchar(graph_mode); putchar(cols & 0xff); putchar((cols >> 8) & 0xff); fwrite(output + pass * cols, sizeof(*output), cols, stdout); /* Carriage return */ putchar('\r'); /* move one pixel down */ putchar(0x1b); putchar('J'); putchar(4 / passes); } /* move one line - passes pixel down */ putchar(0x1b); putchar('J'); putchar(24 - 4); } putchar(0x0c); /* Form-feed */ pbm_freerow(row); free(output); } }