int main(int argc, const char *argv[]) { FILE * ifP; bit * bitrow; int rows; int cols; int format; unsigned int padright; unsigned int row; const char * inputFile; pm_proginit(&argc, argv); if (argc-1 < 1) inputFile = "-"; else { inputFile = argv[1]; if (argc-1 > 2) pm_error("Too many arguments. The only argument is the optional " "input file name"); } ifP = pm_openr(inputFile); pbm_readpbminit(ifP, &cols, &rows, &format); if (rows > INT16MAX || cols > INT16MAX) pm_error("Input image is too large."); bitrow = pbm_allocrow(cols); /* Compute padding to round cols up to the nearest multiple of 16. */ padright = ((cols + 15) / 16) * 16 - cols; putinit(cols, rows); for (row = 0; row < rows; ++row) { unsigned 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); } if (ifP != stdin) fclose(ifP); putrest(); return 0; }
static void convert (int width, int height, int format) { int i ; unsigned char *sixel ; /* A row of sixels */ int sixel_row ; bit *row = pbm_allocrow (width) ; sixel = (unsigned char *) malloc (width + 2) ; sixel_row = 0 ; while (height--) { pbm_readpbmrow (input, row, width, format) ; switch (sixel_row) { case 0 : for (i = 0 ; i < width ; i++) sixel[i] = row[i] ; break ; case 1 : for (i = 0 ; i < width ; i++) sixel[i] += row[i] << 1 ; break ; case 2 : for (i = 0 ; i < width ; i++) sixel[i] += row[i] << 2 ; break ; case 3 : for (i = 0 ; i < width ; i++) sixel[i] += row[i] << 3 ; break ; case 4 : for (i = 0 ; i < width ; i++) sixel[i] += row[i] << 4 ; break ; case 5 : for (i = 0 ; i < width ; i++) sixel[i] += (row[i] << 5) + 077 ; output_sixel_record (sixel, width) ; break ; } if (sixel_row == 5) sixel_row = 0 ; else sixel_row++ ; } if (sixel_row > 0) { /* Incomplete sixel record needs to be output */ for (i = 0 ; i < width ; i++) sixel[i] += 077 ; output_sixel_record (sixel, width) ; } }
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, row, col; pbm_init( &argc, argv ); if ( argc > 2 ) pm_usage( "[pbmfile]" ); if ( argc == 2 ) ifp = pm_openr( argv[1] ); else ifp = stdin; pbm_readpbminit( ifp, &cols, &rows, &format ); if( rows>INT16MAX || cols>INT16MAX ) pm_error ("Input image is too large."); bitrow = pbm_allocrow( cols ); putinit (rows, cols); for ( row = 0; row < rows; ++row ) { #ifdef DEBUG fprintf (stderr, "row %d\n", row); #endif pbm_readpbmrow( ifp, bitrow, cols, format ); for ( col = 0; col < cols; ++col ) putbit( bitrow[col] ); putrow( ); } flushrow (); pm_close( ifp ); exit( 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; }
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); }
bitmap_type * pbm_get_block (unsigned height) { static int image_format; static int image_height; static int image_width = -1; static unsigned scanline_count = 0; static one_byte *saved_scanline = NULL; dimensions_type d; unsigned row; boolean row_has_black; bitmap_type *b = XTALLOC (1, bitmap_type); boolean found_black = false; int c = getc (pbm_input_file); if (c == EOF) return NULL; ungetc (c, pbm_input_file); if (image_width == -1) pbm_readpbminit (pbm_input_file, &image_width, &image_height, &image_format); DIMENSIONS_WIDTH (d) = image_width; DIMENSIONS_HEIGHT (d) = height; *b = new_bitmap (d); for (row = 0; row < height; row += found_black) { if (scanline_count == image_height) FATAL2 ("%s: Tried to read image row %d", pbm_input_filename, scanline_count); if (saved_scanline) { memcpy (BITMAP_ROW (*b, row), saved_scanline, image_width); saved_scanline = NULL; } else { pbm_readpbmrow (pbm_input_file, BITMAP_ROW (*b, row), image_width, image_format); scanline_count++; if (trace_scanlines) { printf ("%5d:", scanline_count); for (c = 0; c < image_width; c++) putchar (BITMAP_ROW (*b, row)[c] ? '*' : ' '); putchar ('\n'); } } /* Ignore this row if it was all-white and we haven't seen any black ones yet. */ row_has_black = memchr (BITMAP_ROW (*b, row), BLACK, image_width) != NULL; if (!found_black && row_has_black) { /* Our first non-blank row. */ found_black = true; } if (row >= height - BLANK_COUNT && row_has_black) { /* We've hit a nonblank row where we shouldn't have. Save this row and return. */ saved_scanline = BITMAP_ROW (*b, row); break; } } BITMAP_HEIGHT (*b) = height - BLANK_COUNT; return b; }
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); }
/* 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); } }