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