2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Write Postscript Level III Format %
21 % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
22 % dedicated to making software imaging solutions freely available. %
24 % You may not use this file except in compliance with the License. You may %
25 % obtain a copy of the License at %
27 % http://www.imagemagick.org/script/license.php %
29 % Unless required by applicable law or agreed to in writing, software %
30 % distributed under the License is distributed on an "AS IS" BASIS, %
31 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
32 % See the License for the specific language governing permissions and %
33 % limitations under the License. %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "MagickCore/studio.h"
44 #include "MagickCore/attribute.h"
45 #include "MagickCore/blob.h"
46 #include "MagickCore/blob-private.h"
47 #include "MagickCore/cache.h"
48 #include "MagickCore/channel.h"
49 #include "MagickCore/color.h"
50 #include "MagickCore/color-private.h"
51 #include "MagickCore/compress.h"
52 #include "MagickCore/constitute.h"
53 #include "MagickCore/draw.h"
54 #include "MagickCore/exception.h"
55 #include "MagickCore/exception-private.h"
56 #include "MagickCore/geometry.h"
57 #include "MagickCore/image.h"
58 #include "MagickCore/image-private.h"
59 #include "MagickCore/list.h"
60 #include "MagickCore/magick.h"
61 #include "MagickCore/memory_.h"
62 #include "MagickCore/monitor.h"
63 #include "MagickCore/monitor-private.h"
64 #include "MagickCore/option.h"
65 #include "MagickCore/pixel-accessor.h"
66 #include "MagickCore/property.h"
67 #include "MagickCore/quantum-private.h"
68 #include "MagickCore/resource_.h"
69 #include "MagickCore/static.h"
70 #include "MagickCore/string_.h"
71 #include "MagickCore/module.h"
72 #include "MagickCore/token.h"
73 #include "MagickCore/utility.h"
74 #include "MagickCore/module.h"
79 #define PS3_NoCompression "0"
80 #define PS3_FaxCompression "1"
81 #define PS3_JPEGCompression "2"
82 #define PS3_LZWCompression "3"
83 #define PS3_RLECompression "4"
84 #define PS3_ZipCompression "5"
86 #define PS3_RGBColorspace "0"
87 #define PS3_CMYKColorspace "1"
89 #define PS3_DirectClass "0"
90 #define PS3_PseudoClass "1"
92 #if defined(MAGICKCORE_TIFF_DELEGATE)
93 #define CCITTParam "-1"
95 #define CCITTParam "0"
101 static MagickBooleanType
102 WritePS3Image(const ImageInfo *,Image *,ExceptionInfo *);
105 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109 % R e g i s t e r P S 3 I m a g e %
113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115 % RegisterPS3Image() adds properties for the PS3 image format to the list of
116 % supported formats. The properties include the image format tag, a method to
117 % read and/or write the format, whether the format supports the saving of more
118 % than one frame to the same file or blob, whether the format supports native
119 % in-memory I/O, and a brief description of the format.
121 % The format of the RegisterPS3Image method is:
123 % size_t RegisterPS3Image(void)
126 ModuleExport size_t RegisterPS3Image(void)
131 entry=SetMagickInfo("EPS3");
132 entry->encoder=(EncodeImageHandler *) WritePS3Image;
133 entry->description=ConstantString("Level III Encapsulated PostScript");
134 entry->module=ConstantString("PS3");
135 entry->seekable_stream=MagickTrue;
136 (void) RegisterMagickInfo(entry);
137 entry=SetMagickInfo("PS3");
138 entry->encoder=(EncodeImageHandler *) WritePS3Image;
139 entry->description=ConstantString("Level III PostScript");
140 entry->module=ConstantString("PS3");
141 entry->seekable_stream=MagickTrue;
142 (void) RegisterMagickInfo(entry);
143 return(MagickImageCoderSignature);
147 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151 % U n r e g i s t e r P S 3 I m a g e %
155 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
157 % UnregisterPS3Image() removes format registrations made by the PS3 module
158 % from the list of supported formats.
160 % The format of the UnregisterPS3Image method is:
162 % UnregisterPS3Image(void)
165 ModuleExport void UnregisterPS3Image(void)
167 (void) UnregisterMagickInfo("EPS3");
168 (void) UnregisterMagickInfo("PS3");
172 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
176 % W r i t e P S 3 I m a g e %
180 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
182 % WritePS3Image() translates an image to encapsulated Postscript Level III
183 % for printing. If the supplied geometry is null, the image is centered on
184 % the Postscript page. Otherwise, the image is positioned as specified by the
187 % The format of the WritePS3Image method is:
189 % MagickBooleanType WritePS3Image(const ImageInfo *image_info,
190 % Image *image,ExceptionInfo *exception)
192 % A description of each parameter follows:
194 % o image_info: Specifies a pointer to a ImageInfo structure.
196 % o image: the image.
198 % o exception: return any errors or warnings in this structure.
202 static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
203 Image *image,Image *inject_image,ExceptionInfo *exception)
221 write_info=CloneImageInfo(image_info);
222 (void) CopyMagickString(write_info->filename,"GROUP4:",MaxTextExtent);
223 (void) CopyMagickString(write_info->magick,"GROUP4",MaxTextExtent);
224 group4_image=CloneImage(inject_image,0,0,MagickTrue,exception);
225 if (group4_image == (Image *) NULL)
227 group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
229 group4_image=DestroyImage(group4_image);
230 if (group4 == (unsigned char *) NULL)
232 write_info=DestroyImageInfo(write_info);
233 if (WriteBlob(image,length,group4) != (ssize_t) length)
235 group4=(unsigned char *) RelinquishMagickMemory(group4);
239 static MagickBooleanType SerializeImage(const ImageInfo *image_info,
240 Image *image,unsigned char **pixels,size_t *length,ExceptionInfo *exception)
245 register const Quantum
251 register unsigned char
257 assert(image != (Image *) NULL);
258 assert(image->signature == MagickSignature);
259 if (image->debug != MagickFalse)
260 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
262 *length=(image->colorspace == CMYKColorspace ? 4 : 3)*(size_t)
263 image->columns*image->rows;
264 *pixels=(unsigned char *) AcquireQuantumMemory(*length,sizeof(**pixels));
265 if (*pixels == (unsigned char *) NULL)
266 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
268 for (y=0; y < (ssize_t) image->rows; y++)
270 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
271 if (p == (const Quantum *) NULL)
273 if (image->colorspace != CMYKColorspace)
274 for (x=0; x < (ssize_t) image->columns; x++)
276 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
277 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
278 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
279 p+=GetPixelChannels(image);
282 for (x=0; x < (ssize_t) image->columns; x++)
284 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
285 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
286 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
287 *q++=ScaleQuantumToChar(GetPixelBlack(image,p));
288 p+=GetPixelChannels(image);
290 if (image->previous == (Image *) NULL)
292 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
294 if (status == MagickFalse)
298 if (status == MagickFalse)
299 *pixels=(unsigned char *) RelinquishMagickMemory(*pixels);
303 static MagickBooleanType SerializeImageChannel(const ImageInfo *image_info,
304 Image *image,unsigned char **pixels,size_t *length,ExceptionInfo *exception)
309 register const Quantum
315 register unsigned char
329 assert(image != (Image *) NULL);
330 assert(image->signature == MagickSignature);
331 if (image->debug != MagickFalse)
332 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
334 pack=IsImageMonochrome(image,exception) == MagickFalse ? 1UL : 8UL;
335 padded_columns=((image->columns+pack-1)/pack)*pack;
336 *length=(size_t) padded_columns*image->rows/pack;
337 *pixels=(unsigned char *) AcquireQuantumMemory(*length,sizeof(**pixels));
338 if (*pixels == (unsigned char *) NULL)
339 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
341 for (y=0; y < (ssize_t) image->rows; y++)
343 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
344 if (p == (const Quantum *) NULL)
347 for (x=0; x < (ssize_t) image->columns; x++)
349 *q++=ScaleQuantumToChar(GetPixelIntensity(image,p));
350 p+=GetPixelChannels(image);
355 for (x=0; x < (ssize_t) padded_columns; x++)
357 bit=(unsigned char) 0x00;
358 if (x < (ssize_t) image->columns)
359 bit=(unsigned char) (GetPixelIntensity(image,p) == (Quantum)
360 TransparentAlpha ? 0x01 : 0x00);
361 code=(code << 1)+bit;
362 if (((x+1) % pack) == 0)
367 p+=GetPixelChannels(image);
370 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
372 if (status == MagickFalse)
375 if (status == MagickFalse)
376 *pixels=(unsigned char *) RelinquishMagickMemory(*pixels);
380 static MagickBooleanType SerializeImageIndexes(const ImageInfo *image_info,
381 Image *image,unsigned char **pixels,size_t *length,ExceptionInfo *exception)
386 register const Quantum
392 register unsigned char
398 assert(image != (Image *) NULL);
399 assert(image->signature == MagickSignature);
400 if (image->debug != MagickFalse)
401 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
403 *length=(size_t) image->columns*image->rows;
404 *pixels=(unsigned char *) AcquireQuantumMemory(*length,sizeof(**pixels));
405 if (*pixels == (unsigned char *) NULL)
406 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
408 for (y=0; y < (ssize_t) image->rows; y++)
410 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
411 if (p == (const Quantum *) NULL)
413 for (x=0; x < (ssize_t) image->columns; x++)
415 *q++=(unsigned char) GetPixelIndex(image,p);
416 p+=GetPixelChannels(image);
418 if (image->previous == (Image *) NULL)
420 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
422 if (status == MagickFalse)
426 if (status == MagickFalse)
427 *pixels=(unsigned char *) RelinquishMagickMemory(*pixels);
431 static MagickBooleanType WritePS3MaskImage(const ImageInfo *image_info,
432 Image *image,const CompressionType compression,ExceptionInfo *exception)
435 buffer[MaxTextExtent];
457 assert(image_info != (ImageInfo *) NULL);
458 assert(image_info->signature == MagickSignature);
459 assert(image != (Image *) NULL);
460 assert(image->signature == MagickSignature);
461 if (image->debug != MagickFalse)
462 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
463 assert(image->matte != MagickFalse);
466 Note BeginData DSC comment for update later.
468 start=TellBlob(image);
469 (void) FormatLocaleString(buffer,MaxTextExtent,
470 "%%%%BeginData:%13ld %s Bytes\n",0L,compression == NoCompression ?
472 (void) WriteBlobString(image,buffer);
473 stop=TellBlob(image);
475 Only lossless compressions for the mask.
482 (void) FormatLocaleString(buffer,MaxTextExtent,
483 "currentfile %.20g %.20g "PS3_NoCompression" ByteStreamDecodeFilter\n",
484 (double) image->columns,(double) image->rows);
488 case Group4Compression:
490 (void) FormatLocaleString(buffer,MaxTextExtent,
491 "currentfile %.20g %.20g "PS3_FaxCompression" ByteStreamDecodeFilter\n",
492 (double) image->columns,(double) image->rows);
497 (void) FormatLocaleString(buffer,MaxTextExtent,
498 "currentfile %.20g %.20g "PS3_LZWCompression" ByteStreamDecodeFilter\n",
499 (double) image->columns,(double) image->rows);
504 (void) FormatLocaleString(buffer,MaxTextExtent,
505 "currentfile %.20g %.20g "PS3_RLECompression" ByteStreamDecodeFilter\n",
506 (double) image->columns,(double) image->rows);
511 (void) FormatLocaleString(buffer,MaxTextExtent,
512 "currentfile %.20g %.20g "PS3_ZipCompression" ByteStreamDecodeFilter\n",
513 (double) image->columns,(double) image->rows);
517 (void) WriteBlobString(image,buffer);
518 (void) WriteBlobString(image,"/ReusableStreamDecode filter\n");
519 mask_image=SeparateImage(image,AlphaChannel,exception);
520 if (mask_image == (Image *) NULL)
521 ThrowWriterException(CoderError,exception->reason);
522 (void) SetImageType(mask_image,BilevelType,exception);
523 (void) SetImageType(mask_image,PaletteType,exception);
524 mask_image->matte=MagickFalse;
525 pixels=(unsigned char *) NULL;
532 status=SerializeImageChannel(image_info,mask_image,&pixels,&length,
534 if (status == MagickFalse)
536 Ascii85Initialize(image);
537 for (i=0; i < (ssize_t) length; i++)
538 Ascii85Encode(image,pixels[i]);
540 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
544 case Group4Compression:
546 if ((compression == FaxCompression) ||
547 (LocaleCompare(CCITTParam,"0") == 0))
548 status=HuffmanEncodeImage(image_info,image,mask_image,exception);
550 status=Huffman2DEncodeImage(image_info,image,mask_image,exception);
555 status=SerializeImageChannel(image_info,mask_image,&pixels,&length,
557 if (status == MagickFalse)
559 status=LZWEncodeImage(image,length,pixels,exception);
560 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
565 status=SerializeImageChannel(image_info,mask_image,&pixels,&length,
567 if (status == MagickFalse)
569 status=PackbitsEncodeImage(image,length,pixels,exception);
570 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
575 status=SerializeImageChannel(image_info,mask_image,&pixels,&length,
577 if (status == MagickFalse)
579 status=ZLIBEncodeImage(image,length,pixels,exception);
580 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
584 mask_image=DestroyImage(mask_image);
585 (void) WriteBlobByte(image,'\n');
586 length=(size_t) (TellBlob(image)-stop);
587 stop=TellBlob(image);
588 offset=SeekBlob(image,start,SEEK_SET);
590 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
591 (void) FormatLocaleString(buffer,MaxTextExtent,
592 "%%%%BeginData:%13ld %s Bytes\n",(long) length,
593 compression == NoCompression ? "ASCII" : "BINARY");
594 (void) WriteBlobString(image,buffer);
595 offset=SeekBlob(image,stop,SEEK_SET);
597 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
598 (void) WriteBlobString(image,"%%EndData\n");
599 (void) WriteBlobString(image, "/mask_stream exch def\n");
603 static MagickBooleanType WritePS3Image(const ImageInfo *image_info,Image *image,
604 ExceptionInfo *exception)
609 "/ByteStreamDecodeFilter",
614 " z "PS3_NoCompression" eq { /ASCII85Decode filter } if",
615 " z "PS3_FaxCompression" eq",
622 " /CCITTFaxDecode filter",
624 " z "PS3_JPEGCompression" eq { /DCTDecode filter } if",
625 " z "PS3_LZWCompression" eq { /LZWDecode filter } if",
626 " z "PS3_RLECompression" eq { /RunLengthDecode filter } if",
627 " z "PS3_ZipCompression" eq { /FlateDecode filter } if",
630 "/DirectClassImageDict",
632 " colorspace "PS3_RGBColorspace" eq",
634 " /DeviceRGB setcolorspace",
639 " /BitsPerComponent 8",
640 " /DataSource pixel_stream",
641 " /MultipleDataSources false",
642 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
643 " /Decode [0 1 0 1 0 1]",
647 " /DeviceCMYK setcolorspace",
652 " /BitsPerComponent 8",
653 " /DataSource pixel_stream",
654 " /MultipleDataSources false",
655 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
657 " compression "PS3_JPEGCompression" eq",
658 " { [1 0 1 0 1 0 1 0] }",
659 " { [0 1 0 1 0 1 0 1] }",
666 "/PseudoClassImageDict",
668 " % Colors in colormap image.",
669 " currentfile buffer readline pop",
670 " token pop /colors exch def pop",
673 " % Depth of grayscale image.",
674 " currentfile buffer readline pop",
675 " token pop /bits exch def pop",
676 " /DeviceGray setcolorspace",
681 " /BitsPerComponent bits",
683 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
684 " /DataSource pixel_stream",
689 " /colormap colors 3 mul string def",
690 " compression "PS3_NoCompression" eq",
691 " { currentfile /ASCII85Decode filter colormap readstring pop pop }",
692 " { currentfile colormap readstring pop pop }",
694 " [ /Indexed /DeviceRGB colors 1 sub colormap ] setcolorspace",
699 " /BitsPerComponent 8",
701 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
702 " /DataSource pixel_stream",
708 "/NonMaskedImageDict",
710 " class "PS3_PseudoClass" eq",
711 " { PseudoClassImageDict }",
712 " { DirectClassImageDict }",
720 " /InterleaveType 3",
721 " /DataDict NonMaskedImageDict",
727 " /BitsPerComponent 1",
728 " /DataSource mask_stream",
729 " /MultipleDataSources false",
730 " /ImageMatrix [ columns 0 0 rows neg 0 rows]",
741 " /buffer 512 string def",
743 " currentfile buffer readline pop",
744 " token pop /x exch def",
745 " token pop /y exch def pop",
747 " % Image size and font size.",
748 " currentfile buffer readline pop",
749 " token pop /x exch def",
750 " token pop /y exch def pop",
751 " currentfile buffer readline pop",
752 " token pop /pointsize exch def pop",
759 " currentfile buffer readline pop",
760 " token pop /clipped exch def pop",
762 " currentfile buffer readline pop",
763 " token pop /sp exch def pop",
764 " % Image pixel size.",
765 " currentfile buffer readline pop",
766 " token pop /columns exch def",
767 " token pop /rows exch def pop",
768 " % Colorspace (RGB/CMYK).",
769 " currentfile buffer readline pop",
770 " token pop /colorspace exch def pop",
772 " currentfile buffer readline pop",
773 " token pop /alpha exch def pop",
775 " currentfile buffer readline pop",
776 " token pop /stencil exch def pop",
777 " % Image class (direct/pseudo).",
778 " currentfile buffer readline pop",
779 " token pop /class exch def pop",
780 " % Compression type.",
781 " currentfile buffer readline pop",
782 " token pop /compression exch def pop",
783 " % Clip and render.",
784 " /pixel_stream currentfile columns rows compression ByteStreamDecodeFilter def",
785 " clipped { ClipImage } if",
786 " alpha stencil not and",
787 " { MaskedImageDict mask_stream resetfile }",
788 " { NonMaskedImageDict }",
790 " stencil { 0 setgray imagemask } { image } ifelse",
791 " sp { showpage } if",
797 buffer[MaxTextExtent],
800 page_geometry[MaxTextExtent];
860 Open output image file.
862 assert(image_info != (const ImageInfo *) NULL);
863 assert(image_info->signature == MagickSignature);
864 assert(image != (Image *) NULL);
865 assert(image->signature == MagickSignature);
866 if (image->debug != MagickFalse)
867 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
868 assert(exception != (ExceptionInfo *) NULL);
869 assert(exception->signature == MagickSignature);
870 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
871 if (status == MagickFalse)
873 compression=image->compression;
874 if (image_info->compression != UndefinedCompression)
875 compression=image_info->compression;
879 case Group4Compression:
881 if ((IsImageMonochrome(image,exception) == MagickFalse) ||
882 (image->matte != MagickFalse))
883 compression=RLECompression;
886 #if !defined(MAGICKCORE_JPEG_DELEGATE)
887 case JPEGCompression:
889 compression=RLECompression;
890 (void) ThrowMagickException(exception,GetMagickModule(),
891 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
896 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
899 compression=RLECompression;
900 (void) ThrowMagickException(exception,GetMagickModule(),
901 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZLIB)",
909 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
915 Scale relative to dots-per-inch.
917 delta.x=DefaultResolution;
918 delta.y=DefaultResolution;
919 resolution.x=image->resolution.x;
920 resolution.y=image->resolution.y;
921 if ((resolution.x == 0.0) || (resolution.y == 0.0))
923 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
924 resolution.x=geometry_info.rho;
925 resolution.y=geometry_info.sigma;
926 if ((flags & SigmaValue) == 0)
927 resolution.y=resolution.x;
929 if (image_info->density != (char *) NULL)
931 flags=ParseGeometry(image_info->density,&geometry_info);
932 resolution.x=geometry_info.rho;
933 resolution.y=geometry_info.sigma;
934 if ((flags & SigmaValue) == 0)
935 resolution.y=resolution.x;
937 if (image->units == PixelsPerCentimeterResolution)
939 resolution.x=(size_t) (100.0*2.54*resolution.x+0.5)/100.0;
940 resolution.y=(size_t) (100.0*2.54*resolution.y+0.5)/100.0;
942 SetGeometry(image,&geometry);
943 (void) FormatLocaleString(page_geometry,MaxTextExtent,"%.20gx%.20g",
944 (double) image->columns,(double) image->rows);
945 if (image_info->page != (char *) NULL)
946 (void) CopyMagickString(page_geometry,image_info->page,MaxTextExtent);
948 if ((image->page.width != 0) && (image->page.height != 0))
949 (void) FormatLocaleString(page_geometry,MaxTextExtent,
950 "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,(double)
951 image->page.height,(double) image->page.x,(double) image->page.y);
953 if ((image->gravity != UndefinedGravity) &&
954 (LocaleCompare(image_info->magick,"PS") == 0))
955 (void) CopyMagickString(page_geometry,PSPageGeometry,MaxTextExtent);
956 (void) ConcatenateMagickString(page_geometry,">",MaxTextExtent);
957 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
958 &geometry.width,&geometry.height);
959 scale.x=(double) (geometry.width*delta.x)/resolution.x;
960 geometry.width=(size_t) floor(scale.x+0.5);
961 scale.y=(double) (geometry.height*delta.y)/resolution.y;
962 geometry.height=(size_t) floor(scale.y+0.5);
963 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
964 (void) ParseGravityGeometry(image,page_geometry,&page_info,exception);
965 if (image->gravity != UndefinedGravity)
967 geometry.x=(-page_info.x);
968 geometry.y=(ssize_t) (media_info.height+page_info.y-image->rows);
971 if (image_info->pointsize != 0.0)
972 pointsize=image_info->pointsize;
974 value=GetImageProperty(image,"label",exception);
975 if (value != (const char *) NULL)
976 text_size=(size_t) (MultilineCensus(value)*pointsize+12);
981 Postscript header on the first page.
983 if (LocaleCompare(image_info->magick,"PS3") == 0)
984 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0\n",MaxTextExtent);
986 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0 EPSF-3.0\n",
988 (void) WriteBlobString(image,buffer);
989 (void) FormatLocaleString(buffer,MaxTextExtent,
990 "%%%%Creator: ImageMagick %s\n",MagickLibVersionText);
991 (void) WriteBlobString(image,buffer);
992 (void) FormatLocaleString(buffer,MaxTextExtent,"%%%%Title: %s\n",
994 (void) WriteBlobString(image,buffer);
995 timer=time((time_t *) NULL);
996 (void) FormatMagickTime(timer,MaxTextExtent,date);
997 (void) FormatLocaleString(buffer,MaxTextExtent,
998 "%%%%CreationDate: %s\n",date);
999 (void) WriteBlobString(image,buffer);
1000 bounds.x1=(double) geometry.x;
1001 bounds.y1=(double) geometry.y;
1002 bounds.x2=(double) geometry.x+scale.x;
1003 bounds.y2=(double) geometry.y+scale.y+text_size;
1004 if ((image_info->adjoin != MagickFalse) &&
1005 (GetNextImageInList(image) != (Image *) NULL))
1007 (void) WriteBlobString(image,"%%BoundingBox: (atend)\n");
1008 (void) WriteBlobString(image,"%%HiResBoundingBox: (atend)\n");
1012 (void) FormatLocaleString(buffer,MaxTextExtent,
1013 "%%%%BoundingBox: %g %g %g %g\n",floor(bounds.x1+0.5),
1014 floor(bounds.y1+0.5),ceil(bounds.x2-0.5),ceil(bounds.y2-0.5));
1015 (void) WriteBlobString(image,buffer);
1016 (void) FormatLocaleString(buffer,MaxTextExtent,
1017 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,
1018 bounds.y1,bounds.x2,bounds.y2);
1019 (void) WriteBlobString(image,buffer);
1020 if (image->colorspace == CMYKColorspace)
1021 (void) WriteBlobString(image,
1022 "%%DocumentProcessColors: Cyan Magenta Yellow Black\n");
1024 if (IsImageGray(image,exception) != MagickFalse)
1025 (void) WriteBlobString(image,
1026 "%%DocumentProcessColors: Black\n");
1031 value=GetImageProperty(image,"label",exception);
1032 if (value != (const char *) NULL)
1033 (void) WriteBlobString(image,
1034 "%%DocumentNeededResources: font Helvetica\n");
1035 (void) WriteBlobString(image,"%%LanguageLevel: 3\n");
1037 Pages, orientation and order.
1039 if (LocaleCompare(image_info->magick,"PS3") != 0)
1040 (void) WriteBlobString(image,"%%Pages: 1\n");
1043 (void) WriteBlobString(image,"%%Orientation: Portrait\n");
1044 (void) WriteBlobString(image,"%%PageOrder: Ascend\n");
1045 if (image_info->adjoin == MagickFalse)
1046 (void) CopyMagickString(buffer,"%%Pages: 1\n",MaxTextExtent);
1048 (void) FormatLocaleString(buffer,MaxTextExtent,
1049 "%%%%Pages: %.20g\n",(double) GetImageListLength(image));
1050 (void) WriteBlobString(image,buffer);
1052 (void) WriteBlobString(image,"%%EndComments\n");
1054 The static postscript procedures prolog.
1056 (void)WriteBlobString(image,"%%BeginProlog\n");
1057 for (q=PostscriptProlog; *q; q++)
1059 (void) WriteBlobString(image,*q);
1060 (void) WriteBlobByte(image,'\n');
1063 One label line for each line in label string.
1065 value=GetImageProperty(image,"label",exception);
1066 if (value != (const char *) NULL)
1068 (void) WriteBlobString(image,"\n %% Labels.\n /Helvetica "
1069 " findfont pointsize scalefont setfont\n");
1070 for (i=(ssize_t) MultilineCensus(value)-1; i >= 0; i--)
1072 (void) WriteBlobString(image,
1073 " currentfile buffer readline pop token pop\n");
1074 (void) FormatLocaleString(buffer,MaxTextExtent,
1075 " 0 y %g add moveto show pop\n",i*pointsize+12);
1076 (void) WriteBlobString(image,buffer);
1080 The static postscript procedures epilog.
1082 for (q=PostscriptEpilog; *q; q++)
1084 (void) WriteBlobString(image,*q);
1085 (void) WriteBlobByte(image,'\n');
1087 (void)WriteBlobString(image,"%%EndProlog\n");
1089 (void) FormatLocaleString(buffer,MaxTextExtent,"%%%%Page: 1 %.20g\n",
1091 (void) WriteBlobString(image,buffer);
1095 (void) FormatLocaleString(buffer,MaxTextExtent,
1096 "%%%%PageBoundingBox: %.20g %.20g %.20g %.20g\n",(double) geometry.x,
1097 (double) geometry.y,geometry.x+(double) geometry.width,geometry.y+
1098 (double) (geometry.height+text_size));
1099 (void) WriteBlobString(image,buffer);
1101 Page process colors if not RGB.
1103 if (image->colorspace == CMYKColorspace)
1104 (void) WriteBlobString(image,
1105 "%%PageProcessColors: Cyan Magenta Yellow Black\n");
1107 if (IsImageGray(image,exception) != MagickFalse)
1108 (void) WriteBlobString(image,"%%PageProcessColors: Black\n");
1110 Adjust document bounding box to bound page bounding box.
1112 if ((double) geometry.x < bounds.x1)
1113 bounds.x1=(double) geometry.x;
1114 if ((double) geometry.y < bounds.y1)
1115 bounds.y1=(double) geometry.y;
1116 if ((double) (geometry.x+scale.x) > bounds.x2)
1117 bounds.x2=(double) geometry.x+scale.x;
1118 if ((double) (geometry.y+scale.y+text_size) > bounds.y2)
1119 bounds.y2=(double) geometry.y+scale.y+text_size;
1121 Page font resource if there's a label.
1123 value=GetImageProperty(image,"label",exception);
1124 if (value != (const char *) NULL)
1125 (void) WriteBlobString(image,"%%PageResources: font Helvetica\n");
1127 PS clipping path from Photoshop clipping path.
1129 if ((image->mask != MagickFalse) ||
1130 (LocaleNCompare("8BIM:",image->magick_filename,5) != 0))
1131 (void) WriteBlobString(image,"/ClipImage {} def\n");
1137 value=GetImageProperty(image,image->magick_filename,exception);
1138 if (value == (const char *) NULL)
1139 return(MagickFalse);
1140 (void) WriteBlobString(image,value);
1141 (void) WriteBlobByte(image,'\n');
1144 Push a dictionary for our own def's if this an EPS.
1146 if (LocaleCompare(image_info->magick,"PS3") != 0)
1147 (void) WriteBlobString(image,"userdict begin\n");
1151 if ((image->matte != MagickFalse) &&
1152 (WritePS3MaskImage(image_info,image,compression,exception) == MagickFalse))
1154 (void) CloseBlob(image);
1155 return(MagickFalse);
1158 Remember position of BeginData comment so we can update it.
1160 start=TellBlob(image);
1161 (void) FormatLocaleString(buffer,MaxTextExtent,
1162 "%%%%BeginData:%13ld %s Bytes\n",0L,
1163 compression == NoCompression ? "ASCII" : "BINARY");
1164 (void) WriteBlobString(image,buffer);
1165 stop=TellBlob(image);
1166 (void) WriteBlobString(image,"DisplayImage\n");
1168 Translate, scale, and font point size.
1170 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n%g %g\n%g\n",
1171 (double) geometry.x,(double) geometry.y,scale.x,scale.y,pointsize);
1172 (void) WriteBlobString(image,buffer);
1176 labels=(char **) NULL;
1177 value=GetImageProperty(image,"label",exception);
1178 if (value != (const char *) NULL)
1179 labels=StringToList(value);
1180 if (labels != (char **) NULL)
1182 for (i=0; labels[i] != (char *) NULL; i++)
1184 if (compression != NoCompression)
1186 for (j=0; labels[i][j] != '\0'; j++)
1187 (void) WriteBlobByte(image,(unsigned char) labels[i][j]);
1188 (void) WriteBlobByte(image,'\n');
1192 (void) WriteBlobString(image,"<~");
1193 Ascii85Initialize(image);
1194 for (j=0; labels[i][j] != '\0'; j++)
1195 Ascii85Encode(image,(unsigned char) labels[i][j]);
1196 Ascii85Flush(image);
1198 labels[i]=DestroyString(labels[i]);
1200 labels=(char **) RelinquishMagickMemory(labels);
1203 Photoshop clipping path active?
1205 if ((image->mask != MagickFalse) &&
1206 (LocaleNCompare("8BIM:",image->magick_filename,5) == 0))
1207 (void) WriteBlobString(image,"true\n");
1209 (void) WriteBlobString(image,"false\n");
1211 Showpage for non-EPS.
1213 (void) WriteBlobString(image, LocaleCompare(image_info->magick,"PS3") == 0 ?
1214 "true\n" : "false\n");
1216 Image columns, rows, and color space.
1218 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n%s\n",
1219 (double) image->columns,(double) image->rows,image->colorspace ==
1220 CMYKColorspace ? PS3_CMYKColorspace : PS3_RGBColorspace);
1221 (void) WriteBlobString(image,buffer);
1225 (void) WriteBlobString(image,image->matte != MagickFalse ?
1226 "true\n" : "false\n");
1228 Render with imagemask operator?
1230 option=GetImageOption(image_info,"ps3:imagemask");
1231 (void) WriteBlobString(image,((option != (const char *) NULL) &&
1232 (IsImageMonochrome(image,exception) != MagickFalse)) ?
1233 "true\n" : "false\n");
1237 pixels=(unsigned char *) NULL;
1239 if ((image_info->type != TrueColorType) &&
1240 (image_info->type != TrueColorMatteType) &&
1241 (image_info->type != ColorSeparationType) &&
1242 (image_info->type != ColorSeparationMatteType) &&
1243 (image->colorspace != CMYKColorspace) &&
1244 ((IsImageGray(image,exception) != MagickFalse) ||
1245 (IsImageMonochrome(image,exception) != MagickFalse)))
1250 (void) WriteBlobString(image,PS3_PseudoClass"\n");
1251 switch (compression)
1256 (void) WriteBlobString(image,PS3_NoCompression"\n");
1259 case FaxCompression:
1260 case Group4Compression:
1262 (void) WriteBlobString(image,PS3_FaxCompression"\n");
1265 case JPEGCompression:
1267 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1270 case LZWCompression:
1272 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1275 case RLECompression:
1277 (void) WriteBlobString(image,PS3_RLECompression"\n");
1280 case ZipCompression:
1282 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1287 Number of colors -- 0 for single component non-color mapped data.
1289 (void) WriteBlobString(image,"0\n");
1291 1 bit or 8 bit components?
1293 (void) FormatLocaleString(buffer,MaxTextExtent,"%d\n",
1294 IsImageMonochrome(image,exception) != MagickFalse ? 1 : 8);
1295 (void) WriteBlobString(image,buffer);
1299 if (compression == JPEGCompression)
1300 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
1302 if ((compression == FaxCompression) ||
1303 (compression == Group4Compression))
1305 if (LocaleCompare(CCITTParam,"0") == 0)
1306 status=HuffmanEncodeImage(image_info,image,image,exception);
1308 status=Huffman2DEncodeImage(image_info,image,image,exception);
1312 status=SerializeImageChannel(image_info,image,&pixels,&length,
1314 if (status == MagickFalse)
1316 (void) CloseBlob(image);
1317 return(MagickFalse);
1319 switch (compression)
1324 Ascii85Initialize(image);
1325 for (i=0; i < (ssize_t) length; i++)
1326 Ascii85Encode(image,pixels[i]);
1327 Ascii85Flush(image);
1331 case LZWCompression:
1333 status=LZWEncodeImage(image,length,pixels,exception);
1336 case RLECompression:
1338 status=PackbitsEncodeImage(image,length,pixels,exception);
1341 case ZipCompression:
1343 status=ZLIBEncodeImage(image,length,pixels,exception);
1347 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1351 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1352 (compression == JPEGCompression))
1357 (void) WriteBlobString(image,PS3_DirectClass"\n");
1358 switch (compression)
1363 (void) WriteBlobString(image,PS3_NoCompression"\n");
1366 case RLECompression:
1368 (void) WriteBlobString(image,PS3_RLECompression"\n");
1371 case JPEGCompression:
1373 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1376 case LZWCompression:
1378 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1381 case ZipCompression:
1383 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1390 if (compression == JPEGCompression)
1391 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
1395 Stream based compressions.
1397 status=SerializeImage(image_info,image,&pixels,&length,exception);
1398 if (status == MagickFalse)
1400 (void) CloseBlob(image);
1401 return(MagickFalse);
1403 switch (compression)
1408 Ascii85Initialize(image);
1409 for (i=0; i < (ssize_t) length; i++)
1410 Ascii85Encode(image,pixels[i]);
1411 Ascii85Flush(image);
1415 case RLECompression:
1417 status=PackbitsEncodeImage(image,length,pixels,exception);
1420 case LZWCompression:
1422 status=LZWEncodeImage(image,length,pixels,exception);
1425 case ZipCompression:
1427 status=ZLIBEncodeImage(image,length,pixels,exception);
1431 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1439 (void) WriteBlobString(image,PS3_PseudoClass"\n");
1440 switch (compression)
1445 (void) WriteBlobString(image,PS3_NoCompression"\n");
1448 case JPEGCompression:
1450 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1453 case RLECompression:
1455 (void) WriteBlobString(image,PS3_RLECompression"\n");
1458 case LZWCompression:
1460 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1463 case ZipCompression:
1465 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1470 Number of colors in color map.
1472 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",
1473 (double) image->colors);
1474 (void) WriteBlobString(image,buffer);
1476 Color map - uncompressed.
1478 if ((compression != NoCompression) &&
1479 (compression != UndefinedCompression))
1481 for (i=0; i < (ssize_t) image->colors; i++)
1483 pixel=ScaleQuantumToChar(image->colormap[i].red);
1484 (void) WriteBlobByte(image,(unsigned char) pixel);
1485 pixel=ScaleQuantumToChar(image->colormap[i].green);
1486 (void) WriteBlobByte(image,(unsigned char) pixel);
1487 pixel=ScaleQuantumToChar(image->colormap[i].blue);
1488 (void) WriteBlobByte(image,(unsigned char) pixel);
1493 Ascii85Initialize(image);
1494 for (i=0; i < (ssize_t) image->colors; i++)
1496 pixel=ScaleQuantumToChar(image->colormap[i].red);
1497 Ascii85Encode(image,(unsigned char) pixel);
1498 pixel=ScaleQuantumToChar(image->colormap[i].green);
1499 Ascii85Encode(image,(unsigned char) pixel);
1500 pixel=ScaleQuantumToChar(image->colormap[i].blue);
1501 Ascii85Encode(image,(unsigned char) pixel);
1503 Ascii85Flush(image);
1505 status=SerializeImageIndexes(image_info,image,&pixels,&length,
1507 if (status == MagickFalse)
1509 (void) CloseBlob(image);
1510 return(MagickFalse);
1512 switch (compression)
1517 Ascii85Initialize(image);
1518 for (i=0; i < (ssize_t) length; i++)
1519 Ascii85Encode(image,pixels[i]);
1520 Ascii85Flush(image);
1524 case JPEGCompression:
1526 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
1529 case RLECompression:
1531 status=PackbitsEncodeImage(image,length,pixels,exception);
1534 case LZWCompression:
1536 status=LZWEncodeImage(image,length,pixels,exception);
1539 case ZipCompression:
1541 status=ZLIBEncodeImage(image,length,pixels,exception);
1545 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1547 (void) WriteBlobByte(image,'\n');
1548 if (status == MagickFalse)
1550 (void) CloseBlob(image);
1551 return(MagickFalse);
1554 Update BeginData now that we know the data size.
1556 length=(size_t) (TellBlob(image)-stop);
1557 stop=TellBlob(image);
1558 offset=SeekBlob(image,start,SEEK_SET);
1560 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
1561 (void) FormatLocaleString(buffer,MaxTextExtent,
1562 "%%%%BeginData:%13ld %s Bytes\n",(long) length,
1563 compression == NoCompression ? "ASCII" : "BINARY");
1564 (void) WriteBlobString(image,buffer);
1565 offset=SeekBlob(image,stop,SEEK_SET);
1566 (void) WriteBlobString(image,"%%EndData\n");
1568 End private dictionary if this an EPS.
1570 if (LocaleCompare(image_info->magick,"PS3") != 0)
1571 (void) WriteBlobString(image,"end\n");
1572 (void) WriteBlobString(image,"%%PageTrailer\n");
1573 if (GetNextImageInList(image) == (Image *) NULL)
1575 image=SyncNextImageInList(image);
1576 status=SetImageProgress(image,SaveImagesTag,scene++,
1577 GetImageListLength(image));
1578 if (status == MagickFalse)
1580 } while (image_info->adjoin != MagickFalse);
1581 (void) WriteBlobString(image,"%%Trailer\n");
1584 (void) FormatLocaleString(buffer,MaxTextExtent,
1585 "%%%%BoundingBox: %g %g %g %g\n",floor(bounds.x1+0.5),
1586 floor(bounds.y1+0.5),ceil(bounds.x2-0.5),ceil(bounds.y2-0.5));
1587 (void) WriteBlobString(image,buffer);
1588 (void) FormatLocaleString(buffer,MaxTextExtent,
1589 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,bounds.y1,
1590 bounds.x2,bounds.y2);
1591 (void) WriteBlobString(image,buffer);
1593 (void) WriteBlobString(image,"%%EOF\n");
1594 (void) CloseBlob(image);