]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - filter/raster.c
Changes to eliminate warnings from new Clang.
[thirdparty/cups.git] / filter / raster.c
index c3247da812a1575f7141c940393f9eb6d624835b..e41358270fb93ac10e177abcf64805ad40466d37 100644 (file)
@@ -50,6 +50,9 @@ struct _cups_raster_s                 /**** Raster stream data ****/
                        *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 */
 };
 
 
@@ -61,7 +64,7 @@ static ssize_t        cups_raster_io(cups_raster_t *r, unsigned char *buf, size_t bytes
 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);
@@ -92,6 +95,246 @@ cupsRasterClose(cups_raster_t *r)   /* I - Stream to close */
 }
 
 
+/*
+ * '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.
  *
@@ -193,7 +436,7 @@ cupsRasterOpenIO(
         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
   {
@@ -287,6 +530,8 @@ cupsRasterReadHeader2(
   * Get the raster header...
   */
 
+  DEBUG_printf(("cupsRasterReadHeader2(r=%p, h=%p)", (void *)r, (void *)h));
+
   if (!cups_raster_read_header(r))
   {
     memset(h, 0, sizeof(cups_page_header2_t));
@@ -325,9 +570,16 @@ cupsRasterReadPixels(cups_raster_t *r,     /* I - Raster stream */
   unsigned     count;                  /* Repetition count */
 
 
+  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)
+  {
+    DEBUG_puts("1cupsRasterReadPixels: Returning 0.");
     return (0);
+  }
+
+  DEBUG_printf(("1cupsRasterReadPixels: compressed=%d, remaining=%u", r->compressed, r->remaining));
 
   if (!r->compressed)
   {
@@ -338,7 +590,10 @@ cupsRasterReadPixels(cups_raster_t *r,     /* I - Raster stream */
     r->remaining -= len / r->header.cupsBytesPerLine;
 
     if (cups_raster_io(r, p, len) < (ssize_t)len)
+    {
+      DEBUG_puts("1cupsRasterReadPixels: Read error, returning 0.");
       return (0);
+    }
 
    /*
     * Swap bytes as needed...
@@ -354,6 +609,8 @@ cupsRasterReadPixels(cups_raster_t *r,      /* I - Raster stream */
     * Return...
     */
 
+    DEBUG_printf(("1cupsRasterReadPixels: Returning %u", len));
+
     return (len);
   }
 
@@ -382,7 +639,10 @@ cupsRasterReadPixels(cups_raster_t *r,     /* I - Raster stream */
       */
 
       if (!cups_raster_read(r, &byte, 1))
+      {
+       DEBUG_puts("1cupsRasterReadPixels: Read error, returning 0.");
        return (0);
+      }
 
       r->count = (unsigned)byte + 1;
 
@@ -399,7 +659,10 @@ cupsRasterReadPixels(cups_raster_t *r,     /* I - Raster stream */
        */
 
         if (!cups_raster_read(r, &byte, 1))
+       {
+         DEBUG_puts("1cupsRasterReadPixels: Read error, returning 0.");
          return (0);
+       }
 
        if (byte & 128)
        {
@@ -413,7 +676,10 @@ cupsRasterReadPixels(cups_raster_t *r,     /* I - Raster stream */
            count = (unsigned)bytes;
 
           if (!cups_raster_read(r, temp, count))
+         {
+           DEBUG_puts("1cupsRasterReadPixels: Read error, returning 0.");
            return (0);
+         }
 
          temp  += count;
          bytes -= count;
@@ -434,7 +700,10 @@ cupsRasterReadPixels(cups_raster_t *r,     /* I - Raster stream */
          bytes -= count;
 
           if (!cups_raster_read(r, temp, r->bpp))
+         {
+           DEBUG_puts("1cupsRasterReadPixels: Read error, returning 0.");
            return (0);
+         }
 
          temp  += r->bpp;
          count -= r->bpp;
@@ -506,6 +775,8 @@ cupsRasterReadPixels(cups_raster_t *r,      /* I - Raster stream */
     p         += bytes;
   }
 
+  DEBUG_printf(("1cupsRasterReadPixels: Returning %u", len));
+
   return (len);
 }
 
@@ -535,7 +806,8 @@ cupsRasterWriteHeader(
   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...
@@ -651,7 +923,8 @@ cupsRasterWriteHeader2(
 
   memcpy(&(r->header), h, sizeof(cups_page_header2_t));
 
-  cups_raster_update(r);
+  if (!cups_raster_update(r))
+    return (0);
 
  /*
   * Write the raster header...
@@ -706,14 +979,10 @@ cupsRasterWriteHeader2(
     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[3]        = htonl((unsigned)(r->header.cupsImagingBBox[0] * r->header.HWResolution[0] / 72.0));
+    fh.cupsInteger[4]        = htonl((unsigned)(r->header.cupsImagingBBox[1] * r->header.HWResolution[1] / 72.0));
+    fh.cupsInteger[5]        = htonl((unsigned)(r->header.cupsImagingBBox[2] * r->header.HWResolution[0] / 72.0));
+    fh.cupsInteger[6]        = htonl((unsigned)(r->header.cupsImagingBBox[3] * r->header.HWResolution[1] / 72.0));
     fh.cupsInteger[7]        = htonl(0xffffff);
 
     return (cups_raster_io(r, (unsigned char *)&fh, sizeof(fh)) == sizeof(fh));
@@ -741,8 +1010,7 @@ cupsRasterWritePixels(cups_raster_t *r,    /* I - Raster stream */
   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);
@@ -799,10 +1067,15 @@ cupsRasterWritePixels(cups_raster_t *r,  /* I - Raster stream */
       * 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);
   }
 
  /*
@@ -826,7 +1099,7 @@ cupsRasterWritePixels(cups_raster_t *r,    /* I - Raster stream */
 
       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;
@@ -855,10 +1128,15 @@ cupsRasterWritePixels(cups_raster_t *r,  /* I - Raster stream */
          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;
@@ -895,7 +1173,10 @@ cupsRasterWritePixels(cups_raster_t *r,   /* I - Raster stream */
        r->remaining --;
 
        if (r->remaining == 0)
-         return ((unsigned)cups_raster_write(r, r->pixels));
+       {
+         if (cups_raster_write(r, r->pixels) <= 0)
+           return (0);
+       }
       }
     }
   }
@@ -915,9 +1196,13 @@ cups_raster_read_header(
   size_t       len;                    /* Length for read/swap */
 
 
+  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...
   */
@@ -927,6 +1212,8 @@ cups_raster_read_header(
   else
     len = sizeof(cups_page_header2_t);
 
+  DEBUG_printf(("4cups_raster_read_header: len=%d", (int)len));
+
  /*
   * Read the header...
   */
@@ -934,7 +1221,10 @@ cups_raster_read_header(
   memset(&(r->header), 0, sizeof(r->header));
 
   if (cups_raster_read(r, (unsigned char *)&(r->header), len) < (ssize_t)len)
+  {
+    DEBUG_printf(("4cups_raster_read_header: EOF, r->iocount=" CUPS_LLFMT, CUPS_LLCAST r->iocount));
     return (0);
+  }
 
  /*
   * Swap bytes as needed...
@@ -946,21 +1236,19 @@ cups_raster_read_header(
                temp;                   /* Temporary copy */
 
 
-    DEBUG_puts("Swapping header bytes...");
+    DEBUG_puts("4cups_raster_read_header: 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));
+      DEBUG_printf(("4cups_raster_read_header: %08x => %08x", temp, *s));
     }
   }
 
@@ -968,9 +1256,12 @@ cups_raster_read_header(
   * 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.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);
 }
 
 
@@ -987,20 +1278,31 @@ cups_raster_io(cups_raster_t *r, /* I - Raster stream */
                total;                  /* Total bytes read/written */
 
 
-  DEBUG_printf(("4cups_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(("5cups_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);
 }
 
@@ -1019,7 +1321,7 @@ cups_raster_read(cups_raster_t *r,        /* I - Raster stream */
                total;                  /* Total bytes read */
 
 
-  DEBUG_printf(("cups_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));
@@ -1029,6 +1331,8 @@ cups_raster_read(cups_raster_t *r,        /* I - Raster stream */
   */
 
   count = (ssize_t)(2 * r->header.cupsBytesPerLine);
+  if (count < 65536)
+    count = 65536;
 
   if ((size_t)count > r->bufsize)
   {
@@ -1061,7 +1365,7 @@ cups_raster_read(cups_raster_t *r,        /* I - Raster stream */
   {
     count = (ssize_t)bytes - total;
 
-    DEBUG_printf(("count=" CUPS_LLFMT ", remaining=" CUPS_LLFMT ", buf=%p, bufptr=%p, bufend=%p...\n", 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)
     {
@@ -1077,6 +1381,10 @@ cups_raster_read(cups_raster_t *r,       /* I - Raster stream */
 
        r->bufptr = r->buffer;
        r->bufend = r->buffer + remaining;
+
+#ifdef DEBUG
+        r->iocount += (size_t)remaining;
+#endif /* DEBUG */
       }
       else
       {
@@ -1089,6 +1397,10 @@ cups_raster_read(cups_raster_t *r,       /* I - Raster stream */
        if (count <= 0)
          return (0);
 
+#ifdef DEBUG
+        r->iocount += (size_t)count;
+#endif /* DEBUG */
+
        continue;
       }
     }
@@ -1138,6 +1450,8 @@ cups_raster_read(cups_raster_t *r,        /* I - Raster stream */
     }
   }
 
+  DEBUG_printf(("6cups_raster_read: Returning %ld", (long)total));
+
   return (total);
 }
 
@@ -1147,7 +1461,7 @@ cups_raster_read(cups_raster_t *r,        /* I - Raster stream */
  *                          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 ||
@@ -1228,6 +1542,10 @@ cups_raster_update(cups_raster_t *r)     /* I - Raster stream */
           r->header.cupsNumColors = r->header.cupsColorSpace -
                                    CUPS_CSPACE_DEVICE1 + 1;
          break;
+
+      default :
+          /* Unknown color space */
+          return (0);
     }
   }
 
@@ -1240,6 +1558,9 @@ cups_raster_update(cups_raster_t *r)      /* I - Raster stream */
   else
     r->bpp = (r->header.cupsBitsPerColor + 7) / 8;
 
+  if (r->bpp == 0)
+    r->bpp = 1;
+
  /*
   * Set the number of remaining rows...
   */
@@ -1258,11 +1579,21 @@ cups_raster_update(cups_raster_t *r)    /* I - Raster stream */
     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);
 }
 
 
@@ -1284,13 +1615,16 @@ cups_raster_write(
                        count;          /* Count */
 
 
-  DEBUG_printf(("cups_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 < 65536)
+    count = 65536;
+
   if ((size_t)count > r->bufsize)
   {
     if (r->buffer)
@@ -1299,7 +1633,10 @@ cups_raster_write(
       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;
@@ -1372,6 +1709,8 @@ cups_raster_write(
     }
   }
 
+  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)));
 }
 
@@ -1396,7 +1735,12 @@ cups_read_fd(void          *ctx, /* I - File descriptor as pointer */
   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);
 }
@@ -1448,7 +1792,10 @@ cups_write_fd(void          *ctx,        /* I - File descriptor pointer */
   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);
 }