2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Write Postscript Level II Format %
20 % Copyright 1999-2018 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->flags^=CoderBlobSupportFlag;
121 entry->mime_type=ConstantString("application/postscript");
122 (void) RegisterMagickInfo(entry);
123 entry=AcquireMagickInfo("PS2","PS2","Level II PostScript");
124 entry->encoder=(EncodeImageHandler *) WritePS2Image;
125 entry->flags|=CoderEncoderSeekableStreamFlag;
126 entry->flags^=CoderBlobSupportFlag;
127 entry->mime_type=ConstantString("application/postscript");
128 (void) RegisterMagickInfo(entry);
129 return(MagickImageCoderSignature);
133 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
137 % U n r e g i s t e r P S 2 I m a g e %
141 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143 % UnregisterPS2Image() removes format registrations made by the
144 % PS2 module from the list of supported formats.
146 % The format of the UnregisterPS2Image method is:
148 % UnregisterPS2Image(void)
151 ModuleExport void UnregisterPS2Image(void)
153 (void) UnregisterMagickInfo("EPS2");
154 (void) UnregisterMagickInfo("PS2");
158 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
162 % W r i t e P S 2 I m a g e %
166 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
168 % WritePS2Image translates an image to encapsulated Postscript
169 % Level II for printing. If the supplied geometry is null, the image is
170 % centered on the Postscript page. Otherwise, the image is positioned as
171 % specified by the geometry.
173 % The format of the WritePS2Image method is:
175 % MagickBooleanType WritePS2Image(const ImageInfo *image_info,
176 % Image *image,ExceptionInfo *exception)
178 % A description of each parameter follows:
180 % o image_info: the image info.
182 % o image: the image.
184 % o exception: return any errors or warnings in this structure.
188 static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
189 Image *image,Image *inject_image,ExceptionInfo *exception)
207 write_info=CloneImageInfo(image_info);
208 (void) CopyMagickString(write_info->filename,"GROUP4:",MagickPathExtent);
209 (void) CopyMagickString(write_info->magick,"GROUP4",MagickPathExtent);
210 group4_image=CloneImage(inject_image,0,0,MagickTrue,exception);
211 if (group4_image == (Image *) NULL)
213 group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
215 group4_image=DestroyImage(group4_image);
216 if (group4 == (unsigned char *) NULL)
218 write_info=DestroyImageInfo(write_info);
219 if (WriteBlob(image,length,group4) != (ssize_t) length)
221 group4=(unsigned char *) RelinquishMagickMemory(group4);
225 static MagickBooleanType WritePS2Image(const ImageInfo *image_info,Image *image,
226 ExceptionInfo *exception)
229 *const PostscriptProlog[]=
233 "%% Display a color image. The image is displayed in color on",
234 "%% Postscript viewers or printers that support color, otherwise",
235 "%% it is displayed as grayscale.",
240 " %% Display a DirectClass image.",
244 " /DeviceRGB setcolorspace",
249 " /BitsPerComponent 8",
250 " /Decode [0 1 0 1 0 1]",
251 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
253 " { /DataSource pixel_stream %s }",
254 " { /DataSource pixel_stream %s } ifelse",
258 " /DeviceCMYK setcolorspace",
263 " /BitsPerComponent 8",
264 " /Decode [1 0 1 0 1 0 1 0]",
265 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
267 " { /DataSource pixel_stream %s }",
268 " { /DataSource pixel_stream %s } ifelse",
276 " %% Display a PseudoClass image.",
279 " %% colors: number of colors in the colormap.",
281 " currentfile buffer readline pop",
282 " token pop /colors exch def pop",
286 " %% Image is grayscale.",
288 " currentfile buffer readline pop",
289 " token pop /bits exch def pop",
290 " /DeviceGray setcolorspace",
295 " /BitsPerComponent bits",
297 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
299 " { /DataSource pixel_stream %s }",
301 " /DataSource pixel_stream %s",
306 " >> /CCITTFaxDecode filter",
313 " %% colormap: red, green, blue color packets.",
315 " /colormap colors 3 mul string def",
316 " currentfile colormap readhexstring pop pop",
317 " currentfile buffer readline pop",
318 " [ /Indexed /DeviceRGB colors 1 sub colormap ] setcolorspace",
323 " /BitsPerComponent 8",
325 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
327 " { /DataSource pixel_stream %s }",
328 " { /DataSource pixel_stream %s } ifelse",
336 " %% Display a DirectClass or PseudoClass image.",
339 " %% x & y translation.",
341 " %% label pointsize.",
343 " %% image columns & rows.",
344 " %% class: 0-DirectClass or 1-PseudoClass.",
345 " %% colorspace: 0-RGB or 1-CMYK.",
346 " %% compression: 0-RLECompression or 1-NoCompression.",
347 " %% hex color packets.",
350 " /buffer 512 string def",
351 " /pixel_stream currentfile def",
353 " currentfile buffer readline pop",
354 " token pop /x exch def",
355 " token pop /y exch def pop",
357 " currentfile buffer readline pop",
358 " token pop /x exch def",
359 " token pop /y exch def pop",
360 " currentfile buffer readline pop",
361 " token pop /pointsize exch def pop",
364 *const PostscriptEpilog[]=
367 " currentfile buffer readline pop",
368 " token pop /columns exch def",
369 " token pop /rows exch def pop",
370 " currentfile buffer readline pop",
371 " token pop /class exch def pop",
372 " currentfile buffer readline pop",
373 " token pop /colorspace exch def pop",
374 " currentfile buffer readline pop",
375 " token pop /compression exch def pop",
376 " class 0 gt { PseudoClassImage } { DirectClassImage } ifelse",
382 buffer[MagickPathExtent],
383 date[MagickPathExtent],
384 page_geometry[MagickPathExtent],
428 register const Quantum
457 Open output image file.
459 assert(image_info != (const ImageInfo *) NULL);
460 assert(image_info->signature == MagickCoreSignature);
461 assert(image != (Image *) NULL);
462 assert(image->signature == MagickCoreSignature);
463 if (image->debug != MagickFalse)
464 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
465 assert(exception != (ExceptionInfo *) NULL);
466 assert(exception->signature == MagickCoreSignature);
467 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
468 if (status == MagickFalse)
470 compression=image->compression;
471 if (image_info->compression != UndefinedCompression)
472 compression=image_info->compression;
475 #if !defined(MAGICKCORE_JPEG_DELEGATE)
476 case JPEGCompression:
478 compression=RLECompression;
479 (void) ThrowMagickException(exception,GetMagickModule(),
480 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
488 (void) memset(&bounds,0,sizeof(bounds));
491 imageListLength=GetImageListLength(image);
495 Scale relative to dots-per-inch.
497 delta.x=DefaultResolution;
498 delta.y=DefaultResolution;
499 resolution.x=image->resolution.x;
500 resolution.y=image->resolution.y;
501 if ((resolution.x == 0.0) || (resolution.y == 0.0))
503 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
504 resolution.x=geometry_info.rho;
505 resolution.y=geometry_info.sigma;
506 if ((flags & SigmaValue) == 0)
507 resolution.y=resolution.x;
509 if (image_info->density != (char *) NULL)
511 flags=ParseGeometry(image_info->density,&geometry_info);
512 resolution.x=geometry_info.rho;
513 resolution.y=geometry_info.sigma;
514 if ((flags & SigmaValue) == 0)
515 resolution.y=resolution.x;
517 if (image->units == PixelsPerCentimeterResolution)
519 resolution.x=(size_t) (100.0*2.54*resolution.x+0.5)/100.0;
520 resolution.y=(size_t) (100.0*2.54*resolution.y+0.5)/100.0;
522 SetGeometry(image,&geometry);
523 (void) FormatLocaleString(page_geometry,MagickPathExtent,"%.20gx%.20g",
524 (double) image->columns,(double) image->rows);
525 if (image_info->page != (char *) NULL)
526 (void) CopyMagickString(page_geometry,image_info->page,MagickPathExtent);
528 if ((image->page.width != 0) && (image->page.height != 0))
529 (void) FormatLocaleString(page_geometry,MagickPathExtent,
530 "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,(double)
531 image->page.height,(double) image->page.x,(double) image->page.y);
533 if ((image->gravity != UndefinedGravity) &&
534 (LocaleCompare(image_info->magick,"PS") == 0))
535 (void) CopyMagickString(page_geometry,PSPageGeometry,MagickPathExtent);
536 (void) ConcatenateMagickString(page_geometry,">",MagickPathExtent);
537 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
538 &geometry.width,&geometry.height);
539 scale.x=(double) (geometry.width*delta.x)/resolution.x;
540 geometry.width=(size_t) floor(scale.x+0.5);
541 scale.y=(double) (geometry.height*delta.y)/resolution.y;
542 geometry.height=(size_t) floor(scale.y+0.5);
543 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
544 (void) ParseGravityGeometry(image,page_geometry,&page_info,exception);
545 if (image->gravity != UndefinedGravity)
547 geometry.x=(-page_info.x);
548 geometry.y=(ssize_t) (media_info.height+page_info.y-image->rows);
551 if (image_info->pointsize != 0.0)
552 pointsize=image_info->pointsize;
554 value=GetImageProperty(image,"label",exception);
555 if (value != (const char *) NULL)
556 text_size=(size_t) (MultilineCensus(value)*pointsize+12);
560 Output Postscript header.
562 if (LocaleCompare(image_info->magick,"PS2") == 0)
563 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0\n",MagickPathExtent);
565 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0 EPSF-3.0\n",
567 (void) WriteBlobString(image,buffer);
568 (void) WriteBlobString(image,"%%Creator: (ImageMagick)\n");
569 (void) FormatLocaleString(buffer,MagickPathExtent,"%%%%Title: (%s)\n",
571 (void) WriteBlobString(image,buffer);
572 timer=time((time_t *) NULL);
573 (void) FormatMagickTime(timer,MagickPathExtent,date);
574 (void) FormatLocaleString(buffer,MagickPathExtent,
575 "%%%%CreationDate: (%s)\n",date);
576 (void) WriteBlobString(image,buffer);
577 bounds.x1=(double) geometry.x;
578 bounds.y1=(double) geometry.y;
579 bounds.x2=(double) geometry.x+geometry.width;
580 bounds.y2=(double) geometry.y+geometry.height+text_size;
581 if ((image_info->adjoin != MagickFalse) &&
582 (GetNextImageInList(image) != (Image *) NULL))
583 (void) CopyMagickString(buffer,"%%BoundingBox: (atend)\n",
587 (void) FormatLocaleString(buffer,MagickPathExtent,
588 "%%%%BoundingBox: %.20g %.20g %.20g %.20g\n",ceil(bounds.x1-0.5),
589 ceil(bounds.y1-0.5),floor(bounds.x2+0.5),floor(bounds.y2+0.5));
590 (void) WriteBlobString(image,buffer);
591 (void) FormatLocaleString(buffer,MagickPathExtent,
592 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,
593 bounds.y1,bounds.x2,bounds.y2);
595 (void) WriteBlobString(image,buffer);
596 value=GetImageProperty(image,"label",exception);
597 if (value != (const char *) NULL)
598 (void) WriteBlobString(image,
599 "%%DocumentNeededResources: font Helvetica\n");
600 (void) WriteBlobString(image,"%%LanguageLevel: 2\n");
601 if (LocaleCompare(image_info->magick,"PS2") != 0)
602 (void) WriteBlobString(image,"%%Pages: 1\n");
605 (void) WriteBlobString(image,"%%Orientation: Portrait\n");
606 (void) WriteBlobString(image,"%%PageOrder: Ascend\n");
607 if (image_info->adjoin == MagickFalse)
608 (void) CopyMagickString(buffer,"%%Pages: 1\n",MagickPathExtent);
610 (void) FormatLocaleString(buffer,MagickPathExtent,
611 "%%%%Pages: %.20g\n",(double) imageListLength);
612 (void) WriteBlobString(image,buffer);
614 if (image->colorspace == CMYKColorspace)
615 (void) WriteBlobString(image,
616 "%%DocumentProcessColors: Cyan Magenta Yellow Black\n");
617 (void) WriteBlobString(image,"%%EndComments\n");
618 (void) WriteBlobString(image,"\n%%BeginDefaults\n");
619 (void) WriteBlobString(image,"%%EndDefaults\n\n");
621 Output Postscript commands.
623 for (q=PostscriptProlog; *q; q++)
629 (void) FormatLocaleString(buffer,MagickPathExtent,*q,
630 "/ASCII85Decode filter");
633 case JPEGCompression:
635 (void) FormatLocaleString(buffer,MagickPathExtent,*q,
636 "/DCTDecode filter");
641 (void) FormatLocaleString(buffer,MagickPathExtent,*q,
642 "/LZWDecode filter");
646 case Group4Compression:
648 (void) FormatLocaleString(buffer,MagickPathExtent,*q," ");
653 (void) FormatLocaleString(buffer,MagickPathExtent,*q,
654 "/RunLengthDecode filter");
658 (void) WriteBlobString(image,buffer);
659 (void) WriteBlobByte(image,'\n');
661 value=GetImageProperty(image,"label",exception);
662 if (value != (const char *) NULL)
664 (void) WriteBlobString(image,
665 " /Helvetica findfont pointsize scalefont setfont\n");
666 for (j=(ssize_t) MultilineCensus(value)-1; j >= 0; j--)
668 (void) WriteBlobString(image," /label 512 string def\n");
669 (void) WriteBlobString(image,
670 " currentfile label readline pop\n");
671 (void) FormatLocaleString(buffer,MagickPathExtent,
672 " 0 y %g add moveto label show pop\n",j*pointsize+12);
673 (void) WriteBlobString(image,buffer);
676 for (q=PostscriptEpilog; *q; q++)
678 (void) FormatLocaleString(buffer,MagickPathExtent,"%s\n",*q);
679 (void) WriteBlobString(image,buffer);
681 if (LocaleCompare(image_info->magick,"PS2") == 0)
682 (void) WriteBlobString(image," showpage\n");
683 (void) WriteBlobString(image,"} bind def\n");
684 (void) WriteBlobString(image,"%%EndProlog\n");
686 (void) FormatLocaleString(buffer,MagickPathExtent,"%%%%Page: 1 %.20g\n",
688 (void) WriteBlobString(image,buffer);
689 (void) FormatLocaleString(buffer,MagickPathExtent,
690 "%%%%PageBoundingBox: %.20g %.20g %.20g %.20g\n",(double) geometry.x,
691 (double) geometry.y,geometry.x+(double) geometry.width,geometry.y+(double)
692 (geometry.height+text_size));
693 (void) WriteBlobString(image,buffer);
694 if ((double) geometry.x < bounds.x1)
695 bounds.x1=(double) geometry.x;
696 if ((double) geometry.y < bounds.y1)
697 bounds.y1=(double) geometry.y;
698 if ((double) (geometry.x+geometry.width-1) > bounds.x2)
699 bounds.x2=(double) geometry.x+geometry.width-1;
700 if ((double) (geometry.y+(geometry.height+text_size)-1) > bounds.y2)
701 bounds.y2=(double) geometry.y+(geometry.height+text_size)-1;
702 value=GetImageProperty(image,"label",exception);
703 if (value != (const char *) NULL)
704 (void) WriteBlobString(image,"%%PageResources: font Helvetica\n");
705 if (LocaleCompare(image_info->magick,"PS2") != 0)
706 (void) WriteBlobString(image,"userdict begin\n");
707 start=TellBlob(image);
708 (void) FormatLocaleString(buffer,MagickPathExtent,
709 "%%%%BeginData:%13ld %s Bytes\n",0L,
710 compression == NoCompression ? "ASCII" : "Binary");
711 (void) WriteBlobString(image,buffer);
712 stop=TellBlob(image);
713 (void) WriteBlobString(image,"DisplayImage\n");
717 (void) FormatLocaleString(buffer,MagickPathExtent,
718 "%.20g %.20g\n%g %g\n%g\n",(double) geometry.x,(double) geometry.y,
719 scale.x,scale.y,pointsize);
720 (void) WriteBlobString(image,buffer);
721 labels=(char **) NULL;
722 value=GetImageProperty(image,"label",exception);
723 if (value != (const char *) NULL)
724 labels=StringToList(value);
725 if (labels != (char **) NULL)
727 for (i=0; labels[i] != (char *) NULL; i++)
729 (void) FormatLocaleString(buffer,MagickPathExtent,"%s \n",
731 (void) WriteBlobString(image,buffer);
732 labels[i]=DestroyString(labels[i]);
734 labels=(char **) RelinquishMagickMemory(labels);
736 number_pixels=(MagickSizeType) image->columns*image->rows;
737 if (number_pixels != (MagickSizeType) ((size_t) number_pixels))
738 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
739 if ((compression == FaxCompression) || (compression == Group4Compression) ||
740 ((image_info->type != TrueColorType) &&
741 (SetImageGray(image,exception) != MagickFalse)))
743 (void) FormatLocaleString(buffer,MagickPathExtent,
744 "%.20g %.20g\n1\n%d\n",(double) image->columns,(double) image->rows,
745 (int) (image->colorspace == CMYKColorspace));
746 (void) WriteBlobString(image,buffer);
747 (void) FormatLocaleString(buffer,MagickPathExtent,"%d\n",(int)
748 ((compression != FaxCompression) &&
749 (compression != Group4Compression)));
750 (void) WriteBlobString(image,buffer);
751 (void) WriteBlobString(image,"0\n");
752 (void) FormatLocaleString(buffer,MagickPathExtent,"%d\n",
753 (compression == FaxCompression) ||
754 (compression == Group4Compression) ? 1 : 8);
755 (void) WriteBlobString(image,buffer);
759 case Group4Compression:
761 if (LocaleCompare(CCITTParam,"0") == 0)
763 (void) HuffmanEncodeImage(image_info,image,image,exception);
766 (void) Huffman2DEncodeImage(image_info,image,image,exception);
769 case JPEGCompression:
771 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
772 if (status == MagickFalse)
774 (void) CloseBlob(image);
785 register unsigned char
789 Allocate pixel array.
791 length=(size_t) number_pixels;
792 pixel_info=AcquireVirtualMemory(length,sizeof(*pixels));
793 if (pixel_info == (MemoryInfo *) NULL)
794 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
795 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
797 Dump runlength encoded pixels.
800 for (y=0; y < (ssize_t) image->rows; y++)
802 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
803 if (p == (const Quantum *) NULL)
805 for (x=0; x < (ssize_t) image->columns; x++)
807 *q++=ScaleQuantumToChar(ClampToQuantum(GetPixelLuma(image,p)));
808 p+=GetPixelChannels(image);
810 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
812 if (progress == MagickFalse)
815 length=(size_t) (q-pixels);
816 if (compression == LZWCompression)
817 status=LZWEncodeImage(image,length,pixels,exception);
819 status=PackbitsEncodeImage(image,length,pixels,exception);
820 pixel_info=RelinquishVirtualMemory(pixel_info);
821 if (status == MagickFalse)
823 (void) CloseBlob(image);
831 Dump uncompressed PseudoColor packets.
833 Ascii85Initialize(image);
834 for (y=0; y < (ssize_t) image->rows; y++)
836 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
837 if (p == (const Quantum *) NULL)
839 for (x=0; x < (ssize_t) image->columns; x++)
841 Ascii85Encode(image,ScaleQuantumToChar(ClampToQuantum(
842 GetPixelLuma(image,p))));
843 p+=GetPixelChannels(image);
845 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
847 if (progress == MagickFalse)
856 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
857 (compression == JPEGCompression) || (image->alpha_trait != UndefinedPixelTrait))
859 (void) FormatLocaleString(buffer,MagickPathExtent,
860 "%.20g %.20g\n0\n%d\n",(double) image->columns,(double) image->rows,
861 (int) (image->colorspace == CMYKColorspace));
862 (void) WriteBlobString(image,buffer);
863 (void) FormatLocaleString(buffer,MagickPathExtent,"%d\n",
864 (int) (compression == NoCompression));
865 (void) WriteBlobString(image,buffer);
868 case JPEGCompression:
870 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
871 if (status == MagickFalse)
873 (void) CloseBlob(image);
884 register unsigned char
888 Allocate pixel array.
890 length=(size_t) number_pixels;
891 pixel_info=AcquireVirtualMemory(length,4*sizeof(*pixels));
892 if (pixel_info == (MemoryInfo *) NULL)
893 ThrowWriterException(ResourceLimitError,
894 "MemoryAllocationFailed");
895 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
897 Dump runlength encoded pixels.
900 for (y=0; y < (ssize_t) image->rows; y++)
902 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
903 if (p == (const Quantum *) NULL)
905 for (x=0; x < (ssize_t) image->columns; x++)
907 if ((image->alpha_trait != UndefinedPixelTrait) &&
908 (GetPixelAlpha(image,p) == (Quantum) TransparentAlpha))
910 *q++=ScaleQuantumToChar(QuantumRange);
911 *q++=ScaleQuantumToChar(QuantumRange);
912 *q++=ScaleQuantumToChar(QuantumRange);
915 if (image->colorspace != CMYKColorspace)
917 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
918 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
919 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
923 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
924 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
925 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
926 *q++=ScaleQuantumToChar(GetPixelBlack(image,p));
928 p+=GetPixelChannels(image);
930 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
932 if (progress == MagickFalse)
935 length=(size_t) (q-pixels);
936 if (compression == LZWCompression)
937 status=LZWEncodeImage(image,length,pixels,exception);
939 status=PackbitsEncodeImage(image,length,pixels,exception);
940 if (status == MagickFalse)
942 (void) CloseBlob(image);
945 pixel_info=RelinquishVirtualMemory(pixel_info);
951 Dump uncompressed DirectColor packets.
953 Ascii85Initialize(image);
954 for (y=0; y < (ssize_t) image->rows; y++)
956 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
957 if (p == (const Quantum *) NULL)
959 for (x=0; x < (ssize_t) image->columns; x++)
961 if ((image->alpha_trait != UndefinedPixelTrait) &&
962 (GetPixelAlpha(image,p) == (Quantum) TransparentAlpha))
964 Ascii85Encode(image,ScaleQuantumToChar((Quantum)
966 Ascii85Encode(image,ScaleQuantumToChar((Quantum)
968 Ascii85Encode(image,ScaleQuantumToChar((Quantum)
972 if (image->colorspace != CMYKColorspace)
974 Ascii85Encode(image,ScaleQuantumToChar(
975 GetPixelRed(image,p)));
976 Ascii85Encode(image,ScaleQuantumToChar(
977 GetPixelGreen(image,p)));
978 Ascii85Encode(image,ScaleQuantumToChar(
979 GetPixelBlue(image,p)));
983 Ascii85Encode(image,ScaleQuantumToChar(
984 GetPixelRed(image,p)));
985 Ascii85Encode(image,ScaleQuantumToChar(
986 GetPixelGreen(image,p)));
987 Ascii85Encode(image,ScaleQuantumToChar(
988 GetPixelBlue(image,p)));
989 Ascii85Encode(image,ScaleQuantumToChar(
990 GetPixelBlack(image,p)));
992 p+=GetPixelChannels(image);
994 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
996 if (progress == MagickFalse)
1007 Dump number of colors and colormap.
1009 (void) FormatLocaleString(buffer,MagickPathExtent,
1010 "%.20g %.20g\n1\n%d\n",(double) image->columns,(double) image->rows,
1011 (int) (image->colorspace == CMYKColorspace));
1012 (void) WriteBlobString(image,buffer);
1013 (void) FormatLocaleString(buffer,MagickPathExtent,"%d\n",
1014 (int) (compression == NoCompression));
1015 (void) WriteBlobString(image,buffer);
1016 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g\n",(double)
1018 (void) WriteBlobString(image,buffer);
1019 for (i=0; i < (ssize_t) image->colors; i++)
1021 (void) FormatLocaleString(buffer,MagickPathExtent,"%02X%02X%02X\n",
1022 ScaleQuantumToChar(image->colormap[i].red),
1023 ScaleQuantumToChar(image->colormap[i].green),
1024 ScaleQuantumToChar(image->colormap[i].blue));
1025 (void) WriteBlobString(image,buffer);
1027 switch (compression)
1029 case RLECompression:
1035 register unsigned char
1039 Allocate pixel array.
1041 length=(size_t) number_pixels;
1042 pixel_info=AcquireVirtualMemory(length,sizeof(*pixels));
1043 if (pixel_info == (MemoryInfo *) NULL)
1044 ThrowWriterException(ResourceLimitError,
1045 "MemoryAllocationFailed");
1046 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
1048 Dump runlength encoded pixels.
1051 for (y=0; y < (ssize_t) image->rows; y++)
1053 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1054 if (p == (const Quantum *) NULL)
1056 for (x=0; x < (ssize_t) image->columns; x++)
1058 *q++=(unsigned char) GetPixelIndex(image,p);
1059 p+=GetPixelChannels(image);
1061 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1063 if (progress == MagickFalse)
1066 length=(size_t) (q-pixels);
1067 if (compression == LZWCompression)
1068 status=LZWEncodeImage(image,length,pixels,exception);
1070 status=PackbitsEncodeImage(image,length,pixels,exception);
1071 pixel_info=RelinquishVirtualMemory(pixel_info);
1072 if (status == MagickFalse)
1074 (void) CloseBlob(image);
1075 return(MagickFalse);
1082 Dump uncompressed PseudoColor packets.
1084 Ascii85Initialize(image);
1085 for (y=0; y < (ssize_t) image->rows; y++)
1087 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1088 if (p == (const Quantum *) NULL)
1090 for (x=0; x < (ssize_t) image->columns; x++)
1092 Ascii85Encode(image,(unsigned char) GetPixelIndex(image,p));
1093 p+=GetPixelChannels(image);
1095 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1097 if (progress == MagickFalse)
1100 Ascii85Flush(image);
1105 (void) WriteBlobByte(image,'\n');
1106 length=(size_t) (TellBlob(image)-stop);
1107 stop=TellBlob(image);
1108 offset=SeekBlob(image,start,SEEK_SET);
1110 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
1111 (void) FormatLocaleString(buffer,MagickPathExtent,
1112 "%%%%BeginData:%13ld %s Bytes\n",(long) length,
1113 compression == NoCompression ? "ASCII" : "Binary");
1114 (void) WriteBlobString(image,buffer);
1115 offset=SeekBlob(image,stop,SEEK_SET);
1116 (void) WriteBlobString(image,"%%EndData\n");
1117 if (LocaleCompare(image_info->magick,"PS2") != 0)
1118 (void) WriteBlobString(image,"end\n");
1119 (void) WriteBlobString(image,"%%PageTrailer\n");
1120 if (GetNextImageInList(image) == (Image *) NULL)
1122 image=SyncNextImageInList(image);
1123 status=SetImageProgress(image,SaveImagesTag,scene++,imageListLength);
1124 if (status == MagickFalse)
1126 } while (image_info->adjoin != MagickFalse);
1127 (void) WriteBlobString(image,"%%Trailer\n");
1130 (void) FormatLocaleString(buffer,MagickPathExtent,
1131 "%%%%BoundingBox: %.20g %.20g %.20g %.20g\n",ceil(bounds.x1-0.5),
1132 ceil(bounds.y1-0.5),floor(bounds.x2+0.5),floor(bounds.y2+0.5));
1133 (void) WriteBlobString(image,buffer);
1134 (void) FormatLocaleString(buffer,MagickPathExtent,
1135 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,bounds.y1,
1136 bounds.x2,bounds.y2);
1137 (void) WriteBlobString(image,buffer);
1139 (void) WriteBlobString(image,"%%EOF\n");
1140 (void) CloseBlob(image);