/*
- * "$Id: raster.c 7720 2008-07-11 22:46:21Z mike $"
+ * "$Id$"
*
* Raster file routines for CUPS.
*
- * Copyright 2007-2011 by Apple Inc.
+ * Copyright 2007-2012 by Apple Inc.
* Copyright 1997-2006 by Easy Software Products.
*
* This file is part of the CUPS Imaging library.
* Include necessary headers...
*/
-#include "image-private.h"
-#if defined(WIN32) || defined(__EMX__)
-# include <io.h>
-# include <winsock2.h> /* for htonl() definition */
-#else
-# include <unistd.h>
-#endif /* WIN32 || __EMX__ */
+#include <cups/raster-private.h>
+#ifdef HAVE_STDINT_H
+# include <stdint.h>
+#endif /* HAVE_STDINT_H */
/*
* Local functions...
*/
+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);
* Open for read - get sync word...
*/
- if ((*r->iocb)(r->ctx, (unsigned char *)&(r->sync), sizeof(r->sync)) !=
+ 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",
break;
}
- if ((*r->iocb)(r->ctx, (unsigned char *)&(r->sync), sizeof(r->sync))
+ if (cups_raster_io(r, (unsigned char *)&(r->sync), sizeof(r->sync))
< sizeof(r->sync))
{
_cupsRasterAddError("Unable to write raster stream header: %s\n",
if (!cups_raster_read_header(r))
return (0);
-
+
/*
* Copy the header to the user-supplied buffer...
*/
* 'cupsRasterReadHeader2()' - Read a raster page header and store it in a
* version 2 page header structure.
*
- * @since CUPS 1.2/Mac OS X 10.5@
+ * @since CUPS 1.2/OS X 10.5@
*/
unsigned /* O - 1 on success, 0 on failure/end-of-file */
if (!cups_raster_read_header(r))
return (0);
-
+
/*
* Copy the header to the user-supplied buffer...
*/
int count; /* Repetition count */
- if (r == NULL || r->mode != CUPS_RASTER_READ || r->remaining == 0)
+ 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 ((*r->iocb)(r->ctx, p, len) < (ssize_t)len)
+ if (cups_raster_io(r, p, len) < (ssize_t)len)
return (0);
/*
* Copy fragment from buffer...
*/
- if ((unsigned)(bytes = r->pend - r->pcurrent) > remaining)
+ if ((unsigned)(bytes = (int)(r->pend - r->pcurrent)) > remaining)
bytes = remaining;
memcpy(p, r->pcurrent, bytes);
*
* @deprecated@
*/
-
+
unsigned /* O - 1 on success, 0 on failure */
cupsRasterWriteHeader(
cups_raster_t *r, /* I - Raster stream */
if (r->mode == CUPS_RASTER_WRITE_PWG)
{
/*
- * PWG raster data is always network byte order with most of the page header
+ * PWG raster data is always network byte order with much of the page header
* zeroed.
*/
cups_page_header2_t fh; /* File page header */
memset(&fh, 0, sizeof(fh));
- fh.HWResolution[0] = htonl(r->header.HWResolution[0]);
- fh.HWResolution[1] = htonl(r->header.HWResolution[1]);
- 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);
-
- return ((*r->iocb)(r->ctx, (unsigned char *)&fh, sizeof(fh)) == sizeof(fh));
+
+ strlcpy(fh.MediaClass, "PwgRaster", sizeof(fh.MediaClass));
+ /* PwgRaster */
+ 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));
+ /* PrintContentType */
+
+ 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]);
+ /* TotalPageCount */
+ fh.cupsInteger[1] = htonl(r->header.cupsInteger[1]);
+ /* CrossFeedTransform */
+ fh.cupsInteger[2] = htonl(r->header.cupsInteger[2]);
+ /* FeedTransform */
+ fh.cupsInteger[3] = htonl(r->header.cupsInteger[3]);
+ /* ImageBoxLeft */
+ fh.cupsInteger[4] = htonl(r->header.cupsInteger[4]);
+ /* ImageBoxTop */
+ fh.cupsInteger[5] = htonl(r->header.cupsInteger[5]);
+ /* ImageBoxRight */
+ fh.cupsInteger[6] = htonl(r->header.cupsInteger[6]);
+ /* ImageBoxBottom */
+ fh.cupsInteger[7] = htonl(r->header.cupsInteger[7]);
+ /* BlackPrimary */
+ fh.cupsInteger[8] = htonl(r->header.cupsInteger[8]);
+ /* PrintQuality */
+ fh.cupsInteger[14] = htonl(r->header.cupsInteger[14]);
+ /* VendorIdentifier */
+ fh.cupsInteger[15] = htonl(r->header.cupsInteger[15]);
+ /* VendorLength */
+
+ memcpy(fh.cupsReal, r->header.cupsReal,
+ sizeof(fh.cupsReal) + sizeof(fh.cupsString));
+ /* VendorData */
+
+ 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 ((*r->iocb)(r->ctx, (unsigned char *)&(r->header), sizeof(r->header))
+ return (cups_raster_io(r, (unsigned char *)&(r->header), sizeof(r->header))
== sizeof(r->header));
}
*
* The page header can be initialized using @link cupsRasterInterpretPPD@.
*
- * @since CUPS 1.2/Mac OS X 10.5@
+ * @since CUPS 1.2/OS X 10.5@
*/
-
+
unsigned /* O - 1 on success, 0 on failure */
cupsRasterWriteHeader2(
cups_raster_t *r, /* I - Raster stream */
cups_page_header2_t fh; /* File page header */
memset(&fh, 0, sizeof(fh));
- fh.HWResolution[0] = htonl(r->header.HWResolution[0]);
- fh.HWResolution[1] = htonl(r->header.HWResolution[1]);
- 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);
-
- return ((*r->iocb)(r->ctx, (unsigned char *)&fh, sizeof(fh)) == 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 ((*r->iocb)(r->ctx, (unsigned char *)&(r->header), sizeof(r->header))
+ return (cups_raster_io(r, (unsigned char *)&(r->header), sizeof(r->header))
== sizeof(r->header));
}
* Write the byte-swapped buffer...
*/
- return ((*r->iocb)(r->ctx, r->buffer, len));
+ return (cups_raster_io(r, r->buffer, len));
}
else
- return ((*r->iocb)(r->ctx, p, len));
+ return (cups_raster_io(r, p, len));
}
/*
* Figure out the number of remaining bytes on the current line...
*/
- if ((bytes = remaining) > (r->pend - r->pcurrent))
- bytes = r->pend - r->pcurrent;
+ if ((bytes = remaining) > (int)(r->pend - r->pcurrent))
+ bytes = (int)(r->pend - r->pcurrent);
if (r->count > 0)
{
cups_raster_update(r);
- return (1);
+ return (r->header.cupsBytesPerLine != 0 && r->header.cupsHeight != 0);
+}
+
+
+/*
+ * 'cups_raster_io()' - Read/write bytes from a context, handling interruptions.
+ */
+
+static int /* O - Bytes read or -1 */
+cups_raster_io(cups_raster_t *r, /* I - Raster stream */
+ unsigned char *buf, /* I - Buffer for read/write */
+ int bytes) /* I - Number of bytes to read/write */
+{
+ ssize_t count; /* Number of bytes read/written */
+ size_t total; /* Total bytes read/written */
+
+
+ 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);
}
DEBUG_printf(("cups_raster_read(r=%p, buf=%p, bytes=%d)\n", r, buf, bytes));
if (!r->compressed)
- return ((*r->iocb)(r->ctx, buf, bytes));
+ return (cups_raster_io(r, buf, bytes));
/*
* Allocate a read buffer as needed...
if ((size_t)count > r->bufsize)
{
- int offset = r->bufptr - r->buffer; /* Offset to current start of buffer */
- int end = r->bufend - r->buffer; /* Offset to current end of buffer */
+ int offset = (int)(r->bufptr - r->buffer);
+ /* Offset to current start of buffer */
+ int end = (int)(r->bufend - r->buffer);
+ /* Offset to current end of buffer */
unsigned char *rptr; /* Pointer in read buffer */
if (r->buffer)
* Loop until we have read everything...
*/
- for (total = 0, remaining = r->bufend - r->bufptr;
+ for (total = 0, remaining = (int)(r->bufend - r->bufptr);
total < bytes;
total += count, buf += count)
{
unsigned char *bufptr; /* Temporary buffer pointer */
-
remaining -= count;
for (bufptr = r->bufptr; count > 0; count --, total ++)
*wptr++ = r->count - 1;
/*
- * Write using a modified TIFF "packbits" compression...
+ * Write using a modified PackBits compression...
*/
for (ptr = pixels; ptr < pend;)
* Encode a sequence of non-repeating pixels...
*/
- for (count = 1; count < 127 && ptr < plast; count ++, ptr += bpp)
+ for (count = 1; count < 128 && ptr < plast; count ++, ptr += bpp)
if (!memcmp(ptr, ptr + bpp, bpp))
break;
count ++;
ptr += bpp;
}
-
+
*wptr++ = 257 - count;
count *= bpp;
}
}
- return ((*r->iocb)(r->ctx, r->buffer, wptr - r->buffer));
+ return (cups_raster_io(r, r->buffer, (int)(wptr - r->buffer)));
}
static ssize_t /* O - Bytes read or -1 */
cups_read_fd(void *ctx, /* I - File descriptor as pointer */
unsigned char *buf, /* I - Buffer for read */
- size_t bytes) /* I - Number of bytes to read */
+ size_t bytes) /* I - Maximum number of bytes to read */
{
int fd = (int)((intptr_t)ctx);
/* File descriptor */
ssize_t count; /* Number of bytes read */
- size_t total; /* Total bytes read */
-
- 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);
- }
- }
+#ifdef WIN32 /* Sigh */
+ while ((count = read(fd, buf, (unsigned)bytes)) < 0)
+#else
+ while ((count = read(fd, buf, bytes)) < 0)
+#endif /* WIN32 */
+ if (errno != EINTR && errno != EAGAIN)
+ return (-1);
- return ((ssize_t)total);
+ return (count);
}
int fd = (int)((intptr_t)ctx);
/* File descriptor */
ssize_t count; /* Number of bytes written */
- size_t total; /* Total bytes written */
- 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);
- }
- }
+#ifdef WIN32 /* Sigh */
+ while ((count = write(fd, buf, (unsigned)bytes)) < 0)
+#else
+ while ((count = write(fd, buf, bytes)) < 0)
+#endif /* WIN32 */
+ if (errno != EINTR && errno != EAGAIN)
+ return (-1);
- return ((ssize_t)total);
+ return (count);
}
/*
- * End of "$Id: raster.c 7720 2008-07-11 22:46:21Z mike $".
+ * End of "$Id$".
*/