#include <cups/raster-private.h>
#ifdef HAVE_STDINT_H
# include <stdint.h>
#endif
struct _cups_raster_s
{
unsigned sync;
void *ctx;
cups_raster_iocb_t iocb;
cups_mode_t mode;
cups_page_header2_t header;
int count,
remaining,
bpp;
unsigned char *pixels,
*pend,
*pcurrent;
int compressed,
swapped;
unsigned char *buffer,
*bufptr,
*bufend;
size_t bufsize;
};
static int cups_raster_io(cups_raster_t *r, unsigned char *buf, int bytes);
static unsigned cups_raster_read_header(cups_raster_t *r);
static int cups_raster_read(cups_raster_t *r, unsigned char *buf,
int bytes);
static void cups_raster_update(cups_raster_t *r);
static int cups_raster_write(cups_raster_t *r,
const unsigned char *pixels);
static ssize_t cups_read_fd(void *ctx, unsigned char *buf, size_t bytes);
static void cups_swap(unsigned char *buf, int bytes);
static ssize_t cups_write_fd(void *ctx, unsigned char *buf, size_t bytes);
void
cupsRasterClose(cups_raster_t *r)
{
if (r != NULL)
{
if (r->buffer)
free(r->buffer);
if (r->pixels)
free(r->pixels);
free(r);
}
}
cups_raster_t *
cupsRasterOpen(int fd,
cups_mode_t mode)
{
if (mode == CUPS_RASTER_READ)
return (cupsRasterOpenIO(cups_read_fd, (void *)((intptr_t)fd), mode));
else
return (cupsRasterOpenIO(cups_write_fd, (void *)((intptr_t)fd), mode));
}
cups_raster_t *
cupsRasterOpenIO(
cups_raster_iocb_t iocb,
void *ctx,
cups_mode_t mode)
{
cups_raster_t *r;
_cupsRasterClearError();
if ((r = calloc(sizeof(cups_raster_t), 1)) == NULL)
{
_cupsRasterAddError("Unable to allocate memory for raster stream: %s\n",
strerror(errno));
return (NULL);
}
r->ctx = ctx;
r->iocb = iocb;
r->mode = mode;
if (mode == CUPS_RASTER_READ)
{
if (cups_raster_io(r, (unsigned char *)&(r->sync), sizeof(r->sync)) !=
sizeof(r->sync))
{
_cupsRasterAddError("Unable to read header from raster stream: %s\n",
strerror(errno));
free(r);
return (NULL);
}
if (r->sync != CUPS_RASTER_SYNC &&
r->sync != CUPS_RASTER_REVSYNC &&
r->sync != CUPS_RASTER_SYNCv1 &&
r->sync != CUPS_RASTER_REVSYNCv1 &&
r->sync != CUPS_RASTER_SYNCv2 &&
r->sync != CUPS_RASTER_REVSYNCv2)
{
_cupsRasterAddError("Unknown raster format %08x!\n", r->sync);
free(r);
return (NULL);
}
if (r->sync == CUPS_RASTER_SYNCv2 ||
r->sync == CUPS_RASTER_REVSYNCv2)
r->compressed = 1;
if (r->sync == CUPS_RASTER_REVSYNC ||
r->sync == CUPS_RASTER_REVSYNCv1 ||
r->sync == CUPS_RASTER_REVSYNCv2)
r->swapped = 1;
DEBUG_printf(("r->swapped=%d, r->sync=%08x\n", r->swapped, r->sync));
}
else
{
switch (mode)
{
default :
case CUPS_RASTER_WRITE :
r->sync = CUPS_RASTER_SYNC;
break;
case CUPS_RASTER_WRITE_COMPRESSED :
r->compressed = 1;
r->sync = CUPS_RASTER_SYNCv2;
break;
case CUPS_RASTER_WRITE_PWG :
r->compressed = 1;
r->sync = htonl(CUPS_RASTER_SYNC_PWG);
r->swapped = r->sync != CUPS_RASTER_SYNC_PWG;
break;
}
if (cups_raster_io(r, (unsigned char *)&(r->sync), sizeof(r->sync))
< sizeof(r->sync))
{
_cupsRasterAddError("Unable to write raster stream header: %s\n",
strerror(errno));
free(r);
return (NULL);
}
}
return (r);
}
unsigned
cupsRasterReadHeader(
cups_raster_t *r,
cups_page_header_t *h)
{
if (!cups_raster_read_header(r))
return (0);
memcpy(h, &(r->header), sizeof(cups_page_header_t));
return (1);
}
unsigned
cupsRasterReadHeader2(
cups_raster_t *r,
cups_page_header2_t *h)
{
if (!cups_raster_read_header(r))
return (0);
memcpy(h, &(r->header), sizeof(cups_page_header2_t));
return (1);
}
unsigned
cupsRasterReadPixels(cups_raster_t *r,
unsigned char *p,
unsigned len)
{
int bytes;
unsigned cupsBytesPerLine;
unsigned remaining;
unsigned char *ptr,
byte,
*temp;
int count;
if (r == NULL || r->mode != CUPS_RASTER_READ || r->remaining == 0 ||
r->header.cupsBytesPerLine == 0)
return (0);
if (!r->compressed)
{
r->remaining -= len / r->header.cupsBytesPerLine;
if (cups_raster_io(r, p, len) < (ssize_t)len)
return (0);
if (r->swapped &&
(r->header.cupsBitsPerColor == 16 ||
r->header.cupsBitsPerPixel == 12 ||
r->header.cupsBitsPerPixel == 16))
cups_swap(p, len);
return (len);
}
remaining = len;
cupsBytesPerLine = r->header.cupsBytesPerLine;
while (remaining > 0 && r->remaining > 0)
{
if (r->count == 0)
{
if (remaining == cupsBytesPerLine)
ptr = p;
else
ptr = r->pixels;
if (!cups_raster_read(r, &byte, 1))
return (0);
r->count = byte + 1;
if (r->count > 1)
ptr = r->pixels;
temp = ptr;
bytes = cupsBytesPerLine;
while (bytes > 0)
{
if (!cups_raster_read(r, &byte, 1))
return (0);
if (byte & 128)
{
count = (257 - byte) * r->bpp;
if (count > bytes)
count = bytes;
if (!cups_raster_read(r, temp, count))
return (0);
temp += count;
bytes -= count;
}
else
{
count = (byte + 1) * r->bpp;
if (count > bytes)
count = bytes;
if (count < r->bpp)
break;
bytes -= count;
if (!cups_raster_read(r, temp, r->bpp))
return (0);
temp += r->bpp;
count -= r->bpp;
while (count > 0)
{
memcpy(temp, temp - r->bpp, r->bpp);
temp += r->bpp;
count -= r->bpp;
}
}
}
if ((r->header.cupsBitsPerColor == 16 ||
r->header.cupsBitsPerPixel == 12 ||
r->header.cupsBitsPerPixel == 16) &&
r->swapped)
cups_swap(ptr, bytes);
if (remaining >= cupsBytesPerLine)
{
bytes = cupsBytesPerLine;
r->pcurrent = r->pixels;
r->count --;
r->remaining --;
}
else
{
bytes = remaining;
r->pcurrent = r->pixels + bytes;
}
if (ptr != p)
memcpy(p, ptr, bytes);
}
else
{
if ((unsigned)(bytes = (int)(r->pend - r->pcurrent)) > remaining)
bytes = remaining;
memcpy(p, r->pcurrent, bytes);
r->pcurrent += bytes;
if (r->pcurrent >= r->pend)
{
r->pcurrent = r->pixels;
r->count --;
r->remaining --;
}
}
remaining -= bytes;
p += bytes;
}
return (len);
}
unsigned
cupsRasterWriteHeader(
cups_raster_t *r,
cups_page_header_t *h)
{
if (r == NULL || r->mode == CUPS_RASTER_READ)
return (0);
memset(&(r->header), 0, sizeof(r->header));
memcpy(&(r->header), h, sizeof(cups_page_header_t));
cups_raster_update(r);
if (r->mode == CUPS_RASTER_WRITE_PWG)
{
cups_page_header2_t fh;
memset(&fh, 0, sizeof(fh));
strlcpy(fh.MediaClass, "PwgRaster", sizeof(fh.MediaClass));
strlcpy(fh.MediaColor, r->header.MediaColor, sizeof(fh.MediaColor));
strlcpy(fh.MediaType, r->header.MediaType, sizeof(fh.MediaType));
strlcpy(fh.OutputType, r->header.OutputType, sizeof(fh.OutputType));
fh.CutMedia = htonl(r->header.CutMedia);
fh.Duplex = htonl(r->header.Duplex);
fh.HWResolution[0] = htonl(r->header.HWResolution[0]);
fh.HWResolution[1] = htonl(r->header.HWResolution[1]);
fh.ImagingBoundingBox[0] = htonl(r->header.ImagingBoundingBox[0]);
fh.ImagingBoundingBox[1] = htonl(r->header.ImagingBoundingBox[1]);
fh.ImagingBoundingBox[2] = htonl(r->header.ImagingBoundingBox[2]);
fh.ImagingBoundingBox[3] = htonl(r->header.ImagingBoundingBox[3]);
fh.InsertSheet = htonl(r->header.InsertSheet);
fh.Jog = htonl(r->header.Jog);
fh.LeadingEdge = htonl(r->header.LeadingEdge);
fh.ManualFeed = htonl(r->header.ManualFeed);
fh.MediaPosition = htonl(r->header.MediaPosition);
fh.MediaWeight = htonl(r->header.MediaWeight);
fh.NumCopies = htonl(r->header.NumCopies);
fh.Orientation = htonl(r->header.Orientation);
fh.PageSize[0] = htonl(r->header.PageSize[0]);
fh.PageSize[1] = htonl(r->header.PageSize[1]);
fh.Tumble = htonl(r->header.Tumble);
fh.cupsWidth = htonl(r->header.cupsWidth);
fh.cupsHeight = htonl(r->header.cupsHeight);
fh.cupsBitsPerColor = htonl(r->header.cupsBitsPerColor);
fh.cupsBitsPerPixel = htonl(r->header.cupsBitsPerPixel);
fh.cupsBytesPerLine = htonl(r->header.cupsBytesPerLine);
fh.cupsColorOrder = htonl(r->header.cupsColorOrder);
fh.cupsColorSpace = htonl(r->header.cupsColorSpace);
fh.cupsNumColors = htonl(r->header.cupsNumColors);
fh.cupsInteger[0] = htonl(r->header.cupsInteger[0]);
fh.cupsInteger[1] = htonl(r->header.cupsInteger[1]);
fh.cupsInteger[2] = htonl(r->header.cupsInteger[2]);
fh.cupsInteger[3] = htonl(r->header.cupsInteger[3]);
fh.cupsInteger[4] = htonl(r->header.cupsInteger[4]);
fh.cupsInteger[5] = htonl(r->header.cupsInteger[5]);
fh.cupsInteger[6] = htonl(r->header.cupsInteger[6]);
fh.cupsInteger[7] = htonl(r->header.cupsInteger[7]);
fh.cupsInteger[8] = htonl(r->header.cupsInteger[8]);
fh.cupsInteger[14] = htonl(r->header.cupsInteger[14]);
fh.cupsInteger[15] = htonl(r->header.cupsInteger[15]);
memcpy(fh.cupsReal, r->header.cupsReal,
sizeof(fh.cupsReal) + sizeof(fh.cupsString));
strlcpy(fh.cupsRenderingIntent, r->header.cupsRenderingIntent,
sizeof(fh.cupsRenderingIntent));
strlcpy(fh.cupsPageSizeName, r->header.cupsPageSizeName,
sizeof(fh.cupsPageSizeName));
return (cups_raster_io(r, (unsigned char *)&fh, sizeof(fh)) == sizeof(fh));
}
else
return (cups_raster_io(r, (unsigned char *)&(r->header), sizeof(r->header))
== sizeof(r->header));
}
unsigned
cupsRasterWriteHeader2(
cups_raster_t *r,
cups_page_header2_t *h)
{
if (r == NULL || r->mode == CUPS_RASTER_READ)
return (0);
memcpy(&(r->header), h, sizeof(cups_page_header2_t));
cups_raster_update(r);
if (r->mode == CUPS_RASTER_WRITE_PWG)
{
cups_page_header2_t fh;
memset(&fh, 0, sizeof(fh));
strlcpy(fh.MediaClass, "PwgRaster", sizeof(fh.MediaClass));
strlcpy(fh.MediaColor, r->header.MediaColor, sizeof(fh.MediaColor));
strlcpy(fh.MediaType, r->header.MediaType, sizeof(fh.MediaType));
strlcpy(fh.OutputType, r->header.OutputType, sizeof(fh.OutputType));
strlcpy(fh.cupsRenderingIntent, r->header.cupsRenderingIntent,
sizeof(fh.cupsRenderingIntent));
strlcpy(fh.cupsPageSizeName, r->header.cupsPageSizeName,
sizeof(fh.cupsPageSizeName));
fh.CutMedia = htonl(r->header.CutMedia);
fh.Duplex = htonl(r->header.Duplex);
fh.HWResolution[0] = htonl(r->header.HWResolution[0]);
fh.HWResolution[1] = htonl(r->header.HWResolution[1]);
fh.ImagingBoundingBox[0] = htonl(r->header.ImagingBoundingBox[0]);
fh.ImagingBoundingBox[1] = htonl(r->header.ImagingBoundingBox[1]);
fh.ImagingBoundingBox[2] = htonl(r->header.ImagingBoundingBox[2]);
fh.ImagingBoundingBox[3] = htonl(r->header.ImagingBoundingBox[3]);
fh.InsertSheet = htonl(r->header.InsertSheet);
fh.Jog = htonl(r->header.Jog);
fh.LeadingEdge = htonl(r->header.LeadingEdge);
fh.ManualFeed = htonl(r->header.ManualFeed);
fh.MediaPosition = htonl(r->header.MediaPosition);
fh.MediaWeight = htonl(r->header.MediaWeight);
fh.NumCopies = htonl(r->header.NumCopies);
fh.Orientation = htonl(r->header.Orientation);
fh.PageSize[0] = htonl(r->header.PageSize[0]);
fh.PageSize[1] = htonl(r->header.PageSize[1]);
fh.Tumble = htonl(r->header.Tumble);
fh.cupsWidth = htonl(r->header.cupsWidth);
fh.cupsHeight = htonl(r->header.cupsHeight);
fh.cupsBitsPerColor = htonl(r->header.cupsBitsPerColor);
fh.cupsBitsPerPixel = htonl(r->header.cupsBitsPerPixel);
fh.cupsBytesPerLine = htonl(r->header.cupsBytesPerLine);
fh.cupsColorOrder = htonl(r->header.cupsColorOrder);
fh.cupsColorSpace = htonl(r->header.cupsColorSpace);
fh.cupsNumColors = htonl(r->header.cupsNumColors);
fh.cupsInteger[0] = htonl(r->header.cupsInteger[0]);
fh.cupsInteger[1] = htonl(r->header.cupsInteger[1]);
fh.cupsInteger[2] = htonl(r->header.cupsInteger[2]);
fh.cupsInteger[3] = htonl((unsigned)(r->header.cupsImagingBBox[0] *
r->header.HWResolution[0]));
fh.cupsInteger[4] = htonl((unsigned)(r->header.cupsImagingBBox[1] *
r->header.HWResolution[1]));
fh.cupsInteger[5] = htonl((unsigned)(r->header.cupsImagingBBox[2] *
r->header.HWResolution[0]));
fh.cupsInteger[6] = htonl((unsigned)(r->header.cupsImagingBBox[3] *
r->header.HWResolution[1]));
fh.cupsInteger[7] = htonl(0xffffff);
return (cups_raster_io(r, (unsigned char *)&fh, sizeof(fh)) == sizeof(fh));
}
else
return (cups_raster_io(r, (unsigned char *)&(r->header), sizeof(r->header))
== sizeof(r->header));
}
unsigned
cupsRasterWritePixels(cups_raster_t *r,
unsigned char *p,
unsigned len)
{
int bytes;
unsigned remaining;
DEBUG_printf(("cupsRasterWritePixels(r=%p, p=%p, len=%u), remaining=%u\n",
r, p, len, r->remaining));
if (r == NULL || r->mode == CUPS_RASTER_READ || r->remaining == 0)
return (0);
if (!r->compressed)
{
r->remaining -= len / r->header.cupsBytesPerLine;
if (r->swapped &&
(r->header.cupsBitsPerColor == 16 ||
r->header.cupsBitsPerPixel == 12 ||
r->header.cupsBitsPerPixel == 16))
{
unsigned char *bufptr;
unsigned count;
if ((size_t)len > r->bufsize)
{
if (r->buffer)
bufptr = realloc(r->buffer, len);
else
bufptr = malloc(len);
if (!bufptr)
return (0);
r->buffer = bufptr;
r->bufsize = len;
}
for (bufptr = r->buffer, count = len; count > 1; count -= 2, bufptr += 2)
{
bufptr[1] = *p++;
bufptr[0] = *p++;
}
if (count)
*bufptr = *p;
return (cups_raster_io(r, r->buffer, len));
}
else
return (cups_raster_io(r, p, len));
}
for (remaining = len; remaining > 0; remaining -= bytes, p += bytes)
{
if ((bytes = remaining) > (int)(r->pend - r->pcurrent))
bytes = (int)(r->pend - r->pcurrent);
if (r->count > 0)
{
if (memcmp(p, r->pcurrent, bytes))
{
if (!cups_raster_write(r, r->pixels))
return (0);
r->count = 0;
}
else
{
r->pcurrent += bytes;
if (r->pcurrent >= r->pend)
{
r->count ++;
r->pcurrent = r->pixels;
r->remaining --;
if (r->remaining == 0)
return (cups_raster_write(r, r->pixels));
else if (r->count == 256)
{
if (cups_raster_write(r, r->pixels) == 0)
return (0);
r->count = 0;
}
}
continue;
}
}
if (r->count == 0)
{
memcpy(r->pcurrent, p, bytes);
r->pcurrent += bytes;
if (r->pcurrent >= r->pend)
{
r->count ++;
r->pcurrent = r->pixels;
r->remaining --;
if (r->remaining == 0)
return (cups_raster_write(r, r->pixels));
}
}
}
return (len);
}
static unsigned
cups_raster_read_header(
cups_raster_t *r)
{
int len;
if (r == NULL || r->mode != CUPS_RASTER_READ)
return (0);
if (r->sync == CUPS_RASTER_SYNCv1 || r->sync == CUPS_RASTER_REVSYNCv1)
len = sizeof(cups_page_header_t);
else
len = sizeof(cups_page_header2_t);
memset(&(r->header), 0, sizeof(r->header));
if (cups_raster_read(r, (unsigned char *)&(r->header), len) < len)
return (0);
if (r->swapped)
{
unsigned *s,
temp;
DEBUG_puts("Swapping header bytes...");
for (len = 81, s = &(r->header.AdvanceDistance);
len > 0;
len --, s ++)
{
DEBUG_printf(("%08x =>", *s));
temp = *s;
*s = ((temp & 0xff) << 24) |
((temp & 0xff00) << 8) |
((temp & 0xff0000) >> 8) |
((temp & 0xff000000) >> 24);
DEBUG_printf((" %08x\n", *s));
}
}
cups_raster_update(r);
return (r->header.cupsBytesPerLine != 0 && r->header.cupsHeight != 0);
}
static int
cups_raster_io(cups_raster_t *r,
unsigned char *buf,
int bytes)
{
ssize_t count;
size_t total;
DEBUG_printf(("4cups_raster_io(r=%p, buf=%p, bytes=%d)", r, buf, bytes));
for (total = 0; total < (size_t)bytes; total += count, buf += count)
{
count = (*r->iocb)(r->ctx, buf, bytes - total);
DEBUG_printf(("5cups_raster_io: count=%d, total=%d", (int)count,
(int)total));
if (count == 0)
return (0);
else if (count < 0)
return (-1);
}
return ((int)total);
}
static int
cups_raster_read(cups_raster_t *r,
unsigned char *buf,
int bytes)
{
int count,
remaining,
total;
DEBUG_printf(("cups_raster_read(r=%p, buf=%p, bytes=%d)\n", r, buf, bytes));
if (!r->compressed)
return (cups_raster_io(r, buf, bytes));
count = 2 * r->header.cupsBytesPerLine;
if ((size_t)count > r->bufsize)
{
int offset = (int)(r->bufptr - r->buffer);
int end = (int)(r->bufend - r->buffer);
unsigned char *rptr;
if (r->buffer)
rptr = realloc(r->buffer, count);
else
rptr = malloc(count);
if (!rptr)
return (0);
r->buffer = rptr;
r->bufptr = rptr + offset;
r->bufend = rptr + end;
r->bufsize = count;
}
for (total = 0, remaining = (int)(r->bufend - r->bufptr);
total < bytes;
total += count, buf += count)
{
count = bytes - total;
DEBUG_printf(("count=%d, remaining=%d, buf=%p, bufptr=%p, bufend=%p...\n",
count, remaining, buf, r->bufptr, r->bufend));
if (remaining == 0)
{
if (count < 16)
{
remaining = (*r->iocb)(r->ctx, r->buffer, r->bufsize);
if (remaining <= 0)
return (0);
r->bufptr = r->buffer;
r->bufend = r->buffer + remaining;
}
else
{
count = (*r->iocb)(r->ctx, buf, count);
if (count <= 0)
return (0);
continue;
}
}
if (count > remaining)
count = remaining;
if (count == 1)
{
*buf = *(r->bufptr)++;
remaining --;
}
else if (count < 128)
{
unsigned char *bufptr;
remaining -= count;
for (bufptr = r->bufptr; count > 0; count --, total ++)
*buf++ = *bufptr++;
r->bufptr = bufptr;
}
else
{
memcpy(buf, r->bufptr, count);
r->bufptr += count;
remaining -= count;
}
}
return (total);
}
static void
cups_raster_update(cups_raster_t *r)
{
if (r->sync == CUPS_RASTER_SYNCv1 || r->sync == CUPS_RASTER_REVSYNCv1 ||
r->header.cupsNumColors == 0)
{
switch (r->header.cupsColorSpace)
{
case CUPS_CSPACE_W :
case CUPS_CSPACE_K :
case CUPS_CSPACE_WHITE :
case CUPS_CSPACE_GOLD :
case CUPS_CSPACE_SILVER :
case CUPS_CSPACE_SW :
r->header.cupsNumColors = 1;
break;
case CUPS_CSPACE_RGB :
case CUPS_CSPACE_CMY :
case CUPS_CSPACE_YMC :
case CUPS_CSPACE_CIEXYZ :
case CUPS_CSPACE_CIELab :
case CUPS_CSPACE_SRGB :
case CUPS_CSPACE_ADOBERGB :
case CUPS_CSPACE_ICC1 :
case CUPS_CSPACE_ICC2 :
case CUPS_CSPACE_ICC3 :
case CUPS_CSPACE_ICC4 :
case CUPS_CSPACE_ICC5 :
case CUPS_CSPACE_ICC6 :
case CUPS_CSPACE_ICC7 :
case CUPS_CSPACE_ICC8 :
case CUPS_CSPACE_ICC9 :
case CUPS_CSPACE_ICCA :
case CUPS_CSPACE_ICCB :
case CUPS_CSPACE_ICCC :
case CUPS_CSPACE_ICCD :
case CUPS_CSPACE_ICCE :
case CUPS_CSPACE_ICCF :
r->header.cupsNumColors = 3;
break;
case CUPS_CSPACE_RGBA :
case CUPS_CSPACE_RGBW :
case CUPS_CSPACE_CMYK :
case CUPS_CSPACE_YMCK :
case CUPS_CSPACE_KCMY :
case CUPS_CSPACE_GMCK :
case CUPS_CSPACE_GMCS :
r->header.cupsNumColors = 4;
break;
case CUPS_CSPACE_KCMYcm :
if (r->header.cupsBitsPerPixel < 8)
r->header.cupsNumColors = 6;
else
r->header.cupsNumColors = 4;
break;
case CUPS_CSPACE_DEVICE1 :
case CUPS_CSPACE_DEVICE2 :
case CUPS_CSPACE_DEVICE3 :
case CUPS_CSPACE_DEVICE4 :
case CUPS_CSPACE_DEVICE5 :
case CUPS_CSPACE_DEVICE6 :
case CUPS_CSPACE_DEVICE7 :
case CUPS_CSPACE_DEVICE8 :
case CUPS_CSPACE_DEVICE9 :
case CUPS_CSPACE_DEVICEA :
case CUPS_CSPACE_DEVICEB :
case CUPS_CSPACE_DEVICEC :
case CUPS_CSPACE_DEVICED :
case CUPS_CSPACE_DEVICEE :
case CUPS_CSPACE_DEVICEF :
r->header.cupsNumColors = r->header.cupsColorSpace -
CUPS_CSPACE_DEVICE1 + 1;
break;
}
}
if (r->header.cupsColorOrder == CUPS_ORDER_CHUNKED)
r->bpp = (r->header.cupsBitsPerPixel + 7) / 8;
else
r->bpp = (r->header.cupsBitsPerColor + 7) / 8;
if (r->header.cupsColorOrder == CUPS_ORDER_PLANAR)
r->remaining = r->header.cupsHeight * r->header.cupsNumColors;
else
r->remaining = r->header.cupsHeight;
if (r->compressed)
{
if (r->pixels != NULL)
free(r->pixels);
r->pixels = calloc(r->header.cupsBytesPerLine, 1);
r->pcurrent = r->pixels;
r->pend = r->pixels + r->header.cupsBytesPerLine;
r->count = 0;
}
}
static int
cups_raster_write(
cups_raster_t *r,
const unsigned char *pixels)
{
const unsigned char *start,
*ptr,
*pend,
*plast;
unsigned char *wptr;
int bpp,
count;
DEBUG_printf(("cups_raster_write(r=%p, pixels=%p)\n", r, pixels));
count = r->header.cupsBytesPerLine * 2;
if ((size_t)count > r->bufsize)
{
if (r->buffer)
wptr = realloc(r->buffer, count);
else
wptr = malloc(count);
if (!wptr)
return (-1);
r->buffer = wptr;
r->bufsize = count;
}
bpp = r->bpp;
pend = pixels + r->header.cupsBytesPerLine;
plast = pend - bpp;
wptr = r->buffer;
*wptr++ = r->count - 1;
for (ptr = pixels; ptr < pend;)
{
start = ptr;
ptr += bpp;
if (ptr == pend)
{
*wptr++ = 0;
for (count = bpp; count > 0; count --)
*wptr++ = *start++;
}
else if (!memcmp(start, ptr, bpp))
{
for (count = 2; count < 128 && ptr < plast; count ++, ptr += bpp)
if (memcmp(ptr, ptr + bpp, bpp))
break;
*wptr++ = count - 1;
for (count = bpp; count > 0; count --)
*wptr++ = *ptr++;
}
else
{
for (count = 1; count < 128 && ptr < plast; count ++, ptr += bpp)
if (!memcmp(ptr, ptr + bpp, bpp))
break;
if (ptr >= plast && count < 128)
{
count ++;
ptr += bpp;
}
*wptr++ = 257 - count;
count *= bpp;
memcpy(wptr, start, count);
wptr += count;
}
}
return (cups_raster_io(r, r->buffer, (int)(wptr - r->buffer)));
}
static ssize_t
cups_read_fd(void *ctx,
unsigned char *buf,
size_t bytes)
{
int fd = (int)((intptr_t)ctx);
ssize_t count;
#ifdef WIN32
while ((count = read(fd, buf, (unsigned)bytes)) < 0)
#else
while ((count = read(fd, buf, bytes)) < 0)
#endif
if (errno != EINTR && errno != EAGAIN)
return (-1);
return (count);
}
static void
cups_swap(unsigned char *buf,
int bytes)
{
unsigned char even, odd;
bytes /= 2;
while (bytes > 0)
{
even = buf[0];
odd = buf[1];
buf[0] = odd;
buf[1] = even;
buf += 2;
bytes --;
}
}
static ssize_t
cups_write_fd(void *ctx,
unsigned char *buf,
size_t bytes)
{
int fd = (int)((intptr_t)ctx);
ssize_t count;
#ifdef WIN32
while ((count = write(fd, buf, (unsigned)bytes)) < 0)
#else
while ((count = write(fd, buf, bytes)) < 0)
#endif
if (errno != EINTR && errno != EAGAIN)
return (-1);
return (count);
}