📄 foo2hp.c
字号:
*/voidoutput_jbig(unsigned char *start, size_t len, void *cbarg){ BIE_CHAIN *current, **root = (BIE_CHAIN **) cbarg; int size = 65536; // Printer does strange things otherwise. int i, state; unsigned char ch; // Delete everything after ff 02 state = 0; for (i = 0; i < len; ++i) { ch = start[i]; switch (state) { case 0: if (ch == 0xff) state = 0xff; break; case 0xff: if (ch == 0x02) { len = i + 1; goto out; } if (state == 0xff) state = 0; break; } }out: if ( (*root) == NULL) { (*root) = malloc(sizeof(BIE_CHAIN)); if (!(*root)) error(1, "Can't allocate space for chain\n"); (*root)->data = NULL; (*root)->next = NULL; (*root)->len = 0; size = 20; if (len != 20) error(1, "First chunk must be BIH and 20 bytes long\n"); } current = *root; while (current->next) current = current->next; while (len > 0) { int amt, left; if (!current->data) { current->data = malloc(size); if (!current->data) error(1, "Can't allocate space for compressed data\n"); } left = size - current->len; amt = (len > left) ? left : len; memcpy(current->data + current->len, start, amt); current->len += amt; len -= amt; start += amt; if (current->len == size) { current->next = malloc(sizeof(BIE_CHAIN)); if (!current->next) error(1, "Can't allocate space for chain\n"); current = current->next; current->data = NULL; current->next = NULL; current->len = 0; } }}intwrite_bitmap_page(int w, int h, int np, unsigned char *bitmaps[4], FILE *ofp){ int x, y; int p; int size; int i; DWORD bih[5]; int w16; start_bitmap_page(w, h, np, ofp); if (Bpp == 2) w16 = (w + 63) & ~63; else w16 = (w + 127) & ~127; debug(2, "w16 = %d\n", w16); for (p = 0; p < np; ++p) { int nitems; nitems = 7; size = nitems * sizeof(ZJ_ITEM_UINT32); ++nitems; bih[0] = 1 << 8; bih[1] = w16*Bpp; bih[2] = 100; bih[3] = 128; bih[4] = (16 << 24) | ((JBG_ILEAVE|JBG_SMID) << 8) | ((JBG_LRLTWO | JBG_TPDON | JBG_TPBON | JBG_DPON) << 0); for (i = 0; i < 5; ++i) bih[i] = be32(bih[i]); size += item_bytelut_write(0, 20, (unsigned char *) bih, NULL); chunk_write_rsvd(ZJT_2600N, 0x74, nitems, size, ofp); item_uint32_write(0x65, 1, ofp); item_uint32_write(0x68, w16*Bpp, ofp); item_uint32_write(0x6b, w16*Bpp, ofp); item_uint32_write(ZJI_INCRY, 100, ofp); item_uint32_write(0x6a, 1, ofp); item_uint32_write(ZJI_VIDEO_BPP, Bpp, ofp); item_uint32_write(ZJI_PLANE, (np==1) ? 4 : p+1, ofp); item_bytelut_write(ZJI_JBIG_BIH, 20, (unsigned char *) bih, ofp); if (Bpp == 2) { for (y = 0; y < h; ++y) for (x = 0; x < w16*Bpp/8; ++x) bitmaps[p][y*w16*Bpp/8 + x] = Mirror24[ bitmaps[p][y*w16*Bpp/8 + x] ]; } } for (y = 0; y < h; y += 100) { for (p = 0; p < np; ++p) { struct jbg_enc_state se; BIE_CHAIN *chain = NULL; int len, eof; len = h - y; if (len > 100) len = 100; eof = (y+100) >= h; jbg_enc_init(&se, w16*Bpp, len, 1, &bitmaps[p], output_jbig, &chain); jbg_enc_options(&se, JbgOptions[0], JbgOptions[1], JbgOptions[2], JbgOptions[3], JbgOptions[4]); jbg_enc_out(&se); jbg_enc_free(&se); write_bitmap_plane((np==1) ? 4 : p+1, eof, len, &chain, ofp); bitmaps[p] += (100*w16*Bpp + 7) / 8; } } end_page(np, ofp); return 0;}voidstart_doc(FILE *fp){ char header[4] = "JZJZ"; // Big-endian data int nitems; int size; fwrite(header, 1, sizeof(header), fp); nitems = 3; size = nitems * sizeof(ZJ_ITEM_UINT32); if (Username) { ++nitems; size += item_str_write(0, Username, NULL); } if (Filename) { ++nitems; size += item_str_write(0, Filename, NULL); } chunk_write_rsvd(ZJT_START_DOC, 0x24, nitems, size, fp); item_uint32_write(ZJI_DMCOLLATE, 0, fp); item_uint32_write(ZJI_DMDUPLEX, Duplex, fp); item_uint32_write(ZJI_PAGECOUNT, 0, fp); if (Username) item_str_write(ZJI_MINOLTA_USERNAME, Username, fp); if (Filename) item_str_write(ZJI_MINOLTA_FILENAME, Filename, fp);}voidend_doc(FILE *fp){ int nitems; nitems = 0; chunk_write(ZJT_END_DOC , nitems, nitems * sizeof(ZJ_ITEM_UINT32), fp);}voidload_tray2(FILE *fp){ int nitems; nitems = 0; chunk_write(ZJT_2600N_PAUSE, nitems, nitems * sizeof(ZJ_ITEM_UINT32), fp);}static int AnyColor;voidcmyk_planes(unsigned char *plane[4], unsigned char *raw, int w, int h){ int rawbpl = (w+1) / 2; int bpl = (w + 7) / 8; int i; int x, y; unsigned char byte; unsigned char mask[8] = { 128, 64, 32, 16, 8, 4, 2, 1 }; int aib = AllIsBlack; int bc = BlackClears; bpl = (bpl + 15) & ~15; AnyColor = 0; for (i = 0; i < 4; ++i) memset(plane[i], 0, bpl * h); // // Unpack the combined plane into individual color planes // // TODO: this can be speeded up using a 256 or 65536 entry lookup table // for (y = 0; y < h; ++y) { for (x = 0; x < w; ++x) { byte = raw[y*rawbpl + x/2]; if (aib && (byte & 0xE0) == 0xE0) { plane[3][y*bpl + x/8] |= mask[x&7]; } else if (byte & 0x10) { plane[3][y*bpl + x/8] |= mask[x&7]; if (!bc) { if (byte & 0x80) plane[0][y*bpl + x/8] |= mask[x&7]; if (byte & 0x40) plane[1][y*bpl + x/8] |= mask[x&7]; if (byte & 0x20) plane[2][y*bpl + x/8] |= mask[x&7]; if (byte & 0xE0) AnyColor |= byte; } } else { if (byte & 0x80) plane[0][y*bpl + x/8] |= mask[x&7]; if (byte & 0x40) plane[1][y*bpl + x/8] |= mask[x&7]; if (byte & 0x20) plane[2][y*bpl + x/8] |= mask[x&7]; if (byte & 0xE0) AnyColor |= byte; } ++x; if (aib && (byte & 0x0E) == 0x0E) { plane[3][y*bpl + x/8] |= mask[x&7]; } else if (byte & 0x1) { plane[3][y*bpl + x/8] |= mask[x&7]; if (!bc) { if (byte & 0x8) plane[0][y*bpl + x/8] |= mask[x&7]; if (byte & 0x4) plane[1][y*bpl + x/8] |= mask[x&7]; if (byte & 0x2) plane[2][y*bpl + x/8] |= mask[x&7]; if (byte & 0xE) AnyColor |= byte; } } else { if (byte & 0x8) plane[0][y*bpl + x/8] |= mask[x&7]; if (byte & 0x4) plane[1][y*bpl + x/8] |= mask[x&7]; if (byte & 0x2) plane[2][y*bpl + x/8] |= mask[x&7]; if (byte & 0xE) AnyColor |= byte; } } } debug(2, "BlackClears = %d; AnyColor = %s %s %s\n", BlackClears, (AnyColor & 0x88) ? "Cyan" : "", (AnyColor & 0x44) ? "Magenta" : "", (AnyColor & 0x22) ? "Yellow" : "" );}intcmyk_page(unsigned char *raw, int w, int h, FILE *ofp){ int i; int bpl = (w + 7) / 8; int bpl16 = (bpl + 15) & ~15; unsigned char *plane[4], *bitmaps[4]; for (i = 0; i < 4; ++i) { plane[i] = malloc(bpl16 * h); if (!plane[i]) error(3, "Cannot allocate space for bit plane\n"); debug(1, "malloc plane[%d] = %x\n", i, plane[i]); } cmyk_planes(plane, raw, w, h); for (i = 0; i < 4; ++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); } } bitmaps[i] = plane[i]; } if (Color2Mono) write_bitmap_page(w, h, 1, &bitmaps[Color2Mono-1], ofp); else if (AnyColor) write_bitmap_page(w, h, 4, bitmaps, ofp); else write_bitmap_page(w, h, 1, &bitmaps[3], ofp); for (i = 0; i < 4; ++i) { debug(1, "free plane[%d] = %x\n", i, plane[i]); free(plane[i]); } return 0;}intpksm_page(unsigned char *plane[4], int w, int h, FILE *ofp){ int i; unsigned char *bitmaps[4]; for (i = 0; i < 4; ++i) bitmaps[i] = plane[i]; if (Color2Mono) write_bitmap_page(w, h, 1, &bitmaps[Color2Mono-1], ofp); else if (AnyColor) write_bitmap_page(w, h, 4, bitmaps, ofp); else write_bitmap_page(w, h, 1, &bitmaps[3], ofp); return 0;}intpbm_page(unsigned char *buf, int w, int h, FILE *ofp){ unsigned char *bitmaps[1]; if (SaveToner && Bpp == 1) { int x, y; int bpl, bpl16; bpl = (w + 7) / 8; bpl16 = (bpl + 15) & ~15; for (y = 0; y < h; y += 2) for (x = 0; x < bpl16; ++x) buf[y*bpl16 + x] &= 0x55; for (y = 1; y < h; y += 2) for (x = 0; x < bpl16; ++x) buf[y*bpl16 + x] &= 0xaa; } bitmaps[0] = buf; write_bitmap_page(w, h, 1, bitmaps, ofp); return 0;}intread_and_clip_image(unsigned char *buf, int rawBpl, int rightBpl, int pixelsPerByte, int bpl, int h, int bpl16, FILE *ifp){ unsigned char *rowbuf, *rowp; int y; int rc; rowbuf = malloc(rawBpl); if (!rowbuf) error(1, "Can't allocate row buffer\n"); // Clip top rows if (UpperLeftY) { for (y = 0; y < UpperLeftY; ++y) { rc = fread(rowbuf, rawBpl, 1, ifp); if (rc == 0) goto eof; if (rc != 1) error(1, "Premature EOF(1) on input at y=%d\n", y); } } // Copy the rows that we want to image rowp = buf; for (y = 0; y < h; ++y, rowp += bpl16) { // Clip left pixel *bytes* if (UpperLeftX) { rc = fread(rowbuf, UpperLeftX / pixelsPerByte, 1, ifp); if (rc == 0 && y == 0 && !UpperLeftY) goto eof; if (rc != 1) error(1, "Premature EOF(2) on input at y=%d\n", y); } if (bpl != bpl16) memset(rowp, 0, bpl16); rc = fread(rowp, bpl, 1, ifp); 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, bpl, ifp); if (rc == EOF) goto done; ++PageNum; if (Duplex == DMDUPLEX_LONGEDGE && (PageNum & 1) == 0) rotate_bytes_180(buf, buf + bpl * h - 1, Mirror4); if (Duplex == DMDUPLEX_MANUALLONG && (PageNum & 1) == 0) rotate_bytes_180(buf, buf + bpl * h - 1, Mirror4); if ((PageNum & 1) == 0 && EvenPages) { SeekRec[SeekIndex].b = ftell(EvenPages); cmyk_page(buf, w, h, EvenPages); SeekRec[SeekIndex].e = ftell(EvenPages); debug(1, "CMYK Page: %d %ld %ld\n", PageNum, SeekRec[SeekIndex].b, SeekRec[SeekIndex].e); SeekIndex++; } else cmyk_page(buf, w, h, ofp); }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"); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -