2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Write Postscript Level II Format %
20 % Copyright 1999-2009 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "magick/studio.h"
43 #include "magick/blob.h"
44 #include "magick/blob-private.h"
45 #include "magick/cache.h"
46 #include "magick/color.h"
47 #include "magick/color-private.h"
48 #include "magick/compress.h"
49 #include "magick/constitute.h"
50 #include "magick/draw.h"
51 #include "magick/exception.h"
52 #include "magick/exception-private.h"
53 #include "magick/geometry.h"
54 #include "magick/image.h"
55 #include "magick/image-private.h"
56 #include "magick/list.h"
57 #include "magick/magick.h"
58 #include "magick/memory_.h"
59 #include "magick/monitor.h"
60 #include "magick/monitor-private.h"
61 #include "magick/monitor-private.h"
62 #include "magick/option.h"
63 #include "magick/resource_.h"
64 #include "magick/property.h"
65 #include "magick/quantum-private.h"
66 #include "magick/static.h"
67 #include "magick/string_.h"
68 #include "magick/module.h"
69 #include "magick/utility.h"
70 #if defined(MAGICKCORE_TIFF_DELEGATE)
71 #if defined(MAGICKCORE_HAVE_TIFFCONF_H)
75 #define CCITTParam "-1"
77 #define CCITTParam "0"
83 static MagickBooleanType
84 WritePS2Image(const ImageInfo *,Image *);
87 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91 % R e g i s t e r P S 2 I m a g e %
95 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
97 % RegisterPS2Image() adds properties for the PS2 image format to
98 % the list of supported formats. The properties include the image format
99 % tag, a method to read and/or write the format, whether the format
100 % supports the saving of more than one frame to the same file or blob,
101 % whether the format supports native in-memory I/O, and a brief
102 % description of the format.
104 % The format of the RegisterPS2Image method is:
106 % unsigned long RegisterPS2Image(void)
109 ModuleExport unsigned long RegisterPS2Image(void)
114 entry=SetMagickInfo("EPS2");
115 entry->encoder=(EncodeImageHandler *) WritePS2Image;
116 entry->adjoin=MagickFalse;
117 entry->seekable_stream=MagickTrue;
118 entry->description=ConstantString("Level II Encapsulated PostScript");
119 entry->module=ConstantString("PS2");
120 (void) RegisterMagickInfo(entry);
121 entry=SetMagickInfo("PS2");
122 entry->encoder=(EncodeImageHandler *) WritePS2Image;
123 entry->seekable_stream=MagickTrue;
124 entry->description=ConstantString("Level II PostScript");
125 entry->module=ConstantString("PS2");
126 (void) RegisterMagickInfo(entry);
127 return(MagickImageCoderSignature);
131 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
135 % U n r e g i s t e r P S 2 I m a g e %
139 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141 % UnregisterPS2Image() removes format registrations made by the
142 % PS2 module from the list of supported formats.
144 % The format of the UnregisterPS2Image method is:
146 % UnregisterPS2Image(void)
149 ModuleExport void UnregisterPS2Image(void)
151 (void) UnregisterMagickInfo("EPS2");
152 (void) UnregisterMagickInfo("PS2");
156 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
160 % W r i t e P S 2 I m a g e %
164 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
166 % WritePS2Image translates an image to encapsulated Postscript
167 % Level II for printing. If the supplied geometry is null, the image is
168 % centered on the Postscript page. Otherwise, the image is positioned as
169 % specified by the geometry.
171 % The format of the WritePS2Image method is:
173 % MagickBooleanType WritePS2Image(const ImageInfo *image_info,Image *image)
175 % A description of each parameter follows:
177 % o image_info: the image info.
179 % o image: the image.
183 #if defined(MAGICKCORE_TIFF_DELEGATE)
184 static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
185 Image *image,Image *inject_image)
188 filename[MaxTextExtent];
225 Write image as CCITTFax4 TIFF image to a temporary file.
227 assert(image_info != (const ImageInfo *) NULL);
228 assert(image_info->signature == MagickSignature);
229 assert(image != (Image *) NULL);
230 assert(image->signature == MagickSignature);
231 if (image->debug != MagickFalse)
232 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
233 assert(inject_image != (Image *) NULL);
234 assert(inject_image->signature == MagickSignature);
235 huffman_image=CloneImage(inject_image,0,0,MagickTrue,&image->exception);
236 if (huffman_image == (Image *) NULL)
239 unique_file=AcquireUniqueFileResource(filename);
240 if (unique_file != -1)
241 file=fdopen(unique_file,"wb");
242 if ((unique_file == -1) || (file == (FILE *) NULL))
244 ThrowFileException(&image->exception,FileOpenError,
245 "UnableToCreateTemporaryFile",filename);
248 (void) FormatMagickString(huffman_image->filename,MaxTextExtent,"tiff:%s",
250 write_info=CloneImageInfo(image_info);
251 SetImageInfoFile(write_info,file);
252 write_info->compression=Group4Compression;
253 write_info->type=BilevelType;
254 (void) SetImageOption(write_info,"quantum:polarity","min-is-white");
255 status=WriteImage(write_info,huffman_image);
257 write_info=DestroyImageInfo(write_info);
258 if (status == MagickFalse)
260 tiff=TIFFOpen(filename,"rb");
261 if (tiff == (TIFF *) NULL)
263 huffman_image=DestroyImage(huffman_image);
265 (void) RelinquishUniqueFileResource(filename);
266 ThrowFileException(&image->exception,FileOpenError,"UnableToOpenFile",
267 image_info->filename);
271 Allocate raw strip buffer.
274 (void) TIFFGetField(tiff,TIFFTAG_STRIPBYTECOUNTS,&byte_count);
275 strip_size=byte_count[0];
276 for (i=1; i < (long) TIFFNumberOfStrips(tiff); i++)
277 if (byte_count[i] > strip_size)
278 strip_size=byte_count[i];
279 buffer=(unsigned char *) AcquireQuantumMemory((size_t) strip_size,
281 if (buffer == (unsigned char *) NULL)
284 huffman_image=DestroyImage(huffman_image);
286 (void) RelinquishUniqueFileResource(filename);
287 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
288 image_info->filename);
291 Compress runlength encoded to 2D Huffman pixels.
293 fillorder=FILLORDER_LSB2MSB;
294 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&fillorder);
295 for (i=0; i < (long) TIFFNumberOfStrips(tiff); i++)
297 count=(ssize_t) TIFFReadRawStrip(tiff,(uint32) i,buffer,(long)
299 if (fillorder == FILLORDER_LSB2MSB)
300 TIFFReverseBits(buffer,(unsigned long) count);
301 (void) WriteBlob(image,(size_t) count,buffer);
303 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
305 huffman_image=DestroyImage(huffman_image);
307 (void) RelinquishUniqueFileResource(filename);
311 static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
312 Image *image,Image *inject_image)
314 assert(image_info != (const ImageInfo *) NULL);
315 assert(image_info->signature == MagickSignature);
316 assert(image != (Image *) NULL);
317 assert(image->signature == MagickSignature);
318 if (image->debug != MagickFalse)
319 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
320 assert(inject_image != (Image *) NULL);
321 assert(inject_image->signature == MagickSignature);
322 (void) ThrowMagickException(&image->exception,GetMagickModule(),
323 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (TIFF)",
329 static MagickBooleanType WritePS2Image(const ImageInfo *image_info,Image *image)
336 "%% Display a color image. The image is displayed in color on",
337 "%% Postscript viewers or printers that support color, otherwise",
338 "%% it is displayed as grayscale.",
343 " %% Display a DirectClass image.",
347 " /DeviceRGB setcolorspace",
352 " /BitsPerComponent 8",
353 " /Decode [0 1 0 1 0 1]",
354 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
356 " { /DataSource pixel_stream /%s filter }",
357 " { /DataSource pixel_stream /%s filter } ifelse",
361 " /DeviceCMYK setcolorspace",
366 " /BitsPerComponent 8",
367 " /Decode [1 0 1 0 1 0 1 0]",
368 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
370 " { /DataSource pixel_stream /%s filter }",
371 " { /DataSource pixel_stream /%s filter } ifelse",
379 " %% Display a PseudoClass image.",
382 " %% colors: number of colors in the colormap.",
384 " currentfile buffer readline pop",
385 " token pop /colors exch def pop",
389 " %% Image is grayscale.",
391 " currentfile buffer readline pop",
392 " token pop /bits exch def pop",
393 " /DeviceGray setcolorspace",
398 " /BitsPerComponent bits",
400 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
402 " { /DataSource pixel_stream /%s filter }",
404 " /DataSource pixel_stream /%s filter",
409 " >> /CCITTFaxDecode filter",
416 " %% colormap: red, green, blue color packets.",
418 " /colormap colors 3 mul string def",
419 " currentfile colormap readhexstring pop pop",
420 " currentfile buffer readline pop",
421 " [ /Indexed /DeviceRGB colors 1 sub colormap ] setcolorspace",
426 " /BitsPerComponent 8",
428 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
430 " { /DataSource pixel_stream /%s filter }",
431 " { /DataSource pixel_stream /%s filter } ifelse",
439 " %% Display a DirectClass or PseudoClass image.",
442 " %% x & y translation.",
444 " %% label pointsize.",
446 " %% image columns & rows.",
447 " %% class: 0-DirectClass or 1-PseudoClass.",
448 " %% colorspace: 0-RGB or 1-CMYK.",
449 " %% compression: 0-RLECompression or 1-NoCompression.",
450 " %% hex color packets.",
453 " /buffer 512 string def",
454 " /pixel_stream currentfile def",
456 " currentfile buffer readline pop",
457 " token pop /x exch def",
458 " token pop /y exch def pop",
460 " currentfile buffer readline pop",
461 " token pop /x exch def",
462 " token pop /y exch def pop",
463 " currentfile buffer readline pop",
464 " token pop /pointsize exch def pop",
465 " /Helvetica findfont pointsize scalefont setfont",
471 " currentfile buffer readline pop",
472 " token pop /columns exch def",
473 " token pop /rows exch def pop",
474 " currentfile buffer readline pop",
475 " token pop /class exch def pop",
476 " currentfile buffer readline pop",
477 " token pop /colorspace exch def pop",
478 " currentfile buffer readline pop",
479 " token pop /compression exch def pop",
480 " class 0 gt { PseudoClassImage } { DirectClassImage } ifelse",
485 buffer[MaxTextExtent],
487 page_geometry[MaxTextExtent],
535 register const IndexPacket
538 register const PixelPacket
564 Open output image file.
566 assert(image_info != (const ImageInfo *) NULL);
567 assert(image_info->signature == MagickSignature);
568 assert(image != (Image *) NULL);
569 assert(image->signature == MagickSignature);
570 if (image->debug != MagickFalse)
571 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
572 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
573 if (status == MagickFalse)
575 compression=image->compression;
576 if (image_info->compression != UndefinedCompression)
577 compression=image_info->compression;
580 #if !defined(MAGICKCORE_JPEG_DELEGATE)
581 case JPEGCompression:
583 compression=RLECompression;
584 (void) ThrowMagickException(&image->exception,GetMagickModule(),
585 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
593 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
599 Scale relative to dots-per-inch.
601 delta.x=DefaultResolution;
602 delta.y=DefaultResolution;
603 resolution.x=image->x_resolution;
604 resolution.y=image->y_resolution;
605 if ((resolution.x == 0.0) || (resolution.y == 0.0))
607 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
608 resolution.x=geometry_info.rho;
609 resolution.y=geometry_info.sigma;
610 if ((flags & SigmaValue) == 0)
611 resolution.y=resolution.x;
613 if (image_info->density != (char *) NULL)
615 flags=ParseGeometry(image_info->density,&geometry_info);
616 resolution.x=geometry_info.rho;
617 resolution.y=geometry_info.sigma;
618 if ((flags & SigmaValue) == 0)
619 resolution.y=resolution.x;
621 if (image->units == PixelsPerCentimeterResolution)
626 SetGeometry(image,&geometry);
627 (void) FormatMagickString(page_geometry,MaxTextExtent,"%lux%lu",
628 image->columns,image->rows);
629 if (image_info->page != (char *) NULL)
630 (void) CopyMagickString(page_geometry,image_info->page,MaxTextExtent);
632 if ((image->page.width != 0) && (image->page.height != 0))
633 (void) FormatMagickString(page_geometry,MaxTextExtent,"%lux%lu%+ld%+ld",
634 image->page.width,image->page.height,image->page.x,image->page.y);
636 if ((image->gravity != UndefinedGravity) &&
637 (LocaleCompare(image_info->magick,"PS") == 0))
638 (void) CopyMagickString(page_geometry,PSPageGeometry,MaxTextExtent);
639 (void) ConcatenateMagickString(page_geometry,">",MaxTextExtent);
640 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
641 &geometry.width,&geometry.height);
642 scale.x=(double) (geometry.width*delta.x)/resolution.x;
643 geometry.width=(unsigned long) (scale.x+0.5);
644 scale.y=(double) (geometry.height*delta.y)/resolution.y;
645 geometry.height=(unsigned long) (scale.y+0.5);
646 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
647 (void) ParseGravityGeometry(image,page_geometry,&page_info,
649 if (image->gravity != UndefinedGravity)
651 geometry.x=(-page_info.x);
652 geometry.y=(long) (media_info.height+page_info.y-image->rows);
655 if (image_info->pointsize != 0.0)
656 pointsize=image_info->pointsize;
658 value=GetImageProperty(image,"label");
659 if (value != (const char *) NULL)
660 text_size=(unsigned long) (MultilineCensus(value)*pointsize+12);
664 Output Postscript header.
666 if (LocaleCompare(image_info->magick,"PS2") == 0)
667 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0\n",MaxTextExtent);
669 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0 EPSF-3.0\n",
671 (void) WriteBlobString(image,buffer);
672 (void) WriteBlobString(image,"%%Creator: (ImageMagick)\n");
673 (void) FormatMagickString(buffer,MaxTextExtent,"%%%%Title: (%s)\n",
675 (void) WriteBlobString(image,buffer);
676 timer=time((time_t *) NULL);
677 (void) FormatMagickTime(timer,MaxTextExtent,date);
678 (void) FormatMagickString(buffer,MaxTextExtent,
679 "%%%%CreationDate: (%s)\n",date);
680 (void) WriteBlobString(image,buffer);
681 bounds.x1=(double) geometry.x;
682 bounds.y1=(double) geometry.y;
683 bounds.x2=(double) geometry.x+geometry.width;
684 bounds.y2=(double) geometry.y+geometry.height+text_size;
685 if ((image_info->adjoin != MagickFalse) &&
686 (GetNextImageInList(image) != (Image *) NULL))
687 (void) CopyMagickString(buffer,"%%BoundingBox: (atend)\n",
691 (void) FormatMagickString(buffer,MaxTextExtent,
692 "%%%%BoundingBox: %ld %ld %ld %ld\n",(long) (bounds.x1+0.5),
693 (long) (bounds.y1+0.5),
694 (long) (bounds.x2+0.5),(long) (bounds.y2+0.5));
695 (void) WriteBlobString(image,buffer);
696 (void) FormatMagickString(buffer,MaxTextExtent,
697 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,bounds.y1,
698 bounds.x2,bounds.y2);
700 (void) WriteBlobString(image,buffer);
701 value=GetImageProperty(image,"label");
702 if (value != (const char *) NULL)
703 (void) WriteBlobString(image,
704 "%%DocumentNeededResources: font Helvetica\n");
705 (void) WriteBlobString(image,"%%LanguageLevel: 2\n");
706 if (LocaleCompare(image_info->magick,"PS2") != 0)
707 (void) WriteBlobString(image,"%%Pages: 1\n");
710 (void) WriteBlobString(image,"%%Orientation: Portrait\n");
711 (void) WriteBlobString(image,"%%PageOrder: Ascend\n");
712 if (image_info->adjoin == MagickFalse)
713 (void) CopyMagickString(buffer,"%%Pages: 1\n",MaxTextExtent);
715 (void) FormatMagickString(buffer,MaxTextExtent,"%%%%Pages: %lu\n",
716 (unsigned long) GetImageListLength(image));
717 (void) WriteBlobString(image,buffer);
719 (void) WriteBlobString(image,"%%EndComments\n");
720 (void) WriteBlobString(image,"\n%%BeginDefaults\n");
721 (void) WriteBlobString(image,"%%EndDefaults\n\n");
723 Output Postscript commands.
725 for (q=PostscriptProlog; *q; q++)
731 (void) FormatMagickString(buffer,MaxTextExtent,*q,
735 case JPEGCompression:
737 (void) FormatMagickString(buffer,MaxTextExtent,*q,"DCTDecode");
742 (void) FormatMagickString(buffer,MaxTextExtent,*q,"LZWDecode");
746 case Group4Compression:
748 (void) FormatMagickString(buffer,MaxTextExtent,*q,
754 (void) FormatMagickString(buffer,MaxTextExtent,*q,
759 (void) WriteBlobString(image,buffer);
760 (void) WriteBlobByte(image,'\n');
762 value=GetImageProperty(image,"label");
763 if (value != (const char *) NULL)
764 for (j=(long) MultilineCensus(value)-1; j >= 0; j--)
766 (void) WriteBlobString(image," /label 512 string def\n");
767 (void) WriteBlobString(image," currentfile label readline pop\n");
768 (void) FormatMagickString(buffer,MaxTextExtent,
769 " 0 y %g add moveto label show pop\n",j*pointsize+12);
770 (void) WriteBlobString(image,buffer);
772 for (q=PostscriptEpilog; *q; q++)
774 (void) FormatMagickString(buffer,MaxTextExtent,"%s\n",*q);
775 (void) WriteBlobString(image,buffer);
777 if (LocaleCompare(image_info->magick,"PS2") == 0)
778 (void) WriteBlobString(image," showpage\n");
779 (void) WriteBlobString(image,"} bind def\n");
780 (void) WriteBlobString(image,"%%EndProlog\n");
782 (void) FormatMagickString(buffer,MaxTextExtent,"%%%%Page: 1 %lu\n",page++);
783 (void) WriteBlobString(image,buffer);
784 (void) FormatMagickString(buffer,MaxTextExtent,
785 "%%%%PageBoundingBox: %ld %ld %ld %ld\n",geometry.x,geometry.y,
786 geometry.x+(long) geometry.width,geometry.y+(long)
787 (geometry.height+text_size));
788 (void) WriteBlobString(image,buffer);
789 if ((double) geometry.x < bounds.x1)
790 bounds.x1=(double) geometry.x;
791 if ((double) geometry.y < bounds.y1)
792 bounds.y1=(double) geometry.y;
793 if ((double) (geometry.x+geometry.width-1) > bounds.x2)
794 bounds.x2=(double) geometry.x+geometry.width-1;
795 if ((double) (geometry.y+(geometry.height+text_size)-1) > bounds.y2)
796 bounds.y2=(double) geometry.y+(geometry.height+text_size)-1;
797 value=GetImageProperty(image,"label");
798 if (value != (const char *) NULL)
799 (void) WriteBlobString(image,"%%PageResources: font Times-Roman\n");
800 if (LocaleCompare(image_info->magick,"PS2") != 0)
801 (void) WriteBlobString(image,"userdict begin\n");
802 start=TellBlob(image);
803 (void) FormatMagickString(buffer,MaxTextExtent,
804 "%%%%BeginData:%13ld %s Bytes\n",0L,
805 compression == NoCompression ? "ASCII" : "Binary");
806 (void) WriteBlobString(image,buffer);
807 stop=TellBlob(image);
808 (void) WriteBlobString(image,"DisplayImage\n");
812 (void) FormatMagickString(buffer,MaxTextExtent,"%ld %ld\n%g %g\n%f\n",
813 geometry.x,geometry.y,scale.x,scale.y,pointsize);
814 (void) WriteBlobString(image,buffer);
815 labels=(char **) NULL;
816 value=GetImageProperty(image,"label");
817 if (value != (const char *) NULL)
818 labels=StringToList(value);
819 if (labels != (char **) NULL)
821 for (i=0; labels[i] != (char *) NULL; i++)
823 (void) FormatMagickString(buffer,MaxTextExtent,"%s \n",
825 (void) WriteBlobString(image,buffer);
826 labels[i]=DestroyString(labels[i]);
828 labels=(char **) RelinquishMagickMemory(labels);
830 number_pixels=(MagickSizeType) image->columns*image->rows;
831 if (number_pixels != (MagickSizeType) ((size_t) number_pixels))
832 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
833 if ((compression == FaxCompression) || (compression == Group4Compression) ||
834 ((image_info->type != TrueColorType) &&
835 (IsGrayImage(image,&image->exception) != MagickFalse)))
837 (void) FormatMagickString(buffer,MaxTextExtent,"%lu %lu\n1\n%d\n",
838 image->columns,image->rows,(int)
839 (image->colorspace == CMYKColorspace));
840 (void) WriteBlobString(image,buffer);
841 (void) FormatMagickString(buffer,MaxTextExtent,"%d\n",
842 (int) ((compression != FaxCompression) &&
843 (compression != Group4Compression)));
844 (void) WriteBlobString(image,buffer);
845 (void) WriteBlobString(image,"0\n");
846 (void) FormatMagickString(buffer,MaxTextExtent,"%d\n",
847 (compression == FaxCompression) ||
848 (compression == Group4Compression) ? 1 : 8);
849 (void) WriteBlobString(image,buffer);
853 case Group4Compression:
855 if (LocaleCompare(CCITTParam,"0") == 0)
857 (void) HuffmanEncodeImage(image_info,image,image);
860 (void) Huffman2DEncodeImage(image_info,image,image);
863 case JPEGCompression:
865 status=InjectImageBlob(image_info,image,image,"jpeg",
867 if (status == MagickFalse)
868 ThrowWriterException(CoderError,image->exception.reason);
874 register unsigned char
878 Allocate pixel array.
880 length=(size_t) number_pixels;
881 pixels=(unsigned char *) AcquireQuantumMemory(length,
883 if (pixels == (unsigned char *) NULL)
884 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
886 Dump Runlength encoded pixels.
889 for (y=0; y < (long) image->rows; y++)
891 p=GetVirtualPixels(image,0,y,image->columns,1,
893 if (p == (const PixelPacket *) NULL)
895 for (x=0; x < (long) image->columns; x++)
897 *q++=ScaleQuantumToChar(PixelIntensityToQuantum(p));
900 progress=SetImageProgress(image,SaveImageTag,y,image->rows);
901 if (progress == MagickFalse)
904 length=(size_t) (q-pixels);
905 if (compression == LZWCompression)
906 status=LZWEncodeImage(image,length,pixels);
908 status=PackbitsEncodeImage(image,length,pixels);
909 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
910 if (status == MagickFalse)
912 (void) CloseBlob(image);
920 Dump uncompressed PseudoColor packets.
922 Ascii85Initialize(image);
923 for (y=0; y < (long) image->rows; y++)
925 p=GetVirtualPixels(image,0,y,image->columns,1,
927 if (p == (const PixelPacket *) NULL)
929 for (x=0; x < (long) image->columns; x++)
932 ScaleQuantumToChar(PixelIntensityToQuantum(p)));
935 progress=SetImageProgress(image,SaveImageTag,y,image->rows);
936 if (progress == MagickFalse)
945 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
946 (compression == JPEGCompression) || (image->matte != MagickFalse))
948 (void) FormatMagickString(buffer,MaxTextExtent,"%lu %lu\n0\n%d\n",
949 image->columns,image->rows,(int)
950 (image->colorspace == CMYKColorspace));
951 (void) WriteBlobString(image,buffer);
952 (void) FormatMagickString(buffer,MaxTextExtent,"%d\n",
953 (int) (compression == NoCompression));
954 (void) WriteBlobString(image,buffer);
957 case JPEGCompression:
959 status=InjectImageBlob(image_info,image,image,"jpeg",
961 if (status == MagickFalse)
962 ThrowWriterException(CoderError,image->exception.reason);
968 register unsigned char
972 Allocate pixel array.
974 length=(size_t) number_pixels;
975 pixels=(unsigned char *) AcquireQuantumMemory(length,
977 if (pixels == (unsigned char *) NULL)
978 ThrowWriterException(ResourceLimitError,
979 "MemoryAllocationFailed");
981 Dump Packbit encoded pixels.
984 for (y=0; y < (long) image->rows; y++)
986 p=GetVirtualPixels(image,0,y,image->columns,1,
988 if (p == (const PixelPacket *) NULL)
990 indexes=GetVirtualIndexQueue(image);
991 for (x=0; x < (long) image->columns; x++)
993 if ((image->matte != MagickFalse) &&
994 (p->opacity == (Quantum) TransparentOpacity))
996 *q++=ScaleQuantumToChar((Quantum) QuantumRange);
997 *q++=ScaleQuantumToChar((Quantum) QuantumRange);
998 *q++=ScaleQuantumToChar((Quantum) QuantumRange);
1001 if (image->colorspace != CMYKColorspace)
1003 *q++=ScaleQuantumToChar(p->red);
1004 *q++=ScaleQuantumToChar(p->green);
1005 *q++=ScaleQuantumToChar(p->blue);
1009 *q++=ScaleQuantumToChar(p->red);
1010 *q++=ScaleQuantumToChar(p->green);
1011 *q++=ScaleQuantumToChar(p->blue);
1012 *q++=ScaleQuantumToChar(indexes[x]);
1016 progress=SetImageProgress(image,SaveImageTag,y,image->rows);
1017 if (progress == MagickFalse)
1020 length=(size_t) (q-pixels);
1021 if (compression == LZWCompression)
1022 status=LZWEncodeImage(image,length,pixels);
1024 status=PackbitsEncodeImage(image,length,pixels);
1025 if (status == MagickFalse)
1027 (void) CloseBlob(image);
1028 return(MagickFalse);
1030 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1036 Dump uncompressed DirectColor packets.
1038 Ascii85Initialize(image);
1039 for (y=0; y < (long) image->rows; y++)
1041 p=GetVirtualPixels(image,0,y,image->columns,1,
1043 if (p == (const PixelPacket *) NULL)
1045 indexes=GetVirtualIndexQueue(image);
1046 for (x=0; x < (long) image->columns; x++)
1048 if ((image->matte != MagickFalse) &&
1049 (p->opacity == (Quantum) TransparentOpacity))
1051 Ascii85Encode(image,ScaleQuantumToChar((Quantum)
1053 Ascii85Encode(image,ScaleQuantumToChar((Quantum)
1055 Ascii85Encode(image,ScaleQuantumToChar((Quantum)
1059 if (image->colorspace != CMYKColorspace)
1061 Ascii85Encode(image,ScaleQuantumToChar(p->red));
1062 Ascii85Encode(image,ScaleQuantumToChar(p->green));
1063 Ascii85Encode(image,ScaleQuantumToChar(p->blue));
1067 Ascii85Encode(image,ScaleQuantumToChar(p->red));
1068 Ascii85Encode(image,ScaleQuantumToChar(p->green));
1069 Ascii85Encode(image,ScaleQuantumToChar(p->blue));
1070 Ascii85Encode(image,ScaleQuantumToChar(indexes[x]));
1074 progress=SetImageProgress(image,SaveImageTag,y,image->rows);
1075 if (progress == MagickFalse)
1078 Ascii85Flush(image);
1086 Dump number of colors and colormap.
1088 (void) FormatMagickString(buffer,MaxTextExtent,"%lu %lu\n1\n%d\n",
1089 image->columns,image->rows,(int)
1090 (image->colorspace == CMYKColorspace));
1091 (void) WriteBlobString(image,buffer);
1092 (void) FormatMagickString(buffer,MaxTextExtent,"%d\n",
1093 (int) (compression == NoCompression));
1094 (void) WriteBlobString(image,buffer);
1095 (void) FormatMagickString(buffer,MaxTextExtent,"%lu\n",image->colors);
1096 (void) WriteBlobString(image,buffer);
1097 for (i=0; i < (long) image->colors; i++)
1099 (void) FormatMagickString(buffer,MaxTextExtent,"%02X%02X%02X\n",
1100 ScaleQuantumToChar(image->colormap[i].red),
1101 ScaleQuantumToChar(image->colormap[i].green),
1102 ScaleQuantumToChar(image->colormap[i].blue));
1103 (void) WriteBlobString(image,buffer);
1105 switch (compression)
1107 case RLECompression:
1110 register unsigned char
1114 Allocate pixel array.
1116 length=(size_t) number_pixels;
1117 pixels=(unsigned char *) AcquireQuantumMemory(length,
1119 if (pixels == (unsigned char *) NULL)
1120 ThrowWriterException(ResourceLimitError,
1121 "MemoryAllocationFailed");
1123 Dump Runlength encoded pixels.
1126 for (y=0; y < (long) image->rows; y++)
1128 p=GetVirtualPixels(image,0,y,image->columns,1,
1130 if (p == (const PixelPacket *) NULL)
1132 indexes=GetVirtualIndexQueue(image);
1133 for (x=0; x < (long) image->columns; x++)
1134 *q++=(unsigned char) indexes[x];
1135 progress=SetImageProgress(image,SaveImageTag,y,image->rows);
1136 if (progress == MagickFalse)
1139 length=(size_t) (q-pixels);
1140 if (compression == LZWCompression)
1141 status=LZWEncodeImage(image,length,pixels);
1143 status=PackbitsEncodeImage(image,length,pixels);
1144 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1145 if (status == MagickFalse)
1147 (void) CloseBlob(image);
1148 return(MagickFalse);
1155 Dump uncompressed PseudoColor packets.
1157 Ascii85Initialize(image);
1158 for (y=0; y < (long) image->rows; y++)
1160 p=GetVirtualPixels(image,0,y,image->columns,1,
1162 if (p == (const PixelPacket *) NULL)
1164 indexes=GetVirtualIndexQueue(image);
1165 for (x=0; x < (long) image->columns; x++)
1166 Ascii85Encode(image,(unsigned char) indexes[x]);
1167 progress=SetImageProgress(image,SaveImageTag,y,image->rows);
1168 if (progress == MagickFalse)
1171 Ascii85Flush(image);
1176 (void) WriteBlobByte(image,'\n');
1177 length=(size_t) (TellBlob(image)-stop);
1178 stop=TellBlob(image);
1179 offset=SeekBlob(image,start,SEEK_SET);
1181 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
1182 (void) FormatMagickString(buffer,MaxTextExtent,
1183 "%%%%BeginData:%13ld %s Bytes\n",(long) length,
1184 compression == NoCompression ? "ASCII" : "Binary");
1185 (void) WriteBlobString(image,buffer);
1186 offset=SeekBlob(image,stop,SEEK_SET);
1187 (void) WriteBlobString(image,"%%EndData\n");
1188 if (LocaleCompare(image_info->magick,"PS2") != 0)
1189 (void) WriteBlobString(image,"end\n");
1190 (void) WriteBlobString(image,"%%PageTrailer\n");
1191 if (GetNextImageInList(image) == (Image *) NULL)
1193 image=SyncNextImageInList(image);
1194 status=SetImageProgress(image,SaveImagesTag,scene++,
1195 GetImageListLength(image));
1196 if (status == MagickFalse)
1198 } while (image_info->adjoin != MagickFalse);
1199 (void) WriteBlobString(image,"%%Trailer\n");
1202 (void) FormatMagickString(buffer,MaxTextExtent,
1203 "%%%%BoundingBox: %ld %ld %ld %ld\n",(long) (bounds.x1+0.5),
1204 (long) (bounds.y1+0.5),(long) (bounds.x2+0.5),(long) (bounds.y2+0.5));
1205 (void) WriteBlobString(image,buffer);
1206 (void) FormatMagickString(buffer,MaxTextExtent,
1207 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,bounds.y1,bounds.x2,
1209 (void) WriteBlobString(image,buffer);
1211 (void) WriteBlobString(image,"%%EOF\n");
1212 (void) CloseBlob(image);