#include <cups/cups.h>
#include <cups/ppd.h>
#include <cups/string.h>
#include "raster.h"
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#define EPSON_9PIN 0
#define EPSON_24PIN 1
#define EPSON_COLOR 2
#define EPSON_PHOTO 3
#define EPSON_ICOLOR 4
#define EPSON_IPHOTO 5
#define pwrite(s,n) fwrite((s), 1, (n), stdout)
unsigned char *Planes[6],
*CompBuffer,
*LineBuffers[2];
int Model,
NumPlanes,
Feed;
int DotBit,
DotBytes,
DotColumns,
LineCount,
EvenOffset,
OddOffset,
Shingling;
void Setup(void);
void StartPage(const ppd_file_t *ppd, const cups_page_header_t *header);
void EndPage(const cups_page_header_t *header);
void Shutdown(void);
void CancelJob(int sig);
void CompressData(const unsigned char *line, int length, int plane,
int type, int xstep, int ystep);
void OutputLine(const cups_page_header_t *header);
void OutputRows(const cups_page_header_t *header, int row);
void
Setup(void)
{
const char *device_uri;
if ((device_uri = getenv("DEVICE_URI")) != NULL &&
strncmp(device_uri, "usb:", 4) == 0)
pwrite("\000\000\000\033\001@EJL 1284.4\n@EJL \n\033@", 29);
}
void
StartPage(const ppd_file_t *ppd,
const cups_page_header_t *header)
{
int n, t;
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
if (ppd->nickname && strstr(ppd->nickname, "OKIDATA") != NULL)
printf("\033{A");
printf("\033@");
Model = ppd->model_number;
switch (ppd->model_number)
{
case EPSON_9PIN :
case EPSON_24PIN :
printf("\033P");
if (header->HWResolution[0] == 360 || header->HWResolution[0] == 240)
{
printf("\033x1");
printf("\033U1");
}
else
{
printf("\033x0");
printf("\033U0");
}
printf("\033l%c\033Q%c", 0,
(int)(10.0 * header->PageSize[0] / 72.0 + 0.5));
printf("\033C%c%c", 0,
(int)(header->PageSize[1] / 72.0 + 0.5));
printf("\033N%c", 0);
DotBytes = header->cupsRowCount / 8;
DotColumns = header->HWResolution[0] / 60;
Shingling = 0;
if (ppd->model_number == EPSON_9PIN)
printf("\033\063\030");
else
switch (header->HWResolution[0])
{
case 60:
case 120 :
printf("\033\063\030");
break;
case 180 :
case 360 :
Shingling = 1;
if (header->HWResolution[1] == 180)
printf("\033\063\010");
else
printf("\033+\010");
break;
}
break;
default :
pwrite("\033(G\001\000\001", 6);
if (Model < EPSON_ICOLOR)
{
pwrite("\033(U\001\000", 5);
putchar(3600 / header->HWResolution[1]);
}
else
{
pwrite("\033(U\005\000", 5);
putchar(1440 / header->HWResolution[1]);
putchar(1440 / header->HWResolution[1]);
putchar(1440 / header->HWResolution[0]);
putchar(0xa0);
putchar(0x05);
}
n = header->PageSize[1] * header->HWResolution[1] / 72.0;
pwrite("\033(C\002\000", 5);
putchar(n);
putchar(n >> 8);
t = (ppd->sizes[1].length - ppd->sizes[1].top) *
header->HWResolution[1] / 72.0;
pwrite("\033(c\004\000", 5);
putchar(t);
putchar(t >> 8);
putchar(n);
putchar(n >> 8);
if (header->HWResolution[1] == 720)
{
pwrite("\033(i\001\000\001", 6);
pwrite("\033(e\002\000\000\001", 7);
}
pwrite("\033(V\002\000\000\000", 7);
DotBytes = 0;
DotColumns = 0;
Shingling = 0;
break;
}
if (header->cupsColorSpace == CUPS_CSPACE_CMY)
NumPlanes = 3;
else if (header->cupsColorSpace == CUPS_CSPACE_KCMY)
NumPlanes = 4;
else if (header->cupsColorSpace == CUPS_CSPACE_KCMYcm)
NumPlanes = 6;
else
NumPlanes = 1;
Feed = 0;
Planes[0] = malloc(header->cupsBytesPerLine);
for (plane = 1; plane < NumPlanes; plane ++)
Planes[plane] = Planes[0] + plane * header->cupsBytesPerLine / NumPlanes;
if (header->cupsCompression || DotBytes)
CompBuffer = calloc(2, header->cupsWidth);
else
CompBuffer = NULL;
if (DotBytes)
{
LineBuffers[0] = calloc(DotBytes, header->cupsWidth * (Shingling + 1));
LineBuffers[1] = LineBuffers[0] + DotBytes * header->cupsWidth;
DotBit = 128;
LineCount = 0;
EvenOffset = 0;
OddOffset = 0;
}
}
void
EndPage(const cups_page_header_t *header)
{
#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
struct sigaction action;
#endif
if (DotBytes && header)
{
if (!Shingling)
OutputRows(header, 0);
else if (OddOffset > EvenOffset)
{
OutputRows(header, 1);
OutputRows(header, 0);
}
else
{
OutputRows(header, 0);
OutputRows(header, 1);
}
}
putchar(12);
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 (CompBuffer)
free(CompBuffer);
if (DotBytes)
free(LineBuffers[0]);
}
void
Shutdown(void)
{
printf("\033@");
}
void
CancelJob(int sig)
{
int i;
(void)sig;
if (DotBytes)
i = DotBytes * 360 * 8;
else
i = 720;
for (; i > 0; i --)
putchar(0);
EndPage(NULL);
Shutdown();
exit(0);
}
void
CompressData(const unsigned char *line,
int length,
int plane,
int type,
int xstep,
int ystep)
{
const unsigned char *line_ptr,
*line_end,
*start;
unsigned char *comp_ptr,
temp;
int count;
static int ctable[6] = { 0, 2, 1, 4, 18, 17 };
line_ptr = line;
line_end = line + length;
if (ystep == 5)
{
for (comp_ptr = (unsigned char *)line; comp_ptr < line_end;)
{
temp = *comp_ptr;
if ((temp & 0xc0) == 0xc0)
temp &= 0xbf;
if ((temp & 0x60) == 0x60)
temp &= 0xdf;
if ((temp & 0x30) == 0x30)
temp &= 0xef;
if ((temp & 0x18) == 0x18)
temp &= 0xf7;
if ((temp & 0x0c) == 0x0c)
temp &= 0xfb;
if ((temp & 0x06) == 0x06)
temp &= 0xfd;
if ((temp & 0x03) == 0x03)
temp &= 0xfe;
*comp_ptr++ = temp;
if ((temp & 0x01) && comp_ptr < line_end && *comp_ptr & 0x80)
*comp_ptr &= 0x7f;
}
}
switch (type)
{
case 0 :
break;
case 1 :
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;
}
putchar(0x0d);
if (Model < EPSON_ICOLOR)
{
if (NumPlanes > 1)
{
if (plane > 3)
printf("\033(r%c%c%c%c", 2, 0, 1, ctable[plane] & 15);
else if (NumPlanes == 3)
printf("\033r%c", ctable[plane + 1]);
else
printf("\033r%c", ctable[plane]);
}
length *= 8;
printf("\033.");
putchar(type);
putchar(ystep);
putchar(xstep);
putchar(1);
putchar(length);
putchar(length >> 8);
}
else
{
printf("\033i");
putchar(ctable[plane]);
putchar(type);
putchar(1);
putchar(length & 255);
putchar(length >> 8);
putchar(1);
putchar(0);
}
pwrite(line_ptr, line_end - line_ptr);
fflush(stdout);
}
void
OutputLine(const cups_page_header_t *header)
{
if (header->cupsRowCount)
{
int width;
unsigned char *tempptr,
*evenptr,
*oddptr;
register int x;
unsigned char bit;
const unsigned char *pixel;
unsigned char *temp;
for (x = header->cupsWidth, bit = 128, pixel = Planes[0],
temp = CompBuffer;
x > 0;
x --, temp ++)
{
if (*pixel & bit)
*temp |= DotBit;
if (bit > 1)
bit >>= 1;
else
{
bit = 128;
pixel ++;
}
}
if (DotBit > 1)
DotBit >>= 1;
else
{
if (Shingling && LineCount != 0)
{
if (LineCount & 1)
{
evenptr = LineBuffers[1] + OddOffset;
oddptr = LineBuffers[0] + EvenOffset + DotBytes;
}
else
{
evenptr = LineBuffers[0] + EvenOffset;
oddptr = LineBuffers[1] + OddOffset + DotBytes;
}
for (width = header->cupsWidth, tempptr = CompBuffer;
width > 0;
width -= 2, tempptr += 2, oddptr += DotBytes * 2,
evenptr += DotBytes * 2)
{
evenptr[0] = tempptr[0];
oddptr[0] = tempptr[1];
}
}
else
{
for (width = header->cupsWidth, tempptr = CompBuffer,
evenptr = LineBuffers[0] + EvenOffset;
width >= 0;
width --, tempptr ++, evenptr += DotBytes)
*evenptr = tempptr[0];
}
if (Shingling && LineCount != 0)
{
EvenOffset ++;
OddOffset ++;
if (EvenOffset == DotBytes)
{
EvenOffset = 0;
OutputRows(header, 0);
}
if (OddOffset == DotBytes)
{
OddOffset = 0;
OutputRows(header, 1);
}
}
else
{
EvenOffset ++;
if (EvenOffset == DotBytes)
{
EvenOffset = 0;
OutputRows(header, 0);
}
}
DotBit = 128;
LineCount ++;
memset(CompBuffer, 0, header->cupsWidth);
}
}
else
{
int plane;
int bytes;
int xstep, ystep;
xstep = 3600 / header->HWResolution[0];
ystep = 3600 / header->HWResolution[1];
bytes = header->cupsBytesPerLine / NumPlanes;
for (plane = 0; plane < NumPlanes; plane ++)
{
if (!Planes[plane][0] &&
memcmp(Planes[plane], Planes[plane] + 1, bytes - 1) == 0)
continue;
if (Feed > 0)
{
pwrite("\033(v\002\000", 5);
putchar(Feed);
putchar(Feed >> 8);
Feed = 0;
}
CompressData(Planes[plane], bytes, plane, header->cupsCompression, xstep,
ystep);
}
Feed ++;
}
}
void
OutputRows(const cups_page_header_t *header,
int row)
{
unsigned i, n;
int dot_count,
dot_min;
unsigned char *dot_ptr,
*ptr;
dot_min = DotBytes * DotColumns;
if (LineBuffers[row][0] != 0 ||
memcmp(LineBuffers[row], LineBuffers[row] + 1,
header->cupsWidth * DotBytes - 1))
{
i = 0;
dot_count = header->cupsWidth * DotBytes;
dot_ptr = LineBuffers[row];
while (dot_count >= dot_min && dot_ptr[0] == 0 &&
memcmp(dot_ptr, dot_ptr + 1, dot_min - 1) == 0)
{
i ++;
dot_ptr += dot_min;
dot_count -= dot_min;
}
while (dot_count >= dot_min && dot_ptr[dot_count - dot_min] == 0 &&
memcmp(dot_ptr + dot_count - dot_min,
dot_ptr + dot_count - dot_min + 1, dot_min - 1) == 0)
dot_count -= dot_min;
putchar(0x1b);
putchar('$');
putchar(i & 255);
putchar(i >> 8);
printf("\033*");
switch (header->HWResolution[0])
{
case 60 :
putchar(0);
break;
case 120 :
putchar(1);
break;
case 180 :
putchar(39);
break;
case 240 :
putchar(3);
break;
case 360 :
if (header->HWResolution[1] == 180)
{
if (Shingling && LineCount != 0)
putchar(40);
else
putchar(41);
}
else
{
if (Shingling && LineCount != 0)
putchar(72);
else
putchar(73);
}
break;
}
n = (unsigned)dot_count / DotBytes;
putchar(n & 255);
putchar(n / 256);
if (header->HWResolution[0] == 120 ||
header->HWResolution[0] == 240)
{
for (n = dot_count / 2, ptr = dot_ptr; n > 0; n --, ptr += 2)
{
putchar(*ptr);
putchar(0);
}
putchar(0x1b);
putchar('$');
putchar(i & 255);
putchar(i >> 8);
if (header->HWResolution[0] == 120)
printf("\033*\001");
else
printf("\033*\003");
n = (unsigned)dot_count / DotBytes;
putchar(n & 255);
putchar(n / 256);
for (n = dot_count / 2, ptr = dot_ptr + 1; n > 0; n --, ptr += 2)
{
putchar(0);
putchar(*ptr);
}
}
else
pwrite(dot_ptr, dot_count);
}
putchar('\n');
if (Shingling && row == 1)
{
if (header->HWResolution[1] == 360)
printf("\n\n\n\n");
else
printf("\n");
}
fflush(stdout);
memset(LineBuffers[row], 0, header->cupsWidth * DotBytes);
}
int
main(int argc,
char *argv[])
{
int fd;
cups_raster_t *ras;
cups_page_header_t header;
ppd_file_t *ppd;
int page;
int y;
setbuf(stderr, NULL);
if (argc < 6 || argc > 7)
{
fputs("ERROR: rastertoepson 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;
OutputLine(&header);
}
EndPage(&header);
}
Shutdown();
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);
}