2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Write Postscript Level III Format %
21 % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
22 % dedicated to making software imaging solutions freely available. %
24 % You may not use this file except in compliance with the License. You may %
25 % obtain a copy of the License at %
27 % http://www.imagemagick.org/script/license.php %
29 % Unless required by applicable law or agreed to in writing, software %
30 % distributed under the License is distributed on an "AS IS" BASIS, %
31 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
32 % See the License for the specific language governing permissions and %
33 % limitations under the License. %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "magick/studio.h"
44 #include "magick/blob.h"
45 #include "magick/blob-private.h"
46 #include "magick/cache.h"
47 #include "magick/color.h"
48 #include "magick/color-private.h"
49 #include "magick/compress.h"
50 #include "magick/constitute.h"
51 #include "magick/draw.h"
52 #include "magick/exception.h"
53 #include "magick/exception-private.h"
54 #include "magick/geometry.h"
55 #include "magick/image.h"
56 #include "magick/image-private.h"
57 #include "magick/list.h"
58 #include "magick/magick.h"
59 #include "magick/memory_.h"
60 #include "magick/monitor.h"
61 #include "magick/monitor-private.h"
62 #include "magick/option.h"
63 #include "magick/property.h"
64 #include "magick/quantum-private.h"
65 #include "magick/resource_.h"
66 #include "magick/static.h"
67 #include "magick/string_.h"
68 #include "magick/module.h"
69 #include "magick/token.h"
70 #include "magick/utility.h"
71 #include "magick/module.h"
76 #define PS3_NoCompression "0"
77 #define PS3_FaxCompression "1"
78 #define PS3_JPEGCompression "2"
79 #define PS3_LZWCompression "3"
80 #define PS3_RLECompression "4"
81 #define PS3_ZipCompression "5"
83 #define PS3_RGBColorspace "0"
84 #define PS3_CMYKColorspace "1"
86 #define PS3_DirectClass "0"
87 #define PS3_PseudoClass "1"
89 #if defined(MAGICKCORE_TIFF_DELEGATE)
90 #define CCITTParam "-1"
92 #define CCITTParam "0"
98 static MagickBooleanType
99 WritePS3Image(const ImageInfo *,Image *);
102 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106 % R e g i s t e r P S 3 I m a g e %
110 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112 % RegisterPS3Image() adds properties for the PS3 image format to the list of
113 % supported formats. The properties include the image format tag, a method to
114 % read and/or write the format, whether the format supports the saving of more
115 % than one frame to the same file or blob, whether the format supports native
116 % in-memory I/O, and a brief description of the format.
118 % The format of the RegisterPS3Image method is:
120 % size_t RegisterPS3Image(void)
123 ModuleExport size_t RegisterPS3Image(void)
128 entry=SetMagickInfo("EPS3");
129 entry->encoder=(EncodeImageHandler *) WritePS3Image;
130 entry->description=ConstantString("Level III Encapsulated PostScript");
131 entry->module=ConstantString("PS3");
132 entry->seekable_stream=MagickTrue;
133 (void) RegisterMagickInfo(entry);
134 entry=SetMagickInfo("PS3");
135 entry->encoder=(EncodeImageHandler *) WritePS3Image;
136 entry->description=ConstantString("Level III PostScript");
137 entry->module=ConstantString("PS3");
138 entry->seekable_stream=MagickTrue;
139 (void) RegisterMagickInfo(entry);
140 return(MagickImageCoderSignature);
144 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148 % U n r e g i s t e r P S 3 I m a g e %
152 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
154 % UnregisterPS3Image() removes format registrations made by the PS3 module
155 % from the list of supported formats.
157 % The format of the UnregisterPS3Image method is:
159 % UnregisterPS3Image(void)
162 ModuleExport void UnregisterPS3Image(void)
164 (void) UnregisterMagickInfo("EPS3");
165 (void) UnregisterMagickInfo("PS3");
169 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
173 % W r i t e P S 3 I m a g e %
177 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
179 % WritePS3Image() translates an image to encapsulated Postscript Level III
180 % for printing. If the supplied geometry is null, the image is centered on
181 % the Postscript page. Otherwise, the image is positioned as specified by the
184 % The format of the WritePS3Image method is:
186 % MagickBooleanType WritePS3Image(const ImageInfo *image_info,Image *image)
188 % A description of each parameter follows:
190 % o image_info: Specifies a pointer to a ImageInfo structure.
192 % o image: the image.
196 static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
197 Image *image,Image *inject_image)
215 write_info=CloneImageInfo(image_info);
216 (void) CopyMagickString(write_info->filename,"GROUP4:",MaxTextExtent);
217 (void) CopyMagickString(write_info->magick,"GROUP4",MaxTextExtent);
218 group4_image=CloneImage(inject_image,0,0,MagickTrue,&image->exception);
219 if (group4_image == (Image *) NULL)
221 group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
223 group4_image=DestroyImage(group4_image);
224 if (group4 == (unsigned char *) NULL)
226 write_info=DestroyImageInfo(write_info);
227 if (WriteBlob(image,length,group4) != (ssize_t) length)
229 group4=(unsigned char *) RelinquishMagickMemory(group4);
234 static MagickBooleanType SerializeImage(const ImageInfo *image_info,
235 Image *image,unsigned char **pixels,size_t *length)
243 register const IndexPacket
246 register const PixelPacket
252 register unsigned char
255 assert(image != (Image *) NULL);
256 assert(image->signature == MagickSignature);
257 if (image->debug != MagickFalse)
258 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
260 *length=(image->colorspace == CMYKColorspace ? 4 : 3)*
261 (size_t) image->columns*image->rows;
262 *pixels=(unsigned char *) AcquireQuantumMemory(*length,sizeof(**pixels));
263 if (*pixels == (unsigned char *) NULL)
264 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
266 for (y=0; y < (ssize_t) image->rows; y++)
268 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
269 if (p == (const PixelPacket *) NULL)
271 indexes=GetVirtualIndexQueue(image);
272 if (image->colorspace != CMYKColorspace)
273 for (x=0; x < (ssize_t) image->columns; x++)
275 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
276 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
277 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
281 for (x=0; x < (ssize_t) image->columns; x++)
283 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
284 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
285 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
286 *q++=ScaleQuantumToChar(indexes[x]);
289 if (image->previous == (Image *) NULL)
291 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
293 if (status == MagickFalse)
297 if (status == MagickFalse)
298 *pixels=(unsigned char *) RelinquishMagickMemory(*pixels);
302 static MagickBooleanType SerializeImageChannel(const ImageInfo *image_info,
303 Image *image,unsigned char **pixels,size_t *length)
311 register const PixelPacket
317 register unsigned char
328 assert(image != (Image *) NULL);
329 assert(image->signature == MagickSignature);
330 if (image->debug != MagickFalse)
331 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
333 pack=IsMonochromeImage(image,&image->exception) == MagickFalse ? 1UL : 8UL;
334 padded_columns=((image->columns+pack-1)/pack)*pack;
335 *length=(size_t) padded_columns*image->rows/pack;
336 *pixels=(unsigned char *) AcquireQuantumMemory(*length,sizeof(**pixels));
337 if (*pixels == (unsigned char *) NULL)
338 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
340 for (y=0; y < (ssize_t) image->rows; y++)
342 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
343 if (p == (const PixelPacket *) NULL)
346 for (x=0; x < (ssize_t) image->columns; x++)
348 *q++=ScaleQuantumToChar(PixelIntensityToQuantum(p));
354 for (x=0; x < (ssize_t) padded_columns; x++)
356 bit=(unsigned char) 0x00;
357 if (x < (ssize_t) image->columns)
358 bit=(unsigned char) (PixelIntensityToQuantum(p) ==
359 (Quantum) TransparentOpacity ? 0x01 : 0x00);
360 code=(code << 1)+bit;
361 if (((x+1) % pack) == 0)
369 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
371 if (status == MagickFalse)
374 if (status == MagickFalse)
375 *pixels=(unsigned char *) RelinquishMagickMemory(*pixels);
379 static MagickBooleanType SerializeImageIndexes(const ImageInfo *image_info,
380 Image *image,unsigned char **pixels,size_t *length)
388 register const IndexPacket
391 register const PixelPacket
397 register unsigned char
400 assert(image != (Image *) NULL);
401 assert(image->signature == MagickSignature);
402 if (image->debug != MagickFalse)
403 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
405 *length=(size_t) image->columns*image->rows;
406 *pixels=(unsigned char *) AcquireQuantumMemory(*length,sizeof(**pixels));
407 if (*pixels == (unsigned char *) NULL)
408 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
410 for (y=0; y < (ssize_t) image->rows; y++)
412 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
413 if (p == (const PixelPacket *) NULL)
415 indexes=GetVirtualIndexQueue(image);
416 for (x=0; x < (ssize_t) image->columns; x++)
417 *q++=(unsigned char) GetIndexPixelComponent(indexes+x);
418 if (image->previous == (Image *) NULL)
420 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
422 if (status == MagickFalse)
426 if (status == MagickFalse)
427 *pixels=(unsigned char *) RelinquishMagickMemory(*pixels);
431 static MagickBooleanType WritePS3MaskImage(const ImageInfo *image_info,
432 Image *image,const CompressionType compression)
435 buffer[MaxTextExtent];
457 assert(image_info != (ImageInfo *) NULL);
458 assert(image_info->signature == MagickSignature);
459 assert(image != (Image *) NULL);
460 assert(image->signature == MagickSignature);
461 if (image->debug != MagickFalse)
462 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
463 assert(image->matte != MagickFalse);
466 Note BeginData DSC comment for update later.
468 start=TellBlob(image);
469 (void) FormatMagickString(buffer,MaxTextExtent,
470 "%%%%BeginData:%13ld %s Bytes\n",0L,
471 compression == NoCompression ? "ASCII" : "BINARY");
472 (void) WriteBlobString(image,buffer);
473 stop=TellBlob(image);
475 Only lossless compressions for the mask.
482 (void) FormatMagickString(buffer,MaxTextExtent,
483 "currentfile %.20g %.20g "PS3_NoCompression" ByteStreamDecodeFilter\n",
484 (double) image->columns,(double) image->rows);
488 case Group4Compression:
490 (void) FormatMagickString(buffer,MaxTextExtent,
491 "currentfile %.20g %.20g "PS3_FaxCompression" ByteStreamDecodeFilter\n",
492 (double) image->columns,(double) image->rows);
497 (void) FormatMagickString(buffer,MaxTextExtent,
498 "currentfile %.20g %.20g "PS3_LZWCompression" ByteStreamDecodeFilter\n",
499 (double) image->columns,(double) image->rows);
504 (void) FormatMagickString(buffer,MaxTextExtent,
505 "currentfile %.20g %.20g "PS3_RLECompression" ByteStreamDecodeFilter\n",
506 (double) image->columns,(double) image->rows);
511 (void) FormatMagickString(buffer,MaxTextExtent,
512 "currentfile %.20g %.20g "PS3_ZipCompression" ByteStreamDecodeFilter\n",
513 (double) image->columns,(double) image->rows);
517 (void) WriteBlobString(image,buffer);
518 (void) WriteBlobString(image,"/ReusableStreamDecode filter\n");
519 mask_image=CloneImage(image,0,0,MagickTrue,&image->exception);
520 if (mask_image == (Image *) NULL)
521 ThrowWriterException(CoderError,image->exception.reason);
522 status=SeparateImageChannel(mask_image,OpacityChannel);
523 if (status == MagickFalse)
525 mask_image=DestroyImage(mask_image);
528 (void) SetImageType(mask_image,BilevelType);
529 (void) SetImageType(mask_image,PaletteType);
530 mask_image->matte=MagickFalse;
531 pixels=(unsigned char *) NULL;
538 status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
539 if (status == MagickFalse)
541 Ascii85Initialize(image);
542 for (i=0; i < (ssize_t) length; i++)
543 Ascii85Encode(image,pixels[i]);
545 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
549 case Group4Compression:
551 if ((compression == FaxCompression) ||
552 (LocaleCompare(CCITTParam,"0") == 0))
553 status=HuffmanEncodeImage(image_info,image,mask_image);
555 status=Huffman2DEncodeImage(image_info,image,mask_image);
560 status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
561 if (status == MagickFalse)
563 status=LZWEncodeImage(image,length,pixels);
564 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
569 status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
570 if (status == MagickFalse)
572 status=PackbitsEncodeImage(image,length,pixels);
573 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
578 status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
579 if (status == MagickFalse)
581 status=ZLIBEncodeImage(image,length,pixels);
582 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
586 mask_image=DestroyImage(mask_image);
587 (void) WriteBlobByte(image,'\n');
588 length=(size_t) (TellBlob(image)-stop);
589 stop=TellBlob(image);
590 offset=SeekBlob(image,start,SEEK_SET);
592 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
593 (void) FormatMagickString(buffer,MaxTextExtent,
594 "%%%%BeginData:%13ld %s Bytes\n",(long) length,
595 compression == NoCompression ? "ASCII" : "BINARY");
596 (void) WriteBlobString(image,buffer);
597 offset=SeekBlob(image,stop,SEEK_SET);
599 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
600 (void) WriteBlobString(image,"%%EndData\n");
601 (void) WriteBlobString(image, "/mask_stream exch def\n");
605 static MagickBooleanType WritePS3Image(const ImageInfo *image_info,Image *image)
610 "/ByteStreamDecodeFilter",
615 " z "PS3_NoCompression" eq { /ASCII85Decode filter } if",
616 " z "PS3_FaxCompression" eq",
623 " /CCITTFaxDecode filter",
625 " z "PS3_JPEGCompression" eq { /DCTDecode filter } if",
626 " z "PS3_LZWCompression" eq { /LZWDecode filter } if",
627 " z "PS3_RLECompression" eq { /RunLengthDecode filter } if",
628 " z "PS3_ZipCompression" eq { /FlateDecode filter } if",
631 "/DirectClassImageDict",
633 " colorspace "PS3_RGBColorspace" eq",
635 " /DeviceRGB setcolorspace",
640 " /BitsPerComponent 8",
641 " /DataSource pixel_stream",
642 " /MultipleDataSources false",
643 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
644 " /Decode [0 1 0 1 0 1]",
648 " /DeviceCMYK setcolorspace",
653 " /BitsPerComponent 8",
654 " /DataSource pixel_stream",
655 " /MultipleDataSources false",
656 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
658 " compression "PS3_JPEGCompression" eq",
659 " { [1 0 1 0 1 0 1 0] }",
660 " { [0 1 0 1 0 1 0 1] }",
667 "/PseudoClassImageDict",
669 " % Colors in colormap image.",
670 " currentfile buffer readline pop",
671 " token pop /colors exch def pop",
674 " % Depth of grayscale image.",
675 " currentfile buffer readline pop",
676 " token pop /bits exch def pop",
677 " /DeviceGray setcolorspace",
682 " /BitsPerComponent bits",
684 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
685 " /DataSource pixel_stream",
690 " /colormap colors 3 mul string def",
691 " compression "PS3_NoCompression" eq",
692 " { currentfile /ASCII85Decode filter colormap readstring pop pop }",
693 " { currentfile colormap readstring pop pop }",
695 " [ /Indexed /DeviceRGB colors 1 sub colormap ] setcolorspace",
700 " /BitsPerComponent 8",
702 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
703 " /DataSource pixel_stream",
709 "/NonMaskedImageDict",
711 " class "PS3_PseudoClass" eq",
712 " { PseudoClassImageDict }",
713 " { DirectClassImageDict }",
721 " /InterleaveType 3",
722 " /DataDict NonMaskedImageDict",
728 " /BitsPerComponent 1",
729 " /DataSource mask_stream",
730 " /MultipleDataSources false",
731 " /ImageMatrix [ columns 0 0 rows neg 0 rows]",
742 " /buffer 512 string def",
744 " currentfile buffer readline pop",
745 " token pop /x exch def",
746 " token pop /y exch def pop",
748 " % Image size and font size.",
749 " currentfile buffer readline pop",
750 " token pop /x exch def",
751 " token pop /y exch def pop",
752 " currentfile buffer readline pop",
753 " token pop /pointsize exch def pop",
760 " currentfile buffer readline pop",
761 " token pop /clipped exch def pop",
763 " currentfile buffer readline pop",
764 " token pop /sp exch def pop",
765 " % Image pixel size.",
766 " currentfile buffer readline pop",
767 " token pop /columns exch def",
768 " token pop /rows exch def pop",
769 " % Colorspace (RGB/CMYK).",
770 " currentfile buffer readline pop",
771 " token pop /colorspace exch def pop",
773 " currentfile buffer readline pop",
774 " token pop /alpha exch def pop",
776 " currentfile buffer readline pop",
777 " token pop /stencil exch def pop",
778 " % Image class (direct/pseudo).",
779 " currentfile buffer readline pop",
780 " token pop /class exch def pop",
781 " % Compression type.",
782 " currentfile buffer readline pop",
783 " token pop /compression exch def pop",
784 " % Clip and render.",
785 " /pixel_stream currentfile columns rows compression ByteStreamDecodeFilter def",
786 " clipped { ClipImage } if",
787 " alpha stencil not and",
788 " { MaskedImageDict mask_stream resetfile }",
789 " { NonMaskedImageDict }",
791 " stencil { 0 setgray imagemask } { image } ifelse",
792 " sp { showpage } if",
798 buffer[MaxTextExtent],
801 page_geometry[MaxTextExtent];
863 Open output image file.
865 assert(image_info != (const ImageInfo *) NULL);
866 assert(image_info->signature == MagickSignature);
867 assert(image != (Image *) NULL);
868 assert(image->signature == MagickSignature);
869 if (image->debug != MagickFalse)
870 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
871 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
872 if (status == MagickFalse)
874 compression=image->compression;
875 if (image_info->compression != UndefinedCompression)
876 compression=image_info->compression;
880 case Group4Compression:
882 if ((IsMonochromeImage(image,&image->exception) == MagickFalse) ||
883 (image->matte != MagickFalse))
884 compression=RLECompression;
887 #if !defined(MAGICKCORE_JPEG_DELEGATE)
888 case JPEGCompression:
890 compression=RLECompression;
891 (void) ThrowMagickException(&image->exception,GetMagickModule(),
892 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
897 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
900 compression=RLECompression;
901 (void) ThrowMagickException(&image->exception,GetMagickModule(),
902 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZLIB)",
910 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
916 Scale relative to dots-per-inch.
918 delta.x=DefaultResolution;
919 delta.y=DefaultResolution;
920 resolution.x=image->x_resolution;
921 resolution.y=image->y_resolution;
922 if ((resolution.x == 0.0) || (resolution.y == 0.0))
924 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
925 resolution.x=geometry_info.rho;
926 resolution.y=geometry_info.sigma;
927 if ((flags & SigmaValue) == 0)
928 resolution.y=resolution.x;
930 if (image_info->density != (char *) NULL)
932 flags=ParseGeometry(image_info->density,&geometry_info);
933 resolution.x=geometry_info.rho;
934 resolution.y=geometry_info.sigma;
935 if ((flags & SigmaValue) == 0)
936 resolution.y=resolution.x;
938 if (image->units == PixelsPerCentimeterResolution)
940 resolution.x=(size_t) (100.0*2.54*resolution.x+0.5)/100.0;
941 resolution.y=(size_t) (100.0*2.54*resolution.y+0.5)/100.0;
943 SetGeometry(image,&geometry);
944 (void) FormatMagickString(page_geometry,MaxTextExtent,"%.20gx%.20g",
945 (double) image->columns,(double) image->rows);
946 if (image_info->page != (char *) NULL)
947 (void) CopyMagickString(page_geometry,image_info->page,MaxTextExtent);
949 if ((image->page.width != 0) && (image->page.height != 0))
950 (void) FormatMagickString(page_geometry,MaxTextExtent,
951 "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,(double)
952 image->page.height,(double) image->page.x,(double) image->page.y);
954 if ((image->gravity != UndefinedGravity) &&
955 (LocaleCompare(image_info->magick,"PS") == 0))
956 (void) CopyMagickString(page_geometry,PSPageGeometry,MaxTextExtent);
957 (void) ConcatenateMagickString(page_geometry,">",MaxTextExtent);
958 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
959 &geometry.width,&geometry.height);
960 scale.x=(double) (geometry.width*delta.x)/resolution.x;
961 geometry.width=(size_t) floor(scale.x+0.5);
962 scale.y=(double) (geometry.height*delta.y)/resolution.y;
963 geometry.height=(size_t) floor(scale.y+0.5);
964 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
965 (void) ParseGravityGeometry(image,page_geometry,&page_info,
967 if (image->gravity != UndefinedGravity)
969 geometry.x=(-page_info.x);
970 geometry.y=(ssize_t) (media_info.height+page_info.y-image->rows);
973 if (image_info->pointsize != 0.0)
974 pointsize=image_info->pointsize;
976 value=GetImageProperty(image,"label");
977 if (value != (const char *) NULL)
978 text_size=(size_t) (MultilineCensus(value)*pointsize+12);
983 Postscript header on the first page.
985 if (LocaleCompare(image_info->magick,"PS3") == 0)
986 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0\n",MaxTextExtent);
988 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0 EPSF-3.0\n",
990 (void) WriteBlobString(image,buffer);
991 (void) FormatMagickString(buffer,MaxTextExtent,
992 "%%%%Creator: ImageMagick %s\n",MagickLibVersionText);
993 (void) WriteBlobString(image,buffer);
994 (void) FormatMagickString(buffer,MaxTextExtent,"%%%%Title: %s\n",
996 (void) WriteBlobString(image,buffer);
997 timer=time((time_t *) NULL);
998 (void) FormatMagickTime(timer,MaxTextExtent,date);
999 (void) FormatMagickString(buffer,MaxTextExtent,
1000 "%%%%CreationDate: %s\n",date);
1001 (void) WriteBlobString(image,buffer);
1002 bounds.x1=(double) geometry.x;
1003 bounds.y1=(double) geometry.y;
1004 bounds.x2=(double) geometry.x+scale.x;
1005 bounds.y2=(double) geometry.y+scale.y+text_size;
1006 if ((image_info->adjoin != MagickFalse) &&
1007 (GetNextImageInList(image) != (Image *) NULL))
1009 (void) WriteBlobString(image,"%%BoundingBox: (atend)\n");
1010 (void) WriteBlobString(image,"%%HiResBoundingBox: (atend)\n");
1014 (void) FormatMagickString(buffer,MaxTextExtent,
1015 "%%%%BoundingBox: %g %g %g %g\n",floor(bounds.x1+0.5),
1016 floor(bounds.y1+0.5),ceil(bounds.x2-0.5),ceil(bounds.y2-0.5));
1017 (void) WriteBlobString(image,buffer);
1018 (void) FormatMagickString(buffer,MaxTextExtent,
1019 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,
1020 bounds.y1,bounds.x2,bounds.y2);
1021 (void) WriteBlobString(image,buffer);
1022 if (image->colorspace == CMYKColorspace)
1023 (void) WriteBlobString(image,
1024 "%%DocumentProcessColors: Cyan Magenta Yellow Black\n");
1026 if (IsGrayImage(image,&image->exception) != MagickFalse)
1027 (void) WriteBlobString(image,
1028 "%%DocumentProcessColors: Black\n");
1033 value=GetImageProperty(image,"label");
1034 if (value != (const char *) NULL)
1035 (void) WriteBlobString(image,
1036 "%%DocumentNeededResources: font Helvetica\n");
1037 (void) WriteBlobString(image,"%%LanguageLevel: 3\n");
1039 Pages, orientation and order.
1041 if (LocaleCompare(image_info->magick,"PS3") != 0)
1042 (void) WriteBlobString(image,"%%Pages: 1\n");
1045 (void) WriteBlobString(image,"%%Orientation: Portrait\n");
1046 (void) WriteBlobString(image,"%%PageOrder: Ascend\n");
1047 if (image_info->adjoin == MagickFalse)
1048 (void) CopyMagickString(buffer,"%%Pages: 1\n",MaxTextExtent);
1050 (void) FormatMagickString(buffer,MaxTextExtent,
1051 "%%%%Pages: %.20g\n",(double) GetImageListLength(image));
1052 (void) WriteBlobString(image,buffer);
1054 (void) WriteBlobString(image,"%%EndComments\n");
1056 The static postscript procedures prolog.
1058 (void)WriteBlobString(image,"%%BeginProlog\n");
1059 for (q=PostscriptProlog; *q; q++)
1061 (void) WriteBlobString(image,*q);
1062 (void) WriteBlobByte(image,'\n');
1065 One label line for each line in label string.
1067 value=GetImageProperty(image,"label");
1068 if (value != (const char *) NULL)
1070 (void) WriteBlobString(image,"\n %% Labels.\n /Helvetica "
1071 " findfont pointsize scalefont setfont\n");
1072 for (i=(ssize_t) MultilineCensus(value)-1; i >= 0; i--)
1074 (void) WriteBlobString(image,
1075 " currentfile buffer readline pop token pop\n");
1076 (void) FormatMagickString(buffer,MaxTextExtent,
1077 " 0 y %g add moveto show pop\n",i*pointsize+12);
1078 (void) WriteBlobString(image,buffer);
1082 The static postscript procedures epilog.
1084 for (q=PostscriptEpilog; *q; q++)
1086 (void) WriteBlobString(image,*q);
1087 (void) WriteBlobByte(image,'\n');
1089 (void)WriteBlobString(image,"%%EndProlog\n");
1091 (void) FormatMagickString(buffer,MaxTextExtent,"%%%%Page: 1 %.20g\n",
1093 (void) WriteBlobString(image,buffer);
1097 (void) FormatMagickString(buffer,MaxTextExtent,
1098 "%%%%PageBoundingBox: %.20g %.20g %.20g %.20g\n",(double) geometry.x,
1099 (double) geometry.y,geometry.x+(double) geometry.width,geometry.y+
1100 (double) (geometry.height+text_size));
1101 (void) WriteBlobString(image,buffer);
1103 Page process colors if not RGB.
1105 if (image->colorspace == CMYKColorspace)
1106 (void) WriteBlobString(image,
1107 "%%PageProcessColors: Cyan Magenta Yellow Black\n");
1109 if (IsGrayImage(image,&image->exception) != MagickFalse)
1110 (void) WriteBlobString(image,"%%PageProcessColors: Black\n");
1112 Adjust document bounding box to bound page bounding box.
1114 if ((double) geometry.x < bounds.x1)
1115 bounds.x1=(double) geometry.x;
1116 if ((double) geometry.y < bounds.y1)
1117 bounds.y1=(double) geometry.y;
1118 if ((double) (geometry.x+scale.x) > bounds.x2)
1119 bounds.x2=(double) geometry.x+scale.x;
1120 if ((double) (geometry.y+scale.y+text_size) > bounds.y2)
1121 bounds.y2=(double) geometry.y+scale.y+text_size;
1123 Page font resource if there's a label.
1125 value=GetImageProperty(image,"label");
1126 if (value != (const char *) NULL)
1127 (void) WriteBlobString(image,"%%PageResources: font Helvetica\n");
1129 PS clipping path from Photoshop clipping path.
1131 if ((image->clip_mask == (Image *) NULL) ||
1132 (LocaleNCompare("8BIM:",image->clip_mask->magick_filename,5) != 0))
1133 (void) WriteBlobString(image,"/ClipImage {} def\n");
1139 value=GetImageProperty(image,image->clip_mask->magick_filename);
1140 if (value == (const char *) NULL)
1141 return(MagickFalse);
1142 (void) WriteBlobString(image,value);
1143 (void) WriteBlobByte(image,'\n');
1146 Push a dictionary for our own def's if this an EPS.
1148 if (LocaleCompare(image_info->magick,"PS3") != 0)
1149 (void) WriteBlobString(image,"userdict begin\n");
1153 if ((image->matte != MagickFalse) &&
1154 (WritePS3MaskImage(image_info,image,compression) == MagickFalse))
1156 (void) CloseBlob(image);
1157 return(MagickFalse);
1160 Remember position of BeginData comment so we can update it.
1162 start=TellBlob(image);
1163 (void) FormatMagickString(buffer,MaxTextExtent,
1164 "%%%%BeginData:%13ld %s Bytes\n",0L,
1165 compression == NoCompression ? "ASCII" : "BINARY");
1166 (void) WriteBlobString(image,buffer);
1167 stop=TellBlob(image);
1168 (void) WriteBlobString(image,"DisplayImage\n");
1170 Translate, scale, and font point size.
1172 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g %.20g\n%g %g\n%g\n",
1173 (double) geometry.x,(double) geometry.y,scale.x,scale.y,pointsize);
1174 (void) WriteBlobString(image,buffer);
1178 labels=(char **) NULL;
1179 value=GetImageProperty(image,"label");
1180 if (value != (const char *) NULL)
1181 labels=StringToList(value);
1182 if (labels != (char **) NULL)
1184 for (i=0; labels[i] != (char *) NULL; i++)
1186 if (compression != NoCompression)
1188 for (j=0; labels[i][j] != '\0'; j++)
1189 (void) WriteBlobByte(image,(unsigned char) labels[i][j]);
1190 (void) WriteBlobByte(image,'\n');
1194 (void) WriteBlobString(image,"<~");
1195 Ascii85Initialize(image);
1196 for (j=0; labels[i][j] != '\0'; j++)
1197 Ascii85Encode(image,(unsigned char) labels[i][j]);
1198 Ascii85Flush(image);
1200 labels[i]=DestroyString(labels[i]);
1202 labels=(char **) RelinquishMagickMemory(labels);
1205 Photoshop clipping path active?
1207 if ((image->clip_mask != (Image *) NULL) &&
1208 (LocaleNCompare("8BIM:",image->clip_mask->magick_filename,5) == 0))
1209 (void) WriteBlobString(image,"true\n");
1211 (void) WriteBlobString(image,"false\n");
1213 Showpage for non-EPS.
1215 (void) WriteBlobString(image, LocaleCompare(image_info->magick,"PS3") == 0 ?
1216 "true\n" : "false\n");
1218 Image columns, rows, and color space.
1220 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g %.20g\n%s\n",
1221 (double) image->columns,(double) image->rows,
1222 image->colorspace == CMYKColorspace ? PS3_CMYKColorspace :
1224 (void) WriteBlobString(image,buffer);
1228 (void) WriteBlobString(image,image->matte != MagickFalse ?
1229 "true\n" : "false\n");
1231 Render with imagemask operator?
1233 option=GetImageOption(image_info,"ps3:imagemask");
1234 (void) WriteBlobString(image,((option != (const char *) NULL) &&
1235 (IsMonochromeImage(image,&image->exception) != MagickFalse)) ?
1236 "true\n" : "false\n");
1240 pixels=(unsigned char *) NULL;
1242 if ((image_info->type != TrueColorType) &&
1243 (image_info->type != TrueColorMatteType) &&
1244 (image_info->type != ColorSeparationType) &&
1245 (image_info->type != ColorSeparationMatteType) &&
1246 (image->colorspace != CMYKColorspace) &&
1247 ((IsGrayImage(image,&image->exception) != MagickFalse) ||
1248 (IsMonochromeImage(image,&image->exception) != MagickFalse)))
1253 (void) WriteBlobString(image,PS3_PseudoClass"\n");
1254 switch (compression)
1259 (void) WriteBlobString(image,PS3_NoCompression"\n");
1262 case FaxCompression:
1263 case Group4Compression:
1265 (void) WriteBlobString(image,PS3_FaxCompression"\n");
1268 case JPEGCompression:
1270 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1273 case LZWCompression:
1275 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1278 case RLECompression:
1280 (void) WriteBlobString(image,PS3_RLECompression"\n");
1283 case ZipCompression:
1285 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1290 Number of colors -- 0 for single component non-color mapped data.
1292 (void) WriteBlobString(image,"0\n");
1294 1 bit or 8 bit components?
1296 (void) FormatMagickString(buffer,MaxTextExtent,"%d\n",
1297 IsMonochromeImage(image,&image->exception) != MagickFalse ? 1 : 8);
1298 (void) WriteBlobString(image,buffer);
1302 if (compression == JPEGCompression)
1303 status=InjectImageBlob(image_info,image,image,"jpeg",
1306 if ((compression == FaxCompression) ||
1307 (compression == Group4Compression))
1309 if (LocaleCompare(CCITTParam,"0") == 0)
1310 status=HuffmanEncodeImage(image_info,image,image);
1312 status=Huffman2DEncodeImage(image_info,image,image);
1316 status=SerializeImageChannel(image_info,image,&pixels,&length);
1317 if (status == MagickFalse)
1319 (void) CloseBlob(image);
1320 return(MagickFalse);
1322 switch (compression)
1327 Ascii85Initialize(image);
1328 for (i=0; i < (ssize_t) length; i++)
1329 Ascii85Encode(image,pixels[i]);
1330 Ascii85Flush(image);
1334 case LZWCompression:
1336 status=LZWEncodeImage(image,length,pixels);
1339 case RLECompression:
1341 status=PackbitsEncodeImage(image,length,pixels);
1344 case ZipCompression:
1346 status=ZLIBEncodeImage(image,length,pixels);
1350 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1354 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1355 (compression == JPEGCompression))
1360 (void) WriteBlobString(image,PS3_DirectClass"\n");
1361 switch (compression)
1366 (void) WriteBlobString(image,PS3_NoCompression"\n");
1369 case RLECompression:
1371 (void) WriteBlobString(image,PS3_RLECompression"\n");
1374 case JPEGCompression:
1376 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1379 case LZWCompression:
1381 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1384 case ZipCompression:
1386 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1393 if (compression == JPEGCompression)
1394 status=InjectImageBlob(image_info,image,image,"jpeg",
1399 Stream based compressions.
1401 status=SerializeImage(image_info,image,&pixels,&length);
1402 if (status == MagickFalse)
1404 (void) CloseBlob(image);
1405 return(MagickFalse);
1407 switch (compression)
1412 Ascii85Initialize(image);
1413 for (i=0; i < (ssize_t) length; i++)
1414 Ascii85Encode(image,pixels[i]);
1415 Ascii85Flush(image);
1419 case RLECompression:
1421 status=PackbitsEncodeImage(image,length,pixels);
1424 case LZWCompression:
1426 status=LZWEncodeImage(image,length,pixels);
1429 case ZipCompression:
1431 status=ZLIBEncodeImage(image,length,pixels);
1435 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1443 (void) WriteBlobString(image,PS3_PseudoClass"\n");
1444 switch (compression)
1449 (void) WriteBlobString(image,PS3_NoCompression"\n");
1452 case JPEGCompression:
1454 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1457 case RLECompression:
1459 (void) WriteBlobString(image,PS3_RLECompression"\n");
1462 case LZWCompression:
1464 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1467 case ZipCompression:
1469 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1474 Number of colors in color map.
1476 (void) FormatMagickString(buffer,MaxTextExtent,"%.20g\n",
1477 (double) image->colors);
1478 (void) WriteBlobString(image,buffer);
1480 Color map - uncompressed.
1482 if ((compression != NoCompression) &&
1483 (compression != UndefinedCompression))
1485 for (i=0; i < (ssize_t) image->colors; i++)
1487 pixel=ScaleQuantumToChar(image->colormap[i].red);
1488 (void) WriteBlobByte(image,(unsigned char) pixel);
1489 pixel=ScaleQuantumToChar(image->colormap[i].green);
1490 (void) WriteBlobByte(image,(unsigned char) pixel);
1491 pixel=ScaleQuantumToChar(image->colormap[i].blue);
1492 (void) WriteBlobByte(image,(unsigned char) pixel);
1497 Ascii85Initialize(image);
1498 for (i=0; i < (ssize_t) image->colors; i++)
1500 pixel=ScaleQuantumToChar(image->colormap[i].red);
1501 Ascii85Encode(image,(unsigned char) pixel);
1502 pixel=ScaleQuantumToChar(image->colormap[i].green);
1503 Ascii85Encode(image,(unsigned char) pixel);
1504 pixel=ScaleQuantumToChar(image->colormap[i].blue);
1505 Ascii85Encode(image,(unsigned char) pixel);
1507 Ascii85Flush(image);
1509 status=SerializeImageIndexes(image_info,image,&pixels,&length);
1510 if (status == MagickFalse)
1512 (void) CloseBlob(image);
1513 return(MagickFalse);
1515 switch (compression)
1520 Ascii85Initialize(image);
1521 for (i=0; i < (ssize_t) length; i++)
1522 Ascii85Encode(image,pixels[i]);
1523 Ascii85Flush(image);
1527 case JPEGCompression:
1529 status=InjectImageBlob(image_info,image,image,"jpeg",
1533 case RLECompression:
1535 status=PackbitsEncodeImage(image,length,pixels);
1538 case LZWCompression:
1540 status=LZWEncodeImage(image,length,pixels);
1543 case ZipCompression:
1545 status=ZLIBEncodeImage(image,length,pixels);
1549 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1551 (void) WriteBlobByte(image,'\n');
1552 if (status == MagickFalse)
1554 (void) CloseBlob(image);
1555 return(MagickFalse);
1558 Update BeginData now that we know the data size.
1560 length=(size_t) (TellBlob(image)-stop);
1561 stop=TellBlob(image);
1562 offset=SeekBlob(image,start,SEEK_SET);
1564 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
1565 (void) FormatMagickString(buffer,MaxTextExtent,
1566 "%%%%BeginData:%13ld %s Bytes\n",(long) length,
1567 compression == NoCompression ? "ASCII" : "BINARY");
1568 (void) WriteBlobString(image,buffer);
1569 offset=SeekBlob(image,stop,SEEK_SET);
1570 (void) WriteBlobString(image,"%%EndData\n");
1572 End private dictionary if this an EPS.
1574 if (LocaleCompare(image_info->magick,"PS3") != 0)
1575 (void) WriteBlobString(image,"end\n");
1576 (void) WriteBlobString(image,"%%PageTrailer\n");
1577 if (GetNextImageInList(image) == (Image *) NULL)
1579 image=SyncNextImageInList(image);
1580 status=SetImageProgress(image,SaveImagesTag,scene++,
1581 GetImageListLength(image));
1582 if (status == MagickFalse)
1584 } while (image_info->adjoin != MagickFalse);
1585 (void) WriteBlobString(image,"%%Trailer\n");
1588 (void) FormatMagickString(buffer,MaxTextExtent,
1589 "%%%%BoundingBox: %g %g %g %g\n",floor(bounds.x1+0.5),
1590 floor(bounds.y1+0.5),ceil(bounds.x2-0.5),ceil(bounds.y2-0.5));
1591 (void) WriteBlobString(image,buffer);
1592 (void) FormatMagickString(buffer,MaxTextExtent,
1593 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,bounds.y1,
1594 bounds.x2,bounds.y2);
1595 (void) WriteBlobString(image,buffer);
1597 (void) WriteBlobString(image,"%%EOF\n");
1598 (void) CloseBlob(image);