2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Write Postscript Level III Format %
21 % Copyright 1999-2010 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 % unsigned long RegisterPS3Image(void)
123 ModuleExport unsigned long 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 < (long) 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 < (long) image->columns; x++)
273 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
274 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
275 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
279 for (x=0; x < (long) 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,y,image->rows);
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)
308 register const PixelPacket
314 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=IsMonochromeImage(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 < (long) image->rows; y++)
339 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
340 if (p == (const PixelPacket *) NULL)
343 for (x=0; x < (long) image->columns; x++)
345 *q++=ScaleQuantumToChar(PixelIntensityToQuantum(p));
351 for (x=0; x < (long) padded_columns; x++)
353 bit=(unsigned char) 0x00;
354 if (x < (long) image->columns)
355 bit=(unsigned char) (PixelIntensityToQuantum(p) ==
356 (Quantum) TransparentOpacity ? 0x01 : 0x00);
357 code=(code << 1)+bit;
358 if (((x+1) % pack) == 0)
366 status=SetImageProgress(image,SaveImageTag,y,image->rows);
367 if (status == MagickFalse)
370 if (status == MagickFalse)
371 *pixels=(unsigned char *) RelinquishMagickMemory(*pixels);
375 static MagickBooleanType SerializeImageIndexes(const ImageInfo *image_info,
376 Image *image,unsigned char **pixels,size_t *length)
384 register const IndexPacket
387 register const PixelPacket
393 register unsigned char
396 assert(image != (Image *) NULL);
397 assert(image->signature == MagickSignature);
398 if (image->debug != MagickFalse)
399 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
401 *length=(size_t) image->columns*image->rows;
402 *pixels=(unsigned char *) AcquireQuantumMemory(*length,sizeof(**pixels));
403 if (*pixels == (unsigned char *) NULL)
404 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
406 for (y=0; y < (long) image->rows; y++)
408 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
409 if (p == (const PixelPacket *) NULL)
411 indexes=GetVirtualIndexQueue(image);
412 for (x=0; x < (long) image->columns; x++)
413 *q++=(unsigned char) indexes[x];
414 if (image->previous == (Image *) NULL)
416 status=SetImageProgress(image,SaveImageTag,y,image->rows);
417 if (status == MagickFalse)
421 if (status == MagickFalse)
422 *pixels=(unsigned char *) RelinquishMagickMemory(*pixels);
426 static MagickBooleanType WritePS3MaskImage(const ImageInfo *image_info,
427 Image *image,const CompressionType compression)
430 buffer[MaxTextExtent];
452 assert(image_info != (ImageInfo *) NULL);
453 assert(image_info->signature == MagickSignature);
454 assert(image != (Image *) NULL);
455 assert(image->signature == MagickSignature);
456 if (image->debug != MagickFalse)
457 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
458 assert(image->matte != MagickFalse);
461 Note BeginData DSC comment for update later.
463 start=TellBlob(image);
464 (void) FormatMagickString(buffer,MaxTextExtent,
465 "%%%%BeginData:%13ld %s Bytes\n",0L,
466 compression == NoCompression ? "ASCII" : "BINARY");
467 (void) WriteBlobString(image,buffer);
468 stop=TellBlob(image);
470 Only lossless compressions for the mask.
477 (void) FormatMagickString(buffer,MaxTextExtent,
478 "currentfile %lu %lu "PS3_NoCompression" ByteStreamDecodeFilter\n",
479 image->columns,image->rows);
483 case Group4Compression:
485 (void) FormatMagickString(buffer,MaxTextExtent,
486 "currentfile %lu %lu "PS3_FaxCompression" ByteStreamDecodeFilter\n",
487 image->columns,image->rows);
492 (void) FormatMagickString(buffer,MaxTextExtent,
493 "currentfile %lu %lu "PS3_LZWCompression" ByteStreamDecodeFilter\n",
494 image->columns,image->rows);
499 (void) FormatMagickString(buffer,MaxTextExtent,
500 "currentfile %lu %lu "PS3_RLECompression" ByteStreamDecodeFilter\n",
501 image->columns,image->rows);
506 (void) FormatMagickString(buffer,MaxTextExtent,
507 "currentfile %lu %lu "PS3_ZipCompression" ByteStreamDecodeFilter\n",
508 image->columns,image->rows);
512 (void) WriteBlobString(image,buffer);
513 (void) WriteBlobString(image,"/ReusableStreamDecode filter\n");
514 mask_image=CloneImage(image,0,0,MagickTrue,&image->exception);
515 if (mask_image == (Image *) NULL)
516 ThrowWriterException(CoderError,image->exception.reason);
517 status=SeparateImageChannel(mask_image,OpacityChannel);
518 if (status == MagickFalse)
520 mask_image=DestroyImage(mask_image);
523 (void) SetImageType(mask_image,BilevelType);
524 (void) SetImageType(mask_image,PaletteType);
525 mask_image->matte=MagickFalse;
526 pixels=(unsigned char *) NULL;
533 status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
534 if (status == MagickFalse)
536 Ascii85Initialize(image);
537 for (i=0; i < (long) length; i++)
538 Ascii85Encode(image,pixels[i]);
540 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
544 case Group4Compression:
546 if ((compression == FaxCompression) ||
547 (LocaleCompare(CCITTParam,"0") == 0))
548 status=HuffmanEncodeImage(image_info,image,mask_image);
550 status=Huffman2DEncodeImage(image_info,image,mask_image);
555 status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
556 if (status == MagickFalse)
558 status=LZWEncodeImage(image,length,pixels);
559 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
564 status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
565 if (status == MagickFalse)
567 status=PackbitsEncodeImage(image,length,pixels);
568 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
573 status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
574 if (status == MagickFalse)
576 status=ZLIBEncodeImage(image,length,pixels);
577 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
581 mask_image=DestroyImage(mask_image);
582 (void) WriteBlobByte(image,'\n');
583 length=(size_t) (TellBlob(image)-stop);
584 stop=TellBlob(image);
585 offset=SeekBlob(image,start,SEEK_SET);
587 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
588 (void) FormatMagickString(buffer,MaxTextExtent,
589 "%%%%BeginData:%13ld %s Bytes\n",(long) length,
590 compression == NoCompression ? "ASCII" : "BINARY");
591 (void) WriteBlobString(image,buffer);
592 offset=SeekBlob(image,stop,SEEK_SET);
594 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
595 (void) WriteBlobString(image,"%%EndData\n");
596 (void) WriteBlobString(image, "/mask_stream exch def\n");
600 static MagickBooleanType WritePS3Image(const ImageInfo *image_info,Image *image)
605 "/ByteStreamDecodeFilter",
610 " z "PS3_NoCompression" eq { /ASCII85Decode filter } if",
611 " z "PS3_FaxCompression" eq",
618 " /CCITTFaxDecode filter",
620 " z "PS3_JPEGCompression" eq { /DCTDecode filter } if",
621 " z "PS3_LZWCompression" eq { /LZWDecode filter } if",
622 " z "PS3_RLECompression" eq { /RunLengthDecode filter } if",
623 " z "PS3_ZipCompression" eq { /FlateDecode filter } if",
626 "/DirectClassImageDict",
628 " colorspace "PS3_RGBColorspace" eq",
630 " /DeviceRGB setcolorspace",
635 " /BitsPerComponent 8",
636 " /DataSource pixel_stream",
637 " /MultipleDataSources false",
638 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
639 " /Decode [0 1 0 1 0 1]",
643 " /DeviceCMYK setcolorspace",
648 " /BitsPerComponent 8",
649 " /DataSource pixel_stream",
650 " /MultipleDataSources false",
651 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
653 " compression "PS3_JPEGCompression" eq",
654 " { [1 0 1 0 1 0 1 0] }",
655 " { [0 1 0 1 0 1 0 1] }",
662 "/PseudoClassImageDict",
664 " % Colors in colormap image.",
665 " currentfile buffer readline pop",
666 " token pop /colors exch def pop",
669 " % Depth of grayscale image.",
670 " currentfile buffer readline pop",
671 " token pop /bits exch def pop",
672 " /DeviceGray setcolorspace",
677 " /BitsPerComponent bits",
679 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
680 " /DataSource pixel_stream",
685 " /colormap colors 3 mul string def",
686 " compression "PS3_NoCompression" eq",
687 " { currentfile /ASCII85Decode filter colormap readstring pop pop }",
688 " { currentfile colormap readstring pop pop }",
690 " [ /Indexed /DeviceRGB colors 1 sub colormap ] setcolorspace",
695 " /BitsPerComponent 8",
697 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
698 " /DataSource pixel_stream",
704 "/NonMaskedImageDict",
706 " class "PS3_PseudoClass" eq",
707 " { PseudoClassImageDict }",
708 " { DirectClassImageDict }",
716 " /InterleaveType 3",
717 " /DataDict NonMaskedImageDict",
723 " /BitsPerComponent 1",
724 " /DataSource mask_stream",
725 " /MultipleDataSources false",
726 " /ImageMatrix [ columns 0 0 rows neg 0 rows]",
737 " /buffer 512 string def",
739 " currentfile buffer readline pop",
740 " token pop /x exch def",
741 " token pop /y exch def pop",
743 " % Image size and font size.",
744 " currentfile buffer readline pop",
745 " token pop /x exch def",
746 " token pop /y exch def pop",
747 " currentfile buffer readline pop",
748 " token pop /pointsize exch def pop",
755 " currentfile buffer readline pop",
756 " token pop /clipped exch def pop",
758 " currentfile buffer readline pop",
759 " token pop /sp exch def pop",
760 " % Image pixel size.",
761 " currentfile buffer readline pop",
762 " token pop /columns exch def",
763 " token pop /rows exch def pop",
764 " % Colorspace (RGB/CMYK).",
765 " currentfile buffer readline pop",
766 " token pop /colorspace exch def pop",
768 " currentfile buffer readline pop",
769 " token pop /alpha exch def pop",
771 " currentfile buffer readline pop",
772 " token pop /stencil exch def pop",
773 " % Image class (direct/pseudo).",
774 " currentfile buffer readline pop",
775 " token pop /class exch def pop",
776 " % Compression type.",
777 " currentfile buffer readline pop",
778 " token pop /compression exch def pop",
779 " % Clip and render.",
780 " /pixel_stream currentfile columns rows compression ByteStreamDecodeFilter def",
781 " clipped { ClipImage } if",
782 " alpha stencil not and",
783 " { MaskedImageDict mask_stream resetfile }",
784 " { NonMaskedImageDict }",
786 " stencil { 0 setgray imagemask } { image } ifelse",
787 " sp { showpage } if",
793 buffer[MaxTextExtent],
796 page_geometry[MaxTextExtent];
858 Open output image file.
860 assert(image_info != (const ImageInfo *) NULL);
861 assert(image_info->signature == MagickSignature);
862 assert(image != (Image *) NULL);
863 assert(image->signature == MagickSignature);
864 if (image->debug != MagickFalse)
865 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
866 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
867 if (status == MagickFalse)
869 compression=image->compression;
870 if (image_info->compression != UndefinedCompression)
871 compression=image_info->compression;
875 case Group4Compression:
877 if ((IsMonochromeImage(image,&image->exception) == MagickFalse) ||
878 (image->matte != MagickFalse))
879 compression=RLECompression;
882 #if !defined(MAGICKCORE_JPEG_DELEGATE)
883 case JPEGCompression:
885 compression=RLECompression;
886 (void) ThrowMagickException(&image->exception,GetMagickModule(),
887 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
892 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
895 compression=RLECompression;
896 (void) ThrowMagickException(&image->exception,GetMagickModule(),
897 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZLIB)",
905 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
911 Scale relative to dots-per-inch.
913 delta.x=DefaultResolution;
914 delta.y=DefaultResolution;
915 resolution.x=image->x_resolution;
916 resolution.y=image->y_resolution;
917 if ((resolution.x == 0.0) || (resolution.y == 0.0))
919 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
920 resolution.x=geometry_info.rho;
921 resolution.y=geometry_info.sigma;
922 if ((flags & SigmaValue) == 0)
923 resolution.y=resolution.x;
925 if (image_info->density != (char *) NULL)
927 flags=ParseGeometry(image_info->density,&geometry_info);
928 resolution.x=geometry_info.rho;
929 resolution.y=geometry_info.sigma;
930 if ((flags & SigmaValue) == 0)
931 resolution.y=resolution.x;
933 if (image->units == PixelsPerCentimeterResolution)
935 resolution.x=(unsigned long) (100.0*2.54*resolution.x+0.5)/100.0;
936 resolution.y=(unsigned long) (100.0*2.54*resolution.y+0.5)/100.0;
938 SetGeometry(image,&geometry);
939 (void) FormatMagickString(page_geometry,MaxTextExtent,"%lux%lu",
940 image->columns,image->rows);
941 if (image_info->page != (char *) NULL)
942 (void) CopyMagickString(page_geometry,image_info->page,MaxTextExtent);
944 if ((image->page.width != 0) && (image->page.height != 0))
945 (void) FormatMagickString(page_geometry,MaxTextExtent,"%lux%lu%+ld%+ld",
946 image->page.width,image->page.height,image->page.x,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=(unsigned long) floor(scale.x+0.5);
956 scale.y=(double) (geometry.height*delta.y)/resolution.y;
957 geometry.height=(unsigned long) 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=(long) (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=(unsigned long) (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) FormatMagickString(buffer,MaxTextExtent,
986 "%%%%Creator: ImageMagick %s\n",MagickLibVersionText);
987 (void) WriteBlobString(image,buffer);
988 (void) FormatMagickString(buffer,MaxTextExtent,"%%%%Title: %s\n",
990 (void) WriteBlobString(image,buffer);
991 timer=time((time_t *) NULL);
992 (void) FormatMagickTime(timer,MaxTextExtent,date);
993 (void) FormatMagickString(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) FormatMagickString(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) FormatMagickString(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 (IsGrayImage(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) FormatMagickString(buffer,MaxTextExtent,"%%%%Pages: %lu\n",
1045 (unsigned long) 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=(long) MultilineCensus(value)-1; i >= 0; i--)
1068 (void) WriteBlobString(image,
1069 " currentfile buffer readline pop token pop\n");
1070 (void) FormatMagickString(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) FormatMagickString(buffer,MaxTextExtent,"%%%%Page: 1 %lu\n",page);
1086 (void) WriteBlobString(image,buffer);
1090 (void) FormatMagickString(buffer,MaxTextExtent,
1091 "%%%%PageBoundingBox: %ld %ld %ld %ld\n",geometry.x,geometry.y,geometry.x+
1092 (long) geometry.width,geometry.y+(long) (geometry.height+text_size));
1093 (void) WriteBlobString(image,buffer);
1095 Page process colors if not RGB.
1097 if (image->colorspace == CMYKColorspace)
1098 (void) WriteBlobString(image,
1099 "%%PageProcessColors: Cyan Magenta Yellow Black\n");
1101 if (IsGrayImage(image,&image->exception) != MagickFalse)
1102 (void) WriteBlobString(image,"%%PageProcessColors: Black\n");
1104 Adjust document bounding box to bound page bounding box.
1106 if ((double) geometry.x < bounds.x1)
1107 bounds.x1=(double) geometry.x;
1108 if ((double) geometry.y < bounds.y1)
1109 bounds.y1=(double) geometry.y;
1110 if ((double) (geometry.x+scale.x) > bounds.x2)
1111 bounds.x2=(double) geometry.x+scale.x;
1112 if ((double) (geometry.y+scale.y+text_size) > bounds.y2)
1113 bounds.y2=(double) geometry.y+scale.y+text_size;
1115 Page font resource if there's a label.
1117 value=GetImageProperty(image,"label");
1118 if (value != (const char *) NULL)
1119 (void) WriteBlobString(image,"%%PageResources: font Helvetica\n");
1121 PS clipping path from Photoshop clipping path.
1123 if ((image->clip_mask == (Image *) NULL) ||
1124 (LocaleNCompare("8BIM:",image->clip_mask->magick_filename,5) != 0))
1125 (void) WriteBlobString(image,"/ClipImage {} def\n");
1131 value=GetImageProperty(image,image->clip_mask->magick_filename);
1132 if (value == (const char *) NULL)
1133 return(MagickFalse);
1134 (void) WriteBlobString(image,value);
1135 (void) WriteBlobByte(image,'\n');
1138 Push a dictionary for our own def's if this an EPS.
1140 if (LocaleCompare(image_info->magick,"PS3") != 0)
1141 (void) WriteBlobString(image,"userdict begin\n");
1145 if ((image->matte != MagickFalse) &&
1146 (WritePS3MaskImage(image_info,image,compression) == MagickFalse))
1148 (void) CloseBlob(image);
1149 return(MagickFalse);
1152 Remember position of BeginData comment so we can update it.
1154 start=TellBlob(image);
1155 (void) FormatMagickString(buffer,MaxTextExtent,
1156 "%%%%BeginData:%13ld %s Bytes\n",0L,
1157 compression == NoCompression ? "ASCII" : "BINARY");
1158 (void) WriteBlobString(image,buffer);
1159 stop=TellBlob(image);
1160 (void) WriteBlobString(image,"DisplayImage\n");
1162 Translate, scale, and font point size.
1164 (void) FormatMagickString(buffer,MaxTextExtent,
1165 "%ld %ld\n%g %g\n%g\n",geometry.x,geometry.y,scale.x,scale.y,
1167 (void) WriteBlobString(image,buffer);
1171 labels=(char **) NULL;
1172 value=GetImageProperty(image,"label");
1173 if (value != (const char *) NULL)
1174 labels=StringToList(value);
1175 if (labels != (char **) NULL)
1177 for (i=0; labels[i] != (char *) NULL; i++)
1179 if (compression != NoCompression)
1181 for (j=0; labels[i][j] != '\0'; j++)
1182 (void) WriteBlobByte(image,(unsigned char) labels[i][j]);
1183 (void) WriteBlobByte(image,'\n');
1187 (void) WriteBlobString(image,"<~");
1188 Ascii85Initialize(image);
1189 for (j=0; labels[i][j] != '\0'; j++)
1190 Ascii85Encode(image,(unsigned char) labels[i][j]);
1191 Ascii85Flush(image);
1193 labels[i]=DestroyString(labels[i]);
1195 labels=(char **) RelinquishMagickMemory(labels);
1198 Photoshop clipping path active?
1200 if ((image->clip_mask != (Image *) NULL) &&
1201 (LocaleNCompare("8BIM:",image->clip_mask->magick_filename,5) == 0))
1202 (void) WriteBlobString(image,"true\n");
1204 (void) WriteBlobString(image,"false\n");
1206 Showpage for non-EPS.
1208 (void) WriteBlobString(image, LocaleCompare(image_info->magick,"PS3") == 0 ?
1209 "true\n" : "false\n");
1211 Image columns, rows, and color space.
1213 (void) FormatMagickString(buffer,MaxTextExtent,"%lu %lu\n%s\n",
1214 image->columns,image->rows,image->colorspace == CMYKColorspace ?
1215 PS3_CMYKColorspace : PS3_RGBColorspace);
1216 (void) WriteBlobString(image,buffer);
1220 (void) WriteBlobString(image,image->matte != MagickFalse ?
1221 "true\n" : "false\n");
1223 Render with imagemask operator?
1225 option=GetImageOption(image_info,"ps3:imagemask");
1226 (void) WriteBlobString(image,((option != (const char *) NULL) &&
1227 (IsMonochromeImage(image,&image->exception) != MagickFalse)) ?
1228 "true\n" : "false\n");
1232 pixels=(unsigned char *) NULL;
1234 if ((image_info->type != TrueColorType) &&
1235 (image_info->type != TrueColorMatteType) &&
1236 (image_info->type != ColorSeparationType) &&
1237 (image_info->type != ColorSeparationMatteType) &&
1238 (image->colorspace != CMYKColorspace) &&
1239 ((IsGrayImage(image,&image->exception) != MagickFalse) ||
1240 (IsMonochromeImage(image,&image->exception) != MagickFalse)))
1245 (void) WriteBlobString(image,PS3_PseudoClass"\n");
1246 switch (compression)
1251 (void) WriteBlobString(image,PS3_NoCompression"\n");
1254 case FaxCompression:
1255 case Group4Compression:
1257 (void) WriteBlobString(image,PS3_FaxCompression"\n");
1260 case JPEGCompression:
1262 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1265 case LZWCompression:
1267 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1270 case RLECompression:
1272 (void) WriteBlobString(image,PS3_RLECompression"\n");
1275 case ZipCompression:
1277 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1282 Number of colors -- 0 for single component non-color mapped data.
1284 (void) WriteBlobString(image,"0\n");
1286 1 bit or 8 bit components?
1288 (void) FormatMagickString(buffer,MaxTextExtent,"%d\n",
1289 IsMonochromeImage(image,&image->exception) != MagickFalse ? 1 : 8);
1290 (void) WriteBlobString(image,buffer);
1294 if (compression == JPEGCompression)
1295 status=InjectImageBlob(image_info,image,image,"jpeg",
1298 if ((compression == FaxCompression) ||
1299 (compression == Group4Compression))
1301 if (LocaleCompare(CCITTParam,"0") == 0)
1302 status=HuffmanEncodeImage(image_info,image,image);
1304 status=Huffman2DEncodeImage(image_info,image,image);
1308 status=SerializeImageChannel(image_info,image,&pixels,&length);
1309 if (status == MagickFalse)
1311 (void) CloseBlob(image);
1312 return(MagickFalse);
1314 switch (compression)
1319 Ascii85Initialize(image);
1320 for (i=0; i < (long) length; i++)
1321 Ascii85Encode(image,pixels[i]);
1322 Ascii85Flush(image);
1326 case LZWCompression:
1328 status=LZWEncodeImage(image,length,pixels);
1331 case RLECompression:
1333 status=PackbitsEncodeImage(image,length,pixels);
1336 case ZipCompression:
1338 status=ZLIBEncodeImage(image,length,pixels);
1342 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1346 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1347 (compression == JPEGCompression))
1352 (void) WriteBlobString(image,PS3_DirectClass"\n");
1353 switch (compression)
1358 (void) WriteBlobString(image,PS3_NoCompression"\n");
1361 case RLECompression:
1363 (void) WriteBlobString(image,PS3_RLECompression"\n");
1366 case JPEGCompression:
1368 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1371 case LZWCompression:
1373 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1376 case ZipCompression:
1378 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1385 if (compression == JPEGCompression)
1386 status=InjectImageBlob(image_info,image,image,"jpeg",
1391 Stream based compressions.
1393 status=SerializeImage(image_info,image,&pixels,&length);
1394 if (status == MagickFalse)
1396 (void) CloseBlob(image);
1397 return(MagickFalse);
1399 switch (compression)
1404 Ascii85Initialize(image);
1405 for (i=0; i < (long) length; i++)
1406 Ascii85Encode(image,pixels[i]);
1407 Ascii85Flush(image);
1411 case RLECompression:
1413 status=PackbitsEncodeImage(image,length,pixels);
1416 case LZWCompression:
1418 status=LZWEncodeImage(image,length,pixels);
1421 case ZipCompression:
1423 status=ZLIBEncodeImage(image,length,pixels);
1427 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1435 (void) WriteBlobString(image,PS3_PseudoClass"\n");
1436 switch (compression)
1441 (void) WriteBlobString(image,PS3_NoCompression"\n");
1444 case JPEGCompression:
1446 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1449 case RLECompression:
1451 (void) WriteBlobString(image,PS3_RLECompression"\n");
1454 case LZWCompression:
1456 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1459 case ZipCompression:
1461 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1466 Number of colors in color map.
1468 (void) FormatMagickString(buffer,MaxTextExtent,"%lu\n",
1470 (void) WriteBlobString(image,buffer);
1472 Color map - uncompressed.
1474 if ((compression != NoCompression) &&
1475 (compression != UndefinedCompression))
1477 for (i=0; i < (long) image->colors; i++)
1479 pixel=ScaleQuantumToChar(image->colormap[i].red);
1480 (void) WriteBlobByte(image,(unsigned char) pixel);
1481 pixel=ScaleQuantumToChar(image->colormap[i].green);
1482 (void) WriteBlobByte(image,(unsigned char) pixel);
1483 pixel=ScaleQuantumToChar(image->colormap[i].blue);
1484 (void) WriteBlobByte(image,(unsigned char) pixel);
1489 Ascii85Initialize(image);
1490 for (i=0; i < (long) image->colors; i++)
1492 pixel=ScaleQuantumToChar(image->colormap[i].red);
1493 Ascii85Encode(image,(unsigned char) pixel);
1494 pixel=ScaleQuantumToChar(image->colormap[i].green);
1495 Ascii85Encode(image,(unsigned char) pixel);
1496 pixel=ScaleQuantumToChar(image->colormap[i].blue);
1497 Ascii85Encode(image,(unsigned char) pixel);
1499 Ascii85Flush(image);
1501 status=SerializeImageIndexes(image_info,image,&pixels,&length);
1502 if (status == MagickFalse)
1504 (void) CloseBlob(image);
1505 return(MagickFalse);
1507 switch (compression)
1512 Ascii85Initialize(image);
1513 for (i=0; i < (long) length; i++)
1514 Ascii85Encode(image,pixels[i]);
1515 Ascii85Flush(image);
1519 case JPEGCompression:
1521 status=InjectImageBlob(image_info,image,image,"jpeg",
1525 case RLECompression:
1527 status=PackbitsEncodeImage(image,length,pixels);
1530 case LZWCompression:
1532 status=LZWEncodeImage(image,length,pixels);
1535 case ZipCompression:
1537 status=ZLIBEncodeImage(image,length,pixels);
1541 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1543 (void) WriteBlobByte(image,'\n');
1544 if (status == MagickFalse)
1546 (void) CloseBlob(image);
1547 return(MagickFalse);
1550 Update BeginData now that we know the data size.
1552 length=(size_t) (TellBlob(image)-stop);
1553 stop=TellBlob(image);
1554 offset=SeekBlob(image,start,SEEK_SET);
1556 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
1557 (void) FormatMagickString(buffer,MaxTextExtent,
1558 "%%%%BeginData:%13ld %s Bytes\n",(long) length,
1559 compression == NoCompression ? "ASCII" : "BINARY");
1560 (void) WriteBlobString(image,buffer);
1561 offset=SeekBlob(image,stop,SEEK_SET);
1562 (void) WriteBlobString(image,"%%EndData\n");
1564 End private dictionary if this an EPS.
1566 if (LocaleCompare(image_info->magick,"PS3") != 0)
1567 (void) WriteBlobString(image,"end\n");
1568 (void) WriteBlobString(image,"%%PageTrailer\n");
1569 if (GetNextImageInList(image) == (Image *) NULL)
1571 image=SyncNextImageInList(image);
1572 status=SetImageProgress(image,SaveImagesTag,scene++,
1573 GetImageListLength(image));
1574 if (status == MagickFalse)
1576 } while (image_info->adjoin != MagickFalse);
1577 (void) WriteBlobString(image,"%%Trailer\n");
1580 (void) FormatMagickString(buffer,MaxTextExtent,
1581 "%%%%BoundingBox: %g %g %g %g\n",floor(bounds.x1+0.5),
1582 floor(bounds.y1+0.5),ceil(bounds.x2-0.5),ceil(bounds.y2-0.5));
1583 (void) WriteBlobString(image,buffer);
1584 (void) FormatMagickString(buffer,MaxTextExtent,
1585 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,bounds.y1,
1586 bounds.x2,bounds.y2);
1587 (void) WriteBlobString(image,buffer);
1589 (void) WriteBlobString(image,"%%EOF\n");
1590 (void) CloseBlob(image);