2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Portable Document Format %
20 % Copyright 1999-2009 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"
76 #if defined(MAGICKCORE_TIFF_DELEGATE)
77 #if defined(MAGICKCORE_HAVE_TIFFCONF_H)
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(__WINDOWS__)
144 #if defined(__WINDOWS__)
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(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(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(__WINDOWS__)
186 NTGhostscriptUnLoadDLL();
188 for (i=0; i < (long) 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(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];
359 assert(image_info != (const ImageInfo *) NULL);
360 assert(image_info->signature == MagickSignature);
361 if (image_info->debug != MagickFalse)
362 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
363 image_info->filename);
364 assert(exception != (ExceptionInfo *) NULL);
365 assert(exception->signature == MagickSignature);
369 image=AcquireImage(image_info);
370 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
371 if (status == MagickFalse)
373 image=DestroyImageList(image);
374 return((Image *) NULL);
376 status=AcquireUniqueSymbolicLink(image_info->filename,input_filename);
377 if (status == MagickFalse)
379 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
380 image_info->filename);
381 image=DestroyImageList(image);
382 return((Image *) NULL);
385 Set the page density.
387 delta.x=DefaultResolution;
388 delta.y=DefaultResolution;
389 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-%lu",
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,"%gx%g%+g%+g",
528 bounds.x2-bounds.x1,bounds.y2-bounds.y1,bounds.x1,bounds.y1);
529 (void) SetImageProperty(image,"pdf:HiResBoundingBox",geometry);
530 page.width=(unsigned long) (bounds.x2-bounds.x1+0.5);
531 page.height=(unsigned long) (bounds.y2-bounds.y1+0.5);
535 (void) CloseBlob(image);
536 if ((fabs(angle) == 90.0) || (fabs(angle) == 270.0))
542 page.width=page.height;
545 if (image_info->colorspace == RGBColorspace)
548 Create Ghostscript control file.
550 file=AcquireUniqueFileResource(postscript_filename);
553 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
554 image_info->filename);
555 image=DestroyImage(image);
556 return((Image *) NULL);
558 count=write(file," ",1);
561 Render Postscript with the Ghostscript delegate.
563 if ((image_info->ping != MagickFalse) ||
564 (image_info->monochrome != MagickFalse))
565 delegate_info=GetDelegateInfo("ps:mono",(char *) NULL,exception);
567 if (cmyk != MagickFalse)
568 delegate_info=GetDelegateInfo("ps:cmyk",(char *) NULL,exception);
570 if (LocaleCompare(image_info->magick,"AI") == 0)
571 delegate_info=GetDelegateInfo("ps:alpha",(char *) NULL,exception);
573 delegate_info=GetDelegateInfo("ps:color",(char *) NULL,exception);
574 if (delegate_info == (const DelegateInfo *) NULL)
576 (void) RelinquishUniqueFileResource(postscript_filename);
577 image=DestroyImage(image);
578 return((Image *) NULL);
581 (void) FormatMagickString(density,MaxTextExtent,"%gx%g",image->x_resolution,
582 image->y_resolution);
583 if (image_info->page != (char *) NULL)
585 (void) ParseAbsoluteGeometry(image_info->page,&page);
586 page.width=(unsigned long) (page.width*image->x_resolution/delta.x+0.5);
587 page.height=(unsigned long) (page.height*image->y_resolution/delta.y+0.5);
588 (void) FormatMagickString(options,MaxTextExtent,"-g%lux%lu ",page.width,
591 if (cmyk != MagickFalse)
592 (void) ConcatenateMagickString(options,"-dUseCIEColor ",MaxTextExtent);
593 if (cropbox != MagickFalse)
594 (void) ConcatenateMagickString(options,"-dUseCropBox ",MaxTextExtent);
595 if (trimbox != MagickFalse)
596 (void) ConcatenateMagickString(options,"-dUseTrimBox ",MaxTextExtent);
597 read_info=CloneImageInfo(image_info);
598 *read_info->magick='\0';
599 if (read_info->number_scenes != 0)
602 pages[MaxTextExtent];
604 (void) FormatMagickString(pages,MaxTextExtent,"-dFirstPage=%lu "
605 "-dLastPage=%lu",read_info->scene+1,read_info->scene+
606 read_info->number_scenes);
607 (void) ConcatenateMagickString(options,pages,MaxTextExtent);
608 read_info->number_scenes=0;
609 if (read_info->scenes != (char *) NULL)
610 *read_info->scenes='\0';
612 if (read_info->authenticate != (char *) NULL)
613 (void) FormatMagickString(options+strlen(options),MaxTextExtent,
614 " -sPDFPassword=%s",read_info->authenticate);
615 (void) CopyMagickString(filename,read_info->filename,MaxTextExtent);
616 (void) AcquireUniqueFilename(read_info->filename);
617 (void) FormatMagickString(command,MaxTextExtent,
618 GetDelegateCommands(delegate_info),
619 read_info->antialias != MagickFalse ? 4 : 1,
620 read_info->antialias != MagickFalse ? 4 : 1,density,options,
621 read_info->filename,postscript_filename,input_filename);
622 status=InvokePDFDelegate(read_info->verbose,command,exception);
623 pdf_image=(Image *) NULL;
624 if ((status != MagickFalse) &&
625 (IsPDFRendered(read_info->filename) != MagickFalse))
626 pdf_image=ReadImage(read_info,exception);
627 (void) RelinquishUniqueFileResource(postscript_filename);
628 (void) RelinquishUniqueFileResource(read_info->filename);
629 (void) RelinquishUniqueFileResource(input_filename);
630 read_info=DestroyImageInfo(read_info);
631 if (pdf_image == (Image *) NULL)
633 ThrowFileException(exception,DelegateError,"PostscriptDelegateFailed",
634 image_info->filename);
635 return((Image *) NULL);
637 if (LocaleCompare(pdf_image->magick,"BMP") == 0)
642 cmyk_image=ConsolidateCMYKImages(pdf_image,exception);
643 if (cmyk_image != (Image *) NULL)
645 pdf_image=DestroyImageList(pdf_image);
646 pdf_image=cmyk_image;
649 if (image_info->number_scenes != 0)
658 Add place holder images to meet the subimage specification requirement.
660 for (i=0; i < (long) image_info->scene; i++)
662 clone_image=CloneImage(pdf_image,1,1,MagickTrue,exception);
663 if (clone_image != (Image *) NULL)
664 PrependImageToList(&pdf_image,clone_image);
669 (void) CopyMagickString(pdf_image->filename,filename,MaxTextExtent);
670 pdf_image->page=page;
671 (void) CloneImageProfiles(pdf_image,image);
672 (void) CloneImageProperties(pdf_image,image);
673 next=SyncNextImageInList(pdf_image);
674 if (next != (Image *) NULL)
676 } while (next != (Image *) NULL);
677 image=DestroyImage(image);
679 for (next=GetFirstImageInList(pdf_image); next != (Image *) NULL; )
682 next=GetNextImageInList(next);
684 return(GetFirstImageInList(pdf_image));
688 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
692 % R e g i s t e r P D F I m a g e %
696 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
698 % RegisterPDFImage() adds properties for the PDF image format to
699 % the list of supported formats. The properties include the image format
700 % tag, a method to read and/or write the format, whether the format
701 % supports the saving of more than one frame to the same file or blob,
702 % whether the format supports native in-memory I/O, and a brief
703 % description of the format.
705 % The format of the RegisterPDFImage method is:
707 % unsigned long RegisterPDFImage(void)
710 ModuleExport unsigned long RegisterPDFImage(void)
715 entry=SetMagickInfo("AI");
716 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
717 entry->encoder=(EncodeImageHandler *) WritePDFImage;
718 entry->adjoin=MagickFalse;
719 entry->blob_support=MagickFalse;
720 entry->seekable_stream=MagickTrue;
721 entry->thread_support=EncoderThreadSupport;
722 entry->description=ConstantString("Adobe Illustrator CS2");
723 entry->module=ConstantString("PDF");
724 (void) RegisterMagickInfo(entry);
725 entry=SetMagickInfo("EPDF");
726 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
727 entry->encoder=(EncodeImageHandler *) WritePDFImage;
728 entry->adjoin=MagickFalse;
729 entry->blob_support=MagickFalse;
730 entry->seekable_stream=MagickTrue;
731 entry->thread_support=EncoderThreadSupport;
732 entry->description=ConstantString("Encapsulated Portable Document Format");
733 entry->module=ConstantString("PDF");
734 (void) RegisterMagickInfo(entry);
735 entry=SetMagickInfo("PDF");
736 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
737 entry->encoder=(EncodeImageHandler *) WritePDFImage;
738 entry->magick=(IsImageFormatHandler *) IsPDF;
739 entry->blob_support=MagickFalse;
740 entry->seekable_stream=MagickTrue;
741 entry->thread_support=EncoderThreadSupport;
742 entry->description=ConstantString("Portable Document Format");
743 entry->module=ConstantString("PDF");
744 (void) RegisterMagickInfo(entry);
745 entry=SetMagickInfo("PDFA");
746 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
747 entry->encoder=(EncodeImageHandler *) WritePDFImage;
748 entry->magick=(IsImageFormatHandler *) IsPDF;
749 entry->blob_support=MagickFalse;
750 entry->seekable_stream=MagickTrue;
751 entry->thread_support=EncoderThreadSupport;
752 entry->description=ConstantString("Portable Document Archive Format");
753 entry->module=ConstantString("PDF");
754 (void) RegisterMagickInfo(entry);
755 return(MagickImageCoderSignature);
759 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
763 % U n r e g i s t e r P D F I m a g e %
767 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
769 % UnregisterPDFImage() removes format registrations made by the
770 % PDF module from the list of supported formats.
772 % The format of the UnregisterPDFImage method is:
774 % UnregisterPDFImage(void)
777 ModuleExport void UnregisterPDFImage(void)
779 (void) UnregisterMagickInfo("AI");
780 (void) UnregisterMagickInfo("EPDF");
781 (void) UnregisterMagickInfo("PDF");
782 (void) UnregisterMagickInfo("PDFA");
786 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
790 % W r i t e P D F I m a g e %
794 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
796 % WritePDFImage() writes an image in the Portable Document image
799 % The format of the WritePDFImage method is:
801 % MagickBooleanType WritePDFImage(const ImageInfo *image_info,Image *image)
803 % A description of each parameter follows.
805 % o image_info: the image info.
807 % o image: The image.
811 static inline size_t MagickMax(const size_t x,const size_t y)
818 static inline size_t MagickMin(const size_t x,const size_t y)
825 static char *EscapeParenthesis(const char *text)
834 buffer[MaxTextExtent];
841 for (i=0; i < (long) MagickMin(strlen(text),(MaxTextExtent-escapes-1)); i++)
843 if ((text[i] == '(') || (text[i] == ')'))
854 #if defined(MAGICKCORE_TIFF_DELEGATE)
855 static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
856 Image *image,Image *inject_image)
859 filename[MaxTextExtent];
896 Write image as CCITTFax4 TIFF image to a temporary file.
898 assert(image_info != (const ImageInfo *) NULL);
899 assert(image_info->signature == MagickSignature);
900 assert(image != (Image *) NULL);
901 assert(image->signature == MagickSignature);
902 if (image->debug != MagickFalse)
903 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
904 assert(inject_image != (Image *) NULL);
905 assert(inject_image->signature == MagickSignature);
906 huffman_image=CloneImage(inject_image,0,0,MagickTrue,&image->exception);
907 if (huffman_image == (Image *) NULL)
910 unique_file=AcquireUniqueFileResource(filename);
911 if (unique_file != -1)
912 file=fdopen(unique_file,"wb");
913 if ((unique_file == -1) || (file == (FILE *) NULL))
915 ThrowFileException(&image->exception,FileOpenError,
916 "UnableToCreateTemporaryFile",filename);
919 (void) FormatMagickString(huffman_image->filename,MaxTextExtent,"tiff:%s",
921 write_info=CloneImageInfo(image_info);
922 SetImageInfoFile(write_info,file);
923 write_info->compression=Group4Compression;
924 (void) SetImageOption(write_info,"quantum:polarity","min-is-white");
925 status=WriteImage(write_info,huffman_image);
927 write_info=DestroyImageInfo(write_info);
928 if (status == MagickFalse)
930 tiff=TIFFOpen(filename,"rb");
931 if (tiff == (TIFF *) NULL)
933 huffman_image=DestroyImage(huffman_image);
935 (void) RelinquishUniqueFileResource(filename);
936 ThrowFileException(&image->exception,FileOpenError,"UnableToOpenFile",
937 image_info->filename);
941 Allocate raw strip buffer.
944 (void) TIFFGetField(tiff,TIFFTAG_STRIPBYTECOUNTS,&byte_count);
945 strip_size=byte_count[0];
946 for (i=1; i < (long) TIFFNumberOfStrips(tiff); i++)
947 if (byte_count[i] > strip_size)
948 strip_size=byte_count[i];
949 buffer=(unsigned char *) AcquireQuantumMemory((size_t) strip_size,
951 if (buffer == (unsigned char *) NULL)
954 huffman_image=DestroyImage(huffman_image);
956 (void) RelinquishUniqueFileResource(filename);
957 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
958 image_info->filename);
961 Compress runlength encoded to 2D Huffman pixels.
963 fillorder=FILLORDER_LSB2MSB;
964 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&fillorder);
965 for (i=0; i < (long) TIFFNumberOfStrips(tiff); i++)
967 count=(ssize_t) TIFFReadRawStrip(tiff,(uint32) i,buffer,(long)
969 if (fillorder == FILLORDER_LSB2MSB)
970 TIFFReverseBits(buffer,(unsigned long) count);
971 (void) WriteBlob(image,(size_t) count,buffer);
973 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
975 huffman_image=DestroyImage(huffman_image);
977 (void) RelinquishUniqueFileResource(filename);
981 static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
982 Image *image,Image *inject_image)
984 assert(image_info != (const ImageInfo *) NULL);
985 assert(image_info->signature == MagickSignature);
986 assert(image != (Image *) NULL);
987 assert(image->signature == MagickSignature);
988 if (image->debug != MagickFalse)
989 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
990 assert(inject_image != (Image *) NULL);
991 assert(inject_image->signature == MagickSignature);
992 (void) ThrowMagickException(&image->exception,GetMagickModule(),
993 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (TIFF)",
999 static MagickBooleanType WritePDFImage(const ImageInfo *image_info,Image *image)
1001 #define CFormat "/Filter [ /%s ]\n"
1002 #define ObjectsPerImage 14
1007 "<?xpacket begin=\"%s\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n"
1008 "<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"
1009 " <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n"
1010 " <rdf:Description rdf:about=\"\"\n"
1011 " xmlns:xap=\"http://ns.adobe.com/xap/1.0/\">\n"
1012 " <xap:ModifyDate>%s</xap:ModifyDate>\n"
1013 " <xap:CreateDate>%s</xap:CreateDate>\n"
1014 " <xap:MetadataDate>%s</xap:MetadataDate>\n"
1015 " <xap:CreatorTool>%s</xap:CreatorTool>\n"
1016 " </rdf:Description>\n"
1017 " <rdf:Description rdf:about=\"\"\n"
1018 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"
1019 " <dc:format>application/pdf</dc:format>\n"
1020 " </rdf:Description>\n"
1021 " <rdf:Description rdf:about=\"\"\n"
1022 " xmlns:xapMM=\"http://ns.adobe.com/xap/1.0/mm/\">\n"
1023 " <xapMM:DocumentID>uuid:6ec119d7-7982-4f56-808d-dfe64f5b35cf</xapMM:DocumentID>\n"
1024 " <xapMM:InstanceID>uuid:a79b99b4-6235-447f-9f6c-ec18ef7555cb</xapMM:InstanceID>\n"
1025 " </rdf:Description>\n"
1026 " <rdf:Description rdf:about=\"\"\n"
1027 " xmlns:pdf=\"http://ns.adobe.com/pdf/1.3/\">\n"
1028 " <pdf:Producer>%s</pdf:Producer>\n"
1029 " </rdf:Description>\n"
1030 " <rdf:Description rdf:about=\"\"\n"
1031 " xmlns:pdfaid=\"http://www.aiim.org/pdfa/ns/id/\">\n"
1032 " <pdfaid:part>1</pdfaid:part>\n"
1033 " <pdfaid:conformance>B</pdfaid:conformance>\n"
1034 " </rdf:Description>\n"
1037 "<?xpacket end=\"w\"?>\n"
1039 XMPProfileMagick[4]= { (char) 0xef, (char) 0xbb, (char) 0xbf, (char) 0x00 };
1042 buffer[MaxTextExtent],
1043 date[MaxTextExtent],
1045 page_geometry[MaxTextExtent];
1091 register const IndexPacket
1094 register const PixelPacket
1097 register unsigned char
1125 Open output image file.
1127 assert(image_info != (const ImageInfo *) NULL);
1128 assert(image_info->signature == MagickSignature);
1129 assert(image != (Image *) NULL);
1130 assert(image->signature == MagickSignature);
1131 if (image->debug != MagickFalse)
1132 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1133 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1134 if (status == MagickFalse)
1137 Allocate X ref memory.
1139 xref=(MagickOffsetType *) AcquireQuantumMemory(2048UL,sizeof(*xref));
1140 if (xref == (MagickOffsetType *) NULL)
1141 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1142 (void) ResetMagickMemory(xref,0,2048UL*sizeof(*xref));
1148 if (image_info->compression == JPEG2000Compression)
1149 version=(unsigned long) MagickMax(version,5);
1150 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
1151 if (next->matte != MagickFalse)
1152 version=(unsigned long) MagickMax(version,4);
1153 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1154 version=(unsigned long) MagickMax(version,6);
1155 (void) FormatMagickString(buffer,MaxTextExtent,"%%PDF-1.%lu \n",version);
1156 (void) WriteBlobString(image,buffer);
1157 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1158 (void) WriteBlobString(image,"%âãÏÓ\n");
1160 Write Catalog object.
1162 xref[object++]=TellBlob(image);
1164 (void) FormatMagickString(buffer,MaxTextExtent,"%lu 0 obj\n",object);
1165 (void) WriteBlobString(image,buffer);
1166 (void) WriteBlobString(image,"<<\n");
1167 if (LocaleCompare(image_info->magick,"PDFA") != 0)
1168 (void) FormatMagickString(buffer,MaxTextExtent,"/Pages %lu 0 R\n",
1172 (void) FormatMagickString(buffer,MaxTextExtent,"/Metadata %lu 0 R\n",
1174 (void) WriteBlobString(image,buffer);
1175 (void) FormatMagickString(buffer,MaxTextExtent,"/Pages %lu 0 R\n",
1178 (void) WriteBlobString(image,buffer);
1179 (void) WriteBlobString(image,"/Type /Catalog\n");
1180 (void) WriteBlobString(image,">>\n");
1181 (void) WriteBlobString(image,"endobj\n");
1182 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1185 create_date[MaxTextExtent],
1186 modify_date[MaxTextExtent],
1187 timestamp[MaxTextExtent],
1188 xmp_profile[MaxTextExtent];
1196 xref[object++]=TellBlob(image);
1197 (void) FormatMagickString(buffer,MaxTextExtent,"%lu 0 obj\n",object);
1198 (void) WriteBlobString(image,buffer);
1199 (void) WriteBlobString(image,"<<\n");
1200 (void) WriteBlobString(image,"/Subtype /XML\n");
1202 value=GetImageProperty(image,"date:modify");
1203 if (value != (const char *) NULL)
1204 (void) CopyMagickString(modify_date,value,MaxTextExtent);
1206 value=GetImageProperty(image,"date:create");
1207 if (value != (const char *) NULL)
1208 (void) CopyMagickString(create_date,value,MaxTextExtent);
1209 (void) FormatMagickTime(time((time_t *) NULL),MaxTextExtent,timestamp);
1210 i=FormatMagickString(xmp_profile,MaxTextExtent,XMPProfile,
1211 XMPProfileMagick,modify_date,create_date,timestamp,
1212 GetMagickVersion(&version),GetMagickVersion(&version));
1213 (void) FormatMagickString(buffer,MaxTextExtent,"/Length %lu\n",1UL*i);
1214 (void) WriteBlobString(image,buffer);
1215 (void) WriteBlobString(image,"/Type /Metadata\n");
1216 (void) WriteBlobString(image,">>\nstream\n");
1217 (void) WriteBlobString(image,xmp_profile);
1218 (void) WriteBlobString(image,"endstream\n");
1219 (void) WriteBlobString(image,"endobj\n");
1224 xref[object++]=TellBlob(image);
1226 (void) FormatMagickString(buffer,MaxTextExtent,"%lu 0 obj\n",object);
1227 (void) WriteBlobString(image,buffer);
1228 (void) WriteBlobString(image,"<<\n");
1229 (void) WriteBlobString(image,"/Type /Pages\n");
1230 (void) FormatMagickString(buffer,MaxTextExtent,"/Kids [ %lu 0 R ",object+1);
1231 (void) WriteBlobString(image,buffer);
1232 count=(long) (pages_id+ObjectsPerImage+1);
1233 if (image_info->adjoin != MagickFalse)
1239 Predict page object id's.
1242 for ( ; GetNextImageInList(kid_image) != (Image *) NULL; count+=ObjectsPerImage)
1244 (void) FormatMagickString(buffer,MaxTextExtent,"%ld 0 R ",count);
1245 (void) WriteBlobString(image,buffer);
1246 kid_image=GetNextImageInList(kid_image);
1248 xref=(MagickOffsetType *) ResizeQuantumMemory(xref,(size_t) count+2048UL,
1250 if (xref == (MagickOffsetType *) NULL)
1251 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1253 (void) WriteBlobString(image,"]\n");
1254 (void) FormatMagickString(buffer,MaxTextExtent,"/Count %lu\n",
1255 (count-pages_id)/ObjectsPerImage);
1256 (void) WriteBlobString(image,buffer);
1257 (void) WriteBlobString(image,">>\n");
1258 (void) WriteBlobString(image,"endobj\n");
1262 compression=image->compression;
1263 if (image_info->compression != UndefinedCompression)
1264 compression=image_info->compression;
1265 switch (compression)
1267 case FaxCompression:
1268 case Group4Compression:
1270 if ((IsMonochromeImage(image,&image->exception) == MagickFalse) ||
1271 (image->matte != MagickFalse))
1272 compression=RLECompression;
1275 #if !defined(MAGICKCORE_JPEG_DELEGATE)
1276 case JPEGCompression:
1278 compression=RLECompression;
1279 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1280 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
1285 #if !defined(MAGICKCORE_JP2_DELEGATE)
1286 case JPEG2000Compression:
1288 compression=RLECompression;
1289 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1290 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JP2)",
1295 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
1296 case ZipCompression:
1298 compression=RLECompression;
1299 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1300 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZLIB)",
1305 case LZWCompression:
1307 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1308 compression=RLECompression; /* LZW compression is forbidden */
1313 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1314 compression=RLECompression; /* ASCII 85 compression is forbidden */
1320 if (compression == JPEG2000Compression)
1322 if (image->colorspace != RGBColorspace)
1323 (void) TransformImageColorspace(image,RGBColorspace);
1326 Scale relative to dots-per-inch.
1328 delta.x=DefaultResolution;
1329 delta.y=DefaultResolution;
1330 resolution.x=image->x_resolution;
1331 resolution.y=image->y_resolution;
1332 if ((resolution.x == 0.0) || (resolution.y == 0.0))
1334 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
1335 resolution.x=geometry_info.rho;
1336 resolution.y=geometry_info.sigma;
1337 if ((flags & SigmaValue) == 0)
1338 resolution.y=resolution.x;
1340 if (image_info->density != (char *) NULL)
1342 flags=ParseGeometry(image_info->density,&geometry_info);
1343 resolution.x=geometry_info.rho;
1344 resolution.y=geometry_info.sigma;
1345 if ((flags & SigmaValue) == 0)
1346 resolution.y=resolution.x;
1348 if (image->units == PixelsPerCentimeterResolution)
1353 SetGeometry(image,&geometry);
1354 (void) FormatMagickString(page_geometry,MaxTextExtent,"%lux%lu",
1355 image->columns,image->rows);
1356 if (image_info->page != (char *) NULL)
1357 (void) CopyMagickString(page_geometry,image_info->page,MaxTextExtent);
1359 if ((image->page.width != 0) && (image->page.height != 0))
1360 (void) FormatMagickString(page_geometry,MaxTextExtent,"%lux%lu%+ld%+ld",
1361 image->page.width,image->page.height,image->page.x,image->page.y);
1363 if ((image->gravity != UndefinedGravity) &&
1364 (LocaleCompare(image_info->magick,"PDF") == 0))
1365 (void) CopyMagickString(page_geometry,PSPageGeometry,MaxTextExtent);
1366 (void) ConcatenateMagickString(page_geometry,">",MaxTextExtent);
1367 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
1368 &geometry.width,&geometry.height);
1369 scale.x=(double) (geometry.width*delta.x)/resolution.x;
1370 geometry.width=(unsigned long) (scale.x+0.5);
1371 scale.y=(double) (geometry.height*delta.y)/resolution.y;
1372 geometry.height=(unsigned long) (scale.y+0.5);
1373 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
1374 (void) ParseGravityGeometry(image,page_geometry,&page_info,
1376 if (image->gravity != UndefinedGravity)
1378 geometry.x=(-page_info.x);
1379 geometry.y=(long) (media_info.height+page_info.y-image->rows);
1382 if (image_info->pointsize != 0.0)
1383 pointsize=image_info->pointsize;
1385 value=GetImageProperty(image,"Label");
1386 if (value != (const char *) NULL)
1387 text_size=(unsigned long) (MultilineCensus(value)*pointsize+12);
1391 xref[object++]=TellBlob(image);
1392 (void) FormatMagickString(buffer,MaxTextExtent,"%lu 0 obj\n",object);
1393 (void) WriteBlobString(image,buffer);
1394 (void) WriteBlobString(image,"<<\n");
1395 (void) WriteBlobString(image,"/Type /Page\n");
1396 (void) FormatMagickString(buffer,MaxTextExtent,"/Parent %lu 0 R\n",
1398 (void) WriteBlobString(image,buffer);
1399 (void) WriteBlobString(image,"/Resources <<\n");
1400 labels=(char **) NULL;
1401 value=GetImageProperty(image,"Label");
1402 if (value != (const char *) NULL)
1403 labels=StringToList(value);
1404 if (labels != (char **) NULL)
1406 (void) FormatMagickString(buffer,MaxTextExtent,
1407 "/Font << /F%lu %lu 0 R >>\n",image->scene,object+4);
1408 (void) WriteBlobString(image,buffer);
1410 (void) FormatMagickString(buffer,MaxTextExtent,
1411 "/XObject << /Im%lu %lu 0 R >>\n",image->scene,object+5);
1412 (void) WriteBlobString(image,buffer);
1413 (void) FormatMagickString(buffer,MaxTextExtent,"/ProcSet %lu 0 R >>\n",
1415 (void) WriteBlobString(image,buffer);
1416 (void) FormatMagickString(buffer,MaxTextExtent,"/MediaBox [0 0 %g %g]\n",
1417 72.0*media_info.width/resolution.x,72.0*media_info.height/resolution.y);
1418 (void) WriteBlobString(image,buffer);
1419 (void) FormatMagickString(buffer,MaxTextExtent,"/CropBox [0 0 %g %g]\n",
1420 72.0*media_info.width/resolution.x,72.0*media_info.height/resolution.y);
1421 (void) WriteBlobString(image,buffer);
1422 (void) FormatMagickString(buffer,MaxTextExtent,"/Contents %lu 0 R\n",
1424 (void) WriteBlobString(image,buffer);
1425 (void) FormatMagickString(buffer,MaxTextExtent,"/Thumb %lu 0 R\n",
1427 (void) WriteBlobString(image,buffer);
1428 (void) WriteBlobString(image,">>\n");
1429 (void) WriteBlobString(image,"endobj\n");
1431 Write Contents object.
1433 xref[object++]=TellBlob(image);
1434 (void) FormatMagickString(buffer,MaxTextExtent,"%lu 0 obj\n",object);
1435 (void) WriteBlobString(image,buffer);
1436 (void) WriteBlobString(image,"<<\n");
1437 (void) FormatMagickString(buffer,MaxTextExtent,"/Length %lu 0 R\n",
1439 (void) WriteBlobString(image,buffer);
1440 (void) WriteBlobString(image,">>\n");
1441 (void) WriteBlobString(image,"stream\n");
1442 offset=TellBlob(image);
1443 (void) WriteBlobString(image,"q\n");
1444 if (labels != (char **) NULL)
1445 for (i=0; labels[i] != (char *) NULL; i++)
1447 (void) WriteBlobString(image,"BT\n");
1448 (void) FormatMagickString(buffer,MaxTextExtent,"/F%lu %g Tf\n",
1449 image->scene,pointsize);
1450 (void) WriteBlobString(image,buffer);
1451 (void) FormatMagickString(buffer,MaxTextExtent,"%ld %ld Td\n",
1452 geometry.x,(long) (geometry.y+geometry.height+i*pointsize+12));
1453 (void) WriteBlobString(image,buffer);
1454 (void) FormatMagickString(buffer,MaxTextExtent,"(%s) Tj\n",labels[i]);
1455 (void) WriteBlobString(image,buffer);
1456 (void) WriteBlobString(image,"ET\n");
1457 labels[i]=DestroyString(labels[i]);
1459 (void) FormatMagickString(buffer,MaxTextExtent,"%g 0 0 %g %ld %ld cm\n",
1460 scale.x,scale.y,geometry.x,geometry.y);
1461 (void) WriteBlobString(image,buffer);
1462 (void) FormatMagickString(buffer,MaxTextExtent,"/Im%lu Do\n",image->scene);
1463 (void) WriteBlobString(image,buffer);
1464 (void) WriteBlobString(image,"Q\n");
1465 offset=TellBlob(image)-offset;
1466 (void) WriteBlobString(image,"endstream\n");
1467 (void) WriteBlobString(image,"endobj\n");
1469 Write Length object.
1471 xref[object++]=TellBlob(image);
1472 (void) FormatMagickString(buffer,MaxTextExtent,"%lu 0 obj\n",object);
1473 (void) WriteBlobString(image,buffer);
1474 (void) FormatMagickString(buffer,MaxTextExtent,"%lu\n",
1475 (unsigned long) offset);
1476 (void) WriteBlobString(image,buffer);
1477 (void) WriteBlobString(image,"endobj\n");
1479 Write Procset object.
1481 xref[object++]=TellBlob(image);
1482 (void) FormatMagickString(buffer,MaxTextExtent,"%lu 0 obj\n",object);
1483 (void) WriteBlobString(image,buffer);
1484 if ((image->storage_class == DirectClass) || (image->colors > 256))
1485 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageC",MaxTextExtent);
1487 if ((compression == FaxCompression) || (compression == Group4Compression))
1488 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageB",MaxTextExtent);
1490 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageI",MaxTextExtent);
1491 (void) WriteBlobString(image,buffer);
1492 (void) WriteBlobString(image," ]\n");
1493 (void) WriteBlobString(image,"endobj\n");
1497 xref[object++]=TellBlob(image);
1498 (void) FormatMagickString(buffer,MaxTextExtent,"%lu 0 obj\n",object);
1499 (void) WriteBlobString(image,buffer);
1500 (void) WriteBlobString(image,"<<\n");
1501 if (labels != (char **) NULL)
1503 (void) WriteBlobString(image,"/Type /Font\n");
1504 (void) WriteBlobString(image,"/Subtype /Type1\n");
1505 (void) FormatMagickString(buffer,MaxTextExtent,"/Name /F%lu\n",
1507 (void) WriteBlobString(image,buffer);
1508 (void) WriteBlobString(image,"/BaseFont /Helvetica\n");
1509 (void) WriteBlobString(image,"/Encoding /MacRomanEncoding\n");
1510 labels=(char **) RelinquishMagickMemory(labels);
1512 (void) WriteBlobString(image,">>\n");
1513 (void) WriteBlobString(image,"endobj\n");
1515 Write XObject object.
1517 xref[object++]=TellBlob(image);
1518 (void) FormatMagickString(buffer,MaxTextExtent,"%lu 0 obj\n",object);
1519 (void) WriteBlobString(image,buffer);
1520 (void) WriteBlobString(image,"<<\n");
1521 (void) WriteBlobString(image,"/Type /XObject\n");
1522 (void) WriteBlobString(image,"/Subtype /Image\n");
1523 (void) FormatMagickString(buffer,MaxTextExtent,"/Name /Im%lu\n",
1525 (void) WriteBlobString(image,buffer);
1526 switch (compression)
1530 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"ASCII85Decode");
1533 case JPEGCompression:
1535 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"DCTDecode");
1536 if (image->colorspace != CMYKColorspace)
1538 (void) WriteBlobString(image,buffer);
1539 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1543 case JPEG2000Compression:
1545 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"JPXDecode");
1546 if (image->colorspace != CMYKColorspace)
1548 (void) WriteBlobString(image,buffer);
1549 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1553 case LZWCompression:
1555 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"LZWDecode");
1558 case ZipCompression:
1560 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"FlateDecode");
1563 case FaxCompression:
1564 case Group4Compression:
1566 (void) CopyMagickString(buffer,"/Filter [ /CCITTFaxDecode ]\n",
1568 (void) WriteBlobString(image,buffer);
1569 (void) FormatMagickString(buffer,MaxTextExtent,"/DecodeParms [ << "
1570 "/K %s /BlackIs1 true /Columns %ld /Rows %ld >> ]\n",CCITTParam,
1571 image->columns,image->rows);
1576 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,
1581 (void) WriteBlobString(image,buffer);
1582 (void) FormatMagickString(buffer,MaxTextExtent,"/Width %lu\n",
1584 (void) WriteBlobString(image,buffer);
1585 (void) FormatMagickString(buffer,MaxTextExtent,"/Height %lu\n",image->rows);
1586 (void) WriteBlobString(image,buffer);
1587 (void) FormatMagickString(buffer,MaxTextExtent,"/ColorSpace %lu 0 R\n",
1589 (void) WriteBlobString(image,buffer);
1590 (void) FormatMagickString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
1591 (compression == FaxCompression) || (compression == Group4Compression) ?
1593 (void) WriteBlobString(image,buffer);
1594 if (image->matte != MagickFalse)
1596 (void) FormatMagickString(buffer,MaxTextExtent,"/SMask %lu 0 R\n",
1598 (void) WriteBlobString(image,buffer);
1600 (void) FormatMagickString(buffer,MaxTextExtent,"/Length %lu 0 R\n",
1602 (void) WriteBlobString(image,buffer);
1603 (void) WriteBlobString(image,">>\n");
1604 (void) WriteBlobString(image,"stream\n");
1605 offset=TellBlob(image);
1606 number_pixels=(MagickSizeType) image->columns*image->rows;
1607 if ((4*number_pixels) != (MagickSizeType) ((size_t) (4*number_pixels)))
1608 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1609 if ((compression == FaxCompression) || (compression == Group4Compression) ||
1610 ((image_info->type != TrueColorType) &&
1611 (IsGrayImage(image,&image->exception) != MagickFalse)))
1613 switch (compression)
1615 case FaxCompression:
1616 case Group4Compression:
1618 if (LocaleCompare(CCITTParam,"0") == 0)
1620 (void) HuffmanEncodeImage(image_info,image,image);
1623 (void) Huffman2DEncodeImage(image_info,image,image);
1626 case JPEGCompression:
1628 status=InjectImageBlob(image_info,image,image,"jpeg",
1630 if (status == MagickFalse)
1631 ThrowWriterException(CoderError,image->exception.reason);
1634 case JPEG2000Compression:
1636 status=InjectImageBlob(image_info,image,image,"jp2",
1638 if (status == MagickFalse)
1639 ThrowWriterException(CoderError,image->exception.reason);
1642 case RLECompression:
1646 Allocate pixel array.
1648 length=(size_t) number_pixels;
1649 pixels=(unsigned char *) AcquireQuantumMemory(length,
1651 if (pixels == (unsigned char *) NULL)
1652 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1654 Dump Runlength encoded pixels.
1657 for (y=0; y < (long) image->rows; y++)
1659 p=GetVirtualPixels(image,0,y,image->columns,1,
1661 if (p == (const PixelPacket *) NULL)
1663 for (x=0; x < (long) image->columns; x++)
1665 *q++=ScaleQuantumToChar(PixelIntensityToQuantum(p));
1668 if (image->previous == (Image *) NULL)
1670 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1671 if (status == MagickFalse)
1675 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1676 if (compression == ZipCompression)
1677 status=ZLIBEncodeImage(image,length,pixels);
1680 if (compression == LZWCompression)
1681 status=LZWEncodeImage(image,length,pixels);
1683 status=PackbitsEncodeImage(image,length,pixels);
1684 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1685 if (status == MagickFalse)
1687 (void) CloseBlob(image);
1688 return(MagickFalse);
1695 Dump uncompressed PseudoColor packets.
1697 Ascii85Initialize(image);
1698 for (y=0; y < (long) image->rows; y++)
1700 p=GetVirtualPixels(image,0,y,image->columns,1,
1702 if (p == (const PixelPacket *) NULL)
1704 for (x=0; x < (long) image->columns; x++)
1706 Ascii85Encode(image,
1707 ScaleQuantumToChar(PixelIntensityToQuantum(p)));
1710 if (image->previous == (Image *) NULL)
1712 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1713 if (status == MagickFalse)
1717 Ascii85Flush(image);
1723 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1724 (compression == JPEGCompression) ||
1725 (compression == JPEG2000Compression))
1726 switch (compression)
1728 case JPEGCompression:
1730 status=InjectImageBlob(image_info,image,image,"jpeg",
1732 if (status == MagickFalse)
1733 ThrowWriterException(CoderError,image->exception.reason);
1736 case JPEG2000Compression:
1738 status=InjectImageBlob(image_info,image,image,"jp2",
1740 if (status == MagickFalse)
1741 ThrowWriterException(CoderError,image->exception.reason);
1744 case RLECompression:
1748 Allocate pixel array.
1750 length=(size_t) number_pixels;
1751 pixels=(unsigned char *) AcquireQuantumMemory(length,
1753 length*=image->colorspace == CMYKColorspace ? 4UL : 3UL;
1754 if (pixels == (unsigned char *) NULL)
1755 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1757 Dump runoffset encoded pixels.
1760 for (y=0; y < (long) image->rows; y++)
1762 p=GetVirtualPixels(image,0,y,image->columns,1,
1764 if (p == (const PixelPacket *) NULL)
1766 indexes=GetVirtualIndexQueue(image);
1767 for (x=0; x < (long) image->columns; x++)
1769 *q++=ScaleQuantumToChar(p->red);
1770 *q++=ScaleQuantumToChar(p->green);
1771 *q++=ScaleQuantumToChar(p->blue);
1772 if (image->colorspace == CMYKColorspace)
1773 *q++=ScaleQuantumToChar(indexes[x]);
1776 if (image->previous == (Image *) NULL)
1778 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1779 if (status == MagickFalse)
1783 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1784 if (compression == ZipCompression)
1785 status=ZLIBEncodeImage(image,length,pixels);
1788 if (compression == LZWCompression)
1789 status=LZWEncodeImage(image,length,pixels);
1791 status=PackbitsEncodeImage(image,length,pixels);
1792 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1793 if (status == MagickFalse)
1795 (void) CloseBlob(image);
1796 return(MagickFalse);
1803 Dump uncompressed DirectColor packets.
1805 Ascii85Initialize(image);
1806 for (y=0; y < (long) image->rows; y++)
1808 p=GetVirtualPixels(image,0,y,image->columns,1,
1810 if (p == (const PixelPacket *) NULL)
1812 indexes=GetVirtualIndexQueue(image);
1813 for (x=0; x < (long) image->columns; x++)
1815 Ascii85Encode(image,ScaleQuantumToChar(p->red));
1816 Ascii85Encode(image,ScaleQuantumToChar(p->green));
1817 Ascii85Encode(image,ScaleQuantumToChar(p->blue));
1818 if (image->colorspace == CMYKColorspace)
1819 Ascii85Encode(image,ScaleQuantumToChar(indexes[x]));
1822 if (image->previous == (Image *) NULL)
1824 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1825 if (status == MagickFalse)
1829 Ascii85Flush(image);
1836 Dump number of colors and colormap.
1838 switch (compression)
1840 case RLECompression:
1844 Allocate pixel array.
1846 length=(size_t) number_pixels;
1847 pixels=(unsigned char *) AcquireQuantumMemory(length,
1849 if (pixels == (unsigned char *) NULL)
1850 ThrowWriterException(ResourceLimitError,
1851 "MemoryAllocationFailed");
1853 Dump Runlength encoded pixels.
1856 for (y=0; y < (long) image->rows; y++)
1858 p=GetVirtualPixels(image,0,y,image->columns,1,
1860 if (p == (const PixelPacket *) NULL)
1862 indexes=GetVirtualIndexQueue(image);
1863 for (x=0; x < (long) image->columns; x++)
1864 *q++=(unsigned char) indexes[x];
1865 if (image->previous == (Image *) NULL)
1867 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1868 if (status == MagickFalse)
1872 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1873 if (compression == ZipCompression)
1874 status=ZLIBEncodeImage(image,length,pixels);
1877 if (compression == LZWCompression)
1878 status=LZWEncodeImage(image,length,pixels);
1880 status=PackbitsEncodeImage(image,length,pixels);
1881 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1882 if (status == MagickFalse)
1884 (void) CloseBlob(image);
1885 return(MagickFalse);
1892 Dump uncompressed PseudoColor packets.
1894 Ascii85Initialize(image);
1895 for (y=0; y < (long) image->rows; y++)
1897 p=GetVirtualPixels(image,0,y,image->columns,1,
1899 if (p == (const PixelPacket *) NULL)
1901 indexes=GetVirtualIndexQueue(image);
1902 for (x=0; x < (long) image->columns; x++)
1903 Ascii85Encode(image,(unsigned char) indexes[x]);
1904 if (image->previous == (Image *) NULL)
1906 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1907 if (status == MagickFalse)
1911 Ascii85Flush(image);
1916 offset=TellBlob(image)-offset;
1917 (void) WriteBlobString(image,"\nendstream\n");
1918 (void) WriteBlobString(image,"endobj\n");
1920 Write Length object.
1922 xref[object++]=TellBlob(image);
1923 (void) FormatMagickString(buffer,MaxTextExtent,"%lu 0 obj\n",object);
1924 (void) WriteBlobString(image,buffer);
1925 (void) FormatMagickString(buffer,MaxTextExtent,"%lu\n",
1926 (unsigned long) offset);
1927 (void) WriteBlobString(image,buffer);
1928 (void) WriteBlobString(image,"endobj\n");
1930 Write Colorspace object.
1932 xref[object++]=TellBlob(image);
1933 (void) FormatMagickString(buffer,MaxTextExtent,"%lu 0 obj\n",object);
1934 (void) WriteBlobString(image,buffer);
1935 if (image->colorspace == CMYKColorspace)
1936 (void) CopyMagickString(buffer,"/DeviceCMYK\n",MaxTextExtent);
1938 if ((compression == FaxCompression) ||
1939 (compression == Group4Compression) ||
1940 ((image_info->type != TrueColorType) &&
1941 (IsGrayImage(image,&image->exception) != MagickFalse)))
1942 (void) CopyMagickString(buffer,"/DeviceGray\n",MaxTextExtent);
1944 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1945 (compression == JPEGCompression) ||
1946 (compression == JPEG2000Compression))
1947 (void) CopyMagickString(buffer,"/DeviceRGB\n",MaxTextExtent);
1949 (void) FormatMagickString(buffer,MaxTextExtent,
1950 "[ /Indexed /DeviceRGB %lu %lu 0 R ]\n",
1951 image->colors-1,object+3);
1952 (void) WriteBlobString(image,buffer);
1953 (void) WriteBlobString(image,"endobj\n");
1957 SetGeometry(image,&geometry);
1958 (void) ParseMetaGeometry("106x106+0+0>",&geometry.x,&geometry.y,
1959 &geometry.width,&geometry.height);
1960 tile_image=ThumbnailImage(image,geometry.width,geometry.height,
1962 if (tile_image == (Image *) NULL)
1963 ThrowWriterException(ResourceLimitError,image->exception.reason);
1964 xref[object++]=TellBlob(image);
1965 (void) FormatMagickString(buffer,MaxTextExtent,"%lu 0 obj\n",object);
1966 (void) WriteBlobString(image,buffer);
1967 (void) WriteBlobString(image,"<<\n");
1968 switch (compression)
1972 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"ASCII85Decode");
1975 case JPEGCompression:
1977 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"DCTDecode");
1978 if (image->colorspace != CMYKColorspace)
1980 (void) WriteBlobString(image,buffer);
1981 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1985 case JPEG2000Compression:
1987 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"JPXDecode");
1988 if (image->colorspace != CMYKColorspace)
1990 (void) WriteBlobString(image,buffer);
1991 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1995 case LZWCompression:
1997 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"LZWDecode");
2000 case ZipCompression:
2002 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"FlateDecode");
2005 case FaxCompression:
2006 case Group4Compression:
2008 (void) CopyMagickString(buffer,"/Filter [ /CCITTFaxDecode ]\n",
2010 (void) WriteBlobString(image,buffer);
2011 (void) FormatMagickString(buffer,MaxTextExtent,"/DecodeParms [ << "
2012 "/K %s /BlackIs1 true /Columns %lu /Rows %lu >> ]\n",CCITTParam,
2013 tile_image->columns,tile_image->rows);
2018 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,
2023 (void) WriteBlobString(image,buffer);
2024 (void) FormatMagickString(buffer,MaxTextExtent,"/Width %lu\n",
2025 tile_image->columns);
2026 (void) WriteBlobString(image,buffer);
2027 (void) FormatMagickString(buffer,MaxTextExtent,"/Height %lu\n",
2029 (void) WriteBlobString(image,buffer);
2030 (void) FormatMagickString(buffer,MaxTextExtent,"/ColorSpace %lu 0 R\n",
2032 (void) WriteBlobString(image,buffer);
2033 (void) FormatMagickString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
2034 (compression == FaxCompression) || (compression == Group4Compression) ?
2036 (void) WriteBlobString(image,buffer);
2037 (void) FormatMagickString(buffer,MaxTextExtent,"/Length %lu 0 R\n",
2039 (void) WriteBlobString(image,buffer);
2040 (void) WriteBlobString(image,">>\n");
2041 (void) WriteBlobString(image,"stream\n");
2042 offset=TellBlob(image);
2043 number_pixels=(MagickSizeType) tile_image->columns*tile_image->rows;
2044 if ((compression == FaxCompression) ||
2045 (compression == Group4Compression) ||
2046 ((image_info->type != TrueColorType) &&
2047 (IsGrayImage(tile_image,&image->exception) != MagickFalse)))
2049 switch (compression)
2051 case FaxCompression:
2052 case Group4Compression:
2054 if (LocaleCompare(CCITTParam,"0") == 0)
2056 (void) HuffmanEncodeImage(image_info,image,tile_image);
2059 (void) Huffman2DEncodeImage(image_info,image,tile_image);
2062 case JPEGCompression:
2064 status=InjectImageBlob(image_info,image,tile_image,"jpeg",
2066 if (status == MagickFalse)
2067 ThrowWriterException(CoderError,tile_image->exception.reason);
2070 case JPEG2000Compression:
2072 status=InjectImageBlob(image_info,image,tile_image,"jp2",
2074 if (status == MagickFalse)
2075 ThrowWriterException(CoderError,tile_image->exception.reason);
2078 case RLECompression:
2082 Allocate pixel array.
2084 length=(size_t) number_pixels;
2085 pixels=(unsigned char *) AcquireQuantumMemory(length,
2087 if (pixels == (unsigned char *) NULL)
2089 tile_image=DestroyImage(tile_image);
2090 ThrowWriterException(ResourceLimitError,
2091 "MemoryAllocationFailed");
2094 Dump Runlength encoded pixels.
2097 for (y=0; y < (long) tile_image->rows; y++)
2099 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2100 &tile_image->exception);
2101 if (p == (const PixelPacket *) NULL)
2103 for (x=0; x < (long) tile_image->columns; x++)
2105 *q++=ScaleQuantumToChar(PixelIntensityToQuantum(p));
2109 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2110 if (compression == ZipCompression)
2111 status=ZLIBEncodeImage(image,length,pixels);
2114 if (compression == LZWCompression)
2115 status=LZWEncodeImage(image,length,pixels);
2117 status=PackbitsEncodeImage(image,length,pixels);
2118 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2119 if (status == MagickFalse)
2121 (void) CloseBlob(image);
2122 return(MagickFalse);
2129 Dump uncompressed PseudoColor packets.
2131 Ascii85Initialize(image);
2132 for (y=0; y < (long) tile_image->rows; y++)
2134 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2135 &tile_image->exception);
2136 if (p == (const PixelPacket *) NULL)
2138 for (x=0; x < (long) tile_image->columns; x++)
2140 Ascii85Encode(image,
2141 ScaleQuantumToChar(PixelIntensityToQuantum(p)));
2145 Ascii85Flush(image);
2151 if ((tile_image->storage_class == DirectClass) ||
2152 (tile_image->colors > 256) || (compression == JPEGCompression) ||
2153 (compression == JPEG2000Compression))
2154 switch (compression)
2156 case JPEGCompression:
2158 status=InjectImageBlob(image_info,image,tile_image,"jpeg",
2160 if (status == MagickFalse)
2161 ThrowWriterException(CoderError,tile_image->exception.reason);
2164 case JPEG2000Compression:
2166 status=InjectImageBlob(image_info,image,tile_image,"jp2",
2168 if (status == MagickFalse)
2169 ThrowWriterException(CoderError,tile_image->exception.reason);
2172 case RLECompression:
2176 Allocate pixel array.
2178 length=(size_t) number_pixels;
2179 pixels=(unsigned char *) AcquireQuantumMemory(length,4*
2181 length*=tile_image->colorspace == CMYKColorspace ? 4UL : 3UL;
2182 if (pixels == (unsigned char *) NULL)
2184 tile_image=DestroyImage(tile_image);
2185 ThrowWriterException(ResourceLimitError,
2186 "MemoryAllocationFailed");
2189 Dump runoffset encoded pixels.
2192 for (y=0; y < (long) tile_image->rows; y++)
2194 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2195 &tile_image->exception);
2196 if (p == (const PixelPacket *) NULL)
2198 indexes=GetVirtualIndexQueue(tile_image);
2199 for (x=0; x < (long) tile_image->columns; x++)
2201 *q++=ScaleQuantumToChar(p->red);
2202 *q++=ScaleQuantumToChar(p->green);
2203 *q++=ScaleQuantumToChar(p->blue);
2204 if (image->colorspace == CMYKColorspace)
2205 *q++=ScaleQuantumToChar(indexes[x]);
2209 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2210 if (compression == ZipCompression)
2211 status=ZLIBEncodeImage(image,length,pixels);
2214 if (compression == LZWCompression)
2215 status=LZWEncodeImage(image,length,pixels);
2217 status=PackbitsEncodeImage(image,length,pixels);
2218 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2219 if (status == MagickFalse)
2221 (void) CloseBlob(image);
2222 return(MagickFalse);
2229 Dump uncompressed DirectColor packets.
2231 Ascii85Initialize(image);
2232 for (y=0; y < (long) tile_image->rows; y++)
2234 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2235 &tile_image->exception);
2236 if (p == (const PixelPacket *) NULL)
2238 indexes=GetVirtualIndexQueue(tile_image);
2239 for (x=0; x < (long) tile_image->columns; x++)
2241 Ascii85Encode(image,ScaleQuantumToChar(p->red));
2242 Ascii85Encode(image,ScaleQuantumToChar(p->green));
2243 Ascii85Encode(image,ScaleQuantumToChar(p->blue));
2244 if (image->colorspace == CMYKColorspace)
2245 Ascii85Encode(image,ScaleQuantumToChar(indexes[x]));
2249 Ascii85Flush(image);
2256 Dump number of colors and colormap.
2258 switch (compression)
2260 case RLECompression:
2264 Allocate pixel array.
2266 length=(size_t) number_pixels;
2267 pixels=(unsigned char *) AcquireQuantumMemory(length,
2269 if (pixels == (unsigned char *) NULL)
2271 tile_image=DestroyImage(tile_image);
2272 ThrowWriterException(ResourceLimitError,
2273 "MemoryAllocationFailed");
2276 Dump Runlength encoded pixels.
2279 for (y=0; y < (long) tile_image->rows; y++)
2281 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2282 &tile_image->exception);
2283 if (p == (const PixelPacket *) NULL)
2285 indexes=GetVirtualIndexQueue(tile_image);
2286 for (x=0; x < (long) tile_image->columns; x++)
2287 *q++=(unsigned char) indexes[x];
2289 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2290 if (compression == ZipCompression)
2291 status=ZLIBEncodeImage(image,length,pixels);
2294 if (compression == LZWCompression)
2295 status=LZWEncodeImage(image,length,pixels);
2297 status=PackbitsEncodeImage(image,length,pixels);
2298 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2299 if (status == MagickFalse)
2301 (void) CloseBlob(image);
2302 return(MagickFalse);
2309 Dump uncompressed PseudoColor packets.
2311 Ascii85Initialize(image);
2312 for (y=0; y < (long) tile_image->rows; y++)
2314 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2315 &tile_image->exception);
2316 if (p == (const PixelPacket *) NULL)
2318 indexes=GetVirtualIndexQueue(tile_image);
2319 for (x=0; x < (long) tile_image->columns; x++)
2320 Ascii85Encode(image,(unsigned char) indexes[x]);
2322 Ascii85Flush(image);
2327 tile_image=DestroyImage(tile_image);
2328 offset=TellBlob(image)-offset;
2329 (void) WriteBlobString(image,"\nendstream\n");
2330 (void) WriteBlobString(image,"endobj\n");
2332 Write Length object.
2334 xref[object++]=TellBlob(image);
2335 (void) FormatMagickString(buffer,MaxTextExtent,"%lu 0 obj\n",object);
2336 (void) WriteBlobString(image,buffer);
2337 (void) FormatMagickString(buffer,MaxTextExtent,"%lu\n",
2338 (unsigned long) offset);
2339 (void) WriteBlobString(image,buffer);
2340 (void) WriteBlobString(image,"endobj\n");
2341 xref[object++]=TellBlob(image);
2342 (void) FormatMagickString(buffer,MaxTextExtent,"%lu 0 obj\n",object);
2343 (void) WriteBlobString(image,buffer);
2344 if ((image->storage_class != DirectClass) && (image->colors <= 256) &&
2345 (compression != FaxCompression) && (compression != Group4Compression))
2348 Write Colormap object.
2350 (void) WriteBlobString(image,"<<\n");
2351 if (compression == NoCompression)
2352 (void) WriteBlobString(image,"/Filter [ /ASCII85Decode ]\n");
2353 (void) FormatMagickString(buffer,MaxTextExtent,"/Length %lu 0 R\n",
2355 (void) WriteBlobString(image,buffer);
2356 (void) WriteBlobString(image,">>\n");
2357 (void) WriteBlobString(image,"stream\n");
2358 offset=TellBlob(image);
2359 if (compression == NoCompression)
2360 Ascii85Initialize(image);
2361 for (i=0; i < (long) image->colors; i++)
2363 if (compression == NoCompression)
2365 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].red));
2366 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].green));
2367 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].blue));
2370 (void) WriteBlobByte(image,
2371 ScaleQuantumToChar(image->colormap[i].red));
2372 (void) WriteBlobByte(image,
2373 ScaleQuantumToChar(image->colormap[i].green));
2374 (void) WriteBlobByte(image,
2375 ScaleQuantumToChar(image->colormap[i].blue));
2377 if (compression == NoCompression)
2378 Ascii85Flush(image);
2379 offset=TellBlob(image)-offset;
2380 (void) WriteBlobString(image,"\nendstream\n");
2382 (void) WriteBlobString(image,"endobj\n");
2384 Write Length object.
2386 xref[object++]=TellBlob(image);
2387 (void) FormatMagickString(buffer,MaxTextExtent,"%lu 0 obj\n",object);
2388 (void) WriteBlobString(image,buffer);
2389 (void) FormatMagickString(buffer,MaxTextExtent,"%lu\n",
2390 (unsigned long) offset);
2391 (void) WriteBlobString(image,buffer);
2392 (void) WriteBlobString(image,"endobj\n");
2394 Write softmask object.
2396 xref[object++]=TellBlob(image);
2397 (void) FormatMagickString(buffer,MaxTextExtent,"%lu 0 obj\n",object);
2398 (void) WriteBlobString(image,buffer);
2399 (void) WriteBlobString(image,"<<\n");
2400 if (image->matte == MagickFalse)
2401 (void) WriteBlobString(image,">>\n");
2404 (void) WriteBlobString(image,"/Type /XObject\n");
2405 (void) WriteBlobString(image,"/Subtype /Image\n");
2406 (void) FormatMagickString(buffer,MaxTextExtent,"/Name /Ma%lu\n",
2408 (void) WriteBlobString(image,buffer);
2409 switch (compression)
2413 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,
2417 case LZWCompression:
2419 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"LZWDecode");
2422 case ZipCompression:
2424 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,
2430 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,
2435 (void) WriteBlobString(image,buffer);
2436 (void) FormatMagickString(buffer,MaxTextExtent,"/Width %lu\n",
2438 (void) WriteBlobString(image,buffer);
2439 (void) FormatMagickString(buffer,MaxTextExtent,"/Height %lu\n",
2441 (void) WriteBlobString(image,buffer);
2442 (void) WriteBlobString(image,"/ColorSpace /DeviceGray\n");
2443 (void) FormatMagickString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
2444 (compression == FaxCompression) || (compression == Group4Compression)
2446 (void) WriteBlobString(image,buffer);
2447 (void) FormatMagickString(buffer,MaxTextExtent,"/Length %lu 0 R\n",
2449 (void) WriteBlobString(image,buffer);
2450 (void) WriteBlobString(image,">>\n");
2451 (void) WriteBlobString(image,"stream\n");
2452 offset=TellBlob(image);
2453 number_pixels=(MagickSizeType) image->columns*image->rows;
2454 switch (compression)
2456 case RLECompression:
2460 Allocate pixel array.
2462 length=(size_t) number_pixels;
2463 pixels=(unsigned char *) AcquireQuantumMemory(length,
2465 if (pixels == (unsigned char *) NULL)
2467 image=DestroyImage(image);
2468 ThrowWriterException(ResourceLimitError,
2469 "MemoryAllocationFailed");
2472 Dump Runlength encoded pixels.
2475 for (y=0; y < (long) image->rows; y++)
2477 p=GetVirtualPixels(image,0,y,image->columns,1,
2479 if (p == (const PixelPacket *) NULL)
2481 for (x=0; x < (long) image->columns; x++)
2483 *q++=ScaleQuantumToChar((Quantum) (QuantumRange-p->opacity));
2487 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2488 if (compression == ZipCompression)
2489 status=ZLIBEncodeImage(image,length,pixels);
2492 if (compression == LZWCompression)
2493 status=LZWEncodeImage(image,length,pixels);
2495 status=PackbitsEncodeImage(image,length,pixels);
2496 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2497 if (status == MagickFalse)
2499 (void) CloseBlob(image);
2500 return(MagickFalse);
2507 Dump uncompressed PseudoColor packets.
2509 Ascii85Initialize(image);
2510 for (y=0; y < (long) image->rows; y++)
2512 p=GetVirtualPixels(image,0,y,image->columns,1,
2514 if (p == (const PixelPacket *) NULL)
2516 for (x=0; x < (long) image->columns; x++)
2518 Ascii85Encode(image,ScaleQuantumToChar((Quantum) (QuantumRange-
2523 Ascii85Flush(image);
2527 offset=TellBlob(image)-offset;
2528 (void) WriteBlobString(image,"\nendstream\n");
2530 (void) WriteBlobString(image,"endobj\n");
2532 Write Length object.
2534 xref[object++]=TellBlob(image);
2535 (void) FormatMagickString(buffer,MaxTextExtent,"%lu 0 obj\n",object);
2536 (void) WriteBlobString(image,buffer);
2537 (void) FormatMagickString(buffer,MaxTextExtent,"%lu\n",(unsigned long)
2539 (void) WriteBlobString(image,buffer);
2540 (void) WriteBlobString(image,"endobj\n");
2541 if (GetNextImageInList(image) == (Image *) NULL)
2543 image=SyncNextImageInList(image);
2544 status=SetImageProgress(image,SaveImagesTag,scene++,
2545 GetImageListLength(image));
2546 if (status == MagickFalse)
2548 } while (image_info->adjoin != MagickFalse);
2550 Write Metadata object.
2552 xref[object++]=TellBlob(image);
2554 (void) FormatMagickString(buffer,MaxTextExtent,"%lu 0 obj\n",object);
2555 (void) WriteBlobString(image,buffer);
2556 (void) WriteBlobString(image,"<<\n");
2557 (void) FormatMagickString(buffer,MaxTextExtent,"/Title (%s)\n",
2558 EscapeParenthesis(image->filename));
2559 (void) WriteBlobString(image,buffer);
2560 seconds=time((time_t *) NULL);
2561 #if defined(MAGICKCORE_HAVE_LOCALTIME_R)
2562 (void) localtime_r(&seconds,&local_time);
2564 (void) memcpy(&local_time,localtime(&seconds),sizeof(local_time));
2566 (void) FormatMagickString(date,MaxTextExtent,"D:%04d%02d%02d%02d%02d%02d",
2567 local_time.tm_year+1900,local_time.tm_mon+1,local_time.tm_mday,
2568 local_time.tm_hour,local_time.tm_min,local_time.tm_sec);
2569 (void) FormatMagickString(buffer,MaxTextExtent,"/CreationDate (%s)\n",date);
2570 (void) WriteBlobString(image,buffer);
2571 (void) FormatMagickString(buffer,MaxTextExtent,"/ModDate (%s)\n",date);
2572 (void) WriteBlobString(image,buffer);
2573 (void) FormatMagickString(buffer,MaxTextExtent,"/Producer (%s)\n",
2574 EscapeParenthesis(GetMagickVersion((unsigned long *) NULL)));
2575 (void) WriteBlobString(image,buffer);
2576 (void) WriteBlobString(image,">>\n");
2577 (void) WriteBlobString(image,"endobj\n");
2581 offset=TellBlob(image)-xref[0]+10;
2582 (void) WriteBlobString(image,"xref\n");
2583 (void) FormatMagickString(buffer,MaxTextExtent,"0 %lu\n",object+1);
2584 (void) WriteBlobString(image,buffer);
2585 (void) WriteBlobString(image,"0000000000 65535 f \n");
2586 for (i=0; i < (long) object; i++)
2588 (void) FormatMagickString(buffer,MaxTextExtent,"%010lu 00000 n \n",
2589 (unsigned long) xref[i]);
2590 (void) WriteBlobString(image,buffer);
2592 (void) WriteBlobString(image,"trailer\n");
2593 (void) WriteBlobString(image,"<<\n");
2594 (void) FormatMagickString(buffer,MaxTextExtent,"/Size %lu\n",object+1);
2595 (void) WriteBlobString(image,buffer);
2596 (void) FormatMagickString(buffer,MaxTextExtent,"/Info %lu 0 R\n",info_id);
2597 (void) WriteBlobString(image,buffer);
2598 (void) FormatMagickString(buffer,MaxTextExtent,"/Root %lu 0 R\n",root_id);
2599 (void) WriteBlobString(image,buffer);
2600 (void) WriteBlobString(image,">>\n");
2601 (void) WriteBlobString(image,"startxref\n");
2602 (void) FormatMagickString(buffer,MaxTextExtent,"%lu\n",
2603 (unsigned long) offset);
2604 (void) WriteBlobString(image,buffer);
2605 (void) WriteBlobString(image,"%%EOF\n");
2606 xref=(MagickOffsetType *) RelinquishMagickMemory(xref);
2607 (void) CloseBlob(image);