*bufptr, /* Current (read) position in buffer */
*bufend; /* End of current (read) buffer */
size_t bufsize; /* Buffer size */
+#ifdef DEBUG
+ size_t iocount; /* Number of bytes read/written */
+#endif /* DEBUG */
};
static unsigned cups_raster_read_header(cups_raster_t *r);
static ssize_t cups_raster_read(cups_raster_t *r, unsigned char *buf,
size_t bytes);
-static void cups_raster_update(cups_raster_t *r);
+static int cups_raster_update(cups_raster_t *r);
static ssize_t 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);
}
+/*
+ * 'cupsRasterInitPWGHeader()' - Initialize a page header for PWG Raster output.
+ *
+ * The "media" argument specifies the media to use.
+ *
+ * The "type" argument specifies a "pwg-raster-document-type-supported" value
+ * that controls the color space and bit depth of the raster data.
+ *
+ * The "xres" and "yres" arguments specify the raster resolution in dots per
+ * inch.
+ *
+ * The "sheet_back" argument specifies a "pwg-raster-document-sheet-back" value
+ * to apply for the back side of a page. Pass @code NULL@ for the front side.
+ *
+ * @since CUPS 2.2@
+ */
+
+int /* O - 1 on success, 0 on failure */
+cupsRasterInitPWGHeader(
+ cups_page_header2_t *h, /* I - Page header */
+ pwg_media_t *media, /* I - PWG media information */
+ const char *type, /* I - PWG raster type string */
+ int xdpi, /* I - Cross-feed direction (horizontal) resolution */
+ int ydpi, /* I - Feed direction (vertical) resolution */
+ const char *sides, /* I - IPP "sides" option value */
+ const char *sheet_back) /* I - Transform for back side or @code NULL@ for none */
+{
+ if (!h || !media || !type || xdpi <= 0 || ydpi <= 0)
+ {
+ _cupsRasterAddError("%s", strerror(EINVAL));
+ return (0);
+ }
+
+ /*
+ * Initialize the page header...
+ */
+
+ memset(h, 0, sizeof(cups_page_header2_t));
+
+ strlcpy(h->cupsPageSizeName, media->pwg, sizeof(h->cupsPageSizeName));
+
+ h->PageSize[0] = (unsigned)(72 * media->width / 2540);
+ h->PageSize[1] = (unsigned)(72 * media->length / 2540);
+
+ /* This never gets written but is needed for some applications */
+ h->cupsPageSize[0] = 72.0f * media->width / 2540.0f;
+ h->cupsPageSize[1] = 72.0f * media->length / 2540.0f;
+
+ h->ImagingBoundingBox[2] = h->PageSize[0];
+ h->ImagingBoundingBox[3] = h->PageSize[1];
+
+ h->HWResolution[0] = (unsigned)xdpi;
+ h->HWResolution[1] = (unsigned)ydpi;
+
+ h->cupsWidth = (unsigned)(media->width * xdpi / 2540);
+ h->cupsHeight = (unsigned)(media->length * ydpi / 2540);
+
+ if (h->cupsWidth > 0x00ffffff || h->cupsHeight > 0x00ffffff)
+ {
+ _cupsRasterAddError("Raster dimensions too large.");
+ return (0);
+ }
+
+ h->cupsInteger[CUPS_RASTER_PWG_ImageBoxRight] = h->cupsWidth;
+ h->cupsInteger[CUPS_RASTER_PWG_ImageBoxBottom] = h->cupsHeight;
+
+ /*
+ * Colorspace and bytes per line...
+ */
+
+ if (!strcmp(type, "adobe-rgb_8"))
+ {
+ h->cupsBitsPerColor = 8;
+ h->cupsBitsPerPixel = 24;
+ h->cupsColorSpace = CUPS_CSPACE_ADOBERGB;
+ }
+ else if (!strcmp(type, "adobe-rgb_16"))
+ {
+ h->cupsBitsPerColor = 16;
+ h->cupsBitsPerPixel = 48;
+ h->cupsColorSpace = CUPS_CSPACE_ADOBERGB;
+ }
+ else if (!strcmp(type, "black_1"))
+ {
+ h->cupsBitsPerColor = 1;
+ h->cupsBitsPerPixel = 1;
+ h->cupsColorSpace = CUPS_CSPACE_K;
+ }
+ else if (!strcmp(type, "black_8"))
+ {
+ h->cupsBitsPerColor = 8;
+ h->cupsBitsPerPixel = 8;
+ h->cupsColorSpace = CUPS_CSPACE_K;
+ }
+ else if (!strcmp(type, "black_16"))
+ {
+ h->cupsBitsPerColor = 16;
+ h->cupsBitsPerPixel = 16;
+ h->cupsColorSpace = CUPS_CSPACE_K;
+ }
+ else if (!strcmp(type, "cmyk_8"))
+ {
+ h->cupsBitsPerColor = 8;
+ h->cupsBitsPerPixel = 32;
+ h->cupsColorSpace = CUPS_CSPACE_CMYK;
+ }
+ else if (!strcmp(type, "cmyk_16"))
+ {
+ h->cupsBitsPerColor = 16;
+ h->cupsBitsPerPixel = 64;
+ h->cupsColorSpace = CUPS_CSPACE_CMYK;
+ }
+ else if (!strncmp(type, "device", 6) && type[6] >= '1' && type[6] <= '9')
+ {
+ int ncolors, bits; /* Number of colors and bits */
+
+
+ if (sscanf(type, "device%d_%d", &ncolors, &bits) != 2 || ncolors > 15 || (bits != 8 && bits != 16))
+ {
+ _cupsRasterAddError("Unsupported raster type \'%s\'.", type);
+ return (0);
+ }
+
+ h->cupsBitsPerColor = (unsigned)bits;
+ h->cupsBitsPerPixel = (unsigned)(ncolors * bits);
+ h->cupsColorSpace = (cups_cspace_t)(CUPS_CSPACE_DEVICE1 + ncolors - 1);
+ }
+ else if (!strcmp(type, "rgb_8"))
+ {
+ h->cupsBitsPerColor = 8;
+ h->cupsBitsPerPixel = 24;
+ h->cupsColorSpace = CUPS_CSPACE_RGB;
+ }
+ else if (!strcmp(type, "rgb_16"))
+ {
+ h->cupsBitsPerColor = 16;
+ h->cupsBitsPerPixel = 48;
+ h->cupsColorSpace = CUPS_CSPACE_RGB;
+ }
+ else if (!strcmp(type, "sgray_1"))
+ {
+ h->cupsBitsPerColor = 1;
+ h->cupsBitsPerPixel = 1;
+ h->cupsColorSpace = CUPS_CSPACE_SW;
+ }
+ else if (!strcmp(type, "sgray_8"))
+ {
+ h->cupsBitsPerColor = 8;
+ h->cupsBitsPerPixel = 8;
+ h->cupsColorSpace = CUPS_CSPACE_SW;
+ }
+ else if (!strcmp(type, "sgray_16"))
+ {
+ h->cupsBitsPerColor = 16;
+ h->cupsBitsPerPixel = 16;
+ h->cupsColorSpace = CUPS_CSPACE_SW;
+ }
+ else if (!strcmp(type, "srgb_8"))
+ {
+ h->cupsBitsPerColor = 8;
+ h->cupsBitsPerPixel = 24;
+ h->cupsColorSpace = CUPS_CSPACE_SRGB;
+ }
+ else if (!strcmp(type, "srgb_16"))
+ {
+ h->cupsBitsPerColor = 16;
+ h->cupsBitsPerPixel = 48;
+ h->cupsColorSpace = CUPS_CSPACE_SRGB;
+ }
+ else
+ {
+ _cupsRasterAddError("Unsupported raster type \'%s\'.", type);
+ return (0);
+ }
+
+ h->cupsColorOrder = CUPS_ORDER_CHUNKED;
+ h->cupsNumColors = h->cupsBitsPerPixel / h->cupsBitsPerColor;
+ h->cupsBytesPerLine = (h->cupsWidth * h->cupsBitsPerPixel + 7) / 8;
+
+ /*
+ * Duplex support...
+ */
+
+ h->cupsInteger[CUPS_RASTER_PWG_CrossFeedTransform] = 1;
+ h->cupsInteger[CUPS_RASTER_PWG_FeedTransform] = 1;
+
+ if (sides)
+ {
+ if (!strcmp(sides, "two-sided-long-edge"))
+ {
+ h->Duplex = 1;
+ }
+ else if (!strcmp(sides, "two-sided-short-edge"))
+ {
+ h->Duplex = 1;
+ h->Tumble = 1;
+ }
+ else if (strcmp(sides, "one-sided"))
+ {
+ _cupsRasterAddError("Unsupported sides value \'%s\'.", sides);
+ return (0);
+ }
+
+ if (sheet_back)
+ {
+ if (!strcmp(sheet_back, "flipped"))
+ {
+ if (h->Tumble)
+ h->cupsInteger[CUPS_RASTER_PWG_CrossFeedTransform] = 0xffffffffU;
+ else
+ h->cupsInteger[CUPS_RASTER_PWG_FeedTransform] = 0xffffffffU;
+ }
+ else if (!strcmp(sheet_back, "manual-tumble"))
+ {
+ if (h->Tumble)
+ {
+ h->cupsInteger[CUPS_RASTER_PWG_CrossFeedTransform] = 0xffffffffU;
+ h->cupsInteger[CUPS_RASTER_PWG_FeedTransform] = 0xffffffffU;
+ }
+ }
+ else if (!strcmp(sheet_back, "rotated"))
+ {
+ if (!h->Tumble)
+ {
+ h->cupsInteger[CUPS_RASTER_PWG_CrossFeedTransform] = 0xffffffffU;
+ h->cupsInteger[CUPS_RASTER_PWG_FeedTransform] = 0xffffffffU;
+ }
+ }
+ else if (strcmp(sheet_back, "normal"))
+ {
+ _cupsRasterAddError("Unsupported sheet_back value \'%s\'.", sheet_back);
+ return (0);
+ }
+ }
+ }
+
+ return (1);
+}
+
+
/*
* 'cupsRasterOpen()' - Open a raster stream using a file descriptor.
*
r->sync == CUPS_RASTER_REVSYNCv2)
r->swapped = 1;
- DEBUG_printf(("r->swapped=%d, r->sync=%08x\n", r->swapped, r->sync));
+ DEBUG_printf(("1cupsRasterOpenIO: r->swapped=%d, r->sync=%08x\n", r->swapped, r->sync));
}
else
{
* Get the raster header...
*/
- DEBUG_printf(("cupsRasterReadHeader2(r=%p, h=%p)", r, h));
+ DEBUG_printf(("cupsRasterReadHeader2(r=%p, h=%p)", (void *)r, (void *)h));
if (!cups_raster_read_header(r))
{
unsigned count; /* Repetition count */
- DEBUG_printf(("cupsRasterReadPixels(r=%p, p=%p, len=%u)", r, p, len));
+ DEBUG_printf(("cupsRasterReadPixels(r=%p, p=%p, len=%u)", (void *)r, (void *)p, len));
if (r == NULL || r->mode != CUPS_RASTER_READ || r->remaining == 0 ||
r->header.cupsBytesPerLine == 0)
memset(&(r->header), 0, sizeof(r->header));
memcpy(&(r->header), h, sizeof(cups_page_header_t));
- cups_raster_update(r);
+ if (!cups_raster_update(r))
+ return (0);
/*
* Write the raster header...
memcpy(&(r->header), h, sizeof(cups_page_header2_t));
- cups_raster_update(r);
+ if (!cups_raster_update(r))
+ return (0);
/*
* Write the raster header...
unsigned remaining; /* Bytes remaining */
- DEBUG_printf(("cupsRasterWritePixels(r=%p, p=%p, len=%u), remaining=%u\n",
- r, p, len, r->remaining));
+ DEBUG_printf(("cupsRasterWritePixels(r=%p, p=%p, len=%u), remaining=%u", (void *)r, (void *)p, len, r->remaining));
if (r == NULL || r->mode == CUPS_RASTER_READ || r->remaining == 0)
return (0);
* Write the byte-swapped buffer...
*/
- return ((unsigned)cups_raster_io(r, r->buffer, len));
+ bytes = cups_raster_io(r, r->buffer, len);
}
else
- return ((unsigned)cups_raster_io(r, p, len));
+ bytes = cups_raster_io(r, p, len);
+
+ if (bytes < len)
+ return (0);
+ else
+ return (len);
}
/*
if (memcmp(p, r->pcurrent, (size_t)bytes))
{
- if (!cups_raster_write(r, r->pixels))
+ if (cups_raster_write(r, r->pixels) <= 0)
return (0);
r->count = 0;
r->remaining --;
if (r->remaining == 0)
- return ((unsigned)cups_raster_write(r, r->pixels));
+ {
+ if (cups_raster_write(r, r->pixels) <= 0)
+ return (0);
+ else
+ return (len);
+ }
else if (r->count == 256)
{
- if (cups_raster_write(r, r->pixels) == 0)
+ if (cups_raster_write(r, r->pixels) <= 0)
return (0);
r->count = 0;
r->remaining --;
if (r->remaining == 0)
- return ((unsigned)cups_raster_write(r, r->pixels));
+ {
+ if (cups_raster_write(r, r->pixels) <= 0)
+ return (0);
+ }
}
}
}
size_t len; /* Length for read/swap */
- DEBUG_printf(("3cups_raster_read_header(r=%p), r->mode=%d", r, r ? r->mode : 0));
+ DEBUG_printf(("3cups_raster_read_header(r=%p), r->mode=%d", (void *)r, r ? r->mode : 0));
if (r == NULL || r->mode != CUPS_RASTER_READ)
return (0);
+ DEBUG_printf(("4cups_raster_read_header: r->iocount=" CUPS_LLFMT, CUPS_LLCAST r->iocount));
+
/*
* Get the length of the raster header...
*/
if (cups_raster_read(r, (unsigned char *)&(r->header), len) < (ssize_t)len)
{
- DEBUG_puts("4cups_raster_read_header: EOF");
+ DEBUG_printf(("4cups_raster_read_header: EOF, r->iocount=" CUPS_LLFMT, CUPS_LLCAST r->iocount));
return (0);
}
* Update the header and row count...
*/
- cups_raster_update(r);
+ if (!cups_raster_update(r))
+ return (0);
DEBUG_printf(("4cups_raster_read_header: cupsBitsPerPixel=%u, cupsBitsPerColor=%u, cupsBytesPerLine=%u, cupsWidth=%u, cupsHeight=%u, r->bpp=%d", r->header.cupsBitsPerPixel, r->header.cupsBitsPerColor, r->header.cupsBytesPerLine, r->header.cupsWidth, r->header.cupsHeight, r->bpp));
- return (r->header.cupsBitsPerPixel != 0 && r->header.cupsBitsPerColor != 0 && r->header.cupsBytesPerLine != 0 && r->header.cupsHeight != 0 && (r->header.cupsBytesPerLine % r->bpp) == 0);
+ return (r->header.cupsBitsPerPixel > 0 && r->header.cupsBitsPerPixel <= 240 && r->header.cupsBitsPerColor > 0 && r->header.cupsBitsPerColor <= 16 && r->header.cupsBytesPerLine > 0 && r->header.cupsBytesPerLine <= 0x7fffffff && r->header.cupsHeight != 0 && (r->header.cupsBytesPerLine % r->bpp) == 0);
}
total; /* Total bytes read/written */
- DEBUG_printf(("5cups_raster_io(r=%p, buf=%p, bytes=" CUPS_LLFMT ")", r, buf, CUPS_LLCAST bytes));
+ DEBUG_printf(("5cups_raster_io(r=%p, buf=%p, bytes=" CUPS_LLFMT ")", (void *)r, (void *)buf, CUPS_LLCAST bytes));
for (total = 0; total < (ssize_t)bytes; total += count, buf += count)
{
count = (*r->iocb)(r->ctx, buf, bytes - (size_t)total);
- DEBUG_printf(("6cups_raster_io: count=%d, total=%d", (int)count,
- (int)total));
+ DEBUG_printf(("6cups_raster_io: count=%d, total=%d", (int)count, (int)total));
if (count == 0)
+ {
+ DEBUG_puts("6cups_raster_io: Returning 0.");
return (0);
+ }
else if (count < 0)
+ {
+ DEBUG_puts("6cups_raster_io: Returning -1 on error.");
return (-1);
+ }
+
+#ifdef DEBUG
+ r->iocount += (size_t)count;
+#endif /* DEBUG */
}
+ DEBUG_printf(("6cups_raster_io: Returning " CUPS_LLFMT ".", CUPS_LLCAST total));
+
return (total);
}
total; /* Total bytes read */
- DEBUG_printf(("5cups_raster_read(r=%p, buf=%p, bytes=" CUPS_LLFMT ")\n", r, buf, CUPS_LLCAST bytes));
+ DEBUG_printf(("5cups_raster_read(r=%p, buf=%p, bytes=" CUPS_LLFMT ")", (void *)r, (void *)buf, CUPS_LLCAST bytes));
if (!r->compressed)
return (cups_raster_io(r, buf, bytes));
*/
count = (ssize_t)(2 * r->header.cupsBytesPerLine);
+ if (count < 65536)
+ count = 65536;
if ((size_t)count > r->bufsize)
{
{
count = (ssize_t)bytes - total;
- DEBUG_printf(("6cups_raster_read: count=" CUPS_LLFMT ", remaining=" CUPS_LLFMT ", buf=%p, bufptr=%p, bufend=%p", CUPS_LLCAST count, CUPS_LLCAST remaining, buf, r->bufptr, r->bufend));
+ DEBUG_printf(("6cups_raster_read: count=" CUPS_LLFMT ", remaining=" CUPS_LLFMT ", buf=%p, bufptr=%p, bufend=%p", CUPS_LLCAST count, CUPS_LLCAST remaining, (void *)buf, (void *)r->bufptr, (void *)r->bufend));
if (remaining == 0)
{
r->bufptr = r->buffer;
r->bufend = r->buffer + remaining;
+
+#ifdef DEBUG
+ r->iocount += (size_t)remaining;
+#endif /* DEBUG */
}
else
{
if (count <= 0)
return (0);
+#ifdef DEBUG
+ r->iocount += (size_t)count;
+#endif /* DEBUG */
+
continue;
}
}
* current page.
*/
-static void
+static int /* O - 1 on success, 0 on failure */
cups_raster_update(cups_raster_t *r) /* I - Raster stream */
{
if (r->sync == CUPS_RASTER_SYNCv1 || r->sync == CUPS_RASTER_REVSYNCv1 ||
r->header.cupsNumColors = r->header.cupsColorSpace -
CUPS_CSPACE_DEVICE1 + 1;
break;
+
+ default :
+ /* Unknown color space */
+ return (0);
}
}
if (r->pixels != NULL)
free(r->pixels);
- r->pixels = calloc(r->header.cupsBytesPerLine, 1);
+ if ((r->pixels = calloc(r->header.cupsBytesPerLine, 1)) == NULL)
+ {
+ r->pcurrent = NULL;
+ r->pend = NULL;
+ r->count = 0;
+
+ return (0);
+ }
+
r->pcurrent = r->pixels;
r->pend = r->pixels + r->header.cupsBytesPerLine;
r->count = 0;
}
+
+ return (1);
}
count; /* Count */
- DEBUG_printf(("3cups_raster_write(r=%p, pixels=%p)\n", r, pixels));
+ DEBUG_printf(("3cups_raster_write(r=%p, pixels=%p)", (void *)r, (void *)pixels));
/*
* Allocate a write buffer as needed...
*/
count = r->header.cupsBytesPerLine * 2;
- if (count < 3)
- count = 3;
+ if (count < 65536)
+ count = 65536;
if ((size_t)count > r->bufsize)
{
wptr = malloc(count);
if (!wptr)
+ {
+ DEBUG_printf(("4cups_raster_write: Unable to allocate " CUPS_LLFMT " bytes for raster buffer: %s", CUPS_LLCAST count, strerror(errno)));
return (-1);
+ }
r->buffer = wptr;
r->bufsize = count;
}
}
+ DEBUG_printf(("4cups_raster_write: Writing " CUPS_LLFMT " bytes.", CUPS_LLCAST (wptr - r->buffer)));
+
return (cups_raster_io(r, r->buffer, (size_t)(wptr - r->buffer)));
}
while ((count = read(fd, buf, bytes)) < 0)
#endif /* WIN32 */
if (errno != EINTR && errno != EAGAIN)
+ {
+ DEBUG_printf(("4cups_read_fd: %s", strerror(errno)));
return (-1);
+ }
+
+ DEBUG_printf(("4cups_read_fd: Returning %d bytes.", (int)count));
return (count);
}
while ((count = write(fd, buf, bytes)) < 0)
#endif /* WIN32 */
if (errno != EINTR && errno != EAGAIN)
+ {
+ DEBUG_printf(("4cups_write_fd: %s", strerror(errno)));
return (-1);
+ }
return (count);
}