2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Portable Document Format %
20 % Copyright 1999-2014 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 "MagickCore/studio.h"
43 #include "MagickCore/attribute.h"
44 #include "MagickCore/artifact.h"
45 #include "MagickCore/blob.h"
46 #include "MagickCore/blob-private.h"
47 #include "MagickCore/cache.h"
48 #include "MagickCore/color.h"
49 #include "MagickCore/color-private.h"
50 #include "MagickCore/colorspace.h"
51 #include "MagickCore/colorspace-private.h"
52 #include "MagickCore/compress.h"
53 #include "MagickCore/constitute.h"
54 #include "MagickCore/delegate.h"
55 #include "MagickCore/delegate-private.h"
56 #include "MagickCore/draw.h"
57 #include "MagickCore/exception.h"
58 #include "MagickCore/exception-private.h"
59 #include "MagickCore/geometry.h"
60 #include "MagickCore/image.h"
61 #include "MagickCore/image-private.h"
62 #include "MagickCore/list.h"
63 #include "MagickCore/magick.h"
64 #include "MagickCore/memory_.h"
65 #include "MagickCore/monitor.h"
66 #include "MagickCore/monitor-private.h"
67 #include "MagickCore/nt-base-private.h"
68 #include "MagickCore/option.h"
69 #include "MagickCore/pixel-accessor.h"
70 #include "MagickCore/profile.h"
71 #include "MagickCore/property.h"
72 #include "MagickCore/quantum-private.h"
73 #include "MagickCore/resource_.h"
74 #include "MagickCore/resize.h"
75 #include "MagickCore/signature.h"
76 #include "MagickCore/static.h"
77 #include "MagickCore/string_.h"
78 #include "MagickCore/module.h"
79 #include "MagickCore/token.h"
80 #include "MagickCore/transform.h"
81 #include "MagickCore/utility.h"
82 #include "MagickCore/module.h"
87 #if defined(MAGICKCORE_TIFF_DELEGATE)
88 #define CCITTParam "-1"
90 #define CCITTParam "0"
96 static MagickBooleanType
97 WritePDFImage(const ImageInfo *,Image *,ExceptionInfo *);
100 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
104 % I n v o k e P D F D e l e g a t e %
108 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110 % InvokePDFDelegate() executes the PDF interpreter with the specified command.
112 % The format of the InvokePDFDelegate method is:
114 % MagickBooleanType InvokePDFDelegate(const MagickBooleanType verbose,
115 % const char *command,ExceptionInfo *exception)
117 % A description of each parameter follows:
119 % o verbose: A value other than zero displays the command prior to
122 % o command: the address of a character string containing the command to
125 % o exception: return any errors or warnings in this structure.
128 static MagickBooleanType InvokePDFDelegate(const MagickBooleanType verbose,
129 const char *command,ExceptionInfo *exception)
134 #if defined(MAGICKCORE_GS_DELEGATE) || defined(MAGICKCORE_WINDOWS_SUPPORT)
151 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
152 ghost_info=NTGhostscriptDLLVectors();
157 ghost_info=(&ghost_info_struct);
158 (void) ResetMagickMemory(&ghost_info,0,sizeof(ghost_info));
159 ghost_info_struct.new_instance=(int (*)(gs_main_instance **,void *))
161 ghost_info_struct.init_with_args=(int (*)(gs_main_instance *,int,char **))
162 gsapi_init_with_args;
163 ghost_info_struct.run_string=(int (*)(gs_main_instance *,const char *,int,
164 int *)) gsapi_run_string;
165 ghost_info_struct.delete_instance=(void (*)(gs_main_instance *))
166 gsapi_delete_instance;
167 ghost_info_struct.exit=(int (*)(gs_main_instance *)) gsapi_exit;
169 if (ghost_info == (GhostInfo *) NULL)
171 status=SystemCommand(MagickFalse,verbose,command,exception);
172 return(status == 0 ? MagickTrue : MagickFalse);
174 if (verbose != MagickFalse)
176 (void) fputs("[ghostscript library]",stdout);
177 (void) fputs(strchr(command,' '),stdout);
179 status=(ghost_info->new_instance)(&interpreter,(void *) NULL);
182 status=SystemCommand(MagickFalse,verbose,command,exception);
183 return(status == 0 ? MagickTrue : MagickFalse);
186 argv=StringToArgv(command,&argc);
187 if (argv == (char **) NULL)
189 status=(ghost_info->init_with_args)(interpreter,argc-1,argv+1);
191 status=(ghost_info->run_string)(interpreter,"systemdict /start get exec\n",
193 (ghost_info->exit)(interpreter);
194 (ghost_info->delete_instance)(interpreter);
195 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
196 NTGhostscriptUnLoadDLL();
198 for (i=0; i < (ssize_t) argc; i++)
199 argv[i]=DestroyString(argv[i]);
200 argv=(char **) RelinquishMagickMemory(argv);
201 if ((status != 0) && (status != -101))
206 message=GetExceptionMessage(errno);
207 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
208 "`%s': %s",command,message);
209 message=DestroyString(message);
210 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
211 "Ghostscript returns status %d, exit code %d",status,code);
216 status=SystemCommand(MagickFalse,verbose,command,exception);
217 return(status == 0 ? MagickTrue : MagickFalse);
222 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
230 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
232 % IsPDF() returns MagickTrue if the image format type, identified by the
233 % magick string, is PDF.
235 % The format of the IsPDF method is:
237 % MagickBooleanType IsPDF(const unsigned char *magick,const size_t offset)
239 % A description of each parameter follows:
241 % o magick: compare image format pattern against these bytes.
243 % o offset: Specifies the offset of the magick string.
246 static MagickBooleanType IsPDF(const unsigned char *magick,const size_t offset)
250 if (LocaleNCompare((const char *) magick,"%PDF-",5) == 0)
256 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
260 % R e a d P D F I m a g e %
264 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
266 % ReadPDFImage() reads a Portable Document Format image file and
267 % returns it. It allocates the memory necessary for the new Image structure
268 % and returns a pointer to the new image.
270 % The format of the ReadPDFImage method is:
272 % Image *ReadPDFImage(const ImageInfo *image_info,ExceptionInfo *exception)
274 % A description of each parameter follows:
276 % o image_info: the image info.
278 % o exception: return any errors or warnings in this structure.
282 static MagickBooleanType IsPDFRendered(const char *path)
290 if ((path == (const char *) NULL) || (*path == '\0'))
292 status=GetPathAttributes(path,&attributes);
293 if ((status != MagickFalse) && S_ISREG(attributes.st_mode) &&
294 (attributes.st_size > 0))
299 static Image *ReadPDFImage(const ImageInfo *image_info,ExceptionInfo *exception)
301 #define CMYKProcessColor "CMYKProcessColor"
302 #define CropBox "CropBox"
303 #define DefaultCMYK "DefaultCMYK"
304 #define DeviceCMYK "DeviceCMYK"
305 #define MediaBox "MediaBox"
306 #define RenderPostscriptText "Rendering Postscript... "
307 #define PDFRotate "Rotate"
308 #define SpotColor "Separation"
309 #define TrimBox "TrimBox"
310 #define PDFVersion "PDF-"
313 command[MaxTextExtent],
314 density[MaxTextExtent],
315 filename[MaxTextExtent],
316 geometry[MaxTextExtent],
317 options[MaxTextExtent],
318 input_filename[MaxTextExtent],
319 postscript_filename[MaxTextExtent];
379 assert(image_info != (const ImageInfo *) NULL);
380 assert(image_info->signature == MagickSignature);
381 if (image_info->debug != MagickFalse)
382 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
383 image_info->filename);
384 assert(exception != (ExceptionInfo *) NULL);
385 assert(exception->signature == MagickSignature);
389 image=AcquireImage(image_info,exception);
390 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
391 if (status == MagickFalse)
393 image=DestroyImageList(image);
394 return((Image *) NULL);
396 status=AcquireUniqueSymbolicLink(image_info->filename,input_filename);
397 if (status == MagickFalse)
399 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
400 image_info->filename);
401 image=DestroyImageList(image);
402 return((Image *) NULL);
405 Set the page density.
407 delta.x=DefaultResolution;
408 delta.y=DefaultResolution;
409 if ((image->resolution.x == 0.0) || (image->resolution.y == 0.0))
411 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
412 image->resolution.x=geometry_info.rho;
413 image->resolution.y=geometry_info.sigma;
414 if ((flags & SigmaValue) == 0)
415 image->resolution.y=image->resolution.x;
417 if (image_info->density != (char *) NULL)
419 flags=ParseGeometry(image_info->density,&geometry_info);
420 image->resolution.x=geometry_info.rho;
421 image->resolution.y=geometry_info.sigma;
422 if ((flags & SigmaValue) == 0)
423 image->resolution.y=image->resolution.x;
425 (void) ParseAbsoluteGeometry(PSPageGeometry,&page);
426 if (image_info->page != (char *) NULL)
427 (void) ParseAbsoluteGeometry(image_info->page,&page);
428 page.width=(size_t) ceil((double) (page.width*image->resolution.x/delta.x)-
430 page.height=(size_t) ceil((double) (page.height*image->resolution.y/delta.y)-
433 Determine page geometry from the PDF media box.
435 cmyk=image_info->colorspace == CMYKColorspace ? MagickTrue : MagickFalse;
436 cropbox=IsStringTrue(GetImageOption(image_info,"pdf:use-cropbox"));
437 trimbox=IsStringTrue(GetImageOption(image_info,"pdf:use-trimbox"));
440 (void) ResetMagickMemory(&bounding_box,0,sizeof(bounding_box));
441 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
442 (void) ResetMagickMemory(&hires_bounds,0,sizeof(hires_bounds));
443 (void) ResetMagickMemory(command,0,sizeof(command));
446 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
454 if ((c != (int) '/') && (c != (int) '%') &&
455 ((size_t) (p-command) < (MaxTextExtent-1)))
459 if (LocaleNCompare(PDFRotate,command,strlen(PDFRotate)) == 0)
460 count=(ssize_t) sscanf(command,"Rotate %lf",&angle);
462 Is this a CMYK document?
464 if (LocaleNCompare(DefaultCMYK,command,strlen(DefaultCMYK)) == 0)
466 if (LocaleNCompare(DeviceCMYK,command,strlen(DeviceCMYK)) == 0)
468 if (LocaleNCompare(CMYKProcessColor,command,strlen(CMYKProcessColor)) == 0)
470 if (LocaleNCompare(SpotColor,command,strlen(SpotColor)) == 0)
474 property[MaxTextExtent],
483 (void) FormatLocaleString(property,MaxTextExtent,"pdf:SpotColor-%.20g",
484 (double) spotcolor++);
486 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
488 if ((isspace(c) != 0) || (c == '/') || ((i+1) == MaxTextExtent))
493 value=AcquireString(name);
494 (void) SubstituteString(&value,"#20"," ");
495 (void) SetImageProperty(image,property,value,exception);
496 value=DestroyString(value);
499 if (LocaleNCompare(PDFVersion,command,strlen(PDFVersion)) == 0)
500 (void) SetImageProperty(image,"pdf:Version",command,exception);
501 if (image_info->page != (char *) NULL)
504 if (cropbox != MagickFalse)
506 if (LocaleNCompare(CropBox,command,strlen(CropBox)) == 0)
509 Note region defined by crop box.
511 count=(ssize_t) sscanf(command,"CropBox [%lf %lf %lf %lf",
512 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
514 count=(ssize_t) sscanf(command,"CropBox[%lf %lf %lf %lf",
515 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
519 if (trimbox != MagickFalse)
521 if (LocaleNCompare(TrimBox,command,strlen(TrimBox)) == 0)
524 Note region defined by trim box.
526 count=(ssize_t) sscanf(command,"TrimBox [%lf %lf %lf %lf",
527 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
529 count=(ssize_t) sscanf(command,"TrimBox[%lf %lf %lf %lf",
530 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
534 if (LocaleNCompare(MediaBox,command,strlen(MediaBox)) == 0)
537 Note region defined by media box.
539 count=(ssize_t) sscanf(command,"MediaBox [%lf %lf %lf %lf",
540 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
542 count=(ssize_t) sscanf(command,"MediaBox[%lf %lf %lf %lf",
543 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
547 if ((fabs(bounds.x2-bounds.x1) <= fabs(hires_bounds.x2-hires_bounds.x1)) ||
548 (fabs(bounds.y2-bounds.y1) <= fabs(hires_bounds.y2-hires_bounds.y1)))
552 if ((fabs(hires_bounds.x2-hires_bounds.x1) >= MagickEpsilon) &&
553 (fabs(hires_bounds.y2-hires_bounds.y1) >= MagickEpsilon))
556 Set PDF render geometry.
558 (void) FormatLocaleString(geometry,MaxTextExtent,"%gx%g%+.15g%+.15g",
559 hires_bounds.x2-bounds.x1,hires_bounds.y2-hires_bounds.y1,
560 hires_bounds.x1,hires_bounds.y1);
561 (void) SetImageProperty(image,"pdf:HiResBoundingBox",geometry,exception);
562 page.width=(size_t) ceil((double) ((hires_bounds.x2-hires_bounds.x1)*
563 image->resolution.x/delta.x)-0.5);
564 page.height=(size_t) ceil((double) ((hires_bounds.y2-hires_bounds.y1)*
565 image->resolution.y/delta.y)-0.5);
568 option=GetImageOption(image_info,"pdf:fit-page");
569 if (option != (char *) NULL)
577 geometry=GetPageGeometry(option);
578 flags=ParseMetaGeometry(geometry,&page.x,&page.y,&page.width,&page.height);
579 if (flags == NoValue)
581 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
582 "InvalidGeometry","`%s'",option);
583 image=DestroyImage(image);
584 return((Image *) NULL);
586 page.width=(size_t) ceil((double) (page.width*image->resolution.x/delta.x)
588 page.height=(size_t) ceil((double) (page.height*image->resolution.y/
590 geometry=DestroyString(geometry);
593 (void) CloseBlob(image);
594 if ((fabs(angle) == 90.0) || (fabs(angle) == 270.0))
600 page.width=page.height;
603 if (IssRGBCompatibleColorspace(image_info->colorspace) != MagickFalse)
606 Create Ghostscript control file.
608 file=AcquireUniqueFileResource(postscript_filename);
611 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
612 image_info->filename);
613 image=DestroyImage(image);
614 return((Image *) NULL);
616 count=write(file," ",1);
619 Render Postscript with the Ghostscript delegate.
621 if ((image_info->ping != MagickFalse) ||
622 (image_info->monochrome != MagickFalse))
623 delegate_info=GetDelegateInfo("ps:mono",(char *) NULL,exception);
625 if (cmyk != MagickFalse)
626 delegate_info=GetDelegateInfo("ps:cmyk",(char *) NULL,exception);
628 delegate_info=GetDelegateInfo("ps:alpha",(char *) NULL,exception);
629 if (delegate_info == (const DelegateInfo *) NULL)
631 (void) RelinquishUniqueFileResource(postscript_filename);
632 image=DestroyImage(image);
633 return((Image *) NULL);
636 (void) FormatLocaleString(density,MaxTextExtent,"%gx%g",image->resolution.x,
637 image->resolution.y);
638 if ((image_info->page != (char *) NULL) || (fitPage != MagickFalse))
639 (void) FormatLocaleString(options,MaxTextExtent,"-g%.20gx%.20g ",(double)
640 page.width,(double) page.height);
641 if (fitPage != MagickFalse)
642 (void) ConcatenateMagickString(options,"-dPSFitPage ",MaxTextExtent);
643 if (cmyk != MagickFalse)
644 (void) ConcatenateMagickString(options,"-dUseCIEColor ",MaxTextExtent);
645 if (cropbox != MagickFalse)
646 (void) ConcatenateMagickString(options,"-dUseCropBox ",MaxTextExtent);
647 if (trimbox != MagickFalse)
648 (void) ConcatenateMagickString(options,"-dUseTrimBox ",MaxTextExtent);
649 read_info=CloneImageInfo(image_info);
650 *read_info->magick='\0';
651 if (read_info->number_scenes != 0)
654 pages[MaxTextExtent];
656 (void) FormatLocaleString(pages,MaxTextExtent,"-dFirstPage=%.20g "
657 "-dLastPage=%.20g",(double) read_info->scene+1,(double)
658 (read_info->scene+read_info->number_scenes));
659 (void) ConcatenateMagickString(options,pages,MaxTextExtent);
660 read_info->number_scenes=0;
661 if (read_info->scenes != (char *) NULL)
662 *read_info->scenes='\0';
664 option=GetImageOption(image_info,"authenticate");
665 if (option != (const char *) NULL)
666 (void) FormatLocaleString(options+strlen(options),MaxTextExtent,
667 " -sPCLPassword=%s",option);
668 (void) CopyMagickString(filename,read_info->filename,MaxTextExtent);
669 (void) AcquireUniqueFilename(filename);
670 (void) ConcatenateMagickString(filename,"%d",MaxTextExtent);
671 (void) FormatLocaleString(command,MaxTextExtent,
672 GetDelegateCommands(delegate_info),
673 read_info->antialias != MagickFalse ? 4 : 1,
674 read_info->antialias != MagickFalse ? 4 : 1,density,options,filename,
675 postscript_filename,input_filename);
676 status=InvokePDFDelegate(read_info->verbose,command,exception);
677 (void) RelinquishUniqueFileResource(postscript_filename);
678 (void) RelinquishUniqueFileResource(input_filename);
679 pdf_image=(Image *) NULL;
680 if (status == MagickFalse)
683 (void) InterpretImageFilename(image_info,image,filename,(int) i,
684 read_info->filename,exception);
685 if (IsPDFRendered(read_info->filename) == MagickFalse)
687 (void) RelinquishUniqueFileResource(read_info->filename);
692 (void) InterpretImageFilename(image_info,image,filename,(int) i,
693 read_info->filename,exception);
694 if (IsPDFRendered(read_info->filename) == MagickFalse)
696 read_info->blob=NULL;
698 next=ReadImage(read_info,exception);
699 (void) RelinquishUniqueFileResource(read_info->filename);
700 if (next == (Image *) NULL)
702 AppendImageToList(&pdf_image,next);
704 read_info=DestroyImageInfo(read_info);
705 if (pdf_image == (Image *) NULL)
707 ThrowFileException(exception,DelegateError,"PostscriptDelegateFailed",
708 image_info->filename);
709 image=DestroyImage(image);
710 return((Image *) NULL);
712 if (LocaleCompare(pdf_image->magick,"BMP") == 0)
717 cmyk_image=ConsolidateCMYKImages(pdf_image,exception);
718 if (cmyk_image != (Image *) NULL)
720 pdf_image=DestroyImageList(pdf_image);
721 pdf_image=cmyk_image;
724 if (image_info->number_scenes != 0)
733 Add place holder images to meet the subimage specification requirement.
735 for (i=0; i < (ssize_t) image_info->scene; i++)
737 clone_image=CloneImage(pdf_image,1,1,MagickTrue,exception);
738 if (clone_image != (Image *) NULL)
739 PrependImageToList(&pdf_image,clone_image);
744 (void) CopyMagickString(pdf_image->filename,filename,MaxTextExtent);
745 pdf_image->page=page;
746 (void) CloneImageProfiles(pdf_image,image);
747 (void) CloneImageProperties(pdf_image,image);
748 next=SyncNextImageInList(pdf_image);
749 if (next != (Image *) NULL)
751 } while (next != (Image *) NULL);
752 image=DestroyImage(image);
754 for (next=GetFirstImageInList(pdf_image); next != (Image *) NULL; )
757 next=GetNextImageInList(next);
759 return(GetFirstImageInList(pdf_image));
763 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
767 % R e g i s t e r P D F I m a g e %
771 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
773 % RegisterPDFImage() adds properties for the PDF image format to
774 % the list of supported formats. The properties include the image format
775 % tag, a method to read and/or write the format, whether the format
776 % supports the saving of more than one frame to the same file or blob,
777 % whether the format supports native in-memory I/O, and a brief
778 % description of the format.
780 % The format of the RegisterPDFImage method is:
782 % size_t RegisterPDFImage(void)
785 ModuleExport size_t RegisterPDFImage(void)
790 entry=SetMagickInfo("AI");
791 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
792 entry->encoder=(EncodeImageHandler *) WritePDFImage;
793 entry->adjoin=MagickFalse;
794 entry->blob_support=MagickFalse;
795 entry->seekable_stream=MagickTrue;
796 entry->description=ConstantString("Adobe Illustrator CS2");
797 entry->mime_type=ConstantString("application/pdf");
798 entry->module=ConstantString("PDF");
799 (void) RegisterMagickInfo(entry);
800 entry=SetMagickInfo("EPDF");
801 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
802 entry->encoder=(EncodeImageHandler *) WritePDFImage;
803 entry->adjoin=MagickFalse;
804 entry->blob_support=MagickFalse;
805 entry->seekable_stream=MagickTrue;
806 entry->description=ConstantString("Encapsulated Portable Document Format");
807 entry->mime_type=ConstantString("application/pdf");
808 entry->module=ConstantString("PDF");
809 (void) RegisterMagickInfo(entry);
810 entry=SetMagickInfo("PDF");
811 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
812 entry->encoder=(EncodeImageHandler *) WritePDFImage;
813 entry->magick=(IsImageFormatHandler *) IsPDF;
814 entry->blob_support=MagickFalse;
815 entry->seekable_stream=MagickTrue;
816 entry->description=ConstantString("Portable Document Format");
817 entry->mime_type=ConstantString("application/pdf");
818 entry->module=ConstantString("PDF");
819 (void) RegisterMagickInfo(entry);
820 entry=SetMagickInfo("PDFA");
821 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
822 entry->encoder=(EncodeImageHandler *) WritePDFImage;
823 entry->magick=(IsImageFormatHandler *) IsPDF;
824 entry->blob_support=MagickFalse;
825 entry->seekable_stream=MagickTrue;
826 entry->description=ConstantString("Portable Document Archive Format");
827 entry->mime_type=ConstantString("application/pdf");
828 entry->module=ConstantString("PDF");
829 (void) RegisterMagickInfo(entry);
830 return(MagickImageCoderSignature);
834 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
838 % U n r e g i s t e r P D F I m a g e %
842 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
844 % UnregisterPDFImage() removes format registrations made by the
845 % PDF module from the list of supported formats.
847 % The format of the UnregisterPDFImage method is:
849 % UnregisterPDFImage(void)
852 ModuleExport void UnregisterPDFImage(void)
854 (void) UnregisterMagickInfo("AI");
855 (void) UnregisterMagickInfo("EPDF");
856 (void) UnregisterMagickInfo("PDF");
857 (void) UnregisterMagickInfo("PDFA");
861 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
865 % W r i t e P D F I m a g e %
869 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
871 % WritePDFImage() writes an image in the Portable Document image
874 % The format of the WritePDFImage method is:
876 % MagickBooleanType WritePDFImage(const ImageInfo *image_info,
877 % Image *image,ExceptionInfo *exception)
879 % A description of each parameter follows.
881 % o image_info: the image info.
883 % o image: The image.
885 % o exception: return any errors or warnings in this structure.
889 static inline size_t MagickMax(const size_t x,const size_t y)
896 static inline size_t MagickMin(const size_t x,const size_t y)
903 static char *EscapeParenthesis(const char *text)
915 buffer[MaxTextExtent];
919 for (i=0; i < (ssize_t) MagickMin(strlen(text),(MaxTextExtent-escapes-1)); i++)
921 if ((text[i] == '(') || (text[i] == ')'))
932 static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
933 Image *image,Image *inject_image,ExceptionInfo *exception)
951 write_info=CloneImageInfo(image_info);
952 (void) CopyMagickString(write_info->filename,"GROUP4:",MaxTextExtent);
953 (void) CopyMagickString(write_info->magick,"GROUP4",MaxTextExtent);
954 group4_image=CloneImage(inject_image,0,0,MagickTrue,exception);
955 if (group4_image == (Image *) NULL)
957 group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
959 group4_image=DestroyImage(group4_image);
960 if (group4 == (unsigned char *) NULL)
962 write_info=DestroyImageInfo(write_info);
963 if (WriteBlob(image,length,group4) != (ssize_t) length)
965 group4=(unsigned char *) RelinquishMagickMemory(group4);
969 static MagickBooleanType WritePDFImage(const ImageInfo *image_info,Image *image,
970 ExceptionInfo *exception)
972 #define CFormat "/Filter [ /%s ]\n"
973 #define ObjectsPerImage 14
975 DisableMSCWarning(4310)
979 "<?xpacket begin=\"%s\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n"
980 "<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"
981 " <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n"
982 " <rdf:Description rdf:about=\"\"\n"
983 " xmlns:xap=\"http://ns.adobe.com/xap/1.0/\">\n"
984 " <xap:ModifyDate>%s</xap:ModifyDate>\n"
985 " <xap:CreateDate>%s</xap:CreateDate>\n"
986 " <xap:MetadataDate>%s</xap:MetadataDate>\n"
987 " <xap:CreatorTool>%s</xap:CreatorTool>\n"
988 " </rdf:Description>\n"
989 " <rdf:Description rdf:about=\"\"\n"
990 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"
991 " <dc:format>application/pdf</dc:format>\n"
994 " <rdf:li xml:lang=\"x-default\">%s</rdf:li>\n"
997 " </rdf:Description>\n"
998 " <rdf:Description rdf:about=\"\"\n"
999 " xmlns:xapMM=\"http://ns.adobe.com/xap/1.0/mm/\">\n"
1000 " <xapMM:DocumentID>uuid:6ec119d7-7982-4f56-808d-dfe64f5b35cf</xapMM:DocumentID>\n"
1001 " <xapMM:InstanceID>uuid:a79b99b4-6235-447f-9f6c-ec18ef7555cb</xapMM:InstanceID>\n"
1002 " </rdf:Description>\n"
1003 " <rdf:Description rdf:about=\"\"\n"
1004 " xmlns:pdf=\"http://ns.adobe.com/pdf/1.3/\">\n"
1005 " <pdf:Producer>%s</pdf:Producer>\n"
1006 " </rdf:Description>\n"
1007 " <rdf:Description rdf:about=\"\"\n"
1008 " xmlns:pdfaid=\"http://www.aiim.org/pdfa/ns/id/\">\n"
1009 " <pdfaid:part>2</pdfaid:part>\n"
1010 " <pdfaid:conformance>B</pdfaid:conformance>\n"
1011 " </rdf:Description>\n"
1014 "<?xpacket end=\"w\"?>\n"
1016 XMPProfileMagick[4]= { (char) 0xef, (char) 0xbb, (char) 0xbf, (char) 0x00 };
1020 basename[MaxTextExtent],
1021 buffer[MaxTextExtent],
1022 date[MaxTextExtent],
1024 page_geometry[MaxTextExtent];
1066 register const Quantum
1069 register unsigned char
1099 Open output image file.
1101 assert(image_info != (const ImageInfo *) NULL);
1102 assert(image_info->signature == MagickSignature);
1103 assert(image != (Image *) NULL);
1104 assert(image->signature == MagickSignature);
1105 if (image->debug != MagickFalse)
1106 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1107 assert(exception != (ExceptionInfo *) NULL);
1108 assert(exception->signature == MagickSignature);
1109 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1110 if (status == MagickFalse)
1113 Allocate X ref memory.
1115 xref=(MagickOffsetType *) AcquireQuantumMemory(2048UL,sizeof(*xref));
1116 if (xref == (MagickOffsetType *) NULL)
1117 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1118 (void) ResetMagickMemory(xref,0,2048UL*sizeof(*xref));
1124 if (image_info->compression == JPEG2000Compression)
1125 version=(size_t) MagickMax(version,5);
1126 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
1127 if (next->alpha_trait == BlendPixelTrait)
1128 version=(size_t) MagickMax(version,4);
1129 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1130 version=(size_t) MagickMax(version,6);
1131 (void) FormatLocaleString(buffer,MaxTextExtent,"%%PDF-1.%.20g \n",(double)
1133 (void) WriteBlobString(image,buffer);
1134 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1136 (void) WriteBlobByte(image,'%');
1137 (void) WriteBlobByte(image,0xe2);
1138 (void) WriteBlobByte(image,0xe3);
1139 (void) WriteBlobByte(image,0xcf);
1140 (void) WriteBlobByte(image,0xd3);
1141 (void) WriteBlobByte(image,'\n');
1144 Write Catalog object.
1146 xref[object++]=TellBlob(image);
1148 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1150 (void) WriteBlobString(image,buffer);
1151 (void) WriteBlobString(image,"<<\n");
1152 if (LocaleCompare(image_info->magick,"PDFA") != 0)
1153 (void) FormatLocaleString(buffer,MaxTextExtent,"/Pages %.20g 0 R\n",(double)
1157 (void) FormatLocaleString(buffer,MaxTextExtent,"/Metadata %.20g 0 R\n",
1159 (void) WriteBlobString(image,buffer);
1160 (void) FormatLocaleString(buffer,MaxTextExtent,"/Pages %.20g 0 R\n",
1163 (void) WriteBlobString(image,buffer);
1164 (void) WriteBlobString(image,"/Type /Catalog\n");
1165 (void) WriteBlobString(image,">>\n");
1166 (void) WriteBlobString(image,"endobj\n");
1167 GetPathComponent(image->filename,BasePath,basename);
1168 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1171 create_date[MaxTextExtent],
1172 modify_date[MaxTextExtent],
1173 timestamp[MaxTextExtent],
1174 xmp_profile[MaxTextExtent];
1182 xref[object++]=TellBlob(image);
1183 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1185 (void) WriteBlobString(image,buffer);
1186 (void) WriteBlobString(image,"<<\n");
1187 (void) WriteBlobString(image,"/Subtype /XML\n");
1189 value=GetImageProperty(image,"date:modify",exception);
1190 if (value != (const char *) NULL)
1191 (void) CopyMagickString(modify_date,value,MaxTextExtent);
1193 value=GetImageProperty(image,"date:create",exception);
1194 if (value != (const char *) NULL)
1195 (void) CopyMagickString(create_date,value,MaxTextExtent);
1196 (void) FormatMagickTime(time((time_t *) NULL),MaxTextExtent,timestamp);
1197 i=FormatLocaleString(xmp_profile,MaxTextExtent,XMPProfile,
1198 XMPProfileMagick,modify_date,create_date,timestamp,
1199 GetMagickVersion(&version),EscapeParenthesis(basename),
1200 GetMagickVersion(&version));
1201 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g\n",(double)
1203 (void) WriteBlobString(image,buffer);
1204 (void) WriteBlobString(image,"/Type /Metadata\n");
1205 (void) WriteBlobString(image,">>\nstream\n");
1206 (void) WriteBlobString(image,xmp_profile);
1207 (void) WriteBlobString(image,"\nendstream\n");
1208 (void) WriteBlobString(image,"endobj\n");
1213 xref[object++]=TellBlob(image);
1215 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1217 (void) WriteBlobString(image,buffer);
1218 (void) WriteBlobString(image,"<<\n");
1219 (void) WriteBlobString(image,"/Type /Pages\n");
1220 (void) FormatLocaleString(buffer,MaxTextExtent,"/Kids [ %.20g 0 R ",(double)
1222 (void) WriteBlobString(image,buffer);
1223 count=(ssize_t) (pages_id+ObjectsPerImage+1);
1224 if (image_info->adjoin != MagickFalse)
1230 Predict page object id's.
1233 for ( ; GetNextImageInList(kid_image) != (Image *) NULL; count+=ObjectsPerImage)
1235 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 R ",(double)
1237 (void) WriteBlobString(image,buffer);
1238 kid_image=GetNextImageInList(kid_image);
1240 xref=(MagickOffsetType *) ResizeQuantumMemory(xref,(size_t) count+2048UL,
1242 if (xref == (MagickOffsetType *) NULL)
1243 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1245 (void) WriteBlobString(image,"]\n");
1246 (void) FormatLocaleString(buffer,MaxTextExtent,"/Count %.20g\n",(double)
1247 ((count-pages_id)/ObjectsPerImage));
1248 (void) WriteBlobString(image,buffer);
1249 (void) WriteBlobString(image,">>\n");
1250 (void) WriteBlobString(image,"endobj\n");
1254 compression=image->compression;
1255 if (image_info->compression != UndefinedCompression)
1256 compression=image_info->compression;
1257 switch (compression)
1259 case FaxCompression:
1260 case Group4Compression:
1262 if ((IsImageMonochrome(image,exception) == MagickFalse) ||
1263 (image->alpha_trait == BlendPixelTrait))
1264 compression=RLECompression;
1267 #if !defined(MAGICKCORE_JPEG_DELEGATE)
1268 case JPEGCompression:
1270 compression=RLECompression;
1271 (void) ThrowMagickException(exception,GetMagickModule(),
1272 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
1277 #if !defined(MAGICKCORE_LIBOPENJP2_DELEGATE)
1278 case JPEG2000Compression:
1280 compression=RLECompression;
1281 (void) ThrowMagickException(exception,GetMagickModule(),
1282 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JP2)",
1287 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
1288 case ZipCompression:
1290 compression=RLECompression;
1291 (void) ThrowMagickException(exception,GetMagickModule(),
1292 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZLIB)",
1297 case LZWCompression:
1299 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1300 compression=RLECompression; /* LZW compression is forbidden */
1305 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1306 compression=RLECompression; /* ASCII 85 compression is forbidden */
1312 if (compression == JPEG2000Compression)
1313 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1315 Scale relative to dots-per-inch.
1317 delta.x=DefaultResolution;
1318 delta.y=DefaultResolution;
1319 resolution.x=image->resolution.x;
1320 resolution.y=image->resolution.y;
1321 if ((resolution.x == 0.0) || (resolution.y == 0.0))
1323 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
1324 resolution.x=geometry_info.rho;
1325 resolution.y=geometry_info.sigma;
1326 if ((flags & SigmaValue) == 0)
1327 resolution.y=resolution.x;
1329 if (image_info->density != (char *) NULL)
1331 flags=ParseGeometry(image_info->density,&geometry_info);
1332 resolution.x=geometry_info.rho;
1333 resolution.y=geometry_info.sigma;
1334 if ((flags & SigmaValue) == 0)
1335 resolution.y=resolution.x;
1337 if (image->units == PixelsPerCentimeterResolution)
1339 resolution.x=(double) ((size_t) (100.0*2.54*resolution.x+0.5)/100.0);
1340 resolution.y=(double) ((size_t) (100.0*2.54*resolution.y+0.5)/100.0);
1342 SetGeometry(image,&geometry);
1343 (void) FormatLocaleString(page_geometry,MaxTextExtent,"%.20gx%.20g",(double)
1344 image->columns,(double) image->rows);
1345 if (image_info->page != (char *) NULL)
1346 (void) CopyMagickString(page_geometry,image_info->page,MaxTextExtent);
1348 if ((image->page.width != 0) && (image->page.height != 0))
1349 (void) FormatLocaleString(page_geometry,MaxTextExtent,
1350 "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,(double)
1351 image->page.height,(double) image->page.x,(double) image->page.y);
1353 if ((image->gravity != UndefinedGravity) &&
1354 (LocaleCompare(image_info->magick,"PDF") == 0))
1355 (void) CopyMagickString(page_geometry,PSPageGeometry,MaxTextExtent);
1356 (void) ConcatenateMagickString(page_geometry,">",MaxTextExtent);
1357 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
1358 &geometry.width,&geometry.height);
1359 scale.x=(double) (geometry.width*delta.x)/resolution.x;
1360 geometry.width=(size_t) floor(scale.x+0.5);
1361 scale.y=(double) (geometry.height*delta.y)/resolution.y;
1362 geometry.height=(size_t) floor(scale.y+0.5);
1363 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
1364 (void) ParseGravityGeometry(image,page_geometry,&page_info,exception);
1365 if (image->gravity != UndefinedGravity)
1367 geometry.x=(-page_info.x);
1368 geometry.y=(ssize_t) (media_info.height+page_info.y-image->rows);
1371 if (image_info->pointsize != 0.0)
1372 pointsize=image_info->pointsize;
1374 value=GetImageProperty(image,"label",exception);
1375 if (value != (const char *) NULL)
1376 text_size=(size_t) (MultilineCensus(value)*pointsize+12);
1381 xref[object++]=TellBlob(image);
1382 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1384 (void) WriteBlobString(image,buffer);
1385 (void) WriteBlobString(image,"<<\n");
1386 (void) WriteBlobString(image,"/Type /Page\n");
1387 (void) FormatLocaleString(buffer,MaxTextExtent,"/Parent %.20g 0 R\n",
1389 (void) WriteBlobString(image,buffer);
1390 (void) WriteBlobString(image,"/Resources <<\n");
1391 labels=(char **) NULL;
1392 value=GetImageProperty(image,"label",exception);
1393 if (value != (const char *) NULL)
1394 labels=StringToList(value);
1395 if (labels != (char **) NULL)
1397 (void) FormatLocaleString(buffer,MaxTextExtent,
1398 "/Font << /F%.20g %.20g 0 R >>\n",(double) image->scene,(double)
1400 (void) WriteBlobString(image,buffer);
1402 (void) FormatLocaleString(buffer,MaxTextExtent,
1403 "/XObject << /Im%.20g %.20g 0 R >>\n",(double) image->scene,(double)
1405 (void) WriteBlobString(image,buffer);
1406 (void) FormatLocaleString(buffer,MaxTextExtent,"/ProcSet %.20g 0 R >>\n",
1408 (void) WriteBlobString(image,buffer);
1409 (void) FormatLocaleString(buffer,MaxTextExtent,
1410 "/MediaBox [0 0 %g %g]\n",72.0*media_info.width/resolution.x,
1411 72.0*media_info.height/resolution.y);
1412 (void) WriteBlobString(image,buffer);
1413 (void) FormatLocaleString(buffer,MaxTextExtent,
1414 "/CropBox [0 0 %g %g]\n",72.0*media_info.width/resolution.x,
1415 72.0*media_info.height/resolution.y);
1416 (void) WriteBlobString(image,buffer);
1417 (void) FormatLocaleString(buffer,MaxTextExtent,"/Contents %.20g 0 R\n",
1419 (void) WriteBlobString(image,buffer);
1420 (void) FormatLocaleString(buffer,MaxTextExtent,"/Thumb %.20g 0 R\n",(double)
1422 (void) WriteBlobString(image,buffer);
1423 (void) WriteBlobString(image,">>\n");
1424 (void) WriteBlobString(image,"endobj\n");
1426 Write Contents object.
1428 xref[object++]=TellBlob(image);
1429 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1431 (void) WriteBlobString(image,buffer);
1432 (void) WriteBlobString(image,"<<\n");
1433 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
1435 (void) WriteBlobString(image,buffer);
1436 (void) WriteBlobString(image,">>\n");
1437 (void) WriteBlobString(image,"stream\n");
1438 offset=TellBlob(image);
1439 (void) WriteBlobString(image,"q\n");
1440 if (labels != (char **) NULL)
1441 for (i=0; labels[i] != (char *) NULL; i++)
1443 (void) WriteBlobString(image,"BT\n");
1444 (void) FormatLocaleString(buffer,MaxTextExtent,"/F%.20g %g Tf\n",
1445 (double) image->scene,pointsize);
1446 (void) WriteBlobString(image,buffer);
1447 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g Td\n",
1448 (double) geometry.x,(double) (geometry.y+geometry.height+i*pointsize+
1450 (void) WriteBlobString(image,buffer);
1451 (void) FormatLocaleString(buffer,MaxTextExtent,"(%s) Tj\n",labels[i]);
1452 (void) WriteBlobString(image,buffer);
1453 (void) WriteBlobString(image,"ET\n");
1454 labels[i]=DestroyString(labels[i]);
1456 (void) FormatLocaleString(buffer,MaxTextExtent,"%g 0 0 %g %.20g %.20g cm\n",
1457 scale.x,scale.y,(double) geometry.x,(double) geometry.y);
1458 (void) WriteBlobString(image,buffer);
1459 (void) FormatLocaleString(buffer,MaxTextExtent,"/Im%.20g Do\n",(double)
1461 (void) WriteBlobString(image,buffer);
1462 (void) WriteBlobString(image,"Q\n");
1463 offset=TellBlob(image)-offset;
1464 (void) WriteBlobString(image,"\nendstream\n");
1465 (void) WriteBlobString(image,"endobj\n");
1467 Write Length object.
1469 xref[object++]=TellBlob(image);
1470 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1472 (void) WriteBlobString(image,buffer);
1473 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
1474 (void) WriteBlobString(image,buffer);
1475 (void) WriteBlobString(image,"endobj\n");
1477 Write Procset object.
1479 xref[object++]=TellBlob(image);
1480 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1482 (void) WriteBlobString(image,buffer);
1483 if ((image->storage_class == DirectClass) || (image->colors > 256))
1484 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageC",MaxTextExtent);
1486 if ((compression == FaxCompression) || (compression == Group4Compression))
1487 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageB",MaxTextExtent);
1489 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageI",MaxTextExtent);
1490 (void) WriteBlobString(image,buffer);
1491 (void) WriteBlobString(image," ]\n");
1492 (void) WriteBlobString(image,"endobj\n");
1496 xref[object++]=TellBlob(image);
1497 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1499 (void) WriteBlobString(image,buffer);
1500 (void) WriteBlobString(image,"<<\n");
1501 if (labels != (char **) NULL)
1503 (void) WriteBlobString(image,"/Type /Font\n");
1504 (void) WriteBlobString(image,"/Subtype /Type1\n");
1505 (void) FormatLocaleString(buffer,MaxTextExtent,"/Name /F%.20g\n",
1506 (double) image->scene);
1507 (void) WriteBlobString(image,buffer);
1508 (void) WriteBlobString(image,"/BaseFont /Helvetica\n");
1509 (void) WriteBlobString(image,"/Encoding /MacRomanEncoding\n");
1510 labels=(char **) RelinquishMagickMemory(labels);
1512 (void) WriteBlobString(image,">>\n");
1513 (void) WriteBlobString(image,"endobj\n");
1515 Write XObject object.
1517 xref[object++]=TellBlob(image);
1518 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1520 (void) WriteBlobString(image,buffer);
1521 (void) WriteBlobString(image,"<<\n");
1522 (void) WriteBlobString(image,"/Type /XObject\n");
1523 (void) WriteBlobString(image,"/Subtype /Image\n");
1524 (void) FormatLocaleString(buffer,MaxTextExtent,"/Name /Im%.20g\n",(double)
1526 (void) WriteBlobString(image,buffer);
1527 switch (compression)
1531 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"ASCII85Decode");
1534 case JPEGCompression:
1536 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"DCTDecode");
1537 if (image->colorspace != CMYKColorspace)
1539 (void) WriteBlobString(image,buffer);
1540 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1544 case JPEG2000Compression:
1546 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"JPXDecode");
1547 if (image->colorspace != CMYKColorspace)
1549 (void) WriteBlobString(image,buffer);
1550 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1554 case LZWCompression:
1556 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"LZWDecode");
1559 case ZipCompression:
1561 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"FlateDecode");
1564 case FaxCompression:
1565 case Group4Compression:
1567 (void) CopyMagickString(buffer,"/Filter [ /CCITTFaxDecode ]\n",
1569 (void) WriteBlobString(image,buffer);
1570 (void) FormatLocaleString(buffer,MaxTextExtent,"/DecodeParms [ << "
1571 "/K %s /BlackIs1 false /Columns %.20g /Rows %.20g >> ]\n",CCITTParam,
1572 (double) image->columns,(double) image->rows);
1577 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
1582 (void) WriteBlobString(image,buffer);
1583 (void) FormatLocaleString(buffer,MaxTextExtent,"/Width %.20g\n",(double)
1585 (void) WriteBlobString(image,buffer);
1586 (void) FormatLocaleString(buffer,MaxTextExtent,"/Height %.20g\n",(double)
1588 (void) WriteBlobString(image,buffer);
1589 (void) FormatLocaleString(buffer,MaxTextExtent,"/ColorSpace %.20g 0 R\n",
1591 (void) WriteBlobString(image,buffer);
1592 (void) FormatLocaleString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
1593 (compression == FaxCompression) || (compression == Group4Compression) ?
1595 (void) WriteBlobString(image,buffer);
1596 if (image->alpha_trait == BlendPixelTrait)
1598 (void) FormatLocaleString(buffer,MaxTextExtent,"/SMask %.20g 0 R\n",
1600 (void) WriteBlobString(image,buffer);
1602 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
1604 (void) WriteBlobString(image,buffer);
1605 (void) WriteBlobString(image,">>\n");
1606 (void) WriteBlobString(image,"stream\n");
1607 offset=TellBlob(image);
1608 number_pixels=(MagickSizeType) image->columns*image->rows;
1609 if ((4*number_pixels) != (MagickSizeType) ((size_t) (4*number_pixels)))
1610 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1611 if ((compression == FaxCompression) || (compression == Group4Compression) ||
1612 ((image_info->type != TrueColorType) &&
1613 (IsImageGray(image,exception) != MagickFalse)))
1615 switch (compression)
1617 case FaxCompression:
1618 case Group4Compression:
1620 if (LocaleCompare(CCITTParam,"0") == 0)
1622 (void) HuffmanEncodeImage(image_info,image,image,exception);
1625 (void) Huffman2DEncodeImage(image_info,image,image,exception);
1628 case JPEGCompression:
1630 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
1631 if (status == MagickFalse)
1633 (void) CloseBlob(image);
1634 return(MagickFalse);
1638 case JPEG2000Compression:
1640 status=InjectImageBlob(image_info,image,image,"jp2",exception);
1641 if (status == MagickFalse)
1643 (void) CloseBlob(image);
1644 return(MagickFalse);
1648 case RLECompression:
1655 Allocate pixel array.
1657 length=(size_t) number_pixels;
1658 pixel_info=AcquireVirtualMemory(length,sizeof(*pixels));
1659 if (pixel_info == (MemoryInfo *) NULL)
1660 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1661 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
1663 Dump Runlength encoded pixels.
1666 for (y=0; y < (ssize_t) image->rows; y++)
1668 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1669 if (p == (const Quantum *) NULL)
1671 for (x=0; x < (ssize_t) image->columns; x++)
1673 *q++=ScaleQuantumToChar(ClampToQuantum(GetPixelLuma(image,p)));
1674 p+=GetPixelChannels(image);
1676 if (image->previous == (Image *) NULL)
1678 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1680 if (status == MagickFalse)
1684 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1685 if (compression == ZipCompression)
1686 status=ZLIBEncodeImage(image,length,pixels,exception);
1689 if (compression == LZWCompression)
1690 status=LZWEncodeImage(image,length,pixels,exception);
1692 status=PackbitsEncodeImage(image,length,pixels,exception);
1693 pixel_info=RelinquishVirtualMemory(pixel_info);
1694 if (status == MagickFalse)
1696 (void) CloseBlob(image);
1697 return(MagickFalse);
1704 Dump uncompressed PseudoColor packets.
1706 Ascii85Initialize(image);
1707 for (y=0; y < (ssize_t) image->rows; y++)
1709 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1710 if (p == (const Quantum *) NULL)
1712 for (x=0; x < (ssize_t) image->columns; x++)
1714 Ascii85Encode(image,ScaleQuantumToChar(ClampToQuantum(
1715 GetPixelLuma(image,p))));
1716 p+=GetPixelChannels(image);
1718 if (image->previous == (Image *) NULL)
1720 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1722 if (status == MagickFalse)
1726 Ascii85Flush(image);
1732 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1733 (compression == JPEGCompression) ||
1734 (compression == JPEG2000Compression))
1735 switch (compression)
1737 case JPEGCompression:
1739 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
1740 if (status == MagickFalse)
1742 (void) CloseBlob(image);
1743 return(MagickFalse);
1747 case JPEG2000Compression:
1749 status=InjectImageBlob(image_info,image,image,"jp2",exception);
1750 if (status == MagickFalse)
1752 (void) CloseBlob(image);
1753 return(MagickFalse);
1757 case RLECompression:
1764 Allocate pixel array.
1766 length=(size_t) number_pixels;
1767 length*=image->colorspace == CMYKColorspace ? 4UL : 3UL;
1768 pixel_info=AcquireVirtualMemory(length,sizeof(*pixels));
1769 if (pixel_info == (MemoryInfo *) NULL)
1770 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1771 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
1773 Dump runoffset encoded pixels.
1776 for (y=0; y < (ssize_t) image->rows; y++)
1778 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1779 if (p == (const Quantum *) NULL)
1781 for (x=0; x < (ssize_t) image->columns; x++)
1783 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1784 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1785 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1786 if (image->colorspace == CMYKColorspace)
1787 *q++=ScaleQuantumToChar(GetPixelBlack(image,p));
1788 p+=GetPixelChannels(image);
1790 if (image->previous == (Image *) NULL)
1792 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1794 if (status == MagickFalse)
1798 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1799 if (compression == ZipCompression)
1800 status=ZLIBEncodeImage(image,length,pixels,exception);
1803 if (compression == LZWCompression)
1804 status=LZWEncodeImage(image,length,pixels,exception);
1806 status=PackbitsEncodeImage(image,length,pixels,exception);
1807 pixel_info=RelinquishVirtualMemory(pixel_info);
1808 if (status == MagickFalse)
1810 (void) CloseBlob(image);
1811 return(MagickFalse);
1818 Dump uncompressed DirectColor packets.
1820 Ascii85Initialize(image);
1821 for (y=0; y < (ssize_t) image->rows; y++)
1823 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1824 if (p == (const Quantum *) NULL)
1826 for (x=0; x < (ssize_t) image->columns; x++)
1828 Ascii85Encode(image,ScaleQuantumToChar(GetPixelRed(image,p)));
1829 Ascii85Encode(image,ScaleQuantumToChar(GetPixelGreen(image,p)));
1830 Ascii85Encode(image,ScaleQuantumToChar(GetPixelBlue(image,p)));
1831 if (image->colorspace == CMYKColorspace)
1832 Ascii85Encode(image,ScaleQuantumToChar(
1833 GetPixelBlack(image,p)));
1834 p+=GetPixelChannels(image);
1836 if (image->previous == (Image *) NULL)
1838 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1840 if (status == MagickFalse)
1844 Ascii85Flush(image);
1851 Dump number of colors and colormap.
1853 switch (compression)
1855 case RLECompression:
1862 Allocate pixel array.
1864 length=(size_t) number_pixels;
1865 pixel_info=AcquireVirtualMemory(length,sizeof(*pixels));
1866 if (pixel_info == (MemoryInfo *) NULL)
1867 ThrowWriterException(ResourceLimitError,
1868 "MemoryAllocationFailed");
1869 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
1871 Dump Runlength encoded pixels.
1874 for (y=0; y < (ssize_t) image->rows; y++)
1876 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1877 if (p == (const Quantum *) NULL)
1879 for (x=0; x < (ssize_t) image->columns; x++)
1881 *q++=(unsigned char) GetPixelIndex(image,p);
1882 p+=GetPixelChannels(image);
1884 if (image->previous == (Image *) NULL)
1886 status=SetImageProgress(image,SaveImageTag,
1887 (MagickOffsetType) y,image->rows);
1888 if (status == MagickFalse)
1892 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1893 if (compression == ZipCompression)
1894 status=ZLIBEncodeImage(image,length,pixels,exception);
1897 if (compression == LZWCompression)
1898 status=LZWEncodeImage(image,length,pixels,exception);
1900 status=PackbitsEncodeImage(image,length,pixels,exception);
1901 pixel_info=RelinquishVirtualMemory(pixel_info);
1902 if (status == MagickFalse)
1904 (void) CloseBlob(image);
1905 return(MagickFalse);
1912 Dump uncompressed PseudoColor packets.
1914 Ascii85Initialize(image);
1915 for (y=0; y < (ssize_t) image->rows; y++)
1917 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1918 if (p == (const Quantum *) NULL)
1920 for (x=0; x < (ssize_t) image->columns; x++)
1922 Ascii85Encode(image,(unsigned char) GetPixelIndex(image,p));
1923 p+=GetPixelChannels(image);
1925 if (image->previous == (Image *) NULL)
1927 status=SetImageProgress(image,SaveImageTag,
1928 (MagickOffsetType) y,image->rows);
1929 if (status == MagickFalse)
1933 Ascii85Flush(image);
1938 offset=TellBlob(image)-offset;
1939 (void) WriteBlobString(image,"\nendstream\n");
1940 (void) WriteBlobString(image,"endobj\n");
1942 Write Length object.
1944 xref[object++]=TellBlob(image);
1945 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1947 (void) WriteBlobString(image,buffer);
1948 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
1949 (void) WriteBlobString(image,buffer);
1950 (void) WriteBlobString(image,"endobj\n");
1952 Write Colorspace object.
1954 xref[object++]=TellBlob(image);
1955 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1957 (void) WriteBlobString(image,buffer);
1958 if (image->colorspace == CMYKColorspace)
1959 (void) CopyMagickString(buffer,"/DeviceCMYK\n",MaxTextExtent);
1961 if ((compression == FaxCompression) ||
1962 (compression == Group4Compression) ||
1963 ((image_info->type != TrueColorType) &&
1964 (IsImageGray(image,exception) != MagickFalse)))
1965 (void) CopyMagickString(buffer,"/DeviceGray\n",MaxTextExtent);
1967 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1968 (compression == JPEGCompression) ||
1969 (compression == JPEG2000Compression))
1970 (void) CopyMagickString(buffer,"/DeviceRGB\n",MaxTextExtent);
1972 (void) FormatLocaleString(buffer,MaxTextExtent,
1973 "[ /Indexed /DeviceRGB %.20g %.20g 0 R ]\n",(double) image->colors-
1974 1,(double) object+3);
1975 (void) WriteBlobString(image,buffer);
1976 (void) WriteBlobString(image,"endobj\n");
1980 SetGeometry(image,&geometry);
1981 (void) ParseMetaGeometry("106x106+0+0>",&geometry.x,&geometry.y,
1982 &geometry.width,&geometry.height);
1983 tile_image=ThumbnailImage(image,geometry.width,geometry.height,exception);
1984 if (tile_image == (Image *) NULL)
1985 return(MagickFalse);
1986 xref[object++]=TellBlob(image);
1987 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1989 (void) WriteBlobString(image,buffer);
1990 (void) WriteBlobString(image,"<<\n");
1991 switch (compression)
1995 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"ASCII85Decode");
1998 case JPEGCompression:
2000 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"DCTDecode");
2001 if (image->colorspace != CMYKColorspace)
2003 (void) WriteBlobString(image,buffer);
2004 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
2008 case JPEG2000Compression:
2010 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"JPXDecode");
2011 if (image->colorspace != CMYKColorspace)
2013 (void) WriteBlobString(image,buffer);
2014 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
2018 case LZWCompression:
2020 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"LZWDecode");
2023 case ZipCompression:
2025 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"FlateDecode");
2028 case FaxCompression:
2029 case Group4Compression:
2031 (void) CopyMagickString(buffer,"/Filter [ /CCITTFaxDecode ]\n",
2033 (void) WriteBlobString(image,buffer);
2034 (void) FormatLocaleString(buffer,MaxTextExtent,"/DecodeParms [ << "
2035 "/K %s /BlackIs1 false /Columns %.20g /Rows %.20g >> ]\n",CCITTParam,
2036 (double) tile_image->columns,(double) tile_image->rows);
2041 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
2046 (void) WriteBlobString(image,buffer);
2047 (void) FormatLocaleString(buffer,MaxTextExtent,"/Width %.20g\n",(double)
2048 tile_image->columns);
2049 (void) WriteBlobString(image,buffer);
2050 (void) FormatLocaleString(buffer,MaxTextExtent,"/Height %.20g\n",(double)
2052 (void) WriteBlobString(image,buffer);
2053 (void) FormatLocaleString(buffer,MaxTextExtent,"/ColorSpace %.20g 0 R\n",
2055 (void) WriteBlobString(image,buffer);
2056 (void) FormatLocaleString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
2057 (compression == FaxCompression) || (compression == Group4Compression) ?
2059 (void) WriteBlobString(image,buffer);
2060 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
2062 (void) WriteBlobString(image,buffer);
2063 (void) WriteBlobString(image,">>\n");
2064 (void) WriteBlobString(image,"stream\n");
2065 offset=TellBlob(image);
2066 number_pixels=(MagickSizeType) tile_image->columns*tile_image->rows;
2067 if ((compression == FaxCompression) ||
2068 (compression == Group4Compression) ||
2069 ((image_info->type != TrueColorType) &&
2070 (IsImageGray(tile_image,exception) != MagickFalse)))
2072 switch (compression)
2074 case FaxCompression:
2075 case Group4Compression:
2077 if (LocaleCompare(CCITTParam,"0") == 0)
2079 (void) HuffmanEncodeImage(image_info,image,tile_image,
2083 (void) Huffman2DEncodeImage(image_info,image,tile_image,exception);
2086 case JPEGCompression:
2088 status=InjectImageBlob(image_info,image,tile_image,"jpeg",
2090 if (status == MagickFalse)
2092 (void) CloseBlob(image);
2093 return(MagickFalse);
2097 case JPEG2000Compression:
2099 status=InjectImageBlob(image_info,image,tile_image,"jp2",exception);
2100 if (status == MagickFalse)
2102 (void) CloseBlob(image);
2103 return(MagickFalse);
2107 case RLECompression:
2114 Allocate pixel array.
2116 length=(size_t) number_pixels;
2117 pixel_info=AcquireVirtualMemory(length,sizeof(*pixels));
2118 if (pixel_info == (MemoryInfo *) NULL)
2120 tile_image=DestroyImage(tile_image);
2121 ThrowWriterException(ResourceLimitError,
2122 "MemoryAllocationFailed");
2124 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
2126 Dump runlength encoded pixels.
2129 for (y=0; y < (ssize_t) tile_image->rows; y++)
2131 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2133 if (p == (const Quantum *) NULL)
2135 for (x=0; x < (ssize_t) tile_image->columns; x++)
2137 *q++=ScaleQuantumToChar(ClampToQuantum(GetPixelLuma(
2139 p+=GetPixelChannels(tile_image);
2142 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2143 if (compression == ZipCompression)
2144 status=ZLIBEncodeImage(image,length,pixels,exception);
2147 if (compression == LZWCompression)
2148 status=LZWEncodeImage(image,length,pixels,exception);
2150 status=PackbitsEncodeImage(image,length,pixels,exception);
2151 pixel_info=RelinquishVirtualMemory(pixel_info);
2152 if (status == MagickFalse)
2154 (void) CloseBlob(image);
2155 return(MagickFalse);
2162 Dump uncompressed PseudoColor packets.
2164 Ascii85Initialize(image);
2165 for (y=0; y < (ssize_t) tile_image->rows; y++)
2167 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2169 if (p == (const Quantum *) NULL)
2171 for (x=0; x < (ssize_t) tile_image->columns; x++)
2173 Ascii85Encode(image,ScaleQuantumToChar(ClampToQuantum(
2174 GetPixelLuma(tile_image,p))));
2175 p+=GetPixelChannels(tile_image);
2178 Ascii85Flush(image);
2184 if ((tile_image->storage_class == DirectClass) ||
2185 (tile_image->colors > 256) || (compression == JPEGCompression) ||
2186 (compression == JPEG2000Compression))
2187 switch (compression)
2189 case JPEGCompression:
2191 status=InjectImageBlob(image_info,image,tile_image,"jpeg",
2193 if (status == MagickFalse)
2195 (void) CloseBlob(image);
2196 return(MagickFalse);
2200 case JPEG2000Compression:
2202 status=InjectImageBlob(image_info,image,tile_image,"jp2",exception);
2203 if (status == MagickFalse)
2205 (void) CloseBlob(image);
2206 return(MagickFalse);
2210 case RLECompression:
2217 Allocate pixel array.
2219 length=(size_t) number_pixels;
2220 length*=tile_image->colorspace == CMYKColorspace ? 4UL : 3UL;
2221 pixel_info=AcquireVirtualMemory(length,4*sizeof(*pixels));
2222 if (pixel_info == (MemoryInfo *) NULL)
2224 tile_image=DestroyImage(tile_image);
2225 ThrowWriterException(ResourceLimitError,
2226 "MemoryAllocationFailed");
2228 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
2230 Dump runlength encoded pixels.
2233 for (y=0; y < (ssize_t) tile_image->rows; y++)
2235 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2237 if (p == (const Quantum *) NULL)
2239 for (x=0; x < (ssize_t) tile_image->columns; x++)
2241 *q++=ScaleQuantumToChar(GetPixelRed(tile_image,p));
2242 *q++=ScaleQuantumToChar(GetPixelGreen(tile_image,p));
2243 *q++=ScaleQuantumToChar(GetPixelBlue(tile_image,p));
2244 if (image->colorspace == CMYKColorspace)
2245 *q++=ScaleQuantumToChar(GetPixelBlack(tile_image,p));
2246 p+=GetPixelChannels(tile_image);
2249 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2250 if (compression == ZipCompression)
2251 status=ZLIBEncodeImage(image,length,pixels,exception);
2254 if (compression == LZWCompression)
2255 status=LZWEncodeImage(image,length,pixels,exception);
2257 status=PackbitsEncodeImage(image,length,pixels,exception);
2258 pixel_info=RelinquishVirtualMemory(pixel_info);
2259 if (status == MagickFalse)
2261 (void) CloseBlob(image);
2262 return(MagickFalse);
2269 Dump uncompressed DirectColor packets.
2271 Ascii85Initialize(image);
2272 for (y=0; y < (ssize_t) tile_image->rows; y++)
2274 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2276 if (p == (const Quantum *) NULL)
2278 for (x=0; x < (ssize_t) tile_image->columns; x++)
2280 Ascii85Encode(image,ScaleQuantumToChar(
2281 GetPixelRed(tile_image,p)));
2282 Ascii85Encode(image,ScaleQuantumToChar(
2283 GetPixelGreen(tile_image,p)));
2284 Ascii85Encode(image,ScaleQuantumToChar(
2285 GetPixelBlue(tile_image,p)));
2286 if (image->colorspace == CMYKColorspace)
2287 Ascii85Encode(image,ScaleQuantumToChar(
2288 GetPixelBlack(tile_image,p)));
2289 p+=GetPixelChannels(tile_image);
2292 Ascii85Flush(image);
2299 Dump number of colors and colormap.
2301 switch (compression)
2303 case RLECompression:
2310 Allocate pixel array.
2312 length=(size_t) number_pixels;
2313 pixel_info=AcquireVirtualMemory(length,sizeof(*pixels));
2314 if (pixel_info == (MemoryInfo *) NULL)
2316 tile_image=DestroyImage(tile_image);
2317 ThrowWriterException(ResourceLimitError,
2318 "MemoryAllocationFailed");
2320 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
2322 Dump runlength encoded pixels.
2325 for (y=0; y < (ssize_t) tile_image->rows; y++)
2327 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2329 if (p == (const Quantum *) NULL)
2331 for (x=0; x < (ssize_t) tile_image->columns; x++)
2333 *q++=(unsigned char) GetPixelIndex(tile_image,p);
2334 p+=GetPixelChannels(tile_image);
2337 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2338 if (compression == ZipCompression)
2339 status=ZLIBEncodeImage(image,length,pixels,exception);
2342 if (compression == LZWCompression)
2343 status=LZWEncodeImage(image,length,pixels,exception);
2345 status=PackbitsEncodeImage(image,length,pixels,exception);
2346 pixel_info=RelinquishVirtualMemory(pixel_info);
2347 if (status == MagickFalse)
2349 (void) CloseBlob(image);
2350 return(MagickFalse);
2357 Dump uncompressed PseudoColor packets.
2359 Ascii85Initialize(image);
2360 for (y=0; y < (ssize_t) tile_image->rows; y++)
2362 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2364 if (p == (const Quantum *) NULL)
2366 for (x=0; x < (ssize_t) tile_image->columns; x++)
2368 Ascii85Encode(image,(unsigned char)
2369 GetPixelIndex(tile_image,p));
2370 p+=GetPixelChannels(image);
2373 Ascii85Flush(image);
2378 tile_image=DestroyImage(tile_image);
2379 offset=TellBlob(image)-offset;
2380 (void) WriteBlobString(image,"\nendstream\n");
2381 (void) WriteBlobString(image,"endobj\n");
2383 Write Length object.
2385 xref[object++]=TellBlob(image);
2386 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2388 (void) WriteBlobString(image,buffer);
2389 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
2390 (void) WriteBlobString(image,buffer);
2391 (void) WriteBlobString(image,"endobj\n");
2392 xref[object++]=TellBlob(image);
2393 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2395 (void) WriteBlobString(image,buffer);
2396 (void) WriteBlobString(image,"<<\n");
2397 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
2398 (compression == FaxCompression) || (compression == Group4Compression))
2399 (void) WriteBlobString(image,">>\n");
2403 Write Colormap object.
2405 if (compression == NoCompression)
2406 (void) WriteBlobString(image,"/Filter [ /ASCII85Decode ]\n");
2407 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
2409 (void) WriteBlobString(image,buffer);
2410 (void) WriteBlobString(image,">>\n");
2411 (void) WriteBlobString(image,"stream\n");
2412 offset=TellBlob(image);
2413 if (compression == NoCompression)
2414 Ascii85Initialize(image);
2415 for (i=0; i < (ssize_t) image->colors; i++)
2417 if (compression == NoCompression)
2419 Ascii85Encode(image,ScaleQuantumToChar(ClampToQuantum(
2420 image->colormap[i].red)));
2421 Ascii85Encode(image,ScaleQuantumToChar(ClampToQuantum(
2422 image->colormap[i].green)));
2423 Ascii85Encode(image,ScaleQuantumToChar(ClampToQuantum(
2424 image->colormap[i].blue)));
2427 (void) WriteBlobByte(image,ScaleQuantumToChar(
2428 ClampToQuantum(image->colormap[i].red)));
2429 (void) WriteBlobByte(image,ScaleQuantumToChar(
2430 ClampToQuantum(image->colormap[i].green)));
2431 (void) WriteBlobByte(image,ScaleQuantumToChar(
2432 ClampToQuantum(image->colormap[i].blue)));
2434 if (compression == NoCompression)
2435 Ascii85Flush(image);
2436 offset=TellBlob(image)-offset;
2437 (void) WriteBlobString(image,"\nendstream\n");
2439 (void) WriteBlobString(image,"endobj\n");
2441 Write Length object.
2443 xref[object++]=TellBlob(image);
2444 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2446 (void) WriteBlobString(image,buffer);
2447 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
2449 (void) WriteBlobString(image,buffer);
2450 (void) WriteBlobString(image,"endobj\n");
2452 Write softmask object.
2454 xref[object++]=TellBlob(image);
2455 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2457 (void) WriteBlobString(image,buffer);
2458 (void) WriteBlobString(image,"<<\n");
2459 if (image->alpha_trait != BlendPixelTrait)
2460 (void) WriteBlobString(image,">>\n");
2463 (void) WriteBlobString(image,"/Type /XObject\n");
2464 (void) WriteBlobString(image,"/Subtype /Image\n");
2465 (void) FormatLocaleString(buffer,MaxTextExtent,"/Name /Ma%.20g\n",
2466 (double) image->scene);
2467 (void) WriteBlobString(image,buffer);
2468 switch (compression)
2472 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
2476 case LZWCompression:
2478 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"LZWDecode");
2481 case ZipCompression:
2483 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
2489 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
2494 (void) WriteBlobString(image,buffer);
2495 (void) FormatLocaleString(buffer,MaxTextExtent,"/Width %.20g\n",(double)
2497 (void) WriteBlobString(image,buffer);
2498 (void) FormatLocaleString(buffer,MaxTextExtent,"/Height %.20g\n",
2499 (double) image->rows);
2500 (void) WriteBlobString(image,buffer);
2501 (void) WriteBlobString(image,"/ColorSpace /DeviceGray\n");
2502 (void) FormatLocaleString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
2503 (compression == FaxCompression) || (compression == Group4Compression)
2505 (void) WriteBlobString(image,buffer);
2506 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
2508 (void) WriteBlobString(image,buffer);
2509 (void) WriteBlobString(image,">>\n");
2510 (void) WriteBlobString(image,"stream\n");
2511 offset=TellBlob(image);
2512 number_pixels=(MagickSizeType) image->columns*image->rows;
2513 switch (compression)
2515 case RLECompression:
2522 Allocate pixel array.
2524 length=(size_t) number_pixels;
2525 pixel_info=AcquireVirtualMemory(length,4*sizeof(*pixels));
2526 if (pixel_info == (MemoryInfo *) NULL)
2528 image=DestroyImage(image);
2529 ThrowWriterException(ResourceLimitError,
2530 "MemoryAllocationFailed");
2532 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
2534 Dump Runlength encoded pixels.
2537 for (y=0; y < (ssize_t) image->rows; y++)
2539 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2540 if (p == (const Quantum *) NULL)
2542 for (x=0; x < (ssize_t) image->columns; x++)
2544 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
2545 p+=GetPixelChannels(image);
2548 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2549 if (compression == ZipCompression)
2550 status=ZLIBEncodeImage(image,length,pixels,exception);
2553 if (compression == LZWCompression)
2554 status=LZWEncodeImage(image,length,pixels,exception);
2556 status=PackbitsEncodeImage(image,length,pixels,exception);
2557 pixel_info=RelinquishVirtualMemory(pixel_info);
2558 if (status == MagickFalse)
2560 (void) CloseBlob(image);
2561 return(MagickFalse);
2568 Dump uncompressed PseudoColor packets.
2570 Ascii85Initialize(image);
2571 for (y=0; y < (ssize_t) image->rows; y++)
2573 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2574 if (p == (const Quantum *) NULL)
2576 for (x=0; x < (ssize_t) image->columns; x++)
2578 Ascii85Encode(image,ScaleQuantumToChar(GetPixelAlpha(image,p)));
2579 p+=GetPixelChannels(image);
2582 Ascii85Flush(image);
2586 offset=TellBlob(image)-offset;
2587 (void) WriteBlobString(image,"\nendstream\n");
2589 (void) WriteBlobString(image,"endobj\n");
2591 Write Length object.
2593 xref[object++]=TellBlob(image);
2594 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2596 (void) WriteBlobString(image,buffer);
2597 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
2598 (void) WriteBlobString(image,buffer);
2599 (void) WriteBlobString(image,"endobj\n");
2600 if (GetNextImageInList(image) == (Image *) NULL)
2602 image=SyncNextImageInList(image);
2603 status=SetImageProgress(image,SaveImagesTag,scene++,
2604 GetImageListLength(image));
2605 if (status == MagickFalse)
2607 } while (image_info->adjoin != MagickFalse);
2609 Write Metadata object.
2611 xref[object++]=TellBlob(image);
2613 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2615 (void) WriteBlobString(image,buffer);
2616 (void) WriteBlobString(image,"<<\n");
2617 (void) FormatLocaleString(buffer,MaxTextExtent,"/Title (%s)\n",
2618 EscapeParenthesis(basename));
2619 (void) WriteBlobString(image,buffer);
2620 seconds=time((time_t *) NULL);
2621 #if defined(MAGICKCORE_HAVE_LOCALTIME_R)
2622 (void) localtime_r(&seconds,&local_time);
2624 (void) memcpy(&local_time,localtime(&seconds),sizeof(local_time));
2626 (void) FormatLocaleString(date,MaxTextExtent,"D:%04d%02d%02d%02d%02d%02d",
2627 local_time.tm_year+1900,local_time.tm_mon+1,local_time.tm_mday,
2628 local_time.tm_hour,local_time.tm_min,local_time.tm_sec);
2629 (void) FormatLocaleString(buffer,MaxTextExtent,"/CreationDate (%s)\n",date);
2630 (void) WriteBlobString(image,buffer);
2631 (void) FormatLocaleString(buffer,MaxTextExtent,"/ModDate (%s)\n",date);
2632 (void) WriteBlobString(image,buffer);
2633 (void) FormatLocaleString(buffer,MaxTextExtent,"/Producer (%s)\n",
2634 EscapeParenthesis(GetMagickVersion((size_t *) NULL)));
2635 (void) WriteBlobString(image,buffer);
2636 (void) WriteBlobString(image,">>\n");
2637 (void) WriteBlobString(image,"endobj\n");
2641 offset=TellBlob(image)-xref[0]+
2642 (LocaleCompare(image_info->magick,"PDFA") == 0 ? 6 : 0)+10;
2643 (void) WriteBlobString(image,"xref\n");
2644 (void) FormatLocaleString(buffer,MaxTextExtent,"0 %.20g\n",(double)
2646 (void) WriteBlobString(image,buffer);
2647 (void) WriteBlobString(image,"0000000000 65535 f \n");
2648 for (i=0; i < (ssize_t) object; i++)
2650 (void) FormatLocaleString(buffer,MaxTextExtent,"%010lu 00000 n \n",
2651 (unsigned long) xref[i]);
2652 (void) WriteBlobString(image,buffer);
2654 (void) WriteBlobString(image,"trailer\n");
2655 (void) WriteBlobString(image,"<<\n");
2656 (void) FormatLocaleString(buffer,MaxTextExtent,"/Size %.20g\n",(double)
2658 (void) WriteBlobString(image,buffer);
2659 (void) FormatLocaleString(buffer,MaxTextExtent,"/Info %.20g 0 R\n",(double)
2661 (void) WriteBlobString(image,buffer);
2662 (void) FormatLocaleString(buffer,MaxTextExtent,"/Root %.20g 0 R\n",(double)
2664 (void) WriteBlobString(image,buffer);
2665 (void) SignatureImage(image,exception);
2666 (void) FormatLocaleString(buffer,MaxTextExtent,"/ID [<%s> <%s>]\n",
2667 GetImageProperty(image,"signature",exception),
2668 GetImageProperty(image,"signature",exception));
2669 (void) WriteBlobString(image,buffer);
2670 (void) WriteBlobString(image,">>\n");
2671 (void) WriteBlobString(image,"startxref\n");
2672 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
2673 (void) WriteBlobString(image,buffer);
2674 (void) WriteBlobString(image,"%%EOF\n");
2675 xref=(MagickOffsetType *) RelinquishMagickMemory(xref);
2676 (void) CloseBlob(image);