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