foo2qpdl.c

来自「Linux下的无线网卡通用驱动程序」· C语言 代码 · 共 1,510 行 · 第 1/3 页

C
1,510
字号
	cksum += be32_write(ofp, 0);	cksum += be32_write(ofp, 0);	cksum += be32_write(ofp, 0);	cksum += be32_write(ofp, 0);	cksum += be32_write(ofp, 0);	cksum += be32_write(ofp, 0);	cksum += write_cksum(current->data, 20, ofp);	be32_write(ofp, cksum);	if (++pn == 5) pn = 1;    }        ++pageno;    if (IsCUPS)	fprintf(stderr, "PAGE: %d %d\n", pageno, Copies);}voidend_page(FILE *ofp){    /* RECTYPE: 0x1 */    fprintf(ofp, "%c", 1);    fprintf(ofp, "%c%c", Copies>>8, Copies); //cksum??}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 (root)    {	if (OutputStartPlane)	    write_plane(nbie == 1 ? 4 : 1, root, ofp);	else	    write_plane(nbie == 1 ? 0 : 1, root, ofp);    }    if (root2)	write_plane(2, root2, ofp);    if (root3)	write_plane(3, root3, 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 = 0x80000;	// 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 *ofp){    time_t	now;    struct tm	*tmp;    char	datetime[14+1];    char	*strmedia[] =		{		    "NORMAL", "THICK", "THIN", "BOND", "COLOR",		    "CARD", "LABEL", "ENV", "USED", "COTTON",		    "RECYCLED", "OHP", "ARCHIVE",		};    #define STRARY(X, A) \            ((X) >= 0 && (X) < sizeof(A)/sizeof(A[0])) \            ? A[X] : "NORMAL"    now = time(NULL);    tmp = localtime(&now);    strftime(datetime, sizeof(datetime), "%Y%m%d", tmp);    fprintf(ofp, "\033%%-12345X@PJL DEFAULT SERVICEDATE=%s\r\n", datetime);    fprintf(ofp, "@PJL SET USERNAME=\"%s\"\r\n",		    Username ? Username : "Unknown");    fprintf(ofp, "@PJL SET JOBNAME=\"%s\"\r\n",		    Filename ? Filename : "<stdin>");    fprintf(ofp, "@PJL SET COLORMODE=%s\r\n",		    Mode == MODE_MONO ? "MONO" : "COLOR");    switch (Duplex)    {    case DMDUPLEX_LONGEDGE:    case DMDUPLEX_MANUALLONG:	fprintf(ofp, "@PJL SET DUPLEX=MANUAL\r\n");	fprintf(ofp, "@PJL SET BINDING=LONGEDGE\r\n");	break;    case DMDUPLEX_SHORTEDGE:    case DMDUPLEX_MANUALSHORT:	fprintf(ofp, "@PJL SET DUPLEX=MANUAL\r\n");	fprintf(ofp, "@PJL SET BINDING=SHORTEDGE\r\n");	break;    }    fprintf(ofp, "@PJL SET PAPERTYPE = %s\r\n", STRARY(MediaCode, strmedia));    fprintf(ofp, "@PJL ENTER LANGUAGE = QPDL\r\n");}voidend_doc(FILE *ofp){    fprintf(ofp, "%c", 9);    fprintf(ofp, "\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;    bpl = (bpl + 15) & ~15;    debug(1, "w=%d, bpl=%d, rawbpl=%d\n", w, bpl, rawbpl);    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, bpl16;    unsigned char *plane[4], *bitmaps[4][1];    struct jbg_enc_state se[4];     RealWidth = w;    w = (w + 127) & ~127;    bpl = (w + 7) / 8;    bpl16 = (bpl + 15) & ~15;    debug(1, "w = %d, bpl = %d, bpl16 = %d\n", w, bpl, bpl16);    for (i = 0; i < 4; ++i)    {	plane[i] = malloc(bpl16 * h);	if (!plane[i]) error(3, "Cannot allocate space for bit plane\n");	chain[i] = NULL;    }    cmyk_planes(plane, raw, RealWidth, 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],			h, 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;    w = (w + 127) & ~127;    debug(1, "w = %d\n", 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],			h, 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],			h, 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)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?