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