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 Count "Count"
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));
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(Count,command,strlen(Count)) == 0)
452 count=strtol(command+6,(char **) NULL,10);
453 if (count > (ssize_t) pages)
454 pages=(size_t) count;
456 if (LocaleNCompare(SpotColor,command,strlen(SpotColor)) == 0)
460 property[MaxTextExtent],
469 (void) FormatMagickString(property,MaxTextExtent,"pdf:SpotColor-%.20g",
470 (double) spotcolor++);
472 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
474 if ((isspace(c) != 0) || (c == '/') || ((i+1) == MaxTextExtent))
479 value=AcquireString(name);
480 (void) SubstituteString(&value,"#20"," ");
481 (void) SetImageProperty(image,property,value);
482 value=DestroyString(value);
485 if (LocaleNCompare(PDFVersion,command,strlen(PDFVersion)) == 0)
486 (void) SetImageProperty(image,"pdf:Version",command);
488 if (cropbox != MagickFalse)
490 if (LocaleNCompare(CropBox,command,strlen(CropBox)) == 0)
493 Note region defined by crop box.
495 count=(ssize_t) sscanf(command,"CropBox [%lf %lf %lf %lf",
496 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
498 count=(ssize_t) sscanf(command,"CropBox[%lf %lf %lf %lf",
499 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
503 if (trimbox != MagickFalse)
505 if (LocaleNCompare(TrimBox,command,strlen(TrimBox)) == 0)
508 Note region defined by trim box.
510 count=(ssize_t) sscanf(command,"TrimBox [%lf %lf %lf %lf",
511 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
513 count=(ssize_t) sscanf(command,"TrimBox[%lf %lf %lf %lf",
514 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
518 if (LocaleNCompare(MediaBox,command,strlen(MediaBox)) == 0)
521 Note region defined by media box.
523 count=(ssize_t) sscanf(command,"MediaBox [%lf %lf %lf %lf",
524 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
526 count=(ssize_t) sscanf(command,"MediaBox[%lf %lf %lf %lf",
527 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
531 if (((bounds.x2 > hires_bounds.x2) && (bounds.y2 > hires_bounds.y2)) ||
532 ((hires_bounds.x2 == 0.0) && (hires_bounds.y2 == 0.0)))
535 Set PDF render geometry.
537 (void) FormatMagickString(geometry,MaxTextExtent,
538 "%gx%g%+.15g%+.15g",bounds.x2-bounds.x1,bounds.y2-bounds.y1,
539 bounds.x1,bounds.y1);
540 (void) SetImageProperty(image,"pdf:HiResBoundingBox",geometry);
541 page.width=(size_t) floor(bounds.x2-bounds.x1+0.5);
542 page.height=(size_t) floor(bounds.y2-bounds.y1+0.5);
546 (void) CloseBlob(image);
547 if ((fabs(angle) == 90.0) || (fabs(angle) == 270.0))
553 page.width=page.height;
556 if (image_info->colorspace == RGBColorspace)
559 Create Ghostscript control file.
561 file=AcquireUniqueFileResource(postscript_filename);
564 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
565 image_info->filename);
566 image=DestroyImage(image);
567 return((Image *) NULL);
569 count=write(file," ",1);
572 Render Postscript with the Ghostscript delegate.
574 if ((image_info->ping != MagickFalse) ||
575 (image_info->monochrome != MagickFalse))
576 delegate_info=GetDelegateInfo("ps:mono",(char *) NULL,exception);
578 if (cmyk != MagickFalse)
579 delegate_info=GetDelegateInfo("ps:cmyk",(char *) NULL,exception);
582 delegate_info=GetDelegateInfo("ps:alpha",(char *) NULL,exception);
584 delegate_info=GetDelegateInfo("ps:color",(char *) NULL,exception);
585 if (delegate_info == (const DelegateInfo *) NULL)
587 (void) RelinquishUniqueFileResource(postscript_filename);
588 image=DestroyImage(image);
589 return((Image *) NULL);
592 if (image_info->density != (char *) NULL)
594 flags=ParseGeometry(image_info->density,&geometry_info);
595 image->x_resolution=geometry_info.rho;
596 image->y_resolution=geometry_info.sigma;
597 if ((flags & SigmaValue) == 0)
598 image->y_resolution=image->x_resolution;
600 (void) FormatMagickString(density,MaxTextExtent,"%gx%g",image->x_resolution,
601 image->y_resolution);
602 if (image_info->page != (char *) NULL)
604 (void) ParseAbsoluteGeometry(image_info->page,&page);
605 page.width=(size_t) floor(page.width*image->x_resolution/delta.x+0.5);
606 page.height=(size_t) floor(page.height*image->y_resolution/delta.y+0.5);
607 (void) FormatMagickString(options,MaxTextExtent,"-g%.20gx%.20g ",(double)
608 page.width,(double) page.height);
610 if (cmyk != MagickFalse)
611 (void) ConcatenateMagickString(options,"-dUseCIEColor ",MaxTextExtent);
612 if (cropbox != MagickFalse)
613 (void) ConcatenateMagickString(options,"-dUseCropBox ",MaxTextExtent);
614 if (trimbox != MagickFalse)
615 (void) ConcatenateMagickString(options,"-dUseTrimBox ",MaxTextExtent);
616 read_info=CloneImageInfo(image_info);
617 *read_info->magick='\0';
618 if (read_info->number_scenes != 0)
621 pages[MaxTextExtent];
623 (void) FormatMagickString(pages,MaxTextExtent,"-dFirstPage=%.20g "
624 "-dLastPage=%.20g",(double) read_info->scene+1,(double)
625 (read_info->scene+read_info->number_scenes));
626 (void) ConcatenateMagickString(options,pages,MaxTextExtent);
627 read_info->number_scenes=0;
628 if (read_info->scenes != (char *) NULL)
629 *read_info->scenes='\0';
631 if (read_info->authenticate != (char *) NULL)
632 (void) FormatMagickString(options+strlen(options),MaxTextExtent,
633 " -sPDFPassword=%s",read_info->authenticate);
634 (void) CopyMagickString(filename,read_info->filename,MaxTextExtent);
635 (void) AcquireUniqueFilename(read_info->filename);
636 (void) FormatMagickString(command,MaxTextExtent,
637 GetDelegateCommands(delegate_info),
638 read_info->antialias != MagickFalse ? 4 : 1,
639 read_info->antialias != MagickFalse ? 4 : 1,density,options,
640 read_info->filename,postscript_filename,input_filename);
641 status=InvokePDFDelegate(read_info->verbose,command,exception);
642 pdf_image=(Image *) NULL;
643 if ((status != MagickFalse) &&
644 (IsPDFRendered(read_info->filename) != MagickFalse))
645 pdf_image=ReadImage(read_info,exception);
646 (void) RelinquishUniqueFileResource(postscript_filename);
647 (void) RelinquishUniqueFileResource(read_info->filename);
648 (void) RelinquishUniqueFileResource(input_filename);
649 read_info=DestroyImageInfo(read_info);
650 if (pdf_image == (Image *) NULL)
652 ThrowFileException(exception,DelegateError,"PostscriptDelegateFailed",
653 image_info->filename);
654 return((Image *) NULL);
656 if (LocaleCompare(pdf_image->magick,"BMP") == 0)
661 cmyk_image=ConsolidateCMYKImages(pdf_image,exception);
662 if (cmyk_image != (Image *) NULL)
664 pdf_image=DestroyImageList(pdf_image);
665 pdf_image=cmyk_image;
668 if (image_info->number_scenes != 0)
677 Add place holder images to meet the subimage specification requirement.
679 for (i=0; i < (ssize_t) image_info->scene; i++)
681 clone_image=CloneImage(pdf_image,1,1,MagickTrue,exception);
682 if (clone_image != (Image *) NULL)
683 PrependImageToList(&pdf_image,clone_image);
688 (void) CopyMagickString(pdf_image->filename,filename,MaxTextExtent);
689 pdf_image->page=page;
690 (void) CloneImageProfiles(pdf_image,image);
691 (void) CloneImageProperties(pdf_image,image);
692 next=SyncNextImageInList(pdf_image);
693 if (next != (Image *) NULL)
695 } while (next != (Image *) NULL);
696 image=DestroyImage(image);
698 for (next=GetFirstImageInList(pdf_image); next != (Image *) NULL; )
701 next=GetNextImageInList(next);
703 return(GetFirstImageInList(pdf_image));
707 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
711 % R e g i s t e r P D F I m a g e %
715 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
717 % RegisterPDFImage() adds properties for the PDF image format to
718 % the list of supported formats. The properties include the image format
719 % tag, a method to read and/or write the format, whether the format
720 % supports the saving of more than one frame to the same file or blob,
721 % whether the format supports native in-memory I/O, and a brief
722 % description of the format.
724 % The format of the RegisterPDFImage method is:
726 % size_t RegisterPDFImage(void)
729 ModuleExport size_t RegisterPDFImage(void)
734 entry=SetMagickInfo("AI");
735 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
736 entry->encoder=(EncodeImageHandler *) WritePDFImage;
737 entry->adjoin=MagickFalse;
738 entry->blob_support=MagickFalse;
739 entry->seekable_stream=MagickTrue;
740 entry->thread_support=EncoderThreadSupport;
741 entry->description=ConstantString("Adobe Illustrator CS2");
742 entry->module=ConstantString("PDF");
743 (void) RegisterMagickInfo(entry);
744 entry=SetMagickInfo("EPDF");
745 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
746 entry->encoder=(EncodeImageHandler *) WritePDFImage;
747 entry->adjoin=MagickFalse;
748 entry->blob_support=MagickFalse;
749 entry->seekable_stream=MagickTrue;
750 entry->thread_support=EncoderThreadSupport;
751 entry->description=ConstantString("Encapsulated Portable Document Format");
752 entry->module=ConstantString("PDF");
753 (void) RegisterMagickInfo(entry);
754 entry=SetMagickInfo("PDF");
755 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
756 entry->encoder=(EncodeImageHandler *) WritePDFImage;
757 entry->magick=(IsImageFormatHandler *) IsPDF;
758 entry->blob_support=MagickFalse;
759 entry->seekable_stream=MagickTrue;
760 entry->thread_support=EncoderThreadSupport;
761 entry->description=ConstantString("Portable Document Format");
762 entry->module=ConstantString("PDF");
763 (void) RegisterMagickInfo(entry);
764 entry=SetMagickInfo("PDFA");
765 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
766 entry->encoder=(EncodeImageHandler *) WritePDFImage;
767 entry->magick=(IsImageFormatHandler *) IsPDF;
768 entry->blob_support=MagickFalse;
769 entry->seekable_stream=MagickTrue;
770 entry->thread_support=EncoderThreadSupport;
771 entry->description=ConstantString("Portable Document Archive Format");
772 entry->module=ConstantString("PDF");
773 (void) RegisterMagickInfo(entry);
774 return(MagickImageCoderSignature);
778 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
782 % U n r e g i s t e r P D F I m a g e %
786 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
788 % UnregisterPDFImage() removes format registrations made by the
789 % PDF module from the list of supported formats.
791 % The format of the UnregisterPDFImage method is:
793 % UnregisterPDFImage(void)
796 ModuleExport void UnregisterPDFImage(void)
798 (void) UnregisterMagickInfo("AI");
799 (void) UnregisterMagickInfo("EPDF");
800 (void) UnregisterMagickInfo("PDF");
801 (void) UnregisterMagickInfo("PDFA");
805 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
809 % W r i t e P D F I m a g e %
813 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
815 % WritePDFImage() writes an image in the Portable Document image
818 % The format of the WritePDFImage method is:
820 % MagickBooleanType WritePDFImage(const ImageInfo *image_info,Image *image)
822 % A description of each parameter follows.
824 % o image_info: the image info.
826 % o image: The image.
830 static inline size_t MagickMax(const size_t x,const size_t y)
837 static inline size_t MagickMin(const size_t x,const size_t y)
844 static char *EscapeParenthesis(const char *text)
853 buffer[MaxTextExtent];
860 for (i=0; i < (ssize_t) MagickMin(strlen(text),(MaxTextExtent-escapes-1)); i++)
862 if ((text[i] == '(') || (text[i] == ')'))
873 static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
874 Image *image,Image *inject_image)
892 write_info=CloneImageInfo(image_info);
893 (void) CopyMagickString(write_info->filename,"GROUP4:",MaxTextExtent);
894 (void) CopyMagickString(write_info->magick,"GROUP4",MaxTextExtent);
895 group4_image=CloneImage(inject_image,0,0,MagickTrue,&image->exception);
896 if (group4_image == (Image *) NULL)
898 group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
900 group4_image=DestroyImage(group4_image);
901 if (group4 == (unsigned char *) NULL)
903 write_info=DestroyImageInfo(write_info);
904 if (WriteBlob(image,length,group4) != (ssize_t) length)
906 group4=(unsigned char *) RelinquishMagickMemory(group4);
910 static MagickBooleanType WritePDFImage(const ImageInfo *image_info,Image *image)
912 #define CFormat "/Filter [ /%s ]\n"
913 #define ObjectsPerImage 14
918 "<?xpacket begin=\"%s\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n"
919 "<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"
920 " <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n"
921 " <rdf:Description rdf:about=\"\"\n"
922 " xmlns:xap=\"http://ns.adobe.com/xap/1.0/\">\n"
923 " <xap:ModifyDate>%s</xap:ModifyDate>\n"
924 " <xap:CreateDate>%s</xap:CreateDate>\n"
925 " <xap:MetadataDate>%s</xap:MetadataDate>\n"
926 " <xap:CreatorTool>%s</xap:CreatorTool>\n"
927 " </rdf:Description>\n"
928 " <rdf:Description rdf:about=\"\"\n"
929 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"
930 " <dc:format>application/pdf</dc:format>\n"
931 " </rdf:Description>\n"
932 " <rdf:Description rdf:about=\"\"\n"
933 " xmlns:xapMM=\"http://ns.adobe.com/xap/1.0/mm/\">\n"
934 " <xapMM:DocumentID>uuid:6ec119d7-7982-4f56-808d-dfe64f5b35cf</xapMM:DocumentID>\n"
935 " <xapMM:InstanceID>uuid:a79b99b4-6235-447f-9f6c-ec18ef7555cb</xapMM:InstanceID>\n"
936 " </rdf:Description>\n"
937 " <rdf:Description rdf:about=\"\"\n"
938 " xmlns:pdf=\"http://ns.adobe.com/pdf/1.3/\">\n"
939 " <pdf:Producer>%s</pdf:Producer>\n"
940 " </rdf:Description>\n"
941 " <rdf:Description rdf:about=\"\"\n"
942 " xmlns:pdfaid=\"http://www.aiim.org/pdfa/ns/id/\">\n"
943 " <pdfaid:part>1</pdfaid:part>\n"
944 " <pdfaid:conformance>B</pdfaid:conformance>\n"
945 " </rdf:Description>\n"
948 "<?xpacket end=\"w\"?>\n"
950 XMPProfileMagick[4]= { (char) 0xef, (char) 0xbb, (char) 0xbf, (char) 0x00 };
953 basename[MaxTextExtent],
954 buffer[MaxTextExtent],
957 page_geometry[MaxTextExtent];
1003 register const IndexPacket
1006 register const PixelPacket
1009 register unsigned char
1037 Open output image file.
1039 assert(image_info != (const ImageInfo *) NULL);
1040 assert(image_info->signature == MagickSignature);
1041 assert(image != (Image *) NULL);
1042 assert(image->signature == MagickSignature);
1043 if (image->debug != MagickFalse)
1044 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1045 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1046 if (status == MagickFalse)
1049 Allocate X ref memory.
1051 xref=(MagickOffsetType *) AcquireQuantumMemory(2048UL,sizeof(*xref));
1052 if (xref == (MagickOffsetType *) NULL)
1053 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1054 (void) ResetMagickMemory(xref,0,2048UL*sizeof(*xref));
1060 if (image_info->compression == JPEG2000Compression)
1061 version=(size_t) MagickMax(version,5);
1062 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
1063 if (next->matte != MagickFalse)
1064 version=(size_t) MagickMax(version,4);
1065 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1066 version=(size_t) MagickMax(version,6);
1067 (void) FormatMagickString(buffer,MaxTextExtent,"%%PDF-1.%.20g \n",
1069 (void) WriteBlobString(image,buffer);
1070 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1071 (void) WriteBlobString(image,"%âãÏÓ\n");
1073 Write Catalog object.
1075 xref[object++]=TellBlob(image);
1077 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1079 (void) WriteBlobString(image,buffer);
1080 (void) WriteBlobString(image,"<<\n");
1081 if (LocaleCompare(image_info->magick,"PDFA") != 0)
1082 (void) FormatMagickString(buffer,MaxTextExtent,"/Pages %.20g 0 R\n",
1086 (void) FormatMagickString(buffer,MaxTextExtent,"/Metadata %.20g 0 R\n",
1088 (void) WriteBlobString(image,buffer);
1089 (void) FormatMagickString(buffer,MaxTextExtent,"/Pages %.20g 0 R\n",
1092 (void) WriteBlobString(image,buffer);
1093 (void) WriteBlobString(image,"/Type /Catalog\n");
1094 (void) WriteBlobString(image,">>\n");
1095 (void) WriteBlobString(image,"endobj\n");
1096 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1099 create_date[MaxTextExtent],
1100 modify_date[MaxTextExtent],
1101 timestamp[MaxTextExtent],
1102 xmp_profile[MaxTextExtent];
1110 xref[object++]=TellBlob(image);
1111 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",
1113 (void) WriteBlobString(image,buffer);
1114 (void) WriteBlobString(image,"<<\n");
1115 (void) WriteBlobString(image,"/Subtype /XML\n");
1117 value=GetImageProperty(image,"date:modify");
1118 if (value != (const char *) NULL)
1119 (void) CopyMagickString(modify_date,value,MaxTextExtent);
1121 value=GetImageProperty(image,"date:create");
1122 if (value != (const char *) NULL)
1123 (void) CopyMagickString(create_date,value,MaxTextExtent);
1124 (void) FormatMagickTime(time((time_t *) NULL),MaxTextExtent,timestamp);
1125 i=FormatMagickString(xmp_profile,MaxTextExtent,XMPProfile,
1126 XMPProfileMagick,modify_date,create_date,timestamp,
1127 GetMagickVersion(&version),GetMagickVersion(&version));
1128 (void) FormatMagickString(buffer,MaxTextExtent,"/Length %.20g\n",
1130 (void) WriteBlobString(image,buffer);
1131 (void) WriteBlobString(image,"/Type /Metadata\n");
1132 (void) WriteBlobString(image,">>\nstream\n");
1133 (void) WriteBlobString(image,xmp_profile);
1134 (void) WriteBlobString(image,"endstream\n");
1135 (void) WriteBlobString(image,"endobj\n");
1140 xref[object++]=TellBlob(image);
1142 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1144 (void) WriteBlobString(image,buffer);
1145 (void) WriteBlobString(image,"<<\n");
1146 (void) WriteBlobString(image,"/Type /Pages\n");
1147 (void) FormatMagickString(buffer,MaxTextExtent,"/Kids [ %.20g 0 R ",
1149 (void) WriteBlobString(image,buffer);
1150 count=(ssize_t) (pages_id+ObjectsPerImage+1);
1151 if (image_info->adjoin != MagickFalse)
1157 Predict page object id's.
1160 for ( ; GetNextImageInList(kid_image) != (Image *) NULL; count+=ObjectsPerImage)
1162 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 R ",(double)
1164 (void) WriteBlobString(image,buffer);
1165 kid_image=GetNextImageInList(kid_image);
1167 xref=(MagickOffsetType *) ResizeQuantumMemory(xref,(size_t) count+2048UL,
1169 if (xref == (MagickOffsetType *) NULL)
1170 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1172 (void) WriteBlobString(image,"]\n");
1173 (void) FormatMagickString(buffer,MaxTextExtent,"/Count %.20g\n",
1174 (double) ((count-pages_id)/ObjectsPerImage));
1175 (void) WriteBlobString(image,buffer);
1176 (void) WriteBlobString(image,">>\n");
1177 (void) WriteBlobString(image,"endobj\n");
1181 compression=image->compression;
1182 if (image_info->compression != UndefinedCompression)
1183 compression=image_info->compression;
1184 switch (compression)
1186 case FaxCompression:
1187 case Group4Compression:
1189 if ((IsMonochromeImage(image,&image->exception) == MagickFalse) ||
1190 (image->matte != MagickFalse))
1191 compression=RLECompression;
1194 #if !defined(MAGICKCORE_JPEG_DELEGATE)
1195 case JPEGCompression:
1197 compression=RLECompression;
1198 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1199 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
1204 #if !defined(MAGICKCORE_JP2_DELEGATE)
1205 case JPEG2000Compression:
1207 compression=RLECompression;
1208 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1209 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JP2)",
1214 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
1215 case ZipCompression:
1217 compression=RLECompression;
1218 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1219 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZLIB)",
1224 case LZWCompression:
1226 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1227 compression=RLECompression; /* LZW compression is forbidden */
1232 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1233 compression=RLECompression; /* ASCII 85 compression is forbidden */
1239 if (compression == JPEG2000Compression)
1241 if (image->colorspace != RGBColorspace)
1242 (void) TransformImageColorspace(image,RGBColorspace);
1245 Scale relative to dots-per-inch.
1247 delta.x=DefaultResolution;
1248 delta.y=DefaultResolution;
1249 resolution.x=image->x_resolution;
1250 resolution.y=image->y_resolution;
1251 if ((resolution.x == 0.0) || (resolution.y == 0.0))
1253 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
1254 resolution.x=geometry_info.rho;
1255 resolution.y=geometry_info.sigma;
1256 if ((flags & SigmaValue) == 0)
1257 resolution.y=resolution.x;
1259 if (image_info->density != (char *) NULL)
1261 flags=ParseGeometry(image_info->density,&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->units == PixelsPerCentimeterResolution)
1269 resolution.x=(size_t) (100.0*2.54*resolution.x+0.5)/100.0;
1270 resolution.y=(size_t) (100.0*2.54*resolution.y+0.5)/100.0;
1272 SetGeometry(image,&geometry);
1273 (void) FormatMagickString(page_geometry,MaxTextExtent,"%.20gx%.20g",
1274 (double) image->columns,(double) image->rows);
1275 if (image_info->page != (char *) NULL)
1276 (void) CopyMagickString(page_geometry,image_info->page,MaxTextExtent);
1278 if ((image->page.width != 0) && (image->page.height != 0))
1279 (void) FormatMagickString(page_geometry,MaxTextExtent,
1280 "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,
1281 (double) image->page.height,(double) image->page.x,(double)
1284 if ((image->gravity != UndefinedGravity) &&
1285 (LocaleCompare(image_info->magick,"PDF") == 0))
1286 (void) CopyMagickString(page_geometry,PSPageGeometry,MaxTextExtent);
1287 (void) ConcatenateMagickString(page_geometry,">",MaxTextExtent);
1288 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
1289 &geometry.width,&geometry.height);
1290 scale.x=(double) (geometry.width*delta.x)/resolution.x;
1291 geometry.width=(size_t) floor(scale.x+0.5);
1292 scale.y=(double) (geometry.height*delta.y)/resolution.y;
1293 geometry.height=(size_t) floor(scale.y+0.5);
1294 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
1295 (void) ParseGravityGeometry(image,page_geometry,&page_info,
1297 if (image->gravity != UndefinedGravity)
1299 geometry.x=(-page_info.x);
1300 geometry.y=(ssize_t) (media_info.height+page_info.y-image->rows);
1303 if (image_info->pointsize != 0.0)
1304 pointsize=image_info->pointsize;
1306 value=GetImageProperty(image,"Label");
1307 if (value != (const char *) NULL)
1308 text_size=(size_t) (MultilineCensus(value)*pointsize+12);
1312 xref[object++]=TellBlob(image);
1313 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1315 (void) WriteBlobString(image,buffer);
1316 (void) WriteBlobString(image,"<<\n");
1317 (void) WriteBlobString(image,"/Type /Page\n");
1318 (void) FormatMagickString(buffer,MaxTextExtent,"/Parent %.20g 0 R\n",
1320 (void) WriteBlobString(image,buffer);
1321 (void) WriteBlobString(image,"/Resources <<\n");
1322 labels=(char **) NULL;
1323 value=GetImageProperty(image,"Label");
1324 if (value != (const char *) NULL)
1325 labels=StringToList(value);
1326 if (labels != (char **) NULL)
1328 (void) FormatMagickString(buffer,MaxTextExtent,
1329 "/Font << /F%.20g %.20g 0 R >>\n",(double) image->scene,(double)
1331 (void) WriteBlobString(image,buffer);
1333 (void) FormatMagickString(buffer,MaxTextExtent,
1334 "/XObject << /Im%.20g %.20g 0 R >>\n",(double) image->scene,(double)
1336 (void) WriteBlobString(image,buffer);
1337 (void) FormatMagickString(buffer,MaxTextExtent,"/ProcSet %.20g 0 R >>\n",
1339 (void) WriteBlobString(image,buffer);
1340 (void) FormatMagickString(buffer,MaxTextExtent,
1341 "/MediaBox [0 0 %g %g]\n",72.0*media_info.width/resolution.x,
1342 72.0*media_info.height/resolution.y);
1343 (void) WriteBlobString(image,buffer);
1344 (void) FormatMagickString(buffer,MaxTextExtent,
1345 "/CropBox [0 0 %g %g]\n",72.0*media_info.width/resolution.x,
1346 72.0*media_info.height/resolution.y);
1347 (void) WriteBlobString(image,buffer);
1348 (void) FormatMagickString(buffer,MaxTextExtent,"/Contents %.20g 0 R\n",
1350 (void) WriteBlobString(image,buffer);
1351 (void) FormatMagickString(buffer,MaxTextExtent,"/Thumb %.20g 0 R\n",
1353 (void) WriteBlobString(image,buffer);
1354 (void) WriteBlobString(image,">>\n");
1355 (void) WriteBlobString(image,"endobj\n");
1357 Write Contents object.
1359 xref[object++]=TellBlob(image);
1360 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1362 (void) WriteBlobString(image,buffer);
1363 (void) WriteBlobString(image,"<<\n");
1364 (void) FormatMagickString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
1366 (void) WriteBlobString(image,buffer);
1367 (void) WriteBlobString(image,">>\n");
1368 (void) WriteBlobString(image,"stream\n");
1369 offset=TellBlob(image);
1370 (void) WriteBlobString(image,"q\n");
1371 if (labels != (char **) NULL)
1372 for (i=0; labels[i] != (char *) NULL; i++)
1374 (void) WriteBlobString(image,"BT\n");
1375 (void) FormatMagickString(buffer,MaxTextExtent,"/F%.20g %g Tf\n",
1376 (double) image->scene,pointsize);
1377 (void) WriteBlobString(image,buffer);
1378 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g %.20g Td\n",
1379 (double) geometry.x,(double) (geometry.y+geometry.height+i*pointsize+
1381 (void) WriteBlobString(image,buffer);
1382 (void) FormatMagickString(buffer,MaxTextExtent,"(%s) Tj\n",labels[i]);
1383 (void) WriteBlobString(image,buffer);
1384 (void) WriteBlobString(image,"ET\n");
1385 labels[i]=DestroyString(labels[i]);
1387 (void) FormatMagickString(buffer,MaxTextExtent,"%g 0 0 %g %.20g %.20g cm\n",
1388 scale.x,scale.y,(double) geometry.x,(double) geometry.y);
1389 (void) WriteBlobString(image,buffer);
1390 (void) FormatMagickString(buffer,MaxTextExtent,"/Im%.20g Do\n",
1391 (double) image->scene);
1392 (void) WriteBlobString(image,buffer);
1393 (void) WriteBlobString(image,"Q\n");
1394 offset=TellBlob(image)-offset;
1395 (void) WriteBlobString(image,"endstream\n");
1396 (void) WriteBlobString(image,"endobj\n");
1398 Write Length object.
1400 xref[object++]=TellBlob(image);
1401 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1403 (void) WriteBlobString(image,buffer);
1404 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
1405 (void) WriteBlobString(image,buffer);
1406 (void) WriteBlobString(image,"endobj\n");
1408 Write Procset object.
1410 xref[object++]=TellBlob(image);
1411 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",
1413 (void) WriteBlobString(image,buffer);
1414 if ((image->storage_class == DirectClass) || (image->colors > 256))
1415 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageC",MaxTextExtent);
1417 if ((compression == FaxCompression) || (compression == Group4Compression))
1418 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageB",MaxTextExtent);
1420 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageI",MaxTextExtent);
1421 (void) WriteBlobString(image,buffer);
1422 (void) WriteBlobString(image," ]\n");
1423 (void) WriteBlobString(image,"endobj\n");
1427 xref[object++]=TellBlob(image);
1428 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",
1430 (void) WriteBlobString(image,buffer);
1431 (void) WriteBlobString(image,"<<\n");
1432 if (labels != (char **) NULL)
1434 (void) WriteBlobString(image,"/Type /Font\n");
1435 (void) WriteBlobString(image,"/Subtype /Type1\n");
1436 (void) FormatMagickString(buffer,MaxTextExtent,"/Name /F%.20g\n",
1437 (double) image->scene);
1438 (void) WriteBlobString(image,buffer);
1439 (void) WriteBlobString(image,"/BaseFont /Helvetica\n");
1440 (void) WriteBlobString(image,"/Encoding /MacRomanEncoding\n");
1441 labels=(char **) RelinquishMagickMemory(labels);
1443 (void) WriteBlobString(image,">>\n");
1444 (void) WriteBlobString(image,"endobj\n");
1446 Write XObject object.
1448 xref[object++]=TellBlob(image);
1449 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1451 (void) WriteBlobString(image,buffer);
1452 (void) WriteBlobString(image,"<<\n");
1453 (void) WriteBlobString(image,"/Type /XObject\n");
1454 (void) WriteBlobString(image,"/Subtype /Image\n");
1455 (void) FormatMagickString(buffer,MaxTextExtent,"/Name /Im%.20g\n",
1456 (double) image->scene);
1457 (void) WriteBlobString(image,buffer);
1458 switch (compression)
1462 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"ASCII85Decode");
1465 case JPEGCompression:
1467 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"DCTDecode");
1468 if (image->colorspace != CMYKColorspace)
1470 (void) WriteBlobString(image,buffer);
1471 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1475 case JPEG2000Compression:
1477 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"JPXDecode");
1478 if (image->colorspace != CMYKColorspace)
1480 (void) WriteBlobString(image,buffer);
1481 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1485 case LZWCompression:
1487 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"LZWDecode");
1490 case ZipCompression:
1492 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"FlateDecode");
1495 case FaxCompression:
1496 case Group4Compression:
1498 (void) CopyMagickString(buffer,"/Filter [ /CCITTFaxDecode ]\n",
1500 (void) WriteBlobString(image,buffer);
1501 (void) FormatMagickString(buffer,MaxTextExtent,"/DecodeParms [ << "
1502 "/K %s /BlackIs1 false /Columns %.20g /Rows %.20g >> ]\n",CCITTParam,
1503 (double) image->columns,(double) image->rows);
1508 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,
1513 (void) WriteBlobString(image,buffer);
1514 (void) FormatMagickString(buffer,MaxTextExtent,"/Width %.20g\n",(double)
1516 (void) WriteBlobString(image,buffer);
1517 (void) FormatMagickString(buffer,MaxTextExtent,"/Height %.20g\n",(double)
1519 (void) WriteBlobString(image,buffer);
1520 (void) FormatMagickString(buffer,MaxTextExtent,"/ColorSpace %.20g 0 R\n",
1522 (void) WriteBlobString(image,buffer);
1523 (void) FormatMagickString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
1524 (compression == FaxCompression) || (compression == Group4Compression) ?
1526 (void) WriteBlobString(image,buffer);
1527 if (image->matte != MagickFalse)
1529 (void) FormatMagickString(buffer,MaxTextExtent,"/SMask %.20g 0 R\n",
1531 (void) WriteBlobString(image,buffer);
1533 (void) FormatMagickString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
1535 (void) WriteBlobString(image,buffer);
1536 (void) WriteBlobString(image,">>\n");
1537 (void) WriteBlobString(image,"stream\n");
1538 offset=TellBlob(image);
1539 number_pixels=(MagickSizeType) image->columns*image->rows;
1540 if ((4*number_pixels) != (MagickSizeType) ((size_t) (4*number_pixels)))
1541 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1542 if ((compression == FaxCompression) || (compression == Group4Compression) ||
1543 ((image_info->type != TrueColorType) &&
1544 (IsGrayImage(image,&image->exception) != MagickFalse)))
1546 switch (compression)
1548 case FaxCompression:
1549 case Group4Compression:
1551 if (LocaleCompare(CCITTParam,"0") == 0)
1553 (void) HuffmanEncodeImage(image_info,image,image);
1556 (void) Huffman2DEncodeImage(image_info,image,image);
1559 case JPEGCompression:
1561 status=InjectImageBlob(image_info,image,image,"jpeg",
1563 if (status == MagickFalse)
1564 ThrowWriterException(CoderError,image->exception.reason);
1567 case JPEG2000Compression:
1569 status=InjectImageBlob(image_info,image,image,"jp2",
1571 if (status == MagickFalse)
1572 ThrowWriterException(CoderError,image->exception.reason);
1575 case RLECompression:
1579 Allocate pixel array.
1581 length=(size_t) number_pixels;
1582 pixels=(unsigned char *) AcquireQuantumMemory(length,
1584 if (pixels == (unsigned char *) NULL)
1585 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1587 Dump Runlength encoded pixels.
1590 for (y=0; y < (ssize_t) image->rows; y++)
1592 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1593 if (p == (const PixelPacket *) NULL)
1595 for (x=0; x < (ssize_t) image->columns; x++)
1597 *q++=ScaleQuantumToChar(PixelIntensityToQuantum(p));
1600 if (image->previous == (Image *) NULL)
1602 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1604 if (status == MagickFalse)
1608 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1609 if (compression == ZipCompression)
1610 status=ZLIBEncodeImage(image,length,pixels);
1613 if (compression == LZWCompression)
1614 status=LZWEncodeImage(image,length,pixels);
1616 status=PackbitsEncodeImage(image,length,pixels);
1617 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1618 if (status == MagickFalse)
1620 (void) CloseBlob(image);
1621 return(MagickFalse);
1628 Dump uncompressed PseudoColor packets.
1630 Ascii85Initialize(image);
1631 for (y=0; y < (ssize_t) image->rows; y++)
1633 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1634 if (p == (const PixelPacket *) NULL)
1636 for (x=0; x < (ssize_t) image->columns; x++)
1638 Ascii85Encode(image,
1639 ScaleQuantumToChar(PixelIntensityToQuantum(p)));
1642 if (image->previous == (Image *) NULL)
1644 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1646 if (status == MagickFalse)
1650 Ascii85Flush(image);
1656 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1657 (compression == JPEGCompression) ||
1658 (compression == JPEG2000Compression))
1659 switch (compression)
1661 case JPEGCompression:
1663 status=InjectImageBlob(image_info,image,image,"jpeg",
1665 if (status == MagickFalse)
1666 ThrowWriterException(CoderError,image->exception.reason);
1669 case JPEG2000Compression:
1671 status=InjectImageBlob(image_info,image,image,"jp2",
1673 if (status == MagickFalse)
1674 ThrowWriterException(CoderError,image->exception.reason);
1677 case RLECompression:
1681 Allocate pixel array.
1683 length=(size_t) number_pixels;
1684 pixels=(unsigned char *) AcquireQuantumMemory(length,
1686 length*=image->colorspace == CMYKColorspace ? 4UL : 3UL;
1687 if (pixels == (unsigned char *) NULL)
1688 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1690 Dump runoffset encoded pixels.
1693 for (y=0; y < (ssize_t) image->rows; y++)
1695 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1696 if (p == (const PixelPacket *) NULL)
1698 indexes=GetVirtualIndexQueue(image);
1699 for (x=0; x < (ssize_t) image->columns; x++)
1701 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
1702 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1703 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
1704 if (image->colorspace == CMYKColorspace)
1705 *q++=ScaleQuantumToChar(indexes[x]);
1708 if (image->previous == (Image *) NULL)
1710 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1712 if (status == MagickFalse)
1716 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1717 if (compression == ZipCompression)
1718 status=ZLIBEncodeImage(image,length,pixels);
1721 if (compression == LZWCompression)
1722 status=LZWEncodeImage(image,length,pixels);
1724 status=PackbitsEncodeImage(image,length,pixels);
1725 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1726 if (status == MagickFalse)
1728 (void) CloseBlob(image);
1729 return(MagickFalse);
1736 Dump uncompressed DirectColor packets.
1738 Ascii85Initialize(image);
1739 for (y=0; y < (ssize_t) image->rows; y++)
1741 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1742 if (p == (const PixelPacket *) NULL)
1744 indexes=GetVirtualIndexQueue(image);
1745 for (x=0; x < (ssize_t) image->columns; x++)
1747 Ascii85Encode(image,ScaleQuantumToChar(
1748 GetRedPixelComponent(p)));
1749 Ascii85Encode(image,ScaleQuantumToChar(
1750 GetGreenPixelComponent(p)));
1751 Ascii85Encode(image,ScaleQuantumToChar(
1752 GetBluePixelComponent(p)));
1753 if (image->colorspace == CMYKColorspace)
1754 Ascii85Encode(image,ScaleQuantumToChar(indexes[x]));
1757 if (image->previous == (Image *) NULL)
1759 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1761 if (status == MagickFalse)
1765 Ascii85Flush(image);
1772 Dump number of colors and colormap.
1774 switch (compression)
1776 case RLECompression:
1780 Allocate pixel array.
1782 length=(size_t) number_pixels;
1783 pixels=(unsigned char *) AcquireQuantumMemory(length,
1785 if (pixels == (unsigned char *) NULL)
1786 ThrowWriterException(ResourceLimitError,
1787 "MemoryAllocationFailed");
1789 Dump Runlength encoded pixels.
1792 for (y=0; y < (ssize_t) image->rows; y++)
1794 p=GetVirtualPixels(image,0,y,image->columns,1,
1796 if (p == (const PixelPacket *) NULL)
1798 indexes=GetVirtualIndexQueue(image);
1799 for (x=0; x < (ssize_t) image->columns; x++)
1800 *q++=(unsigned char) indexes[x];
1801 if (image->previous == (Image *) NULL)
1803 status=SetImageProgress(image,SaveImageTag,
1804 (MagickOffsetType) y,image->rows);
1805 if (status == MagickFalse)
1809 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1810 if (compression == ZipCompression)
1811 status=ZLIBEncodeImage(image,length,pixels);
1814 if (compression == LZWCompression)
1815 status=LZWEncodeImage(image,length,pixels);
1817 status=PackbitsEncodeImage(image,length,pixels);
1818 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1819 if (status == MagickFalse)
1821 (void) CloseBlob(image);
1822 return(MagickFalse);
1829 Dump uncompressed PseudoColor packets.
1831 Ascii85Initialize(image);
1832 for (y=0; y < (ssize_t) image->rows; y++)
1834 p=GetVirtualPixels(image,0,y,image->columns,1,
1836 if (p == (const PixelPacket *) NULL)
1838 indexes=GetVirtualIndexQueue(image);
1839 for (x=0; x < (ssize_t) image->columns; x++)
1840 Ascii85Encode(image,(unsigned char) indexes[x]);
1841 if (image->previous == (Image *) NULL)
1843 status=SetImageProgress(image,SaveImageTag,
1844 (MagickOffsetType) y,image->rows);
1845 if (status == MagickFalse)
1849 Ascii85Flush(image);
1854 offset=TellBlob(image)-offset;
1855 (void) WriteBlobString(image,"\nendstream\n");
1856 (void) WriteBlobString(image,"endobj\n");
1858 Write Length object.
1860 xref[object++]=TellBlob(image);
1861 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1863 (void) WriteBlobString(image,buffer);
1864 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g\n",
1866 (void) WriteBlobString(image,buffer);
1867 (void) WriteBlobString(image,"endobj\n");
1869 Write Colorspace object.
1871 xref[object++]=TellBlob(image);
1872 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1874 (void) WriteBlobString(image,buffer);
1875 if (image->colorspace == CMYKColorspace)
1876 (void) CopyMagickString(buffer,"/DeviceCMYK\n",MaxTextExtent);
1878 if ((compression == FaxCompression) ||
1879 (compression == Group4Compression) ||
1880 ((image_info->type != TrueColorType) &&
1881 (IsGrayImage(image,&image->exception) != MagickFalse)))
1882 (void) CopyMagickString(buffer,"/DeviceGray\n",MaxTextExtent);
1884 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1885 (compression == JPEGCompression) ||
1886 (compression == JPEG2000Compression))
1887 (void) CopyMagickString(buffer,"/DeviceRGB\n",MaxTextExtent);
1889 (void) FormatMagickString(buffer,MaxTextExtent,
1890 "[ /Indexed /DeviceRGB %.20g %.20g 0 R ]\n",(double) image->colors-
1891 1,(double) object+3);
1892 (void) WriteBlobString(image,buffer);
1893 (void) WriteBlobString(image,"endobj\n");
1897 SetGeometry(image,&geometry);
1898 (void) ParseMetaGeometry("106x106+0+0>",&geometry.x,&geometry.y,
1899 &geometry.width,&geometry.height);
1900 tile_image=ThumbnailImage(image,geometry.width,geometry.height,
1902 if (tile_image == (Image *) NULL)
1903 ThrowWriterException(ResourceLimitError,image->exception.reason);
1904 xref[object++]=TellBlob(image);
1905 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1907 (void) WriteBlobString(image,buffer);
1908 (void) WriteBlobString(image,"<<\n");
1909 switch (compression)
1913 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"ASCII85Decode");
1916 case JPEGCompression:
1918 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"DCTDecode");
1919 if (image->colorspace != CMYKColorspace)
1921 (void) WriteBlobString(image,buffer);
1922 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1926 case JPEG2000Compression:
1928 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"JPXDecode");
1929 if (image->colorspace != CMYKColorspace)
1931 (void) WriteBlobString(image,buffer);
1932 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1936 case LZWCompression:
1938 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"LZWDecode");
1941 case ZipCompression:
1943 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"FlateDecode");
1946 case FaxCompression:
1947 case Group4Compression:
1949 (void) CopyMagickString(buffer,"/Filter [ /CCITTFaxDecode ]\n",
1951 (void) WriteBlobString(image,buffer);
1952 (void) FormatMagickString(buffer,MaxTextExtent,"/DecodeParms [ << "
1953 "/K %s /BlackIs1 false /Columns %.20g /Rows %.20g >> ]\n",CCITTParam,
1954 (double) tile_image->columns,(double) tile_image->rows);
1959 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,
1964 (void) WriteBlobString(image,buffer);
1965 (void) FormatMagickString(buffer,MaxTextExtent,"/Width %.20g\n",(double)
1966 tile_image->columns);
1967 (void) WriteBlobString(image,buffer);
1968 (void) FormatMagickString(buffer,MaxTextExtent,"/Height %.20g\n",(double)
1970 (void) WriteBlobString(image,buffer);
1971 (void) FormatMagickString(buffer,MaxTextExtent,"/ColorSpace %.20g 0 R\n",
1973 (void) WriteBlobString(image,buffer);
1974 (void) FormatMagickString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
1975 (compression == FaxCompression) || (compression == Group4Compression) ?
1977 (void) WriteBlobString(image,buffer);
1978 (void) FormatMagickString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
1980 (void) WriteBlobString(image,buffer);
1981 (void) WriteBlobString(image,">>\n");
1982 (void) WriteBlobString(image,"stream\n");
1983 offset=TellBlob(image);
1984 number_pixels=(MagickSizeType) tile_image->columns*tile_image->rows;
1985 if ((compression == FaxCompression) ||
1986 (compression == Group4Compression) ||
1987 ((image_info->type != TrueColorType) &&
1988 (IsGrayImage(tile_image,&image->exception) != MagickFalse)))
1990 switch (compression)
1992 case FaxCompression:
1993 case Group4Compression:
1995 if (LocaleCompare(CCITTParam,"0") == 0)
1997 (void) HuffmanEncodeImage(image_info,image,tile_image);
2000 (void) Huffman2DEncodeImage(image_info,image,tile_image);
2003 case JPEGCompression:
2005 status=InjectImageBlob(image_info,image,tile_image,"jpeg",
2007 if (status == MagickFalse)
2008 ThrowWriterException(CoderError,tile_image->exception.reason);
2011 case JPEG2000Compression:
2013 status=InjectImageBlob(image_info,image,tile_image,"jp2",
2015 if (status == MagickFalse)
2016 ThrowWriterException(CoderError,tile_image->exception.reason);
2019 case RLECompression:
2023 Allocate pixel array.
2025 length=(size_t) number_pixels;
2026 pixels=(unsigned char *) AcquireQuantumMemory(length,
2028 if (pixels == (unsigned char *) NULL)
2030 tile_image=DestroyImage(tile_image);
2031 ThrowWriterException(ResourceLimitError,
2032 "MemoryAllocationFailed");
2035 Dump Runlength encoded pixels.
2038 for (y=0; y < (ssize_t) tile_image->rows; y++)
2040 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2041 &tile_image->exception);
2042 if (p == (const PixelPacket *) NULL)
2044 for (x=0; x < (ssize_t) tile_image->columns; x++)
2046 *q++=ScaleQuantumToChar(PixelIntensityToQuantum(p));
2050 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2051 if (compression == ZipCompression)
2052 status=ZLIBEncodeImage(image,length,pixels);
2055 if (compression == LZWCompression)
2056 status=LZWEncodeImage(image,length,pixels);
2058 status=PackbitsEncodeImage(image,length,pixels);
2059 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2060 if (status == MagickFalse)
2062 (void) CloseBlob(image);
2063 return(MagickFalse);
2070 Dump uncompressed PseudoColor packets.
2072 Ascii85Initialize(image);
2073 for (y=0; y < (ssize_t) tile_image->rows; y++)
2075 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2076 &tile_image->exception);
2077 if (p == (const PixelPacket *) NULL)
2079 for (x=0; x < (ssize_t) tile_image->columns; x++)
2081 Ascii85Encode(image,
2082 ScaleQuantumToChar(PixelIntensityToQuantum(p)));
2086 Ascii85Flush(image);
2092 if ((tile_image->storage_class == DirectClass) ||
2093 (tile_image->colors > 256) || (compression == JPEGCompression) ||
2094 (compression == JPEG2000Compression))
2095 switch (compression)
2097 case JPEGCompression:
2099 status=InjectImageBlob(image_info,image,tile_image,"jpeg",
2101 if (status == MagickFalse)
2102 ThrowWriterException(CoderError,tile_image->exception.reason);
2105 case JPEG2000Compression:
2107 status=InjectImageBlob(image_info,image,tile_image,"jp2",
2109 if (status == MagickFalse)
2110 ThrowWriterException(CoderError,tile_image->exception.reason);
2113 case RLECompression:
2117 Allocate pixel array.
2119 length=(size_t) number_pixels;
2120 pixels=(unsigned char *) AcquireQuantumMemory(length,4*
2122 length*=tile_image->colorspace == CMYKColorspace ? 4UL : 3UL;
2123 if (pixels == (unsigned char *) NULL)
2125 tile_image=DestroyImage(tile_image);
2126 ThrowWriterException(ResourceLimitError,
2127 "MemoryAllocationFailed");
2130 Dump runoffset encoded pixels.
2133 for (y=0; y < (ssize_t) tile_image->rows; y++)
2135 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2136 &tile_image->exception);
2137 if (p == (const PixelPacket *) NULL)
2139 indexes=GetVirtualIndexQueue(tile_image);
2140 for (x=0; x < (ssize_t) tile_image->columns; x++)
2142 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
2143 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
2144 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
2145 if (image->colorspace == CMYKColorspace)
2146 *q++=ScaleQuantumToChar(indexes[x]);
2150 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2151 if (compression == ZipCompression)
2152 status=ZLIBEncodeImage(image,length,pixels);
2155 if (compression == LZWCompression)
2156 status=LZWEncodeImage(image,length,pixels);
2158 status=PackbitsEncodeImage(image,length,pixels);
2159 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2160 if (status == MagickFalse)
2162 (void) CloseBlob(image);
2163 return(MagickFalse);
2170 Dump uncompressed DirectColor packets.
2172 Ascii85Initialize(image);
2173 for (y=0; y < (ssize_t) tile_image->rows; y++)
2175 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2176 &tile_image->exception);
2177 if (p == (const PixelPacket *) NULL)
2179 indexes=GetVirtualIndexQueue(tile_image);
2180 for (x=0; x < (ssize_t) tile_image->columns; x++)
2182 Ascii85Encode(image,ScaleQuantumToChar(
2183 GetRedPixelComponent(p)));
2184 Ascii85Encode(image,ScaleQuantumToChar(
2185 GetGreenPixelComponent(p)));
2186 Ascii85Encode(image,ScaleQuantumToChar(
2187 GetBluePixelComponent(p)));
2188 if (image->colorspace == CMYKColorspace)
2189 Ascii85Encode(image,ScaleQuantumToChar(indexes[x]));
2193 Ascii85Flush(image);
2200 Dump number of colors and colormap.
2202 switch (compression)
2204 case RLECompression:
2208 Allocate pixel array.
2210 length=(size_t) number_pixels;
2211 pixels=(unsigned char *) AcquireQuantumMemory(length,
2213 if (pixels == (unsigned char *) NULL)
2215 tile_image=DestroyImage(tile_image);
2216 ThrowWriterException(ResourceLimitError,
2217 "MemoryAllocationFailed");
2220 Dump Runlength encoded pixels.
2223 for (y=0; y < (ssize_t) tile_image->rows; y++)
2225 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2226 &tile_image->exception);
2227 if (p == (const PixelPacket *) NULL)
2229 indexes=GetVirtualIndexQueue(tile_image);
2230 for (x=0; x < (ssize_t) tile_image->columns; x++)
2231 *q++=(unsigned char) indexes[x];
2233 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2234 if (compression == ZipCompression)
2235 status=ZLIBEncodeImage(image,length,pixels);
2238 if (compression == LZWCompression)
2239 status=LZWEncodeImage(image,length,pixels);
2241 status=PackbitsEncodeImage(image,length,pixels);
2242 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2243 if (status == MagickFalse)
2245 (void) CloseBlob(image);
2246 return(MagickFalse);
2253 Dump uncompressed PseudoColor packets.
2255 Ascii85Initialize(image);
2256 for (y=0; y < (ssize_t) tile_image->rows; y++)
2258 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2259 &tile_image->exception);
2260 if (p == (const PixelPacket *) NULL)
2262 indexes=GetVirtualIndexQueue(tile_image);
2263 for (x=0; x < (ssize_t) tile_image->columns; x++)
2264 Ascii85Encode(image,(unsigned char) indexes[x]);
2266 Ascii85Flush(image);
2271 tile_image=DestroyImage(tile_image);
2272 offset=TellBlob(image)-offset;
2273 (void) WriteBlobString(image,"\nendstream\n");
2274 (void) WriteBlobString(image,"endobj\n");
2276 Write Length object.
2278 xref[object++]=TellBlob(image);
2279 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2281 (void) WriteBlobString(image,buffer);
2282 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
2283 (void) WriteBlobString(image,buffer);
2284 (void) WriteBlobString(image,"endobj\n");
2285 xref[object++]=TellBlob(image);
2286 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2288 (void) WriteBlobString(image,buffer);
2289 if ((image->storage_class != DirectClass) && (image->colors <= 256) &&
2290 (compression != FaxCompression) && (compression != Group4Compression))
2293 Write Colormap object.
2295 (void) WriteBlobString(image,"<<\n");
2296 if (compression == NoCompression)
2297 (void) WriteBlobString(image,"/Filter [ /ASCII85Decode ]\n");
2298 (void) FormatMagickString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
2300 (void) WriteBlobString(image,buffer);
2301 (void) WriteBlobString(image,">>\n");
2302 (void) WriteBlobString(image,"stream\n");
2303 offset=TellBlob(image);
2304 if (compression == NoCompression)
2305 Ascii85Initialize(image);
2306 for (i=0; i < (ssize_t) image->colors; i++)
2308 if (compression == NoCompression)
2310 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].red));
2311 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].green));
2312 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].blue));
2315 (void) WriteBlobByte(image,
2316 ScaleQuantumToChar(image->colormap[i].red));
2317 (void) WriteBlobByte(image,
2318 ScaleQuantumToChar(image->colormap[i].green));
2319 (void) WriteBlobByte(image,
2320 ScaleQuantumToChar(image->colormap[i].blue));
2322 if (compression == NoCompression)
2323 Ascii85Flush(image);
2324 offset=TellBlob(image)-offset;
2325 (void) WriteBlobString(image,"\nendstream\n");
2327 (void) WriteBlobString(image,"endobj\n");
2329 Write Length object.
2331 xref[object++]=TellBlob(image);
2332 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2334 (void) WriteBlobString(image,buffer);
2335 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g\n",(double)
2337 (void) WriteBlobString(image,buffer);
2338 (void) WriteBlobString(image,"endobj\n");
2340 Write softmask object.
2342 xref[object++]=TellBlob(image);
2343 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2345 (void) WriteBlobString(image,buffer);
2346 (void) WriteBlobString(image,"<<\n");
2347 if (image->matte == MagickFalse)
2348 (void) WriteBlobString(image,">>\n");
2351 (void) WriteBlobString(image,"/Type /XObject\n");
2352 (void) WriteBlobString(image,"/Subtype /Image\n");
2353 (void) FormatMagickString(buffer,MaxTextExtent,"/Name /Ma%.20g\n",
2354 (double) image->scene);
2355 (void) WriteBlobString(image,buffer);
2356 switch (compression)
2360 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,
2364 case LZWCompression:
2366 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"LZWDecode");
2369 case ZipCompression:
2371 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,
2377 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,
2382 (void) WriteBlobString(image,buffer);
2383 (void) FormatMagickString(buffer,MaxTextExtent,"/Width %.20g\n",
2384 (double) image->columns);
2385 (void) WriteBlobString(image,buffer);
2386 (void) FormatMagickString(buffer,MaxTextExtent,"/Height %.20g\n",
2387 (double) image->rows);
2388 (void) WriteBlobString(image,buffer);
2389 (void) WriteBlobString(image,"/ColorSpace /DeviceGray\n");
2390 (void) FormatMagickString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
2391 (compression == FaxCompression) || (compression == Group4Compression)
2393 (void) WriteBlobString(image,buffer);
2394 (void) FormatMagickString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
2396 (void) WriteBlobString(image,buffer);
2397 (void) WriteBlobString(image,">>\n");
2398 (void) WriteBlobString(image,"stream\n");
2399 offset=TellBlob(image);
2400 number_pixels=(MagickSizeType) image->columns*image->rows;
2401 switch (compression)
2403 case RLECompression:
2407 Allocate pixel array.
2409 length=(size_t) number_pixels;
2410 pixels=(unsigned char *) AcquireQuantumMemory(length,
2412 if (pixels == (unsigned char *) NULL)
2414 image=DestroyImage(image);
2415 ThrowWriterException(ResourceLimitError,
2416 "MemoryAllocationFailed");
2419 Dump Runlength encoded pixels.
2422 for (y=0; y < (ssize_t) image->rows; y++)
2424 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
2425 if (p == (const PixelPacket *) NULL)
2427 for (x=0; x < (ssize_t) image->columns; x++)
2429 *q++=ScaleQuantumToChar((Quantum) (GetAlphaPixelComponent(p)));
2433 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2434 if (compression == ZipCompression)
2435 status=ZLIBEncodeImage(image,length,pixels);
2438 if (compression == LZWCompression)
2439 status=LZWEncodeImage(image,length,pixels);
2441 status=PackbitsEncodeImage(image,length,pixels);
2442 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2443 if (status == MagickFalse)
2445 (void) CloseBlob(image);
2446 return(MagickFalse);
2453 Dump uncompressed PseudoColor packets.
2455 Ascii85Initialize(image);
2456 for (y=0; y < (ssize_t) image->rows; y++)
2458 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
2459 if (p == (const PixelPacket *) NULL)
2461 for (x=0; x < (ssize_t) image->columns; x++)
2463 Ascii85Encode(image,ScaleQuantumToChar((Quantum) (QuantumRange-
2464 GetOpacityPixelComponent(p))));
2468 Ascii85Flush(image);
2472 offset=TellBlob(image)-offset;
2473 (void) WriteBlobString(image,"\nendstream\n");
2475 (void) WriteBlobString(image,"endobj\n");
2477 Write Length object.
2479 xref[object++]=TellBlob(image);
2480 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2482 (void) WriteBlobString(image,buffer);
2483 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
2484 (void) WriteBlobString(image,buffer);
2485 (void) WriteBlobString(image,"endobj\n");
2486 if (GetNextImageInList(image) == (Image *) NULL)
2488 image=SyncNextImageInList(image);
2489 status=SetImageProgress(image,SaveImagesTag,scene++,
2490 GetImageListLength(image));
2491 if (status == MagickFalse)
2493 } while (image_info->adjoin != MagickFalse);
2495 Write Metadata object.
2497 xref[object++]=TellBlob(image);
2499 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2501 (void) WriteBlobString(image,buffer);
2502 (void) WriteBlobString(image,"<<\n");
2503 GetPathComponent(image->filename,BasePath,basename);
2504 (void) FormatMagickString(buffer,MaxTextExtent,"/Title (%s)\n",
2505 EscapeParenthesis(basename));
2506 (void) WriteBlobString(image,buffer);
2507 seconds=time((time_t *) NULL);
2508 #if defined(MAGICKCORE_HAVE_LOCALTIME_R)
2509 (void) localtime_r(&seconds,&local_time);
2511 (void) memcpy(&local_time,localtime(&seconds),sizeof(local_time));
2513 (void) FormatMagickString(date,MaxTextExtent,"D:%04d%02d%02d%02d%02d%02d",
2514 local_time.tm_year+1900,local_time.tm_mon+1,local_time.tm_mday,
2515 local_time.tm_hour,local_time.tm_min,local_time.tm_sec);
2516 (void) FormatMagickString(buffer,MaxTextExtent,"/CreationDate (%s)\n",date);
2517 (void) WriteBlobString(image,buffer);
2518 (void) FormatMagickString(buffer,MaxTextExtent,"/ModDate (%s)\n",date);
2519 (void) WriteBlobString(image,buffer);
2520 (void) FormatMagickString(buffer,MaxTextExtent,"/Producer (%s)\n",
2521 EscapeParenthesis(GetMagickVersion((size_t *) NULL)));
2522 (void) WriteBlobString(image,buffer);
2523 (void) WriteBlobString(image,">>\n");
2524 (void) WriteBlobString(image,"endobj\n");
2528 offset=TellBlob(image)-xref[0]+10;
2529 (void) WriteBlobString(image,"xref\n");
2530 (void) FormatMagickString(buffer,MaxTextExtent,"0 %.20g\n",(double)
2532 (void) WriteBlobString(image,buffer);
2533 (void) WriteBlobString(image,"0000000000 65535 f \n");
2534 for (i=0; i < (ssize_t) object; i++)
2536 (void) FormatMagickString(buffer,MaxTextExtent,"%010lu 00000 n \n",
2537 (unsigned long) xref[i]);
2538 (void) WriteBlobString(image,buffer);
2540 (void) WriteBlobString(image,"trailer\n");
2541 (void) WriteBlobString(image,"<<\n");
2542 (void) FormatMagickString(buffer,MaxTextExtent,"/Size %.20g\n",(double)
2544 (void) WriteBlobString(image,buffer);
2545 (void) FormatMagickString(buffer,MaxTextExtent,"/Info %.20g 0 R\n",(double)
2547 (void) WriteBlobString(image,buffer);
2548 (void) FormatMagickString(buffer,MaxTextExtent,"/Root %.20g 0 R\n",(double)
2550 (void) WriteBlobString(image,buffer);
2551 (void) WriteBlobString(image,">>\n");
2552 (void) WriteBlobString(image,"startxref\n");
2553 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
2554 (void) WriteBlobString(image,buffer);
2555 (void) WriteBlobString(image,"%%EOF\n");
2556 xref=(MagickOffsetType *) RelinquishMagickMemory(xref);
2557 (void) CloseBlob(image);