]> granicus.if.org Git - imagemagick/blob - coders/pdf.c
9b264880659eeb0f68a2e884f5f67734b719341f
[imagemagick] / coders / pdf.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                            PPPP   DDDD   FFFFF                              %
7 %                            P   P  D   D  F                                  %
8 %                            PPPP   D   D  FFF                                %
9 %                            P      D   D  F                                  %
10 %                            P      DDDD   F                                  %
11 %                                                                             %
12 %                                                                             %
13 %                   Read/Write Portable Document Format                       %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                John Cristy                                  %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      %
21 %  dedicated to making software imaging solutions freely available.           %
22 %                                                                             %
23 %  You may not use this file except in compliance with the License.  You may  %
24 %  obtain a copy of the License at                                            %
25 %                                                                             %
26 %    http://www.imagemagick.org/script/license.php                            %
27 %                                                                             %
28 %  Unless required by applicable law or agreed to in writing, software        %
29 %  distributed under the License is distributed on an "AS IS" BASIS,          %
30 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
31 %  See the License for the specific language governing permissions and        %
32 %  limitations under the License.                                             %
33 %                                                                             %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %
36 %
37 */
38 \f
39 /*
40   Include declarations.
41 */
42 #include "MagickCore/studio.h"
43 #include "MagickCore/attribute.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/color.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/colorspace.h"
50 #include "MagickCore/colorspace-private.h"
51 #include "MagickCore/compress.h"
52 #include "MagickCore/constitute.h"
53 #include "MagickCore/delegate.h"
54 #include "MagickCore/delegate-private.h"
55 #include "MagickCore/draw.h"
56 #include "MagickCore/exception.h"
57 #include "MagickCore/exception-private.h"
58 #include "MagickCore/geometry.h"
59 #include "MagickCore/image.h"
60 #include "MagickCore/image-private.h"
61 #include "MagickCore/list.h"
62 #include "MagickCore/magick.h"
63 #include "MagickCore/memory_.h"
64 #include "MagickCore/monitor.h"
65 #include "MagickCore/monitor-private.h"
66 #include "MagickCore/option.h"
67 #include "MagickCore/pixel-accessor.h"
68 #include "MagickCore/profile.h"
69 #include "MagickCore/property.h"
70 #include "MagickCore/quantum-private.h"
71 #include "MagickCore/resource_.h"
72 #include "MagickCore/resize.h"
73 #include "MagickCore/static.h"
74 #include "MagickCore/string_.h"
75 #include "MagickCore/module.h"
76 #include "MagickCore/transform.h"
77 #include "MagickCore/utility.h"
78 #include "MagickCore/module.h"
79 \f
80 /*
81   Define declarations.
82 */
83 #if defined(MAGICKCORE_TIFF_DELEGATE)
84 #define CCITTParam  "-1"
85 #else
86 #define CCITTParam  "0"
87 #endif
88 \f
89 /*
90   Forward declarations.
91 */
92 static MagickBooleanType
93   WritePDFImage(const ImageInfo *,Image *,ExceptionInfo *);
94 \f
95 /*
96 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
97 %                                                                             %
98 %                                                                             %
99 %                                                                             %
100 %   I n v o k e P D F D e l e g a t e                                         %
101 %                                                                             %
102 %                                                                             %
103 %                                                                             %
104 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105 %
106 %  InvokePDFDelegate() executes the PDF interpreter with the specified command.
107 %
108 %  The format of the InvokePDFDelegate method is:
109 %
110 %      MagickBooleanType InvokePDFDelegate(const MagickBooleanType verbose,
111 %        const char *command,ExceptionInfo *exception)
112 %
113 %  A description of each parameter follows:
114 %
115 %    o verbose: A value other than zero displays the command prior to
116 %      executing it.
117 %
118 %    o command: the address of a character string containing the command to
119 %      execute.
120 %
121 %    o exception: return any errors or warnings in this structure.
122 %
123 */
124 static MagickBooleanType InvokePDFDelegate(const MagickBooleanType verbose,
125   const char *command,ExceptionInfo *exception)
126 {
127   int
128     status;
129
130 #if defined(MAGICKCORE_GS_DELEGATE) || defined(MAGICKCORE_WINDOWS_SUPPORT)
131   char
132     **argv;
133
134   const GhostInfo
135     *ghost_info;
136
137   gs_main_instance
138     *interpreter;
139
140   int
141     argc,
142     code;
143
144   register ssize_t
145     i;
146
147 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
148   ghost_info=NTGhostscriptDLLVectors();
149 #else
150   GhostInfo
151     ghost_info_struct;
152
153   ghost_info=(&ghost_info_struct);
154   (void) ResetMagickMemory(&ghost_info,0,sizeof(ghost_info));
155   ghost_info_struct.new_instance=(int (*)(gs_main_instance **,void *))
156     gsapi_new_instance;
157   ghost_info_struct.init_with_args=(int (*)(gs_main_instance *,int,char **))
158     gsapi_init_with_args;
159   ghost_info_struct.run_string=(int (*)(gs_main_instance *,const char *,int,
160     int *)) gsapi_run_string;
161   ghost_info_struct.delete_instance=(void (*)(gs_main_instance *))
162     gsapi_delete_instance;
163   ghost_info_struct.exit=(int (*)(gs_main_instance *)) gsapi_exit;
164 #endif
165   if (ghost_info == (GhostInfo *) NULL)
166     {
167       status=SystemCommand(MagickFalse,verbose,command,exception);
168       return(status == 0 ? MagickTrue : MagickFalse);
169     }
170   if (verbose != MagickFalse)
171     {
172       (void) fputs("[ghostscript library]",stdout);
173       (void) fputs(strchr(command,' '),stdout);
174     }
175   status=(ghost_info->new_instance)(&interpreter,(void *) NULL);
176   if (status < 0)
177     {
178       status=SystemCommand(MagickFalse,verbose,command,exception);
179       return(status == 0 ? MagickTrue : MagickFalse);
180     }
181   code=0;
182   argv=StringToArgv(command,&argc);
183   status=(ghost_info->init_with_args)(interpreter,argc-1,argv+1);
184   if (status == 0)
185     status=(ghost_info->run_string)(interpreter,"systemdict /start get exec\n",
186       0,&code);
187   (ghost_info->exit)(interpreter);
188   (ghost_info->delete_instance)(interpreter);
189 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
190   NTGhostscriptUnLoadDLL();
191 #endif
192   for (i=0; i < (ssize_t) argc; i++)
193     argv[i]=DestroyString(argv[i]);
194   argv=(char **) RelinquishMagickMemory(argv);
195   if ((status != 0) && (status != -101))
196     {
197       char
198         *message;
199
200       message=GetExceptionMessage(errno);
201       (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
202         "`%s': %s",command,message);
203       message=DestroyString(message);
204       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
205         "Ghostscript returns status %d, exit code %d",status,code);
206       return(MagickFalse);
207     }
208   return(MagickTrue);
209 #else
210   status=SystemCommand(MagickFalse,verbose,command,exception);
211   return(status == 0 ? MagickTrue : MagickFalse);
212 #endif
213 }
214 \f
215 /*
216 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
217 %                                                                             %
218 %                                                                             %
219 %                                                                             %
220 %   I s P D F                                                                 %
221 %                                                                             %
222 %                                                                             %
223 %                                                                             %
224 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
225 %
226 %  IsPDF() returns MagickTrue if the image format type, identified by the
227 %  magick string, is PDF.
228 %
229 %  The format of the IsPDF method is:
230 %
231 %      MagickBooleanType IsPDF(const unsigned char *magick,const size_t offset)
232 %
233 %  A description of each parameter follows:
234 %
235 %    o magick: compare image format pattern against these bytes.
236 %
237 %    o offset: Specifies the offset of the magick string.
238 %
239 */
240 static MagickBooleanType IsPDF(const unsigned char *magick,const size_t offset)
241 {
242   if (offset < 5)
243     return(MagickFalse);
244   if (LocaleNCompare((const char *) magick,"%PDF-",5) == 0)
245     return(MagickTrue);
246   return(MagickFalse);
247 }
248 \f
249 /*
250 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251 %                                                                             %
252 %                                                                             %
253 %                                                                             %
254 %   R e a d P D F I m a g e                                                   %
255 %                                                                             %
256 %                                                                             %
257 %                                                                             %
258 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
259 %
260 %  ReadPDFImage() reads a Portable Document Format image file and
261 %  returns it.  It allocates the memory necessary for the new Image structure
262 %  and returns a pointer to the new image.
263 %
264 %  The format of the ReadPDFImage method is:
265 %
266 %      Image *ReadPDFImage(const ImageInfo *image_info,ExceptionInfo *exception)
267 %
268 %  A description of each parameter follows:
269 %
270 %    o image_info: the image info.
271 %
272 %    o exception: return any errors or warnings in this structure.
273 %
274 */
275
276 static MagickBooleanType IsPDFRendered(const char *path)
277 {
278   MagickBooleanType
279     status;
280
281   struct stat
282     attributes;
283
284   if ((path == (const char *) NULL) || (*path == '\0'))
285     return(MagickFalse);
286   status=GetPathAttributes(path,&attributes);
287   if ((status != MagickFalse) && S_ISREG(attributes.st_mode) &&
288       (attributes.st_size > 0))
289     return(MagickTrue);
290   return(MagickFalse);
291 }
292
293 static Image *ReadPDFImage(const ImageInfo *image_info,ExceptionInfo *exception)
294 {
295 #define CropBox  "CropBox"
296 #define DeviceCMYK  "DeviceCMYK"
297 #define MediaBox  "MediaBox"
298 #define RenderPostscriptText  "Rendering Postscript...  "
299 #define PDFRotate  "Rotate"
300 #define SpotColor  "Separation"
301 #define TrimBox  "TrimBox"
302 #define PDFVersion  "PDF-"
303
304   char
305     command[MaxTextExtent],
306     density[MaxTextExtent],
307     filename[MaxTextExtent],
308     geometry[MaxTextExtent],
309     options[MaxTextExtent],
310     input_filename[MaxTextExtent],
311     postscript_filename[MaxTextExtent];
312
313   const char
314     *option;
315
316   const DelegateInfo
317     *delegate_info;
318
319   double
320     angle;
321
322   GeometryInfo
323     geometry_info;
324
325   Image
326     *image,
327     *next,
328     *pdf_image;
329
330   ImageInfo
331     *read_info;
332
333   int
334     c,
335     file;
336
337   MagickBooleanType
338     cmyk,
339     cropbox,
340     trimbox,
341     status;
342
343   MagickStatusType
344     flags;
345
346   PointInfo
347     delta;
348
349   RectangleInfo
350     bounding_box,
351     page;
352
353   register char
354     *p;
355
356   register ssize_t
357     i;
358
359   SegmentInfo
360     bounds,
361     hires_bounds;
362
363   size_t
364     scene,
365     spotcolor;
366
367   ssize_t
368     count;
369
370   assert(image_info != (const ImageInfo *) NULL);
371   assert(image_info->signature == MagickSignature);
372   if (image_info->debug != MagickFalse)
373     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
374       image_info->filename);
375   assert(exception != (ExceptionInfo *) NULL);
376   assert(exception->signature == MagickSignature);
377   /*
378     Open image file.
379   */
380   image=AcquireImage(image_info);
381   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
382   if (status == MagickFalse)
383     {
384       image=DestroyImageList(image);
385       return((Image *) NULL);
386     }
387   status=AcquireUniqueSymbolicLink(image_info->filename,input_filename);
388   if (status == MagickFalse)
389     {
390       ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
391         image_info->filename);
392       image=DestroyImageList(image);
393       return((Image *) NULL);
394     }
395   /*
396     Set the page density.
397   */
398   delta.x=DefaultResolution;
399   delta.y=DefaultResolution;
400   if ((image->x_resolution == 0.0) || (image->y_resolution == 0.0))
401     {
402       flags=ParseGeometry(PSDensityGeometry,&geometry_info);
403       image->x_resolution=geometry_info.rho;
404       image->y_resolution=geometry_info.sigma;
405       if ((flags & SigmaValue) == 0)
406         image->y_resolution=image->x_resolution;
407     }
408   /*
409     Determine page geometry from the PDF media box.
410   */
411   cmyk=image_info->colorspace == CMYKColorspace ? MagickTrue : MagickFalse;
412   cropbox=MagickFalse;
413   option=GetImageOption(image_info,"pdf:use-cropbox");
414   if (option != (const char *) NULL)
415     cropbox=IsMagickTrue(option);
416   trimbox=MagickFalse;
417   option=GetImageOption(image_info,"pdf:use-trimbox");
418   if (option != (const char *) NULL)
419     trimbox=IsMagickTrue(option);
420   count=0;
421   spotcolor=0;
422   (void) ResetMagickMemory(&bounding_box,0,sizeof(bounding_box));
423   (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
424   (void) ResetMagickMemory(&hires_bounds,0,sizeof(hires_bounds));
425   (void) ResetMagickMemory(&page,0,sizeof(page));
426   (void) ResetMagickMemory(command,0,sizeof(command));
427   hires_bounds.x2=0.0;
428   hires_bounds.y2=0.0;
429   angle=0.0;
430   p=command;
431   for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
432   {
433     /*
434       Note PDF elements.
435     */
436     if (c == '\n')
437       c=' ';
438     *p++=(char) c;
439     if ((c != (int) '/') && (c != (int) '%') &&
440         ((size_t) (p-command) < (MaxTextExtent-1)))
441       continue;
442     *(--p)='\0';
443     p=command;
444     if (LocaleNCompare(PDFRotate,command,strlen(PDFRotate)) == 0)
445       count=(ssize_t) sscanf(command,"Rotate %lf",&angle);
446     /*
447       Is this a CMYK document?
448     */
449     if (LocaleNCompare(DeviceCMYK,command,strlen(DeviceCMYK)) == 0)
450       cmyk=MagickTrue;
451     if (LocaleNCompare(SpotColor,command,strlen(SpotColor)) == 0)
452       {
453         char
454           name[MaxTextExtent],
455           property[MaxTextExtent],
456           *value;
457
458         register ssize_t
459           i;
460
461         /*
462           Note spot names.
463         */
464         (void) FormatLocaleString(property,MaxTextExtent,"pdf:SpotColor-%.20g",
465           (double) spotcolor++);
466         i=0;
467         for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
468         {
469           if ((isspace(c) != 0) || (c == '/') || ((i+1) == MaxTextExtent))
470             break;
471           name[i++]=(char) c;
472         }
473         name[i]='\0';
474         value=AcquireString(name);
475         (void) SubstituteString(&value,"#20"," ");
476         (void) SetImageProperty(image,property,value);
477         value=DestroyString(value);
478         continue;
479       }
480     if (LocaleNCompare(PDFVersion,command,strlen(PDFVersion)) == 0)
481       (void) SetImageProperty(image,"pdf:Version",command);
482     count=0;
483     if (cropbox != MagickFalse)
484       {
485         if (LocaleNCompare(CropBox,command,strlen(CropBox)) == 0)
486           {
487             /*
488               Note region defined by crop box.
489             */
490             count=(ssize_t) sscanf(command,"CropBox [%lf %lf %lf %lf",
491               &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
492             if (count != 4)
493               count=(ssize_t) sscanf(command,"CropBox[%lf %lf %lf %lf",
494                 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
495           }
496       }
497     else
498       if (trimbox != MagickFalse)
499         {
500           if (LocaleNCompare(TrimBox,command,strlen(TrimBox)) == 0)
501             {
502               /*
503                 Note region defined by trim box.
504               */
505               count=(ssize_t) sscanf(command,"TrimBox [%lf %lf %lf %lf",
506                 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
507               if (count != 4)
508                 count=(ssize_t) sscanf(command,"TrimBox[%lf %lf %lf %lf",
509                   &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
510             }
511         }
512       else
513         if (LocaleNCompare(MediaBox,command,strlen(MediaBox)) == 0)
514           {
515             /*
516               Note region defined by media box.
517             */
518             count=(ssize_t) sscanf(command,"MediaBox [%lf %lf %lf %lf",
519               &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
520             if (count != 4)
521               count=(ssize_t) sscanf(command,"MediaBox[%lf %lf %lf %lf",
522                 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
523           }
524     if (count != 4)
525       continue;
526     if (((bounds.x2 > hires_bounds.x2) && (bounds.y2 > hires_bounds.y2)) ||
527         ((hires_bounds.x2 == 0.0) && (hires_bounds.y2 == 0.0)))
528       {
529         /*
530           Set PDF render geometry.
531         */
532         (void) FormatLocaleString(geometry,MaxTextExtent,
533           "%gx%g%+.15g%+.15g",bounds.x2-bounds.x1,bounds.y2-bounds.y1,
534            bounds.x1,bounds.y1);
535         (void) SetImageProperty(image,"pdf:HiResBoundingBox",geometry);
536         page.width=(size_t) floor(bounds.x2-bounds.x1+0.5);
537         page.height=(size_t) floor(bounds.y2-bounds.y1+0.5);
538         hires_bounds=bounds;
539       }
540   }
541   (void) CloseBlob(image);
542   if ((fabs(angle) == 90.0) || (fabs(angle) == 270.0))
543     {
544       size_t
545         swap;
546
547       swap=page.width;
548       page.width=page.height;
549       page.height=swap;
550     }
551   if (IsRGBColorspace(image_info->colorspace) != MagickFalse)
552     cmyk=MagickFalse;
553   /*
554     Create Ghostscript control file.
555   */
556   file=AcquireUniqueFileResource(postscript_filename);
557   if (file == -1)
558     {
559       ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
560         image_info->filename);
561       image=DestroyImage(image);
562       return((Image *) NULL);
563     }
564   count=write(file," ",1);
565   file=close(file)-1;
566   /*
567     Render Postscript with the Ghostscript delegate.
568   */
569   if ((image_info->ping != MagickFalse) ||
570       (image_info->monochrome != MagickFalse))
571     delegate_info=GetDelegateInfo("ps:mono",(char *) NULL,exception);
572   else
573      if (cmyk != MagickFalse)
574        delegate_info=GetDelegateInfo("ps:cmyk",(char *) NULL,exception);
575      else
576        delegate_info=GetDelegateInfo("ps:alpha",(char *) NULL,exception);
577   if (delegate_info == (const DelegateInfo *) NULL)
578     {
579       (void) RelinquishUniqueFileResource(postscript_filename);
580       image=DestroyImage(image);
581       return((Image *) NULL);
582     }
583   *options='\0';
584   if (image_info->density != (char *) NULL)
585     {
586       flags=ParseGeometry(image_info->density,&geometry_info);
587       image->x_resolution=geometry_info.rho;
588       image->y_resolution=geometry_info.sigma;
589       if ((flags & SigmaValue) == 0)
590         image->y_resolution=image->x_resolution;
591     }
592   (void) FormatLocaleString(density,MaxTextExtent,"%gx%g",image->x_resolution,
593     image->y_resolution);
594   if (image_info->page != (char *) NULL)
595     {
596       (void) ParseAbsoluteGeometry(image_info->page,&page);
597       page.width=(size_t) floor((double) (page.width*image->x_resolution/
598         delta.x)+0.5);
599       page.height=(size_t) floor((double) (page.height*image->y_resolution/
600         delta.y)+0.5);
601       (void) FormatLocaleString(options,MaxTextExtent,"-g%.20gx%.20g ",(double)
602         page.width,(double) page.height);
603     }
604   if (cmyk != MagickFalse)
605     (void) ConcatenateMagickString(options,"-dUseCIEColor ",MaxTextExtent);
606   if (cropbox != MagickFalse)
607     (void) ConcatenateMagickString(options,"-dUseCropBox ",MaxTextExtent);
608   if (trimbox != MagickFalse)
609     (void) ConcatenateMagickString(options,"-dUseTrimBox ",MaxTextExtent);
610   read_info=CloneImageInfo(image_info);
611   *read_info->magick='\0';
612   if (read_info->number_scenes != 0)
613     {
614       char
615         pages[MaxTextExtent];
616
617       (void) FormatLocaleString(pages,MaxTextExtent,"-dFirstPage=%.20g "
618         "-dLastPage=%.20g",(double) read_info->scene+1,(double)
619         (read_info->scene+read_info->number_scenes));
620       (void) ConcatenateMagickString(options,pages,MaxTextExtent);
621       read_info->number_scenes=0;
622       if (read_info->scenes != (char *) NULL)
623         *read_info->scenes='\0';
624     }
625   if (read_info->authenticate != (char *) NULL)
626     (void) FormatLocaleString(options+strlen(options),MaxTextExtent,
627       " -sPDFPassword=%s",read_info->authenticate);
628   (void) CopyMagickString(filename,read_info->filename,MaxTextExtent);
629   (void) AcquireUniqueFilename(filename);
630   (void) ConcatenateMagickString(filename,"-%08d",MaxTextExtent);
631   (void) FormatLocaleString(command,MaxTextExtent,
632     GetDelegateCommands(delegate_info),
633     read_info->antialias != MagickFalse ? 4 : 1,
634     read_info->antialias != MagickFalse ? 4 : 1,density,options,filename,
635     postscript_filename,input_filename);
636   status=InvokePDFDelegate(read_info->verbose,command,exception);
637   (void) RelinquishUniqueFileResource(postscript_filename);
638   (void) RelinquishUniqueFileResource(input_filename);
639   pdf_image=(Image *) NULL;
640   if (status == MagickFalse)
641     for (i=1; ; i++)
642     {
643       (void) InterpretImageFilename(image_info,image,filename,(int) i,
644         read_info->filename);
645       if (IsPDFRendered(read_info->filename) == MagickFalse)
646         break;
647       (void) RelinquishUniqueFileResource(read_info->filename);
648     }
649   else
650     for (i=1; ; i++)
651     {
652       (void) InterpretImageFilename(image_info,image,filename,(int) i,
653         read_info->filename);
654       if (IsPDFRendered(read_info->filename) == MagickFalse)
655         break;
656       next=ReadImage(read_info,exception);
657       (void) RelinquishUniqueFileResource(read_info->filename);
658       if (next == (Image *) NULL)
659         break;
660       AppendImageToList(&pdf_image,next);
661     }
662   read_info=DestroyImageInfo(read_info);
663   if (pdf_image == (Image *) NULL)
664     {
665       ThrowFileException(exception,DelegateError,"PostscriptDelegateFailed",
666         image_info->filename);
667       image=DestroyImage(image);
668       return((Image *) NULL);
669     }
670   if (LocaleCompare(pdf_image->magick,"BMP") == 0)
671     {
672       Image
673         *cmyk_image;
674
675       cmyk_image=ConsolidateCMYKImages(pdf_image,exception);
676       if (cmyk_image != (Image *) NULL)
677         {
678           pdf_image=DestroyImageList(pdf_image);
679           pdf_image=cmyk_image;
680         }
681     }
682   if (image_info->number_scenes != 0)
683     {
684       Image
685         *clone_image;
686
687       register ssize_t
688         i;
689
690       /*
691         Add place holder images to meet the subimage specification requirement.
692       */
693       for (i=0; i < (ssize_t) image_info->scene; i++)
694       {
695         clone_image=CloneImage(pdf_image,1,1,MagickTrue,exception);
696         if (clone_image != (Image *) NULL)
697           PrependImageToList(&pdf_image,clone_image);
698       }
699     }
700   do
701   {
702     (void) CopyMagickString(pdf_image->filename,filename,MaxTextExtent);
703     pdf_image->page=page;
704     (void) CloneImageProfiles(pdf_image,image);
705     (void) CloneImageProperties(pdf_image,image);
706     next=SyncNextImageInList(pdf_image);
707     if (next != (Image *) NULL)
708       pdf_image=next;
709   } while (next != (Image *) NULL);
710   image=DestroyImage(image);
711   scene=0;
712   for (next=GetFirstImageInList(pdf_image); next != (Image *) NULL; )
713   {
714     next->scene=scene++;
715     next=GetNextImageInList(next);
716   }
717   return(GetFirstImageInList(pdf_image));
718 }
719 \f
720 /*
721 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
722 %                                                                             %
723 %                                                                             %
724 %                                                                             %
725 %   R e g i s t e r P D F I m a g e                                           %
726 %                                                                             %
727 %                                                                             %
728 %                                                                             %
729 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
730 %
731 %  RegisterPDFImage() adds properties for the PDF image format to
732 %  the list of supported formats.  The properties include the image format
733 %  tag, a method to read and/or write the format, whether the format
734 %  supports the saving of more than one frame to the same file or blob,
735 %  whether the format supports native in-memory I/O, and a brief
736 %  description of the format.
737 %
738 %  The format of the RegisterPDFImage method is:
739 %
740 %      size_t RegisterPDFImage(void)
741 %
742 */
743 ModuleExport size_t RegisterPDFImage(void)
744 {
745   MagickInfo
746     *entry;
747
748   entry=SetMagickInfo("AI");
749   entry->decoder=(DecodeImageHandler *) ReadPDFImage;
750   entry->encoder=(EncodeImageHandler *) WritePDFImage;
751   entry->adjoin=MagickFalse;
752   entry->blob_support=MagickFalse;
753   entry->seekable_stream=MagickTrue;
754   entry->thread_support=EncoderThreadSupport;
755   entry->description=ConstantString("Adobe Illustrator CS2");
756   entry->module=ConstantString("PDF");
757   (void) RegisterMagickInfo(entry);
758   entry=SetMagickInfo("EPDF");
759   entry->decoder=(DecodeImageHandler *) ReadPDFImage;
760   entry->encoder=(EncodeImageHandler *) WritePDFImage;
761   entry->adjoin=MagickFalse;
762   entry->blob_support=MagickFalse;
763   entry->seekable_stream=MagickTrue;
764   entry->thread_support=EncoderThreadSupport;
765   entry->description=ConstantString("Encapsulated Portable Document Format");
766   entry->module=ConstantString("PDF");
767   (void) RegisterMagickInfo(entry);
768   entry=SetMagickInfo("PDF");
769   entry->decoder=(DecodeImageHandler *) ReadPDFImage;
770   entry->encoder=(EncodeImageHandler *) WritePDFImage;
771   entry->magick=(IsImageFormatHandler *) IsPDF;
772   entry->blob_support=MagickFalse;
773   entry->seekable_stream=MagickTrue;
774   entry->thread_support=EncoderThreadSupport;
775   entry->description=ConstantString("Portable Document Format");
776   entry->module=ConstantString("PDF");
777   (void) RegisterMagickInfo(entry);
778   entry=SetMagickInfo("PDFA");
779   entry->decoder=(DecodeImageHandler *) ReadPDFImage;
780   entry->encoder=(EncodeImageHandler *) WritePDFImage;
781   entry->magick=(IsImageFormatHandler *) IsPDF;
782   entry->blob_support=MagickFalse;
783   entry->seekable_stream=MagickTrue;
784   entry->thread_support=EncoderThreadSupport;
785   entry->description=ConstantString("Portable Document Archive Format");
786   entry->module=ConstantString("PDF");
787   (void) RegisterMagickInfo(entry);
788   return(MagickImageCoderSignature);
789 }
790 \f
791 /*
792 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
793 %                                                                             %
794 %                                                                             %
795 %                                                                             %
796 %   U n r e g i s t e r P D F I m a g e                                       %
797 %                                                                             %
798 %                                                                             %
799 %                                                                             %
800 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
801 %
802 %  UnregisterPDFImage() removes format registrations made by the
803 %  PDF module from the list of supported formats.
804 %
805 %  The format of the UnregisterPDFImage method is:
806 %
807 %      UnregisterPDFImage(void)
808 %
809 */
810 ModuleExport void UnregisterPDFImage(void)
811 {
812   (void) UnregisterMagickInfo("AI");
813   (void) UnregisterMagickInfo("EPDF");
814   (void) UnregisterMagickInfo("PDF");
815   (void) UnregisterMagickInfo("PDFA");
816 }
817 \f
818 /*
819 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
820 %                                                                             %
821 %                                                                             %
822 %                                                                             %
823 %   W r i t e P D F I m a g e                                                 %
824 %                                                                             %
825 %                                                                             %
826 %                                                                             %
827 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
828 %
829 %  WritePDFImage() writes an image in the Portable Document image
830 %  format.
831 %
832 %  The format of the WritePDFImage method is:
833 %
834 %      MagickBooleanType WritePDFImage(const ImageInfo *image_info,
835 %        Image *image,ExceptionInfo *exception)
836 %
837 %  A description of each parameter follows.
838 %
839 %    o image_info: the image info.
840 %
841 %    o image:  The image.
842 %
843 %    o exception: return any errors or warnings in this structure.
844 %
845 */
846
847 static inline size_t MagickMax(const size_t x,const size_t y)
848 {
849   if (x > y)
850     return(x);
851   return(y);
852 }
853
854 static inline size_t MagickMin(const size_t x,const size_t y)
855 {
856   if (x < y)
857     return(x);
858   return(y);
859 }
860
861 static char *EscapeParenthesis(const char *text)
862 {
863   register char
864     *p;
865
866   register ssize_t
867     i;
868
869   size_t
870     escapes;
871
872   static char
873     buffer[MaxTextExtent];
874
875   escapes=0;
876   p=buffer;
877   for (i=0; i < (ssize_t) MagickMin(strlen(text),(MaxTextExtent-escapes-1)); i++)
878   {
879     if ((text[i] == '(') || (text[i] == ')'))
880       {
881         *p++='\\';
882         escapes++;
883       }
884     *p++=text[i];
885   }
886   *p='\0';
887   return(buffer);
888 }
889
890 static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
891   Image *image,Image *inject_image,ExceptionInfo *exception)
892 {
893   Image
894     *group4_image;
895
896   ImageInfo
897     *write_info;
898
899   MagickBooleanType
900     status;
901
902   size_t
903     length;
904
905   unsigned char
906     *group4;
907
908   status=MagickTrue;
909   write_info=CloneImageInfo(image_info);
910   (void) CopyMagickString(write_info->filename,"GROUP4:",MaxTextExtent);
911   (void) CopyMagickString(write_info->magick,"GROUP4",MaxTextExtent);
912   group4_image=CloneImage(inject_image,0,0,MagickTrue,exception);
913   if (group4_image == (Image *) NULL)
914     return(MagickFalse);
915   group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
916     exception);
917   group4_image=DestroyImage(group4_image);
918   if (group4 == (unsigned char *) NULL)
919     return(MagickFalse);
920   write_info=DestroyImageInfo(write_info);
921   if (WriteBlob(image,length,group4) != (ssize_t) length)
922     status=MagickFalse;
923   group4=(unsigned char *) RelinquishMagickMemory(group4);
924   return(status);
925 }
926
927 static MagickBooleanType WritePDFImage(const ImageInfo *image_info,Image *image,
928   ExceptionInfo *exception)
929 {
930 #define CFormat  "/Filter [ /%s ]\n"
931 #define ObjectsPerImage  14
932
933   static const char
934     XMPProfile[]=
935     {
936       "<?xpacket begin=\"%s\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n"
937       "<x:xmpmeta xmlns:x=\"adobe:ns:meta/\" x:xmptk=\"Adobe XMP Core 4.0-c316 44.253921, Sun Oct 01 2006 17:08:23\">\n"
938       "   <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n"
939       "      <rdf:Description rdf:about=\"\"\n"
940       "            xmlns:xap=\"http://ns.adobe.com/xap/1.0/\">\n"
941       "         <xap:ModifyDate>%s</xap:ModifyDate>\n"
942       "         <xap:CreateDate>%s</xap:CreateDate>\n"
943       "         <xap:MetadataDate>%s</xap:MetadataDate>\n"
944       "         <xap:CreatorTool>%s</xap:CreatorTool>\n"
945       "      </rdf:Description>\n"
946       "      <rdf:Description rdf:about=\"\"\n"
947       "            xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"
948       "         <dc:format>application/pdf</dc:format>\n"
949       "      </rdf:Description>\n"
950       "      <rdf:Description rdf:about=\"\"\n"
951       "            xmlns:xapMM=\"http://ns.adobe.com/xap/1.0/mm/\">\n"
952       "         <xapMM:DocumentID>uuid:6ec119d7-7982-4f56-808d-dfe64f5b35cf</xapMM:DocumentID>\n"
953       "         <xapMM:InstanceID>uuid:a79b99b4-6235-447f-9f6c-ec18ef7555cb</xapMM:InstanceID>\n"
954       "      </rdf:Description>\n"
955       "      <rdf:Description rdf:about=\"\"\n"
956       "            xmlns:pdf=\"http://ns.adobe.com/pdf/1.3/\">\n"
957       "         <pdf:Producer>%s</pdf:Producer>\n"
958       "      </rdf:Description>\n"
959       "      <rdf:Description rdf:about=\"\"\n"
960       "            xmlns:pdfaid=\"http://www.aiim.org/pdfa/ns/id/\">\n"
961       "         <pdfaid:part>1</pdfaid:part>\n"
962       "         <pdfaid:conformance>B</pdfaid:conformance>\n"
963       "      </rdf:Description>\n"
964       "   </rdf:RDF>\n"
965       "</x:xmpmeta>\n"
966       "<?xpacket end=\"w\"?>\n"
967     },
968     XMPProfileMagick[4]= { (char) 0xef, (char) 0xbb, (char) 0xbf, (char) 0x00 };
969
970   char
971     basename[MaxTextExtent],
972     buffer[MaxTextExtent],
973     date[MaxTextExtent],
974     **labels,
975     page_geometry[MaxTextExtent];
976
977   CompressionType
978     compression;
979
980   const char
981     *value;
982
983   double
984     pointsize;
985
986   GeometryInfo
987     geometry_info;
988
989   Image
990     *next,
991     *tile_image;
992
993   MagickBooleanType
994     status;
995
996   MagickOffsetType
997     offset,
998     scene,
999     *xref;
1000
1001   MagickSizeType
1002     number_pixels;
1003
1004   MagickStatusType
1005     flags;
1006
1007   PointInfo
1008     delta,
1009     resolution,
1010     scale;
1011
1012   RectangleInfo
1013     geometry,
1014     media_info,
1015     page_info;
1016
1017   register const Quantum
1018     *p;
1019
1020   register unsigned char
1021     *q;
1022
1023   register ssize_t
1024     i,
1025     x;
1026
1027   size_t
1028     info_id,
1029     length,
1030     object,
1031     pages_id,
1032     root_id,
1033     text_size,
1034     version;
1035
1036   ssize_t
1037     count,
1038     y;
1039
1040   struct tm
1041     local_time;
1042
1043   time_t
1044     seconds;
1045
1046   unsigned char
1047     *pixels;
1048
1049   /*
1050     Open output image file.
1051   */
1052   assert(image_info != (const ImageInfo *) NULL);
1053   assert(image_info->signature == MagickSignature);
1054   assert(image != (Image *) NULL);
1055   assert(image->signature == MagickSignature);
1056   if (image->debug != MagickFalse)
1057     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1058   assert(exception != (ExceptionInfo *) NULL);
1059   assert(exception->signature == MagickSignature);
1060   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1061   if (status == MagickFalse)
1062     return(status);
1063   /*
1064     Allocate X ref memory.
1065   */
1066   xref=(MagickOffsetType *) AcquireQuantumMemory(2048UL,sizeof(*xref));
1067   if (xref == (MagickOffsetType *) NULL)
1068     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1069   (void) ResetMagickMemory(xref,0,2048UL*sizeof(*xref));
1070   /*
1071     Write Info object.
1072   */
1073   object=0;
1074   version=3;
1075   if (image_info->compression == JPEG2000Compression)
1076     version=(size_t) MagickMax(version,5);
1077   for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
1078     if (next->matte != MagickFalse)
1079       version=(size_t) MagickMax(version,4);
1080   if (LocaleCompare(image_info->magick,"PDFA") == 0)
1081     version=(size_t) MagickMax(version,6);
1082   (void) FormatLocaleString(buffer,MaxTextExtent,"%%PDF-1.%.20g \n",
1083     (double) version);
1084   (void) WriteBlobString(image,buffer);
1085   if (LocaleCompare(image_info->magick,"PDFA") == 0)
1086     (void) WriteBlobString(image,"%âãÏÓ\n");
1087   /*
1088     Write Catalog object.
1089   */
1090   xref[object++]=TellBlob(image);
1091   root_id=object;
1092   (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1093     object);
1094   (void) WriteBlobString(image,buffer);
1095   (void) WriteBlobString(image,"<<\n");
1096   if (LocaleCompare(image_info->magick,"PDFA") != 0)
1097     (void) FormatLocaleString(buffer,MaxTextExtent,"/Pages %.20g 0 R\n",
1098       (double) object+1);
1099   else
1100     {
1101       (void) FormatLocaleString(buffer,MaxTextExtent,"/Metadata %.20g 0 R\n",
1102         (double) object+1);
1103       (void) WriteBlobString(image,buffer);
1104       (void) FormatLocaleString(buffer,MaxTextExtent,"/Pages %.20g 0 R\n",
1105         (double) object+2);
1106     }
1107   (void) WriteBlobString(image,buffer);
1108   (void) WriteBlobString(image,"/Type /Catalog\n");
1109   (void) WriteBlobString(image,">>\n");
1110   (void) WriteBlobString(image,"endobj\n");
1111   if (LocaleCompare(image_info->magick,"PDFA") == 0)
1112     {
1113       char
1114         create_date[MaxTextExtent],
1115         modify_date[MaxTextExtent],
1116         timestamp[MaxTextExtent],
1117         xmp_profile[MaxTextExtent];
1118
1119       size_t
1120         version;
1121
1122       /*
1123         Write XMP object.
1124       */
1125       xref[object++]=TellBlob(image);
1126       (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",
1127         (double) object);
1128       (void) WriteBlobString(image,buffer);
1129       (void) WriteBlobString(image,"<<\n");
1130       (void) WriteBlobString(image,"/Subtype /XML\n");
1131       *modify_date='\0';
1132       value=GetImageProperty(image,"date:modify");
1133       if (value != (const char *) NULL)
1134         (void) CopyMagickString(modify_date,value,MaxTextExtent);
1135       *create_date='\0';
1136       value=GetImageProperty(image,"date:create");
1137       if (value != (const char *) NULL)
1138         (void) CopyMagickString(create_date,value,MaxTextExtent);
1139       (void) FormatMagickTime(time((time_t *) NULL),MaxTextExtent,timestamp);
1140       i=FormatLocaleString(xmp_profile,MaxTextExtent,XMPProfile,
1141         XMPProfileMagick,modify_date,create_date,timestamp,
1142         GetMagickVersion(&version),GetMagickVersion(&version));
1143       (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g\n",
1144         (double) i);
1145       (void) WriteBlobString(image,buffer);
1146       (void) WriteBlobString(image,"/Type /Metadata\n");
1147       (void) WriteBlobString(image,">>\nstream\n");
1148       (void) WriteBlobString(image,xmp_profile);
1149       (void) WriteBlobString(image,"endstream\n");
1150       (void) WriteBlobString(image,"endobj\n");
1151     }
1152   /*
1153     Write Pages object.
1154   */
1155   xref[object++]=TellBlob(image);
1156   pages_id=object;
1157   (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1158     object);
1159   (void) WriteBlobString(image,buffer);
1160   (void) WriteBlobString(image,"<<\n");
1161   (void) WriteBlobString(image,"/Type /Pages\n");
1162   (void) FormatLocaleString(buffer,MaxTextExtent,"/Kids [ %.20g 0 R ",
1163     (double) object+1);
1164   (void) WriteBlobString(image,buffer);
1165   count=(ssize_t) (pages_id+ObjectsPerImage+1);
1166   if (image_info->adjoin != MagickFalse)
1167     {
1168       Image
1169         *kid_image;
1170
1171       /*
1172         Predict page object id's.
1173       */
1174       kid_image=image;
1175       for ( ; GetNextImageInList(kid_image) != (Image *) NULL; count+=ObjectsPerImage)
1176       {
1177         (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 R ",(double)
1178           count);
1179         (void) WriteBlobString(image,buffer);
1180         kid_image=GetNextImageInList(kid_image);
1181       }
1182       xref=(MagickOffsetType *) ResizeQuantumMemory(xref,(size_t) count+2048UL,
1183         sizeof(*xref));
1184       if (xref == (MagickOffsetType *) NULL)
1185         ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1186     }
1187   (void) WriteBlobString(image,"]\n");
1188   (void) FormatLocaleString(buffer,MaxTextExtent,"/Count %.20g\n",
1189     (double) ((count-pages_id)/ObjectsPerImage));
1190   (void) WriteBlobString(image,buffer);
1191   (void) WriteBlobString(image,">>\n");
1192   (void) WriteBlobString(image,"endobj\n");
1193   scene=0;
1194   do
1195   {
1196     compression=image->compression;
1197     if (image_info->compression != UndefinedCompression)
1198       compression=image_info->compression;
1199     switch (compression)
1200     {
1201       case FaxCompression:
1202       case Group4Compression:
1203       {
1204         if ((IsImageMonochrome(image,exception) == MagickFalse) ||
1205             (image->matte != MagickFalse))
1206           compression=RLECompression;
1207         break;
1208       }
1209 #if !defined(MAGICKCORE_JPEG_DELEGATE)
1210       case JPEGCompression:
1211       {
1212         compression=RLECompression;
1213         (void) ThrowMagickException(exception,GetMagickModule(),
1214           MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
1215           image->filename);
1216         break;
1217       }
1218 #endif
1219 #if !defined(MAGICKCORE_JP2_DELEGATE)
1220       case JPEG2000Compression:
1221       {
1222         compression=RLECompression;
1223         (void) ThrowMagickException(exception,GetMagickModule(),
1224           MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JP2)",
1225           image->filename);
1226         break;
1227       }
1228 #endif
1229 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
1230       case ZipCompression:
1231       {
1232         compression=RLECompression;
1233         (void) ThrowMagickException(exception,GetMagickModule(),
1234           MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZLIB)",
1235           image->filename);
1236         break;
1237       }
1238 #endif
1239       case LZWCompression:
1240       {
1241         if (LocaleCompare(image_info->magick,"PDFA") == 0)
1242           compression=RLECompression;  /* LZW compression is forbidden */
1243         break;
1244       }
1245       case NoCompression:
1246       {
1247         if (LocaleCompare(image_info->magick,"PDFA") == 0)
1248           compression=RLECompression; /* ASCII 85 compression is forbidden */
1249         break;
1250       }
1251       default:
1252         break;
1253     }
1254     if (compression == JPEG2000Compression)
1255       {
1256         if (IsRGBColorspace(image->colorspace) == MagickFalse)
1257           (void) TransformImageColorspace(image,RGBColorspace);
1258       }
1259     /*
1260       Scale relative to dots-per-inch.
1261     */
1262     delta.x=DefaultResolution;
1263     delta.y=DefaultResolution;
1264     resolution.x=image->x_resolution;
1265     resolution.y=image->y_resolution;
1266     if ((resolution.x == 0.0) || (resolution.y == 0.0))
1267       {
1268         flags=ParseGeometry(PSDensityGeometry,&geometry_info);
1269         resolution.x=geometry_info.rho;
1270         resolution.y=geometry_info.sigma;
1271         if ((flags & SigmaValue) == 0)
1272           resolution.y=resolution.x;
1273       }
1274     if (image_info->density != (char *) NULL)
1275       {
1276         flags=ParseGeometry(image_info->density,&geometry_info);
1277         resolution.x=geometry_info.rho;
1278         resolution.y=geometry_info.sigma;
1279         if ((flags & SigmaValue) == 0)
1280           resolution.y=resolution.x;
1281       }
1282     if (image->units == PixelsPerCentimeterResolution)
1283       {
1284         resolution.x=(double) ((size_t) (100.0*2.54*resolution.x+0.5)/100.0);
1285         resolution.y=(double) ((size_t) (100.0*2.54*resolution.y+0.5)/100.0);
1286       }
1287     SetGeometry(image,&geometry);
1288     (void) FormatLocaleString(page_geometry,MaxTextExtent,"%.20gx%.20g",
1289       (double) image->columns,(double) image->rows);
1290     if (image_info->page != (char *) NULL)
1291       (void) CopyMagickString(page_geometry,image_info->page,MaxTextExtent);
1292     else
1293       if ((image->page.width != 0) && (image->page.height != 0))
1294         (void) FormatLocaleString(page_geometry,MaxTextExtent,
1295           "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,
1296           (double) image->page.height,(double) image->page.x,(double)
1297           image->page.y);
1298       else
1299         if ((image->gravity != UndefinedGravity) &&
1300             (LocaleCompare(image_info->magick,"PDF") == 0))
1301           (void) CopyMagickString(page_geometry,PSPageGeometry,MaxTextExtent);
1302     (void) ConcatenateMagickString(page_geometry,">",MaxTextExtent);
1303     (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
1304       &geometry.width,&geometry.height);
1305     scale.x=(double) (geometry.width*delta.x)/resolution.x;
1306     geometry.width=(size_t) floor(scale.x+0.5);
1307     scale.y=(double) (geometry.height*delta.y)/resolution.y;
1308     geometry.height=(size_t) floor(scale.y+0.5);
1309     (void) ParseAbsoluteGeometry(page_geometry,&media_info);
1310     (void) ParseGravityGeometry(image,page_geometry,&page_info,exception);
1311     if (image->gravity != UndefinedGravity)
1312       {
1313         geometry.x=(-page_info.x);
1314         geometry.y=(ssize_t) (media_info.height+page_info.y-image->rows);
1315       }
1316     pointsize=12.0;
1317     if (image_info->pointsize != 0.0)
1318       pointsize=image_info->pointsize;
1319     text_size=0;
1320     value=GetImageProperty(image,"label");
1321     if (value != (const char *) NULL)
1322       text_size=(size_t) (MultilineCensus(value)*pointsize+12);
1323     (void) text_size;
1324     /*
1325       Write Page object.
1326     */
1327     xref[object++]=TellBlob(image);
1328     (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1329       object);
1330     (void) WriteBlobString(image,buffer);
1331     (void) WriteBlobString(image,"<<\n");
1332     (void) WriteBlobString(image,"/Type /Page\n");
1333     (void) FormatLocaleString(buffer,MaxTextExtent,"/Parent %.20g 0 R\n",
1334       (double) pages_id);
1335     (void) WriteBlobString(image,buffer);
1336     (void) WriteBlobString(image,"/Resources <<\n");
1337     labels=(char **) NULL;
1338     value=GetImageProperty(image,"label");
1339     if (value != (const char *) NULL)
1340       labels=StringToList(value);
1341     if (labels != (char **) NULL)
1342       {
1343         (void) FormatLocaleString(buffer,MaxTextExtent,
1344           "/Font << /F%.20g %.20g 0 R >>\n",(double) image->scene,(double)
1345           object+4);
1346         (void) WriteBlobString(image,buffer);
1347       }
1348     (void) FormatLocaleString(buffer,MaxTextExtent,
1349       "/XObject << /Im%.20g %.20g 0 R >>\n",(double) image->scene,(double)
1350       object+5);
1351     (void) WriteBlobString(image,buffer);
1352     (void) FormatLocaleString(buffer,MaxTextExtent,"/ProcSet %.20g 0 R >>\n",
1353       (double) object+3);
1354     (void) WriteBlobString(image,buffer);
1355     (void) FormatLocaleString(buffer,MaxTextExtent,
1356       "/MediaBox [0 0 %g %g]\n",72.0*media_info.width/resolution.x,
1357       72.0*media_info.height/resolution.y);
1358     (void) WriteBlobString(image,buffer);
1359     (void) FormatLocaleString(buffer,MaxTextExtent,
1360       "/CropBox [0 0 %g %g]\n",72.0*media_info.width/resolution.x,
1361       72.0*media_info.height/resolution.y);
1362     (void) WriteBlobString(image,buffer);
1363     (void) FormatLocaleString(buffer,MaxTextExtent,"/Contents %.20g 0 R\n",
1364       (double) object+1);
1365     (void) WriteBlobString(image,buffer);
1366     (void) FormatLocaleString(buffer,MaxTextExtent,"/Thumb %.20g 0 R\n",
1367       (double) object+8);
1368     (void) WriteBlobString(image,buffer);
1369     (void) WriteBlobString(image,">>\n");
1370     (void) WriteBlobString(image,"endobj\n");
1371     /*
1372       Write Contents object.
1373     */
1374     xref[object++]=TellBlob(image);
1375     (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1376       object);
1377     (void) WriteBlobString(image,buffer);
1378     (void) WriteBlobString(image,"<<\n");
1379     (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
1380       (double) object+1);
1381     (void) WriteBlobString(image,buffer);
1382     (void) WriteBlobString(image,">>\n");
1383     (void) WriteBlobString(image,"stream\n");
1384     offset=TellBlob(image);
1385     (void) WriteBlobString(image,"q\n");
1386     if (labels != (char **) NULL)
1387       for (i=0; labels[i] != (char *) NULL; i++)
1388       {
1389         (void) WriteBlobString(image,"BT\n");
1390         (void) FormatLocaleString(buffer,MaxTextExtent,"/F%.20g %g Tf\n",
1391           (double) image->scene,pointsize);
1392         (void) WriteBlobString(image,buffer);
1393         (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g Td\n",
1394           (double) geometry.x,(double) (geometry.y+geometry.height+i*pointsize+
1395           12));
1396         (void) WriteBlobString(image,buffer);
1397         (void) FormatLocaleString(buffer,MaxTextExtent,"(%s) Tj\n",labels[i]);
1398         (void) WriteBlobString(image,buffer);
1399         (void) WriteBlobString(image,"ET\n");
1400         labels[i]=DestroyString(labels[i]);
1401       }
1402     (void) FormatLocaleString(buffer,MaxTextExtent,"%g 0 0 %g %.20g %.20g cm\n",
1403       scale.x,scale.y,(double) geometry.x,(double) geometry.y);
1404     (void) WriteBlobString(image,buffer);
1405     (void) FormatLocaleString(buffer,MaxTextExtent,"/Im%.20g Do\n",
1406       (double) image->scene);
1407     (void) WriteBlobString(image,buffer);
1408     (void) WriteBlobString(image,"Q\n");
1409     offset=TellBlob(image)-offset;
1410     (void) WriteBlobString(image,"endstream\n");
1411     (void) WriteBlobString(image,"endobj\n");
1412     /*
1413       Write Length object.
1414     */
1415     xref[object++]=TellBlob(image);
1416     (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1417       object);
1418     (void) WriteBlobString(image,buffer);
1419     (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
1420     (void) WriteBlobString(image,buffer);
1421     (void) WriteBlobString(image,"endobj\n");
1422     /*
1423       Write Procset object.
1424     */
1425     xref[object++]=TellBlob(image);
1426     (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",
1427       (double) object);
1428     (void) WriteBlobString(image,buffer);
1429     if ((image->storage_class == DirectClass) || (image->colors > 256))
1430       (void) CopyMagickString(buffer,"[ /PDF /Text /ImageC",MaxTextExtent);
1431     else
1432       if ((compression == FaxCompression) || (compression == Group4Compression))
1433         (void) CopyMagickString(buffer,"[ /PDF /Text /ImageB",MaxTextExtent);
1434       else
1435         (void) CopyMagickString(buffer,"[ /PDF /Text /ImageI",MaxTextExtent);
1436     (void) WriteBlobString(image,buffer);
1437     (void) WriteBlobString(image," ]\n");
1438     (void) WriteBlobString(image,"endobj\n");
1439     /*
1440       Write Font object.
1441     */
1442     xref[object++]=TellBlob(image);
1443     (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",
1444       (double) object);
1445     (void) WriteBlobString(image,buffer);
1446     (void) WriteBlobString(image,"<<\n");
1447     if (labels != (char **) NULL)
1448       {
1449         (void) WriteBlobString(image,"/Type /Font\n");
1450         (void) WriteBlobString(image,"/Subtype /Type1\n");
1451         (void) FormatLocaleString(buffer,MaxTextExtent,"/Name /F%.20g\n",
1452           (double) image->scene);
1453         (void) WriteBlobString(image,buffer);
1454         (void) WriteBlobString(image,"/BaseFont /Helvetica\n");
1455         (void) WriteBlobString(image,"/Encoding /MacRomanEncoding\n");
1456         labels=(char **) RelinquishMagickMemory(labels);
1457       }
1458     (void) WriteBlobString(image,">>\n");
1459     (void) WriteBlobString(image,"endobj\n");
1460     /*
1461       Write XObject object.
1462     */
1463     xref[object++]=TellBlob(image);
1464     (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1465       object);
1466     (void) WriteBlobString(image,buffer);
1467     (void) WriteBlobString(image,"<<\n");
1468     (void) WriteBlobString(image,"/Type /XObject\n");
1469     (void) WriteBlobString(image,"/Subtype /Image\n");
1470     (void) FormatLocaleString(buffer,MaxTextExtent,"/Name /Im%.20g\n",
1471       (double) image->scene);
1472     (void) WriteBlobString(image,buffer);
1473     switch (compression)
1474     {
1475       case NoCompression:
1476       {
1477         (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"ASCII85Decode");
1478         break;
1479       }
1480       case JPEGCompression:
1481       {
1482         (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"DCTDecode");
1483         if (image->colorspace != CMYKColorspace)
1484           break;
1485         (void) WriteBlobString(image,buffer);
1486         (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1487           MaxTextExtent);
1488         break;
1489       }
1490       case JPEG2000Compression:
1491       {
1492         (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"JPXDecode");
1493         if (image->colorspace != CMYKColorspace)
1494           break;
1495         (void) WriteBlobString(image,buffer);
1496         (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1497           MaxTextExtent);
1498         break;
1499       }
1500       case LZWCompression:
1501       {
1502         (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"LZWDecode");
1503         break;
1504       }
1505       case ZipCompression:
1506       {
1507         (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"FlateDecode");
1508         break;
1509       }
1510       case FaxCompression:
1511       case Group4Compression:
1512       {
1513         (void) CopyMagickString(buffer,"/Filter [ /CCITTFaxDecode ]\n",
1514           MaxTextExtent);
1515         (void) WriteBlobString(image,buffer);
1516         (void) FormatLocaleString(buffer,MaxTextExtent,"/DecodeParms [ << "
1517           "/K %s /BlackIs1 false /Columns %.20g /Rows %.20g >> ]\n",CCITTParam,
1518           (double) image->columns,(double) image->rows);
1519         break;
1520       }
1521       default:
1522       {
1523         (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
1524           "RunLengthDecode");
1525         break;
1526       }
1527     }
1528     (void) WriteBlobString(image,buffer);
1529     (void) FormatLocaleString(buffer,MaxTextExtent,"/Width %.20g\n",(double)
1530       image->columns);
1531     (void) WriteBlobString(image,buffer);
1532     (void) FormatLocaleString(buffer,MaxTextExtent,"/Height %.20g\n",(double)
1533       image->rows);
1534     (void) WriteBlobString(image,buffer);
1535     (void) FormatLocaleString(buffer,MaxTextExtent,"/ColorSpace %.20g 0 R\n",
1536       (double) object+2);
1537     (void) WriteBlobString(image,buffer);
1538     (void) FormatLocaleString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
1539       (compression == FaxCompression) || (compression == Group4Compression) ?
1540       1 : 8);
1541     (void) WriteBlobString(image,buffer);
1542     if (image->matte != MagickFalse)
1543       {
1544         (void) FormatLocaleString(buffer,MaxTextExtent,"/SMask %.20g 0 R\n",
1545           (double) object+7);
1546         (void) WriteBlobString(image,buffer);
1547       }
1548     (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
1549       (double) object+1);
1550     (void) WriteBlobString(image,buffer);
1551     (void) WriteBlobString(image,">>\n");
1552     (void) WriteBlobString(image,"stream\n");
1553     offset=TellBlob(image);
1554     number_pixels=(MagickSizeType) image->columns*image->rows;
1555     if ((4*number_pixels) != (MagickSizeType) ((size_t) (4*number_pixels)))
1556       ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1557     if ((compression == FaxCompression) || (compression == Group4Compression) ||
1558         ((image_info->type != TrueColorType) &&
1559          (IsImageGray(image,exception) != MagickFalse)))
1560       {
1561         switch (compression)
1562         {
1563           case FaxCompression:
1564           case Group4Compression:
1565           {
1566             if (LocaleCompare(CCITTParam,"0") == 0)
1567               {
1568                 (void) HuffmanEncodeImage(image_info,image,image,exception);
1569                 break;
1570               }
1571             (void) Huffman2DEncodeImage(image_info,image,image,exception);
1572             break;
1573           }
1574           case JPEGCompression:
1575           {
1576             status=InjectImageBlob(image_info,image,image,"jpeg",exception);
1577             if (status == MagickFalse)
1578               {
1579                 (void) CloseBlob(image);
1580                 return(MagickFalse);
1581               }
1582             break;
1583           }
1584           case JPEG2000Compression:
1585           {
1586             status=InjectImageBlob(image_info,image,image,"jp2",exception);
1587             if (status == MagickFalse)
1588               {
1589                 (void) CloseBlob(image);
1590                 return(MagickFalse);
1591               }
1592             break;
1593           }
1594           case RLECompression:
1595           default:
1596           {
1597             /*
1598               Allocate pixel array.
1599             */
1600             length=(size_t) number_pixels;
1601             pixels=(unsigned char *) AcquireQuantumMemory(length,
1602               sizeof(*pixels));
1603             if (pixels == (unsigned char *) NULL)
1604               ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1605             /*
1606               Dump Runlength encoded pixels.
1607             */
1608             q=pixels;
1609             for (y=0; y < (ssize_t) image->rows; y++)
1610             {
1611               p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1612               if (p == (const Quantum *) NULL)
1613                 break;
1614               for (x=0; x < (ssize_t) image->columns; x++)
1615               {
1616                 *q++=ScaleQuantumToChar(GetPixelIntensity(image,p));
1617                 p+=GetPixelChannels(image);
1618               }
1619               if (image->previous == (Image *) NULL)
1620                 {
1621                   status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1622                     y,image->rows);
1623                   if (status == MagickFalse)
1624                     break;
1625                 }
1626             }
1627 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1628             if (compression == ZipCompression)
1629               status=ZLIBEncodeImage(image,length,pixels,exception);
1630             else
1631 #endif
1632               if (compression == LZWCompression)
1633                 status=LZWEncodeImage(image,length,pixels,exception);
1634               else
1635                 status=PackbitsEncodeImage(image,length,pixels,exception);
1636             pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1637             if (status == MagickFalse)
1638               {
1639                 (void) CloseBlob(image);
1640                 return(MagickFalse);
1641               }
1642             break;
1643           }
1644           case NoCompression:
1645           {
1646             /*
1647               Dump uncompressed PseudoColor packets.
1648             */
1649             Ascii85Initialize(image);
1650             for (y=0; y < (ssize_t) image->rows; y++)
1651             {
1652               p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1653               if (p == (const Quantum *) NULL)
1654                 break;
1655               for (x=0; x < (ssize_t) image->columns; x++)
1656               {
1657                 Ascii85Encode(image,ScaleQuantumToChar(
1658                   GetPixelIntensity(image,p)));
1659                 p+=GetPixelChannels(image);
1660               }
1661               if (image->previous == (Image *) NULL)
1662                 {
1663                   status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1664                     y,image->rows);
1665                   if (status == MagickFalse)
1666                     break;
1667                 }
1668             }
1669             Ascii85Flush(image);
1670             break;
1671           }
1672         }
1673       }
1674     else
1675       if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1676           (compression == JPEGCompression) ||
1677           (compression == JPEG2000Compression))
1678         switch (compression)
1679         {
1680           case JPEGCompression:
1681           {
1682             status=InjectImageBlob(image_info,image,image,"jpeg",exception);
1683             if (status == MagickFalse)
1684               {
1685                 (void) CloseBlob(image);
1686                 return(MagickFalse);
1687               }
1688             break;
1689           }
1690           case JPEG2000Compression:
1691           {
1692             status=InjectImageBlob(image_info,image,image,"jp2",exception);
1693             if (status == MagickFalse)
1694               {
1695                 (void) CloseBlob(image);
1696                 return(MagickFalse);
1697               }
1698             break;
1699           }
1700           case RLECompression:
1701           default:
1702           {
1703             /*
1704               Allocate pixel array.
1705             */
1706             length=(size_t) number_pixels;
1707             pixels=(unsigned char *) AcquireQuantumMemory(length,
1708               4*sizeof(*pixels));
1709             length*=image->colorspace == CMYKColorspace ? 4UL : 3UL;
1710             if (pixels == (unsigned char *) NULL)
1711               ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1712             /*
1713               Dump runoffset encoded pixels.
1714             */
1715             q=pixels;
1716             for (y=0; y < (ssize_t) image->rows; y++)
1717             {
1718               p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1719               if (p == (const Quantum *) NULL)
1720                 break;
1721               for (x=0; x < (ssize_t) image->columns; x++)
1722               {
1723                 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1724                 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1725                 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1726                 if (image->colorspace == CMYKColorspace)
1727                   *q++=ScaleQuantumToChar(GetPixelBlack(image,p));
1728                 p+=GetPixelChannels(image);
1729               }
1730               if (image->previous == (Image *) NULL)
1731                 {
1732                   status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1733                     y,image->rows);
1734                   if (status == MagickFalse)
1735                     break;
1736                 }
1737             }
1738 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1739             if (compression == ZipCompression)
1740               status=ZLIBEncodeImage(image,length,pixels,exception);
1741             else
1742 #endif
1743               if (compression == LZWCompression)
1744                 status=LZWEncodeImage(image,length,pixels,exception);
1745               else
1746                 status=PackbitsEncodeImage(image,length,pixels,exception);
1747             pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1748             if (status == MagickFalse)
1749               {
1750                 (void) CloseBlob(image);
1751                 return(MagickFalse);
1752               }
1753             break;
1754           }
1755           case NoCompression:
1756           {
1757             /*
1758               Dump uncompressed DirectColor packets.
1759             */
1760             Ascii85Initialize(image);
1761             for (y=0; y < (ssize_t) image->rows; y++)
1762             {
1763               p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1764               if (p == (const Quantum *) NULL)
1765                 break;
1766               for (x=0; x < (ssize_t) image->columns; x++)
1767               {
1768                 Ascii85Encode(image,ScaleQuantumToChar(GetPixelRed(image,p)));
1769                 Ascii85Encode(image,ScaleQuantumToChar(GetPixelGreen(image,p)));
1770                 Ascii85Encode(image,ScaleQuantumToChar(GetPixelBlue(image,p)));
1771                 if (image->colorspace == CMYKColorspace)
1772                   Ascii85Encode(image,ScaleQuantumToChar(
1773                     GetPixelBlack(image,p)));
1774                 p+=GetPixelChannels(image);
1775               }
1776               if (image->previous == (Image *) NULL)
1777                 {
1778                   status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1779                     y,image->rows);
1780                   if (status == MagickFalse)
1781                     break;
1782                 }
1783             }
1784             Ascii85Flush(image);
1785             break;
1786           }
1787         }
1788       else
1789         {
1790           /*
1791             Dump number of colors and colormap.
1792           */
1793           switch (compression)
1794           {
1795             case RLECompression:
1796             default:
1797             {
1798               /*
1799                 Allocate pixel array.
1800               */
1801               length=(size_t) number_pixels;
1802               pixels=(unsigned char *) AcquireQuantumMemory(length,
1803                 sizeof(*pixels));
1804               if (pixels == (unsigned char *) NULL)
1805                 ThrowWriterException(ResourceLimitError,
1806                   "MemoryAllocationFailed");
1807               /*
1808                 Dump Runlength encoded pixels.
1809               */
1810               q=pixels;
1811               for (y=0; y < (ssize_t) image->rows; y++)
1812               {
1813                 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1814                 if (p == (const Quantum *) NULL)
1815                   break;
1816                 for (x=0; x < (ssize_t) image->columns; x++)
1817                 {
1818                   *q++=(unsigned char) GetPixelIndex(image,p);
1819                   p+=GetPixelChannels(image);
1820                 }
1821                 if (image->previous == (Image *) NULL)
1822                   {
1823                     status=SetImageProgress(image,SaveImageTag,
1824                       (MagickOffsetType) y,image->rows);
1825                     if (status == MagickFalse)
1826                       break;
1827                   }
1828               }
1829 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1830               if (compression == ZipCompression)
1831                 status=ZLIBEncodeImage(image,length,pixels,exception);
1832               else
1833 #endif
1834                 if (compression == LZWCompression)
1835                   status=LZWEncodeImage(image,length,pixels,exception);
1836                 else
1837                   status=PackbitsEncodeImage(image,length,pixels,exception);
1838               pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1839               if (status == MagickFalse)
1840                 {
1841                   (void) CloseBlob(image);
1842                   return(MagickFalse);
1843                 }
1844               break;
1845             }
1846             case NoCompression:
1847             {
1848               /*
1849                 Dump uncompressed PseudoColor packets.
1850               */
1851               Ascii85Initialize(image);
1852               for (y=0; y < (ssize_t) image->rows; y++)
1853               {
1854                 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1855                 if (p == (const Quantum *) NULL)
1856                   break;
1857                 for (x=0; x < (ssize_t) image->columns; x++)
1858                 {
1859                   Ascii85Encode(image,(unsigned char) GetPixelIndex(image,p));
1860                   p+=GetPixelChannels(image);
1861                 }
1862                 if (image->previous == (Image *) NULL)
1863                   {
1864                     status=SetImageProgress(image,SaveImageTag,
1865                       (MagickOffsetType) y,image->rows);
1866                     if (status == MagickFalse)
1867                       break;
1868                   }
1869               }
1870               Ascii85Flush(image);
1871               break;
1872             }
1873           }
1874         }
1875     offset=TellBlob(image)-offset;
1876     (void) WriteBlobString(image,"\nendstream\n");
1877     (void) WriteBlobString(image,"endobj\n");
1878     /*
1879       Write Length object.
1880     */
1881     xref[object++]=TellBlob(image);
1882     (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1883       object);
1884     (void) WriteBlobString(image,buffer);
1885     (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",
1886       (double) offset);
1887     (void) WriteBlobString(image,buffer);
1888     (void) WriteBlobString(image,"endobj\n");
1889     /*
1890       Write Colorspace object.
1891     */
1892     xref[object++]=TellBlob(image);
1893     (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1894       object);
1895     (void) WriteBlobString(image,buffer);
1896     if (image->colorspace == CMYKColorspace)
1897       (void) CopyMagickString(buffer,"/DeviceCMYK\n",MaxTextExtent);
1898     else
1899       if ((compression == FaxCompression) ||
1900           (compression == Group4Compression) ||
1901           ((image_info->type != TrueColorType) &&
1902            (IsImageGray(image,exception) != MagickFalse)))
1903           (void) CopyMagickString(buffer,"/DeviceGray\n",MaxTextExtent);
1904       else
1905         if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1906             (compression == JPEGCompression) ||
1907             (compression == JPEG2000Compression))
1908           (void) CopyMagickString(buffer,"/DeviceRGB\n",MaxTextExtent);
1909         else
1910           (void) FormatLocaleString(buffer,MaxTextExtent,
1911             "[ /Indexed /DeviceRGB %.20g %.20g 0 R ]\n",(double) image->colors-
1912             1,(double) object+3);
1913     (void) WriteBlobString(image,buffer);
1914     (void) WriteBlobString(image,"endobj\n");
1915     /*
1916       Write Thumb object.
1917     */
1918     SetGeometry(image,&geometry);
1919     (void) ParseMetaGeometry("106x106+0+0>",&geometry.x,&geometry.y,
1920       &geometry.width,&geometry.height);
1921     tile_image=ThumbnailImage(image,geometry.width,geometry.height,exception);
1922     if (tile_image == (Image *) NULL)
1923       return(MagickFalse);
1924     xref[object++]=TellBlob(image);
1925     (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1926       object);
1927     (void) WriteBlobString(image,buffer);
1928     (void) WriteBlobString(image,"<<\n");
1929     switch (compression)
1930     {
1931       case NoCompression:
1932       {
1933         (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"ASCII85Decode");
1934         break;
1935       }
1936       case JPEGCompression:
1937       {
1938         (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"DCTDecode");
1939         if (image->colorspace != CMYKColorspace)
1940           break;
1941         (void) WriteBlobString(image,buffer);
1942         (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1943           MaxTextExtent);
1944         break;
1945       }
1946       case JPEG2000Compression:
1947       {
1948         (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"JPXDecode");
1949         if (image->colorspace != CMYKColorspace)
1950           break;
1951         (void) WriteBlobString(image,buffer);
1952         (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1953           MaxTextExtent);
1954         break;
1955       }
1956       case LZWCompression:
1957       {
1958         (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"LZWDecode");
1959         break;
1960       }
1961       case ZipCompression:
1962       {
1963         (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"FlateDecode");
1964         break;
1965       }
1966       case FaxCompression:
1967       case Group4Compression:
1968       {
1969         (void) CopyMagickString(buffer,"/Filter [ /CCITTFaxDecode ]\n",
1970           MaxTextExtent);
1971         (void) WriteBlobString(image,buffer);
1972         (void) FormatLocaleString(buffer,MaxTextExtent,"/DecodeParms [ << "
1973           "/K %s /BlackIs1 false /Columns %.20g /Rows %.20g >> ]\n",CCITTParam,
1974           (double) tile_image->columns,(double) tile_image->rows);
1975         break;
1976       }
1977       default:
1978       {
1979         (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
1980           "RunLengthDecode");
1981         break;
1982       }
1983     }
1984     (void) WriteBlobString(image,buffer);
1985     (void) FormatLocaleString(buffer,MaxTextExtent,"/Width %.20g\n",(double)
1986       tile_image->columns);
1987     (void) WriteBlobString(image,buffer);
1988     (void) FormatLocaleString(buffer,MaxTextExtent,"/Height %.20g\n",(double)
1989       tile_image->rows);
1990     (void) WriteBlobString(image,buffer);
1991     (void) FormatLocaleString(buffer,MaxTextExtent,"/ColorSpace %.20g 0 R\n",
1992       (double) object-1);
1993     (void) WriteBlobString(image,buffer);
1994     (void) FormatLocaleString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
1995       (compression == FaxCompression) || (compression == Group4Compression) ?
1996       1 : 8);
1997     (void) WriteBlobString(image,buffer);
1998     (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
1999       (double) object+1);
2000     (void) WriteBlobString(image,buffer);
2001     (void) WriteBlobString(image,">>\n");
2002     (void) WriteBlobString(image,"stream\n");
2003     offset=TellBlob(image);
2004     number_pixels=(MagickSizeType) tile_image->columns*tile_image->rows;
2005     if ((compression == FaxCompression) ||
2006         (compression == Group4Compression) ||
2007         ((image_info->type != TrueColorType) &&
2008          (IsImageGray(tile_image,exception) != MagickFalse)))
2009       {
2010         switch (compression)
2011         {
2012           case FaxCompression:
2013           case Group4Compression:
2014           {
2015             if (LocaleCompare(CCITTParam,"0") == 0)
2016               {
2017                 (void) HuffmanEncodeImage(image_info,image,tile_image,
2018                   exception);
2019                 break;
2020               }
2021             (void) Huffman2DEncodeImage(image_info,image,tile_image,exception);
2022             break;
2023           }
2024           case JPEGCompression:
2025           {
2026             status=InjectImageBlob(image_info,image,tile_image,"jpeg",
2027               exception);
2028             if (status == MagickFalse)
2029               {
2030                 (void) CloseBlob(image);
2031                 return(MagickFalse);
2032               }
2033             break;
2034           }
2035           case JPEG2000Compression:
2036           {
2037             status=InjectImageBlob(image_info,image,tile_image,"jp2",exception);
2038             if (status == MagickFalse)
2039               {
2040                 (void) CloseBlob(image);
2041                 return(MagickFalse);
2042               }
2043             break;
2044           }
2045           case RLECompression:
2046           default:
2047           {
2048             /*
2049               Allocate pixel array.
2050             */
2051             length=(size_t) number_pixels;
2052             pixels=(unsigned char *) AcquireQuantumMemory(length,
2053               sizeof(*pixels));
2054             if (pixels == (unsigned char *) NULL)
2055               {
2056                 tile_image=DestroyImage(tile_image);
2057                 ThrowWriterException(ResourceLimitError,
2058                   "MemoryAllocationFailed");
2059               }
2060             /*
2061               Dump Runlength encoded pixels.
2062             */
2063             q=pixels;
2064             for (y=0; y < (ssize_t) tile_image->rows; y++)
2065             {
2066               p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2067                 exception);
2068               if (p == (const Quantum *) NULL)
2069                 break;
2070               for (x=0; x < (ssize_t) tile_image->columns; x++)
2071               {
2072                 *q++=ScaleQuantumToChar(GetPixelIntensity(tile_image,p));
2073                 p+=GetPixelChannels(tile_image);
2074               }
2075             }
2076 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2077             if (compression == ZipCompression)
2078               status=ZLIBEncodeImage(image,length,pixels,exception);
2079             else
2080 #endif
2081               if (compression == LZWCompression)
2082                 status=LZWEncodeImage(image,length,pixels,exception);
2083               else
2084                 status=PackbitsEncodeImage(image,length,pixels,exception);
2085             pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2086             if (status == MagickFalse)
2087               {
2088                 (void) CloseBlob(image);
2089                 return(MagickFalse);
2090               }
2091             break;
2092           }
2093           case NoCompression:
2094           {
2095             /*
2096               Dump uncompressed PseudoColor packets.
2097             */
2098             Ascii85Initialize(image);
2099             for (y=0; y < (ssize_t) tile_image->rows; y++)
2100             {
2101               p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2102                 exception);
2103               if (p == (const Quantum *) NULL)
2104                 break;
2105               for (x=0; x < (ssize_t) tile_image->columns; x++)
2106               {
2107                 Ascii85Encode(image,
2108                   ScaleQuantumToChar(GetPixelIntensity(tile_image,p)));
2109                 p+=GetPixelChannels(tile_image);
2110               }
2111             }
2112             Ascii85Flush(image);
2113             break;
2114           }
2115         }
2116       }
2117     else
2118       if ((tile_image->storage_class == DirectClass) ||
2119           (tile_image->colors > 256) || (compression == JPEGCompression) ||
2120           (compression == JPEG2000Compression))
2121         switch (compression)
2122         {
2123           case JPEGCompression:
2124           {
2125             status=InjectImageBlob(image_info,image,tile_image,"jpeg",
2126               exception);
2127             if (status == MagickFalse)
2128               {
2129                 (void) CloseBlob(image);
2130                 return(MagickFalse);
2131               }
2132             break;
2133           }
2134           case JPEG2000Compression:
2135           {
2136             status=InjectImageBlob(image_info,image,tile_image,"jp2",exception);
2137             if (status == MagickFalse)
2138               {
2139                 (void) CloseBlob(image);
2140                 return(MagickFalse);
2141               }
2142             break;
2143           }
2144           case RLECompression:
2145           default:
2146           {
2147             /*
2148               Allocate pixel array.
2149             */
2150             length=(size_t) number_pixels;
2151             pixels=(unsigned char *) AcquireQuantumMemory(length,4*
2152               sizeof(*pixels));
2153             length*=tile_image->colorspace == CMYKColorspace ? 4UL : 3UL;
2154             if (pixels == (unsigned char *) NULL)
2155               {
2156                 tile_image=DestroyImage(tile_image);
2157                 ThrowWriterException(ResourceLimitError,
2158                   "MemoryAllocationFailed");
2159               }
2160             /*
2161               Dump runoffset encoded pixels.
2162             */
2163             q=pixels;
2164             for (y=0; y < (ssize_t) tile_image->rows; y++)
2165             {
2166               p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2167                 exception);
2168               if (p == (const Quantum *) NULL)
2169                 break;
2170               for (x=0; x < (ssize_t) tile_image->columns; x++)
2171               {
2172                 *q++=ScaleQuantumToChar(GetPixelRed(tile_image,p));
2173                 *q++=ScaleQuantumToChar(GetPixelGreen(tile_image,p));
2174                 *q++=ScaleQuantumToChar(GetPixelBlue(tile_image,p));
2175                 if (image->colorspace == CMYKColorspace)
2176                   *q++=ScaleQuantumToChar(GetPixelBlack(tile_image,p));
2177                 p+=GetPixelChannels(tile_image);
2178               }
2179             }
2180 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2181             if (compression == ZipCompression)
2182               status=ZLIBEncodeImage(image,length,pixels,exception);
2183             else
2184 #endif
2185               if (compression == LZWCompression)
2186                 status=LZWEncodeImage(image,length,pixels,exception);
2187               else
2188                 status=PackbitsEncodeImage(image,length,pixels,exception);
2189             pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2190             if (status == MagickFalse)
2191               {
2192                 (void) CloseBlob(image);
2193                 return(MagickFalse);
2194               }
2195             break;
2196           }
2197           case NoCompression:
2198           {
2199             /*
2200               Dump uncompressed DirectColor packets.
2201             */
2202             Ascii85Initialize(image);
2203             for (y=0; y < (ssize_t) tile_image->rows; y++)
2204             {
2205               p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2206                 exception);
2207               if (p == (const Quantum *) NULL)
2208                 break;
2209               for (x=0; x < (ssize_t) tile_image->columns; x++)
2210               {
2211                 Ascii85Encode(image,ScaleQuantumToChar(
2212                   GetPixelRed(tile_image,p)));
2213                 Ascii85Encode(image,ScaleQuantumToChar(
2214                   GetPixelGreen(tile_image,p)));
2215                 Ascii85Encode(image,ScaleQuantumToChar(
2216                   GetPixelBlue(tile_image,p)));
2217                 if (image->colorspace == CMYKColorspace)
2218                   Ascii85Encode(image,ScaleQuantumToChar(
2219                     GetPixelBlack(tile_image,p)));
2220                 p+=GetPixelChannels(tile_image);
2221               }
2222             }
2223             Ascii85Flush(image);
2224             break;
2225           }
2226         }
2227       else
2228         {
2229           /*
2230             Dump number of colors and colormap.
2231           */
2232           switch (compression)
2233           {
2234             case RLECompression:
2235             default:
2236             {
2237               /*
2238                 Allocate pixel array.
2239               */
2240               length=(size_t) number_pixels;
2241               pixels=(unsigned char *) AcquireQuantumMemory(length,
2242                 sizeof(*pixels));
2243               if (pixels == (unsigned char *) NULL)
2244                 {
2245                   tile_image=DestroyImage(tile_image);
2246                   ThrowWriterException(ResourceLimitError,
2247                     "MemoryAllocationFailed");
2248                 }
2249               /*
2250                 Dump Runlength encoded pixels.
2251               */
2252               q=pixels;
2253               for (y=0; y < (ssize_t) tile_image->rows; y++)
2254               {
2255                 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2256                   exception);
2257                 if (p == (const Quantum *) NULL)
2258                   break;
2259                 for (x=0; x < (ssize_t) tile_image->columns; x++)
2260                 {
2261                   *q++=(unsigned char) GetPixelIndex(tile_image,p);
2262                   q+=GetPixelChannels(image);
2263                 }
2264               }
2265 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2266               if (compression == ZipCompression)
2267                 status=ZLIBEncodeImage(image,length,pixels,exception);
2268               else
2269 #endif
2270                 if (compression == LZWCompression)
2271                   status=LZWEncodeImage(image,length,pixels,exception);
2272                 else
2273                   status=PackbitsEncodeImage(image,length,pixels,exception);
2274               pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2275               if (status == MagickFalse)
2276                 {
2277                   (void) CloseBlob(image);
2278                   return(MagickFalse);
2279                 }
2280               break;
2281             }
2282             case NoCompression:
2283             {
2284               /*
2285                 Dump uncompressed PseudoColor packets.
2286               */
2287               Ascii85Initialize(image);
2288               for (y=0; y < (ssize_t) tile_image->rows; y++)
2289               {
2290                 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2291                   exception);
2292                 if (p == (const Quantum *) NULL)
2293                   break;
2294                 for (x=0; x < (ssize_t) tile_image->columns; x++)
2295                 {
2296                   Ascii85Encode(image,(unsigned char)
2297                     GetPixelIndex(tile_image,p));
2298                   p+=GetPixelChannels(image);
2299                 }
2300               }
2301               Ascii85Flush(image);
2302               break;
2303             }
2304           }
2305         }
2306     tile_image=DestroyImage(tile_image);
2307     offset=TellBlob(image)-offset;
2308     (void) WriteBlobString(image,"\nendstream\n");
2309     (void) WriteBlobString(image,"endobj\n");
2310     /*
2311       Write Length object.
2312     */
2313     xref[object++]=TellBlob(image);
2314     (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2315       object);
2316     (void) WriteBlobString(image,buffer);
2317     (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
2318     (void) WriteBlobString(image,buffer);
2319     (void) WriteBlobString(image,"endobj\n");
2320     xref[object++]=TellBlob(image);
2321     (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2322       object);
2323     (void) WriteBlobString(image,buffer);
2324     if ((image->storage_class != DirectClass) && (image->colors <= 256) &&
2325         (compression != FaxCompression) && (compression != Group4Compression))
2326       {
2327         /*
2328           Write Colormap object.
2329         */
2330         (void) WriteBlobString(image,"<<\n");
2331         if (compression == NoCompression)
2332           (void) WriteBlobString(image,"/Filter [ /ASCII85Decode ]\n");
2333         (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
2334           (double) object+1);
2335         (void) WriteBlobString(image,buffer);
2336         (void) WriteBlobString(image,">>\n");
2337         (void) WriteBlobString(image,"stream\n");
2338         offset=TellBlob(image);
2339         if (compression == NoCompression)
2340           Ascii85Initialize(image);
2341         for (i=0; i < (ssize_t) image->colors; i++)
2342         {
2343           if (compression == NoCompression)
2344             {
2345               Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].red));
2346               Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].green));
2347               Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].blue));
2348               continue;
2349             }
2350           (void) WriteBlobByte(image,
2351             ScaleQuantumToChar(image->colormap[i].red));
2352           (void) WriteBlobByte(image,
2353             ScaleQuantumToChar(image->colormap[i].green));
2354           (void) WriteBlobByte(image,
2355             ScaleQuantumToChar(image->colormap[i].blue));
2356         }
2357         if (compression == NoCompression)
2358           Ascii85Flush(image);
2359        offset=TellBlob(image)-offset;
2360        (void) WriteBlobString(image,"\nendstream\n");
2361       }
2362     (void) WriteBlobString(image,"endobj\n");
2363     /*
2364       Write Length object.
2365     */
2366     xref[object++]=TellBlob(image);
2367     (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2368       object);
2369     (void) WriteBlobString(image,buffer);
2370     (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
2371       offset);
2372     (void) WriteBlobString(image,buffer);
2373     (void) WriteBlobString(image,"endobj\n");
2374     /*
2375       Write softmask object.
2376     */
2377     xref[object++]=TellBlob(image);
2378     (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2379       object);
2380     (void) WriteBlobString(image,buffer);
2381     (void) WriteBlobString(image,"<<\n");
2382     if (image->matte == MagickFalse)
2383       (void) WriteBlobString(image,">>\n");
2384     else
2385       {
2386         (void) WriteBlobString(image,"/Type /XObject\n");
2387         (void) WriteBlobString(image,"/Subtype /Image\n");
2388         (void) FormatLocaleString(buffer,MaxTextExtent,"/Name /Ma%.20g\n",
2389           (double) image->scene);
2390         (void) WriteBlobString(image,buffer);
2391         switch (compression)
2392         {
2393           case NoCompression:
2394           {
2395             (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
2396               "ASCII85Decode");
2397             break;
2398           }
2399           case LZWCompression:
2400           {
2401             (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"LZWDecode");
2402             break;
2403           }
2404           case ZipCompression:
2405           {
2406             (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
2407               "FlateDecode");
2408             break;
2409           }
2410           default:
2411           {
2412             (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
2413               "RunLengthDecode");
2414             break;
2415           }
2416         }
2417         (void) WriteBlobString(image,buffer);
2418         (void) FormatLocaleString(buffer,MaxTextExtent,"/Width %.20g\n",
2419           (double) image->columns);
2420         (void) WriteBlobString(image,buffer);
2421         (void) FormatLocaleString(buffer,MaxTextExtent,"/Height %.20g\n",
2422           (double) image->rows);
2423         (void) WriteBlobString(image,buffer);
2424         (void) WriteBlobString(image,"/ColorSpace /DeviceGray\n");
2425         (void) FormatLocaleString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
2426           (compression == FaxCompression) || (compression == Group4Compression)
2427           ? 1 : 8);
2428         (void) WriteBlobString(image,buffer);
2429         (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
2430           (double) object+1);
2431         (void) WriteBlobString(image,buffer);
2432         (void) WriteBlobString(image,">>\n");
2433         (void) WriteBlobString(image,"stream\n");
2434         offset=TellBlob(image);
2435         number_pixels=(MagickSizeType) image->columns*image->rows;
2436         switch (compression)
2437         {
2438           case RLECompression:
2439           default:
2440           {
2441             /*
2442               Allocate pixel array.
2443             */
2444             length=(size_t) number_pixels;
2445             pixels=(unsigned char *) AcquireQuantumMemory(length,
2446               sizeof(*pixels));
2447             if (pixels == (unsigned char *) NULL)
2448               {
2449                 image=DestroyImage(image);
2450                 ThrowWriterException(ResourceLimitError,
2451                   "MemoryAllocationFailed");
2452               }
2453             /*
2454               Dump Runlength encoded pixels.
2455             */
2456             q=pixels;
2457             for (y=0; y < (ssize_t) image->rows; y++)
2458             {
2459               p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2460               if (p == (const Quantum *) NULL)
2461                 break;
2462               for (x=0; x < (ssize_t) image->columns; x++)
2463               {
2464                 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
2465                 p+=GetPixelChannels(image);
2466               }
2467             }
2468 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2469             if (compression == ZipCompression)
2470               status=ZLIBEncodeImage(image,length,pixels,exception);
2471             else
2472 #endif
2473               if (compression == LZWCompression)
2474                 status=LZWEncodeImage(image,length,pixels,exception);
2475               else
2476                 status=PackbitsEncodeImage(image,length,pixels,exception);
2477             pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2478             if (status == MagickFalse)
2479               {
2480                 (void) CloseBlob(image);
2481                 return(MagickFalse);
2482               }
2483             break;
2484           }
2485           case NoCompression:
2486           {
2487             /*
2488               Dump uncompressed PseudoColor packets.
2489             */
2490             Ascii85Initialize(image);
2491             for (y=0; y < (ssize_t) image->rows; y++)
2492             {
2493               p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2494               if (p == (const Quantum *) NULL)
2495                 break;
2496               for (x=0; x < (ssize_t) image->columns; x++)
2497               {
2498                 Ascii85Encode(image,ScaleQuantumToChar(GetPixelAlpha(image,p)));
2499                 p+=GetPixelChannels(image);
2500               }
2501             }
2502             Ascii85Flush(image);
2503             break;
2504           }
2505         }
2506         offset=TellBlob(image)-offset;
2507         (void) WriteBlobString(image,"\nendstream\n");
2508       }
2509     (void) WriteBlobString(image,"endobj\n");
2510     /*
2511       Write Length object.
2512     */
2513     xref[object++]=TellBlob(image);
2514     (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2515       object);
2516     (void) WriteBlobString(image,buffer);
2517     (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
2518     (void) WriteBlobString(image,buffer);
2519     (void) WriteBlobString(image,"endobj\n");
2520     if (GetNextImageInList(image) == (Image *) NULL)
2521       break;
2522     image=SyncNextImageInList(image);
2523     status=SetImageProgress(image,SaveImagesTag,scene++,
2524       GetImageListLength(image));
2525     if (status == MagickFalse)
2526       break;
2527   } while (image_info->adjoin != MagickFalse);
2528   /*
2529     Write Metadata object.
2530   */
2531   xref[object++]=TellBlob(image);
2532   info_id=object;
2533   (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2534     object);
2535   (void) WriteBlobString(image,buffer);
2536   (void) WriteBlobString(image,"<<\n");
2537   GetPathComponent(image->filename,BasePath,basename);
2538   (void) FormatLocaleString(buffer,MaxTextExtent,"/Title (%s)\n",
2539     EscapeParenthesis(basename));
2540   (void) WriteBlobString(image,buffer);
2541   seconds=time((time_t *) NULL);
2542 #if defined(MAGICKCORE_HAVE_LOCALTIME_R)
2543   (void) localtime_r(&seconds,&local_time);
2544 #else
2545   (void) memcpy(&local_time,localtime(&seconds),sizeof(local_time));
2546 #endif
2547   (void) FormatLocaleString(date,MaxTextExtent,"D:%04d%02d%02d%02d%02d%02d",
2548     local_time.tm_year+1900,local_time.tm_mon+1,local_time.tm_mday,
2549     local_time.tm_hour,local_time.tm_min,local_time.tm_sec);
2550   (void) FormatLocaleString(buffer,MaxTextExtent,"/CreationDate (%s)\n",date);
2551   (void) WriteBlobString(image,buffer);
2552   (void) FormatLocaleString(buffer,MaxTextExtent,"/ModDate (%s)\n",date);
2553   (void) WriteBlobString(image,buffer);
2554   (void) FormatLocaleString(buffer,MaxTextExtent,"/Producer (%s)\n",
2555     EscapeParenthesis(GetMagickVersion((size_t *) NULL)));
2556   (void) WriteBlobString(image,buffer);
2557   (void) WriteBlobString(image,">>\n");
2558   (void) WriteBlobString(image,"endobj\n");
2559   /*
2560     Write Xref object.
2561   */
2562   offset=TellBlob(image)-xref[0]+10;
2563   (void) WriteBlobString(image,"xref\n");
2564   (void) FormatLocaleString(buffer,MaxTextExtent,"0 %.20g\n",(double)
2565     object+1);
2566   (void) WriteBlobString(image,buffer);
2567   (void) WriteBlobString(image,"0000000000 65535 f \n");
2568   for (i=0; i < (ssize_t) object; i++)
2569   {
2570     (void) FormatLocaleString(buffer,MaxTextExtent,"%010lu 00000 n \n",
2571       (unsigned long) xref[i]);
2572     (void) WriteBlobString(image,buffer);
2573   }
2574   (void) WriteBlobString(image,"trailer\n");
2575   (void) WriteBlobString(image,"<<\n");
2576   (void) FormatLocaleString(buffer,MaxTextExtent,"/Size %.20g\n",(double)
2577     object+1);
2578   (void) WriteBlobString(image,buffer);
2579   (void) FormatLocaleString(buffer,MaxTextExtent,"/Info %.20g 0 R\n",(double)
2580     info_id);
2581   (void) WriteBlobString(image,buffer);
2582   (void) FormatLocaleString(buffer,MaxTextExtent,"/Root %.20g 0 R\n",(double)
2583     root_id);
2584   (void) WriteBlobString(image,buffer);
2585   (void) WriteBlobString(image,">>\n");
2586   (void) WriteBlobString(image,"startxref\n");
2587   (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
2588   (void) WriteBlobString(image,buffer);
2589   (void) WriteBlobString(image,"%%EOF\n");
2590   xref=(MagickOffsetType *) RelinquishMagickMemory(xref);
2591   (void) CloseBlob(image);
2592   return(MagickTrue);
2593 }