2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Portable Document Format %
20 % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "magick/studio.h"
43 #include "magick/blob.h"
44 #include "magick/blob-private.h"
45 #include "magick/cache.h"
46 #include "magick/color.h"
47 #include "magick/color-private.h"
48 #include "magick/colorspace.h"
49 #include "magick/compress.h"
50 #include "magick/constitute.h"
51 #include "magick/delegate.h"
52 #include "magick/delegate-private.h"
53 #include "magick/draw.h"
54 #include "magick/exception.h"
55 #include "magick/exception-private.h"
56 #include "magick/geometry.h"
57 #include "magick/image.h"
58 #include "magick/image-private.h"
59 #include "magick/list.h"
60 #include "magick/magick.h"
61 #include "magick/memory_.h"
62 #include "magick/monitor.h"
63 #include "magick/monitor-private.h"
64 #include "magick/option.h"
65 #include "magick/profile.h"
66 #include "magick/property.h"
67 #include "magick/quantum-private.h"
68 #include "magick/resource_.h"
69 #include "magick/resize.h"
70 #include "magick/static.h"
71 #include "magick/string_.h"
72 #include "magick/module.h"
73 #include "magick/transform.h"
74 #include "magick/utility.h"
75 #include "magick/module.h"
80 #if defined(MAGICKCORE_TIFF_DELEGATE)
81 #define CCITTParam "-1"
83 #define CCITTParam "0"
89 static MagickBooleanType
90 WritePDFImage(const ImageInfo *,Image *);
93 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
97 % I n v o k e P D F D e l e g a t e %
101 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103 % InvokePDFDelegate() executes the PDF interpreter with the specified command.
105 % The format of the InvokePDFDelegate method is:
107 % MagickBooleanType InvokePDFDelegate(const MagickBooleanType verbose,
108 % const char *command,ExceptionInfo *exception)
110 % A description of each parameter follows:
112 % o verbose: A value other than zero displays the command prior to
115 % o command: the address of a character string containing the command to
118 % o exception: return any errors or warnings in this structure.
121 static MagickBooleanType InvokePDFDelegate(const MagickBooleanType verbose,
122 const char *command,ExceptionInfo *exception)
127 #if defined(MAGICKCORE_GS_DELEGATE) || defined(MAGICKCORE_WINDOWS_SUPPORT)
144 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
145 ghost_info=NTGhostscriptDLLVectors();
150 ghost_info=(&ghost_info_struct);
151 (void) ResetMagickMemory(&ghost_info,0,sizeof(ghost_info));
152 ghost_info_struct.new_instance=(int (*)(gs_main_instance **,void *))
154 ghost_info_struct.init_with_args=(int (*)(gs_main_instance *,int,char **))
155 gsapi_init_with_args;
156 ghost_info_struct.run_string=(int (*)(gs_main_instance *,const char *,int,
157 int *)) gsapi_run_string;
158 ghost_info_struct.delete_instance=(void (*)(gs_main_instance *))
159 gsapi_delete_instance;
160 ghost_info_struct.exit=(int (*)(gs_main_instance *)) gsapi_exit;
162 if (ghost_info == (GhostInfo *) NULL)
164 status=SystemCommand(MagickFalse,verbose,command,exception);
165 return(status == 0 ? MagickTrue : MagickFalse);
167 if (verbose != MagickFalse)
169 (void) fputs("[ghostscript library]",stdout);
170 (void) fputs(strchr(command,' '),stdout);
172 status=(ghost_info->new_instance)(&interpreter,(void *) NULL);
175 status=SystemCommand(MagickFalse,verbose,command,exception);
176 return(status == 0 ? MagickTrue : MagickFalse);
179 argv=StringToArgv(command,&argc);
180 status=(ghost_info->init_with_args)(interpreter,argc-1,argv+1);
182 status=(ghost_info->run_string)(interpreter,"systemdict /start get exec\n",
184 (ghost_info->exit)(interpreter);
185 (ghost_info->delete_instance)(interpreter);
186 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
187 NTGhostscriptUnLoadDLL();
189 for (i=0; i < (ssize_t) argc; i++)
190 argv[i]=DestroyString(argv[i]);
191 argv=(char **) RelinquishMagickMemory(argv);
192 if ((status != 0) && (status != -101))
197 message=GetExceptionMessage(errno);
198 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
199 "`%s': %s",command,message);
200 message=DestroyString(message);
201 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
202 "Ghostscript returns status %d, exit code %d",status,code);
207 status=SystemCommand(MagickFalse,verbose,command,exception);
208 return(status == 0 ? MagickTrue : MagickFalse);
213 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
221 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
223 % IsPDF() returns MagickTrue if the image format type, identified by the
224 % magick string, is PDF.
226 % The format of the IsPDF method is:
228 % MagickBooleanType IsPDF(const unsigned char *magick,const size_t offset)
230 % A description of each parameter follows:
232 % o magick: compare image format pattern against these bytes.
234 % o offset: Specifies the offset of the magick string.
237 static MagickBooleanType IsPDF(const unsigned char *magick,const size_t offset)
241 if (LocaleNCompare((const char *) magick,"%PDF-",5) == 0)
247 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251 % R e a d P D F I m a g e %
255 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
257 % ReadPDFImage() reads a Portable Document Format image file and
258 % returns it. It allocates the memory necessary for the new Image structure
259 % and returns a pointer to the new image.
261 % The format of the ReadPDFImage method is:
263 % Image *ReadPDFImage(const ImageInfo *image_info,ExceptionInfo *exception)
265 % A description of each parameter follows:
267 % o image_info: the image info.
269 % o exception: return any errors or warnings in this structure.
273 static MagickBooleanType IsPDFRendered(const char *path)
281 if ((path == (const char *) NULL) || (*path == '\0'))
283 status=GetPathAttributes(path,&attributes);
284 if ((status != MagickFalse) && S_ISREG(attributes.st_mode) &&
285 (attributes.st_size > 0))
290 static Image *ReadPDFImage(const ImageInfo *image_info,ExceptionInfo *exception)
292 #define CropBox "CropBox"
293 #define DeviceCMYK "DeviceCMYK"
294 #define MediaBox "MediaBox"
295 #define RenderPostscriptText "Rendering Postscript... "
296 #define PDFRotate "Rotate"
297 #define SpotColor "Separation"
298 #define TrimBox "TrimBox"
299 #define PDFVersion "PDF-"
302 command[MaxTextExtent],
303 density[MaxTextExtent],
304 filename[MaxTextExtent],
305 geometry[MaxTextExtent],
306 options[MaxTextExtent],
307 input_filename[MaxTextExtent],
308 postscript_filename[MaxTextExtent];
367 assert(image_info != (const ImageInfo *) NULL);
368 assert(image_info->signature == MagickSignature);
369 if (image_info->debug != MagickFalse)
370 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
371 image_info->filename);
372 assert(exception != (ExceptionInfo *) NULL);
373 assert(exception->signature == MagickSignature);
377 image=AcquireImage(image_info);
378 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
379 if (status == MagickFalse)
381 image=DestroyImageList(image);
382 return((Image *) NULL);
384 status=AcquireUniqueSymbolicLink(image_info->filename,input_filename);
385 if (status == MagickFalse)
387 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
388 image_info->filename);
389 image=DestroyImageList(image);
390 return((Image *) NULL);
393 Set the page density.
395 delta.x=DefaultResolution;
396 delta.y=DefaultResolution;
397 if ((image->x_resolution == 0.0) || (image->y_resolution == 0.0))
399 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
400 image->x_resolution=geometry_info.rho;
401 image->y_resolution=geometry_info.sigma;
402 if ((flags & SigmaValue) == 0)
403 image->y_resolution=image->x_resolution;
406 Determine page geometry from the PDF media box.
408 cmyk=image_info->colorspace == CMYKColorspace ? MagickTrue : MagickFalse;
410 option=GetImageOption(image_info,"pdf:use-cropbox");
411 if (option != (const char *) NULL)
412 cropbox=IsMagickTrue(option);
414 option=GetImageOption(image_info,"pdf:use-trimbox");
415 if (option != (const char *) NULL)
416 trimbox=IsMagickTrue(option);
419 (void) ResetMagickMemory(&bounding_box,0,sizeof(bounding_box));
420 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
421 (void) ResetMagickMemory(&hires_bounds,0,sizeof(hires_bounds));
422 (void) ResetMagickMemory(&page,0,sizeof(page));
423 (void) ResetMagickMemory(command,0,sizeof(command));
428 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
436 if ((c != (int) '/') && (c != (int) '%') &&
437 ((size_t) (p-command) < (MaxTextExtent-1)))
441 if (LocaleNCompare(PDFRotate,command,strlen(PDFRotate)) == 0)
442 count=(ssize_t) sscanf(command,"Rotate %lf",&angle);
444 Is this a CMYK document?
446 if (LocaleNCompare(DeviceCMYK,command,strlen(DeviceCMYK)) == 0)
448 if (LocaleNCompare(SpotColor,command,strlen(SpotColor)) == 0)
452 property[MaxTextExtent],
461 (void) FormatLocaleString(property,MaxTextExtent,"pdf:SpotColor-%.20g",
462 (double) spotcolor++);
464 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
466 if ((isspace(c) != 0) || (c == '/') || ((i+1) == MaxTextExtent))
471 value=AcquireString(name);
472 (void) SubstituteString(&value,"#20"," ");
473 (void) SetImageProperty(image,property,value);
474 value=DestroyString(value);
477 if (LocaleNCompare(PDFVersion,command,strlen(PDFVersion)) == 0)
478 (void) SetImageProperty(image,"pdf:Version",command);
480 if (cropbox != MagickFalse)
482 if (LocaleNCompare(CropBox,command,strlen(CropBox)) == 0)
485 Note region defined by crop box.
487 count=(ssize_t) sscanf(command,"CropBox [%lf %lf %lf %lf",
488 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
490 count=(ssize_t) sscanf(command,"CropBox[%lf %lf %lf %lf",
491 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
495 if (trimbox != MagickFalse)
497 if (LocaleNCompare(TrimBox,command,strlen(TrimBox)) == 0)
500 Note region defined by trim box.
502 count=(ssize_t) sscanf(command,"TrimBox [%lf %lf %lf %lf",
503 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
505 count=(ssize_t) sscanf(command,"TrimBox[%lf %lf %lf %lf",
506 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
510 if (LocaleNCompare(MediaBox,command,strlen(MediaBox)) == 0)
513 Note region defined by media box.
515 count=(ssize_t) sscanf(command,"MediaBox [%lf %lf %lf %lf",
516 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
518 count=(ssize_t) sscanf(command,"MediaBox[%lf %lf %lf %lf",
519 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
523 if (((bounds.x2 > hires_bounds.x2) && (bounds.y2 > hires_bounds.y2)) ||
524 ((hires_bounds.x2 == 0.0) && (hires_bounds.y2 == 0.0)))
527 Set PDF render geometry.
529 (void) FormatLocaleString(geometry,MaxTextExtent,
530 "%gx%g%+.15g%+.15g",bounds.x2-bounds.x1,bounds.y2-bounds.y1,
531 bounds.x1,bounds.y1);
532 (void) SetImageProperty(image,"pdf:HiResBoundingBox",geometry);
533 page.width=(size_t) floor(bounds.x2-bounds.x1+0.5);
534 page.height=(size_t) floor(bounds.y2-bounds.y1+0.5);
538 (void) CloseBlob(image);
539 if ((fabs(angle) == 90.0) || (fabs(angle) == 270.0))
545 page.width=page.height;
548 if (image_info->colorspace == RGBColorspace)
551 Create Ghostscript control file.
553 file=AcquireUniqueFileResource(postscript_filename);
556 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
557 image_info->filename);
558 image=DestroyImage(image);
559 return((Image *) NULL);
561 count=write(file," ",1);
564 Render Postscript with the Ghostscript delegate.
566 if ((image_info->ping != MagickFalse) ||
567 (image_info->monochrome != MagickFalse))
568 delegate_info=GetDelegateInfo("ps:mono",(char *) NULL,exception);
570 if (cmyk != MagickFalse)
571 delegate_info=GetDelegateInfo("ps:cmyk",(char *) NULL,exception);
573 delegate_info=GetDelegateInfo("ps:alpha",(char *) NULL,exception);
574 if (delegate_info == (const DelegateInfo *) NULL)
576 (void) RelinquishUniqueFileResource(postscript_filename);
577 image=DestroyImage(image);
578 return((Image *) NULL);
581 if (image_info->density != (char *) NULL)
583 flags=ParseGeometry(image_info->density,&geometry_info);
584 image->x_resolution=geometry_info.rho;
585 image->y_resolution=geometry_info.sigma;
586 if ((flags & SigmaValue) == 0)
587 image->y_resolution=image->x_resolution;
589 (void) FormatLocaleString(density,MaxTextExtent,"%gx%g",image->x_resolution,
590 image->y_resolution);
591 if (image_info->page != (char *) NULL)
593 (void) ParseAbsoluteGeometry(image_info->page,&page);
594 page.width=(size_t) floor((double) (page.width*image->x_resolution/
596 page.height=(size_t) floor((double) (page.height*image->y_resolution/
598 (void) FormatLocaleString(options,MaxTextExtent,"-g%.20gx%.20g ",(double)
599 page.width,(double) page.height);
601 if (cmyk != MagickFalse)
602 (void) ConcatenateMagickString(options,"-dUseCIEColor ",MaxTextExtent);
603 if (cropbox != MagickFalse)
604 (void) ConcatenateMagickString(options,"-dUseCropBox ",MaxTextExtent);
605 if (trimbox != MagickFalse)
606 (void) ConcatenateMagickString(options,"-dUseTrimBox ",MaxTextExtent);
607 read_info=CloneImageInfo(image_info);
608 *read_info->magick='\0';
609 if (read_info->number_scenes != 0)
612 pages[MaxTextExtent];
614 (void) FormatLocaleString(pages,MaxTextExtent,"-dFirstPage=%.20g "
615 "-dLastPage=%.20g",(double) read_info->scene+1,(double)
616 (read_info->scene+read_info->number_scenes));
617 (void) ConcatenateMagickString(options,pages,MaxTextExtent);
618 read_info->number_scenes=0;
619 if (read_info->scenes != (char *) NULL)
620 *read_info->scenes='\0';
622 if (read_info->authenticate != (char *) NULL)
623 (void) FormatLocaleString(options+strlen(options),MaxTextExtent,
624 " -sPDFPassword=%s",read_info->authenticate);
625 (void) CopyMagickString(filename,read_info->filename,MaxTextExtent);
626 (void) AcquireUniqueFilename(filename);
627 (void) ConcatenateMagickString(filename,"-%08d",MaxTextExtent);
628 (void) FormatLocaleString(command,MaxTextExtent,
629 GetDelegateCommands(delegate_info),
630 read_info->antialias != MagickFalse ? 4 : 1,
631 read_info->antialias != MagickFalse ? 4 : 1,density,options,filename,
632 postscript_filename,input_filename);
633 status=InvokePDFDelegate(read_info->verbose,command,exception);
634 (void) RelinquishUniqueFileResource(postscript_filename);
635 (void) RelinquishUniqueFileResource(input_filename);
636 pdf_image=(Image *) NULL;
637 if (status == MagickFalse)
640 (void) InterpretImageFilename(image_info,image,filename,(int) i,
641 read_info->filename);
642 if (IsPDFRendered(read_info->filename) == MagickFalse)
644 (void) RelinquishUniqueFileResource(read_info->filename);
649 (void) InterpretImageFilename(image_info,image,filename,(int) i,
650 read_info->filename);
651 if (IsPDFRendered(read_info->filename) == MagickFalse)
653 next=ReadImage(read_info,exception);
654 (void) RelinquishUniqueFileResource(read_info->filename);
655 if (next == (Image *) NULL)
657 AppendImageToList(&pdf_image,next);
659 read_info=DestroyImageInfo(read_info);
660 if (pdf_image == (Image *) NULL)
662 ThrowFileException(exception,DelegateError,"PostscriptDelegateFailed",
663 image_info->filename);
664 image=DestroyImage(image);
665 return((Image *) NULL);
667 if (LocaleCompare(pdf_image->magick,"BMP") == 0)
672 cmyk_image=ConsolidateCMYKImages(pdf_image,exception);
673 if (cmyk_image != (Image *) NULL)
675 pdf_image=DestroyImageList(pdf_image);
676 pdf_image=cmyk_image;
679 if (image_info->number_scenes != 0)
688 Add place holder images to meet the subimage specification requirement.
690 for (i=0; i < (ssize_t) image_info->scene; i++)
692 clone_image=CloneImage(pdf_image,1,1,MagickTrue,exception);
693 if (clone_image != (Image *) NULL)
694 PrependImageToList(&pdf_image,clone_image);
699 (void) CopyMagickString(pdf_image->filename,filename,MaxTextExtent);
700 pdf_image->page=page;
701 (void) CloneImageProfiles(pdf_image,image);
702 (void) CloneImageProperties(pdf_image,image);
703 next=SyncNextImageInList(pdf_image);
704 if (next != (Image *) NULL)
706 } while (next != (Image *) NULL);
707 image=DestroyImage(image);
709 for (next=GetFirstImageInList(pdf_image); next != (Image *) NULL; )
712 next=GetNextImageInList(next);
714 return(GetFirstImageInList(pdf_image));
718 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
722 % R e g i s t e r P D F I m a g e %
726 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
728 % RegisterPDFImage() adds properties for the PDF image format to
729 % the list of supported formats. The properties include the image format
730 % tag, a method to read and/or write the format, whether the format
731 % supports the saving of more than one frame to the same file or blob,
732 % whether the format supports native in-memory I/O, and a brief
733 % description of the format.
735 % The format of the RegisterPDFImage method is:
737 % size_t RegisterPDFImage(void)
740 ModuleExport size_t RegisterPDFImage(void)
745 entry=SetMagickInfo("AI");
746 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
747 entry->encoder=(EncodeImageHandler *) WritePDFImage;
748 entry->adjoin=MagickFalse;
749 entry->blob_support=MagickFalse;
750 entry->seekable_stream=MagickTrue;
751 entry->thread_support=EncoderThreadSupport;
752 entry->description=ConstantString("Adobe Illustrator CS2");
753 entry->module=ConstantString("PDF");
754 (void) RegisterMagickInfo(entry);
755 entry=SetMagickInfo("EPDF");
756 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
757 entry->encoder=(EncodeImageHandler *) WritePDFImage;
758 entry->adjoin=MagickFalse;
759 entry->blob_support=MagickFalse;
760 entry->seekable_stream=MagickTrue;
761 entry->thread_support=EncoderThreadSupport;
762 entry->description=ConstantString("Encapsulated Portable Document Format");
763 entry->module=ConstantString("PDF");
764 (void) RegisterMagickInfo(entry);
765 entry=SetMagickInfo("PDF");
766 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
767 entry->encoder=(EncodeImageHandler *) WritePDFImage;
768 entry->magick=(IsImageFormatHandler *) IsPDF;
769 entry->blob_support=MagickFalse;
770 entry->seekable_stream=MagickTrue;
771 entry->thread_support=EncoderThreadSupport;
772 entry->description=ConstantString("Portable Document Format");
773 entry->module=ConstantString("PDF");
774 (void) RegisterMagickInfo(entry);
775 entry=SetMagickInfo("PDFA");
776 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
777 entry->encoder=(EncodeImageHandler *) WritePDFImage;
778 entry->magick=(IsImageFormatHandler *) IsPDF;
779 entry->blob_support=MagickFalse;
780 entry->seekable_stream=MagickTrue;
781 entry->thread_support=EncoderThreadSupport;
782 entry->description=ConstantString("Portable Document Archive Format");
783 entry->module=ConstantString("PDF");
784 (void) RegisterMagickInfo(entry);
785 return(MagickImageCoderSignature);
789 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
793 % U n r e g i s t e r P D F I m a g e %
797 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
799 % UnregisterPDFImage() removes format registrations made by the
800 % PDF module from the list of supported formats.
802 % The format of the UnregisterPDFImage method is:
804 % UnregisterPDFImage(void)
807 ModuleExport void UnregisterPDFImage(void)
809 (void) UnregisterMagickInfo("AI");
810 (void) UnregisterMagickInfo("EPDF");
811 (void) UnregisterMagickInfo("PDF");
812 (void) UnregisterMagickInfo("PDFA");
816 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
820 % W r i t e P D F I m a g e %
824 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
826 % WritePDFImage() writes an image in the Portable Document image
829 % The format of the WritePDFImage method is:
831 % MagickBooleanType WritePDFImage(const ImageInfo *image_info,Image *image)
833 % A description of each parameter follows.
835 % o image_info: the image info.
837 % o image: The image.
841 static inline size_t MagickMax(const size_t x,const size_t y)
848 static inline size_t MagickMin(const size_t x,const size_t y)
855 static char *EscapeParenthesis(const char *text)
867 buffer[MaxTextExtent];
871 for (i=0; i < (ssize_t) MagickMin(strlen(text),(MaxTextExtent-escapes-1)); i++)
873 if ((text[i] == '(') || (text[i] == ')'))
884 static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
885 Image *image,Image *inject_image)
903 write_info=CloneImageInfo(image_info);
904 (void) CopyMagickString(write_info->filename,"GROUP4:",MaxTextExtent);
905 (void) CopyMagickString(write_info->magick,"GROUP4",MaxTextExtent);
906 group4_image=CloneImage(inject_image,0,0,MagickTrue,&image->exception);
907 if (group4_image == (Image *) NULL)
909 group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
911 group4_image=DestroyImage(group4_image);
912 if (group4 == (unsigned char *) NULL)
914 write_info=DestroyImageInfo(write_info);
915 if (WriteBlob(image,length,group4) != (ssize_t) length)
917 group4=(unsigned char *) RelinquishMagickMemory(group4);
921 static MagickBooleanType WritePDFImage(const ImageInfo *image_info,Image *image)
923 #define CFormat "/Filter [ /%s ]\n"
924 #define ObjectsPerImage 14
929 "<?xpacket begin=\"%s\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n"
930 "<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"
931 " <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n"
932 " <rdf:Description rdf:about=\"\"\n"
933 " xmlns:xap=\"http://ns.adobe.com/xap/1.0/\">\n"
934 " <xap:ModifyDate>%s</xap:ModifyDate>\n"
935 " <xap:CreateDate>%s</xap:CreateDate>\n"
936 " <xap:MetadataDate>%s</xap:MetadataDate>\n"
937 " <xap:CreatorTool>%s</xap:CreatorTool>\n"
938 " </rdf:Description>\n"
939 " <rdf:Description rdf:about=\"\"\n"
940 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"
941 " <dc:format>application/pdf</dc:format>\n"
942 " </rdf:Description>\n"
943 " <rdf:Description rdf:about=\"\"\n"
944 " xmlns:xapMM=\"http://ns.adobe.com/xap/1.0/mm/\">\n"
945 " <xapMM:DocumentID>uuid:6ec119d7-7982-4f56-808d-dfe64f5b35cf</xapMM:DocumentID>\n"
946 " <xapMM:InstanceID>uuid:a79b99b4-6235-447f-9f6c-ec18ef7555cb</xapMM:InstanceID>\n"
947 " </rdf:Description>\n"
948 " <rdf:Description rdf:about=\"\"\n"
949 " xmlns:pdf=\"http://ns.adobe.com/pdf/1.3/\">\n"
950 " <pdf:Producer>%s</pdf:Producer>\n"
951 " </rdf:Description>\n"
952 " <rdf:Description rdf:about=\"\"\n"
953 " xmlns:pdfaid=\"http://www.aiim.org/pdfa/ns/id/\">\n"
954 " <pdfaid:part>1</pdfaid:part>\n"
955 " <pdfaid:conformance>B</pdfaid:conformance>\n"
956 " </rdf:Description>\n"
959 "<?xpacket end=\"w\"?>\n"
961 XMPProfileMagick[4]= { (char) 0xef, (char) 0xbb, (char) 0xbf, (char) 0x00 };
964 basename[MaxTextExtent],
965 buffer[MaxTextExtent],
968 page_geometry[MaxTextExtent];
1010 register const IndexPacket
1013 register const PixelPacket
1016 register unsigned char
1046 Open output image file.
1048 assert(image_info != (const ImageInfo *) NULL);
1049 assert(image_info->signature == MagickSignature);
1050 assert(image != (Image *) NULL);
1051 assert(image->signature == MagickSignature);
1052 if (image->debug != MagickFalse)
1053 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1054 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1055 if (status == MagickFalse)
1058 Allocate X ref memory.
1060 xref=(MagickOffsetType *) AcquireQuantumMemory(2048UL,sizeof(*xref));
1061 if (xref == (MagickOffsetType *) NULL)
1062 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1063 (void) ResetMagickMemory(xref,0,2048UL*sizeof(*xref));
1069 if (image_info->compression == JPEG2000Compression)
1070 version=(size_t) MagickMax(version,5);
1071 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
1072 if (next->matte != MagickFalse)
1073 version=(size_t) MagickMax(version,4);
1074 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1075 version=(size_t) MagickMax(version,6);
1076 (void) FormatLocaleString(buffer,MaxTextExtent,"%%PDF-1.%.20g \n",
1078 (void) WriteBlobString(image,buffer);
1079 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1080 (void) WriteBlobString(image,"%âãÏÓ\n");
1082 Write Catalog object.
1084 xref[object++]=TellBlob(image);
1086 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1088 (void) WriteBlobString(image,buffer);
1089 (void) WriteBlobString(image,"<<\n");
1090 if (LocaleCompare(image_info->magick,"PDFA") != 0)
1091 (void) FormatLocaleString(buffer,MaxTextExtent,"/Pages %.20g 0 R\n",
1095 (void) FormatLocaleString(buffer,MaxTextExtent,"/Metadata %.20g 0 R\n",
1097 (void) WriteBlobString(image,buffer);
1098 (void) FormatLocaleString(buffer,MaxTextExtent,"/Pages %.20g 0 R\n",
1101 (void) WriteBlobString(image,buffer);
1102 (void) WriteBlobString(image,"/Type /Catalog\n");
1103 (void) WriteBlobString(image,">>\n");
1104 (void) WriteBlobString(image,"endobj\n");
1105 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1108 create_date[MaxTextExtent],
1109 modify_date[MaxTextExtent],
1110 timestamp[MaxTextExtent],
1111 xmp_profile[MaxTextExtent];
1119 xref[object++]=TellBlob(image);
1120 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",
1122 (void) WriteBlobString(image,buffer);
1123 (void) WriteBlobString(image,"<<\n");
1124 (void) WriteBlobString(image,"/Subtype /XML\n");
1126 value=GetImageProperty(image,"date:modify");
1127 if (value != (const char *) NULL)
1128 (void) CopyMagickString(modify_date,value,MaxTextExtent);
1130 value=GetImageProperty(image,"date:create");
1131 if (value != (const char *) NULL)
1132 (void) CopyMagickString(create_date,value,MaxTextExtent);
1133 (void) FormatMagickTime(time((time_t *) NULL),MaxTextExtent,timestamp);
1134 i=FormatLocaleString(xmp_profile,MaxTextExtent,XMPProfile,
1135 XMPProfileMagick,modify_date,create_date,timestamp,
1136 GetMagickVersion(&version),GetMagickVersion(&version));
1137 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g\n",
1139 (void) WriteBlobString(image,buffer);
1140 (void) WriteBlobString(image,"/Type /Metadata\n");
1141 (void) WriteBlobString(image,">>\nstream\n");
1142 (void) WriteBlobString(image,xmp_profile);
1143 (void) WriteBlobString(image,"endstream\n");
1144 (void) WriteBlobString(image,"endobj\n");
1149 xref[object++]=TellBlob(image);
1151 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1153 (void) WriteBlobString(image,buffer);
1154 (void) WriteBlobString(image,"<<\n");
1155 (void) WriteBlobString(image,"/Type /Pages\n");
1156 (void) FormatLocaleString(buffer,MaxTextExtent,"/Kids [ %.20g 0 R ",
1158 (void) WriteBlobString(image,buffer);
1159 count=(ssize_t) (pages_id+ObjectsPerImage+1);
1160 if (image_info->adjoin != MagickFalse)
1166 Predict page object id's.
1169 for ( ; GetNextImageInList(kid_image) != (Image *) NULL; count+=ObjectsPerImage)
1171 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 R ",(double)
1173 (void) WriteBlobString(image,buffer);
1174 kid_image=GetNextImageInList(kid_image);
1176 xref=(MagickOffsetType *) ResizeQuantumMemory(xref,(size_t) count+2048UL,
1178 if (xref == (MagickOffsetType *) NULL)
1179 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1181 (void) WriteBlobString(image,"]\n");
1182 (void) FormatLocaleString(buffer,MaxTextExtent,"/Count %.20g\n",
1183 (double) ((count-pages_id)/ObjectsPerImage));
1184 (void) WriteBlobString(image,buffer);
1185 (void) WriteBlobString(image,">>\n");
1186 (void) WriteBlobString(image,"endobj\n");
1190 compression=image->compression;
1191 if (image_info->compression != UndefinedCompression)
1192 compression=image_info->compression;
1193 switch (compression)
1195 case FaxCompression:
1196 case Group4Compression:
1198 if ((IsMonochromeImage(image,&image->exception) == MagickFalse) ||
1199 (image->matte != MagickFalse))
1200 compression=RLECompression;
1203 #if !defined(MAGICKCORE_JPEG_DELEGATE)
1204 case JPEGCompression:
1206 compression=RLECompression;
1207 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1208 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
1213 #if !defined(MAGICKCORE_JP2_DELEGATE)
1214 case JPEG2000Compression:
1216 compression=RLECompression;
1217 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1218 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JP2)",
1223 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
1224 case ZipCompression:
1226 compression=RLECompression;
1227 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1228 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZLIB)",
1233 case LZWCompression:
1235 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1236 compression=RLECompression; /* LZW compression is forbidden */
1241 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1242 compression=RLECompression; /* ASCII 85 compression is forbidden */
1248 if (compression == JPEG2000Compression)
1250 if (image->colorspace != RGBColorspace)
1251 (void) TransformImageColorspace(image,RGBColorspace);
1254 Scale relative to dots-per-inch.
1256 delta.x=DefaultResolution;
1257 delta.y=DefaultResolution;
1258 resolution.x=image->x_resolution;
1259 resolution.y=image->y_resolution;
1260 if ((resolution.x == 0.0) || (resolution.y == 0.0))
1262 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
1263 resolution.x=geometry_info.rho;
1264 resolution.y=geometry_info.sigma;
1265 if ((flags & SigmaValue) == 0)
1266 resolution.y=resolution.x;
1268 if (image_info->density != (char *) NULL)
1270 flags=ParseGeometry(image_info->density,&geometry_info);
1271 resolution.x=geometry_info.rho;
1272 resolution.y=geometry_info.sigma;
1273 if ((flags & SigmaValue) == 0)
1274 resolution.y=resolution.x;
1276 if (image->units == PixelsPerCentimeterResolution)
1278 resolution.x=(double) ((size_t) (100.0*2.54*resolution.x+0.5)/100.0);
1279 resolution.y=(double) ((size_t) (100.0*2.54*resolution.y+0.5)/100.0);
1281 SetGeometry(image,&geometry);
1282 (void) FormatLocaleString(page_geometry,MaxTextExtent,"%.20gx%.20g",
1283 (double) image->columns,(double) image->rows);
1284 if (image_info->page != (char *) NULL)
1285 (void) CopyMagickString(page_geometry,image_info->page,MaxTextExtent);
1287 if ((image->page.width != 0) && (image->page.height != 0))
1288 (void) FormatLocaleString(page_geometry,MaxTextExtent,
1289 "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,
1290 (double) image->page.height,(double) image->page.x,(double)
1293 if ((image->gravity != UndefinedGravity) &&
1294 (LocaleCompare(image_info->magick,"PDF") == 0))
1295 (void) CopyMagickString(page_geometry,PSPageGeometry,MaxTextExtent);
1296 (void) ConcatenateMagickString(page_geometry,">",MaxTextExtent);
1297 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
1298 &geometry.width,&geometry.height);
1299 scale.x=(double) (geometry.width*delta.x)/resolution.x;
1300 geometry.width=(size_t) floor(scale.x+0.5);
1301 scale.y=(double) (geometry.height*delta.y)/resolution.y;
1302 geometry.height=(size_t) floor(scale.y+0.5);
1303 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
1304 (void) ParseGravityGeometry(image,page_geometry,&page_info,
1306 if (image->gravity != UndefinedGravity)
1308 geometry.x=(-page_info.x);
1309 geometry.y=(ssize_t) (media_info.height+page_info.y-image->rows);
1312 if (image_info->pointsize != 0.0)
1313 pointsize=image_info->pointsize;
1315 value=GetImageProperty(image,"label");
1316 if (value != (const char *) NULL)
1317 text_size=(size_t) (MultilineCensus(value)*pointsize+12);
1322 xref[object++]=TellBlob(image);
1323 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1325 (void) WriteBlobString(image,buffer);
1326 (void) WriteBlobString(image,"<<\n");
1327 (void) WriteBlobString(image,"/Type /Page\n");
1328 (void) FormatLocaleString(buffer,MaxTextExtent,"/Parent %.20g 0 R\n",
1330 (void) WriteBlobString(image,buffer);
1331 (void) WriteBlobString(image,"/Resources <<\n");
1332 labels=(char **) NULL;
1333 value=GetImageProperty(image,"label");
1334 if (value != (const char *) NULL)
1335 labels=StringToList(value);
1336 if (labels != (char **) NULL)
1338 (void) FormatLocaleString(buffer,MaxTextExtent,
1339 "/Font << /F%.20g %.20g 0 R >>\n",(double) image->scene,(double)
1341 (void) WriteBlobString(image,buffer);
1343 (void) FormatLocaleString(buffer,MaxTextExtent,
1344 "/XObject << /Im%.20g %.20g 0 R >>\n",(double) image->scene,(double)
1346 (void) WriteBlobString(image,buffer);
1347 (void) FormatLocaleString(buffer,MaxTextExtent,"/ProcSet %.20g 0 R >>\n",
1349 (void) WriteBlobString(image,buffer);
1350 (void) FormatLocaleString(buffer,MaxTextExtent,
1351 "/MediaBox [0 0 %g %g]\n",72.0*media_info.width/resolution.x,
1352 72.0*media_info.height/resolution.y);
1353 (void) WriteBlobString(image,buffer);
1354 (void) FormatLocaleString(buffer,MaxTextExtent,
1355 "/CropBox [0 0 %g %g]\n",72.0*media_info.width/resolution.x,
1356 72.0*media_info.height/resolution.y);
1357 (void) WriteBlobString(image,buffer);
1358 (void) FormatLocaleString(buffer,MaxTextExtent,"/Contents %.20g 0 R\n",
1360 (void) WriteBlobString(image,buffer);
1361 (void) FormatLocaleString(buffer,MaxTextExtent,"/Thumb %.20g 0 R\n",
1363 (void) WriteBlobString(image,buffer);
1364 (void) WriteBlobString(image,">>\n");
1365 (void) WriteBlobString(image,"endobj\n");
1367 Write Contents object.
1369 xref[object++]=TellBlob(image);
1370 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1372 (void) WriteBlobString(image,buffer);
1373 (void) WriteBlobString(image,"<<\n");
1374 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
1376 (void) WriteBlobString(image,buffer);
1377 (void) WriteBlobString(image,">>\n");
1378 (void) WriteBlobString(image,"stream\n");
1379 offset=TellBlob(image);
1380 (void) WriteBlobString(image,"q\n");
1381 if (labels != (char **) NULL)
1382 for (i=0; labels[i] != (char *) NULL; i++)
1384 (void) WriteBlobString(image,"BT\n");
1385 (void) FormatLocaleString(buffer,MaxTextExtent,"/F%.20g %g Tf\n",
1386 (double) image->scene,pointsize);
1387 (void) WriteBlobString(image,buffer);
1388 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g Td\n",
1389 (double) geometry.x,(double) (geometry.y+geometry.height+i*pointsize+
1391 (void) WriteBlobString(image,buffer);
1392 (void) FormatLocaleString(buffer,MaxTextExtent,"(%s) Tj\n",labels[i]);
1393 (void) WriteBlobString(image,buffer);
1394 (void) WriteBlobString(image,"ET\n");
1395 labels[i]=DestroyString(labels[i]);
1397 (void) FormatLocaleString(buffer,MaxTextExtent,"%g 0 0 %g %.20g %.20g cm\n",
1398 scale.x,scale.y,(double) geometry.x,(double) geometry.y);
1399 (void) WriteBlobString(image,buffer);
1400 (void) FormatLocaleString(buffer,MaxTextExtent,"/Im%.20g Do\n",
1401 (double) image->scene);
1402 (void) WriteBlobString(image,buffer);
1403 (void) WriteBlobString(image,"Q\n");
1404 offset=TellBlob(image)-offset;
1405 (void) WriteBlobString(image,"endstream\n");
1406 (void) WriteBlobString(image,"endobj\n");
1408 Write Length object.
1410 xref[object++]=TellBlob(image);
1411 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1413 (void) WriteBlobString(image,buffer);
1414 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
1415 (void) WriteBlobString(image,buffer);
1416 (void) WriteBlobString(image,"endobj\n");
1418 Write Procset object.
1420 xref[object++]=TellBlob(image);
1421 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",
1423 (void) WriteBlobString(image,buffer);
1424 if ((image->storage_class == DirectClass) || (image->colors > 256))
1425 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageC",MaxTextExtent);
1427 if ((compression == FaxCompression) || (compression == Group4Compression))
1428 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageB",MaxTextExtent);
1430 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageI",MaxTextExtent);
1431 (void) WriteBlobString(image,buffer);
1432 (void) WriteBlobString(image," ]\n");
1433 (void) WriteBlobString(image,"endobj\n");
1437 xref[object++]=TellBlob(image);
1438 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",
1440 (void) WriteBlobString(image,buffer);
1441 (void) WriteBlobString(image,"<<\n");
1442 if (labels != (char **) NULL)
1444 (void) WriteBlobString(image,"/Type /Font\n");
1445 (void) WriteBlobString(image,"/Subtype /Type1\n");
1446 (void) FormatLocaleString(buffer,MaxTextExtent,"/Name /F%.20g\n",
1447 (double) image->scene);
1448 (void) WriteBlobString(image,buffer);
1449 (void) WriteBlobString(image,"/BaseFont /Helvetica\n");
1450 (void) WriteBlobString(image,"/Encoding /MacRomanEncoding\n");
1451 labels=(char **) RelinquishMagickMemory(labels);
1453 (void) WriteBlobString(image,">>\n");
1454 (void) WriteBlobString(image,"endobj\n");
1456 Write XObject object.
1458 xref[object++]=TellBlob(image);
1459 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1461 (void) WriteBlobString(image,buffer);
1462 (void) WriteBlobString(image,"<<\n");
1463 (void) WriteBlobString(image,"/Type /XObject\n");
1464 (void) WriteBlobString(image,"/Subtype /Image\n");
1465 (void) FormatLocaleString(buffer,MaxTextExtent,"/Name /Im%.20g\n",
1466 (double) image->scene);
1467 (void) WriteBlobString(image,buffer);
1468 switch (compression)
1472 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"ASCII85Decode");
1475 case JPEGCompression:
1477 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"DCTDecode");
1478 if (image->colorspace != CMYKColorspace)
1480 (void) WriteBlobString(image,buffer);
1481 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1485 case JPEG2000Compression:
1487 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"JPXDecode");
1488 if (image->colorspace != CMYKColorspace)
1490 (void) WriteBlobString(image,buffer);
1491 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1495 case LZWCompression:
1497 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"LZWDecode");
1500 case ZipCompression:
1502 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"FlateDecode");
1505 case FaxCompression:
1506 case Group4Compression:
1508 (void) CopyMagickString(buffer,"/Filter [ /CCITTFaxDecode ]\n",
1510 (void) WriteBlobString(image,buffer);
1511 (void) FormatLocaleString(buffer,MaxTextExtent,"/DecodeParms [ << "
1512 "/K %s /BlackIs1 false /Columns %.20g /Rows %.20g >> ]\n",CCITTParam,
1513 (double) image->columns,(double) image->rows);
1518 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
1523 (void) WriteBlobString(image,buffer);
1524 (void) FormatLocaleString(buffer,MaxTextExtent,"/Width %.20g\n",(double)
1526 (void) WriteBlobString(image,buffer);
1527 (void) FormatLocaleString(buffer,MaxTextExtent,"/Height %.20g\n",(double)
1529 (void) WriteBlobString(image,buffer);
1530 (void) FormatLocaleString(buffer,MaxTextExtent,"/ColorSpace %.20g 0 R\n",
1532 (void) WriteBlobString(image,buffer);
1533 (void) FormatLocaleString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
1534 (compression == FaxCompression) || (compression == Group4Compression) ?
1536 (void) WriteBlobString(image,buffer);
1537 if (image->matte != MagickFalse)
1539 (void) FormatLocaleString(buffer,MaxTextExtent,"/SMask %.20g 0 R\n",
1541 (void) WriteBlobString(image,buffer);
1543 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
1545 (void) WriteBlobString(image,buffer);
1546 (void) WriteBlobString(image,">>\n");
1547 (void) WriteBlobString(image,"stream\n");
1548 offset=TellBlob(image);
1549 number_pixels=(MagickSizeType) image->columns*image->rows;
1550 if ((4*number_pixels) != (MagickSizeType) ((size_t) (4*number_pixels)))
1551 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1552 if ((compression == FaxCompression) || (compression == Group4Compression) ||
1553 ((image_info->type != TrueColorType) &&
1554 (IsGrayImage(image,&image->exception) != MagickFalse)))
1556 switch (compression)
1558 case FaxCompression:
1559 case Group4Compression:
1561 if (LocaleCompare(CCITTParam,"0") == 0)
1563 (void) HuffmanEncodeImage(image_info,image,image);
1566 (void) Huffman2DEncodeImage(image_info,image,image);
1569 case JPEGCompression:
1571 status=InjectImageBlob(image_info,image,image,"jpeg",
1573 if (status == MagickFalse)
1574 ThrowWriterException(CoderError,image->exception.reason);
1577 case JPEG2000Compression:
1579 status=InjectImageBlob(image_info,image,image,"jp2",
1581 if (status == MagickFalse)
1582 ThrowWriterException(CoderError,image->exception.reason);
1585 case RLECompression:
1589 Allocate pixel array.
1591 length=(size_t) number_pixels;
1592 pixels=(unsigned char *) AcquireQuantumMemory(length,
1594 if (pixels == (unsigned char *) NULL)
1595 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1597 Dump Runlength encoded pixels.
1600 for (y=0; y < (ssize_t) image->rows; y++)
1602 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1603 if (p == (const PixelPacket *) NULL)
1605 for (x=0; x < (ssize_t) image->columns; x++)
1607 *q++=ScaleQuantumToChar(PixelIntensityToQuantum(p));
1610 if (image->previous == (Image *) NULL)
1612 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1614 if (status == MagickFalse)
1618 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1619 if (compression == ZipCompression)
1620 status=ZLIBEncodeImage(image,length,pixels);
1623 if (compression == LZWCompression)
1624 status=LZWEncodeImage(image,length,pixels);
1626 status=PackbitsEncodeImage(image,length,pixels);
1627 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1628 if (status == MagickFalse)
1630 (void) CloseBlob(image);
1631 return(MagickFalse);
1638 Dump uncompressed PseudoColor packets.
1640 Ascii85Initialize(image);
1641 for (y=0; y < (ssize_t) image->rows; y++)
1643 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1644 if (p == (const PixelPacket *) NULL)
1646 for (x=0; x < (ssize_t) image->columns; x++)
1648 Ascii85Encode(image,
1649 ScaleQuantumToChar(PixelIntensityToQuantum(p)));
1652 if (image->previous == (Image *) NULL)
1654 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1656 if (status == MagickFalse)
1660 Ascii85Flush(image);
1666 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1667 (compression == JPEGCompression) ||
1668 (compression == JPEG2000Compression))
1669 switch (compression)
1671 case JPEGCompression:
1673 status=InjectImageBlob(image_info,image,image,"jpeg",
1675 if (status == MagickFalse)
1676 ThrowWriterException(CoderError,image->exception.reason);
1679 case JPEG2000Compression:
1681 status=InjectImageBlob(image_info,image,image,"jp2",
1683 if (status == MagickFalse)
1684 ThrowWriterException(CoderError,image->exception.reason);
1687 case RLECompression:
1691 Allocate pixel array.
1693 length=(size_t) number_pixels;
1694 pixels=(unsigned char *) AcquireQuantumMemory(length,
1696 length*=image->colorspace == CMYKColorspace ? 4UL : 3UL;
1697 if (pixels == (unsigned char *) NULL)
1698 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1700 Dump runoffset encoded pixels.
1703 for (y=0; y < (ssize_t) image->rows; y++)
1705 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1706 if (p == (const PixelPacket *) NULL)
1708 indexes=GetVirtualIndexQueue(image);
1709 for (x=0; x < (ssize_t) image->columns; x++)
1711 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
1712 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1713 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
1714 if (image->colorspace == CMYKColorspace)
1715 *q++=ScaleQuantumToChar(GetIndexPixelComponent(indexes+x));
1718 if (image->previous == (Image *) NULL)
1720 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1722 if (status == MagickFalse)
1726 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1727 if (compression == ZipCompression)
1728 status=ZLIBEncodeImage(image,length,pixels);
1731 if (compression == LZWCompression)
1732 status=LZWEncodeImage(image,length,pixels);
1734 status=PackbitsEncodeImage(image,length,pixels);
1735 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1736 if (status == MagickFalse)
1738 (void) CloseBlob(image);
1739 return(MagickFalse);
1746 Dump uncompressed DirectColor packets.
1748 Ascii85Initialize(image);
1749 for (y=0; y < (ssize_t) image->rows; y++)
1751 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1752 if (p == (const PixelPacket *) NULL)
1754 indexes=GetVirtualIndexQueue(image);
1755 for (x=0; x < (ssize_t) image->columns; x++)
1757 Ascii85Encode(image,ScaleQuantumToChar(
1758 GetRedPixelComponent(p)));
1759 Ascii85Encode(image,ScaleQuantumToChar(
1760 GetGreenPixelComponent(p)));
1761 Ascii85Encode(image,ScaleQuantumToChar(
1762 GetBluePixelComponent(p)));
1763 if (image->colorspace == CMYKColorspace)
1764 Ascii85Encode(image,ScaleQuantumToChar(
1765 GetIndexPixelComponent(indexes+x)));
1768 if (image->previous == (Image *) NULL)
1770 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1772 if (status == MagickFalse)
1776 Ascii85Flush(image);
1783 Dump number of colors and colormap.
1785 switch (compression)
1787 case RLECompression:
1791 Allocate pixel array.
1793 length=(size_t) number_pixels;
1794 pixels=(unsigned char *) AcquireQuantumMemory(length,
1796 if (pixels == (unsigned char *) NULL)
1797 ThrowWriterException(ResourceLimitError,
1798 "MemoryAllocationFailed");
1800 Dump Runlength encoded pixels.
1803 for (y=0; y < (ssize_t) image->rows; y++)
1805 p=GetVirtualPixels(image,0,y,image->columns,1,
1807 if (p == (const PixelPacket *) NULL)
1809 indexes=GetVirtualIndexQueue(image);
1810 for (x=0; x < (ssize_t) image->columns; x++)
1811 *q++=(unsigned char) GetIndexPixelComponent(indexes+x);
1812 if (image->previous == (Image *) NULL)
1814 status=SetImageProgress(image,SaveImageTag,
1815 (MagickOffsetType) y,image->rows);
1816 if (status == MagickFalse)
1820 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1821 if (compression == ZipCompression)
1822 status=ZLIBEncodeImage(image,length,pixels);
1825 if (compression == LZWCompression)
1826 status=LZWEncodeImage(image,length,pixels);
1828 status=PackbitsEncodeImage(image,length,pixels);
1829 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1830 if (status == MagickFalse)
1832 (void) CloseBlob(image);
1833 return(MagickFalse);
1840 Dump uncompressed PseudoColor packets.
1842 Ascii85Initialize(image);
1843 for (y=0; y < (ssize_t) image->rows; y++)
1845 p=GetVirtualPixels(image,0,y,image->columns,1,
1847 if (p == (const PixelPacket *) NULL)
1849 indexes=GetVirtualIndexQueue(image);
1850 for (x=0; x < (ssize_t) image->columns; x++)
1851 Ascii85Encode(image,(unsigned char)
1852 GetIndexPixelComponent(indexes+x));
1853 if (image->previous == (Image *) NULL)
1855 status=SetImageProgress(image,SaveImageTag,
1856 (MagickOffsetType) y,image->rows);
1857 if (status == MagickFalse)
1861 Ascii85Flush(image);
1866 offset=TellBlob(image)-offset;
1867 (void) WriteBlobString(image,"\nendstream\n");
1868 (void) WriteBlobString(image,"endobj\n");
1870 Write Length object.
1872 xref[object++]=TellBlob(image);
1873 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1875 (void) WriteBlobString(image,buffer);
1876 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",
1878 (void) WriteBlobString(image,buffer);
1879 (void) WriteBlobString(image,"endobj\n");
1881 Write Colorspace object.
1883 xref[object++]=TellBlob(image);
1884 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1886 (void) WriteBlobString(image,buffer);
1887 if (image->colorspace == CMYKColorspace)
1888 (void) CopyMagickString(buffer,"/DeviceCMYK\n",MaxTextExtent);
1890 if ((compression == FaxCompression) ||
1891 (compression == Group4Compression) ||
1892 ((image_info->type != TrueColorType) &&
1893 (IsGrayImage(image,&image->exception) != MagickFalse)))
1894 (void) CopyMagickString(buffer,"/DeviceGray\n",MaxTextExtent);
1896 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1897 (compression == JPEGCompression) ||
1898 (compression == JPEG2000Compression))
1899 (void) CopyMagickString(buffer,"/DeviceRGB\n",MaxTextExtent);
1901 (void) FormatLocaleString(buffer,MaxTextExtent,
1902 "[ /Indexed /DeviceRGB %.20g %.20g 0 R ]\n",(double) image->colors-
1903 1,(double) object+3);
1904 (void) WriteBlobString(image,buffer);
1905 (void) WriteBlobString(image,"endobj\n");
1909 SetGeometry(image,&geometry);
1910 (void) ParseMetaGeometry("106x106+0+0>",&geometry.x,&geometry.y,
1911 &geometry.width,&geometry.height);
1912 tile_image=ThumbnailImage(image,geometry.width,geometry.height,
1914 if (tile_image == (Image *) NULL)
1915 ThrowWriterException(ResourceLimitError,image->exception.reason);
1916 xref[object++]=TellBlob(image);
1917 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1919 (void) WriteBlobString(image,buffer);
1920 (void) WriteBlobString(image,"<<\n");
1921 switch (compression)
1925 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"ASCII85Decode");
1928 case JPEGCompression:
1930 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"DCTDecode");
1931 if (image->colorspace != CMYKColorspace)
1933 (void) WriteBlobString(image,buffer);
1934 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1938 case JPEG2000Compression:
1940 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"JPXDecode");
1941 if (image->colorspace != CMYKColorspace)
1943 (void) WriteBlobString(image,buffer);
1944 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1948 case LZWCompression:
1950 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"LZWDecode");
1953 case ZipCompression:
1955 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"FlateDecode");
1958 case FaxCompression:
1959 case Group4Compression:
1961 (void) CopyMagickString(buffer,"/Filter [ /CCITTFaxDecode ]\n",
1963 (void) WriteBlobString(image,buffer);
1964 (void) FormatLocaleString(buffer,MaxTextExtent,"/DecodeParms [ << "
1965 "/K %s /BlackIs1 false /Columns %.20g /Rows %.20g >> ]\n",CCITTParam,
1966 (double) tile_image->columns,(double) tile_image->rows);
1971 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
1976 (void) WriteBlobString(image,buffer);
1977 (void) FormatLocaleString(buffer,MaxTextExtent,"/Width %.20g\n",(double)
1978 tile_image->columns);
1979 (void) WriteBlobString(image,buffer);
1980 (void) FormatLocaleString(buffer,MaxTextExtent,"/Height %.20g\n",(double)
1982 (void) WriteBlobString(image,buffer);
1983 (void) FormatLocaleString(buffer,MaxTextExtent,"/ColorSpace %.20g 0 R\n",
1985 (void) WriteBlobString(image,buffer);
1986 (void) FormatLocaleString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
1987 (compression == FaxCompression) || (compression == Group4Compression) ?
1989 (void) WriteBlobString(image,buffer);
1990 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
1992 (void) WriteBlobString(image,buffer);
1993 (void) WriteBlobString(image,">>\n");
1994 (void) WriteBlobString(image,"stream\n");
1995 offset=TellBlob(image);
1996 number_pixels=(MagickSizeType) tile_image->columns*tile_image->rows;
1997 if ((compression == FaxCompression) ||
1998 (compression == Group4Compression) ||
1999 ((image_info->type != TrueColorType) &&
2000 (IsGrayImage(tile_image,&image->exception) != MagickFalse)))
2002 switch (compression)
2004 case FaxCompression:
2005 case Group4Compression:
2007 if (LocaleCompare(CCITTParam,"0") == 0)
2009 (void) HuffmanEncodeImage(image_info,image,tile_image);
2012 (void) Huffman2DEncodeImage(image_info,image,tile_image);
2015 case JPEGCompression:
2017 status=InjectImageBlob(image_info,image,tile_image,"jpeg",
2019 if (status == MagickFalse)
2020 ThrowWriterException(CoderError,tile_image->exception.reason);
2023 case JPEG2000Compression:
2025 status=InjectImageBlob(image_info,image,tile_image,"jp2",
2027 if (status == MagickFalse)
2028 ThrowWriterException(CoderError,tile_image->exception.reason);
2031 case RLECompression:
2035 Allocate pixel array.
2037 length=(size_t) number_pixels;
2038 pixels=(unsigned char *) AcquireQuantumMemory(length,
2040 if (pixels == (unsigned char *) NULL)
2042 tile_image=DestroyImage(tile_image);
2043 ThrowWriterException(ResourceLimitError,
2044 "MemoryAllocationFailed");
2047 Dump Runlength encoded pixels.
2050 for (y=0; y < (ssize_t) tile_image->rows; y++)
2052 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2053 &tile_image->exception);
2054 if (p == (const PixelPacket *) NULL)
2056 for (x=0; x < (ssize_t) tile_image->columns; x++)
2058 *q++=ScaleQuantumToChar(PixelIntensityToQuantum(p));
2062 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2063 if (compression == ZipCompression)
2064 status=ZLIBEncodeImage(image,length,pixels);
2067 if (compression == LZWCompression)
2068 status=LZWEncodeImage(image,length,pixels);
2070 status=PackbitsEncodeImage(image,length,pixels);
2071 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2072 if (status == MagickFalse)
2074 (void) CloseBlob(image);
2075 return(MagickFalse);
2082 Dump uncompressed PseudoColor packets.
2084 Ascii85Initialize(image);
2085 for (y=0; y < (ssize_t) tile_image->rows; y++)
2087 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2088 &tile_image->exception);
2089 if (p == (const PixelPacket *) NULL)
2091 for (x=0; x < (ssize_t) tile_image->columns; x++)
2093 Ascii85Encode(image,
2094 ScaleQuantumToChar(PixelIntensityToQuantum(p)));
2098 Ascii85Flush(image);
2104 if ((tile_image->storage_class == DirectClass) ||
2105 (tile_image->colors > 256) || (compression == JPEGCompression) ||
2106 (compression == JPEG2000Compression))
2107 switch (compression)
2109 case JPEGCompression:
2111 status=InjectImageBlob(image_info,image,tile_image,"jpeg",
2113 if (status == MagickFalse)
2114 ThrowWriterException(CoderError,tile_image->exception.reason);
2117 case JPEG2000Compression:
2119 status=InjectImageBlob(image_info,image,tile_image,"jp2",
2121 if (status == MagickFalse)
2122 ThrowWriterException(CoderError,tile_image->exception.reason);
2125 case RLECompression:
2129 Allocate pixel array.
2131 length=(size_t) number_pixels;
2132 pixels=(unsigned char *) AcquireQuantumMemory(length,4*
2134 length*=tile_image->colorspace == CMYKColorspace ? 4UL : 3UL;
2135 if (pixels == (unsigned char *) NULL)
2137 tile_image=DestroyImage(tile_image);
2138 ThrowWriterException(ResourceLimitError,
2139 "MemoryAllocationFailed");
2142 Dump runoffset encoded pixels.
2145 for (y=0; y < (ssize_t) tile_image->rows; y++)
2147 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2148 &tile_image->exception);
2149 if (p == (const PixelPacket *) NULL)
2151 indexes=GetVirtualIndexQueue(tile_image);
2152 for (x=0; x < (ssize_t) tile_image->columns; x++)
2154 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
2155 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
2156 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
2157 if (image->colorspace == CMYKColorspace)
2158 *q++=ScaleQuantumToChar(GetIndexPixelComponent(indexes+x));
2162 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2163 if (compression == ZipCompression)
2164 status=ZLIBEncodeImage(image,length,pixels);
2167 if (compression == LZWCompression)
2168 status=LZWEncodeImage(image,length,pixels);
2170 status=PackbitsEncodeImage(image,length,pixels);
2171 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2172 if (status == MagickFalse)
2174 (void) CloseBlob(image);
2175 return(MagickFalse);
2182 Dump uncompressed DirectColor packets.
2184 Ascii85Initialize(image);
2185 for (y=0; y < (ssize_t) tile_image->rows; y++)
2187 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2188 &tile_image->exception);
2189 if (p == (const PixelPacket *) NULL)
2191 indexes=GetVirtualIndexQueue(tile_image);
2192 for (x=0; x < (ssize_t) tile_image->columns; x++)
2194 Ascii85Encode(image,ScaleQuantumToChar(
2195 GetRedPixelComponent(p)));
2196 Ascii85Encode(image,ScaleQuantumToChar(
2197 GetGreenPixelComponent(p)));
2198 Ascii85Encode(image,ScaleQuantumToChar(
2199 GetBluePixelComponent(p)));
2200 if (image->colorspace == CMYKColorspace)
2201 Ascii85Encode(image,ScaleQuantumToChar(
2202 GetIndexPixelComponent(indexes+x)));
2206 Ascii85Flush(image);
2213 Dump number of colors and colormap.
2215 switch (compression)
2217 case RLECompression:
2221 Allocate pixel array.
2223 length=(size_t) number_pixels;
2224 pixels=(unsigned char *) AcquireQuantumMemory(length,
2226 if (pixels == (unsigned char *) NULL)
2228 tile_image=DestroyImage(tile_image);
2229 ThrowWriterException(ResourceLimitError,
2230 "MemoryAllocationFailed");
2233 Dump Runlength encoded pixels.
2236 for (y=0; y < (ssize_t) tile_image->rows; y++)
2238 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2239 &tile_image->exception);
2240 if (p == (const PixelPacket *) NULL)
2242 indexes=GetVirtualIndexQueue(tile_image);
2243 for (x=0; x < (ssize_t) tile_image->columns; x++)
2244 *q++=(unsigned char) GetIndexPixelComponent(indexes+x);
2246 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2247 if (compression == ZipCompression)
2248 status=ZLIBEncodeImage(image,length,pixels);
2251 if (compression == LZWCompression)
2252 status=LZWEncodeImage(image,length,pixels);
2254 status=PackbitsEncodeImage(image,length,pixels);
2255 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2256 if (status == MagickFalse)
2258 (void) CloseBlob(image);
2259 return(MagickFalse);
2266 Dump uncompressed PseudoColor packets.
2268 Ascii85Initialize(image);
2269 for (y=0; y < (ssize_t) tile_image->rows; y++)
2271 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2272 &tile_image->exception);
2273 if (p == (const PixelPacket *) NULL)
2275 indexes=GetVirtualIndexQueue(tile_image);
2276 for (x=0; x < (ssize_t) tile_image->columns; x++)
2277 Ascii85Encode(image,(unsigned char)
2278 GetIndexPixelComponent(indexes+x));
2280 Ascii85Flush(image);
2285 tile_image=DestroyImage(tile_image);
2286 offset=TellBlob(image)-offset;
2287 (void) WriteBlobString(image,"\nendstream\n");
2288 (void) WriteBlobString(image,"endobj\n");
2290 Write Length object.
2292 xref[object++]=TellBlob(image);
2293 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2295 (void) WriteBlobString(image,buffer);
2296 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
2297 (void) WriteBlobString(image,buffer);
2298 (void) WriteBlobString(image,"endobj\n");
2299 xref[object++]=TellBlob(image);
2300 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2302 (void) WriteBlobString(image,buffer);
2303 if ((image->storage_class != DirectClass) && (image->colors <= 256) &&
2304 (compression != FaxCompression) && (compression != Group4Compression))
2307 Write Colormap object.
2309 (void) WriteBlobString(image,"<<\n");
2310 if (compression == NoCompression)
2311 (void) WriteBlobString(image,"/Filter [ /ASCII85Decode ]\n");
2312 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
2314 (void) WriteBlobString(image,buffer);
2315 (void) WriteBlobString(image,">>\n");
2316 (void) WriteBlobString(image,"stream\n");
2317 offset=TellBlob(image);
2318 if (compression == NoCompression)
2319 Ascii85Initialize(image);
2320 for (i=0; i < (ssize_t) image->colors; i++)
2322 if (compression == NoCompression)
2324 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].red));
2325 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].green));
2326 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].blue));
2329 (void) WriteBlobByte(image,
2330 ScaleQuantumToChar(image->colormap[i].red));
2331 (void) WriteBlobByte(image,
2332 ScaleQuantumToChar(image->colormap[i].green));
2333 (void) WriteBlobByte(image,
2334 ScaleQuantumToChar(image->colormap[i].blue));
2336 if (compression == NoCompression)
2337 Ascii85Flush(image);
2338 offset=TellBlob(image)-offset;
2339 (void) WriteBlobString(image,"\nendstream\n");
2341 (void) WriteBlobString(image,"endobj\n");
2343 Write Length object.
2345 xref[object++]=TellBlob(image);
2346 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2348 (void) WriteBlobString(image,buffer);
2349 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
2351 (void) WriteBlobString(image,buffer);
2352 (void) WriteBlobString(image,"endobj\n");
2354 Write softmask object.
2356 xref[object++]=TellBlob(image);
2357 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2359 (void) WriteBlobString(image,buffer);
2360 (void) WriteBlobString(image,"<<\n");
2361 if (image->matte == MagickFalse)
2362 (void) WriteBlobString(image,">>\n");
2365 (void) WriteBlobString(image,"/Type /XObject\n");
2366 (void) WriteBlobString(image,"/Subtype /Image\n");
2367 (void) FormatLocaleString(buffer,MaxTextExtent,"/Name /Ma%.20g\n",
2368 (double) image->scene);
2369 (void) WriteBlobString(image,buffer);
2370 switch (compression)
2374 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
2378 case LZWCompression:
2380 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"LZWDecode");
2383 case ZipCompression:
2385 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
2391 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
2396 (void) WriteBlobString(image,buffer);
2397 (void) FormatLocaleString(buffer,MaxTextExtent,"/Width %.20g\n",
2398 (double) image->columns);
2399 (void) WriteBlobString(image,buffer);
2400 (void) FormatLocaleString(buffer,MaxTextExtent,"/Height %.20g\n",
2401 (double) image->rows);
2402 (void) WriteBlobString(image,buffer);
2403 (void) WriteBlobString(image,"/ColorSpace /DeviceGray\n");
2404 (void) FormatLocaleString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
2405 (compression == FaxCompression) || (compression == Group4Compression)
2407 (void) WriteBlobString(image,buffer);
2408 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
2410 (void) WriteBlobString(image,buffer);
2411 (void) WriteBlobString(image,">>\n");
2412 (void) WriteBlobString(image,"stream\n");
2413 offset=TellBlob(image);
2414 number_pixels=(MagickSizeType) image->columns*image->rows;
2415 switch (compression)
2417 case RLECompression:
2421 Allocate pixel array.
2423 length=(size_t) number_pixels;
2424 pixels=(unsigned char *) AcquireQuantumMemory(length,
2426 if (pixels == (unsigned char *) NULL)
2428 image=DestroyImage(image);
2429 ThrowWriterException(ResourceLimitError,
2430 "MemoryAllocationFailed");
2433 Dump Runlength encoded pixels.
2436 for (y=0; y < (ssize_t) image->rows; y++)
2438 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
2439 if (p == (const PixelPacket *) NULL)
2441 for (x=0; x < (ssize_t) image->columns; x++)
2443 *q++=ScaleQuantumToChar((Quantum) (GetAlphaPixelComponent(p)));
2447 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2448 if (compression == ZipCompression)
2449 status=ZLIBEncodeImage(image,length,pixels);
2452 if (compression == LZWCompression)
2453 status=LZWEncodeImage(image,length,pixels);
2455 status=PackbitsEncodeImage(image,length,pixels);
2456 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2457 if (status == MagickFalse)
2459 (void) CloseBlob(image);
2460 return(MagickFalse);
2467 Dump uncompressed PseudoColor packets.
2469 Ascii85Initialize(image);
2470 for (y=0; y < (ssize_t) image->rows; y++)
2472 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
2473 if (p == (const PixelPacket *) NULL)
2475 for (x=0; x < (ssize_t) image->columns; x++)
2477 Ascii85Encode(image,ScaleQuantumToChar((Quantum) (QuantumRange-
2478 GetOpacityPixelComponent(p))));
2482 Ascii85Flush(image);
2486 offset=TellBlob(image)-offset;
2487 (void) WriteBlobString(image,"\nendstream\n");
2489 (void) WriteBlobString(image,"endobj\n");
2491 Write Length object.
2493 xref[object++]=TellBlob(image);
2494 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2496 (void) WriteBlobString(image,buffer);
2497 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
2498 (void) WriteBlobString(image,buffer);
2499 (void) WriteBlobString(image,"endobj\n");
2500 if (GetNextImageInList(image) == (Image *) NULL)
2502 image=SyncNextImageInList(image);
2503 status=SetImageProgress(image,SaveImagesTag,scene++,
2504 GetImageListLength(image));
2505 if (status == MagickFalse)
2507 } while (image_info->adjoin != MagickFalse);
2509 Write Metadata object.
2511 xref[object++]=TellBlob(image);
2513 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2515 (void) WriteBlobString(image,buffer);
2516 (void) WriteBlobString(image,"<<\n");
2517 GetPathComponent(image->filename,BasePath,basename);
2518 (void) FormatLocaleString(buffer,MaxTextExtent,"/Title (%s)\n",
2519 EscapeParenthesis(basename));
2520 (void) WriteBlobString(image,buffer);
2521 seconds=time((time_t *) NULL);
2522 #if defined(MAGICKCORE_HAVE_LOCALTIME_R)
2523 (void) localtime_r(&seconds,&local_time);
2525 (void) memcpy(&local_time,localtime(&seconds),sizeof(local_time));
2527 (void) FormatLocaleString(date,MaxTextExtent,"D:%04d%02d%02d%02d%02d%02d",
2528 local_time.tm_year+1900,local_time.tm_mon+1,local_time.tm_mday,
2529 local_time.tm_hour,local_time.tm_min,local_time.tm_sec);
2530 (void) FormatLocaleString(buffer,MaxTextExtent,"/CreationDate (%s)\n",date);
2531 (void) WriteBlobString(image,buffer);
2532 (void) FormatLocaleString(buffer,MaxTextExtent,"/ModDate (%s)\n",date);
2533 (void) WriteBlobString(image,buffer);
2534 (void) FormatLocaleString(buffer,MaxTextExtent,"/Producer (%s)\n",
2535 EscapeParenthesis(GetMagickVersion((size_t *) NULL)));
2536 (void) WriteBlobString(image,buffer);
2537 (void) WriteBlobString(image,">>\n");
2538 (void) WriteBlobString(image,"endobj\n");
2542 offset=TellBlob(image)-xref[0]+10;
2543 (void) WriteBlobString(image,"xref\n");
2544 (void) FormatLocaleString(buffer,MaxTextExtent,"0 %.20g\n",(double)
2546 (void) WriteBlobString(image,buffer);
2547 (void) WriteBlobString(image,"0000000000 65535 f \n");
2548 for (i=0; i < (ssize_t) object; i++)
2550 (void) FormatLocaleString(buffer,MaxTextExtent,"%010lu 00000 n \n",
2551 (unsigned long) xref[i]);
2552 (void) WriteBlobString(image,buffer);
2554 (void) WriteBlobString(image,"trailer\n");
2555 (void) WriteBlobString(image,"<<\n");
2556 (void) FormatLocaleString(buffer,MaxTextExtent,"/Size %.20g\n",(double)
2558 (void) WriteBlobString(image,buffer);
2559 (void) FormatLocaleString(buffer,MaxTextExtent,"/Info %.20g 0 R\n",(double)
2561 (void) WriteBlobString(image,buffer);
2562 (void) FormatLocaleString(buffer,MaxTextExtent,"/Root %.20g 0 R\n",(double)
2564 (void) WriteBlobString(image,buffer);
2565 (void) WriteBlobString(image,">>\n");
2566 (void) WriteBlobString(image,"startxref\n");
2567 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
2568 (void) WriteBlobString(image,buffer);
2569 (void) WriteBlobString(image,"%%EOF\n");
2570 xref=(MagickOffsetType *) RelinquishMagickMemory(xref);
2571 (void) CloseBlob(image);