2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Write Postscript Level II Format %
20 % Copyright 1999-2012 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",
383 buffer[MaxTextExtent],
385 page_geometry[MaxTextExtent],
429 register const Quantum
457 Open output image file.
459 assert(image_info != (const ImageInfo *) NULL);
460 assert(image_info->signature == MagickSignature);
461 assert(image != (Image *) NULL);
462 assert(image->signature == MagickSignature);
463 if (image->debug != MagickFalse)
464 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
465 assert(exception != (ExceptionInfo *) NULL);
466 assert(exception->signature == MagickSignature);
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) ResetMagickMemory(&bounds,0,sizeof(bounds));
494 Scale relative to dots-per-inch.
496 delta.x=DefaultResolution;
497 delta.y=DefaultResolution;
498 resolution.x=image->resolution.x;
499 resolution.y=image->resolution.y;
500 if ((resolution.x == 0.0) || (resolution.y == 0.0))
502 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
503 resolution.x=geometry_info.rho;
504 resolution.y=geometry_info.sigma;
505 if ((flags & SigmaValue) == 0)
506 resolution.y=resolution.x;
508 if (image_info->density != (char *) NULL)
510 flags=ParseGeometry(image_info->density,&geometry_info);
511 resolution.x=geometry_info.rho;
512 resolution.y=geometry_info.sigma;
513 if ((flags & SigmaValue) == 0)
514 resolution.y=resolution.x;
516 if (image->units == PixelsPerCentimeterResolution)
518 resolution.x=(size_t) (100.0*2.54*resolution.x+0.5)/100.0;
519 resolution.y=(size_t) (100.0*2.54*resolution.y+0.5)/100.0;
521 SetGeometry(image,&geometry);
522 (void) FormatLocaleString(page_geometry,MaxTextExtent,"%.20gx%.20g",
523 (double) image->columns,(double) image->rows);
524 if (image_info->page != (char *) NULL)
525 (void) CopyMagickString(page_geometry,image_info->page,MaxTextExtent);
527 if ((image->page.width != 0) && (image->page.height != 0))
528 (void) FormatLocaleString(page_geometry,MaxTextExtent,
529 "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,(double)
530 image->page.height,(double) image->page.x,(double) image->page.y);
532 if ((image->gravity != UndefinedGravity) &&
533 (LocaleCompare(image_info->magick,"PS") == 0))
534 (void) CopyMagickString(page_geometry,PSPageGeometry,MaxTextExtent);
535 (void) ConcatenateMagickString(page_geometry,">",MaxTextExtent);
536 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
537 &geometry.width,&geometry.height);
538 scale.x=(double) (geometry.width*delta.x)/resolution.x;
539 geometry.width=(size_t) floor(scale.x+0.5);
540 scale.y=(double) (geometry.height*delta.y)/resolution.y;
541 geometry.height=(size_t) floor(scale.y+0.5);
542 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
543 (void) ParseGravityGeometry(image,page_geometry,&page_info,exception);
544 if (image->gravity != UndefinedGravity)
546 geometry.x=(-page_info.x);
547 geometry.y=(ssize_t) (media_info.height+page_info.y-image->rows);
550 if (image_info->pointsize != 0.0)
551 pointsize=image_info->pointsize;
553 value=GetImageProperty(image,"label",exception);
554 if (value != (const char *) NULL)
555 text_size=(size_t) (MultilineCensus(value)*pointsize+12);
559 Output Postscript header.
561 if (LocaleCompare(image_info->magick,"PS2") == 0)
562 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0\n",MaxTextExtent);
564 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0 EPSF-3.0\n",
566 (void) WriteBlobString(image,buffer);
567 (void) WriteBlobString(image,"%%Creator: (ImageMagick)\n");
568 (void) FormatLocaleString(buffer,MaxTextExtent,"%%%%Title: (%s)\n",
570 (void) WriteBlobString(image,buffer);
571 timer=time((time_t *) NULL);
572 (void) FormatMagickTime(timer,MaxTextExtent,date);
573 (void) FormatLocaleString(buffer,MaxTextExtent,
574 "%%%%CreationDate: (%s)\n",date);
575 (void) WriteBlobString(image,buffer);
576 bounds.x1=(double) geometry.x;
577 bounds.y1=(double) geometry.y;
578 bounds.x2=(double) geometry.x+geometry.width;
579 bounds.y2=(double) geometry.y+geometry.height+text_size;
580 if ((image_info->adjoin != MagickFalse) &&
581 (GetNextImageInList(image) != (Image *) NULL))
582 (void) CopyMagickString(buffer,"%%BoundingBox: (atend)\n",
586 (void) FormatLocaleString(buffer,MaxTextExtent,
587 "%%%%BoundingBox: %.20g %.20g %.20g %.20g\n",ceil(bounds.x1-0.5),
588 ceil(bounds.y1-0.5),floor(bounds.x2+0.5),floor(bounds.y2+0.5));
589 (void) WriteBlobString(image,buffer);
590 (void) FormatLocaleString(buffer,MaxTextExtent,
591 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,
592 bounds.y1,bounds.x2,bounds.y2);
594 (void) WriteBlobString(image,buffer);
595 value=GetImageProperty(image,"label",exception);
596 if (value != (const char *) NULL)
597 (void) WriteBlobString(image,
598 "%%DocumentNeededResources: font Helvetica\n");
599 (void) WriteBlobString(image,"%%LanguageLevel: 2\n");
600 if (LocaleCompare(image_info->magick,"PS2") != 0)
601 (void) WriteBlobString(image,"%%Pages: 1\n");
604 (void) WriteBlobString(image,"%%Orientation: Portrait\n");
605 (void) WriteBlobString(image,"%%PageOrder: Ascend\n");
606 if (image_info->adjoin == MagickFalse)
607 (void) CopyMagickString(buffer,"%%Pages: 1\n",MaxTextExtent);
609 (void) FormatLocaleString(buffer,MaxTextExtent,
610 "%%%%Pages: %.20g\n",(double) GetImageListLength(image));
611 (void) WriteBlobString(image,buffer);
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,MaxTextExtent,*q,
626 "/ASCII85Decode filter");
629 case JPEGCompression:
631 (void) FormatLocaleString(buffer,MaxTextExtent,*q,
632 "/DCTDecode filter");
637 (void) FormatLocaleString(buffer,MaxTextExtent,*q,
638 "/LZWDecode filter");
642 case Group4Compression:
644 (void) FormatLocaleString(buffer,MaxTextExtent,*q," ");
649 (void) FormatLocaleString(buffer,MaxTextExtent,*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)
659 for (j=(ssize_t) MultilineCensus(value)-1; j >= 0; j--)
661 (void) WriteBlobString(image," /label 512 string def\n");
662 (void) WriteBlobString(image," currentfile label readline pop\n");
663 (void) FormatLocaleString(buffer,MaxTextExtent,
664 " 0 y %g add moveto label show pop\n",j*pointsize+12);
665 (void) WriteBlobString(image,buffer);
667 for (q=PostscriptEpilog; *q; q++)
669 (void) FormatLocaleString(buffer,MaxTextExtent,"%s\n",*q);
670 (void) WriteBlobString(image,buffer);
672 if (LocaleCompare(image_info->magick,"PS2") == 0)
673 (void) WriteBlobString(image," showpage\n");
674 (void) WriteBlobString(image,"} bind def\n");
675 (void) WriteBlobString(image,"%%EndProlog\n");
677 (void) FormatLocaleString(buffer,MaxTextExtent,"%%%%Page: 1 %.20g\n",
679 (void) WriteBlobString(image,buffer);
680 (void) FormatLocaleString(buffer,MaxTextExtent,
681 "%%%%PageBoundingBox: %.20g %.20g %.20g %.20g\n",(double) geometry.x,
682 (double) geometry.y,geometry.x+(double) geometry.width,geometry.y+(double)
683 (geometry.height+text_size));
684 (void) WriteBlobString(image,buffer);
685 if ((double) geometry.x < bounds.x1)
686 bounds.x1=(double) geometry.x;
687 if ((double) geometry.y < bounds.y1)
688 bounds.y1=(double) geometry.y;
689 if ((double) (geometry.x+geometry.width-1) > bounds.x2)
690 bounds.x2=(double) geometry.x+geometry.width-1;
691 if ((double) (geometry.y+(geometry.height+text_size)-1) > bounds.y2)
692 bounds.y2=(double) geometry.y+(geometry.height+text_size)-1;
693 value=GetImageProperty(image,"label",exception);
694 if (value != (const char *) NULL)
695 (void) WriteBlobString(image,"%%PageResources: font Times-Roman\n");
696 if (LocaleCompare(image_info->magick,"PS2") != 0)
697 (void) WriteBlobString(image,"userdict begin\n");
698 start=TellBlob(image);
699 (void) FormatLocaleString(buffer,MaxTextExtent,
700 "%%%%BeginData:%13ld %s Bytes\n",0L,
701 compression == NoCompression ? "ASCII" : "Binary");
702 (void) WriteBlobString(image,buffer);
703 stop=TellBlob(image);
704 (void) WriteBlobString(image,"DisplayImage\n");
708 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n%g %g\n%g\n",
709 (double) geometry.x,(double) geometry.y,scale.x,scale.y,pointsize);
710 (void) WriteBlobString(image,buffer);
711 labels=(char **) NULL;
712 value=GetImageProperty(image,"label",exception);
713 if (value != (const char *) NULL)
714 labels=StringToList(value);
715 if (labels != (char **) NULL)
717 for (i=0; labels[i] != (char *) NULL; i++)
719 (void) FormatLocaleString(buffer,MaxTextExtent,"%s \n",
721 (void) WriteBlobString(image,buffer);
722 labels[i]=DestroyString(labels[i]);
724 labels=(char **) RelinquishMagickMemory(labels);
726 number_pixels=(MagickSizeType) image->columns*image->rows;
727 if (number_pixels != (MagickSizeType) ((size_t) number_pixels))
728 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
729 if ((compression == FaxCompression) || (compression == Group4Compression) ||
730 ((image_info->type != TrueColorType) &&
731 (IsImageGray(image,exception) != MagickFalse)))
733 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n1\n%d\n",
734 (double) image->columns,(double) image->rows,(int)
735 (image->colorspace == CMYKColorspace));
736 (void) WriteBlobString(image,buffer);
737 (void) FormatLocaleString(buffer,MaxTextExtent,"%d\n",
738 (int) ((compression != FaxCompression) &&
739 (compression != Group4Compression)));
740 (void) WriteBlobString(image,buffer);
741 (void) WriteBlobString(image,"0\n");
742 (void) FormatLocaleString(buffer,MaxTextExtent,"%d\n",
743 (compression == FaxCompression) ||
744 (compression == Group4Compression) ? 1 : 8);
745 (void) WriteBlobString(image,buffer);
749 case Group4Compression:
751 if (LocaleCompare(CCITTParam,"0") == 0)
753 (void) HuffmanEncodeImage(image_info,image,image,exception);
756 (void) Huffman2DEncodeImage(image_info,image,image,exception);
759 case JPEGCompression:
761 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
762 if (status == MagickFalse)
764 (void) CloseBlob(image);
772 register unsigned char
776 Allocate pixel array.
778 length=(size_t) number_pixels;
779 pixels=(unsigned char *) AcquireQuantumMemory(length,
781 if (pixels == (unsigned char *) NULL)
782 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
784 Dump Runlength encoded pixels.
787 for (y=0; y < (ssize_t) image->rows; y++)
789 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
790 if (p == (const Quantum *) NULL)
792 for (x=0; x < (ssize_t) image->columns; x++)
794 *q++=ScaleQuantumToChar(GetPixelIntensity(image,p));
795 p+=GetPixelChannels(image);
797 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
799 if (progress == MagickFalse)
802 length=(size_t) (q-pixels);
803 if (compression == LZWCompression)
804 status=LZWEncodeImage(image,length,pixels,exception);
806 status=PackbitsEncodeImage(image,length,pixels,exception);
807 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
808 if (status == MagickFalse)
810 (void) CloseBlob(image);
818 Dump uncompressed PseudoColor packets.
820 Ascii85Initialize(image);
821 for (y=0; y < (ssize_t) image->rows; y++)
823 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
824 if (p == (const Quantum *) NULL)
826 for (x=0; x < (ssize_t) image->columns; x++)
828 Ascii85Encode(image,ScaleQuantumToChar(
829 GetPixelIntensity(image,p)));
830 p+=GetPixelChannels(image);
832 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
834 if (progress == MagickFalse)
843 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
844 (compression == JPEGCompression) || (image->matte != MagickFalse))
846 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n0\n%d\n",
847 (double) image->columns,(double) image->rows,(int)
848 (image->colorspace == CMYKColorspace));
849 (void) WriteBlobString(image,buffer);
850 (void) FormatLocaleString(buffer,MaxTextExtent,"%d\n",
851 (int) (compression == NoCompression));
852 (void) WriteBlobString(image,buffer);
855 case JPEGCompression:
857 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
858 if (status == MagickFalse)
860 (void) CloseBlob(image);
868 register unsigned char
872 Allocate pixel array.
874 length=(size_t) number_pixels;
875 pixels=(unsigned char *) AcquireQuantumMemory(length,
877 if (pixels == (unsigned char *) NULL)
878 ThrowWriterException(ResourceLimitError,
879 "MemoryAllocationFailed");
881 Dump Packbit encoded pixels.
884 for (y=0; y < (ssize_t) image->rows; y++)
886 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
887 if (p == (const Quantum *) NULL)
889 for (x=0; x < (ssize_t) image->columns; x++)
891 if ((image->matte != MagickFalse) &&
892 (GetPixelAlpha(image,p) == (Quantum) TransparentAlpha))
894 *q++=ScaleQuantumToChar((Quantum) QuantumRange);
895 *q++=ScaleQuantumToChar((Quantum) QuantumRange);
896 *q++=ScaleQuantumToChar((Quantum) QuantumRange);
899 if (image->colorspace != CMYKColorspace)
901 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
902 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
903 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
907 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
908 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
909 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
910 *q++=ScaleQuantumToChar(GetPixelBlack(image,p));
912 p+=GetPixelChannels(image);
914 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
916 if (progress == MagickFalse)
919 length=(size_t) (q-pixels);
920 if (compression == LZWCompression)
921 status=LZWEncodeImage(image,length,pixels,exception);
923 status=PackbitsEncodeImage(image,length,pixels,exception);
924 if (status == MagickFalse)
926 (void) CloseBlob(image);
929 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
935 Dump uncompressed DirectColor packets.
937 Ascii85Initialize(image);
938 for (y=0; y < (ssize_t) image->rows; y++)
940 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
941 if (p == (const Quantum *) NULL)
943 for (x=0; x < (ssize_t) image->columns; x++)
945 if ((image->matte != MagickFalse) &&
946 (GetPixelAlpha(image,p) == (Quantum) TransparentAlpha))
948 Ascii85Encode(image,ScaleQuantumToChar((Quantum)
950 Ascii85Encode(image,ScaleQuantumToChar((Quantum)
952 Ascii85Encode(image,ScaleQuantumToChar((Quantum)
956 if (image->colorspace != CMYKColorspace)
958 Ascii85Encode(image,ScaleQuantumToChar(
959 GetPixelRed(image,p)));
960 Ascii85Encode(image,ScaleQuantumToChar(
961 GetPixelGreen(image,p)));
962 Ascii85Encode(image,ScaleQuantumToChar(
963 GetPixelBlue(image,p)));
967 Ascii85Encode(image,ScaleQuantumToChar(
968 GetPixelRed(image,p)));
969 Ascii85Encode(image,ScaleQuantumToChar(
970 GetPixelGreen(image,p)));
971 Ascii85Encode(image,ScaleQuantumToChar(
972 GetPixelBlue(image,p)));
973 Ascii85Encode(image,ScaleQuantumToChar(
974 GetPixelBlack(image,p)));
976 p+=GetPixelChannels(image);
978 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
980 if (progress == MagickFalse)
991 Dump number of colors and colormap.
993 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n1\n%d\n",
994 (double) image->columns,(double) image->rows,(int)
995 (image->colorspace == CMYKColorspace));
996 (void) WriteBlobString(image,buffer);
997 (void) FormatLocaleString(buffer,MaxTextExtent,"%d\n",
998 (int) (compression == NoCompression));
999 (void) WriteBlobString(image,buffer);
1000 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
1002 (void) WriteBlobString(image,buffer);
1003 for (i=0; i < (ssize_t) image->colors; i++)
1005 (void) FormatLocaleString(buffer,MaxTextExtent,"%02X%02X%02X\n",
1006 ScaleQuantumToChar(image->colormap[i].red),
1007 ScaleQuantumToChar(image->colormap[i].green),
1008 ScaleQuantumToChar(image->colormap[i].blue));
1009 (void) WriteBlobString(image,buffer);
1011 switch (compression)
1013 case RLECompression:
1016 register unsigned char
1020 Allocate pixel array.
1022 length=(size_t) number_pixels;
1023 pixels=(unsigned char *) AcquireQuantumMemory(length,
1025 if (pixels == (unsigned char *) NULL)
1026 ThrowWriterException(ResourceLimitError,
1027 "MemoryAllocationFailed");
1029 Dump Runlength encoded pixels.
1032 for (y=0; y < (ssize_t) image->rows; y++)
1034 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1035 if (p == (const Quantum *) NULL)
1037 for (x=0; x < (ssize_t) image->columns; x++)
1039 *q++=(unsigned char) GetPixelIndex(image,p);
1040 p+=GetPixelChannels(image);
1042 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1044 if (progress == MagickFalse)
1047 length=(size_t) (q-pixels);
1048 if (compression == LZWCompression)
1049 status=LZWEncodeImage(image,length,pixels,exception);
1051 status=PackbitsEncodeImage(image,length,pixels,exception);
1052 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1053 if (status == MagickFalse)
1055 (void) CloseBlob(image);
1056 return(MagickFalse);
1063 Dump uncompressed PseudoColor packets.
1065 Ascii85Initialize(image);
1066 for (y=0; y < (ssize_t) image->rows; y++)
1068 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1069 if (p == (const Quantum *) NULL)
1071 for (x=0; x < (ssize_t) image->columns; x++)
1073 Ascii85Encode(image,(unsigned char) GetPixelIndex(image,p));
1074 p+=GetPixelChannels(image);
1076 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1078 if (progress == MagickFalse)
1081 Ascii85Flush(image);
1086 (void) WriteBlobByte(image,'\n');
1087 length=(size_t) (TellBlob(image)-stop);
1088 stop=TellBlob(image);
1089 offset=SeekBlob(image,start,SEEK_SET);
1091 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
1092 (void) FormatLocaleString(buffer,MaxTextExtent,
1093 "%%%%BeginData:%13ld %s Bytes\n",(long) length,
1094 compression == NoCompression ? "ASCII" : "Binary");
1095 (void) WriteBlobString(image,buffer);
1096 offset=SeekBlob(image,stop,SEEK_SET);
1097 (void) WriteBlobString(image,"%%EndData\n");
1098 if (LocaleCompare(image_info->magick,"PS2") != 0)
1099 (void) WriteBlobString(image,"end\n");
1100 (void) WriteBlobString(image,"%%PageTrailer\n");
1101 if (GetNextImageInList(image) == (Image *) NULL)
1103 image=SyncNextImageInList(image);
1104 status=SetImageProgress(image,SaveImagesTag,scene++,
1105 GetImageListLength(image));
1106 if (status == MagickFalse)
1108 } while (image_info->adjoin != MagickFalse);
1109 (void) WriteBlobString(image,"%%Trailer\n");
1112 (void) FormatLocaleString(buffer,MaxTextExtent,
1113 "%%%%BoundingBox: %.20g %.20g %.20g %.20g\n",ceil(bounds.x1-0.5),
1114 ceil(bounds.y1-0.5),floor(bounds.x2+0.5),floor(bounds.y2+0.5));
1115 (void) WriteBlobString(image,buffer);
1116 (void) FormatLocaleString(buffer,MaxTextExtent,
1117 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,bounds.y1,
1118 bounds.x2,bounds.y2);
1119 (void) WriteBlobString(image,buffer);
1121 (void) WriteBlobString(image,"%%EOF\n");
1122 (void) CloseBlob(image);