foo2lava.c

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

C
1,670
字号
	    fprintf(ofp, "\033&f%dF", PageWidth * 720 / ResX);	// Custom X	}	fprintf(ofp, "\033&l%dH", SourceCode);	fprintf(ofp, "\033&l%dM", MediaCode);	fprintf(ofp, "\033&l%dE", 0);	fprintf(ofp, "\033*r%dS", (int) w);	fprintf(ofp, "\033*r%dT", (int) h);	fprintf(ofp, "\033&l%dU", 0);	fprintf(ofp, "\033&l%dZ", 0);	fprintf(ofp, "\033*p%dX", ResX / 6);	fprintf(ofp, "\033*p%dY", ResX / 6);	fprintf(ofp, "\033*r1A");	break;    case MODEL_2480MF:	fprintf(ofp, "MediaSize=%s;", PaperStr);	fprintf(ofp, "MediaType=%s;", MediaStr);	fprintf(ofp, "MediaInputTrayCheck=top;");	fprintf(ofp, "RasterObject.BitsPerPixel=1;");	break;    }    if ((PageNum & 1) == 0 && EvenPages)	SeekMedia = ftell(EvenPages) - 4;    ++pageno;    if (IsCUPS)	fprintf(stderr, "PAGE: %d %d\n", pageno, Copies);}voidend_page(FILE *ofp){    switch (Model)    {    case MODEL_2530DL:	fprintf(ofp, "\033*rC");	if (0)	    fprintf(ofp, "\f");		// Eject page	else	    fprintf(ofp, "\033&l0H");	// Eject page	break;    case MODEL_2480MF:	fprintf(ofp, "Event=EndOfPage;");	break;    }}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);    switch (Model)    {    case MODEL_2530DL:	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);	break;    case MODEL_2480MF:	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);	break;    }    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 (Model == MODEL_2480MF)	size = 32768;    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	buf[64];    time_t	now;    struct tm	*tmp;    switch (Model)    {    case MODEL_2530DL:	now = time(NULL);	tmp = localtime(&now);	strftime(buf, sizeof(buf), "%m/%d/%Y", tmp);	fprintf(fp, "\033%%-12345X@PJL JOB NAME=\"%s\"\n",	    Filename ? Filename : "stdin");	fprintf(fp, "\033%%-12345X@PJL JOB USERNAME=\"%s\"\n",	    Username ? Username : "");	fprintf(fp, "\033%%-12345X@PJL JOB TIMESTAMP=\"%s\"\n", buf);	#ifdef linux	{	    struct utsname u;	    u.release[0] = 0;	    uname(&u);	    fprintf(fp, "\033%%-12345X@PJL JOB OSINFO=\"Linux/%s\"\n",		    u.release);	}	#else	    fprintf(fp, "\033%%-12345X@PJL JOB OSINFO=\"%s\"\n", "Unknown");	#endif	fprintf(fp, "\033%%-12345X@PJL ENTER LANGUAGE=LAVAFLOW\n");	fprintf(fp, "\033E");	fprintf(fp, "\033&l%dS", Duplex-1);	fprintf(fp, "\033&l%dG", 0);	fprintf(fp, "\033&u%dD", ResX);	fprintf(fp, "\033&l%dX", 1);	fprintf(fp, "\033&x%dX", 1);	break;    case MODEL_2480MF:	fprintf(fp, "Event=StartOfJob;");	fprintf(fp, "OSVersion=Linux;");	fprintf(fp, "DrvVersion=2.0.1410.0;");	fprintf(fp, "Resolution=%dx%d;", ResX, ResY);	fprintf(fp, "RasterObject.Compression=JBIG;");	fprintf(fp, "Sides=%sSided;", (Duplex-1) ? "Two" : "One");	break;    }}voidend_doc(FILE *fp){    switch (Model)    {    case MODEL_2530DL:	fprintf(fp, "\033E");	fprintf(fp, "\033%%-12345X");	break;    case MODEL_2480MF:	fprintf(fp, "Event=EndOfJob;");	break;    }}voidload_tray2(FILE *fp){    int			nitems;    nitems = 0;    //chunk_write(ZJT_2600N_PAUSE, nitems, nitems * sizeof(ZJ_ITEM_UINT32), fp);}intcompute_image_dots(int w, int h, unsigned char *bitmap){    int	dots = 0;    int x, y, bpl;    bpl = (w + 7) / 8;    for (y = 0; y < h; ++y)	for (x = 0; x < bpl; ++x)	    dots += BlackOnes[ bitmap[y*bpl + x] ];    return dots;}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);	    }	}	Dots[i] = compute_image_dots(w, h, plane[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);    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)    {	Dots[i] = compute_image_dots(w, h, plane[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;     int	x, y;    int	bpl, bpl16;    RealWidth = w;    //if (Model == MODEL_HP1020)	//w = (w + 127) & ~127;    Dots[3] = 0;    bpl = (w + 7) / 8;    //if (Model == MODEL_2300DL)	bpl16 = bpl;    //else	//bpl16 = (bpl + 15) & ~15;    if (SaveToner)    {	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;    }    Dots[3] = compute_image_dots(w, h, buf);    *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;

⌨️ 快捷键说明

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