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