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);
178 argv=StringToArgv(command,&argc);
179 status=(ghost_info->init_with_args)(interpreter,argc-1,argv+1);
181 status=(ghost_info->run_string)(interpreter,"systemdict /start get exec\n",
183 (ghost_info->exit)(interpreter);
184 (ghost_info->delete_instance)(interpreter);
185 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
186 NTGhostscriptUnLoadDLL();
188 for (i=0; i < (ssize_t) argc; i++)
189 argv[i]=DestroyString(argv[i]);
190 argv=(char **) RelinquishMagickMemory(argv);
191 if ((status != 0) && (status != -101))
196 message=GetExceptionMessage(errno);
197 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
198 "`%s': %s",command,message);
199 message=DestroyString(message);
200 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
201 "Ghostscript returns status %d, exit code %d",status,code);
206 status=SystemCommand(MagickFalse,verbose,command,exception);
207 return(status == 0 ? MagickTrue : MagickFalse);
212 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
220 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
222 % IsPDF() returns MagickTrue if the image format type, identified by the
223 % magick string, is PDF.
225 % The format of the IsPDF method is:
227 % MagickBooleanType IsPDF(const unsigned char *magick,const size_t offset)
229 % A description of each parameter follows:
231 % o magick: compare image format pattern against these bytes.
233 % o offset: Specifies the offset of the magick string.
236 static MagickBooleanType IsPDF(const unsigned char *magick,const size_t offset)
240 if (LocaleNCompare((const char *) magick,"%PDF-",5) == 0)
246 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
250 % R e a d P D F I m a g e %
254 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
256 % ReadPDFImage() reads a Portable Document Format image file and
257 % returns it. It allocates the memory necessary for the new Image structure
258 % and returns a pointer to the new image.
260 % The format of the ReadPDFImage method is:
262 % Image *ReadPDFImage(const ImageInfo *image_info,ExceptionInfo *exception)
264 % A description of each parameter follows:
266 % o image_info: the image info.
268 % o exception: return any errors or warnings in this structure.
272 static MagickBooleanType IsPDFRendered(const char *path)
280 if ((path == (const char *) NULL) || (*path == '\0'))
282 status=GetPathAttributes(path,&attributes);
283 if ((status != MagickFalse) && S_ISREG(attributes.st_mode) &&
284 (attributes.st_size > 0))
289 static Image *ReadPDFImage(const ImageInfo *image_info,ExceptionInfo *exception)
291 #define CropBox "CropBox"
292 #define DeviceCMYK "DeviceCMYK"
293 #define MediaBox "MediaBox"
294 #define RenderPostscriptText "Rendering Postscript... "
295 #define PDFRotate "Rotate"
296 #define SpotColor "Separation"
297 #define TrimBox "TrimBox"
298 #define PDFVersion "PDF-"
301 command[MaxTextExtent],
302 density[MaxTextExtent],
303 filename[MaxTextExtent],
304 geometry[MaxTextExtent],
305 options[MaxTextExtent],
306 input_filename[MaxTextExtent],
307 postscript_filename[MaxTextExtent];
365 assert(image_info != (const ImageInfo *) NULL);
366 assert(image_info->signature == MagickSignature);
367 if (image_info->debug != MagickFalse)
368 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
369 image_info->filename);
370 assert(exception != (ExceptionInfo *) NULL);
371 assert(exception->signature == MagickSignature);
375 image=AcquireImage(image_info);
376 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
377 if (status == MagickFalse)
379 image=DestroyImageList(image);
380 return((Image *) NULL);
382 status=AcquireUniqueSymbolicLink(image_info->filename,input_filename);
383 if (status == MagickFalse)
385 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
386 image_info->filename);
387 image=DestroyImageList(image);
388 return((Image *) NULL);
391 Set the page density.
393 delta.x=DefaultResolution;
394 delta.y=DefaultResolution;
395 if ((image->x_resolution == 0.0) || (image->y_resolution == 0.0))
397 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
398 image->x_resolution=geometry_info.rho;
399 image->y_resolution=geometry_info.sigma;
400 if ((flags & SigmaValue) == 0)
401 image->y_resolution=image->x_resolution;
404 Determine page geometry from the PDF media box.
406 cmyk=image_info->colorspace == CMYKColorspace ? MagickTrue : MagickFalse;
408 option=GetImageOption(image_info,"pdf:use-cropbox");
409 if (option != (const char *) NULL)
410 cropbox=IsMagickTrue(option);
412 option=GetImageOption(image_info,"pdf:use-trimbox");
413 if (option != (const char *) NULL)
414 trimbox=IsMagickTrue(option);
417 (void) ResetMagickMemory(&bounding_box,0,sizeof(bounding_box));
418 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
419 (void) ResetMagickMemory(&hires_bounds,0,sizeof(hires_bounds));
420 (void) ResetMagickMemory(&page,0,sizeof(page));
421 (void) ResetMagickMemory(command,0,sizeof(command));
426 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
434 if ((c != (int) '/') && (c != (int) '%') &&
435 ((size_t) (p-command) < (MaxTextExtent-1)))
439 if (LocaleNCompare(PDFRotate,command,strlen(PDFRotate)) == 0)
440 count=(ssize_t) sscanf(command,"Rotate %lf",&angle);
442 Is this a CMYK document?
444 if (LocaleNCompare(DeviceCMYK,command,strlen(DeviceCMYK)) == 0)
446 if (LocaleNCompare(SpotColor,command,strlen(SpotColor)) == 0)
450 property[MaxTextExtent],
459 (void) FormatMagickString(property,MaxTextExtent,"pdf:SpotColor-%.20g",
460 (double) spotcolor++);
462 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
464 if ((isspace(c) != 0) || (c == '/') || ((i+1) == MaxTextExtent))
469 value=AcquireString(name);
470 (void) SubstituteString(&value,"#20"," ");
471 (void) SetImageProperty(image,property,value);
472 value=DestroyString(value);
475 if (LocaleNCompare(PDFVersion,command,strlen(PDFVersion)) == 0)
476 (void) SetImageProperty(image,"pdf:Version",command);
478 if (cropbox != MagickFalse)
480 if (LocaleNCompare(CropBox,command,strlen(CropBox)) == 0)
483 Note region defined by crop box.
485 count=(ssize_t) sscanf(command,"CropBox [%lf %lf %lf %lf",
486 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
488 count=(ssize_t) sscanf(command,"CropBox[%lf %lf %lf %lf",
489 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
493 if (trimbox != MagickFalse)
495 if (LocaleNCompare(TrimBox,command,strlen(TrimBox)) == 0)
498 Note region defined by trim box.
500 count=(ssize_t) sscanf(command,"TrimBox [%lf %lf %lf %lf",
501 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
503 count=(ssize_t) sscanf(command,"TrimBox[%lf %lf %lf %lf",
504 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
508 if (LocaleNCompare(MediaBox,command,strlen(MediaBox)) == 0)
511 Note region defined by media box.
513 count=(ssize_t) sscanf(command,"MediaBox [%lf %lf %lf %lf",
514 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
516 count=(ssize_t) sscanf(command,"MediaBox[%lf %lf %lf %lf",
517 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
521 if (((bounds.x2 > hires_bounds.x2) && (bounds.y2 > hires_bounds.y2)) ||
522 ((hires_bounds.x2 == 0.0) && (hires_bounds.y2 == 0.0)))
525 Set PDF render geometry.
527 (void) FormatMagickString(geometry,MaxTextExtent,
528 "%gx%g%+.15g%+.15g",bounds.x2-bounds.x1,bounds.y2-bounds.y1,
529 bounds.x1,bounds.y1);
530 (void) SetImageProperty(image,"pdf:HiResBoundingBox",geometry);
531 page.width=(size_t) floor(bounds.x2-bounds.x1+0.5);
532 page.height=(size_t) floor(bounds.y2-bounds.y1+0.5);
536 (void) CloseBlob(image);
537 if ((fabs(angle) == 90.0) || (fabs(angle) == 270.0))
543 page.width=page.height;
546 if (image_info->colorspace == RGBColorspace)
549 Create Ghostscript control file.
551 file=AcquireUniqueFileResource(postscript_filename);
554 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
555 image_info->filename);
556 image=DestroyImage(image);
557 return((Image *) NULL);
559 count=write(file," ",1);
562 Render Postscript with the Ghostscript delegate.
564 if ((image_info->ping != MagickFalse) ||
565 (image_info->monochrome != MagickFalse))
566 delegate_info=GetDelegateInfo("ps:mono",(char *) NULL,exception);
568 if (cmyk != MagickFalse)
569 delegate_info=GetDelegateInfo("ps:cmyk",(char *) NULL,exception);
571 if (LocaleCompare(image_info->magick,"AI") == 0)
572 delegate_info=GetDelegateInfo("ps:alpha",(char *) NULL,exception);
574 delegate_info=GetDelegateInfo("ps:color",(char *) NULL,exception);
575 if (delegate_info == (const DelegateInfo *) NULL)
577 (void) RelinquishUniqueFileResource(postscript_filename);
578 image=DestroyImage(image);
579 return((Image *) NULL);
582 if (image_info->density != (char *) NULL)
584 flags=ParseGeometry(image_info->density,&geometry_info);
585 image->x_resolution=geometry_info.rho;
586 image->y_resolution=geometry_info.sigma;
587 if ((flags & SigmaValue) == 0)
588 image->y_resolution=image->x_resolution;
590 (void) FormatMagickString(density,MaxTextExtent,"%gx%g",
591 image->x_resolution,image->y_resolution);
592 if (image_info->page != (char *) NULL)
594 (void) ParseAbsoluteGeometry(image_info->page,&page);
595 page.width=(size_t) floor(page.width*image->x_resolution/delta.x+
597 page.height=(size_t) floor(page.height*image->y_resolution/delta.y+
599 (void) FormatMagickString(options,MaxTextExtent,"-g%.20gx%.20g ",
600 (double) page.width,(double) page.height);
602 if (cmyk != MagickFalse)
603 (void) ConcatenateMagickString(options,"-dUseCIEColor ",MaxTextExtent);
604 if (cropbox != MagickFalse)
605 (void) ConcatenateMagickString(options,"-dUseCropBox ",MaxTextExtent);
606 if (trimbox != MagickFalse)
607 (void) ConcatenateMagickString(options,"-dUseTrimBox ",MaxTextExtent);
608 read_info=CloneImageInfo(image_info);
609 *read_info->magick='\0';
610 if (read_info->number_scenes != 0)
613 pages[MaxTextExtent];
615 (void) FormatMagickString(pages,MaxTextExtent,"-dFirstPage=%.20g "
616 "-dLastPage=%.20g",(double) read_info->scene+1,(double)
617 (read_info->scene+read_info->number_scenes));
618 (void) ConcatenateMagickString(options,pages,MaxTextExtent);
619 read_info->number_scenes=0;
620 if (read_info->scenes != (char *) NULL)
621 *read_info->scenes='\0';
623 if (read_info->authenticate != (char *) NULL)
624 (void) FormatMagickString(options+strlen(options),MaxTextExtent,
625 " -sPDFPassword=%s",read_info->authenticate);
626 (void) CopyMagickString(filename,read_info->filename,MaxTextExtent);
627 (void) AcquireUniqueFilename(read_info->filename);
628 (void) FormatMagickString(command,MaxTextExtent,
629 GetDelegateCommands(delegate_info),
630 read_info->antialias != MagickFalse ? 4 : 1,
631 read_info->antialias != MagickFalse ? 4 : 1,density,options,
632 read_info->filename,postscript_filename,input_filename);
633 status=InvokePDFDelegate(read_info->verbose,command,exception);
634 pdf_image=(Image *) NULL;
635 if ((status != MagickFalse) &&
636 (IsPDFRendered(read_info->filename) != MagickFalse))
637 pdf_image=ReadImage(read_info,exception);
638 (void) RelinquishUniqueFileResource(postscript_filename);
639 (void) RelinquishUniqueFileResource(read_info->filename);
640 (void) RelinquishUniqueFileResource(input_filename);
641 read_info=DestroyImageInfo(read_info);
642 if (pdf_image == (Image *) NULL)
644 ThrowFileException(exception,DelegateError,"PostscriptDelegateFailed",
645 image_info->filename);
646 return((Image *) NULL);
648 if (LocaleCompare(pdf_image->magick,"BMP") == 0)
653 cmyk_image=ConsolidateCMYKImages(pdf_image,exception);
654 if (cmyk_image != (Image *) NULL)
656 pdf_image=DestroyImageList(pdf_image);
657 pdf_image=cmyk_image;
660 if (image_info->number_scenes != 0)
669 Add place holder images to meet the subimage specification requirement.
671 for (i=0; i < (ssize_t) image_info->scene; i++)
673 clone_image=CloneImage(pdf_image,1,1,MagickTrue,exception);
674 if (clone_image != (Image *) NULL)
675 PrependImageToList(&pdf_image,clone_image);
680 (void) CopyMagickString(pdf_image->filename,filename,MaxTextExtent);
681 pdf_image->page=page;
682 (void) CloneImageProfiles(pdf_image,image);
683 (void) CloneImageProperties(pdf_image,image);
684 next=SyncNextImageInList(pdf_image);
685 if (next != (Image *) NULL)
687 } while (next != (Image *) NULL);
688 image=DestroyImage(image);
690 for (next=GetFirstImageInList(pdf_image); next != (Image *) NULL; )
693 next=GetNextImageInList(next);
695 return(GetFirstImageInList(pdf_image));
699 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
703 % R e g i s t e r P D F I m a g e %
707 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
709 % RegisterPDFImage() adds properties for the PDF image format to
710 % the list of supported formats. The properties include the image format
711 % tag, a method to read and/or write the format, whether the format
712 % supports the saving of more than one frame to the same file or blob,
713 % whether the format supports native in-memory I/O, and a brief
714 % description of the format.
716 % The format of the RegisterPDFImage method is:
718 % size_t RegisterPDFImage(void)
721 ModuleExport size_t RegisterPDFImage(void)
726 entry=SetMagickInfo("AI");
727 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
728 entry->encoder=(EncodeImageHandler *) WritePDFImage;
729 entry->adjoin=MagickFalse;
730 entry->blob_support=MagickFalse;
731 entry->seekable_stream=MagickTrue;
732 entry->thread_support=EncoderThreadSupport;
733 entry->description=ConstantString("Adobe Illustrator CS2");
734 entry->module=ConstantString("PDF");
735 (void) RegisterMagickInfo(entry);
736 entry=SetMagickInfo("EPDF");
737 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
738 entry->encoder=(EncodeImageHandler *) WritePDFImage;
739 entry->adjoin=MagickFalse;
740 entry->blob_support=MagickFalse;
741 entry->seekable_stream=MagickTrue;
742 entry->thread_support=EncoderThreadSupport;
743 entry->description=ConstantString("Encapsulated Portable Document Format");
744 entry->module=ConstantString("PDF");
745 (void) RegisterMagickInfo(entry);
746 entry=SetMagickInfo("PDF");
747 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
748 entry->encoder=(EncodeImageHandler *) WritePDFImage;
749 entry->magick=(IsImageFormatHandler *) IsPDF;
750 entry->blob_support=MagickFalse;
751 entry->seekable_stream=MagickTrue;
752 entry->thread_support=EncoderThreadSupport;
753 entry->description=ConstantString("Portable Document Format");
754 entry->module=ConstantString("PDF");
755 (void) RegisterMagickInfo(entry);
756 entry=SetMagickInfo("PDFA");
757 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
758 entry->encoder=(EncodeImageHandler *) WritePDFImage;
759 entry->magick=(IsImageFormatHandler *) IsPDF;
760 entry->blob_support=MagickFalse;
761 entry->seekable_stream=MagickTrue;
762 entry->thread_support=EncoderThreadSupport;
763 entry->description=ConstantString("Portable Document Archive Format");
764 entry->module=ConstantString("PDF");
765 (void) RegisterMagickInfo(entry);
766 return(MagickImageCoderSignature);
770 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
774 % U n r e g i s t e r P D F I m a g e %
778 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
780 % UnregisterPDFImage() removes format registrations made by the
781 % PDF module from the list of supported formats.
783 % The format of the UnregisterPDFImage method is:
785 % UnregisterPDFImage(void)
788 ModuleExport void UnregisterPDFImage(void)
790 (void) UnregisterMagickInfo("AI");
791 (void) UnregisterMagickInfo("EPDF");
792 (void) UnregisterMagickInfo("PDF");
793 (void) UnregisterMagickInfo("PDFA");
797 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
801 % W r i t e P D F I m a g e %
805 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
807 % WritePDFImage() writes an image in the Portable Document image
810 % The format of the WritePDFImage method is:
812 % MagickBooleanType WritePDFImage(const ImageInfo *image_info,Image *image)
814 % A description of each parameter follows.
816 % o image_info: the image info.
818 % o image: The image.
822 static inline size_t MagickMax(const size_t x,const size_t y)
829 static inline size_t MagickMin(const size_t x,const size_t y)
836 static char *EscapeParenthesis(const char *text)
845 buffer[MaxTextExtent];
852 for (i=0; i < (ssize_t) MagickMin(strlen(text),(MaxTextExtent-escapes-1)); i++)
854 if ((text[i] == '(') || (text[i] == ')'))
865 static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
866 Image *image,Image *inject_image)
884 write_info=CloneImageInfo(image_info);
885 (void) CopyMagickString(write_info->filename,"GROUP4:",MaxTextExtent);
886 (void) CopyMagickString(write_info->magick,"GROUP4",MaxTextExtent);
887 group4_image=CloneImage(inject_image,0,0,MagickTrue,&image->exception);
888 if (group4_image == (Image *) NULL)
890 group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
892 group4_image=DestroyImage(group4_image);
893 if (group4 == (unsigned char *) NULL)
895 write_info=DestroyImageInfo(write_info);
896 if (WriteBlob(image,length,group4) != (ssize_t) length)
898 group4=(unsigned char *) RelinquishMagickMemory(group4);
902 static MagickBooleanType WritePDFImage(const ImageInfo *image_info,Image *image)
904 #define CFormat "/Filter [ /%s ]\n"
905 #define ObjectsPerImage 14
910 "<?xpacket begin=\"%s\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n"
911 "<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"
912 " <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n"
913 " <rdf:Description rdf:about=\"\"\n"
914 " xmlns:xap=\"http://ns.adobe.com/xap/1.0/\">\n"
915 " <xap:ModifyDate>%s</xap:ModifyDate>\n"
916 " <xap:CreateDate>%s</xap:CreateDate>\n"
917 " <xap:MetadataDate>%s</xap:MetadataDate>\n"
918 " <xap:CreatorTool>%s</xap:CreatorTool>\n"
919 " </rdf:Description>\n"
920 " <rdf:Description rdf:about=\"\"\n"
921 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"
922 " <dc:format>application/pdf</dc:format>\n"
923 " </rdf:Description>\n"
924 " <rdf:Description rdf:about=\"\"\n"
925 " xmlns:xapMM=\"http://ns.adobe.com/xap/1.0/mm/\">\n"
926 " <xapMM:DocumentID>uuid:6ec119d7-7982-4f56-808d-dfe64f5b35cf</xapMM:DocumentID>\n"
927 " <xapMM:InstanceID>uuid:a79b99b4-6235-447f-9f6c-ec18ef7555cb</xapMM:InstanceID>\n"
928 " </rdf:Description>\n"
929 " <rdf:Description rdf:about=\"\"\n"
930 " xmlns:pdf=\"http://ns.adobe.com/pdf/1.3/\">\n"
931 " <pdf:Producer>%s</pdf:Producer>\n"
932 " </rdf:Description>\n"
933 " <rdf:Description rdf:about=\"\"\n"
934 " xmlns:pdfaid=\"http://www.aiim.org/pdfa/ns/id/\">\n"
935 " <pdfaid:part>1</pdfaid:part>\n"
936 " <pdfaid:conformance>B</pdfaid:conformance>\n"
937 " </rdf:Description>\n"
940 "<?xpacket end=\"w\"?>\n"
942 XMPProfileMagick[4]= { (char) 0xef, (char) 0xbb, (char) 0xbf, (char) 0x00 };
945 buffer[MaxTextExtent],
948 page_geometry[MaxTextExtent];
994 register const IndexPacket
997 register const PixelPacket
1000 register unsigned char
1028 Open output image file.
1030 assert(image_info != (const ImageInfo *) NULL);
1031 assert(image_info->signature == MagickSignature);
1032 assert(image != (Image *) NULL);
1033 assert(image->signature == MagickSignature);
1034 if (image->debug != MagickFalse)
1035 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1036 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1037 if (status == MagickFalse)
1040 Allocate X ref memory.
1042 xref=(MagickOffsetType *) AcquireQuantumMemory(2048UL,sizeof(*xref));
1043 if (xref == (MagickOffsetType *) NULL)
1044 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1045 (void) ResetMagickMemory(xref,0,2048UL*sizeof(*xref));
1051 if (image_info->compression == JPEG2000Compression)
1052 version=(size_t) MagickMax(version,5);
1053 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
1054 if (next->matte != MagickFalse)
1055 version=(size_t) MagickMax(version,4);
1056 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1057 version=(size_t) MagickMax(version,6);
1058 (void) FormatMagickString(buffer,MaxTextExtent,"%%PDF-1.%.20g \n",
1060 (void) WriteBlobString(image,buffer);
1061 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1062 (void) WriteBlobString(image,"%âãÏÓ\n");
1064 Write Catalog object.
1066 xref[object++]=TellBlob(image);
1068 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1070 (void) WriteBlobString(image,buffer);
1071 (void) WriteBlobString(image,"<<\n");
1072 if (LocaleCompare(image_info->magick,"PDFA") != 0)
1073 (void) FormatMagickString(buffer,MaxTextExtent,"/Pages %.20g 0 R\n",
1077 (void) FormatMagickString(buffer,MaxTextExtent,"/Metadata %.20g 0 R\n",
1079 (void) WriteBlobString(image,buffer);
1080 (void) FormatMagickString(buffer,MaxTextExtent,"/Pages %.20g 0 R\n",
1083 (void) WriteBlobString(image,buffer);
1084 (void) WriteBlobString(image,"/Type /Catalog\n");
1085 (void) WriteBlobString(image,">>\n");
1086 (void) WriteBlobString(image,"endobj\n");
1087 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1090 create_date[MaxTextExtent],
1091 modify_date[MaxTextExtent],
1092 timestamp[MaxTextExtent],
1093 xmp_profile[MaxTextExtent];
1101 xref[object++]=TellBlob(image);
1102 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",
1104 (void) WriteBlobString(image,buffer);
1105 (void) WriteBlobString(image,"<<\n");
1106 (void) WriteBlobString(image,"/Subtype /XML\n");
1108 value=GetImageProperty(image,"date:modify");
1109 if (value != (const char *) NULL)
1110 (void) CopyMagickString(modify_date,value,MaxTextExtent);
1112 value=GetImageProperty(image,"date:create");
1113 if (value != (const char *) NULL)
1114 (void) CopyMagickString(create_date,value,MaxTextExtent);
1115 (void) FormatMagickTime(time((time_t *) NULL),MaxTextExtent,timestamp);
1116 i=FormatMagickString(xmp_profile,MaxTextExtent,XMPProfile,
1117 XMPProfileMagick,modify_date,create_date,timestamp,
1118 GetMagickVersion(&version),GetMagickVersion(&version));
1119 (void) FormatMagickString(buffer,MaxTextExtent,"/Length %.20g\n",
1121 (void) WriteBlobString(image,buffer);
1122 (void) WriteBlobString(image,"/Type /Metadata\n");
1123 (void) WriteBlobString(image,">>\nstream\n");
1124 (void) WriteBlobString(image,xmp_profile);
1125 (void) WriteBlobString(image,"endstream\n");
1126 (void) WriteBlobString(image,"endobj\n");
1131 xref[object++]=TellBlob(image);
1133 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1135 (void) WriteBlobString(image,buffer);
1136 (void) WriteBlobString(image,"<<\n");
1137 (void) WriteBlobString(image,"/Type /Pages\n");
1138 (void) FormatMagickString(buffer,MaxTextExtent,"/Kids [ %.20g 0 R ",
1140 (void) WriteBlobString(image,buffer);
1141 count=(ssize_t) (pages_id+ObjectsPerImage+1);
1142 if (image_info->adjoin != MagickFalse)
1148 Predict page object id's.
1151 for ( ; GetNextImageInList(kid_image) != (Image *) NULL; count+=ObjectsPerImage)
1153 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 R ",(double)
1155 (void) WriteBlobString(image,buffer);
1156 kid_image=GetNextImageInList(kid_image);
1158 xref=(MagickOffsetType *) ResizeQuantumMemory(xref,(size_t) count+2048UL,
1160 if (xref == (MagickOffsetType *) NULL)
1161 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1163 (void) WriteBlobString(image,"]\n");
1164 (void) FormatMagickString(buffer,MaxTextExtent,"/Count %.20g\n",
1165 (double) ((count-pages_id)/ObjectsPerImage));
1166 (void) WriteBlobString(image,buffer);
1167 (void) WriteBlobString(image,">>\n");
1168 (void) WriteBlobString(image,"endobj\n");
1172 compression=image->compression;
1173 if (image_info->compression != UndefinedCompression)
1174 compression=image_info->compression;
1175 switch (compression)
1177 case FaxCompression:
1178 case Group4Compression:
1180 if ((IsMonochromeImage(image,&image->exception) == MagickFalse) ||
1181 (image->matte != MagickFalse))
1182 compression=RLECompression;
1185 #if !defined(MAGICKCORE_JPEG_DELEGATE)
1186 case JPEGCompression:
1188 compression=RLECompression;
1189 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1190 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
1195 #if !defined(MAGICKCORE_JP2_DELEGATE)
1196 case JPEG2000Compression:
1198 compression=RLECompression;
1199 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1200 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JP2)",
1205 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
1206 case ZipCompression:
1208 compression=RLECompression;
1209 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1210 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZLIB)",
1215 case LZWCompression:
1217 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1218 compression=RLECompression; /* LZW compression is forbidden */
1223 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1224 compression=RLECompression; /* ASCII 85 compression is forbidden */
1230 if (compression == JPEG2000Compression)
1232 if (image->colorspace != RGBColorspace)
1233 (void) TransformImageColorspace(image,RGBColorspace);
1236 Scale relative to dots-per-inch.
1238 delta.x=DefaultResolution;
1239 delta.y=DefaultResolution;
1240 resolution.x=image->x_resolution;
1241 resolution.y=image->y_resolution;
1242 if ((resolution.x == 0.0) || (resolution.y == 0.0))
1244 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
1245 resolution.x=geometry_info.rho;
1246 resolution.y=geometry_info.sigma;
1247 if ((flags & SigmaValue) == 0)
1248 resolution.y=resolution.x;
1250 if (image_info->density != (char *) NULL)
1252 flags=ParseGeometry(image_info->density,&geometry_info);
1253 resolution.x=geometry_info.rho;
1254 resolution.y=geometry_info.sigma;
1255 if ((flags & SigmaValue) == 0)
1256 resolution.y=resolution.x;
1258 if (image->units == PixelsPerCentimeterResolution)
1260 resolution.x=(size_t) (100.0*2.54*resolution.x+0.5)/100.0;
1261 resolution.y=(size_t) (100.0*2.54*resolution.y+0.5)/100.0;
1263 SetGeometry(image,&geometry);
1264 (void) FormatMagickString(page_geometry,MaxTextExtent,"%.20gx%.20g",
1265 (double) image->columns,(double) image->rows);
1266 if (image_info->page != (char *) NULL)
1267 (void) CopyMagickString(page_geometry,image_info->page,MaxTextExtent);
1269 if ((image->page.width != 0) && (image->page.height != 0))
1270 (void) FormatMagickString(page_geometry,MaxTextExtent,
1271 "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,
1272 (double) image->page.height,(double) image->page.x,(double)
1275 if ((image->gravity != UndefinedGravity) &&
1276 (LocaleCompare(image_info->magick,"PDF") == 0))
1277 (void) CopyMagickString(page_geometry,PSPageGeometry,MaxTextExtent);
1278 (void) ConcatenateMagickString(page_geometry,">",MaxTextExtent);
1279 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
1280 &geometry.width,&geometry.height);
1281 scale.x=(double) (geometry.width*delta.x)/resolution.x;
1282 geometry.width=(size_t) floor(scale.x+0.5);
1283 scale.y=(double) (geometry.height*delta.y)/resolution.y;
1284 geometry.height=(size_t) floor(scale.y+0.5);
1285 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
1286 (void) ParseGravityGeometry(image,page_geometry,&page_info,
1288 if (image->gravity != UndefinedGravity)
1290 geometry.x=(-page_info.x);
1291 geometry.y=(ssize_t) (media_info.height+page_info.y-image->rows);
1294 if (image_info->pointsize != 0.0)
1295 pointsize=image_info->pointsize;
1297 value=GetImageProperty(image,"Label");
1298 if (value != (const char *) NULL)
1299 text_size=(size_t) (MultilineCensus(value)*pointsize+12);
1303 xref[object++]=TellBlob(image);
1304 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1306 (void) WriteBlobString(image,buffer);
1307 (void) WriteBlobString(image,"<<\n");
1308 (void) WriteBlobString(image,"/Type /Page\n");
1309 (void) FormatMagickString(buffer,MaxTextExtent,"/Parent %.20g 0 R\n",
1311 (void) WriteBlobString(image,buffer);
1312 (void) WriteBlobString(image,"/Resources <<\n");
1313 labels=(char **) NULL;
1314 value=GetImageProperty(image,"Label");
1315 if (value != (const char *) NULL)
1316 labels=StringToList(value);
1317 if (labels != (char **) NULL)
1319 (void) FormatMagickString(buffer,MaxTextExtent,
1320 "/Font << /F%.20g %.20g 0 R >>\n",(double) image->scene,(double)
1322 (void) WriteBlobString(image,buffer);
1324 (void) FormatMagickString(buffer,MaxTextExtent,
1325 "/XObject << /Im%.20g %.20g 0 R >>\n",(double) image->scene,(double)
1327 (void) WriteBlobString(image,buffer);
1328 (void) FormatMagickString(buffer,MaxTextExtent,"/ProcSet %.20g 0 R >>\n",
1330 (void) WriteBlobString(image,buffer);
1331 (void) FormatMagickString(buffer,MaxTextExtent,
1332 "/MediaBox [0 0 %g %g]\n",72.0*media_info.width/resolution.x,
1333 72.0*media_info.height/resolution.y);
1334 (void) WriteBlobString(image,buffer);
1335 (void) FormatMagickString(buffer,MaxTextExtent,
1336 "/CropBox [0 0 %g %g]\n",72.0*media_info.width/resolution.x,
1337 72.0*media_info.height/resolution.y);
1338 (void) WriteBlobString(image,buffer);
1339 (void) FormatMagickString(buffer,MaxTextExtent,"/Contents %.20g 0 R\n",
1341 (void) WriteBlobString(image,buffer);
1342 (void) FormatMagickString(buffer,MaxTextExtent,"/Thumb %.20g 0 R\n",
1344 (void) WriteBlobString(image,buffer);
1345 (void) WriteBlobString(image,">>\n");
1346 (void) WriteBlobString(image,"endobj\n");
1348 Write Contents object.
1350 xref[object++]=TellBlob(image);
1351 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1353 (void) WriteBlobString(image,buffer);
1354 (void) WriteBlobString(image,"<<\n");
1355 (void) FormatMagickString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
1357 (void) WriteBlobString(image,buffer);
1358 (void) WriteBlobString(image,">>\n");
1359 (void) WriteBlobString(image,"stream\n");
1360 offset=TellBlob(image);
1361 (void) WriteBlobString(image,"q\n");
1362 if (labels != (char **) NULL)
1363 for (i=0; labels[i] != (char *) NULL; i++)
1365 (void) WriteBlobString(image,"BT\n");
1366 (void) FormatMagickString(buffer,MaxTextExtent,"/F%.20g %g Tf\n",
1367 (double) image->scene,pointsize);
1368 (void) WriteBlobString(image,buffer);
1369 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g %.20g Td\n",
1370 (double) geometry.x,(double) (geometry.y+geometry.height+i*pointsize+
1372 (void) WriteBlobString(image,buffer);
1373 (void) FormatMagickString(buffer,MaxTextExtent,"(%s) Tj\n",labels[i]);
1374 (void) WriteBlobString(image,buffer);
1375 (void) WriteBlobString(image,"ET\n");
1376 labels[i]=DestroyString(labels[i]);
1378 (void) FormatMagickString(buffer,MaxTextExtent,"%g 0 0 %g %.20g %.20g cm\n",
1379 scale.x,scale.y,(double) geometry.x,(double) geometry.y);
1380 (void) WriteBlobString(image,buffer);
1381 (void) FormatMagickString(buffer,MaxTextExtent,"/Im%.20g Do\n",
1382 (double) image->scene);
1383 (void) WriteBlobString(image,buffer);
1384 (void) WriteBlobString(image,"Q\n");
1385 offset=TellBlob(image)-offset;
1386 (void) WriteBlobString(image,"endstream\n");
1387 (void) WriteBlobString(image,"endobj\n");
1389 Write Length object.
1391 xref[object++]=TellBlob(image);
1392 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1394 (void) WriteBlobString(image,buffer);
1395 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
1396 (void) WriteBlobString(image,buffer);
1397 (void) WriteBlobString(image,"endobj\n");
1399 Write Procset object.
1401 xref[object++]=TellBlob(image);
1402 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",
1404 (void) WriteBlobString(image,buffer);
1405 if ((image->storage_class == DirectClass) || (image->colors > 256))
1406 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageC",MaxTextExtent);
1408 if ((compression == FaxCompression) || (compression == Group4Compression))
1409 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageB",MaxTextExtent);
1411 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageI",MaxTextExtent);
1412 (void) WriteBlobString(image,buffer);
1413 (void) WriteBlobString(image," ]\n");
1414 (void) WriteBlobString(image,"endobj\n");
1418 xref[object++]=TellBlob(image);
1419 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",
1421 (void) WriteBlobString(image,buffer);
1422 (void) WriteBlobString(image,"<<\n");
1423 if (labels != (char **) NULL)
1425 (void) WriteBlobString(image,"/Type /Font\n");
1426 (void) WriteBlobString(image,"/Subtype /Type1\n");
1427 (void) FormatMagickString(buffer,MaxTextExtent,"/Name /F%.20g\n",
1428 (double) image->scene);
1429 (void) WriteBlobString(image,buffer);
1430 (void) WriteBlobString(image,"/BaseFont /Helvetica\n");
1431 (void) WriteBlobString(image,"/Encoding /MacRomanEncoding\n");
1432 labels=(char **) RelinquishMagickMemory(labels);
1434 (void) WriteBlobString(image,">>\n");
1435 (void) WriteBlobString(image,"endobj\n");
1437 Write XObject object.
1439 xref[object++]=TellBlob(image);
1440 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1442 (void) WriteBlobString(image,buffer);
1443 (void) WriteBlobString(image,"<<\n");
1444 (void) WriteBlobString(image,"/Type /XObject\n");
1445 (void) WriteBlobString(image,"/Subtype /Image\n");
1446 (void) FormatMagickString(buffer,MaxTextExtent,"/Name /Im%.20g\n",
1447 (double) image->scene);
1448 (void) WriteBlobString(image,buffer);
1449 switch (compression)
1453 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"ASCII85Decode");
1456 case JPEGCompression:
1458 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"DCTDecode");
1459 if (image->colorspace != CMYKColorspace)
1461 (void) WriteBlobString(image,buffer);
1462 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1466 case JPEG2000Compression:
1468 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"JPXDecode");
1469 if (image->colorspace != CMYKColorspace)
1471 (void) WriteBlobString(image,buffer);
1472 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1476 case LZWCompression:
1478 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"LZWDecode");
1481 case ZipCompression:
1483 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"FlateDecode");
1486 case FaxCompression:
1487 case Group4Compression:
1489 (void) CopyMagickString(buffer,"/Filter [ /CCITTFaxDecode ]\n",
1491 (void) WriteBlobString(image,buffer);
1492 (void) FormatMagickString(buffer,MaxTextExtent,"/DecodeParms [ << "
1493 "/K %s /BlackIs1 false /Columns %.20g /Rows %.20g >> ]\n",CCITTParam,
1494 (double) image->columns,(double) image->rows);
1499 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,
1504 (void) WriteBlobString(image,buffer);
1505 (void) FormatMagickString(buffer,MaxTextExtent,"/Width %.20g\n",(double)
1507 (void) WriteBlobString(image,buffer);
1508 (void) FormatMagickString(buffer,MaxTextExtent,"/Height %.20g\n",(double)
1510 (void) WriteBlobString(image,buffer);
1511 (void) FormatMagickString(buffer,MaxTextExtent,"/ColorSpace %.20g 0 R\n",
1513 (void) WriteBlobString(image,buffer);
1514 (void) FormatMagickString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
1515 (compression == FaxCompression) || (compression == Group4Compression) ?
1517 (void) WriteBlobString(image,buffer);
1518 if (image->matte != MagickFalse)
1520 (void) FormatMagickString(buffer,MaxTextExtent,"/SMask %.20g 0 R\n",
1522 (void) WriteBlobString(image,buffer);
1524 (void) FormatMagickString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
1526 (void) WriteBlobString(image,buffer);
1527 (void) WriteBlobString(image,">>\n");
1528 (void) WriteBlobString(image,"stream\n");
1529 offset=TellBlob(image);
1530 number_pixels=(MagickSizeType) image->columns*image->rows;
1531 if ((4*number_pixels) != (MagickSizeType) ((size_t) (4*number_pixels)))
1532 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1533 if ((compression == FaxCompression) || (compression == Group4Compression) ||
1534 ((image_info->type != TrueColorType) &&
1535 (IsGrayImage(image,&image->exception) != MagickFalse)))
1537 switch (compression)
1539 case FaxCompression:
1540 case Group4Compression:
1542 if (LocaleCompare(CCITTParam,"0") == 0)
1544 (void) HuffmanEncodeImage(image_info,image,image);
1547 (void) Huffman2DEncodeImage(image_info,image,image);
1550 case JPEGCompression:
1552 status=InjectImageBlob(image_info,image,image,"jpeg",
1554 if (status == MagickFalse)
1555 ThrowWriterException(CoderError,image->exception.reason);
1558 case JPEG2000Compression:
1560 status=InjectImageBlob(image_info,image,image,"jp2",
1562 if (status == MagickFalse)
1563 ThrowWriterException(CoderError,image->exception.reason);
1566 case RLECompression:
1570 Allocate pixel array.
1572 length=(size_t) number_pixels;
1573 pixels=(unsigned char *) AcquireQuantumMemory(length,
1575 if (pixels == (unsigned char *) NULL)
1576 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1578 Dump Runlength encoded pixels.
1581 for (y=0; y < (ssize_t) image->rows; y++)
1583 p=GetVirtualPixels(image,0,y,image->columns,1,
1585 if (p == (const PixelPacket *) NULL)
1587 for (x=0; x < (ssize_t) image->columns; x++)
1589 *q++=ScaleQuantumToChar(PixelIntensityToQuantum(p));
1592 if (image->previous == (Image *) NULL)
1594 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1596 if (status == MagickFalse)
1600 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1601 if (compression == ZipCompression)
1602 status=ZLIBEncodeImage(image,length,pixels);
1605 if (compression == LZWCompression)
1606 status=LZWEncodeImage(image,length,pixels);
1608 status=PackbitsEncodeImage(image,length,pixels);
1609 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1610 if (status == MagickFalse)
1612 (void) CloseBlob(image);
1613 return(MagickFalse);
1620 Dump uncompressed PseudoColor packets.
1622 Ascii85Initialize(image);
1623 for (y=0; y < (ssize_t) image->rows; y++)
1625 p=GetVirtualPixels(image,0,y,image->columns,1,
1627 if (p == (const PixelPacket *) NULL)
1629 for (x=0; x < (ssize_t) image->columns; x++)
1631 Ascii85Encode(image,
1632 ScaleQuantumToChar(PixelIntensityToQuantum(p)));
1635 if (image->previous == (Image *) NULL)
1637 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1639 if (status == MagickFalse)
1643 Ascii85Flush(image);
1649 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1650 (compression == JPEGCompression) ||
1651 (compression == JPEG2000Compression))
1652 switch (compression)
1654 case JPEGCompression:
1656 status=InjectImageBlob(image_info,image,image,"jpeg",
1658 if (status == MagickFalse)
1659 ThrowWriterException(CoderError,image->exception.reason);
1662 case JPEG2000Compression:
1664 status=InjectImageBlob(image_info,image,image,"jp2",
1666 if (status == MagickFalse)
1667 ThrowWriterException(CoderError,image->exception.reason);
1670 case RLECompression:
1674 Allocate pixel array.
1676 length=(size_t) number_pixels;
1677 pixels=(unsigned char *) AcquireQuantumMemory(length,
1679 length*=image->colorspace == CMYKColorspace ? 4UL : 3UL;
1680 if (pixels == (unsigned char *) NULL)
1681 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1683 Dump runoffset encoded pixels.
1686 for (y=0; y < (ssize_t) image->rows; y++)
1688 p=GetVirtualPixels(image,0,y,image->columns,1,
1690 if (p == (const PixelPacket *) NULL)
1692 indexes=GetVirtualIndexQueue(image);
1693 for (x=0; x < (ssize_t) image->columns; x++)
1695 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
1696 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1697 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
1698 if (image->colorspace == CMYKColorspace)
1699 *q++=ScaleQuantumToChar(indexes[x]);
1702 if (image->previous == (Image *) NULL)
1704 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1706 if (status == MagickFalse)
1710 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1711 if (compression == ZipCompression)
1712 status=ZLIBEncodeImage(image,length,pixels);
1715 if (compression == LZWCompression)
1716 status=LZWEncodeImage(image,length,pixels);
1718 status=PackbitsEncodeImage(image,length,pixels);
1719 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1720 if (status == MagickFalse)
1722 (void) CloseBlob(image);
1723 return(MagickFalse);
1730 Dump uncompressed DirectColor packets.
1732 Ascii85Initialize(image);
1733 for (y=0; y < (ssize_t) image->rows; y++)
1735 p=GetVirtualPixels(image,0,y,image->columns,1,
1737 if (p == (const PixelPacket *) NULL)
1739 indexes=GetVirtualIndexQueue(image);
1740 for (x=0; x < (ssize_t) image->columns; x++)
1742 Ascii85Encode(image,ScaleQuantumToChar(GetRedPixelComponent(p)));
1743 Ascii85Encode(image,ScaleQuantumToChar(GetGreenPixelComponent(p)));
1744 Ascii85Encode(image,ScaleQuantumToChar(GetBluePixelComponent(p)));
1745 if (image->colorspace == CMYKColorspace)
1746 Ascii85Encode(image,ScaleQuantumToChar(indexes[x]));
1749 if (image->previous == (Image *) NULL)
1751 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1753 if (status == MagickFalse)
1757 Ascii85Flush(image);
1764 Dump number of colors and colormap.
1766 switch (compression)
1768 case RLECompression:
1772 Allocate pixel array.
1774 length=(size_t) number_pixels;
1775 pixels=(unsigned char *) AcquireQuantumMemory(length,
1777 if (pixels == (unsigned char *) NULL)
1778 ThrowWriterException(ResourceLimitError,
1779 "MemoryAllocationFailed");
1781 Dump Runlength encoded pixels.
1784 for (y=0; y < (ssize_t) image->rows; y++)
1786 p=GetVirtualPixels(image,0,y,image->columns,1,
1788 if (p == (const PixelPacket *) NULL)
1790 indexes=GetVirtualIndexQueue(image);
1791 for (x=0; x < (ssize_t) image->columns; x++)
1792 *q++=(unsigned char) indexes[x];
1793 if (image->previous == (Image *) NULL)
1795 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1797 if (status == MagickFalse)
1801 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1802 if (compression == ZipCompression)
1803 status=ZLIBEncodeImage(image,length,pixels);
1806 if (compression == LZWCompression)
1807 status=LZWEncodeImage(image,length,pixels);
1809 status=PackbitsEncodeImage(image,length,pixels);
1810 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1811 if (status == MagickFalse)
1813 (void) CloseBlob(image);
1814 return(MagickFalse);
1821 Dump uncompressed PseudoColor packets.
1823 Ascii85Initialize(image);
1824 for (y=0; y < (ssize_t) image->rows; y++)
1826 p=GetVirtualPixels(image,0,y,image->columns,1,
1828 if (p == (const PixelPacket *) NULL)
1830 indexes=GetVirtualIndexQueue(image);
1831 for (x=0; x < (ssize_t) image->columns; x++)
1832 Ascii85Encode(image,(unsigned char) indexes[x]);
1833 if (image->previous == (Image *) NULL)
1835 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1837 if (status == MagickFalse)
1841 Ascii85Flush(image);
1846 offset=TellBlob(image)-offset;
1847 (void) WriteBlobString(image,"\nendstream\n");
1848 (void) WriteBlobString(image,"endobj\n");
1850 Write Length object.
1852 xref[object++]=TellBlob(image);
1853 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1855 (void) WriteBlobString(image,buffer);
1856 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g\n",
1858 (void) WriteBlobString(image,buffer);
1859 (void) WriteBlobString(image,"endobj\n");
1861 Write Colorspace object.
1863 xref[object++]=TellBlob(image);
1864 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1866 (void) WriteBlobString(image,buffer);
1867 if (image->colorspace == CMYKColorspace)
1868 (void) CopyMagickString(buffer,"/DeviceCMYK\n",MaxTextExtent);
1870 if ((compression == FaxCompression) ||
1871 (compression == Group4Compression) ||
1872 ((image_info->type != TrueColorType) &&
1873 (IsGrayImage(image,&image->exception) != MagickFalse)))
1874 (void) CopyMagickString(buffer,"/DeviceGray\n",MaxTextExtent);
1876 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1877 (compression == JPEGCompression) ||
1878 (compression == JPEG2000Compression))
1879 (void) CopyMagickString(buffer,"/DeviceRGB\n",MaxTextExtent);
1881 (void) FormatMagickString(buffer,MaxTextExtent,
1882 "[ /Indexed /DeviceRGB %.20g %.20g 0 R ]\n",(double) image->colors-
1883 1,(double) object+3);
1884 (void) WriteBlobString(image,buffer);
1885 (void) WriteBlobString(image,"endobj\n");
1889 SetGeometry(image,&geometry);
1890 (void) ParseMetaGeometry("106x106+0+0>",&geometry.x,&geometry.y,
1891 &geometry.width,&geometry.height);
1892 tile_image=ThumbnailImage(image,geometry.width,geometry.height,
1894 if (tile_image == (Image *) NULL)
1895 ThrowWriterException(ResourceLimitError,image->exception.reason);
1896 xref[object++]=TellBlob(image);
1897 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1899 (void) WriteBlobString(image,buffer);
1900 (void) WriteBlobString(image,"<<\n");
1901 switch (compression)
1905 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"ASCII85Decode");
1908 case JPEGCompression:
1910 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"DCTDecode");
1911 if (image->colorspace != CMYKColorspace)
1913 (void) WriteBlobString(image,buffer);
1914 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1918 case JPEG2000Compression:
1920 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"JPXDecode");
1921 if (image->colorspace != CMYKColorspace)
1923 (void) WriteBlobString(image,buffer);
1924 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1928 case LZWCompression:
1930 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"LZWDecode");
1933 case ZipCompression:
1935 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"FlateDecode");
1938 case FaxCompression:
1939 case Group4Compression:
1941 (void) CopyMagickString(buffer,"/Filter [ /CCITTFaxDecode ]\n",
1943 (void) WriteBlobString(image,buffer);
1944 (void) FormatMagickString(buffer,MaxTextExtent,"/DecodeParms [ << "
1945 "/K %s /BlackIs1 false /Columns %.20g /Rows %.20g >> ]\n",CCITTParam,
1946 (double) tile_image->columns,(double) tile_image->rows);
1951 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,
1956 (void) WriteBlobString(image,buffer);
1957 (void) FormatMagickString(buffer,MaxTextExtent,"/Width %.20g\n",(double)
1958 tile_image->columns);
1959 (void) WriteBlobString(image,buffer);
1960 (void) FormatMagickString(buffer,MaxTextExtent,"/Height %.20g\n",(double)
1962 (void) WriteBlobString(image,buffer);
1963 (void) FormatMagickString(buffer,MaxTextExtent,"/ColorSpace %.20g 0 R\n",
1965 (void) WriteBlobString(image,buffer);
1966 (void) FormatMagickString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
1967 (compression == FaxCompression) || (compression == Group4Compression) ?
1969 (void) WriteBlobString(image,buffer);
1970 (void) FormatMagickString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
1972 (void) WriteBlobString(image,buffer);
1973 (void) WriteBlobString(image,">>\n");
1974 (void) WriteBlobString(image,"stream\n");
1975 offset=TellBlob(image);
1976 number_pixels=(MagickSizeType) tile_image->columns*tile_image->rows;
1977 if ((compression == FaxCompression) ||
1978 (compression == Group4Compression) ||
1979 ((image_info->type != TrueColorType) &&
1980 (IsGrayImage(tile_image,&image->exception) != MagickFalse)))
1982 switch (compression)
1984 case FaxCompression:
1985 case Group4Compression:
1987 if (LocaleCompare(CCITTParam,"0") == 0)
1989 (void) HuffmanEncodeImage(image_info,image,tile_image);
1992 (void) Huffman2DEncodeImage(image_info,image,tile_image);
1995 case JPEGCompression:
1997 status=InjectImageBlob(image_info,image,tile_image,"jpeg",
1999 if (status == MagickFalse)
2000 ThrowWriterException(CoderError,tile_image->exception.reason);
2003 case JPEG2000Compression:
2005 status=InjectImageBlob(image_info,image,tile_image,"jp2",
2007 if (status == MagickFalse)
2008 ThrowWriterException(CoderError,tile_image->exception.reason);
2011 case RLECompression:
2015 Allocate pixel array.
2017 length=(size_t) number_pixels;
2018 pixels=(unsigned char *) AcquireQuantumMemory(length,
2020 if (pixels == (unsigned char *) NULL)
2022 tile_image=DestroyImage(tile_image);
2023 ThrowWriterException(ResourceLimitError,
2024 "MemoryAllocationFailed");
2027 Dump Runlength encoded pixels.
2030 for (y=0; y < (ssize_t) tile_image->rows; y++)
2032 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2033 &tile_image->exception);
2034 if (p == (const PixelPacket *) NULL)
2036 for (x=0; x < (ssize_t) tile_image->columns; x++)
2038 *q++=ScaleQuantumToChar(PixelIntensityToQuantum(p));
2042 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2043 if (compression == ZipCompression)
2044 status=ZLIBEncodeImage(image,length,pixels);
2047 if (compression == LZWCompression)
2048 status=LZWEncodeImage(image,length,pixels);
2050 status=PackbitsEncodeImage(image,length,pixels);
2051 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2052 if (status == MagickFalse)
2054 (void) CloseBlob(image);
2055 return(MagickFalse);
2062 Dump uncompressed PseudoColor packets.
2064 Ascii85Initialize(image);
2065 for (y=0; y < (ssize_t) tile_image->rows; y++)
2067 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2068 &tile_image->exception);
2069 if (p == (const PixelPacket *) NULL)
2071 for (x=0; x < (ssize_t) tile_image->columns; x++)
2073 Ascii85Encode(image,
2074 ScaleQuantumToChar(PixelIntensityToQuantum(p)));
2078 Ascii85Flush(image);
2084 if ((tile_image->storage_class == DirectClass) ||
2085 (tile_image->colors > 256) || (compression == JPEGCompression) ||
2086 (compression == JPEG2000Compression))
2087 switch (compression)
2089 case JPEGCompression:
2091 status=InjectImageBlob(image_info,image,tile_image,"jpeg",
2093 if (status == MagickFalse)
2094 ThrowWriterException(CoderError,tile_image->exception.reason);
2097 case JPEG2000Compression:
2099 status=InjectImageBlob(image_info,image,tile_image,"jp2",
2101 if (status == MagickFalse)
2102 ThrowWriterException(CoderError,tile_image->exception.reason);
2105 case RLECompression:
2109 Allocate pixel array.
2111 length=(size_t) number_pixels;
2112 pixels=(unsigned char *) AcquireQuantumMemory(length,4*
2114 length*=tile_image->colorspace == CMYKColorspace ? 4UL : 3UL;
2115 if (pixels == (unsigned char *) NULL)
2117 tile_image=DestroyImage(tile_image);
2118 ThrowWriterException(ResourceLimitError,
2119 "MemoryAllocationFailed");
2122 Dump runoffset encoded pixels.
2125 for (y=0; y < (ssize_t) tile_image->rows; y++)
2127 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2128 &tile_image->exception);
2129 if (p == (const PixelPacket *) NULL)
2131 indexes=GetVirtualIndexQueue(tile_image);
2132 for (x=0; x < (ssize_t) tile_image->columns; x++)
2134 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
2135 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
2136 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
2137 if (image->colorspace == CMYKColorspace)
2138 *q++=ScaleQuantumToChar(indexes[x]);
2142 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2143 if (compression == ZipCompression)
2144 status=ZLIBEncodeImage(image,length,pixels);
2147 if (compression == LZWCompression)
2148 status=LZWEncodeImage(image,length,pixels);
2150 status=PackbitsEncodeImage(image,length,pixels);
2151 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2152 if (status == MagickFalse)
2154 (void) CloseBlob(image);
2155 return(MagickFalse);
2162 Dump uncompressed DirectColor packets.
2164 Ascii85Initialize(image);
2165 for (y=0; y < (ssize_t) tile_image->rows; y++)
2167 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2168 &tile_image->exception);
2169 if (p == (const PixelPacket *) NULL)
2171 indexes=GetVirtualIndexQueue(tile_image);
2172 for (x=0; x < (ssize_t) tile_image->columns; x++)
2174 Ascii85Encode(image,ScaleQuantumToChar(GetRedPixelComponent(p)));
2175 Ascii85Encode(image,ScaleQuantumToChar(GetGreenPixelComponent(p)));
2176 Ascii85Encode(image,ScaleQuantumToChar(GetBluePixelComponent(p)));
2177 if (image->colorspace == CMYKColorspace)
2178 Ascii85Encode(image,ScaleQuantumToChar(indexes[x]));
2182 Ascii85Flush(image);
2189 Dump number of colors and colormap.
2191 switch (compression)
2193 case RLECompression:
2197 Allocate pixel array.
2199 length=(size_t) number_pixels;
2200 pixels=(unsigned char *) AcquireQuantumMemory(length,
2202 if (pixels == (unsigned char *) NULL)
2204 tile_image=DestroyImage(tile_image);
2205 ThrowWriterException(ResourceLimitError,
2206 "MemoryAllocationFailed");
2209 Dump Runlength encoded pixels.
2212 for (y=0; y < (ssize_t) tile_image->rows; y++)
2214 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2215 &tile_image->exception);
2216 if (p == (const PixelPacket *) NULL)
2218 indexes=GetVirtualIndexQueue(tile_image);
2219 for (x=0; x < (ssize_t) tile_image->columns; x++)
2220 *q++=(unsigned char) indexes[x];
2222 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2223 if (compression == ZipCompression)
2224 status=ZLIBEncodeImage(image,length,pixels);
2227 if (compression == LZWCompression)
2228 status=LZWEncodeImage(image,length,pixels);
2230 status=PackbitsEncodeImage(image,length,pixels);
2231 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2232 if (status == MagickFalse)
2234 (void) CloseBlob(image);
2235 return(MagickFalse);
2242 Dump uncompressed PseudoColor packets.
2244 Ascii85Initialize(image);
2245 for (y=0; y < (ssize_t) tile_image->rows; y++)
2247 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2248 &tile_image->exception);
2249 if (p == (const PixelPacket *) NULL)
2251 indexes=GetVirtualIndexQueue(tile_image);
2252 for (x=0; x < (ssize_t) tile_image->columns; x++)
2253 Ascii85Encode(image,(unsigned char) indexes[x]);
2255 Ascii85Flush(image);
2260 tile_image=DestroyImage(tile_image);
2261 offset=TellBlob(image)-offset;
2262 (void) WriteBlobString(image,"\nendstream\n");
2263 (void) WriteBlobString(image,"endobj\n");
2265 Write Length object.
2267 xref[object++]=TellBlob(image);
2268 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2270 (void) WriteBlobString(image,buffer);
2271 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
2272 (void) WriteBlobString(image,buffer);
2273 (void) WriteBlobString(image,"endobj\n");
2274 xref[object++]=TellBlob(image);
2275 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2277 (void) WriteBlobString(image,buffer);
2278 if ((image->storage_class != DirectClass) && (image->colors <= 256) &&
2279 (compression != FaxCompression) && (compression != Group4Compression))
2282 Write Colormap object.
2284 (void) WriteBlobString(image,"<<\n");
2285 if (compression == NoCompression)
2286 (void) WriteBlobString(image,"/Filter [ /ASCII85Decode ]\n");
2287 (void) FormatMagickString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
2289 (void) WriteBlobString(image,buffer);
2290 (void) WriteBlobString(image,">>\n");
2291 (void) WriteBlobString(image,"stream\n");
2292 offset=TellBlob(image);
2293 if (compression == NoCompression)
2294 Ascii85Initialize(image);
2295 for (i=0; i < (ssize_t) image->colors; i++)
2297 if (compression == NoCompression)
2299 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].red));
2300 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].green));
2301 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].blue));
2304 (void) WriteBlobByte(image,
2305 ScaleQuantumToChar(image->colormap[i].red));
2306 (void) WriteBlobByte(image,
2307 ScaleQuantumToChar(image->colormap[i].green));
2308 (void) WriteBlobByte(image,
2309 ScaleQuantumToChar(image->colormap[i].blue));
2311 if (compression == NoCompression)
2312 Ascii85Flush(image);
2313 offset=TellBlob(image)-offset;
2314 (void) WriteBlobString(image,"\nendstream\n");
2316 (void) WriteBlobString(image,"endobj\n");
2318 Write Length object.
2320 xref[object++]=TellBlob(image);
2321 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2323 (void) WriteBlobString(image,buffer);
2324 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g\n",(double)
2326 (void) WriteBlobString(image,buffer);
2327 (void) WriteBlobString(image,"endobj\n");
2329 Write softmask object.
2331 xref[object++]=TellBlob(image);
2332 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2334 (void) WriteBlobString(image,buffer);
2335 (void) WriteBlobString(image,"<<\n");
2336 if (image->matte == MagickFalse)
2337 (void) WriteBlobString(image,">>\n");
2340 (void) WriteBlobString(image,"/Type /XObject\n");
2341 (void) WriteBlobString(image,"/Subtype /Image\n");
2342 (void) FormatMagickString(buffer,MaxTextExtent,"/Name /Ma%.20g\n",
2343 (double) image->scene);
2344 (void) WriteBlobString(image,buffer);
2345 switch (compression)
2349 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,
2353 case LZWCompression:
2355 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"LZWDecode");
2358 case ZipCompression:
2360 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,
2366 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,
2371 (void) WriteBlobString(image,buffer);
2372 (void) FormatMagickString(buffer,MaxTextExtent,"/Width %.20g\n",
2373 (double) image->columns);
2374 (void) WriteBlobString(image,buffer);
2375 (void) FormatMagickString(buffer,MaxTextExtent,"/Height %.20g\n",
2376 (double) image->rows);
2377 (void) WriteBlobString(image,buffer);
2378 (void) WriteBlobString(image,"/ColorSpace /DeviceGray\n");
2379 (void) FormatMagickString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
2380 (compression == FaxCompression) || (compression == Group4Compression)
2382 (void) WriteBlobString(image,buffer);
2383 (void) FormatMagickString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
2385 (void) WriteBlobString(image,buffer);
2386 (void) WriteBlobString(image,">>\n");
2387 (void) WriteBlobString(image,"stream\n");
2388 offset=TellBlob(image);
2389 number_pixels=(MagickSizeType) image->columns*image->rows;
2390 switch (compression)
2392 case RLECompression:
2396 Allocate pixel array.
2398 length=(size_t) number_pixels;
2399 pixels=(unsigned char *) AcquireQuantumMemory(length,
2401 if (pixels == (unsigned char *) NULL)
2403 image=DestroyImage(image);
2404 ThrowWriterException(ResourceLimitError,
2405 "MemoryAllocationFailed");
2408 Dump Runlength encoded pixels.
2411 for (y=0; y < (ssize_t) image->rows; y++)
2413 p=GetVirtualPixels(image,0,y,image->columns,1,
2415 if (p == (const PixelPacket *) NULL)
2417 for (x=0; x < (ssize_t) image->columns; x++)
2419 *q++=ScaleQuantumToChar((Quantum) (GetAlphaPixelComponent(p)));
2423 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2424 if (compression == ZipCompression)
2425 status=ZLIBEncodeImage(image,length,pixels);
2428 if (compression == LZWCompression)
2429 status=LZWEncodeImage(image,length,pixels);
2431 status=PackbitsEncodeImage(image,length,pixels);
2432 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2433 if (status == MagickFalse)
2435 (void) CloseBlob(image);
2436 return(MagickFalse);
2443 Dump uncompressed PseudoColor packets.
2445 Ascii85Initialize(image);
2446 for (y=0; y < (ssize_t) image->rows; y++)
2448 p=GetVirtualPixels(image,0,y,image->columns,1,
2450 if (p == (const PixelPacket *) NULL)
2452 for (x=0; x < (ssize_t) image->columns; x++)
2454 Ascii85Encode(image,ScaleQuantumToChar((Quantum) (QuantumRange-
2455 GetOpacityPixelComponent(p))));
2459 Ascii85Flush(image);
2463 offset=TellBlob(image)-offset;
2464 (void) WriteBlobString(image,"\nendstream\n");
2466 (void) WriteBlobString(image,"endobj\n");
2468 Write Length object.
2470 xref[object++]=TellBlob(image);
2471 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2473 (void) WriteBlobString(image,buffer);
2474 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
2475 (void) WriteBlobString(image,buffer);
2476 (void) WriteBlobString(image,"endobj\n");
2477 if (GetNextImageInList(image) == (Image *) NULL)
2479 image=SyncNextImageInList(image);
2480 status=SetImageProgress(image,SaveImagesTag,scene++,
2481 GetImageListLength(image));
2482 if (status == MagickFalse)
2484 } while (image_info->adjoin != MagickFalse);
2486 Write Metadata object.
2488 xref[object++]=TellBlob(image);
2490 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
2492 (void) WriteBlobString(image,buffer);
2493 (void) WriteBlobString(image,"<<\n");
2494 (void) FormatMagickString(buffer,MaxTextExtent,"/Title (%s)\n",
2495 EscapeParenthesis(image->filename));
2496 (void) WriteBlobString(image,buffer);
2497 seconds=time((time_t *) NULL);
2498 #if defined(MAGICKCORE_HAVE_LOCALTIME_R)
2499 (void) localtime_r(&seconds,&local_time);
2501 (void) memcpy(&local_time,localtime(&seconds),sizeof(local_time));
2503 (void) FormatMagickString(date,MaxTextExtent,"D:%04d%02d%02d%02d%02d%02d",
2504 local_time.tm_year+1900,local_time.tm_mon+1,local_time.tm_mday,
2505 local_time.tm_hour,local_time.tm_min,local_time.tm_sec);
2506 (void) FormatMagickString(buffer,MaxTextExtent,"/CreationDate (%s)\n",date);
2507 (void) WriteBlobString(image,buffer);
2508 (void) FormatMagickString(buffer,MaxTextExtent,"/ModDate (%s)\n",date);
2509 (void) WriteBlobString(image,buffer);
2510 (void) FormatMagickString(buffer,MaxTextExtent,"/Producer (%s)\n",
2511 EscapeParenthesis(GetMagickVersion((size_t *) NULL)));
2512 (void) WriteBlobString(image,buffer);
2513 (void) WriteBlobString(image,">>\n");
2514 (void) WriteBlobString(image,"endobj\n");
2518 offset=TellBlob(image)-xref[0]+10;
2519 (void) WriteBlobString(image,"xref\n");
2520 (void) FormatMagickString(buffer,MaxTextExtent,"0 %.20g\n",(double)
2522 (void) WriteBlobString(image,buffer);
2523 (void) WriteBlobString(image,"0000000000 65535 f \n");
2524 for (i=0; i < (ssize_t) object; i++)
2526 (void) FormatMagickString(buffer,MaxTextExtent,"%010lu 00000 n \n",
2527 (unsigned long) xref[i]);
2528 (void) WriteBlobString(image,buffer);
2530 (void) WriteBlobString(image,"trailer\n");
2531 (void) WriteBlobString(image,"<<\n");
2532 (void) FormatMagickString(buffer,MaxTextExtent,"/Size %.20g\n",(double)
2534 (void) WriteBlobString(image,buffer);
2535 (void) FormatMagickString(buffer,MaxTextExtent,"/Info %.20g 0 R\n",(double)
2537 (void) WriteBlobString(image,buffer);
2538 (void) FormatMagickString(buffer,MaxTextExtent,"/Root %.20g 0 R\n",(double)
2540 (void) WriteBlobString(image,buffer);
2541 (void) WriteBlobString(image,">>\n");
2542 (void) WriteBlobString(image,"startxref\n");
2543 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
2544 (void) WriteBlobString(image,buffer);
2545 (void) WriteBlobString(image,"%%EOF\n");
2546 xref=(MagickOffsetType *) RelinquishMagickMemory(xref);
2547 (void) CloseBlob(image);