📄 foo2xqx.c
字号:
}done: free(buf); return 0;}static unsigned longgetint(FILE *fp){ int c; unsigned long i; while ((c = getc(fp)) != EOF && !isdigit(c)) if (c == '#') while ((c = getc(fp)) != EOF && !(c == 13 || c == 10)) ; if (c != EOF) { ungetc(c, fp); fscanf(fp, "%lu", &i); } return i;}static voidskip_to_nl(FILE *fp){ for (;;) { int c; c = getc(fp); if (c == EOF) error(1, "Premature EOF on input stream\n"); if (c == '\n') return; }}intpbm_header(FILE *ifp, int *p4eatenp, int *wp, int *hp){ int c1, c2; if (*p4eatenp) *p4eatenp = 0; // P4 already eaten in main else { c1 = getc(ifp); if (c1 == EOF) return 0; c2 = getc(ifp); if (c1 != 'P' || c2 != '4') error(1, "Not a pbmraw data stream\n"); } 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 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; plane[i] = malloc(bpl * h); if (!plane[i]) error(1, "Can't allocate plane buffer\n"); rc = read_and_clip_image(plane[i], rawBpl, rightBpl, 8, bpl, h, bpl, 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 + bpl*h; p < e; ++p) if (*p) { AnyColor |= 1<<i; break; } } if (Duplex == DMDUPLEX_LONGEDGE && (PageNum & 1) == 0) rotate_bytes_180(plane[i], plane[i] + bpl * h - 1, Mirror1); if (Duplex == DMDUPLEX_MANUALLONG && (PageNum & 1) == 0) rotate_bytes_180(plane[i], plane[i] + bpl * 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 = 0; 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; debug(1, "bpl=%d bpl16=%d\n", bpl, bpl16); 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);}voidblank_page(FILE *ofp){ int w, h, bpl, bpl16 = 0; 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; if (Mode == MODE_COLOR) { mode = getc(in); 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 { mode = getc(in); 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, "cd:g:n:m:p:r:s:tu:l:L:ABPJ:S:U:X:D:V?h")) != EOF) switch (c) { 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; Bpp = ResX / 600; ResX = 600; if (SaveToner) { SaveToner = 0; EconoMode = 1; } switch (Duplex) { case DMDUPLEX_LONGEDGE: case DMDUPLEX_SHORTEDGE: case DMDUPLEX_MANUALLONG: case DMDUPLEX_MANUALSHORT: EvenPages = tmpfile(); break; } start_doc(stdout); 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) { DWORD pause; // 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 */ // Write even pages in reverse order for (i = SeekIndex-1; i >= 0; --i) { debug(1, "EvenPage: %d %ld %ld %ld\n", i, SeekRec[i].b, SeekRec[i].e, SeekRec[i].pause); fseek(EvenPages, SeekRec[i].pause, 0L); if (i == SeekIndex-1) pause = be32(2); else pause = be32(3); fwrite(&pause, 1, sizeof(DWORD), EvenPages); 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 + -