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