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)
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,&image->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)
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,&image->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)
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,&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,&image->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)
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,&image->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)
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,&image->exception);
522 if (mask_image == (Image *) NULL)
523 ThrowWriterException(CoderError,image->exception.reason);
524 channel_mask=SetPixelChannelMask(mask_image,AlphaChannel);
525 status=SeparateImage(mask_image);
526 (void) SetPixelChannelMap(mask_image,channel_mask);
527 if (status == MagickFalse)
529 mask_image=DestroyImage(mask_image);
532 (void) SetImageType(mask_image,BilevelType);
533 (void) SetImageType(mask_image,PaletteType);
534 mask_image->matte=MagickFalse;
535 pixels=(unsigned char *) NULL;
542 status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
543 if (status == MagickFalse)
545 Ascii85Initialize(image);
546 for (i=0; i < (ssize_t) length; i++)
547 Ascii85Encode(image,pixels[i]);
549 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
553 case Group4Compression:
555 if ((compression == FaxCompression) ||
556 (LocaleCompare(CCITTParam,"0") == 0))
557 status=HuffmanEncodeImage(image_info,image,mask_image);
559 status=Huffman2DEncodeImage(image_info,image,mask_image);
564 status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
565 if (status == MagickFalse)
567 status=LZWEncodeImage(image,length,pixels);
568 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
573 status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
574 if (status == MagickFalse)
576 status=PackbitsEncodeImage(image,length,pixels);
577 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
582 status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
583 if (status == MagickFalse)
585 status=ZLIBEncodeImage(image,length,pixels);
586 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
590 mask_image=DestroyImage(mask_image);
591 (void) WriteBlobByte(image,'\n');
592 length=(size_t) (TellBlob(image)-stop);
593 stop=TellBlob(image);
594 offset=SeekBlob(image,start,SEEK_SET);
596 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
597 (void) FormatLocaleString(buffer,MaxTextExtent,
598 "%%%%BeginData:%13ld %s Bytes\n",(long) length,
599 compression == NoCompression ? "ASCII" : "BINARY");
600 (void) WriteBlobString(image,buffer);
601 offset=SeekBlob(image,stop,SEEK_SET);
603 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
604 (void) WriteBlobString(image,"%%EndData\n");
605 (void) WriteBlobString(image, "/mask_stream exch def\n");
609 static MagickBooleanType WritePS3Image(const ImageInfo *image_info,Image *image,
610 ExceptionInfo *exception)
615 "/ByteStreamDecodeFilter",
620 " z "PS3_NoCompression" eq { /ASCII85Decode filter } if",
621 " z "PS3_FaxCompression" eq",
628 " /CCITTFaxDecode filter",
630 " z "PS3_JPEGCompression" eq { /DCTDecode filter } if",
631 " z "PS3_LZWCompression" eq { /LZWDecode filter } if",
632 " z "PS3_RLECompression" eq { /RunLengthDecode filter } if",
633 " z "PS3_ZipCompression" eq { /FlateDecode filter } if",
636 "/DirectClassImageDict",
638 " colorspace "PS3_RGBColorspace" eq",
640 " /DeviceRGB setcolorspace",
645 " /BitsPerComponent 8",
646 " /DataSource pixel_stream",
647 " /MultipleDataSources false",
648 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
649 " /Decode [0 1 0 1 0 1]",
653 " /DeviceCMYK setcolorspace",
658 " /BitsPerComponent 8",
659 " /DataSource pixel_stream",
660 " /MultipleDataSources false",
661 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
663 " compression "PS3_JPEGCompression" eq",
664 " { [1 0 1 0 1 0 1 0] }",
665 " { [0 1 0 1 0 1 0 1] }",
672 "/PseudoClassImageDict",
674 " % Colors in colormap image.",
675 " currentfile buffer readline pop",
676 " token pop /colors exch def pop",
679 " % Depth of grayscale image.",
680 " currentfile buffer readline pop",
681 " token pop /bits exch def pop",
682 " /DeviceGray setcolorspace",
687 " /BitsPerComponent bits",
689 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
690 " /DataSource pixel_stream",
695 " /colormap colors 3 mul string def",
696 " compression "PS3_NoCompression" eq",
697 " { currentfile /ASCII85Decode filter colormap readstring pop pop }",
698 " { currentfile colormap readstring pop pop }",
700 " [ /Indexed /DeviceRGB colors 1 sub colormap ] setcolorspace",
705 " /BitsPerComponent 8",
707 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
708 " /DataSource pixel_stream",
714 "/NonMaskedImageDict",
716 " class "PS3_PseudoClass" eq",
717 " { PseudoClassImageDict }",
718 " { DirectClassImageDict }",
726 " /InterleaveType 3",
727 " /DataDict NonMaskedImageDict",
733 " /BitsPerComponent 1",
734 " /DataSource mask_stream",
735 " /MultipleDataSources false",
736 " /ImageMatrix [ columns 0 0 rows neg 0 rows]",
747 " /buffer 512 string def",
749 " currentfile buffer readline pop",
750 " token pop /x exch def",
751 " token pop /y exch def pop",
753 " % Image size and font size.",
754 " currentfile buffer readline pop",
755 " token pop /x exch def",
756 " token pop /y exch def pop",
757 " currentfile buffer readline pop",
758 " token pop /pointsize exch def pop",
765 " currentfile buffer readline pop",
766 " token pop /clipped exch def pop",
768 " currentfile buffer readline pop",
769 " token pop /sp exch def pop",
770 " % Image pixel size.",
771 " currentfile buffer readline pop",
772 " token pop /columns exch def",
773 " token pop /rows exch def pop",
774 " % Colorspace (RGB/CMYK).",
775 " currentfile buffer readline pop",
776 " token pop /colorspace exch def pop",
778 " currentfile buffer readline pop",
779 " token pop /alpha exch def pop",
781 " currentfile buffer readline pop",
782 " token pop /stencil exch def pop",
783 " % Image class (direct/pseudo).",
784 " currentfile buffer readline pop",
785 " token pop /class exch def pop",
786 " % Compression type.",
787 " currentfile buffer readline pop",
788 " token pop /compression exch def pop",
789 " % Clip and render.",
790 " /pixel_stream currentfile columns rows compression ByteStreamDecodeFilter def",
791 " clipped { ClipImage } if",
792 " alpha stencil not and",
793 " { MaskedImageDict mask_stream resetfile }",
794 " { NonMaskedImageDict }",
796 " stencil { 0 setgray imagemask } { image } ifelse",
797 " sp { showpage } if",
803 buffer[MaxTextExtent],
806 page_geometry[MaxTextExtent];
866 Open output image file.
868 assert(image_info != (const ImageInfo *) NULL);
869 assert(image_info->signature == MagickSignature);
870 assert(image != (Image *) NULL);
871 assert(image->signature == MagickSignature);
872 if (image->debug != MagickFalse)
873 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
874 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
875 if (status == MagickFalse)
877 compression=image->compression;
878 if (image_info->compression != UndefinedCompression)
879 compression=image_info->compression;
883 case Group4Compression:
885 if ((IsImageMonochrome(image,exception) == MagickFalse) ||
886 (image->matte != MagickFalse))
887 compression=RLECompression;
890 #if !defined(MAGICKCORE_JPEG_DELEGATE)
891 case JPEGCompression:
893 compression=RLECompression;
894 (void) ThrowMagickException(exception,GetMagickModule(),
895 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
900 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
903 compression=RLECompression;
904 (void) ThrowMagickException(exception,GetMagickModule(),
905 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZLIB)",
913 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
919 Scale relative to dots-per-inch.
921 delta.x=DefaultResolution;
922 delta.y=DefaultResolution;
923 resolution.x=image->x_resolution;
924 resolution.y=image->y_resolution;
925 if ((resolution.x == 0.0) || (resolution.y == 0.0))
927 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
928 resolution.x=geometry_info.rho;
929 resolution.y=geometry_info.sigma;
930 if ((flags & SigmaValue) == 0)
931 resolution.y=resolution.x;
933 if (image_info->density != (char *) NULL)
935 flags=ParseGeometry(image_info->density,&geometry_info);
936 resolution.x=geometry_info.rho;
937 resolution.y=geometry_info.sigma;
938 if ((flags & SigmaValue) == 0)
939 resolution.y=resolution.x;
941 if (image->units == PixelsPerCentimeterResolution)
943 resolution.x=(size_t) (100.0*2.54*resolution.x+0.5)/100.0;
944 resolution.y=(size_t) (100.0*2.54*resolution.y+0.5)/100.0;
946 SetGeometry(image,&geometry);
947 (void) FormatLocaleString(page_geometry,MaxTextExtent,"%.20gx%.20g",
948 (double) image->columns,(double) image->rows);
949 if (image_info->page != (char *) NULL)
950 (void) CopyMagickString(page_geometry,image_info->page,MaxTextExtent);
952 if ((image->page.width != 0) && (image->page.height != 0))
953 (void) FormatLocaleString(page_geometry,MaxTextExtent,
954 "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,(double)
955 image->page.height,(double) image->page.x,(double) image->page.y);
957 if ((image->gravity != UndefinedGravity) &&
958 (LocaleCompare(image_info->magick,"PS") == 0))
959 (void) CopyMagickString(page_geometry,PSPageGeometry,MaxTextExtent);
960 (void) ConcatenateMagickString(page_geometry,">",MaxTextExtent);
961 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
962 &geometry.width,&geometry.height);
963 scale.x=(double) (geometry.width*delta.x)/resolution.x;
964 geometry.width=(size_t) floor(scale.x+0.5);
965 scale.y=(double) (geometry.height*delta.y)/resolution.y;
966 geometry.height=(size_t) floor(scale.y+0.5);
967 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
968 (void) ParseGravityGeometry(image,page_geometry,&page_info,exception);
969 if (image->gravity != UndefinedGravity)
971 geometry.x=(-page_info.x);
972 geometry.y=(ssize_t) (media_info.height+page_info.y-image->rows);
975 if (image_info->pointsize != 0.0)
976 pointsize=image_info->pointsize;
978 value=GetImageProperty(image,"label");
979 if (value != (const char *) NULL)
980 text_size=(size_t) (MultilineCensus(value)*pointsize+12);
985 Postscript header on the first page.
987 if (LocaleCompare(image_info->magick,"PS3") == 0)
988 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0\n",MaxTextExtent);
990 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0 EPSF-3.0\n",
992 (void) WriteBlobString(image,buffer);
993 (void) FormatLocaleString(buffer,MaxTextExtent,
994 "%%%%Creator: ImageMagick %s\n",MagickLibVersionText);
995 (void) WriteBlobString(image,buffer);
996 (void) FormatLocaleString(buffer,MaxTextExtent,"%%%%Title: %s\n",
998 (void) WriteBlobString(image,buffer);
999 timer=time((time_t *) NULL);
1000 (void) FormatMagickTime(timer,MaxTextExtent,date);
1001 (void) FormatLocaleString(buffer,MaxTextExtent,
1002 "%%%%CreationDate: %s\n",date);
1003 (void) WriteBlobString(image,buffer);
1004 bounds.x1=(double) geometry.x;
1005 bounds.y1=(double) geometry.y;
1006 bounds.x2=(double) geometry.x+scale.x;
1007 bounds.y2=(double) geometry.y+scale.y+text_size;
1008 if ((image_info->adjoin != MagickFalse) &&
1009 (GetNextImageInList(image) != (Image *) NULL))
1011 (void) WriteBlobString(image,"%%BoundingBox: (atend)\n");
1012 (void) WriteBlobString(image,"%%HiResBoundingBox: (atend)\n");
1016 (void) FormatLocaleString(buffer,MaxTextExtent,
1017 "%%%%BoundingBox: %g %g %g %g\n",floor(bounds.x1+0.5),
1018 floor(bounds.y1+0.5),ceil(bounds.x2-0.5),ceil(bounds.y2-0.5));
1019 (void) WriteBlobString(image,buffer);
1020 (void) FormatLocaleString(buffer,MaxTextExtent,
1021 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,
1022 bounds.y1,bounds.x2,bounds.y2);
1023 (void) WriteBlobString(image,buffer);
1024 if (image->colorspace == CMYKColorspace)
1025 (void) WriteBlobString(image,
1026 "%%DocumentProcessColors: Cyan Magenta Yellow Black\n");
1028 if (IsImageGray(image,exception) != MagickFalse)
1029 (void) WriteBlobString(image,
1030 "%%DocumentProcessColors: Black\n");
1035 value=GetImageProperty(image,"label");
1036 if (value != (const char *) NULL)
1037 (void) WriteBlobString(image,
1038 "%%DocumentNeededResources: font Helvetica\n");
1039 (void) WriteBlobString(image,"%%LanguageLevel: 3\n");
1041 Pages, orientation and order.
1043 if (LocaleCompare(image_info->magick,"PS3") != 0)
1044 (void) WriteBlobString(image,"%%Pages: 1\n");
1047 (void) WriteBlobString(image,"%%Orientation: Portrait\n");
1048 (void) WriteBlobString(image,"%%PageOrder: Ascend\n");
1049 if (image_info->adjoin == MagickFalse)
1050 (void) CopyMagickString(buffer,"%%Pages: 1\n",MaxTextExtent);
1052 (void) FormatLocaleString(buffer,MaxTextExtent,
1053 "%%%%Pages: %.20g\n",(double) GetImageListLength(image));
1054 (void) WriteBlobString(image,buffer);
1056 (void) WriteBlobString(image,"%%EndComments\n");
1058 The static postscript procedures prolog.
1060 (void)WriteBlobString(image,"%%BeginProlog\n");
1061 for (q=PostscriptProlog; *q; q++)
1063 (void) WriteBlobString(image,*q);
1064 (void) WriteBlobByte(image,'\n');
1067 One label line for each line in label string.
1069 value=GetImageProperty(image,"label");
1070 if (value != (const char *) NULL)
1072 (void) WriteBlobString(image,"\n %% Labels.\n /Helvetica "
1073 " findfont pointsize scalefont setfont\n");
1074 for (i=(ssize_t) MultilineCensus(value)-1; i >= 0; i--)
1076 (void) WriteBlobString(image,
1077 " currentfile buffer readline pop token pop\n");
1078 (void) FormatLocaleString(buffer,MaxTextExtent,
1079 " 0 y %g add moveto show pop\n",i*pointsize+12);
1080 (void) WriteBlobString(image,buffer);
1084 The static postscript procedures epilog.
1086 for (q=PostscriptEpilog; *q; q++)
1088 (void) WriteBlobString(image,*q);
1089 (void) WriteBlobByte(image,'\n');
1091 (void)WriteBlobString(image,"%%EndProlog\n");
1093 (void) FormatLocaleString(buffer,MaxTextExtent,"%%%%Page: 1 %.20g\n",
1095 (void) WriteBlobString(image,buffer);
1099 (void) FormatLocaleString(buffer,MaxTextExtent,
1100 "%%%%PageBoundingBox: %.20g %.20g %.20g %.20g\n",(double) geometry.x,
1101 (double) geometry.y,geometry.x+(double) geometry.width,geometry.y+
1102 (double) (geometry.height+text_size));
1103 (void) WriteBlobString(image,buffer);
1105 Page process colors if not RGB.
1107 if (image->colorspace == CMYKColorspace)
1108 (void) WriteBlobString(image,
1109 "%%PageProcessColors: Cyan Magenta Yellow Black\n");
1111 if (IsImageGray(image,exception) != MagickFalse)
1112 (void) WriteBlobString(image,"%%PageProcessColors: Black\n");
1114 Adjust document bounding box to bound page bounding box.
1116 if ((double) geometry.x < bounds.x1)
1117 bounds.x1=(double) geometry.x;
1118 if ((double) geometry.y < bounds.y1)
1119 bounds.y1=(double) geometry.y;
1120 if ((double) (geometry.x+scale.x) > bounds.x2)
1121 bounds.x2=(double) geometry.x+scale.x;
1122 if ((double) (geometry.y+scale.y+text_size) > bounds.y2)
1123 bounds.y2=(double) geometry.y+scale.y+text_size;
1125 Page font resource if there's a label.
1127 value=GetImageProperty(image,"label");
1128 if (value != (const char *) NULL)
1129 (void) WriteBlobString(image,"%%PageResources: font Helvetica\n");
1131 PS clipping path from Photoshop clipping path.
1133 if ((image->clip_mask == (Image *) NULL) ||
1134 (LocaleNCompare("8BIM:",image->clip_mask->magick_filename,5) != 0))
1135 (void) WriteBlobString(image,"/ClipImage {} def\n");
1141 value=GetImageProperty(image,image->clip_mask->magick_filename);
1142 if (value == (const char *) NULL)
1143 return(MagickFalse);
1144 (void) WriteBlobString(image,value);
1145 (void) WriteBlobByte(image,'\n');
1148 Push a dictionary for our own def's if this an EPS.
1150 if (LocaleCompare(image_info->magick,"PS3") != 0)
1151 (void) WriteBlobString(image,"userdict begin\n");
1155 if ((image->matte != MagickFalse) &&
1156 (WritePS3MaskImage(image_info,image,compression) == MagickFalse))
1158 (void) CloseBlob(image);
1159 return(MagickFalse);
1162 Remember position of BeginData comment so we can update it.
1164 start=TellBlob(image);
1165 (void) FormatLocaleString(buffer,MaxTextExtent,
1166 "%%%%BeginData:%13ld %s Bytes\n",0L,
1167 compression == NoCompression ? "ASCII" : "BINARY");
1168 (void) WriteBlobString(image,buffer);
1169 stop=TellBlob(image);
1170 (void) WriteBlobString(image,"DisplayImage\n");
1172 Translate, scale, and font point size.
1174 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n%g %g\n%g\n",
1175 (double) geometry.x,(double) geometry.y,scale.x,scale.y,pointsize);
1176 (void) WriteBlobString(image,buffer);
1180 labels=(char **) NULL;
1181 value=GetImageProperty(image,"label");
1182 if (value != (const char *) NULL)
1183 labels=StringToList(value);
1184 if (labels != (char **) NULL)
1186 for (i=0; labels[i] != (char *) NULL; i++)
1188 if (compression != NoCompression)
1190 for (j=0; labels[i][j] != '\0'; j++)
1191 (void) WriteBlobByte(image,(unsigned char) labels[i][j]);
1192 (void) WriteBlobByte(image,'\n');
1196 (void) WriteBlobString(image,"<~");
1197 Ascii85Initialize(image);
1198 for (j=0; labels[i][j] != '\0'; j++)
1199 Ascii85Encode(image,(unsigned char) labels[i][j]);
1200 Ascii85Flush(image);
1202 labels[i]=DestroyString(labels[i]);
1204 labels=(char **) RelinquishMagickMemory(labels);
1207 Photoshop clipping path active?
1209 if ((image->clip_mask != (Image *) NULL) &&
1210 (LocaleNCompare("8BIM:",image->clip_mask->magick_filename,5) == 0))
1211 (void) WriteBlobString(image,"true\n");
1213 (void) WriteBlobString(image,"false\n");
1215 Showpage for non-EPS.
1217 (void) WriteBlobString(image, LocaleCompare(image_info->magick,"PS3") == 0 ?
1218 "true\n" : "false\n");
1220 Image columns, rows, and color space.
1222 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n%s\n",
1223 (double) image->columns,(double) image->rows,image->colorspace ==
1224 CMYKColorspace ? PS3_CMYKColorspace : PS3_RGBColorspace);
1225 (void) WriteBlobString(image,buffer);
1229 (void) WriteBlobString(image,image->matte != MagickFalse ?
1230 "true\n" : "false\n");
1232 Render with imagemask operator?
1234 option=GetImageOption(image_info,"ps3:imagemask");
1235 (void) WriteBlobString(image,((option != (const char *) NULL) &&
1236 (IsImageMonochrome(image,exception) != MagickFalse)) ?
1237 "true\n" : "false\n");
1241 pixels=(unsigned char *) NULL;
1243 if ((image_info->type != TrueColorType) &&
1244 (image_info->type != TrueColorMatteType) &&
1245 (image_info->type != ColorSeparationType) &&
1246 (image_info->type != ColorSeparationMatteType) &&
1247 (image->colorspace != CMYKColorspace) &&
1248 ((IsImageGray(image,exception) != MagickFalse) ||
1249 (IsImageMonochrome(image,exception) != MagickFalse)))
1254 (void) WriteBlobString(image,PS3_PseudoClass"\n");
1255 switch (compression)
1260 (void) WriteBlobString(image,PS3_NoCompression"\n");
1263 case FaxCompression:
1264 case Group4Compression:
1266 (void) WriteBlobString(image,PS3_FaxCompression"\n");
1269 case JPEGCompression:
1271 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1274 case LZWCompression:
1276 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1279 case RLECompression:
1281 (void) WriteBlobString(image,PS3_RLECompression"\n");
1284 case ZipCompression:
1286 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1291 Number of colors -- 0 for single component non-color mapped data.
1293 (void) WriteBlobString(image,"0\n");
1295 1 bit or 8 bit components?
1297 (void) FormatLocaleString(buffer,MaxTextExtent,"%d\n",
1298 IsImageMonochrome(image,exception) != MagickFalse ? 1 : 8);
1299 (void) WriteBlobString(image,buffer);
1303 if (compression == JPEGCompression)
1304 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
1306 if ((compression == FaxCompression) ||
1307 (compression == Group4Compression))
1309 if (LocaleCompare(CCITTParam,"0") == 0)
1310 status=HuffmanEncodeImage(image_info,image,image);
1312 status=Huffman2DEncodeImage(image_info,image,image);
1316 status=SerializeImageChannel(image_info,image,&pixels,&length);
1317 if (status == MagickFalse)
1319 (void) CloseBlob(image);
1320 return(MagickFalse);
1322 switch (compression)
1327 Ascii85Initialize(image);
1328 for (i=0; i < (ssize_t) length; i++)
1329 Ascii85Encode(image,pixels[i]);
1330 Ascii85Flush(image);
1334 case LZWCompression:
1336 status=LZWEncodeImage(image,length,pixels);
1339 case RLECompression:
1341 status=PackbitsEncodeImage(image,length,pixels);
1344 case ZipCompression:
1346 status=ZLIBEncodeImage(image,length,pixels);
1350 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1354 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1355 (compression == JPEGCompression))
1360 (void) WriteBlobString(image,PS3_DirectClass"\n");
1361 switch (compression)
1366 (void) WriteBlobString(image,PS3_NoCompression"\n");
1369 case RLECompression:
1371 (void) WriteBlobString(image,PS3_RLECompression"\n");
1374 case JPEGCompression:
1376 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1379 case LZWCompression:
1381 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1384 case ZipCompression:
1386 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1393 if (compression == JPEGCompression)
1394 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
1398 Stream based compressions.
1400 status=SerializeImage(image_info,image,&pixels,&length);
1401 if (status == MagickFalse)
1403 (void) CloseBlob(image);
1404 return(MagickFalse);
1406 switch (compression)
1411 Ascii85Initialize(image);
1412 for (i=0; i < (ssize_t) length; i++)
1413 Ascii85Encode(image,pixels[i]);
1414 Ascii85Flush(image);
1418 case RLECompression:
1420 status=PackbitsEncodeImage(image,length,pixels);
1423 case LZWCompression:
1425 status=LZWEncodeImage(image,length,pixels);
1428 case ZipCompression:
1430 status=ZLIBEncodeImage(image,length,pixels);
1434 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1442 (void) WriteBlobString(image,PS3_PseudoClass"\n");
1443 switch (compression)
1448 (void) WriteBlobString(image,PS3_NoCompression"\n");
1451 case JPEGCompression:
1453 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1456 case RLECompression:
1458 (void) WriteBlobString(image,PS3_RLECompression"\n");
1461 case LZWCompression:
1463 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1466 case ZipCompression:
1468 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1473 Number of colors in color map.
1475 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",
1476 (double) image->colors);
1477 (void) WriteBlobString(image,buffer);
1479 Color map - uncompressed.
1481 if ((compression != NoCompression) &&
1482 (compression != UndefinedCompression))
1484 for (i=0; i < (ssize_t) image->colors; i++)
1486 pixel=ScaleQuantumToChar(image->colormap[i].red);
1487 (void) WriteBlobByte(image,(unsigned char) pixel);
1488 pixel=ScaleQuantumToChar(image->colormap[i].green);
1489 (void) WriteBlobByte(image,(unsigned char) pixel);
1490 pixel=ScaleQuantumToChar(image->colormap[i].blue);
1491 (void) WriteBlobByte(image,(unsigned char) pixel);
1496 Ascii85Initialize(image);
1497 for (i=0; i < (ssize_t) image->colors; i++)
1499 pixel=ScaleQuantumToChar(image->colormap[i].red);
1500 Ascii85Encode(image,(unsigned char) pixel);
1501 pixel=ScaleQuantumToChar(image->colormap[i].green);
1502 Ascii85Encode(image,(unsigned char) pixel);
1503 pixel=ScaleQuantumToChar(image->colormap[i].blue);
1504 Ascii85Encode(image,(unsigned char) pixel);
1506 Ascii85Flush(image);
1508 status=SerializeImageIndexes(image_info,image,&pixels,&length);
1509 if (status == MagickFalse)
1511 (void) CloseBlob(image);
1512 return(MagickFalse);
1514 switch (compression)
1519 Ascii85Initialize(image);
1520 for (i=0; i < (ssize_t) length; i++)
1521 Ascii85Encode(image,pixels[i]);
1522 Ascii85Flush(image);
1526 case JPEGCompression:
1528 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
1531 case RLECompression:
1533 status=PackbitsEncodeImage(image,length,pixels);
1536 case LZWCompression:
1538 status=LZWEncodeImage(image,length,pixels);
1541 case ZipCompression:
1543 status=ZLIBEncodeImage(image,length,pixels);
1547 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1549 (void) WriteBlobByte(image,'\n');
1550 if (status == MagickFalse)
1552 (void) CloseBlob(image);
1553 return(MagickFalse);
1556 Update BeginData now that we know the data size.
1558 length=(size_t) (TellBlob(image)-stop);
1559 stop=TellBlob(image);
1560 offset=SeekBlob(image,start,SEEK_SET);
1562 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
1563 (void) FormatLocaleString(buffer,MaxTextExtent,
1564 "%%%%BeginData:%13ld %s Bytes\n",(long) length,
1565 compression == NoCompression ? "ASCII" : "BINARY");
1566 (void) WriteBlobString(image,buffer);
1567 offset=SeekBlob(image,stop,SEEK_SET);
1568 (void) WriteBlobString(image,"%%EndData\n");
1570 End private dictionary if this an EPS.
1572 if (LocaleCompare(image_info->magick,"PS3") != 0)
1573 (void) WriteBlobString(image,"end\n");
1574 (void) WriteBlobString(image,"%%PageTrailer\n");
1575 if (GetNextImageInList(image) == (Image *) NULL)
1577 image=SyncNextImageInList(image);
1578 status=SetImageProgress(image,SaveImagesTag,scene++,
1579 GetImageListLength(image));
1580 if (status == MagickFalse)
1582 } while (image_info->adjoin != MagickFalse);
1583 (void) WriteBlobString(image,"%%Trailer\n");
1586 (void) FormatLocaleString(buffer,MaxTextExtent,
1587 "%%%%BoundingBox: %g %g %g %g\n",floor(bounds.x1+0.5),
1588 floor(bounds.y1+0.5),ceil(bounds.x2-0.5),ceil(bounds.y2-0.5));
1589 (void) WriteBlobString(image,buffer);
1590 (void) FormatLocaleString(buffer,MaxTextExtent,
1591 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,bounds.y1,
1592 bounds.x2,bounds.y2);
1593 (void) WriteBlobString(image,buffer);
1595 (void) WriteBlobString(image,"%%EOF\n");
1596 (void) CloseBlob(image);