📄 foo2hp.c
字号:
skip_to_nl(ifp); *wp = getint(ifp); *hp = getint(ifp); skip_to_nl(ifp); return 1;}intpksm_pages(FILE *ifp, FILE *ofp){ unsigned char *plane[4]; int rawW, rawH, rawBpl; int saveW = 0, saveH = 0; int rightBpl; int w, h, bpl; int bpl16; int i; int rc; int p4eaten = 1; // // Save the original Upper Right clip values as the logical offset, // because we may adjust them slightly below, in the interest of speed. // if (LogicalClip & LOGICAL_CLIP_X) LogicalOffsetX = UpperLeftX; if (LogicalClip & LOGICAL_CLIP_Y) LogicalOffsetY = UpperLeftY; for (;;) { ++PageNum; AnyColor = 0; for (i = 0; i < 4; ++i) { if (pbm_header(ifp, &p4eaten, &rawW, &rawH) == 0) { if (i == 0) goto eof; else error(1, "Premature EOF(pksm) on page %d hdr, plane %d\n", PageNum, i); } if (i == 0) { saveW = rawW; saveH = rawH; } if (saveW != rawW) error(1, "Image width changed from %d to %d on plane %d\n", saveW, rawW, i); if (saveH != rawH) error(1, "Image height changed from %d to %d on plane %d\n", saveH, rawH, i); rawBpl = (rawW + 7) / 8; // We only clip multiples of 8 pixels off the leading edge, and // add any remainder to the amount we clip from the right edge. // Its fast, and good enough for government work. LowerRightX += UpperLeftX & 7; UpperLeftX &= ~7; w = rawW - UpperLeftX - LowerRightX; h = rawH - UpperLeftY - LowerRightY; bpl = (w + 7) / 8; rightBpl = (rawW - UpperLeftX + 7) / 8; bpl16 = (bpl + 15) & ~15; plane[i] = malloc(bpl16 * h); if (!plane[i]) error(1, "Can't allocate plane buffer\n"); rc = read_and_clip_image(plane[i], rawBpl, rightBpl, 8, bpl, h, bpl16, ifp); if (rc == EOF) error(1, "Premature EOF(pksm) on page %d data, plane %d\n", PageNum, i); if (Debug >= 9) { FILE *dfp; char fname[256]; sprintf(fname, "xxxplane%d", i); dfp = fopen(fname, "w"); if (dfp) { fwrite(plane[i], bpl*h, 1, dfp); fclose(dfp); } } // See if we can optimize this to be a monochrome page if (!AnyColor && i != 3) { unsigned char *p, *e; for (p = plane[i], e = p + bpl16*h; p < e; ++p) if (*p) { AnyColor |= 1<<i; break; } } if (Duplex == DMDUPLEX_LONGEDGE && (PageNum & 1) == 0) rotate_bytes_180(plane[i], plane[i] + bpl16 * h - 1, Mirror1); if (Duplex == DMDUPLEX_MANUALLONG && (PageNum & 1) == 0) rotate_bytes_180(plane[i], plane[i] + bpl16 * h - 1, Mirror1); } debug(2, "AnyColor = %s %s %s\n", (AnyColor & 0x01) ? "Cyan" : "", (AnyColor & 0x02) ? "Magenta" : "", (AnyColor & 0x04) ? "Yellow" : "" ); if ((PageNum & 1) == 0 && EvenPages) { SeekRec[SeekIndex].b = ftell(EvenPages); pksm_page(plane, w, h, EvenPages); SeekRec[SeekIndex].e = ftell(EvenPages); debug(1, "PKSM Page: %d %ld %ld\n", PageNum, SeekRec[SeekIndex].b, SeekRec[SeekIndex].e); SeekIndex++; } else pksm_page(plane, w, h, ofp); for (i = 0; i < 4; ++i) free(plane[i]); }eof: return (0);}intpbm_pages(FILE *ifp, FILE *ofp){ unsigned char *buf; int rawW, rawH, rawBpl; int rightBpl; int w, h, bpl; int bpl16; int rc; int p4eaten = 1; // // Save the original Upper Right clip values as the logical offset, // because we may adjust them slightly below, in the interest of speed. // if (LogicalClip & LOGICAL_CLIP_X) LogicalOffsetX = UpperLeftX; if (LogicalClip & LOGICAL_CLIP_Y) LogicalOffsetY = UpperLeftY; for (;;) { if (pbm_header(ifp, &p4eaten, &rawW, &rawH) == 0) break; rawBpl = (rawW + 7) / 8; // We only clip multiples of 8 pixels off the leading edge, and // add any remainder to the amount we clip from the right edge. // Its fast, and good enough for government work. LowerRightX += UpperLeftX & 7; UpperLeftX &= ~7; w = rawW - UpperLeftX - LowerRightX; h = rawH - UpperLeftY - LowerRightY; bpl = (w + 7) / 8; rightBpl = (rawW - UpperLeftX + 7) / 8; bpl16 = (bpl + 15) & ~15; buf = malloc(bpl16 * h); if (!buf) error(1, "Can't allocate page buffer\n"); rc = read_and_clip_image(buf, rawBpl, rightBpl, 8, bpl, h, bpl16, ifp); if (rc == EOF) error(1, "Premature EOF(pbm) on input stream\n"); ++PageNum; if (Duplex == DMDUPLEX_LONGEDGE && (PageNum & 1) == 0) rotate_bytes_180(buf, buf + bpl16 * h - 1, Mirror1); if ((PageNum & 1) == 0 && EvenPages) { if (Duplex == DMDUPLEX_MANUALLONG) rotate_bytes_180(buf, buf + bpl16 * h - 1, Mirror1); SeekRec[SeekIndex].b = ftell(EvenPages); pbm_page(buf, w, h, EvenPages); SeekRec[SeekIndex].e = ftell(EvenPages); debug(1, "PBM Page: %d %ld %ld\n", PageNum, SeekRec[SeekIndex].b, SeekRec[SeekIndex].e); SeekIndex++; } else pbm_page(buf, w, h, ofp); free(buf); } return (0);}intcups_pages(FILE *ifp, FILE *ofp){ unsigned char *plane[4]; int p, np = 1; int bpc; int bpp; int co, cs; int rawW, rawH, rawBpl; int rightBpl; int w, h, bpl, bpl16; int rc; int version = 0, swapped = 0, compress = 0; int seek; unsigned magic; cups_page_header_t c1; cups_page_header2_t c2; if (fread(&magic, sizeof(magic), 1, ifp) != 1) error(1, "Premature EOF reading magic number\n"); switch (magic) { case CUPS_RASTER_SYNCv1: version = 1; swapped = 0; compress = 0; break; case CUPS_RASTER_REVSYNCv1: version = 1; swapped = 1; compress = 0; break; case CUPS_RASTER_SYNCv2: version = 2; swapped = 0; compress = 1; break; case CUPS_RASTER_REVSYNCv2: version = 2; swapped = 1; compress = 1; break; case CUPS_RASTER_SYNC: version = 3; swapped = 0; compress = 0; break; case CUPS_RASTER_REVSYNC: version = 3; swapped = 1; compress = 0; break; default: error(1, "Illegal cups magic number '%4.4s'\n", (char *) &magic); break; } debug(2, "CUPS magic number '%4.4s', version=%d, swapped=%d, compress=%d\n", (char *) &magic, version, swapped, compress); if (swapped) error(1, "Don't know how to swap '%4.4s'\n", (char *) &magic); if (compress) error(1, "Don't know how to decompress '%4.4s'\n", (char *) &magic); // // Save the original Upper Right clip values as the logical offset, // because we may adjust them slightly below, in the interest of speed. // if (LogicalClip & LOGICAL_CLIP_X) LogicalOffsetX = UpperLeftX; if (LogicalClip & LOGICAL_CLIP_Y) LogicalOffsetY = UpperLeftY; // We only clip multiples of 4 pixels off the leading edge, and // add any remainder to the amount we clip from the right edge. // Its fast, and good enough for government work. LowerRightX += UpperLeftX & 3; UpperLeftX &= ~3; AnyColor = 1; for (;;) { ++PageNum; seek = sizeof(magic); if (fread(&c1, sizeof(c1), 1, ifp) != 1) break; seek += sizeof(c1); if (version != 1) { if (fread(&c2, sizeof(c2), 1, ifp) != 1) error(1, "Premature EOF skipping CUPS header V2\n"); seek += sizeof(c2); } rawW = c1.cupsWidth; rawH = c1.cupsHeight; // c1.cupsMediaType bpc = c1.cupsBitsPerColor; if (bpc != 2) error(1, "Illegal number of bits per color (%d)\n", bpc); bpp = c1.cupsBitsPerPixel; rawBpl = c1.cupsBytesPerLine; co = c1.cupsColorOrder; if (co != CUPS_ORDER_PLANAR) error(1, "Illegal color order (%d)\n", co); cs = c1.cupsColorSpace; switch (cs) { case CUPS_CSPACE_K: np = 1; break; case CUPS_CSPACE_CMYK: np = 4; break; default: error(1, "Illegal color space (%d)\n", cs); break; } debug(2,"data at %ld\n", seek); debug(1, "%s: %d x %d x %d, %d (%d)\n", cs==3 ? "mono" : "color", rawW, rawH, bpp, rawBpl, rawBpl*8); w = rawW - UpperLeftX - LowerRightX; h = rawH - UpperLeftY - LowerRightY; bpl = (w + 3) / 4; debug(2, "w = %d, bpl = %d\n", w, bpl); rightBpl = (rawW - UpperLeftX + 3) / 4; bpl16 = (bpl + 15) & ~15; debug(2, "rawBpl = %d, bpl16 = %d\n", rawBpl, bpl16); for (p = 0; p < np; ++p) { plane[p] = malloc(bpl16 * h); if (!plane[p]) error(1, "Unable to allocate plane[%d] of %d x %d = %d bytes\n", p, rawW, rawH, rawBpl * rawH); } for (p = 0; p < np; ++p) { rc = read_and_clip_image(plane[p], rawBpl, rightBpl, 4, bpl, h, bpl16, ifp); if (rc == EOF) error(1, "Premature EOF on plane[%d]\n", p); if (Duplex == DMDUPLEX_LONGEDGE && (PageNum & 1) == 0) rotate_bytes_180(plane[p], plane[p] + bpl16 * h - 1, Mirror1); if (Duplex == DMDUPLEX_MANUALLONG && (PageNum & 1) == 0) rotate_bytes_180(plane[p], plane[p] + bpl16 * h - 1, Mirror1); } if ((PageNum & 1) == 0 && EvenPages) { SeekRec[SeekIndex].b = ftell(EvenPages); if (np == 1) pbm_page(plane[0], w, h, EvenPages); else pksm_page(plane, w, h, EvenPages); SeekRec[SeekIndex].e = ftell(EvenPages); debug(1, "CUPS Page: %d %ld %ld\n", PageNum, SeekRec[SeekIndex].b, SeekRec[SeekIndex].e); SeekIndex++; } else { if (np == 1) pbm_page(plane[0], w, h, ofp); else pksm_page(plane, w, h, ofp); } for (p = 0; p < np; ++p) free(plane[p]); } return 0;}voidblank_page(FILE *ofp){ int w, h, bpl, bpl16; unsigned char *plane; w = PageWidth - UpperLeftX - LowerRightX; h = PageHeight - UpperLeftY - LowerRightY; bpl = (w + 7) / 8; bpl16 = (bpl + 15) & ~15; plane = malloc(bpl16 * h); if (!plane) error(1, "Unable to allocate blank plane (%d bytes)\n", bpl16*h); memset(plane, 0, bpl16*h); pbm_page(plane, w, h, ofp); ++PageNum; free(plane);}intparse_xy(char *str, int *xp, int *yp){ char *p; if (!str || str[0] == 0) return -1; *xp = strtoul(str, &p, 0); if (str == p) return -2; while (*p && (*p < '0' || *p > '9')) ++p; str = p; if (str[0] == 0) return -3; *yp = strtoul(str, &p, 0); if (str == p) return -4; return (0);}voiddo_one(FILE *in){ int mode; mode = getc(in); if (mode == 't' || (mode >= '2' && mode <='5') ) { ungetc(mode, in); cups_pages(in, stdout); } else if (Mode == MODE_COLOR) { if (mode != 'P') { ungetc(mode, in); cmyk_pages(in, stdout); } else { mode = getc(in); if (mode == '4') pksm_pages(in, stdout); else error(1, "Not a pksmraw file!\n"); } } else { if (mode != 'P') error(1, "Not a pbm file!\n"); mode = getc(in); if (mode == '4') pbm_pages(in, stdout); else error(1, "Not a pbmraw file!\n"); }}intmain(int argc, char *argv[]){ int c; int i, j; while ( (c = getopt(argc, argv, "b:cd:g:n:m:p:r:s:tu:l:L:ABPJ:S:U:X:D:V?h")) != EOF) switch (c) { case 'b': Bpp = atoi(optarg); if (Bpp != 1 && Bpp != 2) error(1, "Illegal value '%s' for -b\n", optarg); break; case 'c': Mode = MODE_COLOR; break; case 'S': Color2Mono = atoi(optarg); Mode = MODE_COLOR; if (Color2Mono < 0 || Color2Mono > 4) error(1, "Illegal value '%s' for -C\n", optarg); break; case 'd': Duplex = atoi(optarg); break; case 'g': if (parse_xy(optarg, &PageWidth, &PageHeight)) error(1, "Illegal format '%s' for -g\n", optarg); if (PageWidth < 0 || PageWidth > 1000000) error(1, "Illegal X value '%s' for -g\n", optarg); if (PageHeight < 0 || PageHeight > 1000000) error(1, "Illegal Y value '%s' for -g\n", optarg); break; case 'm': MediaCode = atoi(optarg); break; case 'n': Copies = atoi(optarg); break; case 'p': PaperCode = atoi(optarg); break; case 'r': if (parse_xy(optarg, &ResX, &ResY)) error(1, "Illegal format '%s' for -r\n", optarg); break; case 's': SourceCode = atoi(optarg); break; case 't': SaveToner = 1; break; case 'u': if (strcmp(optarg, "0") == 0) break; if (parse_xy(optarg, &UpperLeftX, &UpperLeftY)) error(1, "Illegal format '%s' for -u\n", optarg); break; case 'l': if (strcmp(optarg, "0") == 0) break; if (parse_xy(optarg, &LowerRightX, &LowerRightY)) error(1, "Illegal format '%s' for -l\n", optarg); break; case 'L': LogicalClip = atoi(optarg); if (LogicalClip < 0 || LogicalClip > 3) error(1, "Illegal value '%s' for -L\n", optarg); break; case 'A': AllIsBlack = !AllIsBlack; break; case 'B': BlackClears = !BlackClears; break; case 'P': OutputStartPlane = !OutputStartPlane; break; case 'J': if (optarg[0]) Filename = optarg; break; case 'U': if (optarg[0]) Username = optarg; break; case 'X': ExtraPad = atoi(optarg); break; case 'D': Debug = atoi(optarg); break; case 'V': printf("%s\n", Version); exit(0); default: usage(); exit(1); } if (UpperLeftX < 0 || UpperLeftX >= PageWidth) error(1, "Illegal X value '%d' for -u\n", UpperLeftX); if (UpperLeftY < 0 || UpperLeftY >= PageHeight) error(1, "Illegal Y value '%d' for -u\n", UpperLeftY); if (LowerRightX < 0 || LowerRightX >= PageWidth) error(1, "Illegal X value '%d' for -l\n", LowerRightX); if (LowerRightY < 0 || LowerRightY >= PageHeight) error(1, "Illegal Y value '%d' for -l\n", LowerRightY); argc -= optind; argv += optind; if (getenv("DEVICE_URL")) IsCUPS = 1; start_doc(stdout); switch (Duplex) { case DMDUPLEX_LONGEDGE: case DMDUPLEX_SHORTEDGE: case DMDUPLEX_MANUALLONG: case DMDUPLEX_MANUALSHORT: EvenPages = tmpfile(); break; } if (argc == 0) { do_one(stdin); } else { for (i = 0; i < argc; ++i) { FILE *ifp; ifp = fopen(argv[i], "r"); if (!ifp) error(1, "Can't open '%s' for reading\n", argv[i]); do_one(ifp); fclose(ifp); } } /* * Do manual duplex */ if (EvenPages) { // Handle odd page count if ( (PageNum & 1) == 1) { SeekRec[SeekIndex].b = ftell(EvenPages); blank_page(EvenPages); SeekRec[SeekIndex].e = ftell(EvenPages); debug(1, "Blank Page: %d %ld %ld\n", PageNum, SeekRec[SeekIndex].b, SeekRec[SeekIndex].e); SeekIndex++; } // Manual Pause load_tray2(stdout); // Write even pages in reverse order for (i = SeekIndex-1; i >= 0; --i) { debug(1, "EvenPage: %d %ld %ld\n", i, SeekRec[i].b, SeekRec[i].e); fseek(EvenPages, SeekRec[i].b, 0L); for (j = 0; j < (SeekRec[i].e - SeekRec[i].b); ++j) putc(getc(EvenPages), stdout); } fclose(EvenPages); } end_doc(stdout); exit(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -