/* * "$Id: rastertohp.c 1 2007-04-11 06:10:17Z mike $" * * Hewlett-Packard Page Control Language filter for the Common UNIX * Printing System (CUPS). * * Copyright 1993-2001 by Easy Software Products. * * These coded instructions, statements, and computer programs are the * property of Easy Software Products and are protected by Federal * copyright law. Distribution and use rights are outlined in the file * "LICENSE.txt" which should have been included with this file. If this * file is missing or damaged please contact Easy Software Products * at: * * Attn: CUPS Licensing Information * Easy Software Products * 44141 Airport View Drive, Suite 204 * Hollywood, Maryland 20636-3111 USA * * Voice: (301) 373-9603 * EMail: cups-info@cups.org * WWW: http://www.cups.org * * Contents: * * Setup() - Prepare the printer for printing. * StartPage() - Start a page of graphics. * EndPage() - Finish a page of graphics. * Shutdown() - Shutdown the printer. * CancelJob() - Cancel the current job... * CompressData() - Compress a line of graphics. * OutputLine() - Output a line of graphics. * DitherLine() - Dither a line of pixels. * MakeLut() - Make a lookup table for pixel values. * PackLine() - Pack a line of pixels into bits. * main() - Main entry and processing of driver. */ /* * Include necessary headers... */ #include #include #include #include #include #include #include #include /* * Globals... */ unsigned char *Pixels[4], /* Input buffers */ *Planes[4], /* Output buffers */ *CompBuffer, /* Compression buffer */ *BitBuffer; /* Buffer for output bits */ int *ErrorBuffers[2][4]; /* Error-diffusion buffers */ int PixelLut[256]; /* Lookup values for pixels */ unsigned char BGLut[256], /* Black generation lookup table */ UCRLut[256]; /* Undercolor removal lookup table */ int NumPlanes, /* Number of color planes */ ColorBits, /* Number of bits per color */ Feed, /* Number of lines to skip */ Duplex, /* Current duplex mode */ Page; /* Current page number */ /* * Prototypes... */ void Setup(void); void StartPage(ppd_file_t *ppd, cups_page_header_t *header); void EndPage(void); void Shutdown(void); void CancelJob(int sig); void CompressData(unsigned char *line, int length, int plane, int type); void CorrectLine(int width); void OutputLine(cups_page_header_t *header); void DitherLine(unsigned char *pixels, int width, int plane, int y); void MakeLut(float g, float d); void PackLine(unsigned char *pixels, unsigned char *planes, int width); /* * 'Setup()' - Prepare the printer for printing. */ void Setup(void) { /* * Send a PCL reset sequence. */ putchar(0x1b); putchar('E'); } /* * 'StartPage()' - Start a page of graphics. */ void StartPage(ppd_file_t *ppd, /* I - PPD file */ cups_page_header_t *header) /* I - Page header */ { int plane; /* Looping var */ #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) struct sigaction action; /* Actions for POSIX signals */ #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ /* * Register a signal handler to eject the current page if the * job is cancelled. */ #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */ sigset(SIGTERM, CancelJob); #elif defined(HAVE_SIGACTION) memset(&action, 0, sizeof(action)); sigemptyset(&action.sa_mask); action.sa_handler = CancelJob; sigaction(SIGTERM, &action, NULL); #else signal(SIGTERM, CancelJob); #endif /* HAVE_SIGSET */ /* * Setup printer/job attributes... */ Duplex = header->Duplex; if (header->cupsBitsPerColor == 8) ColorBits = 1; else ColorBits = header->cupsBitsPerColor; if (!Duplex || (Page & 1)) { /* * Set the media type, position, and size... */ printf("\033&l6D\033&k12H"); /* Set 6 LPI, 10 CPI */ printf("\033&l0O"); /* Set portrait orientation */ switch (header->PageSize[1]) { case 540 : /* Monarch Envelope */ printf("\033&l80A"); /* Set page size */ break; case 624 : /* DL Envelope */ printf("\033&l90A"); /* Set page size */ break; case 649 : /* C5 Envelope */ printf("\033&l91A"); /* Set page size */ break; case 684 : /* COM-10 Envelope */ printf("\033&l81A"); /* Set page size */ break; case 709 : /* B5 Envelope */ printf("\033&l100A"); /* Set page size */ break; case 756 : /* Executive */ printf("\033&l1A"); /* Set page size */ break; case 792 : /* Letter */ printf("\033&l2A"); /* Set page size */ break; case 842 : /* A4 */ printf("\033&l26A"); /* Set page size */ break; case 1008 : /* Legal */ printf("\033&l3A"); /* Set page size */ break; case 1191 : /* A3 */ printf("\033&l27A"); /* Set page size */ break; case 1224 : /* Tabloid */ printf("\033&l6A"); /* Set page size */ break; } printf("\033&l%dP", /* Set page length */ header->PageSize[1] / 12); printf("\033&l0E"); /* Set top margin to 0 */ printf("\033&l%dX", header->NumCopies); /* Set number copies */ if (header->MediaPosition) printf("\033&l%dH", /* Set media position */ header->MediaPosition); if (header->cupsMediaType) printf("\033&l%dM", /* Set media type */ header->cupsMediaType); if (header->Duplex) printf("\033&l%dS", /* Set duplex mode */ header->Duplex + header->Tumble); printf("\033&l0L"); /* Turn off perforation skip */ if (ppd && ppd->model_number == 2) printf("\033&l-2H"); /* Load media */ } else printf("\033&a2G"); /* Set back side */ /* * Set graphics mode... */ if (ppd->model_number == 2 || header->HWResolution[0] != header->HWResolution[1]) { /* * Figure out the number of color planes... */ if (header->cupsColorSpace == CUPS_CSPACE_KCMY) NumPlanes = 4; else NumPlanes = 1; /* * Send 26-byte configure image data command with horizontal and * vertical resolutions as well as a color count... */ printf("\033*g26W"); putchar(2); /* Format 2 */ putchar(NumPlanes); /* Output planes */ putchar(header->HWResolution[0] >> 8); /* Black resolution */ putchar(header->HWResolution[0]); putchar(header->HWResolution[1] >> 8); putchar(header->HWResolution[1]); putchar(0); putchar(1 << ColorBits); /* # of black levels */ putchar(header->HWResolution[0] >> 8); /* Cyan resolution */ putchar(header->HWResolution[0]); putchar(header->HWResolution[1] >> 8); putchar(header->HWResolution[1]); putchar(0); putchar(1 << ColorBits); /* # of cyan levels */ putchar(header->HWResolution[0] >> 8); /* Magenta resolution */ putchar(header->HWResolution[0]); putchar(header->HWResolution[1] >> 8); putchar(header->HWResolution[1]); putchar(0); putchar(1 << ColorBits); /* # of magenta levels */ putchar(header->HWResolution[0] >> 8); /* Yellow resolution */ putchar(header->HWResolution[0]); putchar(header->HWResolution[1] >> 8); putchar(header->HWResolution[1]); putchar(0); putchar(1 << ColorBits); /* # of yellow levels */ } else { printf("\033*t%dR", header->HWResolution[0]); /* Set resolution */ if (header->cupsColorSpace == CUPS_CSPACE_KCMY) { NumPlanes = 4; printf("\033*r-4U"); /* Set KCMY graphics */ } else if (header->cupsColorSpace == CUPS_CSPACE_CMY) { NumPlanes = 3; printf("\033*r-3U"); /* Set CMY graphics */ } else NumPlanes = 1; /* Black&white graphics */ } /* * Set size and position of graphics... */ printf("\033*r%dS", header->cupsWidth); /* Set width */ printf("\033*r%dT", header->cupsHeight); /* Set height */ printf("\033&a0H"); /* Set horizontal position */ if (ppd) printf("\033&a%.0fV", /* Set vertical position */ 10.0 * (ppd->sizes[0].length - ppd->sizes[0].top)); else printf("\033&a0V"); /* Set top-of-page */ printf("\033*r1A"); /* Start graphics */ if (header->cupsCompression) printf("\033*b%dM", /* Set compression */ header->cupsCompression); Feed = 0; /* No blank lines yet */ /* * Allocate memory for a line of graphics... */ Planes[0] = malloc(header->cupsBytesPerLine); for (plane = 1; plane < NumPlanes; plane ++) Planes[plane] = Planes[0] + plane * header->cupsBytesPerLine / NumPlanes; if (header->cupsBitsPerColor == 8) { /* * Allocate memory for dithering... */ Pixels[0] = malloc(header->cupsBytesPerLine); for (plane = 1; plane < NumPlanes; plane ++) Pixels[plane] = Pixels[0] + plane * header->cupsBytesPerLine / NumPlanes; ErrorBuffers[0][0] = calloc(sizeof(int), NumPlanes * (header->cupsWidth + 4)); for (plane = 1; plane < NumPlanes; plane ++) ErrorBuffers[0][plane] = ErrorBuffers[0][0] + plane * (header->cupsWidth + 4); ErrorBuffers[1][0] = calloc(sizeof(int), NumPlanes * (header->cupsWidth + 4)); for (plane = 1; plane < NumPlanes; plane ++) ErrorBuffers[1][plane] = ErrorBuffers[1][0] + plane * (header->cupsWidth + 4); /* * Set density for dithering... */ switch (header->HWResolution[0]) { case 150 : MakeLut(1.0, 1.0); break; case 300 : MakeLut(1.3, 1.0); break; case 600 : if (header->HWResolution[1] == 300) MakeLut(1.4, 0.75); else MakeLut(1.5, 0.7); break; } } else { Pixels[0] = NULL; ErrorBuffers[0][0] = NULL; ErrorBuffers[1][0] = NULL; } if (ColorBits > 1) BitBuffer = malloc(ColorBits * ((header->cupsWidth + 7) / 8)); else BitBuffer = NULL; if (header->cupsCompression) CompBuffer = malloc(header->cupsBytesPerLine * 2); else CompBuffer = NULL; } /* * 'EndPage()' - Finish a page of graphics. */ void EndPage(void) { #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) struct sigaction action; /* Actions for POSIX signals */ #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ /* * Eject the current page... */ if (NumPlanes > 1) { printf("\033*rC"); /* End color GFX */ if (!(Duplex && (Page & 1))) printf("\033&l0H"); /* Eject current page */ } else { printf("\033*r0B"); /* End GFX */ if (!(Duplex && (Page & 1))) printf("\014"); /* Eject current page */ } fflush(stdout); /* * Unregister the signal handler... */ #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */ sigset(SIGTERM, SIG_IGN); #elif defined(HAVE_SIGACTION) memset(&action, 0, sizeof(action)); sigemptyset(&action.sa_mask); action.sa_handler = SIG_IGN; sigaction(SIGTERM, &action, NULL); #else signal(SIGTERM, SIG_IGN); #endif /* HAVE_SIGSET */ /* * Free memory... */ free(Planes[0]); if (Pixels[0]) free(Pixels[0]); if (ErrorBuffers[0][0]) free(ErrorBuffers[0][0]); if (ErrorBuffers[1][0]) free(ErrorBuffers[1][0]); if (BitBuffer) free(BitBuffer); if (CompBuffer) free(CompBuffer); } /* * 'Shutdown()' - Shutdown the printer. */ void Shutdown(void) { /* * Send a PCL reset sequence. */ putchar(0x1b); putchar('E'); } /* * 'CancelJob()' - Cancel the current job... */ void CancelJob(int sig) /* I - Signal */ { int i; /* Looping var */ (void)sig; /* * Send out lots of NUL bytes to clear out any pending raster data... */ for (i = 0; i < 600; i ++) putchar(0); /* * End the current page and exit... */ EndPage(); Shutdown(); exit(0); } /* * 'CompressData()' - Compress a line of graphics. */ void CompressData(unsigned char *line, /* I - Data to compress */ int length, /* I - Number of bytes */ int plane, /* I - Color plane */ int type) /* I - Type of compression */ { unsigned char *line_ptr, /* Current byte pointer */ *line_end, /* End-of-line byte pointer */ *comp_ptr, /* Pointer into compression buffer */ *start; /* Start of compression sequence */ int count; /* Count of bytes for output */ switch (type) { default : /* * Do no compression... */ line_ptr = line; line_end = line + length; break; case 1 : /* * Do run-length encoding... */ line_end = line + length; for (line_ptr = line, comp_ptr = CompBuffer; line_ptr < line_end; comp_ptr += 2, line_ptr += count) { for (count = 1; (line_ptr + count) < line_end && line_ptr[0] == line_ptr[count] && count < 256; count ++); comp_ptr[0] = count - 1; comp_ptr[1] = line_ptr[0]; } line_ptr = CompBuffer; line_end = comp_ptr; break; case 2 : /* * Do TIFF pack-bits encoding... */ line_ptr = line; line_end = line + length; comp_ptr = CompBuffer; while (line_ptr < line_end) { if ((line_ptr + 1) >= line_end) { /* * Single byte on the end... */ *comp_ptr++ = 0x00; *comp_ptr++ = *line_ptr++; } else if (line_ptr[0] == line_ptr[1]) { /* * Repeated sequence... */ line_ptr ++; count = 2; while (line_ptr < (line_end - 1) && line_ptr[0] == line_ptr[1] && count < 127) { line_ptr ++; count ++; } *comp_ptr++ = 257 - count; *comp_ptr++ = *line_ptr++; } else { /* * Non-repeated sequence... */ start = line_ptr; line_ptr ++; count = 1; while (line_ptr < (line_end - 1) && line_ptr[0] != line_ptr[1] && count < 127) { line_ptr ++; count ++; } *comp_ptr++ = count - 1; memcpy(comp_ptr, start, count); comp_ptr += count; } } line_ptr = CompBuffer; line_end = comp_ptr; break; } /* * Set the length of the data and write a raster plane... */ printf("\033*b%d%c", line_end - line_ptr, plane); fwrite(line_ptr, line_end - line_ptr, 1, stdout); } /* * 'CorrectLine()' - Update the K and CMY colors for 4-color output. */ void CorrectLine(int width) /* I - Width of line */ { int ucr, /* Current UCR value */ c; /* Color value */ unsigned char *kptr, /* Current black data */ *cptr, /* Current cyan data */ *mptr, /* Current magenta data */ *yptr; /* Current yellow data */ for (kptr = Pixels[0], cptr = Pixels[1], mptr = Pixels[2], yptr = Pixels[3]; width > 0; width --) { ucr = *kptr; *kptr++ = BGLut[ucr]; ucr = UCRLut[ucr]; if ((c = *cptr + ucr) > 255) *cptr++ = 255; else *cptr++ = c; if ((c = *mptr + ucr) > 255) *mptr++ = 255; else *mptr++ = c; if ((c = *yptr + ucr) > 255) *yptr++ = 255; else *yptr++ = c; } } /* * 'OutputLine()' - Output a line of graphics. */ void OutputLine(cups_page_header_t *header) /* I - Page header */ { int plane, /* Current plane */ bytes, /* Bytes to write */ count; /* Bytes to convert */ unsigned char bit, /* Current plane data */ bit0, /* Current low bit data */ bit1, /* Current high bit data */ *plane_ptr, /* Pointer into Planes */ *bit_ptr; /* Pointer into BitBuffer */ /* * Output whitespace as needed... */ if (Feed > 0) { printf("\033*b%dY", Feed); Feed = 0; } /* * Write bitmap data as needed... */ bytes = (header->cupsWidth + 7) / 8; for (plane = 0; plane < NumPlanes; plane ++) if (ColorBits == 1) { /* * Send bits as-is... */ CompressData(Planes[plane], bytes, plane < (NumPlanes - 1) ? 'V' : 'W', header->cupsCompression); } else { /* * Separate low and high bit data into separate buffers. */ for (count = header->cupsBytesPerLine / NumPlanes, plane_ptr = Planes[plane], bit_ptr = BitBuffer; count > 0; count -= 2, plane_ptr += 2, bit_ptr ++) { bit = plane_ptr[0]; bit0 = ((bit & 64) << 1) | ((bit & 16) << 2) | ((bit & 4) << 3) | ((bit & 1) << 4); bit1 = (bit & 128) | ((bit & 32) << 1) | ((bit & 8) << 2) | ((bit & 2) << 3); if (count > 1) { bit = plane_ptr[1]; bit0 |= (bit & 1) | ((bit & 4) >> 1) | ((bit & 16) >> 2) | ((bit & 64) >> 3); bit1 |= ((bit & 2) >> 1) | ((bit & 8) >> 2) | ((bit & 32) >> 3) | ((bit & 128) >> 4); } bit_ptr[0] = bit0; bit_ptr[bytes] = bit1; } /* * Send low and high bits... */ CompressData(BitBuffer, bytes, 'V', header->cupsCompression); CompressData(BitBuffer + bytes, bytes, plane < (NumPlanes - 1) ? 'V' : 'W', header->cupsCompression); } fflush(stdout); } /* * 'DitherLine()' - Dither a line of pixels. */ void DitherLine(unsigned char *pixels, /* IO - Pixels on the line */ int width, /* I - Width of the line */ int plane, /* I - Color plane */ int y) /* I - Current row on the line */ { int error, /* Current error value */ errval0, /* Error value for current line */ errval1, /* Error value for next line */ *error0, /* Pointer to current error line */ *error1; /* Pointer to next error line */ int errbase, errrange; if (y & 1) { /* Dither right to left. */ error0 = ErrorBuffers[1][plane] + width + 1; error1 = ErrorBuffers[0][plane] + width + 1; pixels += width - 1; while (width > 0) { /* Compute the error for this pixel. */ error = (PixelLut[*pixels] + *error0) / 128; if (error < 0) error = 0; else if (error > 255) error = 255; /* Set the randomness factor... */ if (error < 128) errrange = error / 15; else errrange = (255 - error) / 15; errbase = 8 - errrange; errrange = errrange * 2 + 1; /* Set the dithered pixel value (0 or 1). */ if (error < 128) *pixels = 0; else { *pixels = 1; error -= 255; } /* Randomize the error value. */ if (errrange) errbase += (rand() % errrange); errval0 = errbase * error; errval1 = (16 - errbase) * error; /* Spread the error around. */ error0[-2] += 2 * errval0; error0[-1] += 4 * errval0; error0[0] = 0; error1[2] += errval1; error1[1] += 2 * errval1; error1[0] += 4 * errval1; error1[-1] += 2 * errval0; error1[-2] += errval1; /* Move to the next pixel */ pixels --; error0 --; error1 --; width --; } } else { /* Dither left to right. */ error0 = ErrorBuffers[0][plane] + 2; error1 = ErrorBuffers[1][plane] + 2; while (width > 0) { /* Compute the error for this pixel. */ error = (PixelLut[*pixels] + *error0) / 128; if (error < 0) error = 0; else if (error > 255) error = 255; /* Set the randomness factor... */ if (error < 128) errrange = error / 15; else errrange = (255 - error) / 15; errbase = 8 - errrange; errrange = errrange * 2 + 1; /* Set the dithered pixel value (0 or 1). */ if (error < 128) *pixels = 0; else { *pixels = 1; error -= 255; } /* Randomize the error value. */ if (errrange) errbase += (rand() % errrange); errval0 = errbase * error; errval1 = (16 - errbase) * error; /* Spread the error around. */ error0[2] += 2 * errval0; error0[1] += 4 * errval0; error0[0] = 0; error1[-2] += errval1; error1[-1] += 2 * errval1; error1[0] += 4 * errval1; error1[1] += 2 * errval0; error1[2] += errval1; /* Move to the next pixel */ pixels ++; error0 ++; error1 ++; width --; } } } /* * 'MakeLut()' - Make a lookup table for pixel values. */ void MakeLut(float g, /* I - Gamma correction */ float d) /* I - Density correction */ { int i; /* Looping var */ float v; /* Current value */ /* * Build a gamma/density lookup table for dithering... */ d *= 32767.0f; for (i = 0; i < 256; i ++) { v = 1.0f - pow((float)(255 - i) / 255.0f, 0.58823529); PixelLut[i] = (int)(d * pow(v, g) + 0.5f); } /* * Build generic BG/UCR lookup tables to better choose between * CMY and K when producing CMYK output. */ #define lower 191 /* About 75% */ #define upper 255 /* Just shy of 100% */ #define delta (upper - lower) for (i = 0; i < lower; i ++) { /* * No use black until the lower bound... */ BGLut[i] = 0; UCRLut[i] = i; } for (; i < upper; i ++) { /* * Transition from CMY to black between the lower and upper bound... */ BGLut[i] = upper * (i - lower) * (i - lower) / delta / delta; UCRLut[i] = lower - lower * (i - lower) * (i - lower) / delta / delta; } for (; i < 256; i ++) { /* * Use only black for everything else... */ BGLut[i] = i; UCRLut[i] = 0; } } /* * 'PackLine()' - Pack a line of pixels into bits. */ void PackLine(unsigned char *pixels, /* I - Pixel buffer */ unsigned char *planes, /* O - Bit plane buffer */ int width) /* I - Width of line */ { unsigned char bit; /* Current bit */ unsigned char byte; /* Current byte */ for (bit = 128, byte = 0; width > 0; width --, pixels ++) { if (*pixels) byte |= bit; if (bit > 1) bit >>= 1; else { *planes++ = byte; bit = 128; byte = 0; } } if (bit > 1) *planes++ = byte; } /* * 'main()' - Main entry and processing of driver. */ int /* O - Exit status */ main(int argc, /* I - Number of command-line arguments */ char *argv[]) /* I - Command-line arguments */ { int fd; /* File descriptor */ cups_raster_t *ras; /* Raster stream for printing */ cups_page_header_t header; /* Page header from file */ int y; /* Current line */ int plane; /* Current color plane */ ppd_file_t *ppd; /* PPD file */ /* * Make sure status messages are not buffered... */ setbuf(stderr, NULL); /* * Check command-line... */ if (argc < 6 || argc > 7) { /* * We don't have the correct number of arguments; write an error message * and return. */ fputs("ERROR: rastertopcl job-id user title copies options [file]\n", stderr); return (1); } /* * Open the page stream... */ if (argc == 7) { if ((fd = open(argv[6], O_RDONLY)) == -1) { perror("ERROR: Unable to open raster file - "); sleep(1); return (1); } } else fd = 0; ras = cupsRasterOpen(fd, CUPS_RASTER_READ); /* * Initialize the print device... */ ppd = ppdOpenFile(getenv("PPD")); Setup(); /* * Process pages as needed... */ Page = 0; while (cupsRasterReadHeader(ras, &header)) { /* * Write a status message with the page number and number of copies. */ Page ++; fprintf(stderr, "PAGE: %d %d\n", Page, header.NumCopies); /* * Start the page... */ StartPage(ppd, &header); /* * Loop for each line on the page... */ for (y = 0; y < header.cupsHeight; y ++) { /* * Let the user know how far we have progressed... */ if ((y & 127) == 0) fprintf(stderr, "INFO: Printing page %d, %d%% complete...\n", Page, 100 * y / header.cupsHeight); /* * Read a line of graphics... */ if (header.cupsBitsPerColor == 8) { /* * Read and dither pixels... */ if (cupsRasterReadPixels(ras, Pixels[0], header.cupsBytesPerLine) < 1) break; /* * Fix CMY and K for 4-color output... */ if (NumPlanes == 4) CorrectLine(header.cupsWidth); for (plane = 0; plane < NumPlanes; plane ++) { DitherLine(Pixels[plane], header.cupsWidth, plane, y); PackLine(Pixels[plane], Planes[plane], header.cupsWidth); } } else if (cupsRasterReadPixels(ras, Planes[0], header.cupsBytesPerLine) < 1) break; /* * See if the line is blank; if not, write it to the printer... */ if (Planes[0][0] || memcmp(Planes[0], Planes[0] + 1, header.cupsBytesPerLine - 1)) OutputLine(&header); else Feed ++; } /* * Eject the page... */ EndPage(); } /* * Shutdown the printer... */ Shutdown(); if (ppd) ppdClose(ppd); /* * Close the raster stream... */ cupsRasterClose(ras); if (fd != 0) close(fd); /* * If no pages were printed, send an error message... */ if (Page == 0) fputs("ERROR: No pages found!\n", stderr); else fputs("INFO: Ready to print.\n", stderr); return (Page == 0); } /* * End of "$Id: rastertohp.c 1 2007-04-11 06:10:17Z mike $". */