2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Write Postscript Level II Format %
20 % Copyright 1999-2017 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 % https://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 "MagickCore/studio.h"
43 #include "MagickCore/attribute.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/color.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/compress.h"
50 #include "MagickCore/constitute.h"
51 #include "MagickCore/draw.h"
52 #include "MagickCore/exception.h"
53 #include "MagickCore/exception-private.h"
54 #include "MagickCore/geometry.h"
55 #include "MagickCore/image.h"
56 #include "MagickCore/image-private.h"
57 #include "MagickCore/list.h"
58 #include "MagickCore/magick.h"
59 #include "MagickCore/memory_.h"
60 #include "MagickCore/monitor.h"
61 #include "MagickCore/monitor-private.h"
62 #include "MagickCore/monitor-private.h"
63 #include "MagickCore/option.h"
64 #include "MagickCore/pixel-accessor.h"
65 #include "MagickCore/property.h"
66 #include "MagickCore/quantum-private.h"
67 #include "MagickCore/resource_.h"
68 #include "MagickCore/static.h"
69 #include "MagickCore/string_.h"
70 #include "MagickCore/module.h"
71 #include "MagickCore/utility.h"
76 #if defined(MAGICKCORE_TIFF_DELEGATE)
77 #define CCITTParam "-1"
79 #define CCITTParam "0"
85 static MagickBooleanType
86 WritePS2Image(const ImageInfo *,Image *,ExceptionInfo *);
89 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93 % R e g i s t e r P S 2 I m a g e %
97 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
99 % RegisterPS2Image() adds properties for the PS2 image format to
100 % the list of supported formats. The properties include the image format
101 % tag, a method to read and/or write the format, whether the format
102 % supports the saving of more than one frame to the same file or blob,
103 % whether the format supports native in-memory I/O, and a brief
104 % description of the format.
106 % The format of the RegisterPS2Image method is:
108 % size_t RegisterPS2Image(void)
111 ModuleExport size_t RegisterPS2Image(void)
116 entry=AcquireMagickInfo("PS2","EPS2","Level II Encapsulated PostScript");
117 entry->encoder=(EncodeImageHandler *) WritePS2Image;
118 entry->flags^=CoderAdjoinFlag;
119 entry->flags|=CoderEncoderSeekableStreamFlag;
120 entry->mime_type=ConstantString("application/postscript");
121 (void) RegisterMagickInfo(entry);
122 entry=AcquireMagickInfo("PS2","PS2","Level II PostScript");
123 entry->encoder=(EncodeImageHandler *) WritePS2Image;
124 entry->flags|=CoderEncoderSeekableStreamFlag;
125 entry->mime_type=ConstantString("application/postscript");
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,
174 % Image *image,ExceptionInfo *exception)
176 % A description of each parameter follows:
178 % o image_info: the image info.
180 % o image: the image.
182 % o exception: return any errors or warnings in this structure.
186 static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
187 Image *image,Image *inject_image,ExceptionInfo *exception)
205 write_info=CloneImageInfo(image_info);
206 (void) CopyMagickString(write_info->filename,"GROUP4:",MagickPathExtent);
207 (void) CopyMagickString(write_info->magick,"GROUP4",MagickPathExtent);
208 group4_image=CloneImage(inject_image,0,0,MagickTrue,exception);
209 if (group4_image == (Image *) NULL)
211 group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
213 group4_image=DestroyImage(group4_image);
214 if (group4 == (unsigned char *) NULL)
216 write_info=DestroyImageInfo(write_info);
217 if (WriteBlob(image,length,group4) != (ssize_t) length)
219 group4=(unsigned char *) RelinquishMagickMemory(group4);
223 static MagickBooleanType WritePS2Image(const ImageInfo *image_info,Image *image,
224 ExceptionInfo *exception)
227 *const PostscriptProlog[]=
231 "%% Display a color image. The image is displayed in color on",
232 "%% Postscript viewers or printers that support color, otherwise",
233 "%% it is displayed as grayscale.",
238 " %% Display a DirectClass image.",
242 " /DeviceRGB setcolorspace",
247 " /BitsPerComponent 8",
248 " /Decode [0 1 0 1 0 1]",
249 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
251 " { /DataSource pixel_stream %s }",
252 " { /DataSource pixel_stream %s } ifelse",
256 " /DeviceCMYK setcolorspace",
261 " /BitsPerComponent 8",
262 " /Decode [1 0 1 0 1 0 1 0]",
263 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
265 " { /DataSource pixel_stream %s }",
266 " { /DataSource pixel_stream %s } ifelse",
274 " %% Display a PseudoClass image.",
277 " %% colors: number of colors in the colormap.",
279 " currentfile buffer readline pop",
280 " token pop /colors exch def pop",
284 " %% Image is grayscale.",
286 " currentfile buffer readline pop",
287 " token pop /bits exch def pop",
288 " /DeviceGray setcolorspace",
293 " /BitsPerComponent bits",
295 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
297 " { /DataSource pixel_stream %s }",
299 " /DataSource pixel_stream %s",
304 " >> /CCITTFaxDecode filter",
311 " %% colormap: red, green, blue color packets.",
313 " /colormap colors 3 mul string def",
314 " currentfile colormap readhexstring pop pop",
315 " currentfile buffer readline pop",
316 " [ /Indexed /DeviceRGB colors 1 sub colormap ] setcolorspace",
321 " /BitsPerComponent 8",
323 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
325 " { /DataSource pixel_stream %s }",
326 " { /DataSource pixel_stream %s } ifelse",
334 " %% Display a DirectClass or PseudoClass image.",
337 " %% x & y translation.",
339 " %% label pointsize.",
341 " %% image columns & rows.",
342 " %% class: 0-DirectClass or 1-PseudoClass.",
343 " %% colorspace: 0-RGB or 1-CMYK.",
344 " %% compression: 0-RLECompression or 1-NoCompression.",
345 " %% hex color packets.",
348 " /buffer 512 string def",
349 " /pixel_stream currentfile def",
351 " currentfile buffer readline pop",
352 " token pop /x exch def",
353 " token pop /y exch def pop",
355 " currentfile buffer readline pop",
356 " token pop /x exch def",
357 " token pop /y exch def pop",
358 " currentfile buffer readline pop",
359 " token pop /pointsize exch def pop",
362 *const PostscriptEpilog[]=
365 " currentfile buffer readline pop",
366 " token pop /columns exch def",
367 " token pop /rows exch def pop",
368 " currentfile buffer readline pop",
369 " token pop /class exch def pop",
370 " currentfile buffer readline pop",
371 " token pop /colorspace exch def pop",
372 " currentfile buffer readline pop",
373 " token pop /compression exch def pop",
374 " class 0 gt { PseudoClassImage } { DirectClassImage } ifelse",
380 buffer[MagickPathExtent],
381 date[MagickPathExtent],
382 page_geometry[MagickPathExtent],
426 register const Quantum
454 Open output image file.
456 assert(image_info != (const ImageInfo *) NULL);
457 assert(image_info->signature == MagickCoreSignature);
458 assert(image != (Image *) NULL);
459 assert(image->signature == MagickCoreSignature);
460 if (image->debug != MagickFalse)
461 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
462 assert(exception != (ExceptionInfo *) NULL);
463 assert(exception->signature == MagickCoreSignature);
464 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
465 if (status == MagickFalse)
467 compression=image->compression;
468 if (image_info->compression != UndefinedCompression)
469 compression=image_info->compression;
472 #if !defined(MAGICKCORE_JPEG_DELEGATE)
473 case JPEGCompression:
475 compression=RLECompression;
476 (void) ThrowMagickException(exception,GetMagickModule(),
477 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
485 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
491 Scale relative to dots-per-inch.
493 delta.x=DefaultResolution;
494 delta.y=DefaultResolution;
495 resolution.x=image->resolution.x;
496 resolution.y=image->resolution.y;
497 if ((resolution.x == 0.0) || (resolution.y == 0.0))
499 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
500 resolution.x=geometry_info.rho;
501 resolution.y=geometry_info.sigma;
502 if ((flags & SigmaValue) == 0)
503 resolution.y=resolution.x;
505 if (image_info->density != (char *) NULL)
507 flags=ParseGeometry(image_info->density,&geometry_info);
508 resolution.x=geometry_info.rho;
509 resolution.y=geometry_info.sigma;
510 if ((flags & SigmaValue) == 0)
511 resolution.y=resolution.x;
513 if (image->units == PixelsPerCentimeterResolution)
515 resolution.x=(size_t) (100.0*2.54*resolution.x+0.5)/100.0;
516 resolution.y=(size_t) (100.0*2.54*resolution.y+0.5)/100.0;
518 SetGeometry(image,&geometry);
519 (void) FormatLocaleString(page_geometry,MagickPathExtent,"%.20gx%.20g",
520 (double) image->columns,(double) image->rows);
521 if (image_info->page != (char *) NULL)
522 (void) CopyMagickString(page_geometry,image_info->page,MagickPathExtent);
524 if ((image->page.width != 0) && (image->page.height != 0))
525 (void) FormatLocaleString(page_geometry,MagickPathExtent,
526 "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,(double)
527 image->page.height,(double) image->page.x,(double) image->page.y);
529 if ((image->gravity != UndefinedGravity) &&
530 (LocaleCompare(image_info->magick,"PS") == 0))
531 (void) CopyMagickString(page_geometry,PSPageGeometry,MagickPathExtent);
532 (void) ConcatenateMagickString(page_geometry,">",MagickPathExtent);
533 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
534 &geometry.width,&geometry.height);
535 scale.x=(double) (geometry.width*delta.x)/resolution.x;
536 geometry.width=(size_t) floor(scale.x+0.5);
537 scale.y=(double) (geometry.height*delta.y)/resolution.y;
538 geometry.height=(size_t) floor(scale.y+0.5);
539 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
540 (void) ParseGravityGeometry(image,page_geometry,&page_info,exception);
541 if (image->gravity != UndefinedGravity)
543 geometry.x=(-page_info.x);
544 geometry.y=(ssize_t) (media_info.height+page_info.y-image->rows);
547 if (image_info->pointsize != 0.0)
548 pointsize=image_info->pointsize;
550 value=GetImageProperty(image,"label",exception);
551 if (value != (const char *) NULL)
552 text_size=(size_t) (MultilineCensus(value)*pointsize+12);
556 Output Postscript header.
558 if (LocaleCompare(image_info->magick,"PS2") == 0)
559 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0\n",MagickPathExtent);
561 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0 EPSF-3.0\n",
563 (void) WriteBlobString(image,buffer);
564 (void) WriteBlobString(image,"%%Creator: (ImageMagick)\n");
565 (void) FormatLocaleString(buffer,MagickPathExtent,"%%%%Title: (%s)\n",
567 (void) WriteBlobString(image,buffer);
568 timer=time((time_t *) NULL);
569 (void) FormatMagickTime(timer,MagickPathExtent,date);
570 (void) FormatLocaleString(buffer,MagickPathExtent,
571 "%%%%CreationDate: (%s)\n",date);
572 (void) WriteBlobString(image,buffer);
573 bounds.x1=(double) geometry.x;
574 bounds.y1=(double) geometry.y;
575 bounds.x2=(double) geometry.x+geometry.width;
576 bounds.y2=(double) geometry.y+geometry.height+text_size;
577 if ((image_info->adjoin != MagickFalse) &&
578 (GetNextImageInList(image) != (Image *) NULL))
579 (void) CopyMagickString(buffer,"%%BoundingBox: (atend)\n",
583 (void) FormatLocaleString(buffer,MagickPathExtent,
584 "%%%%BoundingBox: %.20g %.20g %.20g %.20g\n",ceil(bounds.x1-0.5),
585 ceil(bounds.y1-0.5),floor(bounds.x2+0.5),floor(bounds.y2+0.5));
586 (void) WriteBlobString(image,buffer);
587 (void) FormatLocaleString(buffer,MagickPathExtent,
588 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,
589 bounds.y1,bounds.x2,bounds.y2);
591 (void) WriteBlobString(image,buffer);
592 value=GetImageProperty(image,"label",exception);
593 if (value != (const char *) NULL)
594 (void) WriteBlobString(image,
595 "%%DocumentNeededResources: font Helvetica\n");
596 (void) WriteBlobString(image,"%%LanguageLevel: 2\n");
597 if (LocaleCompare(image_info->magick,"PS2") != 0)
598 (void) WriteBlobString(image,"%%Pages: 1\n");
601 (void) WriteBlobString(image,"%%Orientation: Portrait\n");
602 (void) WriteBlobString(image,"%%PageOrder: Ascend\n");
603 if (image_info->adjoin == MagickFalse)
604 (void) CopyMagickString(buffer,"%%Pages: 1\n",MagickPathExtent);
606 (void) FormatLocaleString(buffer,MagickPathExtent,
607 "%%%%Pages: %.20g\n",(double) GetImageListLength(image));
608 (void) WriteBlobString(image,buffer);
610 if (image->colorspace == CMYKColorspace)
611 (void) WriteBlobString(image,
612 "%%DocumentProcessColors: Cyan Magenta Yellow Black\n");
613 (void) WriteBlobString(image,"%%EndComments\n");
614 (void) WriteBlobString(image,"\n%%BeginDefaults\n");
615 (void) WriteBlobString(image,"%%EndDefaults\n\n");
617 Output Postscript commands.
619 for (q=PostscriptProlog; *q; q++)
625 (void) FormatLocaleString(buffer,MagickPathExtent,*q,
626 "/ASCII85Decode filter");
629 case JPEGCompression:
631 (void) FormatLocaleString(buffer,MagickPathExtent,*q,
632 "/DCTDecode filter");
637 (void) FormatLocaleString(buffer,MagickPathExtent,*q,
638 "/LZWDecode filter");
642 case Group4Compression:
644 (void) FormatLocaleString(buffer,MagickPathExtent,*q," ");
649 (void) FormatLocaleString(buffer,MagickPathExtent,*q,
650 "/RunLengthDecode filter");
654 (void) WriteBlobString(image,buffer);
655 (void) WriteBlobByte(image,'\n');
657 value=GetImageProperty(image,"label",exception);
658 if (value != (const char *) NULL)
660 (void) WriteBlobString(image,
661 " /Helvetica findfont pointsize scalefont setfont\n");
662 for (j=(ssize_t) MultilineCensus(value)-1; j >= 0; j--)
664 (void) WriteBlobString(image," /label 512 string def\n");
665 (void) WriteBlobString(image,
666 " currentfile label readline pop\n");
667 (void) FormatLocaleString(buffer,MagickPathExtent,
668 " 0 y %g add moveto label show pop\n",j*pointsize+12);
669 (void) WriteBlobString(image,buffer);
672 for (q=PostscriptEpilog; *q; q++)
674 (void) FormatLocaleString(buffer,MagickPathExtent,"%s\n",*q);
675 (void) WriteBlobString(image,buffer);
677 if (LocaleCompare(image_info->magick,"PS2") == 0)
678 (void) WriteBlobString(image," showpage\n");
679 (void) WriteBlobString(image,"} bind def\n");
680 (void) WriteBlobString(image,"%%EndProlog\n");
682 (void) FormatLocaleString(buffer,MagickPathExtent,"%%%%Page: 1 %.20g\n",
684 (void) WriteBlobString(image,buffer);
685 (void) FormatLocaleString(buffer,MagickPathExtent,
686 "%%%%PageBoundingBox: %.20g %.20g %.20g %.20g\n",(double) geometry.x,
687 (double) geometry.y,geometry.x+(double) geometry.width,geometry.y+(double)
688 (geometry.height+text_size));
689 (void) WriteBlobString(image,buffer);
690 if ((double) geometry.x < bounds.x1)
691 bounds.x1=(double) geometry.x;
692 if ((double) geometry.y < bounds.y1)
693 bounds.y1=(double) geometry.y;
694 if ((double) (geometry.x+geometry.width-1) > bounds.x2)
695 bounds.x2=(double) geometry.x+geometry.width-1;
696 if ((double) (geometry.y+(geometry.height+text_size)-1) > bounds.y2)
697 bounds.y2=(double) geometry.y+(geometry.height+text_size)-1;
698 value=GetImageProperty(image,"label",exception);
699 if (value != (const char *) NULL)
700 (void) WriteBlobString(image,"%%PageResources: font Helvetica\n");
701 if (LocaleCompare(image_info->magick,"PS2") != 0)
702 (void) WriteBlobString(image,"userdict begin\n");
703 start=TellBlob(image);
704 (void) FormatLocaleString(buffer,MagickPathExtent,
705 "%%%%BeginData:%13ld %s Bytes\n",0L,
706 compression == NoCompression ? "ASCII" : "Binary");
707 (void) WriteBlobString(image,buffer);
708 stop=TellBlob(image);
709 (void) WriteBlobString(image,"DisplayImage\n");
713 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g %.20g\n%g %g\n%g\n",
714 (double) geometry.x,(double) geometry.y,scale.x,scale.y,pointsize);
715 (void) WriteBlobString(image,buffer);
716 labels=(char **) NULL;
717 value=GetImageProperty(image,"label",exception);
718 if (value != (const char *) NULL)
719 labels=StringToList(value);
720 if (labels != (char **) NULL)
722 for (i=0; labels[i] != (char *) NULL; i++)
724 (void) FormatLocaleString(buffer,MagickPathExtent,"%s \n",
726 (void) WriteBlobString(image,buffer);
727 labels[i]=DestroyString(labels[i]);
729 labels=(char **) RelinquishMagickMemory(labels);
731 number_pixels=(MagickSizeType) image->columns*image->rows;
732 if (number_pixels != (MagickSizeType) ((size_t) number_pixels))
733 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
734 if ((compression == FaxCompression) || (compression == Group4Compression) ||
735 ((image_info->type != TrueColorType) &&
736 (SetImageGray(image,exception) != MagickFalse)))
738 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g %.20g\n1\n%d\n",
739 (double) image->columns,(double) image->rows,(int)
740 (image->colorspace == CMYKColorspace));
741 (void) WriteBlobString(image,buffer);
742 (void) FormatLocaleString(buffer,MagickPathExtent,"%d\n",
743 (int) ((compression != FaxCompression) &&
744 (compression != Group4Compression)));
745 (void) WriteBlobString(image,buffer);
746 (void) WriteBlobString(image,"0\n");
747 (void) FormatLocaleString(buffer,MagickPathExtent,"%d\n",
748 (compression == FaxCompression) ||
749 (compression == Group4Compression) ? 1 : 8);
750 (void) WriteBlobString(image,buffer);
754 case Group4Compression:
756 if (LocaleCompare(CCITTParam,"0") == 0)
758 (void) HuffmanEncodeImage(image_info,image,image,exception);
761 (void) Huffman2DEncodeImage(image_info,image,image,exception);
764 case JPEGCompression:
766 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
767 if (status == MagickFalse)
769 (void) CloseBlob(image);
780 register unsigned char
784 Allocate pixel array.
786 length=(size_t) number_pixels;
787 pixel_info=AcquireVirtualMemory(length,sizeof(*pixels));
788 if (pixel_info == (MemoryInfo *) NULL)
789 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
790 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
792 Dump runlength encoded pixels.
795 for (y=0; y < (ssize_t) image->rows; y++)
797 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
798 if (p == (const Quantum *) NULL)
800 for (x=0; x < (ssize_t) image->columns; x++)
802 *q++=ScaleQuantumToChar(ClampToQuantum(GetPixelLuma(image,p)));
803 p+=GetPixelChannels(image);
805 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
807 if (progress == MagickFalse)
810 length=(size_t) (q-pixels);
811 if (compression == LZWCompression)
812 status=LZWEncodeImage(image,length,pixels,exception);
814 status=PackbitsEncodeImage(image,length,pixels,exception);
815 pixel_info=RelinquishVirtualMemory(pixel_info);
816 if (status == MagickFalse)
818 (void) CloseBlob(image);
826 Dump uncompressed PseudoColor packets.
828 Ascii85Initialize(image);
829 for (y=0; y < (ssize_t) image->rows; y++)
831 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
832 if (p == (const Quantum *) NULL)
834 for (x=0; x < (ssize_t) image->columns; x++)
836 Ascii85Encode(image,ScaleQuantumToChar(ClampToQuantum(
837 GetPixelLuma(image,p))));
838 p+=GetPixelChannels(image);
840 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
842 if (progress == MagickFalse)
851 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
852 (compression == JPEGCompression) || (image->alpha_trait != UndefinedPixelTrait))
854 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g %.20g\n0\n%d\n",
855 (double) image->columns,(double) image->rows,(int)
856 (image->colorspace == CMYKColorspace));
857 (void) WriteBlobString(image,buffer);
858 (void) FormatLocaleString(buffer,MagickPathExtent,"%d\n",
859 (int) (compression == NoCompression));
860 (void) WriteBlobString(image,buffer);
863 case JPEGCompression:
865 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
866 if (status == MagickFalse)
868 (void) CloseBlob(image);
879 register unsigned char
883 Allocate pixel array.
885 length=(size_t) number_pixels;
886 pixel_info=AcquireVirtualMemory(length,4*sizeof(*pixels));
887 if (pixel_info == (MemoryInfo *) NULL)
888 ThrowWriterException(ResourceLimitError,
889 "MemoryAllocationFailed");
890 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
892 Dump runlength encoded pixels.
895 for (y=0; y < (ssize_t) image->rows; y++)
897 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
898 if (p == (const Quantum *) NULL)
900 for (x=0; x < (ssize_t) image->columns; x++)
902 if ((image->alpha_trait != UndefinedPixelTrait) &&
903 (GetPixelAlpha(image,p) == (Quantum) TransparentAlpha))
905 *q++=ScaleQuantumToChar(QuantumRange);
906 *q++=ScaleQuantumToChar(QuantumRange);
907 *q++=ScaleQuantumToChar(QuantumRange);
910 if (image->colorspace != CMYKColorspace)
912 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
913 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
914 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
918 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
919 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
920 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
921 *q++=ScaleQuantumToChar(GetPixelBlack(image,p));
923 p+=GetPixelChannels(image);
925 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
927 if (progress == MagickFalse)
930 length=(size_t) (q-pixels);
931 if (compression == LZWCompression)
932 status=LZWEncodeImage(image,length,pixels,exception);
934 status=PackbitsEncodeImage(image,length,pixels,exception);
935 if (status == MagickFalse)
937 (void) CloseBlob(image);
940 pixel_info=RelinquishVirtualMemory(pixel_info);
946 Dump uncompressed DirectColor packets.
948 Ascii85Initialize(image);
949 for (y=0; y < (ssize_t) image->rows; y++)
951 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
952 if (p == (const Quantum *) NULL)
954 for (x=0; x < (ssize_t) image->columns; x++)
956 if ((image->alpha_trait != UndefinedPixelTrait) &&
957 (GetPixelAlpha(image,p) == (Quantum) TransparentAlpha))
959 Ascii85Encode(image,ScaleQuantumToChar((Quantum)
961 Ascii85Encode(image,ScaleQuantumToChar((Quantum)
963 Ascii85Encode(image,ScaleQuantumToChar((Quantum)
967 if (image->colorspace != CMYKColorspace)
969 Ascii85Encode(image,ScaleQuantumToChar(
970 GetPixelRed(image,p)));
971 Ascii85Encode(image,ScaleQuantumToChar(
972 GetPixelGreen(image,p)));
973 Ascii85Encode(image,ScaleQuantumToChar(
974 GetPixelBlue(image,p)));
978 Ascii85Encode(image,ScaleQuantumToChar(
979 GetPixelRed(image,p)));
980 Ascii85Encode(image,ScaleQuantumToChar(
981 GetPixelGreen(image,p)));
982 Ascii85Encode(image,ScaleQuantumToChar(
983 GetPixelBlue(image,p)));
984 Ascii85Encode(image,ScaleQuantumToChar(
985 GetPixelBlack(image,p)));
987 p+=GetPixelChannels(image);
989 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
991 if (progress == MagickFalse)
1002 Dump number of colors and colormap.
1004 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g %.20g\n1\n%d\n",
1005 (double) image->columns,(double) image->rows,(int)
1006 (image->colorspace == CMYKColorspace));
1007 (void) WriteBlobString(image,buffer);
1008 (void) FormatLocaleString(buffer,MagickPathExtent,"%d\n",
1009 (int) (compression == NoCompression));
1010 (void) WriteBlobString(image,buffer);
1011 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g\n",(double)
1013 (void) WriteBlobString(image,buffer);
1014 for (i=0; i < (ssize_t) image->colors; i++)
1016 (void) FormatLocaleString(buffer,MagickPathExtent,"%02X%02X%02X\n",
1017 ScaleQuantumToChar(image->colormap[i].red),
1018 ScaleQuantumToChar(image->colormap[i].green),
1019 ScaleQuantumToChar(image->colormap[i].blue));
1020 (void) WriteBlobString(image,buffer);
1022 switch (compression)
1024 case RLECompression:
1030 register unsigned char
1034 Allocate pixel array.
1036 length=(size_t) number_pixels;
1037 pixel_info=AcquireVirtualMemory(length,sizeof(*pixels));
1038 if (pixel_info == (MemoryInfo *) NULL)
1039 ThrowWriterException(ResourceLimitError,
1040 "MemoryAllocationFailed");
1041 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
1043 Dump runlength encoded pixels.
1046 for (y=0; y < (ssize_t) image->rows; y++)
1048 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1049 if (p == (const Quantum *) NULL)
1051 for (x=0; x < (ssize_t) image->columns; x++)
1053 *q++=(unsigned char) GetPixelIndex(image,p);
1054 p+=GetPixelChannels(image);
1056 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1058 if (progress == MagickFalse)
1061 length=(size_t) (q-pixels);
1062 if (compression == LZWCompression)
1063 status=LZWEncodeImage(image,length,pixels,exception);
1065 status=PackbitsEncodeImage(image,length,pixels,exception);
1066 pixel_info=RelinquishVirtualMemory(pixel_info);
1067 if (status == MagickFalse)
1069 (void) CloseBlob(image);
1070 return(MagickFalse);
1077 Dump uncompressed PseudoColor packets.
1079 Ascii85Initialize(image);
1080 for (y=0; y < (ssize_t) image->rows; y++)
1082 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1083 if (p == (const Quantum *) NULL)
1085 for (x=0; x < (ssize_t) image->columns; x++)
1087 Ascii85Encode(image,(unsigned char) GetPixelIndex(image,p));
1088 p+=GetPixelChannels(image);
1090 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1092 if (progress == MagickFalse)
1095 Ascii85Flush(image);
1100 (void) WriteBlobByte(image,'\n');
1101 length=(size_t) (TellBlob(image)-stop);
1102 stop=TellBlob(image);
1103 offset=SeekBlob(image,start,SEEK_SET);
1105 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
1106 (void) FormatLocaleString(buffer,MagickPathExtent,
1107 "%%%%BeginData:%13ld %s Bytes\n",(long) length,
1108 compression == NoCompression ? "ASCII" : "Binary");
1109 (void) WriteBlobString(image,buffer);
1110 offset=SeekBlob(image,stop,SEEK_SET);
1111 (void) WriteBlobString(image,"%%EndData\n");
1112 if (LocaleCompare(image_info->magick,"PS2") != 0)
1113 (void) WriteBlobString(image,"end\n");
1114 (void) WriteBlobString(image,"%%PageTrailer\n");
1115 if (GetNextImageInList(image) == (Image *) NULL)
1117 image=SyncNextImageInList(image);
1118 status=SetImageProgress(image,SaveImagesTag,scene++,
1119 GetImageListLength(image));
1120 if (status == MagickFalse)
1122 } while (image_info->adjoin != MagickFalse);
1123 (void) WriteBlobString(image,"%%Trailer\n");
1126 (void) FormatLocaleString(buffer,MagickPathExtent,
1127 "%%%%BoundingBox: %.20g %.20g %.20g %.20g\n",ceil(bounds.x1-0.5),
1128 ceil(bounds.y1-0.5),floor(bounds.x2+0.5),floor(bounds.y2+0.5));
1129 (void) WriteBlobString(image,buffer);
1130 (void) FormatLocaleString(buffer,MagickPathExtent,
1131 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,bounds.y1,
1132 bounds.x2,bounds.y2);
1133 (void) WriteBlobString(image,buffer);
1135 (void) WriteBlobString(image,"%%EOF\n");
1136 (void) CloseBlob(image);