int main(int argc, char * argv[]) { struct cmdlineInfo cmdline; FILE * ifP; bool eof; pbm_init(&argc, argv); parseCommandLine(argc, argv, &cmdline); ifP = pm_openr(cmdline.inputFilename); eof = FALSE; while (!eof) { doPage(ifP, cmdline); pbm_nextimage(ifP, &eof); } pm_close(ifP); 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); } }