]> granicus.if.org Git - imagemagick/blob - coders/ps2.c
https://github.com/ImageMagick/ImageMagick/issues/1286
[imagemagick] / coders / ps2.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                            PPPP   SSSSS  22222                              %
7 %                            P   P  SS        22                              %
8 %                            PPPP    SSS    222                               %
9 %                            P         SS  22                                 %
10 %                            P      SSSSS  22222                              %
11 %                                                                             %
12 %                                                                             %
13 %                      Write Postscript Level II Format                       %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                   Cristy                                    %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2018 ImageMagick Studio LLC, a non-profit organization      %
21 %  dedicated to making software imaging solutions freely available.           %
22 %                                                                             %
23 %  You may not use this file except in compliance with the License.  You may  %
24 %  obtain a copy of the License at                                            %
25 %                                                                             %
26 %    https://www.imagemagick.org/script/license.php                           %
27 %                                                                             %
28 %  Unless required by applicable law or agreed to in writing, software        %
29 %  distributed under the License is distributed on an "AS IS" BASIS,          %
30 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
31 %  See the License for the specific language governing permissions and        %
32 %  limitations under the License.                                             %
33 %                                                                             %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %
36 %
37 */
38 \f
39 /*
40   Include declarations.
41 */
42 #include "MagickCore/studio.h"
43 #include "MagickCore/attribute.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/color.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/compress.h"
50 #include "MagickCore/constitute.h"
51 #include "MagickCore/draw.h"
52 #include "MagickCore/exception.h"
53 #include "MagickCore/exception-private.h"
54 #include "MagickCore/geometry.h"
55 #include "MagickCore/image.h"
56 #include "MagickCore/image-private.h"
57 #include "MagickCore/list.h"
58 #include "MagickCore/magick.h"
59 #include "MagickCore/memory_.h"
60 #include "MagickCore/monitor.h"
61 #include "MagickCore/monitor-private.h"
62 #include "MagickCore/monitor-private.h"
63 #include "MagickCore/option.h"
64 #include "MagickCore/pixel-accessor.h"
65 #include "MagickCore/property.h"
66 #include "MagickCore/quantum-private.h"
67 #include "MagickCore/resource_.h"
68 #include "MagickCore/static.h"
69 #include "MagickCore/string_.h"
70 #include "MagickCore/module.h"
71 #include "MagickCore/utility.h"
72 \f
73 /*
74   Define declarations.
75 */
76 #if defined(MAGICKCORE_TIFF_DELEGATE)
77 #define CCITTParam  "-1"
78 #else
79 #define CCITTParam  "0"
80 #endif
81 \f
82 /*
83   Forward declarations.
84 */
85 static MagickBooleanType
86   WritePS2Image(const ImageInfo *,Image *,ExceptionInfo *);
87 \f
88 /*
89 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90 %                                                                             %
91 %                                                                             %
92 %                                                                             %
93 %   R e g i s t e r P S 2 I m a g e                                           %
94 %                                                                             %
95 %                                                                             %
96 %                                                                             %
97 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
98 %
99 %  RegisterPS2Image() adds properties for the PS2 image format to
100 %  the list of supported formats.  The properties include the image format
101 %  tag, a method to read and/or write the format, whether the format
102 %  supports the saving of more than one frame to the same file or blob,
103 %  whether the format supports native in-memory I/O, and a brief
104 %  description of the format.
105 %
106 %  The format of the RegisterPS2Image method is:
107 %
108 %      size_t RegisterPS2Image(void)
109 %
110 */
111 ModuleExport size_t RegisterPS2Image(void)
112 {
113   MagickInfo
114     *entry;
115
116   entry=AcquireMagickInfo("PS2","EPS2","Level II Encapsulated PostScript");
117   entry->encoder=(EncodeImageHandler *) WritePS2Image;
118   entry->flags^=CoderAdjoinFlag;
119   entry->flags|=CoderEncoderSeekableStreamFlag;
120   entry->flags^=CoderBlobSupportFlag;
121   entry->mime_type=ConstantString("application/postscript");
122   (void) RegisterMagickInfo(entry);
123   entry=AcquireMagickInfo("PS2","PS2","Level II PostScript");
124   entry->encoder=(EncodeImageHandler *) WritePS2Image;
125   entry->flags|=CoderEncoderSeekableStreamFlag;
126   entry->flags^=CoderBlobSupportFlag;
127   entry->mime_type=ConstantString("application/postscript");
128   (void) RegisterMagickInfo(entry);
129   return(MagickImageCoderSignature);
130 }
131 \f
132 /*
133 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
134 %                                                                             %
135 %                                                                             %
136 %                                                                             %
137 %   U n r e g i s t e r P S 2 I m a g e                                       %
138 %                                                                             %
139 %                                                                             %
140 %                                                                             %
141 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142 %
143 %  UnregisterPS2Image() removes format registrations made by the
144 %  PS2 module from the list of supported formats.
145 %
146 %  The format of the UnregisterPS2Image method is:
147 %
148 %      UnregisterPS2Image(void)
149 %
150 */
151 ModuleExport void UnregisterPS2Image(void)
152 {
153   (void) UnregisterMagickInfo("EPS2");
154   (void) UnregisterMagickInfo("PS2");
155 }
156 \f
157 /*
158 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
159 %                                                                             %
160 %                                                                             %
161 %                                                                             %
162 %   W r i t e P S 2 I m a g e                                                 %
163 %                                                                             %
164 %                                                                             %
165 %                                                                             %
166 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
167 %
168 %  WritePS2Image translates an image to encapsulated Postscript
169 %  Level II for printing.  If the supplied geometry is null, the image is
170 %  centered on the Postscript page.  Otherwise, the image is positioned as
171 %  specified by the geometry.
172 %
173 %  The format of the WritePS2Image method is:
174 %
175 %      MagickBooleanType WritePS2Image(const ImageInfo *image_info,
176 %        Image *image,ExceptionInfo *exception)
177 %
178 %  A description of each parameter follows:
179 %
180 %    o image_info: the image info.
181 %
182 %    o image: the image.
183 %
184 %    o exception: return any errors or warnings in this structure.
185 %
186 */
187
188 static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
189   Image *image,Image *inject_image,ExceptionInfo *exception)
190 {
191   Image
192     *group4_image;
193
194   ImageInfo
195     *write_info;
196
197   MagickBooleanType
198     status;
199
200   size_t
201     length;
202
203   unsigned char
204     *group4;
205
206   status=MagickTrue;
207   write_info=CloneImageInfo(image_info);
208   (void) CopyMagickString(write_info->filename,"GROUP4:",MagickPathExtent);
209   (void) CopyMagickString(write_info->magick,"GROUP4",MagickPathExtent);
210   group4_image=CloneImage(inject_image,0,0,MagickTrue,exception);
211   if (group4_image == (Image *) NULL)
212     return(MagickFalse);
213   group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
214     exception);
215   group4_image=DestroyImage(group4_image);
216   if (group4 == (unsigned char *) NULL)
217     return(MagickFalse);
218   write_info=DestroyImageInfo(write_info);
219   if (WriteBlob(image,length,group4) != (ssize_t) length)
220     status=MagickFalse;
221   group4=(unsigned char *) RelinquishMagickMemory(group4);
222   return(status);
223 }
224
225 static MagickBooleanType WritePS2Image(const ImageInfo *image_info,Image *image,
226   ExceptionInfo *exception)
227 {
228   static const char
229     *const PostscriptProlog[]=
230     {
231       "%%%%BeginProlog",
232       "%%",
233       "%% Display a color image.  The image is displayed in color on",
234       "%% Postscript viewers or printers that support color, otherwise",
235       "%% it is displayed as grayscale.",
236       "%%",
237       "/DirectClassImage",
238       "{",
239       "  %%",
240       "  %% Display a DirectClass image.",
241       "  %%",
242       "  colorspace 0 eq",
243       "  {",
244       "    /DeviceRGB setcolorspace",
245       "    <<",
246       "      /ImageType 1",
247       "      /Width columns",
248       "      /Height rows",
249       "      /BitsPerComponent 8",
250       "      /Decode [0 1 0 1 0 1]",
251       "      /ImageMatrix [columns 0 0 rows neg 0 rows]",
252       "      compression 0 gt",
253       "      { /DataSource pixel_stream %s }",
254       "      { /DataSource pixel_stream %s } ifelse",
255       "    >> image",
256       "  }",
257       "  {",
258       "    /DeviceCMYK setcolorspace",
259       "    <<",
260       "      /ImageType 1",
261       "      /Width columns",
262       "      /Height rows",
263       "      /BitsPerComponent 8",
264       "      /Decode [1 0 1 0 1 0 1 0]",
265       "      /ImageMatrix [columns 0 0 rows neg 0 rows]",
266       "      compression 0 gt",
267       "      { /DataSource pixel_stream %s }",
268       "      { /DataSource pixel_stream %s } ifelse",
269       "    >> image",
270       "  } ifelse",
271       "} bind def",
272       "",
273       "/PseudoClassImage",
274       "{",
275       "  %%",
276       "  %% Display a PseudoClass image.",
277       "  %%",
278       "  %% Parameters:",
279       "  %%   colors: number of colors in the colormap.",
280       "  %%",
281       "  currentfile buffer readline pop",
282       "  token pop /colors exch def pop",
283       "  colors 0 eq",
284       "  {",
285       "    %%",
286       "    %% Image is grayscale.",
287       "    %%",
288       "    currentfile buffer readline pop",
289       "    token pop /bits exch def pop",
290       "    /DeviceGray setcolorspace",
291       "    <<",
292       "      /ImageType 1",
293       "      /Width columns",
294       "      /Height rows",
295       "      /BitsPerComponent bits",
296       "      /Decode [0 1]",
297       "      /ImageMatrix [columns 0 0 rows neg 0 rows]",
298       "      compression 0 gt",
299       "      { /DataSource pixel_stream %s }",
300       "      {",
301       "        /DataSource pixel_stream %s",
302       "        <<",
303       "           /K " CCITTParam,
304       "           /Columns columns",
305       "           /Rows rows",
306       "        >> /CCITTFaxDecode filter",
307       "      } ifelse",
308       "    >> image",
309       "  }",
310       "  {",
311       "    %%",
312       "    %% Parameters:",
313       "    %%   colormap: red, green, blue color packets.",
314       "    %%",
315       "    /colormap colors 3 mul string def",
316       "    currentfile colormap readhexstring pop pop",
317       "    currentfile buffer readline pop",
318       "    [ /Indexed /DeviceRGB colors 1 sub colormap ] setcolorspace",
319       "    <<",
320       "      /ImageType 1",
321       "      /Width columns",
322       "      /Height rows",
323       "      /BitsPerComponent 8",
324       "      /Decode [0 255]",
325       "      /ImageMatrix [columns 0 0 rows neg 0 rows]",
326       "      compression 0 gt",
327       "      { /DataSource pixel_stream %s }",
328       "      { /DataSource pixel_stream %s } ifelse",
329       "    >> image",
330       "  } ifelse",
331       "} bind def",
332       "",
333       "/DisplayImage",
334       "{",
335       "  %%",
336       "  %% Display a DirectClass or PseudoClass image.",
337       "  %%",
338       "  %% Parameters:",
339       "  %%   x & y translation.",
340       "  %%   x & y scale.",
341       "  %%   label pointsize.",
342       "  %%   image label.",
343       "  %%   image columns & rows.",
344       "  %%   class: 0-DirectClass or 1-PseudoClass.",
345       "  %%   colorspace: 0-RGB or 1-CMYK.",
346       "  %%   compression: 0-RLECompression or 1-NoCompression.",
347       "  %%   hex color packets.",
348       "  %%",
349       "  gsave",
350       "  /buffer 512 string def",
351       "  /pixel_stream currentfile def",
352       "",
353       "  currentfile buffer readline pop",
354       "  token pop /x exch def",
355       "  token pop /y exch def pop",
356       "  x y translate",
357       "  currentfile buffer readline pop",
358       "  token pop /x exch def",
359       "  token pop /y exch def pop",
360       "  currentfile buffer readline pop",
361       "  token pop /pointsize exch def pop",
362       (const char *) NULL
363     },
364     *const PostscriptEpilog[]=
365     {
366       "  x y scale",
367       "  currentfile buffer readline pop",
368       "  token pop /columns exch def",
369       "  token pop /rows exch def pop",
370       "  currentfile buffer readline pop",
371       "  token pop /class exch def pop",
372       "  currentfile buffer readline pop",
373       "  token pop /colorspace exch def pop",
374       "  currentfile buffer readline pop",
375       "  token pop /compression exch def pop",
376       "  class 0 gt { PseudoClassImage } { DirectClassImage } ifelse",
377       "  grestore",
378       (const char *) NULL
379     };
380
381   char
382     buffer[MagickPathExtent],
383     date[MagickPathExtent],
384     page_geometry[MagickPathExtent],
385     **labels;
386
387   CompressionType
388     compression;
389
390   const char
391     *const *q,
392     *value;
393
394   double
395     pointsize;
396
397   GeometryInfo
398     geometry_info;
399
400   MagickOffsetType
401     scene,
402     start,
403     stop;
404
405   MagickBooleanType
406     progress,
407     status;
408
409   MagickOffsetType
410     offset;
411
412   MagickSizeType
413     number_pixels;
414
415   MagickStatusType
416     flags;
417
418   PointInfo
419     delta,
420     resolution,
421     scale;
422
423   RectangleInfo
424     geometry,
425     media_info,
426     page_info;
427
428   register const Quantum
429     *p;
430
431   register ssize_t
432     x;
433
434   register ssize_t
435     i;
436
437   SegmentInfo
438     bounds;
439
440   size_t
441     imageListLength,
442     length,
443     page,
444     text_size;
445
446   ssize_t
447     j,
448     y;
449
450   time_t
451     timer;
452
453   unsigned char
454     *pixels;
455
456   /*
457     Open output image file.
458   */
459   assert(image_info != (const ImageInfo *) NULL);
460   assert(image_info->signature == MagickCoreSignature);
461   assert(image != (Image *) NULL);
462   assert(image->signature == MagickCoreSignature);
463   if (image->debug != MagickFalse)
464     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
465   assert(exception != (ExceptionInfo *) NULL);
466   assert(exception->signature == MagickCoreSignature);
467   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
468   if (status == MagickFalse)
469     return(status);
470   compression=image->compression;
471   if (image_info->compression != UndefinedCompression)
472     compression=image_info->compression;
473   switch (compression)
474   {
475 #if !defined(MAGICKCORE_JPEG_DELEGATE)
476     case JPEGCompression:
477     {
478       compression=RLECompression;
479       (void) ThrowMagickException(exception,GetMagickModule(),
480         MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
481         image->filename);
482       break;
483     }
484 #endif
485     default:
486       break;
487   }
488   (void) memset(&bounds,0,sizeof(bounds));
489   page=1;
490   scene=0;
491   imageListLength=GetImageListLength(image);
492   do
493   {
494     /*
495       Scale relative to dots-per-inch.
496     */
497     delta.x=DefaultResolution;
498     delta.y=DefaultResolution;
499     resolution.x=image->resolution.x;
500     resolution.y=image->resolution.y;
501     if ((resolution.x == 0.0) || (resolution.y == 0.0))
502       {
503         flags=ParseGeometry(PSDensityGeometry,&geometry_info);
504         resolution.x=geometry_info.rho;
505         resolution.y=geometry_info.sigma;
506         if ((flags & SigmaValue) == 0)
507           resolution.y=resolution.x;
508       }
509     if (image_info->density != (char *) NULL)
510       {
511         flags=ParseGeometry(image_info->density,&geometry_info);
512         resolution.x=geometry_info.rho;
513         resolution.y=geometry_info.sigma;
514         if ((flags & SigmaValue) == 0)
515           resolution.y=resolution.x;
516       }
517     if (image->units == PixelsPerCentimeterResolution)
518       {
519         resolution.x=(size_t) (100.0*2.54*resolution.x+0.5)/100.0;
520         resolution.y=(size_t) (100.0*2.54*resolution.y+0.5)/100.0;
521       }
522     SetGeometry(image,&geometry);
523     (void) FormatLocaleString(page_geometry,MagickPathExtent,"%.20gx%.20g",
524       (double) image->columns,(double) image->rows);
525     if (image_info->page != (char *) NULL)
526       (void) CopyMagickString(page_geometry,image_info->page,MagickPathExtent);
527     else
528       if ((image->page.width != 0) && (image->page.height != 0))
529         (void) FormatLocaleString(page_geometry,MagickPathExtent,
530           "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,(double)
531           image->page.height,(double) image->page.x,(double) image->page.y);
532       else
533         if ((image->gravity != UndefinedGravity) &&
534             (LocaleCompare(image_info->magick,"PS") == 0))
535           (void) CopyMagickString(page_geometry,PSPageGeometry,MagickPathExtent);
536     (void) ConcatenateMagickString(page_geometry,">",MagickPathExtent);
537     (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
538       &geometry.width,&geometry.height);
539     scale.x=(double) (geometry.width*delta.x)/resolution.x;
540     geometry.width=(size_t) floor(scale.x+0.5);
541     scale.y=(double) (geometry.height*delta.y)/resolution.y;
542     geometry.height=(size_t) floor(scale.y+0.5);
543     (void) ParseAbsoluteGeometry(page_geometry,&media_info);
544     (void) ParseGravityGeometry(image,page_geometry,&page_info,exception);
545     if (image->gravity != UndefinedGravity)
546       {
547         geometry.x=(-page_info.x);
548         geometry.y=(ssize_t) (media_info.height+page_info.y-image->rows);
549       }
550     pointsize=12.0;
551     if (image_info->pointsize != 0.0)
552       pointsize=image_info->pointsize;
553     text_size=0;
554     value=GetImageProperty(image,"label",exception);
555     if (value != (const char *) NULL)
556       text_size=(size_t) (MultilineCensus(value)*pointsize+12);
557     if (page == 1)
558       {
559         /*
560           Output Postscript header.
561         */
562         if (LocaleCompare(image_info->magick,"PS2") == 0)
563           (void) CopyMagickString(buffer,"%!PS-Adobe-3.0\n",MagickPathExtent);
564         else
565           (void) CopyMagickString(buffer,"%!PS-Adobe-3.0 EPSF-3.0\n",
566             MagickPathExtent);
567         (void) WriteBlobString(image,buffer);
568         (void) WriteBlobString(image,"%%Creator: (ImageMagick)\n");
569         (void) FormatLocaleString(buffer,MagickPathExtent,"%%%%Title: (%s)\n",
570           image->filename);
571         (void) WriteBlobString(image,buffer);
572         timer=time((time_t *) NULL);
573         (void) FormatMagickTime(timer,MagickPathExtent,date);
574         (void) FormatLocaleString(buffer,MagickPathExtent,
575           "%%%%CreationDate: (%s)\n",date);
576         (void) WriteBlobString(image,buffer);
577         bounds.x1=(double) geometry.x;
578         bounds.y1=(double) geometry.y;
579         bounds.x2=(double) geometry.x+geometry.width;
580         bounds.y2=(double) geometry.y+geometry.height+text_size;
581         if ((image_info->adjoin != MagickFalse) &&
582             (GetNextImageInList(image) != (Image *) NULL))
583           (void) CopyMagickString(buffer,"%%BoundingBox: (atend)\n",
584             MagickPathExtent);
585         else
586           {
587             (void) FormatLocaleString(buffer,MagickPathExtent,
588               "%%%%BoundingBox: %.20g %.20g %.20g %.20g\n",ceil(bounds.x1-0.5),
589               ceil(bounds.y1-0.5),floor(bounds.x2+0.5),floor(bounds.y2+0.5));
590             (void) WriteBlobString(image,buffer);
591             (void) FormatLocaleString(buffer,MagickPathExtent,
592               "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,
593               bounds.y1,bounds.x2,bounds.y2);
594           }
595         (void) WriteBlobString(image,buffer);
596         value=GetImageProperty(image,"label",exception);
597         if (value != (const char *) NULL)
598           (void) WriteBlobString(image,
599             "%%DocumentNeededResources: font Helvetica\n");
600         (void) WriteBlobString(image,"%%LanguageLevel: 2\n");
601         if (LocaleCompare(image_info->magick,"PS2") != 0)
602           (void) WriteBlobString(image,"%%Pages: 1\n");
603         else
604           {
605             (void) WriteBlobString(image,"%%Orientation: Portrait\n");
606             (void) WriteBlobString(image,"%%PageOrder: Ascend\n");
607             if (image_info->adjoin == MagickFalse)
608               (void) CopyMagickString(buffer,"%%Pages: 1\n",MagickPathExtent);
609             else
610               (void) FormatLocaleString(buffer,MagickPathExtent,
611                 "%%%%Pages: %.20g\n",(double) imageListLength);
612             (void) WriteBlobString(image,buffer);
613           }
614         if (image->colorspace == CMYKColorspace)
615           (void) WriteBlobString(image,
616             "%%DocumentProcessColors: Cyan Magenta Yellow Black\n");
617         (void) WriteBlobString(image,"%%EndComments\n");
618         (void) WriteBlobString(image,"\n%%BeginDefaults\n");
619         (void) WriteBlobString(image,"%%EndDefaults\n\n");
620         /*
621           Output Postscript commands.
622         */
623         for (q=PostscriptProlog; *q; q++)
624         {
625           switch (compression)
626           {
627             case NoCompression:
628             {
629               (void) FormatLocaleString(buffer,MagickPathExtent,*q,
630                 "/ASCII85Decode filter");
631               break;
632             }
633             case JPEGCompression:
634             {
635               (void) FormatLocaleString(buffer,MagickPathExtent,*q,
636                 "/DCTDecode filter");
637               break;
638             }
639             case LZWCompression:
640             {
641               (void) FormatLocaleString(buffer,MagickPathExtent,*q,
642                 "/LZWDecode filter");
643               break;
644             }
645             case FaxCompression:
646             case Group4Compression:
647             {
648               (void) FormatLocaleString(buffer,MagickPathExtent,*q," ");
649               break;
650             }
651             default:
652             {
653               (void) FormatLocaleString(buffer,MagickPathExtent,*q,
654                 "/RunLengthDecode filter");
655               break;
656             }
657           }
658           (void) WriteBlobString(image,buffer);
659           (void) WriteBlobByte(image,'\n');
660         }
661         value=GetImageProperty(image,"label",exception);
662         if (value != (const char *) NULL)
663           {
664             (void) WriteBlobString(image,
665               "  /Helvetica findfont pointsize scalefont setfont\n");
666             for (j=(ssize_t) MultilineCensus(value)-1; j >= 0; j--)
667             {
668               (void) WriteBlobString(image,"  /label 512 string def\n");
669               (void) WriteBlobString(image,
670                 "  currentfile label readline pop\n");
671               (void) FormatLocaleString(buffer,MagickPathExtent,
672                 "  0 y %g add moveto label show pop\n",j*pointsize+12);
673               (void) WriteBlobString(image,buffer);
674             }
675           }
676         for (q=PostscriptEpilog; *q; q++)
677         {
678           (void) FormatLocaleString(buffer,MagickPathExtent,"%s\n",*q);
679           (void) WriteBlobString(image,buffer);
680         }
681         if (LocaleCompare(image_info->magick,"PS2") == 0)
682           (void) WriteBlobString(image,"  showpage\n");
683         (void) WriteBlobString(image,"} bind def\n");
684         (void) WriteBlobString(image,"%%EndProlog\n");
685       }
686     (void) FormatLocaleString(buffer,MagickPathExtent,"%%%%Page:  1 %.20g\n",
687       (double) page++);
688     (void) WriteBlobString(image,buffer);
689     (void) FormatLocaleString(buffer,MagickPathExtent,
690       "%%%%PageBoundingBox: %.20g %.20g %.20g %.20g\n",(double) geometry.x,
691       (double) geometry.y,geometry.x+(double) geometry.width,geometry.y+(double)
692       (geometry.height+text_size));
693     (void) WriteBlobString(image,buffer);
694     if ((double) geometry.x < bounds.x1)
695       bounds.x1=(double) geometry.x;
696     if ((double) geometry.y < bounds.y1)
697       bounds.y1=(double) geometry.y;
698     if ((double) (geometry.x+geometry.width-1) > bounds.x2)
699       bounds.x2=(double) geometry.x+geometry.width-1;
700     if ((double) (geometry.y+(geometry.height+text_size)-1) > bounds.y2)
701       bounds.y2=(double) geometry.y+(geometry.height+text_size)-1;
702     value=GetImageProperty(image,"label",exception);
703     if (value != (const char *) NULL)
704       (void) WriteBlobString(image,"%%PageResources: font Helvetica\n");
705     if (LocaleCompare(image_info->magick,"PS2") != 0)
706       (void) WriteBlobString(image,"userdict begin\n");
707     start=TellBlob(image);
708     (void) FormatLocaleString(buffer,MagickPathExtent,
709       "%%%%BeginData:%13ld %s Bytes\n",0L,
710       compression == NoCompression ? "ASCII" : "Binary");
711     (void) WriteBlobString(image,buffer);
712     stop=TellBlob(image);
713     (void) WriteBlobString(image,"DisplayImage\n");
714     /*
715       Output image data.
716     */
717     (void) FormatLocaleString(buffer,MagickPathExtent,
718       "%.20g %.20g\n%g %g\n%g\n",(double) geometry.x,(double) geometry.y,
719       scale.x,scale.y,pointsize);
720     (void) WriteBlobString(image,buffer);
721     labels=(char **) NULL;
722     value=GetImageProperty(image,"label",exception);
723     if (value != (const char *) NULL)
724       labels=StringToList(value);
725     if (labels != (char **) NULL)
726       {
727         for (i=0; labels[i] != (char *) NULL; i++)
728         {
729           (void) FormatLocaleString(buffer,MagickPathExtent,"%s \n",
730             labels[i]);
731           (void) WriteBlobString(image,buffer);
732           labels[i]=DestroyString(labels[i]);
733         }
734         labels=(char **) RelinquishMagickMemory(labels);
735       }
736     number_pixels=(MagickSizeType) image->columns*image->rows;
737     if (number_pixels != (MagickSizeType) ((size_t) number_pixels))
738       ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
739     if ((compression == FaxCompression) || (compression == Group4Compression) ||
740         ((image_info->type != TrueColorType) &&
741          (SetImageGray(image,exception) != MagickFalse)))
742       {
743         (void) FormatLocaleString(buffer,MagickPathExtent,
744           "%.20g %.20g\n1\n%d\n",(double) image->columns,(double) image->rows,
745           (int) (image->colorspace == CMYKColorspace));
746         (void) WriteBlobString(image,buffer);
747         (void) FormatLocaleString(buffer,MagickPathExtent,"%d\n",(int)
748           ((compression != FaxCompression) &&
749            (compression != Group4Compression)));
750         (void) WriteBlobString(image,buffer);
751         (void) WriteBlobString(image,"0\n");
752         (void) FormatLocaleString(buffer,MagickPathExtent,"%d\n",
753            (compression == FaxCompression) ||
754            (compression == Group4Compression) ? 1 : 8);
755         (void) WriteBlobString(image,buffer);
756         switch (compression)
757         {
758           case FaxCompression:
759           case Group4Compression:
760           {
761             if (LocaleCompare(CCITTParam,"0") == 0)
762               {
763                 (void) HuffmanEncodeImage(image_info,image,image,exception);
764                 break;
765               }
766             (void) Huffman2DEncodeImage(image_info,image,image,exception);
767             break;
768           }
769           case JPEGCompression:
770           {
771             status=InjectImageBlob(image_info,image,image,"jpeg",exception);
772             if (status == MagickFalse)
773               {
774                 (void) CloseBlob(image);
775                 return(MagickFalse);
776               }
777             break;
778           }
779           case RLECompression:
780           default:
781           {
782             MemoryInfo
783               *pixel_info;
784
785             register unsigned char
786               *q;
787
788             /*
789               Allocate pixel array.
790             */
791             length=(size_t) number_pixels;
792             pixel_info=AcquireVirtualMemory(length,sizeof(*pixels));
793             if (pixel_info == (MemoryInfo *) NULL)
794               ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
795             pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
796             /*
797               Dump runlength encoded pixels.
798             */
799             q=pixels;
800             for (y=0; y < (ssize_t) image->rows; y++)
801             {
802               p=GetVirtualPixels(image,0,y,image->columns,1,exception);
803               if (p == (const Quantum *) NULL)
804                 break;
805               for (x=0; x < (ssize_t) image->columns; x++)
806               {
807                 *q++=ScaleQuantumToChar(ClampToQuantum(GetPixelLuma(image,p)));
808                 p+=GetPixelChannels(image);
809               }
810               progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
811                 image->rows);
812               if (progress == MagickFalse)
813                 break;
814             }
815             length=(size_t) (q-pixels);
816             if (compression == LZWCompression)
817               status=LZWEncodeImage(image,length,pixels,exception);
818             else
819               status=PackbitsEncodeImage(image,length,pixels,exception);
820             pixel_info=RelinquishVirtualMemory(pixel_info);
821             if (status == MagickFalse)
822               {
823                 (void) CloseBlob(image);
824                 return(MagickFalse);
825               }
826             break;
827           }
828           case NoCompression:
829           {
830             /*
831               Dump uncompressed PseudoColor packets.
832             */
833             Ascii85Initialize(image);
834             for (y=0; y < (ssize_t) image->rows; y++)
835             {
836               p=GetVirtualPixels(image,0,y,image->columns,1,exception);
837               if (p == (const Quantum *) NULL)
838                 break;
839               for (x=0; x < (ssize_t) image->columns; x++)
840               {
841                 Ascii85Encode(image,ScaleQuantumToChar(ClampToQuantum(
842                   GetPixelLuma(image,p))));
843                 p+=GetPixelChannels(image);
844               }
845               progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
846                 y,image->rows);
847               if (progress == MagickFalse)
848                 break;
849             }
850             Ascii85Flush(image);
851             break;
852           }
853         }
854       }
855     else
856       if ((image->storage_class == DirectClass) || (image->colors > 256) ||
857           (compression == JPEGCompression) || (image->alpha_trait != UndefinedPixelTrait))
858         {
859           (void) FormatLocaleString(buffer,MagickPathExtent,
860             "%.20g %.20g\n0\n%d\n",(double) image->columns,(double) image->rows,
861             (int) (image->colorspace == CMYKColorspace));
862           (void) WriteBlobString(image,buffer);
863           (void) FormatLocaleString(buffer,MagickPathExtent,"%d\n",
864             (int) (compression == NoCompression));
865           (void) WriteBlobString(image,buffer);
866           switch (compression)
867           {
868             case JPEGCompression:
869             {
870               status=InjectImageBlob(image_info,image,image,"jpeg",exception);
871               if (status == MagickFalse)
872                 {
873                   (void) CloseBlob(image);
874                   return(MagickFalse);
875                 }
876               break;
877             }
878             case RLECompression:
879             default:
880             {
881               MemoryInfo
882                 *pixel_info;
883
884               register unsigned char
885                 *q;
886
887               /*
888                 Allocate pixel array.
889               */
890               length=(size_t) number_pixels;
891               pixel_info=AcquireVirtualMemory(length,4*sizeof(*pixels));
892               if (pixel_info == (MemoryInfo *) NULL)
893                 ThrowWriterException(ResourceLimitError,
894                   "MemoryAllocationFailed");
895               pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
896               /*
897                 Dump runlength encoded pixels.
898               */
899               q=pixels;
900               for (y=0; y < (ssize_t) image->rows; y++)
901               {
902                 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
903                 if (p == (const Quantum *) NULL)
904                   break;
905                 for (x=0; x < (ssize_t) image->columns; x++)
906                 {
907                   if ((image->alpha_trait != UndefinedPixelTrait) &&
908                       (GetPixelAlpha(image,p) == (Quantum) TransparentAlpha))
909                     {
910                       *q++=ScaleQuantumToChar(QuantumRange);
911                       *q++=ScaleQuantumToChar(QuantumRange);
912                       *q++=ScaleQuantumToChar(QuantumRange);
913                     }
914                   else
915                     if (image->colorspace != CMYKColorspace)
916                       {
917                         *q++=ScaleQuantumToChar(GetPixelRed(image,p));
918                         *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
919                         *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
920                       }
921                     else
922                       {
923                         *q++=ScaleQuantumToChar(GetPixelRed(image,p));
924                         *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
925                         *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
926                         *q++=ScaleQuantumToChar(GetPixelBlack(image,p));
927                       }
928                   p+=GetPixelChannels(image);
929                 }
930                 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
931                   y,image->rows);
932                 if (progress == MagickFalse)
933                   break;
934               }
935               length=(size_t) (q-pixels);
936               if (compression == LZWCompression)
937                 status=LZWEncodeImage(image,length,pixels,exception);
938               else
939                 status=PackbitsEncodeImage(image,length,pixels,exception);
940               if (status == MagickFalse)
941                 {
942                   (void) CloseBlob(image);
943                   return(MagickFalse);
944                 }
945               pixel_info=RelinquishVirtualMemory(pixel_info);
946               break;
947             }
948             case NoCompression:
949             {
950               /*
951                 Dump uncompressed DirectColor packets.
952               */
953               Ascii85Initialize(image);
954               for (y=0; y < (ssize_t) image->rows; y++)
955               {
956                 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
957                 if (p == (const Quantum *) NULL)
958                   break;
959                 for (x=0; x < (ssize_t) image->columns; x++)
960                 {
961                   if ((image->alpha_trait != UndefinedPixelTrait) &&
962                       (GetPixelAlpha(image,p) == (Quantum) TransparentAlpha))
963                     {
964                       Ascii85Encode(image,ScaleQuantumToChar((Quantum)
965                         QuantumRange));
966                       Ascii85Encode(image,ScaleQuantumToChar((Quantum)
967                         QuantumRange));
968                       Ascii85Encode(image,ScaleQuantumToChar((Quantum)
969                         QuantumRange));
970                     }
971                   else
972                     if (image->colorspace != CMYKColorspace)
973                       {
974                         Ascii85Encode(image,ScaleQuantumToChar(
975                           GetPixelRed(image,p)));
976                         Ascii85Encode(image,ScaleQuantumToChar(
977                           GetPixelGreen(image,p)));
978                         Ascii85Encode(image,ScaleQuantumToChar(
979                           GetPixelBlue(image,p)));
980                       }
981                     else
982                       {
983                         Ascii85Encode(image,ScaleQuantumToChar(
984                           GetPixelRed(image,p)));
985                         Ascii85Encode(image,ScaleQuantumToChar(
986                           GetPixelGreen(image,p)));
987                         Ascii85Encode(image,ScaleQuantumToChar(
988                           GetPixelBlue(image,p)));
989                         Ascii85Encode(image,ScaleQuantumToChar(
990                           GetPixelBlack(image,p)));
991                       }
992                   p+=GetPixelChannels(image);
993                 }
994                 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
995                   y,image->rows);
996                 if (progress == MagickFalse)
997                   break;
998               }
999               Ascii85Flush(image);
1000               break;
1001             }
1002           }
1003         }
1004       else
1005         {
1006           /*
1007             Dump number of colors and colormap.
1008           */
1009           (void) FormatLocaleString(buffer,MagickPathExtent,
1010             "%.20g %.20g\n1\n%d\n",(double) image->columns,(double) image->rows,
1011             (int) (image->colorspace == CMYKColorspace));
1012           (void) WriteBlobString(image,buffer);
1013           (void) FormatLocaleString(buffer,MagickPathExtent,"%d\n",
1014             (int) (compression == NoCompression));
1015           (void) WriteBlobString(image,buffer);
1016           (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g\n",(double)
1017             image->colors);
1018           (void) WriteBlobString(image,buffer);
1019           for (i=0; i < (ssize_t) image->colors; i++)
1020           {
1021             (void) FormatLocaleString(buffer,MagickPathExtent,"%02X%02X%02X\n",
1022               ScaleQuantumToChar(image->colormap[i].red),
1023               ScaleQuantumToChar(image->colormap[i].green),
1024               ScaleQuantumToChar(image->colormap[i].blue));
1025             (void) WriteBlobString(image,buffer);
1026           }
1027           switch (compression)
1028           {
1029             case RLECompression:
1030             default:
1031             {
1032               MemoryInfo
1033                 *pixel_info;
1034
1035               register unsigned char
1036                 *q;
1037
1038               /*
1039                 Allocate pixel array.
1040               */
1041               length=(size_t) number_pixels;
1042               pixel_info=AcquireVirtualMemory(length,sizeof(*pixels));
1043               if (pixel_info == (MemoryInfo *) NULL)
1044                 ThrowWriterException(ResourceLimitError,
1045                   "MemoryAllocationFailed");
1046               pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
1047               /*
1048                 Dump runlength encoded pixels.
1049               */
1050               q=pixels;
1051               for (y=0; y < (ssize_t) image->rows; y++)
1052               {
1053                 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1054                 if (p == (const Quantum *) NULL)
1055                   break;
1056                 for (x=0; x < (ssize_t) image->columns; x++)
1057                 {
1058                   *q++=(unsigned char) GetPixelIndex(image,p);
1059                   p+=GetPixelChannels(image);
1060                 }
1061                 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1062                   y,image->rows);
1063                 if (progress == MagickFalse)
1064                   break;
1065               }
1066               length=(size_t) (q-pixels);
1067               if (compression == LZWCompression)
1068                 status=LZWEncodeImage(image,length,pixels,exception);
1069               else
1070                 status=PackbitsEncodeImage(image,length,pixels,exception);
1071               pixel_info=RelinquishVirtualMemory(pixel_info);
1072               if (status == MagickFalse)
1073                 {
1074                   (void) CloseBlob(image);
1075                   return(MagickFalse);
1076                 }
1077               break;
1078             }
1079             case NoCompression:
1080             {
1081               /*
1082                 Dump uncompressed PseudoColor packets.
1083               */
1084               Ascii85Initialize(image);
1085               for (y=0; y < (ssize_t) image->rows; y++)
1086               {
1087                 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1088                 if (p == (const Quantum *) NULL)
1089                   break;
1090                 for (x=0; x < (ssize_t) image->columns; x++)
1091                 {
1092                   Ascii85Encode(image,(unsigned char) GetPixelIndex(image,p));
1093                   p+=GetPixelChannels(image);
1094                 }
1095                 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1096                   y,image->rows);
1097                 if (progress == MagickFalse)
1098                   break;
1099               }
1100               Ascii85Flush(image);
1101               break;
1102             }
1103           }
1104         }
1105     (void) WriteBlobByte(image,'\n');
1106     length=(size_t) (TellBlob(image)-stop);
1107     stop=TellBlob(image);
1108     offset=SeekBlob(image,start,SEEK_SET);
1109     if (offset < 0)
1110       ThrowWriterException(CorruptImageError,"ImproperImageHeader");
1111     (void) FormatLocaleString(buffer,MagickPathExtent,
1112       "%%%%BeginData:%13ld %s Bytes\n",(long) length,
1113       compression == NoCompression ? "ASCII" : "Binary");
1114     (void) WriteBlobString(image,buffer);
1115     offset=SeekBlob(image,stop,SEEK_SET);
1116     (void) WriteBlobString(image,"%%EndData\n");
1117     if (LocaleCompare(image_info->magick,"PS2") != 0)
1118       (void) WriteBlobString(image,"end\n");
1119     (void) WriteBlobString(image,"%%PageTrailer\n");
1120     if (GetNextImageInList(image) == (Image *) NULL)
1121       break;
1122     image=SyncNextImageInList(image);
1123     status=SetImageProgress(image,SaveImagesTag,scene++,imageListLength);
1124     if (status == MagickFalse)
1125       break;
1126   } while (image_info->adjoin != MagickFalse);
1127   (void) WriteBlobString(image,"%%Trailer\n");
1128   if (page > 1)
1129     {
1130       (void) FormatLocaleString(buffer,MagickPathExtent,
1131         "%%%%BoundingBox: %.20g %.20g %.20g %.20g\n",ceil(bounds.x1-0.5),
1132         ceil(bounds.y1-0.5),floor(bounds.x2+0.5),floor(bounds.y2+0.5));
1133       (void) WriteBlobString(image,buffer);
1134       (void) FormatLocaleString(buffer,MagickPathExtent,
1135         "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,bounds.y1,
1136         bounds.x2,bounds.y2);
1137       (void) WriteBlobString(image,buffer);
1138     }
1139   (void) WriteBlobString(image,"%%EOF\n");
1140   (void) CloseBlob(image);
1141   return(MagickTrue);
1142 }