#include <cups/cups.h>
#include <cups/string.h>
#include "raster.h"
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
unsigned char *Planes[4],
*CompBuffer,
*BitBuffer;
int NumPlanes,
ColorBits,
Feed,
Duplex,
Page;
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 OutputLine(cups_page_header_t *header);
void
Setup(void)
{
putchar(0x1b);
putchar('E');
}
void
StartPage(ppd_file_t *ppd,
cups_page_header_t *header)
{
int plane;
#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
struct sigaction action;
#endif
#ifdef HAVE_SIGSET
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
fprintf(stderr, "DEBUG: StartPage...\n");
fprintf(stderr, "DEBUG: MediaClass = \"%s\"\n", header->MediaClass);
fprintf(stderr, "DEBUG: MediaColor = \"%s\"\n", header->MediaColor);
fprintf(stderr, "DEBUG: MediaType = \"%s\"\n", header->MediaType);
fprintf(stderr, "DEBUG: OutputType = \"%s\"\n", header->OutputType);
fprintf(stderr, "DEBUG: AdvanceDistance = %d\n", header->AdvanceDistance);
fprintf(stderr, "DEBUG: AdvanceMedia = %d\n", header->AdvanceMedia);
fprintf(stderr, "DEBUG: Collate = %d\n", header->Collate);
fprintf(stderr, "DEBUG: CutMedia = %d\n", header->CutMedia);
fprintf(stderr, "DEBUG: Duplex = %d\n", header->Duplex);
fprintf(stderr, "DEBUG: HWResolution = [ %d %d ]\n", header->HWResolution[0],
header->HWResolution[1]);
fprintf(stderr, "DEBUG: ImagingBoundingBox = [ %d %d %d %d ]\n",
header->ImagingBoundingBox[0], header->ImagingBoundingBox[1],
header->ImagingBoundingBox[2], header->ImagingBoundingBox[3]);
fprintf(stderr, "DEBUG: InsertSheet = %d\n", header->InsertSheet);
fprintf(stderr, "DEBUG: Jog = %d\n", header->Jog);
fprintf(stderr, "DEBUG: LeadingEdge = %d\n", header->LeadingEdge);
fprintf(stderr, "DEBUG: Margins = [ %d %d ]\n", header->Margins[0],
header->Margins[1]);
fprintf(stderr, "DEBUG: ManualFeed = %d\n", header->ManualFeed);
fprintf(stderr, "DEBUG: MediaPosition = %d\n", header->MediaPosition);
fprintf(stderr, "DEBUG: MediaWeight = %d\n", header->MediaWeight);
fprintf(stderr, "DEBUG: MirrorPrint = %d\n", header->MirrorPrint);
fprintf(stderr, "DEBUG: NegativePrint = %d\n", header->NegativePrint);
fprintf(stderr, "DEBUG: NumCopies = %d\n", header->NumCopies);
fprintf(stderr, "DEBUG: Orientation = %d\n", header->Orientation);
fprintf(stderr, "DEBUG: OutputFaceUp = %d\n", header->OutputFaceUp);
fprintf(stderr, "DEBUG: PageSize = [ %d %d ]\n", header->PageSize[0],
header->PageSize[1]);
fprintf(stderr, "DEBUG: Separations = %d\n", header->Separations);
fprintf(stderr, "DEBUG: TraySwitch = %d\n", header->TraySwitch);
fprintf(stderr, "DEBUG: Tumble = %d\n", header->Tumble);
fprintf(stderr, "DEBUG: cupsWidth = %d\n", header->cupsWidth);
fprintf(stderr, "DEBUG: cupsHeight = %d\n", header->cupsHeight);
fprintf(stderr, "DEBUG: cupsMediaType = %d\n", header->cupsMediaType);
fprintf(stderr, "DEBUG: cupsBitsPerColor = %d\n", header->cupsBitsPerColor);
fprintf(stderr, "DEBUG: cupsBitsPerPixel = %d\n", header->cupsBitsPerPixel);
fprintf(stderr, "DEBUG: cupsBytesPerLine = %d\n", header->cupsBytesPerLine);
fprintf(stderr, "DEBUG: cupsColorOrder = %d\n", header->cupsColorOrder);
fprintf(stderr, "DEBUG: cupsColorSpace = %d\n", header->cupsColorSpace);
fprintf(stderr, "DEBUG: cupsCompression = %d\n", header->cupsCompression);
Duplex = header->Duplex;
ColorBits = header->cupsBitsPerColor;
if ((!Duplex || (Page & 1)) && header->MediaPosition)
printf("\033&l%dH",
header->MediaPosition);
if (Duplex && ppd && ppd->model_number == 2)
{
printf("\033&l-2H");
if (Page & 1)
printf("\033&l2S");
}
if (!Duplex || (Page & 1) || (ppd && ppd->model_number == 2))
{
printf("\033&l6D\033&k12H");
printf("\033&l0O");
switch (header->PageSize[1])
{
case 540 :
printf("\033&l80A");
break;
case 624 :
printf("\033&l90A");
break;
case 649 :
printf("\033&l91A");
break;
case 684 :
printf("\033&l81A");
break;
case 709 :
printf("\033&l100A");
break;
case 756 :
printf("\033&l1A");
break;
case 792 :
printf("\033&l2A");
break;
case 842 :
printf("\033&l26A");
break;
case 1008 :
printf("\033&l3A");
break;
case 1191 :
printf("\033&l27A");
break;
case 1224 :
printf("\033&l6A");
break;
}
printf("\033&l%dP",
header->PageSize[1] / 12);
printf("\033&l0E");
}
if (!Duplex || (Page & 1))
{
printf("\033&l%dX", header->NumCopies);
if (header->cupsMediaType &&
(!ppd || ppd->model_number != 2 || header->HWResolution[0] == 600))
printf("\033&l%dM",
header->cupsMediaType);
if (!ppd || ppd->model_number != 2)
{
if (header->Duplex)
printf("\033&l%dS",
header->Duplex + header->Tumble);
printf("\033&l0L");
}
}
else if (!ppd || ppd->model_number != 2)
printf("\033&a2G");
if (ppd->model_number == 2)
{
if (header->cupsColorSpace == CUPS_CSPACE_KCMY)
NumPlanes = 4;
else
NumPlanes = 1;
printf("\033&u%dD", header->HWResolution[0]);
printf("\033&l0e0L");
printf("\033*p0Y\033*p0X");
printf("\033*g26W");
putchar(2);
putchar(NumPlanes);
putchar(header->HWResolution[0] >> 8);
putchar(header->HWResolution[0]);
putchar(header->HWResolution[1] >> 8);
putchar(header->HWResolution[1]);
putchar(0);
putchar(1 << ColorBits);
putchar(header->HWResolution[0] >> 8);
putchar(header->HWResolution[0]);
putchar(header->HWResolution[1] >> 8);
putchar(header->HWResolution[1]);
putchar(0);
putchar(1 << ColorBits);
putchar(header->HWResolution[0] >> 8);
putchar(header->HWResolution[0]);
putchar(header->HWResolution[1] >> 8);
putchar(header->HWResolution[1]);
putchar(0);
putchar(1 << ColorBits);
putchar(header->HWResolution[0] >> 8);
putchar(header->HWResolution[0]);
putchar(header->HWResolution[1] >> 8);
putchar(header->HWResolution[1]);
putchar(0);
putchar(1 << ColorBits);
printf("\033&l0H");
}
else
{
printf("\033*t%dR", header->HWResolution[0]);
if (header->cupsColorSpace == CUPS_CSPACE_KCMY)
{
NumPlanes = 4;
printf("\033*r-4U");
}
else if (header->cupsColorSpace == CUPS_CSPACE_CMY)
{
NumPlanes = 3;
printf("\033*r-3U");
}
else
NumPlanes = 1;
printf("\033*r%dS", header->cupsWidth);
printf("\033*r%dT", header->cupsHeight);
printf("\033&a0H");
if (ppd)
printf("\033&a%.0fV",
10.0 * (ppd->sizes[0].length - ppd->sizes[0].top));
else
printf("\033&a0V");
}
printf("\033*r1A");
if (header->cupsCompression)
printf("\033*b%dM",
header->cupsCompression);
Feed = 0;
Planes[0] = malloc(header->cupsBytesPerLine);
for (plane = 1; plane < NumPlanes; plane ++)
Planes[plane] = Planes[0] + plane * header->cupsBytesPerLine / NumPlanes;
if (ColorBits > 1)
BitBuffer = malloc(ColorBits * ((header->cupsWidth + 7) / 8));
else
BitBuffer = NULL;
if (header->cupsCompression)
CompBuffer = malloc(header->cupsBytesPerLine * 2);
else
CompBuffer = NULL;
}
void
EndPage(void)
{
#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
struct sigaction action;
#endif
if (NumPlanes > 1)
{
printf("\033*rC");
if (!(Duplex && (Page & 1)))
printf("\033&l0H");
}
else
{
printf("\033*r0B");
if (!(Duplex && (Page & 1)))
printf("\014");
}
fflush(stdout);
#ifdef HAVE_SIGSET
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
free(Planes[0]);
if (BitBuffer)
free(BitBuffer);
if (CompBuffer)
free(CompBuffer);
}
void
Shutdown(void)
{
putchar(0x1b);
putchar('E');
}
void
CancelJob(int sig)
{
int i;
(void)sig;
for (i = 0; i < 600; i ++)
putchar(0);
EndPage();
Shutdown();
exit(0);
}
void
CompressData(unsigned char *line,
int length,
int plane,
int type)
{
unsigned char *line_ptr,
*line_end,
*comp_ptr,
*start;
int count;
switch (type)
{
default :
line_ptr = line;
line_end = line + length;
break;
case 1 :
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 :
line_ptr = line;
line_end = line + length;
comp_ptr = CompBuffer;
while (line_ptr < line_end)
{
if ((line_ptr + 1) >= line_end)
{
*comp_ptr++ = 0x00;
*comp_ptr++ = *line_ptr++;
}
else if (line_ptr[0] == line_ptr[1])
{
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
{
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;
}
printf("\033*b%d%c", (int)(line_end - line_ptr), plane);
fwrite(line_ptr, line_end - line_ptr, 1, stdout);
}
void
OutputLine(cups_page_header_t *header)
{
int plane,
bytes,
count;
unsigned char bit,
bit0,
bit1,
*plane_ptr,
*bit_ptr;
if (Feed > 0)
{
printf("\033*b%dY", Feed);
Feed = 0;
}
bytes = (header->cupsWidth + 7) / 8;
for (plane = 0; plane < NumPlanes; plane ++)
if (ColorBits == 1)
{
CompressData(Planes[plane], bytes, plane < (NumPlanes - 1) ? 'V' : 'W',
header->cupsCompression);
}
else
{
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;
}
CompressData(BitBuffer, bytes, 'V', header->cupsCompression);
CompressData(BitBuffer + bytes, bytes, plane < (NumPlanes - 1) ? 'V' : 'W',
header->cupsCompression);
}
fflush(stdout);
}
int
main(int argc,
char *argv[])
{
int fd;
cups_raster_t *ras;
cups_page_header_t header;
int y;
ppd_file_t *ppd;
setbuf(stderr, NULL);
if (argc < 6 || argc > 7)
{
fputs("ERROR: rastertopcl job-id user title copies options [file]\n", stderr);
return (1);
}
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);
ppd = ppdOpenFile(getenv("PPD"));
Setup();
Page = 0;
while (cupsRasterReadHeader(ras, &header))
{
Page ++;
fprintf(stderr, "PAGE: %d %d\n", Page, header.NumCopies);
StartPage(ppd, &header);
for (y = 0; y < header.cupsHeight; y ++)
{
if ((y & 127) == 0)
fprintf(stderr, "INFO: Printing page %d, %d%% complete...\n", Page,
100 * y / header.cupsHeight);
if (cupsRasterReadPixels(ras, Planes[0], header.cupsBytesPerLine) < 1)
break;
if (Planes[0][0] ||
memcmp(Planes[0], Planes[0] + 1, header.cupsBytesPerLine - 1))
OutputLine(&header);
else
Feed ++;
}
EndPage();
}
Shutdown();
if (ppd)
ppdClose(ppd);
cupsRasterClose(ras);
if (fd != 0)
close(fd);
if (Page == 0)
fputs("ERROR: No pages found!\n", stderr);
else
fputs("INFO: " CUPS_SVERSION " is ready to print.\n", stderr);
return (Page == 0);
}