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 "magick/studio.h"
44 #include "magick/blob.h"
45 #include "magick/blob-private.h"
46 #include "magick/cache.h"
47 #include "magick/color.h"
48 #include "magick/color-private.h"
49 #include "magick/compress.h"
50 #include "magick/constitute.h"
51 #include "magick/draw.h"
52 #include "magick/exception.h"
53 #include "magick/exception-private.h"
54 #include "magick/geometry.h"
55 #include "magick/image.h"
56 #include "magick/image-private.h"
57 #include "magick/list.h"
58 #include "magick/magick.h"
59 #include "magick/memory_.h"
60 #include "magick/monitor.h"
61 #include "magick/monitor-private.h"
62 #include "magick/option.h"
63 #include "magick/property.h"
64 #include "magick/quantum-private.h"
65 #include "magick/resource_.h"
66 #include "magick/static.h"
67 #include "magick/string_.h"
68 #include "magick/module.h"
69 #include "magick/token.h"
70 #include "magick/utility.h"
71 #include "magick/module.h"
76 #define PS3_NoCompression "0"
77 #define PS3_FaxCompression "1"
78 #define PS3_JPEGCompression "2"
79 #define PS3_LZWCompression "3"
80 #define PS3_RLECompression "4"
81 #define PS3_ZipCompression "5"
83 #define PS3_RGBColorspace "0"
84 #define PS3_CMYKColorspace "1"
86 #define PS3_DirectClass "0"
87 #define PS3_PseudoClass "1"
89 #if defined(MAGICKCORE_TIFF_DELEGATE)
90 #define CCITTParam "-1"
92 #define CCITTParam "0"
98 static MagickBooleanType
99 WritePS3Image(const ImageInfo *,Image *);
102 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106 % R e g i s t e r P S 3 I m a g e %
110 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112 % RegisterPS3Image() adds properties for the PS3 image format to the list of
113 % supported formats. The properties include the image format tag, a method to
114 % read and/or write the format, whether the format supports the saving of more
115 % than one frame to the same file or blob, whether the format supports native
116 % in-memory I/O, and a brief description of the format.
118 % The format of the RegisterPS3Image method is:
120 % size_t RegisterPS3Image(void)
123 ModuleExport size_t RegisterPS3Image(void)
128 entry=SetMagickInfo("EPS3");
129 entry->encoder=(EncodeImageHandler *) WritePS3Image;
130 entry->description=ConstantString("Level III Encapsulated PostScript");
131 entry->module=ConstantString("PS3");
132 (void) RegisterMagickInfo(entry);
133 entry=SetMagickInfo("PS3");
134 entry->encoder=(EncodeImageHandler *) WritePS3Image;
135 entry->description=ConstantString("Level III PostScript");
136 entry->module=ConstantString("PS3");
137 (void) RegisterMagickInfo(entry);
138 return(MagickImageCoderSignature);
142 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
146 % U n r e g i s t e r P S 3 I m a g e %
150 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
152 % UnregisterPS3Image() removes format registrations made by the PS3 module
153 % from the list of supported formats.
155 % The format of the UnregisterPS3Image method is:
157 % UnregisterPS3Image(void)
160 ModuleExport void UnregisterPS3Image(void)
162 (void) UnregisterMagickInfo("EPS3");
163 (void) UnregisterMagickInfo("PS3");
167 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
171 % W r i t e P S 3 I m a g e %
175 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
177 % WritePS3Image() translates an image to encapsulated Postscript Level III
178 % for printing. If the supplied geometry is null, the image is centered on
179 % the Postscript page. Otherwise, the image is positioned as specified by the
182 % The format of the WritePS3Image method is:
184 % MagickBooleanType WritePS3Image(const ImageInfo *image_info,Image *image)
186 % A description of each parameter follows:
188 % o image_info: Specifies a pointer to a ImageInfo structure.
190 % o image: the image.
194 static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
195 Image *image,Image *inject_image)
213 write_info=CloneImageInfo(image_info);
214 (void) CopyMagickString(write_info->filename,"GROUP4:",MaxTextExtent);
215 (void) CopyMagickString(write_info->magick,"GROUP4",MaxTextExtent);
216 group4_image=CloneImage(inject_image,0,0,MagickTrue,&image->exception);
217 if (group4_image == (Image *) NULL)
219 group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
221 group4_image=DestroyImage(group4_image);
222 if (group4 == (unsigned char *) NULL)
224 write_info=DestroyImageInfo(write_info);
225 if (WriteBlob(image,length,group4) != (ssize_t) length)
227 group4=(unsigned char *) RelinquishMagickMemory(group4);
232 static MagickBooleanType SerializeImage(const ImageInfo *image_info,
233 Image *image,unsigned char **pixels,size_t *length)
241 register const IndexPacket
244 register const PixelPacket
250 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)*
259 (size_t) 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 PixelPacket *) NULL)
269 indexes=GetVirtualIndexQueue(image);
270 if (image->colorspace != CMYKColorspace)
271 for (x=0; x < (ssize_t) image->columns; x++)
273 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
274 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
275 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
279 for (x=0; x < (ssize_t) image->columns; x++)
281 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
282 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
283 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
284 *q++=ScaleQuantumToChar(indexes[x]);
287 if (image->previous == (Image *) NULL)
289 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
291 if (status == MagickFalse)
295 if (status == MagickFalse)
296 *pixels=(unsigned char *) RelinquishMagickMemory(*pixels);
300 static MagickBooleanType SerializeImageChannel(const ImageInfo *image_info,
301 Image *image,unsigned char **pixels,size_t *length)
309 register const PixelPacket
315 register unsigned char
326 assert(image != (Image *) NULL);
327 assert(image->signature == MagickSignature);
328 if (image->debug != MagickFalse)
329 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
331 pack=IsMonochromeImage(image,&image->exception) == MagickFalse ? 1UL : 8UL;
332 padded_columns=((image->columns+pack-1)/pack)*pack;
333 *length=(size_t) padded_columns*image->rows/pack;
334 *pixels=(unsigned char *) AcquireQuantumMemory(*length,sizeof(**pixels));
335 if (*pixels == (unsigned char *) NULL)
336 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
338 for (y=0; y < (ssize_t) image->rows; y++)
340 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
341 if (p == (const PixelPacket *) NULL)
344 for (x=0; x < (ssize_t) image->columns; x++)
346 *q++=ScaleQuantumToChar(PixelIntensityToQuantum(p));
352 for (x=0; x < (ssize_t) padded_columns; x++)
354 bit=(unsigned char) 0x00;
355 if (x < (ssize_t) image->columns)
356 bit=(unsigned char) (PixelIntensityToQuantum(p) ==
357 (Quantum) TransparentOpacity ? 0x01 : 0x00);
358 code=(code << 1)+bit;
359 if (((x+1) % pack) == 0)
367 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
369 if (status == MagickFalse)
372 if (status == MagickFalse)
373 *pixels=(unsigned char *) RelinquishMagickMemory(*pixels);
377 static MagickBooleanType SerializeImageIndexes(const ImageInfo *image_info,
378 Image *image,unsigned char **pixels,size_t *length)
386 register const IndexPacket
389 register const PixelPacket
395 register unsigned char
398 assert(image != (Image *) NULL);
399 assert(image->signature == MagickSignature);
400 if (image->debug != MagickFalse)
401 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
403 *length=(size_t) image->columns*image->rows;
404 *pixels=(unsigned char *) AcquireQuantumMemory(*length,sizeof(**pixels));
405 if (*pixels == (unsigned char *) NULL)
406 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
408 for (y=0; y < (ssize_t) image->rows; y++)
410 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
411 if (p == (const PixelPacket *) NULL)
413 indexes=GetVirtualIndexQueue(image);
414 for (x=0; x < (ssize_t) image->columns; x++)
415 *q++=(unsigned char) indexes[x];
416 if (image->previous == (Image *) NULL)
418 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
420 if (status == MagickFalse)
424 if (status == MagickFalse)
425 *pixels=(unsigned char *) RelinquishMagickMemory(*pixels);
429 static MagickBooleanType WritePS3MaskImage(const ImageInfo *image_info,
430 Image *image,const CompressionType compression)
433 buffer[MaxTextExtent];
455 assert(image_info != (ImageInfo *) NULL);
456 assert(image_info->signature == MagickSignature);
457 assert(image != (Image *) NULL);
458 assert(image->signature == MagickSignature);
459 if (image->debug != MagickFalse)
460 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
461 assert(image->matte != MagickFalse);
464 Note BeginData DSC comment for update later.
466 start=TellBlob(image);
467 (void) FormatMagickString(buffer,MaxTextExtent,
468 "%%%%BeginData:%13ld %s Bytes\n",0L,
469 compression == NoCompression ? "ASCII" : "BINARY");
470 (void) WriteBlobString(image,buffer);
471 stop=TellBlob(image);
473 Only lossless compressions for the mask.
480 (void) FormatMagickString(buffer,MaxTextExtent,
481 "currentfile %.20g %.20g "PS3_NoCompression" ByteStreamDecodeFilter\n",
482 (double) image->columns,(double) image->rows);
486 case Group4Compression:
488 (void) FormatMagickString(buffer,MaxTextExtent,
489 "currentfile %.20g %.20g "PS3_FaxCompression" ByteStreamDecodeFilter\n",
490 (double) image->columns,(double) image->rows);
495 (void) FormatMagickString(buffer,MaxTextExtent,
496 "currentfile %.20g %.20g "PS3_LZWCompression" ByteStreamDecodeFilter\n",
497 (double) image->columns,(double) image->rows);
502 (void) FormatMagickString(buffer,MaxTextExtent,
503 "currentfile %.20g %.20g "PS3_RLECompression" ByteStreamDecodeFilter\n",
504 (double) image->columns,(double) image->rows);
509 (void) FormatMagickString(buffer,MaxTextExtent,
510 "currentfile %.20g %.20g "PS3_ZipCompression" ByteStreamDecodeFilter\n",
511 (double) image->columns,(double) image->rows);
515 (void) WriteBlobString(image,buffer);
516 (void) WriteBlobString(image,"/ReusableStreamDecode filter\n");
517 mask_image=CloneImage(image,0,0,MagickTrue,&image->exception);
518 if (mask_image == (Image *) NULL)
519 ThrowWriterException(CoderError,image->exception.reason);
520 status=SeparateImageChannel(mask_image,OpacityChannel);
521 if (status == MagickFalse)
523 mask_image=DestroyImage(mask_image);
526 (void) SetImageType(mask_image,BilevelType);
527 (void) SetImageType(mask_image,PaletteType);
528 mask_image->matte=MagickFalse;
529 pixels=(unsigned char *) NULL;
536 status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
537 if (status == MagickFalse)
539 Ascii85Initialize(image);
540 for (i=0; i < (ssize_t) length; i++)
541 Ascii85Encode(image,pixels[i]);
543 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
547 case Group4Compression:
549 if ((compression == FaxCompression) ||
550 (LocaleCompare(CCITTParam,"0") == 0))
551 status=HuffmanEncodeImage(image_info,image,mask_image);
553 status=Huffman2DEncodeImage(image_info,image,mask_image);
558 status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
559 if (status == MagickFalse)
561 status=LZWEncodeImage(image,length,pixels);
562 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
567 status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
568 if (status == MagickFalse)
570 status=PackbitsEncodeImage(image,length,pixels);
571 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
576 status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
577 if (status == MagickFalse)
579 status=ZLIBEncodeImage(image,length,pixels);
580 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
584 mask_image=DestroyImage(mask_image);
585 (void) WriteBlobByte(image,'\n');
586 length=(size_t) (TellBlob(image)-stop);
587 stop=TellBlob(image);
588 offset=SeekBlob(image,start,SEEK_SET);
590 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
591 (void) FormatMagickString(buffer,MaxTextExtent,
592 "%%%%BeginData:%13ld %s Bytes\n",(long) length,
593 compression == NoCompression ? "ASCII" : "BINARY");
594 (void) WriteBlobString(image,buffer);
595 offset=SeekBlob(image,stop,SEEK_SET);
597 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
598 (void) WriteBlobString(image,"%%EndData\n");
599 (void) WriteBlobString(image, "/mask_stream exch def\n");
603 static MagickBooleanType WritePS3Image(const ImageInfo *image_info,Image *image)
608 "/ByteStreamDecodeFilter",
613 " z "PS3_NoCompression" eq { /ASCII85Decode filter } if",
614 " z "PS3_FaxCompression" eq",
621 " /CCITTFaxDecode filter",
623 " z "PS3_JPEGCompression" eq { /DCTDecode filter } if",
624 " z "PS3_LZWCompression" eq { /LZWDecode filter } if",
625 " z "PS3_RLECompression" eq { /RunLengthDecode filter } if",
626 " z "PS3_ZipCompression" eq { /FlateDecode filter } if",
629 "/DirectClassImageDict",
631 " colorspace "PS3_RGBColorspace" eq",
633 " /DeviceRGB setcolorspace",
638 " /BitsPerComponent 8",
639 " /DataSource pixel_stream",
640 " /MultipleDataSources false",
641 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
642 " /Decode [0 1 0 1 0 1]",
646 " /DeviceCMYK setcolorspace",
651 " /BitsPerComponent 8",
652 " /DataSource pixel_stream",
653 " /MultipleDataSources false",
654 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
656 " compression "PS3_JPEGCompression" eq",
657 " { [1 0 1 0 1 0 1 0] }",
658 " { [0 1 0 1 0 1 0 1] }",
665 "/PseudoClassImageDict",
667 " % Colors in colormap image.",
668 " currentfile buffer readline pop",
669 " token pop /colors exch def pop",
672 " % Depth of grayscale image.",
673 " currentfile buffer readline pop",
674 " token pop /bits exch def pop",
675 " /DeviceGray setcolorspace",
680 " /BitsPerComponent bits",
682 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
683 " /DataSource pixel_stream",
688 " /colormap colors 3 mul string def",
689 " compression "PS3_NoCompression" eq",
690 " { currentfile /ASCII85Decode filter colormap readstring pop pop }",
691 " { currentfile colormap readstring pop pop }",
693 " [ /Indexed /DeviceRGB colors 1 sub colormap ] setcolorspace",
698 " /BitsPerComponent 8",
700 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
701 " /DataSource pixel_stream",
707 "/NonMaskedImageDict",
709 " class "PS3_PseudoClass" eq",
710 " { PseudoClassImageDict }",
711 " { DirectClassImageDict }",
719 " /InterleaveType 3",
720 " /DataDict NonMaskedImageDict",
726 " /BitsPerComponent 1",
727 " /DataSource mask_stream",
728 " /MultipleDataSources false",
729 " /ImageMatrix [ columns 0 0 rows neg 0 rows]",
740 " /buffer 512 string def",
742 " currentfile buffer readline pop",
743 " token pop /x exch def",
744 " token pop /y exch def pop",
746 " % Image size and font size.",
747 " currentfile buffer readline pop",
748 " token pop /x exch def",
749 " token pop /y exch def pop",
750 " currentfile buffer readline pop",
751 " token pop /pointsize exch def pop",
758 " currentfile buffer readline pop",
759 " token pop /clipped exch def pop",
761 " currentfile buffer readline pop",
762 " token pop /sp exch def pop",
763 " % Image pixel size.",
764 " currentfile buffer readline pop",
765 " token pop /columns exch def",
766 " token pop /rows exch def pop",
767 " % Colorspace (RGB/CMYK).",
768 " currentfile buffer readline pop",
769 " token pop /colorspace exch def pop",
771 " currentfile buffer readline pop",
772 " token pop /alpha exch def pop",
774 " currentfile buffer readline pop",
775 " token pop /stencil exch def pop",
776 " % Image class (direct/pseudo).",
777 " currentfile buffer readline pop",
778 " token pop /class exch def pop",
779 " % Compression type.",
780 " currentfile buffer readline pop",
781 " token pop /compression exch def pop",
782 " % Clip and render.",
783 " /pixel_stream currentfile columns rows compression ByteStreamDecodeFilter def",
784 " clipped { ClipImage } if",
785 " alpha stencil not and",
786 " { MaskedImageDict mask_stream resetfile }",
787 " { NonMaskedImageDict }",
789 " stencil { 0 setgray imagemask } { image } ifelse",
790 " sp { showpage } if",
796 buffer[MaxTextExtent],
799 page_geometry[MaxTextExtent];
861 Open output image file.
863 assert(image_info != (const ImageInfo *) NULL);
864 assert(image_info->signature == MagickSignature);
865 assert(image != (Image *) NULL);
866 assert(image->signature == MagickSignature);
867 if (image->debug != MagickFalse)
868 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
869 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
870 if (status == MagickFalse)
872 compression=image->compression;
873 if (image_info->compression != UndefinedCompression)
874 compression=image_info->compression;
878 case Group4Compression:
880 if ((IsMonochromeImage(image,&image->exception) == MagickFalse) ||
881 (image->matte != MagickFalse))
882 compression=RLECompression;
885 #if !defined(MAGICKCORE_JPEG_DELEGATE)
886 case JPEGCompression:
888 compression=RLECompression;
889 (void) ThrowMagickException(&image->exception,GetMagickModule(),
890 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
895 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
898 compression=RLECompression;
899 (void) ThrowMagickException(&image->exception,GetMagickModule(),
900 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZLIB)",
908 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
914 Scale relative to dots-per-inch.
916 delta.x=DefaultResolution;
917 delta.y=DefaultResolution;
918 resolution.x=image->x_resolution;
919 resolution.y=image->y_resolution;
920 if ((resolution.x == 0.0) || (resolution.y == 0.0))
922 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
923 resolution.x=geometry_info.rho;
924 resolution.y=geometry_info.sigma;
925 if ((flags & SigmaValue) == 0)
926 resolution.y=resolution.x;
928 if (image_info->density != (char *) NULL)
930 flags=ParseGeometry(image_info->density,&geometry_info);
931 resolution.x=geometry_info.rho;
932 resolution.y=geometry_info.sigma;
933 if ((flags & SigmaValue) == 0)
934 resolution.y=resolution.x;
936 if (image->units == PixelsPerCentimeterResolution)
938 resolution.x=(size_t) (100.0*2.54*resolution.x+0.5)/100.0;
939 resolution.y=(size_t) (100.0*2.54*resolution.y+0.5)/100.0;
941 SetGeometry(image,&geometry);
942 (void) FormatMagickString(page_geometry,MaxTextExtent,"%.20gx%.20g",
943 (double) image->columns,(double) image->rows);
944 if (image_info->page != (char *) NULL)
945 (void) CopyMagickString(page_geometry,image_info->page,MaxTextExtent);
947 if ((image->page.width != 0) && (image->page.height != 0))
948 (void) FormatMagickString(page_geometry,MaxTextExtent,
949 "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,(double)
950 image->page.height,(double) image->page.x,(double) image->page.y);
952 if ((image->gravity != UndefinedGravity) &&
953 (LocaleCompare(image_info->magick,"PS") == 0))
954 (void) CopyMagickString(page_geometry,PSPageGeometry,MaxTextExtent);
955 (void) ConcatenateMagickString(page_geometry,">",MaxTextExtent);
956 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
957 &geometry.width,&geometry.height);
958 scale.x=(double) (geometry.width*delta.x)/resolution.x;
959 geometry.width=(size_t) floor(scale.x+0.5);
960 scale.y=(double) (geometry.height*delta.y)/resolution.y;
961 geometry.height=(size_t) floor(scale.y+0.5);
962 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
963 (void) ParseGravityGeometry(image,page_geometry,&page_info,
965 if (image->gravity != UndefinedGravity)
967 geometry.x=(-page_info.x);
968 geometry.y=(ssize_t) (media_info.height+page_info.y-image->rows);
971 if (image_info->pointsize != 0.0)
972 pointsize=image_info->pointsize;
974 value=GetImageProperty(image,"label");
975 if (value != (const char *) NULL)
976 text_size=(size_t) (MultilineCensus(value)*pointsize+12);
981 Postscript header on the first page.
983 if (LocaleCompare(image_info->magick,"PS3") == 0)
984 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0\n",MaxTextExtent);
986 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0 EPSF-3.0\n",
988 (void) WriteBlobString(image,buffer);
989 (void) FormatMagickString(buffer,MaxTextExtent,
990 "%%%%Creator: ImageMagick %s\n",MagickLibVersionText);
991 (void) WriteBlobString(image,buffer);
992 (void) FormatMagickString(buffer,MaxTextExtent,"%%%%Title: %s\n",
994 (void) WriteBlobString(image,buffer);
995 timer=time((time_t *) NULL);
996 (void) FormatMagickTime(timer,MaxTextExtent,date);
997 (void) FormatMagickString(buffer,MaxTextExtent,
998 "%%%%CreationDate: %s\n",date);
999 (void) WriteBlobString(image,buffer);
1000 bounds.x1=(double) geometry.x;
1001 bounds.y1=(double) geometry.y;
1002 bounds.x2=(double) geometry.x+scale.x;
1003 bounds.y2=(double) geometry.y+scale.y+text_size;
1004 if ((image_info->adjoin != MagickFalse) &&
1005 (GetNextImageInList(image) != (Image *) NULL))
1007 (void) WriteBlobString(image,"%%BoundingBox: (atend)\n");
1008 (void) WriteBlobString(image,"%%HiResBoundingBox: (atend)\n");
1012 (void) FormatMagickString(buffer,MaxTextExtent,
1013 "%%%%BoundingBox: %g %g %g %g\n",floor(bounds.x1+0.5),
1014 floor(bounds.y1+0.5),ceil(bounds.x2-0.5),ceil(bounds.y2-0.5));
1015 (void) WriteBlobString(image,buffer);
1016 (void) FormatMagickString(buffer,MaxTextExtent,
1017 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,
1018 bounds.y1,bounds.x2,bounds.y2);
1019 (void) WriteBlobString(image,buffer);
1020 if (image->colorspace == CMYKColorspace)
1021 (void) WriteBlobString(image,
1022 "%%DocumentProcessColors: Cyan Magenta Yellow Black\n");
1024 if (IsGrayImage(image,&image->exception) != MagickFalse)
1025 (void) WriteBlobString(image,
1026 "%%DocumentProcessColors: Black\n");
1031 value=GetImageProperty(image,"label");
1032 if (value != (const char *) NULL)
1033 (void) WriteBlobString(image,
1034 "%%DocumentNeededResources: font Helvetica\n");
1035 (void) WriteBlobString(image,"%%LanguageLevel: 3\n");
1037 Pages, orientation and order.
1039 if (LocaleCompare(image_info->magick,"PS3") != 0)
1040 (void) WriteBlobString(image,"%%Pages: 1\n");
1043 (void) WriteBlobString(image,"%%Orientation: Portrait\n");
1044 (void) WriteBlobString(image,"%%PageOrder: Ascend\n");
1045 if (image_info->adjoin == MagickFalse)
1046 (void) CopyMagickString(buffer,"%%Pages: 1\n",MaxTextExtent);
1048 (void) FormatMagickString(buffer,MaxTextExtent,
1049 "%%%%Pages: %.20g\n",(double) GetImageListLength(image));
1050 (void) WriteBlobString(image,buffer);
1052 (void) WriteBlobString(image,"%%EndComments\n");
1054 The static postscript procedures prolog.
1056 (void)WriteBlobString(image,"%%BeginProlog\n");
1057 for (q=PostscriptProlog; *q; q++)
1059 (void) WriteBlobString(image,*q);
1060 (void) WriteBlobByte(image,'\n');
1063 One label line for each line in label string.
1065 value=GetImageProperty(image,"label");
1066 if (value != (const char *) NULL)
1068 (void) WriteBlobString(image,"\n %% Labels.\n /Helvetica "
1069 " findfont pointsize scalefont setfont\n");
1070 for (i=(ssize_t) MultilineCensus(value)-1; i >= 0; i--)
1072 (void) WriteBlobString(image,
1073 " currentfile buffer readline pop token pop\n");
1074 (void) FormatMagickString(buffer,MaxTextExtent,
1075 " 0 y %g add moveto show pop\n",i*pointsize+12);
1076 (void) WriteBlobString(image,buffer);
1080 The static postscript procedures epilog.
1082 for (q=PostscriptEpilog; *q; q++)
1084 (void) WriteBlobString(image,*q);
1085 (void) WriteBlobByte(image,'\n');
1087 (void)WriteBlobString(image,"%%EndProlog\n");
1089 (void) FormatMagickString(buffer,MaxTextExtent,"%%%%Page: 1 %.20g\n",
1091 (void) WriteBlobString(image,buffer);
1095 (void) FormatMagickString(buffer,MaxTextExtent,
1096 "%%%%PageBoundingBox: %.20g %.20g %.20g %.20g\n",(double) geometry.x,
1097 (double) geometry.y,geometry.x+(double) geometry.width,geometry.y+
1098 (double) (geometry.height+text_size));
1099 (void) WriteBlobString(image,buffer);
1101 Page process colors if not RGB.
1103 if (image->colorspace == CMYKColorspace)
1104 (void) WriteBlobString(image,
1105 "%%PageProcessColors: Cyan Magenta Yellow Black\n");
1107 if (IsGrayImage(image,&image->exception) != MagickFalse)
1108 (void) WriteBlobString(image,"%%PageProcessColors: Black\n");
1110 Adjust document bounding box to bound page bounding box.
1112 if ((double) geometry.x < bounds.x1)
1113 bounds.x1=(double) geometry.x;
1114 if ((double) geometry.y < bounds.y1)
1115 bounds.y1=(double) geometry.y;
1116 if ((double) (geometry.x+scale.x) > bounds.x2)
1117 bounds.x2=(double) geometry.x+scale.x;
1118 if ((double) (geometry.y+scale.y+text_size) > bounds.y2)
1119 bounds.y2=(double) geometry.y+scale.y+text_size;
1121 Page font resource if there's a label.
1123 value=GetImageProperty(image,"label");
1124 if (value != (const char *) NULL)
1125 (void) WriteBlobString(image,"%%PageResources: font Helvetica\n");
1127 PS clipping path from Photoshop clipping path.
1129 if ((image->clip_mask == (Image *) NULL) ||
1130 (LocaleNCompare("8BIM:",image->clip_mask->magick_filename,5) != 0))
1131 (void) WriteBlobString(image,"/ClipImage {} def\n");
1137 value=GetImageProperty(image,image->clip_mask->magick_filename);
1138 if (value == (const char *) NULL)
1139 return(MagickFalse);
1140 (void) WriteBlobString(image,value);
1141 (void) WriteBlobByte(image,'\n');
1144 Push a dictionary for our own def's if this an EPS.
1146 if (LocaleCompare(image_info->magick,"PS3") != 0)
1147 (void) WriteBlobString(image,"userdict begin\n");
1151 if ((image->matte != MagickFalse) &&
1152 (WritePS3MaskImage(image_info,image,compression) == MagickFalse))
1154 (void) CloseBlob(image);
1155 return(MagickFalse);
1158 Remember position of BeginData comment so we can update it.
1160 start=TellBlob(image);
1161 (void) FormatMagickString(buffer,MaxTextExtent,
1162 "%%%%BeginData:%13ld %s Bytes\n",0L,
1163 compression == NoCompression ? "ASCII" : "BINARY");
1164 (void) WriteBlobString(image,buffer);
1165 stop=TellBlob(image);
1166 (void) WriteBlobString(image,"DisplayImage\n");
1168 Translate, scale, and font point size.
1170 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g %.20g\n%g %g\n%g\n",
1171 (double) geometry.x,(double) geometry.y,scale.x,scale.y,pointsize);
1172 (void) WriteBlobString(image,buffer);
1176 labels=(char **) NULL;
1177 value=GetImageProperty(image,"label");
1178 if (value != (const char *) NULL)
1179 labels=StringToList(value);
1180 if (labels != (char **) NULL)
1182 for (i=0; labels[i] != (char *) NULL; i++)
1184 if (compression != NoCompression)
1186 for (j=0; labels[i][j] != '\0'; j++)
1187 (void) WriteBlobByte(image,(unsigned char) labels[i][j]);
1188 (void) WriteBlobByte(image,'\n');
1192 (void) WriteBlobString(image,"<~");
1193 Ascii85Initialize(image);
1194 for (j=0; labels[i][j] != '\0'; j++)
1195 Ascii85Encode(image,(unsigned char) labels[i][j]);
1196 Ascii85Flush(image);
1198 labels[i]=DestroyString(labels[i]);
1200 labels=(char **) RelinquishMagickMemory(labels);
1203 Photoshop clipping path active?
1205 if ((image->clip_mask != (Image *) NULL) &&
1206 (LocaleNCompare("8BIM:",image->clip_mask->magick_filename,5) == 0))
1207 (void) WriteBlobString(image,"true\n");
1209 (void) WriteBlobString(image,"false\n");
1211 Showpage for non-EPS.
1213 (void) WriteBlobString(image, LocaleCompare(image_info->magick,"PS3") == 0 ?
1214 "true\n" : "false\n");
1216 Image columns, rows, and color space.
1218 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g %.20g\n%s\n",
1219 (double) image->columns,(double) image->rows,
1220 image->colorspace == CMYKColorspace ? PS3_CMYKColorspace :
1222 (void) WriteBlobString(image,buffer);
1226 (void) WriteBlobString(image,image->matte != MagickFalse ?
1227 "true\n" : "false\n");
1229 Render with imagemask operator?
1231 option=GetImageOption(image_info,"ps3:imagemask");
1232 (void) WriteBlobString(image,((option != (const char *) NULL) &&
1233 (IsMonochromeImage(image,&image->exception) != MagickFalse)) ?
1234 "true\n" : "false\n");
1238 pixels=(unsigned char *) NULL;
1240 if ((image_info->type != TrueColorType) &&
1241 (image_info->type != TrueColorMatteType) &&
1242 (image_info->type != ColorSeparationType) &&
1243 (image_info->type != ColorSeparationMatteType) &&
1244 (image->colorspace != CMYKColorspace) &&
1245 ((IsGrayImage(image,&image->exception) != MagickFalse) ||
1246 (IsMonochromeImage(image,&image->exception) != MagickFalse)))
1251 (void) WriteBlobString(image,PS3_PseudoClass"\n");
1252 switch (compression)
1257 (void) WriteBlobString(image,PS3_NoCompression"\n");
1260 case FaxCompression:
1261 case Group4Compression:
1263 (void) WriteBlobString(image,PS3_FaxCompression"\n");
1266 case JPEGCompression:
1268 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1271 case LZWCompression:
1273 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1276 case RLECompression:
1278 (void) WriteBlobString(image,PS3_RLECompression"\n");
1281 case ZipCompression:
1283 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1288 Number of colors -- 0 for single component non-color mapped data.
1290 (void) WriteBlobString(image,"0\n");
1292 1 bit or 8 bit components?
1294 (void) FormatMagickString(buffer,MaxTextExtent,"%d\n",
1295 IsMonochromeImage(image,&image->exception) != MagickFalse ? 1 : 8);
1296 (void) WriteBlobString(image,buffer);
1300 if (compression == JPEGCompression)
1301 status=InjectImageBlob(image_info,image,image,"jpeg",
1304 if ((compression == FaxCompression) ||
1305 (compression == Group4Compression))
1307 if (LocaleCompare(CCITTParam,"0") == 0)
1308 status=HuffmanEncodeImage(image_info,image,image);
1310 status=Huffman2DEncodeImage(image_info,image,image);
1314 status=SerializeImageChannel(image_info,image,&pixels,&length);
1315 if (status == MagickFalse)
1317 (void) CloseBlob(image);
1318 return(MagickFalse);
1320 switch (compression)
1325 Ascii85Initialize(image);
1326 for (i=0; i < (ssize_t) length; i++)
1327 Ascii85Encode(image,pixels[i]);
1328 Ascii85Flush(image);
1332 case LZWCompression:
1334 status=LZWEncodeImage(image,length,pixels);
1337 case RLECompression:
1339 status=PackbitsEncodeImage(image,length,pixels);
1342 case ZipCompression:
1344 status=ZLIBEncodeImage(image,length,pixels);
1348 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1352 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1353 (compression == JPEGCompression))
1358 (void) WriteBlobString(image,PS3_DirectClass"\n");
1359 switch (compression)
1364 (void) WriteBlobString(image,PS3_NoCompression"\n");
1367 case RLECompression:
1369 (void) WriteBlobString(image,PS3_RLECompression"\n");
1372 case JPEGCompression:
1374 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1377 case LZWCompression:
1379 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1382 case ZipCompression:
1384 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1391 if (compression == JPEGCompression)
1392 status=InjectImageBlob(image_info,image,image,"jpeg",
1397 Stream based compressions.
1399 status=SerializeImage(image_info,image,&pixels,&length);
1400 if (status == MagickFalse)
1402 (void) CloseBlob(image);
1403 return(MagickFalse);
1405 switch (compression)
1410 Ascii85Initialize(image);
1411 for (i=0; i < (ssize_t) length; i++)
1412 Ascii85Encode(image,pixels[i]);
1413 Ascii85Flush(image);
1417 case RLECompression:
1419 status=PackbitsEncodeImage(image,length,pixels);
1422 case LZWCompression:
1424 status=LZWEncodeImage(image,length,pixels);
1427 case ZipCompression:
1429 status=ZLIBEncodeImage(image,length,pixels);
1433 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1441 (void) WriteBlobString(image,PS3_PseudoClass"\n");
1442 switch (compression)
1447 (void) WriteBlobString(image,PS3_NoCompression"\n");
1450 case JPEGCompression:
1452 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1455 case RLECompression:
1457 (void) WriteBlobString(image,PS3_RLECompression"\n");
1460 case LZWCompression:
1462 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1465 case ZipCompression:
1467 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1472 Number of colors in color map.
1474 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g\n",
1475 (double) image->colors);
1476 (void) WriteBlobString(image,buffer);
1478 Color map - uncompressed.
1480 if ((compression != NoCompression) &&
1481 (compression != UndefinedCompression))
1483 for (i=0; i < (ssize_t) image->colors; i++)
1485 pixel=ScaleQuantumToChar(image->colormap[i].red);
1486 (void) WriteBlobByte(image,(unsigned char) pixel);
1487 pixel=ScaleQuantumToChar(image->colormap[i].green);
1488 (void) WriteBlobByte(image,(unsigned char) pixel);
1489 pixel=ScaleQuantumToChar(image->colormap[i].blue);
1490 (void) WriteBlobByte(image,(unsigned char) pixel);
1495 Ascii85Initialize(image);
1496 for (i=0; i < (ssize_t) image->colors; i++)
1498 pixel=ScaleQuantumToChar(image->colormap[i].red);
1499 Ascii85Encode(image,(unsigned char) pixel);
1500 pixel=ScaleQuantumToChar(image->colormap[i].green);
1501 Ascii85Encode(image,(unsigned char) pixel);
1502 pixel=ScaleQuantumToChar(image->colormap[i].blue);
1503 Ascii85Encode(image,(unsigned char) pixel);
1505 Ascii85Flush(image);
1507 status=SerializeImageIndexes(image_info,image,&pixels,&length);
1508 if (status == MagickFalse)
1510 (void) CloseBlob(image);
1511 return(MagickFalse);
1513 switch (compression)
1518 Ascii85Initialize(image);
1519 for (i=0; i < (ssize_t) length; i++)
1520 Ascii85Encode(image,pixels[i]);
1521 Ascii85Flush(image);
1525 case JPEGCompression:
1527 status=InjectImageBlob(image_info,image,image,"jpeg",
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) FormatMagickString(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) FormatMagickString(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) FormatMagickString(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);