2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Portable Document Format %
20 % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "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/transform.h"
77 #include "MagickCore/utility.h"
78 #include "MagickCore/module.h"
83 #if defined(MAGICKCORE_TIFF_DELEGATE)
84 #define CCITTParam "-1"
86 #define CCITTParam "0"
92 static MagickBooleanType
93 WritePDFImage(const ImageInfo *,Image *);
96 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
100 % I n v o k e P D F D e l e g a t e %
104 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106 % InvokePDFDelegate() executes the PDF interpreter with the specified command.
108 % The format of the InvokePDFDelegate method is:
110 % MagickBooleanType InvokePDFDelegate(const MagickBooleanType verbose,
111 % const char *command,ExceptionInfo *exception)
113 % A description of each parameter follows:
115 % o verbose: A value other than zero displays the command prior to
118 % o command: the address of a character string containing the command to
121 % o exception: return any errors or warnings in this structure.
124 static MagickBooleanType InvokePDFDelegate(const MagickBooleanType verbose,
125 const char *command,ExceptionInfo *exception)
130 #if defined(MAGICKCORE_GS_DELEGATE) || defined(MAGICKCORE_WINDOWS_SUPPORT)
147 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
148 ghost_info=NTGhostscriptDLLVectors();
153 ghost_info=(&ghost_info_struct);
154 (void) ResetMagickMemory(&ghost_info,0,sizeof(ghost_info));
155 ghost_info_struct.new_instance=(int (*)(gs_main_instance **,void *))
157 ghost_info_struct.init_with_args=(int (*)(gs_main_instance *,int,char **))
158 gsapi_init_with_args;
159 ghost_info_struct.run_string=(int (*)(gs_main_instance *,const char *,int,
160 int *)) gsapi_run_string;
161 ghost_info_struct.delete_instance=(void (*)(gs_main_instance *))
162 gsapi_delete_instance;
163 ghost_info_struct.exit=(int (*)(gs_main_instance *)) gsapi_exit;
165 if (ghost_info == (GhostInfo *) NULL)
167 status=SystemCommand(MagickFalse,verbose,command,exception);
168 return(status == 0 ? MagickTrue : MagickFalse);
170 if (verbose != MagickFalse)
172 (void) fputs("[ghostscript library]",stdout);
173 (void) fputs(strchr(command,' '),stdout);
175 status=(ghost_info->new_instance)(&interpreter,(void *) NULL);
178 status=SystemCommand(MagickFalse,verbose,command,exception);
179 return(status == 0 ? MagickTrue : MagickFalse);
182 argv=StringToArgv(command,&argc);
183 status=(ghost_info->init_with_args)(interpreter,argc-1,argv+1);
185 status=(ghost_info->run_string)(interpreter,"systemdict /start get exec\n",
187 (ghost_info->exit)(interpreter);
188 (ghost_info->delete_instance)(interpreter);
189 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
190 NTGhostscriptUnLoadDLL();
192 for (i=0; i < (ssize_t) argc; i++)
193 argv[i]=DestroyString(argv[i]);
194 argv=(char **) RelinquishMagickMemory(argv);
195 if ((status != 0) && (status != -101))
200 message=GetExceptionMessage(errno);
201 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
202 "`%s': %s",command,message);
203 message=DestroyString(message);
204 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
205 "Ghostscript returns status %d, exit code %d",status,code);
210 status=SystemCommand(MagickFalse,verbose,command,exception);
211 return(status == 0 ? MagickTrue : MagickFalse);
216 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
224 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
226 % IsPDF() returns MagickTrue if the image format type, identified by the
227 % magick string, is PDF.
229 % The format of the IsPDF method is:
231 % MagickBooleanType IsPDF(const unsigned char *magick,const size_t offset)
233 % A description of each parameter follows:
235 % o magick: compare image format pattern against these bytes.
237 % o offset: Specifies the offset of the magick string.
240 static MagickBooleanType IsPDF(const unsigned char *magick,const size_t offset)
244 if (LocaleNCompare((const char *) magick,"%PDF-",5) == 0)
250 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254 % R e a d P D F I m a g e %
258 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
260 % ReadPDFImage() reads a Portable Document Format image file and
261 % returns it. It allocates the memory necessary for the new Image structure
262 % and returns a pointer to the new image.
264 % The format of the ReadPDFImage method is:
266 % Image *ReadPDFImage(const ImageInfo *image_info,ExceptionInfo *exception)
268 % A description of each parameter follows:
270 % o image_info: the image info.
272 % o exception: return any errors or warnings in this structure.
276 static MagickBooleanType IsPDFRendered(const char *path)
284 if ((path == (const char *) NULL) || (*path == '\0'))
286 status=GetPathAttributes(path,&attributes);
287 if ((status != MagickFalse) && S_ISREG(attributes.st_mode) &&
288 (attributes.st_size > 0))
293 static Image *ReadPDFImage(const ImageInfo *image_info,ExceptionInfo *exception)
295 #define CropBox "CropBox"
296 #define DeviceCMYK "DeviceCMYK"
297 #define MediaBox "MediaBox"
298 #define RenderPostscriptText "Rendering Postscript... "
299 #define PDFRotate "Rotate"
300 #define SpotColor "Separation"
301 #define TrimBox "TrimBox"
302 #define PDFVersion "PDF-"
305 command[MaxTextExtent],
306 density[MaxTextExtent],
307 filename[MaxTextExtent],
308 geometry[MaxTextExtent],
309 options[MaxTextExtent],
310 input_filename[MaxTextExtent],
311 postscript_filename[MaxTextExtent];
370 assert(image_info != (const ImageInfo *) NULL);
371 assert(image_info->signature == MagickSignature);
372 if (image_info->debug != MagickFalse)
373 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
374 image_info->filename);
375 assert(exception != (ExceptionInfo *) NULL);
376 assert(exception->signature == MagickSignature);
380 image=AcquireImage(image_info);
381 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
382 if (status == MagickFalse)
384 image=DestroyImageList(image);
385 return((Image *) NULL);
387 status=AcquireUniqueSymbolicLink(image_info->filename,input_filename);
388 if (status == MagickFalse)
390 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
391 image_info->filename);
392 image=DestroyImageList(image);
393 return((Image *) NULL);
396 Set the page density.
398 delta.x=DefaultResolution;
399 delta.y=DefaultResolution;
400 if ((image->x_resolution == 0.0) || (image->y_resolution == 0.0))
402 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
403 image->x_resolution=geometry_info.rho;
404 image->y_resolution=geometry_info.sigma;
405 if ((flags & SigmaValue) == 0)
406 image->y_resolution=image->x_resolution;
409 Determine page geometry from the PDF media box.
411 cmyk=image_info->colorspace == CMYKColorspace ? MagickTrue : MagickFalse;
413 option=GetImageOption(image_info,"pdf:use-cropbox");
414 if (option != (const char *) NULL)
415 cropbox=IsMagickTrue(option);
417 option=GetImageOption(image_info,"pdf:use-trimbox");
418 if (option != (const char *) NULL)
419 trimbox=IsMagickTrue(option);
422 (void) ResetMagickMemory(&bounding_box,0,sizeof(bounding_box));
423 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
424 (void) ResetMagickMemory(&hires_bounds,0,sizeof(hires_bounds));
425 (void) ResetMagickMemory(&page,0,sizeof(page));
426 (void) ResetMagickMemory(command,0,sizeof(command));
431 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
439 if ((c != (int) '/') && (c != (int) '%') &&
440 ((size_t) (p-command) < (MaxTextExtent-1)))
444 if (LocaleNCompare(PDFRotate,command,strlen(PDFRotate)) == 0)
445 count=(ssize_t) sscanf(command,"Rotate %lf",&angle);
447 Is this a CMYK document?
449 if (LocaleNCompare(DeviceCMYK,command,strlen(DeviceCMYK)) == 0)
451 if (LocaleNCompare(SpotColor,command,strlen(SpotColor)) == 0)
455 property[MaxTextExtent],
464 (void) FormatLocaleString(property,MaxTextExtent,"pdf:SpotColor-%.20g",
465 (double) spotcolor++);
467 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
469 if ((isspace(c) != 0) || (c == '/') || ((i+1) == MaxTextExtent))
474 value=AcquireString(name);
475 (void) SubstituteString(&value,"#20"," ");
476 (void) SetImageProperty(image,property,value);
477 value=DestroyString(value);
480 if (LocaleNCompare(PDFVersion,command,strlen(PDFVersion)) == 0)
481 (void) SetImageProperty(image,"pdf:Version",command);
483 if (cropbox != MagickFalse)
485 if (LocaleNCompare(CropBox,command,strlen(CropBox)) == 0)
488 Note region defined by crop box.
490 count=(ssize_t) sscanf(command,"CropBox [%lf %lf %lf %lf",
491 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
493 count=(ssize_t) sscanf(command,"CropBox[%lf %lf %lf %lf",
494 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
498 if (trimbox != MagickFalse)
500 if (LocaleNCompare(TrimBox,command,strlen(TrimBox)) == 0)
503 Note region defined by trim box.
505 count=(ssize_t) sscanf(command,"TrimBox [%lf %lf %lf %lf",
506 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
508 count=(ssize_t) sscanf(command,"TrimBox[%lf %lf %lf %lf",
509 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
513 if (LocaleNCompare(MediaBox,command,strlen(MediaBox)) == 0)
516 Note region defined by media box.
518 count=(ssize_t) sscanf(command,"MediaBox [%lf %lf %lf %lf",
519 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
521 count=(ssize_t) sscanf(command,"MediaBox[%lf %lf %lf %lf",
522 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
526 if (((bounds.x2 > hires_bounds.x2) && (bounds.y2 > hires_bounds.y2)) ||
527 ((hires_bounds.x2 == 0.0) && (hires_bounds.y2 == 0.0)))
530 Set PDF render geometry.
532 (void) FormatLocaleString(geometry,MaxTextExtent,
533 "%gx%g%+.15g%+.15g",bounds.x2-bounds.x1,bounds.y2-bounds.y1,
534 bounds.x1,bounds.y1);
535 (void) SetImageProperty(image,"pdf:HiResBoundingBox",geometry);
536 page.width=(size_t) floor(bounds.x2-bounds.x1+0.5);
537 page.height=(size_t) floor(bounds.y2-bounds.y1+0.5);
541 (void) CloseBlob(image);
542 if ((fabs(angle) == 90.0) || (fabs(angle) == 270.0))
548 page.width=page.height;
551 if (IsRGBColorspace(image_info->colorspace) != MagickFalse)
554 Create Ghostscript control file.
556 file=AcquireUniqueFileResource(postscript_filename);
559 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
560 image_info->filename);
561 image=DestroyImage(image);
562 return((Image *) NULL);
564 count=write(file," ",1);
567 Render Postscript with the Ghostscript delegate.
569 if ((image_info->ping != MagickFalse) ||
570 (image_info->monochrome != MagickFalse))
571 delegate_info=GetDelegateInfo("ps:mono",(char *) NULL,exception);
573 if (cmyk != MagickFalse)
574 delegate_info=GetDelegateInfo("ps:cmyk",(char *) NULL,exception);
576 delegate_info=GetDelegateInfo("ps:alpha",(char *) NULL,exception);
577 if (delegate_info == (const DelegateInfo *) NULL)
579 (void) RelinquishUniqueFileResource(postscript_filename);
580 image=DestroyImage(image);
581 return((Image *) NULL);
584 if (image_info->density != (char *) NULL)
586 flags=ParseGeometry(image_info->density,&geometry_info);
587 image->x_resolution=geometry_info.rho;
588 image->y_resolution=geometry_info.sigma;
589 if ((flags & SigmaValue) == 0)
590 image->y_resolution=image->x_resolution;
592 (void) FormatLocaleString(density,MaxTextExtent,"%gx%g",image->x_resolution,
593 image->y_resolution);
594 if (image_info->page != (char *) NULL)
596 (void) ParseAbsoluteGeometry(image_info->page,&page);
597 page.width=(size_t) floor((double) (page.width*image->x_resolution/
599 page.height=(size_t) floor((double) (page.height*image->y_resolution/
601 (void) FormatLocaleString(options,MaxTextExtent,"-g%.20gx%.20g ",(double)
602 page.width,(double) page.height);
604 if (cmyk != MagickFalse)
605 (void) ConcatenateMagickString(options,"-dUseCIEColor ",MaxTextExtent);
606 if (cropbox != MagickFalse)
607 (void) ConcatenateMagickString(options,"-dUseCropBox ",MaxTextExtent);
608 if (trimbox != MagickFalse)
609 (void) ConcatenateMagickString(options,"-dUseTrimBox ",MaxTextExtent);
610 read_info=CloneImageInfo(image_info);
611 *read_info->magick='\0';
612 if (read_info->number_scenes != 0)
615 pages[MaxTextExtent];
617 (void) FormatLocaleString(pages,MaxTextExtent,"-dFirstPage=%.20g "
618 "-dLastPage=%.20g",(double) read_info->scene+1,(double)
619 (read_info->scene+read_info->number_scenes));
620 (void) ConcatenateMagickString(options,pages,MaxTextExtent);
621 read_info->number_scenes=0;
622 if (read_info->scenes != (char *) NULL)
623 *read_info->scenes='\0';
625 if (read_info->authenticate != (char *) NULL)
626 (void) FormatLocaleString(options+strlen(options),MaxTextExtent,
627 " -sPDFPassword=%s",read_info->authenticate);
628 (void) CopyMagickString(filename,read_info->filename,MaxTextExtent);
629 (void) AcquireUniqueFilename(filename);
630 (void) ConcatenateMagickString(filename,"-%08d",MaxTextExtent);
631 (void) FormatLocaleString(command,MaxTextExtent,
632 GetDelegateCommands(delegate_info),
633 read_info->antialias != MagickFalse ? 4 : 1,
634 read_info->antialias != MagickFalse ? 4 : 1,density,options,filename,
635 postscript_filename,input_filename);
636 status=InvokePDFDelegate(read_info->verbose,command,exception);
637 (void) RelinquishUniqueFileResource(postscript_filename);
638 (void) RelinquishUniqueFileResource(input_filename);
639 pdf_image=(Image *) NULL;
640 if (status == MagickFalse)
643 (void) InterpretImageFilename(image_info,image,filename,(int) i,
644 read_info->filename);
645 if (IsPDFRendered(read_info->filename) == MagickFalse)
647 (void) RelinquishUniqueFileResource(read_info->filename);
652 (void) InterpretImageFilename(image_info,image,filename,(int) i,
653 read_info->filename);
654 if (IsPDFRendered(read_info->filename) == MagickFalse)
656 next=ReadImage(read_info,exception);
657 (void) RelinquishUniqueFileResource(read_info->filename);
658 if (next == (Image *) NULL)
660 AppendImageToList(&pdf_image,next);
662 read_info=DestroyImageInfo(read_info);
663 if (pdf_image == (Image *) NULL)
665 ThrowFileException(exception,DelegateError,"PostscriptDelegateFailed",
666 image_info->filename);
667 image=DestroyImage(image);
668 return((Image *) NULL);
670 if (LocaleCompare(pdf_image->magick,"BMP") == 0)
675 cmyk_image=ConsolidateCMYKImages(pdf_image,exception);
676 if (cmyk_image != (Image *) NULL)
678 pdf_image=DestroyImageList(pdf_image);
679 pdf_image=cmyk_image;
682 if (image_info->number_scenes != 0)
691 Add place holder images to meet the subimage specification requirement.
693 for (i=0; i < (ssize_t) image_info->scene; i++)
695 clone_image=CloneImage(pdf_image,1,1,MagickTrue,exception);
696 if (clone_image != (Image *) NULL)
697 PrependImageToList(&pdf_image,clone_image);
702 (void) CopyMagickString(pdf_image->filename,filename,MaxTextExtent);
703 pdf_image->page=page;
704 (void) CloneImageProfiles(pdf_image,image);
705 (void) CloneImageProperties(pdf_image,image);
706 next=SyncNextImageInList(pdf_image);
707 if (next != (Image *) NULL)
709 } while (next != (Image *) NULL);
710 image=DestroyImage(image);
712 for (next=GetFirstImageInList(pdf_image); next != (Image *) NULL; )
715 next=GetNextImageInList(next);
717 return(GetFirstImageInList(pdf_image));
721 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
725 % R e g i s t e r P D F I m a g e %
729 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
731 % RegisterPDFImage() adds properties for the PDF image format to
732 % the list of supported formats. The properties include the image format
733 % tag, a method to read and/or write the format, whether the format
734 % supports the saving of more than one frame to the same file or blob,
735 % whether the format supports native in-memory I/O, and a brief
736 % description of the format.
738 % The format of the RegisterPDFImage method is:
740 % size_t RegisterPDFImage(void)
743 ModuleExport size_t RegisterPDFImage(void)
748 entry=SetMagickInfo("AI");
749 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
750 entry->encoder=(EncodeImageHandler *) WritePDFImage;
751 entry->adjoin=MagickFalse;
752 entry->blob_support=MagickFalse;
753 entry->seekable_stream=MagickTrue;
754 entry->thread_support=EncoderThreadSupport;
755 entry->description=ConstantString("Adobe Illustrator CS2");
756 entry->module=ConstantString("PDF");
757 (void) RegisterMagickInfo(entry);
758 entry=SetMagickInfo("EPDF");
759 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
760 entry->encoder=(EncodeImageHandler *) WritePDFImage;
761 entry->adjoin=MagickFalse;
762 entry->blob_support=MagickFalse;
763 entry->seekable_stream=MagickTrue;
764 entry->thread_support=EncoderThreadSupport;
765 entry->description=ConstantString("Encapsulated Portable Document Format");
766 entry->module=ConstantString("PDF");
767 (void) RegisterMagickInfo(entry);
768 entry=SetMagickInfo("PDF");
769 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
770 entry->encoder=(EncodeImageHandler *) WritePDFImage;
771 entry->magick=(IsImageFormatHandler *) IsPDF;
772 entry->blob_support=MagickFalse;
773 entry->seekable_stream=MagickTrue;
774 entry->thread_support=EncoderThreadSupport;
775 entry->description=ConstantString("Portable Document Format");
776 entry->module=ConstantString("PDF");
777 (void) RegisterMagickInfo(entry);
778 entry=SetMagickInfo("PDFA");
779 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
780 entry->encoder=(EncodeImageHandler *) WritePDFImage;
781 entry->magick=(IsImageFormatHandler *) IsPDF;
782 entry->blob_support=MagickFalse;
783 entry->seekable_stream=MagickTrue;
784 entry->thread_support=EncoderThreadSupport;
785 entry->description=ConstantString("Portable Document Archive Format");
786 entry->module=ConstantString("PDF");
787 (void) RegisterMagickInfo(entry);
788 return(MagickImageCoderSignature);
792 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
796 % U n r e g i s t e r P D F I m a g e %
800 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
802 % UnregisterPDFImage() removes format registrations made by the
803 % PDF module from the list of supported formats.
805 % The format of the UnregisterPDFImage method is:
807 % UnregisterPDFImage(void)
810 ModuleExport void UnregisterPDFImage(void)
812 (void) UnregisterMagickInfo("AI");
813 (void) UnregisterMagickInfo("EPDF");
814 (void) UnregisterMagickInfo("PDF");
815 (void) UnregisterMagickInfo("PDFA");
819 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
823 % W r i t e P D F I m a g e %
827 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
829 % WritePDFImage() writes an image in the Portable Document image
832 % The format of the WritePDFImage method is:
834 % MagickBooleanType WritePDFImage(const ImageInfo *image_info,Image *image)
836 % A description of each parameter follows.
838 % o image_info: the image info.
840 % o image: The image.
844 static inline size_t MagickMax(const size_t x,const size_t y)
851 static inline size_t MagickMin(const size_t x,const size_t y)
858 static char *EscapeParenthesis(const char *text)
870 buffer[MaxTextExtent];
874 for (i=0; i < (ssize_t) MagickMin(strlen(text),(MaxTextExtent-escapes-1)); i++)
876 if ((text[i] == '(') || (text[i] == ')'))
887 static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
888 Image *image,Image *inject_image)
906 write_info=CloneImageInfo(image_info);
907 (void) CopyMagickString(write_info->filename,"GROUP4:",MaxTextExtent);
908 (void) CopyMagickString(write_info->magick,"GROUP4",MaxTextExtent);
909 group4_image=CloneImage(inject_image,0,0,MagickTrue,&image->exception);
910 if (group4_image == (Image *) NULL)
912 group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
914 group4_image=DestroyImage(group4_image);
915 if (group4 == (unsigned char *) NULL)
917 write_info=DestroyImageInfo(write_info);
918 if (WriteBlob(image,length,group4) != (ssize_t) length)
920 group4=(unsigned char *) RelinquishMagickMemory(group4);
924 static MagickBooleanType WritePDFImage(const ImageInfo *image_info,Image *image)
926 #define CFormat "/Filter [ /%s ]\n"
927 #define ObjectsPerImage 14
932 "<?xpacket begin=\"%s\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n"
933 "<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"
934 " <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n"
935 " <rdf:Description rdf:about=\"\"\n"
936 " xmlns:xap=\"http://ns.adobe.com/xap/1.0/\">\n"
937 " <xap:ModifyDate>%s</xap:ModifyDate>\n"
938 " <xap:CreateDate>%s</xap:CreateDate>\n"
939 " <xap:MetadataDate>%s</xap:MetadataDate>\n"
940 " <xap:CreatorTool>%s</xap:CreatorTool>\n"
941 " </rdf:Description>\n"
942 " <rdf:Description rdf:about=\"\"\n"
943 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"
944 " <dc:format>application/pdf</dc:format>\n"
945 " </rdf:Description>\n"
946 " <rdf:Description rdf:about=\"\"\n"
947 " xmlns:xapMM=\"http://ns.adobe.com/xap/1.0/mm/\">\n"
948 " <xapMM:DocumentID>uuid:6ec119d7-7982-4f56-808d-dfe64f5b35cf</xapMM:DocumentID>\n"
949 " <xapMM:InstanceID>uuid:a79b99b4-6235-447f-9f6c-ec18ef7555cb</xapMM:InstanceID>\n"
950 " </rdf:Description>\n"
951 " <rdf:Description rdf:about=\"\"\n"
952 " xmlns:pdf=\"http://ns.adobe.com/pdf/1.3/\">\n"
953 " <pdf:Producer>%s</pdf:Producer>\n"
954 " </rdf:Description>\n"
955 " <rdf:Description rdf:about=\"\"\n"
956 " xmlns:pdfaid=\"http://www.aiim.org/pdfa/ns/id/\">\n"
957 " <pdfaid:part>1</pdfaid:part>\n"
958 " <pdfaid:conformance>B</pdfaid:conformance>\n"
959 " </rdf:Description>\n"
962 "<?xpacket end=\"w\"?>\n"
964 XMPProfileMagick[4]= { (char) 0xef, (char) 0xbb, (char) 0xbf, (char) 0x00 };
967 basename[MaxTextExtent],
968 buffer[MaxTextExtent],
971 page_geometry[MaxTextExtent];
1013 register const Quantum
1016 register unsigned char
1046 Open output image file.
1048 assert(image_info != (const ImageInfo *) NULL);
1049 assert(image_info->signature == MagickSignature);
1050 assert(image != (Image *) NULL);
1051 assert(image->signature == MagickSignature);
1052 if (image->debug != MagickFalse)
1053 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1054 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1055 if (status == MagickFalse)
1058 Allocate X ref memory.
1060 xref=(MagickOffsetType *) AcquireQuantumMemory(2048UL,sizeof(*xref));
1061 if (xref == (MagickOffsetType *) NULL)
1062 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1063 (void) ResetMagickMemory(xref,0,2048UL*sizeof(*xref));
1069 if (image_info->compression == JPEG2000Compression)
1070 version=(size_t) MagickMax(version,5);
1071 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
1072 if (next->matte != MagickFalse)
1073 version=(size_t) MagickMax(version,4);
1074 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1075 version=(size_t) MagickMax(version,6);
1076 (void) FormatLocaleString(buffer,MaxTextExtent,"%%PDF-1.%.20g \n",
1078 (void) WriteBlobString(image,buffer);
1079 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1080 (void) WriteBlobString(image,"%âãÏÓ\n");
1082 Write Catalog object.
1084 xref[object++]=TellBlob(image);
1086 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1088 (void) WriteBlobString(image,buffer);
1089 (void) WriteBlobString(image,"<<\n");
1090 if (LocaleCompare(image_info->magick,"PDFA") != 0)
1091 (void) FormatLocaleString(buffer,MaxTextExtent,"/Pages %.20g 0 R\n",
1095 (void) FormatLocaleString(buffer,MaxTextExtent,"/Metadata %.20g 0 R\n",
1097 (void) WriteBlobString(image,buffer);
1098 (void) FormatLocaleString(buffer,MaxTextExtent,"/Pages %.20g 0 R\n",
1101 (void) WriteBlobString(image,buffer);
1102 (void) WriteBlobString(image,"/Type /Catalog\n");
1103 (void) WriteBlobString(image,">>\n");
1104 (void) WriteBlobString(image,"endobj\n");
1105 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1108 create_date[MaxTextExtent],
1109 modify_date[MaxTextExtent],
1110 timestamp[MaxTextExtent],
1111 xmp_profile[MaxTextExtent];
1119 xref[object++]=TellBlob(image);
1120 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",
1122 (void) WriteBlobString(image,buffer);
1123 (void) WriteBlobString(image,"<<\n");
1124 (void) WriteBlobString(image,"/Subtype /XML\n");
1126 value=GetImageProperty(image,"date:modify");
1127 if (value != (const char *) NULL)
1128 (void) CopyMagickString(modify_date,value,MaxTextExtent);
1130 value=GetImageProperty(image,"date:create");
1131 if (value != (const char *) NULL)
1132 (void) CopyMagickString(create_date,value,MaxTextExtent);
1133 (void) FormatMagickTime(time((time_t *) NULL),MaxTextExtent,timestamp);
1134 i=FormatLocaleString(xmp_profile,MaxTextExtent,XMPProfile,
1135 XMPProfileMagick,modify_date,create_date,timestamp,
1136 GetMagickVersion(&version),GetMagickVersion(&version));
1137 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g\n",
1139 (void) WriteBlobString(image,buffer);
1140 (void) WriteBlobString(image,"/Type /Metadata\n");
1141 (void) WriteBlobString(image,">>\nstream\n");
1142 (void) WriteBlobString(image,xmp_profile);
1143 (void) WriteBlobString(image,"endstream\n");
1144 (void) WriteBlobString(image,"endobj\n");
1149 xref[object++]=TellBlob(image);
1151 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1153 (void) WriteBlobString(image,buffer);
1154 (void) WriteBlobString(image,"<<\n");
1155 (void) WriteBlobString(image,"/Type /Pages\n");
1156 (void) FormatLocaleString(buffer,MaxTextExtent,"/Kids [ %.20g 0 R ",
1158 (void) WriteBlobString(image,buffer);
1159 count=(ssize_t) (pages_id+ObjectsPerImage+1);
1160 if (image_info->adjoin != MagickFalse)
1166 Predict page object id's.
1169 for ( ; GetNextImageInList(kid_image) != (Image *) NULL; count+=ObjectsPerImage)
1171 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 R ",(double)
1173 (void) WriteBlobString(image,buffer);
1174 kid_image=GetNextImageInList(kid_image);
1176 xref=(MagickOffsetType *) ResizeQuantumMemory(xref,(size_t) count+2048UL,
1178 if (xref == (MagickOffsetType *) NULL)
1179 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1181 (void) WriteBlobString(image,"]\n");
1182 (void) FormatLocaleString(buffer,MaxTextExtent,"/Count %.20g\n",
1183 (double) ((count-pages_id)/ObjectsPerImage));
1184 (void) WriteBlobString(image,buffer);
1185 (void) WriteBlobString(image,">>\n");
1186 (void) WriteBlobString(image,"endobj\n");
1190 compression=image->compression;
1191 if (image_info->compression != UndefinedCompression)
1192 compression=image_info->compression;
1193 switch (compression)
1195 case FaxCompression:
1196 case Group4Compression:
1198 if ((IsImageMonochrome(image,&image->exception) == MagickFalse) ||
1199 (image->matte != MagickFalse))
1200 compression=RLECompression;
1203 #if !defined(MAGICKCORE_JPEG_DELEGATE)
1204 case JPEGCompression:
1206 compression=RLECompression;
1207 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1208 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
1213 #if !defined(MAGICKCORE_JP2_DELEGATE)
1214 case JPEG2000Compression:
1216 compression=RLECompression;
1217 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1218 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JP2)",
1223 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
1224 case ZipCompression:
1226 compression=RLECompression;
1227 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1228 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZLIB)",
1233 case LZWCompression:
1235 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1236 compression=RLECompression; /* LZW compression is forbidden */
1241 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1242 compression=RLECompression; /* ASCII 85 compression is forbidden */
1248 if (compression == JPEG2000Compression)
1250 if (IsRGBColorspace(image->colorspace) == MagickFalse)
1251 (void) TransformImageColorspace(image,RGBColorspace);
1254 Scale relative to dots-per-inch.
1256 delta.x=DefaultResolution;
1257 delta.y=DefaultResolution;
1258 resolution.x=image->x_resolution;
1259 resolution.y=image->y_resolution;
1260 if ((resolution.x == 0.0) || (resolution.y == 0.0))
1262 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
1263 resolution.x=geometry_info.rho;
1264 resolution.y=geometry_info.sigma;
1265 if ((flags & SigmaValue) == 0)
1266 resolution.y=resolution.x;
1268 if (image_info->density != (char *) NULL)
1270 flags=ParseGeometry(image_info->density,&geometry_info);
1271 resolution.x=geometry_info.rho;
1272 resolution.y=geometry_info.sigma;
1273 if ((flags & SigmaValue) == 0)
1274 resolution.y=resolution.x;
1276 if (image->units == PixelsPerCentimeterResolution)
1278 resolution.x=(double) ((size_t) (100.0*2.54*resolution.x+0.5)/100.0);
1279 resolution.y=(double) ((size_t) (100.0*2.54*resolution.y+0.5)/100.0);
1281 SetGeometry(image,&geometry);
1282 (void) FormatLocaleString(page_geometry,MaxTextExtent,"%.20gx%.20g",
1283 (double) image->columns,(double) image->rows);
1284 if (image_info->page != (char *) NULL)
1285 (void) CopyMagickString(page_geometry,image_info->page,MaxTextExtent);
1287 if ((image->page.width != 0) && (image->page.height != 0))
1288 (void) FormatLocaleString(page_geometry,MaxTextExtent,
1289 "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,
1290 (double) image->page.height,(double) image->page.x,(double)
1293 if ((image->gravity != UndefinedGravity) &&
1294 (LocaleCompare(image_info->magick,"PDF") == 0))
1295 (void) CopyMagickString(page_geometry,PSPageGeometry,MaxTextExtent);
1296 (void) ConcatenateMagickString(page_geometry,">",MaxTextExtent);
1297 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
1298 &geometry.width,&geometry.height);
1299 scale.x=(double) (geometry.width*delta.x)/resolution.x;
1300 geometry.width=(size_t) floor(scale.x+0.5);
1301 scale.y=(double) (geometry.height*delta.y)/resolution.y;
1302 geometry.height=(size_t) floor(scale.y+0.5);
1303 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
1304 (void) ParseGravityGeometry(image,page_geometry,&page_info,
1306 if (image->gravity != UndefinedGravity)
1308 geometry.x=(-page_info.x);
1309 geometry.y=(ssize_t) (media_info.height+page_info.y-image->rows);
1312 if (image_info->pointsize != 0.0)
1313 pointsize=image_info->pointsize;
1315 value=GetImageProperty(image,"label");
1316 if (value != (const char *) NULL)
1317 text_size=(size_t) (MultilineCensus(value)*pointsize+12);
1322 xref[object++]=TellBlob(image);
1323 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1325 (void) WriteBlobString(image,buffer);
1326 (void) WriteBlobString(image,"<<\n");
1327 (void) WriteBlobString(image,"/Type /Page\n");
1328 (void) FormatLocaleString(buffer,MaxTextExtent,"/Parent %.20g 0 R\n",
1330 (void) WriteBlobString(image,buffer);
1331 (void) WriteBlobString(image,"/Resources <<\n");
1332 labels=(char **) NULL;
1333 value=GetImageProperty(image,"label");
1334 if (value != (const char *) NULL)
1335 labels=StringToList(value);
1336 if (labels != (char **) NULL)
1338 (void) FormatLocaleString(buffer,MaxTextExtent,
1339 "/Font << /F%.20g %.20g 0 R >>\n",(double) image->scene,(double)
1341 (void) WriteBlobString(image,buffer);
1343 (void) FormatLocaleString(buffer,MaxTextExtent,
1344 "/XObject << /Im%.20g %.20g 0 R >>\n",(double) image->scene,(double)
1346 (void) WriteBlobString(image,buffer);
1347 (void) FormatLocaleString(buffer,MaxTextExtent,"/ProcSet %.20g 0 R >>\n",
1349 (void) WriteBlobString(image,buffer);
1350 (void) FormatLocaleString(buffer,MaxTextExtent,
1351 "/MediaBox [0 0 %g %g]\n",72.0*media_info.width/resolution.x,
1352 72.0*media_info.height/resolution.y);
1353 (void) WriteBlobString(image,buffer);
1354 (void) FormatLocaleString(buffer,MaxTextExtent,
1355 "/CropBox [0 0 %g %g]\n",72.0*media_info.width/resolution.x,
1356 72.0*media_info.height/resolution.y);
1357 (void) WriteBlobString(image,buffer);
1358 (void) FormatLocaleString(buffer,MaxTextExtent,"/Contents %.20g 0 R\n",
1360 (void) WriteBlobString(image,buffer);
1361 (void) FormatLocaleString(buffer,MaxTextExtent,"/Thumb %.20g 0 R\n",
1363 (void) WriteBlobString(image,buffer);
1364 (void) WriteBlobString(image,">>\n");
1365 (void) WriteBlobString(image,"endobj\n");
1367 Write Contents object.
1369 xref[object++]=TellBlob(image);
1370 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1372 (void) WriteBlobString(image,buffer);
1373 (void) WriteBlobString(image,"<<\n");
1374 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
1376 (void) WriteBlobString(image,buffer);
1377 (void) WriteBlobString(image,">>\n");
1378 (void) WriteBlobString(image,"stream\n");
1379 offset=TellBlob(image);
1380 (void) WriteBlobString(image,"q\n");
1381 if (labels != (char **) NULL)
1382 for (i=0; labels[i] != (char *) NULL; i++)
1384 (void) WriteBlobString(image,"BT\n");
1385 (void) FormatLocaleString(buffer,MaxTextExtent,"/F%.20g %g Tf\n",
1386 (double) image->scene,pointsize);
1387 (void) WriteBlobString(image,buffer);
1388 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g Td\n",
1389 (double) geometry.x,(double) (geometry.y+geometry.height+i*pointsize+
1391 (void) WriteBlobString(image,buffer);
1392 (void) FormatLocaleString(buffer,MaxTextExtent,"(%s) Tj\n",labels[i]);
1393 (void) WriteBlobString(image,buffer);
1394 (void) WriteBlobString(image,"ET\n");
1395 labels[i]=DestroyString(labels[i]);
1397 (void) FormatLocaleString(buffer,MaxTextExtent,"%g 0 0 %g %.20g %.20g cm\n",
1398 scale.x,scale.y,(double) geometry.x,(double) geometry.y);
1399 (void) WriteBlobString(image,buffer);
1400 (void) FormatLocaleString(buffer,MaxTextExtent,"/Im%.20g Do\n",
1401 (double) image->scene);
1402 (void) WriteBlobString(image,buffer);
1403 (void) WriteBlobString(image,"Q\n");
1404 offset=TellBlob(image)-offset;
1405 (void) WriteBlobString(image,"endstream\n");
1406 (void) WriteBlobString(image,"endobj\n");
1408 Write Length object.
1410 xref[object++]=TellBlob(image);
1411 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1413 (void) WriteBlobString(image,buffer);
1414 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
1415 (void) WriteBlobString(image,buffer);
1416 (void) WriteBlobString(image,"endobj\n");
1418 Write Procset object.
1420 xref[object++]=TellBlob(image);
1421 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",
1423 (void) WriteBlobString(image,buffer);
1424 if ((image->storage_class == DirectClass) || (image->colors > 256))
1425 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageC",MaxTextExtent);
1427 if ((compression == FaxCompression) || (compression == Group4Compression))
1428 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageB",MaxTextExtent);
1430 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageI",MaxTextExtent);
1431 (void) WriteBlobString(image,buffer);
1432 (void) WriteBlobString(image," ]\n");
1433 (void) WriteBlobString(image,"endobj\n");
1437 xref[object++]=TellBlob(image);
1438 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",
1440 (void) WriteBlobString(image,buffer);
1441 (void) WriteBlobString(image,"<<\n");
1442 if (labels != (char **) NULL)
1444 (void) WriteBlobString(image,"/Type /Font\n");
1445 (void) WriteBlobString(image,"/Subtype /Type1\n");
1446 (void) FormatLocaleString(buffer,MaxTextExtent,"/Name /F%.20g\n",
1447 (double) image->scene);
1448 (void) WriteBlobString(image,buffer);
1449 (void) WriteBlobString(image,"/BaseFont /Helvetica\n");
1450 (void) WriteBlobString(image,"/Encoding /MacRomanEncoding\n");
1451 labels=(char **) RelinquishMagickMemory(labels);
1453 (void) WriteBlobString(image,">>\n");
1454 (void) WriteBlobString(image,"endobj\n");
1456 Write XObject object.
1458 xref[object++]=TellBlob(image);
1459 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1461 (void) WriteBlobString(image,buffer);
1462 (void) WriteBlobString(image,"<<\n");
1463 (void) WriteBlobString(image,"/Type /XObject\n");
1464 (void) WriteBlobString(image,"/Subtype /Image\n");
1465 (void) FormatLocaleString(buffer,MaxTextExtent,"/Name /Im%.20g\n",
1466 (double) image->scene);
1467 (void) WriteBlobString(image,buffer);
1468 switch (compression)
1472 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"ASCII85Decode");
1475 case JPEGCompression:
1477 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"DCTDecode");
1478 if (image->colorspace != CMYKColorspace)
1480 (void) WriteBlobString(image,buffer);
1481 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1485 case JPEG2000Compression:
1487 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"JPXDecode");
1488 if (image->colorspace != CMYKColorspace)
1490 (void) WriteBlobString(image,buffer);
1491 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1495 case LZWCompression:
1497 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"LZWDecode");
1500 case ZipCompression:
1502 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"FlateDecode");
1505 case FaxCompression:
1506 case Group4Compression:
1508 (void) CopyMagickString(buffer,"/Filter [ /CCITTFaxDecode ]\n",
1510 (void) WriteBlobString(image,buffer);
1511 (void) FormatLocaleString(buffer,MaxTextExtent,"/DecodeParms [ << "
1512 "/K %s /BlackIs1 false /Columns %.20g /Rows %.20g >> ]\n",CCITTParam,
1513 (double) image->columns,(double) image->rows);
1518 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
1523 (void) WriteBlobString(image,buffer);
1524 (void) FormatLocaleString(buffer,MaxTextExtent,"/Width %.20g\n",(double)
1526 (void) WriteBlobString(image,buffer);
1527 (void) FormatLocaleString(buffer,MaxTextExtent,"/Height %.20g\n",(double)
1529 (void) WriteBlobString(image,buffer);
1530 (void) FormatLocaleString(buffer,MaxTextExtent,"/ColorSpace %.20g 0 R\n",
1532 (void) WriteBlobString(image,buffer);
1533 (void) FormatLocaleString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
1534 (compression == FaxCompression) || (compression == Group4Compression) ?
1536 (void) WriteBlobString(image,buffer);
1537 if (image->matte != MagickFalse)
1539 (void) FormatLocaleString(buffer,MaxTextExtent,"/SMask %.20g 0 R\n",
1541 (void) WriteBlobString(image,buffer);
1543 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
1545 (void) WriteBlobString(image,buffer);
1546 (void) WriteBlobString(image,">>\n");
1547 (void) WriteBlobString(image,"stream\n");
1548 offset=TellBlob(image);
1549 number_pixels=(MagickSizeType) image->columns*image->rows;
1550 if ((4*number_pixels) != (MagickSizeType) ((size_t) (4*number_pixels)))
1551 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1552 if ((compression == FaxCompression) || (compression == Group4Compression) ||
1553 ((image_info->type != TrueColorType) &&
1554 (IsImageGray(image,&image->exception) != MagickFalse)))
1556 switch (compression)
1558 case FaxCompression:
1559 case Group4Compression:
1561 if (LocaleCompare(CCITTParam,"0") == 0)
1563 (void) HuffmanEncodeImage(image_info,image,image);
1566 (void) Huffman2DEncodeImage(image_info,image,image);
1569 case JPEGCompression:
1571 status=InjectImageBlob(image_info,image,image,"jpeg",
1573 if (status == MagickFalse)
1574 ThrowWriterException(CoderError,image->exception.reason);
1577 case JPEG2000Compression:
1579 status=InjectImageBlob(image_info,image,image,"jp2",
1581 if (status == MagickFalse)
1582 ThrowWriterException(CoderError,image->exception.reason);
1585 case RLECompression:
1589 Allocate pixel array.
1591 length=(size_t) number_pixels;
1592 pixels=(unsigned char *) AcquireQuantumMemory(length,
1594 if (pixels == (unsigned char *) NULL)
1595 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1597 Dump Runlength encoded pixels.
1600 for (y=0; y < (ssize_t) image->rows; y++)
1602 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1603 if (p == (const Quantum *) NULL)
1605 for (x=0; x < (ssize_t) image->columns; x++)
1607 *q++=ScaleQuantumToChar(GetPixelIntensity(image,p));
1608 p+=GetPixelChannels(image);
1610 if (image->previous == (Image *) NULL)
1612 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1614 if (status == MagickFalse)
1618 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1619 if (compression == ZipCompression)
1620 status=ZLIBEncodeImage(image,length,pixels);
1623 if (compression == LZWCompression)
1624 status=LZWEncodeImage(image,length,pixels);
1626 status=PackbitsEncodeImage(image,length,pixels);
1627 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1628 if (status == MagickFalse)
1630 (void) CloseBlob(image);
1631 return(MagickFalse);
1638 Dump uncompressed PseudoColor packets.
1640 Ascii85Initialize(image);
1641 for (y=0; y < (ssize_t) image->rows; y++)
1643 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1644 if (p == (const Quantum *) NULL)
1646 for (x=0; x < (ssize_t) image->columns; x++)
1648 Ascii85Encode(image,ScaleQuantumToChar(
1649 GetPixelIntensity(image,p)));
1650 p+=GetPixelChannels(image);
1652 if (image->previous == (Image *) NULL)
1654 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1656 if (status == MagickFalse)
1660 Ascii85Flush(image);
1666 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1667 (compression == JPEGCompression) ||
1668 (compression == JPEG2000Compression))
1669 switch (compression)
1671 case JPEGCompression:
1673 status=InjectImageBlob(image_info,image,image,"jpeg",
1675 if (status == MagickFalse)
1676 ThrowWriterException(CoderError,image->exception.reason);
1679 case JPEG2000Compression:
1681 status=InjectImageBlob(image_info,image,image,"jp2",
1683 if (status == MagickFalse)
1684 ThrowWriterException(CoderError,image->exception.reason);
1687 case RLECompression:
1691 Allocate pixel array.
1693 length=(size_t) number_pixels;
1694 pixels=(unsigned char *) AcquireQuantumMemory(length,
1696 length*=image->colorspace == CMYKColorspace ? 4UL : 3UL;
1697 if (pixels == (unsigned char *) NULL)
1698 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1700 Dump runoffset encoded pixels.
1703 for (y=0; y < (ssize_t) image->rows; y++)
1705 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1706 if (p == (const Quantum *) NULL)
1708 for (x=0; x < (ssize_t) image->columns; x++)
1710 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1711 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1712 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1713 if (image->colorspace == CMYKColorspace)
1714 *q++=ScaleQuantumToChar(GetPixelBlack(image,p));
1715 p+=GetPixelChannels(image);
1717 if (image->previous == (Image *) NULL)
1719 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1721 if (status == MagickFalse)
1725 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1726 if (compression == ZipCompression)
1727 status=ZLIBEncodeImage(image,length,pixels);
1730 if (compression == LZWCompression)
1731 status=LZWEncodeImage(image,length,pixels);
1733 status=PackbitsEncodeImage(image,length,pixels);
1734 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1735 if (status == MagickFalse)
1737 (void) CloseBlob(image);
1738 return(MagickFalse);
1745 Dump uncompressed DirectColor packets.
1747 Ascii85Initialize(image);
1748 for (y=0; y < (ssize_t) image->rows; y++)
1750 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1751 if (p == (const Quantum *) NULL)
1753 for (x=0; x < (ssize_t) image->columns; x++)
1755 Ascii85Encode(image,ScaleQuantumToChar(GetPixelRed(image,p)));
1756 Ascii85Encode(image,ScaleQuantumToChar(GetPixelGreen(image,p)));
1757 Ascii85Encode(image,ScaleQuantumToChar(GetPixelBlue(image,p)));
1758 if (image->colorspace == CMYKColorspace)
1759 Ascii85Encode(image,ScaleQuantumToChar(
1760 GetPixelBlack(image,p)));
1761 p+=GetPixelChannels(image);
1763 if (image->previous == (Image *) NULL)
1765 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1767 if (status == MagickFalse)
1771 Ascii85Flush(image);
1778 Dump number of colors and colormap.
1780 switch (compression)
1782 case RLECompression:
1786 Allocate pixel array.
1788 length=(size_t) number_pixels;
1789 pixels=(unsigned char *) AcquireQuantumMemory(length,
1791 if (pixels == (unsigned char *) NULL)
1792 ThrowWriterException(ResourceLimitError,
1793 "MemoryAllocationFailed");
1795 Dump Runlength encoded pixels.
1798 for (y=0; y < (ssize_t) image->rows; y++)
1800 p=GetVirtualPixels(image,0,y,image->columns,1,
1802 if (p == (const Quantum *) NULL)
1804 for (x=0; x < (ssize_t) image->columns; x++)
1806 *q++=(unsigned char) GetPixelIndex(image,p);
1807 p+=GetPixelChannels(image);
1809 if (image->previous == (Image *) NULL)
1811 status=SetImageProgress(image,SaveImageTag,
1812 (MagickOffsetType) y,image->rows);
1813 if (status == MagickFalse)
1817 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1818 if (compression == ZipCompression)
1819 status=ZLIBEncodeImage(image,length,pixels);
1822 if (compression == LZWCompression)
1823 status=LZWEncodeImage(image,length,pixels);
1825 status=PackbitsEncodeImage(image,length,pixels);
1826 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1827 if (status == MagickFalse)
1829 (void) CloseBlob(image);
1830 return(MagickFalse);
1837 Dump uncompressed PseudoColor packets.
1839 Ascii85Initialize(image);
1840 for (y=0; y < (ssize_t) image->rows; y++)
1842 p=GetVirtualPixels(image,0,y,image->columns,1,
1844 if (p == (const Quantum *) NULL)
1846 for (x=0; x < (ssize_t) image->columns; x++)
1848 Ascii85Encode(image,(unsigned char) GetPixelIndex(image,p));
1849 p+=GetPixelChannels(image);
1851 if (image->previous == (Image *) NULL)
1853 status=SetImageProgress(image,SaveImageTag,
1854 (MagickOffsetType) y,image->rows);
1855 if (status == MagickFalse)
1859 Ascii85Flush(image);
1864 offset=TellBlob(image)-offset;
1865 (void) WriteBlobString(image,"\nendstream\n");
1866 (void) WriteBlobString(image,"endobj\n");
1868 Write Length object.
1870 xref[object++]=TellBlob(image);
1871 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1873 (void) WriteBlobString(image,buffer);
1874 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",
1876 (void) WriteBlobString(image,buffer);
1877 (void) WriteBlobString(image,"endobj\n");
1879 Write Colorspace object.
1881 xref[object++]=TellBlob(image);
1882 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1884 (void) WriteBlobString(image,buffer);
1885 if (image->colorspace == CMYKColorspace)
1886 (void) CopyMagickString(buffer,"/DeviceCMYK\n",MaxTextExtent);
1888 if ((compression == FaxCompression) ||
1889 (compression == Group4Compression) ||
1890 ((image_info->type != TrueColorType) &&
1891 (IsImageGray(image,&image->exception) != MagickFalse)))
1892 (void) CopyMagickString(buffer,"/DeviceGray\n",MaxTextExtent);
1894 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1895 (compression == JPEGCompression) ||
1896 (compression == JPEG2000Compression))
1897 (void) CopyMagickString(buffer,"/DeviceRGB\n",MaxTextExtent);
1899 (void) FormatLocaleString(buffer,MaxTextExtent,
1900 "[ /Indexed /DeviceRGB %.20g %.20g 0 R ]\n",(double) image->colors-
1901 1,(double) object+3);
1902 (void) WriteBlobString(image,buffer);
1903 (void) WriteBlobString(image,"endobj\n");
1907 SetGeometry(image,&geometry);
1908 (void) ParseMetaGeometry("106x106+0+0>",&geometry.x,&geometry.y,
1909 &geometry.width,&geometry.height);
1910 tile_image=ThumbnailImage(image,geometry.width,geometry.height,
1912 if (tile_image == (Image *) NULL)
1913 ThrowWriterException(ResourceLimitError,image->exception.reason);
1914 xref[object++]=TellBlob(image);
1915 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1917 (void) WriteBlobString(image,buffer);
1918 (void) WriteBlobString(image,"<<\n");
1919 switch (compression)
1923 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"ASCII85Decode");
1926 case JPEGCompression:
1928 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"DCTDecode");
1929 if (image->colorspace != CMYKColorspace)
1931 (void) WriteBlobString(image,buffer);
1932 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1936 case JPEG2000Compression:
1938 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"JPXDecode");
1939 if (image->colorspace != CMYKColorspace)
1941 (void) WriteBlobString(image,buffer);
1942 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1946 case LZWCompression:
1948 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"LZWDecode");
1951 case ZipCompression:
1953 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"FlateDecode");
1956 case FaxCompression:
1957 case Group4Compression:
1959 (void) CopyMagickString(buffer,"/Filter [ /CCITTFaxDecode ]\n",
1961 (void) WriteBlobString(image,buffer);
1962 (void) FormatLocaleString(buffer,MaxTextExtent,"/DecodeParms [ << "
1963 "/K %s /BlackIs1 false /Columns %.20g /Rows %.20g >> ]\n",CCITTParam,
1964 (double) tile_image->columns,(double) tile_image->rows);
1969 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
1974 (void) WriteBlobString(image,buffer);
1975 (void) FormatLocaleString(buffer,MaxTextExtent,"/Width %.20g\n",(double)
1976 tile_image->columns);
1977 (void) WriteBlobString(image,buffer);
1978 (void) FormatLocaleString(buffer,MaxTextExtent,"/Height %.20g\n",(double)
1980 (void) WriteBlobString(image,buffer);
1981 (void) FormatLocaleString(buffer,MaxTextExtent,"/ColorSpace %.20g 0 R\n",
1983 (void) WriteBlobString(image,buffer);
1984 (void) FormatLocaleString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
1985 (compression == FaxCompression) || (compression == Group4Compression) ?
1987 (void) WriteBlobString(image,buffer);
1988 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
1990 (void) WriteBlobString(image,buffer);
1991 (void) WriteBlobString(image,">>\n");
1992 (void) WriteBlobString(image,"stream\n");
1993 offset=TellBlob(image);
1994 number_pixels=(MagickSizeType) tile_image->columns*tile_image->rows;
1995 if ((compression == FaxCompression) ||
1996 (compression == Group4Compression) ||
1997 ((image_info->type != TrueColorType) &&
1998 (IsImageGray(tile_image,&image->exception) != MagickFalse)))
2000 switch (compression)
2002 case FaxCompression:
2003 case Group4Compression:
2005 if (LocaleCompare(CCITTParam,"0") == 0)
2007 (void) HuffmanEncodeImage(image_info,image,tile_image);
2010 (void) Huffman2DEncodeImage(image_info,image,tile_image);
2013 case JPEGCompression:
2015 status=InjectImageBlob(image_info,image,tile_image,"jpeg",
2017 if (status == MagickFalse)
2018 ThrowWriterException(CoderError,tile_image->exception.reason);
2021 case JPEG2000Compression:
2023 status=InjectImageBlob(image_info,image,tile_image,"jp2",
2025 if (status == MagickFalse)
2026 ThrowWriterException(CoderError,tile_image->exception.reason);
2029 case RLECompression:
2033 Allocate pixel array.
2035 length=(size_t) number_pixels;
2036 pixels=(unsigned char *) AcquireQuantumMemory(length,
2038 if (pixels == (unsigned char *) NULL)
2040 tile_image=DestroyImage(tile_image);
2041 ThrowWriterException(ResourceLimitError,
2042 "MemoryAllocationFailed");
2045 Dump Runlength encoded pixels.
2048 for (y=0; y < (ssize_t) tile_image->rows; y++)
2050 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2051 &tile_image->exception);
2052 if (p == (const Quantum *) NULL)
2054 for (x=0; x < (ssize_t) tile_image->columns; x++)
2056 *q++=ScaleQuantumToChar(GetPixelIntensity(tile_image,p));
2057 p+=GetPixelChannels(tile_image);
2060 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2061 if (compression == ZipCompression)
2062 status=ZLIBEncodeImage(image,length,pixels);
2065 if (compression == LZWCompression)
2066 status=LZWEncodeImage(image,length,pixels);
2068 status=PackbitsEncodeImage(image,length,pixels);
2069 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2070 if (status == MagickFalse)
2072 (void) CloseBlob(image);
2073 return(MagickFalse);
2080 Dump uncompressed PseudoColor packets.
2082 Ascii85Initialize(image);
2083 for (y=0; y < (ssize_t) tile_image->rows; y++)
2085 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2086 &tile_image->exception);
2087 if (p == (const Quantum *) NULL)
2089 for (x=0; x < (ssize_t) tile_image->columns; x++)
2091 Ascii85Encode(image,
2092 ScaleQuantumToChar(GetPixelIntensity(tile_image,p)));
2093 p+=GetPixelChannels(tile_image);
2096 Ascii85Flush(image);
2102 if ((tile_image->storage_class == DirectClass) ||
2103 (tile_image->colors > 256) || (compression == JPEGCompression) ||
2104 (compression == JPEG2000Compression))
2105 switch (compression)
2107 case JPEGCompression:
2109 status=InjectImageBlob(image_info,image,tile_image,"jpeg",
2111 if (status == MagickFalse)
2112 ThrowWriterException(CoderError,tile_image->exception.reason);
2115 case JPEG2000Compression:
2117 status=InjectImageBlob(image_info,image,tile_image,"jp2",
2119 if (status == MagickFalse)
2120 ThrowWriterException(CoderError,tile_image->exception.reason);
2123 case RLECompression:
2127 Allocate pixel array.
2129 length=(size_t) number_pixels;
2130 pixels=(unsigned char *) AcquireQuantumMemory(length,4*
2132 length*=tile_image->colorspace == CMYKColorspace ? 4UL : 3UL;
2133 if (pixels == (unsigned char *) NULL)
2135 tile_image=DestroyImage(tile_image);
2136 ThrowWriterException(ResourceLimitError,
2137 "MemoryAllocationFailed");
2140 Dump runoffset encoded pixels.
2143 for (y=0; y < (ssize_t) tile_image->rows; y++)
2145 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2146 &tile_image->exception);
2147 if (p == (const Quantum *) NULL)
2149 for (x=0; x < (ssize_t) tile_image->columns; x++)
2151 *q++=ScaleQuantumToChar(GetPixelRed(tile_image,p));
2152 *q++=ScaleQuantumToChar(GetPixelGreen(tile_image,p));
2153 *q++=ScaleQuantumToChar(GetPixelBlue(tile_image,p));
2154 if (image->colorspace == CMYKColorspace)
2155 *q++=ScaleQuantumToChar(GetPixelBlack(tile_image,p));
2156 p+=GetPixelChannels(tile_image);
2159 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2160 if (compression == ZipCompression)
2161 status=ZLIBEncodeImage(image,length,pixels);
2164 if (compression == LZWCompression)
2165 status=LZWEncodeImage(image,length,pixels);
2167 status=PackbitsEncodeImage(image,length,pixels);
2168 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2169 if (status == MagickFalse)
2171 (void) CloseBlob(image);
2172 return(MagickFalse);
2179 Dump uncompressed DirectColor packets.
2181 Ascii85Initialize(image);
2182 for (y=0; y < (ssize_t) tile_image->rows; y++)
2184 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2185 &tile_image->exception);
2186 if (p == (const Quantum *) NULL)
2188 for (x=0; x < (ssize_t) tile_image->columns; x++)
2190 Ascii85Encode(image,ScaleQuantumToChar(
2191 GetPixelRed(tile_image,p)));
2192 Ascii85Encode(image,ScaleQuantumToChar(
2193 GetPixelGreen(tile_image,p)));
2194 Ascii85Encode(image,ScaleQuantumToChar(
2195 GetPixelBlue(tile_image,p)));
2196 if (image->colorspace == CMYKColorspace)
2197 Ascii85Encode(image,ScaleQuantumToChar(
2198 GetPixelBlack(tile_image,p)));
2199 p+=GetPixelChannels(tile_image);
2202 Ascii85Flush(image);
2209 Dump number of colors and colormap.
2211 switch (compression)
2213 case RLECompression:
2217 Allocate pixel array.
2219 length=(size_t) number_pixels;
2220 pixels=(unsigned char *) AcquireQuantumMemory(length,
2222 if (pixels == (unsigned char *) NULL)
2224 tile_image=DestroyImage(tile_image);
2225 ThrowWriterException(ResourceLimitError,
2226 "MemoryAllocationFailed");
2229 Dump Runlength encoded pixels.
2232 for (y=0; y < (ssize_t) tile_image->rows; y++)
2234 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2235 &tile_image->exception);
2236 if (p == (const Quantum *) NULL)
2238 for (x=0; x < (ssize_t) tile_image->columns; x++)
2240 *q++=(unsigned char) GetPixelIndex(tile_image,p);
2241 q+=GetPixelChannels(image);
2244 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2245 if (compression == ZipCompression)
2246 status=ZLIBEncodeImage(image,length,pixels);
2249 if (compression == LZWCompression)
2250 status=LZWEncodeImage(image,length,pixels);
2252 status=PackbitsEncodeImage(image,length,pixels);
2253 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2254 if (status == MagickFalse)
2256 (void) CloseBlob(image);
2257 return(MagickFalse);
2264 Dump uncompressed PseudoColor packets.
2266 Ascii85Initialize(image);
2267 for (y=0; y < (ssize_t) tile_image->rows; y++)
2269 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2270 &tile_image->exception);
2271 if (p == (const Quantum *) NULL)
2273 for (x=0; x < (ssize_t) tile_image->columns; x++)
2275 Ascii85Encode(image,(unsigned char)
2276 GetPixelIndex(tile_image,p));
2277 p+=GetPixelChannels(image);
2280 Ascii85Flush(image);
2285 tile_image=DestroyImage(tile_image);
2286 offset=TellBlob(image)-offset;
2287 (void) WriteBlobString(image,"\nendstream\n");
2288 (void) WriteBlobString(image,"endobj\n");
2290 Write Length object.
2292 xref[object++]=TellBlob(image);
2293 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2295 (void) WriteBlobString(image,buffer);
2296 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
2297 (void) WriteBlobString(image,buffer);
2298 (void) WriteBlobString(image,"endobj\n");
2299 xref[object++]=TellBlob(image);
2300 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2302 (void) WriteBlobString(image,buffer);
2303 if ((image->storage_class != DirectClass) && (image->colors <= 256) &&
2304 (compression != FaxCompression) && (compression != Group4Compression))
2307 Write Colormap object.
2309 (void) WriteBlobString(image,"<<\n");
2310 if (compression == NoCompression)
2311 (void) WriteBlobString(image,"/Filter [ /ASCII85Decode ]\n");
2312 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
2314 (void) WriteBlobString(image,buffer);
2315 (void) WriteBlobString(image,">>\n");
2316 (void) WriteBlobString(image,"stream\n");
2317 offset=TellBlob(image);
2318 if (compression == NoCompression)
2319 Ascii85Initialize(image);
2320 for (i=0; i < (ssize_t) image->colors; i++)
2322 if (compression == NoCompression)
2324 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].red));
2325 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].green));
2326 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].blue));
2329 (void) WriteBlobByte(image,
2330 ScaleQuantumToChar(image->colormap[i].red));
2331 (void) WriteBlobByte(image,
2332 ScaleQuantumToChar(image->colormap[i].green));
2333 (void) WriteBlobByte(image,
2334 ScaleQuantumToChar(image->colormap[i].blue));
2336 if (compression == NoCompression)
2337 Ascii85Flush(image);
2338 offset=TellBlob(image)-offset;
2339 (void) WriteBlobString(image,"\nendstream\n");
2341 (void) WriteBlobString(image,"endobj\n");
2343 Write Length object.
2345 xref[object++]=TellBlob(image);
2346 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2348 (void) WriteBlobString(image,buffer);
2349 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
2351 (void) WriteBlobString(image,buffer);
2352 (void) WriteBlobString(image,"endobj\n");
2354 Write softmask object.
2356 xref[object++]=TellBlob(image);
2357 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2359 (void) WriteBlobString(image,buffer);
2360 (void) WriteBlobString(image,"<<\n");
2361 if (image->matte == MagickFalse)
2362 (void) WriteBlobString(image,">>\n");
2365 (void) WriteBlobString(image,"/Type /XObject\n");
2366 (void) WriteBlobString(image,"/Subtype /Image\n");
2367 (void) FormatLocaleString(buffer,MaxTextExtent,"/Name /Ma%.20g\n",
2368 (double) image->scene);
2369 (void) WriteBlobString(image,buffer);
2370 switch (compression)
2374 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
2378 case LZWCompression:
2380 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"LZWDecode");
2383 case ZipCompression:
2385 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
2391 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
2396 (void) WriteBlobString(image,buffer);
2397 (void) FormatLocaleString(buffer,MaxTextExtent,"/Width %.20g\n",
2398 (double) image->columns);
2399 (void) WriteBlobString(image,buffer);
2400 (void) FormatLocaleString(buffer,MaxTextExtent,"/Height %.20g\n",
2401 (double) image->rows);
2402 (void) WriteBlobString(image,buffer);
2403 (void) WriteBlobString(image,"/ColorSpace /DeviceGray\n");
2404 (void) FormatLocaleString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
2405 (compression == FaxCompression) || (compression == Group4Compression)
2407 (void) WriteBlobString(image,buffer);
2408 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
2410 (void) WriteBlobString(image,buffer);
2411 (void) WriteBlobString(image,">>\n");
2412 (void) WriteBlobString(image,"stream\n");
2413 offset=TellBlob(image);
2414 number_pixels=(MagickSizeType) image->columns*image->rows;
2415 switch (compression)
2417 case RLECompression:
2421 Allocate pixel array.
2423 length=(size_t) number_pixels;
2424 pixels=(unsigned char *) AcquireQuantumMemory(length,
2426 if (pixels == (unsigned char *) NULL)
2428 image=DestroyImage(image);
2429 ThrowWriterException(ResourceLimitError,
2430 "MemoryAllocationFailed");
2433 Dump Runlength encoded pixels.
2436 for (y=0; y < (ssize_t) image->rows; y++)
2438 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
2439 if (p == (const Quantum *) NULL)
2441 for (x=0; x < (ssize_t) image->columns; x++)
2443 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
2444 p+=GetPixelChannels(image);
2447 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2448 if (compression == ZipCompression)
2449 status=ZLIBEncodeImage(image,length,pixels);
2452 if (compression == LZWCompression)
2453 status=LZWEncodeImage(image,length,pixels);
2455 status=PackbitsEncodeImage(image,length,pixels);
2456 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2457 if (status == MagickFalse)
2459 (void) CloseBlob(image);
2460 return(MagickFalse);
2467 Dump uncompressed PseudoColor packets.
2469 Ascii85Initialize(image);
2470 for (y=0; y < (ssize_t) image->rows; y++)
2472 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
2473 if (p == (const Quantum *) NULL)
2475 for (x=0; x < (ssize_t) image->columns; x++)
2477 Ascii85Encode(image,ScaleQuantumToChar(GetPixelAlpha(image,p)));
2478 p+=GetPixelChannels(image);
2481 Ascii85Flush(image);
2485 offset=TellBlob(image)-offset;
2486 (void) WriteBlobString(image,"\nendstream\n");
2488 (void) WriteBlobString(image,"endobj\n");
2490 Write Length object.
2492 xref[object++]=TellBlob(image);
2493 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2495 (void) WriteBlobString(image,buffer);
2496 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
2497 (void) WriteBlobString(image,buffer);
2498 (void) WriteBlobString(image,"endobj\n");
2499 if (GetNextImageInList(image) == (Image *) NULL)
2501 image=SyncNextImageInList(image);
2502 status=SetImageProgress(image,SaveImagesTag,scene++,
2503 GetImageListLength(image));
2504 if (status == MagickFalse)
2506 } while (image_info->adjoin != MagickFalse);
2508 Write Metadata object.
2510 xref[object++]=TellBlob(image);
2512 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2514 (void) WriteBlobString(image,buffer);
2515 (void) WriteBlobString(image,"<<\n");
2516 GetPathComponent(image->filename,BasePath,basename);
2517 (void) FormatLocaleString(buffer,MaxTextExtent,"/Title (%s)\n",
2518 EscapeParenthesis(basename));
2519 (void) WriteBlobString(image,buffer);
2520 seconds=time((time_t *) NULL);
2521 #if defined(MAGICKCORE_HAVE_LOCALTIME_R)
2522 (void) localtime_r(&seconds,&local_time);
2524 (void) memcpy(&local_time,localtime(&seconds),sizeof(local_time));
2526 (void) FormatLocaleString(date,MaxTextExtent,"D:%04d%02d%02d%02d%02d%02d",
2527 local_time.tm_year+1900,local_time.tm_mon+1,local_time.tm_mday,
2528 local_time.tm_hour,local_time.tm_min,local_time.tm_sec);
2529 (void) FormatLocaleString(buffer,MaxTextExtent,"/CreationDate (%s)\n",date);
2530 (void) WriteBlobString(image,buffer);
2531 (void) FormatLocaleString(buffer,MaxTextExtent,"/ModDate (%s)\n",date);
2532 (void) WriteBlobString(image,buffer);
2533 (void) FormatLocaleString(buffer,MaxTextExtent,"/Producer (%s)\n",
2534 EscapeParenthesis(GetMagickVersion((size_t *) NULL)));
2535 (void) WriteBlobString(image,buffer);
2536 (void) WriteBlobString(image,">>\n");
2537 (void) WriteBlobString(image,"endobj\n");
2541 offset=TellBlob(image)-xref[0]+10;
2542 (void) WriteBlobString(image,"xref\n");
2543 (void) FormatLocaleString(buffer,MaxTextExtent,"0 %.20g\n",(double)
2545 (void) WriteBlobString(image,buffer);
2546 (void) WriteBlobString(image,"0000000000 65535 f \n");
2547 for (i=0; i < (ssize_t) object; i++)
2549 (void) FormatLocaleString(buffer,MaxTextExtent,"%010lu 00000 n \n",
2550 (unsigned long) xref[i]);
2551 (void) WriteBlobString(image,buffer);
2553 (void) WriteBlobString(image,"trailer\n");
2554 (void) WriteBlobString(image,"<<\n");
2555 (void) FormatLocaleString(buffer,MaxTextExtent,"/Size %.20g\n",(double)
2557 (void) WriteBlobString(image,buffer);
2558 (void) FormatLocaleString(buffer,MaxTextExtent,"/Info %.20g 0 R\n",(double)
2560 (void) WriteBlobString(image,buffer);
2561 (void) FormatLocaleString(buffer,MaxTextExtent,"/Root %.20g 0 R\n",(double)
2563 (void) WriteBlobString(image,buffer);
2564 (void) WriteBlobString(image,">>\n");
2565 (void) WriteBlobString(image,"startxref\n");
2566 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
2567 (void) WriteBlobString(image,buffer);
2568 (void) WriteBlobString(image,"%%EOF\n");
2569 xref=(MagickOffsetType *) RelinquishMagickMemory(xref);
2570 (void) CloseBlob(image);