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 entry->seekable_stream=MagickTrue;
133 (void) RegisterMagickInfo(entry);
134 entry=SetMagickInfo("PS3");
135 entry->encoder=(EncodeImageHandler *) WritePS3Image;
136 entry->description=ConstantString("Level III PostScript");
137 entry->module=ConstantString("PS3");
138 entry->seekable_stream=MagickTrue;
139 (void) RegisterMagickInfo(entry);
140 return(MagickImageCoderSignature);
144 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148 % U n r e g i s t e r P S 3 I m a g e %
152 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
154 % UnregisterPS3Image() removes format registrations made by the PS3 module
155 % from the list of supported formats.
157 % The format of the UnregisterPS3Image method is:
159 % UnregisterPS3Image(void)
162 ModuleExport void UnregisterPS3Image(void)
164 (void) UnregisterMagickInfo("EPS3");
165 (void) UnregisterMagickInfo("PS3");
169 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
173 % W r i t e P S 3 I m a g e %
177 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
179 % WritePS3Image() translates an image to encapsulated Postscript Level III
180 % for printing. If the supplied geometry is null, the image is centered on
181 % the Postscript page. Otherwise, the image is positioned as specified by the
184 % The format of the WritePS3Image method is:
186 % MagickBooleanType WritePS3Image(const ImageInfo *image_info,Image *image)
188 % A description of each parameter follows:
190 % o image_info: Specifies a pointer to a ImageInfo structure.
192 % o image: the image.
196 static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
197 Image *image,Image *inject_image)
215 write_info=CloneImageInfo(image_info);
216 (void) CopyMagickString(write_info->filename,"GROUP4:",MaxTextExtent);
217 (void) CopyMagickString(write_info->magick,"GROUP4",MaxTextExtent);
218 group4_image=CloneImage(inject_image,0,0,MagickTrue,&image->exception);
219 if (group4_image == (Image *) NULL)
221 group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
223 group4_image=DestroyImage(group4_image);
224 if (group4 == (unsigned char *) NULL)
226 write_info=DestroyImageInfo(write_info);
227 if (WriteBlob(image,length,group4) != (ssize_t) length)
229 group4=(unsigned char *) RelinquishMagickMemory(group4);
233 static MagickBooleanType SerializeImage(const ImageInfo *image_info,
234 Image *image,unsigned char **pixels,size_t *length)
239 register const IndexPacket
242 register const PixelPacket
248 register unsigned char
254 assert(image != (Image *) NULL);
255 assert(image->signature == MagickSignature);
256 if (image->debug != MagickFalse)
257 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
259 *length=(image->colorspace == CMYKColorspace ? 4 : 3)*(size_t)
260 image->columns*image->rows;
261 *pixels=(unsigned char *) AcquireQuantumMemory(*length,sizeof(**pixels));
262 if (*pixels == (unsigned char *) NULL)
263 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
265 for (y=0; y < (ssize_t) image->rows; y++)
267 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
268 if (p == (const PixelPacket *) NULL)
270 indexes=GetVirtualIndexQueue(image);
271 if (image->colorspace != CMYKColorspace)
272 for (x=0; x < (ssize_t) image->columns; x++)
274 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
275 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
276 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
280 for (x=0; x < (ssize_t) image->columns; x++)
282 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
283 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
284 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
285 *q++=ScaleQuantumToChar(GetIndexPixelComponent(indexes+x));
288 if (image->previous == (Image *) NULL)
290 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
292 if (status == MagickFalse)
296 if (status == MagickFalse)
297 *pixels=(unsigned char *) RelinquishMagickMemory(*pixels);
301 static MagickBooleanType SerializeImageChannel(const ImageInfo *image_info,
302 Image *image,unsigned char **pixels,size_t *length)
307 register const PixelPacket
313 register unsigned char
327 assert(image != (Image *) NULL);
328 assert(image->signature == MagickSignature);
329 if (image->debug != MagickFalse)
330 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
332 pack=IsMonochromeImage(image,&image->exception) == MagickFalse ? 1UL : 8UL;
333 padded_columns=((image->columns+pack-1)/pack)*pack;
334 *length=(size_t) padded_columns*image->rows/pack;
335 *pixels=(unsigned char *) AcquireQuantumMemory(*length,sizeof(**pixels));
336 if (*pixels == (unsigned char *) NULL)
337 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
339 for (y=0; y < (ssize_t) image->rows; y++)
341 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
342 if (p == (const PixelPacket *) NULL)
345 for (x=0; x < (ssize_t) image->columns; x++)
347 *q++=ScaleQuantumToChar(PixelIntensityToQuantum(p));
353 for (x=0; x < (ssize_t) padded_columns; x++)
355 bit=(unsigned char) 0x00;
356 if (x < (ssize_t) image->columns)
357 bit=(unsigned char) (PixelIntensityToQuantum(p) == (Quantum)
358 TransparentOpacity ? 0x01 : 0x00);
359 code=(code << 1)+bit;
360 if (((x+1) % pack) == 0)
368 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
370 if (status == MagickFalse)
373 if (status == MagickFalse)
374 *pixels=(unsigned char *) RelinquishMagickMemory(*pixels);
378 static MagickBooleanType SerializeImageIndexes(const ImageInfo *image_info,
379 Image *image,unsigned char **pixels,size_t *length)
384 register const IndexPacket
387 register const PixelPacket
393 register unsigned char
399 assert(image != (Image *) NULL);
400 assert(image->signature == MagickSignature);
401 if (image->debug != MagickFalse)
402 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
404 *length=(size_t) image->columns*image->rows;
405 *pixels=(unsigned char *) AcquireQuantumMemory(*length,sizeof(**pixels));
406 if (*pixels == (unsigned char *) NULL)
407 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
409 for (y=0; y < (ssize_t) image->rows; y++)
411 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
412 if (p == (const PixelPacket *) NULL)
414 indexes=GetVirtualIndexQueue(image);
415 for (x=0; x < (ssize_t) image->columns; x++)
416 *q++=(unsigned char) GetIndexPixelComponent(indexes+x);
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)
434 buffer[MaxTextExtent];
456 assert(image_info != (ImageInfo *) NULL);
457 assert(image_info->signature == MagickSignature);
458 assert(image != (Image *) NULL);
459 assert(image->signature == MagickSignature);
460 if (image->debug != MagickFalse)
461 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
462 assert(image->matte != MagickFalse);
465 Note BeginData DSC comment for update later.
467 start=TellBlob(image);
468 (void) FormatLocaleString(buffer,MaxTextExtent,
469 "%%%%BeginData:%13ld %s Bytes\n",0L,compression == NoCompression ?
471 (void) WriteBlobString(image,buffer);
472 stop=TellBlob(image);
474 Only lossless compressions for the mask.
481 (void) FormatLocaleString(buffer,MaxTextExtent,
482 "currentfile %.20g %.20g "PS3_NoCompression" ByteStreamDecodeFilter\n",
483 (double) image->columns,(double) image->rows);
487 case Group4Compression:
489 (void) FormatLocaleString(buffer,MaxTextExtent,
490 "currentfile %.20g %.20g "PS3_FaxCompression" ByteStreamDecodeFilter\n",
491 (double) image->columns,(double) image->rows);
496 (void) FormatLocaleString(buffer,MaxTextExtent,
497 "currentfile %.20g %.20g "PS3_LZWCompression" ByteStreamDecodeFilter\n",
498 (double) image->columns,(double) image->rows);
503 (void) FormatLocaleString(buffer,MaxTextExtent,
504 "currentfile %.20g %.20g "PS3_RLECompression" ByteStreamDecodeFilter\n",
505 (double) image->columns,(double) image->rows);
510 (void) FormatLocaleString(buffer,MaxTextExtent,
511 "currentfile %.20g %.20g "PS3_ZipCompression" ByteStreamDecodeFilter\n",
512 (double) image->columns,(double) image->rows);
516 (void) WriteBlobString(image,buffer);
517 (void) WriteBlobString(image,"/ReusableStreamDecode filter\n");
518 mask_image=CloneImage(image,0,0,MagickTrue,&image->exception);
519 if (mask_image == (Image *) NULL)
520 ThrowWriterException(CoderError,image->exception.reason);
521 status=SeparateImageChannel(mask_image,OpacityChannel);
522 if (status == MagickFalse)
524 mask_image=DestroyImage(mask_image);
527 (void) SetImageType(mask_image,BilevelType);
528 (void) SetImageType(mask_image,PaletteType);
529 mask_image->matte=MagickFalse;
530 pixels=(unsigned char *) NULL;
537 status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
538 if (status == MagickFalse)
540 Ascii85Initialize(image);
541 for (i=0; i < (ssize_t) length; i++)
542 Ascii85Encode(image,pixels[i]);
544 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
548 case Group4Compression:
550 if ((compression == FaxCompression) ||
551 (LocaleCompare(CCITTParam,"0") == 0))
552 status=HuffmanEncodeImage(image_info,image,mask_image);
554 status=Huffman2DEncodeImage(image_info,image,mask_image);
559 status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
560 if (status == MagickFalse)
562 status=LZWEncodeImage(image,length,pixels);
563 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
568 status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
569 if (status == MagickFalse)
571 status=PackbitsEncodeImage(image,length,pixels);
572 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
577 status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
578 if (status == MagickFalse)
580 status=ZLIBEncodeImage(image,length,pixels);
581 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
585 mask_image=DestroyImage(mask_image);
586 (void) WriteBlobByte(image,'\n');
587 length=(size_t) (TellBlob(image)-stop);
588 stop=TellBlob(image);
589 offset=SeekBlob(image,start,SEEK_SET);
591 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
592 (void) FormatLocaleString(buffer,MaxTextExtent,
593 "%%%%BeginData:%13ld %s Bytes\n",(long) length,
594 compression == NoCompression ? "ASCII" : "BINARY");
595 (void) WriteBlobString(image,buffer);
596 offset=SeekBlob(image,stop,SEEK_SET);
598 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
599 (void) WriteBlobString(image,"%%EndData\n");
600 (void) WriteBlobString(image, "/mask_stream exch def\n");
604 static MagickBooleanType WritePS3Image(const ImageInfo *image_info,Image *image)
609 "/ByteStreamDecodeFilter",
614 " z "PS3_NoCompression" eq { /ASCII85Decode filter } if",
615 " z "PS3_FaxCompression" eq",
622 " /CCITTFaxDecode filter",
624 " z "PS3_JPEGCompression" eq { /DCTDecode filter } if",
625 " z "PS3_LZWCompression" eq { /LZWDecode filter } if",
626 " z "PS3_RLECompression" eq { /RunLengthDecode filter } if",
627 " z "PS3_ZipCompression" eq { /FlateDecode filter } if",
630 "/DirectClassImageDict",
632 " colorspace "PS3_RGBColorspace" eq",
634 " /DeviceRGB setcolorspace",
639 " /BitsPerComponent 8",
640 " /DataSource pixel_stream",
641 " /MultipleDataSources false",
642 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
643 " /Decode [0 1 0 1 0 1]",
647 " /DeviceCMYK setcolorspace",
652 " /BitsPerComponent 8",
653 " /DataSource pixel_stream",
654 " /MultipleDataSources false",
655 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
657 " compression "PS3_JPEGCompression" eq",
658 " { [1 0 1 0 1 0 1 0] }",
659 " { [0 1 0 1 0 1 0 1] }",
666 "/PseudoClassImageDict",
668 " % Colors in colormap image.",
669 " currentfile buffer readline pop",
670 " token pop /colors exch def pop",
673 " % Depth of grayscale image.",
674 " currentfile buffer readline pop",
675 " token pop /bits exch def pop",
676 " /DeviceGray setcolorspace",
681 " /BitsPerComponent bits",
683 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
684 " /DataSource pixel_stream",
689 " /colormap colors 3 mul string def",
690 " compression "PS3_NoCompression" eq",
691 " { currentfile /ASCII85Decode filter colormap readstring pop pop }",
692 " { currentfile colormap readstring pop pop }",
694 " [ /Indexed /DeviceRGB colors 1 sub colormap ] setcolorspace",
699 " /BitsPerComponent 8",
701 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
702 " /DataSource pixel_stream",
708 "/NonMaskedImageDict",
710 " class "PS3_PseudoClass" eq",
711 " { PseudoClassImageDict }",
712 " { DirectClassImageDict }",
720 " /InterleaveType 3",
721 " /DataDict NonMaskedImageDict",
727 " /BitsPerComponent 1",
728 " /DataSource mask_stream",
729 " /MultipleDataSources false",
730 " /ImageMatrix [ columns 0 0 rows neg 0 rows]",
741 " /buffer 512 string def",
743 " currentfile buffer readline pop",
744 " token pop /x exch def",
745 " token pop /y exch def pop",
747 " % Image size and font size.",
748 " currentfile buffer readline pop",
749 " token pop /x exch def",
750 " token pop /y exch def pop",
751 " currentfile buffer readline pop",
752 " token pop /pointsize exch def pop",
759 " currentfile buffer readline pop",
760 " token pop /clipped exch def pop",
762 " currentfile buffer readline pop",
763 " token pop /sp exch def pop",
764 " % Image pixel size.",
765 " currentfile buffer readline pop",
766 " token pop /columns exch def",
767 " token pop /rows exch def pop",
768 " % Colorspace (RGB/CMYK).",
769 " currentfile buffer readline pop",
770 " token pop /colorspace exch def pop",
772 " currentfile buffer readline pop",
773 " token pop /alpha exch def pop",
775 " currentfile buffer readline pop",
776 " token pop /stencil exch def pop",
777 " % Image class (direct/pseudo).",
778 " currentfile buffer readline pop",
779 " token pop /class exch def pop",
780 " % Compression type.",
781 " currentfile buffer readline pop",
782 " token pop /compression exch def pop",
783 " % Clip and render.",
784 " /pixel_stream currentfile columns rows compression ByteStreamDecodeFilter def",
785 " clipped { ClipImage } if",
786 " alpha stencil not and",
787 " { MaskedImageDict mask_stream resetfile }",
788 " { NonMaskedImageDict }",
790 " stencil { 0 setgray imagemask } { image } ifelse",
791 " sp { showpage } if",
797 buffer[MaxTextExtent],
800 page_geometry[MaxTextExtent];
860 Open output image file.
862 assert(image_info != (const ImageInfo *) NULL);
863 assert(image_info->signature == MagickSignature);
864 assert(image != (Image *) NULL);
865 assert(image->signature == MagickSignature);
866 if (image->debug != MagickFalse)
867 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
868 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
869 if (status == MagickFalse)
871 compression=image->compression;
872 if (image_info->compression != UndefinedCompression)
873 compression=image_info->compression;
877 case Group4Compression:
879 if ((IsMonochromeImage(image,&image->exception) == MagickFalse) ||
880 (image->matte != MagickFalse))
881 compression=RLECompression;
884 #if !defined(MAGICKCORE_JPEG_DELEGATE)
885 case JPEGCompression:
887 compression=RLECompression;
888 (void) ThrowMagickException(&image->exception,GetMagickModule(),
889 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
894 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
897 compression=RLECompression;
898 (void) ThrowMagickException(&image->exception,GetMagickModule(),
899 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZLIB)",
907 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
913 Scale relative to dots-per-inch.
915 delta.x=DefaultResolution;
916 delta.y=DefaultResolution;
917 resolution.x=image->x_resolution;
918 resolution.y=image->y_resolution;
919 if ((resolution.x == 0.0) || (resolution.y == 0.0))
921 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
922 resolution.x=geometry_info.rho;
923 resolution.y=geometry_info.sigma;
924 if ((flags & SigmaValue) == 0)
925 resolution.y=resolution.x;
927 if (image_info->density != (char *) NULL)
929 flags=ParseGeometry(image_info->density,&geometry_info);
930 resolution.x=geometry_info.rho;
931 resolution.y=geometry_info.sigma;
932 if ((flags & SigmaValue) == 0)
933 resolution.y=resolution.x;
935 if (image->units == PixelsPerCentimeterResolution)
937 resolution.x=(size_t) (100.0*2.54*resolution.x+0.5)/100.0;
938 resolution.y=(size_t) (100.0*2.54*resolution.y+0.5)/100.0;
940 SetGeometry(image,&geometry);
941 (void) FormatLocaleString(page_geometry,MaxTextExtent,"%.20gx%.20g",
942 (double) image->columns,(double) image->rows);
943 if (image_info->page != (char *) NULL)
944 (void) CopyMagickString(page_geometry,image_info->page,MaxTextExtent);
946 if ((image->page.width != 0) && (image->page.height != 0))
947 (void) FormatLocaleString(page_geometry,MaxTextExtent,
948 "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,(double)
949 image->page.height,(double) image->page.x,(double) image->page.y);
951 if ((image->gravity != UndefinedGravity) &&
952 (LocaleCompare(image_info->magick,"PS") == 0))
953 (void) CopyMagickString(page_geometry,PSPageGeometry,MaxTextExtent);
954 (void) ConcatenateMagickString(page_geometry,">",MaxTextExtent);
955 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
956 &geometry.width,&geometry.height);
957 scale.x=(double) (geometry.width*delta.x)/resolution.x;
958 geometry.width=(size_t) floor(scale.x+0.5);
959 scale.y=(double) (geometry.height*delta.y)/resolution.y;
960 geometry.height=(size_t) floor(scale.y+0.5);
961 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
962 (void) ParseGravityGeometry(image,page_geometry,&page_info,
964 if (image->gravity != UndefinedGravity)
966 geometry.x=(-page_info.x);
967 geometry.y=(ssize_t) (media_info.height+page_info.y-image->rows);
970 if (image_info->pointsize != 0.0)
971 pointsize=image_info->pointsize;
973 value=GetImageProperty(image,"label");
974 if (value != (const char *) NULL)
975 text_size=(size_t) (MultilineCensus(value)*pointsize+12);
980 Postscript header on the first page.
982 if (LocaleCompare(image_info->magick,"PS3") == 0)
983 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0\n",MaxTextExtent);
985 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0 EPSF-3.0\n",
987 (void) WriteBlobString(image,buffer);
988 (void) FormatLocaleString(buffer,MaxTextExtent,
989 "%%%%Creator: ImageMagick %s\n",MagickLibVersionText);
990 (void) WriteBlobString(image,buffer);
991 (void) FormatLocaleString(buffer,MaxTextExtent,"%%%%Title: %s\n",
993 (void) WriteBlobString(image,buffer);
994 timer=time((time_t *) NULL);
995 (void) FormatMagickTime(timer,MaxTextExtent,date);
996 (void) FormatLocaleString(buffer,MaxTextExtent,
997 "%%%%CreationDate: %s\n",date);
998 (void) WriteBlobString(image,buffer);
999 bounds.x1=(double) geometry.x;
1000 bounds.y1=(double) geometry.y;
1001 bounds.x2=(double) geometry.x+scale.x;
1002 bounds.y2=(double) geometry.y+scale.y+text_size;
1003 if ((image_info->adjoin != MagickFalse) &&
1004 (GetNextImageInList(image) != (Image *) NULL))
1006 (void) WriteBlobString(image,"%%BoundingBox: (atend)\n");
1007 (void) WriteBlobString(image,"%%HiResBoundingBox: (atend)\n");
1011 (void) FormatLocaleString(buffer,MaxTextExtent,
1012 "%%%%BoundingBox: %g %g %g %g\n",floor(bounds.x1+0.5),
1013 floor(bounds.y1+0.5),ceil(bounds.x2-0.5),ceil(bounds.y2-0.5));
1014 (void) WriteBlobString(image,buffer);
1015 (void) FormatLocaleString(buffer,MaxTextExtent,
1016 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,
1017 bounds.y1,bounds.x2,bounds.y2);
1018 (void) WriteBlobString(image,buffer);
1019 if (image->colorspace == CMYKColorspace)
1020 (void) WriteBlobString(image,
1021 "%%DocumentProcessColors: Cyan Magenta Yellow Black\n");
1023 if (IsGrayImage(image,&image->exception) != MagickFalse)
1024 (void) WriteBlobString(image,
1025 "%%DocumentProcessColors: Black\n");
1030 value=GetImageProperty(image,"label");
1031 if (value != (const char *) NULL)
1032 (void) WriteBlobString(image,
1033 "%%DocumentNeededResources: font Helvetica\n");
1034 (void) WriteBlobString(image,"%%LanguageLevel: 3\n");
1036 Pages, orientation and order.
1038 if (LocaleCompare(image_info->magick,"PS3") != 0)
1039 (void) WriteBlobString(image,"%%Pages: 1\n");
1042 (void) WriteBlobString(image,"%%Orientation: Portrait\n");
1043 (void) WriteBlobString(image,"%%PageOrder: Ascend\n");
1044 if (image_info->adjoin == MagickFalse)
1045 (void) CopyMagickString(buffer,"%%Pages: 1\n",MaxTextExtent);
1047 (void) FormatLocaleString(buffer,MaxTextExtent,
1048 "%%%%Pages: %.20g\n",(double) GetImageListLength(image));
1049 (void) WriteBlobString(image,buffer);
1051 (void) WriteBlobString(image,"%%EndComments\n");
1053 The static postscript procedures prolog.
1055 (void)WriteBlobString(image,"%%BeginProlog\n");
1056 for (q=PostscriptProlog; *q; q++)
1058 (void) WriteBlobString(image,*q);
1059 (void) WriteBlobByte(image,'\n');
1062 One label line for each line in label string.
1064 value=GetImageProperty(image,"label");
1065 if (value != (const char *) NULL)
1067 (void) WriteBlobString(image,"\n %% Labels.\n /Helvetica "
1068 " findfont pointsize scalefont setfont\n");
1069 for (i=(ssize_t) MultilineCensus(value)-1; i >= 0; i--)
1071 (void) WriteBlobString(image,
1072 " currentfile buffer readline pop token pop\n");
1073 (void) FormatLocaleString(buffer,MaxTextExtent,
1074 " 0 y %g add moveto show pop\n",i*pointsize+12);
1075 (void) WriteBlobString(image,buffer);
1079 The static postscript procedures epilog.
1081 for (q=PostscriptEpilog; *q; q++)
1083 (void) WriteBlobString(image,*q);
1084 (void) WriteBlobByte(image,'\n');
1086 (void)WriteBlobString(image,"%%EndProlog\n");
1088 (void) FormatLocaleString(buffer,MaxTextExtent,"%%%%Page: 1 %.20g\n",
1090 (void) WriteBlobString(image,buffer);
1094 (void) FormatLocaleString(buffer,MaxTextExtent,
1095 "%%%%PageBoundingBox: %.20g %.20g %.20g %.20g\n",(double) geometry.x,
1096 (double) geometry.y,geometry.x+(double) geometry.width,geometry.y+
1097 (double) (geometry.height+text_size));
1098 (void) WriteBlobString(image,buffer);
1100 Page process colors if not RGB.
1102 if (image->colorspace == CMYKColorspace)
1103 (void) WriteBlobString(image,
1104 "%%PageProcessColors: Cyan Magenta Yellow Black\n");
1106 if (IsGrayImage(image,&image->exception) != MagickFalse)
1107 (void) WriteBlobString(image,"%%PageProcessColors: Black\n");
1109 Adjust document bounding box to bound page bounding box.
1111 if ((double) geometry.x < bounds.x1)
1112 bounds.x1=(double) geometry.x;
1113 if ((double) geometry.y < bounds.y1)
1114 bounds.y1=(double) geometry.y;
1115 if ((double) (geometry.x+scale.x) > bounds.x2)
1116 bounds.x2=(double) geometry.x+scale.x;
1117 if ((double) (geometry.y+scale.y+text_size) > bounds.y2)
1118 bounds.y2=(double) geometry.y+scale.y+text_size;
1120 Page font resource if there's a label.
1122 value=GetImageProperty(image,"label");
1123 if (value != (const char *) NULL)
1124 (void) WriteBlobString(image,"%%PageResources: font Helvetica\n");
1126 PS clipping path from Photoshop clipping path.
1128 if ((image->clip_mask == (Image *) NULL) ||
1129 (LocaleNCompare("8BIM:",image->clip_mask->magick_filename,5) != 0))
1130 (void) WriteBlobString(image,"/ClipImage {} def\n");
1136 value=GetImageProperty(image,image->clip_mask->magick_filename);
1137 if (value == (const char *) NULL)
1138 return(MagickFalse);
1139 (void) WriteBlobString(image,value);
1140 (void) WriteBlobByte(image,'\n');
1143 Push a dictionary for our own def's if this an EPS.
1145 if (LocaleCompare(image_info->magick,"PS3") != 0)
1146 (void) WriteBlobString(image,"userdict begin\n");
1150 if ((image->matte != MagickFalse) &&
1151 (WritePS3MaskImage(image_info,image,compression) == MagickFalse))
1153 (void) CloseBlob(image);
1154 return(MagickFalse);
1157 Remember position of BeginData comment so we can update it.
1159 start=TellBlob(image);
1160 (void) FormatLocaleString(buffer,MaxTextExtent,
1161 "%%%%BeginData:%13ld %s Bytes\n",0L,
1162 compression == NoCompression ? "ASCII" : "BINARY");
1163 (void) WriteBlobString(image,buffer);
1164 stop=TellBlob(image);
1165 (void) WriteBlobString(image,"DisplayImage\n");
1167 Translate, scale, and font point size.
1169 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n%g %g\n%g\n",
1170 (double) geometry.x,(double) geometry.y,scale.x,scale.y,pointsize);
1171 (void) WriteBlobString(image,buffer);
1175 labels=(char **) NULL;
1176 value=GetImageProperty(image,"label");
1177 if (value != (const char *) NULL)
1178 labels=StringToList(value);
1179 if (labels != (char **) NULL)
1181 for (i=0; labels[i] != (char *) NULL; i++)
1183 if (compression != NoCompression)
1185 for (j=0; labels[i][j] != '\0'; j++)
1186 (void) WriteBlobByte(image,(unsigned char) labels[i][j]);
1187 (void) WriteBlobByte(image,'\n');
1191 (void) WriteBlobString(image,"<~");
1192 Ascii85Initialize(image);
1193 for (j=0; labels[i][j] != '\0'; j++)
1194 Ascii85Encode(image,(unsigned char) labels[i][j]);
1195 Ascii85Flush(image);
1197 labels[i]=DestroyString(labels[i]);
1199 labels=(char **) RelinquishMagickMemory(labels);
1202 Photoshop clipping path active?
1204 if ((image->clip_mask != (Image *) NULL) &&
1205 (LocaleNCompare("8BIM:",image->clip_mask->magick_filename,5) == 0))
1206 (void) WriteBlobString(image,"true\n");
1208 (void) WriteBlobString(image,"false\n");
1210 Showpage for non-EPS.
1212 (void) WriteBlobString(image, LocaleCompare(image_info->magick,"PS3") == 0 ?
1213 "true\n" : "false\n");
1215 Image columns, rows, and color space.
1217 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n%s\n",
1218 (double) image->columns,(double) image->rows,image->colorspace ==
1219 CMYKColorspace ? PS3_CMYKColorspace : PS3_RGBColorspace);
1220 (void) WriteBlobString(image,buffer);
1224 (void) WriteBlobString(image,image->matte != MagickFalse ?
1225 "true\n" : "false\n");
1227 Render with imagemask operator?
1229 option=GetImageOption(image_info,"ps3:imagemask");
1230 (void) WriteBlobString(image,((option != (const char *) NULL) &&
1231 (IsMonochromeImage(image,&image->exception) != MagickFalse)) ?
1232 "true\n" : "false\n");
1236 pixels=(unsigned char *) NULL;
1238 if ((image_info->type != TrueColorType) &&
1239 (image_info->type != TrueColorMatteType) &&
1240 (image_info->type != ColorSeparationType) &&
1241 (image_info->type != ColorSeparationMatteType) &&
1242 (image->colorspace != CMYKColorspace) &&
1243 ((IsGrayImage(image,&image->exception) != MagickFalse) ||
1244 (IsMonochromeImage(image,&image->exception) != MagickFalse)))
1249 (void) WriteBlobString(image,PS3_PseudoClass"\n");
1250 switch (compression)
1255 (void) WriteBlobString(image,PS3_NoCompression"\n");
1258 case FaxCompression:
1259 case Group4Compression:
1261 (void) WriteBlobString(image,PS3_FaxCompression"\n");
1264 case JPEGCompression:
1266 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1269 case LZWCompression:
1271 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1274 case RLECompression:
1276 (void) WriteBlobString(image,PS3_RLECompression"\n");
1279 case ZipCompression:
1281 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1286 Number of colors -- 0 for single component non-color mapped data.
1288 (void) WriteBlobString(image,"0\n");
1290 1 bit or 8 bit components?
1292 (void) FormatLocaleString(buffer,MaxTextExtent,"%d\n",
1293 IsMonochromeImage(image,&image->exception) != MagickFalse ? 1 : 8);
1294 (void) WriteBlobString(image,buffer);
1298 if (compression == JPEGCompression)
1299 status=InjectImageBlob(image_info,image,image,"jpeg",
1302 if ((compression == FaxCompression) ||
1303 (compression == Group4Compression))
1305 if (LocaleCompare(CCITTParam,"0") == 0)
1306 status=HuffmanEncodeImage(image_info,image,image);
1308 status=Huffman2DEncodeImage(image_info,image,image);
1312 status=SerializeImageChannel(image_info,image,&pixels,&length);
1313 if (status == MagickFalse)
1315 (void) CloseBlob(image);
1316 return(MagickFalse);
1318 switch (compression)
1323 Ascii85Initialize(image);
1324 for (i=0; i < (ssize_t) length; i++)
1325 Ascii85Encode(image,pixels[i]);
1326 Ascii85Flush(image);
1330 case LZWCompression:
1332 status=LZWEncodeImage(image,length,pixels);
1335 case RLECompression:
1337 status=PackbitsEncodeImage(image,length,pixels);
1340 case ZipCompression:
1342 status=ZLIBEncodeImage(image,length,pixels);
1346 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1350 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1351 (compression == JPEGCompression))
1356 (void) WriteBlobString(image,PS3_DirectClass"\n");
1357 switch (compression)
1362 (void) WriteBlobString(image,PS3_NoCompression"\n");
1365 case RLECompression:
1367 (void) WriteBlobString(image,PS3_RLECompression"\n");
1370 case JPEGCompression:
1372 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1375 case LZWCompression:
1377 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1380 case ZipCompression:
1382 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1389 if (compression == JPEGCompression)
1390 status=InjectImageBlob(image_info,image,image,"jpeg",
1395 Stream based compressions.
1397 status=SerializeImage(image_info,image,&pixels,&length);
1398 if (status == MagickFalse)
1400 (void) CloseBlob(image);
1401 return(MagickFalse);
1403 switch (compression)
1408 Ascii85Initialize(image);
1409 for (i=0; i < (ssize_t) length; i++)
1410 Ascii85Encode(image,pixels[i]);
1411 Ascii85Flush(image);
1415 case RLECompression:
1417 status=PackbitsEncodeImage(image,length,pixels);
1420 case LZWCompression:
1422 status=LZWEncodeImage(image,length,pixels);
1425 case ZipCompression:
1427 status=ZLIBEncodeImage(image,length,pixels);
1431 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1439 (void) WriteBlobString(image,PS3_PseudoClass"\n");
1440 switch (compression)
1445 (void) WriteBlobString(image,PS3_NoCompression"\n");
1448 case JPEGCompression:
1450 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1453 case RLECompression:
1455 (void) WriteBlobString(image,PS3_RLECompression"\n");
1458 case LZWCompression:
1460 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1463 case ZipCompression:
1465 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1470 Number of colors in color map.
1472 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",
1473 (double) image->colors);
1474 (void) WriteBlobString(image,buffer);
1476 Color map - uncompressed.
1478 if ((compression != NoCompression) &&
1479 (compression != UndefinedCompression))
1481 for (i=0; i < (ssize_t) image->colors; i++)
1483 pixel=ScaleQuantumToChar(image->colormap[i].red);
1484 (void) WriteBlobByte(image,(unsigned char) pixel);
1485 pixel=ScaleQuantumToChar(image->colormap[i].green);
1486 (void) WriteBlobByte(image,(unsigned char) pixel);
1487 pixel=ScaleQuantumToChar(image->colormap[i].blue);
1488 (void) WriteBlobByte(image,(unsigned char) pixel);
1493 Ascii85Initialize(image);
1494 for (i=0; i < (ssize_t) image->colors; i++)
1496 pixel=ScaleQuantumToChar(image->colormap[i].red);
1497 Ascii85Encode(image,(unsigned char) pixel);
1498 pixel=ScaleQuantumToChar(image->colormap[i].green);
1499 Ascii85Encode(image,(unsigned char) pixel);
1500 pixel=ScaleQuantumToChar(image->colormap[i].blue);
1501 Ascii85Encode(image,(unsigned char) pixel);
1503 Ascii85Flush(image);
1505 status=SerializeImageIndexes(image_info,image,&pixels,&length);
1506 if (status == MagickFalse)
1508 (void) CloseBlob(image);
1509 return(MagickFalse);
1511 switch (compression)
1516 Ascii85Initialize(image);
1517 for (i=0; i < (ssize_t) length; i++)
1518 Ascii85Encode(image,pixels[i]);
1519 Ascii85Flush(image);
1523 case JPEGCompression:
1525 status=InjectImageBlob(image_info,image,image,"jpeg",
1529 case RLECompression:
1531 status=PackbitsEncodeImage(image,length,pixels);
1534 case LZWCompression:
1536 status=LZWEncodeImage(image,length,pixels);
1539 case ZipCompression:
1541 status=ZLIBEncodeImage(image,length,pixels);
1545 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1547 (void) WriteBlobByte(image,'\n');
1548 if (status == MagickFalse)
1550 (void) CloseBlob(image);
1551 return(MagickFalse);
1554 Update BeginData now that we know the data size.
1556 length=(size_t) (TellBlob(image)-stop);
1557 stop=TellBlob(image);
1558 offset=SeekBlob(image,start,SEEK_SET);
1560 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
1561 (void) FormatLocaleString(buffer,MaxTextExtent,
1562 "%%%%BeginData:%13ld %s Bytes\n",(long) length,
1563 compression == NoCompression ? "ASCII" : "BINARY");
1564 (void) WriteBlobString(image,buffer);
1565 offset=SeekBlob(image,stop,SEEK_SET);
1566 (void) WriteBlobString(image,"%%EndData\n");
1568 End private dictionary if this an EPS.
1570 if (LocaleCompare(image_info->magick,"PS3") != 0)
1571 (void) WriteBlobString(image,"end\n");
1572 (void) WriteBlobString(image,"%%PageTrailer\n");
1573 if (GetNextImageInList(image) == (Image *) NULL)
1575 image=SyncNextImageInList(image);
1576 status=SetImageProgress(image,SaveImagesTag,scene++,
1577 GetImageListLength(image));
1578 if (status == MagickFalse)
1580 } while (image_info->adjoin != MagickFalse);
1581 (void) WriteBlobString(image,"%%Trailer\n");
1584 (void) FormatLocaleString(buffer,MaxTextExtent,
1585 "%%%%BoundingBox: %g %g %g %g\n",floor(bounds.x1+0.5),
1586 floor(bounds.y1+0.5),ceil(bounds.x2-0.5),ceil(bounds.y2-0.5));
1587 (void) WriteBlobString(image,buffer);
1588 (void) FormatLocaleString(buffer,MaxTextExtent,
1589 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,bounds.y1,
1590 bounds.x2,bounds.y2);
1591 (void) WriteBlobString(image,buffer);
1593 (void) WriteBlobString(image,"%%EOF\n");
1594 (void) CloseBlob(image);