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/static.h"
74 #include "MagickCore/string_.h"
75 #include "MagickCore/module.h"
76 #include "MagickCore/token.h"
77 #include "MagickCore/transform.h"
78 #include "MagickCore/utility.h"
79 #include "MagickCore/module.h"
84 #if defined(MAGICKCORE_TIFF_DELEGATE)
85 #define CCITTParam "-1"
87 #define CCITTParam "0"
93 static MagickBooleanType
94 WritePDFImage(const ImageInfo *,Image *,ExceptionInfo *);
97 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101 % I n v o k e P D F D e l e g a t e %
105 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
107 % InvokePDFDelegate() executes the PDF interpreter with the specified command.
109 % The format of the InvokePDFDelegate method is:
111 % MagickBooleanType InvokePDFDelegate(const MagickBooleanType verbose,
112 % const char *command,ExceptionInfo *exception)
114 % A description of each parameter follows:
116 % o verbose: A value other than zero displays the command prior to
119 % o command: the address of a character string containing the command to
122 % o exception: return any errors or warnings in this structure.
125 static MagickBooleanType InvokePDFDelegate(const MagickBooleanType verbose,
126 const char *command,ExceptionInfo *exception)
131 #if defined(MAGICKCORE_GS_DELEGATE) || defined(MAGICKCORE_WINDOWS_SUPPORT)
148 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
149 ghost_info=NTGhostscriptDLLVectors();
154 ghost_info=(&ghost_info_struct);
155 (void) ResetMagickMemory(&ghost_info,0,sizeof(ghost_info));
156 ghost_info_struct.new_instance=(int (*)(gs_main_instance **,void *))
158 ghost_info_struct.init_with_args=(int (*)(gs_main_instance *,int,char **))
159 gsapi_init_with_args;
160 ghost_info_struct.run_string=(int (*)(gs_main_instance *,const char *,int,
161 int *)) gsapi_run_string;
162 ghost_info_struct.delete_instance=(void (*)(gs_main_instance *))
163 gsapi_delete_instance;
164 ghost_info_struct.exit=(int (*)(gs_main_instance *)) gsapi_exit;
166 if (ghost_info == (GhostInfo *) NULL)
168 status=SystemCommand(MagickFalse,verbose,command,exception);
169 return(status == 0 ? MagickTrue : MagickFalse);
171 if (verbose != MagickFalse)
173 (void) fputs("[ghostscript library]",stdout);
174 (void) fputs(strchr(command,' '),stdout);
176 status=(ghost_info->new_instance)(&interpreter,(void *) NULL);
179 status=SystemCommand(MagickFalse,verbose,command,exception);
180 return(status == 0 ? MagickTrue : MagickFalse);
183 argv=StringToArgv(command,&argc);
184 status=(ghost_info->init_with_args)(interpreter,argc-1,argv+1);
186 status=(ghost_info->run_string)(interpreter,"systemdict /start get exec\n",
188 (ghost_info->exit)(interpreter);
189 (ghost_info->delete_instance)(interpreter);
190 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
191 NTGhostscriptUnLoadDLL();
193 for (i=0; i < (ssize_t) argc; i++)
194 argv[i]=DestroyString(argv[i]);
195 argv=(char **) RelinquishMagickMemory(argv);
196 if ((status != 0) && (status != -101))
201 message=GetExceptionMessage(errno);
202 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
203 "`%s': %s",command,message);
204 message=DestroyString(message);
205 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
206 "Ghostscript returns status %d, exit code %d",status,code);
211 status=SystemCommand(MagickFalse,verbose,command,exception);
212 return(status == 0 ? MagickTrue : MagickFalse);
217 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
225 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
227 % IsPDF() returns MagickTrue if the image format type, identified by the
228 % magick string, is PDF.
230 % The format of the IsPDF method is:
232 % MagickBooleanType IsPDF(const unsigned char *magick,const size_t offset)
234 % A description of each parameter follows:
236 % o magick: compare image format pattern against these bytes.
238 % o offset: Specifies the offset of the magick string.
241 static MagickBooleanType IsPDF(const unsigned char *magick,const size_t offset)
245 if (LocaleNCompare((const char *) magick,"%PDF-",5) == 0)
251 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
255 % R e a d P D F I m a g e %
259 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
261 % ReadPDFImage() reads a Portable Document Format image file and
262 % returns it. It allocates the memory necessary for the new Image structure
263 % and returns a pointer to the new image.
265 % The format of the ReadPDFImage method is:
267 % Image *ReadPDFImage(const ImageInfo *image_info,ExceptionInfo *exception)
269 % A description of each parameter follows:
271 % o image_info: the image info.
273 % o exception: return any errors or warnings in this structure.
277 static MagickBooleanType IsPDFRendered(const char *path)
285 if ((path == (const char *) NULL) || (*path == '\0'))
287 status=GetPathAttributes(path,&attributes);
288 if ((status != MagickFalse) && S_ISREG(attributes.st_mode) &&
289 (attributes.st_size > 0))
294 static Image *ReadPDFImage(const ImageInfo *image_info,ExceptionInfo *exception)
296 #define CMYKProcessColor "CMYKProcessColor"
297 #define CropBox "CropBox"
298 #define DefaultCMYK "DefaultCMYK"
299 #define DeviceCMYK "DeviceCMYK"
300 #define MediaBox "MediaBox"
301 #define RenderPostscriptText "Rendering Postscript... "
302 #define PDFRotate "Rotate"
303 #define SpotColor "Separation"
304 #define TrimBox "TrimBox"
305 #define PDFVersion "PDF-"
308 command[MaxTextExtent],
309 density[MaxTextExtent],
310 filename[MaxTextExtent],
311 geometry[MaxTextExtent],
312 options[MaxTextExtent],
313 input_filename[MaxTextExtent],
314 postscript_filename[MaxTextExtent];
373 assert(image_info != (const ImageInfo *) NULL);
374 assert(image_info->signature == MagickSignature);
375 if (image_info->debug != MagickFalse)
376 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
377 image_info->filename);
378 assert(exception != (ExceptionInfo *) NULL);
379 assert(exception->signature == MagickSignature);
383 image=AcquireImage(image_info,exception);
384 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
385 if (status == MagickFalse)
387 image=DestroyImageList(image);
388 return((Image *) NULL);
390 status=AcquireUniqueSymbolicLink(image_info->filename,input_filename);
391 if (status == MagickFalse)
393 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
394 image_info->filename);
395 image=DestroyImageList(image);
396 return((Image *) NULL);
399 Set the page density.
401 delta.x=DefaultResolution;
402 delta.y=DefaultResolution;
403 if ((image->resolution.x == 0.0) || (image->resolution.y == 0.0))
405 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
406 image->resolution.x=geometry_info.rho;
407 image->resolution.y=geometry_info.sigma;
408 if ((flags & SigmaValue) == 0)
409 image->resolution.y=image->resolution.x;
411 if (image_info->density != (char *) NULL)
413 flags=ParseGeometry(image_info->density,&geometry_info);
414 image->resolution.x=geometry_info.rho;
415 image->resolution.y=geometry_info.sigma;
416 if ((flags & SigmaValue) == 0)
417 image->resolution.y=image->resolution.x;
419 (void) ParseAbsoluteGeometry(PSPageGeometry,&page);
420 if (image_info->page != (char *) NULL)
421 (void) ParseAbsoluteGeometry(image_info->page,&page);
422 page.width=(size_t) ceil((double) (page.width*image->resolution.x/delta.x)-
424 page.height=(size_t) ceil((double) (page.height*image->resolution.y/delta.y)-
427 Determine page geometry from the PDF media box.
429 cmyk=image_info->colorspace == CMYKColorspace ? MagickTrue : MagickFalse;
430 cropbox=IsStringTrue(GetImageOption(image_info,"pdf:use-cropbox"));
431 trimbox=IsStringTrue(GetImageOption(image_info,"pdf:use-trimbox"));
434 (void) ResetMagickMemory(&bounding_box,0,sizeof(bounding_box));
435 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
436 (void) ResetMagickMemory(&hires_bounds,0,sizeof(hires_bounds));
437 (void) ResetMagickMemory(command,0,sizeof(command));
440 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
448 if ((c != (int) '/') && (c != (int) '%') &&
449 ((size_t) (p-command) < (MaxTextExtent-1)))
453 if (LocaleNCompare(PDFRotate,command,strlen(PDFRotate)) == 0)
454 count=(ssize_t) sscanf(command,"Rotate %lf",&angle);
456 Is this a CMYK document?
458 if (LocaleNCompare(DefaultCMYK,command,strlen(DefaultCMYK)) == 0)
460 if (LocaleNCompare(DeviceCMYK,command,strlen(DeviceCMYK)) == 0)
462 if (LocaleNCompare(CMYKProcessColor,command,strlen(CMYKProcessColor)) == 0)
464 if (LocaleNCompare(SpotColor,command,strlen(SpotColor)) == 0)
468 property[MaxTextExtent],
477 (void) FormatLocaleString(property,MaxTextExtent,"pdf:SpotColor-%.20g",
478 (double) spotcolor++);
480 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
482 if ((isspace(c) != 0) || (c == '/') || ((i+1) == MaxTextExtent))
487 value=AcquireString(name);
488 (void) SubstituteString(&value,"#20"," ");
489 (void) SetImageProperty(image,property,value,exception);
490 value=DestroyString(value);
493 if (LocaleNCompare(PDFVersion,command,strlen(PDFVersion)) == 0)
494 (void) SetImageProperty(image,"pdf:Version",command,exception);
495 if (image_info->page != (char *) NULL)
498 if (cropbox != MagickFalse)
500 if (LocaleNCompare(CropBox,command,strlen(CropBox)) == 0)
503 Note region defined by crop box.
505 count=(ssize_t) sscanf(command,"CropBox [%lf %lf %lf %lf",
506 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
508 count=(ssize_t) sscanf(command,"CropBox[%lf %lf %lf %lf",
509 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
513 if (trimbox != MagickFalse)
515 if (LocaleNCompare(TrimBox,command,strlen(TrimBox)) == 0)
518 Note region defined by trim box.
520 count=(ssize_t) sscanf(command,"TrimBox [%lf %lf %lf %lf",
521 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
523 count=(ssize_t) sscanf(command,"TrimBox[%lf %lf %lf %lf",
524 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
528 if (LocaleNCompare(MediaBox,command,strlen(MediaBox)) == 0)
531 Note region defined by media box.
533 count=(ssize_t) sscanf(command,"MediaBox [%lf %lf %lf %lf",
534 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
536 count=(ssize_t) sscanf(command,"MediaBox[%lf %lf %lf %lf",
537 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
541 if ((fabs(bounds.x2-bounds.x1) <= fabs(hires_bounds.x2-hires_bounds.x1)) ||
542 (fabs(bounds.y2-bounds.y1) <= fabs(hires_bounds.y2-hires_bounds.y1)))
546 if ((fabs(hires_bounds.x2-hires_bounds.x1) >= MagickEpsilon) &&
547 (fabs(hires_bounds.y2-hires_bounds.y1) >= MagickEpsilon))
550 Set PDF render geometry.
552 (void) FormatLocaleString(geometry,MaxTextExtent,"%gx%g%+.15g%+.15g",
553 hires_bounds.x2-bounds.x1,hires_bounds.y2-hires_bounds.y1,
554 hires_bounds.x1,hires_bounds.y1);
555 (void) SetImageProperty(image,"pdf:HiResBoundingBox",geometry,exception);
556 page.width=(size_t) ceil((double) ((hires_bounds.x2-hires_bounds.x1)*
557 image->resolution.x/delta.x)-0.5);
558 page.height=(size_t) ceil((double) ((hires_bounds.y2-hires_bounds.y1)*
559 image->resolution.y/delta.y)-0.5);
561 (void) CloseBlob(image);
562 if ((fabs(angle) == 90.0) || (fabs(angle) == 270.0))
568 page.width=page.height;
571 if (IssRGBCompatibleColorspace(image_info->colorspace) != MagickFalse)
574 Create Ghostscript control file.
576 file=AcquireUniqueFileResource(postscript_filename);
579 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
580 image_info->filename);
581 image=DestroyImage(image);
582 return((Image *) NULL);
584 count=write(file," ",1);
587 Render Postscript with the Ghostscript delegate.
589 if ((image_info->ping != MagickFalse) ||
590 (image_info->monochrome != MagickFalse))
591 delegate_info=GetDelegateInfo("ps:mono",(char *) NULL,exception);
593 if (cmyk != MagickFalse)
594 delegate_info=GetDelegateInfo("ps:cmyk",(char *) NULL,exception);
596 delegate_info=GetDelegateInfo("ps:alpha",(char *) NULL,exception);
597 if (delegate_info == (const DelegateInfo *) NULL)
599 (void) RelinquishUniqueFileResource(postscript_filename);
600 image=DestroyImage(image);
601 return((Image *) NULL);
604 (void) FormatLocaleString(density,MaxTextExtent,"%gx%g",image->resolution.x,
605 image->resolution.y);
606 if (image_info->page != (char *) NULL)
607 (void) FormatLocaleString(options,MaxTextExtent,"-g%.20gx%.20g ",(double)
608 page.width,(double) page.height);
609 if (cmyk != MagickFalse)
610 (void) ConcatenateMagickString(options,"-dUseCIEColor ",MaxTextExtent);
611 if (cropbox != MagickFalse)
612 (void) ConcatenateMagickString(options,"-dUseCropBox ",MaxTextExtent);
613 if (trimbox != MagickFalse)
614 (void) ConcatenateMagickString(options,"-dUseTrimBox ",MaxTextExtent);
615 read_info=CloneImageInfo(image_info);
616 *read_info->magick='\0';
617 if (read_info->number_scenes != 0)
620 pages[MaxTextExtent];
622 (void) FormatLocaleString(pages,MaxTextExtent,"-dFirstPage=%.20g "
623 "-dLastPage=%.20g",(double) read_info->scene+1,(double)
624 (read_info->scene+read_info->number_scenes));
625 (void) ConcatenateMagickString(options,pages,MaxTextExtent);
626 read_info->number_scenes=0;
627 if (read_info->scenes != (char *) NULL)
628 *read_info->scenes='\0';
630 option=GetImageOption(read_info,"authenticate");
631 if (option != (const char *) NULL)
632 (void) FormatLocaleString(options+strlen(options),MaxTextExtent,
633 " -sPCLPassword=%s",option);
634 (void) CopyMagickString(filename,read_info->filename,MaxTextExtent);
635 (void) AcquireUniqueFilename(filename);
636 (void) ConcatenateMagickString(filename,"-%08d",MaxTextExtent);
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"
957 " </rdf:Description>\n"
958 " <rdf:Description rdf:about=\"\"\n"
959 " xmlns:xapMM=\"http://ns.adobe.com/xap/1.0/mm/\">\n"
960 " <xapMM:DocumentID>uuid:6ec119d7-7982-4f56-808d-dfe64f5b35cf</xapMM:DocumentID>\n"
961 " <xapMM:InstanceID>uuid:a79b99b4-6235-447f-9f6c-ec18ef7555cb</xapMM:InstanceID>\n"
962 " </rdf:Description>\n"
963 " <rdf:Description rdf:about=\"\"\n"
964 " xmlns:pdf=\"http://ns.adobe.com/pdf/1.3/\">\n"
965 " <pdf:Producer>%s</pdf:Producer>\n"
966 " </rdf:Description>\n"
967 " <rdf:Description rdf:about=\"\"\n"
968 " xmlns:pdfaid=\"http://www.aiim.org/pdfa/ns/id/\">\n"
969 " <pdfaid:part>1</pdfaid:part>\n"
970 " <pdfaid:conformance>B</pdfaid:conformance>\n"
971 " </rdf:Description>\n"
974 "<?xpacket end=\"w\"?>\n"
976 XMPProfileMagick[4]= { (char) 0xef, (char) 0xbb, (char) 0xbf, (char) 0x00 };
979 basename[MaxTextExtent],
980 buffer[MaxTextExtent],
983 page_geometry[MaxTextExtent];
1025 register const Quantum
1028 register unsigned char
1058 Open output image file.
1060 assert(image_info != (const ImageInfo *) NULL);
1061 assert(image_info->signature == MagickSignature);
1062 assert(image != (Image *) NULL);
1063 assert(image->signature == MagickSignature);
1064 if (image->debug != MagickFalse)
1065 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1066 assert(exception != (ExceptionInfo *) NULL);
1067 assert(exception->signature == MagickSignature);
1068 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1069 if (status == MagickFalse)
1072 Allocate X ref memory.
1074 xref=(MagickOffsetType *) AcquireQuantumMemory(2048UL,sizeof(*xref));
1075 if (xref == (MagickOffsetType *) NULL)
1076 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1077 (void) ResetMagickMemory(xref,0,2048UL*sizeof(*xref));
1083 if (image_info->compression == JPEG2000Compression)
1084 version=(size_t) MagickMax(version,5);
1085 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
1086 if (next->matte != MagickFalse)
1087 version=(size_t) MagickMax(version,4);
1088 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1089 version=(size_t) MagickMax(version,6);
1090 (void) FormatLocaleString(buffer,MaxTextExtent,"%%PDF-1.%.20g \n",(double)
1092 (void) WriteBlobString(image,buffer);
1093 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1094 (void) WriteBlobString(image,"%âãÏÓ\n");
1096 Write Catalog object.
1098 xref[object++]=TellBlob(image);
1100 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1102 (void) WriteBlobString(image,buffer);
1103 (void) WriteBlobString(image,"<<\n");
1104 if (LocaleCompare(image_info->magick,"PDFA") != 0)
1105 (void) FormatLocaleString(buffer,MaxTextExtent,"/Pages %.20g 0 R\n",(double)
1109 (void) FormatLocaleString(buffer,MaxTextExtent,"/Metadata %.20g 0 R\n",
1111 (void) WriteBlobString(image,buffer);
1112 (void) FormatLocaleString(buffer,MaxTextExtent,"/Pages %.20g 0 R\n",
1115 (void) WriteBlobString(image,buffer);
1116 (void) WriteBlobString(image,"/Type /Catalog\n");
1117 (void) WriteBlobString(image,">>\n");
1118 (void) WriteBlobString(image,"endobj\n");
1119 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1122 create_date[MaxTextExtent],
1123 modify_date[MaxTextExtent],
1124 timestamp[MaxTextExtent],
1125 xmp_profile[MaxTextExtent];
1133 xref[object++]=TellBlob(image);
1134 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1136 (void) WriteBlobString(image,buffer);
1137 (void) WriteBlobString(image,"<<\n");
1138 (void) WriteBlobString(image,"/Subtype /XML\n");
1140 value=GetImageProperty(image,"date:modify",exception);
1141 if (value != (const char *) NULL)
1142 (void) CopyMagickString(modify_date,value,MaxTextExtent);
1144 value=GetImageProperty(image,"date:create",exception);
1145 if (value != (const char *) NULL)
1146 (void) CopyMagickString(create_date,value,MaxTextExtent);
1147 (void) FormatMagickTime(time((time_t *) NULL),MaxTextExtent,timestamp);
1148 i=FormatLocaleString(xmp_profile,MaxTextExtent,XMPProfile,
1149 XMPProfileMagick,modify_date,create_date,timestamp,
1150 GetMagickVersion(&version),GetMagickVersion(&version));
1151 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g\n",(double)
1153 (void) WriteBlobString(image,buffer);
1154 (void) WriteBlobString(image,"/Type /Metadata\n");
1155 (void) WriteBlobString(image,">>\nstream\n");
1156 (void) WriteBlobString(image,xmp_profile);
1157 (void) WriteBlobString(image,"endstream\n");
1158 (void) WriteBlobString(image,"endobj\n");
1163 xref[object++]=TellBlob(image);
1165 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1167 (void) WriteBlobString(image,buffer);
1168 (void) WriteBlobString(image,"<<\n");
1169 (void) WriteBlobString(image,"/Type /Pages\n");
1170 (void) FormatLocaleString(buffer,MaxTextExtent,"/Kids [ %.20g 0 R ",(double)
1172 (void) WriteBlobString(image,buffer);
1173 count=(ssize_t) (pages_id+ObjectsPerImage+1);
1174 if (image_info->adjoin != MagickFalse)
1180 Predict page object id's.
1183 for ( ; GetNextImageInList(kid_image) != (Image *) NULL; count+=ObjectsPerImage)
1185 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 R ",(double)
1187 (void) WriteBlobString(image,buffer);
1188 kid_image=GetNextImageInList(kid_image);
1190 xref=(MagickOffsetType *) ResizeQuantumMemory(xref,(size_t) count+2048UL,
1192 if (xref == (MagickOffsetType *) NULL)
1193 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1195 (void) WriteBlobString(image,"]\n");
1196 (void) FormatLocaleString(buffer,MaxTextExtent,"/Count %.20g\n",(double)
1197 ((count-pages_id)/ObjectsPerImage));
1198 (void) WriteBlobString(image,buffer);
1199 (void) WriteBlobString(image,">>\n");
1200 (void) WriteBlobString(image,"endobj\n");
1204 compression=image->compression;
1205 if (image_info->compression != UndefinedCompression)
1206 compression=image_info->compression;
1207 switch (compression)
1209 case FaxCompression:
1210 case Group4Compression:
1212 if ((IsImageMonochrome(image,exception) == MagickFalse) ||
1213 (image->matte != MagickFalse))
1214 compression=RLECompression;
1217 #if !defined(MAGICKCORE_JPEG_DELEGATE)
1218 case JPEGCompression:
1220 compression=RLECompression;
1221 (void) ThrowMagickException(exception,GetMagickModule(),
1222 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
1227 #if !defined(MAGICKCORE_JP2_DELEGATE)
1228 case JPEG2000Compression:
1230 compression=RLECompression;
1231 (void) ThrowMagickException(exception,GetMagickModule(),
1232 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JP2)",
1237 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
1238 case ZipCompression:
1240 compression=RLECompression;
1241 (void) ThrowMagickException(exception,GetMagickModule(),
1242 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZLIB)",
1247 case LZWCompression:
1249 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1250 compression=RLECompression; /* LZW compression is forbidden */
1255 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1256 compression=RLECompression; /* ASCII 85 compression is forbidden */
1262 if (compression == JPEG2000Compression)
1264 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
1265 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1268 Scale relative to dots-per-inch.
1270 delta.x=DefaultResolution;
1271 delta.y=DefaultResolution;
1272 resolution.x=image->resolution.x;
1273 resolution.y=image->resolution.y;
1274 if ((resolution.x == 0.0) || (resolution.y == 0.0))
1276 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
1277 resolution.x=geometry_info.rho;
1278 resolution.y=geometry_info.sigma;
1279 if ((flags & SigmaValue) == 0)
1280 resolution.y=resolution.x;
1282 if (image_info->density != (char *) NULL)
1284 flags=ParseGeometry(image_info->density,&geometry_info);
1285 resolution.x=geometry_info.rho;
1286 resolution.y=geometry_info.sigma;
1287 if ((flags & SigmaValue) == 0)
1288 resolution.y=resolution.x;
1290 if (image->units == PixelsPerCentimeterResolution)
1292 resolution.x=(double) ((size_t) (100.0*2.54*resolution.x+0.5)/100.0);
1293 resolution.y=(double) ((size_t) (100.0*2.54*resolution.y+0.5)/100.0);
1295 SetGeometry(image,&geometry);
1296 (void) FormatLocaleString(page_geometry,MaxTextExtent,"%.20gx%.20g",(double)
1297 image->columns,(double) image->rows);
1298 if (image_info->page != (char *) NULL)
1299 (void) CopyMagickString(page_geometry,image_info->page,MaxTextExtent);
1301 if ((image->page.width != 0) && (image->page.height != 0))
1302 (void) FormatLocaleString(page_geometry,MaxTextExtent,
1303 "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,(double)
1304 image->page.height,(double) image->page.x,(double) image->page.y);
1306 if ((image->gravity != UndefinedGravity) &&
1307 (LocaleCompare(image_info->magick,"PDF") == 0))
1308 (void) CopyMagickString(page_geometry,PSPageGeometry,MaxTextExtent);
1309 (void) ConcatenateMagickString(page_geometry,">",MaxTextExtent);
1310 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
1311 &geometry.width,&geometry.height);
1312 scale.x=(double) (geometry.width*delta.x)/resolution.x;
1313 geometry.width=(size_t) floor(scale.x+0.5);
1314 scale.y=(double) (geometry.height*delta.y)/resolution.y;
1315 geometry.height=(size_t) floor(scale.y+0.5);
1316 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
1317 (void) ParseGravityGeometry(image,page_geometry,&page_info,exception);
1318 if (image->gravity != UndefinedGravity)
1320 geometry.x=(-page_info.x);
1321 geometry.y=(ssize_t) (media_info.height+page_info.y-image->rows);
1324 if (image_info->pointsize != 0.0)
1325 pointsize=image_info->pointsize;
1327 value=GetImageProperty(image,"label",exception);
1328 if (value != (const char *) NULL)
1329 text_size=(size_t) (MultilineCensus(value)*pointsize+12);
1334 xref[object++]=TellBlob(image);
1335 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1337 (void) WriteBlobString(image,buffer);
1338 (void) WriteBlobString(image,"<<\n");
1339 (void) WriteBlobString(image,"/Type /Page\n");
1340 (void) FormatLocaleString(buffer,MaxTextExtent,"/Parent %.20g 0 R\n",
1342 (void) WriteBlobString(image,buffer);
1343 (void) WriteBlobString(image,"/Resources <<\n");
1344 labels=(char **) NULL;
1345 value=GetImageProperty(image,"label",exception);
1346 if (value != (const char *) NULL)
1347 labels=StringToList(value);
1348 if (labels != (char **) NULL)
1350 (void) FormatLocaleString(buffer,MaxTextExtent,
1351 "/Font << /F%.20g %.20g 0 R >>\n",(double) image->scene,(double)
1353 (void) WriteBlobString(image,buffer);
1355 (void) FormatLocaleString(buffer,MaxTextExtent,
1356 "/XObject << /Im%.20g %.20g 0 R >>\n",(double) image->scene,(double)
1358 (void) WriteBlobString(image,buffer);
1359 (void) FormatLocaleString(buffer,MaxTextExtent,"/ProcSet %.20g 0 R >>\n",
1361 (void) WriteBlobString(image,buffer);
1362 (void) FormatLocaleString(buffer,MaxTextExtent,
1363 "/MediaBox [0 0 %g %g]\n",72.0*media_info.width/resolution.x,
1364 72.0*media_info.height/resolution.y);
1365 (void) WriteBlobString(image,buffer);
1366 (void) FormatLocaleString(buffer,MaxTextExtent,
1367 "/CropBox [0 0 %g %g]\n",72.0*media_info.width/resolution.x,
1368 72.0*media_info.height/resolution.y);
1369 (void) WriteBlobString(image,buffer);
1370 (void) FormatLocaleString(buffer,MaxTextExtent,"/Contents %.20g 0 R\n",
1372 (void) WriteBlobString(image,buffer);
1373 (void) FormatLocaleString(buffer,MaxTextExtent,"/Thumb %.20g 0 R\n",(double)
1375 (void) WriteBlobString(image,buffer);
1376 (void) WriteBlobString(image,">>\n");
1377 (void) WriteBlobString(image,"endobj\n");
1379 Write Contents object.
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) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
1388 (void) WriteBlobString(image,buffer);
1389 (void) WriteBlobString(image,">>\n");
1390 (void) WriteBlobString(image,"stream\n");
1391 offset=TellBlob(image);
1392 (void) WriteBlobString(image,"q\n");
1393 if (labels != (char **) NULL)
1394 for (i=0; labels[i] != (char *) NULL; i++)
1396 (void) WriteBlobString(image,"BT\n");
1397 (void) FormatLocaleString(buffer,MaxTextExtent,"/F%.20g %g Tf\n",
1398 (double) image->scene,pointsize);
1399 (void) WriteBlobString(image,buffer);
1400 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g Td\n",
1401 (double) geometry.x,(double) (geometry.y+geometry.height+i*pointsize+
1403 (void) WriteBlobString(image,buffer);
1404 (void) FormatLocaleString(buffer,MaxTextExtent,"(%s) Tj\n",labels[i]);
1405 (void) WriteBlobString(image,buffer);
1406 (void) WriteBlobString(image,"ET\n");
1407 labels[i]=DestroyString(labels[i]);
1409 (void) FormatLocaleString(buffer,MaxTextExtent,"%g 0 0 %g %.20g %.20g cm\n",
1410 scale.x,scale.y,(double) geometry.x,(double) geometry.y);
1411 (void) WriteBlobString(image,buffer);
1412 (void) FormatLocaleString(buffer,MaxTextExtent,"/Im%.20g Do\n",(double)
1414 (void) WriteBlobString(image,buffer);
1415 (void) WriteBlobString(image,"Q\n");
1416 offset=TellBlob(image)-offset;
1417 (void) WriteBlobString(image,"endstream\n");
1418 (void) WriteBlobString(image,"endobj\n");
1420 Write Length object.
1422 xref[object++]=TellBlob(image);
1423 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1425 (void) WriteBlobString(image,buffer);
1426 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
1427 (void) WriteBlobString(image,buffer);
1428 (void) WriteBlobString(image,"endobj\n");
1430 Write Procset object.
1432 xref[object++]=TellBlob(image);
1433 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1435 (void) WriteBlobString(image,buffer);
1436 if ((image->storage_class == DirectClass) || (image->colors > 256))
1437 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageC",MaxTextExtent);
1439 if ((compression == FaxCompression) || (compression == Group4Compression))
1440 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageB",MaxTextExtent);
1442 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageI",MaxTextExtent);
1443 (void) WriteBlobString(image,buffer);
1444 (void) WriteBlobString(image," ]\n");
1445 (void) WriteBlobString(image,"endobj\n");
1449 xref[object++]=TellBlob(image);
1450 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1452 (void) WriteBlobString(image,buffer);
1453 (void) WriteBlobString(image,"<<\n");
1454 if (labels != (char **) NULL)
1456 (void) WriteBlobString(image,"/Type /Font\n");
1457 (void) WriteBlobString(image,"/Subtype /Type1\n");
1458 (void) FormatLocaleString(buffer,MaxTextExtent,"/Name /F%.20g\n",
1459 (double) image->scene);
1460 (void) WriteBlobString(image,buffer);
1461 (void) WriteBlobString(image,"/BaseFont /Helvetica\n");
1462 (void) WriteBlobString(image,"/Encoding /MacRomanEncoding\n");
1463 labels=(char **) RelinquishMagickMemory(labels);
1465 (void) WriteBlobString(image,">>\n");
1466 (void) WriteBlobString(image,"endobj\n");
1468 Write XObject object.
1470 xref[object++]=TellBlob(image);
1471 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1473 (void) WriteBlobString(image,buffer);
1474 (void) WriteBlobString(image,"<<\n");
1475 (void) WriteBlobString(image,"/Type /XObject\n");
1476 (void) WriteBlobString(image,"/Subtype /Image\n");
1477 (void) FormatLocaleString(buffer,MaxTextExtent,"/Name /Im%.20g\n",(double)
1479 (void) WriteBlobString(image,buffer);
1480 switch (compression)
1484 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"ASCII85Decode");
1487 case JPEGCompression:
1489 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"DCTDecode");
1490 if (image->colorspace != CMYKColorspace)
1492 (void) WriteBlobString(image,buffer);
1493 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1497 case JPEG2000Compression:
1499 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"JPXDecode");
1500 if (image->colorspace != CMYKColorspace)
1502 (void) WriteBlobString(image,buffer);
1503 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1507 case LZWCompression:
1509 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"LZWDecode");
1512 case ZipCompression:
1514 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"FlateDecode");
1517 case FaxCompression:
1518 case Group4Compression:
1520 (void) CopyMagickString(buffer,"/Filter [ /CCITTFaxDecode ]\n",
1522 (void) WriteBlobString(image,buffer);
1523 (void) FormatLocaleString(buffer,MaxTextExtent,"/DecodeParms [ << "
1524 "/K %s /BlackIs1 false /Columns %.20g /Rows %.20g >> ]\n",CCITTParam,
1525 (double) image->columns,(double) image->rows);
1530 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
1535 (void) WriteBlobString(image,buffer);
1536 (void) FormatLocaleString(buffer,MaxTextExtent,"/Width %.20g\n",(double)
1538 (void) WriteBlobString(image,buffer);
1539 (void) FormatLocaleString(buffer,MaxTextExtent,"/Height %.20g\n",(double)
1541 (void) WriteBlobString(image,buffer);
1542 (void) FormatLocaleString(buffer,MaxTextExtent,"/ColorSpace %.20g 0 R\n",
1544 (void) WriteBlobString(image,buffer);
1545 (void) FormatLocaleString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
1546 (compression == FaxCompression) || (compression == Group4Compression) ?
1548 (void) WriteBlobString(image,buffer);
1549 if (image->matte != MagickFalse)
1551 (void) FormatLocaleString(buffer,MaxTextExtent,"/SMask %.20g 0 R\n",
1553 (void) WriteBlobString(image,buffer);
1555 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
1557 (void) WriteBlobString(image,buffer);
1558 (void) WriteBlobString(image,">>\n");
1559 (void) WriteBlobString(image,"stream\n");
1560 offset=TellBlob(image);
1561 number_pixels=(MagickSizeType) image->columns*image->rows;
1562 if ((4*number_pixels) != (MagickSizeType) ((size_t) (4*number_pixels)))
1563 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1564 if ((compression == FaxCompression) || (compression == Group4Compression) ||
1565 ((image_info->type != TrueColorType) &&
1566 (IsImageGray(image,exception) != MagickFalse)))
1568 switch (compression)
1570 case FaxCompression:
1571 case Group4Compression:
1573 if (LocaleCompare(CCITTParam,"0") == 0)
1575 (void) HuffmanEncodeImage(image_info,image,image,exception);
1578 (void) Huffman2DEncodeImage(image_info,image,image,exception);
1581 case JPEGCompression:
1583 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
1584 if (status == MagickFalse)
1586 (void) CloseBlob(image);
1587 return(MagickFalse);
1591 case JPEG2000Compression:
1593 status=InjectImageBlob(image_info,image,image,"jp2",exception);
1594 if (status == MagickFalse)
1596 (void) CloseBlob(image);
1597 return(MagickFalse);
1601 case RLECompression:
1605 Allocate pixel array.
1607 length=(size_t) number_pixels;
1608 pixels=(unsigned char *) AcquireQuantumMemory(length,
1610 if (pixels == (unsigned char *) NULL)
1611 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1613 Dump Runlength encoded pixels.
1616 for (y=0; y < (ssize_t) image->rows; y++)
1618 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1619 if (p == (const Quantum *) NULL)
1621 for (x=0; x < (ssize_t) image->columns; x++)
1623 *q++=ScaleQuantumToChar(GetPixelIntensity(image,p));
1624 p+=GetPixelChannels(image);
1626 if (image->previous == (Image *) NULL)
1628 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1630 if (status == MagickFalse)
1634 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1635 if (compression == ZipCompression)
1636 status=ZLIBEncodeImage(image,length,pixels,exception);
1639 if (compression == LZWCompression)
1640 status=LZWEncodeImage(image,length,pixels,exception);
1642 status=PackbitsEncodeImage(image,length,pixels,exception);
1643 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1644 if (status == MagickFalse)
1646 (void) CloseBlob(image);
1647 return(MagickFalse);
1654 Dump uncompressed PseudoColor packets.
1656 Ascii85Initialize(image);
1657 for (y=0; y < (ssize_t) image->rows; y++)
1659 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1660 if (p == (const Quantum *) NULL)
1662 for (x=0; x < (ssize_t) image->columns; x++)
1664 Ascii85Encode(image,ScaleQuantumToChar(
1665 GetPixelIntensity(image,p)));
1666 p+=GetPixelChannels(image);
1668 if (image->previous == (Image *) NULL)
1670 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1672 if (status == MagickFalse)
1676 Ascii85Flush(image);
1682 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1683 (compression == JPEGCompression) ||
1684 (compression == JPEG2000Compression))
1685 switch (compression)
1687 case JPEGCompression:
1689 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
1690 if (status == MagickFalse)
1692 (void) CloseBlob(image);
1693 return(MagickFalse);
1697 case JPEG2000Compression:
1699 status=InjectImageBlob(image_info,image,image,"jp2",exception);
1700 if (status == MagickFalse)
1702 (void) CloseBlob(image);
1703 return(MagickFalse);
1707 case RLECompression:
1711 Allocate pixel array.
1713 length=(size_t) number_pixels;
1714 pixels=(unsigned char *) AcquireQuantumMemory(length,
1716 length*=image->colorspace == CMYKColorspace ? 4UL : 3UL;
1717 if (pixels == (unsigned char *) NULL)
1718 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1720 Dump runoffset encoded pixels.
1723 for (y=0; y < (ssize_t) image->rows; y++)
1725 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1726 if (p == (const Quantum *) NULL)
1728 for (x=0; x < (ssize_t) image->columns; x++)
1730 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1731 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1732 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1733 if (image->colorspace == CMYKColorspace)
1734 *q++=ScaleQuantumToChar(GetPixelBlack(image,p));
1735 p+=GetPixelChannels(image);
1737 if (image->previous == (Image *) NULL)
1739 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1741 if (status == MagickFalse)
1745 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1746 if (compression == ZipCompression)
1747 status=ZLIBEncodeImage(image,length,pixels,exception);
1750 if (compression == LZWCompression)
1751 status=LZWEncodeImage(image,length,pixels,exception);
1753 status=PackbitsEncodeImage(image,length,pixels,exception);
1754 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1755 if (status == MagickFalse)
1757 (void) CloseBlob(image);
1758 return(MagickFalse);
1765 Dump uncompressed DirectColor packets.
1767 Ascii85Initialize(image);
1768 for (y=0; y < (ssize_t) image->rows; y++)
1770 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1771 if (p == (const Quantum *) NULL)
1773 for (x=0; x < (ssize_t) image->columns; x++)
1775 Ascii85Encode(image,ScaleQuantumToChar(GetPixelRed(image,p)));
1776 Ascii85Encode(image,ScaleQuantumToChar(GetPixelGreen(image,p)));
1777 Ascii85Encode(image,ScaleQuantumToChar(GetPixelBlue(image,p)));
1778 if (image->colorspace == CMYKColorspace)
1779 Ascii85Encode(image,ScaleQuantumToChar(
1780 GetPixelBlack(image,p)));
1781 p+=GetPixelChannels(image);
1783 if (image->previous == (Image *) NULL)
1785 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1787 if (status == MagickFalse)
1791 Ascii85Flush(image);
1798 Dump number of colors and colormap.
1800 switch (compression)
1802 case RLECompression:
1806 Allocate pixel array.
1808 length=(size_t) number_pixels;
1809 pixels=(unsigned char *) AcquireQuantumMemory(length,
1811 if (pixels == (unsigned char *) NULL)
1812 ThrowWriterException(ResourceLimitError,
1813 "MemoryAllocationFailed");
1815 Dump Runlength encoded pixels.
1818 for (y=0; y < (ssize_t) image->rows; y++)
1820 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1821 if (p == (const Quantum *) NULL)
1823 for (x=0; x < (ssize_t) image->columns; x++)
1825 *q++=(unsigned char) GetPixelIndex(image,p);
1826 p+=GetPixelChannels(image);
1828 if (image->previous == (Image *) NULL)
1830 status=SetImageProgress(image,SaveImageTag,
1831 (MagickOffsetType) y,image->rows);
1832 if (status == MagickFalse)
1836 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1837 if (compression == ZipCompression)
1838 status=ZLIBEncodeImage(image,length,pixels,exception);
1841 if (compression == LZWCompression)
1842 status=LZWEncodeImage(image,length,pixels,exception);
1844 status=PackbitsEncodeImage(image,length,pixels,exception);
1845 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1846 if (status == MagickFalse)
1848 (void) CloseBlob(image);
1849 return(MagickFalse);
1856 Dump uncompressed PseudoColor packets.
1858 Ascii85Initialize(image);
1859 for (y=0; y < (ssize_t) image->rows; y++)
1861 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1862 if (p == (const Quantum *) NULL)
1864 for (x=0; x < (ssize_t) image->columns; x++)
1866 Ascii85Encode(image,(unsigned char) GetPixelIndex(image,p));
1867 p+=GetPixelChannels(image);
1869 if (image->previous == (Image *) NULL)
1871 status=SetImageProgress(image,SaveImageTag,
1872 (MagickOffsetType) y,image->rows);
1873 if (status == MagickFalse)
1877 Ascii85Flush(image);
1882 offset=TellBlob(image)-offset;
1883 (void) WriteBlobString(image,"\nendstream\n");
1884 (void) WriteBlobString(image,"endobj\n");
1886 Write Length object.
1888 xref[object++]=TellBlob(image);
1889 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1891 (void) WriteBlobString(image,buffer);
1892 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
1893 (void) WriteBlobString(image,buffer);
1894 (void) WriteBlobString(image,"endobj\n");
1896 Write Colorspace object.
1898 xref[object++]=TellBlob(image);
1899 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1901 (void) WriteBlobString(image,buffer);
1902 if (image->colorspace == CMYKColorspace)
1903 (void) CopyMagickString(buffer,"/DeviceCMYK\n",MaxTextExtent);
1905 if ((compression == FaxCompression) ||
1906 (compression == Group4Compression) ||
1907 ((image_info->type != TrueColorType) &&
1908 (IsImageGray(image,exception) != MagickFalse)))
1909 (void) CopyMagickString(buffer,"/DeviceGray\n",MaxTextExtent);
1911 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1912 (compression == JPEGCompression) ||
1913 (compression == JPEG2000Compression))
1914 (void) CopyMagickString(buffer,"/DeviceRGB\n",MaxTextExtent);
1916 (void) FormatLocaleString(buffer,MaxTextExtent,
1917 "[ /Indexed /DeviceRGB %.20g %.20g 0 R ]\n",(double) image->colors-
1918 1,(double) object+3);
1919 (void) WriteBlobString(image,buffer);
1920 (void) WriteBlobString(image,"endobj\n");
1924 SetGeometry(image,&geometry);
1925 (void) ParseMetaGeometry("106x106+0+0>",&geometry.x,&geometry.y,
1926 &geometry.width,&geometry.height);
1927 tile_image=ThumbnailImage(image,geometry.width,geometry.height,exception);
1928 if (tile_image == (Image *) NULL)
1929 return(MagickFalse);
1930 xref[object++]=TellBlob(image);
1931 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1933 (void) WriteBlobString(image,buffer);
1934 (void) WriteBlobString(image,"<<\n");
1935 switch (compression)
1939 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"ASCII85Decode");
1942 case JPEGCompression:
1944 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"DCTDecode");
1945 if (image->colorspace != CMYKColorspace)
1947 (void) WriteBlobString(image,buffer);
1948 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1952 case JPEG2000Compression:
1954 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"JPXDecode");
1955 if (image->colorspace != CMYKColorspace)
1957 (void) WriteBlobString(image,buffer);
1958 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1962 case LZWCompression:
1964 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"LZWDecode");
1967 case ZipCompression:
1969 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"FlateDecode");
1972 case FaxCompression:
1973 case Group4Compression:
1975 (void) CopyMagickString(buffer,"/Filter [ /CCITTFaxDecode ]\n",
1977 (void) WriteBlobString(image,buffer);
1978 (void) FormatLocaleString(buffer,MaxTextExtent,"/DecodeParms [ << "
1979 "/K %s /BlackIs1 false /Columns %.20g /Rows %.20g >> ]\n",CCITTParam,
1980 (double) tile_image->columns,(double) tile_image->rows);
1985 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
1990 (void) WriteBlobString(image,buffer);
1991 (void) FormatLocaleString(buffer,MaxTextExtent,"/Width %.20g\n",(double)
1992 tile_image->columns);
1993 (void) WriteBlobString(image,buffer);
1994 (void) FormatLocaleString(buffer,MaxTextExtent,"/Height %.20g\n",(double)
1996 (void) WriteBlobString(image,buffer);
1997 (void) FormatLocaleString(buffer,MaxTextExtent,"/ColorSpace %.20g 0 R\n",
1999 (void) WriteBlobString(image,buffer);
2000 (void) FormatLocaleString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
2001 (compression == FaxCompression) || (compression == Group4Compression) ?
2003 (void) WriteBlobString(image,buffer);
2004 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
2006 (void) WriteBlobString(image,buffer);
2007 (void) WriteBlobString(image,">>\n");
2008 (void) WriteBlobString(image,"stream\n");
2009 offset=TellBlob(image);
2010 number_pixels=(MagickSizeType) tile_image->columns*tile_image->rows;
2011 if ((compression == FaxCompression) ||
2012 (compression == Group4Compression) ||
2013 ((image_info->type != TrueColorType) &&
2014 (IsImageGray(tile_image,exception) != MagickFalse)))
2016 switch (compression)
2018 case FaxCompression:
2019 case Group4Compression:
2021 if (LocaleCompare(CCITTParam,"0") == 0)
2023 (void) HuffmanEncodeImage(image_info,image,tile_image,
2027 (void) Huffman2DEncodeImage(image_info,image,tile_image,exception);
2030 case JPEGCompression:
2032 status=InjectImageBlob(image_info,image,tile_image,"jpeg",
2034 if (status == MagickFalse)
2036 (void) CloseBlob(image);
2037 return(MagickFalse);
2041 case JPEG2000Compression:
2043 status=InjectImageBlob(image_info,image,tile_image,"jp2",exception);
2044 if (status == MagickFalse)
2046 (void) CloseBlob(image);
2047 return(MagickFalse);
2051 case RLECompression:
2055 Allocate pixel array.
2057 length=(size_t) number_pixels;
2058 pixels=(unsigned char *) AcquireQuantumMemory(length,
2060 if (pixels == (unsigned char *) NULL)
2062 tile_image=DestroyImage(tile_image);
2063 ThrowWriterException(ResourceLimitError,
2064 "MemoryAllocationFailed");
2067 Dump Runlength encoded pixels.
2070 for (y=0; y < (ssize_t) tile_image->rows; y++)
2072 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2074 if (p == (const Quantum *) NULL)
2076 for (x=0; x < (ssize_t) tile_image->columns; x++)
2078 *q++=ScaleQuantumToChar(GetPixelIntensity(tile_image,p));
2079 p+=GetPixelChannels(tile_image);
2082 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2083 if (compression == ZipCompression)
2084 status=ZLIBEncodeImage(image,length,pixels,exception);
2087 if (compression == LZWCompression)
2088 status=LZWEncodeImage(image,length,pixels,exception);
2090 status=PackbitsEncodeImage(image,length,pixels,exception);
2091 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2092 if (status == MagickFalse)
2094 (void) CloseBlob(image);
2095 return(MagickFalse);
2102 Dump uncompressed PseudoColor packets.
2104 Ascii85Initialize(image);
2105 for (y=0; y < (ssize_t) tile_image->rows; y++)
2107 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2109 if (p == (const Quantum *) NULL)
2111 for (x=0; x < (ssize_t) tile_image->columns; x++)
2113 Ascii85Encode(image,
2114 ScaleQuantumToChar(GetPixelIntensity(tile_image,p)));
2115 p+=GetPixelChannels(tile_image);
2118 Ascii85Flush(image);
2124 if ((tile_image->storage_class == DirectClass) ||
2125 (tile_image->colors > 256) || (compression == JPEGCompression) ||
2126 (compression == JPEG2000Compression))
2127 switch (compression)
2129 case JPEGCompression:
2131 status=InjectImageBlob(image_info,image,tile_image,"jpeg",
2133 if (status == MagickFalse)
2135 (void) CloseBlob(image);
2136 return(MagickFalse);
2140 case JPEG2000Compression:
2142 status=InjectImageBlob(image_info,image,tile_image,"jp2",exception);
2143 if (status == MagickFalse)
2145 (void) CloseBlob(image);
2146 return(MagickFalse);
2150 case RLECompression:
2154 Allocate pixel array.
2156 length=(size_t) number_pixels;
2157 pixels=(unsigned char *) AcquireQuantumMemory(length,4*
2159 length*=tile_image->colorspace == CMYKColorspace ? 4UL : 3UL;
2160 if (pixels == (unsigned char *) NULL)
2162 tile_image=DestroyImage(tile_image);
2163 ThrowWriterException(ResourceLimitError,
2164 "MemoryAllocationFailed");
2167 Dump runoffset encoded pixels.
2170 for (y=0; y < (ssize_t) tile_image->rows; y++)
2172 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2174 if (p == (const Quantum *) NULL)
2176 for (x=0; x < (ssize_t) tile_image->columns; x++)
2178 *q++=ScaleQuantumToChar(GetPixelRed(tile_image,p));
2179 *q++=ScaleQuantumToChar(GetPixelGreen(tile_image,p));
2180 *q++=ScaleQuantumToChar(GetPixelBlue(tile_image,p));
2181 if (image->colorspace == CMYKColorspace)
2182 *q++=ScaleQuantumToChar(GetPixelBlack(tile_image,p));
2183 p+=GetPixelChannels(tile_image);
2186 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2187 if (compression == ZipCompression)
2188 status=ZLIBEncodeImage(image,length,pixels,exception);
2191 if (compression == LZWCompression)
2192 status=LZWEncodeImage(image,length,pixels,exception);
2194 status=PackbitsEncodeImage(image,length,pixels,exception);
2195 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2196 if (status == MagickFalse)
2198 (void) CloseBlob(image);
2199 return(MagickFalse);
2206 Dump uncompressed DirectColor packets.
2208 Ascii85Initialize(image);
2209 for (y=0; y < (ssize_t) tile_image->rows; y++)
2211 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2213 if (p == (const Quantum *) NULL)
2215 for (x=0; x < (ssize_t) tile_image->columns; x++)
2217 Ascii85Encode(image,ScaleQuantumToChar(
2218 GetPixelRed(tile_image,p)));
2219 Ascii85Encode(image,ScaleQuantumToChar(
2220 GetPixelGreen(tile_image,p)));
2221 Ascii85Encode(image,ScaleQuantumToChar(
2222 GetPixelBlue(tile_image,p)));
2223 if (image->colorspace == CMYKColorspace)
2224 Ascii85Encode(image,ScaleQuantumToChar(
2225 GetPixelBlack(tile_image,p)));
2226 p+=GetPixelChannels(tile_image);
2229 Ascii85Flush(image);
2236 Dump number of colors and colormap.
2238 switch (compression)
2240 case RLECompression:
2244 Allocate pixel array.
2246 length=(size_t) number_pixels;
2247 pixels=(unsigned char *) AcquireQuantumMemory(length,
2249 if (pixels == (unsigned char *) NULL)
2251 tile_image=DestroyImage(tile_image);
2252 ThrowWriterException(ResourceLimitError,
2253 "MemoryAllocationFailed");
2256 Dump Runlength encoded pixels.
2259 for (y=0; y < (ssize_t) tile_image->rows; y++)
2261 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2263 if (p == (const Quantum *) NULL)
2265 for (x=0; x < (ssize_t) tile_image->columns; x++)
2267 *q++=(unsigned char) GetPixelIndex(tile_image,p);
2268 p+=GetPixelChannels(tile_image);
2271 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2272 if (compression == ZipCompression)
2273 status=ZLIBEncodeImage(image,length,pixels,exception);
2276 if (compression == LZWCompression)
2277 status=LZWEncodeImage(image,length,pixels,exception);
2279 status=PackbitsEncodeImage(image,length,pixels,exception);
2280 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2281 if (status == MagickFalse)
2283 (void) CloseBlob(image);
2284 return(MagickFalse);
2291 Dump uncompressed PseudoColor packets.
2293 Ascii85Initialize(image);
2294 for (y=0; y < (ssize_t) tile_image->rows; y++)
2296 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2298 if (p == (const Quantum *) NULL)
2300 for (x=0; x < (ssize_t) tile_image->columns; x++)
2302 Ascii85Encode(image,(unsigned char)
2303 GetPixelIndex(tile_image,p));
2304 p+=GetPixelChannels(image);
2307 Ascii85Flush(image);
2312 tile_image=DestroyImage(tile_image);
2313 offset=TellBlob(image)-offset;
2314 (void) WriteBlobString(image,"\nendstream\n");
2315 (void) WriteBlobString(image,"endobj\n");
2317 Write Length object.
2319 xref[object++]=TellBlob(image);
2320 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2322 (void) WriteBlobString(image,buffer);
2323 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
2324 (void) WriteBlobString(image,buffer);
2325 (void) WriteBlobString(image,"endobj\n");
2326 xref[object++]=TellBlob(image);
2327 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2329 (void) WriteBlobString(image,buffer);
2330 if ((image->storage_class != DirectClass) && (image->colors <= 256) &&
2331 (compression != FaxCompression) && (compression != Group4Compression))
2334 Write Colormap object.
2336 (void) WriteBlobString(image,"<<\n");
2337 if (compression == NoCompression)
2338 (void) WriteBlobString(image,"/Filter [ /ASCII85Decode ]\n");
2339 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
2341 (void) WriteBlobString(image,buffer);
2342 (void) WriteBlobString(image,">>\n");
2343 (void) WriteBlobString(image,"stream\n");
2344 offset=TellBlob(image);
2345 if (compression == NoCompression)
2346 Ascii85Initialize(image);
2347 for (i=0; i < (ssize_t) image->colors; i++)
2349 if (compression == NoCompression)
2351 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].red));
2352 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].green));
2353 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].blue));
2356 (void) WriteBlobByte(image,
2357 ScaleQuantumToChar(image->colormap[i].red));
2358 (void) WriteBlobByte(image,
2359 ScaleQuantumToChar(image->colormap[i].green));
2360 (void) WriteBlobByte(image,
2361 ScaleQuantumToChar(image->colormap[i].blue));
2363 if (compression == NoCompression)
2364 Ascii85Flush(image);
2365 offset=TellBlob(image)-offset;
2366 (void) WriteBlobString(image,"\nendstream\n");
2368 (void) WriteBlobString(image,"endobj\n");
2370 Write Length object.
2372 xref[object++]=TellBlob(image);
2373 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2375 (void) WriteBlobString(image,buffer);
2376 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
2378 (void) WriteBlobString(image,buffer);
2379 (void) WriteBlobString(image,"endobj\n");
2381 Write softmask object.
2383 xref[object++]=TellBlob(image);
2384 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2386 (void) WriteBlobString(image,buffer);
2387 (void) WriteBlobString(image,"<<\n");
2388 if (image->matte == MagickFalse)
2389 (void) WriteBlobString(image,">>\n");
2392 (void) WriteBlobString(image,"/Type /XObject\n");
2393 (void) WriteBlobString(image,"/Subtype /Image\n");
2394 (void) FormatLocaleString(buffer,MaxTextExtent,"/Name /Ma%.20g\n",
2395 (double) image->scene);
2396 (void) WriteBlobString(image,buffer);
2397 switch (compression)
2401 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
2405 case LZWCompression:
2407 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"LZWDecode");
2410 case ZipCompression:
2412 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
2418 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
2423 (void) WriteBlobString(image,buffer);
2424 (void) FormatLocaleString(buffer,MaxTextExtent,"/Width %.20g\n",(double)
2426 (void) WriteBlobString(image,buffer);
2427 (void) FormatLocaleString(buffer,MaxTextExtent,"/Height %.20g\n",
2428 (double) image->rows);
2429 (void) WriteBlobString(image,buffer);
2430 (void) WriteBlobString(image,"/ColorSpace /DeviceGray\n");
2431 (void) FormatLocaleString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
2432 (compression == FaxCompression) || (compression == Group4Compression)
2434 (void) WriteBlobString(image,buffer);
2435 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
2437 (void) WriteBlobString(image,buffer);
2438 (void) WriteBlobString(image,">>\n");
2439 (void) WriteBlobString(image,"stream\n");
2440 offset=TellBlob(image);
2441 number_pixels=(MagickSizeType) image->columns*image->rows;
2442 switch (compression)
2444 case RLECompression:
2448 Allocate pixel array.
2450 length=(size_t) number_pixels;
2451 pixels=(unsigned char *) AcquireQuantumMemory(length,
2453 if (pixels == (unsigned char *) NULL)
2455 image=DestroyImage(image);
2456 ThrowWriterException(ResourceLimitError,
2457 "MemoryAllocationFailed");
2460 Dump Runlength encoded pixels.
2463 for (y=0; y < (ssize_t) image->rows; y++)
2465 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2466 if (p == (const Quantum *) NULL)
2468 for (x=0; x < (ssize_t) image->columns; x++)
2470 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
2471 p+=GetPixelChannels(image);
2474 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2475 if (compression == ZipCompression)
2476 status=ZLIBEncodeImage(image,length,pixels,exception);
2479 if (compression == LZWCompression)
2480 status=LZWEncodeImage(image,length,pixels,exception);
2482 status=PackbitsEncodeImage(image,length,pixels,exception);
2483 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2484 if (status == MagickFalse)
2486 (void) CloseBlob(image);
2487 return(MagickFalse);
2494 Dump uncompressed PseudoColor packets.
2496 Ascii85Initialize(image);
2497 for (y=0; y < (ssize_t) image->rows; y++)
2499 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2500 if (p == (const Quantum *) NULL)
2502 for (x=0; x < (ssize_t) image->columns; x++)
2504 Ascii85Encode(image,ScaleQuantumToChar(GetPixelAlpha(image,p)));
2505 p+=GetPixelChannels(image);
2508 Ascii85Flush(image);
2512 offset=TellBlob(image)-offset;
2513 (void) WriteBlobString(image,"\nendstream\n");
2515 (void) WriteBlobString(image,"endobj\n");
2517 Write Length object.
2519 xref[object++]=TellBlob(image);
2520 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2522 (void) WriteBlobString(image,buffer);
2523 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
2524 (void) WriteBlobString(image,buffer);
2525 (void) WriteBlobString(image,"endobj\n");
2526 if (GetNextImageInList(image) == (Image *) NULL)
2528 image=SyncNextImageInList(image);
2529 status=SetImageProgress(image,SaveImagesTag,scene++,
2530 GetImageListLength(image));
2531 if (status == MagickFalse)
2533 } while (image_info->adjoin != MagickFalse);
2535 Write Metadata object.
2537 xref[object++]=TellBlob(image);
2539 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2541 (void) WriteBlobString(image,buffer);
2542 (void) WriteBlobString(image,"<<\n");
2543 GetPathComponent(image->filename,BasePath,basename);
2544 (void) FormatLocaleString(buffer,MaxTextExtent,"/Title (%s)\n",
2545 EscapeParenthesis(basename));
2546 (void) WriteBlobString(image,buffer);
2547 seconds=time((time_t *) NULL);
2548 #if defined(MAGICKCORE_HAVE_LOCALTIME_R)
2549 (void) localtime_r(&seconds,&local_time);
2551 (void) memcpy(&local_time,localtime(&seconds),sizeof(local_time));
2553 (void) FormatLocaleString(date,MaxTextExtent,"D:%04d%02d%02d%02d%02d%02d",
2554 local_time.tm_year+1900,local_time.tm_mon+1,local_time.tm_mday,
2555 local_time.tm_hour,local_time.tm_min,local_time.tm_sec);
2556 (void) FormatLocaleString(buffer,MaxTextExtent,"/CreationDate (%s)\n",date);
2557 (void) WriteBlobString(image,buffer);
2558 (void) FormatLocaleString(buffer,MaxTextExtent,"/ModDate (%s)\n",date);
2559 (void) WriteBlobString(image,buffer);
2560 (void) FormatLocaleString(buffer,MaxTextExtent,"/Producer (%s)\n",
2561 EscapeParenthesis(GetMagickVersion((size_t *) NULL)));
2562 (void) WriteBlobString(image,buffer);
2563 (void) WriteBlobString(image,">>\n");
2564 (void) WriteBlobString(image,"endobj\n");
2568 offset=TellBlob(image)-xref[0]+
2569 (LocaleCompare(image_info->magick,"PDFA") == 0 ? 6 : 0)+10;
2570 (void) WriteBlobString(image,"xref\n");
2571 (void) FormatLocaleString(buffer,MaxTextExtent,"0 %.20g\n",(double)
2573 (void) WriteBlobString(image,buffer);
2574 (void) WriteBlobString(image,"0000000000 65535 f \n");
2575 for (i=0; i < (ssize_t) object; i++)
2577 (void) FormatLocaleString(buffer,MaxTextExtent,"%010lu 00000 n \n",
2578 (unsigned long) xref[i]);
2579 (void) WriteBlobString(image,buffer);
2581 (void) WriteBlobString(image,"trailer\n");
2582 (void) WriteBlobString(image,"<<\n");
2583 (void) FormatLocaleString(buffer,MaxTextExtent,"/Size %.20g\n",(double)
2585 (void) WriteBlobString(image,buffer);
2586 (void) FormatLocaleString(buffer,MaxTextExtent,"/Info %.20g 0 R\n",(double)
2588 (void) WriteBlobString(image,buffer);
2589 (void) FormatLocaleString(buffer,MaxTextExtent,"/Root %.20g 0 R\n",(double)
2591 (void) WriteBlobString(image,buffer);
2592 (void) WriteBlobString(image,">>\n");
2593 (void) WriteBlobString(image,"startxref\n");
2594 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
2595 (void) WriteBlobString(image,buffer);
2596 (void) WriteBlobString(image,"%%EOF\n");
2597 xref=(MagickOffsetType *) RelinquishMagickMemory(xref);
2598 (void) CloseBlob(image);