📄 foo2oak.c
字号:
if (rc == 0 && y == 0 && !UpperLeftY && !UpperLeftX) goto eof; if (rc != 1) error(1, "Premature EOF(3) on input at y=%d\n", y); // Clip right pixels if (rightBpl != bpl) { rc = fread(rowbuf, rightBpl - bpl, 1, ifp); if (rc != 1) error(1, "Premature EOF(4) on input at y=%d\n", y); } } // Clip bottom rows if (LowerRightY) { for (y = 0; y < LowerRightY; ++y) { rc = fread(rowbuf, rawBpl, 1, ifp); if (rc != 1) error(1, "Premature EOF(5) on input at y=%d\n", y); } } free(rowbuf); return (0);eof: free(rowbuf); return (EOF);}intcmyk_pages(FILE *ifp, FILE *ofp){ unsigned char *buf; int rawW, rawH, rawBpl; int rightBpl; int w, h, bpl; int rc; // // 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; rawW = PageWidth; rawH = PageHeight; rawBpl = (PageWidth + 1) / 2; // We only clip multiples of 2 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 & 1; UpperLeftX &= ~1; w = rawW - UpperLeftX - LowerRightX; h = rawH - UpperLeftY - LowerRightY; bpl = (w + 1) / 2; rightBpl = (rawW - UpperLeftX + 1) / 2; buf = malloc(bpl * h); if (!buf) error(1, "Unable to allocate page buffer of %d x %d = %d bytes\n", rawW, rawH, rawBpl * rawH); for (;;) { rc = read_and_clip_image(buf, rawBpl, rightBpl, 2, bpl, h, ifp); if (rc == EOF) goto done; cmyk_page(buf, w, h, ofp); }done: free(buf); return 0;}#if 0 #include <ctype.h>#else static int isdigit(int c) { return (c >= '0' && c <= '9'); }#endifstatic 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;}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_pages(FILE *ifp, FILE *ofp){ unsigned char *buf; int c1, c2; int rawW, rawH, rawBpl; int rightBpl; int w, h, bpl; int rc; int first = 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 (first) first = 0; // P4 already eaten in main else { c1 = getc(ifp); if (c1 == EOF) break; c2 = getc(ifp); if (c1 != 'P' || c2 != '4') error(1, "Not a pbmraw data stream\n"); } skip_to_nl(ifp); rawW = getint(ifp); rawH = getint(ifp); skip_to_nl(ifp); 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; buf = malloc(bpl * h); if (!buf) error(1, "Can't allocate page buffer\n"); rc = read_and_clip_image(buf, rawBpl, rightBpl, 8, bpl, h, ifp); if (rc == EOF) error(1, "Premature EOF(pbm) on input stream\n"); pbm_page(buf, w, h, ofp); free(buf); } return (0);}intpgm_pages(FILE *ifp, FILE *ofp, int first){ unsigned char *buf; int c1, c2; int rawW, rawH, maxVal; int rawBpl, rightBpl; int w, h, bpl; int rc; for (;;) { if (first) first = 0; // P5 already eaten in main else { c1 = getc(ifp); if (c1 == EOF) break; c2 = getc(ifp); if (c1 != 'P' || c2 != '5') error(1, "Not a pgmraw data stream\n"); } skip_to_nl(ifp); rawW = getint(ifp); rawH = getint(ifp); maxVal = getint(ifp); skip_to_nl(ifp); if (maxVal != 255) error(1, "Don't know how to handle pgm maxVal '%d'\n", maxVal); rawBpl = rawW; w = rawW - UpperLeftX - LowerRightX; h = rawH - UpperLeftY - LowerRightY; bpl = w; rightBpl = rawW - UpperLeftX; buf = malloc(bpl * h); if (!buf) error(1, "Can't allocate page buffer\n"); rc = read_and_clip_image(buf, rawBpl, rightBpl, 1, bpl, h, ifp); if (rc == EOF) error(1, "Premature EOF(pgm) on input stream\n"); pgm_page(buf, w, h, ofp); free(buf); } return (0);}intcups_pages(FILE *ifp, FILE *ofp){ unsigned char *buf; int bpc; int rawW, rawH, rawBpl; int rightBpl; int w, h, bpl; int rc; char hdr[512]; // // 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; // 00000000: 74 53 61 52 00 00 00 00 00 00 00 00 00 00 00 00 | tSaR // 00000170: 00 00 00 00 00 00 00 00 ec 13 00 00 c8 19 00 00 // 00000180: 00 00 00 00 02 00 00 00 02 00 00 00 ec 13 00 00 if (fread(hdr, 4, 1, ifp) != 1) error(1, "Preamture EOF reading magic number\n"); if (memcmp(hdr, "tSaR", 4) != 0 && memcmp(hdr, "RaSt", 4) != 0) error(1, "Illegal magic number\n"); if (fread(hdr, 0x178-4, 1, ifp) != 1) error(1, "Preamture EOF skipping start of CUPS header\n"); if (fread(&rawW, 4, 1, ifp) != 1) error(1, "Preamture EOF reading width\n"); if (fread(&rawH, 4, 1, ifp) != 1) error(1, "Preamture EOF reading height\n"); if (fread(&hdr, 4, 1, ifp) != 1) error(1, "Preamture EOF skipping mediaType\n"); if (fread(&bpc, 4, 1, ifp) != 1) error(1, "Preamture EOF reading height\n"); if (bpc != 2) error(1, "Illegal number of bits per color (%d)\n", bpc); if (fread(&hdr, 4, 1, ifp) != 1) error(1, "Preamture EOF skipping bitPerPixel\n"); if (fread(&rawBpl, 4, 1, ifp) != 1) error(1, "Preamture EOF reading height\n"); if (fread(&hdr, 6*4, 1, ifp) != 1) error(1, "Preamture EOF skipping end of CUPS header\n"); debug(1, "%d x %d, %d\n", rawW, rawH, rawBpl); // We only clip multiples of 1 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 & 0; UpperLeftX &= ~0; w = rawW - UpperLeftX - LowerRightX; h = rawH - UpperLeftY - LowerRightY; bpl = (w + 0) / 1; rightBpl = (rawW - UpperLeftX + 0) / 1; buf = malloc(bpl * h); if (!buf) error(1, "Unable to allocate page buffer of %d x %d = %d bytes\n", rawW, rawH, rawBpl * rawH); for (;;) { rc = read_and_clip_image(buf, rawBpl, rightBpl, 1, bpl, h, ifp); if (rc == EOF) goto done; cups_page(buf, w, h, ofp); }done: free(buf); return 0;}voiddo_one(FILE *in){ int mode; mode = getc(in); if (mode == 't') { ungetc(mode, in); cups_pages(in, stdout); } else if (mode != 'P' || Mode == MODE_COLOR) { ungetc(mode, in); cmyk_pages(in, stdout); } else { mode = getc(in); if (mode == '4') pbm_pages(in, stdout); else if (mode == '5') pgm_pages(in, stdout, 1); else error(1, "Not a bitcmyk, cups, pbm, or pgm file!\n"); }}intmain(int argc, char *argv[]){ int c; while ( (c = getopt(argc, argv, "b:cd:g:n:m:p:r:s:u:l:L:ABJ:S:U: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 '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 'J': if (optarg[0]) Filename = optarg; break; case 'U': if (optarg[0]) Username = optarg; break; case 'D': Debug = atoi(optarg); if (Debug == 12345678) { // Hack to force time to zero for regression tests ZeroTime = 1; Debug = 0; } 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); if (argc == 0) { do_one(stdin); } else { int i; 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); } } end_doc(stdout); exit(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -