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