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