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