2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Write Postscript Level III Format %
21 % Copyright 1999-2011 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/color.h"
49 #include "MagickCore/color-private.h"
50 #include "MagickCore/compress.h"
51 #include "MagickCore/constitute.h"
52 #include "MagickCore/draw.h"
53 #include "MagickCore/exception.h"
54 #include "MagickCore/exception-private.h"
55 #include "MagickCore/geometry.h"
56 #include "MagickCore/image.h"
57 #include "MagickCore/image-private.h"
58 #include "MagickCore/list.h"
59 #include "MagickCore/magick.h"
60 #include "MagickCore/memory_.h"
61 #include "MagickCore/monitor.h"
62 #include "MagickCore/monitor-private.h"
63 #include "MagickCore/option.h"
64 #include "MagickCore/pixel-accessor.h"
65 #include "MagickCore/property.h"
66 #include "MagickCore/quantum-private.h"
67 #include "MagickCore/resource_.h"
68 #include "MagickCore/static.h"
69 #include "MagickCore/string_.h"
70 #include "MagickCore/module.h"
71 #include "MagickCore/token.h"
72 #include "MagickCore/utility.h"
73 #include "MagickCore/module.h"
78 #define PS3_NoCompression "0"
79 #define PS3_FaxCompression "1"
80 #define PS3_JPEGCompression "2"
81 #define PS3_LZWCompression "3"
82 #define PS3_RLECompression "4"
83 #define PS3_ZipCompression "5"
85 #define PS3_RGBColorspace "0"
86 #define PS3_CMYKColorspace "1"
88 #define PS3_DirectClass "0"
89 #define PS3_PseudoClass "1"
91 #if defined(MAGICKCORE_TIFF_DELEGATE)
92 #define CCITTParam "-1"
94 #define CCITTParam "0"
100 static MagickBooleanType
101 WritePS3Image(const ImageInfo *,Image *,ExceptionInfo *);
104 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108 % R e g i s t e r P S 3 I m a g e %
112 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
114 % RegisterPS3Image() adds properties for the PS3 image format to the list of
115 % supported formats. The properties include the image format tag, a method to
116 % read and/or write the format, whether the format supports the saving of more
117 % than one frame to the same file or blob, whether the format supports native
118 % in-memory I/O, and a brief description of the format.
120 % The format of the RegisterPS3Image method is:
122 % size_t RegisterPS3Image(void)
125 ModuleExport size_t RegisterPS3Image(void)
130 entry=SetMagickInfo("EPS3");
131 entry->encoder=(EncodeImageHandler *) WritePS3Image;
132 entry->description=ConstantString("Level III Encapsulated PostScript");
133 entry->module=ConstantString("PS3");
134 entry->seekable_stream=MagickTrue;
135 (void) RegisterMagickInfo(entry);
136 entry=SetMagickInfo("PS3");
137 entry->encoder=(EncodeImageHandler *) WritePS3Image;
138 entry->description=ConstantString("Level III PostScript");
139 entry->module=ConstantString("PS3");
140 entry->seekable_stream=MagickTrue;
141 (void) RegisterMagickInfo(entry);
142 return(MagickImageCoderSignature);
146 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150 % U n r e g i s t e r P S 3 I m a g e %
154 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
156 % UnregisterPS3Image() removes format registrations made by the PS3 module
157 % from the list of supported formats.
159 % The format of the UnregisterPS3Image method is:
161 % UnregisterPS3Image(void)
164 ModuleExport void UnregisterPS3Image(void)
166 (void) UnregisterMagickInfo("EPS3");
167 (void) UnregisterMagickInfo("PS3");
171 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
175 % W r i t e P S 3 I m a g e %
179 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
181 % WritePS3Image() translates an image to encapsulated Postscript Level III
182 % for printing. If the supplied geometry is null, the image is centered on
183 % the Postscript page. Otherwise, the image is positioned as specified by the
186 % The format of the WritePS3Image method is:
188 % MagickBooleanType WritePS3Image(const ImageInfo *image_info,
189 % Image *image,ExceptionInfo *exception)
191 % A description of each parameter follows:
193 % o image_info: Specifies a pointer to a ImageInfo structure.
195 % o image: the image.
197 % o exception: return any errors or warnings in this structure.
201 static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
202 Image *image,Image *inject_image,ExceptionInfo *exception)
220 write_info=CloneImageInfo(image_info);
221 (void) CopyMagickString(write_info->filename,"GROUP4:",MaxTextExtent);
222 (void) CopyMagickString(write_info->magick,"GROUP4",MaxTextExtent);
223 group4_image=CloneImage(inject_image,0,0,MagickTrue,exception);
224 if (group4_image == (Image *) NULL)
226 group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
228 group4_image=DestroyImage(group4_image);
229 if (group4 == (unsigned char *) NULL)
231 write_info=DestroyImageInfo(write_info);
232 if (WriteBlob(image,length,group4) != (ssize_t) length)
234 group4=(unsigned char *) RelinquishMagickMemory(group4);
238 static MagickBooleanType SerializeImage(const ImageInfo *image_info,
239 Image *image,unsigned char **pixels,size_t *length,ExceptionInfo *exception)
244 register const Quantum
250 register unsigned char
256 assert(image != (Image *) NULL);
257 assert(image->signature == MagickSignature);
258 if (image->debug != MagickFalse)
259 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
261 *length=(image->colorspace == CMYKColorspace ? 4 : 3)*(size_t)
262 image->columns*image->rows;
263 *pixels=(unsigned char *) AcquireQuantumMemory(*length,sizeof(**pixels));
264 if (*pixels == (unsigned char *) NULL)
265 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
267 for (y=0; y < (ssize_t) image->rows; y++)
269 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
270 if (p == (const Quantum *) NULL)
272 if (image->colorspace != CMYKColorspace)
273 for (x=0; x < (ssize_t) image->columns; x++)
275 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
276 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
277 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
278 p+=GetPixelChannels(image);
281 for (x=0; x < (ssize_t) image->columns; x++)
283 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
284 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
285 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
286 *q++=ScaleQuantumToChar(GetPixelBlack(image,p));
287 p+=GetPixelChannels(image);
289 if (image->previous == (Image *) NULL)
291 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
293 if (status == MagickFalse)
297 if (status == MagickFalse)
298 *pixels=(unsigned char *) RelinquishMagickMemory(*pixels);
302 static MagickBooleanType SerializeImageChannel(const ImageInfo *image_info,
303 Image *image,unsigned char **pixels,size_t *length,ExceptionInfo *exception)
308 register const Quantum
314 register unsigned char
328 assert(image != (Image *) NULL);
329 assert(image->signature == MagickSignature);
330 if (image->debug != MagickFalse)
331 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
333 pack=IsImageMonochrome(image,exception) == MagickFalse ? 1UL : 8UL;
334 padded_columns=((image->columns+pack-1)/pack)*pack;
335 *length=(size_t) padded_columns*image->rows/pack;
336 *pixels=(unsigned char *) AcquireQuantumMemory(*length,sizeof(**pixels));
337 if (*pixels == (unsigned char *) NULL)
338 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
340 for (y=0; y < (ssize_t) image->rows; y++)
342 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
343 if (p == (const Quantum *) NULL)
346 for (x=0; x < (ssize_t) image->columns; x++)
348 *q++=ScaleQuantumToChar(GetPixelIntensity(image,p));
349 p+=GetPixelChannels(image);
354 for (x=0; x < (ssize_t) padded_columns; x++)
356 bit=(unsigned char) 0x00;
357 if (x < (ssize_t) image->columns)
358 bit=(unsigned char) (GetPixelIntensity(image,p) == (Quantum)
359 TransparentAlpha ? 0x01 : 0x00);
360 code=(code << 1)+bit;
361 if (((x+1) % pack) == 0)
366 p+=GetPixelChannels(image);
369 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
371 if (status == MagickFalse)
374 if (status == MagickFalse)
375 *pixels=(unsigned char *) RelinquishMagickMemory(*pixels);
379 static MagickBooleanType SerializeImageIndexes(const ImageInfo *image_info,
380 Image *image,unsigned char **pixels,size_t *length,ExceptionInfo *exception)
385 register const Quantum
391 register unsigned char
397 assert(image != (Image *) NULL);
398 assert(image->signature == MagickSignature);
399 if (image->debug != MagickFalse)
400 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
402 *length=(size_t) image->columns*image->rows;
403 *pixels=(unsigned char *) AcquireQuantumMemory(*length,sizeof(**pixels));
404 if (*pixels == (unsigned char *) NULL)
405 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
407 for (y=0; y < (ssize_t) image->rows; y++)
409 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
410 if (p == (const Quantum *) NULL)
412 for (x=0; x < (ssize_t) image->columns; x++)
414 *q++=(unsigned char) GetPixelIndex(image,p);
415 p+=GetPixelChannels(image);
417 if (image->previous == (Image *) NULL)
419 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
421 if (status == MagickFalse)
425 if (status == MagickFalse)
426 *pixels=(unsigned char *) RelinquishMagickMemory(*pixels);
430 static MagickBooleanType WritePS3MaskImage(const ImageInfo *image_info,
431 Image *image,const CompressionType compression,ExceptionInfo *exception)
437 buffer[MaxTextExtent];
459 assert(image_info != (ImageInfo *) NULL);
460 assert(image_info->signature == MagickSignature);
461 assert(image != (Image *) NULL);
462 assert(image->signature == MagickSignature);
463 if (image->debug != MagickFalse)
464 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
465 assert(image->matte != MagickFalse);
468 Note BeginData DSC comment for update later.
470 start=TellBlob(image);
471 (void) FormatLocaleString(buffer,MaxTextExtent,
472 "%%%%BeginData:%13ld %s Bytes\n",0L,compression == NoCompression ?
474 (void) WriteBlobString(image,buffer);
475 stop=TellBlob(image);
477 Only lossless compressions for the mask.
484 (void) FormatLocaleString(buffer,MaxTextExtent,
485 "currentfile %.20g %.20g "PS3_NoCompression" ByteStreamDecodeFilter\n",
486 (double) image->columns,(double) image->rows);
490 case Group4Compression:
492 (void) FormatLocaleString(buffer,MaxTextExtent,
493 "currentfile %.20g %.20g "PS3_FaxCompression" ByteStreamDecodeFilter\n",
494 (double) image->columns,(double) image->rows);
499 (void) FormatLocaleString(buffer,MaxTextExtent,
500 "currentfile %.20g %.20g "PS3_LZWCompression" ByteStreamDecodeFilter\n",
501 (double) image->columns,(double) image->rows);
506 (void) FormatLocaleString(buffer,MaxTextExtent,
507 "currentfile %.20g %.20g "PS3_RLECompression" ByteStreamDecodeFilter\n",
508 (double) image->columns,(double) image->rows);
513 (void) FormatLocaleString(buffer,MaxTextExtent,
514 "currentfile %.20g %.20g "PS3_ZipCompression" ByteStreamDecodeFilter\n",
515 (double) image->columns,(double) image->rows);
519 (void) WriteBlobString(image,buffer);
520 (void) WriteBlobString(image,"/ReusableStreamDecode filter\n");
521 mask_image=CloneImage(image,0,0,MagickTrue,exception);
522 if (mask_image == (Image *) NULL)
523 ThrowWriterException(CoderError,exception->reason);
524 channel_mask=SetPixelChannelMask(mask_image,AlphaChannel);
525 status=SeparateImage(mask_image,exception);
526 (void) SetPixelChannelMapMask(mask_image,channel_mask);
527 if (status == MagickFalse)
529 mask_image=DestroyImage(mask_image);
532 (void) SetImageType(mask_image,BilevelType,exception);
533 (void) SetImageType(mask_image,PaletteType,exception);
534 mask_image->matte=MagickFalse;
535 pixels=(unsigned char *) NULL;
542 status=SerializeImageChannel(image_info,mask_image,&pixels,&length,
544 if (status == MagickFalse)
546 Ascii85Initialize(image);
547 for (i=0; i < (ssize_t) length; i++)
548 Ascii85Encode(image,pixels[i]);
550 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
554 case Group4Compression:
556 if ((compression == FaxCompression) ||
557 (LocaleCompare(CCITTParam,"0") == 0))
558 status=HuffmanEncodeImage(image_info,image,mask_image,exception);
560 status=Huffman2DEncodeImage(image_info,image,mask_image,exception);
565 status=SerializeImageChannel(image_info,mask_image,&pixels,&length,
567 if (status == MagickFalse)
569 status=LZWEncodeImage(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=PackbitsEncodeImage(image,length,pixels,exception);
580 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
585 status=SerializeImageChannel(image_info,mask_image,&pixels,&length,
587 if (status == MagickFalse)
589 status=ZLIBEncodeImage(image,length,pixels,exception);
590 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
594 mask_image=DestroyImage(mask_image);
595 (void) WriteBlobByte(image,'\n');
596 length=(size_t) (TellBlob(image)-stop);
597 stop=TellBlob(image);
598 offset=SeekBlob(image,start,SEEK_SET);
600 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
601 (void) FormatLocaleString(buffer,MaxTextExtent,
602 "%%%%BeginData:%13ld %s Bytes\n",(long) length,
603 compression == NoCompression ? "ASCII" : "BINARY");
604 (void) WriteBlobString(image,buffer);
605 offset=SeekBlob(image,stop,SEEK_SET);
607 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
608 (void) WriteBlobString(image,"%%EndData\n");
609 (void) WriteBlobString(image, "/mask_stream exch def\n");
613 static MagickBooleanType WritePS3Image(const ImageInfo *image_info,Image *image,
614 ExceptionInfo *exception)
619 "/ByteStreamDecodeFilter",
624 " z "PS3_NoCompression" eq { /ASCII85Decode filter } if",
625 " z "PS3_FaxCompression" eq",
632 " /CCITTFaxDecode filter",
634 " z "PS3_JPEGCompression" eq { /DCTDecode filter } if",
635 " z "PS3_LZWCompression" eq { /LZWDecode filter } if",
636 " z "PS3_RLECompression" eq { /RunLengthDecode filter } if",
637 " z "PS3_ZipCompression" eq { /FlateDecode filter } if",
640 "/DirectClassImageDict",
642 " colorspace "PS3_RGBColorspace" eq",
644 " /DeviceRGB setcolorspace",
649 " /BitsPerComponent 8",
650 " /DataSource pixel_stream",
651 " /MultipleDataSources false",
652 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
653 " /Decode [0 1 0 1 0 1]",
657 " /DeviceCMYK setcolorspace",
662 " /BitsPerComponent 8",
663 " /DataSource pixel_stream",
664 " /MultipleDataSources false",
665 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
667 " compression "PS3_JPEGCompression" eq",
668 " { [1 0 1 0 1 0 1 0] }",
669 " { [0 1 0 1 0 1 0 1] }",
676 "/PseudoClassImageDict",
678 " % Colors in colormap image.",
679 " currentfile buffer readline pop",
680 " token pop /colors exch def pop",
683 " % Depth of grayscale image.",
684 " currentfile buffer readline pop",
685 " token pop /bits exch def pop",
686 " /DeviceGray setcolorspace",
691 " /BitsPerComponent bits",
693 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
694 " /DataSource pixel_stream",
699 " /colormap colors 3 mul string def",
700 " compression "PS3_NoCompression" eq",
701 " { currentfile /ASCII85Decode filter colormap readstring pop pop }",
702 " { currentfile colormap readstring pop pop }",
704 " [ /Indexed /DeviceRGB colors 1 sub colormap ] setcolorspace",
709 " /BitsPerComponent 8",
711 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
712 " /DataSource pixel_stream",
718 "/NonMaskedImageDict",
720 " class "PS3_PseudoClass" eq",
721 " { PseudoClassImageDict }",
722 " { DirectClassImageDict }",
730 " /InterleaveType 3",
731 " /DataDict NonMaskedImageDict",
737 " /BitsPerComponent 1",
738 " /DataSource mask_stream",
739 " /MultipleDataSources false",
740 " /ImageMatrix [ columns 0 0 rows neg 0 rows]",
751 " /buffer 512 string def",
753 " currentfile buffer readline pop",
754 " token pop /x exch def",
755 " token pop /y exch def pop",
757 " % Image size and font size.",
758 " currentfile buffer readline pop",
759 " token pop /x exch def",
760 " token pop /y exch def pop",
761 " currentfile buffer readline pop",
762 " token pop /pointsize exch def pop",
769 " currentfile buffer readline pop",
770 " token pop /clipped exch def pop",
772 " currentfile buffer readline pop",
773 " token pop /sp exch def pop",
774 " % Image pixel size.",
775 " currentfile buffer readline pop",
776 " token pop /columns exch def",
777 " token pop /rows exch def pop",
778 " % Colorspace (RGB/CMYK).",
779 " currentfile buffer readline pop",
780 " token pop /colorspace exch def pop",
782 " currentfile buffer readline pop",
783 " token pop /alpha exch def pop",
785 " currentfile buffer readline pop",
786 " token pop /stencil exch def pop",
787 " % Image class (direct/pseudo).",
788 " currentfile buffer readline pop",
789 " token pop /class exch def pop",
790 " % Compression type.",
791 " currentfile buffer readline pop",
792 " token pop /compression exch def pop",
793 " % Clip and render.",
794 " /pixel_stream currentfile columns rows compression ByteStreamDecodeFilter def",
795 " clipped { ClipImage } if",
796 " alpha stencil not and",
797 " { MaskedImageDict mask_stream resetfile }",
798 " { NonMaskedImageDict }",
800 " stencil { 0 setgray imagemask } { image } ifelse",
801 " sp { showpage } if",
807 buffer[MaxTextExtent],
810 page_geometry[MaxTextExtent];
870 Open output image file.
872 assert(image_info != (const ImageInfo *) NULL);
873 assert(image_info->signature == MagickSignature);
874 assert(image != (Image *) NULL);
875 assert(image->signature == MagickSignature);
876 if (image->debug != MagickFalse)
877 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
878 assert(exception != (ExceptionInfo *) NULL);
879 assert(exception->signature == MagickSignature);
880 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
881 if (status == MagickFalse)
883 compression=image->compression;
884 if (image_info->compression != UndefinedCompression)
885 compression=image_info->compression;
889 case Group4Compression:
891 if ((IsImageMonochrome(image,exception) == MagickFalse) ||
892 (image->matte != MagickFalse))
893 compression=RLECompression;
896 #if !defined(MAGICKCORE_JPEG_DELEGATE)
897 case JPEGCompression:
899 compression=RLECompression;
900 (void) ThrowMagickException(exception,GetMagickModule(),
901 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
906 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
909 compression=RLECompression;
910 (void) ThrowMagickException(exception,GetMagickModule(),
911 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZLIB)",
919 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
925 Scale relative to dots-per-inch.
927 delta.x=DefaultResolution;
928 delta.y=DefaultResolution;
929 resolution.x=image->resolution.x;
930 resolution.y=image->resolution.y;
931 if ((resolution.x == 0.0) || (resolution.y == 0.0))
933 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
934 resolution.x=geometry_info.rho;
935 resolution.y=geometry_info.sigma;
936 if ((flags & SigmaValue) == 0)
937 resolution.y=resolution.x;
939 if (image_info->density != (char *) NULL)
941 flags=ParseGeometry(image_info->density,&geometry_info);
942 resolution.x=geometry_info.rho;
943 resolution.y=geometry_info.sigma;
944 if ((flags & SigmaValue) == 0)
945 resolution.y=resolution.x;
947 if (image->units == PixelsPerCentimeterResolution)
949 resolution.x=(size_t) (100.0*2.54*resolution.x+0.5)/100.0;
950 resolution.y=(size_t) (100.0*2.54*resolution.y+0.5)/100.0;
952 SetGeometry(image,&geometry);
953 (void) FormatLocaleString(page_geometry,MaxTextExtent,"%.20gx%.20g",
954 (double) image->columns,(double) image->rows);
955 if (image_info->page != (char *) NULL)
956 (void) CopyMagickString(page_geometry,image_info->page,MaxTextExtent);
958 if ((image->page.width != 0) && (image->page.height != 0))
959 (void) FormatLocaleString(page_geometry,MaxTextExtent,
960 "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,(double)
961 image->page.height,(double) image->page.x,(double) image->page.y);
963 if ((image->gravity != UndefinedGravity) &&
964 (LocaleCompare(image_info->magick,"PS") == 0))
965 (void) CopyMagickString(page_geometry,PSPageGeometry,MaxTextExtent);
966 (void) ConcatenateMagickString(page_geometry,">",MaxTextExtent);
967 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
968 &geometry.width,&geometry.height);
969 scale.x=(double) (geometry.width*delta.x)/resolution.x;
970 geometry.width=(size_t) floor(scale.x+0.5);
971 scale.y=(double) (geometry.height*delta.y)/resolution.y;
972 geometry.height=(size_t) floor(scale.y+0.5);
973 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
974 (void) ParseGravityGeometry(image,page_geometry,&page_info,exception);
975 if (image->gravity != UndefinedGravity)
977 geometry.x=(-page_info.x);
978 geometry.y=(ssize_t) (media_info.height+page_info.y-image->rows);
981 if (image_info->pointsize != 0.0)
982 pointsize=image_info->pointsize;
984 value=GetImageProperty(image,"label",exception);
985 if (value != (const char *) NULL)
986 text_size=(size_t) (MultilineCensus(value)*pointsize+12);
991 Postscript header on the first page.
993 if (LocaleCompare(image_info->magick,"PS3") == 0)
994 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0\n",MaxTextExtent);
996 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0 EPSF-3.0\n",
998 (void) WriteBlobString(image,buffer);
999 (void) FormatLocaleString(buffer,MaxTextExtent,
1000 "%%%%Creator: ImageMagick %s\n",MagickLibVersionText);
1001 (void) WriteBlobString(image,buffer);
1002 (void) FormatLocaleString(buffer,MaxTextExtent,"%%%%Title: %s\n",
1004 (void) WriteBlobString(image,buffer);
1005 timer=time((time_t *) NULL);
1006 (void) FormatMagickTime(timer,MaxTextExtent,date);
1007 (void) FormatLocaleString(buffer,MaxTextExtent,
1008 "%%%%CreationDate: %s\n",date);
1009 (void) WriteBlobString(image,buffer);
1010 bounds.x1=(double) geometry.x;
1011 bounds.y1=(double) geometry.y;
1012 bounds.x2=(double) geometry.x+scale.x;
1013 bounds.y2=(double) geometry.y+scale.y+text_size;
1014 if ((image_info->adjoin != MagickFalse) &&
1015 (GetNextImageInList(image) != (Image *) NULL))
1017 (void) WriteBlobString(image,"%%BoundingBox: (atend)\n");
1018 (void) WriteBlobString(image,"%%HiResBoundingBox: (atend)\n");
1022 (void) FormatLocaleString(buffer,MaxTextExtent,
1023 "%%%%BoundingBox: %g %g %g %g\n",floor(bounds.x1+0.5),
1024 floor(bounds.y1+0.5),ceil(bounds.x2-0.5),ceil(bounds.y2-0.5));
1025 (void) WriteBlobString(image,buffer);
1026 (void) FormatLocaleString(buffer,MaxTextExtent,
1027 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,
1028 bounds.y1,bounds.x2,bounds.y2);
1029 (void) WriteBlobString(image,buffer);
1030 if (image->colorspace == CMYKColorspace)
1031 (void) WriteBlobString(image,
1032 "%%DocumentProcessColors: Cyan Magenta Yellow Black\n");
1034 if (IsImageGray(image,exception) != MagickFalse)
1035 (void) WriteBlobString(image,
1036 "%%DocumentProcessColors: Black\n");
1041 value=GetImageProperty(image,"label",exception);
1042 if (value != (const char *) NULL)
1043 (void) WriteBlobString(image,
1044 "%%DocumentNeededResources: font Helvetica\n");
1045 (void) WriteBlobString(image,"%%LanguageLevel: 3\n");
1047 Pages, orientation and order.
1049 if (LocaleCompare(image_info->magick,"PS3") != 0)
1050 (void) WriteBlobString(image,"%%Pages: 1\n");
1053 (void) WriteBlobString(image,"%%Orientation: Portrait\n");
1054 (void) WriteBlobString(image,"%%PageOrder: Ascend\n");
1055 if (image_info->adjoin == MagickFalse)
1056 (void) CopyMagickString(buffer,"%%Pages: 1\n",MaxTextExtent);
1058 (void) FormatLocaleString(buffer,MaxTextExtent,
1059 "%%%%Pages: %.20g\n",(double) GetImageListLength(image));
1060 (void) WriteBlobString(image,buffer);
1062 (void) WriteBlobString(image,"%%EndComments\n");
1064 The static postscript procedures prolog.
1066 (void)WriteBlobString(image,"%%BeginProlog\n");
1067 for (q=PostscriptProlog; *q; q++)
1069 (void) WriteBlobString(image,*q);
1070 (void) WriteBlobByte(image,'\n');
1073 One label line for each line in label string.
1075 value=GetImageProperty(image,"label",exception);
1076 if (value != (const char *) NULL)
1078 (void) WriteBlobString(image,"\n %% Labels.\n /Helvetica "
1079 " findfont pointsize scalefont setfont\n");
1080 for (i=(ssize_t) MultilineCensus(value)-1; i >= 0; i--)
1082 (void) WriteBlobString(image,
1083 " currentfile buffer readline pop token pop\n");
1084 (void) FormatLocaleString(buffer,MaxTextExtent,
1085 " 0 y %g add moveto show pop\n",i*pointsize+12);
1086 (void) WriteBlobString(image,buffer);
1090 The static postscript procedures epilog.
1092 for (q=PostscriptEpilog; *q; q++)
1094 (void) WriteBlobString(image,*q);
1095 (void) WriteBlobByte(image,'\n');
1097 (void)WriteBlobString(image,"%%EndProlog\n");
1099 (void) FormatLocaleString(buffer,MaxTextExtent,"%%%%Page: 1 %.20g\n",
1101 (void) WriteBlobString(image,buffer);
1105 (void) FormatLocaleString(buffer,MaxTextExtent,
1106 "%%%%PageBoundingBox: %.20g %.20g %.20g %.20g\n",(double) geometry.x,
1107 (double) geometry.y,geometry.x+(double) geometry.width,geometry.y+
1108 (double) (geometry.height+text_size));
1109 (void) WriteBlobString(image,buffer);
1111 Page process colors if not RGB.
1113 if (image->colorspace == CMYKColorspace)
1114 (void) WriteBlobString(image,
1115 "%%PageProcessColors: Cyan Magenta Yellow Black\n");
1117 if (IsImageGray(image,exception) != MagickFalse)
1118 (void) WriteBlobString(image,"%%PageProcessColors: Black\n");
1120 Adjust document bounding box to bound page bounding box.
1122 if ((double) geometry.x < bounds.x1)
1123 bounds.x1=(double) geometry.x;
1124 if ((double) geometry.y < bounds.y1)
1125 bounds.y1=(double) geometry.y;
1126 if ((double) (geometry.x+scale.x) > bounds.x2)
1127 bounds.x2=(double) geometry.x+scale.x;
1128 if ((double) (geometry.y+scale.y+text_size) > bounds.y2)
1129 bounds.y2=(double) geometry.y+scale.y+text_size;
1131 Page font resource if there's a label.
1133 value=GetImageProperty(image,"label",exception);
1134 if (value != (const char *) NULL)
1135 (void) WriteBlobString(image,"%%PageResources: font Helvetica\n");
1137 PS clipping path from Photoshop clipping path.
1139 if ((image->clip_mask == (Image *) NULL) ||
1140 (LocaleNCompare("8BIM:",image->clip_mask->magick_filename,5) != 0))
1141 (void) WriteBlobString(image,"/ClipImage {} def\n");
1147 value=GetImageProperty(image,image->clip_mask->magick_filename,
1149 if (value == (const char *) NULL)
1150 return(MagickFalse);
1151 (void) WriteBlobString(image,value);
1152 (void) WriteBlobByte(image,'\n');
1155 Push a dictionary for our own def's if this an EPS.
1157 if (LocaleCompare(image_info->magick,"PS3") != 0)
1158 (void) WriteBlobString(image,"userdict begin\n");
1162 if ((image->matte != MagickFalse) &&
1163 (WritePS3MaskImage(image_info,image,compression,exception) == MagickFalse))
1165 (void) CloseBlob(image);
1166 return(MagickFalse);
1169 Remember position of BeginData comment so we can update it.
1171 start=TellBlob(image);
1172 (void) FormatLocaleString(buffer,MaxTextExtent,
1173 "%%%%BeginData:%13ld %s Bytes\n",0L,
1174 compression == NoCompression ? "ASCII" : "BINARY");
1175 (void) WriteBlobString(image,buffer);
1176 stop=TellBlob(image);
1177 (void) WriteBlobString(image,"DisplayImage\n");
1179 Translate, scale, and font point size.
1181 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n%g %g\n%g\n",
1182 (double) geometry.x,(double) geometry.y,scale.x,scale.y,pointsize);
1183 (void) WriteBlobString(image,buffer);
1187 labels=(char **) NULL;
1188 value=GetImageProperty(image,"label",exception);
1189 if (value != (const char *) NULL)
1190 labels=StringToList(value);
1191 if (labels != (char **) NULL)
1193 for (i=0; labels[i] != (char *) NULL; i++)
1195 if (compression != NoCompression)
1197 for (j=0; labels[i][j] != '\0'; j++)
1198 (void) WriteBlobByte(image,(unsigned char) labels[i][j]);
1199 (void) WriteBlobByte(image,'\n');
1203 (void) WriteBlobString(image,"<~");
1204 Ascii85Initialize(image);
1205 for (j=0; labels[i][j] != '\0'; j++)
1206 Ascii85Encode(image,(unsigned char) labels[i][j]);
1207 Ascii85Flush(image);
1209 labels[i]=DestroyString(labels[i]);
1211 labels=(char **) RelinquishMagickMemory(labels);
1214 Photoshop clipping path active?
1216 if ((image->clip_mask != (Image *) NULL) &&
1217 (LocaleNCompare("8BIM:",image->clip_mask->magick_filename,5) == 0))
1218 (void) WriteBlobString(image,"true\n");
1220 (void) WriteBlobString(image,"false\n");
1222 Showpage for non-EPS.
1224 (void) WriteBlobString(image, LocaleCompare(image_info->magick,"PS3") == 0 ?
1225 "true\n" : "false\n");
1227 Image columns, rows, and color space.
1229 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n%s\n",
1230 (double) image->columns,(double) image->rows,image->colorspace ==
1231 CMYKColorspace ? PS3_CMYKColorspace : PS3_RGBColorspace);
1232 (void) WriteBlobString(image,buffer);
1236 (void) WriteBlobString(image,image->matte != MagickFalse ?
1237 "true\n" : "false\n");
1239 Render with imagemask operator?
1241 option=GetImageOption(image_info,"ps3:imagemask");
1242 (void) WriteBlobString(image,((option != (const char *) NULL) &&
1243 (IsImageMonochrome(image,exception) != MagickFalse)) ?
1244 "true\n" : "false\n");
1248 pixels=(unsigned char *) NULL;
1250 if ((image_info->type != TrueColorType) &&
1251 (image_info->type != TrueColorMatteType) &&
1252 (image_info->type != ColorSeparationType) &&
1253 (image_info->type != ColorSeparationMatteType) &&
1254 (image->colorspace != CMYKColorspace) &&
1255 ((IsImageGray(image,exception) != MagickFalse) ||
1256 (IsImageMonochrome(image,exception) != MagickFalse)))
1261 (void) WriteBlobString(image,PS3_PseudoClass"\n");
1262 switch (compression)
1267 (void) WriteBlobString(image,PS3_NoCompression"\n");
1270 case FaxCompression:
1271 case Group4Compression:
1273 (void) WriteBlobString(image,PS3_FaxCompression"\n");
1276 case JPEGCompression:
1278 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1281 case LZWCompression:
1283 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1286 case RLECompression:
1288 (void) WriteBlobString(image,PS3_RLECompression"\n");
1291 case ZipCompression:
1293 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1298 Number of colors -- 0 for single component non-color mapped data.
1300 (void) WriteBlobString(image,"0\n");
1302 1 bit or 8 bit components?
1304 (void) FormatLocaleString(buffer,MaxTextExtent,"%d\n",
1305 IsImageMonochrome(image,exception) != MagickFalse ? 1 : 8);
1306 (void) WriteBlobString(image,buffer);
1310 if (compression == JPEGCompression)
1311 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
1313 if ((compression == FaxCompression) ||
1314 (compression == Group4Compression))
1316 if (LocaleCompare(CCITTParam,"0") == 0)
1317 status=HuffmanEncodeImage(image_info,image,image,exception);
1319 status=Huffman2DEncodeImage(image_info,image,image,exception);
1323 status=SerializeImageChannel(image_info,image,&pixels,&length,
1325 if (status == MagickFalse)
1327 (void) CloseBlob(image);
1328 return(MagickFalse);
1330 switch (compression)
1335 Ascii85Initialize(image);
1336 for (i=0; i < (ssize_t) length; i++)
1337 Ascii85Encode(image,pixels[i]);
1338 Ascii85Flush(image);
1342 case LZWCompression:
1344 status=LZWEncodeImage(image,length,pixels,exception);
1347 case RLECompression:
1349 status=PackbitsEncodeImage(image,length,pixels,exception);
1352 case ZipCompression:
1354 status=ZLIBEncodeImage(image,length,pixels,exception);
1358 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1362 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1363 (compression == JPEGCompression))
1368 (void) WriteBlobString(image,PS3_DirectClass"\n");
1369 switch (compression)
1374 (void) WriteBlobString(image,PS3_NoCompression"\n");
1377 case RLECompression:
1379 (void) WriteBlobString(image,PS3_RLECompression"\n");
1382 case JPEGCompression:
1384 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1387 case LZWCompression:
1389 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1392 case ZipCompression:
1394 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1401 if (compression == JPEGCompression)
1402 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
1406 Stream based compressions.
1408 status=SerializeImage(image_info,image,&pixels,&length,exception);
1409 if (status == MagickFalse)
1411 (void) CloseBlob(image);
1412 return(MagickFalse);
1414 switch (compression)
1419 Ascii85Initialize(image);
1420 for (i=0; i < (ssize_t) length; i++)
1421 Ascii85Encode(image,pixels[i]);
1422 Ascii85Flush(image);
1426 case RLECompression:
1428 status=PackbitsEncodeImage(image,length,pixels,exception);
1431 case LZWCompression:
1433 status=LZWEncodeImage(image,length,pixels,exception);
1436 case ZipCompression:
1438 status=ZLIBEncodeImage(image,length,pixels,exception);
1442 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1450 (void) WriteBlobString(image,PS3_PseudoClass"\n");
1451 switch (compression)
1456 (void) WriteBlobString(image,PS3_NoCompression"\n");
1459 case JPEGCompression:
1461 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1464 case RLECompression:
1466 (void) WriteBlobString(image,PS3_RLECompression"\n");
1469 case LZWCompression:
1471 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1474 case ZipCompression:
1476 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1481 Number of colors in color map.
1483 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",
1484 (double) image->colors);
1485 (void) WriteBlobString(image,buffer);
1487 Color map - uncompressed.
1489 if ((compression != NoCompression) &&
1490 (compression != UndefinedCompression))
1492 for (i=0; i < (ssize_t) image->colors; i++)
1494 pixel=ScaleQuantumToChar(image->colormap[i].red);
1495 (void) WriteBlobByte(image,(unsigned char) pixel);
1496 pixel=ScaleQuantumToChar(image->colormap[i].green);
1497 (void) WriteBlobByte(image,(unsigned char) pixel);
1498 pixel=ScaleQuantumToChar(image->colormap[i].blue);
1499 (void) WriteBlobByte(image,(unsigned char) pixel);
1504 Ascii85Initialize(image);
1505 for (i=0; i < (ssize_t) image->colors; i++)
1507 pixel=ScaleQuantumToChar(image->colormap[i].red);
1508 Ascii85Encode(image,(unsigned char) pixel);
1509 pixel=ScaleQuantumToChar(image->colormap[i].green);
1510 Ascii85Encode(image,(unsigned char) pixel);
1511 pixel=ScaleQuantumToChar(image->colormap[i].blue);
1512 Ascii85Encode(image,(unsigned char) pixel);
1514 Ascii85Flush(image);
1516 status=SerializeImageIndexes(image_info,image,&pixels,&length,
1518 if (status == MagickFalse)
1520 (void) CloseBlob(image);
1521 return(MagickFalse);
1523 switch (compression)
1528 Ascii85Initialize(image);
1529 for (i=0; i < (ssize_t) length; i++)
1530 Ascii85Encode(image,pixels[i]);
1531 Ascii85Flush(image);
1535 case JPEGCompression:
1537 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
1540 case RLECompression:
1542 status=PackbitsEncodeImage(image,length,pixels,exception);
1545 case LZWCompression:
1547 status=LZWEncodeImage(image,length,pixels,exception);
1550 case ZipCompression:
1552 status=ZLIBEncodeImage(image,length,pixels,exception);
1556 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1558 (void) WriteBlobByte(image,'\n');
1559 if (status == MagickFalse)
1561 (void) CloseBlob(image);
1562 return(MagickFalse);
1565 Update BeginData now that we know the data size.
1567 length=(size_t) (TellBlob(image)-stop);
1568 stop=TellBlob(image);
1569 offset=SeekBlob(image,start,SEEK_SET);
1571 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
1572 (void) FormatLocaleString(buffer,MaxTextExtent,
1573 "%%%%BeginData:%13ld %s Bytes\n",(long) length,
1574 compression == NoCompression ? "ASCII" : "BINARY");
1575 (void) WriteBlobString(image,buffer);
1576 offset=SeekBlob(image,stop,SEEK_SET);
1577 (void) WriteBlobString(image,"%%EndData\n");
1579 End private dictionary if this an EPS.
1581 if (LocaleCompare(image_info->magick,"PS3") != 0)
1582 (void) WriteBlobString(image,"end\n");
1583 (void) WriteBlobString(image,"%%PageTrailer\n");
1584 if (GetNextImageInList(image) == (Image *) NULL)
1586 image=SyncNextImageInList(image);
1587 status=SetImageProgress(image,SaveImagesTag,scene++,
1588 GetImageListLength(image));
1589 if (status == MagickFalse)
1591 } while (image_info->adjoin != MagickFalse);
1592 (void) WriteBlobString(image,"%%Trailer\n");
1595 (void) FormatLocaleString(buffer,MaxTextExtent,
1596 "%%%%BoundingBox: %g %g %g %g\n",floor(bounds.x1+0.5),
1597 floor(bounds.y1+0.5),ceil(bounds.x2-0.5),ceil(bounds.y2-0.5));
1598 (void) WriteBlobString(image,buffer);
1599 (void) FormatLocaleString(buffer,MaxTextExtent,
1600 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,bounds.y1,
1601 bounds.x2,bounds.y2);
1602 (void) WriteBlobString(image,buffer);
1604 (void) WriteBlobString(image,"%%EOF\n");
1605 (void) CloseBlob(image);