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 "magick/studio.h"
43 #include "magick/blob.h"
44 #include "magick/blob-private.h"
45 #include "magick/cache.h"
46 #include "magick/color.h"
47 #include "magick/color-private.h"
48 #include "magick/colorspace.h"
49 #include "magick/compress.h"
50 #include "magick/constitute.h"
51 #include "magick/delegate.h"
52 #include "magick/delegate-private.h"
53 #include "magick/draw.h"
54 #include "magick/exception.h"
55 #include "magick/exception-private.h"
56 #include "magick/geometry.h"
57 #include "magick/image.h"
58 #include "magick/image-private.h"
59 #include "magick/list.h"
60 #include "magick/magick.h"
61 #include "magick/memory_.h"
62 #include "magick/monitor.h"
63 #include "magick/monitor-private.h"
64 #include "magick/option.h"
65 #include "magick/profile.h"
66 #include "magick/property.h"
67 #include "magick/quantum-private.h"
68 #include "magick/resource_.h"
69 #include "magick/resize.h"
70 #include "magick/static.h"
71 #include "magick/string_.h"
72 #include "magick/module.h"
73 #include "magick/transform.h"
74 #include "magick/utility.h"
75 #include "magick/module.h"
80 #if defined(MAGICKCORE_TIFF_DELEGATE)
81 #define CCITTParam "-1"
83 #define CCITTParam "0"
89 static MagickBooleanType
90 WritePDFImage(const ImageInfo *,Image *);
93 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
97 % I n v o k e P D F D e l e g a t e %
101 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103 % InvokePDFDelegate() executes the PDF interpreter with the specified command.
105 % The format of the InvokePDFDelegate method is:
107 % MagickBooleanType InvokePDFDelegate(const MagickBooleanType verbose,
108 % const char *command,ExceptionInfo *exception)
110 % A description of each parameter follows:
112 % o verbose: A value other than zero displays the command prior to
115 % o command: the address of a character string containing the command to
118 % o exception: return any errors or warnings in this structure.
121 static MagickBooleanType InvokePDFDelegate(const MagickBooleanType verbose,
122 const char *command,ExceptionInfo *exception)
127 #if defined(MAGICKCORE_GS_DELEGATE) || defined(MAGICKCORE_WINDOWS_SUPPORT)
144 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
145 ghost_info=NTGhostscriptDLLVectors();
150 ghost_info=(&ghost_info_struct);
151 (void) ResetMagickMemory(&ghost_info,0,sizeof(ghost_info));
152 ghost_info_struct.new_instance=(int (*)(gs_main_instance **,void *))
154 ghost_info_struct.init_with_args=(int (*)(gs_main_instance *,int,char **))
155 gsapi_init_with_args;
156 ghost_info_struct.run_string=(int (*)(gs_main_instance *,const char *,int,
157 int *)) gsapi_run_string;
158 ghost_info_struct.delete_instance=(void (*)(gs_main_instance *))
159 gsapi_delete_instance;
160 ghost_info_struct.exit=(int (*)(gs_main_instance *)) gsapi_exit;
162 if (ghost_info == (GhostInfo *) NULL)
164 status=SystemCommand(MagickFalse,verbose,command,exception);
165 return(status == 0 ? MagickTrue : MagickFalse);
167 if (verbose != MagickFalse)
169 (void) fputs("[ghostscript library]",stdout);
170 (void) fputs(strchr(command,' '),stdout);
172 status=(ghost_info->new_instance)(&interpreter,(void *) NULL);
175 status=SystemCommand(MagickFalse,verbose,command,exception);
176 return(status == 0 ? MagickTrue : MagickFalse);
179 argv=StringToArgv(command,&argc);
180 status=(ghost_info->init_with_args)(interpreter,argc-1,argv+1);
182 status=(ghost_info->run_string)(interpreter,"systemdict /start get exec\n",
184 (ghost_info->exit)(interpreter);
185 (ghost_info->delete_instance)(interpreter);
186 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
187 NTGhostscriptUnLoadDLL();
189 for (i=0; i < (ssize_t) argc; i++)
190 argv[i]=DestroyString(argv[i]);
191 argv=(char **) RelinquishMagickMemory(argv);
192 if ((status != 0) && (status != -101))
197 message=GetExceptionMessage(errno);
198 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
199 "`%s': %s",command,message);
200 message=DestroyString(message);
201 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
202 "Ghostscript returns status %d, exit code %d",status,code);
207 status=SystemCommand(MagickFalse,verbose,command,exception);
208 return(status == 0 ? MagickTrue : MagickFalse);
213 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
221 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
223 % IsPDF() returns MagickTrue if the image format type, identified by the
224 % magick string, is PDF.
226 % The format of the IsPDF method is:
228 % MagickBooleanType IsPDF(const unsigned char *magick,const size_t offset)
230 % A description of each parameter follows:
232 % o magick: compare image format pattern against these bytes.
234 % o offset: Specifies the offset of the magick string.
237 static MagickBooleanType IsPDF(const unsigned char *magick,const size_t offset)
241 if (LocaleNCompare((const char *) magick,"%PDF-",5) == 0)
247 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251 % R e a d P D F I m a g e %
255 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
257 % ReadPDFImage() reads a Portable Document Format image file and
258 % returns it. It allocates the memory necessary for the new Image structure
259 % and returns a pointer to the new image.
261 % The format of the ReadPDFImage method is:
263 % Image *ReadPDFImage(const ImageInfo *image_info,ExceptionInfo *exception)
265 % A description of each parameter follows:
267 % o image_info: the image info.
269 % o exception: return any errors or warnings in this structure.
273 static MagickBooleanType IsPDFRendered(const char *path)
281 if ((path == (const char *) NULL) || (*path == '\0'))
283 status=GetPathAttributes(path,&attributes);
284 if ((status != MagickFalse) && S_ISREG(attributes.st_mode) &&
285 (attributes.st_size > 0))
290 static Image *ReadPDFImage(const ImageInfo *image_info,ExceptionInfo *exception)
292 #define AdobeIllustrator "âãÏÓ"
293 #define CropBox "CropBox"
294 #define DeviceCMYK "DeviceCMYK"
295 #define MediaBox "MediaBox"
296 #define RenderPostscriptText "Rendering Postscript... "
297 #define PDFRotate "Rotate"
298 #define SpotColor "Separation"
299 #define TrimBox "TrimBox"
300 #define PDFVersion "PDF-"
303 command[MaxTextExtent],
304 density[MaxTextExtent],
305 filename[MaxTextExtent],
306 geometry[MaxTextExtent],
307 options[MaxTextExtent],
308 input_filename[MaxTextExtent],
309 postscript_filename[MaxTextExtent];
368 assert(image_info != (const ImageInfo *) NULL);
369 assert(image_info->signature == MagickSignature);
370 if (image_info->debug != MagickFalse)
371 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
372 image_info->filename);
373 assert(exception != (ExceptionInfo *) NULL);
374 assert(exception->signature == MagickSignature);
378 image=AcquireImage(image_info);
379 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
380 if (status == MagickFalse)
382 image=DestroyImageList(image);
383 return((Image *) NULL);
385 status=AcquireUniqueSymbolicLink(image_info->filename,input_filename);
386 if (status == MagickFalse)
388 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
389 image_info->filename);
390 image=DestroyImageList(image);
391 return((Image *) NULL);
394 Set the page density.
396 delta.x=DefaultResolution;
397 delta.y=DefaultResolution;
398 if ((image->x_resolution == 0.0) || (image->y_resolution == 0.0))
400 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
401 image->x_resolution=geometry_info.rho;
402 image->y_resolution=geometry_info.sigma;
403 if ((flags & SigmaValue) == 0)
404 image->y_resolution=image->x_resolution;
407 Determine page geometry from the PDF media box.
409 cmyk=image_info->colorspace == CMYKColorspace ? MagickTrue : MagickFalse;
411 option=GetImageOption(image_info,"pdf:use-cropbox");
412 if (option != (const char *) NULL)
413 cropbox=IsMagickTrue(option);
415 option=GetImageOption(image_info,"pdf:use-trimbox");
416 if (option != (const char *) NULL)
417 trimbox=IsMagickTrue(option);
420 (void) ResetMagickMemory(&bounding_box,0,sizeof(bounding_box));
421 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
422 (void) ResetMagickMemory(&hires_bounds,0,sizeof(hires_bounds));
423 (void) ResetMagickMemory(&page,0,sizeof(page));
424 (void) ResetMagickMemory(command,0,sizeof(command));
428 adobe_illustrator=MagickFalse;
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(AdobeIllustrator,command,strlen(AdobeIllustrator)) == 0)
451 adobe_illustrator=MagickTrue;
452 if (LocaleNCompare(SpotColor,command,strlen(SpotColor)) == 0)
456 property[MaxTextExtent],
465 (void) FormatMagickString(property,MaxTextExtent,"pdf:SpotColor-%.20g",
466 (double) spotcolor++);
468 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
470 if ((isspace(c) != 0) || (c == '/') || ((i+1) == MaxTextExtent))
475 value=AcquireString(name);
476 (void) SubstituteString(&value,"#20"," ");
477 (void) SetImageProperty(image,property,value);
478 value=DestroyString(value);
481 if (LocaleNCompare(PDFVersion,command,strlen(PDFVersion)) == 0)
482 (void) SetImageProperty(image,"pdf:Version",command);
484 if (cropbox != MagickFalse)
486 if (LocaleNCompare(CropBox,command,strlen(CropBox)) == 0)
489 Note region defined by crop box.
491 count=(ssize_t) sscanf(command,"CropBox [%lf %lf %lf %lf",
492 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
494 count=(ssize_t) sscanf(command,"CropBox[%lf %lf %lf %lf",
495 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
499 if (trimbox != MagickFalse)
501 if (LocaleNCompare(TrimBox,command,strlen(TrimBox)) == 0)
504 Note region defined by trim box.
506 count=(ssize_t) sscanf(command,"TrimBox [%lf %lf %lf %lf",
507 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
509 count=(ssize_t) sscanf(command,"TrimBox[%lf %lf %lf %lf",
510 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
514 if (LocaleNCompare(MediaBox,command,strlen(MediaBox)) == 0)
517 Note region defined by media box.
519 count=(ssize_t) sscanf(command,"MediaBox [%lf %lf %lf %lf",
520 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
522 count=(ssize_t) sscanf(command,"MediaBox[%lf %lf %lf %lf",
523 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
527 if (((bounds.x2 > hires_bounds.x2) && (bounds.y2 > hires_bounds.y2)) ||
528 ((hires_bounds.x2 == 0.0) && (hires_bounds.y2 == 0.0)))
531 Set PDF render geometry.
533 (void) FormatMagickString(geometry,MaxTextExtent,
534 "%gx%g%+.15g%+.15g",bounds.x2-bounds.x1,bounds.y2-bounds.y1,
535 bounds.x1,bounds.y1);
536 (void) SetImageProperty(image,"pdf:HiResBoundingBox",geometry);
537 page.width=(size_t) floor(bounds.x2-bounds.x1+0.5);
538 page.height=(size_t) floor(bounds.y2-bounds.y1+0.5);
542 (void) CloseBlob(image);
543 if ((fabs(angle) == 90.0) || (fabs(angle) == 270.0))
549 page.width=page.height;
552 if (image_info->colorspace == RGBColorspace)
555 Create Ghostscript control file.
557 file=AcquireUniqueFileResource(postscript_filename);
560 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
561 image_info->filename);
562 image=DestroyImage(image);
563 return((Image *) NULL);
565 count=write(file," ",1);
568 Render Postscript with the Ghostscript delegate.
570 if ((image_info->ping != MagickFalse) ||
571 (image_info->monochrome != MagickFalse))
572 delegate_info=GetDelegateInfo("ps:mono",(char *) NULL,exception);
574 if (cmyk != MagickFalse)
575 delegate_info=GetDelegateInfo("ps:cmyk",(char *) NULL,exception);
577 if (adobe_illustrator != MagickFalse)
578 delegate_info=GetDelegateInfo("ps:alpha",(char *) NULL,exception);
580 delegate_info=GetDelegateInfo("ps:color",(char *) NULL,exception);
581 if (delegate_info == (const DelegateInfo *) NULL)
583 (void) RelinquishUniqueFileResource(postscript_filename);
584 image=DestroyImage(image);
585 return((Image *) NULL);
588 if (image_info->density != (char *) NULL)
590 flags=ParseGeometry(image_info->density,&geometry_info);
591 image->x_resolution=geometry_info.rho;
592 image->y_resolution=geometry_info.sigma;
593 if ((flags & SigmaValue) == 0)
594 image->y_resolution=image->x_resolution;
596 (void) FormatMagickString(density,MaxTextExtent,"%gx%g",
597 image->x_resolution,image->y_resolution);
598 if (image_info->page != (char *) NULL)
600 (void) ParseAbsoluteGeometry(image_info->page,&page);
601 page.width=(size_t) floor(page.width*image->x_resolution/delta.x+
603 page.height=(size_t) floor(page.height*image->y_resolution/delta.y+
605 (void) FormatMagickString(options,MaxTextExtent,"-g%.20gx%.20g ",
606 (double) page.width,(double) page.height);
608 if (cmyk != MagickFalse)
609 (void) ConcatenateMagickString(options,"-dUseCIEColor ",MaxTextExtent);
610 if (cropbox != MagickFalse)
611 (void) ConcatenateMagickString(options,"-dUseCropBox ",MaxTextExtent);
612 if (trimbox != MagickFalse)
613 (void) ConcatenateMagickString(options,"-dUseTrimBox ",MaxTextExtent);
614 read_info=CloneImageInfo(image_info);
615 *read_info->magick='\0';
616 if (read_info->number_scenes != 0)
619 pages[MaxTextExtent];
621 (void) FormatMagickString(pages,MaxTextExtent,"-dFirstPage=%.20g "
622 "-dLastPage=%.20g",(double) read_info->scene+1,(double)
623 (read_info->scene+read_info->number_scenes));
624 (void) ConcatenateMagickString(options,pages,MaxTextExtent);
625 read_info->number_scenes=0;
626 if (read_info->scenes != (char *) NULL)
627 *read_info->scenes='\0';
629 if (read_info->authenticate != (char *) NULL)
630 (void) FormatMagickString(options+strlen(options),MaxTextExtent,
631 " -sPDFPassword=%s",read_info->authenticate);
632 (void) CopyMagickString(filename,read_info->filename,MaxTextExtent);
633 (void) AcquireUniqueFilename(read_info->filename);
634 (void) FormatMagickString(command,MaxTextExtent,
635 GetDelegateCommands(delegate_info),
636 read_info->antialias != MagickFalse ? 4 : 1,
637 read_info->antialias != MagickFalse ? 4 : 1,density,options,
638 read_info->filename,postscript_filename,input_filename);
639 status=InvokePDFDelegate(read_info->verbose,command,exception);
640 pdf_image=(Image *) NULL;
641 if ((status != MagickFalse) &&
642 (IsPDFRendered(read_info->filename) != MagickFalse))
643 pdf_image=ReadImage(read_info,exception);
644 (void) RelinquishUniqueFileResource(postscript_filename);
645 (void) RelinquishUniqueFileResource(read_info->filename);
646 (void) RelinquishUniqueFileResource(input_filename);
647 read_info=DestroyImageInfo(read_info);
648 if (pdf_image == (Image *) NULL)
650 ThrowFileException(exception,DelegateError,"PostscriptDelegateFailed",
651 image_info->filename);
652 return((Image *) NULL);
654 if (LocaleCompare(pdf_image->magick,"BMP") == 0)
659 cmyk_image=ConsolidateCMYKImages(pdf_image,exception);
660 if (cmyk_image != (Image *) NULL)
662 pdf_image=DestroyImageList(pdf_image);
663 pdf_image=cmyk_image;
666 if (image_info->number_scenes != 0)
675 Add place holder images to meet the subimage specification requirement.
677 for (i=0; i < (ssize_t) image_info->scene; i++)
679 clone_image=CloneImage(pdf_image,1,1,MagickTrue,exception);
680 if (clone_image != (Image *) NULL)
681 PrependImageToList(&pdf_image,clone_image);
686 (void) CopyMagickString(pdf_image->filename,filename,MaxTextExtent);
687 pdf_image->page=page;
688 (void) CloneImageProfiles(pdf_image,image);
689 (void) CloneImageProperties(pdf_image,image);
690 next=SyncNextImageInList(pdf_image);
691 if (next != (Image *) NULL)
693 } while (next != (Image *) NULL);
694 image=DestroyImage(image);
696 for (next=GetFirstImageInList(pdf_image); next != (Image *) NULL; )
699 next=GetNextImageInList(next);
701 return(GetFirstImageInList(pdf_image));
705 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
709 % R e g i s t e r P D F I m a g e %
713 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
715 % RegisterPDFImage() adds properties for the PDF image format to
716 % the list of supported formats. The properties include the image format
717 % tag, a method to read and/or write the format, whether the format
718 % supports the saving of more than one frame to the same file or blob,
719 % whether the format supports native in-memory I/O, and a brief
720 % description of the format.
722 % The format of the RegisterPDFImage method is:
724 % size_t RegisterPDFImage(void)
727 ModuleExport size_t RegisterPDFImage(void)
732 entry=SetMagickInfo("AI");
733 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
734 entry->encoder=(EncodeImageHandler *) WritePDFImage;
735 entry->adjoin=MagickFalse;
736 entry->blob_support=MagickFalse;
737 entry->seekable_stream=MagickTrue;
738 entry->thread_support=EncoderThreadSupport;
739 entry->description=ConstantString("Adobe Illustrator CS2");
740 entry->module=ConstantString("PDF");
741 (void) RegisterMagickInfo(entry);
742 entry=SetMagickInfo("EPDF");
743 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
744 entry->encoder=(EncodeImageHandler *) WritePDFImage;
745 entry->adjoin=MagickFalse;
746 entry->blob_support=MagickFalse;
747 entry->seekable_stream=MagickTrue;
748 entry->thread_support=EncoderThreadSupport;
749 entry->description=ConstantString("Encapsulated Portable Document Format");
750 entry->module=ConstantString("PDF");
751 (void) RegisterMagickInfo(entry);
752 entry=SetMagickInfo("PDF");
753 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
754 entry->encoder=(EncodeImageHandler *) WritePDFImage;
755 entry->magick=(IsImageFormatHandler *) IsPDF;
756 entry->blob_support=MagickFalse;
757 entry->seekable_stream=MagickTrue;
758 entry->thread_support=EncoderThreadSupport;
759 entry->description=ConstantString("Portable Document Format");
760 entry->module=ConstantString("PDF");
761 (void) RegisterMagickInfo(entry);
762 entry=SetMagickInfo("PDFA");
763 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
764 entry->encoder=(EncodeImageHandler *) WritePDFImage;
765 entry->magick=(IsImageFormatHandler *) IsPDF;
766 entry->blob_support=MagickFalse;
767 entry->seekable_stream=MagickTrue;
768 entry->thread_support=EncoderThreadSupport;
769 entry->description=ConstantString("Portable Document Archive Format");
770 entry->module=ConstantString("PDF");
771 (void) RegisterMagickInfo(entry);
772 return(MagickImageCoderSignature);
776 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
780 % U n r e g i s t e r P D F I m a g e %
784 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
786 % UnregisterPDFImage() removes format registrations made by the
787 % PDF module from the list of supported formats.
789 % The format of the UnregisterPDFImage method is:
791 % UnregisterPDFImage(void)
794 ModuleExport void UnregisterPDFImage(void)
796 (void) UnregisterMagickInfo("AI");
797 (void) UnregisterMagickInfo("EPDF");
798 (void) UnregisterMagickInfo("PDF");
799 (void) UnregisterMagickInfo("PDFA");
803 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
807 % W r i t e P D F I m a g e %
811 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
813 % WritePDFImage() writes an image in the Portable Document image
816 % The format of the WritePDFImage method is:
818 % MagickBooleanType WritePDFImage(const ImageInfo *image_info,Image *image)
820 % A description of each parameter follows.
822 % o image_info: the image info.
824 % o image: The image.
828 static inline size_t MagickMax(const size_t x,const size_t y)
835 static inline size_t MagickMin(const size_t x,const size_t y)
842 static char *EscapeParenthesis(const char *text)
851 buffer[MaxTextExtent];
858 for (i=0; i < (ssize_t) MagickMin(strlen(text),(MaxTextExtent-escapes-1)); i++)
860 if ((text[i] == '(') || (text[i] == ')'))
871 static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
872 Image *image,Image *inject_image)
890 write_info=CloneImageInfo(image_info);
891 (void) CopyMagickString(write_info->filename,"GROUP4:",MaxTextExtent);
892 (void) CopyMagickString(write_info->magick,"GROUP4",MaxTextExtent);
893 group4_image=CloneImage(inject_image,0,0,MagickTrue,&image->exception);
894 if (group4_image == (Image *) NULL)
896 group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
898 group4_image=DestroyImage(group4_image);
899 if (group4 == (unsigned char *) NULL)
901 write_info=DestroyImageInfo(write_info);
902 if (WriteBlob(image,length,group4) != (ssize_t) length)
904 group4=(unsigned char *) RelinquishMagickMemory(group4);
908 static MagickBooleanType WritePDFImage(const ImageInfo *image_info,Image *image)
910 #define CFormat "/Filter [ /%s ]\n"
911 #define ObjectsPerImage 14
916 "<?xpacket begin=\"%s\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n"
917 "<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"
918 " <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n"
919 " <rdf:Description rdf:about=\"\"\n"
920 " xmlns:xap=\"http://ns.adobe.com/xap/1.0/\">\n"
921 " <xap:ModifyDate>%s</xap:ModifyDate>\n"
922 " <xap:CreateDate>%s</xap:CreateDate>\n"
923 " <xap:MetadataDate>%s</xap:MetadataDate>\n"
924 " <xap:CreatorTool>%s</xap:CreatorTool>\n"
925 " </rdf:Description>\n"
926 " <rdf:Description rdf:about=\"\"\n"
927 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"
928 " <dc:format>application/pdf</dc:format>\n"
929 " </rdf:Description>\n"
930 " <rdf:Description rdf:about=\"\"\n"
931 " xmlns:xapMM=\"http://ns.adobe.com/xap/1.0/mm/\">\n"
932 " <xapMM:DocumentID>uuid:6ec119d7-7982-4f56-808d-dfe64f5b35cf</xapMM:DocumentID>\n"
933 " <xapMM:InstanceID>uuid:a79b99b4-6235-447f-9f6c-ec18ef7555cb</xapMM:InstanceID>\n"
934 " </rdf:Description>\n"
935 " <rdf:Description rdf:about=\"\"\n"
936 " xmlns:pdf=\"http://ns.adobe.com/pdf/1.3/\">\n"
937 " <pdf:Producer>%s</pdf:Producer>\n"
938 " </rdf:Description>\n"
939 " <rdf:Description rdf:about=\"\"\n"
940 " xmlns:pdfaid=\"http://www.aiim.org/pdfa/ns/id/\">\n"
941 " <pdfaid:part>1</pdfaid:part>\n"
942 " <pdfaid:conformance>B</pdfaid:conformance>\n"
943 " </rdf:Description>\n"
946 "<?xpacket end=\"w\"?>\n"
948 XMPProfileMagick[4]= { (char) 0xef, (char) 0xbb, (char) 0xbf, (char) 0x00 };
951 buffer[MaxTextExtent],
954 page_geometry[MaxTextExtent];
1000 register const IndexPacket
1003 register const PixelPacket
1006 register unsigned char
1034 Open output image file.
1036 assert(image_info != (const ImageInfo *) NULL);
1037 assert(image_info->signature == MagickSignature);
1038 assert(image != (Image *) NULL);
1039 assert(image->signature == MagickSignature);
1040 if (image->debug != MagickFalse)
1041 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1042 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1043 if (status == MagickFalse)
1046 Allocate X ref memory.
1048 xref=(MagickOffsetType *) AcquireQuantumMemory(2048UL,sizeof(*xref));
1049 if (xref == (MagickOffsetType *) NULL)
1050 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1051 (void) ResetMagickMemory(xref,0,2048UL*sizeof(*xref));
1057 if (image_info->compression == JPEG2000Compression)
1058 version=(size_t) MagickMax(version,5);
1059 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
1060 if (next->matte != MagickFalse)
1061 version=(size_t) MagickMax(version,4);
1062 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1063 version=(size_t) MagickMax(version,6);
1064 (void) FormatMagickString(buffer,MaxTextExtent,"%%PDF-1.%.20g \n",
1066 (void) WriteBlobString(image,buffer);
1067 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1068 (void) WriteBlobString(image,"%âãÏÓ\n");
1070 Write Catalog object.
1072 xref[object++]=TellBlob(image);
1074 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1076 (void) WriteBlobString(image,buffer);
1077 (void) WriteBlobString(image,"<<\n");
1078 if (LocaleCompare(image_info->magick,"PDFA") != 0)
1079 (void) FormatMagickString(buffer,MaxTextExtent,"/Pages %.20g 0 R\n",
1083 (void) FormatMagickString(buffer,MaxTextExtent,"/Metadata %.20g 0 R\n",
1085 (void) WriteBlobString(image,buffer);
1086 (void) FormatMagickString(buffer,MaxTextExtent,"/Pages %.20g 0 R\n",
1089 (void) WriteBlobString(image,buffer);
1090 (void) WriteBlobString(image,"/Type /Catalog\n");
1091 (void) WriteBlobString(image,">>\n");
1092 (void) WriteBlobString(image,"endobj\n");
1093 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1096 create_date[MaxTextExtent],
1097 modify_date[MaxTextExtent],
1098 timestamp[MaxTextExtent],
1099 xmp_profile[MaxTextExtent];
1107 xref[object++]=TellBlob(image);
1108 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",
1110 (void) WriteBlobString(image,buffer);
1111 (void) WriteBlobString(image,"<<\n");
1112 (void) WriteBlobString(image,"/Subtype /XML\n");
1114 value=GetImageProperty(image,"date:modify");
1115 if (value != (const char *) NULL)
1116 (void) CopyMagickString(modify_date,value,MaxTextExtent);
1118 value=GetImageProperty(image,"date:create");
1119 if (value != (const char *) NULL)
1120 (void) CopyMagickString(create_date,value,MaxTextExtent);
1121 (void) FormatMagickTime(time((time_t *) NULL),MaxTextExtent,timestamp);
1122 i=FormatMagickString(xmp_profile,MaxTextExtent,XMPProfile,
1123 XMPProfileMagick,modify_date,create_date,timestamp,
1124 GetMagickVersion(&version),GetMagickVersion(&version));
1125 (void) FormatMagickString(buffer,MaxTextExtent,"/Length %.20g\n",
1127 (void) WriteBlobString(image,buffer);
1128 (void) WriteBlobString(image,"/Type /Metadata\n");
1129 (void) WriteBlobString(image,">>\nstream\n");
1130 (void) WriteBlobString(image,xmp_profile);
1131 (void) WriteBlobString(image,"endstream\n");
1132 (void) WriteBlobString(image,"endobj\n");
1137 xref[object++]=TellBlob(image);
1139 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1141 (void) WriteBlobString(image,buffer);
1142 (void) WriteBlobString(image,"<<\n");
1143 (void) WriteBlobString(image,"/Type /Pages\n");
1144 (void) FormatMagickString(buffer,MaxTextExtent,"/Kids [ %.20g 0 R ",
1146 (void) WriteBlobString(image,buffer);
1147 count=(ssize_t) (pages_id+ObjectsPerImage+1);
1148 if (image_info->adjoin != MagickFalse)
1154 Predict page object id's.
1157 for ( ; GetNextImageInList(kid_image) != (Image *) NULL; count+=ObjectsPerImage)
1159 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 R ",(double)
1161 (void) WriteBlobString(image,buffer);
1162 kid_image=GetNextImageInList(kid_image);
1164 xref=(MagickOffsetType *) ResizeQuantumMemory(xref,(size_t) count+2048UL,
1166 if (xref == (MagickOffsetType *) NULL)
1167 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1169 (void) WriteBlobString(image,"]\n");
1170 (void) FormatMagickString(buffer,MaxTextExtent,"/Count %.20g\n",
1171 (double) ((count-pages_id)/ObjectsPerImage));
1172 (void) WriteBlobString(image,buffer);
1173 (void) WriteBlobString(image,">>\n");
1174 (void) WriteBlobString(image,"endobj\n");
1178 compression=image->compression;
1179 if (image_info->compression != UndefinedCompression)
1180 compression=image_info->compression;
1181 switch (compression)
1183 case FaxCompression:
1184 case Group4Compression:
1186 if ((IsMonochromeImage(image,&image->exception) == MagickFalse) ||
1187 (image->matte != MagickFalse))
1188 compression=RLECompression;
1191 #if !defined(MAGICKCORE_JPEG_DELEGATE)
1192 case JPEGCompression:
1194 compression=RLECompression;
1195 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1196 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
1201 #if !defined(MAGICKCORE_JP2_DELEGATE)
1202 case JPEG2000Compression:
1204 compression=RLECompression;
1205 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1206 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JP2)",
1211 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
1212 case ZipCompression:
1214 compression=RLECompression;
1215 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1216 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZLIB)",
1221 case LZWCompression:
1223 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1224 compression=RLECompression; /* LZW compression is forbidden */
1229 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1230 compression=RLECompression; /* ASCII 85 compression is forbidden */
1236 if (compression == JPEG2000Compression)
1238 if (image->colorspace != RGBColorspace)
1239 (void) TransformImageColorspace(image,RGBColorspace);
1242 Scale relative to dots-per-inch.
1244 delta.x=DefaultResolution;
1245 delta.y=DefaultResolution;
1246 resolution.x=image->x_resolution;
1247 resolution.y=image->y_resolution;
1248 if ((resolution.x == 0.0) || (resolution.y == 0.0))
1250 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
1251 resolution.x=geometry_info.rho;
1252 resolution.y=geometry_info.sigma;
1253 if ((flags & SigmaValue) == 0)
1254 resolution.y=resolution.x;
1256 if (image_info->density != (char *) NULL)
1258 flags=ParseGeometry(image_info->density,&geometry_info);
1259 resolution.x=geometry_info.rho;
1260 resolution.y=geometry_info.sigma;
1261 if ((flags & SigmaValue) == 0)
1262 resolution.y=resolution.x;
1264 if (image->units == PixelsPerCentimeterResolution)
1266 resolution.x=(size_t) (100.0*2.54*resolution.x+0.5)/100.0;
1267 resolution.y=(size_t) (100.0*2.54*resolution.y+0.5)/100.0;
1269 SetGeometry(image,&geometry);
1270 (void) FormatMagickString(page_geometry,MaxTextExtent,"%.20gx%.20g",
1271 (double) image->columns,(double) image->rows);
1272 if (image_info->page != (char *) NULL)
1273 (void) CopyMagickString(page_geometry,image_info->page,MaxTextExtent);
1275 if ((image->page.width != 0) && (image->page.height != 0))
1276 (void) FormatMagickString(page_geometry,MaxTextExtent,
1277 "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,
1278 (double) image->page.height,(double) image->page.x,(double)
1281 if ((image->gravity != UndefinedGravity) &&
1282 (LocaleCompare(image_info->magick,"PDF") == 0))
1283 (void) CopyMagickString(page_geometry,PSPageGeometry,MaxTextExtent);
1284 (void) ConcatenateMagickString(page_geometry,">",MaxTextExtent);
1285 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
1286 &geometry.width,&geometry.height);
1287 scale.x=(double) (geometry.width*delta.x)/resolution.x;
1288 geometry.width=(size_t) floor(scale.x+0.5);
1289 scale.y=(double) (geometry.height*delta.y)/resolution.y;
1290 geometry.height=(size_t) floor(scale.y+0.5);
1291 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
1292 (void) ParseGravityGeometry(image,page_geometry,&page_info,
1294 if (image->gravity != UndefinedGravity)
1296 geometry.x=(-page_info.x);
1297 geometry.y=(ssize_t) (media_info.height+page_info.y-image->rows);
1300 if (image_info->pointsize != 0.0)
1301 pointsize=image_info->pointsize;
1303 value=GetImageProperty(image,"Label");
1304 if (value != (const char *) NULL)
1305 text_size=(size_t) (MultilineCensus(value)*pointsize+12);
1309 xref[object++]=TellBlob(image);
1310 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1312 (void) WriteBlobString(image,buffer);
1313 (void) WriteBlobString(image,"<<\n");
1314 (void) WriteBlobString(image,"/Type /Page\n");
1315 (void) FormatMagickString(buffer,MaxTextExtent,"/Parent %.20g 0 R\n",
1317 (void) WriteBlobString(image,buffer);
1318 (void) WriteBlobString(image,"/Resources <<\n");
1319 labels=(char **) NULL;
1320 value=GetImageProperty(image,"Label");
1321 if (value != (const char *) NULL)
1322 labels=StringToList(value);
1323 if (labels != (char **) NULL)
1325 (void) FormatMagickString(buffer,MaxTextExtent,
1326 "/Font << /F%.20g %.20g 0 R >>\n",(double) image->scene,(double)
1328 (void) WriteBlobString(image,buffer);
1330 (void) FormatMagickString(buffer,MaxTextExtent,
1331 "/XObject << /Im%.20g %.20g 0 R >>\n",(double) image->scene,(double)
1333 (void) WriteBlobString(image,buffer);
1334 (void) FormatMagickString(buffer,MaxTextExtent,"/ProcSet %.20g 0 R >>\n",
1336 (void) WriteBlobString(image,buffer);
1337 (void) FormatMagickString(buffer,MaxTextExtent,
1338 "/MediaBox [0 0 %g %g]\n",72.0*media_info.width/resolution.x,
1339 72.0*media_info.height/resolution.y);
1340 (void) WriteBlobString(image,buffer);
1341 (void) FormatMagickString(buffer,MaxTextExtent,
1342 "/CropBox [0 0 %g %g]\n",72.0*media_info.width/resolution.x,
1343 72.0*media_info.height/resolution.y);
1344 (void) WriteBlobString(image,buffer);
1345 (void) FormatMagickString(buffer,MaxTextExtent,"/Contents %.20g 0 R\n",
1347 (void) WriteBlobString(image,buffer);
1348 (void) FormatMagickString(buffer,MaxTextExtent,"/Thumb %.20g 0 R\n",
1350 (void) WriteBlobString(image,buffer);
1351 (void) WriteBlobString(image,">>\n");
1352 (void) WriteBlobString(image,"endobj\n");
1354 Write Contents object.
1356 xref[object++]=TellBlob(image);
1357 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1359 (void) WriteBlobString(image,buffer);
1360 (void) WriteBlobString(image,"<<\n");
1361 (void) FormatMagickString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
1363 (void) WriteBlobString(image,buffer);
1364 (void) WriteBlobString(image,">>\n");
1365 (void) WriteBlobString(image,"stream\n");
1366 offset=TellBlob(image);
1367 (void) WriteBlobString(image,"q\n");
1368 if (labels != (char **) NULL)
1369 for (i=0; labels[i] != (char *) NULL; i++)
1371 (void) WriteBlobString(image,"BT\n");
1372 (void) FormatMagickString(buffer,MaxTextExtent,"/F%.20g %g Tf\n",
1373 (double) image->scene,pointsize);
1374 (void) WriteBlobString(image,buffer);
1375 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g %.20g Td\n",
1376 (double) geometry.x,(double) (geometry.y+geometry.height+i*pointsize+
1378 (void) WriteBlobString(image,buffer);
1379 (void) FormatMagickString(buffer,MaxTextExtent,"(%s) Tj\n",labels[i]);
1380 (void) WriteBlobString(image,buffer);
1381 (void) WriteBlobString(image,"ET\n");
1382 labels[i]=DestroyString(labels[i]);
1384 (void) FormatMagickString(buffer,MaxTextExtent,"%g 0 0 %g %.20g %.20g cm\n",
1385 scale.x,scale.y,(double) geometry.x,(double) geometry.y);
1386 (void) WriteBlobString(image,buffer);
1387 (void) FormatMagickString(buffer,MaxTextExtent,"/Im%.20g Do\n",
1388 (double) image->scene);
1389 (void) WriteBlobString(image,buffer);
1390 (void) WriteBlobString(image,"Q\n");
1391 offset=TellBlob(image)-offset;
1392 (void) WriteBlobString(image,"endstream\n");
1393 (void) WriteBlobString(image,"endobj\n");
1395 Write Length object.
1397 xref[object++]=TellBlob(image);
1398 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1400 (void) WriteBlobString(image,buffer);
1401 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
1402 (void) WriteBlobString(image,buffer);
1403 (void) WriteBlobString(image,"endobj\n");
1405 Write Procset object.
1407 xref[object++]=TellBlob(image);
1408 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",
1410 (void) WriteBlobString(image,buffer);
1411 if ((image->storage_class == DirectClass) || (image->colors > 256))
1412 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageC",MaxTextExtent);
1414 if ((compression == FaxCompression) || (compression == Group4Compression))
1415 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageB",MaxTextExtent);
1417 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageI",MaxTextExtent);
1418 (void) WriteBlobString(image,buffer);
1419 (void) WriteBlobString(image," ]\n");
1420 (void) WriteBlobString(image,"endobj\n");
1424 xref[object++]=TellBlob(image);
1425 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",
1427 (void) WriteBlobString(image,buffer);
1428 (void) WriteBlobString(image,"<<\n");
1429 if (labels != (char **) NULL)
1431 (void) WriteBlobString(image,"/Type /Font\n");
1432 (void) WriteBlobString(image,"/Subtype /Type1\n");
1433 (void) FormatMagickString(buffer,MaxTextExtent,"/Name /F%.20g\n",
1434 (double) image->scene);
1435 (void) WriteBlobString(image,buffer);
1436 (void) WriteBlobString(image,"/BaseFont /Helvetica\n");
1437 (void) WriteBlobString(image,"/Encoding /MacRomanEncoding\n");
1438 labels=(char **) RelinquishMagickMemory(labels);
1440 (void) WriteBlobString(image,">>\n");
1441 (void) WriteBlobString(image,"endobj\n");
1443 Write XObject object.
1445 xref[object++]=TellBlob(image);
1446 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1448 (void) WriteBlobString(image,buffer);
1449 (void) WriteBlobString(image,"<<\n");
1450 (void) WriteBlobString(image,"/Type /XObject\n");
1451 (void) WriteBlobString(image,"/Subtype /Image\n");
1452 (void) FormatMagickString(buffer,MaxTextExtent,"/Name /Im%.20g\n",
1453 (double) image->scene);
1454 (void) WriteBlobString(image,buffer);
1455 switch (compression)
1459 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"ASCII85Decode");
1462 case JPEGCompression:
1464 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"DCTDecode");
1465 if (image->colorspace != CMYKColorspace)
1467 (void) WriteBlobString(image,buffer);
1468 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1472 case JPEG2000Compression:
1474 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"JPXDecode");
1475 if (image->colorspace != CMYKColorspace)
1477 (void) WriteBlobString(image,buffer);
1478 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1482 case LZWCompression:
1484 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"LZWDecode");
1487 case ZipCompression:
1489 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"FlateDecode");
1492 case FaxCompression:
1493 case Group4Compression:
1495 (void) CopyMagickString(buffer,"/Filter [ /CCITTFaxDecode ]\n",
1497 (void) WriteBlobString(image,buffer);
1498 (void) FormatMagickString(buffer,MaxTextExtent,"/DecodeParms [ << "
1499 "/K %s /BlackIs1 false /Columns %.20g /Rows %.20g >> ]\n",CCITTParam,
1500 (double) image->columns,(double) image->rows);
1505 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,
1510 (void) WriteBlobString(image,buffer);
1511 (void) FormatMagickString(buffer,MaxTextExtent,"/Width %.20g\n",(double)
1513 (void) WriteBlobString(image,buffer);
1514 (void) FormatMagickString(buffer,MaxTextExtent,"/Height %.20g\n",(double)
1516 (void) WriteBlobString(image,buffer);
1517 (void) FormatMagickString(buffer,MaxTextExtent,"/ColorSpace %.20g 0 R\n",
1519 (void) WriteBlobString(image,buffer);
1520 (void) FormatMagickString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
1521 (compression == FaxCompression) || (compression == Group4Compression) ?
1523 (void) WriteBlobString(image,buffer);
1524 if (image->matte != MagickFalse)
1526 (void) FormatMagickString(buffer,MaxTextExtent,"/SMask %.20g 0 R\n",
1528 (void) WriteBlobString(image,buffer);
1530 (void) FormatMagickString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
1532 (void) WriteBlobString(image,buffer);
1533 (void) WriteBlobString(image,">>\n");
1534 (void) WriteBlobString(image,"stream\n");
1535 offset=TellBlob(image);
1536 number_pixels=(MagickSizeType) image->columns*image->rows;
1537 if ((4*number_pixels) != (MagickSizeType) ((size_t) (4*number_pixels)))
1538 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1539 if ((compression == FaxCompression) || (compression == Group4Compression) ||
1540 ((image_info->type != TrueColorType) &&
1541 (IsGrayImage(image,&image->exception) != MagickFalse)))
1543 switch (compression)
1545 case FaxCompression:
1546 case Group4Compression:
1548 if (LocaleCompare(CCITTParam,"0") == 0)
1550 (void) HuffmanEncodeImage(image_info,image,image);
1553 (void) Huffman2DEncodeImage(image_info,image,image);
1556 case JPEGCompression:
1558 status=InjectImageBlob(image_info,image,image,"jpeg",
1560 if (status == MagickFalse)
1561 ThrowWriterException(CoderError,image->exception.reason);
1564 case JPEG2000Compression:
1566 status=InjectImageBlob(image_info,image,image,"jp2",
1568 if (status == MagickFalse)
1569 ThrowWriterException(CoderError,image->exception.reason);
1572 case RLECompression:
1576 Allocate pixel array.
1578 length=(size_t) number_pixels;
1579 pixels=(unsigned char *) AcquireQuantumMemory(length,
1581 if (pixels == (unsigned char *) NULL)
1582 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1584 Dump Runlength encoded pixels.
1587 for (y=0; y < (ssize_t) image->rows; y++)
1589 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1590 if (p == (const PixelPacket *) NULL)
1592 for (x=0; x < (ssize_t) image->columns; x++)
1594 *q++=ScaleQuantumToChar(PixelIntensityToQuantum(p));
1597 if (image->previous == (Image *) NULL)
1599 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1601 if (status == MagickFalse)
1605 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1606 if (compression == ZipCompression)
1607 status=ZLIBEncodeImage(image,length,pixels);
1610 if (compression == LZWCompression)
1611 status=LZWEncodeImage(image,length,pixels);
1613 status=PackbitsEncodeImage(image,length,pixels);
1614 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1615 if (status == MagickFalse)
1617 (void) CloseBlob(image);
1618 return(MagickFalse);
1625 Dump uncompressed PseudoColor packets.
1627 Ascii85Initialize(image);
1628 for (y=0; y < (ssize_t) image->rows; y++)
1630 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1631 if (p == (const PixelPacket *) NULL)
1633 for (x=0; x < (ssize_t) image->columns; x++)
1635 Ascii85Encode(image,
1636 ScaleQuantumToChar(PixelIntensityToQuantum(p)));
1639 if (image->previous == (Image *) NULL)
1641 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1643 if (status == MagickFalse)
1647 Ascii85Flush(image);
1653 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1654 (compression == JPEGCompression) ||
1655 (compression == JPEG2000Compression))
1656 switch (compression)
1658 case JPEGCompression:
1660 status=InjectImageBlob(image_info,image,image,"jpeg",
1662 if (status == MagickFalse)
1663 ThrowWriterException(CoderError,image->exception.reason);
1666 case JPEG2000Compression:
1668 status=InjectImageBlob(image_info,image,image,"jp2",
1670 if (status == MagickFalse)
1671 ThrowWriterException(CoderError,image->exception.reason);
1674 case RLECompression:
1678 Allocate pixel array.
1680 length=(size_t) number_pixels;
1681 pixels=(unsigned char *) AcquireQuantumMemory(length,
1683 length*=image->colorspace == CMYKColorspace ? 4UL : 3UL;
1684 if (pixels == (unsigned char *) NULL)
1685 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1687 Dump runoffset encoded pixels.
1690 for (y=0; y < (ssize_t) image->rows; y++)
1692 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1693 if (p == (const PixelPacket *) NULL)
1695 indexes=GetVirtualIndexQueue(image);
1696 for (x=0; x < (ssize_t) image->columns; x++)
1698 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
1699 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1700 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
1701 if (image->colorspace == CMYKColorspace)
1702 *q++=ScaleQuantumToChar(indexes[x]);
1705 if (image->previous == (Image *) NULL)
1707 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1709 if (status == MagickFalse)
1713 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1714 if (compression == ZipCompression)
1715 status=ZLIBEncodeImage(image,length,pixels);
1718 if (compression == LZWCompression)
1719 status=LZWEncodeImage(image,length,pixels);
1721 status=PackbitsEncodeImage(image,length,pixels);
1722 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1723 if (status == MagickFalse)
1725 (void) CloseBlob(image);
1726 return(MagickFalse);
1733 Dump uncompressed DirectColor packets.
1735 Ascii85Initialize(image);
1736 for (y=0; y < (ssize_t) image->rows; y++)
1738 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1739 if (p == (const PixelPacket *) NULL)
1741 indexes=GetVirtualIndexQueue(image);
1742 for (x=0; x < (ssize_t) image->columns; x++)
1744 Ascii85Encode(image,ScaleQuantumToChar(
1745 GetRedPixelComponent(p)));
1746 Ascii85Encode(image,ScaleQuantumToChar(
1747 GetGreenPixelComponent(p)));
1748 Ascii85Encode(image,ScaleQuantumToChar(
1749 GetBluePixelComponent(p)));
1750 if (image->colorspace == CMYKColorspace)
1751 Ascii85Encode(image,ScaleQuantumToChar(indexes[x]));
1754 if (image->previous == (Image *) NULL)
1756 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1758 if (status == MagickFalse)
1762 Ascii85Flush(image);
1769 Dump number of colors and colormap.
1771 switch (compression)
1773 case RLECompression:
1777 Allocate pixel array.
1779 length=(size_t) number_pixels;
1780 pixels=(unsigned char *) AcquireQuantumMemory(length,
1782 if (pixels == (unsigned char *) NULL)
1783 ThrowWriterException(ResourceLimitError,
1784 "MemoryAllocationFailed");
1786 Dump Runlength encoded pixels.
1789 for (y=0; y < (ssize_t) image->rows; y++)
1791 p=GetVirtualPixels(image,0,y,image->columns,1,
1793 if (p == (const PixelPacket *) NULL)
1795 indexes=GetVirtualIndexQueue(image);
1796 for (x=0; x < (ssize_t) image->columns; x++)
1797 *q++=(unsigned char) indexes[x];
1798 if (image->previous == (Image *) NULL)
1800 status=SetImageProgress(image,SaveImageTag,
1801 (MagickOffsetType) y,image->rows);
1802 if (status == MagickFalse)
1806 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1807 if (compression == ZipCompression)
1808 status=ZLIBEncodeImage(image,length,pixels);
1811 if (compression == LZWCompression)
1812 status=LZWEncodeImage(image,length,pixels);
1814 status=PackbitsEncodeImage(image,length,pixels);
1815 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1816 if (status == MagickFalse)
1818 (void) CloseBlob(image);
1819 return(MagickFalse);
1826 Dump uncompressed PseudoColor packets.
1828 Ascii85Initialize(image);
1829 for (y=0; y < (ssize_t) image->rows; y++)
1831 p=GetVirtualPixels(image,0,y,image->columns,1,
1833 if (p == (const PixelPacket *) NULL)
1835 indexes=GetVirtualIndexQueue(image);
1836 for (x=0; x < (ssize_t) image->columns; x++)
1837 Ascii85Encode(image,(unsigned char) indexes[x]);
1838 if (image->previous == (Image *) NULL)
1840 status=SetImageProgress(image,SaveImageTag,
1841 (MagickOffsetType) y,image->rows);
1842 if (status == MagickFalse)
1846 Ascii85Flush(image);
1851 offset=TellBlob(image)-offset;
1852 (void) WriteBlobString(image,"\nendstream\n");
1853 (void) WriteBlobString(image,"endobj\n");
1855 Write Length object.
1857 xref[object++]=TellBlob(image);
1858 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1860 (void) WriteBlobString(image,buffer);
1861 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g\n",
1863 (void) WriteBlobString(image,buffer);
1864 (void) WriteBlobString(image,"endobj\n");
1866 Write Colorspace object.
1868 xref[object++]=TellBlob(image);
1869 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1871 (void) WriteBlobString(image,buffer);
1872 if (image->colorspace == CMYKColorspace)
1873 (void) CopyMagickString(buffer,"/DeviceCMYK\n",MaxTextExtent);
1875 if ((compression == FaxCompression) ||
1876 (compression == Group4Compression) ||
1877 ((image_info->type != TrueColorType) &&
1878 (IsGrayImage(image,&image->exception) != MagickFalse)))
1879 (void) CopyMagickString(buffer,"/DeviceGray\n",MaxTextExtent);
1881 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1882 (compression == JPEGCompression) ||
1883 (compression == JPEG2000Compression))
1884 (void) CopyMagickString(buffer,"/DeviceRGB\n",MaxTextExtent);
1886 (void) FormatMagickString(buffer,MaxTextExtent,
1887 "[ /Indexed /DeviceRGB %.20g %.20g 0 R ]\n",(double) image->colors-
1888 1,(double) object+3);
1889 (void) WriteBlobString(image,buffer);
1890 (void) WriteBlobString(image,"endobj\n");
1894 SetGeometry(image,&geometry);
1895 (void) ParseMetaGeometry("106x106+0+0>",&geometry.x,&geometry.y,
1896 &geometry.width,&geometry.height);
1897 tile_image=ThumbnailImage(image,geometry.width,geometry.height,
1899 if (tile_image == (Image *) NULL)
1900 ThrowWriterException(ResourceLimitError,image->exception.reason);
1901 xref[object++]=TellBlob(image);
1902 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1904 (void) WriteBlobString(image,buffer);
1905 (void) WriteBlobString(image,"<<\n");
1906 switch (compression)
1910 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"ASCII85Decode");
1913 case JPEGCompression:
1915 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"DCTDecode");
1916 if (image->colorspace != CMYKColorspace)
1918 (void) WriteBlobString(image,buffer);
1919 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1923 case JPEG2000Compression:
1925 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"JPXDecode");
1926 if (image->colorspace != CMYKColorspace)
1928 (void) WriteBlobString(image,buffer);
1929 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1933 case LZWCompression:
1935 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"LZWDecode");
1938 case ZipCompression:
1940 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"FlateDecode");
1943 case FaxCompression:
1944 case Group4Compression:
1946 (void) CopyMagickString(buffer,"/Filter [ /CCITTFaxDecode ]\n",
1948 (void) WriteBlobString(image,buffer);
1949 (void) FormatMagickString(buffer,MaxTextExtent,"/DecodeParms [ << "
1950 "/K %s /BlackIs1 false /Columns %.20g /Rows %.20g >> ]\n",CCITTParam,
1951 (double) tile_image->columns,(double) tile_image->rows);
1956 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,
1961 (void) WriteBlobString(image,buffer);
1962 (void) FormatMagickString(buffer,MaxTextExtent,"/Width %.20g\n",(double)
1963 tile_image->columns);
1964 (void) WriteBlobString(image,buffer);
1965 (void) FormatMagickString(buffer,MaxTextExtent,"/Height %.20g\n",(double)
1967 (void) WriteBlobString(image,buffer);
1968 (void) FormatMagickString(buffer,MaxTextExtent,"/ColorSpace %.20g 0 R\n",
1970 (void) WriteBlobString(image,buffer);
1971 (void) FormatMagickString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
1972 (compression == FaxCompression) || (compression == Group4Compression) ?
1974 (void) WriteBlobString(image,buffer);
1975 (void) FormatMagickString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
1977 (void) WriteBlobString(image,buffer);
1978 (void) WriteBlobString(image,">>\n");
1979 (void) WriteBlobString(image,"stream\n");
1980 offset=TellBlob(image);
1981 number_pixels=(MagickSizeType) tile_image->columns*tile_image->rows;
1982 if ((compression == FaxCompression) ||
1983 (compression == Group4Compression) ||
1984 ((image_info->type != TrueColorType) &&
1985 (IsGrayImage(tile_image,&image->exception) != MagickFalse)))
1987 switch (compression)
1989 case FaxCompression:
1990 case Group4Compression:
1992 if (LocaleCompare(CCITTParam,"0") == 0)
1994 (void) HuffmanEncodeImage(image_info,image,tile_image);
1997 (void) Huffman2DEncodeImage(image_info,image,tile_image);
2000 case JPEGCompression:
2002 status=InjectImageBlob(image_info,image,tile_image,"jpeg",
2004 if (status == MagickFalse)
2005 ThrowWriterException(CoderError,tile_image->exception.reason);
2008 case JPEG2000Compression:
2010 status=InjectImageBlob(image_info,image,tile_image,"jp2",
2012 if (status == MagickFalse)
2013 ThrowWriterException(CoderError,tile_image->exception.reason);
2016 case RLECompression:
2020 Allocate pixel array.
2022 length=(size_t) number_pixels;
2023 pixels=(unsigned char *) AcquireQuantumMemory(length,
2025 if (pixels == (unsigned char *) NULL)
2027 tile_image=DestroyImage(tile_image);
2028 ThrowWriterException(ResourceLimitError,
2029 "MemoryAllocationFailed");
2032 Dump Runlength encoded pixels.
2035 for (y=0; y < (ssize_t) tile_image->rows; y++)
2037 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2038 &tile_image->exception);
2039 if (p == (const PixelPacket *) NULL)
2041 for (x=0; x < (ssize_t) tile_image->columns; x++)
2043 *q++=ScaleQuantumToChar(PixelIntensityToQuantum(p));
2047 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2048 if (compression == ZipCompression)
2049 status=ZLIBEncodeImage(image,length,pixels);
2052 if (compression == LZWCompression)
2053 status=LZWEncodeImage(image,length,pixels);
2055 status=PackbitsEncodeImage(image,length,pixels);
2056 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2057 if (status == MagickFalse)
2059 (void) CloseBlob(image);
2060 return(MagickFalse);
2067 Dump uncompressed PseudoColor packets.
2069 Ascii85Initialize(image);
2070 for (y=0; y < (ssize_t) tile_image->rows; y++)
2072 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2073 &tile_image->exception);
2074 if (p == (const PixelPacket *) NULL)
2076 for (x=0; x < (ssize_t) tile_image->columns; x++)
2078 Ascii85Encode(image,
2079 ScaleQuantumToChar(PixelIntensityToQuantum(p)));
2083 Ascii85Flush(image);
2089 if ((tile_image->storage_class == DirectClass) ||
2090 (tile_image->colors > 256) || (compression == JPEGCompression) ||
2091 (compression == JPEG2000Compression))
2092 switch (compression)
2094 case JPEGCompression:
2096 status=InjectImageBlob(image_info,image,tile_image,"jpeg",
2098 if (status == MagickFalse)
2099 ThrowWriterException(CoderError,tile_image->exception.reason);
2102 case JPEG2000Compression:
2104 status=InjectImageBlob(image_info,image,tile_image,"jp2",
2106 if (status == MagickFalse)
2107 ThrowWriterException(CoderError,tile_image->exception.reason);
2110 case RLECompression:
2114 Allocate pixel array.
2116 length=(size_t) number_pixels;
2117 pixels=(unsigned char *) AcquireQuantumMemory(length,4*
2119 length*=tile_image->colorspace == CMYKColorspace ? 4UL : 3UL;
2120 if (pixels == (unsigned char *) NULL)
2122 tile_image=DestroyImage(tile_image);
2123 ThrowWriterException(ResourceLimitError,
2124 "MemoryAllocationFailed");
2127 Dump runoffset encoded pixels.
2130 for (y=0; y < (ssize_t) tile_image->rows; y++)
2132 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2133 &tile_image->exception);
2134 if (p == (const PixelPacket *) NULL)
2136 indexes=GetVirtualIndexQueue(tile_image);
2137 for (x=0; x < (ssize_t) tile_image->columns; x++)
2139 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
2140 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
2141 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
2142 if (image->colorspace == CMYKColorspace)
2143 *q++=ScaleQuantumToChar(indexes[x]);
2147 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2148 if (compression == ZipCompression)
2149 status=ZLIBEncodeImage(image,length,pixels);
2152 if (compression == LZWCompression)
2153 status=LZWEncodeImage(image,length,pixels);
2155 status=PackbitsEncodeImage(image,length,pixels);
2156 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2157 if (status == MagickFalse)
2159 (void) CloseBlob(image);
2160 return(MagickFalse);
2167 Dump uncompressed DirectColor packets.
2169 Ascii85Initialize(image);
2170 for (y=0; y < (ssize_t) tile_image->rows; y++)
2172 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2173 &tile_image->exception);
2174 if (p == (const PixelPacket *) NULL)
2176 indexes=GetVirtualIndexQueue(tile_image);
2177 for (x=0; x < (ssize_t) tile_image->columns; x++)
2179 Ascii85Encode(image,ScaleQuantumToChar(
2180 GetRedPixelComponent(p)));
2181 Ascii85Encode(image,ScaleQuantumToChar(
2182 GetGreenPixelComponent(p)));
2183 Ascii85Encode(image,ScaleQuantumToChar(
2184 GetBluePixelComponent(p)));
2185 if (image->colorspace == CMYKColorspace)
2186 Ascii85Encode(image,ScaleQuantumToChar(indexes[x]));
2190 Ascii85Flush(image);
2197 Dump number of colors and colormap.
2199 switch (compression)
2201 case RLECompression:
2205 Allocate pixel array.
2207 length=(size_t) number_pixels;
2208 pixels=(unsigned char *) AcquireQuantumMemory(length,
2210 if (pixels == (unsigned char *) NULL)
2212 tile_image=DestroyImage(tile_image);
2213 ThrowWriterException(ResourceLimitError,
2214 "MemoryAllocationFailed");
2217 Dump Runlength encoded pixels.
2220 for (y=0; y < (ssize_t) tile_image->rows; y++)
2222 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2223 &tile_image->exception);
2224 if (p == (const PixelPacket *) NULL)
2226 indexes=GetVirtualIndexQueue(tile_image);
2227 for (x=0; x < (ssize_t) tile_image->columns; x++)
2228 *q++=(unsigned char) indexes[x];
2230 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2231 if (compression == ZipCompression)
2232 status=ZLIBEncodeImage(image,length,pixels);
2235 if (compression == LZWCompression)
2236 status=LZWEncodeImage(image,length,pixels);
2238 status=PackbitsEncodeImage(image,length,pixels);
2239 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2240 if (status == MagickFalse)
2242 (void) CloseBlob(image);
2243 return(MagickFalse);
2250 Dump uncompressed PseudoColor packets.
2252 Ascii85Initialize(image);
2253 for (y=0; y < (ssize_t) tile_image->rows; y++)
2255 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2256 &tile_image->exception);
2257 if (p == (const PixelPacket *) NULL)
2259 indexes=GetVirtualIndexQueue(tile_image);
2260 for (x=0; x < (ssize_t) tile_image->columns; x++)
2261 Ascii85Encode(image,(unsigned char) indexes[x]);
2263 Ascii85Flush(image);
2268 tile_image=DestroyImage(tile_image);
2269 offset=TellBlob(image)-offset;
2270 (void) WriteBlobString(image,"\nendstream\n");
2271 (void) WriteBlobString(image,"endobj\n");
2273 Write Length object.
2275 xref[object++]=TellBlob(image);
2276 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2278 (void) WriteBlobString(image,buffer);
2279 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
2280 (void) WriteBlobString(image,buffer);
2281 (void) WriteBlobString(image,"endobj\n");
2282 xref[object++]=TellBlob(image);
2283 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2285 (void) WriteBlobString(image,buffer);
2286 if ((image->storage_class != DirectClass) && (image->colors <= 256) &&
2287 (compression != FaxCompression) && (compression != Group4Compression))
2290 Write Colormap object.
2292 (void) WriteBlobString(image,"<<\n");
2293 if (compression == NoCompression)
2294 (void) WriteBlobString(image,"/Filter [ /ASCII85Decode ]\n");
2295 (void) FormatMagickString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
2297 (void) WriteBlobString(image,buffer);
2298 (void) WriteBlobString(image,">>\n");
2299 (void) WriteBlobString(image,"stream\n");
2300 offset=TellBlob(image);
2301 if (compression == NoCompression)
2302 Ascii85Initialize(image);
2303 for (i=0; i < (ssize_t) image->colors; i++)
2305 if (compression == NoCompression)
2307 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].red));
2308 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].green));
2309 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].blue));
2312 (void) WriteBlobByte(image,
2313 ScaleQuantumToChar(image->colormap[i].red));
2314 (void) WriteBlobByte(image,
2315 ScaleQuantumToChar(image->colormap[i].green));
2316 (void) WriteBlobByte(image,
2317 ScaleQuantumToChar(image->colormap[i].blue));
2319 if (compression == NoCompression)
2320 Ascii85Flush(image);
2321 offset=TellBlob(image)-offset;
2322 (void) WriteBlobString(image,"\nendstream\n");
2324 (void) WriteBlobString(image,"endobj\n");
2326 Write Length object.
2328 xref[object++]=TellBlob(image);
2329 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2331 (void) WriteBlobString(image,buffer);
2332 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g\n",(double)
2334 (void) WriteBlobString(image,buffer);
2335 (void) WriteBlobString(image,"endobj\n");
2337 Write softmask object.
2339 xref[object++]=TellBlob(image);
2340 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2342 (void) WriteBlobString(image,buffer);
2343 (void) WriteBlobString(image,"<<\n");
2344 if (image->matte == MagickFalse)
2345 (void) WriteBlobString(image,">>\n");
2348 (void) WriteBlobString(image,"/Type /XObject\n");
2349 (void) WriteBlobString(image,"/Subtype /Image\n");
2350 (void) FormatMagickString(buffer,MaxTextExtent,"/Name /Ma%.20g\n",
2351 (double) image->scene);
2352 (void) WriteBlobString(image,buffer);
2353 switch (compression)
2357 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,
2361 case LZWCompression:
2363 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"LZWDecode");
2366 case ZipCompression:
2368 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,
2374 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,
2379 (void) WriteBlobString(image,buffer);
2380 (void) FormatMagickString(buffer,MaxTextExtent,"/Width %.20g\n",
2381 (double) image->columns);
2382 (void) WriteBlobString(image,buffer);
2383 (void) FormatMagickString(buffer,MaxTextExtent,"/Height %.20g\n",
2384 (double) image->rows);
2385 (void) WriteBlobString(image,buffer);
2386 (void) WriteBlobString(image,"/ColorSpace /DeviceGray\n");
2387 (void) FormatMagickString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
2388 (compression == FaxCompression) || (compression == Group4Compression)
2390 (void) WriteBlobString(image,buffer);
2391 (void) FormatMagickString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
2393 (void) WriteBlobString(image,buffer);
2394 (void) WriteBlobString(image,">>\n");
2395 (void) WriteBlobString(image,"stream\n");
2396 offset=TellBlob(image);
2397 number_pixels=(MagickSizeType) image->columns*image->rows;
2398 switch (compression)
2400 case RLECompression:
2404 Allocate pixel array.
2406 length=(size_t) number_pixels;
2407 pixels=(unsigned char *) AcquireQuantumMemory(length,
2409 if (pixels == (unsigned char *) NULL)
2411 image=DestroyImage(image);
2412 ThrowWriterException(ResourceLimitError,
2413 "MemoryAllocationFailed");
2416 Dump Runlength encoded pixels.
2419 for (y=0; y < (ssize_t) image->rows; y++)
2421 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
2422 if (p == (const PixelPacket *) NULL)
2424 for (x=0; x < (ssize_t) image->columns; x++)
2426 *q++=ScaleQuantumToChar((Quantum) (GetAlphaPixelComponent(p)));
2430 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2431 if (compression == ZipCompression)
2432 status=ZLIBEncodeImage(image,length,pixels);
2435 if (compression == LZWCompression)
2436 status=LZWEncodeImage(image,length,pixels);
2438 status=PackbitsEncodeImage(image,length,pixels);
2439 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2440 if (status == MagickFalse)
2442 (void) CloseBlob(image);
2443 return(MagickFalse);
2450 Dump uncompressed PseudoColor packets.
2452 Ascii85Initialize(image);
2453 for (y=0; y < (ssize_t) image->rows; y++)
2455 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
2456 if (p == (const PixelPacket *) NULL)
2458 for (x=0; x < (ssize_t) image->columns; x++)
2460 Ascii85Encode(image,ScaleQuantumToChar((Quantum) (QuantumRange-
2461 GetOpacityPixelComponent(p))));
2465 Ascii85Flush(image);
2469 offset=TellBlob(image)-offset;
2470 (void) WriteBlobString(image,"\nendstream\n");
2472 (void) WriteBlobString(image,"endobj\n");
2474 Write Length object.
2476 xref[object++]=TellBlob(image);
2477 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2479 (void) WriteBlobString(image,buffer);
2480 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
2481 (void) WriteBlobString(image,buffer);
2482 (void) WriteBlobString(image,"endobj\n");
2483 if (GetNextImageInList(image) == (Image *) NULL)
2485 image=SyncNextImageInList(image);
2486 status=SetImageProgress(image,SaveImagesTag,scene++,
2487 GetImageListLength(image));
2488 if (status == MagickFalse)
2490 } while (image_info->adjoin != MagickFalse);
2492 Write Metadata object.
2494 xref[object++]=TellBlob(image);
2496 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2498 (void) WriteBlobString(image,buffer);
2499 (void) WriteBlobString(image,"<<\n");
2500 (void) FormatMagickString(buffer,MaxTextExtent,"/Title (%s)\n",
2501 EscapeParenthesis(image->filename));
2502 (void) WriteBlobString(image,buffer);
2503 seconds=time((time_t *) NULL);
2504 #if defined(MAGICKCORE_HAVE_LOCALTIME_R)
2505 (void) localtime_r(&seconds,&local_time);
2507 (void) memcpy(&local_time,localtime(&seconds),sizeof(local_time));
2509 (void) FormatMagickString(date,MaxTextExtent,"D:%04d%02d%02d%02d%02d%02d",
2510 local_time.tm_year+1900,local_time.tm_mon+1,local_time.tm_mday,
2511 local_time.tm_hour,local_time.tm_min,local_time.tm_sec);
2512 (void) FormatMagickString(buffer,MaxTextExtent,"/CreationDate (%s)\n",date);
2513 (void) WriteBlobString(image,buffer);
2514 (void) FormatMagickString(buffer,MaxTextExtent,"/ModDate (%s)\n",date);
2515 (void) WriteBlobString(image,buffer);
2516 (void) FormatMagickString(buffer,MaxTextExtent,"/Producer (%s)\n",
2517 EscapeParenthesis(GetMagickVersion((size_t *) NULL)));
2518 (void) WriteBlobString(image,buffer);
2519 (void) WriteBlobString(image,">>\n");
2520 (void) WriteBlobString(image,"endobj\n");
2524 offset=TellBlob(image)-xref[0]+10;
2525 (void) WriteBlobString(image,"xref\n");
2526 (void) FormatMagickString(buffer,MaxTextExtent,"0 %.20g\n",(double)
2528 (void) WriteBlobString(image,buffer);
2529 (void) WriteBlobString(image,"0000000000 65535 f \n");
2530 for (i=0; i < (ssize_t) object; i++)
2532 (void) FormatMagickString(buffer,MaxTextExtent,"%010lu 00000 n \n",
2533 (unsigned long) xref[i]);
2534 (void) WriteBlobString(image,buffer);
2536 (void) WriteBlobString(image,"trailer\n");
2537 (void) WriteBlobString(image,"<<\n");
2538 (void) FormatMagickString(buffer,MaxTextExtent,"/Size %.20g\n",(double)
2540 (void) WriteBlobString(image,buffer);
2541 (void) FormatMagickString(buffer,MaxTextExtent,"/Info %.20g 0 R\n",(double)
2543 (void) WriteBlobString(image,buffer);
2544 (void) FormatMagickString(buffer,MaxTextExtent,"/Root %.20g 0 R\n",(double)
2546 (void) WriteBlobString(image,buffer);
2547 (void) WriteBlobString(image,">>\n");
2548 (void) WriteBlobString(image,"startxref\n");
2549 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
2550 (void) WriteBlobString(image,buffer);
2551 (void) WriteBlobString(image,"%%EOF\n");
2552 xref=(MagickOffsetType *) RelinquishMagickMemory(xref);
2553 (void) CloseBlob(image);