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