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