2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Write Postscript Level III Format %
21 % Copyright 1999-2009 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"
72 #if defined(MAGICKCORE_TIFF_DELEGATE)
73 #define CCITTParam "-1"
75 #define CCITTParam "0"
81 #define PS3_NoCompression "0"
82 #define PS3_FaxCompression "1"
83 #define PS3_JPEGCompression "2"
84 #define PS3_LZWCompression "3"
85 #define PS3_RLECompression "4"
86 #define PS3_ZipCompression "5"
88 #define PS3_RGBColorspace "0"
89 #define PS3_CMYKColorspace "1"
91 #define PS3_DirectClass "0"
92 #define PS3_PseudoClass "1"
97 static MagickBooleanType
98 WritePS3Image(const ImageInfo *,Image *);
101 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105 % R e g i s t e r P S 3 I m a g e %
109 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111 % RegisterPS3Image() adds properties for the PS3 image format to the list of
112 % supported formats. The properties include the image format tag, a method to
113 % read and/or write the format, whether the format supports the saving of more
114 % than one frame to the same file or blob, whether the format supports native
115 % in-memory I/O, and a brief description of the format.
117 % The format of the RegisterPS3Image method is:
119 % unsigned long RegisterPS3Image(void)
122 ModuleExport unsigned long RegisterPS3Image(void)
127 entry=SetMagickInfo("EPS3");
128 entry->encoder=(EncodeImageHandler *) WritePS3Image;
129 entry->description=ConstantString("Level III Encapsulated PostScript");
130 entry->module=ConstantString("PS3");
131 (void) RegisterMagickInfo(entry);
132 entry=SetMagickInfo("PS3");
133 entry->encoder=(EncodeImageHandler *) WritePS3Image;
134 entry->description=ConstantString("Level III PostScript");
135 entry->module=ConstantString("PS3");
136 (void) RegisterMagickInfo(entry);
137 return(MagickImageCoderSignature);
141 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
145 % U n r e g i s t e r P S 3 I m a g e %
149 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151 % UnregisterPS3Image() removes format registrations made by the PS3 module
152 % from the list of supported formats.
154 % The format of the UnregisterPS3Image method is:
156 % UnregisterPS3Image(void)
159 ModuleExport void UnregisterPS3Image(void)
161 (void) UnregisterMagickInfo("EPS3");
162 (void) UnregisterMagickInfo("PS3");
166 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
170 % W r i t e P S 3 I m a g e %
174 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
176 % WritePS3Image() translates an image to encapsulated Postscript Level III
177 % for printing. If the supplied geometry is null, the image is centered on
178 % the Postscript page. Otherwise, the image is positioned as specified by the
181 % The format of the WritePS3Image method is:
183 % MagickBooleanType WritePS3Image(const ImageInfo *image_info,Image *image)
185 % A description of each parameter follows:
187 % o image_info: Specifies a pointer to a ImageInfo structure.
189 % o image: the image.
193 static MagickBooleanType SerializeImage(const ImageInfo *image_info,
194 Image *image,unsigned char **pixels,size_t *length)
202 register const IndexPacket
205 register const PixelPacket
211 register unsigned char
214 assert(image != (Image *) NULL);
215 assert(image->signature == MagickSignature);
216 if (image->debug != MagickFalse)
217 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
219 *length=(image->colorspace == CMYKColorspace ? 4 : 3)*
220 (size_t) image->columns*image->rows;
221 *pixels=(unsigned char *) AcquireQuantumMemory(*length,sizeof(**pixels));
222 if (*pixels == (unsigned char *) NULL)
223 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
225 for (y=0; y < (long) image->rows; y++)
227 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
228 if (p == (const PixelPacket *) NULL)
230 indexes=GetVirtualIndexQueue(image);
231 if (image->colorspace != CMYKColorspace)
232 for (x=0; x < (long) image->columns; x++)
234 *q++=ScaleQuantumToChar(p->red);
235 *q++=ScaleQuantumToChar(p->green);
236 *q++=ScaleQuantumToChar(p->blue);
240 for (x=0; x < (long) image->columns; x++)
242 *q++=ScaleQuantumToChar(p->red);
243 *q++=ScaleQuantumToChar(p->green);
244 *q++=ScaleQuantumToChar(p->blue);
245 *q++=ScaleQuantumToChar(indexes[x]);
248 if (image->previous == (Image *) NULL)
250 status=SetImageProgress(image,SaveImageTag,y,image->rows);
251 if (status == MagickFalse)
255 if (status == MagickFalse)
256 *pixels=(unsigned char *) RelinquishMagickMemory(*pixels);
260 static MagickBooleanType SerializeImageChannel(const ImageInfo *image_info,
261 Image *image,unsigned char **pixels,size_t *length)
269 register const PixelPacket
275 register unsigned char
286 assert(image != (Image *) NULL);
287 assert(image->signature == MagickSignature);
288 if (image->debug != MagickFalse)
289 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
291 pack=IsMonochromeImage(image,&image->exception) == MagickFalse ? 1UL : 8UL;
292 padded_columns=((image->columns+pack-1)/pack)*pack;
293 *length=(size_t) padded_columns*image->rows/pack;
294 *pixels=(unsigned char *) AcquireQuantumMemory(*length,sizeof(**pixels));
295 if (*pixels == (unsigned char *) NULL)
296 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
298 for (y=0; y < (long) image->rows; y++)
300 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
301 if (p == (const PixelPacket *) NULL)
304 for (x=0; x < (long) image->columns; x++)
306 *q++=ScaleQuantumToChar(PixelIntensityToQuantum(p));
312 for (x=0; x < (long) padded_columns; x++)
314 bit=(unsigned char) 0x00;
315 if (x < (long) image->columns)
316 bit=(unsigned char) (PixelIntensityToQuantum(p) ==
317 (Quantum) TransparentOpacity ? 0x01 : 0x00);
318 code=(code << 1)+bit;
319 if (((x+1) % pack) == 0)
327 status=SetImageProgress(image,SaveImageTag,y,image->rows);
328 if (status == MagickFalse)
331 if (status == MagickFalse)
332 *pixels=(unsigned char *) RelinquishMagickMemory(*pixels);
336 static MagickBooleanType SerializeImageIndexes(const ImageInfo *image_info,
337 Image *image,unsigned char **pixels,size_t *length)
345 register const IndexPacket
348 register const PixelPacket
354 register unsigned char
357 assert(image != (Image *) NULL);
358 assert(image->signature == MagickSignature);
359 if (image->debug != MagickFalse)
360 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
362 *length=(size_t) image->columns*image->rows;
363 *pixels=(unsigned char *) AcquireQuantumMemory(*length,sizeof(**pixels));
364 if (*pixels == (unsigned char *) NULL)
365 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
367 for (y=0; y < (long) image->rows; y++)
369 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
370 if (p == (const PixelPacket *) NULL)
372 indexes=GetVirtualIndexQueue(image);
373 for (x=0; x < (long) image->columns; x++)
374 *q++=(unsigned char) indexes[x];
375 if (image->previous == (Image *) NULL)
377 status=SetImageProgress(image,SaveImageTag,y,image->rows);
378 if (status == MagickFalse)
382 if (status == MagickFalse)
383 *pixels=(unsigned char *) RelinquishMagickMemory(*pixels);
387 static MagickBooleanType WritePS3MaskImage(const ImageInfo *image_info,
388 Image *image,const CompressionType compression)
391 buffer[MaxTextExtent];
413 assert(image_info != (ImageInfo *) NULL);
414 assert(image_info->signature == MagickSignature);
415 assert(image != (Image *) NULL);
416 assert(image->signature == MagickSignature);
417 if (image->debug != MagickFalse)
418 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
419 assert(image->matte != MagickFalse);
422 Note BeginData DSC comment for update later.
424 start=TellBlob(image);
425 (void) FormatMagickString(buffer,MaxTextExtent,
426 "%%%%BeginData:%13ld %s Bytes\n",0L,
427 compression == NoCompression ? "ASCII" : "BINARY");
428 (void) WriteBlobString(image,buffer);
429 stop=TellBlob(image);
431 Only lossless compressions for the mask.
438 (void) FormatMagickString(buffer,MaxTextExtent,
439 "currentfile %lu %lu "PS3_NoCompression" ByteStreamDecodeFilter\n",
440 image->columns,image->rows);
444 case Group4Compression:
446 (void) FormatMagickString(buffer,MaxTextExtent,
447 "currentfile %lu %lu "PS3_FaxCompression" ByteStreamDecodeFilter\n",
448 image->columns,image->rows);
453 (void) FormatMagickString(buffer,MaxTextExtent,
454 "currentfile %lu %lu "PS3_LZWCompression" ByteStreamDecodeFilter\n",
455 image->columns,image->rows);
460 (void) FormatMagickString(buffer,MaxTextExtent,
461 "currentfile %lu %lu "PS3_RLECompression" ByteStreamDecodeFilter\n",
462 image->columns,image->rows);
467 (void) FormatMagickString(buffer,MaxTextExtent,
468 "currentfile %lu %lu "PS3_ZipCompression" ByteStreamDecodeFilter\n",
469 image->columns,image->rows);
473 (void) WriteBlobString(image,buffer);
474 (void) WriteBlobString(image,"/ReusableStreamDecode filter\n");
475 mask_image=CloneImage(image,0,0,MagickTrue,&image->exception);
476 if (mask_image == (Image *) NULL)
477 ThrowWriterException(CoderError,image->exception.reason);
478 status=SeparateImageChannel(mask_image,OpacityChannel);
479 if (status == MagickFalse)
481 mask_image=DestroyImage(mask_image);
484 (void) SetImageType(mask_image,BilevelType);
485 (void) SetImageType(mask_image,PaletteType);
486 mask_image->matte=MagickFalse;
487 pixels=(unsigned char *) NULL;
494 status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
495 if (status == MagickFalse)
497 Ascii85Initialize(image);
498 for (i=0; i < (long) length; i++)
499 Ascii85Encode(image,pixels[i]);
501 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
505 case Group4Compression:
507 if ((compression == FaxCompression) ||
508 (LocaleCompare(CCITTParam,"0") == 0))
509 status=HuffmanEncodeImage(image_info,image,mask_image);
511 status=Huffman2DEncodeImage(image_info,image,mask_image);
516 status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
517 if (status == MagickFalse)
519 status=LZWEncodeImage(image,length,pixels);
520 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
525 status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
526 if (status == MagickFalse)
528 status=PackbitsEncodeImage(image,length,pixels);
529 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
534 status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
535 if (status == MagickFalse)
537 status=ZLIBEncodeImage(image,length,pixels);
538 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
542 mask_image=DestroyImage(mask_image);
543 (void) WriteBlobByte(image,'\n');
544 length=(size_t) (TellBlob(image)-stop);
545 stop=TellBlob(image);
546 offset=SeekBlob(image,start,SEEK_SET);
548 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
549 (void) FormatMagickString(buffer,MaxTextExtent,
550 "%%%%BeginData:%13ld %s Bytes\n",(long) length,
551 compression == NoCompression ? "ASCII" : "BINARY");
552 (void) WriteBlobString(image,buffer);
553 offset=SeekBlob(image,stop,SEEK_SET);
555 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
556 (void) WriteBlobString(image,"%%EndData\n");
557 (void) WriteBlobString(image, "/mask_stream exch def\n");
561 static MagickBooleanType WritePS3Image(const ImageInfo *image_info,Image *image)
566 "/ByteStreamDecodeFilter",
571 " z "PS3_NoCompression" eq { /ASCII85Decode filter } if",
572 " z "PS3_FaxCompression" eq",
579 " /CCITTFaxDecode filter",
581 " z "PS3_JPEGCompression" eq { /DCTDecode filter } if",
582 " z "PS3_LZWCompression" eq { /LZWDecode filter } if",
583 " z "PS3_RLECompression" eq { /RunLengthDecode filter } if",
584 " z "PS3_ZipCompression" eq { /FlateDecode filter } if",
587 "/DirectClassImageDict",
589 " colorspace "PS3_RGBColorspace" eq",
591 " /DeviceRGB setcolorspace",
596 " /BitsPerComponent 8",
597 " /DataSource pixel_stream",
598 " /MultipleDataSources false",
599 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
600 " /Decode [0 1 0 1 0 1]",
604 " /DeviceCMYK setcolorspace",
609 " /BitsPerComponent 8",
610 " /DataSource pixel_stream",
611 " /MultipleDataSources false",
612 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
614 " compression "PS3_JPEGCompression" eq",
615 " { [1 0 1 0 1 0 1 0] }",
616 " { [0 1 0 1 0 1 0 1] }",
623 "/PseudoClassImageDict",
625 " % Colors in colormap image.",
626 " currentfile buffer readline pop",
627 " token pop /colors exch def pop",
630 " % Depth of grayscale image.",
631 " currentfile buffer readline pop",
632 " token pop /bits exch def pop",
633 " /DeviceGray setcolorspace",
638 " /BitsPerComponent bits",
640 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
641 " /DataSource pixel_stream",
646 " /colormap colors 3 mul string def",
647 " compression "PS3_NoCompression" eq",
648 " { currentfile /ASCII85Decode filter colormap readstring pop pop }",
649 " { currentfile colormap readstring pop pop }",
651 " [ /Indexed /DeviceRGB colors 1 sub colormap ] setcolorspace",
656 " /BitsPerComponent 8",
658 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
659 " /DataSource pixel_stream",
665 "/NonMaskedImageDict",
667 " class "PS3_PseudoClass" eq",
668 " { PseudoClassImageDict }",
669 " { DirectClassImageDict }",
677 " /InterleaveType 3",
678 " /DataDict NonMaskedImageDict",
684 " /BitsPerComponent 1",
685 " /DataSource mask_stream",
686 " /MultipleDataSources false",
687 " /ImageMatrix [ columns 0 0 rows neg 0 rows]",
698 " /buffer 512 string def",
700 " currentfile buffer readline pop",
701 " token pop /x exch def",
702 " token pop /y exch def pop",
704 " % Image size and font size.",
705 " currentfile buffer readline pop",
706 " token pop /x exch def",
707 " token pop /y exch def pop",
708 " currentfile buffer readline pop",
709 " token pop /pointsize exch def pop",
716 " currentfile buffer readline pop",
717 " token pop /clipped exch def pop",
719 " currentfile buffer readline pop",
720 " token pop /sp exch def pop",
721 " % Image pixel size.",
722 " currentfile buffer readline pop",
723 " token pop /columns exch def",
724 " token pop /rows exch def pop",
725 " % Colorspace (RGB/CMYK).",
726 " currentfile buffer readline pop",
727 " token pop /colorspace exch def pop",
729 " currentfile buffer readline pop",
730 " token pop /alpha exch def pop",
732 " currentfile buffer readline pop",
733 " token pop /stencil exch def pop",
734 " % Image class (direct/pseudo).",
735 " currentfile buffer readline pop",
736 " token pop /class exch def pop",
737 " % Compression type.",
738 " currentfile buffer readline pop",
739 " token pop /compression exch def pop",
740 " % Clip and render.",
741 " /pixel_stream currentfile columns rows compression ByteStreamDecodeFilter def",
742 " clipped { ClipImage } if",
743 " alpha stencil not and",
744 " { MaskedImageDict mask_stream resetfile }",
745 " { NonMaskedImageDict }",
747 " stencil { 0 setgray imagemask } { image } ifelse",
748 " sp { showpage } if",
754 buffer[MaxTextExtent],
757 page_geometry[MaxTextExtent];
819 Open output image file.
821 assert(image_info != (const ImageInfo *) NULL);
822 assert(image_info->signature == MagickSignature);
823 assert(image != (Image *) NULL);
824 assert(image->signature == MagickSignature);
825 if (image->debug != MagickFalse)
826 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
827 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
828 if (status == MagickFalse)
830 compression=image->compression;
831 if (image_info->compression != UndefinedCompression)
832 compression=image_info->compression;
836 case Group4Compression:
838 if ((IsMonochromeImage(image,&image->exception) == MagickFalse) ||
839 (image->matte != MagickFalse))
840 compression=RLECompression;
843 #if !defined(MAGICKCORE_JPEG_DELEGATE)
844 case JPEGCompression:
846 compression=RLECompression;
847 (void) ThrowMagickException(&image->exception,GetMagickModule(),
848 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
853 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
856 compression=RLECompression;
857 (void) ThrowMagickException(&image->exception,GetMagickModule(),
858 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZLIB)",
866 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
872 Scale relative to dots-per-inch.
874 delta.x=DefaultResolution;
875 delta.y=DefaultResolution;
876 resolution.x=image->x_resolution;
877 resolution.y=image->y_resolution;
878 if ((resolution.x == 0.0) || (resolution.y == 0.0))
880 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
881 resolution.x=geometry_info.rho;
882 resolution.y=geometry_info.sigma;
883 if ((flags & SigmaValue) == 0)
884 resolution.y=resolution.x;
886 if (image_info->density != (char *) NULL)
888 flags=ParseGeometry(image_info->density,&geometry_info);
889 resolution.x=geometry_info.rho;
890 resolution.y=geometry_info.sigma;
891 if ((flags & SigmaValue) == 0)
892 resolution.y=resolution.x;
894 if (image->units == PixelsPerCentimeterResolution)
899 SetGeometry(image,&geometry);
900 (void) FormatMagickString(page_geometry,MaxTextExtent,"%lux%lu",
901 image->columns,image->rows);
902 if (image_info->page != (char *) NULL)
903 (void) CopyMagickString(page_geometry,image_info->page,MaxTextExtent);
905 if ((image->page.width != 0) && (image->page.height != 0))
906 (void) FormatMagickString(page_geometry,MaxTextExtent,"%lux%lu%+ld%+ld",
907 image->page.width,image->page.height,image->page.x,image->page.y);
909 if ((image->gravity != UndefinedGravity) &&
910 (LocaleCompare(image_info->magick,"PS") == 0))
911 (void) CopyMagickString(page_geometry,PSPageGeometry,MaxTextExtent);
912 (void) ConcatenateMagickString(page_geometry,">",MaxTextExtent);
913 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
914 &geometry.width,&geometry.height);
915 scale.x=(double) (geometry.width*delta.x)/resolution.x;
916 geometry.width=(unsigned long) (scale.x+0.5);
917 scale.y=(double) (geometry.height*delta.y)/resolution.y;
918 geometry.height=(unsigned long) (scale.y+0.5);
919 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
920 (void) ParseGravityGeometry(image,page_geometry,&page_info,
922 if (image->gravity != UndefinedGravity)
924 geometry.x=(-page_info.x);
925 geometry.y=(long) (media_info.height+page_info.y-image->rows);
928 if (image_info->pointsize != 0.0)
929 pointsize=image_info->pointsize;
931 value=GetImageProperty(image,"label");
932 if (value != (const char *) NULL)
933 text_size=(unsigned long) (MultilineCensus(value)*pointsize+12);
938 Postscript header on the first page.
940 if (LocaleCompare(image_info->magick,"PS3") == 0)
941 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0\n",MaxTextExtent);
943 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0 EPSF-3.0\n",
945 (void) WriteBlobString(image,buffer);
946 (void) FormatMagickString(buffer,MaxTextExtent,
947 "%%%%Creator: ImageMagick %s\n",MagickLibVersionText);
948 (void) WriteBlobString(image,buffer);
949 (void) FormatMagickString(buffer,MaxTextExtent,"%%%%Title: %s\n",
951 (void) WriteBlobString(image,buffer);
952 timer=time((time_t *) NULL);
953 (void) FormatMagickTime(timer,MaxTextExtent,date);
954 (void) FormatMagickString(buffer,MaxTextExtent,
955 "%%%%CreationDate: %s\n",date);
956 (void) WriteBlobString(image,buffer);
957 bounds.x1=(double) geometry.x;
958 bounds.y1=(double) geometry.y;
959 bounds.x2=(double) geometry.x+scale.x;
960 bounds.y2=(double) geometry.y+scale.y+text_size;
961 if ((image_info->adjoin != MagickFalse) &&
962 (GetNextImageInList(image) != (Image *) NULL))
964 (void) WriteBlobString(image,"%%BoundingBox: (atend)\n");
965 (void) WriteBlobString(image,"%%HiResBoundingBox: (atend)\n");
969 (void) FormatMagickString(buffer,MaxTextExtent,
970 "%%%%BoundingBox: %g %g %g %g\n",floor(bounds.x1+0.5),
971 floor(bounds.y1+0.5),ceil(bounds.x2-0.5),ceil(bounds.y2-0.5));
972 (void) WriteBlobString(image,buffer);
973 (void) FormatMagickString(buffer,MaxTextExtent,
974 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,bounds.y1,
975 bounds.x2,bounds.y2);
976 (void) WriteBlobString(image,buffer);
977 if (image->colorspace == CMYKColorspace)
978 (void) WriteBlobString(image,
979 "%%DocumentProcessColors: Cyan Magenta Yellow Black\n");
981 if (IsGrayImage(image,&image->exception) != MagickFalse)
982 (void) WriteBlobString(image,
983 "%%DocumentProcessColors: Black\n");
988 value=GetImageProperty(image,"label");
989 if (value != (const char *) NULL)
990 (void) WriteBlobString(image,
991 "%%DocumentNeededResources: font Helvetica\n");
992 (void) WriteBlobString(image,"%%LanguageLevel: 3\n");
994 Pages, orientation and order.
996 if (LocaleCompare(image_info->magick,"PS3") != 0)
997 (void) WriteBlobString(image,"%%Pages: 1\n");
1000 (void) WriteBlobString(image,"%%Orientation: Portrait\n");
1001 (void) WriteBlobString(image,"%%PageOrder: Ascend\n");
1002 if (image_info->adjoin == MagickFalse)
1003 (void) CopyMagickString(buffer,"%%Pages: 1\n",MaxTextExtent);
1005 (void) FormatMagickString(buffer,MaxTextExtent,"%%%%Pages: %lu\n",
1006 (unsigned long) GetImageListLength(image));
1007 (void) WriteBlobString(image,buffer);
1009 (void) WriteBlobString(image,"%%EndComments\n");
1011 The static postscript procedures prolog.
1013 (void)WriteBlobString(image,"%%BeginProlog\n");
1014 for (q=PostscriptProlog; *q; q++)
1016 (void) WriteBlobString(image,*q);
1017 (void) WriteBlobByte(image,'\n');
1020 One label line for each line in label string.
1022 value=GetImageProperty(image,"label");
1023 if (value != (const char *) NULL)
1025 (void) WriteBlobString(image,"\n %% Labels.\n /Helvetica "
1026 " findfont pointsize scalefont setfont\n");
1027 for (i=(long) MultilineCensus(value)-1; i >= 0; i--)
1029 (void) WriteBlobString(image,
1030 " currentfile buffer readline pop token pop\n");
1031 (void) FormatMagickString(buffer,MaxTextExtent,
1032 " 0 y %g add moveto show pop\n",i*pointsize+12);
1033 (void) WriteBlobString(image,buffer);
1037 The static postscript procedures epilog.
1039 for (q=PostscriptEpilog; *q; q++)
1041 (void) WriteBlobString(image,*q);
1042 (void) WriteBlobByte(image,'\n');
1044 (void)WriteBlobString(image,"%%EndProlog\n");
1046 (void) FormatMagickString(buffer,MaxTextExtent,"%%%%Page: 1 %lu\n",page);
1047 (void) WriteBlobString(image,buffer);
1051 (void) FormatMagickString(buffer,MaxTextExtent,
1052 "%%%%PageBoundingBox: %ld %ld %ld %ld\n",geometry.x,geometry.y,geometry.x+
1053 (long) geometry.width,geometry.y+(long) (geometry.height+text_size));
1054 (void) WriteBlobString(image,buffer);
1056 Page process colors if not RGB.
1058 if (image->colorspace == CMYKColorspace)
1059 (void) WriteBlobString(image,
1060 "%%PageProcessColors: Cyan Magenta Yellow Black\n");
1062 if (IsGrayImage(image,&image->exception) != MagickFalse)
1063 (void) WriteBlobString(image,"%%PageProcessColors: Black\n");
1065 Adjust document bounding box to bound page bounding box.
1067 if ((double) geometry.x < bounds.x1)
1068 bounds.x1=(double) geometry.x;
1069 if ((double) geometry.y < bounds.y1)
1070 bounds.y1=(double) geometry.y;
1071 if ((double) (geometry.x+scale.x) > bounds.x2)
1072 bounds.x2=(double) geometry.x+scale.x;
1073 if ((double) (geometry.y+scale.y+text_size) > bounds.y2)
1074 bounds.y2=(double) geometry.y+scale.y+text_size;
1076 Page font resource if there's a label.
1078 value=GetImageProperty(image,"label");
1079 if (value != (const char *) NULL)
1080 (void) WriteBlobString(image,"%%PageResources: font Helvetica\n");
1082 PS clipping path from Photoshop clipping path.
1084 if ((image->clip_mask == (Image *) NULL) ||
1085 (LocaleNCompare("8BIM:",image->clip_mask->magick_filename,5) != 0))
1086 (void) WriteBlobString(image,"/ClipImage {} def\n");
1092 value=GetImageProperty(image,image->clip_mask->magick_filename);
1093 if (value == (const char *) NULL)
1094 return(MagickFalse);
1095 (void) WriteBlobString(image,value);
1096 (void) WriteBlobByte(image,'\n');
1099 Push a dictionary for our own def's if this an EPS.
1101 if (LocaleCompare(image_info->magick,"PS3") != 0)
1102 (void) WriteBlobString(image,"userdict begin\n");
1106 if ((image->matte != MagickFalse) &&
1107 (WritePS3MaskImage(image_info,image,compression) == MagickFalse))
1109 (void) CloseBlob(image);
1110 return(MagickFalse);
1113 Remember position of BeginData comment so we can update it.
1115 start=TellBlob(image);
1116 (void) FormatMagickString(buffer,MaxTextExtent,
1117 "%%%%BeginData:%13ld %s Bytes\n",0L,
1118 compression == NoCompression ? "ASCII" : "BINARY");
1119 (void) WriteBlobString(image,buffer);
1120 stop=TellBlob(image);
1121 (void) WriteBlobString(image,"DisplayImage\n");
1123 Translate, scale, and font point size.
1125 (void) FormatMagickString(buffer,MaxTextExtent,"%ld %ld\n%g %g\n%f\n",
1126 geometry.x,geometry.y,scale.x,scale.y,pointsize);
1127 (void) WriteBlobString(image,buffer);
1131 labels=(char **) NULL;
1132 value=GetImageProperty(image,"label");
1133 if (value != (const char *) NULL)
1134 labels=StringToList(value);
1135 if (labels != (char **) NULL)
1137 for (i=0; labels[i] != (char *) NULL; i++)
1139 if (compression != NoCompression)
1141 for (j=0; labels[i][j] != '\0'; j++)
1142 (void) WriteBlobByte(image,(unsigned char) labels[i][j]);
1143 (void) WriteBlobByte(image,'\n');
1147 (void) WriteBlobString(image,"<~");
1148 Ascii85Initialize(image);
1149 for (j=0; labels[i][j] != '\0'; j++)
1150 Ascii85Encode(image,(unsigned char) labels[i][j]);
1151 Ascii85Flush(image);
1153 labels[i]=DestroyString(labels[i]);
1155 labels=(char **) RelinquishMagickMemory(labels);
1158 Photoshop clipping path active?
1160 if ((image->clip_mask != (Image *) NULL) &&
1161 (LocaleNCompare("8BIM:",image->clip_mask->magick_filename,5) == 0))
1162 (void) WriteBlobString(image,"true\n");
1164 (void) WriteBlobString(image,"false\n");
1166 Showpage for non-EPS.
1168 (void) WriteBlobString(image, LocaleCompare(image_info->magick,"PS3") == 0 ?
1169 "true\n" : "false\n");
1171 Image columns, rows, and color space.
1173 (void) FormatMagickString(buffer,MaxTextExtent,"%lu %lu\n%s\n",
1174 image->columns,image->rows,image->colorspace == CMYKColorspace ?
1175 PS3_CMYKColorspace : PS3_RGBColorspace);
1176 (void) WriteBlobString(image,buffer);
1180 (void) WriteBlobString(image,image->matte != MagickFalse ?
1181 "true\n" : "false\n");
1183 Render with imagemask operator?
1185 option=GetImageOption(image_info,"ps3:imagemask");
1186 (void) WriteBlobString(image,((option != (const char *) NULL) &&
1187 (IsMonochromeImage(image,&image->exception) != MagickFalse)) ?
1188 "true\n" : "false\n");
1192 pixels=(unsigned char *) NULL;
1194 if ((image_info->type != TrueColorType) &&
1195 (image_info->type != TrueColorMatteType) &&
1196 (image_info->type != ColorSeparationType) &&
1197 (image_info->type != ColorSeparationMatteType) &&
1198 (image->colorspace != CMYKColorspace) &&
1199 ((IsGrayImage(image,&image->exception) != MagickFalse) ||
1200 (IsMonochromeImage(image,&image->exception) != MagickFalse)))
1205 (void) WriteBlobString(image,PS3_PseudoClass"\n");
1206 switch (compression)
1211 (void) WriteBlobString(image,PS3_NoCompression"\n");
1214 case FaxCompression:
1215 case Group4Compression:
1217 (void) WriteBlobString(image,PS3_FaxCompression"\n");
1220 case JPEGCompression:
1222 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1225 case LZWCompression:
1227 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1230 case RLECompression:
1232 (void) WriteBlobString(image,PS3_RLECompression"\n");
1235 case ZipCompression:
1237 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1242 Number of colors -- 0 for single component non-color mapped data.
1244 (void) WriteBlobString(image,"0\n");
1246 1 bit or 8 bit components?
1248 (void) FormatMagickString(buffer,MaxTextExtent,"%d\n",
1249 IsMonochromeImage(image,&image->exception) != MagickFalse ? 1 : 8);
1250 (void) WriteBlobString(image,buffer);
1254 if (compression == JPEGCompression)
1255 status=InjectImageBlob(image_info,image,image,"jpeg",
1258 if ((compression == FaxCompression) ||
1259 (compression == Group4Compression))
1261 if (LocaleCompare(CCITTParam,"0") == 0)
1262 status=HuffmanEncodeImage(image_info,image,image);
1264 status=Huffman2DEncodeImage(image_info,image,image);
1268 status=SerializeImageChannel(image_info,image,&pixels,&length);
1269 if (status == MagickFalse)
1271 (void) CloseBlob(image);
1272 return(MagickFalse);
1274 switch (compression)
1279 Ascii85Initialize(image);
1280 for (i=0; i < (long) length; i++)
1281 Ascii85Encode(image,pixels[i]);
1282 Ascii85Flush(image);
1286 case LZWCompression:
1288 status=LZWEncodeImage(image,length,pixels);
1291 case RLECompression:
1293 status=PackbitsEncodeImage(image,length,pixels);
1296 case ZipCompression:
1298 status=ZLIBEncodeImage(image,length,pixels);
1302 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1306 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1307 (compression == JPEGCompression))
1312 (void) WriteBlobString(image,PS3_DirectClass"\n");
1313 switch (compression)
1318 (void) WriteBlobString(image,PS3_NoCompression"\n");
1321 case RLECompression:
1323 (void) WriteBlobString(image,PS3_RLECompression"\n");
1326 case JPEGCompression:
1328 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1331 case LZWCompression:
1333 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1336 case ZipCompression:
1338 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1345 if (compression == JPEGCompression)
1346 status=InjectImageBlob(image_info,image,image,"jpeg",
1351 Stream based compressions.
1353 status=SerializeImage(image_info,image,&pixels,&length);
1354 if (status == MagickFalse)
1356 (void) CloseBlob(image);
1357 return(MagickFalse);
1359 switch (compression)
1364 Ascii85Initialize(image);
1365 for (i=0; i < (long) length; i++)
1366 Ascii85Encode(image,pixels[i]);
1367 Ascii85Flush(image);
1371 case RLECompression:
1373 status=PackbitsEncodeImage(image,length,pixels);
1376 case LZWCompression:
1378 status=LZWEncodeImage(image,length,pixels);
1381 case ZipCompression:
1383 status=ZLIBEncodeImage(image,length,pixels);
1387 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1395 (void) WriteBlobString(image,PS3_PseudoClass"\n");
1396 switch (compression)
1401 (void) WriteBlobString(image,PS3_NoCompression"\n");
1404 case JPEGCompression:
1406 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1409 case RLECompression:
1411 (void) WriteBlobString(image,PS3_RLECompression"\n");
1414 case LZWCompression:
1416 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1419 case ZipCompression:
1421 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1426 Number of colors in color map.
1428 (void) FormatMagickString(buffer,MaxTextExtent,"%lu\n",
1430 (void) WriteBlobString(image,buffer);
1432 Color map - uncompressed.
1434 if ((compression != NoCompression) &&
1435 (compression != UndefinedCompression))
1437 for (i=0; i < (long) image->colors; i++)
1439 pixel=ScaleQuantumToChar(image->colormap[i].red);
1440 (void) WriteBlobByte(image,(unsigned char) pixel);
1441 pixel=ScaleQuantumToChar(image->colormap[i].green);
1442 (void) WriteBlobByte(image,(unsigned char) pixel);
1443 pixel=ScaleQuantumToChar(image->colormap[i].blue);
1444 (void) WriteBlobByte(image,(unsigned char) pixel);
1449 Ascii85Initialize(image);
1450 for (i=0; i < (long) image->colors; i++)
1452 pixel=ScaleQuantumToChar(image->colormap[i].red);
1453 Ascii85Encode(image,(unsigned char) pixel);
1454 pixel=ScaleQuantumToChar(image->colormap[i].green);
1455 Ascii85Encode(image,(unsigned char) pixel);
1456 pixel=ScaleQuantumToChar(image->colormap[i].blue);
1457 Ascii85Encode(image,(unsigned char) pixel);
1459 Ascii85Flush(image);
1461 status=SerializeImageIndexes(image_info,image,&pixels,&length);
1462 if (status == MagickFalse)
1464 (void) CloseBlob(image);
1465 return(MagickFalse);
1467 switch (compression)
1472 Ascii85Initialize(image);
1473 for (i=0; i < (long) length; i++)
1474 Ascii85Encode(image,pixels[i]);
1475 Ascii85Flush(image);
1479 case JPEGCompression:
1481 status=InjectImageBlob(image_info,image,image,"jpeg",
1485 case RLECompression:
1487 status=PackbitsEncodeImage(image,length,pixels);
1490 case LZWCompression:
1492 status=LZWEncodeImage(image,length,pixels);
1495 case ZipCompression:
1497 status=ZLIBEncodeImage(image,length,pixels);
1501 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1503 (void) WriteBlobByte(image,'\n');
1504 if (status == MagickFalse)
1506 (void) CloseBlob(image);
1507 return(MagickFalse);
1510 Update BeginData now that we know the data size.
1512 length=(size_t) (TellBlob(image)-stop);
1513 stop=TellBlob(image);
1514 offset=SeekBlob(image,start,SEEK_SET);
1516 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
1517 (void) FormatMagickString(buffer,MaxTextExtent,
1518 "%%%%BeginData:%13ld %s Bytes\n",(long) length,
1519 compression == NoCompression ? "ASCII" : "BINARY");
1520 (void) WriteBlobString(image,buffer);
1521 offset=SeekBlob(image,stop,SEEK_SET);
1522 (void) WriteBlobString(image,"%%EndData\n");
1524 End private dictionary if this an EPS.
1526 if (LocaleCompare(image_info->magick,"PS3") != 0)
1527 (void) WriteBlobString(image,"end\n");
1528 (void) WriteBlobString(image,"%%PageTrailer\n");
1529 if (GetNextImageInList(image) == (Image *) NULL)
1531 image=SyncNextImageInList(image);
1532 status=SetImageProgress(image,SaveImagesTag,scene++,
1533 GetImageListLength(image));
1534 if (status == MagickFalse)
1536 } while (image_info->adjoin != MagickFalse);
1537 (void) WriteBlobString(image,"%%Trailer\n");
1540 (void) FormatMagickString(buffer,MaxTextExtent,
1541 "%%%%BoundingBox: %g %g %g %g\n",floor(bounds.x1+0.5),
1542 floor(bounds.y1+0.5),ceil(bounds.x2-0.5),ceil(bounds.y2-0.5));
1543 (void) WriteBlobString(image,buffer);
1544 (void) FormatMagickString(buffer,MaxTextExtent,
1545 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,bounds.y1,
1546 bounds.x2,bounds.y2);
1547 (void) WriteBlobString(image,buffer);
1549 (void) WriteBlobString(image,"%%EOF\n");
1550 (void) CloseBlob(image);