#include "image-private.h"
#include <cups/debug.h>
#include <stdlib.h>
#include <errno.h>
#include <cups/string.h>
#if defined(WIN32) || defined(__EMX__)
# include <io.h>
#else
# include <unistd.h>
#endif
struct _cups_raster_s
{
unsigned sync;
int fd;
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;
int bufsize;
};
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 int cups_read(int fd, unsigned char *buf, int bytes);
static void cups_swap(unsigned char *buf, int bytes);
static int cups_write(int fd, const unsigned char *buf, int 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)
{
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->fd = fd;
r->mode = mode == CUPS_RASTER_WRITE_COMPRESSED ? CUPS_RASTER_WRITE : mode;
if (mode == CUPS_RASTER_READ)
{
if (!cups_read(r->fd, (unsigned char *)&(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
{
if (mode == CUPS_RASTER_WRITE_COMPRESSED)
{
r->compressed = 1;
r->sync = CUPS_RASTER_SYNCv2;
}
else
r->sync = CUPS_RASTER_SYNC;
if (cups_write(r->fd, (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)
return (0);
if (!r->compressed)
{
r->remaining -= len / r->header.cupsBytesPerLine;
if (!cups_read(r->fd, p, len))
return (0);
if ((r->header.cupsBitsPerColor == 16 ||
r->header.cupsBitsPerPixel == 12 ||
r->header.cupsBitsPerPixel == 16) &&
r->swapped)
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 ((bytes = 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_WRITE)
return (0);
memset(&(r->header), 0, sizeof(r->header));
memcpy(&(r->header), h, sizeof(cups_page_header_t));
cups_raster_update(r);
return (cups_write(r->fd, (unsigned char *)&(r->header), sizeof(r->header))
> 0);
}
unsigned
cupsRasterWriteHeader2(
cups_raster_t *r,
cups_page_header2_t *h)
{
if (r == NULL || r->mode != CUPS_RASTER_WRITE)
return (0);
memcpy(&(r->header), h, sizeof(cups_page_header2_t));
cups_raster_update(r);
return (cups_write(r->fd, (unsigned char *)&(r->header), sizeof(r->header))
> 0);
}
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_WRITE || r->remaining == 0)
return (0);
if (!r->compressed)
{
r->remaining -= len / r->header.cupsBytesPerLine;
return (cups_write(r->fd, p, len));
}
for (remaining = len; remaining > 0; remaining -= bytes, p += bytes)
{
if ((bytes = remaining) > (r->pend - r->pcurrent))
bytes = 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 (1);
}
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_read(r->fd, buf, bytes));
count = 2 * r->header.cupsBytesPerLine;
if (count > r->bufsize)
{
int offset = r->bufptr - r->buffer;
int end = 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 = 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 = cups_read(r->fd, r->buffer, r->bufsize);
if (remaining <= 0)
return (0);
r->bufptr = r->buffer;
r->bufend = r->buffer + remaining;
}
else
{
count = cups_read(r->fd, 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 :
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_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;
}
}
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 (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 < 127 && 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_write(r->fd, r->buffer, wptr - r->buffer));
}
static int
cups_read(int fd,
unsigned char *buf,
int bytes)
{
int count,
total;
for (total = 0; total < bytes; total += count, buf += count)
{
count = read(fd, buf, bytes - total);
if (count == 0)
return (0);
else if (count < 0)
{
if (errno == EINTR)
count = 0;
else
return (-1);
}
}
return (total);
}
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 int
cups_write(int fd,
const unsigned char *buf,
int bytes)
{
int count,
total;
for (total = 0; total < bytes; total += count, buf += count)
{
count = write(fd, buf, bytes - total);
if (count < 0)
{
if (errno == EINTR)
count = 0;
else
return (-1);
}
}
return (total);
}