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