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