2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Portable Document Format %
20 % Copyright 1999-2010 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(__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(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(__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(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 ICCBased "ICCBased"
294 #define MediaBox "MediaBox"
295 #define RenderPostscriptText "Rendering Postscript... "
296 #define PDFRotate "Rotate"
297 #define SpotColor "Separation"
298 #define TrimBox "TrimBox"
299 #define PDFVersion "PDF-"
302 command[MaxTextExtent],
303 density[MaxTextExtent],
304 filename[MaxTextExtent],
305 geometry[MaxTextExtent],
306 options[MaxTextExtent],
307 input_filename[MaxTextExtent],
308 postscript_filename[MaxTextExtent];
360 assert(image_info != (const ImageInfo *) NULL);
361 assert(image_info->signature == MagickSignature);
362 if (image_info->debug != MagickFalse)
363 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
364 image_info->filename);
365 assert(exception != (ExceptionInfo *) NULL);
366 assert(exception->signature == MagickSignature);
370 image=AcquireImage(image_info);
371 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
372 if (status == MagickFalse)
374 image=DestroyImageList(image);
375 return((Image *) NULL);
377 status=AcquireUniqueSymbolicLink(image_info->filename,input_filename);
378 if (status == MagickFalse)
380 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
381 image_info->filename);
382 image=DestroyImageList(image);
383 return((Image *) NULL);
386 Set the page density.
388 delta.x=DefaultResolution;
389 delta.y=DefaultResolution;
390 if ((image->x_resolution == 0.0) || (image->y_resolution == 0.0))
398 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
399 image->x_resolution=geometry_info.rho;
400 image->y_resolution=geometry_info.sigma;
401 if ((flags & SigmaValue) == 0)
402 image->y_resolution=image->x_resolution;
405 Determine page geometry from the PDF media box.
407 cmyk=image_info->colorspace == CMYKColorspace ? MagickTrue : MagickFalse;
409 option=GetImageOption(image_info,"pdf:use-cropbox");
410 if (option != (const char *) NULL)
411 cropbox=IsMagickTrue(option);
413 option=GetImageOption(image_info,"pdf:use-trimbox");
414 if (option != (const char *) NULL)
415 trimbox=IsMagickTrue(option);
418 (void) ResetMagickMemory(&bounding_box,0,sizeof(bounding_box));
419 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
420 (void) ResetMagickMemory(&hires_bounds,0,sizeof(hires_bounds));
421 (void) ResetMagickMemory(&page,0,sizeof(page));
422 (void) ResetMagickMemory(command,0,sizeof(command));
427 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
435 if ((c != (int) '/') && (c != (int) '%') &&
436 ((size_t) (p-command) < (MaxTextExtent-1)))
440 if (LocaleNCompare(PDFRotate,command,strlen(PDFRotate)) == 0)
441 count=(ssize_t) sscanf(command,"Rotate %lf",&angle);
443 Is this a CMYK document?
445 if (LocaleNCompare(DeviceCMYK,command,strlen(DeviceCMYK)) == 0)
447 if (LocaleNCompare(ICCbased,command,strlen(ICCbased)) == 0)
449 if (LocaleNCompare(SpotColor,command,strlen(SpotColor)) == 0)
453 property[MaxTextExtent],
462 (void) FormatMagickString(property,MaxTextExtent,"pdf:SpotColor-%lu",
465 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
467 if ((isspace(c) != 0) || (c == '/') || ((i+1) == MaxTextExtent))
472 value=AcquireString(name);
473 (void) SubstituteString(&value,"#20"," ");
474 (void) SetImageProperty(image,property,value);
475 value=DestroyString(value);
478 if (LocaleNCompare(PDFVersion,command,strlen(PDFVersion)) == 0)
479 (void) SetImageProperty(image,"pdf:Version",command);
481 if (cropbox != MagickFalse)
483 if (LocaleNCompare(CropBox,command,strlen(CropBox)) == 0)
486 Note region defined by crop box.
488 count=(ssize_t) sscanf(command,"CropBox [%lf %lf %lf %lf",
489 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
491 count=(ssize_t) sscanf(command,"CropBox[%lf %lf %lf %lf",
492 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
496 if (trimbox != MagickFalse)
498 if (LocaleNCompare(TrimBox,command,strlen(TrimBox)) == 0)
501 Note region defined by trim box.
503 count=(ssize_t) sscanf(command,"TrimBox [%lf %lf %lf %lf",
504 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
506 count=(ssize_t) sscanf(command,"TrimBox[%lf %lf %lf %lf",
507 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
511 if (LocaleNCompare(MediaBox,command,strlen(MediaBox)) == 0)
514 Note region defined by media box.
516 count=(ssize_t) sscanf(command,"MediaBox [%lf %lf %lf %lf",
517 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
519 count=(ssize_t) sscanf(command,"MediaBox[%lf %lf %lf %lf",
520 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
524 if (((bounds.x2 > hires_bounds.x2) && (bounds.y2 > hires_bounds.y2)) ||
525 ((hires_bounds.x2 == 0.0) && (hires_bounds.y2 == 0.0)))
528 Set PDF render geometry.
530 (void) FormatMagickString(geometry,MaxTextExtent,
531 "%gx%g%+.15g%+.15g",bounds.x2-bounds.x1,bounds.y2-bounds.y1,
532 bounds.x1,bounds.y1);
533 (void) SetImageProperty(image,"pdf:HiResBoundingBox",geometry);
534 page.width=(unsigned long) (bounds.x2-bounds.x1+0.5);
535 page.height=(unsigned long) (bounds.y2-bounds.y1+0.5);
539 (void) CloseBlob(image);
540 if ((fabs(angle) == 90.0) || (fabs(angle) == 270.0))
546 page.width=page.height;
549 if (image_info->colorspace == RGBColorspace)
552 Create Ghostscript control file.
554 file=AcquireUniqueFileResource(postscript_filename);
557 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
558 image_info->filename);
559 image=DestroyImage(image);
560 return((Image *) NULL);
562 count=write(file," ",1);
565 Render Postscript with the Ghostscript delegate.
567 if ((image_info->ping != MagickFalse) ||
568 (image_info->monochrome != MagickFalse))
569 delegate_info=GetDelegateInfo("ps:mono",(char *) NULL,exception);
571 if (cmyk != MagickFalse)
572 delegate_info=GetDelegateInfo("ps:cmyk",(char *) NULL,exception);
574 if (LocaleCompare(image_info->magick,"AI") == 0)
575 delegate_info=GetDelegateInfo("ps:alpha",(char *) NULL,exception);
577 delegate_info=GetDelegateInfo("ps:color",(char *) NULL,exception);
578 if (delegate_info == (const DelegateInfo *) NULL)
580 (void) RelinquishUniqueFileResource(postscript_filename);
581 image=DestroyImage(image);
582 return((Image *) NULL);
585 (void) FormatMagickString(density,MaxTextExtent,"%gx%g",
586 image->x_resolution,image->y_resolution);
587 if (image_info->page != (char *) NULL)
589 (void) ParseAbsoluteGeometry(image_info->page,&page);
590 page.width=(unsigned long) (page.width*image->x_resolution/delta.x+0.5);
591 page.height=(unsigned long) (page.height*image->y_resolution/delta.y+0.5);
592 (void) FormatMagickString(options,MaxTextExtent,"-g%lux%lu ",page.width,
595 if (cmyk != MagickFalse)
596 (void) ConcatenateMagickString(options,"-dUseCIEColor ",MaxTextExtent);
597 if (cropbox != MagickFalse)
598 (void) ConcatenateMagickString(options,"-dUseCropBox ",MaxTextExtent);
599 if (trimbox != MagickFalse)
600 (void) ConcatenateMagickString(options,"-dUseTrimBox ",MaxTextExtent);
601 read_info=CloneImageInfo(image_info);
602 *read_info->magick='\0';
603 if (read_info->number_scenes != 0)
606 pages[MaxTextExtent];
608 (void) FormatMagickString(pages,MaxTextExtent,"-dFirstPage=%lu "
609 "-dLastPage=%lu",read_info->scene+1,read_info->scene+
610 read_info->number_scenes);
611 (void) ConcatenateMagickString(options,pages,MaxTextExtent);
612 read_info->number_scenes=0;
613 if (read_info->scenes != (char *) NULL)
614 *read_info->scenes='\0';
616 if (read_info->authenticate != (char *) NULL)
617 (void) FormatMagickString(options+strlen(options),MaxTextExtent,
618 " -sPDFPassword=%s",read_info->authenticate);
619 (void) CopyMagickString(filename,read_info->filename,MaxTextExtent);
620 (void) AcquireUniqueFilename(read_info->filename);
621 (void) FormatMagickString(command,MaxTextExtent,
622 GetDelegateCommands(delegate_info),
623 read_info->antialias != MagickFalse ? 4 : 1,
624 read_info->antialias != MagickFalse ? 4 : 1,density,options,
625 read_info->filename,postscript_filename,input_filename);
626 status=InvokePDFDelegate(read_info->verbose,command,exception);
627 pdf_image=(Image *) NULL;
628 if ((status != MagickFalse) &&
629 (IsPDFRendered(read_info->filename) != MagickFalse))
630 pdf_image=ReadImage(read_info,exception);
631 (void) RelinquishUniqueFileResource(postscript_filename);
632 (void) RelinquishUniqueFileResource(read_info->filename);
633 (void) RelinquishUniqueFileResource(input_filename);
634 read_info=DestroyImageInfo(read_info);
635 if (pdf_image == (Image *) NULL)
637 ThrowFileException(exception,DelegateError,"PostscriptDelegateFailed",
638 image_info->filename);
639 return((Image *) NULL);
641 if (LocaleCompare(pdf_image->magick,"BMP") == 0)
646 cmyk_image=ConsolidateCMYKImages(pdf_image,exception);
647 if (cmyk_image != (Image *) NULL)
649 pdf_image=DestroyImageList(pdf_image);
650 pdf_image=cmyk_image;
653 if (image_info->number_scenes != 0)
662 Add place holder images to meet the subimage specification requirement.
664 for (i=0; i < (long) image_info->scene; i++)
666 clone_image=CloneImage(pdf_image,1,1,MagickTrue,exception);
667 if (clone_image != (Image *) NULL)
668 PrependImageToList(&pdf_image,clone_image);
673 (void) CopyMagickString(pdf_image->filename,filename,MaxTextExtent);
674 pdf_image->page=page;
675 (void) CloneImageProfiles(pdf_image,image);
676 (void) CloneImageProperties(pdf_image,image);
677 next=SyncNextImageInList(pdf_image);
678 if (next != (Image *) NULL)
680 } while (next != (Image *) NULL);
681 image=DestroyImage(image);
683 for (next=GetFirstImageInList(pdf_image); next != (Image *) NULL; )
686 next=GetNextImageInList(next);
688 return(GetFirstImageInList(pdf_image));
692 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
696 % R e g i s t e r P D F I m a g e %
700 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
702 % RegisterPDFImage() adds properties for the PDF image format to
703 % the list of supported formats. The properties include the image format
704 % tag, a method to read and/or write the format, whether the format
705 % supports the saving of more than one frame to the same file or blob,
706 % whether the format supports native in-memory I/O, and a brief
707 % description of the format.
709 % The format of the RegisterPDFImage method is:
711 % unsigned long RegisterPDFImage(void)
714 ModuleExport unsigned long RegisterPDFImage(void)
719 entry=SetMagickInfo("AI");
720 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
721 entry->encoder=(EncodeImageHandler *) WritePDFImage;
722 entry->adjoin=MagickFalse;
723 entry->blob_support=MagickFalse;
724 entry->seekable_stream=MagickTrue;
725 entry->thread_support=EncoderThreadSupport;
726 entry->description=ConstantString("Adobe Illustrator CS2");
727 entry->module=ConstantString("PDF");
728 (void) RegisterMagickInfo(entry);
729 entry=SetMagickInfo("EPDF");
730 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
731 entry->encoder=(EncodeImageHandler *) WritePDFImage;
732 entry->adjoin=MagickFalse;
733 entry->blob_support=MagickFalse;
734 entry->seekable_stream=MagickTrue;
735 entry->thread_support=EncoderThreadSupport;
736 entry->description=ConstantString("Encapsulated Portable Document Format");
737 entry->module=ConstantString("PDF");
738 (void) RegisterMagickInfo(entry);
739 entry=SetMagickInfo("PDF");
740 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
741 entry->encoder=(EncodeImageHandler *) WritePDFImage;
742 entry->magick=(IsImageFormatHandler *) IsPDF;
743 entry->blob_support=MagickFalse;
744 entry->seekable_stream=MagickTrue;
745 entry->thread_support=EncoderThreadSupport;
746 entry->description=ConstantString("Portable Document Format");
747 entry->module=ConstantString("PDF");
748 (void) RegisterMagickInfo(entry);
749 entry=SetMagickInfo("PDFA");
750 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
751 entry->encoder=(EncodeImageHandler *) WritePDFImage;
752 entry->magick=(IsImageFormatHandler *) IsPDF;
753 entry->blob_support=MagickFalse;
754 entry->seekable_stream=MagickTrue;
755 entry->thread_support=EncoderThreadSupport;
756 entry->description=ConstantString("Portable Document Archive Format");
757 entry->module=ConstantString("PDF");
758 (void) RegisterMagickInfo(entry);
759 return(MagickImageCoderSignature);
763 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
767 % U n r e g i s t e r P D F I m a g e %
771 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
773 % UnregisterPDFImage() removes format registrations made by the
774 % PDF module from the list of supported formats.
776 % The format of the UnregisterPDFImage method is:
778 % UnregisterPDFImage(void)
781 ModuleExport void UnregisterPDFImage(void)
783 (void) UnregisterMagickInfo("AI");
784 (void) UnregisterMagickInfo("EPDF");
785 (void) UnregisterMagickInfo("PDF");
786 (void) UnregisterMagickInfo("PDFA");
790 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
794 % W r i t e P D F I m a g e %
798 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
800 % WritePDFImage() writes an image in the Portable Document image
803 % The format of the WritePDFImage method is:
805 % MagickBooleanType WritePDFImage(const ImageInfo *image_info,Image *image)
807 % A description of each parameter follows.
809 % o image_info: the image info.
811 % o image: The image.
815 static inline size_t MagickMax(const size_t x,const size_t y)
822 static inline size_t MagickMin(const size_t x,const size_t y)
829 static char *EscapeParenthesis(const char *text)
838 buffer[MaxTextExtent];
845 for (i=0; i < (long) MagickMin(strlen(text),(MaxTextExtent-escapes-1)); i++)
847 if ((text[i] == '(') || (text[i] == ')'))
858 static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
859 Image *image,Image *inject_image)
877 write_info=CloneImageInfo(image_info);
878 (void) CopyMagickString(write_info->filename,"GROUP4:",MaxTextExtent);
879 (void) CopyMagickString(write_info->magick,"GROUP4",MaxTextExtent);
880 group4_image=CloneImage(inject_image,0,0,MagickTrue,&image->exception);
881 if (group4_image == (Image *) NULL)
883 group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
885 group4_image=DestroyImage(group4_image);
886 if (group4 == (unsigned char *) NULL)
888 write_info=DestroyImageInfo(write_info);
889 if (WriteBlob(image,length,group4) != (ssize_t) length)
891 group4=(unsigned char *) RelinquishMagickMemory(group4);
895 static MagickBooleanType WritePDFImage(const ImageInfo *image_info,Image *image)
897 #define CFormat "/Filter [ /%s ]\n"
898 #define ObjectsPerImage 14
903 "<?xpacket begin=\"%s\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n"
904 "<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"
905 " <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n"
906 " <rdf:Description rdf:about=\"\"\n"
907 " xmlns:xap=\"http://ns.adobe.com/xap/1.0/\">\n"
908 " <xap:ModifyDate>%s</xap:ModifyDate>\n"
909 " <xap:CreateDate>%s</xap:CreateDate>\n"
910 " <xap:MetadataDate>%s</xap:MetadataDate>\n"
911 " <xap:CreatorTool>%s</xap:CreatorTool>\n"
912 " </rdf:Description>\n"
913 " <rdf:Description rdf:about=\"\"\n"
914 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"
915 " <dc:format>application/pdf</dc:format>\n"
916 " </rdf:Description>\n"
917 " <rdf:Description rdf:about=\"\"\n"
918 " xmlns:xapMM=\"http://ns.adobe.com/xap/1.0/mm/\">\n"
919 " <xapMM:DocumentID>uuid:6ec119d7-7982-4f56-808d-dfe64f5b35cf</xapMM:DocumentID>\n"
920 " <xapMM:InstanceID>uuid:a79b99b4-6235-447f-9f6c-ec18ef7555cb</xapMM:InstanceID>\n"
921 " </rdf:Description>\n"
922 " <rdf:Description rdf:about=\"\"\n"
923 " xmlns:pdf=\"http://ns.adobe.com/pdf/1.3/\">\n"
924 " <pdf:Producer>%s</pdf:Producer>\n"
925 " </rdf:Description>\n"
926 " <rdf:Description rdf:about=\"\"\n"
927 " xmlns:pdfaid=\"http://www.aiim.org/pdfa/ns/id/\">\n"
928 " <pdfaid:part>1</pdfaid:part>\n"
929 " <pdfaid:conformance>B</pdfaid:conformance>\n"
930 " </rdf:Description>\n"
933 "<?xpacket end=\"w\"?>\n"
935 XMPProfileMagick[4]= { (char) 0xef, (char) 0xbb, (char) 0xbf, (char) 0x00 };
938 buffer[MaxTextExtent],
941 page_geometry[MaxTextExtent];
987 register const IndexPacket
990 register const PixelPacket
993 register unsigned char
1021 Open output image file.
1023 assert(image_info != (const ImageInfo *) NULL);
1024 assert(image_info->signature == MagickSignature);
1025 assert(image != (Image *) NULL);
1026 assert(image->signature == MagickSignature);
1027 if (image->debug != MagickFalse)
1028 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1029 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1030 if (status == MagickFalse)
1033 Allocate X ref memory.
1035 xref=(MagickOffsetType *) AcquireQuantumMemory(2048UL,sizeof(*xref));
1036 if (xref == (MagickOffsetType *) NULL)
1037 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1038 (void) ResetMagickMemory(xref,0,2048UL*sizeof(*xref));
1044 if (image_info->compression == JPEG2000Compression)
1045 version=(unsigned long) MagickMax(version,5);
1046 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
1047 if (next->matte != MagickFalse)
1048 version=(unsigned long) MagickMax(version,4);
1049 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1050 version=(unsigned long) MagickMax(version,6);
1051 (void) FormatMagickString(buffer,MaxTextExtent,"%%PDF-1.%lu \n",version);
1052 (void) WriteBlobString(image,buffer);
1053 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1054 (void) WriteBlobString(image,"%âãÏÓ\n");
1056 Write Catalog object.
1058 xref[object++]=TellBlob(image);
1060 (void) FormatMagickString(buffer,MaxTextExtent,"%lu 0 obj\n",object);
1061 (void) WriteBlobString(image,buffer);
1062 (void) WriteBlobString(image,"<<\n");
1063 if (LocaleCompare(image_info->magick,"PDFA") != 0)
1064 (void) FormatMagickString(buffer,MaxTextExtent,"/Pages %lu 0 R\n",
1068 (void) FormatMagickString(buffer,MaxTextExtent,"/Metadata %lu 0 R\n",
1070 (void) WriteBlobString(image,buffer);
1071 (void) FormatMagickString(buffer,MaxTextExtent,"/Pages %lu 0 R\n",
1074 (void) WriteBlobString(image,buffer);
1075 (void) WriteBlobString(image,"/Type /Catalog\n");
1076 (void) WriteBlobString(image,">>\n");
1077 (void) WriteBlobString(image,"endobj\n");
1078 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1081 create_date[MaxTextExtent],
1082 modify_date[MaxTextExtent],
1083 timestamp[MaxTextExtent],
1084 xmp_profile[MaxTextExtent];
1092 xref[object++]=TellBlob(image);
1093 (void) FormatMagickString(buffer,MaxTextExtent,"%lu 0 obj\n",object);
1094 (void) WriteBlobString(image,buffer);
1095 (void) WriteBlobString(image,"<<\n");
1096 (void) WriteBlobString(image,"/Subtype /XML\n");
1098 value=GetImageProperty(image,"date:modify");
1099 if (value != (const char *) NULL)
1100 (void) CopyMagickString(modify_date,value,MaxTextExtent);
1102 value=GetImageProperty(image,"date:create");
1103 if (value != (const char *) NULL)
1104 (void) CopyMagickString(create_date,value,MaxTextExtent);
1105 (void) FormatMagickTime(time((time_t *) NULL),MaxTextExtent,timestamp);
1106 i=FormatMagickString(xmp_profile,MaxTextExtent,XMPProfile,
1107 XMPProfileMagick,modify_date,create_date,timestamp,
1108 GetMagickVersion(&version),GetMagickVersion(&version));
1109 (void) FormatMagickString(buffer,MaxTextExtent,"/Length %lu\n",1UL*i);
1110 (void) WriteBlobString(image,buffer);
1111 (void) WriteBlobString(image,"/Type /Metadata\n");
1112 (void) WriteBlobString(image,">>\nstream\n");
1113 (void) WriteBlobString(image,xmp_profile);
1114 (void) WriteBlobString(image,"endstream\n");
1115 (void) WriteBlobString(image,"endobj\n");
1120 xref[object++]=TellBlob(image);
1122 (void) FormatMagickString(buffer,MaxTextExtent,"%lu 0 obj\n",object);
1123 (void) WriteBlobString(image,buffer);
1124 (void) WriteBlobString(image,"<<\n");
1125 (void) WriteBlobString(image,"/Type /Pages\n");
1126 (void) FormatMagickString(buffer,MaxTextExtent,"/Kids [ %lu 0 R ",object+1);
1127 (void) WriteBlobString(image,buffer);
1128 count=(long) (pages_id+ObjectsPerImage+1);
1129 if (image_info->adjoin != MagickFalse)
1135 Predict page object id's.
1138 for ( ; GetNextImageInList(kid_image) != (Image *) NULL; count+=ObjectsPerImage)
1140 (void) FormatMagickString(buffer,MaxTextExtent,"%ld 0 R ",count);
1141 (void) WriteBlobString(image,buffer);
1142 kid_image=GetNextImageInList(kid_image);
1144 xref=(MagickOffsetType *) ResizeQuantumMemory(xref,(size_t) count+2048UL,
1146 if (xref == (MagickOffsetType *) NULL)
1147 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1149 (void) WriteBlobString(image,"]\n");
1150 (void) FormatMagickString(buffer,MaxTextExtent,"/Count %lu\n",
1151 (count-pages_id)/ObjectsPerImage);
1152 (void) WriteBlobString(image,buffer);
1153 (void) WriteBlobString(image,">>\n");
1154 (void) WriteBlobString(image,"endobj\n");
1158 compression=image->compression;
1159 if (image_info->compression != UndefinedCompression)
1160 compression=image_info->compression;
1161 switch (compression)
1163 case FaxCompression:
1164 case Group4Compression:
1166 if ((IsMonochromeImage(image,&image->exception) == MagickFalse) ||
1167 (image->matte != MagickFalse))
1168 compression=RLECompression;
1171 #if !defined(MAGICKCORE_JPEG_DELEGATE)
1172 case JPEGCompression:
1174 compression=RLECompression;
1175 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1176 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
1181 #if !defined(MAGICKCORE_JP2_DELEGATE)
1182 case JPEG2000Compression:
1184 compression=RLECompression;
1185 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1186 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JP2)",
1191 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
1192 case ZipCompression:
1194 compression=RLECompression;
1195 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1196 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZLIB)",
1201 case LZWCompression:
1203 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1204 compression=RLECompression; /* LZW compression is forbidden */
1209 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1210 compression=RLECompression; /* ASCII 85 compression is forbidden */
1216 if (compression == JPEG2000Compression)
1218 if (image->colorspace != RGBColorspace)
1219 (void) TransformImageColorspace(image,RGBColorspace);
1222 Scale relative to dots-per-inch.
1224 delta.x=DefaultResolution;
1225 delta.y=DefaultResolution;
1226 resolution.x=image->x_resolution;
1227 resolution.y=image->y_resolution;
1228 if ((resolution.x == 0.0) || (resolution.y == 0.0))
1230 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
1231 resolution.x=geometry_info.rho;
1232 resolution.y=geometry_info.sigma;
1233 if ((flags & SigmaValue) == 0)
1234 resolution.y=resolution.x;
1236 if (image_info->density != (char *) NULL)
1238 flags=ParseGeometry(image_info->density,&geometry_info);
1239 resolution.x=geometry_info.rho;
1240 resolution.y=geometry_info.sigma;
1241 if ((flags & SigmaValue) == 0)
1242 resolution.y=resolution.x;
1244 if (image->units == PixelsPerCentimeterResolution)
1249 SetGeometry(image,&geometry);
1250 (void) FormatMagickString(page_geometry,MaxTextExtent,"%lux%lu",
1251 image->columns,image->rows);
1252 if (image_info->page != (char *) NULL)
1253 (void) CopyMagickString(page_geometry,image_info->page,MaxTextExtent);
1255 if ((image->page.width != 0) && (image->page.height != 0))
1256 (void) FormatMagickString(page_geometry,MaxTextExtent,"%lux%lu%+ld%+ld",
1257 image->page.width,image->page.height,image->page.x,image->page.y);
1259 if ((image->gravity != UndefinedGravity) &&
1260 (LocaleCompare(image_info->magick,"PDF") == 0))
1261 (void) CopyMagickString(page_geometry,PSPageGeometry,MaxTextExtent);
1262 (void) ConcatenateMagickString(page_geometry,">",MaxTextExtent);
1263 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
1264 &geometry.width,&geometry.height);
1265 scale.x=(double) (geometry.width*delta.x)/resolution.x;
1266 geometry.width=(unsigned long) (scale.x+0.5);
1267 scale.y=(double) (geometry.height*delta.y)/resolution.y;
1268 geometry.height=(unsigned long) (scale.y+0.5);
1269 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
1270 (void) ParseGravityGeometry(image,page_geometry,&page_info,
1272 if (image->gravity != UndefinedGravity)
1274 geometry.x=(-page_info.x);
1275 geometry.y=(long) (media_info.height+page_info.y-image->rows);
1278 if (image_info->pointsize != 0.0)
1279 pointsize=image_info->pointsize;
1281 value=GetImageProperty(image,"Label");
1282 if (value != (const char *) NULL)
1283 text_size=(unsigned long) (MultilineCensus(value)*pointsize+12);
1287 xref[object++]=TellBlob(image);
1288 (void) FormatMagickString(buffer,MaxTextExtent,"%lu 0 obj\n",object);
1289 (void) WriteBlobString(image,buffer);
1290 (void) WriteBlobString(image,"<<\n");
1291 (void) WriteBlobString(image,"/Type /Page\n");
1292 (void) FormatMagickString(buffer,MaxTextExtent,"/Parent %lu 0 R\n",
1294 (void) WriteBlobString(image,buffer);
1295 (void) WriteBlobString(image,"/Resources <<\n");
1296 labels=(char **) NULL;
1297 value=GetImageProperty(image,"Label");
1298 if (value != (const char *) NULL)
1299 labels=StringToList(value);
1300 if (labels != (char **) NULL)
1302 (void) FormatMagickString(buffer,MaxTextExtent,
1303 "/Font << /F%lu %lu 0 R >>\n",image->scene,object+4);
1304 (void) WriteBlobString(image,buffer);
1306 (void) FormatMagickString(buffer,MaxTextExtent,
1307 "/XObject << /Im%lu %lu 0 R >>\n",image->scene,object+5);
1308 (void) WriteBlobString(image,buffer);
1309 (void) FormatMagickString(buffer,MaxTextExtent,"/ProcSet %lu 0 R >>\n",
1311 (void) WriteBlobString(image,buffer);
1312 (void) FormatMagickString(buffer,MaxTextExtent,
1313 "/MediaBox [0 0 %g %g]\n",72.0*media_info.width/resolution.x,
1314 72.0*media_info.height/resolution.y);
1315 (void) WriteBlobString(image,buffer);
1316 (void) FormatMagickString(buffer,MaxTextExtent,
1317 "/CropBox [0 0 %g %g]\n",72.0*media_info.width/resolution.x,
1318 72.0*media_info.height/resolution.y);
1319 (void) WriteBlobString(image,buffer);
1320 (void) FormatMagickString(buffer,MaxTextExtent,"/Contents %lu 0 R\n",
1322 (void) WriteBlobString(image,buffer);
1323 (void) FormatMagickString(buffer,MaxTextExtent,"/Thumb %lu 0 R\n",
1325 (void) WriteBlobString(image,buffer);
1326 (void) WriteBlobString(image,">>\n");
1327 (void) WriteBlobString(image,"endobj\n");
1329 Write Contents object.
1331 xref[object++]=TellBlob(image);
1332 (void) FormatMagickString(buffer,MaxTextExtent,"%lu 0 obj\n",object);
1333 (void) WriteBlobString(image,buffer);
1334 (void) WriteBlobString(image,"<<\n");
1335 (void) FormatMagickString(buffer,MaxTextExtent,"/Length %lu 0 R\n",
1337 (void) WriteBlobString(image,buffer);
1338 (void) WriteBlobString(image,">>\n");
1339 (void) WriteBlobString(image,"stream\n");
1340 offset=TellBlob(image);
1341 (void) WriteBlobString(image,"q\n");
1342 if (labels != (char **) NULL)
1343 for (i=0; labels[i] != (char *) NULL; i++)
1345 (void) WriteBlobString(image,"BT\n");
1346 (void) FormatMagickString(buffer,MaxTextExtent,"/F%lu %g Tf\n",
1347 image->scene,pointsize);
1348 (void) WriteBlobString(image,buffer);
1349 (void) FormatMagickString(buffer,MaxTextExtent,"%ld %ld Td\n",
1350 geometry.x,(long) (geometry.y+geometry.height+i*pointsize+12));
1351 (void) WriteBlobString(image,buffer);
1352 (void) FormatMagickString(buffer,MaxTextExtent,"(%s) Tj\n",labels[i]);
1353 (void) WriteBlobString(image,buffer);
1354 (void) WriteBlobString(image,"ET\n");
1355 labels[i]=DestroyString(labels[i]);
1357 (void) FormatMagickString(buffer,MaxTextExtent,
1358 "%g 0 0 %g %ld %ld cm\n",scale.x,scale.y,geometry.x,geometry.y);
1359 (void) WriteBlobString(image,buffer);
1360 (void) FormatMagickString(buffer,MaxTextExtent,"/Im%lu Do\n",image->scene);
1361 (void) WriteBlobString(image,buffer);
1362 (void) WriteBlobString(image,"Q\n");
1363 offset=TellBlob(image)-offset;
1364 (void) WriteBlobString(image,"endstream\n");
1365 (void) WriteBlobString(image,"endobj\n");
1367 Write Length object.
1369 xref[object++]=TellBlob(image);
1370 (void) FormatMagickString(buffer,MaxTextExtent,"%lu 0 obj\n",object);
1371 (void) WriteBlobString(image,buffer);
1372 (void) FormatMagickString(buffer,MaxTextExtent,"%lu\n",
1373 (unsigned long) offset);
1374 (void) WriteBlobString(image,buffer);
1375 (void) WriteBlobString(image,"endobj\n");
1377 Write Procset object.
1379 xref[object++]=TellBlob(image);
1380 (void) FormatMagickString(buffer,MaxTextExtent,"%lu 0 obj\n",object);
1381 (void) WriteBlobString(image,buffer);
1382 if ((image->storage_class == DirectClass) || (image->colors > 256))
1383 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageC",MaxTextExtent);
1385 if ((compression == FaxCompression) || (compression == Group4Compression))
1386 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageB",MaxTextExtent);
1388 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageI",MaxTextExtent);
1389 (void) WriteBlobString(image,buffer);
1390 (void) WriteBlobString(image," ]\n");
1391 (void) WriteBlobString(image,"endobj\n");
1395 xref[object++]=TellBlob(image);
1396 (void) FormatMagickString(buffer,MaxTextExtent,"%lu 0 obj\n",object);
1397 (void) WriteBlobString(image,buffer);
1398 (void) WriteBlobString(image,"<<\n");
1399 if (labels != (char **) NULL)
1401 (void) WriteBlobString(image,"/Type /Font\n");
1402 (void) WriteBlobString(image,"/Subtype /Type1\n");
1403 (void) FormatMagickString(buffer,MaxTextExtent,"/Name /F%lu\n",
1405 (void) WriteBlobString(image,buffer);
1406 (void) WriteBlobString(image,"/BaseFont /Helvetica\n");
1407 (void) WriteBlobString(image,"/Encoding /MacRomanEncoding\n");
1408 labels=(char **) RelinquishMagickMemory(labels);
1410 (void) WriteBlobString(image,">>\n");
1411 (void) WriteBlobString(image,"endobj\n");
1413 Write XObject object.
1415 xref[object++]=TellBlob(image);
1416 (void) FormatMagickString(buffer,MaxTextExtent,"%lu 0 obj\n",object);
1417 (void) WriteBlobString(image,buffer);
1418 (void) WriteBlobString(image,"<<\n");
1419 (void) WriteBlobString(image,"/Type /XObject\n");
1420 (void) WriteBlobString(image,"/Subtype /Image\n");
1421 (void) FormatMagickString(buffer,MaxTextExtent,"/Name /Im%lu\n",
1423 (void) WriteBlobString(image,buffer);
1424 switch (compression)
1428 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"ASCII85Decode");
1431 case JPEGCompression:
1433 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"DCTDecode");
1434 if (image->colorspace != CMYKColorspace)
1436 (void) WriteBlobString(image,buffer);
1437 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1441 case JPEG2000Compression:
1443 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"JPXDecode");
1444 if (image->colorspace != CMYKColorspace)
1446 (void) WriteBlobString(image,buffer);
1447 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1451 case LZWCompression:
1453 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"LZWDecode");
1456 case ZipCompression:
1458 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"FlateDecode");
1461 case FaxCompression:
1462 case Group4Compression:
1464 (void) CopyMagickString(buffer,"/Filter [ /CCITTFaxDecode ]\n",
1466 (void) WriteBlobString(image,buffer);
1467 (void) FormatMagickString(buffer,MaxTextExtent,"/DecodeParms [ << "
1468 "/K %s /BlackIs1 false /Columns %ld /Rows %ld >> ]\n",CCITTParam,
1469 image->columns,image->rows);
1474 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,
1479 (void) WriteBlobString(image,buffer);
1480 (void) FormatMagickString(buffer,MaxTextExtent,"/Width %lu\n",
1482 (void) WriteBlobString(image,buffer);
1483 (void) FormatMagickString(buffer,MaxTextExtent,"/Height %lu\n",image->rows);
1484 (void) WriteBlobString(image,buffer);
1485 (void) FormatMagickString(buffer,MaxTextExtent,"/ColorSpace %lu 0 R\n",
1487 (void) WriteBlobString(image,buffer);
1488 (void) FormatMagickString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
1489 (compression == FaxCompression) || (compression == Group4Compression) ?
1491 (void) WriteBlobString(image,buffer);
1492 if (image->matte != MagickFalse)
1494 (void) FormatMagickString(buffer,MaxTextExtent,"/SMask %lu 0 R\n",
1496 (void) WriteBlobString(image,buffer);
1498 (void) FormatMagickString(buffer,MaxTextExtent,"/Length %lu 0 R\n",
1500 (void) WriteBlobString(image,buffer);
1501 (void) WriteBlobString(image,">>\n");
1502 (void) WriteBlobString(image,"stream\n");
1503 offset=TellBlob(image);
1504 number_pixels=(MagickSizeType) image->columns*image->rows;
1505 if ((4*number_pixels) != (MagickSizeType) ((size_t) (4*number_pixels)))
1506 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1507 if ((compression == FaxCompression) || (compression == Group4Compression) ||
1508 ((image_info->type != TrueColorType) &&
1509 (IsGrayImage(image,&image->exception) != MagickFalse)))
1511 switch (compression)
1513 case FaxCompression:
1514 case Group4Compression:
1516 if (LocaleCompare(CCITTParam,"0") == 0)
1518 (void) HuffmanEncodeImage(image_info,image,image);
1521 (void) Huffman2DEncodeImage(image_info,image,image);
1524 case JPEGCompression:
1526 status=InjectImageBlob(image_info,image,image,"jpeg",
1528 if (status == MagickFalse)
1529 ThrowWriterException(CoderError,image->exception.reason);
1532 case JPEG2000Compression:
1534 status=InjectImageBlob(image_info,image,image,"jp2",
1536 if (status == MagickFalse)
1537 ThrowWriterException(CoderError,image->exception.reason);
1540 case RLECompression:
1544 Allocate pixel array.
1546 length=(size_t) number_pixels;
1547 pixels=(unsigned char *) AcquireQuantumMemory(length,
1549 if (pixels == (unsigned char *) NULL)
1550 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1552 Dump Runlength encoded pixels.
1555 for (y=0; y < (long) image->rows; y++)
1557 p=GetVirtualPixels(image,0,y,image->columns,1,
1559 if (p == (const PixelPacket *) NULL)
1561 for (x=0; x < (long) image->columns; x++)
1563 *q++=ScaleQuantumToChar(PixelIntensityToQuantum(p));
1566 if (image->previous == (Image *) NULL)
1568 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1569 if (status == MagickFalse)
1573 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1574 if (compression == ZipCompression)
1575 status=ZLIBEncodeImage(image,length,pixels);
1578 if (compression == LZWCompression)
1579 status=LZWEncodeImage(image,length,pixels);
1581 status=PackbitsEncodeImage(image,length,pixels);
1582 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1583 if (status == MagickFalse)
1585 (void) CloseBlob(image);
1586 return(MagickFalse);
1593 Dump uncompressed PseudoColor packets.
1595 Ascii85Initialize(image);
1596 for (y=0; y < (long) image->rows; y++)
1598 p=GetVirtualPixels(image,0,y,image->columns,1,
1600 if (p == (const PixelPacket *) NULL)
1602 for (x=0; x < (long) image->columns; x++)
1604 Ascii85Encode(image,
1605 ScaleQuantumToChar(PixelIntensityToQuantum(p)));
1608 if (image->previous == (Image *) NULL)
1610 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1611 if (status == MagickFalse)
1615 Ascii85Flush(image);
1621 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1622 (compression == JPEGCompression) ||
1623 (compression == JPEG2000Compression))
1624 switch (compression)
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 length*=image->colorspace == CMYKColorspace ? 4UL : 3UL;
1652 if (pixels == (unsigned char *) NULL)
1653 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1655 Dump runoffset encoded pixels.
1658 for (y=0; y < (long) image->rows; y++)
1660 p=GetVirtualPixels(image,0,y,image->columns,1,
1662 if (p == (const PixelPacket *) NULL)
1664 indexes=GetVirtualIndexQueue(image);
1665 for (x=0; x < (long) image->columns; x++)
1667 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
1668 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1669 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
1670 if (image->colorspace == CMYKColorspace)
1671 *q++=ScaleQuantumToChar(indexes[x]);
1674 if (image->previous == (Image *) NULL)
1676 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1677 if (status == MagickFalse)
1681 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1682 if (compression == ZipCompression)
1683 status=ZLIBEncodeImage(image,length,pixels);
1686 if (compression == LZWCompression)
1687 status=LZWEncodeImage(image,length,pixels);
1689 status=PackbitsEncodeImage(image,length,pixels);
1690 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1691 if (status == MagickFalse)
1693 (void) CloseBlob(image);
1694 return(MagickFalse);
1701 Dump uncompressed DirectColor packets.
1703 Ascii85Initialize(image);
1704 for (y=0; y < (long) image->rows; y++)
1706 p=GetVirtualPixels(image,0,y,image->columns,1,
1708 if (p == (const PixelPacket *) NULL)
1710 indexes=GetVirtualIndexQueue(image);
1711 for (x=0; x < (long) image->columns; x++)
1713 Ascii85Encode(image,ScaleQuantumToChar(GetRedPixelComponent(p)));
1714 Ascii85Encode(image,ScaleQuantumToChar(GetGreenPixelComponent(p)));
1715 Ascii85Encode(image,ScaleQuantumToChar(GetBluePixelComponent(p)));
1716 if (image->colorspace == CMYKColorspace)
1717 Ascii85Encode(image,ScaleQuantumToChar(indexes[x]));
1720 if (image->previous == (Image *) NULL)
1722 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1723 if (status == MagickFalse)
1727 Ascii85Flush(image);
1734 Dump number of colors and colormap.
1736 switch (compression)
1738 case RLECompression:
1742 Allocate pixel array.
1744 length=(size_t) number_pixels;
1745 pixels=(unsigned char *) AcquireQuantumMemory(length,
1747 if (pixels == (unsigned char *) NULL)
1748 ThrowWriterException(ResourceLimitError,
1749 "MemoryAllocationFailed");
1751 Dump Runlength encoded pixels.
1754 for (y=0; y < (long) image->rows; y++)
1756 p=GetVirtualPixels(image,0,y,image->columns,1,
1758 if (p == (const PixelPacket *) NULL)
1760 indexes=GetVirtualIndexQueue(image);
1761 for (x=0; x < (long) image->columns; x++)
1762 *q++=(unsigned char) indexes[x];
1763 if (image->previous == (Image *) NULL)
1765 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1766 if (status == MagickFalse)
1770 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1771 if (compression == ZipCompression)
1772 status=ZLIBEncodeImage(image,length,pixels);
1775 if (compression == LZWCompression)
1776 status=LZWEncodeImage(image,length,pixels);
1778 status=PackbitsEncodeImage(image,length,pixels);
1779 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1780 if (status == MagickFalse)
1782 (void) CloseBlob(image);
1783 return(MagickFalse);
1790 Dump uncompressed PseudoColor packets.
1792 Ascii85Initialize(image);
1793 for (y=0; y < (long) image->rows; y++)
1795 p=GetVirtualPixels(image,0,y,image->columns,1,
1797 if (p == (const PixelPacket *) NULL)
1799 indexes=GetVirtualIndexQueue(image);
1800 for (x=0; x < (long) image->columns; x++)
1801 Ascii85Encode(image,(unsigned char) indexes[x]);
1802 if (image->previous == (Image *) NULL)
1804 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1805 if (status == MagickFalse)
1809 Ascii85Flush(image);
1814 offset=TellBlob(image)-offset;
1815 (void) WriteBlobString(image,"\nendstream\n");
1816 (void) WriteBlobString(image,"endobj\n");
1818 Write Length object.
1820 xref[object++]=TellBlob(image);
1821 (void) FormatMagickString(buffer,MaxTextExtent,"%lu 0 obj\n",object);
1822 (void) WriteBlobString(image,buffer);
1823 (void) FormatMagickString(buffer,MaxTextExtent,"%lu\n",
1824 (unsigned long) offset);
1825 (void) WriteBlobString(image,buffer);
1826 (void) WriteBlobString(image,"endobj\n");
1828 Write Colorspace object.
1830 xref[object++]=TellBlob(image);
1831 (void) FormatMagickString(buffer,MaxTextExtent,"%lu 0 obj\n",object);
1832 (void) WriteBlobString(image,buffer);
1833 if (image->colorspace == CMYKColorspace)
1834 (void) CopyMagickString(buffer,"/DeviceCMYK\n",MaxTextExtent);
1836 if ((compression == FaxCompression) ||
1837 (compression == Group4Compression) ||
1838 ((image_info->type != TrueColorType) &&
1839 (IsGrayImage(image,&image->exception) != MagickFalse)))
1840 (void) CopyMagickString(buffer,"/DeviceGray\n",MaxTextExtent);
1842 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1843 (compression == JPEGCompression) ||
1844 (compression == JPEG2000Compression))
1845 (void) CopyMagickString(buffer,"/DeviceRGB\n",MaxTextExtent);
1847 (void) FormatMagickString(buffer,MaxTextExtent,
1848 "[ /Indexed /DeviceRGB %lu %lu 0 R ]\n",
1849 image->colors-1,object+3);
1850 (void) WriteBlobString(image,buffer);
1851 (void) WriteBlobString(image,"endobj\n");
1855 SetGeometry(image,&geometry);
1856 (void) ParseMetaGeometry("106x106+0+0>",&geometry.x,&geometry.y,
1857 &geometry.width,&geometry.height);
1858 tile_image=ThumbnailImage(image,geometry.width,geometry.height,
1860 if (tile_image == (Image *) NULL)
1861 ThrowWriterException(ResourceLimitError,image->exception.reason);
1862 xref[object++]=TellBlob(image);
1863 (void) FormatMagickString(buffer,MaxTextExtent,"%lu 0 obj\n",object);
1864 (void) WriteBlobString(image,buffer);
1865 (void) WriteBlobString(image,"<<\n");
1866 switch (compression)
1870 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"ASCII85Decode");
1873 case JPEGCompression:
1875 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"DCTDecode");
1876 if (image->colorspace != CMYKColorspace)
1878 (void) WriteBlobString(image,buffer);
1879 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1883 case JPEG2000Compression:
1885 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"JPXDecode");
1886 if (image->colorspace != CMYKColorspace)
1888 (void) WriteBlobString(image,buffer);
1889 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1893 case LZWCompression:
1895 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"LZWDecode");
1898 case ZipCompression:
1900 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"FlateDecode");
1903 case FaxCompression:
1904 case Group4Compression:
1906 (void) CopyMagickString(buffer,"/Filter [ /CCITTFaxDecode ]\n",
1908 (void) WriteBlobString(image,buffer);
1909 (void) FormatMagickString(buffer,MaxTextExtent,"/DecodeParms [ << "
1910 "/K %s /BlackIs1 false /Columns %lu /Rows %lu >> ]\n",CCITTParam,
1911 tile_image->columns,tile_image->rows);
1916 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,
1921 (void) WriteBlobString(image,buffer);
1922 (void) FormatMagickString(buffer,MaxTextExtent,"/Width %lu\n",
1923 tile_image->columns);
1924 (void) WriteBlobString(image,buffer);
1925 (void) FormatMagickString(buffer,MaxTextExtent,"/Height %lu\n",
1927 (void) WriteBlobString(image,buffer);
1928 (void) FormatMagickString(buffer,MaxTextExtent,"/ColorSpace %lu 0 R\n",
1930 (void) WriteBlobString(image,buffer);
1931 (void) FormatMagickString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
1932 (compression == FaxCompression) || (compression == Group4Compression) ?
1934 (void) WriteBlobString(image,buffer);
1935 (void) FormatMagickString(buffer,MaxTextExtent,"/Length %lu 0 R\n",
1937 (void) WriteBlobString(image,buffer);
1938 (void) WriteBlobString(image,">>\n");
1939 (void) WriteBlobString(image,"stream\n");
1940 offset=TellBlob(image);
1941 number_pixels=(MagickSizeType) tile_image->columns*tile_image->rows;
1942 if ((compression == FaxCompression) ||
1943 (compression == Group4Compression) ||
1944 ((image_info->type != TrueColorType) &&
1945 (IsGrayImage(tile_image,&image->exception) != MagickFalse)))
1947 switch (compression)
1949 case FaxCompression:
1950 case Group4Compression:
1952 if (LocaleCompare(CCITTParam,"0") == 0)
1954 (void) HuffmanEncodeImage(image_info,image,tile_image);
1957 (void) Huffman2DEncodeImage(image_info,image,tile_image);
1960 case JPEGCompression:
1962 status=InjectImageBlob(image_info,image,tile_image,"jpeg",
1964 if (status == MagickFalse)
1965 ThrowWriterException(CoderError,tile_image->exception.reason);
1968 case JPEG2000Compression:
1970 status=InjectImageBlob(image_info,image,tile_image,"jp2",
1972 if (status == MagickFalse)
1973 ThrowWriterException(CoderError,tile_image->exception.reason);
1976 case RLECompression:
1980 Allocate pixel array.
1982 length=(size_t) number_pixels;
1983 pixels=(unsigned char *) AcquireQuantumMemory(length,
1985 if (pixels == (unsigned char *) NULL)
1987 tile_image=DestroyImage(tile_image);
1988 ThrowWriterException(ResourceLimitError,
1989 "MemoryAllocationFailed");
1992 Dump Runlength encoded pixels.
1995 for (y=0; y < (long) tile_image->rows; y++)
1997 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
1998 &tile_image->exception);
1999 if (p == (const PixelPacket *) NULL)
2001 for (x=0; x < (long) tile_image->columns; x++)
2003 *q++=ScaleQuantumToChar(PixelIntensityToQuantum(p));
2007 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2008 if (compression == ZipCompression)
2009 status=ZLIBEncodeImage(image,length,pixels);
2012 if (compression == LZWCompression)
2013 status=LZWEncodeImage(image,length,pixels);
2015 status=PackbitsEncodeImage(image,length,pixels);
2016 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2017 if (status == MagickFalse)
2019 (void) CloseBlob(image);
2020 return(MagickFalse);
2027 Dump uncompressed PseudoColor packets.
2029 Ascii85Initialize(image);
2030 for (y=0; y < (long) 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 < (long) tile_image->columns; x++)
2038 Ascii85Encode(image,
2039 ScaleQuantumToChar(PixelIntensityToQuantum(p)));
2043 Ascii85Flush(image);
2049 if ((tile_image->storage_class == DirectClass) ||
2050 (tile_image->colors > 256) || (compression == JPEGCompression) ||
2051 (compression == JPEG2000Compression))
2052 switch (compression)
2054 case JPEGCompression:
2056 status=InjectImageBlob(image_info,image,tile_image,"jpeg",
2058 if (status == MagickFalse)
2059 ThrowWriterException(CoderError,tile_image->exception.reason);
2062 case JPEG2000Compression:
2064 status=InjectImageBlob(image_info,image,tile_image,"jp2",
2066 if (status == MagickFalse)
2067 ThrowWriterException(CoderError,tile_image->exception.reason);
2070 case RLECompression:
2074 Allocate pixel array.
2076 length=(size_t) number_pixels;
2077 pixels=(unsigned char *) AcquireQuantumMemory(length,4*
2079 length*=tile_image->colorspace == CMYKColorspace ? 4UL : 3UL;
2080 if (pixels == (unsigned char *) NULL)
2082 tile_image=DestroyImage(tile_image);
2083 ThrowWriterException(ResourceLimitError,
2084 "MemoryAllocationFailed");
2087 Dump runoffset encoded pixels.
2090 for (y=0; y < (long) tile_image->rows; y++)
2092 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2093 &tile_image->exception);
2094 if (p == (const PixelPacket *) NULL)
2096 indexes=GetVirtualIndexQueue(tile_image);
2097 for (x=0; x < (long) tile_image->columns; x++)
2099 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
2100 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
2101 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
2102 if (image->colorspace == CMYKColorspace)
2103 *q++=ScaleQuantumToChar(indexes[x]);
2107 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2108 if (compression == ZipCompression)
2109 status=ZLIBEncodeImage(image,length,pixels);
2112 if (compression == LZWCompression)
2113 status=LZWEncodeImage(image,length,pixels);
2115 status=PackbitsEncodeImage(image,length,pixels);
2116 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2117 if (status == MagickFalse)
2119 (void) CloseBlob(image);
2120 return(MagickFalse);
2127 Dump uncompressed DirectColor packets.
2129 Ascii85Initialize(image);
2130 for (y=0; y < (long) tile_image->rows; y++)
2132 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2133 &tile_image->exception);
2134 if (p == (const PixelPacket *) NULL)
2136 indexes=GetVirtualIndexQueue(tile_image);
2137 for (x=0; x < (long) tile_image->columns; x++)
2139 Ascii85Encode(image,ScaleQuantumToChar(GetRedPixelComponent(p)));
2140 Ascii85Encode(image,ScaleQuantumToChar(GetGreenPixelComponent(p)));
2141 Ascii85Encode(image,ScaleQuantumToChar(GetBluePixelComponent(p)));
2142 if (image->colorspace == CMYKColorspace)
2143 Ascii85Encode(image,ScaleQuantumToChar(indexes[x]));
2147 Ascii85Flush(image);
2154 Dump number of colors and colormap.
2156 switch (compression)
2158 case RLECompression:
2162 Allocate pixel array.
2164 length=(size_t) number_pixels;
2165 pixels=(unsigned char *) AcquireQuantumMemory(length,
2167 if (pixels == (unsigned char *) NULL)
2169 tile_image=DestroyImage(tile_image);
2170 ThrowWriterException(ResourceLimitError,
2171 "MemoryAllocationFailed");
2174 Dump Runlength encoded pixels.
2177 for (y=0; y < (long) tile_image->rows; y++)
2179 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2180 &tile_image->exception);
2181 if (p == (const PixelPacket *) NULL)
2183 indexes=GetVirtualIndexQueue(tile_image);
2184 for (x=0; x < (long) tile_image->columns; x++)
2185 *q++=(unsigned char) indexes[x];
2187 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2188 if (compression == ZipCompression)
2189 status=ZLIBEncodeImage(image,length,pixels);
2192 if (compression == LZWCompression)
2193 status=LZWEncodeImage(image,length,pixels);
2195 status=PackbitsEncodeImage(image,length,pixels);
2196 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2197 if (status == MagickFalse)
2199 (void) CloseBlob(image);
2200 return(MagickFalse);
2207 Dump uncompressed PseudoColor packets.
2209 Ascii85Initialize(image);
2210 for (y=0; y < (long) tile_image->rows; y++)
2212 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
2213 &tile_image->exception);
2214 if (p == (const PixelPacket *) NULL)
2216 indexes=GetVirtualIndexQueue(tile_image);
2217 for (x=0; x < (long) tile_image->columns; x++)
2218 Ascii85Encode(image,(unsigned char) indexes[x]);
2220 Ascii85Flush(image);
2225 tile_image=DestroyImage(tile_image);
2226 offset=TellBlob(image)-offset;
2227 (void) WriteBlobString(image,"\nendstream\n");
2228 (void) WriteBlobString(image,"endobj\n");
2230 Write Length object.
2232 xref[object++]=TellBlob(image);
2233 (void) FormatMagickString(buffer,MaxTextExtent,"%lu 0 obj\n",object);
2234 (void) WriteBlobString(image,buffer);
2235 (void) FormatMagickString(buffer,MaxTextExtent,"%lu\n",
2236 (unsigned long) offset);
2237 (void) WriteBlobString(image,buffer);
2238 (void) WriteBlobString(image,"endobj\n");
2239 xref[object++]=TellBlob(image);
2240 (void) FormatMagickString(buffer,MaxTextExtent,"%lu 0 obj\n",object);
2241 (void) WriteBlobString(image,buffer);
2242 if ((image->storage_class != DirectClass) && (image->colors <= 256) &&
2243 (compression != FaxCompression) && (compression != Group4Compression))
2246 Write Colormap object.
2248 (void) WriteBlobString(image,"<<\n");
2249 if (compression == NoCompression)
2250 (void) WriteBlobString(image,"/Filter [ /ASCII85Decode ]\n");
2251 (void) FormatMagickString(buffer,MaxTextExtent,"/Length %lu 0 R\n",
2253 (void) WriteBlobString(image,buffer);
2254 (void) WriteBlobString(image,">>\n");
2255 (void) WriteBlobString(image,"stream\n");
2256 offset=TellBlob(image);
2257 if (compression == NoCompression)
2258 Ascii85Initialize(image);
2259 for (i=0; i < (long) image->colors; i++)
2261 if (compression == NoCompression)
2263 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].red));
2264 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].green));
2265 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].blue));
2268 (void) WriteBlobByte(image,
2269 ScaleQuantumToChar(image->colormap[i].red));
2270 (void) WriteBlobByte(image,
2271 ScaleQuantumToChar(image->colormap[i].green));
2272 (void) WriteBlobByte(image,
2273 ScaleQuantumToChar(image->colormap[i].blue));
2275 if (compression == NoCompression)
2276 Ascii85Flush(image);
2277 offset=TellBlob(image)-offset;
2278 (void) WriteBlobString(image,"\nendstream\n");
2280 (void) WriteBlobString(image,"endobj\n");
2282 Write Length object.
2284 xref[object++]=TellBlob(image);
2285 (void) FormatMagickString(buffer,MaxTextExtent,"%lu 0 obj\n",object);
2286 (void) WriteBlobString(image,buffer);
2287 (void) FormatMagickString(buffer,MaxTextExtent,"%lu\n",
2288 (unsigned long) offset);
2289 (void) WriteBlobString(image,buffer);
2290 (void) WriteBlobString(image,"endobj\n");
2292 Write softmask object.
2294 xref[object++]=TellBlob(image);
2295 (void) FormatMagickString(buffer,MaxTextExtent,"%lu 0 obj\n",object);
2296 (void) WriteBlobString(image,buffer);
2297 (void) WriteBlobString(image,"<<\n");
2298 if (image->matte == MagickFalse)
2299 (void) WriteBlobString(image,">>\n");
2302 (void) WriteBlobString(image,"/Type /XObject\n");
2303 (void) WriteBlobString(image,"/Subtype /Image\n");
2304 (void) FormatMagickString(buffer,MaxTextExtent,"/Name /Ma%lu\n",
2306 (void) WriteBlobString(image,buffer);
2307 switch (compression)
2311 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,
2315 case LZWCompression:
2317 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,"LZWDecode");
2320 case ZipCompression:
2322 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,
2328 (void) FormatMagickString(buffer,MaxTextExtent,CFormat,
2333 (void) WriteBlobString(image,buffer);
2334 (void) FormatMagickString(buffer,MaxTextExtent,"/Width %lu\n",
2336 (void) WriteBlobString(image,buffer);
2337 (void) FormatMagickString(buffer,MaxTextExtent,"/Height %lu\n",
2339 (void) WriteBlobString(image,buffer);
2340 (void) WriteBlobString(image,"/ColorSpace /DeviceGray\n");
2341 (void) FormatMagickString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
2342 (compression == FaxCompression) || (compression == Group4Compression)
2344 (void) WriteBlobString(image,buffer);
2345 (void) FormatMagickString(buffer,MaxTextExtent,"/Length %lu 0 R\n",
2347 (void) WriteBlobString(image,buffer);
2348 (void) WriteBlobString(image,">>\n");
2349 (void) WriteBlobString(image,"stream\n");
2350 offset=TellBlob(image);
2351 number_pixels=(MagickSizeType) image->columns*image->rows;
2352 switch (compression)
2354 case RLECompression:
2358 Allocate pixel array.
2360 length=(size_t) number_pixels;
2361 pixels=(unsigned char *) AcquireQuantumMemory(length,
2363 if (pixels == (unsigned char *) NULL)
2365 image=DestroyImage(image);
2366 ThrowWriterException(ResourceLimitError,
2367 "MemoryAllocationFailed");
2370 Dump Runlength encoded pixels.
2373 for (y=0; y < (long) image->rows; y++)
2375 p=GetVirtualPixels(image,0,y,image->columns,1,
2377 if (p == (const PixelPacket *) NULL)
2379 for (x=0; x < (long) image->columns; x++)
2381 *q++=ScaleQuantumToChar((Quantum) (GetAlphaPixelComponent(p)));
2385 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2386 if (compression == ZipCompression)
2387 status=ZLIBEncodeImage(image,length,pixels);
2390 if (compression == LZWCompression)
2391 status=LZWEncodeImage(image,length,pixels);
2393 status=PackbitsEncodeImage(image,length,pixels);
2394 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2395 if (status == MagickFalse)
2397 (void) CloseBlob(image);
2398 return(MagickFalse);
2405 Dump uncompressed PseudoColor packets.
2407 Ascii85Initialize(image);
2408 for (y=0; y < (long) image->rows; y++)
2410 p=GetVirtualPixels(image,0,y,image->columns,1,
2412 if (p == (const PixelPacket *) NULL)
2414 for (x=0; x < (long) image->columns; x++)
2416 Ascii85Encode(image,ScaleQuantumToChar((Quantum) (QuantumRange-
2417 GetOpacityPixelComponent(p))));
2421 Ascii85Flush(image);
2425 offset=TellBlob(image)-offset;
2426 (void) WriteBlobString(image,"\nendstream\n");
2428 (void) WriteBlobString(image,"endobj\n");
2430 Write Length object.
2432 xref[object++]=TellBlob(image);
2433 (void) FormatMagickString(buffer,MaxTextExtent,"%lu 0 obj\n",object);
2434 (void) WriteBlobString(image,buffer);
2435 (void) FormatMagickString(buffer,MaxTextExtent,"%lu\n",(unsigned long)
2437 (void) WriteBlobString(image,buffer);
2438 (void) WriteBlobString(image,"endobj\n");
2439 if (GetNextImageInList(image) == (Image *) NULL)
2441 image=SyncNextImageInList(image);
2442 status=SetImageProgress(image,SaveImagesTag,scene++,
2443 GetImageListLength(image));
2444 if (status == MagickFalse)
2446 } while (image_info->adjoin != MagickFalse);
2448 Write Metadata object.
2450 xref[object++]=TellBlob(image);
2452 (void) FormatMagickString(buffer,MaxTextExtent,"%lu 0 obj\n",object);
2453 (void) WriteBlobString(image,buffer);
2454 (void) WriteBlobString(image,"<<\n");
2455 (void) FormatMagickString(buffer,MaxTextExtent,"/Title (%s)\n",
2456 EscapeParenthesis(image->filename));
2457 (void) WriteBlobString(image,buffer);
2458 seconds=time((time_t *) NULL);
2459 #if defined(MAGICKCORE_HAVE_LOCALTIME_R)
2460 (void) localtime_r(&seconds,&local_time);
2462 (void) memcpy(&local_time,localtime(&seconds),sizeof(local_time));
2464 (void) FormatMagickString(date,MaxTextExtent,"D:%04d%02d%02d%02d%02d%02d",
2465 local_time.tm_year+1900,local_time.tm_mon+1,local_time.tm_mday,
2466 local_time.tm_hour,local_time.tm_min,local_time.tm_sec);
2467 (void) FormatMagickString(buffer,MaxTextExtent,"/CreationDate (%s)\n",date);
2468 (void) WriteBlobString(image,buffer);
2469 (void) FormatMagickString(buffer,MaxTextExtent,"/ModDate (%s)\n",date);
2470 (void) WriteBlobString(image,buffer);
2471 (void) FormatMagickString(buffer,MaxTextExtent,"/Producer (%s)\n",
2472 EscapeParenthesis(GetMagickVersion((unsigned long *) NULL)));
2473 (void) WriteBlobString(image,buffer);
2474 (void) WriteBlobString(image,">>\n");
2475 (void) WriteBlobString(image,"endobj\n");
2479 offset=TellBlob(image)-xref[0]+10;
2480 (void) WriteBlobString(image,"xref\n");
2481 (void) FormatMagickString(buffer,MaxTextExtent,"0 %lu\n",object+1);
2482 (void) WriteBlobString(image,buffer);
2483 (void) WriteBlobString(image,"0000000000 65535 f \n");
2484 for (i=0; i < (long) object; i++)
2486 (void) FormatMagickString(buffer,MaxTextExtent,"%010lu 00000 n \n",
2487 (unsigned long) xref[i]);
2488 (void) WriteBlobString(image,buffer);
2490 (void) WriteBlobString(image,"trailer\n");
2491 (void) WriteBlobString(image,"<<\n");
2492 (void) FormatMagickString(buffer,MaxTextExtent,"/Size %lu\n",object+1);
2493 (void) WriteBlobString(image,buffer);
2494 (void) FormatMagickString(buffer,MaxTextExtent,"/Info %lu 0 R\n",info_id);
2495 (void) WriteBlobString(image,buffer);
2496 (void) FormatMagickString(buffer,MaxTextExtent,"/Root %lu 0 R\n",root_id);
2497 (void) WriteBlobString(image,buffer);
2498 (void) WriteBlobString(image,">>\n");
2499 (void) WriteBlobString(image,"startxref\n");
2500 (void) FormatMagickString(buffer,MaxTextExtent,"%lu\n",
2501 (unsigned long) offset);
2502 (void) WriteBlobString(image,buffer);
2503 (void) WriteBlobString(image,"%%EOF\n");
2504 xref=(MagickOffsetType *) RelinquishMagickMemory(xref);
2505 (void) CloseBlob(image);