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