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 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); }
int main (int argc, char **argv) { int argc_copy = argc ; char **argv_copy = argv ; int argn; const char * const usage = "[-left <nn>] [-right <nn>] [-top <nn>] " "[-bottom <nn>] [-formlength <nn>] [pbmfile]"; /* Options */ /* These defaults are for a DEC LN03 with A4 paper (2400x3400 pixels) */ const char *opt_left_margin = "0"; const char *opt_top_margin = opt_left_margin; const char *opt_right_margin = "2400"; const char *opt_bottom_margin = "3400"; const char *opt_form_length = opt_bottom_margin; int width, height, format ; pbm_init (&argc_copy, argv_copy) ; argn = 1; while( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' ) { if( pm_keymatch(argv[argn], "-left", 2) ) { if( ++argn >= argc ) pm_usage(usage); opt_left_margin = argv[argn]; } else if( pm_keymatch(argv[argn], "-right", 2) ) { if( ++argn >= argc ) pm_usage(usage); opt_right_margin = argv[argn]; } else if( pm_keymatch(argv[argn], "-top", 2) ) { if( ++argn >= argc ) pm_usage(usage); opt_top_margin = argv[argn]; } else if( pm_keymatch(argv[argn], "-bottom", 2) ) { if( ++argn >= argc ) pm_usage(usage); opt_bottom_margin = argv[argn]; } else if( pm_keymatch(argv[argn], "-formlength", 2) ) { if( ++argn >= argc ) pm_usage(usage); opt_form_length = argv[argn]; } else pm_usage(usage); ++argn; } if( argn < argc ) { input = pm_openr( argv[argn] ); argn++; } else input = stdin; if( argn != argc ) pm_usage(usage); /* Initialise pbm file */ pbm_readpbminit (input, &width, &height, &format) ; if (format != PBM_FORMAT && format != RPBM_FORMAT) pm_error ("input not in PBM format") ; /* * In explanation of the sequence below: * <ESC>[!p DECSTR soft terminal reset * <ESC>[11h PUM select unit of measurement * <ESC>[7 I SSU select pixel as size unit * <ESC>[?52l DECOPM origin is corner of printable area * <ESC>[%s;%ss DECSLRM left and right margins * <ESC>[%s;%sr DECSTBM top and bottom margins * <ESC>[%st DECSLPP form length * <ESC>P0;0;1q select sixel graphics mode * "1;1 DECGRA aspect ratio (1:1) */ /* Initialise sixel file */ printf ("\033[!p\033[11h\033[7 I\033[?52l\033[%s;%ss\033" "[%s;%sr\033[%st\033P0;0;1q\"1;1", opt_left_margin, opt_right_margin, opt_top_margin, opt_bottom_margin, opt_form_length); /* Convert data */ convert (width, height, format) ; /* Terminate sixel data */ print ("\033\\\n") ; /* If the program failed, it previously aborted with nonzero completion code, via various function calls. */ return 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* ifP; int rows, cols; int format; unsigned int row, idx, len; unsigned int h, v; unsigned char *bytes, *cprbytes; struct cmdlineInfo cmdline; pbm_init(&argc, argv); parseCommandLine(argc, argv, &cmdline); ifP = pm_openr(cmdline.inputFileName); pbm_readpbminit(ifP, &cols, &rows, &format); bytes = malloc(24*pbm_packed_bytes(cols)+2); cprbytes = malloc(2*24*pbm_packed_bytes(cols)); if (bytes == NULL || cprbytes == NULL) pm_error("Cannot allocate memory"); h = v = 3600/cmdline.resolution; /* Set raster graphic mode. */ printf("%c%c%c%c%c%c", esc, '(', 'G', 1, 0, 1); /* Set line spacing in units of 1/360 inches. */ printf("%c%c%c", esc, '+', 24*h/10); /* Write out raster stripes 24 rows high. */ for (row = 0; row < rows; row += 24) { unsigned int const linesThisStripe = (rows-row<24) ? rows%24 : 24; printf("%c%c%c%c%c%c%c%c", esc, '.', cmdline.compress, v, h, linesThisStripe, cols%256, cols/256); /* Read pbm rows, each padded to full byte */ for (idx = 0; idx < 24 && row+idx < rows; ++idx) pbm_readpbmrow_packed(ifP,bytes+idx*pbm_packed_bytes(cols), cols,format); /* Write raster data. */ if (cmdline.compress != 0) { /* compressed */ len = enc_epson_rle(linesThisStripe * pbm_packed_bytes(cols), bytes, cprbytes); fwrite(cprbytes,len,1,stdout); } else /* uncompressed */ fwrite(bytes, pbm_packed_bytes(cols), linesThisStripe, stdout); if (rows-row >= 24) putchar('\n'); } free(bytes); free(cprbytes); pm_close(ifP); /* Reset printer. */ printf("%c%c", esc, '@'); 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); } }
/*{{{ void main(int argc, char **argv)*/ void main(int argc, char **argv) { int i; int c; char *ptype=NULL; extern char *optarg; extern int optind; pbm_init(&argc, argv); /*{{{ argument parsing*/ while((c=getopt(argc, argv,"vhp:"))!=EOF) switch(c) { case 'v' : verbose=1; break; case 'h' : printf("Available drivers:\n"); for(i=0; i<PRINTERS; i++) printf(" %s\n", printer[i].name); putchar('\n'); usage(); break; case 'p' : ptype=optarg; break; } /*}}} */ if(verbose) fprintf(stderr, "verbose mode:\n"); /*{{{ open file or stdin*/ switch(argc-optind) { case 1: if(verbose) fprintf(stderr, "opening %s ...\n", argv[optind]); fp=pm_openr(argv[optind]); break; case 0: if(verbose) fprintf(stderr, "opening stdin ...\n"); fp=stdin; break; default: usage(); } /*}}} */ /*{{{ check printer, read header and go*/ i=0; /*{{{ check printerdevice*/ if(ptype) while(strcmp(ptype, printer[i].name) && i<PRINTERS) i++; if(verbose && i<PRINTERS) fprintf(stderr, "selecting printertype %s\n", printer[i].name); if (i>=PRINTERS) { fprintf(stderr, "%s: '%s': No such printerdriver\n", argv[0], ptype); usage(); } /*}}} */ while ((c=fgetc(fp))!=EOF) { /* read multiple pages */ ungetc(c, fp); /* Anybody has a better idea ? */ /*{{{ read header*/ pbm_readpbminit(fp, &cols, &rows, &format); if(format==RPBM_FORMAT) readpbmrow=my_readpbmrow; else readpbmrow=pbm_readpbmrow; /*}}} */ prtdrv[printer[i].prtno](i); /* go */ } /*}}} */ printf("\033@"); /* reset printer */ pm_close(fp); exit(0); }
int main(int argc, const char * argv[]) { FILE * ifP; int rows, cols; int format; unsigned int row; unsigned int idx; unsigned int outColByteCt; unsigned int stripeByteCt; unsigned int hres, vres; unsigned char * inBuff; unsigned char * bitrow[256]; unsigned char * compressedData; struct CmdlineInfo cmdline; pm_proginit(&argc, argv); parseCommandLine(argc, argv, &cmdline); ifP = pm_openr(cmdline.inputFileName); pbm_readpbminit(ifP, &cols, &rows, &format); if (cols / 256 > 127) /* Limit in official Epson manual */ pm_error("Image width is too large"); outColByteCt = pbm_packed_bytes(cols); stripeByteCt = cmdline.stripeHeight * outColByteCt; MALLOCARRAY(inBuff, stripeByteCt); if (inBuff == NULL) pm_error("Out of memory trying to create input buffer of %u bytes", stripeByteCt); if (cmdline.compress != 0) pm_rlenc_allocoutbuf(&compressedData, stripeByteCt, PM_RLE_PACKBITS); else compressedData = NULL; for (idx = 0; idx <= cmdline.stripeHeight; ++idx) bitrow[idx]= &inBuff[idx * outColByteCt]; hres = vres = 3600 / cmdline.resolution; /* Possible values for hres, vres: 20, 10, 5 */ if (!cmdline.raw) writeSetup(hres); /* Write out raster stripes */ for (row = 0; row < rows; row += cmdline.stripeHeight ) { unsigned int const rowsThisStripe = MIN(rows - row, cmdline.stripeHeight); unsigned int const outCols = outColByteCt * 8; if (rowsThisStripe > 0) { unsigned int idx; printf("%c%c%c%c%c%c%c%c", esc, '.', cmdline.compress, vres, hres, cmdline.stripeHeight, outCols % 256, outCols / 256); /* Read pbm rows, each padded to full byte */ for (idx = 0; idx < rowsThisStripe; ++idx) { pbm_readpbmrow_packed (ifP, bitrow[idx], cols, format); pbm_cleanrowend_packed(bitrow[idx], cols); } /* If at bottom pad with empty rows up to stripe height */ if (rowsThisStripe < cmdline.stripeHeight ) memset(bitrow[rowsThisStripe], 0, (cmdline.stripeHeight - rowsThisStripe) * outColByteCt); /* Write raster data */ if (cmdline.compress != 0) { /* compressed */ size_t compressedDataCt; pm_rlenc_compressbyte(inBuff, compressedData, PM_RLE_PACKBITS, stripeByteCt, &compressedDataCt); fwrite(compressedData, compressedDataCt, 1, stdout); } else /* uncompressed */ fwrite(inBuff, stripeByteCt, 1, stdout); /* Emit newline to print the stripe */ putchar('\n'); } } free(inBuff); free(compressedData); pm_close(ifP); /* Form feed */ if (cmdline.formfeed) putchar('\f'); if (!cmdline.raw) { /* Reset printer. a*/ printf("%c%c", esc, '@'); } return 0; }