#include <cups/cups.h>
#include <cups/ppd.h>
#include <cups/string-private.h>
#include <cups/language-private.h>
#include <cups/raster.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
unsigned char *Planes[4],
*CompBuffer,
*BitBuffer;
unsigned NumPlanes,
ColorBits,
Feed;
int Duplex,
Page,
Canceled;
void Setup(void);
void StartPage(ppd_file_t *ppd, cups_page_header2_t *header);
void EndPage(void);
void Shutdown(void);
void CancelJob(int sig);
void CompressData(unsigned char *line, unsigned length, unsigned plane, unsigned type);
void OutputLine(cups_page_header2_t *header);
void
Setup(void)
{
putchar(0x1b);
putchar('E');
}
void
StartPage(ppd_file_t *ppd,
cups_page_header2_t *header)
{
unsigned plane;
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 595 :
printf("\033&l25A");
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)
{
int mode = Duplex ? 1 + header->Tumble != 0 : 0;
printf("\033&l%dS", mode);
printf("\033&l0L");
}
}
else if (!ppd || ppd->model_number != 2)
printf("\033&a2G");
if (ppd && 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((int)NumPlanes);
putchar((int)(header->HWResolution[0] >> 8));
putchar((int)header->HWResolution[0]);
putchar((int)(header->HWResolution[1] >> 8));
putchar((int)header->HWResolution[1]);
putchar(0);
putchar(1 << ColorBits);
putchar((int)(header->HWResolution[0] >> 8));
putchar((int)header->HWResolution[0]);
putchar((int)(header->HWResolution[1] >> 8));
putchar((int)header->HWResolution[1]);
putchar(0);
putchar(1 << ColorBits);
putchar((int)(header->HWResolution[0] >> 8));
putchar((int)header->HWResolution[0]);
putchar((int)(header->HWResolution[1] >> 8));
putchar((int)header->HWResolution[1]);
putchar(0);
putchar(1 << ColorBits);
putchar((int)(header->HWResolution[0] >> 8));
putchar((int)header->HWResolution[0]);
putchar((int)(header->HWResolution[1] >> 8));
putchar((int)header->HWResolution[1]);
putchar(0);
putchar(1 << ColorBits);
printf("\033&l0H");
}
else
{
printf("\033*t%uR", 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%uS", header->cupsWidth);
printf("\033*r%uT", 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%uM",
header->cupsCompression);
Feed = 0;
if ((Planes[0] = malloc(header->cupsBytesPerLine + NumPlanes)) == NULL)
{
fputs("ERROR: Unable to allocate memory\n", stderr);
exit(1);
}
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 + 2);
else
CompBuffer = NULL;
}
void
EndPage(void)
{
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);
free(Planes[0]);
if (BitBuffer)
free(BitBuffer);
if (CompBuffer)
free(CompBuffer);
}
void
Shutdown(void)
{
putchar(0x1b);
putchar('E');
}
void
CancelJob(int sig)
{
(void)sig;
Canceled = 1;
}
void
CompressData(unsigned char *line,
unsigned length,
unsigned plane,
unsigned type)
{
unsigned char *line_ptr,
*line_end,
*comp_ptr,
*start;
unsigned 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] = (unsigned char)(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++ = (unsigned char)(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++ = (unsigned char)(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, (size_t)(line_end - line_ptr), 1, stdout);
}
void
OutputLine(cups_page_header2_t *header)
{
unsigned 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 = (unsigned char)(((bit & 64) << 1) | ((bit & 16) << 2) | ((bit & 4) << 3) | ((bit & 1) << 4));
bit1 = (unsigned char)((bit & 128) | ((bit & 32) << 1) | ((bit & 8) << 2) | ((bit & 2) << 3));
if (count > 1)
{
bit = plane_ptr[1];
bit0 |= (unsigned char)((bit & 1) | ((bit & 4) >> 1) | ((bit & 16) >> 2) | ((bit & 64) >> 3));
bit1 |= (unsigned char)(((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_header2_t header;
unsigned y;
ppd_file_t *ppd;
#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
struct sigaction action;
#endif
setbuf(stderr, NULL);
if (argc < 6 || argc > 7)
{
_cupsLangPrintFilter(stderr, "ERROR",
_("%s job-id user title copies options [file]"),
"rastertohp");
return (1);
}
if (argc == 7)
{
if ((fd = open(argv[6], O_RDONLY)) == -1)
{
_cupsLangPrintError("ERROR", _("Unable to open raster file"));
sleep(1);
return (1);
}
}
else
fd = 0;
ras = cupsRasterOpen(fd, CUPS_RASTER_READ);
Canceled = 0;
#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
ppd = ppdOpenFile(getenv("PPD"));
if (!ppd)
{
ppd_status_t status;
int linenum;
_cupsLangPrintFilter(stderr, "ERROR",
_("The PPD file could not be opened."));
status = ppdLastError(&linenum);
fprintf(stderr, "DEBUG: %s on line %d.\n", ppdErrorString(status), linenum);
return (1);
}
Setup();
Page = 0;
while (cupsRasterReadHeader2(ras, &header))
{
if (Canceled)
break;
Page ++;
fprintf(stderr, "PAGE: %d %d\n", Page, header.NumCopies);
_cupsLangPrintFilter(stderr, "INFO", _("Starting page %d."), Page);
StartPage(ppd, &header);
for (y = 0; y < header.cupsHeight; y ++)
{
if (Canceled)
break;
if ((y & 127) == 0)
{
_cupsLangPrintFilter(stderr, "INFO",
_("Printing page %d, %u%% complete."),
Page, 100 * y / header.cupsHeight);
fprintf(stderr, "ATTR: job-media-progress=%u\n",
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 ++;
}
_cupsLangPrintFilter(stderr, "INFO", _("Finished page %d."), Page);
EndPage();
if (Canceled)
break;
}
Shutdown();
if (ppd)
ppdClose(ppd);
cupsRasterClose(ras);
if (fd != 0)
close(fd);
if (Page == 0)
{
_cupsLangPrintFilter(stderr, "ERROR", _("No pages were found."));
return (1);
}
else
return (0);
}