]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - filter/rastertopwg.c
Fix source file header text duplication text duplication.
[thirdparty/cups.git] / filter / rastertopwg.c
index 622aa605ab0985f2e5f6cf61eaeadb5e87374be4..5026f565432f8aab306168c5e639fb2cb1d546ce 100644 (file)
@@ -1,21 +1,15 @@
 /*
- * "$Id$"
+ * CUPS raster to PWG raster format filter for CUPS.
  *
- *   CUPS raster to PWG raster format filter for CUPS.
+ * Copyright 2011, 2014-2016 Apple Inc.
  *
- *   Copyright 2011 Apple Inc.
+ * These coded instructions, statements, and computer programs are the
+ * property of Apple Inc. and are protected by Federal copyright law.
+ * Distribution and use rights are outlined in the file "LICENSE.txt"
+ * which should have been included with this file.  If this file is
+ * missing or damaged, see the license at "http://www.cups.org/".
  *
- *   These coded instructions, statements, and computer programs are the
- *   property of Apple Inc. and are protected by Federal copyright law.
- *   Distribution and use rights are outlined in the file "LICENSE.txt"
- *   which should have been included with this file.  If this file is
- *   file is missing or damaged, see the license at "http://www.cups.org/".
- *
- *   This file is subject to the Apple OS-Developed Software exception.
- *
- * Contents:
- *
- *   main() - Main entry for filter.
+ * This file is subject to the Apple OS-Developed Software exception.
  */
 
 /*
@@ -23,6 +17,7 @@
  */
 
 #include <cups/cups-private.h>
+#include <cups/ppd-private.h>
 #include <cups/raster.h>
 #include <unistd.h>
 #include <fcntl.h>
@@ -36,14 +31,16 @@ int                                 /* O - Exit status */
 main(int  argc,                                /* I - Number of command-line args */
      char *argv[])                     /* I - Command-line arguments */
 {
+  const char           *final_content_type;
+                                       /* FINAL_CONTENT_TYPE env var */
   int                  fd;             /* Raster file */
   cups_raster_t                *inras,         /* Input raster stream */
                        *outras;        /* Output raster stream */
   cups_page_header2_t  inheader,       /* Input raster page header */
                        outheader;      /* Output raster page header */
-  int                  y;              /* Current line */
+  unsigned             y;              /* Current line */
   unsigned char                *line;          /* Line buffer */
-  int                  page = 0,       /* Current page */
+  unsigned             page = 0,       /* Current page */
                        page_width,     /* Actual page width */
                        page_height,    /* Actual page height */
                        page_top,       /* Top margin */
@@ -53,10 +50,10 @@ main(int  argc,                             /* I - Number of command-line args */
                        lineoffset;     /* Offset into line */
   unsigned char                white;          /* White pixel */
   ppd_file_t           *ppd;           /* PPD file */
-  ppd_attr_t           *back;          /* cupsBackSize attribute */
+  ppd_attr_t           *back;          /* cupsBackSide attribute */
   _ppd_cache_t         *cache;         /* PPD cache */
-  _pwg_size_t          *pwg_size;      /* PWG media size */
-  _pwg_media_t         *pwg_media;     /* PWG media name */
+  pwg_size_t           *pwg_size;      /* PWG media size */
+  pwg_media_t          *pwg_media;     /* PWG media name */
   int                  num_options;    /* Number of options */
   cups_option_t                *options = NULL;/* Options */
   const char           *val;           /* Option value */
@@ -78,8 +75,11 @@ main(int  argc,                              /* I - Number of command-line args */
   else
     fd = 0;
 
+  if ((final_content_type = getenv("FINAL_CONTENT_TYPE")) == NULL)
+    final_content_type = "image/pwg-raster";
+
   inras  = cupsRasterOpen(fd, CUPS_RASTER_READ);
-  outras = cupsRasterOpen(1, CUPS_RASTER_WRITE_PWG);
+  outras = cupsRasterOpen(1, !strcmp(final_content_type, "image/pwg-raster") ? CUPS_RASTER_WRITE_PWG : CUPS_RASTER_WRITE_APPLE);
 
   ppd   = ppdOpenFile(getenv("PPD"));
   back  = ppdFindAttr(ppd, "cupsBackSide", NULL);
@@ -93,6 +93,29 @@ main(int  argc,                              /* I - Number of command-line args */
 
   while (cupsRasterReadHeader2(inras, &inheader))
   {
+   /*
+    * Show page device dictionary...
+    */
+
+    fprintf(stderr, "DEBUG: Duplex = %d\n", inheader.Duplex);
+    fprintf(stderr, "DEBUG: HWResolution = [ %d %d ]\n", inheader.HWResolution[0], inheader.HWResolution[1]);
+    fprintf(stderr, "DEBUG: ImagingBoundingBox = [ %d %d %d %d ]\n", inheader.ImagingBoundingBox[0], inheader.ImagingBoundingBox[1], inheader.ImagingBoundingBox[2], inheader.ImagingBoundingBox[3]);
+    fprintf(stderr, "DEBUG: Margins = [ %d %d ]\n", inheader.Margins[0], inheader.Margins[1]);
+    fprintf(stderr, "DEBUG: ManualFeed = %d\n", inheader.ManualFeed);
+    fprintf(stderr, "DEBUG: MediaPosition = %d\n", inheader.MediaPosition);
+    fprintf(stderr, "DEBUG: NumCopies = %d\n", inheader.NumCopies);
+    fprintf(stderr, "DEBUG: Orientation = %d\n", inheader.Orientation);
+    fprintf(stderr, "DEBUG: PageSize = [ %d %d ]\n", inheader.PageSize[0], inheader.PageSize[1]);
+    fprintf(stderr, "DEBUG: cupsWidth = %d\n", inheader.cupsWidth);
+    fprintf(stderr, "DEBUG: cupsHeight = %d\n", inheader.cupsHeight);
+    fprintf(stderr, "DEBUG: cupsMediaType = %d\n", inheader.cupsMediaType);
+    fprintf(stderr, "DEBUG: cupsBitsPerColor = %d\n", inheader.cupsBitsPerColor);
+    fprintf(stderr, "DEBUG: cupsBitsPerPixel = %d\n", inheader.cupsBitsPerPixel);
+    fprintf(stderr, "DEBUG: cupsBytesPerLine = %d\n", inheader.cupsBytesPerLine);
+    fprintf(stderr, "DEBUG: cupsColorOrder = %d\n", inheader.cupsColorOrder);
+    fprintf(stderr, "DEBUG: cupsColorSpace = %d\n", inheader.cupsColorSpace);
+    fprintf(stderr, "DEBUG: cupsCompression = %d\n", inheader.cupsCompression);
+
    /*
     * Compute the real raster size...
     */
@@ -101,18 +124,21 @@ main(int  argc,                           /* I - Number of command-line args */
 
     fprintf(stderr, "PAGE: %d %d\n", page, inheader.NumCopies);
 
-    page_width  = (int)(inheader.cupsPageSize[0] * inheader.HWResolution[0] /
-                        72.0);
-    page_height = (int)(inheader.cupsPageSize[1] * inheader.HWResolution[1] /
-                        72.0);
-    page_left   = (int)(inheader.cupsImagingBBox[0] *
-                        inheader.HWResolution[0] / 72.0);
-    page_bottom = (int)(inheader.cupsImagingBBox[1] *
-                        inheader.HWResolution[1] / 72.0);
+    page_width  = (unsigned)(inheader.cupsPageSize[0] * inheader.HWResolution[0] / 72.0);
+    page_height = (unsigned)(inheader.cupsPageSize[1] * inheader.HWResolution[1] / 72.0);
+    page_left   = (unsigned)(inheader.cupsImagingBBox[0] * inheader.HWResolution[0] / 72.0);
+    page_bottom = (unsigned)(inheader.cupsImagingBBox[1] * inheader.HWResolution[1] / 72.0);
     page_top    = page_height - page_bottom - inheader.cupsHeight;
     linesize    = (page_width * inheader.cupsBitsPerPixel + 7) / 8;
     lineoffset  = page_left * inheader.cupsBitsPerPixel / 8; /* Round down */
 
+    if (page_left > page_width || page_top > page_height || page_bottom > page_height)
+    {
+      _cupsLangPrintFilter(stderr, "ERROR", _("Unsupported raster data."));
+      fprintf(stderr, "DEBUG: Bad bottom/left/top margin on page %d.\n", page);
+      return (1);
+    }
+
     switch (inheader.cupsColorSpace)
     {
       case CUPS_CSPACE_W :
@@ -192,14 +218,14 @@ main(int  argc,                           /* I - Number of command-line args */
                 sizeof(outheader.OutputType));
       else
       {
-        fprintf(stderr, "DEBUG: Unsupported print-content-type \"%s\".\n", val);
+        fputs("DEBUG: Unsupported print-content-optimize value.\n", stderr);
         outheader.OutputType[0] = '\0';
       }
     }
 
     if ((val = cupsGetOption("print-quality", num_options, options)) != NULL)
     {
-      int quality = atoi(val);         /* print-quality value */
+      unsigned quality = (unsigned)atoi(val);          /* print-quality value */
 
       if (quality >= IPP_QUALITY_DRAFT && quality <= IPP_QUALITY_HIGH)
        outheader.cupsInteger[8] = quality;
@@ -233,8 +259,7 @@ main(int  argc,                             /* I - Number of command-line args */
                 sizeof(outheader.cupsRenderingIntent));
       else
       {
-        fprintf(stderr, "DEBUG: Unsupported print-rendering-intent \"%s\".\n",
-                val);
+        fputs("DEBUG: Unsupported print-rendering-intent value.\n", stderr);
         outheader.cupsRenderingIntent[0] = '\0';
       }
     }
@@ -247,10 +272,8 @@ main(int  argc,                            /* I - Number of command-line args */
     }
     else
     {
-      pwg_media = _pwgMediaForSize((int)(2540.0 * inheader.cupsPageSize[0] /
-                                         72.0),
-                                   (int)(2540.0 * inheader.cupsPageSize[1] /
-                                         72.0));
+      pwg_media = pwgMediaForSize((int)(2540.0 * inheader.cupsPageSize[0] / 72.0),
+                                 (int)(2540.0 * inheader.cupsPageSize[1] / 72.0));
 
       if (pwg_media)
         strlcpy(outheader.cupsPageSizeName, pwg_media->pwg,
@@ -270,7 +293,7 @@ main(int  argc,                             /* I - Number of command-line args */
       {
         if (inheader.Tumble)
         {
-         outheader.cupsInteger[1] = -1;/* CrossFeedTransform */
+         outheader.cupsInteger[1] = ~0U;/* CrossFeedTransform */
          outheader.cupsInteger[2] = 1; /* FeedTransform */
 
          outheader.cupsInteger[3] = page_width - page_left -
@@ -286,7 +309,7 @@ main(int  argc,                             /* I - Number of command-line args */
         else
         {
          outheader.cupsInteger[1] = 1; /* CrossFeedTransform */
-         outheader.cupsInteger[2] = -1;/* FeedTransform */
+         outheader.cupsInteger[2] = ~0U;/* FeedTransform */
 
          outheader.cupsInteger[3] = page_left;
                                        /* ImageBoxLeft */
@@ -302,8 +325,8 @@ main(int  argc,                             /* I - Number of command-line args */
       {
         if (inheader.Tumble)
         {
-         outheader.cupsInteger[1] = -1;/* CrossFeedTransform */
-         outheader.cupsInteger[2] = -1;/* FeedTransform */
+         outheader.cupsInteger[1] = ~0U;/* CrossFeedTransform */
+         outheader.cupsInteger[2] = ~0U;/* FeedTransform */
 
          outheader.cupsInteger[3] = page_width - page_left -
                                     inheader.cupsWidth;
@@ -334,8 +357,8 @@ main(int  argc,                             /* I - Number of command-line args */
       {
         if (inheader.Tumble)
         {
-         outheader.cupsInteger[1] = -1;/* CrossFeedTransform */
-         outheader.cupsInteger[2] = -1;/* FeedTransform */
+         outheader.cupsInteger[1] = ~0U;/* CrossFeedTransform */
+         outheader.cupsInteger[2] = ~0U;/* FeedTransform */
 
          outheader.cupsInteger[3] = page_width - page_left -
                                     inheader.cupsWidth;
@@ -368,7 +391,7 @@ main(int  argc,                             /* I - Number of command-line args */
         * Unsupported value...
         */
 
-        fprintf(stderr, "DEBUG: Unsupported cupsBackSide \"%s\".\n", back->value);
+        fputs("DEBUG: Unsupported cupsBackSide value.\n", stderr);
 
        outheader.cupsInteger[1] = 1;   /* CrossFeedTransform */
        outheader.cupsInteger[2] = 1;   /* FeedTransform */
@@ -409,6 +432,9 @@ main(int  argc,                             /* I - Number of command-line args */
     * Copy raster data...
     */
 
+    if (linesize < inheader.cupsBytesPerLine)
+      linesize = inheader.cupsBytesPerLine;
+
     line = malloc(linesize);
 
     memset(line, white, linesize);
@@ -423,7 +449,14 @@ main(int  argc,                            /* I - Number of command-line args */
 
     for (y = inheader.cupsHeight; y > 0; y --)
     {
-      cupsRasterReadPixels(inras, line + lineoffset, inheader.cupsBytesPerLine);
+      if (cupsRasterReadPixels(inras, line + lineoffset, inheader.cupsBytesPerLine) != inheader.cupsBytesPerLine)
+      {
+       _cupsLangPrintFilter(stderr, "ERROR", _("Error reading raster data."));
+       fprintf(stderr, "DEBUG: Unable to read line %d for page %d.\n",
+               inheader.cupsHeight - y + page_top + 1, page);
+       return (1);
+      }
+
       if (!cupsRasterWritePixels(outras, line, outheader.cupsBytesPerLine))
       {
        _cupsLangPrintFilter(stderr, "ERROR", _("Error sending raster data."));
@@ -454,8 +487,3 @@ main(int  argc,                             /* I - Number of command-line args */
 
   return (0);
 }
-
-
-/*
- * End of "$Id$".
- */