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/compress.h"
51 #include "MagickCore/constitute.h"
52 #include "MagickCore/delegate.h"
53 #include "MagickCore/delegate-private.h"
54 #include "MagickCore/draw.h"
55 #include "MagickCore/exception.h"
56 #include "MagickCore/exception-private.h"
57 #include "MagickCore/geometry.h"
58 #include "MagickCore/image.h"
59 #include "MagickCore/image-private.h"
60 #include "MagickCore/list.h"
61 #include "MagickCore/magick.h"
62 #include "MagickCore/memory_.h"
63 #include "MagickCore/monitor.h"
64 #include "MagickCore/monitor-private.h"
65 #include "MagickCore/option.h"
66 #include "MagickCore/pixel-accessor.h"
67 #include "MagickCore/profile.h"
68 #include "MagickCore/property.h"
69 #include "MagickCore/quantum-private.h"
70 #include "MagickCore/resource_.h"
71 #include "MagickCore/resize.h"
72 #include "MagickCore/static.h"
73 #include "MagickCore/string_.h"
74 #include "MagickCore/module.h"
75 #include "MagickCore/transform.h"
76 #include "MagickCore/utility.h"
77 #include "MagickCore/module.h"
82 #if defined(MAGICKCORE_TIFF_DELEGATE)
83 #define CCITTParam "-1"
85 #define CCITTParam "0"
91 static MagickBooleanType
92 WritePDFImage(const ImageInfo *,Image *);
95 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
99 % I n v o k e P D F D e l e g a t e %
103 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105 % InvokePDFDelegate() executes the PDF interpreter with the specified command.
107 % The format of the InvokePDFDelegate method is:
109 % MagickBooleanType InvokePDFDelegate(const MagickBooleanType verbose,
110 % const char *command,ExceptionInfo *exception)
112 % A description of each parameter follows:
114 % o verbose: A value other than zero displays the command prior to
117 % o command: the address of a character string containing the command to
120 % o exception: return any errors or warnings in this structure.
123 static MagickBooleanType InvokePDFDelegate(const MagickBooleanType verbose,
124 const char *command,ExceptionInfo *exception)
129 #if defined(MAGICKCORE_GS_DELEGATE) || defined(MAGICKCORE_WINDOWS_SUPPORT)
146 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
147 ghost_info=NTGhostscriptDLLVectors();
152 ghost_info=(&ghost_info_struct);
153 (void) ResetMagickMemory(&ghost_info,0,sizeof(ghost_info));
154 ghost_info_struct.new_instance=(int (*)(gs_main_instance **,void *))
156 ghost_info_struct.init_with_args=(int (*)(gs_main_instance *,int,char **))
157 gsapi_init_with_args;
158 ghost_info_struct.run_string=(int (*)(gs_main_instance *,const char *,int,
159 int *)) gsapi_run_string;
160 ghost_info_struct.delete_instance=(void (*)(gs_main_instance *))
161 gsapi_delete_instance;
162 ghost_info_struct.exit=(int (*)(gs_main_instance *)) gsapi_exit;
164 if (ghost_info == (GhostInfo *) NULL)
166 status=SystemCommand(MagickFalse,verbose,command,exception);
167 return(status == 0 ? MagickTrue : MagickFalse);
169 if (verbose != MagickFalse)
171 (void) fputs("[ghostscript library]",stdout);
172 (void) fputs(strchr(command,' '),stdout);
174 status=(ghost_info->new_instance)(&interpreter,(void *) NULL);
177 status=SystemCommand(MagickFalse,verbose,command,exception);
178 return(status == 0 ? MagickTrue : MagickFalse);
181 argv=StringToArgv(command,&argc);
182 status=(ghost_info->init_with_args)(interpreter,argc-1,argv+1);
184 status=(ghost_info->run_string)(interpreter,"systemdict /start get exec\n",
186 (ghost_info->exit)(interpreter);
187 (ghost_info->delete_instance)(interpreter);
188 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
189 NTGhostscriptUnLoadDLL();
191 for (i=0; i < (ssize_t) argc; i++)
192 argv[i]=DestroyString(argv[i]);
193 argv=(char **) RelinquishMagickMemory(argv);
194 if ((status != 0) && (status != -101))
199 message=GetExceptionMessage(errno);
200 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
201 "`%s': %s",command,message);
202 message=DestroyString(message);
203 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
204 "Ghostscript returns status %d, exit code %d",status,code);
209 status=SystemCommand(MagickFalse,verbose,command,exception);
210 return(status == 0 ? MagickTrue : MagickFalse);
215 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
223 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
225 % IsPDF() returns MagickTrue if the image format type, identified by the
226 % magick string, is PDF.
228 % The format of the IsPDF method is:
230 % MagickBooleanType IsPDF(const unsigned char *magick,const size_t offset)
232 % A description of each parameter follows:
234 % o magick: compare image format pattern against these bytes.
236 % o offset: Specifies the offset of the magick string.
239 static MagickBooleanType IsPDF(const unsigned char *magick,const size_t offset)
243 if (LocaleNCompare((const char *) magick,"%PDF-",5) == 0)
249 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
253 % R e a d P D F I m a g e %
257 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
259 % ReadPDFImage() reads a Portable Document Format image file and
260 % returns it. It allocates the memory necessary for the new Image structure
261 % and returns a pointer to the new image.
263 % The format of the ReadPDFImage method is:
265 % Image *ReadPDFImage(const ImageInfo *image_info,ExceptionInfo *exception)
267 % A description of each parameter follows:
269 % o image_info: the image info.
271 % o exception: return any errors or warnings in this structure.
275 static MagickBooleanType IsPDFRendered(const char *path)
283 if ((path == (const char *) NULL) || (*path == '\0'))
285 status=GetPathAttributes(path,&attributes);
286 if ((status != MagickFalse) && S_ISREG(attributes.st_mode) &&
287 (attributes.st_size > 0))
292 static Image *ReadPDFImage(const ImageInfo *image_info,ExceptionInfo *exception)
294 #define CropBox "CropBox"
295 #define DeviceCMYK "DeviceCMYK"
296 #define MediaBox "MediaBox"
297 #define RenderPostscriptText "Rendering Postscript... "
298 #define PDFRotate "Rotate"
299 #define SpotColor "Separation"
300 #define TrimBox "TrimBox"
301 #define PDFVersion "PDF-"
304 command[MaxTextExtent],
305 density[MaxTextExtent],
306 filename[MaxTextExtent],
307 geometry[MaxTextExtent],
308 options[MaxTextExtent],
309 input_filename[MaxTextExtent],
310 postscript_filename[MaxTextExtent];
369 assert(image_info != (const ImageInfo *) NULL);
370 assert(image_info->signature == MagickSignature);
371 if (image_info->debug != MagickFalse)
372 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
373 image_info->filename);
374 assert(exception != (ExceptionInfo *) NULL);
375 assert(exception->signature == MagickSignature);
379 image=AcquireImage(image_info);
380 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
381 if (status == MagickFalse)
383 image=DestroyImageList(image);
384 return((Image *) NULL);
386 status=AcquireUniqueSymbolicLink(image_info->filename,input_filename);
387 if (status == MagickFalse)
389 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
390 image_info->filename);
391 image=DestroyImageList(image);
392 return((Image *) NULL);
395 Set the page density.
397 delta.x=DefaultResolution;
398 delta.y=DefaultResolution;
399 if ((image->x_resolution == 0.0) || (image->y_resolution == 0.0))
401 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
402 image->x_resolution=geometry_info.rho;
403 image->y_resolution=geometry_info.sigma;
404 if ((flags & SigmaValue) == 0)
405 image->y_resolution=image->x_resolution;
408 Determine page geometry from the PDF media box.
410 cmyk=image_info->colorspace == CMYKColorspace ? MagickTrue : MagickFalse;
412 option=GetImageOption(image_info,"pdf:use-cropbox");
413 if (option != (const char *) NULL)
414 cropbox=IsMagickTrue(option);
416 option=GetImageOption(image_info,"pdf:use-trimbox");
417 if (option != (const char *) NULL)
418 trimbox=IsMagickTrue(option);
421 (void) ResetMagickMemory(&bounding_box,0,sizeof(bounding_box));
422 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
423 (void) ResetMagickMemory(&hires_bounds,0,sizeof(hires_bounds));
424 (void) ResetMagickMemory(&page,0,sizeof(page));
425 (void) ResetMagickMemory(command,0,sizeof(command));
430 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
438 if ((c != (int) '/') && (c != (int) '%') &&
439 ((size_t) (p-command) < (MaxTextExtent-1)))
443 if (LocaleNCompare(PDFRotate,command,strlen(PDFRotate)) == 0)
444 count=(ssize_t) sscanf(command,"Rotate %lf",&angle);
446 Is this a CMYK document?
448 if (LocaleNCompare(DeviceCMYK,command,strlen(DeviceCMYK)) == 0)
450 if (LocaleNCompare(SpotColor,command,strlen(SpotColor)) == 0)
454 property[MaxTextExtent],
463 (void) FormatLocaleString(property,MaxTextExtent,"pdf:SpotColor-%.20g",
464 (double) spotcolor++);
466 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
468 if ((isspace(c) != 0) || (c == '/') || ((i+1) == MaxTextExtent))
473 value=AcquireString(name);
474 (void) SubstituteString(&value,"#20"," ");
475 (void) SetImageProperty(image,property,value);
476 value=DestroyString(value);
479 if (LocaleNCompare(PDFVersion,command,strlen(PDFVersion)) == 0)
480 (void) SetImageProperty(image,"pdf:Version",command);
482 if (cropbox != MagickFalse)
484 if (LocaleNCompare(CropBox,command,strlen(CropBox)) == 0)
487 Note region defined by crop box.
489 count=(ssize_t) sscanf(command,"CropBox [%lf %lf %lf %lf",
490 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
492 count=(ssize_t) sscanf(command,"CropBox[%lf %lf %lf %lf",
493 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
497 if (trimbox != MagickFalse)
499 if (LocaleNCompare(TrimBox,command,strlen(TrimBox)) == 0)
502 Note region defined by trim box.
504 count=(ssize_t) sscanf(command,"TrimBox [%lf %lf %lf %lf",
505 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
507 count=(ssize_t) sscanf(command,"TrimBox[%lf %lf %lf %lf",
508 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
512 if (LocaleNCompare(MediaBox,command,strlen(MediaBox)) == 0)
515 Note region defined by media box.
517 count=(ssize_t) sscanf(command,"MediaBox [%lf %lf %lf %lf",
518 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
520 count=(ssize_t) sscanf(command,"MediaBox[%lf %lf %lf %lf",
521 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
525 if (((bounds.x2 > hires_bounds.x2) && (bounds.y2 > hires_bounds.y2)) ||
526 ((hires_bounds.x2 == 0.0) && (hires_bounds.y2 == 0.0)))
529 Set PDF render geometry.
531 (void) FormatLocaleString(geometry,MaxTextExtent,
532 "%gx%g%+.15g%+.15g",bounds.x2-bounds.x1,bounds.y2-bounds.y1,
533 bounds.x1,bounds.y1);
534 (void) SetImageProperty(image,"pdf:HiResBoundingBox",geometry);
535 page.width=(size_t) floor(bounds.x2-bounds.x1+0.5);
536 page.height=(size_t) floor(bounds.y2-bounds.y1+0.5);
540 (void) CloseBlob(image);
541 if ((fabs(angle) == 90.0) || (fabs(angle) == 270.0))
547 page.width=page.height;
550 if (image_info->colorspace == RGBColorspace)
553 Create Ghostscript control file.
555 file=AcquireUniqueFileResource(postscript_filename);
558 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
559 image_info->filename);
560 image=DestroyImage(image);
561 return((Image *) NULL);
563 count=write(file," ",1);
566 Render Postscript with the Ghostscript delegate.
568 if ((image_info->ping != MagickFalse) ||
569 (image_info->monochrome != MagickFalse))
570 delegate_info=GetDelegateInfo("ps:mono",(char *) NULL,exception);
572 if (cmyk != MagickFalse)
573 delegate_info=GetDelegateInfo("ps:cmyk",(char *) NULL,exception);
575 delegate_info=GetDelegateInfo("ps:alpha",(char *) NULL,exception);
576 if (delegate_info == (const DelegateInfo *) NULL)
578 (void) RelinquishUniqueFileResource(postscript_filename);
579 image=DestroyImage(image);
580 return((Image *) NULL);
583 if (image_info->density != (char *) NULL)
585 flags=ParseGeometry(image_info->density,&geometry_info);
586 image->x_resolution=geometry_info.rho;
587 image->y_resolution=geometry_info.sigma;
588 if ((flags & SigmaValue) == 0)
589 image->y_resolution=image->x_resolution;
591 (void) FormatLocaleString(density,MaxTextExtent,"%gx%g",image->x_resolution,
592 image->y_resolution);
593 if (image_info->page != (char *) NULL)
595 (void) ParseAbsoluteGeometry(image_info->page,&page);
596 page.width=(size_t) floor((double) (page.width*image->x_resolution/
598 page.height=(size_t) floor((double) (page.height*image->y_resolution/
600 (void) FormatLocaleString(options,MaxTextExtent,"-g%.20gx%.20g ",(double)
601 page.width,(double) page.height);
603 if (cmyk != MagickFalse)
604 (void) ConcatenateMagickString(options,"-dUseCIEColor ",MaxTextExtent);
605 if (cropbox != MagickFalse)
606 (void) ConcatenateMagickString(options,"-dUseCropBox ",MaxTextExtent);
607 if (trimbox != MagickFalse)
608 (void) ConcatenateMagickString(options,"-dUseTrimBox ",MaxTextExtent);
609 read_info=CloneImageInfo(image_info);
610 *read_info->magick='\0';
611 if (read_info->number_scenes != 0)
614 pages[MaxTextExtent];
616 (void) FormatLocaleString(pages,MaxTextExtent,"-dFirstPage=%.20g "
617 "-dLastPage=%.20g",(double) read_info->scene+1,(double)
618 (read_info->scene+read_info->number_scenes));
619 (void) ConcatenateMagickString(options,pages,MaxTextExtent);
620 read_info->number_scenes=0;
621 if (read_info->scenes != (char *) NULL)
622 *read_info->scenes='\0';
624 if (read_info->authenticate != (char *) NULL)
625 (void) FormatLocaleString(options+strlen(options),MaxTextExtent,
626 " -sPDFPassword=%s",read_info->authenticate);
627 (void) CopyMagickString(filename,read_info->filename,MaxTextExtent);
628 (void) AcquireUniqueFilename(filename);
629 (void) ConcatenateMagickString(filename,"-%08d",MaxTextExtent);
630 (void) FormatLocaleString(command,MaxTextExtent,
631 GetDelegateCommands(delegate_info),
632 read_info->antialias != MagickFalse ? 4 : 1,
633 read_info->antialias != MagickFalse ? 4 : 1,density,options,filename,
634 postscript_filename,input_filename);
635 status=InvokePDFDelegate(read_info->verbose,command,exception);
636 (void) RelinquishUniqueFileResource(postscript_filename);
637 (void) RelinquishUniqueFileResource(input_filename);
638 pdf_image=(Image *) NULL;
639 if (status == MagickFalse)
642 (void) InterpretImageFilename(image_info,image,filename,(int) i,
643 read_info->filename);
644 if (IsPDFRendered(read_info->filename) == MagickFalse)
646 (void) RelinquishUniqueFileResource(read_info->filename);
651 (void) InterpretImageFilename(image_info,image,filename,(int) i,
652 read_info->filename);
653 if (IsPDFRendered(read_info->filename) == MagickFalse)
655 next=ReadImage(read_info,exception);
656 (void) RelinquishUniqueFileResource(read_info->filename);
657 if (next == (Image *) NULL)
659 AppendImageToList(&pdf_image,next);
661 read_info=DestroyImageInfo(read_info);
662 if (pdf_image == (Image *) NULL)
664 ThrowFileException(exception,DelegateError,"PostscriptDelegateFailed",
665 image_info->filename);
666 image=DestroyImage(image);
667 return((Image *) NULL);
669 if (LocaleCompare(pdf_image->magick,"BMP") == 0)
674 cmyk_image=ConsolidateCMYKImages(pdf_image,exception);
675 if (cmyk_image != (Image *) NULL)
677 pdf_image=DestroyImageList(pdf_image);
678 pdf_image=cmyk_image;
681 if (image_info->number_scenes != 0)
690 Add place holder images to meet the subimage specification requirement.
692 for (i=0; i < (ssize_t) image_info->scene; i++)
694 clone_image=CloneImage(pdf_image,1,1,MagickTrue,exception);
695 if (clone_image != (Image *) NULL)
696 PrependImageToList(&pdf_image,clone_image);
701 (void) CopyMagickString(pdf_image->filename,filename,MaxTextExtent);
702 pdf_image->page=page;
703 (void) CloneImageProfiles(pdf_image,image);
704 (void) CloneImageProperties(pdf_image,image);
705 next=SyncNextImageInList(pdf_image);
706 if (next != (Image *) NULL)
708 } while (next != (Image *) NULL);
709 image=DestroyImage(image);
711 for (next=GetFirstImageInList(pdf_image); next != (Image *) NULL; )
714 next=GetNextImageInList(next);
716 return(GetFirstImageInList(pdf_image));
720 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
724 % R e g i s t e r P D F I m a g e %
728 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
730 % RegisterPDFImage() adds properties for the PDF image format to
731 % the list of supported formats. The properties include the image format
732 % tag, a method to read and/or write the format, whether the format
733 % supports the saving of more than one frame to the same file or blob,
734 % whether the format supports native in-memory I/O, and a brief
735 % description of the format.
737 % The format of the RegisterPDFImage method is:
739 % size_t RegisterPDFImage(void)
742 ModuleExport size_t RegisterPDFImage(void)
747 entry=SetMagickInfo("AI");
748 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
749 entry->encoder=(EncodeImageHandler *) WritePDFImage;
750 entry->adjoin=MagickFalse;
751 entry->blob_support=MagickFalse;
752 entry->seekable_stream=MagickTrue;
753 entry->thread_support=EncoderThreadSupport;
754 entry->description=ConstantString("Adobe Illustrator CS2");
755 entry->module=ConstantString("PDF");
756 (void) RegisterMagickInfo(entry);
757 entry=SetMagickInfo("EPDF");
758 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
759 entry->encoder=(EncodeImageHandler *) WritePDFImage;
760 entry->adjoin=MagickFalse;
761 entry->blob_support=MagickFalse;
762 entry->seekable_stream=MagickTrue;
763 entry->thread_support=EncoderThreadSupport;
764 entry->description=ConstantString("Encapsulated Portable Document Format");
765 entry->module=ConstantString("PDF");
766 (void) RegisterMagickInfo(entry);
767 entry=SetMagickInfo("PDF");
768 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
769 entry->encoder=(EncodeImageHandler *) WritePDFImage;
770 entry->magick=(IsImageFormatHandler *) IsPDF;
771 entry->blob_support=MagickFalse;
772 entry->seekable_stream=MagickTrue;
773 entry->thread_support=EncoderThreadSupport;
774 entry->description=ConstantString("Portable Document Format");
775 entry->module=ConstantString("PDF");
776 (void) RegisterMagickInfo(entry);
777 entry=SetMagickInfo("PDFA");
778 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
779 entry->encoder=(EncodeImageHandler *) WritePDFImage;
780 entry->magick=(IsImageFormatHandler *) IsPDF;
781 entry->blob_support=MagickFalse;
782 entry->seekable_stream=MagickTrue;
783 entry->thread_support=EncoderThreadSupport;
784 entry->description=ConstantString("Portable Document Archive Format");
785 entry->module=ConstantString("PDF");
786 (void) RegisterMagickInfo(entry);
787 return(MagickImageCoderSignature);
791 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
795 % U n r e g i s t e r P D F I m a g e %
799 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
801 % UnregisterPDFImage() removes format registrations made by the
802 % PDF module from the list of supported formats.
804 % The format of the UnregisterPDFImage method is:
806 % UnregisterPDFImage(void)
809 ModuleExport void UnregisterPDFImage(void)
811 (void) UnregisterMagickInfo("AI");
812 (void) UnregisterMagickInfo("EPDF");
813 (void) UnregisterMagickInfo("PDF");
814 (void) UnregisterMagickInfo("PDFA");
818 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
822 % W r i t e P D F I m a g e %
826 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
828 % WritePDFImage() writes an image in the Portable Document image
831 % The format of the WritePDFImage method is:
833 % MagickBooleanType WritePDFImage(const ImageInfo *image_info,Image *image)
835 % A description of each parameter follows.
837 % o image_info: the image info.
839 % o image: The image.
843 static inline size_t MagickMax(const size_t x,const size_t y)
850 static inline size_t MagickMin(const size_t x,const size_t y)
857 static char *EscapeParenthesis(const char *text)
869 buffer[MaxTextExtent];
873 for (i=0; i < (ssize_t) MagickMin(strlen(text),(MaxTextExtent-escapes-1)); i++)
875 if ((text[i] == '(') || (text[i] == ')'))
886 static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
887 Image *image,Image *inject_image)
905 write_info=CloneImageInfo(image_info);
906 (void) CopyMagickString(write_info->filename,"GROUP4:",MaxTextExtent);
907 (void) CopyMagickString(write_info->magick,"GROUP4",MaxTextExtent);
908 group4_image=CloneImage(inject_image,0,0,MagickTrue,&image->exception);
909 if (group4_image == (Image *) NULL)
911 group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
913 group4_image=DestroyImage(group4_image);
914 if (group4 == (unsigned char *) NULL)
916 write_info=DestroyImageInfo(write_info);
917 if (WriteBlob(image,length,group4) != (ssize_t) length)
919 group4=(unsigned char *) RelinquishMagickMemory(group4);
923 static MagickBooleanType WritePDFImage(const ImageInfo *image_info,Image *image)
925 #define CFormat "/Filter [ /%s ]\n"
926 #define ObjectsPerImage 14
931 "<?xpacket begin=\"%s\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n"
932 "<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"
933 " <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n"
934 " <rdf:Description rdf:about=\"\"\n"
935 " xmlns:xap=\"http://ns.adobe.com/xap/1.0/\">\n"
936 " <xap:ModifyDate>%s</xap:ModifyDate>\n"
937 " <xap:CreateDate>%s</xap:CreateDate>\n"
938 " <xap:MetadataDate>%s</xap:MetadataDate>\n"
939 " <xap:CreatorTool>%s</xap:CreatorTool>\n"
940 " </rdf:Description>\n"
941 " <rdf:Description rdf:about=\"\"\n"
942 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"
943 " <dc:format>application/pdf</dc:format>\n"
944 " </rdf:Description>\n"
945 " <rdf:Description rdf:about=\"\"\n"
946 " xmlns:xapMM=\"http://ns.adobe.com/xap/1.0/mm/\">\n"
947 " <xapMM:DocumentID>uuid:6ec119d7-7982-4f56-808d-dfe64f5b35cf</xapMM:DocumentID>\n"
948 " <xapMM:InstanceID>uuid:a79b99b4-6235-447f-9f6c-ec18ef7555cb</xapMM:InstanceID>\n"
949 " </rdf:Description>\n"
950 " <rdf:Description rdf:about=\"\"\n"
951 " xmlns:pdf=\"http://ns.adobe.com/pdf/1.3/\">\n"
952 " <pdf:Producer>%s</pdf:Producer>\n"
953 " </rdf:Description>\n"
954 " <rdf:Description rdf:about=\"\"\n"
955 " xmlns:pdfaid=\"http://www.aiim.org/pdfa/ns/id/\">\n"
956 " <pdfaid:part>1</pdfaid:part>\n"
957 " <pdfaid:conformance>B</pdfaid:conformance>\n"
958 " </rdf:Description>\n"
961 "<?xpacket end=\"w\"?>\n"
963 XMPProfileMagick[4]= { (char) 0xef, (char) 0xbb, (char) 0xbf, (char) 0x00 };
966 basename[MaxTextExtent],
967 buffer[MaxTextExtent],
970 page_geometry[MaxTextExtent];
1012 register const Quantum
1015 register unsigned char
1045 Open output image file.
1047 assert(image_info != (const ImageInfo *) NULL);
1048 assert(image_info->signature == MagickSignature);
1049 assert(image != (Image *) NULL);
1050 assert(image->signature == MagickSignature);
1051 if (image->debug != MagickFalse)
1052 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1053 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1054 if (status == MagickFalse)
1057 Allocate X ref memory.
1059 xref=(MagickOffsetType *) AcquireQuantumMemory(2048UL,sizeof(*xref));
1060 if (xref == (MagickOffsetType *) NULL)
1061 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1062 (void) ResetMagickMemory(xref,0,2048UL*sizeof(*xref));
1068 if (image_info->compression == JPEG2000Compression)
1069 version=(size_t) MagickMax(version,5);
1070 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
1071 if (next->matte != MagickFalse)
1072 version=(size_t) MagickMax(version,4);
1073 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1074 version=(size_t) MagickMax(version,6);
1075 (void) FormatLocaleString(buffer,MaxTextExtent,"%%PDF-1.%.20g \n",
1077 (void) WriteBlobString(image,buffer);
1078 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1079 (void) WriteBlobString(image,"%âãÏÓ\n");
1081 Write Catalog object.
1083 xref[object++]=TellBlob(image);
1085 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1087 (void) WriteBlobString(image,buffer);
1088 (void) WriteBlobString(image,"<<\n");
1089 if (LocaleCompare(image_info->magick,"PDFA") != 0)
1090 (void) FormatLocaleString(buffer,MaxTextExtent,"/Pages %.20g 0 R\n",
1094 (void) FormatLocaleString(buffer,MaxTextExtent,"/Metadata %.20g 0 R\n",
1096 (void) WriteBlobString(image,buffer);
1097 (void) FormatLocaleString(buffer,MaxTextExtent,"/Pages %.20g 0 R\n",
1100 (void) WriteBlobString(image,buffer);
1101 (void) WriteBlobString(image,"/Type /Catalog\n");
1102 (void) WriteBlobString(image,">>\n");
1103 (void) WriteBlobString(image,"endobj\n");
1104 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1107 create_date[MaxTextExtent],
1108 modify_date[MaxTextExtent],
1109 timestamp[MaxTextExtent],
1110 xmp_profile[MaxTextExtent];
1118 xref[object++]=TellBlob(image);
1119 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",
1121 (void) WriteBlobString(image,buffer);
1122 (void) WriteBlobString(image,"<<\n");
1123 (void) WriteBlobString(image,"/Subtype /XML\n");
1125 value=GetImageProperty(image,"date:modify");
1126 if (value != (const char *) NULL)
1127 (void) CopyMagickString(modify_date,value,MaxTextExtent);
1129 value=GetImageProperty(image,"date:create");
1130 if (value != (const char *) NULL)
1131 (void) CopyMagickString(create_date,value,MaxTextExtent);
1132 (void) FormatMagickTime(time((time_t *) NULL),MaxTextExtent,timestamp);
1133 i=FormatLocaleString(xmp_profile,MaxTextExtent,XMPProfile,
1134 XMPProfileMagick,modify_date,create_date,timestamp,
1135 GetMagickVersion(&version),GetMagickVersion(&version));
1136 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g\n",
1138 (void) WriteBlobString(image,buffer);
1139 (void) WriteBlobString(image,"/Type /Metadata\n");
1140 (void) WriteBlobString(image,">>\nstream\n");
1141 (void) WriteBlobString(image,xmp_profile);
1142 (void) WriteBlobString(image,"endstream\n");
1143 (void) WriteBlobString(image,"endobj\n");
1148 xref[object++]=TellBlob(image);
1150 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1152 (void) WriteBlobString(image,buffer);
1153 (void) WriteBlobString(image,"<<\n");
1154 (void) WriteBlobString(image,"/Type /Pages\n");
1155 (void) FormatLocaleString(buffer,MaxTextExtent,"/Kids [ %.20g 0 R ",
1157 (void) WriteBlobString(image,buffer);
1158 count=(ssize_t) (pages_id+ObjectsPerImage+1);
1159 if (image_info->adjoin != MagickFalse)
1165 Predict page object id's.
1168 for ( ; GetNextImageInList(kid_image) != (Image *) NULL; count+=ObjectsPerImage)
1170 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 R ",(double)
1172 (void) WriteBlobString(image,buffer);
1173 kid_image=GetNextImageInList(kid_image);
1175 xref=(MagickOffsetType *) ResizeQuantumMemory(xref,(size_t) count+2048UL,
1177 if (xref == (MagickOffsetType *) NULL)
1178 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1180 (void) WriteBlobString(image,"]\n");
1181 (void) FormatLocaleString(buffer,MaxTextExtent,"/Count %.20g\n",
1182 (double) ((count-pages_id)/ObjectsPerImage));
1183 (void) WriteBlobString(image,buffer);
1184 (void) WriteBlobString(image,">>\n");
1185 (void) WriteBlobString(image,"endobj\n");
1189 compression=image->compression;
1190 if (image_info->compression != UndefinedCompression)
1191 compression=image_info->compression;
1192 switch (compression)
1194 case FaxCompression:
1195 case Group4Compression:
1197 if ((IsImageMonochrome(image,&image->exception) == MagickFalse) ||
1198 (image->matte != MagickFalse))
1199 compression=RLECompression;
1202 #if !defined(MAGICKCORE_JPEG_DELEGATE)
1203 case JPEGCompression:
1205 compression=RLECompression;
1206 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1207 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
1212 #if !defined(MAGICKCORE_JP2_DELEGATE)
1213 case JPEG2000Compression:
1215 compression=RLECompression;
1216 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1217 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JP2)",
1222 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
1223 case ZipCompression:
1225 compression=RLECompression;
1226 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1227 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZLIB)",
1232 case LZWCompression:
1234 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1235 compression=RLECompression; /* LZW compression is forbidden */
1240 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1241 compression=RLECompression; /* ASCII 85 compression is forbidden */
1247 if (compression == JPEG2000Compression)
1249 if (image->colorspace != RGBColorspace)
1250 (void) TransformImageColorspace(image,RGBColorspace);
1253 Scale relative to dots-per-inch.
1255 delta.x=DefaultResolution;
1256 delta.y=DefaultResolution;
1257 resolution.x=image->x_resolution;
1258 resolution.y=image->y_resolution;
1259 if ((resolution.x == 0.0) || (resolution.y == 0.0))
1261 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
1262 resolution.x=geometry_info.rho;
1263 resolution.y=geometry_info.sigma;
1264 if ((flags & SigmaValue) == 0)
1265 resolution.y=resolution.x;
1267 if (image_info->density != (char *) NULL)
1269 flags=ParseGeometry(image_info->density,&geometry_info);
1270 resolution.x=geometry_info.rho;
1271 resolution.y=geometry_info.sigma;
1272 if ((flags & SigmaValue) == 0)
1273 resolution.y=resolution.x;
1275 if (image->units == PixelsPerCentimeterResolution)
1277 resolution.x=(double) ((size_t) (100.0*2.54*resolution.x+0.5)/100.0);
1278 resolution.y=(double) ((size_t) (100.0*2.54*resolution.y+0.5)/100.0);
1280 SetGeometry(image,&geometry);
1281 (void) FormatLocaleString(page_geometry,MaxTextExtent,"%.20gx%.20g",
1282 (double) image->columns,(double) image->rows);
1283 if (image_info->page != (char *) NULL)
1284 (void) CopyMagickString(page_geometry,image_info->page,MaxTextExtent);
1286 if ((image->page.width != 0) && (image->page.height != 0))
1287 (void) FormatLocaleString(page_geometry,MaxTextExtent,
1288 "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,
1289 (double) image->page.height,(double) image->page.x,(double)
1292 if ((image->gravity != UndefinedGravity) &&
1293 (LocaleCompare(image_info->magick,"PDF") == 0))
1294 (void) CopyMagickString(page_geometry,PSPageGeometry,MaxTextExtent);
1295 (void) ConcatenateMagickString(page_geometry,">",MaxTextExtent);
1296 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
1297 &geometry.width,&geometry.height);
1298 scale.x=(double) (geometry.width*delta.x)/resolution.x;
1299 geometry.width=(size_t) floor(scale.x+0.5);
1300 scale.y=(double) (geometry.height*delta.y)/resolution.y;
1301 geometry.height=(size_t) floor(scale.y+0.5);
1302 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
1303 (void) ParseGravityGeometry(image,page_geometry,&page_info,
1305 if (image->gravity != UndefinedGravity)
1307 geometry.x=(-page_info.x);
1308 geometry.y=(ssize_t) (media_info.height+page_info.y-image->rows);
1311 if (image_info->pointsize != 0.0)
1312 pointsize=image_info->pointsize;
1314 value=GetImageProperty(image,"label");
1315 if (value != (const char *) NULL)
1316 text_size=(size_t) (MultilineCensus(value)*pointsize+12);
1321 xref[object++]=TellBlob(image);
1322 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1324 (void) WriteBlobString(image,buffer);
1325 (void) WriteBlobString(image,"<<\n");
1326 (void) WriteBlobString(image,"/Type /Page\n");
1327 (void) FormatLocaleString(buffer,MaxTextExtent,"/Parent %.20g 0 R\n",
1329 (void) WriteBlobString(image,buffer);
1330 (void) WriteBlobString(image,"/Resources <<\n");
1331 labels=(char **) NULL;
1332 value=GetImageProperty(image,"label");
1333 if (value != (const char *) NULL)
1334 labels=StringToList(value);
1335 if (labels != (char **) NULL)
1337 (void) FormatLocaleString(buffer,MaxTextExtent,
1338 "/Font << /F%.20g %.20g 0 R >>\n",(double) image->scene,(double)
1340 (void) WriteBlobString(image,buffer);
1342 (void) FormatLocaleString(buffer,MaxTextExtent,
1343 "/XObject << /Im%.20g %.20g 0 R >>\n",(double) image->scene,(double)
1345 (void) WriteBlobString(image,buffer);
1346 (void) FormatLocaleString(buffer,MaxTextExtent,"/ProcSet %.20g 0 R >>\n",
1348 (void) WriteBlobString(image,buffer);
1349 (void) FormatLocaleString(buffer,MaxTextExtent,
1350 "/MediaBox [0 0 %g %g]\n",72.0*media_info.width/resolution.x,
1351 72.0*media_info.height/resolution.y);
1352 (void) WriteBlobString(image,buffer);
1353 (void) FormatLocaleString(buffer,MaxTextExtent,
1354 "/CropBox [0 0 %g %g]\n",72.0*media_info.width/resolution.x,
1355 72.0*media_info.height/resolution.y);
1356 (void) WriteBlobString(image,buffer);
1357 (void) FormatLocaleString(buffer,MaxTextExtent,"/Contents %.20g 0 R\n",
1359 (void) WriteBlobString(image,buffer);
1360 (void) FormatLocaleString(buffer,MaxTextExtent,"/Thumb %.20g 0 R\n",
1362 (void) WriteBlobString(image,buffer);
1363 (void) WriteBlobString(image,">>\n");
1364 (void) WriteBlobString(image,"endobj\n");
1366 Write Contents object.
1368 xref[object++]=TellBlob(image);
1369 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1371 (void) WriteBlobString(image,buffer);
1372 (void) WriteBlobString(image,"<<\n");
1373 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
1375 (void) WriteBlobString(image,buffer);
1376 (void) WriteBlobString(image,">>\n");
1377 (void) WriteBlobString(image,"stream\n");
1378 offset=TellBlob(image);
1379 (void) WriteBlobString(image,"q\n");
1380 if (labels != (char **) NULL)
1381 for (i=0; labels[i] != (char *) NULL; i++)
1383 (void) WriteBlobString(image,"BT\n");
1384 (void) FormatLocaleString(buffer,MaxTextExtent,"/F%.20g %g Tf\n",
1385 (double) image->scene,pointsize);
1386 (void) WriteBlobString(image,buffer);
1387 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g Td\n",
1388 (double) geometry.x,(double) (geometry.y+geometry.height+i*pointsize+
1390 (void) WriteBlobString(image,buffer);
1391 (void) FormatLocaleString(buffer,MaxTextExtent,"(%s) Tj\n",labels[i]);
1392 (void) WriteBlobString(image,buffer);
1393 (void) WriteBlobString(image,"ET\n");
1394 labels[i]=DestroyString(labels[i]);
1396 (void) FormatLocaleString(buffer,MaxTextExtent,"%g 0 0 %g %.20g %.20g cm\n",
1397 scale.x,scale.y,(double) geometry.x,(double) geometry.y);
1398 (void) WriteBlobString(image,buffer);
1399 (void) FormatLocaleString(buffer,MaxTextExtent,"/Im%.20g Do\n",
1400 (double) image->scene);
1401 (void) WriteBlobString(image,buffer);
1402 (void) WriteBlobString(image,"Q\n");
1403 offset=TellBlob(image)-offset;
1404 (void) WriteBlobString(image,"endstream\n");
1405 (void) WriteBlobString(image,"endobj\n");
1407 Write Length object.
1409 xref[object++]=TellBlob(image);
1410 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1412 (void) WriteBlobString(image,buffer);
1413 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
1414 (void) WriteBlobString(image,buffer);
1415 (void) WriteBlobString(image,"endobj\n");
1417 Write Procset object.
1419 xref[object++]=TellBlob(image);
1420 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",
1422 (void) WriteBlobString(image,buffer);
1423 if ((image->storage_class == DirectClass) || (image->colors > 256))
1424 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageC",MaxTextExtent);
1426 if ((compression == FaxCompression) || (compression == Group4Compression))
1427 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageB",MaxTextExtent);
1429 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageI",MaxTextExtent);
1430 (void) WriteBlobString(image,buffer);
1431 (void) WriteBlobString(image," ]\n");
1432 (void) WriteBlobString(image,"endobj\n");
1436 xref[object++]=TellBlob(image);
1437 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",
1439 (void) WriteBlobString(image,buffer);
1440 (void) WriteBlobString(image,"<<\n");
1441 if (labels != (char **) NULL)
1443 (void) WriteBlobString(image,"/Type /Font\n");
1444 (void) WriteBlobString(image,"/Subtype /Type1\n");
1445 (void) FormatLocaleString(buffer,MaxTextExtent,"/Name /F%.20g\n",
1446 (double) image->scene);
1447 (void) WriteBlobString(image,buffer);
1448 (void) WriteBlobString(image,"/BaseFont /Helvetica\n");
1449 (void) WriteBlobString(image,"/Encoding /MacRomanEncoding\n");
1450 labels=(char **) RelinquishMagickMemory(labels);
1452 (void) WriteBlobString(image,">>\n");
1453 (void) WriteBlobString(image,"endobj\n");
1455 Write XObject object.
1457 xref[object++]=TellBlob(image);
1458 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1460 (void) WriteBlobString(image,buffer);
1461 (void) WriteBlobString(image,"<<\n");
1462 (void) WriteBlobString(image,"/Type /XObject\n");
1463 (void) WriteBlobString(image,"/Subtype /Image\n");
1464 (void) FormatLocaleString(buffer,MaxTextExtent,"/Name /Im%.20g\n",
1465 (double) image->scene);
1466 (void) WriteBlobString(image,buffer);
1467 switch (compression)
1471 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"ASCII85Decode");
1474 case JPEGCompression:
1476 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"DCTDecode");
1477 if (image->colorspace != CMYKColorspace)
1479 (void) WriteBlobString(image,buffer);
1480 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1484 case JPEG2000Compression:
1486 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"JPXDecode");
1487 if (image->colorspace != CMYKColorspace)
1489 (void) WriteBlobString(image,buffer);
1490 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1494 case LZWCompression:
1496 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"LZWDecode");
1499 case ZipCompression:
1501 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"FlateDecode");
1504 case FaxCompression:
1505 case Group4Compression:
1507 (void) CopyMagickString(buffer,"/Filter [ /CCITTFaxDecode ]\n",
1509 (void) WriteBlobString(image,buffer);
1510 (void) FormatLocaleString(buffer,MaxTextExtent,"/DecodeParms [ << "
1511 "/K %s /BlackIs1 false /Columns %.20g /Rows %.20g >> ]\n",CCITTParam,
1512 (double) image->columns,(double) image->rows);
1517 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
1522 (void) WriteBlobString(image,buffer);
1523 (void) FormatLocaleString(buffer,MaxTextExtent,"/Width %.20g\n",(double)
1525 (void) WriteBlobString(image,buffer);
1526 (void) FormatLocaleString(buffer,MaxTextExtent,"/Height %.20g\n",(double)
1528 (void) WriteBlobString(image,buffer);
1529 (void) FormatLocaleString(buffer,MaxTextExtent,"/ColorSpace %.20g 0 R\n",
1531 (void) WriteBlobString(image,buffer);
1532 (void) FormatLocaleString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
1533 (compression == FaxCompression) || (compression == Group4Compression) ?
1535 (void) WriteBlobString(image,buffer);
1536 if (image->matte != MagickFalse)
1538 (void) FormatLocaleString(buffer,MaxTextExtent,"/SMask %.20g 0 R\n",
1540 (void) WriteBlobString(image,buffer);
1542 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
1544 (void) WriteBlobString(image,buffer);
1545 (void) WriteBlobString(image,">>\n");
1546 (void) WriteBlobString(image,"stream\n");
1547 offset=TellBlob(image);
1548 number_pixels=(MagickSizeType) image->columns*image->rows;
1549 if ((4*number_pixels) != (MagickSizeType) ((size_t) (4*number_pixels)))
1550 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1551 if ((compression == FaxCompression) || (compression == Group4Compression) ||
1552 ((image_info->type != TrueColorType) &&
1553 (IsImageGray(image,&image->exception) != MagickFalse)))
1555 switch (compression)
1557 case FaxCompression:
1558 case Group4Compression:
1560 if (LocaleCompare(CCITTParam,"0") == 0)
1562 (void) HuffmanEncodeImage(image_info,image,image);
1565 (void) Huffman2DEncodeImage(image_info,image,image);
1568 case JPEGCompression:
1570 status=InjectImageBlob(image_info,image,image,"jpeg",
1572 if (status == MagickFalse)
1573 ThrowWriterException(CoderError,image->exception.reason);
1576 case JPEG2000Compression:
1578 status=InjectImageBlob(image_info,image,image,"jp2",
1580 if (status == MagickFalse)
1581 ThrowWriterException(CoderError,image->exception.reason);
1584 case RLECompression:
1588 Allocate pixel array.
1590 length=(size_t) number_pixels;
1591 pixels=(unsigned char *) AcquireQuantumMemory(length,
1593 if (pixels == (unsigned char *) NULL)
1594 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1596 Dump Runlength encoded pixels.
1599 for (y=0; y < (ssize_t) image->rows; y++)
1601 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1602 if (p == (const Quantum *) NULL)
1604 for (x=0; x < (ssize_t) image->columns; x++)
1606 *q++=ScaleQuantumToChar(GetPixelIntensity(image,p));
1607 p+=GetPixelChannels(image);
1609 if (image->previous == (Image *) NULL)
1611 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1613 if (status == MagickFalse)
1617 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1618 if (compression == ZipCompression)
1619 status=ZLIBEncodeImage(image,length,pixels);
1622 if (compression == LZWCompression)
1623 status=LZWEncodeImage(image,length,pixels);
1625 status=PackbitsEncodeImage(image,length,pixels);
1626 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1627 if (status == MagickFalse)
1629 (void) CloseBlob(image);
1630 return(MagickFalse);
1637 Dump uncompressed PseudoColor packets.
1639 Ascii85Initialize(image);
1640 for (y=0; y < (ssize_t) image->rows; y++)
1642 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1643 if (p == (const Quantum *) NULL)
1645 for (x=0; x < (ssize_t) image->columns; x++)
1647 Ascii85Encode(image,ScaleQuantumToChar(
1648 GetPixelIntensity(image,p)));
1649 p+=GetPixelChannels(image);
1651 if (image->previous == (Image *) NULL)
1653 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1655 if (status == MagickFalse)
1659 Ascii85Flush(image);
1665 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1666 (compression == JPEGCompression) ||
1667 (compression == JPEG2000Compression))
1668 switch (compression)
1670 case JPEGCompression:
1672 status=InjectImageBlob(image_info,image,image,"jpeg",
1674 if (status == MagickFalse)
1675 ThrowWriterException(CoderError,image->exception.reason);
1678 case JPEG2000Compression:
1680 status=InjectImageBlob(image_info,image,image,"jp2",
1682 if (status == MagickFalse)
1683 ThrowWriterException(CoderError,image->exception.reason);
1686 case RLECompression:
1690 Allocate pixel array.
1692 length=(size_t) number_pixels;
1693 pixels=(unsigned char *) AcquireQuantumMemory(length,
1695 length*=image->colorspace == CMYKColorspace ? 4UL : 3UL;
1696 if (pixels == (unsigned char *) NULL)
1697 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1699 Dump runoffset encoded pixels.
1702 for (y=0; y < (ssize_t) image->rows; y++)
1704 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1705 if (p == (const Quantum *) NULL)
1707 for (x=0; x < (ssize_t) image->columns; x++)
1709 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1710 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1711 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1712 if (image->colorspace == CMYKColorspace)
1713 *q++=ScaleQuantumToChar(GetPixelBlack(image,p));
1714 p+=GetPixelChannels(image);
1716 if (image->previous == (Image *) NULL)
1718 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1720 if (status == MagickFalse)
1724 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1725 if (compression == ZipCompression)
1726 status=ZLIBEncodeImage(image,length,pixels);
1729 if (compression == LZWCompression)
1730 status=LZWEncodeImage(image,length,pixels);
1732 status=PackbitsEncodeImage(image,length,pixels);
1733 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1734 if (status == MagickFalse)
1736 (void) CloseBlob(image);
1737 return(MagickFalse);
1744 Dump uncompressed DirectColor packets.
1746 Ascii85Initialize(image);
1747 for (y=0; y < (ssize_t) image->rows; y++)
1749 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1750 if (p == (const Quantum *) NULL)
1752 for (x=0; x < (ssize_t) image->columns; x++)
1754 Ascii85Encode(image,ScaleQuantumToChar(GetPixelRed(image,p)));
1755 Ascii85Encode(image,ScaleQuantumToChar(GetPixelGreen(image,p)));
1756 Ascii85Encode(image,ScaleQuantumToChar(GetPixelBlue(image,p)));
1757 if (image->colorspace == CMYKColorspace)
1758 Ascii85Encode(image,ScaleQuantumToChar(
1759 GetPixelBlack(image,p)));
1760 p+=GetPixelChannels(image);
1762 if (image->previous == (Image *) NULL)
1764 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1766 if (status == MagickFalse)
1770 Ascii85Flush(image);
1777 Dump number of colors and colormap.
1779 switch (compression)
1781 case RLECompression:
1785 Allocate pixel array.
1787 length=(size_t) number_pixels;
1788 pixels=(unsigned char *) AcquireQuantumMemory(length,
1790 if (pixels == (unsigned char *) NULL)
1791 ThrowWriterException(ResourceLimitError,
1792 "MemoryAllocationFailed");
1794 Dump Runlength encoded pixels.
1797 for (y=0; y < (ssize_t) image->rows; y++)
1799 p=GetVirtualPixels(image,0,y,image->columns,1,
1801 if (p == (const Quantum *) NULL)
1803 for (x=0; x < (ssize_t) image->columns; x++)
1805 *q++=(unsigned char) GetPixelIndex(image,p);
1806 p+=GetPixelChannels(image);
1808 if (image->previous == (Image *) NULL)
1810 status=SetImageProgress(image,SaveImageTag,
1811 (MagickOffsetType) y,image->rows);
1812 if (status == MagickFalse)
1816 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1817 if (compression == ZipCompression)
1818 status=ZLIBEncodeImage(image,length,pixels);
1821 if (compression == LZWCompression)
1822 status=LZWEncodeImage(image,length,pixels);
1824 status=PackbitsEncodeImage(image,length,pixels);
1825 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1826 if (status == MagickFalse)
1828 (void) CloseBlob(image);
1829 return(MagickFalse);
1836 Dump uncompressed PseudoColor packets.
1838 Ascii85Initialize(image);
1839 for (y=0; y < (ssize_t) image->rows; y++)
1841 p=GetVirtualPixels(image,0,y,image->columns,1,
1843 if (p == (const Quantum *) NULL)
1845 for (x=0; x < (ssize_t) image->columns; x++)
1847 Ascii85Encode(image,(unsigned char) GetPixelIndex(image,p));
1848 p+=GetPixelChannels(image);
1850 if (image->previous == (Image *) NULL)
1852 status=SetImageProgress(image,SaveImageTag,
1853 (MagickOffsetType) y,image->rows);
1854 if (status == MagickFalse)
1858 Ascii85Flush(image);
1863 offset=TellBlob(image)-offset;
1864 (void) WriteBlobString(image,"\nendstream\n");
1865 (void) WriteBlobString(image,"endobj\n");
1867 Write Length object.
1869 xref[object++]=TellBlob(image);
1870 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1872 (void) WriteBlobString(image,buffer);
1873 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",
1875 (void) WriteBlobString(image,buffer);
1876 (void) WriteBlobString(image,"endobj\n");
1878 Write Colorspace object.
1880 xref[object++]=TellBlob(image);
1881 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1883 (void) WriteBlobString(image,buffer);
1884 if (image->colorspace == CMYKColorspace)
1885 (void) CopyMagickString(buffer,"/DeviceCMYK\n",MaxTextExtent);
1887 if ((compression == FaxCompression) ||
1888 (compression == Group4Compression) ||
1889 ((image_info->type != TrueColorType) &&
1890 (IsImageGray(image,&image->exception) != MagickFalse)))
1891 (void) CopyMagickString(buffer,"/DeviceGray\n",MaxTextExtent);
1893 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1894 (compression == JPEGCompression) ||
1895 (compression == JPEG2000Compression))
1896 (void) CopyMagickString(buffer,"/DeviceRGB\n",MaxTextExtent);
1898 (void) FormatLocaleString(buffer,MaxTextExtent,
1899 "[ /Indexed /DeviceRGB %.20g %.20g 0 R ]\n",(double) image->colors-
1900 1,(double) object+3);
1901 (void) WriteBlobString(image,buffer);
1902 (void) WriteBlobString(image,"endobj\n");
1906 SetGeometry(image,&geometry);
1907 (void) ParseMetaGeometry("106x106+0+0>",&geometry.x,&geometry.y,
1908 &geometry.width,&geometry.height);
1909 tile_image=ThumbnailImage(image,geometry.width,geometry.height,
1911 if (tile_image == (Image *) NULL)
1912 ThrowWriterException(ResourceLimitError,image->exception.reason);
1913 xref[object++]=TellBlob(image);
1914 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1916 (void) WriteBlobString(image,buffer);
1917 (void) WriteBlobString(image,"<<\n");
1918 switch (compression)
1922 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"ASCII85Decode");
1925 case JPEGCompression:
1927 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"DCTDecode");
1928 if (image->colorspace != CMYKColorspace)
1930 (void) WriteBlobString(image,buffer);
1931 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1935 case JPEG2000Compression:
1937 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"JPXDecode");
1938 if (image->colorspace != CMYKColorspace)
1940 (void) WriteBlobString(image,buffer);
1941 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1945 case LZWCompression:
1947 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"LZWDecode");
1950 case ZipCompression:
1952 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"FlateDecode");
1955 case FaxCompression:
1956 case Group4Compression:
1958 (void) CopyMagickString(buffer,"/Filter [ /CCITTFaxDecode ]\n",
1960 (void) WriteBlobString(image,buffer);
1961 (void) FormatLocaleString(buffer,MaxTextExtent,"/DecodeParms [ << "
1962 "/K %s /BlackIs1 false /Columns %.20g /Rows %.20g >> ]\n",CCITTParam,
1963 (double) tile_image->columns,(double) tile_image->rows);
1968 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
1973 (void) WriteBlobString(image,buffer);
1974 (void) FormatLocaleString(buffer,MaxTextExtent,"/Width %.20g\n",(double)
1975 tile_image->columns);
1976 (void) WriteBlobString(image,buffer);
1977 (void) FormatLocaleString(buffer,MaxTextExtent,"/Height %.20g\n",(double)
1979 (void) WriteBlobString(image,buffer);
1980 (void) FormatLocaleString(buffer,MaxTextExtent,"/ColorSpace %.20g 0 R\n",
1982 (void) WriteBlobString(image,buffer);
1983 (void) FormatLocaleString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
1984 (compression == FaxCompression) || (compression == Group4Compression) ?
1986 (void) WriteBlobString(image,buffer);
1987 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
1989 (void) WriteBlobString(image,buffer);
1990 (void) WriteBlobString(image,">>\n");
1991 (void) WriteBlobString(image,"stream\n");
1992 offset=TellBlob(image);
1993 number_pixels=(MagickSizeType) tile_image->columns*tile_image->rows;
1994 if ((compression == FaxCompression) ||
1995 (compression == Group4Compression) ||
1996 ((image_info->type != TrueColorType) &&
1997 (IsImageGray(tile_image,&image->exception) != MagickFalse)))
1999 switch (compression)
2001 case FaxCompression:
2002 case Group4Compression:
2004 if (LocaleCompare(CCITTParam,"0") == 0)
2006 (void) HuffmanEncodeImage(image_info,image,tile_image);
2009 (void) Huffman2DEncodeImage(image_info,image,tile_image);
2012 case JPEGCompression:
2014 status=InjectImageBlob(image_info,image,tile_image,"jpeg",
2016 if (status == MagickFalse)
2017 ThrowWriterException(CoderError,tile_image->exception.reason);
2020 case JPEG2000Compression:
2022 status=InjectImageBlob(image_info,image,tile_image,"jp2",
2024 if (status == MagickFalse)
2025 ThrowWriterException(CoderError,tile_image->exception.reason);
2028 case RLECompression:
2032 Allocate pixel array.
2034 length=(size_t) number_pixels;
2035 pixels=(unsigned char *) AcquireQuantumMemory(length,
2037 if (pixels == (unsigned char *) NULL)
2039 tile_image=DestroyImage(tile_image);
2040 ThrowWriterException(ResourceLimitError,
2041 "MemoryAllocationFailed");
2044 Dump Runlength encoded pixels.
2047 for (y=0; y < (ssize_t) tile_image->rows; y++)
2049 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2050 &tile_image->exception);
2051 if (p == (const Quantum *) NULL)
2053 for (x=0; x < (ssize_t) tile_image->columns; x++)
2055 *q++=ScaleQuantumToChar(GetPixelIntensity(tile_image,p));
2056 p+=GetPixelChannels(tile_image);
2059 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2060 if (compression == ZipCompression)
2061 status=ZLIBEncodeImage(image,length,pixels);
2064 if (compression == LZWCompression)
2065 status=LZWEncodeImage(image,length,pixels);
2067 status=PackbitsEncodeImage(image,length,pixels);
2068 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2069 if (status == MagickFalse)
2071 (void) CloseBlob(image);
2072 return(MagickFalse);
2079 Dump uncompressed PseudoColor packets.
2081 Ascii85Initialize(image);
2082 for (y=0; y < (ssize_t) tile_image->rows; y++)
2084 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2085 &tile_image->exception);
2086 if (p == (const Quantum *) NULL)
2088 for (x=0; x < (ssize_t) tile_image->columns; x++)
2090 Ascii85Encode(image,
2091 ScaleQuantumToChar(GetPixelIntensity(tile_image,p)));
2092 p+=GetPixelChannels(tile_image);
2095 Ascii85Flush(image);
2101 if ((tile_image->storage_class == DirectClass) ||
2102 (tile_image->colors > 256) || (compression == JPEGCompression) ||
2103 (compression == JPEG2000Compression))
2104 switch (compression)
2106 case JPEGCompression:
2108 status=InjectImageBlob(image_info,image,tile_image,"jpeg",
2110 if (status == MagickFalse)
2111 ThrowWriterException(CoderError,tile_image->exception.reason);
2114 case JPEG2000Compression:
2116 status=InjectImageBlob(image_info,image,tile_image,"jp2",
2118 if (status == MagickFalse)
2119 ThrowWriterException(CoderError,tile_image->exception.reason);
2122 case RLECompression:
2126 Allocate pixel array.
2128 length=(size_t) number_pixels;
2129 pixels=(unsigned char *) AcquireQuantumMemory(length,4*
2131 length*=tile_image->colorspace == CMYKColorspace ? 4UL : 3UL;
2132 if (pixels == (unsigned char *) NULL)
2134 tile_image=DestroyImage(tile_image);
2135 ThrowWriterException(ResourceLimitError,
2136 "MemoryAllocationFailed");
2139 Dump runoffset encoded pixels.
2142 for (y=0; y < (ssize_t) tile_image->rows; y++)
2144 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2145 &tile_image->exception);
2146 if (p == (const Quantum *) NULL)
2148 for (x=0; x < (ssize_t) tile_image->columns; x++)
2150 *q++=ScaleQuantumToChar(GetPixelRed(tile_image,p));
2151 *q++=ScaleQuantumToChar(GetPixelGreen(tile_image,p));
2152 *q++=ScaleQuantumToChar(GetPixelBlue(tile_image,p));
2153 if (image->colorspace == CMYKColorspace)
2154 *q++=ScaleQuantumToChar(GetPixelBlack(tile_image,p));
2155 p+=GetPixelChannels(tile_image);
2158 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2159 if (compression == ZipCompression)
2160 status=ZLIBEncodeImage(image,length,pixels);
2163 if (compression == LZWCompression)
2164 status=LZWEncodeImage(image,length,pixels);
2166 status=PackbitsEncodeImage(image,length,pixels);
2167 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2168 if (status == MagickFalse)
2170 (void) CloseBlob(image);
2171 return(MagickFalse);
2178 Dump uncompressed DirectColor packets.
2180 Ascii85Initialize(image);
2181 for (y=0; y < (ssize_t) tile_image->rows; y++)
2183 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2184 &tile_image->exception);
2185 if (p == (const Quantum *) NULL)
2187 for (x=0; x < (ssize_t) tile_image->columns; x++)
2189 Ascii85Encode(image,ScaleQuantumToChar(
2190 GetPixelRed(tile_image,p)));
2191 Ascii85Encode(image,ScaleQuantumToChar(
2192 GetPixelGreen(tile_image,p)));
2193 Ascii85Encode(image,ScaleQuantumToChar(
2194 GetPixelBlue(tile_image,p)));
2195 if (image->colorspace == CMYKColorspace)
2196 Ascii85Encode(image,ScaleQuantumToChar(
2197 GetPixelBlack(tile_image,p)));
2198 p+=GetPixelChannels(tile_image);
2201 Ascii85Flush(image);
2208 Dump number of colors and colormap.
2210 switch (compression)
2212 case RLECompression:
2216 Allocate pixel array.
2218 length=(size_t) number_pixels;
2219 pixels=(unsigned char *) AcquireQuantumMemory(length,
2221 if (pixels == (unsigned char *) NULL)
2223 tile_image=DestroyImage(tile_image);
2224 ThrowWriterException(ResourceLimitError,
2225 "MemoryAllocationFailed");
2228 Dump Runlength encoded pixels.
2231 for (y=0; y < (ssize_t) tile_image->rows; y++)
2233 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2234 &tile_image->exception);
2235 if (p == (const Quantum *) NULL)
2237 for (x=0; x < (ssize_t) tile_image->columns; x++)
2239 *q++=(unsigned char) GetPixelIndex(tile_image,p);
2240 q+=GetPixelChannels(image);
2243 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2244 if (compression == ZipCompression)
2245 status=ZLIBEncodeImage(image,length,pixels);
2248 if (compression == LZWCompression)
2249 status=LZWEncodeImage(image,length,pixels);
2251 status=PackbitsEncodeImage(image,length,pixels);
2252 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2253 if (status == MagickFalse)
2255 (void) CloseBlob(image);
2256 return(MagickFalse);
2263 Dump uncompressed PseudoColor packets.
2265 Ascii85Initialize(image);
2266 for (y=0; y < (ssize_t) tile_image->rows; y++)
2268 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2269 &tile_image->exception);
2270 if (p == (const Quantum *) NULL)
2272 for (x=0; x < (ssize_t) tile_image->columns; x++)
2274 Ascii85Encode(image,(unsigned char)
2275 GetPixelIndex(tile_image,p));
2276 p+=GetPixelChannels(image);
2279 Ascii85Flush(image);
2284 tile_image=DestroyImage(tile_image);
2285 offset=TellBlob(image)-offset;
2286 (void) WriteBlobString(image,"\nendstream\n");
2287 (void) WriteBlobString(image,"endobj\n");
2289 Write Length object.
2291 xref[object++]=TellBlob(image);
2292 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2294 (void) WriteBlobString(image,buffer);
2295 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
2296 (void) WriteBlobString(image,buffer);
2297 (void) WriteBlobString(image,"endobj\n");
2298 xref[object++]=TellBlob(image);
2299 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2301 (void) WriteBlobString(image,buffer);
2302 if ((image->storage_class != DirectClass) && (image->colors <= 256) &&
2303 (compression != FaxCompression) && (compression != Group4Compression))
2306 Write Colormap object.
2308 (void) WriteBlobString(image,"<<\n");
2309 if (compression == NoCompression)
2310 (void) WriteBlobString(image,"/Filter [ /ASCII85Decode ]\n");
2311 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
2313 (void) WriteBlobString(image,buffer);
2314 (void) WriteBlobString(image,">>\n");
2315 (void) WriteBlobString(image,"stream\n");
2316 offset=TellBlob(image);
2317 if (compression == NoCompression)
2318 Ascii85Initialize(image);
2319 for (i=0; i < (ssize_t) image->colors; i++)
2321 if (compression == NoCompression)
2323 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].red));
2324 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].green));
2325 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].blue));
2328 (void) WriteBlobByte(image,
2329 ScaleQuantumToChar(image->colormap[i].red));
2330 (void) WriteBlobByte(image,
2331 ScaleQuantumToChar(image->colormap[i].green));
2332 (void) WriteBlobByte(image,
2333 ScaleQuantumToChar(image->colormap[i].blue));
2335 if (compression == NoCompression)
2336 Ascii85Flush(image);
2337 offset=TellBlob(image)-offset;
2338 (void) WriteBlobString(image,"\nendstream\n");
2340 (void) WriteBlobString(image,"endobj\n");
2342 Write Length object.
2344 xref[object++]=TellBlob(image);
2345 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2347 (void) WriteBlobString(image,buffer);
2348 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
2350 (void) WriteBlobString(image,buffer);
2351 (void) WriteBlobString(image,"endobj\n");
2353 Write softmask object.
2355 xref[object++]=TellBlob(image);
2356 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2358 (void) WriteBlobString(image,buffer);
2359 (void) WriteBlobString(image,"<<\n");
2360 if (image->matte == MagickFalse)
2361 (void) WriteBlobString(image,">>\n");
2364 (void) WriteBlobString(image,"/Type /XObject\n");
2365 (void) WriteBlobString(image,"/Subtype /Image\n");
2366 (void) FormatLocaleString(buffer,MaxTextExtent,"/Name /Ma%.20g\n",
2367 (double) image->scene);
2368 (void) WriteBlobString(image,buffer);
2369 switch (compression)
2373 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
2377 case LZWCompression:
2379 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"LZWDecode");
2382 case ZipCompression:
2384 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
2390 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
2395 (void) WriteBlobString(image,buffer);
2396 (void) FormatLocaleString(buffer,MaxTextExtent,"/Width %.20g\n",
2397 (double) image->columns);
2398 (void) WriteBlobString(image,buffer);
2399 (void) FormatLocaleString(buffer,MaxTextExtent,"/Height %.20g\n",
2400 (double) image->rows);
2401 (void) WriteBlobString(image,buffer);
2402 (void) WriteBlobString(image,"/ColorSpace /DeviceGray\n");
2403 (void) FormatLocaleString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
2404 (compression == FaxCompression) || (compression == Group4Compression)
2406 (void) WriteBlobString(image,buffer);
2407 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
2409 (void) WriteBlobString(image,buffer);
2410 (void) WriteBlobString(image,">>\n");
2411 (void) WriteBlobString(image,"stream\n");
2412 offset=TellBlob(image);
2413 number_pixels=(MagickSizeType) image->columns*image->rows;
2414 switch (compression)
2416 case RLECompression:
2420 Allocate pixel array.
2422 length=(size_t) number_pixels;
2423 pixels=(unsigned char *) AcquireQuantumMemory(length,
2425 if (pixels == (unsigned char *) NULL)
2427 image=DestroyImage(image);
2428 ThrowWriterException(ResourceLimitError,
2429 "MemoryAllocationFailed");
2432 Dump Runlength encoded pixels.
2435 for (y=0; y < (ssize_t) image->rows; y++)
2437 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
2438 if (p == (const Quantum *) NULL)
2440 for (x=0; x < (ssize_t) image->columns; x++)
2442 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
2443 p+=GetPixelChannels(image);
2446 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2447 if (compression == ZipCompression)
2448 status=ZLIBEncodeImage(image,length,pixels);
2451 if (compression == LZWCompression)
2452 status=LZWEncodeImage(image,length,pixels);
2454 status=PackbitsEncodeImage(image,length,pixels);
2455 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2456 if (status == MagickFalse)
2458 (void) CloseBlob(image);
2459 return(MagickFalse);
2466 Dump uncompressed PseudoColor packets.
2468 Ascii85Initialize(image);
2469 for (y=0; y < (ssize_t) image->rows; y++)
2471 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
2472 if (p == (const Quantum *) NULL)
2474 for (x=0; x < (ssize_t) image->columns; x++)
2476 Ascii85Encode(image,ScaleQuantumToChar(GetPixelAlpha(image,p)));
2477 p+=GetPixelChannels(image);
2480 Ascii85Flush(image);
2484 offset=TellBlob(image)-offset;
2485 (void) WriteBlobString(image,"\nendstream\n");
2487 (void) WriteBlobString(image,"endobj\n");
2489 Write Length object.
2491 xref[object++]=TellBlob(image);
2492 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2494 (void) WriteBlobString(image,buffer);
2495 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
2496 (void) WriteBlobString(image,buffer);
2497 (void) WriteBlobString(image,"endobj\n");
2498 if (GetNextImageInList(image) == (Image *) NULL)
2500 image=SyncNextImageInList(image);
2501 status=SetImageProgress(image,SaveImagesTag,scene++,
2502 GetImageListLength(image));
2503 if (status == MagickFalse)
2505 } while (image_info->adjoin != MagickFalse);
2507 Write Metadata object.
2509 xref[object++]=TellBlob(image);
2511 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2513 (void) WriteBlobString(image,buffer);
2514 (void) WriteBlobString(image,"<<\n");
2515 GetPathComponent(image->filename,BasePath,basename);
2516 (void) FormatLocaleString(buffer,MaxTextExtent,"/Title (%s)\n",
2517 EscapeParenthesis(basename));
2518 (void) WriteBlobString(image,buffer);
2519 seconds=time((time_t *) NULL);
2520 #if defined(MAGICKCORE_HAVE_LOCALTIME_R)
2521 (void) localtime_r(&seconds,&local_time);
2523 (void) memcpy(&local_time,localtime(&seconds),sizeof(local_time));
2525 (void) FormatLocaleString(date,MaxTextExtent,"D:%04d%02d%02d%02d%02d%02d",
2526 local_time.tm_year+1900,local_time.tm_mon+1,local_time.tm_mday,
2527 local_time.tm_hour,local_time.tm_min,local_time.tm_sec);
2528 (void) FormatLocaleString(buffer,MaxTextExtent,"/CreationDate (%s)\n",date);
2529 (void) WriteBlobString(image,buffer);
2530 (void) FormatLocaleString(buffer,MaxTextExtent,"/ModDate (%s)\n",date);
2531 (void) WriteBlobString(image,buffer);
2532 (void) FormatLocaleString(buffer,MaxTextExtent,"/Producer (%s)\n",
2533 EscapeParenthesis(GetMagickVersion((size_t *) NULL)));
2534 (void) WriteBlobString(image,buffer);
2535 (void) WriteBlobString(image,">>\n");
2536 (void) WriteBlobString(image,"endobj\n");
2540 offset=TellBlob(image)-xref[0]+10;
2541 (void) WriteBlobString(image,"xref\n");
2542 (void) FormatLocaleString(buffer,MaxTextExtent,"0 %.20g\n",(double)
2544 (void) WriteBlobString(image,buffer);
2545 (void) WriteBlobString(image,"0000000000 65535 f \n");
2546 for (i=0; i < (ssize_t) object; i++)
2548 (void) FormatLocaleString(buffer,MaxTextExtent,"%010lu 00000 n \n",
2549 (unsigned long) xref[i]);
2550 (void) WriteBlobString(image,buffer);
2552 (void) WriteBlobString(image,"trailer\n");
2553 (void) WriteBlobString(image,"<<\n");
2554 (void) FormatLocaleString(buffer,MaxTextExtent,"/Size %.20g\n",(double)
2556 (void) WriteBlobString(image,buffer);
2557 (void) FormatLocaleString(buffer,MaxTextExtent,"/Info %.20g 0 R\n",(double)
2559 (void) WriteBlobString(image,buffer);
2560 (void) FormatLocaleString(buffer,MaxTextExtent,"/Root %.20g 0 R\n",(double)
2562 (void) WriteBlobString(image,buffer);
2563 (void) WriteBlobString(image,">>\n");
2564 (void) WriteBlobString(image,"startxref\n");
2565 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
2566 (void) WriteBlobString(image,buffer);
2567 (void) WriteBlobString(image,"%%EOF\n");
2568 xref=(MagickOffsetType *) RelinquishMagickMemory(xref);
2569 (void) CloseBlob(image);