2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Portable Document Format %
20 % Copyright 1999-2012 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/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/color.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/colorspace.h"
50 #include "MagickCore/colorspace-private.h"
51 #include "MagickCore/compress.h"
52 #include "MagickCore/constitute.h"
53 #include "MagickCore/delegate.h"
54 #include "MagickCore/delegate-private.h"
55 #include "MagickCore/draw.h"
56 #include "MagickCore/exception.h"
57 #include "MagickCore/exception-private.h"
58 #include "MagickCore/geometry.h"
59 #include "MagickCore/image.h"
60 #include "MagickCore/image-private.h"
61 #include "MagickCore/list.h"
62 #include "MagickCore/magick.h"
63 #include "MagickCore/memory_.h"
64 #include "MagickCore/monitor.h"
65 #include "MagickCore/monitor-private.h"
66 #include "MagickCore/option.h"
67 #include "MagickCore/pixel-accessor.h"
68 #include "MagickCore/profile.h"
69 #include "MagickCore/property.h"
70 #include "MagickCore/quantum-private.h"
71 #include "MagickCore/resource_.h"
72 #include "MagickCore/resize.h"
73 #include "MagickCore/signature.h"
74 #include "MagickCore/static.h"
75 #include "MagickCore/string_.h"
76 #include "MagickCore/module.h"
77 #include "MagickCore/token.h"
78 #include "MagickCore/transform.h"
79 #include "MagickCore/utility.h"
80 #include "MagickCore/module.h"
85 #if defined(MAGICKCORE_TIFF_DELEGATE)
86 #define CCITTParam "-1"
88 #define CCITTParam "0"
94 static MagickBooleanType
95 WritePDFImage(const ImageInfo *,Image *,ExceptionInfo *);
98 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102 % I n v o k e P D F D e l e g a t e %
106 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108 % InvokePDFDelegate() executes the PDF interpreter with the specified command.
110 % The format of the InvokePDFDelegate method is:
112 % MagickBooleanType InvokePDFDelegate(const MagickBooleanType verbose,
113 % const char *command,ExceptionInfo *exception)
115 % A description of each parameter follows:
117 % o verbose: A value other than zero displays the command prior to
120 % o command: the address of a character string containing the command to
123 % o exception: return any errors or warnings in this structure.
126 static MagickBooleanType InvokePDFDelegate(const MagickBooleanType verbose,
127 const char *command,ExceptionInfo *exception)
132 #if defined(MAGICKCORE_GS_DELEGATE) || defined(MAGICKCORE_WINDOWS_SUPPORT)
149 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
150 ghost_info=NTGhostscriptDLLVectors();
155 ghost_info=(&ghost_info_struct);
156 (void) ResetMagickMemory(&ghost_info,0,sizeof(ghost_info));
157 ghost_info_struct.new_instance=(int (*)(gs_main_instance **,void *))
159 ghost_info_struct.init_with_args=(int (*)(gs_main_instance *,int,char **))
160 gsapi_init_with_args;
161 ghost_info_struct.run_string=(int (*)(gs_main_instance *,const char *,int,
162 int *)) gsapi_run_string;
163 ghost_info_struct.delete_instance=(void (*)(gs_main_instance *))
164 gsapi_delete_instance;
165 ghost_info_struct.exit=(int (*)(gs_main_instance *)) gsapi_exit;
167 if (ghost_info == (GhostInfo *) NULL)
169 status=SystemCommand(MagickFalse,verbose,command,exception);
170 return(status == 0 ? MagickTrue : MagickFalse);
172 if (verbose != MagickFalse)
174 (void) fputs("[ghostscript library]",stdout);
175 (void) fputs(strchr(command,' '),stdout);
177 status=(ghost_info->new_instance)(&interpreter,(void *) NULL);
180 status=SystemCommand(MagickFalse,verbose,command,exception);
181 return(status == 0 ? MagickTrue : MagickFalse);
184 argv=StringToArgv(command,&argc);
185 status=(ghost_info->init_with_args)(interpreter,argc-1,argv+1);
187 status=(ghost_info->run_string)(interpreter,"systemdict /start get exec\n",
189 (ghost_info->exit)(interpreter);
190 (ghost_info->delete_instance)(interpreter);
191 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
192 NTGhostscriptUnLoadDLL();
194 for (i=0; i < (ssize_t) argc; i++)
195 argv[i]=DestroyString(argv[i]);
196 argv=(char **) RelinquishMagickMemory(argv);
197 if ((status != 0) && (status != -101))
202 message=GetExceptionMessage(errno);
203 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
204 "`%s': %s",command,message);
205 message=DestroyString(message);
206 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
207 "Ghostscript returns status %d, exit code %d",status,code);
212 status=SystemCommand(MagickFalse,verbose,command,exception);
213 return(status == 0 ? MagickTrue : MagickFalse);
218 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
226 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
228 % IsPDF() returns MagickTrue if the image format type, identified by the
229 % magick string, is PDF.
231 % The format of the IsPDF method is:
233 % MagickBooleanType IsPDF(const unsigned char *magick,const size_t offset)
235 % A description of each parameter follows:
237 % o magick: compare image format pattern against these bytes.
239 % o offset: Specifies the offset of the magick string.
242 static MagickBooleanType IsPDF(const unsigned char *magick,const size_t offset)
246 if (LocaleNCompare((const char *) magick,"%PDF-",5) == 0)
252 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
256 % R e a d P D F I m a g e %
260 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
262 % ReadPDFImage() reads a Portable Document Format image file and
263 % returns it. It allocates the memory necessary for the new Image structure
264 % and returns a pointer to the new image.
266 % The format of the ReadPDFImage method is:
268 % Image *ReadPDFImage(const ImageInfo *image_info,ExceptionInfo *exception)
270 % A description of each parameter follows:
272 % o image_info: the image info.
274 % o exception: return any errors or warnings in this structure.
278 static MagickBooleanType IsPDFRendered(const char *path)
286 if ((path == (const char *) NULL) || (*path == '\0'))
288 status=GetPathAttributes(path,&attributes);
289 if ((status != MagickFalse) && S_ISREG(attributes.st_mode) &&
290 (attributes.st_size > 0))
295 static Image *ReadPDFImage(const ImageInfo *image_info,ExceptionInfo *exception)
297 #define CMYKProcessColor "CMYKProcessColor"
298 #define CropBox "CropBox"
299 #define DefaultCMYK "DefaultCMYK"
300 #define DeviceCMYK "DeviceCMYK"
301 #define MediaBox "MediaBox"
302 #define RenderPostscriptText "Rendering Postscript... "
303 #define PDFRotate "Rotate"
304 #define SpotColor "Separation"
305 #define TrimBox "TrimBox"
306 #define PDFVersion "PDF-"
309 command[MaxTextExtent],
310 density[MaxTextExtent],
311 filename[MaxTextExtent],
312 geometry[MaxTextExtent],
313 options[MaxTextExtent],
314 input_filename[MaxTextExtent],
315 postscript_filename[MaxTextExtent];
374 assert(image_info != (const ImageInfo *) NULL);
375 assert(image_info->signature == MagickSignature);
376 if (image_info->debug != MagickFalse)
377 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
378 image_info->filename);
379 assert(exception != (ExceptionInfo *) NULL);
380 assert(exception->signature == MagickSignature);
384 image=AcquireImage(image_info,exception);
385 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
386 if (status == MagickFalse)
388 image=DestroyImageList(image);
389 return((Image *) NULL);
391 status=AcquireUniqueSymbolicLink(image_info->filename,input_filename);
392 if (status == MagickFalse)
394 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
395 image_info->filename);
396 image=DestroyImageList(image);
397 return((Image *) NULL);
400 Set the page density.
402 delta.x=DefaultResolution;
403 delta.y=DefaultResolution;
404 if ((image->resolution.x == 0.0) || (image->resolution.y == 0.0))
406 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
407 image->resolution.x=geometry_info.rho;
408 image->resolution.y=geometry_info.sigma;
409 if ((flags & SigmaValue) == 0)
410 image->resolution.y=image->resolution.x;
412 if (image_info->density != (char *) NULL)
414 flags=ParseGeometry(image_info->density,&geometry_info);
415 image->resolution.x=geometry_info.rho;
416 image->resolution.y=geometry_info.sigma;
417 if ((flags & SigmaValue) == 0)
418 image->resolution.y=image->resolution.x;
420 (void) ParseAbsoluteGeometry(PSPageGeometry,&page);
421 if (image_info->page != (char *) NULL)
422 (void) ParseAbsoluteGeometry(image_info->page,&page);
423 page.width=(size_t) ceil((double) (page.width*image->resolution.x/delta.x)-
425 page.height=(size_t) ceil((double) (page.height*image->resolution.y/delta.y)-
428 Determine page geometry from the PDF media box.
430 cmyk=image_info->colorspace == CMYKColorspace ? MagickTrue : MagickFalse;
431 cropbox=IsStringTrue(GetImageOption(image_info,"pdf:use-cropbox"));
432 trimbox=IsStringTrue(GetImageOption(image_info,"pdf:use-trimbox"));
435 (void) ResetMagickMemory(&bounding_box,0,sizeof(bounding_box));
436 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
437 (void) ResetMagickMemory(&hires_bounds,0,sizeof(hires_bounds));
438 (void) ResetMagickMemory(command,0,sizeof(command));
441 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
449 if ((c != (int) '/') && (c != (int) '%') &&
450 ((size_t) (p-command) < (MaxTextExtent-1)))
454 if (LocaleNCompare(PDFRotate,command,strlen(PDFRotate)) == 0)
455 count=(ssize_t) sscanf(command,"Rotate %lf",&angle);
457 Is this a CMYK document?
459 if (LocaleNCompare(DefaultCMYK,command,strlen(DefaultCMYK)) == 0)
461 if (LocaleNCompare(DeviceCMYK,command,strlen(DeviceCMYK)) == 0)
463 if (LocaleNCompare(CMYKProcessColor,command,strlen(CMYKProcessColor)) == 0)
465 if (LocaleNCompare(SpotColor,command,strlen(SpotColor)) == 0)
469 property[MaxTextExtent],
478 (void) FormatLocaleString(property,MaxTextExtent,"pdf:SpotColor-%.20g",
479 (double) spotcolor++);
481 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
483 if ((isspace(c) != 0) || (c == '/') || ((i+1) == MaxTextExtent))
488 value=AcquireString(name);
489 (void) SubstituteString(&value,"#20"," ");
490 (void) SetImageProperty(image,property,value,exception);
491 value=DestroyString(value);
494 if (LocaleNCompare(PDFVersion,command,strlen(PDFVersion)) == 0)
495 (void) SetImageProperty(image,"pdf:Version",command,exception);
496 if (image_info->page != (char *) NULL)
499 if (cropbox != MagickFalse)
501 if (LocaleNCompare(CropBox,command,strlen(CropBox)) == 0)
504 Note region defined by crop box.
506 count=(ssize_t) sscanf(command,"CropBox [%lf %lf %lf %lf",
507 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
509 count=(ssize_t) sscanf(command,"CropBox[%lf %lf %lf %lf",
510 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
514 if (trimbox != MagickFalse)
516 if (LocaleNCompare(TrimBox,command,strlen(TrimBox)) == 0)
519 Note region defined by trim box.
521 count=(ssize_t) sscanf(command,"TrimBox [%lf %lf %lf %lf",
522 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
524 count=(ssize_t) sscanf(command,"TrimBox[%lf %lf %lf %lf",
525 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
529 if (LocaleNCompare(MediaBox,command,strlen(MediaBox)) == 0)
532 Note region defined by media box.
534 count=(ssize_t) sscanf(command,"MediaBox [%lf %lf %lf %lf",
535 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
537 count=(ssize_t) sscanf(command,"MediaBox[%lf %lf %lf %lf",
538 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
542 if ((fabs(bounds.x2-bounds.x1) <= fabs(hires_bounds.x2-hires_bounds.x1)) ||
543 (fabs(bounds.y2-bounds.y1) <= fabs(hires_bounds.y2-hires_bounds.y1)))
547 if ((fabs(hires_bounds.x2-hires_bounds.x1) >= MagickEpsilon) &&
548 (fabs(hires_bounds.y2-hires_bounds.y1) >= MagickEpsilon))
551 Set PDF render geometry.
553 (void) FormatLocaleString(geometry,MaxTextExtent,"%gx%g%+.15g%+.15g",
554 hires_bounds.x2-bounds.x1,hires_bounds.y2-hires_bounds.y1,
555 hires_bounds.x1,hires_bounds.y1);
556 (void) SetImageProperty(image,"pdf:HiResBoundingBox",geometry,exception);
557 page.width=(size_t) ceil((double) ((hires_bounds.x2-hires_bounds.x1)*
558 image->resolution.x/delta.x)-0.5);
559 page.height=(size_t) ceil((double) ((hires_bounds.y2-hires_bounds.y1)*
560 image->resolution.y/delta.y)-0.5);
562 (void) CloseBlob(image);
563 if ((fabs(angle) == 90.0) || (fabs(angle) == 270.0))
569 page.width=page.height;
572 if (IssRGBCompatibleColorspace(image_info->colorspace) != MagickFalse)
575 Create Ghostscript control file.
577 file=AcquireUniqueFileResource(postscript_filename);
580 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
581 image_info->filename);
582 image=DestroyImage(image);
583 return((Image *) NULL);
585 count=write(file," ",1);
588 Render Postscript with the Ghostscript delegate.
590 if ((image_info->ping != MagickFalse) ||
591 (image_info->monochrome != MagickFalse))
592 delegate_info=GetDelegateInfo("ps:mono",(char *) NULL,exception);
594 if (cmyk != MagickFalse)
595 delegate_info=GetDelegateInfo("ps:cmyk",(char *) NULL,exception);
597 delegate_info=GetDelegateInfo("ps:alpha",(char *) NULL,exception);
598 if (delegate_info == (const DelegateInfo *) NULL)
600 (void) RelinquishUniqueFileResource(postscript_filename);
601 image=DestroyImage(image);
602 return((Image *) NULL);
605 (void) FormatLocaleString(density,MaxTextExtent,"%gx%g",image->resolution.x,
606 image->resolution.y);
607 if (image_info->page != (char *) NULL)
608 (void) FormatLocaleString(options,MaxTextExtent,"-g%.20gx%.20g ",(double)
609 page.width,(double) page.height);
610 if (cmyk != MagickFalse)
611 (void) ConcatenateMagickString(options,"-dUseCIEColor ",MaxTextExtent);
612 if (cropbox != MagickFalse)
613 (void) ConcatenateMagickString(options,"-dUseCropBox ",MaxTextExtent);
614 if (trimbox != MagickFalse)
615 (void) ConcatenateMagickString(options,"-dUseTrimBox ",MaxTextExtent);
616 read_info=CloneImageInfo(image_info);
617 *read_info->magick='\0';
618 if (read_info->number_scenes != 0)
621 pages[MaxTextExtent];
623 (void) FormatLocaleString(pages,MaxTextExtent,"-dFirstPage=%.20g "
624 "-dLastPage=%.20g",(double) read_info->scene+1,(double)
625 (read_info->scene+read_info->number_scenes));
626 (void) ConcatenateMagickString(options,pages,MaxTextExtent);
627 read_info->number_scenes=0;
628 if (read_info->scenes != (char *) NULL)
629 *read_info->scenes='\0';
631 option=GetImageOption(read_info,"authenticate");
632 if (option != (const char *) NULL)
633 (void) FormatLocaleString(options+strlen(options),MaxTextExtent,
634 " -sPCLPassword=%s",option);
635 (void) CopyMagickString(filename,read_info->filename,MaxTextExtent);
636 (void) AcquireUniqueFilename(filename);
637 (void) FormatLocaleString(command,MaxTextExtent,
638 GetDelegateCommands(delegate_info),
639 read_info->antialias != MagickFalse ? 4 : 1,
640 read_info->antialias != MagickFalse ? 4 : 1,density,options,filename,
641 postscript_filename,input_filename);
642 status=InvokePDFDelegate(read_info->verbose,command,exception);
643 (void) RelinquishUniqueFileResource(postscript_filename);
644 (void) RelinquishUniqueFileResource(input_filename);
645 pdf_image=(Image *) NULL;
646 if (status == MagickFalse)
649 (void) InterpretImageFilename(image_info,image,filename,(int) i,
650 read_info->filename,exception);
651 if (IsPDFRendered(read_info->filename) == MagickFalse)
653 (void) RelinquishUniqueFileResource(read_info->filename);
658 (void) InterpretImageFilename(image_info,image,filename,(int) i,
659 read_info->filename,exception);
660 if (IsPDFRendered(read_info->filename) == MagickFalse)
662 read_info->blob=NULL;
664 next=ReadImage(read_info,exception);
665 (void) RelinquishUniqueFileResource(read_info->filename);
666 if (next == (Image *) NULL)
668 AppendImageToList(&pdf_image,next);
670 read_info=DestroyImageInfo(read_info);
671 if (pdf_image == (Image *) NULL)
673 ThrowFileException(exception,DelegateError,"PostscriptDelegateFailed",
674 image_info->filename);
675 image=DestroyImage(image);
676 return((Image *) NULL);
678 if (LocaleCompare(pdf_image->magick,"BMP") == 0)
683 cmyk_image=ConsolidateCMYKImages(pdf_image,exception);
684 if (cmyk_image != (Image *) NULL)
686 pdf_image=DestroyImageList(pdf_image);
687 pdf_image=cmyk_image;
690 if (image_info->number_scenes != 0)
699 Add place holder images to meet the subimage specification requirement.
701 for (i=0; i < (ssize_t) image_info->scene; i++)
703 clone_image=CloneImage(pdf_image,1,1,MagickTrue,exception);
704 if (clone_image != (Image *) NULL)
705 PrependImageToList(&pdf_image,clone_image);
710 (void) CopyMagickString(pdf_image->filename,filename,MaxTextExtent);
711 pdf_image->page=page;
712 (void) CloneImageProfiles(pdf_image,image);
713 (void) CloneImageProperties(pdf_image,image);
714 next=SyncNextImageInList(pdf_image);
715 if (next != (Image *) NULL)
717 } while (next != (Image *) NULL);
718 image=DestroyImage(image);
720 for (next=GetFirstImageInList(pdf_image); next != (Image *) NULL; )
723 next=GetNextImageInList(next);
725 return(GetFirstImageInList(pdf_image));
729 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
733 % R e g i s t e r P D F I m a g e %
737 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
739 % RegisterPDFImage() adds properties for the PDF image format to
740 % the list of supported formats. The properties include the image format
741 % tag, a method to read and/or write the format, whether the format
742 % supports the saving of more than one frame to the same file or blob,
743 % whether the format supports native in-memory I/O, and a brief
744 % description of the format.
746 % The format of the RegisterPDFImage method is:
748 % size_t RegisterPDFImage(void)
751 ModuleExport size_t RegisterPDFImage(void)
756 entry=SetMagickInfo("AI");
757 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
758 entry->encoder=(EncodeImageHandler *) WritePDFImage;
759 entry->adjoin=MagickFalse;
760 entry->blob_support=MagickFalse;
761 entry->seekable_stream=MagickTrue;
762 entry->thread_support=EncoderThreadSupport;
763 entry->description=ConstantString("Adobe Illustrator CS2");
764 entry->module=ConstantString("PDF");
765 (void) RegisterMagickInfo(entry);
766 entry=SetMagickInfo("EPDF");
767 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
768 entry->encoder=(EncodeImageHandler *) WritePDFImage;
769 entry->adjoin=MagickFalse;
770 entry->blob_support=MagickFalse;
771 entry->seekable_stream=MagickTrue;
772 entry->thread_support=EncoderThreadSupport;
773 entry->description=ConstantString("Encapsulated Portable Document Format");
774 entry->module=ConstantString("PDF");
775 (void) RegisterMagickInfo(entry);
776 entry=SetMagickInfo("PDF");
777 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
778 entry->encoder=(EncodeImageHandler *) WritePDFImage;
779 entry->magick=(IsImageFormatHandler *) IsPDF;
780 entry->blob_support=MagickFalse;
781 entry->seekable_stream=MagickTrue;
782 entry->thread_support=EncoderThreadSupport;
783 entry->description=ConstantString("Portable Document Format");
784 entry->module=ConstantString("PDF");
785 (void) RegisterMagickInfo(entry);
786 entry=SetMagickInfo("PDFA");
787 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
788 entry->encoder=(EncodeImageHandler *) WritePDFImage;
789 entry->magick=(IsImageFormatHandler *) IsPDF;
790 entry->blob_support=MagickFalse;
791 entry->seekable_stream=MagickTrue;
792 entry->thread_support=EncoderThreadSupport;
793 entry->description=ConstantString("Portable Document Archive Format");
794 entry->module=ConstantString("PDF");
795 (void) RegisterMagickInfo(entry);
796 return(MagickImageCoderSignature);
800 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
804 % U n r e g i s t e r P D F I m a g e %
808 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
810 % UnregisterPDFImage() removes format registrations made by the
811 % PDF module from the list of supported formats.
813 % The format of the UnregisterPDFImage method is:
815 % UnregisterPDFImage(void)
818 ModuleExport void UnregisterPDFImage(void)
820 (void) UnregisterMagickInfo("AI");
821 (void) UnregisterMagickInfo("EPDF");
822 (void) UnregisterMagickInfo("PDF");
823 (void) UnregisterMagickInfo("PDFA");
827 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
831 % W r i t e P D F I m a g e %
835 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
837 % WritePDFImage() writes an image in the Portable Document image
840 % The format of the WritePDFImage method is:
842 % MagickBooleanType WritePDFImage(const ImageInfo *image_info,
843 % Image *image,ExceptionInfo *exception)
845 % A description of each parameter follows.
847 % o image_info: the image info.
849 % o image: The image.
851 % o exception: return any errors or warnings in this structure.
855 static inline size_t MagickMax(const size_t x,const size_t y)
862 static inline size_t MagickMin(const size_t x,const size_t y)
869 static char *EscapeParenthesis(const char *text)
881 buffer[MaxTextExtent];
885 for (i=0; i < (ssize_t) MagickMin(strlen(text),(MaxTextExtent-escapes-1)); i++)
887 if ((text[i] == '(') || (text[i] == ')'))
898 static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
899 Image *image,Image *inject_image,ExceptionInfo *exception)
917 write_info=CloneImageInfo(image_info);
918 (void) CopyMagickString(write_info->filename,"GROUP4:",MaxTextExtent);
919 (void) CopyMagickString(write_info->magick,"GROUP4",MaxTextExtent);
920 group4_image=CloneImage(inject_image,0,0,MagickTrue,exception);
921 if (group4_image == (Image *) NULL)
923 group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
925 group4_image=DestroyImage(group4_image);
926 if (group4 == (unsigned char *) NULL)
928 write_info=DestroyImageInfo(write_info);
929 if (WriteBlob(image,length,group4) != (ssize_t) length)
931 group4=(unsigned char *) RelinquishMagickMemory(group4);
935 static MagickBooleanType WritePDFImage(const ImageInfo *image_info,Image *image,
936 ExceptionInfo *exception)
938 #define CFormat "/Filter [ /%s ]\n"
939 #define ObjectsPerImage 14
944 "<?xpacket begin=\"%s\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n"
945 "<x:xmpmeta xmlns:x=\"adobe:ns:meta/\" x:xmptk=\"Adobe XMP Core 4.0-c316 44.253921, Sun Oct 01 2006 17:08:23\">\n"
946 " <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n"
947 " <rdf:Description rdf:about=\"\"\n"
948 " xmlns:xap=\"http://ns.adobe.com/xap/1.0/\">\n"
949 " <xap:ModifyDate>%s</xap:ModifyDate>\n"
950 " <xap:CreateDate>%s</xap:CreateDate>\n"
951 " <xap:MetadataDate>%s</xap:MetadataDate>\n"
952 " <xap:CreatorTool>%s</xap:CreatorTool>\n"
953 " </rdf:Description>\n"
954 " <rdf:Description rdf:about=\"\"\n"
955 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"
956 " <dc:format>application/pdf</dc:format>\n"
959 " <rdf:li xml:lang=\"x-default\">%s</rdf:li>\n"
962 " </rdf:Description>\n"
963 " <rdf:Description rdf:about=\"\"\n"
964 " xmlns:xapMM=\"http://ns.adobe.com/xap/1.0/mm/\">\n"
965 " <xapMM:DocumentID>uuid:6ec119d7-7982-4f56-808d-dfe64f5b35cf</xapMM:DocumentID>\n"
966 " <xapMM:InstanceID>uuid:a79b99b4-6235-447f-9f6c-ec18ef7555cb</xapMM:InstanceID>\n"
967 " </rdf:Description>\n"
968 " <rdf:Description rdf:about=\"\"\n"
969 " xmlns:pdf=\"http://ns.adobe.com/pdf/1.3/\">\n"
970 " <pdf:Producer>%s</pdf:Producer>\n"
971 " </rdf:Description>\n"
972 " <rdf:Description rdf:about=\"\"\n"
973 " xmlns:pdfaid=\"http://www.aiim.org/pdfa/ns/id/\">\n"
974 " <pdfaid:part>2</pdfaid:part>\n"
975 " <pdfaid:conformance>B</pdfaid:conformance>\n"
976 " </rdf:Description>\n"
979 "<?xpacket end=\"w\"?>\n"
981 XMPProfileMagick[4]= { (char) 0xef, (char) 0xbb, (char) 0xbf, (char) 0x00 };
984 basename[MaxTextExtent],
985 buffer[MaxTextExtent],
988 page_geometry[MaxTextExtent];
1030 register const Quantum
1033 register unsigned char
1063 Open output image file.
1065 assert(image_info != (const ImageInfo *) NULL);
1066 assert(image_info->signature == MagickSignature);
1067 assert(image != (Image *) NULL);
1068 assert(image->signature == MagickSignature);
1069 if (image->debug != MagickFalse)
1070 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1071 assert(exception != (ExceptionInfo *) NULL);
1072 assert(exception->signature == MagickSignature);
1073 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1074 if (status == MagickFalse)
1077 Allocate X ref memory.
1079 xref=(MagickOffsetType *) AcquireQuantumMemory(2048UL,sizeof(*xref));
1080 if (xref == (MagickOffsetType *) NULL)
1081 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1082 (void) ResetMagickMemory(xref,0,2048UL*sizeof(*xref));
1088 if (image_info->compression == JPEG2000Compression)
1089 version=(size_t) MagickMax(version,5);
1090 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
1091 if (next->alpha_trait == BlendPixelTrait)
1092 version=(size_t) MagickMax(version,4);
1093 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1094 version=(size_t) MagickMax(version,6);
1095 (void) FormatLocaleString(buffer,MaxTextExtent,"%%PDF-1.%.20g \n",(double)
1097 (void) WriteBlobString(image,buffer);
1098 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1099 (void) WriteBlobString(image,"%âãÏÓ\n");
1101 Write Catalog object.
1103 xref[object++]=TellBlob(image);
1105 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1107 (void) WriteBlobString(image,buffer);
1108 (void) WriteBlobString(image,"<<\n");
1109 if (LocaleCompare(image_info->magick,"PDFA") != 0)
1110 (void) FormatLocaleString(buffer,MaxTextExtent,"/Pages %.20g 0 R\n",(double)
1114 (void) FormatLocaleString(buffer,MaxTextExtent,"/Metadata %.20g 0 R\n",
1116 (void) WriteBlobString(image,buffer);
1117 (void) FormatLocaleString(buffer,MaxTextExtent,"/Pages %.20g 0 R\n",
1120 (void) WriteBlobString(image,buffer);
1121 (void) WriteBlobString(image,"/Type /Catalog\n");
1122 (void) WriteBlobString(image,">>\n");
1123 (void) WriteBlobString(image,"endobj\n");
1124 GetPathComponent(image->filename,BasePath,basename);
1125 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1128 create_date[MaxTextExtent],
1129 modify_date[MaxTextExtent],
1130 timestamp[MaxTextExtent],
1131 xmp_profile[MaxTextExtent];
1139 xref[object++]=TellBlob(image);
1140 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1142 (void) WriteBlobString(image,buffer);
1143 (void) WriteBlobString(image,"<<\n");
1144 (void) WriteBlobString(image,"/Subtype /XML\n");
1146 value=GetImageProperty(image,"date:modify",exception);
1147 if (value != (const char *) NULL)
1148 (void) CopyMagickString(modify_date,value,MaxTextExtent);
1150 value=GetImageProperty(image,"date:create",exception);
1151 if (value != (const char *) NULL)
1152 (void) CopyMagickString(create_date,value,MaxTextExtent);
1153 (void) FormatMagickTime(time((time_t *) NULL),MaxTextExtent,timestamp);
1154 i=FormatLocaleString(xmp_profile,MaxTextExtent,XMPProfile,
1155 XMPProfileMagick,modify_date,create_date,timestamp,
1156 GetMagickVersion(&version),EscapeParenthesis(basename),
1157 GetMagickVersion(&version));
1158 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g\n",(double)
1160 (void) WriteBlobString(image,buffer);
1161 (void) WriteBlobString(image,"/Type /Metadata\n");
1162 (void) WriteBlobString(image,">>\nstream\n");
1163 (void) WriteBlobString(image,xmp_profile);
1164 (void) WriteBlobString(image,"\nendstream\n");
1165 (void) WriteBlobString(image,"endobj\n");
1170 xref[object++]=TellBlob(image);
1172 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1174 (void) WriteBlobString(image,buffer);
1175 (void) WriteBlobString(image,"<<\n");
1176 (void) WriteBlobString(image,"/Type /Pages\n");
1177 (void) FormatLocaleString(buffer,MaxTextExtent,"/Kids [ %.20g 0 R ",(double)
1179 (void) WriteBlobString(image,buffer);
1180 count=(ssize_t) (pages_id+ObjectsPerImage+1);
1181 if (image_info->adjoin != MagickFalse)
1187 Predict page object id's.
1190 for ( ; GetNextImageInList(kid_image) != (Image *) NULL; count+=ObjectsPerImage)
1192 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 R ",(double)
1194 (void) WriteBlobString(image,buffer);
1195 kid_image=GetNextImageInList(kid_image);
1197 xref=(MagickOffsetType *) ResizeQuantumMemory(xref,(size_t) count+2048UL,
1199 if (xref == (MagickOffsetType *) NULL)
1200 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1202 (void) WriteBlobString(image,"]\n");
1203 (void) FormatLocaleString(buffer,MaxTextExtent,"/Count %.20g\n",(double)
1204 ((count-pages_id)/ObjectsPerImage));
1205 (void) WriteBlobString(image,buffer);
1206 (void) WriteBlobString(image,">>\n");
1207 (void) WriteBlobString(image,"endobj\n");
1211 compression=image->compression;
1212 if (image_info->compression != UndefinedCompression)
1213 compression=image_info->compression;
1214 switch (compression)
1216 case FaxCompression:
1217 case Group4Compression:
1219 if ((IsImageMonochrome(image,exception) == MagickFalse) ||
1220 (image->alpha_trait == BlendPixelTrait))
1221 compression=RLECompression;
1224 #if !defined(MAGICKCORE_JPEG_DELEGATE)
1225 case JPEGCompression:
1227 compression=RLECompression;
1228 (void) ThrowMagickException(exception,GetMagickModule(),
1229 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
1234 #if !defined(MAGICKCORE_JP2_DELEGATE)
1235 case JPEG2000Compression:
1237 compression=RLECompression;
1238 (void) ThrowMagickException(exception,GetMagickModule(),
1239 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JP2)",
1244 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
1245 case ZipCompression:
1247 compression=RLECompression;
1248 (void) ThrowMagickException(exception,GetMagickModule(),
1249 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZLIB)",
1254 case LZWCompression:
1256 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1257 compression=RLECompression; /* LZW compression is forbidden */
1262 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1263 compression=RLECompression; /* ASCII 85 compression is forbidden */
1269 if (compression == JPEG2000Compression)
1271 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
1272 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1275 Scale relative to dots-per-inch.
1277 delta.x=DefaultResolution;
1278 delta.y=DefaultResolution;
1279 resolution.x=image->resolution.x;
1280 resolution.y=image->resolution.y;
1281 if ((resolution.x == 0.0) || (resolution.y == 0.0))
1283 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
1284 resolution.x=geometry_info.rho;
1285 resolution.y=geometry_info.sigma;
1286 if ((flags & SigmaValue) == 0)
1287 resolution.y=resolution.x;
1289 if (image_info->density != (char *) NULL)
1291 flags=ParseGeometry(image_info->density,&geometry_info);
1292 resolution.x=geometry_info.rho;
1293 resolution.y=geometry_info.sigma;
1294 if ((flags & SigmaValue) == 0)
1295 resolution.y=resolution.x;
1297 if (image->units == PixelsPerCentimeterResolution)
1299 resolution.x=(double) ((size_t) (100.0*2.54*resolution.x+0.5)/100.0);
1300 resolution.y=(double) ((size_t) (100.0*2.54*resolution.y+0.5)/100.0);
1302 SetGeometry(image,&geometry);
1303 (void) FormatLocaleString(page_geometry,MaxTextExtent,"%.20gx%.20g",(double)
1304 image->columns,(double) image->rows);
1305 if (image_info->page != (char *) NULL)
1306 (void) CopyMagickString(page_geometry,image_info->page,MaxTextExtent);
1308 if ((image->page.width != 0) && (image->page.height != 0))
1309 (void) FormatLocaleString(page_geometry,MaxTextExtent,
1310 "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,(double)
1311 image->page.height,(double) image->page.x,(double) image->page.y);
1313 if ((image->gravity != UndefinedGravity) &&
1314 (LocaleCompare(image_info->magick,"PDF") == 0))
1315 (void) CopyMagickString(page_geometry,PSPageGeometry,MaxTextExtent);
1316 (void) ConcatenateMagickString(page_geometry,">",MaxTextExtent);
1317 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
1318 &geometry.width,&geometry.height);
1319 scale.x=(double) (geometry.width*delta.x)/resolution.x;
1320 geometry.width=(size_t) floor(scale.x+0.5);
1321 scale.y=(double) (geometry.height*delta.y)/resolution.y;
1322 geometry.height=(size_t) floor(scale.y+0.5);
1323 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
1324 (void) ParseGravityGeometry(image,page_geometry,&page_info,exception);
1325 if (image->gravity != UndefinedGravity)
1327 geometry.x=(-page_info.x);
1328 geometry.y=(ssize_t) (media_info.height+page_info.y-image->rows);
1331 if (image_info->pointsize != 0.0)
1332 pointsize=image_info->pointsize;
1334 value=GetImageProperty(image,"label",exception);
1335 if (value != (const char *) NULL)
1336 text_size=(size_t) (MultilineCensus(value)*pointsize+12);
1341 xref[object++]=TellBlob(image);
1342 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1344 (void) WriteBlobString(image,buffer);
1345 (void) WriteBlobString(image,"<<\n");
1346 (void) WriteBlobString(image,"/Type /Page\n");
1347 (void) FormatLocaleString(buffer,MaxTextExtent,"/Parent %.20g 0 R\n",
1349 (void) WriteBlobString(image,buffer);
1350 (void) WriteBlobString(image,"/Resources <<\n");
1351 labels=(char **) NULL;
1352 value=GetImageProperty(image,"label",exception);
1353 if (value != (const char *) NULL)
1354 labels=StringToList(value);
1355 if (labels != (char **) NULL)
1357 (void) FormatLocaleString(buffer,MaxTextExtent,
1358 "/Font << /F%.20g %.20g 0 R >>\n",(double) image->scene,(double)
1360 (void) WriteBlobString(image,buffer);
1362 (void) FormatLocaleString(buffer,MaxTextExtent,
1363 "/XObject << /Im%.20g %.20g 0 R >>\n",(double) image->scene,(double)
1365 (void) WriteBlobString(image,buffer);
1366 (void) FormatLocaleString(buffer,MaxTextExtent,"/ProcSet %.20g 0 R >>\n",
1368 (void) WriteBlobString(image,buffer);
1369 (void) FormatLocaleString(buffer,MaxTextExtent,
1370 "/MediaBox [0 0 %g %g]\n",72.0*media_info.width/resolution.x,
1371 72.0*media_info.height/resolution.y);
1372 (void) WriteBlobString(image,buffer);
1373 (void) FormatLocaleString(buffer,MaxTextExtent,
1374 "/CropBox [0 0 %g %g]\n",72.0*media_info.width/resolution.x,
1375 72.0*media_info.height/resolution.y);
1376 (void) WriteBlobString(image,buffer);
1377 (void) FormatLocaleString(buffer,MaxTextExtent,"/Contents %.20g 0 R\n",
1379 (void) WriteBlobString(image,buffer);
1380 (void) FormatLocaleString(buffer,MaxTextExtent,"/Thumb %.20g 0 R\n",(double)
1382 (void) WriteBlobString(image,buffer);
1383 (void) WriteBlobString(image,">>\n");
1384 (void) WriteBlobString(image,"endobj\n");
1386 Write Contents object.
1388 xref[object++]=TellBlob(image);
1389 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1391 (void) WriteBlobString(image,buffer);
1392 (void) WriteBlobString(image,"<<\n");
1393 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
1395 (void) WriteBlobString(image,buffer);
1396 (void) WriteBlobString(image,">>\n");
1397 (void) WriteBlobString(image,"stream\n");
1398 offset=TellBlob(image);
1399 (void) WriteBlobString(image,"q\n");
1400 if (labels != (char **) NULL)
1401 for (i=0; labels[i] != (char *) NULL; i++)
1403 (void) WriteBlobString(image,"BT\n");
1404 (void) FormatLocaleString(buffer,MaxTextExtent,"/F%.20g %g Tf\n",
1405 (double) image->scene,pointsize);
1406 (void) WriteBlobString(image,buffer);
1407 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g Td\n",
1408 (double) geometry.x,(double) (geometry.y+geometry.height+i*pointsize+
1410 (void) WriteBlobString(image,buffer);
1411 (void) FormatLocaleString(buffer,MaxTextExtent,"(%s) Tj\n",labels[i]);
1412 (void) WriteBlobString(image,buffer);
1413 (void) WriteBlobString(image,"ET\n");
1414 labels[i]=DestroyString(labels[i]);
1416 (void) FormatLocaleString(buffer,MaxTextExtent,"%g 0 0 %g %.20g %.20g cm\n",
1417 scale.x,scale.y,(double) geometry.x,(double) geometry.y);
1418 (void) WriteBlobString(image,buffer);
1419 (void) FormatLocaleString(buffer,MaxTextExtent,"/Im%.20g Do\n",(double)
1421 (void) WriteBlobString(image,buffer);
1422 (void) WriteBlobString(image,"Q\n");
1423 offset=TellBlob(image)-offset;
1424 (void) WriteBlobString(image,"\nendstream\n");
1425 (void) WriteBlobString(image,"endobj\n");
1427 Write Length object.
1429 xref[object++]=TellBlob(image);
1430 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1432 (void) WriteBlobString(image,buffer);
1433 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
1434 (void) WriteBlobString(image,buffer);
1435 (void) WriteBlobString(image,"endobj\n");
1437 Write Procset object.
1439 xref[object++]=TellBlob(image);
1440 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1442 (void) WriteBlobString(image,buffer);
1443 if ((image->storage_class == DirectClass) || (image->colors > 256))
1444 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageC",MaxTextExtent);
1446 if ((compression == FaxCompression) || (compression == Group4Compression))
1447 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageB",MaxTextExtent);
1449 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageI",MaxTextExtent);
1450 (void) WriteBlobString(image,buffer);
1451 (void) WriteBlobString(image," ]\n");
1452 (void) WriteBlobString(image,"endobj\n");
1456 xref[object++]=TellBlob(image);
1457 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1459 (void) WriteBlobString(image,buffer);
1460 (void) WriteBlobString(image,"<<\n");
1461 if (labels != (char **) NULL)
1463 (void) WriteBlobString(image,"/Type /Font\n");
1464 (void) WriteBlobString(image,"/Subtype /Type1\n");
1465 (void) FormatLocaleString(buffer,MaxTextExtent,"/Name /F%.20g\n",
1466 (double) image->scene);
1467 (void) WriteBlobString(image,buffer);
1468 (void) WriteBlobString(image,"/BaseFont /Helvetica\n");
1469 (void) WriteBlobString(image,"/Encoding /MacRomanEncoding\n");
1470 labels=(char **) RelinquishMagickMemory(labels);
1472 (void) WriteBlobString(image,">>\n");
1473 (void) WriteBlobString(image,"endobj\n");
1475 Write XObject object.
1477 xref[object++]=TellBlob(image);
1478 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1480 (void) WriteBlobString(image,buffer);
1481 (void) WriteBlobString(image,"<<\n");
1482 (void) WriteBlobString(image,"/Type /XObject\n");
1483 (void) WriteBlobString(image,"/Subtype /Image\n");
1484 (void) FormatLocaleString(buffer,MaxTextExtent,"/Name /Im%.20g\n",(double)
1486 (void) WriteBlobString(image,buffer);
1487 switch (compression)
1491 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"ASCII85Decode");
1494 case JPEGCompression:
1496 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"DCTDecode");
1497 if (image->colorspace != CMYKColorspace)
1499 (void) WriteBlobString(image,buffer);
1500 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1504 case JPEG2000Compression:
1506 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"JPXDecode");
1507 if (image->colorspace != CMYKColorspace)
1509 (void) WriteBlobString(image,buffer);
1510 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1514 case LZWCompression:
1516 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"LZWDecode");
1519 case ZipCompression:
1521 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"FlateDecode");
1524 case FaxCompression:
1525 case Group4Compression:
1527 (void) CopyMagickString(buffer,"/Filter [ /CCITTFaxDecode ]\n",
1529 (void) WriteBlobString(image,buffer);
1530 (void) FormatLocaleString(buffer,MaxTextExtent,"/DecodeParms [ << "
1531 "/K %s /BlackIs1 false /Columns %.20g /Rows %.20g >> ]\n",CCITTParam,
1532 (double) image->columns,(double) image->rows);
1537 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
1542 (void) WriteBlobString(image,buffer);
1543 (void) FormatLocaleString(buffer,MaxTextExtent,"/Width %.20g\n",(double)
1545 (void) WriteBlobString(image,buffer);
1546 (void) FormatLocaleString(buffer,MaxTextExtent,"/Height %.20g\n",(double)
1548 (void) WriteBlobString(image,buffer);
1549 (void) FormatLocaleString(buffer,MaxTextExtent,"/ColorSpace %.20g 0 R\n",
1551 (void) WriteBlobString(image,buffer);
1552 (void) FormatLocaleString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
1553 (compression == FaxCompression) || (compression == Group4Compression) ?
1555 (void) WriteBlobString(image,buffer);
1556 if (image->alpha_trait == BlendPixelTrait)
1558 (void) FormatLocaleString(buffer,MaxTextExtent,"/SMask %.20g 0 R\n",
1560 (void) WriteBlobString(image,buffer);
1562 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
1564 (void) WriteBlobString(image,buffer);
1565 (void) WriteBlobString(image,">>\n");
1566 (void) WriteBlobString(image,"stream\n");
1567 offset=TellBlob(image);
1568 number_pixels=(MagickSizeType) image->columns*image->rows;
1569 if ((4*number_pixels) != (MagickSizeType) ((size_t) (4*number_pixels)))
1570 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1571 if ((compression == FaxCompression) || (compression == Group4Compression) ||
1572 ((image_info->type != TrueColorType) &&
1573 (IsImageGray(image,exception) != MagickFalse)))
1575 switch (compression)
1577 case FaxCompression:
1578 case Group4Compression:
1580 if (LocaleCompare(CCITTParam,"0") == 0)
1582 (void) HuffmanEncodeImage(image_info,image,image,exception);
1585 (void) Huffman2DEncodeImage(image_info,image,image,exception);
1588 case JPEGCompression:
1590 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
1591 if (status == MagickFalse)
1593 (void) CloseBlob(image);
1594 return(MagickFalse);
1598 case JPEG2000Compression:
1600 status=InjectImageBlob(image_info,image,image,"jp2",exception);
1601 if (status == MagickFalse)
1603 (void) CloseBlob(image);
1604 return(MagickFalse);
1608 case RLECompression:
1612 Allocate pixel array.
1614 length=(size_t) number_pixels;
1615 pixels=(unsigned char *) AcquireQuantumMemory(length,
1617 if (pixels == (unsigned char *) NULL)
1618 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1620 Dump Runlength encoded pixels.
1623 for (y=0; y < (ssize_t) image->rows; y++)
1625 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1626 if (p == (const Quantum *) NULL)
1628 for (x=0; x < (ssize_t) image->columns; x++)
1630 *q++=ScaleQuantumToChar(GetPixelIntensity(image,p));
1631 p+=GetPixelChannels(image);
1633 if (image->previous == (Image *) NULL)
1635 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1637 if (status == MagickFalse)
1641 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1642 if (compression == ZipCompression)
1643 status=ZLIBEncodeImage(image,length,pixels,exception);
1646 if (compression == LZWCompression)
1647 status=LZWEncodeImage(image,length,pixels,exception);
1649 status=PackbitsEncodeImage(image,length,pixels,exception);
1650 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1651 if (status == MagickFalse)
1653 (void) CloseBlob(image);
1654 return(MagickFalse);
1661 Dump uncompressed PseudoColor packets.
1663 Ascii85Initialize(image);
1664 for (y=0; y < (ssize_t) image->rows; y++)
1666 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1667 if (p == (const Quantum *) NULL)
1669 for (x=0; x < (ssize_t) image->columns; x++)
1671 Ascii85Encode(image,ScaleQuantumToChar(
1672 GetPixelIntensity(image,p)));
1673 p+=GetPixelChannels(image);
1675 if (image->previous == (Image *) NULL)
1677 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1679 if (status == MagickFalse)
1683 Ascii85Flush(image);
1689 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1690 (compression == JPEGCompression) ||
1691 (compression == JPEG2000Compression))
1692 switch (compression)
1694 case JPEGCompression:
1696 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
1697 if (status == MagickFalse)
1699 (void) CloseBlob(image);
1700 return(MagickFalse);
1704 case JPEG2000Compression:
1706 status=InjectImageBlob(image_info,image,image,"jp2",exception);
1707 if (status == MagickFalse)
1709 (void) CloseBlob(image);
1710 return(MagickFalse);
1714 case RLECompression:
1718 Allocate pixel array.
1720 length=(size_t) number_pixels;
1721 pixels=(unsigned char *) AcquireQuantumMemory(length,
1723 length*=image->colorspace == CMYKColorspace ? 4UL : 3UL;
1724 if (pixels == (unsigned char *) NULL)
1725 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1727 Dump runoffset encoded pixels.
1730 for (y=0; y < (ssize_t) image->rows; y++)
1732 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1733 if (p == (const Quantum *) NULL)
1735 for (x=0; x < (ssize_t) image->columns; x++)
1737 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1738 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1739 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1740 if (image->colorspace == CMYKColorspace)
1741 *q++=ScaleQuantumToChar(GetPixelBlack(image,p));
1742 p+=GetPixelChannels(image);
1744 if (image->previous == (Image *) NULL)
1746 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1748 if (status == MagickFalse)
1752 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1753 if (compression == ZipCompression)
1754 status=ZLIBEncodeImage(image,length,pixels,exception);
1757 if (compression == LZWCompression)
1758 status=LZWEncodeImage(image,length,pixels,exception);
1760 status=PackbitsEncodeImage(image,length,pixels,exception);
1761 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1762 if (status == MagickFalse)
1764 (void) CloseBlob(image);
1765 return(MagickFalse);
1772 Dump uncompressed DirectColor packets.
1774 Ascii85Initialize(image);
1775 for (y=0; y < (ssize_t) image->rows; y++)
1777 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1778 if (p == (const Quantum *) NULL)
1780 for (x=0; x < (ssize_t) image->columns; x++)
1782 Ascii85Encode(image,ScaleQuantumToChar(GetPixelRed(image,p)));
1783 Ascii85Encode(image,ScaleQuantumToChar(GetPixelGreen(image,p)));
1784 Ascii85Encode(image,ScaleQuantumToChar(GetPixelBlue(image,p)));
1785 if (image->colorspace == CMYKColorspace)
1786 Ascii85Encode(image,ScaleQuantumToChar(
1787 GetPixelBlack(image,p)));
1788 p+=GetPixelChannels(image);
1790 if (image->previous == (Image *) NULL)
1792 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1794 if (status == MagickFalse)
1798 Ascii85Flush(image);
1805 Dump number of colors and colormap.
1807 switch (compression)
1809 case RLECompression:
1813 Allocate pixel array.
1815 length=(size_t) number_pixels;
1816 pixels=(unsigned char *) AcquireQuantumMemory(length,
1818 if (pixels == (unsigned char *) NULL)
1819 ThrowWriterException(ResourceLimitError,
1820 "MemoryAllocationFailed");
1822 Dump Runlength encoded pixels.
1825 for (y=0; y < (ssize_t) image->rows; y++)
1827 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1828 if (p == (const Quantum *) NULL)
1830 for (x=0; x < (ssize_t) image->columns; x++)
1832 *q++=(unsigned char) GetPixelIndex(image,p);
1833 p+=GetPixelChannels(image);
1835 if (image->previous == (Image *) NULL)
1837 status=SetImageProgress(image,SaveImageTag,
1838 (MagickOffsetType) y,image->rows);
1839 if (status == MagickFalse)
1843 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1844 if (compression == ZipCompression)
1845 status=ZLIBEncodeImage(image,length,pixels,exception);
1848 if (compression == LZWCompression)
1849 status=LZWEncodeImage(image,length,pixels,exception);
1851 status=PackbitsEncodeImage(image,length,pixels,exception);
1852 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1853 if (status == MagickFalse)
1855 (void) CloseBlob(image);
1856 return(MagickFalse);
1863 Dump uncompressed PseudoColor packets.
1865 Ascii85Initialize(image);
1866 for (y=0; y < (ssize_t) image->rows; y++)
1868 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1869 if (p == (const Quantum *) NULL)
1871 for (x=0; x < (ssize_t) image->columns; x++)
1873 Ascii85Encode(image,(unsigned char) GetPixelIndex(image,p));
1874 p+=GetPixelChannels(image);
1876 if (image->previous == (Image *) NULL)
1878 status=SetImageProgress(image,SaveImageTag,
1879 (MagickOffsetType) y,image->rows);
1880 if (status == MagickFalse)
1884 Ascii85Flush(image);
1889 offset=TellBlob(image)-offset;
1890 (void) WriteBlobString(image,"\nendstream\n");
1891 (void) WriteBlobString(image,"endobj\n");
1893 Write Length object.
1895 xref[object++]=TellBlob(image);
1896 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1898 (void) WriteBlobString(image,buffer);
1899 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
1900 (void) WriteBlobString(image,buffer);
1901 (void) WriteBlobString(image,"endobj\n");
1903 Write Colorspace object.
1905 xref[object++]=TellBlob(image);
1906 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1908 (void) WriteBlobString(image,buffer);
1909 if (image->colorspace == CMYKColorspace)
1910 (void) CopyMagickString(buffer,"/DeviceCMYK\n",MaxTextExtent);
1912 if ((compression == FaxCompression) ||
1913 (compression == Group4Compression) ||
1914 ((image_info->type != TrueColorType) &&
1915 (IsImageGray(image,exception) != MagickFalse)))
1916 (void) CopyMagickString(buffer,"/DeviceGray\n",MaxTextExtent);
1918 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1919 (compression == JPEGCompression) ||
1920 (compression == JPEG2000Compression))
1921 (void) CopyMagickString(buffer,"/DeviceRGB\n",MaxTextExtent);
1923 (void) FormatLocaleString(buffer,MaxTextExtent,
1924 "[ /Indexed /DeviceRGB %.20g %.20g 0 R ]\n",(double) image->colors-
1925 1,(double) object+3);
1926 (void) WriteBlobString(image,buffer);
1927 (void) WriteBlobString(image,"endobj\n");
1931 SetGeometry(image,&geometry);
1932 (void) ParseMetaGeometry("106x106+0+0>",&geometry.x,&geometry.y,
1933 &geometry.width,&geometry.height);
1934 tile_image=ThumbnailImage(image,geometry.width,geometry.height,exception);
1935 if (tile_image == (Image *) NULL)
1936 return(MagickFalse);
1937 xref[object++]=TellBlob(image);
1938 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1940 (void) WriteBlobString(image,buffer);
1941 (void) WriteBlobString(image,"<<\n");
1942 switch (compression)
1946 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"ASCII85Decode");
1949 case JPEGCompression:
1951 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"DCTDecode");
1952 if (image->colorspace != CMYKColorspace)
1954 (void) WriteBlobString(image,buffer);
1955 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1959 case JPEG2000Compression:
1961 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"JPXDecode");
1962 if (image->colorspace != CMYKColorspace)
1964 (void) WriteBlobString(image,buffer);
1965 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1969 case LZWCompression:
1971 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"LZWDecode");
1974 case ZipCompression:
1976 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"FlateDecode");
1979 case FaxCompression:
1980 case Group4Compression:
1982 (void) CopyMagickString(buffer,"/Filter [ /CCITTFaxDecode ]\n",
1984 (void) WriteBlobString(image,buffer);
1985 (void) FormatLocaleString(buffer,MaxTextExtent,"/DecodeParms [ << "
1986 "/K %s /BlackIs1 false /Columns %.20g /Rows %.20g >> ]\n",CCITTParam,
1987 (double) tile_image->columns,(double) tile_image->rows);
1992 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
1997 (void) WriteBlobString(image,buffer);
1998 (void) FormatLocaleString(buffer,MaxTextExtent,"/Width %.20g\n",(double)
1999 tile_image->columns);
2000 (void) WriteBlobString(image,buffer);
2001 (void) FormatLocaleString(buffer,MaxTextExtent,"/Height %.20g\n",(double)
2003 (void) WriteBlobString(image,buffer);
2004 (void) FormatLocaleString(buffer,MaxTextExtent,"/ColorSpace %.20g 0 R\n",
2006 (void) WriteBlobString(image,buffer);
2007 (void) FormatLocaleString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
2008 (compression == FaxCompression) || (compression == Group4Compression) ?
2010 (void) WriteBlobString(image,buffer);
2011 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
2013 (void) WriteBlobString(image,buffer);
2014 (void) WriteBlobString(image,">>\n");
2015 (void) WriteBlobString(image,"stream\n");
2016 offset=TellBlob(image);
2017 number_pixels=(MagickSizeType) tile_image->columns*tile_image->rows;
2018 if ((compression == FaxCompression) ||
2019 (compression == Group4Compression) ||
2020 ((image_info->type != TrueColorType) &&
2021 (IsImageGray(tile_image,exception) != MagickFalse)))
2023 switch (compression)
2025 case FaxCompression:
2026 case Group4Compression:
2028 if (LocaleCompare(CCITTParam,"0") == 0)
2030 (void) HuffmanEncodeImage(image_info,image,tile_image,
2034 (void) Huffman2DEncodeImage(image_info,image,tile_image,exception);
2037 case JPEGCompression:
2039 status=InjectImageBlob(image_info,image,tile_image,"jpeg",
2041 if (status == MagickFalse)
2043 (void) CloseBlob(image);
2044 return(MagickFalse);
2048 case JPEG2000Compression:
2050 status=InjectImageBlob(image_info,image,tile_image,"jp2",exception);
2051 if (status == MagickFalse)
2053 (void) CloseBlob(image);
2054 return(MagickFalse);
2058 case RLECompression:
2062 Allocate pixel array.
2064 length=(size_t) number_pixels;
2065 pixels=(unsigned char *) AcquireQuantumMemory(length,
2067 if (pixels == (unsigned char *) NULL)
2069 tile_image=DestroyImage(tile_image);
2070 ThrowWriterException(ResourceLimitError,
2071 "MemoryAllocationFailed");
2074 Dump Runlength encoded pixels.
2077 for (y=0; y < (ssize_t) tile_image->rows; y++)
2079 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2081 if (p == (const Quantum *) NULL)
2083 for (x=0; x < (ssize_t) tile_image->columns; x++)
2085 *q++=ScaleQuantumToChar(GetPixelIntensity(tile_image,p));
2086 p+=GetPixelChannels(tile_image);
2089 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2090 if (compression == ZipCompression)
2091 status=ZLIBEncodeImage(image,length,pixels,exception);
2094 if (compression == LZWCompression)
2095 status=LZWEncodeImage(image,length,pixels,exception);
2097 status=PackbitsEncodeImage(image,length,pixels,exception);
2098 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2099 if (status == MagickFalse)
2101 (void) CloseBlob(image);
2102 return(MagickFalse);
2109 Dump uncompressed PseudoColor packets.
2111 Ascii85Initialize(image);
2112 for (y=0; y < (ssize_t) tile_image->rows; y++)
2114 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2116 if (p == (const Quantum *) NULL)
2118 for (x=0; x < (ssize_t) tile_image->columns; x++)
2120 Ascii85Encode(image,
2121 ScaleQuantumToChar(GetPixelIntensity(tile_image,p)));
2122 p+=GetPixelChannels(tile_image);
2125 Ascii85Flush(image);
2131 if ((tile_image->storage_class == DirectClass) ||
2132 (tile_image->colors > 256) || (compression == JPEGCompression) ||
2133 (compression == JPEG2000Compression))
2134 switch (compression)
2136 case JPEGCompression:
2138 status=InjectImageBlob(image_info,image,tile_image,"jpeg",
2140 if (status == MagickFalse)
2142 (void) CloseBlob(image);
2143 return(MagickFalse);
2147 case JPEG2000Compression:
2149 status=InjectImageBlob(image_info,image,tile_image,"jp2",exception);
2150 if (status == MagickFalse)
2152 (void) CloseBlob(image);
2153 return(MagickFalse);
2157 case RLECompression:
2161 Allocate pixel array.
2163 length=(size_t) number_pixels;
2164 pixels=(unsigned char *) AcquireQuantumMemory(length,4*
2166 length*=tile_image->colorspace == CMYKColorspace ? 4UL : 3UL;
2167 if (pixels == (unsigned char *) NULL)
2169 tile_image=DestroyImage(tile_image);
2170 ThrowWriterException(ResourceLimitError,
2171 "MemoryAllocationFailed");
2174 Dump runoffset encoded pixels.
2177 for (y=0; y < (ssize_t) tile_image->rows; y++)
2179 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2181 if (p == (const Quantum *) NULL)
2183 for (x=0; x < (ssize_t) tile_image->columns; x++)
2185 *q++=ScaleQuantumToChar(GetPixelRed(tile_image,p));
2186 *q++=ScaleQuantumToChar(GetPixelGreen(tile_image,p));
2187 *q++=ScaleQuantumToChar(GetPixelBlue(tile_image,p));
2188 if (image->colorspace == CMYKColorspace)
2189 *q++=ScaleQuantumToChar(GetPixelBlack(tile_image,p));
2190 p+=GetPixelChannels(tile_image);
2193 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2194 if (compression == ZipCompression)
2195 status=ZLIBEncodeImage(image,length,pixels,exception);
2198 if (compression == LZWCompression)
2199 status=LZWEncodeImage(image,length,pixels,exception);
2201 status=PackbitsEncodeImage(image,length,pixels,exception);
2202 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2203 if (status == MagickFalse)
2205 (void) CloseBlob(image);
2206 return(MagickFalse);
2213 Dump uncompressed DirectColor packets.
2215 Ascii85Initialize(image);
2216 for (y=0; y < (ssize_t) tile_image->rows; y++)
2218 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2220 if (p == (const Quantum *) NULL)
2222 for (x=0; x < (ssize_t) tile_image->columns; x++)
2224 Ascii85Encode(image,ScaleQuantumToChar(
2225 GetPixelRed(tile_image,p)));
2226 Ascii85Encode(image,ScaleQuantumToChar(
2227 GetPixelGreen(tile_image,p)));
2228 Ascii85Encode(image,ScaleQuantumToChar(
2229 GetPixelBlue(tile_image,p)));
2230 if (image->colorspace == CMYKColorspace)
2231 Ascii85Encode(image,ScaleQuantumToChar(
2232 GetPixelBlack(tile_image,p)));
2233 p+=GetPixelChannels(tile_image);
2236 Ascii85Flush(image);
2243 Dump number of colors and colormap.
2245 switch (compression)
2247 case RLECompression:
2251 Allocate pixel array.
2253 length=(size_t) number_pixels;
2254 pixels=(unsigned char *) AcquireQuantumMemory(length,
2256 if (pixels == (unsigned char *) NULL)
2258 tile_image=DestroyImage(tile_image);
2259 ThrowWriterException(ResourceLimitError,
2260 "MemoryAllocationFailed");
2263 Dump Runlength encoded pixels.
2266 for (y=0; y < (ssize_t) tile_image->rows; y++)
2268 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2270 if (p == (const Quantum *) NULL)
2272 for (x=0; x < (ssize_t) tile_image->columns; x++)
2274 *q++=(unsigned char) GetPixelIndex(tile_image,p);
2275 p+=GetPixelChannels(tile_image);
2278 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2279 if (compression == ZipCompression)
2280 status=ZLIBEncodeImage(image,length,pixels,exception);
2283 if (compression == LZWCompression)
2284 status=LZWEncodeImage(image,length,pixels,exception);
2286 status=PackbitsEncodeImage(image,length,pixels,exception);
2287 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2288 if (status == MagickFalse)
2290 (void) CloseBlob(image);
2291 return(MagickFalse);
2298 Dump uncompressed PseudoColor packets.
2300 Ascii85Initialize(image);
2301 for (y=0; y < (ssize_t) tile_image->rows; y++)
2303 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2305 if (p == (const Quantum *) NULL)
2307 for (x=0; x < (ssize_t) tile_image->columns; x++)
2309 Ascii85Encode(image,(unsigned char)
2310 GetPixelIndex(tile_image,p));
2311 p+=GetPixelChannels(image);
2314 Ascii85Flush(image);
2319 tile_image=DestroyImage(tile_image);
2320 offset=TellBlob(image)-offset;
2321 (void) WriteBlobString(image,"\nendstream\n");
2322 (void) WriteBlobString(image,"endobj\n");
2324 Write Length object.
2326 xref[object++]=TellBlob(image);
2327 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2329 (void) WriteBlobString(image,buffer);
2330 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
2331 (void) WriteBlobString(image,buffer);
2332 (void) WriteBlobString(image,"endobj\n");
2333 xref[object++]=TellBlob(image);
2334 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2336 (void) WriteBlobString(image,buffer);
2337 if ((image->storage_class != DirectClass) && (image->colors <= 256) &&
2338 (compression != FaxCompression) && (compression != Group4Compression))
2341 Write Colormap object.
2343 (void) WriteBlobString(image,"<<\n");
2344 if (compression == NoCompression)
2345 (void) WriteBlobString(image,"/Filter [ /ASCII85Decode ]\n");
2346 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
2348 (void) WriteBlobString(image,buffer);
2349 (void) WriteBlobString(image,">>\n");
2350 (void) WriteBlobString(image,"stream\n");
2351 offset=TellBlob(image);
2352 if (compression == NoCompression)
2353 Ascii85Initialize(image);
2354 for (i=0; i < (ssize_t) image->colors; i++)
2356 if (compression == NoCompression)
2358 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].red));
2359 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].green));
2360 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].blue));
2363 (void) WriteBlobByte(image,
2364 ScaleQuantumToChar(image->colormap[i].red));
2365 (void) WriteBlobByte(image,
2366 ScaleQuantumToChar(image->colormap[i].green));
2367 (void) WriteBlobByte(image,
2368 ScaleQuantumToChar(image->colormap[i].blue));
2370 if (compression == NoCompression)
2371 Ascii85Flush(image);
2372 offset=TellBlob(image)-offset;
2373 (void) WriteBlobString(image,"\nendstream\n");
2375 (void) WriteBlobString(image,"endobj\n");
2377 Write Length object.
2379 xref[object++]=TellBlob(image);
2380 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2382 (void) WriteBlobString(image,buffer);
2383 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
2385 (void) WriteBlobString(image,buffer);
2386 (void) WriteBlobString(image,"endobj\n");
2388 Write softmask object.
2390 xref[object++]=TellBlob(image);
2391 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2393 (void) WriteBlobString(image,buffer);
2394 (void) WriteBlobString(image,"<<\n");
2395 if (image->alpha_trait != BlendPixelTrait)
2396 (void) WriteBlobString(image,">>\n");
2399 (void) WriteBlobString(image,"/Type /XObject\n");
2400 (void) WriteBlobString(image,"/Subtype /Image\n");
2401 (void) FormatLocaleString(buffer,MaxTextExtent,"/Name /Ma%.20g\n",
2402 (double) image->scene);
2403 (void) WriteBlobString(image,buffer);
2404 switch (compression)
2408 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
2412 case LZWCompression:
2414 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"LZWDecode");
2417 case ZipCompression:
2419 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
2425 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
2430 (void) WriteBlobString(image,buffer);
2431 (void) FormatLocaleString(buffer,MaxTextExtent,"/Width %.20g\n",(double)
2433 (void) WriteBlobString(image,buffer);
2434 (void) FormatLocaleString(buffer,MaxTextExtent,"/Height %.20g\n",
2435 (double) image->rows);
2436 (void) WriteBlobString(image,buffer);
2437 (void) WriteBlobString(image,"/ColorSpace /DeviceGray\n");
2438 (void) FormatLocaleString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
2439 (compression == FaxCompression) || (compression == Group4Compression)
2441 (void) WriteBlobString(image,buffer);
2442 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
2444 (void) WriteBlobString(image,buffer);
2445 (void) WriteBlobString(image,">>\n");
2446 (void) WriteBlobString(image,"stream\n");
2447 offset=TellBlob(image);
2448 number_pixels=(MagickSizeType) image->columns*image->rows;
2449 switch (compression)
2451 case RLECompression:
2455 Allocate pixel array.
2457 length=(size_t) number_pixels;
2458 pixels=(unsigned char *) AcquireQuantumMemory(length,
2460 if (pixels == (unsigned char *) NULL)
2462 image=DestroyImage(image);
2463 ThrowWriterException(ResourceLimitError,
2464 "MemoryAllocationFailed");
2467 Dump Runlength encoded pixels.
2470 for (y=0; y < (ssize_t) image->rows; y++)
2472 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2473 if (p == (const Quantum *) NULL)
2475 for (x=0; x < (ssize_t) image->columns; x++)
2477 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
2478 p+=GetPixelChannels(image);
2481 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2482 if (compression == ZipCompression)
2483 status=ZLIBEncodeImage(image,length,pixels,exception);
2486 if (compression == LZWCompression)
2487 status=LZWEncodeImage(image,length,pixels,exception);
2489 status=PackbitsEncodeImage(image,length,pixels,exception);
2490 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2491 if (status == MagickFalse)
2493 (void) CloseBlob(image);
2494 return(MagickFalse);
2501 Dump uncompressed PseudoColor packets.
2503 Ascii85Initialize(image);
2504 for (y=0; y < (ssize_t) image->rows; y++)
2506 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2507 if (p == (const Quantum *) NULL)
2509 for (x=0; x < (ssize_t) image->columns; x++)
2511 Ascii85Encode(image,ScaleQuantumToChar(GetPixelAlpha(image,p)));
2512 p+=GetPixelChannels(image);
2515 Ascii85Flush(image);
2519 offset=TellBlob(image)-offset;
2520 (void) WriteBlobString(image,"\nendstream\n");
2522 (void) WriteBlobString(image,"endobj\n");
2524 Write Length object.
2526 xref[object++]=TellBlob(image);
2527 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2529 (void) WriteBlobString(image,buffer);
2530 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
2531 (void) WriteBlobString(image,buffer);
2532 (void) WriteBlobString(image,"endobj\n");
2533 if (GetNextImageInList(image) == (Image *) NULL)
2535 image=SyncNextImageInList(image);
2536 status=SetImageProgress(image,SaveImagesTag,scene++,
2537 GetImageListLength(image));
2538 if (status == MagickFalse)
2540 } while (image_info->adjoin != MagickFalse);
2542 Write Metadata object.
2544 xref[object++]=TellBlob(image);
2546 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2548 (void) WriteBlobString(image,buffer);
2549 (void) WriteBlobString(image,"<<\n");
2550 (void) FormatLocaleString(buffer,MaxTextExtent,"/Title (%s)\n",
2551 EscapeParenthesis(basename));
2552 (void) WriteBlobString(image,buffer);
2553 seconds=time((time_t *) NULL);
2554 #if defined(MAGICKCORE_HAVE_LOCALTIME_R)
2555 (void) localtime_r(&seconds,&local_time);
2557 (void) memcpy(&local_time,localtime(&seconds),sizeof(local_time));
2559 (void) FormatLocaleString(date,MaxTextExtent,"D:%04d%02d%02d%02d%02d%02d",
2560 local_time.tm_year+1900,local_time.tm_mon+1,local_time.tm_mday,
2561 local_time.tm_hour,local_time.tm_min,local_time.tm_sec);
2562 (void) FormatLocaleString(buffer,MaxTextExtent,"/CreationDate (%s)\n",date);
2563 (void) WriteBlobString(image,buffer);
2564 (void) FormatLocaleString(buffer,MaxTextExtent,"/ModDate (%s)\n",date);
2565 (void) WriteBlobString(image,buffer);
2566 (void) FormatLocaleString(buffer,MaxTextExtent,"/Producer (%s)\n",
2567 EscapeParenthesis(GetMagickVersion((size_t *) NULL)));
2568 (void) WriteBlobString(image,buffer);
2569 (void) WriteBlobString(image,">>\n");
2570 (void) WriteBlobString(image,"endobj\n");
2574 offset=TellBlob(image)-xref[0]+
2575 (LocaleCompare(image_info->magick,"PDFA") == 0 ? 6 : 0)+10;
2576 (void) WriteBlobString(image,"xref\n");
2577 (void) FormatLocaleString(buffer,MaxTextExtent,"0 %.20g\n",(double)
2579 (void) WriteBlobString(image,buffer);
2580 (void) WriteBlobString(image,"0000000000 65535 f \n");
2581 for (i=0; i < (ssize_t) object; i++)
2583 (void) FormatLocaleString(buffer,MaxTextExtent,"%010lu 00000 n \n",
2584 (unsigned long) xref[i]);
2585 (void) WriteBlobString(image,buffer);
2587 (void) WriteBlobString(image,"trailer\n");
2588 (void) WriteBlobString(image,"<<\n");
2589 (void) FormatLocaleString(buffer,MaxTextExtent,"/Size %.20g\n",(double)
2591 (void) WriteBlobString(image,buffer);
2592 (void) FormatLocaleString(buffer,MaxTextExtent,"/Info %.20g 0 R\n",(double)
2594 (void) WriteBlobString(image,buffer);
2595 (void) FormatLocaleString(buffer,MaxTextExtent,"/Root %.20g 0 R\n",(double)
2597 (void) WriteBlobString(image,buffer);
2598 (void) SignatureImage(image,exception);
2599 (void) FormatLocaleString(buffer,MaxTextExtent,"/ID [<%s> <%s>]\n",
2600 GetImageProperty(image,"signature",exception),
2601 GetImageProperty(image,"signature",exception));
2602 (void) WriteBlobString(image,buffer);
2603 (void) WriteBlobString(image,">>\n");
2604 (void) WriteBlobString(image,"startxref\n");
2605 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
2606 (void) WriteBlobString(image,buffer);
2607 (void) WriteBlobString(image,"%%EOF\n");
2608 xref=(MagickOffsetType *) RelinquishMagickMemory(xref);
2609 (void) CloseBlob(image);