📄 foo2xqx.c
字号:
}voidend_page(FILE *ofp){ chunk_write(XQX_END_PAGE, 0, ofp);}intwrite_page(BIE_CHAIN **root, BIE_CHAIN **root2, BIE_CHAIN **root3, BIE_CHAIN **root4, FILE *ofp){ int nbie = root2 ? 4 : 1; start_page(root, nbie, ofp); if (root3) write_plane(3, root3, ofp); if (root2) write_plane(2, root2, ofp); if (root) { if (OutputStartPlane) write_plane(nbie == 1 ? 4 : 1, root, ofp); else write_plane(nbie == 1 ? 0 : 1, root, ofp); } if (root4) write_plane(4, root4, ofp); end_page(ofp); return 0;}/* * This creates a linked list of compressed data. The first item * in the list is the BIH and is always 20 bytes in size. Each following * item is 65536 bytes in length. The last item length is whatever remains. */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. 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; } }}voidstart_doc(FILE *fp){ char header[4] = ",XQX"; // Big-endian data int nitems; time_t now; struct tm *tmp; char datetime[14+1]; now = time(NULL); tmp = localtime(&now); strftime(datetime, sizeof(datetime), "%Y%m%d%H%M%S", tmp); fprintf(fp, "\033%%-12345X@PJL JOB\n"); fprintf(fp, "@PJL SET JAMRECOVERY=OFF\n"); fprintf(fp, "@PJL SET DENSITY=3\n"); fprintf(fp, "@PJL SET ECONOMODE=OFF\n"); fprintf(fp, "@PJL SET RET=MEDIUM\n"); fprintf(fp, "@PJL INFO STATUS\n"); fprintf(fp, "@PJL USTATUS DEVICE = ON\n"); fprintf(fp, "@PJL USTATUS JOB = ON\n"); fprintf(fp, "@PJL USTATUS PAGE = ON\n"); fprintf(fp, "@PJL USTATUS TIMED = 30\n"); fprintf(fp, "@PJL SET JOBATTR=\"JobAttr4=%s\"", datetime); fputc(0, fp); fprintf(fp, "\033%%-12345X"); fwrite(header, 1, sizeof(header), fp); nitems = 7; chunk_write(XQX_START_DOC, nitems, fp); item_uint32_write(0x80000000, 84, fp); item_uint32_write(0x10000005, 1, fp); item_uint32_write(0x10000001, 0, fp); item_uint32_write(XQXI_DMDUPLEX, (Duplex != DMDUPLEX_OFF) ? 2 : 0, fp); item_uint32_write(0x10000000, 0, fp); item_uint32_write(0x10000003, 1, fp); item_uint32_write(XQXI_END, 0xdeadbeef, fp);}voidend_doc(FILE *fp){ int nitems; nitems = 0; chunk_write(XQX_END_DOC , nitems, fp); fprintf(fp, "\033%%-12345X@PJL EOJ\n"); fprintf(fp, "\033%%-12345X");}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; 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){ BIE_CHAIN *chain[4]; int i; int bpl = (w + 7) / 8; unsigned char *plane[4], *bitmaps[4][1]; struct jbg_enc_state se[4]; RealWidth = w; for (i = 0; i < 4; ++i) { plane[i] = malloc(bpl * h); if (!plane[i]) error(3, "Cannot allocate space for bit plane\n"); chain[i] = NULL; } 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]; jbg_enc_init(&se[i], w, h, 1, bitmaps[i], output_jbig, &chain[i]); jbg_enc_options(&se[i], JbgOptions[0], JbgOptions[1], JbgOptions[2], JbgOptions[3], JbgOptions[4]); jbg_enc_out(&se[i]); jbg_enc_free(&se[i]); } if (Color2Mono) write_page(&chain[Color2Mono-1], NULL, NULL, NULL, ofp); else if (AnyColor) write_page(&chain[0], &chain[1], &chain[2], &chain[3], ofp); else write_page(&chain[3], NULL, NULL, NULL, ofp); for (i = 0; i < 4; ++i) free(plane[i]); return 0;}intpksm_page(unsigned char *plane[4], int w, int h, FILE *ofp){ BIE_CHAIN *chain[4]; int i; unsigned char *bitmaps[4][1]; struct jbg_enc_state se[4]; RealWidth = w; for (i = 0; i < 4; ++i) chain[i] = NULL; for (i = 0; i < 4; ++i) { *bitmaps[i] = plane[i]; jbg_enc_init(&se[i], w, h, 1, bitmaps[i], output_jbig, &chain[i]); jbg_enc_options(&se[i], JbgOptions[0], JbgOptions[1], JbgOptions[2], JbgOptions[3], JbgOptions[4]); jbg_enc_out(&se[i]); jbg_enc_free(&se[i]); } if (Color2Mono) write_page(&chain[Color2Mono-1], NULL, NULL, NULL, ofp); else if (AnyColor) write_page(&chain[0], &chain[1], &chain[2], &chain[3], ofp); else write_page(&chain[3], NULL, NULL, NULL, ofp); return 0;}intpbm_page(unsigned char *buf, int w, int h, FILE *ofp){ BIE_CHAIN *chain = NULL; unsigned char *bitmaps[1]; struct jbg_enc_state se; RealWidth = w; w = (w + 127) & ~127; if (SaveToner) { 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 = buf; jbg_enc_init(&se, w, h, 1, bitmaps, 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_page(&chain, NULL, NULL, NULL, 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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -