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