2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Write Postscript Level III Format %
21 % Copyright 1999-2018 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 % https://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/artifact.h"
45 #include "MagickCore/attribute.h"
46 #include "MagickCore/blob.h"
47 #include "MagickCore/blob-private.h"
48 #include "MagickCore/cache.h"
49 #include "MagickCore/channel.h"
50 #include "MagickCore/color.h"
51 #include "MagickCore/color-private.h"
52 #include "MagickCore/compress.h"
53 #include "MagickCore/constitute.h"
54 #include "MagickCore/draw.h"
55 #include "MagickCore/exception.h"
56 #include "MagickCore/exception-private.h"
57 #include "MagickCore/geometry.h"
58 #include "MagickCore/image.h"
59 #include "MagickCore/image-private.h"
60 #include "MagickCore/list.h"
61 #include "MagickCore/magick.h"
62 #include "MagickCore/memory_.h"
63 #include "MagickCore/monitor.h"
64 #include "MagickCore/monitor-private.h"
65 #include "MagickCore/option.h"
66 #include "MagickCore/pixel-accessor.h"
67 #include "MagickCore/property.h"
68 #include "MagickCore/quantum-private.h"
69 #include "MagickCore/resource_.h"
70 #include "MagickCore/static.h"
71 #include "MagickCore/string_.h"
72 #include "MagickCore/module.h"
73 #include "MagickCore/token.h"
74 #include "MagickCore/utility.h"
75 #include "MagickCore/module.h"
80 #define PS3_NoCompression "0"
81 #define PS3_FaxCompression "1"
82 #define PS3_JPEGCompression "2"
83 #define PS3_LZWCompression "3"
84 #define PS3_RLECompression "4"
85 #define PS3_ZipCompression "5"
87 #define PS3_RGBColorspace "0"
88 #define PS3_CMYKColorspace "1"
90 #define PS3_DirectClass "0"
91 #define PS3_PseudoClass "1"
93 #if defined(MAGICKCORE_TIFF_DELEGATE)
94 #define CCITTParam "-1"
96 #define CCITTParam "0"
100 Forward declarations.
102 static MagickBooleanType
103 WritePS3Image(const ImageInfo *,Image *,ExceptionInfo *);
106 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110 % R e g i s t e r P S 3 I m a g e %
114 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116 % RegisterPS3Image() adds properties for the PS3 image format to the list of
117 % supported formats. The properties include the image format tag, a method to
118 % read and/or write the format, whether the format supports the saving of more
119 % than one frame to the same file or blob, whether the format supports native
120 % in-memory I/O, and a brief description of the format.
122 % The format of the RegisterPS3Image method is:
124 % size_t RegisterPS3Image(void)
127 ModuleExport size_t RegisterPS3Image(void)
132 entry=AcquireMagickInfo("PS3","EPS3","Level III Encapsulated PostScript");
133 entry->encoder=(EncodeImageHandler *) WritePS3Image;
134 entry->mime_type=ConstantString("application/postscript");
135 entry->flags|=CoderEncoderSeekableStreamFlag;
136 entry->flags^=CoderBlobSupportFlag;
137 (void) RegisterMagickInfo(entry);
138 entry=AcquireMagickInfo("PS3","PS3","Level III PostScript");
139 entry->encoder=(EncodeImageHandler *) WritePS3Image;
140 entry->mime_type=ConstantString("application/postscript");
141 entry->flags|=CoderEncoderSeekableStreamFlag;
142 entry->flags^=CoderBlobSupportFlag;
143 (void) RegisterMagickInfo(entry);
144 return(MagickImageCoderSignature);
148 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
152 % U n r e g i s t e r P S 3 I m a g e %
156 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
158 % UnregisterPS3Image() removes format registrations made by the PS3 module
159 % from the list of supported formats.
161 % The format of the UnregisterPS3Image method is:
163 % UnregisterPS3Image(void)
166 ModuleExport void UnregisterPS3Image(void)
168 (void) UnregisterMagickInfo("EPS3");
169 (void) UnregisterMagickInfo("PS3");
173 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
177 % W r i t e P S 3 I m a g e %
181 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183 % WritePS3Image() translates an image to encapsulated Postscript Level III
184 % for printing. If the supplied geometry is null, the image is centered on
185 % the Postscript page. Otherwise, the image is positioned as specified by the
188 % The format of the WritePS3Image method is:
190 % MagickBooleanType WritePS3Image(const ImageInfo *image_info,
191 % Image *image,ExceptionInfo *exception)
193 % A description of each parameter follows:
195 % o image_info: Specifies a pointer to a ImageInfo structure.
197 % o image: the image.
199 % o exception: return any errors or warnings in this structure.
203 static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
204 Image *image,Image *inject_image,ExceptionInfo *exception)
222 write_info=CloneImageInfo(image_info);
223 (void) CopyMagickString(write_info->filename,"GROUP4:",MagickPathExtent);
224 (void) CopyMagickString(write_info->magick,"GROUP4",MagickPathExtent);
225 group4_image=CloneImage(inject_image,0,0,MagickTrue,exception);
226 if (group4_image == (Image *) NULL)
228 group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
230 group4_image=DestroyImage(group4_image);
231 if (group4 == (unsigned char *) NULL)
233 write_info=DestroyImageInfo(write_info);
234 if (WriteBlob(image,length,group4) != (ssize_t) length)
236 group4=(unsigned char *) RelinquishMagickMemory(group4);
240 static MagickBooleanType SerializeImage(const ImageInfo *image_info,
241 Image *image,MemoryInfo **pixel_info,size_t *length,ExceptionInfo *exception)
246 register const Quantum
252 register unsigned char
258 assert(image != (Image *) NULL);
259 assert(image->signature == MagickCoreSignature);
260 if (image->debug != MagickFalse)
261 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
263 *length=(image->colorspace == CMYKColorspace ? 4 : 3)*(size_t)
264 image->columns*image->rows;
265 *pixel_info=AcquireVirtualMemory(*length,sizeof(*q));
266 if (*pixel_info == (MemoryInfo *) NULL)
267 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
268 q=(unsigned char *) GetVirtualMemoryBlob(*pixel_info);
269 for (y=0; y < (ssize_t) image->rows; y++)
271 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
272 if (p == (const Quantum *) NULL)
274 if (image->colorspace != CMYKColorspace)
275 for (x=0; x < (ssize_t) image->columns; x++)
277 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
278 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
279 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
280 p+=GetPixelChannels(image);
283 for (x=0; x < (ssize_t) image->columns; x++)
285 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
286 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
287 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
288 *q++=ScaleQuantumToChar(GetPixelBlack(image,p));
289 p+=GetPixelChannels(image);
291 if (image->previous == (Image *) NULL)
293 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
295 if (status == MagickFalse)
299 if (status == MagickFalse)
300 *pixel_info=RelinquishVirtualMemory(*pixel_info);
304 static MagickBooleanType SerializeImageChannel(const ImageInfo *image_info,
305 Image *image,MemoryInfo **pixel_info,size_t *length,ExceptionInfo *exception)
310 register const Quantum
316 register unsigned char
330 assert(image != (Image *) NULL);
331 assert(image->signature == MagickCoreSignature);
332 if (image->debug != MagickFalse)
333 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
335 pack=SetImageMonochrome(image,exception) == MagickFalse ? 1UL : 8UL;
336 padded_columns=((image->columns+pack-1)/pack)*pack;
337 *length=(size_t) padded_columns*image->rows/pack;
338 *pixel_info=AcquireVirtualMemory(*length,sizeof(*q));
339 if (*pixel_info == (MemoryInfo *) NULL)
340 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
341 q=(unsigned char *) GetVirtualMemoryBlob(*pixel_info);
342 for (y=0; y < (ssize_t) image->rows; y++)
344 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
345 if (p == (const Quantum *) NULL)
348 for (x=0; x < (ssize_t) image->columns; x++)
350 *q++=ScaleQuantumToChar(ClampToQuantum(GetPixelLuma(image,p)));
351 p+=GetPixelChannels(image);
356 for (x=0; x < (ssize_t) padded_columns; x++)
358 bit=(unsigned char) 0x00;
359 if (x < (ssize_t) image->columns)
360 bit=(unsigned char) (GetPixelLuma(image,p) == TransparentAlpha ?
362 code=(code << 1)+bit;
363 if (((x+1) % pack) == 0)
368 p+=GetPixelChannels(image);
371 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
373 if (status == MagickFalse)
376 if (status == MagickFalse)
377 *pixel_info=RelinquishVirtualMemory(*pixel_info);
381 static MagickBooleanType SerializeImageIndexes(const ImageInfo *image_info,
382 Image *image,MemoryInfo **pixel_info,size_t *length,ExceptionInfo *exception)
387 register const Quantum
393 register unsigned char
399 assert(image != (Image *) NULL);
400 assert(image->signature == MagickCoreSignature);
401 if (image->debug != MagickFalse)
402 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
404 *length=(size_t) image->columns*image->rows;
405 *pixel_info=AcquireVirtualMemory(*length,sizeof(*q));
406 if (*pixel_info == (MemoryInfo *) NULL)
407 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
408 q=(unsigned char *) GetVirtualMemoryBlob(*pixel_info);
409 for (y=0; y < (ssize_t) image->rows; y++)
411 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
412 if (p == (const Quantum *) NULL)
414 for (x=0; x < (ssize_t) image->columns; x++)
416 *q++=(unsigned char) GetPixelIndex(image,p);
417 p+=GetPixelChannels(image);
419 if (image->previous == (Image *) NULL)
421 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
423 if (status == MagickFalse)
427 if (status == MagickFalse)
428 *pixel_info=RelinquishVirtualMemory(*pixel_info);
432 static MagickBooleanType WritePS3MaskImage(const ImageInfo *image_info,
433 Image *image,const CompressionType compression,ExceptionInfo *exception)
436 buffer[MagickPathExtent];
461 assert(image_info != (ImageInfo *) NULL);
462 assert(image_info->signature == MagickCoreSignature);
463 assert(image != (Image *) NULL);
464 assert(image->signature == MagickCoreSignature);
465 if (image->debug != MagickFalse)
466 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
467 assert(image->alpha_trait != UndefinedPixelTrait);
470 Note BeginData DSC comment for update later.
472 start=TellBlob(image);
474 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
475 (void) FormatLocaleString(buffer,MagickPathExtent,
476 "%%%%BeginData:%13ld %s Bytes\n",0L,compression == NoCompression ?
478 (void) WriteBlobString(image,buffer);
479 stop=TellBlob(image);
481 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
483 Only lossless compressions for the mask.
490 (void) FormatLocaleString(buffer,MagickPathExtent,
491 "currentfile %.20g %.20g " PS3_NoCompression
492 " ByteStreamDecodeFilter\n",(double) image->columns,(double)
497 case Group4Compression:
499 (void) FormatLocaleString(buffer,MagickPathExtent,
500 "currentfile %.20g %.20g " PS3_FaxCompression
501 " ByteStreamDecodeFilter\n",(double) image->columns,(double)
507 (void) FormatLocaleString(buffer,MagickPathExtent,
508 "currentfile %.20g %.20g " PS3_LZWCompression
509 " ByteStreamDecodeFilter\n",(double) image->columns,(double)
515 (void) FormatLocaleString(buffer,MagickPathExtent,
516 "currentfile %.20g %.20g " PS3_RLECompression
517 " ByteStreamDecodeFilter\n",(double) image->columns,(double)
523 (void) FormatLocaleString(buffer,MagickPathExtent,
524 "currentfile %.20g %.20g " PS3_ZipCompression
525 " ByteStreamDecodeFilter\n",(double) image->columns,(double)
530 (void) WriteBlobString(image,buffer);
531 (void) WriteBlobString(image,"/ReusableStreamDecode filter\n");
532 mask_image=SeparateImage(image,AlphaChannel,exception);
533 if (mask_image == (Image *) NULL)
534 ThrowWriterException(CoderError,exception->reason);
535 (void) SetImageType(mask_image,BilevelType,exception);
536 (void) SetImageType(mask_image,PaletteType,exception);
537 mask_image->alpha_trait=UndefinedPixelTrait;
538 pixels=(unsigned char *) NULL;
545 status=SerializeImageChannel(image_info,mask_image,&pixel_info,&length,
547 if (status == MagickFalse)
549 Ascii85Initialize(image);
550 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
551 for (i=0; i < (ssize_t) length; i++)
552 Ascii85Encode(image,pixels[i]);
554 pixel_info=RelinquishVirtualMemory(pixel_info);
558 case Group4Compression:
560 if ((compression == FaxCompression) ||
561 (LocaleCompare(CCITTParam,"0") == 0))
562 status=HuffmanEncodeImage(image_info,image,mask_image,exception);
564 status=Huffman2DEncodeImage(image_info,image,mask_image,exception);
569 status=SerializeImageChannel(image_info,mask_image,&pixel_info,&length,
571 if (status == MagickFalse)
573 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
574 status=LZWEncodeImage(image,length,pixels,exception);
575 pixel_info=RelinquishVirtualMemory(pixel_info);
580 status=SerializeImageChannel(image_info,mask_image,&pixel_info,&length,
582 if (status == MagickFalse)
584 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
585 status=PackbitsEncodeImage(image,length,pixels,exception);
586 pixel_info=RelinquishVirtualMemory(pixel_info);
591 status=SerializeImageChannel(image_info,mask_image,&pixel_info,&length,
593 if (status == MagickFalse)
595 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
596 status=ZLIBEncodeImage(image,length,pixels,exception);
597 pixel_info=RelinquishVirtualMemory(pixel_info);
601 mask_image=DestroyImage(mask_image);
602 (void) WriteBlobByte(image,'\n');
603 length=(size_t) (TellBlob(image)-stop);
604 stop=TellBlob(image);
606 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
607 offset=SeekBlob(image,start,SEEK_SET);
609 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
610 (void) FormatLocaleString(buffer,MagickPathExtent,
611 "%%%%BeginData:%13ld %s Bytes\n",(long) length,
612 compression == NoCompression ? "ASCII" : "BINARY");
613 (void) WriteBlobString(image,buffer);
614 offset=SeekBlob(image,stop,SEEK_SET);
616 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
617 (void) WriteBlobString(image,"%%EndData\n");
618 (void) WriteBlobString(image, "/mask_stream exch def\n");
622 static MagickBooleanType WritePS3Image(const ImageInfo *image_info,Image *image,
623 ExceptionInfo *exception)
626 *const PostscriptProlog[]=
628 "/ByteStreamDecodeFilter",
633 " z " PS3_NoCompression " eq { /ASCII85Decode filter } if",
634 " z " PS3_FaxCompression " eq",
641 " /CCITTFaxDecode filter",
643 " z " PS3_JPEGCompression " eq { /DCTDecode filter } if",
644 " z " PS3_LZWCompression " eq { /LZWDecode filter } if",
645 " z " PS3_RLECompression " eq { /RunLengthDecode filter } if",
646 " z " PS3_ZipCompression " eq { /FlateDecode filter } if",
649 "/DirectClassImageDict",
651 " colorspace " PS3_RGBColorspace " eq",
653 " /DeviceRGB setcolorspace",
658 " /BitsPerComponent 8",
659 " /DataSource pixel_stream",
660 " /MultipleDataSources false",
661 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
662 " /Decode [0 1 0 1 0 1]",
666 " /DeviceCMYK setcolorspace",
671 " /BitsPerComponent 8",
672 " /DataSource pixel_stream",
673 " /MultipleDataSources false",
674 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
676 " compression " PS3_JPEGCompression " eq",
677 " { [1 0 1 0 1 0 1 0] }",
678 " { [0 1 0 1 0 1 0 1] }",
685 "/PseudoClassImageDict",
687 " % Colors in colormap image.",
688 " currentfile buffer readline pop",
689 " token pop /colors exch def pop",
692 " % Depth of grayscale image.",
693 " currentfile buffer readline pop",
694 " token pop /bits exch def pop",
695 " /DeviceGray setcolorspace",
700 " /BitsPerComponent bits",
702 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
703 " /DataSource pixel_stream",
708 " /colormap colors 3 mul string def",
709 " compression " PS3_NoCompression " eq",
710 " { currentfile /ASCII85Decode filter colormap readstring pop pop }",
711 " { currentfile colormap readstring pop pop }",
713 " [ /Indexed /DeviceRGB colors 1 sub colormap ] setcolorspace",
718 " /BitsPerComponent 8",
720 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
721 " /DataSource pixel_stream",
727 "/NonMaskedImageDict",
729 " class " PS3_PseudoClass " eq",
730 " { PseudoClassImageDict }",
731 " { DirectClassImageDict }",
739 " /InterleaveType 3",
740 " /DataDict NonMaskedImageDict",
746 " /BitsPerComponent 1",
747 " /DataSource mask_stream",
748 " /MultipleDataSources false",
749 " /ImageMatrix [ columns 0 0 rows neg 0 rows]",
761 " /buffer 512 string def",
763 " currentfile buffer readline pop",
764 " token pop /x exch def",
765 " token pop /y exch def pop",
767 " % Image size and font size.",
768 " currentfile buffer readline pop",
769 " token pop /x exch def",
770 " token pop /y exch def pop",
771 " currentfile buffer readline pop",
772 " token pop /pointsize exch def pop",
775 *const PostscriptEpilog[]=
779 " currentfile buffer readline pop",
780 " token pop /clipped exch def pop",
782 " currentfile buffer readline pop",
783 " token pop /sp exch def pop",
784 " % Image pixel size.",
785 " currentfile buffer readline pop",
786 " token pop /columns exch def",
787 " token pop /rows exch def pop",
788 " % Colorspace (RGB/CMYK).",
789 " currentfile buffer readline pop",
790 " token pop /colorspace exch def pop",
792 " currentfile buffer readline pop",
793 " token pop /alpha exch def pop",
795 " currentfile buffer readline pop",
796 " token pop /stencil exch def pop",
797 " % Image class (direct/pseudo).",
798 " currentfile buffer readline pop",
799 " token pop /class exch def pop",
800 " % Compression type.",
801 " currentfile buffer readline pop",
802 " token pop /compression exch def pop",
803 " % Clip and render.",
804 " /pixel_stream currentfile columns rows compression ByteStreamDecodeFilter def",
805 " clipped { ClipImage } if",
806 " alpha stencil not and",
807 " { MaskedImageDict mask_stream resetfile }",
808 " { NonMaskedImageDict }",
810 " stencil { 0 setgray imagemask } { image } ifelse",
812 " sp { showpage } if",
818 buffer[MagickPathExtent],
819 date[MagickPathExtent],
821 page_geometry[MagickPathExtent];
885 Open output image file.
887 assert(image_info != (const ImageInfo *) NULL);
888 assert(image_info->signature == MagickCoreSignature);
889 assert(image != (Image *) NULL);
890 assert(image->signature == MagickCoreSignature);
891 if (image->debug != MagickFalse)
892 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
893 assert(exception != (ExceptionInfo *) NULL);
894 assert(exception->signature == MagickCoreSignature);
895 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
896 if (status == MagickFalse)
898 compression=image->compression;
899 if (image_info->compression != UndefinedCompression)
900 compression=image_info->compression;
904 case Group4Compression:
906 if ((SetImageMonochrome(image,exception) == MagickFalse) ||
907 (image->alpha_trait != UndefinedPixelTrait))
908 compression=RLECompression;
911 #if !defined(MAGICKCORE_JPEG_DELEGATE)
912 case JPEGCompression:
914 compression=RLECompression;
915 (void) ThrowMagickException(exception,GetMagickModule(),
916 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
921 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
924 compression=RLECompression;
925 (void) ThrowMagickException(exception,GetMagickModule(),
926 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZLIB)",
934 (void) memset(&bounds,0,sizeof(bounds));
937 imageListLength=GetImageListLength(image);
941 Scale relative to dots-per-inch.
943 delta.x=DefaultResolution;
944 delta.y=DefaultResolution;
945 resolution.x=image->resolution.x;
946 resolution.y=image->resolution.y;
947 if ((resolution.x == 0.0) || (resolution.y == 0.0))
949 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
950 resolution.x=geometry_info.rho;
951 resolution.y=geometry_info.sigma;
952 if ((flags & SigmaValue) == 0)
953 resolution.y=resolution.x;
955 if (image_info->density != (char *) NULL)
957 flags=ParseGeometry(image_info->density,&geometry_info);
958 resolution.x=geometry_info.rho;
959 resolution.y=geometry_info.sigma;
960 if ((flags & SigmaValue) == 0)
961 resolution.y=resolution.x;
963 if (image->units == PixelsPerCentimeterResolution)
965 resolution.x=(size_t) ((100.0*2.54*resolution.x+0.5)/100.0);
966 resolution.y=(size_t) ((100.0*2.54*resolution.y+0.5)/100.0);
968 SetGeometry(image,&geometry);
969 (void) FormatLocaleString(page_geometry,MagickPathExtent,"%.20gx%.20g",
970 (double) image->columns,(double) image->rows);
971 if (image_info->page != (char *) NULL)
972 (void) CopyMagickString(page_geometry,image_info->page,MagickPathExtent);
974 if ((image->page.width != 0) && (image->page.height != 0))
975 (void) FormatLocaleString(page_geometry,MagickPathExtent,
976 "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,(double)
977 image->page.height,(double) image->page.x,(double) image->page.y);
979 if ((image->gravity != UndefinedGravity) &&
980 (LocaleCompare(image_info->magick,"PS") == 0))
981 (void) CopyMagickString(page_geometry,PSPageGeometry,MagickPathExtent);
982 (void) ConcatenateMagickString(page_geometry,">",MagickPathExtent);
983 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
984 &geometry.width,&geometry.height);
985 scale.x=(double) (geometry.width*delta.x)/resolution.x;
986 geometry.width=(size_t) floor(scale.x+0.5);
987 scale.y=(double) (geometry.height*delta.y)/resolution.y;
988 geometry.height=(size_t) floor(scale.y+0.5);
989 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
990 (void) ParseGravityGeometry(image,page_geometry,&page_info,exception);
991 if (image->gravity != UndefinedGravity)
993 geometry.x=(-page_info.x);
994 geometry.y=(ssize_t) (media_info.height+page_info.y-image->rows);
997 if (image_info->pointsize != 0.0)
998 pointsize=image_info->pointsize;
1000 value=GetImageProperty(image,"label",exception);
1001 if (value != (const char *) NULL)
1002 text_size=(size_t) (MultilineCensus(value)*pointsize+12);
1007 Postscript header on the first page.
1009 if (LocaleCompare(image_info->magick,"PS3") == 0)
1010 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0\n",MagickPathExtent);
1012 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0 EPSF-3.0\n",
1014 (void) WriteBlobString(image,buffer);
1015 (void) FormatLocaleString(buffer,MagickPathExtent,
1016 "%%%%Creator: ImageMagick %s\n",MagickLibVersionText);
1017 (void) WriteBlobString(image,buffer);
1018 (void) FormatLocaleString(buffer,MagickPathExtent,"%%%%Title: %s\n",
1020 (void) WriteBlobString(image,buffer);
1021 timer=time((time_t *) NULL);
1022 (void) FormatMagickTime(timer,MagickPathExtent,date);
1023 (void) FormatLocaleString(buffer,MagickPathExtent,
1024 "%%%%CreationDate: %s\n",date);
1025 (void) WriteBlobString(image,buffer);
1026 bounds.x1=(double) geometry.x;
1027 bounds.y1=(double) geometry.y;
1028 bounds.x2=(double) geometry.x+scale.x;
1029 bounds.y2=(double) geometry.y+scale.y+text_size;
1030 if ((image_info->adjoin != MagickFalse) &&
1031 (GetNextImageInList(image) != (Image *) NULL))
1033 (void) WriteBlobString(image,"%%BoundingBox: (atend)\n");
1034 (void) WriteBlobString(image,"%%HiResBoundingBox: (atend)\n");
1038 (void) FormatLocaleString(buffer,MagickPathExtent,
1039 "%%%%BoundingBox: %g %g %g %g\n",ceil(bounds.x1-0.5),
1040 ceil(bounds.y1-0.5),floor(bounds.x2+0.5),floor(bounds.y2+0.5));
1041 (void) WriteBlobString(image,buffer);
1042 (void) FormatLocaleString(buffer,MagickPathExtent,
1043 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,
1044 bounds.y1,bounds.x2,bounds.y2);
1045 (void) WriteBlobString(image,buffer);
1046 if (image->colorspace == CMYKColorspace)
1047 (void) WriteBlobString(image,
1048 "%%DocumentProcessColors: Cyan Magenta Yellow Black\n");
1050 if (SetImageGray(image,exception) != MagickFalse)
1051 (void) WriteBlobString(image,
1052 "%%DocumentProcessColors: Black\n");
1057 value=GetImageProperty(image,"label",exception);
1058 if (value != (const char *) NULL)
1059 (void) WriteBlobString(image,
1060 "%%DocumentNeededResources: font Helvetica\n");
1061 (void) WriteBlobString(image,"%%LanguageLevel: 3\n");
1063 Pages, orientation and order.
1065 if (LocaleCompare(image_info->magick,"PS3") != 0)
1066 (void) WriteBlobString(image,"%%Pages: 1\n");
1069 (void) WriteBlobString(image,"%%Orientation: Portrait\n");
1070 (void) WriteBlobString(image,"%%PageOrder: Ascend\n");
1071 if (image_info->adjoin == MagickFalse)
1072 (void) CopyMagickString(buffer,"%%Pages: 1\n",MagickPathExtent);
1074 (void) FormatLocaleString(buffer,MagickPathExtent,
1075 "%%%%Pages: %.20g\n",(double) imageListLength);
1076 (void) WriteBlobString(image,buffer);
1078 if (image->colorspace == CMYKColorspace)
1079 (void) WriteBlobString(image,
1080 "%%DocumentProcessColors: Cyan Magenta Yellow Black\n");
1081 (void) WriteBlobString(image,"%%EndComments\n");
1083 The static postscript procedures prolog.
1085 (void)WriteBlobString(image,"%%BeginProlog\n");
1086 for (q=PostscriptProlog; *q; q++)
1088 (void) WriteBlobString(image,*q);
1089 (void) WriteBlobByte(image,'\n');
1092 One label line for each line in label string.
1094 value=GetImageProperty(image,"label",exception);
1095 if (value != (const char *) NULL)
1097 (void) WriteBlobString(image,"\n %% Labels.\n /Helvetica "
1098 " findfont pointsize scalefont setfont\n");
1099 for (i=(ssize_t) MultilineCensus(value)-1; i >= 0; i--)
1101 (void) WriteBlobString(image,
1102 " currentfile buffer readline pop token pop\n");
1103 (void) FormatLocaleString(buffer,MagickPathExtent,
1104 " 0 y %g add moveto show pop\n",i*pointsize+12);
1105 (void) WriteBlobString(image,buffer);
1109 The static postscript procedures epilog.
1111 for (q=PostscriptEpilog; *q; q++)
1113 (void) WriteBlobString(image,*q);
1114 (void) WriteBlobByte(image,'\n');
1116 (void)WriteBlobString(image,"%%EndProlog\n");
1118 (void) FormatLocaleString(buffer,MagickPathExtent,"%%%%Page: 1 %.20g\n",
1120 (void) WriteBlobString(image,buffer);
1124 (void) FormatLocaleString(buffer,MagickPathExtent,
1125 "%%%%PageBoundingBox: %.20g %.20g %.20g %.20g\n",(double) geometry.x,
1126 (double) geometry.y,geometry.x+(double) geometry.width,geometry.y+
1127 (double) (geometry.height+text_size));
1128 (void) WriteBlobString(image,buffer);
1130 Page process colors if not RGB.
1132 if (image->colorspace == CMYKColorspace)
1133 (void) WriteBlobString(image,
1134 "%%PageProcessColors: Cyan Magenta Yellow Black\n");
1136 if (SetImageGray(image,exception) != MagickFalse)
1137 (void) WriteBlobString(image,"%%PageProcessColors: Black\n");
1139 Adjust document bounding box to bound page bounding box.
1141 if ((double) geometry.x < bounds.x1)
1142 bounds.x1=(double) geometry.x;
1143 if ((double) geometry.y < bounds.y1)
1144 bounds.y1=(double) geometry.y;
1145 if ((double) (geometry.x+scale.x) > bounds.x2)
1146 bounds.x2=(double) geometry.x+scale.x;
1147 if ((double) (geometry.y+scale.y+text_size) > bounds.y2)
1148 bounds.y2=(double) geometry.y+scale.y+text_size;
1150 Page font resource if there's a label.
1152 value=GetImageProperty(image,"label",exception);
1153 if (value != (const char *) NULL)
1154 (void) WriteBlobString(image,"%%PageResources: font Helvetica\n");
1156 PS clipping path from Photoshop clipping path.
1158 if (((image->channels & WriteMaskChannel) != 0) ||
1159 (LocaleNCompare("8BIM:",image->magick_filename,5) != 0))
1160 (void) WriteBlobString(image,"/ClipImage {} def\n");
1166 value=GetImageProperty(image,image->magick_filename,exception);
1167 if (value == (const char *) NULL)
1168 return(MagickFalse);
1169 (void) WriteBlobString(image,value);
1170 (void) WriteBlobByte(image,'\n');
1173 Push a dictionary for our own def's if this an EPS.
1175 if (LocaleCompare(image_info->magick,"PS3") != 0)
1176 (void) WriteBlobString(image,"userdict begin\n");
1180 if ((image->alpha_trait != UndefinedPixelTrait) &&
1181 (WritePS3MaskImage(image_info,image,compression,exception) == MagickFalse))
1183 (void) CloseBlob(image);
1184 return(MagickFalse);
1187 Remember position of BeginData comment so we can update it.
1189 start=TellBlob(image);
1191 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
1192 (void) FormatLocaleString(buffer,MagickPathExtent,
1193 "%%%%BeginData:%13ld %s Bytes\n",0L,
1194 compression == NoCompression ? "ASCII" : "BINARY");
1195 (void) WriteBlobString(image,buffer);
1196 stop=TellBlob(image);
1198 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
1199 (void) WriteBlobString(image,"DisplayImage\n");
1201 Translate, scale, and font point size.
1203 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g %.20g\n%g %g\n%g\n",
1204 (double) geometry.x,(double) geometry.y,scale.x,scale.y,pointsize);
1205 (void) WriteBlobString(image,buffer);
1209 labels=(char **) NULL;
1210 value=GetImageProperty(image,"label",exception);
1211 if (value != (const char *) NULL)
1212 labels=StringToList(value);
1213 if (labels != (char **) NULL)
1215 for (i=0; labels[i] != (char *) NULL; i++)
1217 if (compression != NoCompression)
1219 for (j=0; labels[i][j] != '\0'; j++)
1220 (void) WriteBlobByte(image,(unsigned char) labels[i][j]);
1221 (void) WriteBlobByte(image,'\n');
1225 (void) WriteBlobString(image,"<~");
1226 Ascii85Initialize(image);
1227 for (j=0; labels[i][j] != '\0'; j++)
1228 Ascii85Encode(image,(unsigned char) labels[i][j]);
1229 Ascii85Flush(image);
1231 labels[i]=DestroyString(labels[i]);
1233 labels=(char **) RelinquishMagickMemory(labels);
1236 Photoshop clipping path active?
1238 if (((image->channels & WriteMaskChannel) != 0) &&
1239 (LocaleNCompare("8BIM:",image->magick_filename,5) == 0))
1240 (void) WriteBlobString(image,"true\n");
1242 (void) WriteBlobString(image,"false\n");
1244 Showpage for non-EPS.
1246 (void) WriteBlobString(image, LocaleCompare(image_info->magick,"PS3") == 0 ?
1247 "true\n" : "false\n");
1249 Image columns, rows, and color space.
1251 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g %.20g\n%s\n",
1252 (double) image->columns,(double) image->rows,image->colorspace ==
1253 CMYKColorspace ? PS3_CMYKColorspace : PS3_RGBColorspace);
1254 (void) WriteBlobString(image,buffer);
1258 (void) WriteBlobString(image,image->alpha_trait != UndefinedPixelTrait ?
1259 "true\n" : "false\n");
1261 Render with imagemask operator?
1263 option=GetImageOption(image_info,"ps3:imagemask");
1264 (void) WriteBlobString(image,((option != (const char *) NULL) &&
1265 (SetImageMonochrome(image,exception) != MagickFalse)) ?
1266 "true\n" : "false\n");
1270 pixels=(unsigned char *) NULL;
1272 if ((image_info->type != TrueColorType) &&
1273 (image_info->type != TrueColorAlphaType) &&
1274 (image_info->type != ColorSeparationType) &&
1275 (image_info->type != ColorSeparationAlphaType) &&
1276 (image->colorspace != CMYKColorspace) &&
1277 ((SetImageGray(image,exception) != MagickFalse) ||
1278 (SetImageMonochrome(image,exception) != MagickFalse)))
1283 (void) WriteBlobString(image,PS3_PseudoClass"\n");
1284 switch (compression)
1289 (void) WriteBlobString(image,PS3_NoCompression"\n");
1292 case FaxCompression:
1293 case Group4Compression:
1295 (void) WriteBlobString(image,PS3_FaxCompression"\n");
1298 case JPEGCompression:
1300 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1303 case LZWCompression:
1305 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1308 case RLECompression:
1310 (void) WriteBlobString(image,PS3_RLECompression"\n");
1313 case ZipCompression:
1315 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1320 Number of colors -- 0 for single component non-color mapped data.
1322 (void) WriteBlobString(image,"0\n");
1324 1 bit or 8 bit components?
1326 (void) FormatLocaleString(buffer,MagickPathExtent,"%d\n",
1327 SetImageMonochrome(image,exception) != MagickFalse ? 1 : 8);
1328 (void) WriteBlobString(image,buffer);
1332 if (compression == JPEGCompression)
1333 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
1335 if ((compression == FaxCompression) ||
1336 (compression == Group4Compression))
1338 if (LocaleCompare(CCITTParam,"0") == 0)
1339 status=HuffmanEncodeImage(image_info,image,image,exception);
1341 status=Huffman2DEncodeImage(image_info,image,image,exception);
1345 status=SerializeImageChannel(image_info,image,&pixel_info,&length,
1347 if (status == MagickFalse)
1349 (void) CloseBlob(image);
1350 return(MagickFalse);
1352 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
1353 switch (compression)
1358 Ascii85Initialize(image);
1359 for (i=0; i < (ssize_t) length; i++)
1360 Ascii85Encode(image,pixels[i]);
1361 Ascii85Flush(image);
1365 case LZWCompression:
1367 status=LZWEncodeImage(image,length,pixels,exception);
1370 case RLECompression:
1372 status=PackbitsEncodeImage(image,length,pixels,exception);
1375 case ZipCompression:
1377 status=ZLIBEncodeImage(image,length,pixels,exception);
1381 pixel_info=RelinquishVirtualMemory(pixel_info);
1385 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1386 (compression == JPEGCompression))
1391 (void) WriteBlobString(image,PS3_DirectClass"\n");
1392 switch (compression)
1397 (void) WriteBlobString(image,PS3_NoCompression"\n");
1400 case RLECompression:
1402 (void) WriteBlobString(image,PS3_RLECompression"\n");
1405 case JPEGCompression:
1407 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1410 case LZWCompression:
1412 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1415 case ZipCompression:
1417 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1424 if (compression == JPEGCompression)
1425 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
1429 Stream based compressions.
1431 status=SerializeImage(image_info,image,&pixel_info,&length,
1433 if (status == MagickFalse)
1435 (void) CloseBlob(image);
1436 return(MagickFalse);
1438 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
1439 switch (compression)
1444 Ascii85Initialize(image);
1445 for (i=0; i < (ssize_t) length; i++)
1446 Ascii85Encode(image,pixels[i]);
1447 Ascii85Flush(image);
1451 case RLECompression:
1453 status=PackbitsEncodeImage(image,length,pixels,exception);
1456 case LZWCompression:
1458 status=LZWEncodeImage(image,length,pixels,exception);
1461 case ZipCompression:
1463 status=ZLIBEncodeImage(image,length,pixels,exception);
1467 pixel_info=RelinquishVirtualMemory(pixel_info);
1475 (void) WriteBlobString(image,PS3_PseudoClass"\n");
1476 switch (compression)
1481 (void) WriteBlobString(image,PS3_NoCompression"\n");
1484 case RLECompression:
1486 (void) WriteBlobString(image,PS3_RLECompression"\n");
1489 case LZWCompression:
1491 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1494 case ZipCompression:
1496 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1501 Number of colors in color map.
1503 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g\n",
1504 (double) image->colors);
1505 (void) WriteBlobString(image,buffer);
1507 Color map - uncompressed.
1509 if ((compression != NoCompression) &&
1510 (compression != UndefinedCompression))
1512 for (i=0; i < (ssize_t) image->colors; i++)
1514 pixel=ScaleQuantumToChar(image->colormap[i].red);
1515 (void) WriteBlobByte(image,(unsigned char) pixel);
1516 pixel=ScaleQuantumToChar(image->colormap[i].green);
1517 (void) WriteBlobByte(image,(unsigned char) pixel);
1518 pixel=ScaleQuantumToChar(image->colormap[i].blue);
1519 (void) WriteBlobByte(image,(unsigned char) pixel);
1524 Ascii85Initialize(image);
1525 for (i=0; i < (ssize_t) image->colors; i++)
1527 pixel=ScaleQuantumToChar(image->colormap[i].red);
1528 Ascii85Encode(image,(unsigned char) pixel);
1529 pixel=ScaleQuantumToChar(image->colormap[i].green);
1530 Ascii85Encode(image,(unsigned char) pixel);
1531 pixel=ScaleQuantumToChar(image->colormap[i].blue);
1532 Ascii85Encode(image,(unsigned char) pixel);
1534 Ascii85Flush(image);
1536 status=SerializeImageIndexes(image_info,image,&pixel_info,&length,
1538 if (status == MagickFalse)
1540 (void) CloseBlob(image);
1541 return(MagickFalse);
1543 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
1544 switch (compression)
1549 Ascii85Initialize(image);
1550 for (i=0; i < (ssize_t) length; i++)
1551 Ascii85Encode(image,pixels[i]);
1552 Ascii85Flush(image);
1556 case RLECompression:
1558 status=PackbitsEncodeImage(image,length,pixels,exception);
1561 case LZWCompression:
1563 status=LZWEncodeImage(image,length,pixels,exception);
1566 case ZipCompression:
1568 status=ZLIBEncodeImage(image,length,pixels,exception);
1572 pixel_info=RelinquishVirtualMemory(pixel_info);
1574 (void) WriteBlobByte(image,'\n');
1575 if (status == MagickFalse)
1577 (void) CloseBlob(image);
1578 return(MagickFalse);
1581 Update BeginData now that we know the data size.
1583 length=(size_t) (TellBlob(image)-stop);
1584 stop=TellBlob(image);
1586 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
1587 offset=SeekBlob(image,start,SEEK_SET);
1589 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
1590 (void) FormatLocaleString(buffer,MagickPathExtent,
1591 "%%%%BeginData:%13ld %s Bytes\n",(long) length,
1592 compression == NoCompression ? "ASCII" : "BINARY");
1593 (void) WriteBlobString(image,buffer);
1594 offset=SeekBlob(image,stop,SEEK_SET);
1595 (void) WriteBlobString(image,"%%EndData\n");
1597 End private dictionary if this an EPS.
1599 if (LocaleCompare(image_info->magick,"PS3") != 0)
1600 (void) WriteBlobString(image,"end\n");
1601 (void) WriteBlobString(image,"%%PageTrailer\n");
1602 if (GetNextImageInList(image) == (Image *) NULL)
1604 image=SyncNextImageInList(image);
1605 status=SetImageProgress(image,SaveImagesTag,scene++,imageListLength);
1606 if (status == MagickFalse)
1608 } while (image_info->adjoin != MagickFalse);
1609 (void) WriteBlobString(image,"%%Trailer\n");
1612 (void) FormatLocaleString(buffer,MagickPathExtent,
1613 "%%%%BoundingBox: %g %g %g %g\n",ceil(bounds.x1-0.5),
1614 ceil(bounds.y1-0.5),floor(bounds.x2+0.5),floor(bounds.y2+0.5));
1615 (void) WriteBlobString(image,buffer);
1616 (void) FormatLocaleString(buffer,MagickPathExtent,
1617 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,bounds.y1,bounds.x2,
1619 (void) WriteBlobString(image,buffer);
1621 (void) WriteBlobString(image,"%%EOF\n");
1622 (void) CloseBlob(image);