2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Write Postscript Level II Format %
20 % Copyright 1999-2011 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 "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=SetMagickInfo("EPS2");
117 entry->encoder=(EncodeImageHandler *) WritePS2Image;
118 entry->adjoin=MagickFalse;
119 entry->seekable_stream=MagickTrue;
120 entry->description=ConstantString("Level II Encapsulated PostScript");
121 entry->module=ConstantString("PS2");
122 (void) RegisterMagickInfo(entry);
123 entry=SetMagickInfo("PS2");
124 entry->encoder=(EncodeImageHandler *) WritePS2Image;
125 entry->seekable_stream=MagickTrue;
126 entry->description=ConstantString("Level II PostScript");
127 entry->module=ConstantString("PS2");
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:",MaxTextExtent);
209 (void) CopyMagickString(write_info->magick,"GROUP4",MaxTextExtent);
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)
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",
362 " /Helvetica findfont pointsize scalefont setfont",
368 " currentfile buffer readline pop",
369 " token pop /columns exch def",
370 " token pop /rows exch def pop",
371 " currentfile buffer readline pop",
372 " token pop /class exch def pop",
373 " currentfile buffer readline pop",
374 " token pop /colorspace exch def pop",
375 " currentfile buffer readline pop",
376 " token pop /compression exch def pop",
377 " class 0 gt { PseudoClassImage } { DirectClassImage } ifelse",
382 buffer[MaxTextExtent],
384 page_geometry[MaxTextExtent],
428 register const Quantum
456 Open output image file.
458 assert(image_info != (const ImageInfo *) NULL);
459 assert(image_info->signature == MagickSignature);
460 assert(image != (Image *) NULL);
461 assert(image->signature == MagickSignature);
462 if (image->debug != MagickFalse)
463 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
464 assert(exception != (ExceptionInfo *) NULL);
465 assert(exception->signature == MagickSignature);
466 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
467 if (status == MagickFalse)
469 compression=image->compression;
470 if (image_info->compression != UndefinedCompression)
471 compression=image_info->compression;
474 #if !defined(MAGICKCORE_JPEG_DELEGATE)
475 case JPEGCompression:
477 compression=RLECompression;
478 (void) ThrowMagickException(exception,GetMagickModule(),
479 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
487 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
493 Scale relative to dots-per-inch.
495 delta.x=DefaultResolution;
496 delta.y=DefaultResolution;
497 resolution.x=image->resolution.x;
498 resolution.y=image->resolution.y;
499 if ((resolution.x == 0.0) || (resolution.y == 0.0))
501 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
502 resolution.x=geometry_info.rho;
503 resolution.y=geometry_info.sigma;
504 if ((flags & SigmaValue) == 0)
505 resolution.y=resolution.x;
507 if (image_info->density != (char *) NULL)
509 flags=ParseGeometry(image_info->density,&geometry_info);
510 resolution.x=geometry_info.rho;
511 resolution.y=geometry_info.sigma;
512 if ((flags & SigmaValue) == 0)
513 resolution.y=resolution.x;
515 if (image->units == PixelsPerCentimeterResolution)
517 resolution.x=(size_t) (100.0*2.54*resolution.x+0.5)/100.0;
518 resolution.y=(size_t) (100.0*2.54*resolution.y+0.5)/100.0;
520 SetGeometry(image,&geometry);
521 (void) FormatLocaleString(page_geometry,MaxTextExtent,"%.20gx%.20g",
522 (double) image->columns,(double) image->rows);
523 if (image_info->page != (char *) NULL)
524 (void) CopyMagickString(page_geometry,image_info->page,MaxTextExtent);
526 if ((image->page.width != 0) && (image->page.height != 0))
527 (void) FormatLocaleString(page_geometry,MaxTextExtent,
528 "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,(double)
529 image->page.height,(double) image->page.x,(double) image->page.y);
531 if ((image->gravity != UndefinedGravity) &&
532 (LocaleCompare(image_info->magick,"PS") == 0))
533 (void) CopyMagickString(page_geometry,PSPageGeometry,MaxTextExtent);
534 (void) ConcatenateMagickString(page_geometry,">",MaxTextExtent);
535 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
536 &geometry.width,&geometry.height);
537 scale.x=(double) (geometry.width*delta.x)/resolution.x;
538 geometry.width=(size_t) floor(scale.x+0.5);
539 scale.y=(double) (geometry.height*delta.y)/resolution.y;
540 geometry.height=(size_t) floor(scale.y+0.5);
541 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
542 (void) ParseGravityGeometry(image,page_geometry,&page_info,exception);
543 if (image->gravity != UndefinedGravity)
545 geometry.x=(-page_info.x);
546 geometry.y=(ssize_t) (media_info.height+page_info.y-image->rows);
549 if (image_info->pointsize != 0.0)
550 pointsize=image_info->pointsize;
552 value=GetImageProperty(image,"label",exception);
553 if (value != (const char *) NULL)
554 text_size=(size_t) (MultilineCensus(value)*pointsize+12);
558 Output Postscript header.
560 if (LocaleCompare(image_info->magick,"PS2") == 0)
561 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0\n",MaxTextExtent);
563 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0 EPSF-3.0\n",
565 (void) WriteBlobString(image,buffer);
566 (void) WriteBlobString(image,"%%Creator: (ImageMagick)\n");
567 (void) FormatLocaleString(buffer,MaxTextExtent,"%%%%Title: (%s)\n",
569 (void) WriteBlobString(image,buffer);
570 timer=time((time_t *) NULL);
571 (void) FormatMagickTime(timer,MaxTextExtent,date);
572 (void) FormatLocaleString(buffer,MaxTextExtent,
573 "%%%%CreationDate: (%s)\n",date);
574 (void) WriteBlobString(image,buffer);
575 bounds.x1=(double) geometry.x;
576 bounds.y1=(double) geometry.y;
577 bounds.x2=(double) geometry.x+geometry.width;
578 bounds.y2=(double) geometry.y+geometry.height+text_size;
579 if ((image_info->adjoin != MagickFalse) &&
580 (GetNextImageInList(image) != (Image *) NULL))
581 (void) CopyMagickString(buffer,"%%BoundingBox: (atend)\n",
585 (void) FormatLocaleString(buffer,MaxTextExtent,
586 "%%%%BoundingBox: %.20g %.20g %.20g %.20g\n",ceil(bounds.x1-0.5),
587 ceil(bounds.y1-0.5),floor(bounds.x2+0.5),floor(bounds.y2+0.5));
588 (void) WriteBlobString(image,buffer);
589 (void) FormatLocaleString(buffer,MaxTextExtent,
590 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,
591 bounds.y1,bounds.x2,bounds.y2);
593 (void) WriteBlobString(image,buffer);
594 value=GetImageProperty(image,"label",exception);
595 if (value != (const char *) NULL)
596 (void) WriteBlobString(image,
597 "%%DocumentNeededResources: font Helvetica\n");
598 (void) WriteBlobString(image,"%%LanguageLevel: 2\n");
599 if (LocaleCompare(image_info->magick,"PS2") != 0)
600 (void) WriteBlobString(image,"%%Pages: 1\n");
603 (void) WriteBlobString(image,"%%Orientation: Portrait\n");
604 (void) WriteBlobString(image,"%%PageOrder: Ascend\n");
605 if (image_info->adjoin == MagickFalse)
606 (void) CopyMagickString(buffer,"%%Pages: 1\n",MaxTextExtent);
608 (void) FormatLocaleString(buffer,MaxTextExtent,
609 "%%%%Pages: %.20g\n",(double) GetImageListLength(image));
610 (void) WriteBlobString(image,buffer);
612 (void) WriteBlobString(image,"%%EndComments\n");
613 (void) WriteBlobString(image,"\n%%BeginDefaults\n");
614 (void) WriteBlobString(image,"%%EndDefaults\n\n");
616 Output Postscript commands.
618 for (q=PostscriptProlog; *q; q++)
624 (void) FormatLocaleString(buffer,MaxTextExtent,*q,
625 "/ASCII85Decode filter");
628 case JPEGCompression:
630 (void) FormatLocaleString(buffer,MaxTextExtent,*q,
631 "/DCTDecode filter");
636 (void) FormatLocaleString(buffer,MaxTextExtent,*q,
637 "/LZWDecode filter");
641 case Group4Compression:
643 (void) FormatLocaleString(buffer,MaxTextExtent,*q," ");
648 (void) FormatLocaleString(buffer,MaxTextExtent,*q,
649 "/RunLengthDecode filter");
653 (void) WriteBlobString(image,buffer);
654 (void) WriteBlobByte(image,'\n');
656 value=GetImageProperty(image,"label",exception);
657 if (value != (const char *) NULL)
658 for (j=(ssize_t) MultilineCensus(value)-1; j >= 0; j--)
660 (void) WriteBlobString(image," /label 512 string def\n");
661 (void) WriteBlobString(image," currentfile label readline pop\n");
662 (void) FormatLocaleString(buffer,MaxTextExtent,
663 " 0 y %g add moveto label show pop\n",j*pointsize+12);
664 (void) WriteBlobString(image,buffer);
666 for (q=PostscriptEpilog; *q; q++)
668 (void) FormatLocaleString(buffer,MaxTextExtent,"%s\n",*q);
669 (void) WriteBlobString(image,buffer);
671 if (LocaleCompare(image_info->magick,"PS2") == 0)
672 (void) WriteBlobString(image," showpage\n");
673 (void) WriteBlobString(image,"} bind def\n");
674 (void) WriteBlobString(image,"%%EndProlog\n");
676 (void) FormatLocaleString(buffer,MaxTextExtent,"%%%%Page: 1 %.20g\n",
678 (void) WriteBlobString(image,buffer);
679 (void) FormatLocaleString(buffer,MaxTextExtent,
680 "%%%%PageBoundingBox: %.20g %.20g %.20g %.20g\n",(double) geometry.x,
681 (double) geometry.y,geometry.x+(double) geometry.width,geometry.y+(double)
682 (geometry.height+text_size));
683 (void) WriteBlobString(image,buffer);
684 if ((double) geometry.x < bounds.x1)
685 bounds.x1=(double) geometry.x;
686 if ((double) geometry.y < bounds.y1)
687 bounds.y1=(double) geometry.y;
688 if ((double) (geometry.x+geometry.width-1) > bounds.x2)
689 bounds.x2=(double) geometry.x+geometry.width-1;
690 if ((double) (geometry.y+(geometry.height+text_size)-1) > bounds.y2)
691 bounds.y2=(double) geometry.y+(geometry.height+text_size)-1;
692 value=GetImageProperty(image,"label",exception);
693 if (value != (const char *) NULL)
694 (void) WriteBlobString(image,"%%PageResources: font Times-Roman\n");
695 if (LocaleCompare(image_info->magick,"PS2") != 0)
696 (void) WriteBlobString(image,"userdict begin\n");
697 start=TellBlob(image);
698 (void) FormatLocaleString(buffer,MaxTextExtent,
699 "%%%%BeginData:%13ld %s Bytes\n",0L,
700 compression == NoCompression ? "ASCII" : "Binary");
701 (void) WriteBlobString(image,buffer);
702 stop=TellBlob(image);
703 (void) WriteBlobString(image,"DisplayImage\n");
707 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n%g %g\n%g\n",
708 (double) geometry.x,(double) geometry.y,scale.x,scale.y,pointsize);
709 (void) WriteBlobString(image,buffer);
710 labels=(char **) NULL;
711 value=GetImageProperty(image,"label",exception);
712 if (value != (const char *) NULL)
713 labels=StringToList(value);
714 if (labels != (char **) NULL)
716 for (i=0; labels[i] != (char *) NULL; i++)
718 (void) FormatLocaleString(buffer,MaxTextExtent,"%s \n",
720 (void) WriteBlobString(image,buffer);
721 labels[i]=DestroyString(labels[i]);
723 labels=(char **) RelinquishMagickMemory(labels);
725 number_pixels=(MagickSizeType) image->columns*image->rows;
726 if (number_pixels != (MagickSizeType) ((size_t) number_pixels))
727 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
728 if ((compression == FaxCompression) || (compression == Group4Compression) ||
729 ((image_info->type != TrueColorType) &&
730 (IsImageGray(image,exception) != MagickFalse)))
732 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n1\n%d\n",
733 (double) image->columns,(double) image->rows,(int)
734 (image->colorspace == CMYKColorspace));
735 (void) WriteBlobString(image,buffer);
736 (void) FormatLocaleString(buffer,MaxTextExtent,"%d\n",
737 (int) ((compression != FaxCompression) &&
738 (compression != Group4Compression)));
739 (void) WriteBlobString(image,buffer);
740 (void) WriteBlobString(image,"0\n");
741 (void) FormatLocaleString(buffer,MaxTextExtent,"%d\n",
742 (compression == FaxCompression) ||
743 (compression == Group4Compression) ? 1 : 8);
744 (void) WriteBlobString(image,buffer);
748 case Group4Compression:
750 if (LocaleCompare(CCITTParam,"0") == 0)
752 (void) HuffmanEncodeImage(image_info,image,image,exception);
755 (void) Huffman2DEncodeImage(image_info,image,image,exception);
758 case JPEGCompression:
760 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
761 if (status == MagickFalse)
763 (void) CloseBlob(image);
771 register unsigned char
775 Allocate pixel array.
777 length=(size_t) number_pixels;
778 pixels=(unsigned char *) AcquireQuantumMemory(length,
780 if (pixels == (unsigned char *) NULL)
781 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
783 Dump Runlength encoded pixels.
786 for (y=0; y < (ssize_t) image->rows; y++)
788 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
789 if (p == (const Quantum *) NULL)
791 for (x=0; x < (ssize_t) image->columns; x++)
793 *q++=ScaleQuantumToChar(GetPixelIntensity(image,p));
794 p+=GetPixelChannels(image);
796 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
798 if (progress == MagickFalse)
801 length=(size_t) (q-pixels);
802 if (compression == LZWCompression)
803 status=LZWEncodeImage(image,length,pixels,exception);
805 status=PackbitsEncodeImage(image,length,pixels,exception);
806 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
807 if (status == MagickFalse)
809 (void) CloseBlob(image);
817 Dump uncompressed PseudoColor packets.
819 Ascii85Initialize(image);
820 for (y=0; y < (ssize_t) image->rows; y++)
822 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
823 if (p == (const Quantum *) NULL)
825 for (x=0; x < (ssize_t) image->columns; x++)
827 Ascii85Encode(image,ScaleQuantumToChar(
828 GetPixelIntensity(image,p)));
829 p+=GetPixelChannels(image);
831 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
833 if (progress == MagickFalse)
842 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
843 (compression == JPEGCompression) || (image->matte != MagickFalse))
845 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n0\n%d\n",
846 (double) image->columns,(double) image->rows,(int)
847 (image->colorspace == CMYKColorspace));
848 (void) WriteBlobString(image,buffer);
849 (void) FormatLocaleString(buffer,MaxTextExtent,"%d\n",
850 (int) (compression == NoCompression));
851 (void) WriteBlobString(image,buffer);
854 case JPEGCompression:
856 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
857 if (status == MagickFalse)
859 (void) CloseBlob(image);
867 register unsigned char
871 Allocate pixel array.
873 length=(size_t) number_pixels;
874 pixels=(unsigned char *) AcquireQuantumMemory(length,
876 if (pixels == (unsigned char *) NULL)
877 ThrowWriterException(ResourceLimitError,
878 "MemoryAllocationFailed");
880 Dump Packbit encoded pixels.
883 for (y=0; y < (ssize_t) image->rows; y++)
885 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
886 if (p == (const Quantum *) NULL)
888 for (x=0; x < (ssize_t) image->columns; x++)
890 if ((image->matte != MagickFalse) &&
891 (GetPixelAlpha(image,p) == (Quantum) TransparentAlpha))
893 *q++=ScaleQuantumToChar((Quantum) QuantumRange);
894 *q++=ScaleQuantumToChar((Quantum) QuantumRange);
895 *q++=ScaleQuantumToChar((Quantum) QuantumRange);
898 if (image->colorspace != CMYKColorspace)
900 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
901 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
902 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
906 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
907 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
908 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
909 *q++=ScaleQuantumToChar(GetPixelBlack(image,p));
911 p+=GetPixelChannels(image);
913 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
915 if (progress == MagickFalse)
918 length=(size_t) (q-pixels);
919 if (compression == LZWCompression)
920 status=LZWEncodeImage(image,length,pixels,exception);
922 status=PackbitsEncodeImage(image,length,pixels,exception);
923 if (status == MagickFalse)
925 (void) CloseBlob(image);
928 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
934 Dump uncompressed DirectColor packets.
936 Ascii85Initialize(image);
937 for (y=0; y < (ssize_t) image->rows; y++)
939 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
940 if (p == (const Quantum *) NULL)
942 for (x=0; x < (ssize_t) image->columns; x++)
944 if ((image->matte != MagickFalse) &&
945 (GetPixelAlpha(image,p) == (Quantum) TransparentAlpha))
947 Ascii85Encode(image,ScaleQuantumToChar((Quantum)
949 Ascii85Encode(image,ScaleQuantumToChar((Quantum)
951 Ascii85Encode(image,ScaleQuantumToChar((Quantum)
955 if (image->colorspace != CMYKColorspace)
957 Ascii85Encode(image,ScaleQuantumToChar(
958 GetPixelRed(image,p)));
959 Ascii85Encode(image,ScaleQuantumToChar(
960 GetPixelGreen(image,p)));
961 Ascii85Encode(image,ScaleQuantumToChar(
962 GetPixelBlue(image,p)));
966 Ascii85Encode(image,ScaleQuantumToChar(
967 GetPixelRed(image,p)));
968 Ascii85Encode(image,ScaleQuantumToChar(
969 GetPixelGreen(image,p)));
970 Ascii85Encode(image,ScaleQuantumToChar(
971 GetPixelBlue(image,p)));
972 Ascii85Encode(image,ScaleQuantumToChar(
973 GetPixelBlack(image,p)));
975 p+=GetPixelChannels(image);
977 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
979 if (progress == MagickFalse)
990 Dump number of colors and colormap.
992 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n1\n%d\n",
993 (double) image->columns,(double) image->rows,(int)
994 (image->colorspace == CMYKColorspace));
995 (void) WriteBlobString(image,buffer);
996 (void) FormatLocaleString(buffer,MaxTextExtent,"%d\n",
997 (int) (compression == NoCompression));
998 (void) WriteBlobString(image,buffer);
999 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
1001 (void) WriteBlobString(image,buffer);
1002 for (i=0; i < (ssize_t) image->colors; i++)
1004 (void) FormatLocaleString(buffer,MaxTextExtent,"%02X%02X%02X\n",
1005 ScaleQuantumToChar(image->colormap[i].red),
1006 ScaleQuantumToChar(image->colormap[i].green),
1007 ScaleQuantumToChar(image->colormap[i].blue));
1008 (void) WriteBlobString(image,buffer);
1010 switch (compression)
1012 case RLECompression:
1015 register unsigned char
1019 Allocate pixel array.
1021 length=(size_t) number_pixels;
1022 pixels=(unsigned char *) AcquireQuantumMemory(length,
1024 if (pixels == (unsigned char *) NULL)
1025 ThrowWriterException(ResourceLimitError,
1026 "MemoryAllocationFailed");
1028 Dump Runlength encoded pixels.
1031 for (y=0; y < (ssize_t) image->rows; y++)
1033 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1034 if (p == (const Quantum *) NULL)
1036 for (x=0; x < (ssize_t) image->columns; x++)
1038 *q++=(unsigned char) GetPixelIndex(image,p);
1039 p+=GetPixelChannels(image);
1041 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1043 if (progress == MagickFalse)
1046 length=(size_t) (q-pixels);
1047 if (compression == LZWCompression)
1048 status=LZWEncodeImage(image,length,pixels,exception);
1050 status=PackbitsEncodeImage(image,length,pixels,exception);
1051 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1052 if (status == MagickFalse)
1054 (void) CloseBlob(image);
1055 return(MagickFalse);
1062 Dump uncompressed PseudoColor packets.
1064 Ascii85Initialize(image);
1065 for (y=0; y < (ssize_t) image->rows; y++)
1067 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1068 if (p == (const Quantum *) NULL)
1070 for (x=0; x < (ssize_t) image->columns; x++)
1072 Ascii85Encode(image,(unsigned char) GetPixelIndex(image,p));
1073 p+=GetPixelChannels(image);
1075 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1077 if (progress == MagickFalse)
1080 Ascii85Flush(image);
1085 (void) WriteBlobByte(image,'\n');
1086 length=(size_t) (TellBlob(image)-stop);
1087 stop=TellBlob(image);
1088 offset=SeekBlob(image,start,SEEK_SET);
1090 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
1091 (void) FormatLocaleString(buffer,MaxTextExtent,
1092 "%%%%BeginData:%13ld %s Bytes\n",(long) length,
1093 compression == NoCompression ? "ASCII" : "Binary");
1094 (void) WriteBlobString(image,buffer);
1095 offset=SeekBlob(image,stop,SEEK_SET);
1096 (void) WriteBlobString(image,"%%EndData\n");
1097 if (LocaleCompare(image_info->magick,"PS2") != 0)
1098 (void) WriteBlobString(image,"end\n");
1099 (void) WriteBlobString(image,"%%PageTrailer\n");
1100 if (GetNextImageInList(image) == (Image *) NULL)
1102 image=SyncNextImageInList(image);
1103 status=SetImageProgress(image,SaveImagesTag,scene++,
1104 GetImageListLength(image));
1105 if (status == MagickFalse)
1107 } while (image_info->adjoin != MagickFalse);
1108 (void) WriteBlobString(image,"%%Trailer\n");
1111 (void) FormatLocaleString(buffer,MaxTextExtent,
1112 "%%%%BoundingBox: %.20g %.20g %.20g %.20g\n",ceil(bounds.x1-0.5),
1113 ceil(bounds.y1-0.5),floor(bounds.x2+0.5),floor(bounds.y2+0.5));
1114 (void) WriteBlobString(image,buffer);
1115 (void) FormatLocaleString(buffer,MaxTextExtent,
1116 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,bounds.y1,
1117 bounds.x2,bounds.y2);
1118 (void) WriteBlobString(image,buffer);
1120 (void) WriteBlobString(image,"%%EOF\n");
1121 (void) CloseBlob(image);