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"
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)
212 write_info=CloneImageInfo(image_info);
213 (void) CopyMagickString(write_info->filename,"GROUP4:",MaxTextExtent);
214 (void) CopyMagickString(write_info->magick,"GROUP4",MaxTextExtent);
215 group4_image=CloneImage(inject_image,0,0,MagickTrue,&image->exception);
216 if (group4_image == (Image *) NULL)
218 group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
220 group4_image=DestroyImage(group4_image);
221 if (group4 == (unsigned char *) NULL)
223 write_info=DestroyImageInfo(write_info);
224 if (WriteBlob(image,length,group4) != (ssize_t) length)
226 group4=(unsigned char *) RelinquishMagickMemory(group4);
231 static MagickBooleanType SerializeImage(const ImageInfo *image_info,
232 Image *image,unsigned char **pixels,size_t *length)
240 register const IndexPacket
243 register const PixelPacket
249 register unsigned char
252 assert(image != (Image *) NULL);
253 assert(image->signature == MagickSignature);
254 if (image->debug != MagickFalse)
255 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
257 *length=(image->colorspace == CMYKColorspace ? 4 : 3)*
258 (size_t) image->columns*image->rows;
259 *pixels=(unsigned char *) AcquireQuantumMemory(*length,sizeof(**pixels));
260 if (*pixels == (unsigned char *) NULL)
261 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
263 for (y=0; y < (long) image->rows; y++)
265 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
266 if (p == (const PixelPacket *) NULL)
268 indexes=GetVirtualIndexQueue(image);
269 if (image->colorspace != CMYKColorspace)
270 for (x=0; x < (long) image->columns; x++)
272 *q++=ScaleQuantumToChar(p->red);
273 *q++=ScaleQuantumToChar(p->green);
274 *q++=ScaleQuantumToChar(p->blue);
278 for (x=0; x < (long) image->columns; x++)
280 *q++=ScaleQuantumToChar(p->red);
281 *q++=ScaleQuantumToChar(p->green);
282 *q++=ScaleQuantumToChar(p->blue);
283 *q++=ScaleQuantumToChar(indexes[x]);
286 if (image->previous == (Image *) NULL)
288 status=SetImageProgress(image,SaveImageTag,y,image->rows);
289 if (status == MagickFalse)
293 if (status == MagickFalse)
294 *pixels=(unsigned char *) RelinquishMagickMemory(*pixels);
298 static MagickBooleanType SerializeImageChannel(const ImageInfo *image_info,
299 Image *image,unsigned char **pixels,size_t *length)
307 register const PixelPacket
313 register unsigned char
324 assert(image != (Image *) NULL);
325 assert(image->signature == MagickSignature);
326 if (image->debug != MagickFalse)
327 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
329 pack=IsMonochromeImage(image,&image->exception) == MagickFalse ? 1UL : 8UL;
330 padded_columns=((image->columns+pack-1)/pack)*pack;
331 *length=(size_t) padded_columns*image->rows/pack;
332 *pixels=(unsigned char *) AcquireQuantumMemory(*length,sizeof(**pixels));
333 if (*pixels == (unsigned char *) NULL)
334 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
336 for (y=0; y < (long) image->rows; y++)
338 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
339 if (p == (const PixelPacket *) NULL)
342 for (x=0; x < (long) image->columns; x++)
344 *q++=ScaleQuantumToChar(PixelIntensityToQuantum(p));
350 for (x=0; x < (long) padded_columns; x++)
352 bit=(unsigned char) 0x00;
353 if (x < (long) image->columns)
354 bit=(unsigned char) (PixelIntensityToQuantum(p) ==
355 (Quantum) TransparentOpacity ? 0x01 : 0x00);
356 code=(code << 1)+bit;
357 if (((x+1) % pack) == 0)
365 status=SetImageProgress(image,SaveImageTag,y,image->rows);
366 if (status == MagickFalse)
369 if (status == MagickFalse)
370 *pixels=(unsigned char *) RelinquishMagickMemory(*pixels);
374 static MagickBooleanType SerializeImageIndexes(const ImageInfo *image_info,
375 Image *image,unsigned char **pixels,size_t *length)
383 register const IndexPacket
386 register const PixelPacket
392 register unsigned char
395 assert(image != (Image *) NULL);
396 assert(image->signature == MagickSignature);
397 if (image->debug != MagickFalse)
398 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
400 *length=(size_t) image->columns*image->rows;
401 *pixels=(unsigned char *) AcquireQuantumMemory(*length,sizeof(**pixels));
402 if (*pixels == (unsigned char *) NULL)
403 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
405 for (y=0; y < (long) image->rows; y++)
407 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
408 if (p == (const PixelPacket *) NULL)
410 indexes=GetVirtualIndexQueue(image);
411 for (x=0; x < (long) image->columns; x++)
412 *q++=(unsigned char) indexes[x];
413 if (image->previous == (Image *) NULL)
415 status=SetImageProgress(image,SaveImageTag,y,image->rows);
416 if (status == MagickFalse)
420 if (status == MagickFalse)
421 *pixels=(unsigned char *) RelinquishMagickMemory(*pixels);
425 static MagickBooleanType WritePS3MaskImage(const ImageInfo *image_info,
426 Image *image,const CompressionType compression)
429 buffer[MaxTextExtent];
451 assert(image_info != (ImageInfo *) NULL);
452 assert(image_info->signature == MagickSignature);
453 assert(image != (Image *) NULL);
454 assert(image->signature == MagickSignature);
455 if (image->debug != MagickFalse)
456 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
457 assert(image->matte != MagickFalse);
460 Note BeginData DSC comment for update later.
462 start=TellBlob(image);
463 (void) FormatMagickString(buffer,MaxTextExtent,
464 "%%%%BeginData:%13ld %s Bytes\n",0L,
465 compression == NoCompression ? "ASCII" : "BINARY");
466 (void) WriteBlobString(image,buffer);
467 stop=TellBlob(image);
469 Only lossless compressions for the mask.
476 (void) FormatMagickString(buffer,MaxTextExtent,
477 "currentfile %lu %lu "PS3_NoCompression" ByteStreamDecodeFilter\n",
478 image->columns,image->rows);
482 case Group4Compression:
484 (void) FormatMagickString(buffer,MaxTextExtent,
485 "currentfile %lu %lu "PS3_FaxCompression" ByteStreamDecodeFilter\n",
486 image->columns,image->rows);
491 (void) FormatMagickString(buffer,MaxTextExtent,
492 "currentfile %lu %lu "PS3_LZWCompression" ByteStreamDecodeFilter\n",
493 image->columns,image->rows);
498 (void) FormatMagickString(buffer,MaxTextExtent,
499 "currentfile %lu %lu "PS3_RLECompression" ByteStreamDecodeFilter\n",
500 image->columns,image->rows);
505 (void) FormatMagickString(buffer,MaxTextExtent,
506 "currentfile %lu %lu "PS3_ZipCompression" ByteStreamDecodeFilter\n",
507 image->columns,image->rows);
511 (void) WriteBlobString(image,buffer);
512 (void) WriteBlobString(image,"/ReusableStreamDecode filter\n");
513 mask_image=CloneImage(image,0,0,MagickTrue,&image->exception);
514 if (mask_image == (Image *) NULL)
515 ThrowWriterException(CoderError,image->exception.reason);
516 status=SeparateImageChannel(mask_image,OpacityChannel);
517 if (status == MagickFalse)
519 mask_image=DestroyImage(mask_image);
522 (void) SetImageType(mask_image,BilevelType);
523 (void) SetImageType(mask_image,PaletteType);
524 mask_image->matte=MagickFalse;
525 pixels=(unsigned char *) NULL;
532 status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
533 if (status == MagickFalse)
535 Ascii85Initialize(image);
536 for (i=0; i < (long) length; i++)
537 Ascii85Encode(image,pixels[i]);
539 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
543 case Group4Compression:
545 if ((compression == FaxCompression) ||
546 (LocaleCompare(CCITTParam,"0") == 0))
547 status=HuffmanEncodeImage(image_info,image,mask_image);
549 status=Huffman2DEncodeImage(image_info,image,mask_image);
554 status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
555 if (status == MagickFalse)
557 status=LZWEncodeImage(image,length,pixels);
558 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
563 status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
564 if (status == MagickFalse)
566 status=PackbitsEncodeImage(image,length,pixels);
567 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
572 status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
573 if (status == MagickFalse)
575 status=ZLIBEncodeImage(image,length,pixels);
576 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
580 mask_image=DestroyImage(mask_image);
581 (void) WriteBlobByte(image,'\n');
582 length=(size_t) (TellBlob(image)-stop);
583 stop=TellBlob(image);
584 offset=SeekBlob(image,start,SEEK_SET);
586 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
587 (void) FormatMagickString(buffer,MaxTextExtent,
588 "%%%%BeginData:%13ld %s Bytes\n",(long) length,
589 compression == NoCompression ? "ASCII" : "BINARY");
590 (void) WriteBlobString(image,buffer);
591 offset=SeekBlob(image,stop,SEEK_SET);
593 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
594 (void) WriteBlobString(image,"%%EndData\n");
595 (void) WriteBlobString(image, "/mask_stream exch def\n");
599 static MagickBooleanType WritePS3Image(const ImageInfo *image_info,Image *image)
604 "/ByteStreamDecodeFilter",
609 " z "PS3_NoCompression" eq { /ASCII85Decode filter } if",
610 " z "PS3_FaxCompression" eq",
617 " /CCITTFaxDecode filter",
619 " z "PS3_JPEGCompression" eq { /DCTDecode filter } if",
620 " z "PS3_LZWCompression" eq { /LZWDecode filter } if",
621 " z "PS3_RLECompression" eq { /RunLengthDecode filter } if",
622 " z "PS3_ZipCompression" eq { /FlateDecode filter } if",
625 "/DirectClassImageDict",
627 " colorspace "PS3_RGBColorspace" eq",
629 " /DeviceRGB setcolorspace",
634 " /BitsPerComponent 8",
635 " /DataSource pixel_stream",
636 " /MultipleDataSources false",
637 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
638 " /Decode [0 1 0 1 0 1]",
642 " /DeviceCMYK setcolorspace",
647 " /BitsPerComponent 8",
648 " /DataSource pixel_stream",
649 " /MultipleDataSources false",
650 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
652 " compression "PS3_JPEGCompression" eq",
653 " { [1 0 1 0 1 0 1 0] }",
654 " { [0 1 0 1 0 1 0 1] }",
661 "/PseudoClassImageDict",
663 " % Colors in colormap image.",
664 " currentfile buffer readline pop",
665 " token pop /colors exch def pop",
668 " % Depth of grayscale image.",
669 " currentfile buffer readline pop",
670 " token pop /bits exch def pop",
671 " /DeviceGray setcolorspace",
676 " /BitsPerComponent bits",
678 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
679 " /DataSource pixel_stream",
684 " /colormap colors 3 mul string def",
685 " compression "PS3_NoCompression" eq",
686 " { currentfile /ASCII85Decode filter colormap readstring pop pop }",
687 " { currentfile colormap readstring pop pop }",
689 " [ /Indexed /DeviceRGB colors 1 sub colormap ] setcolorspace",
694 " /BitsPerComponent 8",
696 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
697 " /DataSource pixel_stream",
703 "/NonMaskedImageDict",
705 " class "PS3_PseudoClass" eq",
706 " { PseudoClassImageDict }",
707 " { DirectClassImageDict }",
715 " /InterleaveType 3",
716 " /DataDict NonMaskedImageDict",
722 " /BitsPerComponent 1",
723 " /DataSource mask_stream",
724 " /MultipleDataSources false",
725 " /ImageMatrix [ columns 0 0 rows neg 0 rows]",
736 " /buffer 512 string def",
738 " currentfile buffer readline pop",
739 " token pop /x exch def",
740 " token pop /y exch def pop",
742 " % Image size and font size.",
743 " currentfile buffer readline pop",
744 " token pop /x exch def",
745 " token pop /y exch def pop",
746 " currentfile buffer readline pop",
747 " token pop /pointsize exch def pop",
754 " currentfile buffer readline pop",
755 " token pop /clipped exch def pop",
757 " currentfile buffer readline pop",
758 " token pop /sp exch def pop",
759 " % Image pixel size.",
760 " currentfile buffer readline pop",
761 " token pop /columns exch def",
762 " token pop /rows exch def pop",
763 " % Colorspace (RGB/CMYK).",
764 " currentfile buffer readline pop",
765 " token pop /colorspace exch def pop",
767 " currentfile buffer readline pop",
768 " token pop /alpha exch def pop",
770 " currentfile buffer readline pop",
771 " token pop /stencil exch def pop",
772 " % Image class (direct/pseudo).",
773 " currentfile buffer readline pop",
774 " token pop /class exch def pop",
775 " % Compression type.",
776 " currentfile buffer readline pop",
777 " token pop /compression exch def pop",
778 " % Clip and render.",
779 " /pixel_stream currentfile columns rows compression ByteStreamDecodeFilter def",
780 " clipped { ClipImage } if",
781 " alpha stencil not and",
782 " { MaskedImageDict mask_stream resetfile }",
783 " { NonMaskedImageDict }",
785 " stencil { 0 setgray imagemask } { image } ifelse",
786 " sp { showpage } if",
792 buffer[MaxTextExtent],
795 page_geometry[MaxTextExtent];
857 Open output image file.
859 assert(image_info != (const ImageInfo *) NULL);
860 assert(image_info->signature == MagickSignature);
861 assert(image != (Image *) NULL);
862 assert(image->signature == MagickSignature);
863 if (image->debug != MagickFalse)
864 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
865 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
866 if (status == MagickFalse)
868 compression=image->compression;
869 if (image_info->compression != UndefinedCompression)
870 compression=image_info->compression;
874 case Group4Compression:
876 if ((IsMonochromeImage(image,&image->exception) == MagickFalse) ||
877 (image->matte != MagickFalse))
878 compression=RLECompression;
881 #if !defined(MAGICKCORE_JPEG_DELEGATE)
882 case JPEGCompression:
884 compression=RLECompression;
885 (void) ThrowMagickException(&image->exception,GetMagickModule(),
886 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
891 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
894 compression=RLECompression;
895 (void) ThrowMagickException(&image->exception,GetMagickModule(),
896 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZLIB)",
904 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
910 Scale relative to dots-per-inch.
912 delta.x=DefaultResolution;
913 delta.y=DefaultResolution;
914 resolution.x=image->x_resolution;
915 resolution.y=image->y_resolution;
916 if ((resolution.x == 0.0) || (resolution.y == 0.0))
918 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
919 resolution.x=geometry_info.rho;
920 resolution.y=geometry_info.sigma;
921 if ((flags & SigmaValue) == 0)
922 resolution.y=resolution.x;
924 if (image_info->density != (char *) NULL)
926 flags=ParseGeometry(image_info->density,&geometry_info);
927 resolution.x=geometry_info.rho;
928 resolution.y=geometry_info.sigma;
929 if ((flags & SigmaValue) == 0)
930 resolution.y=resolution.x;
932 if (image->units == PixelsPerCentimeterResolution)
937 SetGeometry(image,&geometry);
938 (void) FormatMagickString(page_geometry,MaxTextExtent,"%lux%lu",
939 image->columns,image->rows);
940 if (image_info->page != (char *) NULL)
941 (void) CopyMagickString(page_geometry,image_info->page,MaxTextExtent);
943 if ((image->page.width != 0) && (image->page.height != 0))
944 (void) FormatMagickString(page_geometry,MaxTextExtent,"%lux%lu%+ld%+ld",
945 image->page.width,image->page.height,image->page.x,image->page.y);
947 if ((image->gravity != UndefinedGravity) &&
948 (LocaleCompare(image_info->magick,"PS") == 0))
949 (void) CopyMagickString(page_geometry,PSPageGeometry,MaxTextExtent);
950 (void) ConcatenateMagickString(page_geometry,">",MaxTextExtent);
951 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
952 &geometry.width,&geometry.height);
953 scale.x=(double) (geometry.width*delta.x)/resolution.x;
954 geometry.width=(unsigned long) (scale.x+0.5);
955 scale.y=(double) (geometry.height*delta.y)/resolution.y;
956 geometry.height=(unsigned long) (scale.y+0.5);
957 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
958 (void) ParseGravityGeometry(image,page_geometry,&page_info,
960 if (image->gravity != UndefinedGravity)
962 geometry.x=(-page_info.x);
963 geometry.y=(long) (media_info.height+page_info.y-image->rows);
966 if (image_info->pointsize != 0.0)
967 pointsize=image_info->pointsize;
969 value=GetImageProperty(image,"label");
970 if (value != (const char *) NULL)
971 text_size=(unsigned long) (MultilineCensus(value)*pointsize+12);
976 Postscript header on the first page.
978 if (LocaleCompare(image_info->magick,"PS3") == 0)
979 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0\n",MaxTextExtent);
981 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0 EPSF-3.0\n",
983 (void) WriteBlobString(image,buffer);
984 (void) FormatMagickString(buffer,MaxTextExtent,
985 "%%%%Creator: ImageMagick %s\n",MagickLibVersionText);
986 (void) WriteBlobString(image,buffer);
987 (void) FormatMagickString(buffer,MaxTextExtent,"%%%%Title: %s\n",
989 (void) WriteBlobString(image,buffer);
990 timer=time((time_t *) NULL);
991 (void) FormatMagickTime(timer,MaxTextExtent,date);
992 (void) FormatMagickString(buffer,MaxTextExtent,
993 "%%%%CreationDate: %s\n",date);
994 (void) WriteBlobString(image,buffer);
995 bounds.x1=(double) geometry.x;
996 bounds.y1=(double) geometry.y;
997 bounds.x2=(double) geometry.x+scale.x;
998 bounds.y2=(double) geometry.y+scale.y+text_size;
999 if ((image_info->adjoin != MagickFalse) &&
1000 (GetNextImageInList(image) != (Image *) NULL))
1002 (void) WriteBlobString(image,"%%BoundingBox: (atend)\n");
1003 (void) WriteBlobString(image,"%%HiResBoundingBox: (atend)\n");
1007 (void) FormatMagickString(buffer,MaxTextExtent,
1008 "%%%%BoundingBox: %g %g %g %g\n",floor(bounds.x1+0.5),
1009 floor(bounds.y1+0.5),ceil(bounds.x2-0.5),ceil(bounds.y2-0.5));
1010 (void) WriteBlobString(image,buffer);
1011 (void) FormatMagickString(buffer,MaxTextExtent,
1012 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,bounds.y1,
1013 bounds.x2,bounds.y2);
1014 (void) WriteBlobString(image,buffer);
1015 if (image->colorspace == CMYKColorspace)
1016 (void) WriteBlobString(image,
1017 "%%DocumentProcessColors: Cyan Magenta Yellow Black\n");
1019 if (IsGrayImage(image,&image->exception) != MagickFalse)
1020 (void) WriteBlobString(image,
1021 "%%DocumentProcessColors: Black\n");
1026 value=GetImageProperty(image,"label");
1027 if (value != (const char *) NULL)
1028 (void) WriteBlobString(image,
1029 "%%DocumentNeededResources: font Helvetica\n");
1030 (void) WriteBlobString(image,"%%LanguageLevel: 3\n");
1032 Pages, orientation and order.
1034 if (LocaleCompare(image_info->magick,"PS3") != 0)
1035 (void) WriteBlobString(image,"%%Pages: 1\n");
1038 (void) WriteBlobString(image,"%%Orientation: Portrait\n");
1039 (void) WriteBlobString(image,"%%PageOrder: Ascend\n");
1040 if (image_info->adjoin == MagickFalse)
1041 (void) CopyMagickString(buffer,"%%Pages: 1\n",MaxTextExtent);
1043 (void) FormatMagickString(buffer,MaxTextExtent,"%%%%Pages: %lu\n",
1044 (unsigned long) GetImageListLength(image));
1045 (void) WriteBlobString(image,buffer);
1047 (void) WriteBlobString(image,"%%EndComments\n");
1049 The static postscript procedures prolog.
1051 (void)WriteBlobString(image,"%%BeginProlog\n");
1052 for (q=PostscriptProlog; *q; q++)
1054 (void) WriteBlobString(image,*q);
1055 (void) WriteBlobByte(image,'\n');
1058 One label line for each line in label string.
1060 value=GetImageProperty(image,"label");
1061 if (value != (const char *) NULL)
1063 (void) WriteBlobString(image,"\n %% Labels.\n /Helvetica "
1064 " findfont pointsize scalefont setfont\n");
1065 for (i=(long) MultilineCensus(value)-1; i >= 0; i--)
1067 (void) WriteBlobString(image,
1068 " currentfile buffer readline pop token pop\n");
1069 (void) FormatMagickString(buffer,MaxTextExtent,
1070 " 0 y %g add moveto show pop\n",i*pointsize+12);
1071 (void) WriteBlobString(image,buffer);
1075 The static postscript procedures epilog.
1077 for (q=PostscriptEpilog; *q; q++)
1079 (void) WriteBlobString(image,*q);
1080 (void) WriteBlobByte(image,'\n');
1082 (void)WriteBlobString(image,"%%EndProlog\n");
1084 (void) FormatMagickString(buffer,MaxTextExtent,"%%%%Page: 1 %lu\n",page);
1085 (void) WriteBlobString(image,buffer);
1089 (void) FormatMagickString(buffer,MaxTextExtent,
1090 "%%%%PageBoundingBox: %ld %ld %ld %ld\n",geometry.x,geometry.y,geometry.x+
1091 (long) geometry.width,geometry.y+(long) (geometry.height+text_size));
1092 (void) WriteBlobString(image,buffer);
1094 Page process colors if not RGB.
1096 if (image->colorspace == CMYKColorspace)
1097 (void) WriteBlobString(image,
1098 "%%PageProcessColors: Cyan Magenta Yellow Black\n");
1100 if (IsGrayImage(image,&image->exception) != MagickFalse)
1101 (void) WriteBlobString(image,"%%PageProcessColors: Black\n");
1103 Adjust document bounding box to bound page bounding box.
1105 if ((double) geometry.x < bounds.x1)
1106 bounds.x1=(double) geometry.x;
1107 if ((double) geometry.y < bounds.y1)
1108 bounds.y1=(double) geometry.y;
1109 if ((double) (geometry.x+scale.x) > bounds.x2)
1110 bounds.x2=(double) geometry.x+scale.x;
1111 if ((double) (geometry.y+scale.y+text_size) > bounds.y2)
1112 bounds.y2=(double) geometry.y+scale.y+text_size;
1114 Page font resource if there's a label.
1116 value=GetImageProperty(image,"label");
1117 if (value != (const char *) NULL)
1118 (void) WriteBlobString(image,"%%PageResources: font Helvetica\n");
1120 PS clipping path from Photoshop clipping path.
1122 if ((image->clip_mask == (Image *) NULL) ||
1123 (LocaleNCompare("8BIM:",image->clip_mask->magick_filename,5) != 0))
1124 (void) WriteBlobString(image,"/ClipImage {} def\n");
1130 value=GetImageProperty(image,image->clip_mask->magick_filename);
1131 if (value == (const char *) NULL)
1132 return(MagickFalse);
1133 (void) WriteBlobString(image,value);
1134 (void) WriteBlobByte(image,'\n');
1137 Push a dictionary for our own def's if this an EPS.
1139 if (LocaleCompare(image_info->magick,"PS3") != 0)
1140 (void) WriteBlobString(image,"userdict begin\n");
1144 if ((image->matte != MagickFalse) &&
1145 (WritePS3MaskImage(image_info,image,compression) == MagickFalse))
1147 (void) CloseBlob(image);
1148 return(MagickFalse);
1151 Remember position of BeginData comment so we can update it.
1153 start=TellBlob(image);
1154 (void) FormatMagickString(buffer,MaxTextExtent,
1155 "%%%%BeginData:%13ld %s Bytes\n",0L,
1156 compression == NoCompression ? "ASCII" : "BINARY");
1157 (void) WriteBlobString(image,buffer);
1158 stop=TellBlob(image);
1159 (void) WriteBlobString(image,"DisplayImage\n");
1161 Translate, scale, and font point size.
1163 (void) FormatMagickString(buffer,MaxTextExtent,"%ld %ld\n%g %g\n%f\n",
1164 geometry.x,geometry.y,scale.x,scale.y,pointsize);
1165 (void) WriteBlobString(image,buffer);
1169 labels=(char **) NULL;
1170 value=GetImageProperty(image,"label");
1171 if (value != (const char *) NULL)
1172 labels=StringToList(value);
1173 if (labels != (char **) NULL)
1175 for (i=0; labels[i] != (char *) NULL; i++)
1177 if (compression != NoCompression)
1179 for (j=0; labels[i][j] != '\0'; j++)
1180 (void) WriteBlobByte(image,(unsigned char) labels[i][j]);
1181 (void) WriteBlobByte(image,'\n');
1185 (void) WriteBlobString(image,"<~");
1186 Ascii85Initialize(image);
1187 for (j=0; labels[i][j] != '\0'; j++)
1188 Ascii85Encode(image,(unsigned char) labels[i][j]);
1189 Ascii85Flush(image);
1191 labels[i]=DestroyString(labels[i]);
1193 labels=(char **) RelinquishMagickMemory(labels);
1196 Photoshop clipping path active?
1198 if ((image->clip_mask != (Image *) NULL) &&
1199 (LocaleNCompare("8BIM:",image->clip_mask->magick_filename,5) == 0))
1200 (void) WriteBlobString(image,"true\n");
1202 (void) WriteBlobString(image,"false\n");
1204 Showpage for non-EPS.
1206 (void) WriteBlobString(image, LocaleCompare(image_info->magick,"PS3") == 0 ?
1207 "true\n" : "false\n");
1209 Image columns, rows, and color space.
1211 (void) FormatMagickString(buffer,MaxTextExtent,"%lu %lu\n%s\n",
1212 image->columns,image->rows,image->colorspace == CMYKColorspace ?
1213 PS3_CMYKColorspace : PS3_RGBColorspace);
1214 (void) WriteBlobString(image,buffer);
1218 (void) WriteBlobString(image,image->matte != MagickFalse ?
1219 "true\n" : "false\n");
1221 Render with imagemask operator?
1223 option=GetImageOption(image_info,"ps3:imagemask");
1224 (void) WriteBlobString(image,((option != (const char *) NULL) &&
1225 (IsMonochromeImage(image,&image->exception) != MagickFalse)) ?
1226 "true\n" : "false\n");
1230 pixels=(unsigned char *) NULL;
1232 if ((image_info->type != TrueColorType) &&
1233 (image_info->type != TrueColorMatteType) &&
1234 (image_info->type != ColorSeparationType) &&
1235 (image_info->type != ColorSeparationMatteType) &&
1236 (image->colorspace != CMYKColorspace) &&
1237 ((IsGrayImage(image,&image->exception) != MagickFalse) ||
1238 (IsMonochromeImage(image,&image->exception) != MagickFalse)))
1243 (void) WriteBlobString(image,PS3_PseudoClass"\n");
1244 switch (compression)
1249 (void) WriteBlobString(image,PS3_NoCompression"\n");
1252 case FaxCompression:
1253 case Group4Compression:
1255 (void) WriteBlobString(image,PS3_FaxCompression"\n");
1258 case JPEGCompression:
1260 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1263 case LZWCompression:
1265 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1268 case RLECompression:
1270 (void) WriteBlobString(image,PS3_RLECompression"\n");
1273 case ZipCompression:
1275 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1280 Number of colors -- 0 for single component non-color mapped data.
1282 (void) WriteBlobString(image,"0\n");
1284 1 bit or 8 bit components?
1286 (void) FormatMagickString(buffer,MaxTextExtent,"%d\n",
1287 IsMonochromeImage(image,&image->exception) != MagickFalse ? 1 : 8);
1288 (void) WriteBlobString(image,buffer);
1292 if (compression == JPEGCompression)
1293 status=InjectImageBlob(image_info,image,image,"jpeg",
1296 if ((compression == FaxCompression) ||
1297 (compression == Group4Compression))
1299 if (LocaleCompare(CCITTParam,"0") == 0)
1300 status=HuffmanEncodeImage(image_info,image,image);
1302 status=Huffman2DEncodeImage(image_info,image,image);
1306 status=SerializeImageChannel(image_info,image,&pixels,&length);
1307 if (status == MagickFalse)
1309 (void) CloseBlob(image);
1310 return(MagickFalse);
1312 switch (compression)
1317 Ascii85Initialize(image);
1318 for (i=0; i < (long) length; i++)
1319 Ascii85Encode(image,pixels[i]);
1320 Ascii85Flush(image);
1324 case LZWCompression:
1326 status=LZWEncodeImage(image,length,pixels);
1329 case RLECompression:
1331 status=PackbitsEncodeImage(image,length,pixels);
1334 case ZipCompression:
1336 status=ZLIBEncodeImage(image,length,pixels);
1340 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1344 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1345 (compression == JPEGCompression))
1350 (void) WriteBlobString(image,PS3_DirectClass"\n");
1351 switch (compression)
1356 (void) WriteBlobString(image,PS3_NoCompression"\n");
1359 case RLECompression:
1361 (void) WriteBlobString(image,PS3_RLECompression"\n");
1364 case JPEGCompression:
1366 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1369 case LZWCompression:
1371 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1374 case ZipCompression:
1376 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1383 if (compression == JPEGCompression)
1384 status=InjectImageBlob(image_info,image,image,"jpeg",
1389 Stream based compressions.
1391 status=SerializeImage(image_info,image,&pixels,&length);
1392 if (status == MagickFalse)
1394 (void) CloseBlob(image);
1395 return(MagickFalse);
1397 switch (compression)
1402 Ascii85Initialize(image);
1403 for (i=0; i < (long) length; i++)
1404 Ascii85Encode(image,pixels[i]);
1405 Ascii85Flush(image);
1409 case RLECompression:
1411 status=PackbitsEncodeImage(image,length,pixels);
1414 case LZWCompression:
1416 status=LZWEncodeImage(image,length,pixels);
1419 case ZipCompression:
1421 status=ZLIBEncodeImage(image,length,pixels);
1425 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1433 (void) WriteBlobString(image,PS3_PseudoClass"\n");
1434 switch (compression)
1439 (void) WriteBlobString(image,PS3_NoCompression"\n");
1442 case JPEGCompression:
1444 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1447 case RLECompression:
1449 (void) WriteBlobString(image,PS3_RLECompression"\n");
1452 case LZWCompression:
1454 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1457 case ZipCompression:
1459 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1464 Number of colors in color map.
1466 (void) FormatMagickString(buffer,MaxTextExtent,"%lu\n",
1468 (void) WriteBlobString(image,buffer);
1470 Color map - uncompressed.
1472 if ((compression != NoCompression) &&
1473 (compression != UndefinedCompression))
1475 for (i=0; i < (long) image->colors; i++)
1477 pixel=ScaleQuantumToChar(image->colormap[i].red);
1478 (void) WriteBlobByte(image,(unsigned char) pixel);
1479 pixel=ScaleQuantumToChar(image->colormap[i].green);
1480 (void) WriteBlobByte(image,(unsigned char) pixel);
1481 pixel=ScaleQuantumToChar(image->colormap[i].blue);
1482 (void) WriteBlobByte(image,(unsigned char) pixel);
1487 Ascii85Initialize(image);
1488 for (i=0; i < (long) image->colors; i++)
1490 pixel=ScaleQuantumToChar(image->colormap[i].red);
1491 Ascii85Encode(image,(unsigned char) pixel);
1492 pixel=ScaleQuantumToChar(image->colormap[i].green);
1493 Ascii85Encode(image,(unsigned char) pixel);
1494 pixel=ScaleQuantumToChar(image->colormap[i].blue);
1495 Ascii85Encode(image,(unsigned char) pixel);
1497 Ascii85Flush(image);
1499 status=SerializeImageIndexes(image_info,image,&pixels,&length);
1500 if (status == MagickFalse)
1502 (void) CloseBlob(image);
1503 return(MagickFalse);
1505 switch (compression)
1510 Ascii85Initialize(image);
1511 for (i=0; i < (long) length; i++)
1512 Ascii85Encode(image,pixels[i]);
1513 Ascii85Flush(image);
1517 case JPEGCompression:
1519 status=InjectImageBlob(image_info,image,image,"jpeg",
1523 case RLECompression:
1525 status=PackbitsEncodeImage(image,length,pixels);
1528 case LZWCompression:
1530 status=LZWEncodeImage(image,length,pixels);
1533 case ZipCompression:
1535 status=ZLIBEncodeImage(image,length,pixels);
1539 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1541 (void) WriteBlobByte(image,'\n');
1542 if (status == MagickFalse)
1544 (void) CloseBlob(image);
1545 return(MagickFalse);
1548 Update BeginData now that we know the data size.
1550 length=(size_t) (TellBlob(image)-stop);
1551 stop=TellBlob(image);
1552 offset=SeekBlob(image,start,SEEK_SET);
1554 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
1555 (void) FormatMagickString(buffer,MaxTextExtent,
1556 "%%%%BeginData:%13ld %s Bytes\n",(long) length,
1557 compression == NoCompression ? "ASCII" : "BINARY");
1558 (void) WriteBlobString(image,buffer);
1559 offset=SeekBlob(image,stop,SEEK_SET);
1560 (void) WriteBlobString(image,"%%EndData\n");
1562 End private dictionary if this an EPS.
1564 if (LocaleCompare(image_info->magick,"PS3") != 0)
1565 (void) WriteBlobString(image,"end\n");
1566 (void) WriteBlobString(image,"%%PageTrailer\n");
1567 if (GetNextImageInList(image) == (Image *) NULL)
1569 image=SyncNextImageInList(image);
1570 status=SetImageProgress(image,SaveImagesTag,scene++,
1571 GetImageListLength(image));
1572 if (status == MagickFalse)
1574 } while (image_info->adjoin != MagickFalse);
1575 (void) WriteBlobString(image,"%%Trailer\n");
1578 (void) FormatMagickString(buffer,MaxTextExtent,
1579 "%%%%BoundingBox: %g %g %g %g\n",floor(bounds.x1+0.5),
1580 floor(bounds.y1+0.5),ceil(bounds.x2-0.5),ceil(bounds.y2-0.5));
1581 (void) WriteBlobString(image,buffer);
1582 (void) FormatMagickString(buffer,MaxTextExtent,
1583 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,bounds.y1,
1584 bounds.x2,bounds.y2);
1585 (void) WriteBlobString(image,buffer);
1587 (void) WriteBlobString(image,"%%EOF\n");
1588 (void) CloseBlob(image);