]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - filter/raster.c
Update svn:keyword properties.
[thirdparty/cups.git] / filter / raster.c
index c6d2fec3bb90d11b471f8f5cf40b1e93586e2467..801129e603e00e752543a9bfe101f1432bdda6d8 100644 (file)
@@ -1,9 +1,9 @@
 /*
- * "$Id: raster.c 7720 2008-07-11 22:46:21Z mike $"
+ * "$Id$"
  *
  *   Raster file routines for CUPS.
  *
- *   Copyright 2007-2010 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.
@@ -19,7 +19,8 @@
  * Contents:
  *
  *   cupsRasterClose()         - Close a raster stream.
- *   cupsRasterOpen()          - Open a raster stream.
+ *   cupsRasterOpen()          - Open a raster stream using a file descriptor.
+ *   cupsRasterOpenIO()        - Open a raster stream using a callback function.
  *   cupsRasterReadHeader()    - Read a raster page header and store it in a
  *                               version 1 page header structure.
  *   cupsRasterReadHeader2()   - Read a raster page header and store it in a
  *   cupsRasterWriteHeader2()  - Write a raster page header from a version 2
  *                               page header structure.
  *   cupsRasterWritePixels()   - Write raster pixels.
- *   cups_raster_read()        - Read through the raster buffer.
  *   cups_raster_read_header() - Read a raster page header.
+ *   cups_raster_read()        - Read through the raster buffer.
  *   cups_raster_update()      - Update the raster header and row count for the
  *                               current page.
- *   cups_raster_write()       - Write a row of raster data...
- *   cups_read()               - Read bytes from a file.
+ *   cups_raster_write()       - Write a row of compressed raster data...
+ *   cups_read_fd()            - Read bytes from a file.
  *   cups_swap()               - Swap bytes in raster data...
- *   cups_write()              - Write bytes to a file.
+ *   cups_write_fd()           - Write bytes to a file.
  */
 
 /*
  * Include necessary headers...
  */
 
-#include "image-private.h"
-#if defined(WIN32) || defined(__EMX__)
-#  include <io.h>
-#else
-#  include <unistd.h>
-#endif /* WIN32 || __EMX__ */
+#include <cups/raster-private.h>
+#ifdef HAVE_STDINT_H
+#  include <stdint.h>
+#endif /* HAVE_STDINT_H */
 
 
 /*
@@ -59,7 +58,8 @@
 struct _cups_raster_s                  /**** Raster stream data ****/
 {
   unsigned             sync;           /* Sync word from start of stream */
-  int                  fd;             /* File descriptor */
+  void                 *ctx;           /* File descriptor */
+  cups_raster_iocb_t   iocb;           /* IO callback */
   cups_mode_t          mode;           /* Read/write mode */
   cups_page_header2_t  header;         /* Raster header for current page */
   int                  count,          /* Current row run-length count */
@@ -73,7 +73,7 @@ struct _cups_raster_s                 /**** Raster stream data ****/
   unsigned char                *buffer,        /* Read/write buffer */
                        *bufptr,        /* Current (read) position in buffer */
                        *bufend;        /* End of current (read) buffer */
-  int                  bufsize;        /* Buffer size */
+  size_t               bufsize;        /* Buffer size */
 };
 
 
@@ -81,14 +81,16 @@ struct _cups_raster_s                       /**** Raster stream data ****/
  * 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);
 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 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 int     cups_write(int fd, const unsigned char *buf, int bytes);
+static ssize_t cups_write_fd(void *ctx, unsigned char *buf, size_t bytes);
 
 
 /*
@@ -115,21 +117,53 @@ cupsRasterClose(cups_raster_t *r) /* I - Stream to close */
 
 
 /*
- * 'cupsRasterOpen()' - Open a raster stream.
+ * 'cupsRasterOpen()' - Open a raster stream using a file descriptor.
  *
  * This function associates a raster stream with the given file descriptor.
  * For most printer driver filters, "fd" will be 0 (stdin).  For most raster
  * image processor (RIP) filters that generate raster data, "fd" will be 1
  * (stdout).
  *
- * When writing raster data, the @code CUPS_RASTER_WRITE@ or
- * @code CUPS_RASTER_WRITE_COMPRESS@ mode can be used - compressed output
- * is generally 25-50% smaller but adds a 100-300% execution time overhead.
+ * When writing raster data, the @code CUPS_RASTER_WRITE@,
+ * @code CUPS_RASTER_WRITE_COMPRESS@, or @code CUPS_RASTER_WRITE_PWG@ mode can
+ * be used - compressed and PWG output is generally 25-50% smaller but adds a
+ * 100-300% execution time overhead.
  */
 
 cups_raster_t *                                /* O - New stream */
 cupsRasterOpen(int         fd,         /* I - File descriptor */
-               cups_mode_t mode)       /* I - Mode - @code CUPS_RASTER_READ@, @code CUPS_RASTER_WRITE@, or @code CUPS_RASTER_WRITE_COMPRESSED@ */
+               cups_mode_t mode)       /* I - Mode - @code CUPS_RASTER_READ@,
+                                              @code CUPS_RASTER_WRITE@,
+                                              @code CUPS_RASTER_WRITE_COMPRESSED@,
+                                              or @code CUPS_RASTER_WRITE_PWG@ */
+{
+  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));
+}
+
+
+/*
+ * 'cupsRasterOpenIO()' - Open a raster stream using a callback function.
+ *
+ * This function associates a raster stream with the given callback function and
+ * context pointer.
+ *
+ * When writing raster data, the @code CUPS_RASTER_WRITE@,
+ * @code CUPS_RASTER_WRITE_COMPRESS@, or @code CUPS_RASTER_WRITE_PWG@ mode can
+ * be used - compressed and PWG output is generally 25-50% smaller but adds a
+ * 100-300% execution time overhead.
+ */
+
+cups_raster_t *                                /* O - New stream */
+cupsRasterOpenIO(
+    cups_raster_iocb_t iocb,           /* I - Read/write callback */
+    void               *ctx,           /* I - Context pointer for callback */
+    cups_mode_t        mode)           /* I - Mode - @code CUPS_RASTER_READ@,
+                                              @code CUPS_RASTER_WRITE@,
+                                              @code CUPS_RASTER_WRITE_COMPRESSED@,
+                                              or @code CUPS_RASTER_WRITE_PWG@ */
 {
   cups_raster_t        *r;                     /* New stream */
 
@@ -143,8 +177,9 @@ cupsRasterOpen(int         fd,              /* I - File descriptor */
     return (NULL);
   }
 
-  r->fd   = fd;
-  r->mode = mode == CUPS_RASTER_WRITE_COMPRESSED ? CUPS_RASTER_WRITE : mode;
+  r->ctx  = ctx;
+  r->iocb = iocb;
+  r->mode = mode;
 
   if (mode == CUPS_RASTER_READ)
   {
@@ -152,7 +187,8 @@ cupsRasterOpen(int         fd,              /* I - File descriptor */
     * Open for read - get sync word...
     */
 
-    if (!cups_read(r->fd, (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",
                           strerror(errno));
@@ -189,15 +225,26 @@ cupsRasterOpen(int         fd,            /* I - File descriptor */
     * Open for write - put sync word...
     */
 
-    if (mode == CUPS_RASTER_WRITE_COMPRESSED)
+    switch (mode)
     {
-      r->compressed = 1;
-      r->sync       = CUPS_RASTER_SYNCv2;
+      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;
     }
-    else
-      r->sync = CUPS_RASTER_SYNC;
 
-    if (cups_write(r->fd, (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",
@@ -235,7 +282,7 @@ cupsRasterReadHeader(
 
   if (!cups_raster_read_header(r))
     return (0);
-  
+
  /*
   * Copy the header to the user-supplied buffer...
   */
@@ -250,7 +297,7 @@ cupsRasterReadHeader(
  * '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 */
@@ -264,7 +311,7 @@ cupsRasterReadHeader2(
 
   if (!cups_raster_read_header(r))
     return (0);
-  
+
  /*
   * Copy the header to the user-supplied buffer...
   */
@@ -297,7 +344,8 @@ cupsRasterReadPixels(cups_raster_t *r,      /* I - Raster stream */
   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)
@@ -308,17 +356,17 @@ cupsRasterReadPixels(cups_raster_t *r,    /* I - Raster stream */
 
     r->remaining -= len / r->header.cupsBytesPerLine;
 
-    if (!cups_read(r->fd, p, len))
+    if (cups_raster_io(r, p, len) < (ssize_t)len)
       return (0);
 
    /*
     * Swap bytes as needed...
     */
 
-    if ((r->header.cupsBitsPerColor == 16 ||
+    if (r->swapped &&
+        (r->header.cupsBitsPerColor == 16 ||
          r->header.cupsBitsPerPixel == 12 ||
-         r->header.cupsBitsPerPixel == 16) &&
-        r->swapped)
+         r->header.cupsBitsPerPixel == 16))
       cups_swap(p, len);
 
    /*
@@ -349,7 +397,7 @@ cupsRasterReadPixels(cups_raster_t *r,      /* I - Raster stream */
        ptr = r->pixels;
 
      /*
-      * Read using a modified TIFF "packbits" compression...
+      * Read using a modified PackBits compression...
       */
 
       if (!cups_raster_read(r, &byte, 1))
@@ -459,7 +507,7 @@ cupsRasterReadPixels(cups_raster_t *r,      /* I - Raster stream */
       * Copy fragment from buffer...
       */
 
-      if ((bytes = r->pend - r->pcurrent) > remaining)
+      if ((unsigned)(bytes = (int)(r->pend - r->pcurrent)) > remaining)
         bytes = remaining;
 
       memcpy(p, r->pcurrent, bytes);
@@ -489,13 +537,13 @@ cupsRasterReadPixels(cups_raster_t *r,    /* I - Raster stream */
  *
  * @deprecated@
  */
+
 unsigned                               /* O - 1 on success, 0 on failure */
 cupsRasterWriteHeader(
     cups_raster_t      *r,             /* I - Raster stream */
     cups_page_header_t *h)             /* I - Raster page header */
 {
-  if (r == NULL || r->mode != CUPS_RASTER_WRITE)
+  if (r == NULL || r->mode == CUPS_RASTER_READ)
     return (0);
 
  /*
@@ -512,8 +560,88 @@ cupsRasterWriteHeader(
   * Write the raster header...
   */
 
-  return (cups_write(r->fd, (unsigned char *)&(r->header), sizeof(r->header))
-              > 0);
+  if (r->mode == CUPS_RASTER_WRITE_PWG)
+  {
+   /*
+    * 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));
+
+    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 (cups_raster_io(r, (unsigned char *)&(r->header), sizeof(r->header))
+               == sizeof(r->header));
 }
 
 
@@ -523,15 +651,15 @@ cupsRasterWriteHeader(
  *
  * 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 *h)            /* I - Raster page header */
 {
-  if (r == NULL || r->mode != CUPS_RASTER_WRITE)
+  if (r == NULL || r->mode == CUPS_RASTER_READ)
     return (0);
 
  /*
@@ -547,8 +675,70 @@ cupsRasterWriteHeader2(
   * Write the raster header...
   */
 
-  return (cups_write(r->fd, (unsigned char *)&(r->header), sizeof(r->header))
-              > 0);
+  if (r->mode == CUPS_RASTER_WRITE_PWG)
+  {
+   /*
+    * PWG raster data is always network byte order with most of the page header
+    * zeroed.
+    */
+
+    cups_page_header2_t        fh;             /* File page header */
+
+    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));
 }
 
 
@@ -572,18 +762,65 @@ cupsRasterWritePixels(cups_raster_t *r,   /* I - Raster stream */
   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)
+  if (r == NULL || r->mode == CUPS_RASTER_READ || r->remaining == 0)
     return (0);
 
   if (!r->compressed)
   {
    /*
-    * Without compression, just write the raster data raw...
+    * Without compression, just write the raster data raw unless the data needs
+    * to be swapped...
     */
 
     r->remaining -= len / r->header.cupsBytesPerLine;
 
-    return (cups_write(r->fd, p, len));
+    if (r->swapped &&
+        (r->header.cupsBitsPerColor == 16 ||
+         r->header.cupsBitsPerPixel == 12 ||
+         r->header.cupsBitsPerPixel == 16))
+    {
+      unsigned char    *bufptr;        /* Pointer into write buffer */
+      unsigned         count;          /* Remaining count */
+
+     /*
+      * Allocate a write buffer as needed...
+      */
+
+      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;
+      }
+
+     /*
+      * Byte swap the pixels...
+      */
+
+      for (bufptr = r->buffer, count = len; count > 1; count -= 2, bufptr += 2)
+      {
+        bufptr[1] = *p++;
+        bufptr[0] = *p++;
+      }
+
+      if (count)                       /* This should never happen... */
+        *bufptr = *p;
+
+     /*
+      * Write the byte-swapped buffer...
+      */
+
+      return (cups_raster_io(r, r->buffer, len));
+    }
+    else
+      return (cups_raster_io(r, p, len));
   }
 
  /*
@@ -596,8 +833,8 @@ cupsRasterWritePixels(cups_raster_t *r,     /* I - Raster stream */
     * 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)
     {
@@ -751,7 +988,38 @@ cups_raster_read_header(
 
   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);
 }
 
 
@@ -772,7 +1040,7 @@ cups_raster_read(cups_raster_t *r, /* I - Raster stream */
   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));
+    return (cups_raster_io(r, buf, bytes));
 
  /*
   * Allocate a read buffer as needed...
@@ -780,10 +1048,12 @@ cups_raster_read(cups_raster_t *r,       /* I - Raster stream */
 
   count = 2 * r->header.cupsBytesPerLine;
 
-  if (count > r->bufsize)
+  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)
@@ -804,7 +1074,7 @@ cups_raster_read(cups_raster_t *r, /* I - Raster stream */
   * 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)
   {
@@ -821,7 +1091,7 @@ cups_raster_read(cups_raster_t *r, /* I - Raster stream */
         * Read into the raster buffer and then copy...
        */
 
-        remaining = cups_read(r->fd, r->buffer, r->bufsize);
+        remaining = (*r->iocb)(r->ctx, r->buffer, r->bufsize);
        if (remaining <= 0)
          return (0);
 
@@ -834,7 +1104,7 @@ cups_raster_read(cups_raster_t *r, /* I - Raster stream */
         * Read directly into "buf"...
        */
 
-       count = cups_read(r->fd, buf, count);
+       count = (*r->iocb)(r->ctx, buf, count);
 
        if (count <= 0)
          return (0);
@@ -869,7 +1139,6 @@ cups_raster_read(cups_raster_t *r, /* I - Raster stream */
 
       unsigned char    *bufptr;        /* Temporary buffer pointer */
 
-
       remaining -= count;
 
       for (bufptr = r->bufptr; count > 0; count --, total ++)
@@ -1042,7 +1311,7 @@ cups_raster_write(
   */
 
   count = r->header.cupsBytesPerLine * 2;
-  if (count > r->bufsize)
+  if ((size_t)count > r->bufsize)
   {
     if (r->buffer)
       wptr = realloc(r->buffer, count);
@@ -1067,7 +1336,7 @@ cups_raster_write(
   *wptr++ = r->count - 1;
 
  /*
-  * Write using a modified TIFF "packbits" compression...
+  * Write using a modified PackBits compression...
   */
 
   for (ptr = pixels; ptr < pend;)
@@ -1105,7 +1374,7 @@ cups_raster_write(
       * 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;
 
@@ -1114,7 +1383,7 @@ cups_raster_write(
         count ++;
        ptr += bpp;
       }
+
       *wptr++ = 257 - count;
 
       count *= bpp;
@@ -1123,39 +1392,33 @@ cups_raster_write(
     }
   }
 
-  return (cups_write(r->fd, r->buffer, wptr - r->buffer));
+  return (cups_raster_io(r, r->buffer, (int)(wptr - r->buffer)));
 }
 
 
 /*
- * 'cups_read()' - Read bytes from a file.
+ * 'cups_read_fd()' - Read bytes from a file.
  */
 
-static int                             /* O - Bytes read or -1 */
-cups_read(int           fd,            /* I - File descriptor */
-          unsigned char *buf,          /* I - Buffer for read */
-         int           bytes)          /* I - Number of bytes to read */
+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 - Maximum number of bytes to read */
 {
-  int  count,                          /* Number of bytes read */
-       total;                          /* Total bytes read */
-
+  int          fd = (int)((intptr_t)ctx);
+                                       /* File descriptor */
+  ssize_t      count;                  /* Number of 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 (total);
+  return (count);
 }
 
 
@@ -1186,35 +1449,31 @@ cups_swap(unsigned char *buf,           /* I - Buffer to swap */
 
 
 /*
- * 'cups_write()' - Write bytes to a file.
+ * 'cups_write_fd()' - Write bytes to a file.
  */
 
-static int                             /* O - Bytes written or -1 */
-cups_write(int                 fd,     /* I - File descriptor */
-           const unsigned char *buf,   /* I - Bytes to write */
-          int                 bytes)   /* I - Number of bytes to write */
+static ssize_t                         /* O - Bytes written or -1 */
+cups_write_fd(void          *ctx,      /* I - File descriptor pointer */
+              unsigned char *buf,      /* I - Bytes to write */
+             size_t        bytes)      /* I - Number of bytes to write */
 {
-  int  count,                          /* Number of bytes written */
-       total;                          /* Total bytes written */
+  int          fd = (int)((intptr_t)ctx);
+                                       /* File descriptor */
+  ssize_t      count;                  /* Number of 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 (total);
+  return (count);
 }
 
 
 /*
- * End of "$Id: raster.c 7720 2008-07-11 22:46:21Z mike $".
+ * End of "$Id$".
  */