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 *);
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,Image *image)
190 % A description of each parameter follows:
192 % o image_info: Specifies a pointer to a ImageInfo structure.
194 % o image: the image.
198 static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
199 Image *image,Image *inject_image)
217 write_info=CloneImageInfo(image_info);
218 (void) CopyMagickString(write_info->filename,"GROUP4:",MaxTextExtent);
219 (void) CopyMagickString(write_info->magick,"GROUP4",MaxTextExtent);
220 group4_image=CloneImage(inject_image,0,0,MagickTrue,&image->exception);
221 if (group4_image == (Image *) NULL)
223 group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
225 group4_image=DestroyImage(group4_image);
226 if (group4 == (unsigned char *) NULL)
228 write_info=DestroyImageInfo(write_info);
229 if (WriteBlob(image,length,group4) != (ssize_t) length)
231 group4=(unsigned char *) RelinquishMagickMemory(group4);
235 static MagickBooleanType SerializeImage(const ImageInfo *image_info,
236 Image *image,unsigned char **pixels,size_t *length)
241 register const Quantum
247 register unsigned char
253 assert(image != (Image *) NULL);
254 assert(image->signature == MagickSignature);
255 if (image->debug != MagickFalse)
256 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
258 *length=(image->colorspace == CMYKColorspace ? 4 : 3)*(size_t)
259 image->columns*image->rows;
260 *pixels=(unsigned char *) AcquireQuantumMemory(*length,sizeof(**pixels));
261 if (*pixels == (unsigned char *) NULL)
262 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
264 for (y=0; y < (ssize_t) image->rows; y++)
266 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
267 if (p == (const Quantum *) NULL)
269 if (image->colorspace != CMYKColorspace)
270 for (x=0; x < (ssize_t) image->columns; x++)
272 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
273 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
274 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
275 p+=GetPixelComponents(image);
278 for (x=0; x < (ssize_t) image->columns; x++)
280 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
281 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
282 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
283 *q++=ScaleQuantumToChar(GetPixelBlack(image,p));
284 p+=GetPixelComponents(image);
286 if (image->previous == (Image *) NULL)
288 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
290 if (status == MagickFalse)
294 if (status == MagickFalse)
295 *pixels=(unsigned char *) RelinquishMagickMemory(*pixels);
299 static MagickBooleanType SerializeImageChannel(const ImageInfo *image_info,
300 Image *image,unsigned char **pixels,size_t *length)
305 register const Quantum
311 register unsigned char
325 assert(image != (Image *) NULL);
326 assert(image->signature == MagickSignature);
327 if (image->debug != MagickFalse)
328 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
330 pack=IsImageMonochrome(image,&image->exception) == MagickFalse ? 1UL : 8UL;
331 padded_columns=((image->columns+pack-1)/pack)*pack;
332 *length=(size_t) padded_columns*image->rows/pack;
333 *pixels=(unsigned char *) AcquireQuantumMemory(*length,sizeof(**pixels));
334 if (*pixels == (unsigned char *) NULL)
335 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
337 for (y=0; y < (ssize_t) image->rows; y++)
339 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
340 if (p == (const Quantum *) NULL)
343 for (x=0; x < (ssize_t) image->columns; x++)
345 *q++=ScaleQuantumToChar(GetPixelIntensity(image,p));
346 p+=GetPixelComponents(image);
351 for (x=0; x < (ssize_t) padded_columns; x++)
353 bit=(unsigned char) 0x00;
354 if (x < (ssize_t) image->columns)
355 bit=(unsigned char) (GetPixelIntensity(image,p) == (Quantum)
356 TransparentAlpha ? 0x01 : 0x00);
357 code=(code << 1)+bit;
358 if (((x+1) % pack) == 0)
363 p+=GetPixelComponents(image);
366 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
368 if (status == MagickFalse)
371 if (status == MagickFalse)
372 *pixels=(unsigned char *) RelinquishMagickMemory(*pixels);
376 static MagickBooleanType SerializeImageIndexes(const ImageInfo *image_info,
377 Image *image,unsigned char **pixels,size_t *length)
382 register const Quantum
388 register unsigned char
394 assert(image != (Image *) NULL);
395 assert(image->signature == MagickSignature);
396 if (image->debug != MagickFalse)
397 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
399 *length=(size_t) image->columns*image->rows;
400 *pixels=(unsigned char *) AcquireQuantumMemory(*length,sizeof(**pixels));
401 if (*pixels == (unsigned char *) NULL)
402 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
404 for (y=0; y < (ssize_t) image->rows; y++)
406 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
407 if (p == (const Quantum *) NULL)
409 for (x=0; x < (ssize_t) image->columns; x++)
411 *q++=(unsigned char) GetPixelIndex(image,p);
412 p+=GetPixelComponents(image);
414 if (image->previous == (Image *) NULL)
416 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
418 if (status == MagickFalse)
422 if (status == MagickFalse)
423 *pixels=(unsigned char *) RelinquishMagickMemory(*pixels);
427 static MagickBooleanType WritePS3MaskImage(const ImageInfo *image_info,
428 Image *image,const CompressionType compression)
431 buffer[MaxTextExtent];
453 assert(image_info != (ImageInfo *) NULL);
454 assert(image_info->signature == MagickSignature);
455 assert(image != (Image *) NULL);
456 assert(image->signature == MagickSignature);
457 if (image->debug != MagickFalse)
458 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
459 assert(image->matte != MagickFalse);
462 Note BeginData DSC comment for update later.
464 start=TellBlob(image);
465 (void) FormatLocaleString(buffer,MaxTextExtent,
466 "%%%%BeginData:%13ld %s Bytes\n",0L,compression == NoCompression ?
468 (void) WriteBlobString(image,buffer);
469 stop=TellBlob(image);
471 Only lossless compressions for the mask.
478 (void) FormatLocaleString(buffer,MaxTextExtent,
479 "currentfile %.20g %.20g "PS3_NoCompression" ByteStreamDecodeFilter\n",
480 (double) image->columns,(double) image->rows);
484 case Group4Compression:
486 (void) FormatLocaleString(buffer,MaxTextExtent,
487 "currentfile %.20g %.20g "PS3_FaxCompression" ByteStreamDecodeFilter\n",
488 (double) image->columns,(double) image->rows);
493 (void) FormatLocaleString(buffer,MaxTextExtent,
494 "currentfile %.20g %.20g "PS3_LZWCompression" ByteStreamDecodeFilter\n",
495 (double) image->columns,(double) image->rows);
500 (void) FormatLocaleString(buffer,MaxTextExtent,
501 "currentfile %.20g %.20g "PS3_RLECompression" ByteStreamDecodeFilter\n",
502 (double) image->columns,(double) image->rows);
507 (void) FormatLocaleString(buffer,MaxTextExtent,
508 "currentfile %.20g %.20g "PS3_ZipCompression" ByteStreamDecodeFilter\n",
509 (double) image->columns,(double) image->rows);
513 (void) WriteBlobString(image,buffer);
514 (void) WriteBlobString(image,"/ReusableStreamDecode filter\n");
515 mask_image=CloneImage(image,0,0,MagickTrue,&image->exception);
516 if (mask_image == (Image *) NULL)
517 ThrowWriterException(CoderError,image->exception.reason);
518 status=SeparateImageChannel(mask_image,OpacityChannel);
519 if (status == MagickFalse)
521 mask_image=DestroyImage(mask_image);
524 (void) SetImageType(mask_image,BilevelType);
525 (void) SetImageType(mask_image,PaletteType);
526 mask_image->matte=MagickFalse;
527 pixels=(unsigned char *) NULL;
534 status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
535 if (status == MagickFalse)
537 Ascii85Initialize(image);
538 for (i=0; i < (ssize_t) length; i++)
539 Ascii85Encode(image,pixels[i]);
541 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
545 case Group4Compression:
547 if ((compression == FaxCompression) ||
548 (LocaleCompare(CCITTParam,"0") == 0))
549 status=HuffmanEncodeImage(image_info,image,mask_image);
551 status=Huffman2DEncodeImage(image_info,image,mask_image);
556 status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
557 if (status == MagickFalse)
559 status=LZWEncodeImage(image,length,pixels);
560 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
565 status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
566 if (status == MagickFalse)
568 status=PackbitsEncodeImage(image,length,pixels);
569 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
574 status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
575 if (status == MagickFalse)
577 status=ZLIBEncodeImage(image,length,pixels);
578 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
582 mask_image=DestroyImage(mask_image);
583 (void) WriteBlobByte(image,'\n');
584 length=(size_t) (TellBlob(image)-stop);
585 stop=TellBlob(image);
586 offset=SeekBlob(image,start,SEEK_SET);
588 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
589 (void) FormatLocaleString(buffer,MaxTextExtent,
590 "%%%%BeginData:%13ld %s Bytes\n",(long) length,
591 compression == NoCompression ? "ASCII" : "BINARY");
592 (void) WriteBlobString(image,buffer);
593 offset=SeekBlob(image,stop,SEEK_SET);
595 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
596 (void) WriteBlobString(image,"%%EndData\n");
597 (void) WriteBlobString(image, "/mask_stream exch def\n");
601 static MagickBooleanType WritePS3Image(const ImageInfo *image_info,Image *image)
606 "/ByteStreamDecodeFilter",
611 " z "PS3_NoCompression" eq { /ASCII85Decode filter } if",
612 " z "PS3_FaxCompression" eq",
619 " /CCITTFaxDecode filter",
621 " z "PS3_JPEGCompression" eq { /DCTDecode filter } if",
622 " z "PS3_LZWCompression" eq { /LZWDecode filter } if",
623 " z "PS3_RLECompression" eq { /RunLengthDecode filter } if",
624 " z "PS3_ZipCompression" eq { /FlateDecode filter } if",
627 "/DirectClassImageDict",
629 " colorspace "PS3_RGBColorspace" eq",
631 " /DeviceRGB setcolorspace",
636 " /BitsPerComponent 8",
637 " /DataSource pixel_stream",
638 " /MultipleDataSources false",
639 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
640 " /Decode [0 1 0 1 0 1]",
644 " /DeviceCMYK setcolorspace",
649 " /BitsPerComponent 8",
650 " /DataSource pixel_stream",
651 " /MultipleDataSources false",
652 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
654 " compression "PS3_JPEGCompression" eq",
655 " { [1 0 1 0 1 0 1 0] }",
656 " { [0 1 0 1 0 1 0 1] }",
663 "/PseudoClassImageDict",
665 " % Colors in colormap image.",
666 " currentfile buffer readline pop",
667 " token pop /colors exch def pop",
670 " % Depth of grayscale image.",
671 " currentfile buffer readline pop",
672 " token pop /bits exch def pop",
673 " /DeviceGray setcolorspace",
678 " /BitsPerComponent bits",
680 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
681 " /DataSource pixel_stream",
686 " /colormap colors 3 mul string def",
687 " compression "PS3_NoCompression" eq",
688 " { currentfile /ASCII85Decode filter colormap readstring pop pop }",
689 " { currentfile colormap readstring pop pop }",
691 " [ /Indexed /DeviceRGB colors 1 sub colormap ] setcolorspace",
696 " /BitsPerComponent 8",
698 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
699 " /DataSource pixel_stream",
705 "/NonMaskedImageDict",
707 " class "PS3_PseudoClass" eq",
708 " { PseudoClassImageDict }",
709 " { DirectClassImageDict }",
717 " /InterleaveType 3",
718 " /DataDict NonMaskedImageDict",
724 " /BitsPerComponent 1",
725 " /DataSource mask_stream",
726 " /MultipleDataSources false",
727 " /ImageMatrix [ columns 0 0 rows neg 0 rows]",
738 " /buffer 512 string def",
740 " currentfile buffer readline pop",
741 " token pop /x exch def",
742 " token pop /y exch def pop",
744 " % Image size and font size.",
745 " currentfile buffer readline pop",
746 " token pop /x exch def",
747 " token pop /y exch def pop",
748 " currentfile buffer readline pop",
749 " token pop /pointsize exch def pop",
756 " currentfile buffer readline pop",
757 " token pop /clipped exch def pop",
759 " currentfile buffer readline pop",
760 " token pop /sp exch def pop",
761 " % Image pixel size.",
762 " currentfile buffer readline pop",
763 " token pop /columns exch def",
764 " token pop /rows exch def pop",
765 " % Colorspace (RGB/CMYK).",
766 " currentfile buffer readline pop",
767 " token pop /colorspace exch def pop",
769 " currentfile buffer readline pop",
770 " token pop /alpha exch def pop",
772 " currentfile buffer readline pop",
773 " token pop /stencil exch def pop",
774 " % Image class (direct/pseudo).",
775 " currentfile buffer readline pop",
776 " token pop /class exch def pop",
777 " % Compression type.",
778 " currentfile buffer readline pop",
779 " token pop /compression exch def pop",
780 " % Clip and render.",
781 " /pixel_stream currentfile columns rows compression ByteStreamDecodeFilter def",
782 " clipped { ClipImage } if",
783 " alpha stencil not and",
784 " { MaskedImageDict mask_stream resetfile }",
785 " { NonMaskedImageDict }",
787 " stencil { 0 setgray imagemask } { image } ifelse",
788 " sp { showpage } if",
794 buffer[MaxTextExtent],
797 page_geometry[MaxTextExtent];
857 Open output image file.
859 assert(image_info != (const ImageInfo *) NULL);
860 assert(image_info->signature == MagickSignature);
861 assert(image != (Image *) NULL);
862 assert(image->signature == MagickSignature);
863 if (image->debug != MagickFalse)
864 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
865 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
866 if (status == MagickFalse)
868 compression=image->compression;
869 if (image_info->compression != UndefinedCompression)
870 compression=image_info->compression;
874 case Group4Compression:
876 if ((IsImageMonochrome(image,&image->exception) == MagickFalse) ||
877 (image->matte != MagickFalse))
878 compression=RLECompression;
881 #if !defined(MAGICKCORE_JPEG_DELEGATE)
882 case JPEGCompression:
884 compression=RLECompression;
885 (void) ThrowMagickException(&image->exception,GetMagickModule(),
886 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
891 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
894 compression=RLECompression;
895 (void) ThrowMagickException(&image->exception,GetMagickModule(),
896 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZLIB)",
904 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
910 Scale relative to dots-per-inch.
912 delta.x=DefaultResolution;
913 delta.y=DefaultResolution;
914 resolution.x=image->x_resolution;
915 resolution.y=image->y_resolution;
916 if ((resolution.x == 0.0) || (resolution.y == 0.0))
918 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
919 resolution.x=geometry_info.rho;
920 resolution.y=geometry_info.sigma;
921 if ((flags & SigmaValue) == 0)
922 resolution.y=resolution.x;
924 if (image_info->density != (char *) NULL)
926 flags=ParseGeometry(image_info->density,&geometry_info);
927 resolution.x=geometry_info.rho;
928 resolution.y=geometry_info.sigma;
929 if ((flags & SigmaValue) == 0)
930 resolution.y=resolution.x;
932 if (image->units == PixelsPerCentimeterResolution)
934 resolution.x=(size_t) (100.0*2.54*resolution.x+0.5)/100.0;
935 resolution.y=(size_t) (100.0*2.54*resolution.y+0.5)/100.0;
937 SetGeometry(image,&geometry);
938 (void) FormatLocaleString(page_geometry,MaxTextExtent,"%.20gx%.20g",
939 (double) image->columns,(double) image->rows);
940 if (image_info->page != (char *) NULL)
941 (void) CopyMagickString(page_geometry,image_info->page,MaxTextExtent);
943 if ((image->page.width != 0) && (image->page.height != 0))
944 (void) FormatLocaleString(page_geometry,MaxTextExtent,
945 "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,(double)
946 image->page.height,(double) image->page.x,(double) image->page.y);
948 if ((image->gravity != UndefinedGravity) &&
949 (LocaleCompare(image_info->magick,"PS") == 0))
950 (void) CopyMagickString(page_geometry,PSPageGeometry,MaxTextExtent);
951 (void) ConcatenateMagickString(page_geometry,">",MaxTextExtent);
952 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
953 &geometry.width,&geometry.height);
954 scale.x=(double) (geometry.width*delta.x)/resolution.x;
955 geometry.width=(size_t) floor(scale.x+0.5);
956 scale.y=(double) (geometry.height*delta.y)/resolution.y;
957 geometry.height=(size_t) floor(scale.y+0.5);
958 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
959 (void) ParseGravityGeometry(image,page_geometry,&page_info,
961 if (image->gravity != UndefinedGravity)
963 geometry.x=(-page_info.x);
964 geometry.y=(ssize_t) (media_info.height+page_info.y-image->rows);
967 if (image_info->pointsize != 0.0)
968 pointsize=image_info->pointsize;
970 value=GetImageProperty(image,"label");
971 if (value != (const char *) NULL)
972 text_size=(size_t) (MultilineCensus(value)*pointsize+12);
977 Postscript header on the first page.
979 if (LocaleCompare(image_info->magick,"PS3") == 0)
980 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0\n",MaxTextExtent);
982 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0 EPSF-3.0\n",
984 (void) WriteBlobString(image,buffer);
985 (void) FormatLocaleString(buffer,MaxTextExtent,
986 "%%%%Creator: ImageMagick %s\n",MagickLibVersionText);
987 (void) WriteBlobString(image,buffer);
988 (void) FormatLocaleString(buffer,MaxTextExtent,"%%%%Title: %s\n",
990 (void) WriteBlobString(image,buffer);
991 timer=time((time_t *) NULL);
992 (void) FormatMagickTime(timer,MaxTextExtent,date);
993 (void) FormatLocaleString(buffer,MaxTextExtent,
994 "%%%%CreationDate: %s\n",date);
995 (void) WriteBlobString(image,buffer);
996 bounds.x1=(double) geometry.x;
997 bounds.y1=(double) geometry.y;
998 bounds.x2=(double) geometry.x+scale.x;
999 bounds.y2=(double) geometry.y+scale.y+text_size;
1000 if ((image_info->adjoin != MagickFalse) &&
1001 (GetNextImageInList(image) != (Image *) NULL))
1003 (void) WriteBlobString(image,"%%BoundingBox: (atend)\n");
1004 (void) WriteBlobString(image,"%%HiResBoundingBox: (atend)\n");
1008 (void) FormatLocaleString(buffer,MaxTextExtent,
1009 "%%%%BoundingBox: %g %g %g %g\n",floor(bounds.x1+0.5),
1010 floor(bounds.y1+0.5),ceil(bounds.x2-0.5),ceil(bounds.y2-0.5));
1011 (void) WriteBlobString(image,buffer);
1012 (void) FormatLocaleString(buffer,MaxTextExtent,
1013 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,
1014 bounds.y1,bounds.x2,bounds.y2);
1015 (void) WriteBlobString(image,buffer);
1016 if (image->colorspace == CMYKColorspace)
1017 (void) WriteBlobString(image,
1018 "%%DocumentProcessColors: Cyan Magenta Yellow Black\n");
1020 if (IsImageGray(image,&image->exception) != MagickFalse)
1021 (void) WriteBlobString(image,
1022 "%%DocumentProcessColors: Black\n");
1027 value=GetImageProperty(image,"label");
1028 if (value != (const char *) NULL)
1029 (void) WriteBlobString(image,
1030 "%%DocumentNeededResources: font Helvetica\n");
1031 (void) WriteBlobString(image,"%%LanguageLevel: 3\n");
1033 Pages, orientation and order.
1035 if (LocaleCompare(image_info->magick,"PS3") != 0)
1036 (void) WriteBlobString(image,"%%Pages: 1\n");
1039 (void) WriteBlobString(image,"%%Orientation: Portrait\n");
1040 (void) WriteBlobString(image,"%%PageOrder: Ascend\n");
1041 if (image_info->adjoin == MagickFalse)
1042 (void) CopyMagickString(buffer,"%%Pages: 1\n",MaxTextExtent);
1044 (void) FormatLocaleString(buffer,MaxTextExtent,
1045 "%%%%Pages: %.20g\n",(double) GetImageListLength(image));
1046 (void) WriteBlobString(image,buffer);
1048 (void) WriteBlobString(image,"%%EndComments\n");
1050 The static postscript procedures prolog.
1052 (void)WriteBlobString(image,"%%BeginProlog\n");
1053 for (q=PostscriptProlog; *q; q++)
1055 (void) WriteBlobString(image,*q);
1056 (void) WriteBlobByte(image,'\n');
1059 One label line for each line in label string.
1061 value=GetImageProperty(image,"label");
1062 if (value != (const char *) NULL)
1064 (void) WriteBlobString(image,"\n %% Labels.\n /Helvetica "
1065 " findfont pointsize scalefont setfont\n");
1066 for (i=(ssize_t) MultilineCensus(value)-1; i >= 0; i--)
1068 (void) WriteBlobString(image,
1069 " currentfile buffer readline pop token pop\n");
1070 (void) FormatLocaleString(buffer,MaxTextExtent,
1071 " 0 y %g add moveto show pop\n",i*pointsize+12);
1072 (void) WriteBlobString(image,buffer);
1076 The static postscript procedures epilog.
1078 for (q=PostscriptEpilog; *q; q++)
1080 (void) WriteBlobString(image,*q);
1081 (void) WriteBlobByte(image,'\n');
1083 (void)WriteBlobString(image,"%%EndProlog\n");
1085 (void) FormatLocaleString(buffer,MaxTextExtent,"%%%%Page: 1 %.20g\n",
1087 (void) WriteBlobString(image,buffer);
1091 (void) FormatLocaleString(buffer,MaxTextExtent,
1092 "%%%%PageBoundingBox: %.20g %.20g %.20g %.20g\n",(double) geometry.x,
1093 (double) geometry.y,geometry.x+(double) geometry.width,geometry.y+
1094 (double) (geometry.height+text_size));
1095 (void) WriteBlobString(image,buffer);
1097 Page process colors if not RGB.
1099 if (image->colorspace == CMYKColorspace)
1100 (void) WriteBlobString(image,
1101 "%%PageProcessColors: Cyan Magenta Yellow Black\n");
1103 if (IsImageGray(image,&image->exception) != MagickFalse)
1104 (void) WriteBlobString(image,"%%PageProcessColors: Black\n");
1106 Adjust document bounding box to bound page bounding box.
1108 if ((double) geometry.x < bounds.x1)
1109 bounds.x1=(double) geometry.x;
1110 if ((double) geometry.y < bounds.y1)
1111 bounds.y1=(double) geometry.y;
1112 if ((double) (geometry.x+scale.x) > bounds.x2)
1113 bounds.x2=(double) geometry.x+scale.x;
1114 if ((double) (geometry.y+scale.y+text_size) > bounds.y2)
1115 bounds.y2=(double) geometry.y+scale.y+text_size;
1117 Page font resource if there's a label.
1119 value=GetImageProperty(image,"label");
1120 if (value != (const char *) NULL)
1121 (void) WriteBlobString(image,"%%PageResources: font Helvetica\n");
1123 PS clipping path from Photoshop clipping path.
1125 if ((image->clip_mask == (Image *) NULL) ||
1126 (LocaleNCompare("8BIM:",image->clip_mask->magick_filename,5) != 0))
1127 (void) WriteBlobString(image,"/ClipImage {} def\n");
1133 value=GetImageProperty(image,image->clip_mask->magick_filename);
1134 if (value == (const char *) NULL)
1135 return(MagickFalse);
1136 (void) WriteBlobString(image,value);
1137 (void) WriteBlobByte(image,'\n');
1140 Push a dictionary for our own def's if this an EPS.
1142 if (LocaleCompare(image_info->magick,"PS3") != 0)
1143 (void) WriteBlobString(image,"userdict begin\n");
1147 if ((image->matte != MagickFalse) &&
1148 (WritePS3MaskImage(image_info,image,compression) == MagickFalse))
1150 (void) CloseBlob(image);
1151 return(MagickFalse);
1154 Remember position of BeginData comment so we can update it.
1156 start=TellBlob(image);
1157 (void) FormatLocaleString(buffer,MaxTextExtent,
1158 "%%%%BeginData:%13ld %s Bytes\n",0L,
1159 compression == NoCompression ? "ASCII" : "BINARY");
1160 (void) WriteBlobString(image,buffer);
1161 stop=TellBlob(image);
1162 (void) WriteBlobString(image,"DisplayImage\n");
1164 Translate, scale, and font point size.
1166 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n%g %g\n%g\n",
1167 (double) geometry.x,(double) geometry.y,scale.x,scale.y,pointsize);
1168 (void) WriteBlobString(image,buffer);
1172 labels=(char **) NULL;
1173 value=GetImageProperty(image,"label");
1174 if (value != (const char *) NULL)
1175 labels=StringToList(value);
1176 if (labels != (char **) NULL)
1178 for (i=0; labels[i] != (char *) NULL; i++)
1180 if (compression != NoCompression)
1182 for (j=0; labels[i][j] != '\0'; j++)
1183 (void) WriteBlobByte(image,(unsigned char) labels[i][j]);
1184 (void) WriteBlobByte(image,'\n');
1188 (void) WriteBlobString(image,"<~");
1189 Ascii85Initialize(image);
1190 for (j=0; labels[i][j] != '\0'; j++)
1191 Ascii85Encode(image,(unsigned char) labels[i][j]);
1192 Ascii85Flush(image);
1194 labels[i]=DestroyString(labels[i]);
1196 labels=(char **) RelinquishMagickMemory(labels);
1199 Photoshop clipping path active?
1201 if ((image->clip_mask != (Image *) NULL) &&
1202 (LocaleNCompare("8BIM:",image->clip_mask->magick_filename,5) == 0))
1203 (void) WriteBlobString(image,"true\n");
1205 (void) WriteBlobString(image,"false\n");
1207 Showpage for non-EPS.
1209 (void) WriteBlobString(image, LocaleCompare(image_info->magick,"PS3") == 0 ?
1210 "true\n" : "false\n");
1212 Image columns, rows, and color space.
1214 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n%s\n",
1215 (double) image->columns,(double) image->rows,image->colorspace ==
1216 CMYKColorspace ? PS3_CMYKColorspace : PS3_RGBColorspace);
1217 (void) WriteBlobString(image,buffer);
1221 (void) WriteBlobString(image,image->matte != MagickFalse ?
1222 "true\n" : "false\n");
1224 Render with imagemask operator?
1226 option=GetImageOption(image_info,"ps3:imagemask");
1227 (void) WriteBlobString(image,((option != (const char *) NULL) &&
1228 (IsImageMonochrome(image,&image->exception) != MagickFalse)) ?
1229 "true\n" : "false\n");
1233 pixels=(unsigned char *) NULL;
1235 if ((image_info->type != TrueColorType) &&
1236 (image_info->type != TrueColorMatteType) &&
1237 (image_info->type != ColorSeparationType) &&
1238 (image_info->type != ColorSeparationMatteType) &&
1239 (image->colorspace != CMYKColorspace) &&
1240 ((IsImageGray(image,&image->exception) != MagickFalse) ||
1241 (IsImageMonochrome(image,&image->exception) != MagickFalse)))
1246 (void) WriteBlobString(image,PS3_PseudoClass"\n");
1247 switch (compression)
1252 (void) WriteBlobString(image,PS3_NoCompression"\n");
1255 case FaxCompression:
1256 case Group4Compression:
1258 (void) WriteBlobString(image,PS3_FaxCompression"\n");
1261 case JPEGCompression:
1263 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1266 case LZWCompression:
1268 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1271 case RLECompression:
1273 (void) WriteBlobString(image,PS3_RLECompression"\n");
1276 case ZipCompression:
1278 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1283 Number of colors -- 0 for single component non-color mapped data.
1285 (void) WriteBlobString(image,"0\n");
1287 1 bit or 8 bit components?
1289 (void) FormatLocaleString(buffer,MaxTextExtent,"%d\n",
1290 IsImageMonochrome(image,&image->exception) != MagickFalse ? 1 : 8);
1291 (void) WriteBlobString(image,buffer);
1295 if (compression == JPEGCompression)
1296 status=InjectImageBlob(image_info,image,image,"jpeg",
1299 if ((compression == FaxCompression) ||
1300 (compression == Group4Compression))
1302 if (LocaleCompare(CCITTParam,"0") == 0)
1303 status=HuffmanEncodeImage(image_info,image,image);
1305 status=Huffman2DEncodeImage(image_info,image,image);
1309 status=SerializeImageChannel(image_info,image,&pixels,&length);
1310 if (status == MagickFalse)
1312 (void) CloseBlob(image);
1313 return(MagickFalse);
1315 switch (compression)
1320 Ascii85Initialize(image);
1321 for (i=0; i < (ssize_t) length; i++)
1322 Ascii85Encode(image,pixels[i]);
1323 Ascii85Flush(image);
1327 case LZWCompression:
1329 status=LZWEncodeImage(image,length,pixels);
1332 case RLECompression:
1334 status=PackbitsEncodeImage(image,length,pixels);
1337 case ZipCompression:
1339 status=ZLIBEncodeImage(image,length,pixels);
1343 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1347 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1348 (compression == JPEGCompression))
1353 (void) WriteBlobString(image,PS3_DirectClass"\n");
1354 switch (compression)
1359 (void) WriteBlobString(image,PS3_NoCompression"\n");
1362 case RLECompression:
1364 (void) WriteBlobString(image,PS3_RLECompression"\n");
1367 case JPEGCompression:
1369 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1372 case LZWCompression:
1374 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1377 case ZipCompression:
1379 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1386 if (compression == JPEGCompression)
1387 status=InjectImageBlob(image_info,image,image,"jpeg",
1392 Stream based compressions.
1394 status=SerializeImage(image_info,image,&pixels,&length);
1395 if (status == MagickFalse)
1397 (void) CloseBlob(image);
1398 return(MagickFalse);
1400 switch (compression)
1405 Ascii85Initialize(image);
1406 for (i=0; i < (ssize_t) length; i++)
1407 Ascii85Encode(image,pixels[i]);
1408 Ascii85Flush(image);
1412 case RLECompression:
1414 status=PackbitsEncodeImage(image,length,pixels);
1417 case LZWCompression:
1419 status=LZWEncodeImage(image,length,pixels);
1422 case ZipCompression:
1424 status=ZLIBEncodeImage(image,length,pixels);
1428 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1436 (void) WriteBlobString(image,PS3_PseudoClass"\n");
1437 switch (compression)
1442 (void) WriteBlobString(image,PS3_NoCompression"\n");
1445 case JPEGCompression:
1447 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1450 case RLECompression:
1452 (void) WriteBlobString(image,PS3_RLECompression"\n");
1455 case LZWCompression:
1457 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1460 case ZipCompression:
1462 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1467 Number of colors in color map.
1469 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",
1470 (double) image->colors);
1471 (void) WriteBlobString(image,buffer);
1473 Color map - uncompressed.
1475 if ((compression != NoCompression) &&
1476 (compression != UndefinedCompression))
1478 for (i=0; i < (ssize_t) image->colors; i++)
1480 pixel=ScaleQuantumToChar(image->colormap[i].red);
1481 (void) WriteBlobByte(image,(unsigned char) pixel);
1482 pixel=ScaleQuantumToChar(image->colormap[i].green);
1483 (void) WriteBlobByte(image,(unsigned char) pixel);
1484 pixel=ScaleQuantumToChar(image->colormap[i].blue);
1485 (void) WriteBlobByte(image,(unsigned char) pixel);
1490 Ascii85Initialize(image);
1491 for (i=0; i < (ssize_t) image->colors; i++)
1493 pixel=ScaleQuantumToChar(image->colormap[i].red);
1494 Ascii85Encode(image,(unsigned char) pixel);
1495 pixel=ScaleQuantumToChar(image->colormap[i].green);
1496 Ascii85Encode(image,(unsigned char) pixel);
1497 pixel=ScaleQuantumToChar(image->colormap[i].blue);
1498 Ascii85Encode(image,(unsigned char) pixel);
1500 Ascii85Flush(image);
1502 status=SerializeImageIndexes(image_info,image,&pixels,&length);
1503 if (status == MagickFalse)
1505 (void) CloseBlob(image);
1506 return(MagickFalse);
1508 switch (compression)
1513 Ascii85Initialize(image);
1514 for (i=0; i < (ssize_t) length; i++)
1515 Ascii85Encode(image,pixels[i]);
1516 Ascii85Flush(image);
1520 case JPEGCompression:
1522 status=InjectImageBlob(image_info,image,image,"jpeg",
1526 case RLECompression:
1528 status=PackbitsEncodeImage(image,length,pixels);
1531 case LZWCompression:
1533 status=LZWEncodeImage(image,length,pixels);
1536 case ZipCompression:
1538 status=ZLIBEncodeImage(image,length,pixels);
1542 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1544 (void) WriteBlobByte(image,'\n');
1545 if (status == MagickFalse)
1547 (void) CloseBlob(image);
1548 return(MagickFalse);
1551 Update BeginData now that we know the data size.
1553 length=(size_t) (TellBlob(image)-stop);
1554 stop=TellBlob(image);
1555 offset=SeekBlob(image,start,SEEK_SET);
1557 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
1558 (void) FormatLocaleString(buffer,MaxTextExtent,
1559 "%%%%BeginData:%13ld %s Bytes\n",(long) length,
1560 compression == NoCompression ? "ASCII" : "BINARY");
1561 (void) WriteBlobString(image,buffer);
1562 offset=SeekBlob(image,stop,SEEK_SET);
1563 (void) WriteBlobString(image,"%%EndData\n");
1565 End private dictionary if this an EPS.
1567 if (LocaleCompare(image_info->magick,"PS3") != 0)
1568 (void) WriteBlobString(image,"end\n");
1569 (void) WriteBlobString(image,"%%PageTrailer\n");
1570 if (GetNextImageInList(image) == (Image *) NULL)
1572 image=SyncNextImageInList(image);
1573 status=SetImageProgress(image,SaveImagesTag,scene++,
1574 GetImageListLength(image));
1575 if (status == MagickFalse)
1577 } while (image_info->adjoin != MagickFalse);
1578 (void) WriteBlobString(image,"%%Trailer\n");
1581 (void) FormatLocaleString(buffer,MaxTextExtent,
1582 "%%%%BoundingBox: %g %g %g %g\n",floor(bounds.x1+0.5),
1583 floor(bounds.y1+0.5),ceil(bounds.x2-0.5),ceil(bounds.y2-0.5));
1584 (void) WriteBlobString(image,buffer);
1585 (void) FormatLocaleString(buffer,MaxTextExtent,
1586 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,bounds.y1,
1587 bounds.x2,bounds.y2);
1588 (void) WriteBlobString(image,buffer);
1590 (void) WriteBlobString(image,"%%EOF\n");
1591 (void) CloseBlob(image);