]> granicus.if.org Git - imagemagick/blob - coders/ps3.c
(no commit message)
[imagemagick] / coders / ps3.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                            PPPP   SSSSS  33333                              %
7 %                            P   P  SS        33                              %
8 %                            PPPP    SSS    333                               %
9 %                            P         SS     33                              %
10 %                            P      SSSSS  33333                              %
11 %                                                                             %
12 %                                                                             %
13 %                     Write Postscript Level III Format                       %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                John Cristy                                  %
17 %                              Lars Ruben Skyum                               %
18 %                                 July 1992                                   %
19 %                                                                             %
20 %                                                                             %
21 %  Copyright 1999-2009 ImageMagick Studio LLC, a non-profit organization      %
22 %  dedicated to making software imaging solutions freely available.           %
23 %                                                                             %
24 %  You may not use this file except in compliance with the License.  You may  %
25 %  obtain a copy of the License at                                            %
26 %                                                                             %
27 %    http://www.imagemagick.org/script/license.php                            %
28 %                                                                             %
29 %  Unless required by applicable law or agreed to in writing, software        %
30 %  distributed under the License is distributed on an "AS IS" BASIS,          %
31 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
32 %  See the License for the specific language governing permissions and        %
33 %  limitations under the License.                                             %
34 %                                                                             %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 %
37 %
38 */
39 \f
40 /*
41   Include declarations.
42 */
43 #include "magick/studio.h"
44 #include "magick/blob.h"
45 #include "magick/blob-private.h"
46 #include "magick/cache.h"
47 #include "magick/color.h"
48 #include "magick/color-private.h"
49 #include "magick/compress.h"
50 #include "magick/constitute.h"
51 #include "magick/draw.h"
52 #include "magick/exception.h"
53 #include "magick/exception-private.h"
54 #include "magick/geometry.h"
55 #include "magick/image.h"
56 #include "magick/image-private.h"
57 #include "magick/list.h"
58 #include "magick/magick.h"
59 #include "magick/memory_.h"
60 #include "magick/monitor.h"
61 #include "magick/monitor-private.h"
62 #include "magick/option.h"
63 #include "magick/property.h"
64 #include "magick/quantum-private.h"
65 #include "magick/resource_.h"
66 #include "magick/static.h"
67 #include "magick/string_.h"
68 #include "magick/module.h"
69 #include "magick/token.h"
70 #include "magick/utility.h"
71 #include "magick/module.h"
72 \f
73 /*
74   Define declarations.
75 */
76 #define PS3_NoCompression "0"
77 #define PS3_FaxCompression "1"
78 #define PS3_JPEGCompression "2"
79 #define PS3_LZWCompression "3"
80 #define PS3_RLECompression "4"
81 #define PS3_ZipCompression "5"
82
83 #define PS3_RGBColorspace "0"
84 #define PS3_CMYKColorspace "1"
85
86 #define PS3_DirectClass "0"
87 #define PS3_PseudoClass "1"
88
89 #if defined(MAGICKCORE_TIFF_DELEGATE)
90 #define CCITTParam  "-1"
91 #else
92 #define CCITTParam  "0"
93 #endif
94 \f
95 /*
96   Forward declarations.
97 */
98 static MagickBooleanType
99   WritePS3Image(const ImageInfo *,Image *);
100 \f
101 /*
102 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103 %                                                                             %
104 %                                                                             %
105 %                                                                             %
106 %   R e g i s t e r P S 3 I m a g e                                           %
107 %                                                                             %
108 %                                                                             %
109 %                                                                             %
110 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111 %
112 %  RegisterPS3Image() adds properties for the PS3 image format to the list of
113 %  supported formats.  The properties include the image format tag, a method to
114 %  read and/or write the format, whether the format supports the saving of more
115 %  than one frame to the same file or blob, whether the format supports native
116 %  in-memory I/O, and a brief description of the format.
117 %
118 %  The format of the RegisterPS3Image method is:
119 %
120 %      unsigned long RegisterPS3Image(void)
121 %
122 */
123 ModuleExport unsigned long RegisterPS3Image(void)
124 {
125   MagickInfo
126     *entry;
127
128   entry=SetMagickInfo("EPS3");
129   entry->encoder=(EncodeImageHandler *) WritePS3Image;
130   entry->description=ConstantString("Level III Encapsulated PostScript");
131   entry->module=ConstantString("PS3");
132   (void) RegisterMagickInfo(entry);
133   entry=SetMagickInfo("PS3");
134   entry->encoder=(EncodeImageHandler *) WritePS3Image;
135   entry->description=ConstantString("Level III PostScript");
136   entry->module=ConstantString("PS3");
137   (void) RegisterMagickInfo(entry);
138   return(MagickImageCoderSignature);
139 }
140 \f
141 /*
142 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143 %                                                                             %
144 %                                                                             %
145 %                                                                             %
146 %   U n r e g i s t e r P S 3 I m a g e                                       %
147 %                                                                             %
148 %                                                                             %
149 %                                                                             %
150 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151 %
152 %  UnregisterPS3Image() removes format registrations made by the PS3 module
153 %  from the list of supported formats.
154 %
155 %  The format of the UnregisterPS3Image method is:
156 %
157 %      UnregisterPS3Image(void)
158 %
159 */
160 ModuleExport void UnregisterPS3Image(void)
161 {
162   (void) UnregisterMagickInfo("EPS3");
163   (void) UnregisterMagickInfo("PS3");
164 }
165 \f
166 /*
167 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
168 %                                                                             %
169 %                                                                             %
170 %                                                                             %
171 %   W r i t e P S 3 I m a g e                                                 %
172 %                                                                             %
173 %                                                                             %
174 %                                                                             %
175 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
176 %
177 %  WritePS3Image() translates an image to encapsulated Postscript Level III
178 %  for printing.  If the supplied geometry is null, the image is centered on
179 %  the Postscript page.  Otherwise, the image is positioned as specified by the
180 %  geometry.
181 %
182 %  The format of the WritePS3Image method is:
183 %
184 %      MagickBooleanType WritePS3Image(const ImageInfo *image_info,Image *image)
185 %
186 %  A description of each parameter follows:
187 %
188 %    o image_info: Specifies a pointer to a ImageInfo structure.
189 %
190 %    o image: the image.
191 %
192 */
193
194 static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
195   Image *image,Image *inject_image)
196 {
197   Image
198     *group4_image;
199
200   ImageInfo
201     *write_info;
202
203   MagickBooleanType
204     status;
205
206   size_t
207     length;
208
209   unsigned char
210     *group4;
211
212   write_info=CloneImageInfo(image_info);
213   (void) CopyMagickString(write_info->filename,"GROUP4:",MaxTextExtent);
214   (void) CopyMagickString(write_info->magick,"GROUP4",MaxTextExtent);
215   group4_image=CloneImage(inject_image,0,0,MagickTrue,&image->exception);
216   if (group4_image == (Image *) NULL)
217     return(MagickFalse);
218   group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
219     &image->exception);
220   group4_image=DestroyImage(group4_image);
221   if (group4 == (unsigned char *) NULL)
222     return(MagickFalse);
223   write_info=DestroyImageInfo(write_info);
224   if (WriteBlob(image,length,group4) != (ssize_t) length)
225     status=MagickFalse;
226   group4=(unsigned char *) RelinquishMagickMemory(group4);
227   return(status);
228 }
229
230
231 static MagickBooleanType SerializeImage(const ImageInfo *image_info,
232   Image *image,unsigned char **pixels,size_t *length)
233 {
234   long
235     y;
236
237   MagickBooleanType
238     status;
239
240   register const IndexPacket
241     *indexes;
242
243   register const PixelPacket
244     *p;
245
246   register long
247     x;
248
249   register unsigned char
250     *q;
251
252   assert(image != (Image *) NULL);
253   assert(image->signature == MagickSignature);
254   if (image->debug != MagickFalse)
255     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
256   status=MagickTrue;
257   *length=(image->colorspace == CMYKColorspace ? 4 : 3)*
258     (size_t) image->columns*image->rows;
259   *pixels=(unsigned char *) AcquireQuantumMemory(*length,sizeof(**pixels));
260   if (*pixels == (unsigned char *) NULL)
261     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
262   q=(*pixels);
263   for (y=0; y < (long) image->rows; y++)
264   {
265     p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
266     if (p == (const PixelPacket *) NULL)
267       break;
268     indexes=GetVirtualIndexQueue(image);
269     if (image->colorspace != CMYKColorspace)
270       for (x=0; x < (long) image->columns; x++)
271       {
272         *q++=ScaleQuantumToChar(p->red);
273         *q++=ScaleQuantumToChar(p->green);
274         *q++=ScaleQuantumToChar(p->blue);
275         p++;
276       }
277     else
278       for (x=0; x < (long) image->columns; x++)
279       {
280         *q++=ScaleQuantumToChar(p->red);
281         *q++=ScaleQuantumToChar(p->green);
282         *q++=ScaleQuantumToChar(p->blue);
283         *q++=ScaleQuantumToChar(indexes[x]);
284         p++;
285       }
286     if (image->previous == (Image *) NULL)
287       {
288         status=SetImageProgress(image,SaveImageTag,y,image->rows);
289         if (status == MagickFalse)
290           break;
291       }
292   }
293   if (status == MagickFalse)
294     *pixels=(unsigned char *) RelinquishMagickMemory(*pixels);
295   return(status);
296 }
297
298 static MagickBooleanType SerializeImageChannel(const ImageInfo *image_info,
299   Image *image,unsigned char **pixels,size_t *length)
300 {
301   long
302     y;
303
304   MagickBooleanType
305     status;
306
307   register const PixelPacket
308     *p;
309
310   register long
311     x;
312
313   register unsigned char
314     *q;
315
316   unsigned char
317     code,
318     bit;
319
320   unsigned long
321     pack,
322     padded_columns;
323
324   assert(image != (Image *) NULL);
325   assert(image->signature == MagickSignature);
326   if (image->debug != MagickFalse)
327     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
328   status=MagickTrue;
329   pack=IsMonochromeImage(image,&image->exception) == MagickFalse ? 1UL : 8UL;
330   padded_columns=((image->columns+pack-1)/pack)*pack;
331   *length=(size_t) padded_columns*image->rows/pack;
332   *pixels=(unsigned char *) AcquireQuantumMemory(*length,sizeof(**pixels));
333   if (*pixels == (unsigned char *) NULL)
334     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
335   q=(*pixels);
336   for (y=0; y < (long) image->rows; y++)
337   {
338     p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
339     if (p == (const PixelPacket *) NULL)
340       break;
341     if (pack == 1)
342       for (x=0; x < (long) image->columns; x++)
343       {
344         *q++=ScaleQuantumToChar(PixelIntensityToQuantum(p));
345         p++;
346       }
347     else
348       {
349         code='\0';
350         for (x=0; x < (long) padded_columns; x++)
351         {
352           bit=(unsigned char) 0x00;
353           if (x < (long) image->columns)
354             bit=(unsigned char) (PixelIntensityToQuantum(p) ==
355               (Quantum) TransparentOpacity ? 0x01 : 0x00);
356           code=(code << 1)+bit;
357           if (((x+1) % pack) == 0)
358             {
359               *q++=code;
360               code='\0';
361             }
362           p++;
363         }
364       }
365     status=SetImageProgress(image,SaveImageTag,y,image->rows);
366     if (status == MagickFalse)
367       break;
368   }
369   if (status == MagickFalse)
370     *pixels=(unsigned char *) RelinquishMagickMemory(*pixels);
371   return(status);
372 }
373
374 static MagickBooleanType SerializeImageIndexes(const ImageInfo *image_info,
375   Image *image,unsigned char **pixels,size_t *length)
376 {
377   long
378     y;
379
380   MagickBooleanType
381     status;
382
383   register const IndexPacket
384     *indexes;
385
386   register const PixelPacket
387     *p;
388
389   register long
390     x;
391
392   register unsigned char
393     *q;
394
395   assert(image != (Image *) NULL);
396   assert(image->signature == MagickSignature);
397   if (image->debug != MagickFalse)
398     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
399   status=MagickTrue;
400   *length=(size_t) image->columns*image->rows;
401   *pixels=(unsigned char *) AcquireQuantumMemory(*length,sizeof(**pixels));
402   if (*pixels == (unsigned char *) NULL)
403     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
404   q=(*pixels);
405   for (y=0; y < (long) image->rows; y++)
406   {
407     p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
408     if (p == (const PixelPacket *) NULL)
409       break;
410     indexes=GetVirtualIndexQueue(image);
411     for (x=0; x < (long) image->columns; x++)
412       *q++=(unsigned char) indexes[x];
413     if (image->previous == (Image *) NULL)
414       {
415         status=SetImageProgress(image,SaveImageTag,y,image->rows);
416         if (status == MagickFalse)
417           break;
418       }
419   }
420   if (status == MagickFalse)
421     *pixels=(unsigned char *) RelinquishMagickMemory(*pixels);
422   return(status);
423 }
424
425 static MagickBooleanType WritePS3MaskImage(const ImageInfo *image_info,
426   Image *image,const CompressionType compression)
427 {
428   char
429     buffer[MaxTextExtent];
430
431   Image
432     *mask_image;
433
434   MagickBooleanType
435     status;
436
437   MagickOffsetType
438     offset,
439     start,
440     stop;
441
442   register long
443     i;
444
445   size_t
446     length;
447
448   unsigned char
449     *pixels;
450
451   assert(image_info != (ImageInfo *) NULL);
452   assert(image_info->signature == MagickSignature);
453   assert(image != (Image *) NULL);
454   assert(image->signature == MagickSignature);
455   if (image->debug != MagickFalse)
456     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
457   assert(image->matte != MagickFalse);
458   status=MagickTrue;
459   /*
460     Note BeginData DSC comment for update later.
461   */
462   start=TellBlob(image);
463   (void) FormatMagickString(buffer,MaxTextExtent,
464     "%%%%BeginData:%13ld %s Bytes\n",0L,
465     compression == NoCompression ? "ASCII" : "BINARY");
466   (void) WriteBlobString(image,buffer);
467   stop=TellBlob(image);
468   /*
469     Only lossless compressions for the mask.
470   */
471   switch (compression)
472   {
473     case NoCompression:
474     default:
475     {
476       (void) FormatMagickString(buffer,MaxTextExtent,
477         "currentfile %lu %lu "PS3_NoCompression" ByteStreamDecodeFilter\n",
478         image->columns,image->rows);
479       break;
480     }
481     case FaxCompression:
482     case Group4Compression:
483     {
484       (void) FormatMagickString(buffer,MaxTextExtent,
485         "currentfile %lu %lu "PS3_FaxCompression" ByteStreamDecodeFilter\n",
486         image->columns,image->rows);
487       break;
488     }
489     case LZWCompression:
490     {
491       (void) FormatMagickString(buffer,MaxTextExtent,
492         "currentfile %lu %lu "PS3_LZWCompression" ByteStreamDecodeFilter\n",
493         image->columns,image->rows);
494       break;
495     }
496     case RLECompression:
497     {
498       (void) FormatMagickString(buffer,MaxTextExtent,
499         "currentfile %lu %lu "PS3_RLECompression" ByteStreamDecodeFilter\n",
500         image->columns,image->rows);
501       break;
502     }
503     case ZipCompression:
504     {
505       (void) FormatMagickString(buffer,MaxTextExtent,
506         "currentfile %lu %lu "PS3_ZipCompression" ByteStreamDecodeFilter\n",
507         image->columns,image->rows);
508       break;
509     }
510   }
511   (void) WriteBlobString(image,buffer);
512   (void) WriteBlobString(image,"/ReusableStreamDecode filter\n");
513   mask_image=CloneImage(image,0,0,MagickTrue,&image->exception);
514   if (mask_image == (Image *) NULL)
515     ThrowWriterException(CoderError,image->exception.reason);
516   status=SeparateImageChannel(mask_image,OpacityChannel);
517   if (status == MagickFalse)
518     {
519       mask_image=DestroyImage(mask_image);
520       return(MagickFalse);
521     }
522   (void) SetImageType(mask_image,BilevelType);
523   (void) SetImageType(mask_image,PaletteType);
524   mask_image->matte=MagickFalse;
525   pixels=(unsigned char *) NULL;
526   length=0;
527   switch (compression)
528   {
529     case NoCompression:
530     default:
531     {
532       status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
533       if (status == MagickFalse)
534         break;
535       Ascii85Initialize(image);
536       for (i=0; i < (long) length; i++)
537         Ascii85Encode(image,pixels[i]);
538       Ascii85Flush(image);
539       pixels=(unsigned char *) RelinquishMagickMemory(pixels);
540       break;
541     }
542     case FaxCompression:
543     case Group4Compression:
544     {
545       if ((compression == FaxCompression) ||
546           (LocaleCompare(CCITTParam,"0") == 0))
547         status=HuffmanEncodeImage(image_info,image,mask_image);
548       else
549         status=Huffman2DEncodeImage(image_info,image,mask_image);
550       break;
551     }
552     case LZWCompression:
553     {
554       status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
555       if (status == MagickFalse)
556         break;
557       status=LZWEncodeImage(image,length,pixels);
558       pixels=(unsigned char *) RelinquishMagickMemory(pixels);
559       break;
560     }
561     case RLECompression:
562     {
563       status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
564       if (status == MagickFalse)
565         break;
566       status=PackbitsEncodeImage(image,length,pixels);
567       pixels=(unsigned char *) RelinquishMagickMemory(pixels);
568       break;
569     }
570     case ZipCompression:
571     {
572       status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
573       if (status == MagickFalse)
574         break;
575       status=ZLIBEncodeImage(image,length,pixels);
576       pixels=(unsigned char *) RelinquishMagickMemory(pixels);
577       break;
578     }
579   }
580   mask_image=DestroyImage(mask_image);
581   (void) WriteBlobByte(image,'\n');
582   length=(size_t) (TellBlob(image)-stop);
583   stop=TellBlob(image);
584   offset=SeekBlob(image,start,SEEK_SET);
585   if (offset < 0)
586     ThrowWriterException(CorruptImageError,"ImproperImageHeader");
587   (void) FormatMagickString(buffer,MaxTextExtent,
588     "%%%%BeginData:%13ld %s Bytes\n",(long) length,
589     compression == NoCompression ? "ASCII" : "BINARY");
590   (void) WriteBlobString(image,buffer);
591   offset=SeekBlob(image,stop,SEEK_SET);
592   if (offset < 0)
593     ThrowWriterException(CorruptImageError,"ImproperImageHeader");
594   (void) WriteBlobString(image,"%%EndData\n");
595   (void) WriteBlobString(image, "/mask_stream exch def\n");
596   return(status);
597 }
598
599 static MagickBooleanType WritePS3Image(const ImageInfo *image_info,Image *image)
600 {
601   static const char
602     *PostscriptProlog[]=
603     {
604       "/ByteStreamDecodeFilter",
605       "{",
606       "  /z exch def",
607       "  /r exch def",
608       "  /c exch def",
609       "  z "PS3_NoCompression" eq { /ASCII85Decode filter } if",
610       "  z "PS3_FaxCompression" eq",
611       "  {",
612       "    <<",
613       "      /K "CCITTParam,
614       "      /Columns c",
615       "      /Rows r",
616       "    >>",
617       "    /CCITTFaxDecode filter",
618       "  } if",
619       "  z "PS3_JPEGCompression" eq { /DCTDecode filter } if",
620       "  z "PS3_LZWCompression" eq { /LZWDecode filter } if",
621       "  z "PS3_RLECompression" eq { /RunLengthDecode filter } if",
622       "  z "PS3_ZipCompression" eq { /FlateDecode filter } if",
623       "} bind def",
624       "",
625       "/DirectClassImageDict",
626       "{",
627       "  colorspace "PS3_RGBColorspace" eq",
628       "  {",
629       "    /DeviceRGB setcolorspace",
630       "    <<",
631       "      /ImageType 1",
632       "      /Width columns",
633       "      /Height rows",
634       "      /BitsPerComponent 8",
635       "      /DataSource pixel_stream",
636       "      /MultipleDataSources false",
637       "      /ImageMatrix [columns 0 0 rows neg 0 rows]",
638       "      /Decode [0 1 0 1 0 1]",
639       "    >>",
640       "  }",
641       "  {",
642       "    /DeviceCMYK setcolorspace",
643       "    <<",
644       "      /ImageType 1",
645       "      /Width columns",
646       "      /Height rows",
647       "      /BitsPerComponent 8",
648       "      /DataSource pixel_stream",
649       "      /MultipleDataSources false",
650       "      /ImageMatrix [columns 0 0 rows neg 0 rows]",
651       "      /Decode",
652       "        compression "PS3_JPEGCompression" eq",
653       "        { [1 0 1 0 1 0 1 0] }",
654       "        { [0 1 0 1 0 1 0 1] }",
655       "        ifelse",
656       "    >>",
657       "  }",
658       "  ifelse",
659       "} bind def",
660       "",
661       "/PseudoClassImageDict",
662       "{",
663       "  % Colors in colormap image.",
664       "  currentfile buffer readline pop",
665       "  token pop /colors exch def pop",
666       "  colors 0 eq",
667       "  {",
668       "    % Depth of grayscale image.",
669       "    currentfile buffer readline pop",
670       "    token pop /bits exch def pop",
671       "    /DeviceGray setcolorspace",
672       "    <<",
673       "      /ImageType 1",
674       "      /Width columns",
675       "      /Height rows",
676       "      /BitsPerComponent bits",
677       "      /Decode [0 1]",
678       "      /ImageMatrix [columns 0 0 rows neg 0 rows]",
679       "      /DataSource pixel_stream",
680       "    >>",
681       "  }",
682       "  {",
683       "    % RGB colormap.",
684       "    /colormap colors 3 mul string def",
685       "    compression "PS3_NoCompression" eq",
686       "    { currentfile /ASCII85Decode filter colormap readstring pop pop }",
687       "    { currentfile colormap readstring pop pop }",
688       "    ifelse",
689       "    [ /Indexed /DeviceRGB colors 1 sub colormap ] setcolorspace",
690       "    <<",
691       "      /ImageType 1",
692       "      /Width columns",
693       "      /Height rows",
694       "      /BitsPerComponent 8",
695       "      /Decode [0 255]",
696       "      /ImageMatrix [columns 0 0 rows neg 0 rows]",
697       "      /DataSource pixel_stream",
698       "    >>",
699       "  }",
700       "  ifelse",
701       "} bind def",
702       "",
703       "/NonMaskedImageDict",
704       "{",
705       "  class "PS3_PseudoClass" eq",
706       "  { PseudoClassImageDict }",
707       "  { DirectClassImageDict }",
708       "  ifelse",
709       "} bind def",
710       "",
711       "/MaskedImageDict",
712       "{",
713       "  <<",
714       "    /ImageType 3",
715       "    /InterleaveType 3",
716       "    /DataDict NonMaskedImageDict",
717       "    /MaskDict",
718       "    <<",
719       "      /ImageType 1",
720       "      /Width columns",
721       "      /Height rows",
722       "      /BitsPerComponent 1",
723       "      /DataSource mask_stream",
724       "      /MultipleDataSources false",
725       "      /ImageMatrix [ columns 0 0 rows neg 0 rows]",
726       "      /Decode [ 0 1 ]",
727       "    >>",
728       "  >>",
729       "} bind def",
730       "",
731       "/ClipImage",
732       "{} def",
733       "",
734       "/DisplayImage",
735       "{",
736       "  /buffer 512 string def",
737       "  % Translation.",
738       "  currentfile buffer readline pop",
739       "  token pop /x exch def",
740       "  token pop /y exch def pop",
741       "  x y translate",
742       "  % Image size and font size.",
743       "  currentfile buffer readline pop",
744       "  token pop /x exch def",
745       "  token pop /y exch def pop",
746       "  currentfile buffer readline pop",
747       "  token pop /pointsize exch def pop",
748       (char *) NULL
749     },
750     *PostscriptEpilog[]=
751     {
752       "  x y scale",
753       "  % Clipping path.",
754       "  currentfile buffer readline pop",
755       "  token pop /clipped exch def pop",
756       "  % Showpage.",
757       "  currentfile buffer readline pop",
758       "  token pop /sp exch def pop",
759       "  % Image pixel size.",
760       "  currentfile buffer readline pop",
761       "  token pop /columns exch def",
762       "  token pop /rows exch def pop",
763       "  % Colorspace (RGB/CMYK).",
764       "  currentfile buffer readline pop",
765       "  token pop /colorspace exch def pop",
766       "  % Transparency.",
767       "  currentfile buffer readline pop",
768       "  token pop /alpha exch def pop",
769       "  % Stencil mask?",
770       "  currentfile buffer readline pop",
771       "  token pop /stencil exch def pop",
772       "  % Image class (direct/pseudo).",
773       "  currentfile buffer readline pop",
774       "  token pop /class exch def pop",
775       "  % Compression type.",
776       "  currentfile buffer readline pop",
777       "  token pop /compression exch def pop",
778       "  % Clip and render.",
779       "  /pixel_stream currentfile columns rows compression ByteStreamDecodeFilter def",
780       "  clipped { ClipImage } if",
781       "  alpha stencil not and",
782       "  { MaskedImageDict mask_stream resetfile }",
783       "  { NonMaskedImageDict }",
784       "  ifelse",
785       "  stencil { 0 setgray imagemask } { image } ifelse",
786       "  sp { showpage } if",
787       "} bind def",
788       (char *) NULL
789     };
790
791   char
792     buffer[MaxTextExtent],
793     date[MaxTextExtent],
794     **labels,
795     page_geometry[MaxTextExtent];
796
797   CompressionType
798     compression;
799
800   const char
801     *option,
802     **q,
803     *value;
804
805   double
806     pointsize;
807
808   GeometryInfo
809     geometry_info;
810
811   long
812     j;
813
814   MagickBooleanType
815     status;
816
817   MagickOffsetType
818     offset,
819     scene,
820     start,
821     stop;
822
823   MagickStatusType
824     flags;
825
826   PointInfo
827     delta,
828     resolution,
829     scale;
830
831   RectangleInfo
832     geometry,
833     media_info,
834     page_info;
835
836   register long
837     i;
838
839   SegmentInfo
840     bounds;
841
842   size_t
843     length;
844
845   time_t
846     timer;
847
848   unsigned char
849     *pixels;
850
851   unsigned long
852     page,
853     pixel,
854     text_size;
855
856   /*
857     Open output image file.
858   */
859   assert(image_info != (const ImageInfo *) NULL);
860   assert(image_info->signature == MagickSignature);
861   assert(image != (Image *) NULL);
862   assert(image->signature == MagickSignature);
863   if (image->debug != MagickFalse)
864     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
865   status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
866   if (status == MagickFalse)
867     return(MagickFalse);
868   compression=image->compression;
869   if (image_info->compression != UndefinedCompression)
870     compression=image_info->compression;
871   switch (compression)
872   {
873     case FaxCompression:
874     case Group4Compression:
875     { 
876       if ((IsMonochromeImage(image,&image->exception) == MagickFalse) ||
877           (image->matte != MagickFalse))
878         compression=RLECompression;
879       break;
880     }
881 #if !defined(MAGICKCORE_JPEG_DELEGATE)
882     case JPEGCompression:
883     {
884       compression=RLECompression;
885       (void) ThrowMagickException(&image->exception,GetMagickModule(),
886         MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
887         image->filename);
888       break;
889     }
890 #endif
891 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
892     case ZipCompression:
893     {
894       compression=RLECompression;
895       (void) ThrowMagickException(&image->exception,GetMagickModule(),
896         MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZLIB)",
897         image->filename);
898       break;
899     }
900 #endif
901     default:
902       break;
903   }
904   (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
905   page=0;
906   scene=0;
907   do
908   {
909     /*
910       Scale relative to dots-per-inch.
911     */
912     delta.x=DefaultResolution;
913     delta.y=DefaultResolution;
914     resolution.x=image->x_resolution;
915     resolution.y=image->y_resolution;
916     if ((resolution.x == 0.0) || (resolution.y == 0.0))
917       {
918         flags=ParseGeometry(PSDensityGeometry,&geometry_info);
919         resolution.x=geometry_info.rho;
920         resolution.y=geometry_info.sigma;
921         if ((flags & SigmaValue) == 0)
922           resolution.y=resolution.x;
923       }
924     if (image_info->density != (char *) NULL)
925       {
926         flags=ParseGeometry(image_info->density,&geometry_info);
927         resolution.x=geometry_info.rho;
928         resolution.y=geometry_info.sigma;
929         if ((flags & SigmaValue) == 0)
930           resolution.y=resolution.x;
931       }
932     if (image->units == PixelsPerCentimeterResolution)
933       {
934         resolution.x*=2.54;
935         resolution.y*=2.54;
936       }
937     SetGeometry(image,&geometry);
938     (void) FormatMagickString(page_geometry,MaxTextExtent,"%lux%lu",
939       image->columns,image->rows);
940     if (image_info->page != (char *) NULL)
941       (void) CopyMagickString(page_geometry,image_info->page,MaxTextExtent);
942     else
943       if ((image->page.width != 0) && (image->page.height != 0))
944         (void) FormatMagickString(page_geometry,MaxTextExtent,"%lux%lu%+ld%+ld",
945           image->page.width,image->page.height,image->page.x,image->page.y);
946       else
947         if ((image->gravity != UndefinedGravity) &&
948             (LocaleCompare(image_info->magick,"PS") == 0))
949           (void) CopyMagickString(page_geometry,PSPageGeometry,MaxTextExtent);
950     (void) ConcatenateMagickString(page_geometry,">",MaxTextExtent);
951     (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
952       &geometry.width,&geometry.height);
953     scale.x=(double) (geometry.width*delta.x)/resolution.x;
954     geometry.width=(unsigned long) (scale.x+0.5);
955     scale.y=(double) (geometry.height*delta.y)/resolution.y;
956     geometry.height=(unsigned long) (scale.y+0.5);
957     (void) ParseAbsoluteGeometry(page_geometry,&media_info);
958     (void) ParseGravityGeometry(image,page_geometry,&page_info,
959       &image->exception);
960     if (image->gravity != UndefinedGravity)
961       {
962         geometry.x=(-page_info.x);
963         geometry.y=(long) (media_info.height+page_info.y-image->rows);
964       }
965     pointsize=12.0;
966     if (image_info->pointsize != 0.0)
967       pointsize=image_info->pointsize;
968     text_size=0;
969     value=GetImageProperty(image,"label");
970     if (value != (const char *) NULL)
971       text_size=(unsigned long) (MultilineCensus(value)*pointsize+12);
972     page++;
973     if (page == 1)
974       {
975         /*
976           Postscript header on the first page.
977         */
978         if (LocaleCompare(image_info->magick,"PS3") == 0)
979           (void) CopyMagickString(buffer,"%!PS-Adobe-3.0\n",MaxTextExtent);
980         else
981           (void) CopyMagickString(buffer,"%!PS-Adobe-3.0 EPSF-3.0\n",
982             MaxTextExtent);
983         (void) WriteBlobString(image,buffer);
984         (void) FormatMagickString(buffer,MaxTextExtent,
985           "%%%%Creator: ImageMagick %s\n",MagickLibVersionText);
986         (void) WriteBlobString(image,buffer);
987         (void) FormatMagickString(buffer,MaxTextExtent,"%%%%Title: %s\n",
988           image->filename);
989         (void) WriteBlobString(image,buffer);
990         timer=time((time_t *) NULL);
991         (void) FormatMagickTime(timer,MaxTextExtent,date);
992         (void) FormatMagickString(buffer,MaxTextExtent,
993           "%%%%CreationDate: %s\n",date);
994         (void) WriteBlobString(image,buffer);
995         bounds.x1=(double) geometry.x;
996         bounds.y1=(double) geometry.y;
997         bounds.x2=(double) geometry.x+scale.x;
998         bounds.y2=(double) geometry.y+scale.y+text_size;
999         if ((image_info->adjoin != MagickFalse) &&
1000             (GetNextImageInList(image) != (Image *) NULL))
1001           {
1002             (void) WriteBlobString(image,"%%BoundingBox: (atend)\n");
1003             (void) WriteBlobString(image,"%%HiResBoundingBox: (atend)\n");
1004           }
1005         else
1006           {
1007             (void) FormatMagickString(buffer,MaxTextExtent,
1008               "%%%%BoundingBox: %g %g %g %g\n",floor(bounds.x1+0.5),
1009               floor(bounds.y1+0.5),ceil(bounds.x2-0.5),ceil(bounds.y2-0.5));
1010             (void) WriteBlobString(image,buffer);
1011             (void) FormatMagickString(buffer,MaxTextExtent,
1012               "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,bounds.y1,
1013               bounds.x2,bounds.y2);
1014             (void) WriteBlobString(image,buffer);
1015             if (image->colorspace == CMYKColorspace)
1016               (void) WriteBlobString(image,
1017                 "%%DocumentProcessColors: Cyan Magenta Yellow Black\n");
1018             else
1019               if (IsGrayImage(image,&image->exception) != MagickFalse)
1020                 (void) WriteBlobString(image,
1021                   "%%DocumentProcessColors: Black\n");
1022           }
1023         /*
1024           Font resources
1025         */
1026         value=GetImageProperty(image,"label");
1027         if (value != (const char *) NULL)
1028           (void) WriteBlobString(image,
1029             "%%DocumentNeededResources: font Helvetica\n");
1030         (void) WriteBlobString(image,"%%LanguageLevel: 3\n");
1031         /*
1032           Pages, orientation and order.
1033         */
1034         if (LocaleCompare(image_info->magick,"PS3") != 0)
1035           (void) WriteBlobString(image,"%%Pages: 1\n");
1036         else
1037           {
1038             (void) WriteBlobString(image,"%%Orientation: Portrait\n");
1039             (void) WriteBlobString(image,"%%PageOrder: Ascend\n");
1040             if (image_info->adjoin == MagickFalse)
1041               (void) CopyMagickString(buffer,"%%Pages: 1\n",MaxTextExtent);
1042             else
1043               (void) FormatMagickString(buffer,MaxTextExtent,"%%%%Pages: %lu\n",
1044                 (unsigned long) GetImageListLength(image));
1045             (void) WriteBlobString(image,buffer);
1046           }
1047         (void) WriteBlobString(image,"%%EndComments\n");
1048         /*
1049           The static postscript procedures prolog.
1050         */
1051         (void)WriteBlobString(image,"%%BeginProlog\n");
1052         for (q=PostscriptProlog; *q; q++)
1053         {
1054           (void) WriteBlobString(image,*q);
1055           (void) WriteBlobByte(image,'\n');
1056         }
1057         /*
1058           One label line for each line in label string.
1059         */
1060         value=GetImageProperty(image,"label");
1061         if (value != (const char *) NULL)
1062           {
1063               (void) WriteBlobString(image,"\n  %% Labels.\n  /Helvetica "
1064               " findfont pointsize scalefont setfont\n");
1065             for (i=(long) MultilineCensus(value)-1; i >= 0; i--)
1066             {
1067               (void) WriteBlobString(image,
1068                 "  currentfile buffer readline pop token pop\n");
1069               (void) FormatMagickString(buffer,MaxTextExtent,
1070                 "  0 y %g add moveto show pop\n",i*pointsize+12);
1071               (void) WriteBlobString(image,buffer);
1072             }
1073           }
1074         /*
1075           The static postscript procedures epilog.
1076         */
1077         for (q=PostscriptEpilog; *q; q++)
1078         {
1079           (void) WriteBlobString(image,*q);
1080           (void) WriteBlobByte(image,'\n');
1081         }
1082         (void)WriteBlobString(image,"%%EndProlog\n");
1083       }
1084     (void) FormatMagickString(buffer,MaxTextExtent,"%%%%Page: 1 %lu\n",page);
1085     (void) WriteBlobString(image,buffer);
1086     /*
1087       Page bounding box.
1088     */
1089     (void) FormatMagickString(buffer,MaxTextExtent,
1090       "%%%%PageBoundingBox: %ld %ld %ld %ld\n",geometry.x,geometry.y,geometry.x+
1091       (long) geometry.width,geometry.y+(long) (geometry.height+text_size));
1092     (void) WriteBlobString(image,buffer);
1093     /*
1094       Page process colors if not RGB.
1095     */
1096     if (image->colorspace == CMYKColorspace)
1097       (void) WriteBlobString(image,
1098         "%%PageProcessColors: Cyan Magenta Yellow Black\n");
1099     else
1100       if (IsGrayImage(image,&image->exception) != MagickFalse)
1101         (void) WriteBlobString(image,"%%PageProcessColors: Black\n");
1102     /*
1103       Adjust document bounding box to bound page bounding box.
1104     */
1105     if ((double) geometry.x < bounds.x1)
1106       bounds.x1=(double) geometry.x;
1107     if ((double) geometry.y < bounds.y1)
1108       bounds.y1=(double) geometry.y;
1109     if ((double) (geometry.x+scale.x) > bounds.x2)
1110       bounds.x2=(double) geometry.x+scale.x;
1111     if ((double) (geometry.y+scale.y+text_size) > bounds.y2)
1112       bounds.y2=(double) geometry.y+scale.y+text_size;
1113     /*
1114       Page font resource if there's a label.
1115     */
1116     value=GetImageProperty(image,"label");
1117     if (value != (const char *) NULL)
1118       (void) WriteBlobString(image,"%%PageResources: font Helvetica\n");
1119     /*
1120       PS clipping path from Photoshop clipping path.
1121     */
1122     if ((image->clip_mask == (Image *) NULL) ||
1123         (LocaleNCompare("8BIM:",image->clip_mask->magick_filename,5) != 0))
1124       (void) WriteBlobString(image,"/ClipImage {} def\n");
1125     else
1126       {
1127         const char
1128           *value;
1129
1130         value=GetImageProperty(image,image->clip_mask->magick_filename);
1131         if (value == (const char *) NULL)
1132           return(MagickFalse);
1133         (void) WriteBlobString(image,value);
1134         (void) WriteBlobByte(image,'\n');
1135       }
1136     /*
1137       Push a dictionary for our own def's if this an EPS.
1138     */
1139     if (LocaleCompare(image_info->magick,"PS3") != 0)
1140       (void) WriteBlobString(image,"userdict begin\n");
1141     /*
1142       Image mask.
1143     */
1144     if ((image->matte != MagickFalse) &&
1145         (WritePS3MaskImage(image_info,image,compression) == MagickFalse))
1146       {
1147         (void) CloseBlob(image);
1148         return(MagickFalse);
1149       }
1150     /*
1151       Remember position of BeginData comment so we can update it.
1152     */
1153     start=TellBlob(image);
1154     (void) FormatMagickString(buffer,MaxTextExtent,
1155       "%%%%BeginData:%13ld %s Bytes\n",0L,
1156       compression == NoCompression ? "ASCII" : "BINARY");
1157     (void) WriteBlobString(image,buffer);
1158     stop=TellBlob(image);
1159     (void) WriteBlobString(image,"DisplayImage\n");
1160     /*
1161       Translate, scale, and font point size.
1162     */
1163     (void) FormatMagickString(buffer,MaxTextExtent,"%ld %ld\n%g %g\n%f\n",
1164       geometry.x,geometry.y,scale.x,scale.y,pointsize);
1165     (void) WriteBlobString(image,buffer);
1166     /*
1167       Output labels.
1168     */
1169     labels=(char **) NULL;
1170     value=GetImageProperty(image,"label");
1171     if (value != (const char *) NULL)
1172       labels=StringToList(value);
1173     if (labels != (char **) NULL)
1174       {
1175         for (i=0; labels[i] != (char *) NULL; i++)
1176         {
1177           if (compression != NoCompression)
1178             {
1179               for (j=0; labels[i][j] != '\0'; j++)
1180                 (void) WriteBlobByte(image,(unsigned char) labels[i][j]);
1181               (void) WriteBlobByte(image,'\n');
1182             }
1183           else
1184             {
1185               (void) WriteBlobString(image,"<~");
1186               Ascii85Initialize(image);
1187               for (j=0; labels[i][j] != '\0'; j++)
1188                 Ascii85Encode(image,(unsigned char) labels[i][j]);
1189               Ascii85Flush(image);
1190             }
1191           labels[i]=DestroyString(labels[i]);
1192         }
1193         labels=(char **) RelinquishMagickMemory(labels);
1194       }
1195     /*
1196       Photoshop clipping path active?
1197     */
1198     if ((image->clip_mask != (Image *) NULL) &&
1199         (LocaleNCompare("8BIM:",image->clip_mask->magick_filename,5) == 0))
1200         (void) WriteBlobString(image,"true\n");
1201       else
1202         (void) WriteBlobString(image,"false\n");
1203     /*
1204       Showpage for non-EPS.
1205     */
1206     (void) WriteBlobString(image, LocaleCompare(image_info->magick,"PS3") == 0 ?
1207       "true\n" : "false\n");
1208     /*
1209       Image columns, rows, and color space.
1210     */
1211     (void) FormatMagickString(buffer,MaxTextExtent,"%lu %lu\n%s\n",
1212       image->columns,image->rows,image->colorspace == CMYKColorspace ?
1213       PS3_CMYKColorspace : PS3_RGBColorspace);
1214     (void) WriteBlobString(image,buffer);
1215     /*
1216       Masked image?
1217     */
1218     (void) WriteBlobString(image,image->matte != MagickFalse ?
1219       "true\n" : "false\n");
1220     /*
1221       Render with imagemask operator?
1222     */
1223     option=GetImageOption(image_info,"ps3:imagemask");
1224     (void) WriteBlobString(image,((option != (const char *) NULL) &&
1225       (IsMonochromeImage(image,&image->exception) != MagickFalse)) ?
1226       "true\n" : "false\n");
1227     /*
1228       Output pixel data.
1229     */
1230     pixels=(unsigned char *) NULL;
1231     length=0;
1232     if ((image_info->type != TrueColorType) &&
1233         (image_info->type != TrueColorMatteType) &&
1234         (image_info->type != ColorSeparationType) &&
1235         (image_info->type != ColorSeparationMatteType) &&
1236         (image->colorspace != CMYKColorspace) &&
1237         ((IsGrayImage(image,&image->exception) != MagickFalse) ||
1238          (IsMonochromeImage(image,&image->exception) != MagickFalse)))
1239       {
1240         /*
1241           Gray images.
1242         */
1243         (void) WriteBlobString(image,PS3_PseudoClass"\n");
1244         switch (compression)
1245         {
1246           case NoCompression:
1247           default:
1248           {
1249             (void) WriteBlobString(image,PS3_NoCompression"\n");
1250             break;
1251           }
1252           case FaxCompression:
1253           case Group4Compression:
1254           {
1255             (void) WriteBlobString(image,PS3_FaxCompression"\n");
1256             break;
1257           }
1258           case JPEGCompression:
1259           {
1260             (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1261             break;
1262           }
1263           case LZWCompression:
1264           {
1265             (void) WriteBlobString(image,PS3_LZWCompression"\n");
1266             break;
1267           }
1268           case RLECompression:
1269           {
1270             (void) WriteBlobString(image,PS3_RLECompression"\n");
1271             break;
1272           }
1273           case ZipCompression:
1274           {
1275             (void) WriteBlobString(image,PS3_ZipCompression"\n");
1276             break;
1277           }
1278         }
1279         /*
1280           Number of colors -- 0 for single component non-color mapped data.
1281         */
1282         (void) WriteBlobString(image,"0\n");
1283         /*
1284           1 bit or 8 bit components?
1285         */
1286         (void) FormatMagickString(buffer,MaxTextExtent,"%d\n",
1287           IsMonochromeImage(image,&image->exception) != MagickFalse ? 1 : 8);
1288         (void) WriteBlobString(image,buffer);
1289         /*
1290           Image data.
1291         */
1292         if (compression == JPEGCompression)
1293           status=InjectImageBlob(image_info,image,image,"jpeg",
1294             &image->exception);
1295         else
1296           if ((compression == FaxCompression) ||
1297               (compression == Group4Compression))
1298             {
1299               if (LocaleCompare(CCITTParam,"0") == 0)
1300                 status=HuffmanEncodeImage(image_info,image,image);
1301               else
1302                 status=Huffman2DEncodeImage(image_info,image,image);
1303             }
1304           else
1305             {
1306               status=SerializeImageChannel(image_info,image,&pixels,&length);
1307               if (status == MagickFalse)
1308                 {
1309                   (void) CloseBlob(image);
1310                   return(MagickFalse);
1311                 }
1312               switch (compression)
1313               {
1314                 case NoCompression:
1315                 default:
1316                 {
1317                   Ascii85Initialize(image);
1318                   for (i=0; i < (long) length; i++)
1319                     Ascii85Encode(image,pixels[i]);
1320                   Ascii85Flush(image);
1321                   status=MagickTrue;
1322                   break;
1323                 }
1324                 case LZWCompression:
1325                 {
1326                   status=LZWEncodeImage(image,length,pixels);
1327                   break;
1328                 }
1329                 case RLECompression:
1330                 {
1331                   status=PackbitsEncodeImage(image,length,pixels);
1332                   break;
1333                 }
1334                 case ZipCompression:
1335                 {
1336                   status=ZLIBEncodeImage(image,length,pixels);
1337                   break;
1338                 }
1339               }
1340               pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1341             }
1342       }
1343     else
1344       if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1345           (compression == JPEGCompression))
1346         {
1347           /*
1348             Truecolor image.
1349           */
1350           (void) WriteBlobString(image,PS3_DirectClass"\n");
1351           switch (compression)
1352           {
1353             case NoCompression:
1354             default:
1355             {
1356               (void) WriteBlobString(image,PS3_NoCompression"\n");
1357               break;
1358             }
1359             case RLECompression:
1360             {
1361               (void) WriteBlobString(image,PS3_RLECompression"\n");
1362               break;
1363             }
1364             case JPEGCompression:
1365             {
1366               (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1367               break;
1368             }
1369             case LZWCompression:
1370             {
1371               (void) WriteBlobString(image,PS3_LZWCompression"\n");
1372               break;
1373             }
1374             case ZipCompression:
1375             {
1376               (void) WriteBlobString(image,PS3_ZipCompression"\n");
1377               break;
1378             }
1379           }
1380           /*
1381             Image data.
1382           */
1383           if (compression == JPEGCompression)
1384             status=InjectImageBlob(image_info,image,image,"jpeg",
1385               &image->exception);
1386           else
1387             {
1388               /*
1389                 Stream based compressions.
1390               */
1391               status=SerializeImage(image_info,image,&pixels,&length);
1392               if (status == MagickFalse)
1393                 {
1394                   (void) CloseBlob(image);
1395                   return(MagickFalse);
1396                 }
1397               switch (compression)
1398               {
1399                 case NoCompression:
1400                 default:
1401                 {
1402                   Ascii85Initialize(image);
1403                   for (i=0; i < (long) length; i++)
1404                     Ascii85Encode(image,pixels[i]);
1405                   Ascii85Flush(image);
1406                   status=MagickTrue;
1407                   break;
1408                 }
1409                 case RLECompression:
1410                 {
1411                   status=PackbitsEncodeImage(image,length,pixels);
1412                   break;
1413                 }
1414                 case LZWCompression:
1415                 {
1416                   status=LZWEncodeImage(image,length,pixels);
1417                   break;
1418                 }
1419                 case ZipCompression:
1420                 {
1421                   status=ZLIBEncodeImage(image,length,pixels);
1422                   break;
1423                 }
1424               }
1425               pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1426             }
1427           }
1428         else
1429           {
1430             /*
1431               Colormapped images.
1432             */
1433             (void) WriteBlobString(image,PS3_PseudoClass"\n");
1434             switch (compression)
1435             {
1436               case NoCompression:
1437               default:
1438               {
1439                 (void) WriteBlobString(image,PS3_NoCompression"\n");
1440                 break;
1441               }
1442               case JPEGCompression:
1443               {
1444                 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1445                 break;
1446               }
1447               case RLECompression:
1448               {
1449                 (void) WriteBlobString(image,PS3_RLECompression"\n");
1450                 break;
1451               }
1452               case LZWCompression:
1453               {
1454                 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1455                 break;
1456               }
1457               case ZipCompression:
1458               {
1459                 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1460                 break;
1461               }
1462             }
1463             /*
1464               Number of colors in color map.
1465             */
1466             (void) FormatMagickString(buffer,MaxTextExtent,"%lu\n",
1467               image->colors);
1468             (void) WriteBlobString(image,buffer);
1469             /*
1470               Color map - uncompressed.
1471             */
1472             if ((compression != NoCompression) &&
1473                 (compression != UndefinedCompression))
1474               {
1475                 for (i=0; i < (long) image->colors; i++)
1476                 {
1477                   pixel=ScaleQuantumToChar(image->colormap[i].red);
1478                   (void) WriteBlobByte(image,(unsigned char) pixel);
1479                   pixel=ScaleQuantumToChar(image->colormap[i].green);
1480                   (void) WriteBlobByte(image,(unsigned char) pixel);
1481                   pixel=ScaleQuantumToChar(image->colormap[i].blue);
1482                   (void) WriteBlobByte(image,(unsigned char) pixel);
1483                 }
1484               }
1485             else
1486               {
1487                 Ascii85Initialize(image);
1488                 for (i=0; i < (long) image->colors; i++)
1489                 {
1490                   pixel=ScaleQuantumToChar(image->colormap[i].red);
1491                   Ascii85Encode(image,(unsigned char) pixel);
1492                   pixel=ScaleQuantumToChar(image->colormap[i].green);
1493                   Ascii85Encode(image,(unsigned char) pixel);
1494                   pixel=ScaleQuantumToChar(image->colormap[i].blue);
1495                   Ascii85Encode(image,(unsigned char) pixel);
1496                 }
1497                 Ascii85Flush(image);
1498               }
1499             status=SerializeImageIndexes(image_info,image,&pixels,&length);
1500             if (status == MagickFalse)
1501               {
1502                 (void) CloseBlob(image);
1503                 return(MagickFalse);
1504               }
1505             switch (compression)
1506             {
1507               case NoCompression:
1508               default:
1509               {
1510                 Ascii85Initialize(image);
1511                 for (i=0; i < (long) length; i++)
1512                   Ascii85Encode(image,pixels[i]);
1513                 Ascii85Flush(image);
1514                 status=MagickTrue;
1515                 break;
1516               }
1517               case JPEGCompression:
1518               {
1519                 status=InjectImageBlob(image_info,image,image,"jpeg",
1520                   &image->exception);
1521                 break;
1522               }
1523               case RLECompression:
1524               {
1525                 status=PackbitsEncodeImage(image,length,pixels);
1526                 break;
1527               }
1528               case LZWCompression:
1529               {
1530                 status=LZWEncodeImage(image,length,pixels);
1531                 break;
1532               }
1533               case ZipCompression:
1534               {
1535                 status=ZLIBEncodeImage(image,length,pixels);
1536                 break;
1537               }
1538             }
1539             pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1540           }
1541     (void) WriteBlobByte(image,'\n');
1542     if (status == MagickFalse)
1543       {
1544         (void) CloseBlob(image);
1545         return(MagickFalse);
1546       }
1547     /*
1548       Update BeginData now that we know the data size.
1549     */
1550     length=(size_t) (TellBlob(image)-stop);
1551     stop=TellBlob(image);
1552     offset=SeekBlob(image,start,SEEK_SET);
1553     if (offset < 0)
1554       ThrowWriterException(CorruptImageError,"ImproperImageHeader");
1555     (void) FormatMagickString(buffer,MaxTextExtent,
1556       "%%%%BeginData:%13ld %s Bytes\n",(long) length,
1557       compression == NoCompression ? "ASCII" : "BINARY");
1558     (void) WriteBlobString(image,buffer);
1559     offset=SeekBlob(image,stop,SEEK_SET);
1560     (void) WriteBlobString(image,"%%EndData\n");
1561     /*
1562       End private dictionary if this an EPS.
1563     */
1564     if (LocaleCompare(image_info->magick,"PS3") != 0)
1565       (void) WriteBlobString(image,"end\n");
1566     (void) WriteBlobString(image,"%%PageTrailer\n");
1567     if (GetNextImageInList(image) == (Image *) NULL)
1568       break;
1569     image=SyncNextImageInList(image);
1570     status=SetImageProgress(image,SaveImagesTag,scene++,
1571       GetImageListLength(image));
1572     if (status == MagickFalse)
1573       break;
1574   } while (image_info->adjoin != MagickFalse);
1575   (void) WriteBlobString(image,"%%Trailer\n");
1576   if (page > 1)
1577     {
1578       (void) FormatMagickString(buffer,MaxTextExtent,
1579         "%%%%BoundingBox: %g %g %g %g\n",floor(bounds.x1+0.5),
1580         floor(bounds.y1+0.5),ceil(bounds.x2-0.5),ceil(bounds.y2-0.5));
1581       (void) WriteBlobString(image,buffer);
1582       (void) FormatMagickString(buffer,MaxTextExtent,
1583         "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,bounds.y1,
1584         bounds.x2,bounds.y2);
1585       (void) WriteBlobString(image,buffer);
1586     }
1587   (void) WriteBlobString(image,"%%EOF\n");
1588   (void) CloseBlob(image);
1589   return(MagickTrue);
1590 }