]> granicus.if.org Git - imagemagick/blobdiff - coders/pcl.c
(no commit message)
[imagemagick] / coders / pcl.c
index 22b5b62986223ba65647db20b24729937e0694c6..0fc6ca823f7ee49700c280a4f6dd7e2464e9f453 100644 (file)
@@ -17,7 +17,7 @@
 %                                 July 1992                                   %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2009 ImageMagick Studio LLC, a non-profit organization      %
+%  Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization      %
 %  dedicated to making software imaging solutions freely available.           %
 %                                                                             %
 %  You may not use this file except in compliance with the License.  You may  %
@@ -60,6 +60,7 @@
 #include "magick/memory_.h"
 #include "magick/monitor.h"
 #include "magick/monitor-private.h"
+#include "magick/option.h"
 #include "magick/profile.h"
 #include "magick/resource_.h"
 #include "magick/quantum-private.h"
@@ -105,10 +106,10 @@ static MagickBooleanType IsPCL(const unsigned char *magick,const size_t length)
 {
   if (length < 4)
     return(MagickFalse);
-  if (memcmp(magick,"\033E\033",3) == 0)
-    return(MagickTrue);
   if (memcmp(magick,"\033E\033&",4) == 0)
     return(MagickFalse);
+  if (memcmp(magick,"\033E\033",3) == 0)
+    return(MagickTrue);
   return(MagickFalse);
 }
 \f
@@ -177,7 +178,7 @@ static Image *ReadPCLImage(const ImageInfo *image_info,ExceptionInfo *exception)
   register char
     *p;
 
-  register long
+  register ssize_t
     c;
 
   SegmentInfo
@@ -186,7 +187,7 @@ static Image *ReadPCLImage(const ImageInfo *image_info,ExceptionInfo *exception)
   ssize_t
     count;
 
-  unsigned long
+  size_t
     height,
     width;
 
@@ -289,8 +290,8 @@ static Image *ReadPCLImage(const ImageInfo *image_info,ExceptionInfo *exception)
     /*
       Set PCL render geometry.
     */
-    width=(unsigned long) (bounds.x2-bounds.x1+0.5);
-    height=(unsigned long) (bounds.y2-bounds.y1+0.5);
+    width=(size_t) floor(bounds.x2-bounds.x1+0.5);
+    height=(size_t) floor(bounds.y2-bounds.y1+0.5);
     if (width > page.width)
       page.width=width;
     if (height > page.height)
@@ -304,8 +305,8 @@ static Image *ReadPCLImage(const ImageInfo *image_info,ExceptionInfo *exception)
     (void) ParseAbsoluteGeometry(PSPageGeometry,&page);
   if (image_info->page != (char *) NULL)
     (void) ParseAbsoluteGeometry(image_info->page,&page);
-  (void) FormatMagickString(geometry,MaxTextExtent,"%lux%lu",
-    page.width,page.height);
+  (void) FormatMagickString(geometry,MaxTextExtent,"%.20gx%.20g",(double)
+    page.width,(double) page.height);
   if (image_info->monochrome != MagickFalse)
     delegate_info=GetDelegateInfo("pcl:mono",(char *) NULL,exception);
   else
@@ -322,22 +323,23 @@ static Image *ReadPCLImage(const ImageInfo *image_info,ExceptionInfo *exception)
     (void) ParseAbsoluteGeometry(image_info->page,&page);
   (void) FormatMagickString(density,MaxTextExtent,"%gx%g",
     image->x_resolution,image->y_resolution);
-  page.width=(unsigned long) (page.width*image->x_resolution/delta.x+0.5);
-  page.height=(unsigned long) (page.height*image->y_resolution/delta.y+0.5);
-  (void) FormatMagickString(options,MaxTextExtent,"-g%lux%lu ",
-    page.width,page.height);
+  page.width=(size_t) floor(page.width*image->x_resolution/delta.x+0.5);
+  page.height=(size_t) floor(page.height*image->y_resolution/delta.y+
+    0.5);
+  (void) FormatMagickString(options,MaxTextExtent,"-g%.20gx%.20g ",(double)
+     page.width,(double) page.height);
   image=DestroyImage(image);
   read_info=CloneImageInfo(image_info);
   *read_info->magick='\0';
   if (read_info->number_scenes != 0)
     {
       if (read_info->number_scenes != 1)
-        (void) FormatMagickString(options,MaxTextExtent,"-dLastPage=%lu",
-          read_info->scene+read_info->number_scenes);
+        (void) FormatMagickString(options,MaxTextExtent,"-dLastPage=%.20g",
+          (double) (read_info->scene+read_info->number_scenes));
       else
         (void) FormatMagickString(options,MaxTextExtent,
-          "-dFirstPage=%lu -dLastPage=%lu",read_info->scene+1,read_info->scene+
-          read_info->number_scenes);
+          "-dFirstPage=%.20g -dLastPage=%.20g",(double) read_info->scene+1,
+          (double) (read_info->scene+read_info->number_scenes));
       read_info->number_scenes=0;
       if (read_info->scenes != (char *) NULL)
         *read_info->scenes='\0';
@@ -352,8 +354,8 @@ static Image *ReadPCLImage(const ImageInfo *image_info,ExceptionInfo *exception)
     read_info->antialias != MagickFalse ? 4 : 1,
     read_info->antialias != MagickFalse ? 4 : 1,density,options,
     read_info->filename,input_filename);
-  status=SystemCommand(read_info->verbose,command,exception) != 0 ? MagickTrue :
-    MagickFalse;
+  status=SystemCommand(MagickFalse,read_info->verbose,command,exception) != 0 ?
+    MagickTrue : MagickFalse;
   image=ReadImage(read_info,exception);
   (void) RelinquishUniqueFileResource(read_info->filename);
   (void) RelinquishUniqueFileResource(input_filename);
@@ -403,10 +405,10 @@ static Image *ReadPCLImage(const ImageInfo *image_info,ExceptionInfo *exception)
 %
 %  The format of the RegisterPCLImage method is:
 %
-%      unsigned long RegisterPCLImage(void)
+%      size_t RegisterPCLImage(void)
 %
 */
-ModuleExport unsigned long RegisterPCLImage(void)
+ModuleExport size_t RegisterPCLImage(void)
 {
   MagickInfo
     *entry;
@@ -483,7 +485,7 @@ static size_t PCLDeltaCompressImage(const size_t length,
     j,
     replacement;
 
-  register long
+  register ssize_t
     i,
     x;
 
@@ -491,10 +493,10 @@ static size_t PCLDeltaCompressImage(const size_t length,
     *q;
 
   q=compress_pixels;
-  for (x=0; x < (long) length; )
+  for (x=0; x < (ssize_t) length; )
   {
     j=0;
-    for (i=0; x < (long) length; x++)
+    for (i=0; x < (ssize_t) length; x++)
     {
       if (*pixels++ != *previous_pixels++)
         {
@@ -503,8 +505,9 @@ static size_t PCLDeltaCompressImage(const size_t length,
         }
       j++;
     }
-    for ( ; x < (long) length; x++)
+    while (x < (ssize_t) length)
     {
+      x++;
       if (*pixels == *previous_pixels)
         break;
       i++;
@@ -550,10 +553,10 @@ static size_t PCLPackbitsCompressImage(const size_t length,
   int
     count;
 
-  long
+  ssize_t
     j;
 
-  register long
+  register ssize_t
     x;
 
   register unsigned char
@@ -566,7 +569,7 @@ static size_t PCLPackbitsCompressImage(const size_t length,
     Compress pixels with Packbits encoding.
   */
   q=compress_pixels;
-  for (x=(long) length; x != 0; )
+  for (x=(ssize_t) length; x != 0; )
   {
     switch (x)
     {
@@ -608,7 +611,7 @@ static size_t PCLPackbitsCompressImage(const size_t length,
               Packed run.
             */
             count=3;
-            while (((long) count < x) && (*pixels == *(pixels+count)))
+            while (((ssize_t) count < x) && (*pixels == *(pixels+count)))
             {
               count++;
               if (count >= 127)
@@ -629,12 +632,12 @@ static size_t PCLPackbitsCompressImage(const size_t length,
         {
           packbits[count+1]=pixels[count];
           count++;
-          if (((long) count >= (x-3)) || (count >= 127))
+          if (((ssize_t) count >= (x-3)) || (count >= 127))
             break;
         }
         x-=count;
         *packbits=(unsigned char) (count-1);
-        for (j=0; j <= (long) count; j++)
+        for (j=0; j <= (ssize_t) count; j++)
           *q++=packbits[j];
         pixels+=count;
         break;
@@ -650,7 +653,10 @@ static MagickBooleanType WritePCLImage(const ImageInfo *image_info,Image *image)
   char
     buffer[MaxTextExtent];
 
-  long
+  const char
+    *option;
+
+  ssize_t
     y;
 
   MagickBooleanType
@@ -665,7 +671,7 @@ static MagickBooleanType WritePCLImage(const ImageInfo *image_info,Image *image)
   register const PixelPacket
     *p;
 
-  register long
+  register ssize_t
     i,
     x;
 
@@ -682,8 +688,9 @@ static MagickBooleanType WritePCLImage(const ImageInfo *image_info,Image *image)
     *pixels,
     *previous_pixels;
 
-  unsigned long
-    density;
+  size_t
+    density,
+    one;
 
   /*
     Open output image file.
@@ -704,9 +711,10 @@ static MagickBooleanType WritePCLImage(const ImageInfo *image_info,Image *image)
         geometry;
 
       (void) ParseGeometry(image_info->density,&geometry);
-      density=(unsigned long) geometry.rho;
+      density=(size_t) geometry.rho;
     }
   scene=0;
+  one=1;
   do
   {
     if (image->colorspace != RGBColorspace)
@@ -716,10 +724,11 @@ static MagickBooleanType WritePCLImage(const ImageInfo *image_info,Image *image)
     */
     (void) WriteBlobString(image,"\033E");  /* printer reset */
     (void) WriteBlobString(image,"\033*r3F");  /* set presentation mode */
-    (void) FormatMagickString(buffer,MaxTextExtent,"\033*r%lus%luT",
-      image->columns,image->rows);
+    (void) FormatMagickString(buffer,MaxTextExtent,"\033*r%.20gs%.20gT",
+      (double) image->columns,(double) image->rows);
     (void) WriteBlobString(image,buffer);
-    (void) FormatMagickString(buffer,MaxTextExtent,"\033*t%ldR",density);
+    (void) FormatMagickString(buffer,MaxTextExtent,"\033*t%.20gR",(double)
+      density);
     (void) WriteBlobString(image,buffer);
     (void) WriteBlobString(image,"\033&l0E");  /* top margin 0 */
     if (IsMonochromeImage(image,&image->exception) != MagickFalse)
@@ -768,26 +777,35 @@ static MagickBooleanType WritePCLImage(const ImageInfo *image_info,Image *image)
           (void) WriteBlobByte(image,8); /* bits per red component */
           (void) WriteBlobByte(image,8); /* bits per green component */
           (void) WriteBlobByte(image,8); /* bits per blue component */
-          for (i=0; i < (long) image->colors; i++)
+          for (i=0; i < (ssize_t) image->colors; i++)
           {
             (void) FormatMagickString(buffer,MaxTextExtent,
-              "\033*v%da%db%dc%ldI",ScaleQuantumToChar(image->colormap[i].red),
+              "\033*v%da%db%dc%.20gI",
+              ScaleQuantumToChar(image->colormap[i].red),
               ScaleQuantumToChar(image->colormap[i].green),
-              ScaleQuantumToChar(image->colormap[i].blue),i);
+              ScaleQuantumToChar(image->colormap[i].blue),(double) i);
             (void) WriteBlobString(image,buffer);
           }
-          for ( ; i < (1L << bits_per_pixel); i++)
+          one=1;
+          for ( ; i < (ssize_t) (one << bits_per_pixel); i++)
           {
-            (void) FormatMagickString(buffer,MaxTextExtent,"\033*v%luI",i);
+            (void) FormatMagickString(buffer,MaxTextExtent,"\033*v%.20gI",
+              (double) i);
             (void) WriteBlobString(image,buffer);
           }
         }
-    (void) WriteBlobString(image,"\033*r1A");  /* start raster graphics */
+    option=GetImageOption(image_info,"pcl:fit-to-page");
+    if ((option != (const char *) NULL) &&
+        (IsMagickTrue(option) != MagickFalse))
+      (void) WriteBlobString(image,"\033*r3A");
+    else
+      (void) WriteBlobString(image,"\033*r1A");  /* start raster graphics */
     (void) WriteBlobString(image,"\033*b0Y");  /* set y offset */
     length=(image->columns*bits_per_pixel+7)/8;
-    pixels=(unsigned char *) AcquireQuantumMemory(length,sizeof(*pixels));
+    pixels=(unsigned char *) AcquireQuantumMemory(length+1,sizeof(*pixels));
     if (pixels == (unsigned char *) NULL)
       ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
+    (void) ResetMagickMemory(pixels,0,(length+1)*sizeof(*pixels));
     compress_pixels=(unsigned char *) NULL;
     previous_pixels=(unsigned char *) NULL;
     switch (image->compression)
@@ -807,20 +825,24 @@ static MagickBooleanType WritePCLImage(const ImageInfo *image_info,Image *image)
             pixels=(unsigned char *) RelinquishMagickMemory(pixels);
             ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
           }
+        (void) ResetMagickMemory(compress_pixels,0,(length+256)*
+          sizeof(*compress_pixels));
         (void) FormatMagickString(buffer,MaxTextExtent,"\033*b2M");
         (void) WriteBlobString(image,buffer);
         break;
       }
       default:
       {
-        compress_pixels=(unsigned char *) AcquireQuantumMemory(length+
-          (length >> 3),sizeof(*compress_pixels));
+        compress_pixels=(unsigned char *) AcquireQuantumMemory(length+32,
+          sizeof(*compress_pixels));
         if (compress_pixels == (unsigned char *) NULL)
           {
             pixels=(unsigned char *) RelinquishMagickMemory(pixels);
             ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
           }
-        previous_pixels=(unsigned char *) AcquireQuantumMemory(length,
+        (void) ResetMagickMemory(compress_pixels,0,(length+32)*
+          sizeof(*compress_pixels));
+        previous_pixels=(unsigned char *) AcquireQuantumMemory(length+1,
           sizeof(*previous_pixels));
         if (previous_pixels == (unsigned char *) NULL)
           {
@@ -829,12 +851,14 @@ static MagickBooleanType WritePCLImage(const ImageInfo *image_info,Image *image)
             pixels=(unsigned char *) RelinquishMagickMemory(pixels);
             ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
           }
+        (void) ResetMagickMemory(previous_pixels,0,(length+1)*
+          sizeof(*previous_pixels));
         (void) FormatMagickString(buffer,MaxTextExtent,"\033*b3M");
         (void) WriteBlobString(image,buffer);
         break;
       }
     }
-    for (y=0; y < (long) image->rows; y++)
+    for (y=0; y < (ssize_t) image->rows; y++)
     {
       p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
       if (p == (const PixelPacket *) NULL)
@@ -854,7 +878,7 @@ static MagickBooleanType WritePCLImage(const ImageInfo *image_info,Image *image)
           */
           bit=0;
           byte=0;
-          for (x=0; x < (long) image->columns; x++)
+          for (x=0; x < (ssize_t) image->columns; x++)
           {
             byte<<=1;
             if (PixelIntensity(p) >= ((MagickRealType) QuantumRange/2.0))
@@ -877,7 +901,7 @@ static MagickBooleanType WritePCLImage(const ImageInfo *image_info,Image *image)
           /*
             Colormapped image.
           */
-          for (x=0; x < (long) image->columns; x++)
+          for (x=0; x < (ssize_t) image->columns; x++)
             *q++=(unsigned char) indexes[x];
           break;
         }
@@ -887,11 +911,11 @@ static MagickBooleanType WritePCLImage(const ImageInfo *image_info,Image *image)
           /*
             Truecolor image.
           */
-          for (x=0; x < (long) image->columns; x++)
+          for (x=0; x < (ssize_t) image->columns; x++)
           {
-            *q++=ScaleQuantumToChar(p->red);
-            *q++=ScaleQuantumToChar(p->green);
-            *q++=ScaleQuantumToChar(p->blue);
+            *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
+            *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
+            *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
             p++;
           }
           break;
@@ -901,8 +925,8 @@ static MagickBooleanType WritePCLImage(const ImageInfo *image_info,Image *image)
       {
         case NoCompression:
         {
-          (void) FormatMagickString(buffer,MaxTextExtent,"\033*b%luW",
-            (unsigned long) length);
+          (void) FormatMagickString(buffer,MaxTextExtent,"\033*b%.20gW",
+            (double) length);
           (void) WriteBlobString(image,buffer);
           (void) WriteBlob(image,length,pixels);
           break;
@@ -911,8 +935,8 @@ static MagickBooleanType WritePCLImage(const ImageInfo *image_info,Image *image)
         {
           packets=PCLPackbitsCompressImage(length,pixels,
             compress_pixels);
-          (void) FormatMagickString(buffer,MaxTextExtent,"\033*b%luW",
-            (unsigned long) packets);
+          (void) FormatMagickString(buffer,MaxTextExtent,"\033*b%.20gW",
+            (double) packets);
           (void) WriteBlobString(image,buffer);
           (void) WriteBlob(image,packets,compress_pixels);
           break;
@@ -920,12 +944,12 @@ static MagickBooleanType WritePCLImage(const ImageInfo *image_info,Image *image)
         default:
         {
           if (y == 0)
-            for (i=0; i < (long) length; i++)
+            for (i=0; i < (ssize_t) length; i++)
               previous_pixels[i]=(~pixels[i]);
           packets=PCLDeltaCompressImage(length,previous_pixels,pixels,
             compress_pixels);
-          (void) FormatMagickString(buffer,MaxTextExtent,"\033*b%luW",
-            (unsigned long) packets);
+          (void) FormatMagickString(buffer,MaxTextExtent,"\033*b%.20gW",
+            (double) packets);
           (void) WriteBlobString(image,buffer);
           (void) WriteBlob(image,packets,compress_pixels);
           (void) CopyMagickMemory(previous_pixels,pixels,length*