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 CropBox "CropBox"
297 #define DeviceCMYK "DeviceCMYK"
298 #define MediaBox "MediaBox"
299 #define RenderPostscriptText "Rendering Postscript... "
300 #define PDFRotate "Rotate"
301 #define SpotColor "Separation"
302 #define TrimBox "TrimBox"
303 #define PDFVersion "PDF-"
306 command[MaxTextExtent],
307 density[MaxTextExtent],
308 filename[MaxTextExtent],
309 geometry[MaxTextExtent],
310 options[MaxTextExtent],
311 input_filename[MaxTextExtent],
312 postscript_filename[MaxTextExtent];
371 assert(image_info != (const ImageInfo *) NULL);
372 assert(image_info->signature == MagickSignature);
373 if (image_info->debug != MagickFalse)
374 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
375 image_info->filename);
376 assert(exception != (ExceptionInfo *) NULL);
377 assert(exception->signature == MagickSignature);
381 image=AcquireImage(image_info,exception);
382 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
383 if (status == MagickFalse)
385 image=DestroyImageList(image);
386 return((Image *) NULL);
388 status=AcquireUniqueSymbolicLink(image_info->filename,input_filename);
389 if (status == MagickFalse)
391 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
392 image_info->filename);
393 image=DestroyImageList(image);
394 return((Image *) NULL);
397 Set the page density.
399 delta.x=DefaultResolution;
400 delta.y=DefaultResolution;
401 if ((image->resolution.x == 0.0) || (image->resolution.y == 0.0))
403 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
404 image->resolution.x=geometry_info.rho;
405 image->resolution.y=geometry_info.sigma;
406 if ((flags & SigmaValue) == 0)
407 image->resolution.y=image->resolution.x;
409 if (image_info->density != (char *) NULL)
411 flags=ParseGeometry(image_info->density,&geometry_info);
412 image->resolution.x=geometry_info.rho;
413 image->resolution.y=geometry_info.sigma;
414 if ((flags & SigmaValue) == 0)
415 image->resolution.y=image->resolution.x;
417 (void) ParseAbsoluteGeometry(PSPageGeometry,&page);
418 if (image_info->page != (char *) NULL)
419 (void) ParseAbsoluteGeometry(image_info->page,&page);
420 page.width=(size_t) ceil((double) (page.width*image->resolution.x/delta.x)-
422 page.height=(size_t) ceil((double) (page.height*image->resolution.y/delta.y)-
425 Determine page geometry from the PDF media box.
427 cmyk=image_info->colorspace == CMYKColorspace ? MagickTrue : MagickFalse;
429 option=GetImageOption(image_info,"pdf:use-cropbox");
430 if (option != (const char *) NULL)
431 cropbox=IsMagickTrue(option);
433 option=GetImageOption(image_info,"pdf:use-trimbox");
434 if (option != (const char *) NULL)
435 trimbox=IsMagickTrue(option);
438 (void) ResetMagickMemory(&bounding_box,0,sizeof(bounding_box));
439 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
440 (void) ResetMagickMemory(&hires_bounds,0,sizeof(hires_bounds));
441 (void) ResetMagickMemory(command,0,sizeof(command));
444 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
452 if ((c != (int) '/') && (c != (int) '%') &&
453 ((size_t) (p-command) < (MaxTextExtent-1)))
457 if (LocaleNCompare(PDFRotate,command,strlen(PDFRotate)) == 0)
458 count=(ssize_t) sscanf(command,"Rotate %lf",&angle);
460 Is this a CMYK document?
462 if (LocaleNCompare(DeviceCMYK,command,strlen(DeviceCMYK)) == 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 (IsRGBColorspace(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 next=ReadImage(read_info,exception);
663 (void) RelinquishUniqueFileResource(read_info->filename);
664 if (next == (Image *) NULL)
666 AppendImageToList(&pdf_image,next);
668 read_info=DestroyImageInfo(read_info);
669 if (pdf_image == (Image *) NULL)
671 ThrowFileException(exception,DelegateError,"PostscriptDelegateFailed",
672 image_info->filename);
673 image=DestroyImage(image);
674 return((Image *) NULL);
676 if (LocaleCompare(pdf_image->magick,"BMP") == 0)
681 cmyk_image=ConsolidateCMYKImages(pdf_image,exception);
682 if (cmyk_image != (Image *) NULL)
684 pdf_image=DestroyImageList(pdf_image);
685 pdf_image=cmyk_image;
688 if (image_info->number_scenes != 0)
697 Add place holder images to meet the subimage specification requirement.
699 for (i=0; i < (ssize_t) image_info->scene; i++)
701 clone_image=CloneImage(pdf_image,1,1,MagickTrue,exception);
702 if (clone_image != (Image *) NULL)
703 PrependImageToList(&pdf_image,clone_image);
708 (void) CopyMagickString(pdf_image->filename,filename,MaxTextExtent);
709 pdf_image->page=page;
710 (void) CloneImageProfiles(pdf_image,image);
711 (void) CloneImageProperties(pdf_image,image);
712 next=SyncNextImageInList(pdf_image);
713 if (next != (Image *) NULL)
715 } while (next != (Image *) NULL);
716 image=DestroyImage(image);
718 for (next=GetFirstImageInList(pdf_image); next != (Image *) NULL; )
721 next=GetNextImageInList(next);
723 return(GetFirstImageInList(pdf_image));
727 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
731 % R e g i s t e r P D F I m a g e %
735 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
737 % RegisterPDFImage() adds properties for the PDF image format to
738 % the list of supported formats. The properties include the image format
739 % tag, a method to read and/or write the format, whether the format
740 % supports the saving of more than one frame to the same file or blob,
741 % whether the format supports native in-memory I/O, and a brief
742 % description of the format.
744 % The format of the RegisterPDFImage method is:
746 % size_t RegisterPDFImage(void)
749 ModuleExport size_t RegisterPDFImage(void)
754 entry=SetMagickInfo("AI");
755 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
756 entry->encoder=(EncodeImageHandler *) WritePDFImage;
757 entry->adjoin=MagickFalse;
758 entry->blob_support=MagickFalse;
759 entry->seekable_stream=MagickTrue;
760 entry->thread_support=EncoderThreadSupport;
761 entry->description=ConstantString("Adobe Illustrator CS2");
762 entry->module=ConstantString("PDF");
763 (void) RegisterMagickInfo(entry);
764 entry=SetMagickInfo("EPDF");
765 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
766 entry->encoder=(EncodeImageHandler *) WritePDFImage;
767 entry->adjoin=MagickFalse;
768 entry->blob_support=MagickFalse;
769 entry->seekable_stream=MagickTrue;
770 entry->thread_support=EncoderThreadSupport;
771 entry->description=ConstantString("Encapsulated Portable Document Format");
772 entry->module=ConstantString("PDF");
773 (void) RegisterMagickInfo(entry);
774 entry=SetMagickInfo("PDF");
775 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
776 entry->encoder=(EncodeImageHandler *) WritePDFImage;
777 entry->magick=(IsImageFormatHandler *) IsPDF;
778 entry->blob_support=MagickFalse;
779 entry->seekable_stream=MagickTrue;
780 entry->thread_support=EncoderThreadSupport;
781 entry->description=ConstantString("Portable Document Format");
782 entry->module=ConstantString("PDF");
783 (void) RegisterMagickInfo(entry);
784 entry=SetMagickInfo("PDFA");
785 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
786 entry->encoder=(EncodeImageHandler *) WritePDFImage;
787 entry->magick=(IsImageFormatHandler *) IsPDF;
788 entry->blob_support=MagickFalse;
789 entry->seekable_stream=MagickTrue;
790 entry->thread_support=EncoderThreadSupport;
791 entry->description=ConstantString("Portable Document Archive Format");
792 entry->module=ConstantString("PDF");
793 (void) RegisterMagickInfo(entry);
794 return(MagickImageCoderSignature);
798 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
802 % U n r e g i s t e r P D F I m a g e %
806 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
808 % UnregisterPDFImage() removes format registrations made by the
809 % PDF module from the list of supported formats.
811 % The format of the UnregisterPDFImage method is:
813 % UnregisterPDFImage(void)
816 ModuleExport void UnregisterPDFImage(void)
818 (void) UnregisterMagickInfo("AI");
819 (void) UnregisterMagickInfo("EPDF");
820 (void) UnregisterMagickInfo("PDF");
821 (void) UnregisterMagickInfo("PDFA");
825 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
829 % W r i t e P D F I m a g e %
833 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
835 % WritePDFImage() writes an image in the Portable Document image
838 % The format of the WritePDFImage method is:
840 % MagickBooleanType WritePDFImage(const ImageInfo *image_info,
841 % Image *image,ExceptionInfo *exception)
843 % A description of each parameter follows.
845 % o image_info: the image info.
847 % o image: The image.
849 % o exception: return any errors or warnings in this structure.
853 static inline size_t MagickMax(const size_t x,const size_t y)
860 static inline size_t MagickMin(const size_t x,const size_t y)
867 static char *EscapeParenthesis(const char *text)
879 buffer[MaxTextExtent];
883 for (i=0; i < (ssize_t) MagickMin(strlen(text),(MaxTextExtent-escapes-1)); i++)
885 if ((text[i] == '(') || (text[i] == ')'))
896 static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
897 Image *image,Image *inject_image,ExceptionInfo *exception)
915 write_info=CloneImageInfo(image_info);
916 (void) CopyMagickString(write_info->filename,"GROUP4:",MaxTextExtent);
917 (void) CopyMagickString(write_info->magick,"GROUP4",MaxTextExtent);
918 group4_image=CloneImage(inject_image,0,0,MagickTrue,exception);
919 if (group4_image == (Image *) NULL)
921 group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
923 group4_image=DestroyImage(group4_image);
924 if (group4 == (unsigned char *) NULL)
926 write_info=DestroyImageInfo(write_info);
927 if (WriteBlob(image,length,group4) != (ssize_t) length)
929 group4=(unsigned char *) RelinquishMagickMemory(group4);
933 static MagickBooleanType WritePDFImage(const ImageInfo *image_info,Image *image,
934 ExceptionInfo *exception)
936 #define CFormat "/Filter [ /%s ]\n"
937 #define ObjectsPerImage 14
942 "<?xpacket begin=\"%s\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n"
943 "<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"
944 " <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n"
945 " <rdf:Description rdf:about=\"\"\n"
946 " xmlns:xap=\"http://ns.adobe.com/xap/1.0/\">\n"
947 " <xap:ModifyDate>%s</xap:ModifyDate>\n"
948 " <xap:CreateDate>%s</xap:CreateDate>\n"
949 " <xap:MetadataDate>%s</xap:MetadataDate>\n"
950 " <xap:CreatorTool>%s</xap:CreatorTool>\n"
951 " </rdf:Description>\n"
952 " <rdf:Description rdf:about=\"\"\n"
953 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"
954 " <dc:format>application/pdf</dc:format>\n"
955 " </rdf:Description>\n"
956 " <rdf:Description rdf:about=\"\"\n"
957 " xmlns:xapMM=\"http://ns.adobe.com/xap/1.0/mm/\">\n"
958 " <xapMM:DocumentID>uuid:6ec119d7-7982-4f56-808d-dfe64f5b35cf</xapMM:DocumentID>\n"
959 " <xapMM:InstanceID>uuid:a79b99b4-6235-447f-9f6c-ec18ef7555cb</xapMM:InstanceID>\n"
960 " </rdf:Description>\n"
961 " <rdf:Description rdf:about=\"\"\n"
962 " xmlns:pdf=\"http://ns.adobe.com/pdf/1.3/\">\n"
963 " <pdf:Producer>%s</pdf:Producer>\n"
964 " </rdf:Description>\n"
965 " <rdf:Description rdf:about=\"\"\n"
966 " xmlns:pdfaid=\"http://www.aiim.org/pdfa/ns/id/\">\n"
967 " <pdfaid:part>1</pdfaid:part>\n"
968 " <pdfaid:conformance>B</pdfaid:conformance>\n"
969 " </rdf:Description>\n"
972 "<?xpacket end=\"w\"?>\n"
974 XMPProfileMagick[4]= { (char) 0xef, (char) 0xbb, (char) 0xbf, (char) 0x00 };
977 basename[MaxTextExtent],
978 buffer[MaxTextExtent],
981 page_geometry[MaxTextExtent];
1023 register const Quantum
1026 register unsigned char
1056 Open output image file.
1058 assert(image_info != (const ImageInfo *) NULL);
1059 assert(image_info->signature == MagickSignature);
1060 assert(image != (Image *) NULL);
1061 assert(image->signature == MagickSignature);
1062 if (image->debug != MagickFalse)
1063 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1064 assert(exception != (ExceptionInfo *) NULL);
1065 assert(exception->signature == MagickSignature);
1066 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1067 if (status == MagickFalse)
1070 Allocate X ref memory.
1072 xref=(MagickOffsetType *) AcquireQuantumMemory(2048UL,sizeof(*xref));
1073 if (xref == (MagickOffsetType *) NULL)
1074 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1075 (void) ResetMagickMemory(xref,0,2048UL*sizeof(*xref));
1081 if (image_info->compression == JPEG2000Compression)
1082 version=(size_t) MagickMax(version,5);
1083 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
1084 if (next->matte != MagickFalse)
1085 version=(size_t) MagickMax(version,4);
1086 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1087 version=(size_t) MagickMax(version,6);
1088 (void) FormatLocaleString(buffer,MaxTextExtent,"%%PDF-1.%.20g \n",
1090 (void) WriteBlobString(image,buffer);
1091 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1092 (void) WriteBlobString(image,"%âãÏÓ\n");
1094 Write Catalog object.
1096 xref[object++]=TellBlob(image);
1098 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1100 (void) WriteBlobString(image,buffer);
1101 (void) WriteBlobString(image,"<<\n");
1102 if (LocaleCompare(image_info->magick,"PDFA") != 0)
1103 (void) FormatLocaleString(buffer,MaxTextExtent,"/Pages %.20g 0 R\n",
1107 (void) FormatLocaleString(buffer,MaxTextExtent,"/Metadata %.20g 0 R\n",
1109 (void) WriteBlobString(image,buffer);
1110 (void) FormatLocaleString(buffer,MaxTextExtent,"/Pages %.20g 0 R\n",
1113 (void) WriteBlobString(image,buffer);
1114 (void) WriteBlobString(image,"/Type /Catalog\n");
1115 (void) WriteBlobString(image,">>\n");
1116 (void) WriteBlobString(image,"endobj\n");
1117 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1120 create_date[MaxTextExtent],
1121 modify_date[MaxTextExtent],
1122 timestamp[MaxTextExtent],
1123 xmp_profile[MaxTextExtent];
1131 xref[object++]=TellBlob(image);
1132 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",
1134 (void) WriteBlobString(image,buffer);
1135 (void) WriteBlobString(image,"<<\n");
1136 (void) WriteBlobString(image,"/Subtype /XML\n");
1138 value=GetImageProperty(image,"date:modify",exception);
1139 if (value != (const char *) NULL)
1140 (void) CopyMagickString(modify_date,value,MaxTextExtent);
1142 value=GetImageProperty(image,"date:create",exception);
1143 if (value != (const char *) NULL)
1144 (void) CopyMagickString(create_date,value,MaxTextExtent);
1145 (void) FormatMagickTime(time((time_t *) NULL),MaxTextExtent,timestamp);
1146 i=FormatLocaleString(xmp_profile,MaxTextExtent,XMPProfile,
1147 XMPProfileMagick,modify_date,create_date,timestamp,
1148 GetMagickVersion(&version),GetMagickVersion(&version));
1149 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g\n",
1151 (void) WriteBlobString(image,buffer);
1152 (void) WriteBlobString(image,"/Type /Metadata\n");
1153 (void) WriteBlobString(image,">>\nstream\n");
1154 (void) WriteBlobString(image,xmp_profile);
1155 (void) WriteBlobString(image,"endstream\n");
1156 (void) WriteBlobString(image,"endobj\n");
1161 xref[object++]=TellBlob(image);
1163 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1165 (void) WriteBlobString(image,buffer);
1166 (void) WriteBlobString(image,"<<\n");
1167 (void) WriteBlobString(image,"/Type /Pages\n");
1168 (void) FormatLocaleString(buffer,MaxTextExtent,"/Kids [ %.20g 0 R ",
1170 (void) WriteBlobString(image,buffer);
1171 count=(ssize_t) (pages_id+ObjectsPerImage+1);
1172 if (image_info->adjoin != MagickFalse)
1178 Predict page object id's.
1181 for ( ; GetNextImageInList(kid_image) != (Image *) NULL; count+=ObjectsPerImage)
1183 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 R ",(double)
1185 (void) WriteBlobString(image,buffer);
1186 kid_image=GetNextImageInList(kid_image);
1188 xref=(MagickOffsetType *) ResizeQuantumMemory(xref,(size_t) count+2048UL,
1190 if (xref == (MagickOffsetType *) NULL)
1191 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1193 (void) WriteBlobString(image,"]\n");
1194 (void) FormatLocaleString(buffer,MaxTextExtent,"/Count %.20g\n",
1195 (double) ((count-pages_id)/ObjectsPerImage));
1196 (void) WriteBlobString(image,buffer);
1197 (void) WriteBlobString(image,">>\n");
1198 (void) WriteBlobString(image,"endobj\n");
1202 compression=image->compression;
1203 if (image_info->compression != UndefinedCompression)
1204 compression=image_info->compression;
1205 switch (compression)
1207 case FaxCompression:
1208 case Group4Compression:
1210 if ((IsImageMonochrome(image,exception) == MagickFalse) ||
1211 (image->matte != MagickFalse))
1212 compression=RLECompression;
1215 #if !defined(MAGICKCORE_JPEG_DELEGATE)
1216 case JPEGCompression:
1218 compression=RLECompression;
1219 (void) ThrowMagickException(exception,GetMagickModule(),
1220 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
1225 #if !defined(MAGICKCORE_JP2_DELEGATE)
1226 case JPEG2000Compression:
1228 compression=RLECompression;
1229 (void) ThrowMagickException(exception,GetMagickModule(),
1230 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JP2)",
1235 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
1236 case ZipCompression:
1238 compression=RLECompression;
1239 (void) ThrowMagickException(exception,GetMagickModule(),
1240 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZLIB)",
1245 case LZWCompression:
1247 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1248 compression=RLECompression; /* LZW compression is forbidden */
1253 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1254 compression=RLECompression; /* ASCII 85 compression is forbidden */
1260 if (compression == JPEG2000Compression)
1262 if (IsRGBColorspace(image->colorspace) == MagickFalse)
1263 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1266 Scale relative to dots-per-inch.
1268 delta.x=DefaultResolution;
1269 delta.y=DefaultResolution;
1270 resolution.x=image->resolution.x;
1271 resolution.y=image->resolution.y;
1272 if ((resolution.x == 0.0) || (resolution.y == 0.0))
1274 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
1275 resolution.x=geometry_info.rho;
1276 resolution.y=geometry_info.sigma;
1277 if ((flags & SigmaValue) == 0)
1278 resolution.y=resolution.x;
1280 if (image_info->density != (char *) NULL)
1282 flags=ParseGeometry(image_info->density,&geometry_info);
1283 resolution.x=geometry_info.rho;
1284 resolution.y=geometry_info.sigma;
1285 if ((flags & SigmaValue) == 0)
1286 resolution.y=resolution.x;
1288 if (image->units == PixelsPerCentimeterResolution)
1290 resolution.x=(double) ((size_t) (100.0*2.54*resolution.x+0.5)/100.0);
1291 resolution.y=(double) ((size_t) (100.0*2.54*resolution.y+0.5)/100.0);
1293 SetGeometry(image,&geometry);
1294 (void) FormatLocaleString(page_geometry,MaxTextExtent,"%.20gx%.20g",
1295 (double) image->columns,(double) image->rows);
1296 if (image_info->page != (char *) NULL)
1297 (void) CopyMagickString(page_geometry,image_info->page,MaxTextExtent);
1299 if ((image->page.width != 0) && (image->page.height != 0))
1300 (void) FormatLocaleString(page_geometry,MaxTextExtent,
1301 "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,
1302 (double) image->page.height,(double) image->page.x,(double)
1305 if ((image->gravity != UndefinedGravity) &&
1306 (LocaleCompare(image_info->magick,"PDF") == 0))
1307 (void) CopyMagickString(page_geometry,PSPageGeometry,MaxTextExtent);
1308 (void) ConcatenateMagickString(page_geometry,">",MaxTextExtent);
1309 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
1310 &geometry.width,&geometry.height);
1311 scale.x=(double) (geometry.width*delta.x)/resolution.x;
1312 geometry.width=(size_t) floor(scale.x+0.5);
1313 scale.y=(double) (geometry.height*delta.y)/resolution.y;
1314 geometry.height=(size_t) floor(scale.y+0.5);
1315 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
1316 (void) ParseGravityGeometry(image,page_geometry,&page_info,exception);
1317 if (image->gravity != UndefinedGravity)
1319 geometry.x=(-page_info.x);
1320 geometry.y=(ssize_t) (media_info.height+page_info.y-image->rows);
1323 if (image_info->pointsize != 0.0)
1324 pointsize=image_info->pointsize;
1326 value=GetImageProperty(image,"label",exception);
1327 if (value != (const char *) NULL)
1328 text_size=(size_t) (MultilineCensus(value)*pointsize+12);
1333 xref[object++]=TellBlob(image);
1334 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1336 (void) WriteBlobString(image,buffer);
1337 (void) WriteBlobString(image,"<<\n");
1338 (void) WriteBlobString(image,"/Type /Page\n");
1339 (void) FormatLocaleString(buffer,MaxTextExtent,"/Parent %.20g 0 R\n",
1341 (void) WriteBlobString(image,buffer);
1342 (void) WriteBlobString(image,"/Resources <<\n");
1343 labels=(char **) NULL;
1344 value=GetImageProperty(image,"label",exception);
1345 if (value != (const char *) NULL)
1346 labels=StringToList(value);
1347 if (labels != (char **) NULL)
1349 (void) FormatLocaleString(buffer,MaxTextExtent,
1350 "/Font << /F%.20g %.20g 0 R >>\n",(double) image->scene,(double)
1352 (void) WriteBlobString(image,buffer);
1354 (void) FormatLocaleString(buffer,MaxTextExtent,
1355 "/XObject << /Im%.20g %.20g 0 R >>\n",(double) image->scene,(double)
1357 (void) WriteBlobString(image,buffer);
1358 (void) FormatLocaleString(buffer,MaxTextExtent,"/ProcSet %.20g 0 R >>\n",
1360 (void) WriteBlobString(image,buffer);
1361 (void) FormatLocaleString(buffer,MaxTextExtent,
1362 "/MediaBox [0 0 %g %g]\n",72.0*media_info.width/resolution.x,
1363 72.0*media_info.height/resolution.y);
1364 (void) WriteBlobString(image,buffer);
1365 (void) FormatLocaleString(buffer,MaxTextExtent,
1366 "/CropBox [0 0 %g %g]\n",72.0*media_info.width/resolution.x,
1367 72.0*media_info.height/resolution.y);
1368 (void) WriteBlobString(image,buffer);
1369 (void) FormatLocaleString(buffer,MaxTextExtent,"/Contents %.20g 0 R\n",
1371 (void) WriteBlobString(image,buffer);
1372 (void) FormatLocaleString(buffer,MaxTextExtent,"/Thumb %.20g 0 R\n",
1374 (void) WriteBlobString(image,buffer);
1375 (void) WriteBlobString(image,">>\n");
1376 (void) WriteBlobString(image,"endobj\n");
1378 Write Contents object.
1380 xref[object++]=TellBlob(image);
1381 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1383 (void) WriteBlobString(image,buffer);
1384 (void) WriteBlobString(image,"<<\n");
1385 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
1387 (void) WriteBlobString(image,buffer);
1388 (void) WriteBlobString(image,">>\n");
1389 (void) WriteBlobString(image,"stream\n");
1390 offset=TellBlob(image);
1391 (void) WriteBlobString(image,"q\n");
1392 if (labels != (char **) NULL)
1393 for (i=0; labels[i] != (char *) NULL; i++)
1395 (void) WriteBlobString(image,"BT\n");
1396 (void) FormatLocaleString(buffer,MaxTextExtent,"/F%.20g %g Tf\n",
1397 (double) image->scene,pointsize);
1398 (void) WriteBlobString(image,buffer);
1399 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g Td\n",
1400 (double) geometry.x,(double) (geometry.y+geometry.height+i*pointsize+
1402 (void) WriteBlobString(image,buffer);
1403 (void) FormatLocaleString(buffer,MaxTextExtent,"(%s) Tj\n",labels[i]);
1404 (void) WriteBlobString(image,buffer);
1405 (void) WriteBlobString(image,"ET\n");
1406 labels[i]=DestroyString(labels[i]);
1408 (void) FormatLocaleString(buffer,MaxTextExtent,"%g 0 0 %g %.20g %.20g cm\n",
1409 scale.x,scale.y,(double) geometry.x,(double) geometry.y);
1410 (void) WriteBlobString(image,buffer);
1411 (void) FormatLocaleString(buffer,MaxTextExtent,"/Im%.20g Do\n",
1412 (double) image->scene);
1413 (void) WriteBlobString(image,buffer);
1414 (void) WriteBlobString(image,"Q\n");
1415 offset=TellBlob(image)-offset;
1416 (void) WriteBlobString(image,"endstream\n");
1417 (void) WriteBlobString(image,"endobj\n");
1419 Write Length object.
1421 xref[object++]=TellBlob(image);
1422 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1424 (void) WriteBlobString(image,buffer);
1425 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
1426 (void) WriteBlobString(image,buffer);
1427 (void) WriteBlobString(image,"endobj\n");
1429 Write Procset object.
1431 xref[object++]=TellBlob(image);
1432 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",
1434 (void) WriteBlobString(image,buffer);
1435 if ((image->storage_class == DirectClass) || (image->colors > 256))
1436 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageC",MaxTextExtent);
1438 if ((compression == FaxCompression) || (compression == Group4Compression))
1439 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageB",MaxTextExtent);
1441 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageI",MaxTextExtent);
1442 (void) WriteBlobString(image,buffer);
1443 (void) WriteBlobString(image," ]\n");
1444 (void) WriteBlobString(image,"endobj\n");
1448 xref[object++]=TellBlob(image);
1449 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",
1451 (void) WriteBlobString(image,buffer);
1452 (void) WriteBlobString(image,"<<\n");
1453 if (labels != (char **) NULL)
1455 (void) WriteBlobString(image,"/Type /Font\n");
1456 (void) WriteBlobString(image,"/Subtype /Type1\n");
1457 (void) FormatLocaleString(buffer,MaxTextExtent,"/Name /F%.20g\n",
1458 (double) image->scene);
1459 (void) WriteBlobString(image,buffer);
1460 (void) WriteBlobString(image,"/BaseFont /Helvetica\n");
1461 (void) WriteBlobString(image,"/Encoding /MacRomanEncoding\n");
1462 labels=(char **) RelinquishMagickMemory(labels);
1464 (void) WriteBlobString(image,">>\n");
1465 (void) WriteBlobString(image,"endobj\n");
1467 Write XObject object.
1469 xref[object++]=TellBlob(image);
1470 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1472 (void) WriteBlobString(image,buffer);
1473 (void) WriteBlobString(image,"<<\n");
1474 (void) WriteBlobString(image,"/Type /XObject\n");
1475 (void) WriteBlobString(image,"/Subtype /Image\n");
1476 (void) FormatLocaleString(buffer,MaxTextExtent,"/Name /Im%.20g\n",
1477 (double) image->scene);
1478 (void) WriteBlobString(image,buffer);
1479 switch (compression)
1483 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"ASCII85Decode");
1486 case JPEGCompression:
1488 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"DCTDecode");
1489 if (image->colorspace != CMYKColorspace)
1491 (void) WriteBlobString(image,buffer);
1492 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1496 case JPEG2000Compression:
1498 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"JPXDecode");
1499 if (image->colorspace != CMYKColorspace)
1501 (void) WriteBlobString(image,buffer);
1502 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1506 case LZWCompression:
1508 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"LZWDecode");
1511 case ZipCompression:
1513 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"FlateDecode");
1516 case FaxCompression:
1517 case Group4Compression:
1519 (void) CopyMagickString(buffer,"/Filter [ /CCITTFaxDecode ]\n",
1521 (void) WriteBlobString(image,buffer);
1522 (void) FormatLocaleString(buffer,MaxTextExtent,"/DecodeParms [ << "
1523 "/K %s /BlackIs1 false /Columns %.20g /Rows %.20g >> ]\n",CCITTParam,
1524 (double) image->columns,(double) image->rows);
1529 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
1534 (void) WriteBlobString(image,buffer);
1535 (void) FormatLocaleString(buffer,MaxTextExtent,"/Width %.20g\n",(double)
1537 (void) WriteBlobString(image,buffer);
1538 (void) FormatLocaleString(buffer,MaxTextExtent,"/Height %.20g\n",(double)
1540 (void) WriteBlobString(image,buffer);
1541 (void) FormatLocaleString(buffer,MaxTextExtent,"/ColorSpace %.20g 0 R\n",
1543 (void) WriteBlobString(image,buffer);
1544 (void) FormatLocaleString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
1545 (compression == FaxCompression) || (compression == Group4Compression) ?
1547 (void) WriteBlobString(image,buffer);
1548 if (image->matte != MagickFalse)
1550 (void) FormatLocaleString(buffer,MaxTextExtent,"/SMask %.20g 0 R\n",
1552 (void) WriteBlobString(image,buffer);
1554 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
1556 (void) WriteBlobString(image,buffer);
1557 (void) WriteBlobString(image,">>\n");
1558 (void) WriteBlobString(image,"stream\n");
1559 offset=TellBlob(image);
1560 number_pixels=(MagickSizeType) image->columns*image->rows;
1561 if ((4*number_pixels) != (MagickSizeType) ((size_t) (4*number_pixels)))
1562 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1563 if ((compression == FaxCompression) || (compression == Group4Compression) ||
1564 ((image_info->type != TrueColorType) &&
1565 (IsImageGray(image,exception) != MagickFalse)))
1567 switch (compression)
1569 case FaxCompression:
1570 case Group4Compression:
1572 if (LocaleCompare(CCITTParam,"0") == 0)
1574 (void) HuffmanEncodeImage(image_info,image,image,exception);
1577 (void) Huffman2DEncodeImage(image_info,image,image,exception);
1580 case JPEGCompression:
1582 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
1583 if (status == MagickFalse)
1585 (void) CloseBlob(image);
1586 return(MagickFalse);
1590 case JPEG2000Compression:
1592 status=InjectImageBlob(image_info,image,image,"jp2",exception);
1593 if (status == MagickFalse)
1595 (void) CloseBlob(image);
1596 return(MagickFalse);
1600 case RLECompression:
1604 Allocate pixel array.
1606 length=(size_t) number_pixels;
1607 pixels=(unsigned char *) AcquireQuantumMemory(length,
1609 if (pixels == (unsigned char *) NULL)
1610 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1612 Dump Runlength encoded pixels.
1615 for (y=0; y < (ssize_t) image->rows; y++)
1617 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1618 if (p == (const Quantum *) NULL)
1620 for (x=0; x < (ssize_t) image->columns; x++)
1622 *q++=ScaleQuantumToChar(GetPixelIntensity(image,p));
1623 p+=GetPixelChannels(image);
1625 if (image->previous == (Image *) NULL)
1627 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1629 if (status == MagickFalse)
1633 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1634 if (compression == ZipCompression)
1635 status=ZLIBEncodeImage(image,length,pixels,exception);
1638 if (compression == LZWCompression)
1639 status=LZWEncodeImage(image,length,pixels,exception);
1641 status=PackbitsEncodeImage(image,length,pixels,exception);
1642 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1643 if (status == MagickFalse)
1645 (void) CloseBlob(image);
1646 return(MagickFalse);
1653 Dump uncompressed PseudoColor packets.
1655 Ascii85Initialize(image);
1656 for (y=0; y < (ssize_t) image->rows; y++)
1658 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1659 if (p == (const Quantum *) NULL)
1661 for (x=0; x < (ssize_t) image->columns; x++)
1663 Ascii85Encode(image,ScaleQuantumToChar(
1664 GetPixelIntensity(image,p)));
1665 p+=GetPixelChannels(image);
1667 if (image->previous == (Image *) NULL)
1669 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1671 if (status == MagickFalse)
1675 Ascii85Flush(image);
1681 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1682 (compression == JPEGCompression) ||
1683 (compression == JPEG2000Compression))
1684 switch (compression)
1686 case JPEGCompression:
1688 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
1689 if (status == MagickFalse)
1691 (void) CloseBlob(image);
1692 return(MagickFalse);
1696 case JPEG2000Compression:
1698 status=InjectImageBlob(image_info,image,image,"jp2",exception);
1699 if (status == MagickFalse)
1701 (void) CloseBlob(image);
1702 return(MagickFalse);
1706 case RLECompression:
1710 Allocate pixel array.
1712 length=(size_t) number_pixels;
1713 pixels=(unsigned char *) AcquireQuantumMemory(length,
1715 length*=image->colorspace == CMYKColorspace ? 4UL : 3UL;
1716 if (pixels == (unsigned char *) NULL)
1717 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1719 Dump runoffset encoded pixels.
1722 for (y=0; y < (ssize_t) image->rows; y++)
1724 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1725 if (p == (const Quantum *) NULL)
1727 for (x=0; x < (ssize_t) image->columns; x++)
1729 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1730 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1731 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1732 if (image->colorspace == CMYKColorspace)
1733 *q++=ScaleQuantumToChar(GetPixelBlack(image,p));
1734 p+=GetPixelChannels(image);
1736 if (image->previous == (Image *) NULL)
1738 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1740 if (status == MagickFalse)
1744 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1745 if (compression == ZipCompression)
1746 status=ZLIBEncodeImage(image,length,pixels,exception);
1749 if (compression == LZWCompression)
1750 status=LZWEncodeImage(image,length,pixels,exception);
1752 status=PackbitsEncodeImage(image,length,pixels,exception);
1753 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1754 if (status == MagickFalse)
1756 (void) CloseBlob(image);
1757 return(MagickFalse);
1764 Dump uncompressed DirectColor packets.
1766 Ascii85Initialize(image);
1767 for (y=0; y < (ssize_t) image->rows; y++)
1769 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1770 if (p == (const Quantum *) NULL)
1772 for (x=0; x < (ssize_t) image->columns; x++)
1774 Ascii85Encode(image,ScaleQuantumToChar(GetPixelRed(image,p)));
1775 Ascii85Encode(image,ScaleQuantumToChar(GetPixelGreen(image,p)));
1776 Ascii85Encode(image,ScaleQuantumToChar(GetPixelBlue(image,p)));
1777 if (image->colorspace == CMYKColorspace)
1778 Ascii85Encode(image,ScaleQuantumToChar(
1779 GetPixelBlack(image,p)));
1780 p+=GetPixelChannels(image);
1782 if (image->previous == (Image *) NULL)
1784 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1786 if (status == MagickFalse)
1790 Ascii85Flush(image);
1797 Dump number of colors and colormap.
1799 switch (compression)
1801 case RLECompression:
1805 Allocate pixel array.
1807 length=(size_t) number_pixels;
1808 pixels=(unsigned char *) AcquireQuantumMemory(length,
1810 if (pixels == (unsigned char *) NULL)
1811 ThrowWriterException(ResourceLimitError,
1812 "MemoryAllocationFailed");
1814 Dump Runlength encoded pixels.
1817 for (y=0; y < (ssize_t) image->rows; y++)
1819 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1820 if (p == (const Quantum *) NULL)
1822 for (x=0; x < (ssize_t) image->columns; x++)
1824 *q++=(unsigned char) GetPixelIndex(image,p);
1825 p+=GetPixelChannels(image);
1827 if (image->previous == (Image *) NULL)
1829 status=SetImageProgress(image,SaveImageTag,
1830 (MagickOffsetType) y,image->rows);
1831 if (status == MagickFalse)
1835 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1836 if (compression == ZipCompression)
1837 status=ZLIBEncodeImage(image,length,pixels,exception);
1840 if (compression == LZWCompression)
1841 status=LZWEncodeImage(image,length,pixels,exception);
1843 status=PackbitsEncodeImage(image,length,pixels,exception);
1844 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1845 if (status == MagickFalse)
1847 (void) CloseBlob(image);
1848 return(MagickFalse);
1855 Dump uncompressed PseudoColor packets.
1857 Ascii85Initialize(image);
1858 for (y=0; y < (ssize_t) image->rows; y++)
1860 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1861 if (p == (const Quantum *) NULL)
1863 for (x=0; x < (ssize_t) image->columns; x++)
1865 Ascii85Encode(image,(unsigned char) GetPixelIndex(image,p));
1866 p+=GetPixelChannels(image);
1868 if (image->previous == (Image *) NULL)
1870 status=SetImageProgress(image,SaveImageTag,
1871 (MagickOffsetType) y,image->rows);
1872 if (status == MagickFalse)
1876 Ascii85Flush(image);
1881 offset=TellBlob(image)-offset;
1882 (void) WriteBlobString(image,"\nendstream\n");
1883 (void) WriteBlobString(image,"endobj\n");
1885 Write Length object.
1887 xref[object++]=TellBlob(image);
1888 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1890 (void) WriteBlobString(image,buffer);
1891 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",
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 q+=GetPixelChannels(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",
2425 (double) image->columns);
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]+10;
2569 (void) WriteBlobString(image,"xref\n");
2570 (void) FormatLocaleString(buffer,MaxTextExtent,"0 %.20g\n",(double)
2572 (void) WriteBlobString(image,buffer);
2573 (void) WriteBlobString(image,"0000000000 65535 f \n");
2574 for (i=0; i < (ssize_t) object; i++)
2576 (void) FormatLocaleString(buffer,MaxTextExtent,"%010lu 00000 n \n",
2577 (unsigned long) xref[i]);
2578 (void) WriteBlobString(image,buffer);
2580 (void) WriteBlobString(image,"trailer\n");
2581 (void) WriteBlobString(image,"<<\n");
2582 (void) FormatLocaleString(buffer,MaxTextExtent,"/Size %.20g\n",(double)
2584 (void) WriteBlobString(image,buffer);
2585 (void) FormatLocaleString(buffer,MaxTextExtent,"/Info %.20g 0 R\n",(double)
2587 (void) WriteBlobString(image,buffer);
2588 (void) FormatLocaleString(buffer,MaxTextExtent,"/Root %.20g 0 R\n",(double)
2590 (void) WriteBlobString(image,buffer);
2591 (void) WriteBlobString(image,">>\n");
2592 (void) WriteBlobString(image,"startxref\n");
2593 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
2594 (void) WriteBlobString(image,buffer);
2595 (void) WriteBlobString(image,"%%EOF\n");
2596 xref=(MagickOffsetType *) RelinquishMagickMemory(xref);
2597 (void) CloseBlob(image);