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 *);
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,Image *image)
177 % A description of each parameter follows:
179 % o image_info: the image info.
181 % o image: the image.
185 static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
186 Image *image,Image *inject_image)
204 write_info=CloneImageInfo(image_info);
205 (void) CopyMagickString(write_info->filename,"GROUP4:",MaxTextExtent);
206 (void) CopyMagickString(write_info->magick,"GROUP4",MaxTextExtent);
207 group4_image=CloneImage(inject_image,0,0,MagickTrue,&image->exception);
208 if (group4_image == (Image *) NULL)
210 group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
212 group4_image=DestroyImage(group4_image);
213 if (group4 == (unsigned char *) NULL)
215 write_info=DestroyImageInfo(write_info);
216 if (WriteBlob(image,length,group4) != (ssize_t) length)
218 group4=(unsigned char *) RelinquishMagickMemory(group4);
222 static MagickBooleanType WritePS2Image(const ImageInfo *image_info,Image *image)
229 "%% Display a color image. The image is displayed in color on",
230 "%% Postscript viewers or printers that support color, otherwise",
231 "%% it is displayed as grayscale.",
236 " %% Display a DirectClass image.",
240 " /DeviceRGB setcolorspace",
245 " /BitsPerComponent 8",
246 " /Decode [0 1 0 1 0 1]",
247 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
249 " { /DataSource pixel_stream %s }",
250 " { /DataSource pixel_stream %s } ifelse",
254 " /DeviceCMYK setcolorspace",
259 " /BitsPerComponent 8",
260 " /Decode [1 0 1 0 1 0 1 0]",
261 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
263 " { /DataSource pixel_stream %s }",
264 " { /DataSource pixel_stream %s } ifelse",
272 " %% Display a PseudoClass image.",
275 " %% colors: number of colors in the colormap.",
277 " currentfile buffer readline pop",
278 " token pop /colors exch def pop",
282 " %% Image is grayscale.",
284 " currentfile buffer readline pop",
285 " token pop /bits exch def pop",
286 " /DeviceGray setcolorspace",
291 " /BitsPerComponent bits",
293 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
295 " { /DataSource pixel_stream %s }",
297 " /DataSource pixel_stream %s",
302 " >> /CCITTFaxDecode filter",
309 " %% colormap: red, green, blue color packets.",
311 " /colormap colors 3 mul string def",
312 " currentfile colormap readhexstring pop pop",
313 " currentfile buffer readline pop",
314 " [ /Indexed /DeviceRGB colors 1 sub colormap ] setcolorspace",
319 " /BitsPerComponent 8",
321 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
323 " { /DataSource pixel_stream %s }",
324 " { /DataSource pixel_stream %s } ifelse",
332 " %% Display a DirectClass or PseudoClass image.",
335 " %% x & y translation.",
337 " %% label pointsize.",
339 " %% image columns & rows.",
340 " %% class: 0-DirectClass or 1-PseudoClass.",
341 " %% colorspace: 0-RGB or 1-CMYK.",
342 " %% compression: 0-RLECompression or 1-NoCompression.",
343 " %% hex color packets.",
346 " /buffer 512 string def",
347 " /pixel_stream currentfile def",
349 " currentfile buffer readline pop",
350 " token pop /x exch def",
351 " token pop /y exch def pop",
353 " currentfile buffer readline pop",
354 " token pop /x exch def",
355 " token pop /y exch def pop",
356 " currentfile buffer readline pop",
357 " token pop /pointsize exch def pop",
358 " /Helvetica findfont pointsize scalefont setfont",
364 " currentfile buffer readline pop",
365 " token pop /columns exch def",
366 " token pop /rows exch def pop",
367 " currentfile buffer readline pop",
368 " token pop /class exch def pop",
369 " currentfile buffer readline pop",
370 " token pop /colorspace exch def pop",
371 " currentfile buffer readline pop",
372 " token pop /compression exch def pop",
373 " class 0 gt { PseudoClassImage } { DirectClassImage } ifelse",
378 buffer[MaxTextExtent],
380 page_geometry[MaxTextExtent],
424 register const Quantum
452 Open output image file.
454 assert(image_info != (const ImageInfo *) NULL);
455 assert(image_info->signature == MagickSignature);
456 assert(image != (Image *) NULL);
457 assert(image->signature == MagickSignature);
458 if (image->debug != MagickFalse)
459 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
460 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
461 if (status == MagickFalse)
463 compression=image->compression;
464 if (image_info->compression != UndefinedCompression)
465 compression=image_info->compression;
468 #if !defined(MAGICKCORE_JPEG_DELEGATE)
469 case JPEGCompression:
471 compression=RLECompression;
472 (void) ThrowMagickException(&image->exception,GetMagickModule(),
473 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
481 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
487 Scale relative to dots-per-inch.
489 delta.x=DefaultResolution;
490 delta.y=DefaultResolution;
491 resolution.x=image->x_resolution;
492 resolution.y=image->y_resolution;
493 if ((resolution.x == 0.0) || (resolution.y == 0.0))
495 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
496 resolution.x=geometry_info.rho;
497 resolution.y=geometry_info.sigma;
498 if ((flags & SigmaValue) == 0)
499 resolution.y=resolution.x;
501 if (image_info->density != (char *) NULL)
503 flags=ParseGeometry(image_info->density,&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->units == PixelsPerCentimeterResolution)
511 resolution.x=(size_t) (100.0*2.54*resolution.x+0.5)/100.0;
512 resolution.y=(size_t) (100.0*2.54*resolution.y+0.5)/100.0;
514 SetGeometry(image,&geometry);
515 (void) FormatLocaleString(page_geometry,MaxTextExtent,"%.20gx%.20g",
516 (double) image->columns,(double) image->rows);
517 if (image_info->page != (char *) NULL)
518 (void) CopyMagickString(page_geometry,image_info->page,MaxTextExtent);
520 if ((image->page.width != 0) && (image->page.height != 0))
521 (void) FormatLocaleString(page_geometry,MaxTextExtent,
522 "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,(double)
523 image->page.height,(double) image->page.x,(double) image->page.y);
525 if ((image->gravity != UndefinedGravity) &&
526 (LocaleCompare(image_info->magick,"PS") == 0))
527 (void) CopyMagickString(page_geometry,PSPageGeometry,MaxTextExtent);
528 (void) ConcatenateMagickString(page_geometry,">",MaxTextExtent);
529 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
530 &geometry.width,&geometry.height);
531 scale.x=(double) (geometry.width*delta.x)/resolution.x;
532 geometry.width=(size_t) floor(scale.x+0.5);
533 scale.y=(double) (geometry.height*delta.y)/resolution.y;
534 geometry.height=(size_t) floor(scale.y+0.5);
535 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
536 (void) ParseGravityGeometry(image,page_geometry,&page_info,
538 if (image->gravity != UndefinedGravity)
540 geometry.x=(-page_info.x);
541 geometry.y=(ssize_t) (media_info.height+page_info.y-image->rows);
544 if (image_info->pointsize != 0.0)
545 pointsize=image_info->pointsize;
547 value=GetImageProperty(image,"label");
548 if (value != (const char *) NULL)
549 text_size=(size_t) (MultilineCensus(value)*pointsize+12);
553 Output Postscript header.
555 if (LocaleCompare(image_info->magick,"PS2") == 0)
556 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0\n",MaxTextExtent);
558 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0 EPSF-3.0\n",
560 (void) WriteBlobString(image,buffer);
561 (void) WriteBlobString(image,"%%Creator: (ImageMagick)\n");
562 (void) FormatLocaleString(buffer,MaxTextExtent,"%%%%Title: (%s)\n",
564 (void) WriteBlobString(image,buffer);
565 timer=time((time_t *) NULL);
566 (void) FormatMagickTime(timer,MaxTextExtent,date);
567 (void) FormatLocaleString(buffer,MaxTextExtent,
568 "%%%%CreationDate: (%s)\n",date);
569 (void) WriteBlobString(image,buffer);
570 bounds.x1=(double) geometry.x;
571 bounds.y1=(double) geometry.y;
572 bounds.x2=(double) geometry.x+geometry.width;
573 bounds.y2=(double) geometry.y+geometry.height+text_size;
574 if ((image_info->adjoin != MagickFalse) &&
575 (GetNextImageInList(image) != (Image *) NULL))
576 (void) CopyMagickString(buffer,"%%BoundingBox: (atend)\n",
580 (void) FormatLocaleString(buffer,MaxTextExtent,
581 "%%%%BoundingBox: %.20g %.20g %.20g %.20g\n",ceil(bounds.x1-0.5),
582 ceil(bounds.y1-0.5),floor(bounds.x2+0.5),floor(bounds.y2+0.5));
583 (void) WriteBlobString(image,buffer);
584 (void) FormatLocaleString(buffer,MaxTextExtent,
585 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,
586 bounds.y1,bounds.x2,bounds.y2);
588 (void) WriteBlobString(image,buffer);
589 value=GetImageProperty(image,"label");
590 if (value != (const char *) NULL)
591 (void) WriteBlobString(image,
592 "%%DocumentNeededResources: font Helvetica\n");
593 (void) WriteBlobString(image,"%%LanguageLevel: 2\n");
594 if (LocaleCompare(image_info->magick,"PS2") != 0)
595 (void) WriteBlobString(image,"%%Pages: 1\n");
598 (void) WriteBlobString(image,"%%Orientation: Portrait\n");
599 (void) WriteBlobString(image,"%%PageOrder: Ascend\n");
600 if (image_info->adjoin == MagickFalse)
601 (void) CopyMagickString(buffer,"%%Pages: 1\n",MaxTextExtent);
603 (void) FormatLocaleString(buffer,MaxTextExtent,
604 "%%%%Pages: %.20g\n",(double) GetImageListLength(image));
605 (void) WriteBlobString(image,buffer);
607 (void) WriteBlobString(image,"%%EndComments\n");
608 (void) WriteBlobString(image,"\n%%BeginDefaults\n");
609 (void) WriteBlobString(image,"%%EndDefaults\n\n");
611 Output Postscript commands.
613 for (q=PostscriptProlog; *q; q++)
619 (void) FormatLocaleString(buffer,MaxTextExtent,*q,
620 "/ASCII85Decode filter");
623 case JPEGCompression:
625 (void) FormatLocaleString(buffer,MaxTextExtent,*q,
626 "/DCTDecode filter");
631 (void) FormatLocaleString(buffer,MaxTextExtent,*q,
632 "/LZWDecode filter");
636 case Group4Compression:
638 (void) FormatLocaleString(buffer,MaxTextExtent,*q," ");
643 (void) FormatLocaleString(buffer,MaxTextExtent,*q,
644 "/RunLengthDecode filter");
648 (void) WriteBlobString(image,buffer);
649 (void) WriteBlobByte(image,'\n');
651 value=GetImageProperty(image,"label");
652 if (value != (const char *) NULL)
653 for (j=(ssize_t) MultilineCensus(value)-1; j >= 0; j--)
655 (void) WriteBlobString(image," /label 512 string def\n");
656 (void) WriteBlobString(image," currentfile label readline pop\n");
657 (void) FormatLocaleString(buffer,MaxTextExtent,
658 " 0 y %g add moveto label show pop\n",j*pointsize+12);
659 (void) WriteBlobString(image,buffer);
661 for (q=PostscriptEpilog; *q; q++)
663 (void) FormatLocaleString(buffer,MaxTextExtent,"%s\n",*q);
664 (void) WriteBlobString(image,buffer);
666 if (LocaleCompare(image_info->magick,"PS2") == 0)
667 (void) WriteBlobString(image," showpage\n");
668 (void) WriteBlobString(image,"} bind def\n");
669 (void) WriteBlobString(image,"%%EndProlog\n");
671 (void) FormatLocaleString(buffer,MaxTextExtent,"%%%%Page: 1 %.20g\n",
673 (void) WriteBlobString(image,buffer);
674 (void) FormatLocaleString(buffer,MaxTextExtent,
675 "%%%%PageBoundingBox: %.20g %.20g %.20g %.20g\n",(double) geometry.x,
676 (double) geometry.y,geometry.x+(double) geometry.width,geometry.y+(double)
677 (geometry.height+text_size));
678 (void) WriteBlobString(image,buffer);
679 if ((double) geometry.x < bounds.x1)
680 bounds.x1=(double) geometry.x;
681 if ((double) geometry.y < bounds.y1)
682 bounds.y1=(double) geometry.y;
683 if ((double) (geometry.x+geometry.width-1) > bounds.x2)
684 bounds.x2=(double) geometry.x+geometry.width-1;
685 if ((double) (geometry.y+(geometry.height+text_size)-1) > bounds.y2)
686 bounds.y2=(double) geometry.y+(geometry.height+text_size)-1;
687 value=GetImageProperty(image,"label");
688 if (value != (const char *) NULL)
689 (void) WriteBlobString(image,"%%PageResources: font Times-Roman\n");
690 if (LocaleCompare(image_info->magick,"PS2") != 0)
691 (void) WriteBlobString(image,"userdict begin\n");
692 start=TellBlob(image);
693 (void) FormatLocaleString(buffer,MaxTextExtent,
694 "%%%%BeginData:%13ld %s Bytes\n",0L,
695 compression == NoCompression ? "ASCII" : "Binary");
696 (void) WriteBlobString(image,buffer);
697 stop=TellBlob(image);
698 (void) WriteBlobString(image,"DisplayImage\n");
702 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n%g %g\n%g\n",
703 (double) geometry.x,(double) geometry.y,scale.x,scale.y,pointsize);
704 (void) WriteBlobString(image,buffer);
705 labels=(char **) NULL;
706 value=GetImageProperty(image,"label");
707 if (value != (const char *) NULL)
708 labels=StringToList(value);
709 if (labels != (char **) NULL)
711 for (i=0; labels[i] != (char *) NULL; i++)
713 (void) FormatLocaleString(buffer,MaxTextExtent,"%s \n",
715 (void) WriteBlobString(image,buffer);
716 labels[i]=DestroyString(labels[i]);
718 labels=(char **) RelinquishMagickMemory(labels);
720 number_pixels=(MagickSizeType) image->columns*image->rows;
721 if (number_pixels != (MagickSizeType) ((size_t) number_pixels))
722 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
723 if ((compression == FaxCompression) || (compression == Group4Compression) ||
724 ((image_info->type != TrueColorType) &&
725 (IsImageGray(image,&image->exception) != MagickFalse)))
727 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n1\n%d\n",
728 (double) image->columns,(double) image->rows,(int)
729 (image->colorspace == CMYKColorspace));
730 (void) WriteBlobString(image,buffer);
731 (void) FormatLocaleString(buffer,MaxTextExtent,"%d\n",
732 (int) ((compression != FaxCompression) &&
733 (compression != Group4Compression)));
734 (void) WriteBlobString(image,buffer);
735 (void) WriteBlobString(image,"0\n");
736 (void) FormatLocaleString(buffer,MaxTextExtent,"%d\n",
737 (compression == FaxCompression) ||
738 (compression == Group4Compression) ? 1 : 8);
739 (void) WriteBlobString(image,buffer);
743 case Group4Compression:
745 if (LocaleCompare(CCITTParam,"0") == 0)
747 (void) HuffmanEncodeImage(image_info,image,image);
750 (void) Huffman2DEncodeImage(image_info,image,image);
753 case JPEGCompression:
755 status=InjectImageBlob(image_info,image,image,"jpeg",
757 if (status == MagickFalse)
758 ThrowWriterException(CoderError,image->exception.reason);
764 register unsigned char
768 Allocate pixel array.
770 length=(size_t) number_pixels;
771 pixels=(unsigned char *) AcquireQuantumMemory(length,
773 if (pixels == (unsigned char *) NULL)
774 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
776 Dump Runlength encoded pixels.
779 for (y=0; y < (ssize_t) image->rows; y++)
781 p=GetVirtualPixels(image,0,y,image->columns,1,
783 if (p == (const Quantum *) NULL)
785 for (x=0; x < (ssize_t) image->columns; x++)
787 *q++=ScaleQuantumToChar(GetPixelIntensity(image,p));
788 p+=GetPixelChannels(image);
790 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
792 if (progress == MagickFalse)
795 length=(size_t) (q-pixels);
796 if (compression == LZWCompression)
797 status=LZWEncodeImage(image,length,pixels);
799 status=PackbitsEncodeImage(image,length,pixels);
800 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
801 if (status == MagickFalse)
803 (void) CloseBlob(image);
811 Dump uncompressed PseudoColor packets.
813 Ascii85Initialize(image);
814 for (y=0; y < (ssize_t) image->rows; y++)
816 p=GetVirtualPixels(image,0,y,image->columns,1,
818 if (p == (const Quantum *) NULL)
820 for (x=0; x < (ssize_t) image->columns; x++)
822 Ascii85Encode(image,ScaleQuantumToChar(
823 GetPixelIntensity(image,p)));
824 p+=GetPixelChannels(image);
826 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
828 if (progress == MagickFalse)
837 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
838 (compression == JPEGCompression) || (image->matte != MagickFalse))
840 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n0\n%d\n",
841 (double) image->columns,(double) image->rows,(int)
842 (image->colorspace == CMYKColorspace));
843 (void) WriteBlobString(image,buffer);
844 (void) FormatLocaleString(buffer,MaxTextExtent,"%d\n",
845 (int) (compression == NoCompression));
846 (void) WriteBlobString(image,buffer);
849 case JPEGCompression:
851 status=InjectImageBlob(image_info,image,image,"jpeg",
853 if (status == MagickFalse)
854 ThrowWriterException(CoderError,image->exception.reason);
860 register unsigned char
864 Allocate pixel array.
866 length=(size_t) number_pixels;
867 pixels=(unsigned char *) AcquireQuantumMemory(length,
869 if (pixels == (unsigned char *) NULL)
870 ThrowWriterException(ResourceLimitError,
871 "MemoryAllocationFailed");
873 Dump Packbit encoded pixels.
876 for (y=0; y < (ssize_t) image->rows; y++)
878 p=GetVirtualPixels(image,0,y,image->columns,1,
880 if (p == (const Quantum *) NULL)
882 for (x=0; x < (ssize_t) image->columns; x++)
884 if ((image->matte != MagickFalse) &&
885 (GetPixelAlpha(image,p) == (Quantum) TransparentAlpha))
887 *q++=ScaleQuantumToChar((Quantum) QuantumRange);
888 *q++=ScaleQuantumToChar((Quantum) QuantumRange);
889 *q++=ScaleQuantumToChar((Quantum) QuantumRange);
892 if (image->colorspace != CMYKColorspace)
894 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
895 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
896 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
900 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
901 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
902 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
903 *q++=ScaleQuantumToChar(GetPixelBlack(image,p));
905 p+=GetPixelChannels(image);
907 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
909 if (progress == MagickFalse)
912 length=(size_t) (q-pixels);
913 if (compression == LZWCompression)
914 status=LZWEncodeImage(image,length,pixels);
916 status=PackbitsEncodeImage(image,length,pixels);
917 if (status == MagickFalse)
919 (void) CloseBlob(image);
922 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
928 Dump uncompressed DirectColor packets.
930 Ascii85Initialize(image);
931 for (y=0; y < (ssize_t) image->rows; y++)
933 p=GetVirtualPixels(image,0,y,image->columns,1,
935 if (p == (const Quantum *) NULL)
937 for (x=0; x < (ssize_t) image->columns; x++)
939 if ((image->matte != MagickFalse) &&
940 (GetPixelAlpha(image,p) == (Quantum) TransparentAlpha))
942 Ascii85Encode(image,ScaleQuantumToChar((Quantum)
944 Ascii85Encode(image,ScaleQuantumToChar((Quantum)
946 Ascii85Encode(image,ScaleQuantumToChar((Quantum)
950 if (image->colorspace != CMYKColorspace)
952 Ascii85Encode(image,ScaleQuantumToChar(
953 GetPixelRed(image,p)));
954 Ascii85Encode(image,ScaleQuantumToChar(
955 GetPixelGreen(image,p)));
956 Ascii85Encode(image,ScaleQuantumToChar(
957 GetPixelBlue(image,p)));
961 Ascii85Encode(image,ScaleQuantumToChar(
962 GetPixelRed(image,p)));
963 Ascii85Encode(image,ScaleQuantumToChar(
964 GetPixelGreen(image,p)));
965 Ascii85Encode(image,ScaleQuantumToChar(
966 GetPixelBlue(image,p)));
967 Ascii85Encode(image,ScaleQuantumToChar(
968 GetPixelBlack(image,p)));
970 p+=GetPixelChannels(image);
972 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
974 if (progress == MagickFalse)
985 Dump number of colors and colormap.
987 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n1\n%d\n",
988 (double) image->columns,(double) image->rows,(int)
989 (image->colorspace == CMYKColorspace));
990 (void) WriteBlobString(image,buffer);
991 (void) FormatLocaleString(buffer,MaxTextExtent,"%d\n",
992 (int) (compression == NoCompression));
993 (void) WriteBlobString(image,buffer);
994 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
996 (void) WriteBlobString(image,buffer);
997 for (i=0; i < (ssize_t) image->colors; i++)
999 (void) FormatLocaleString(buffer,MaxTextExtent,"%02X%02X%02X\n",
1000 ScaleQuantumToChar(image->colormap[i].red),
1001 ScaleQuantumToChar(image->colormap[i].green),
1002 ScaleQuantumToChar(image->colormap[i].blue));
1003 (void) WriteBlobString(image,buffer);
1005 switch (compression)
1007 case RLECompression:
1010 register unsigned char
1014 Allocate pixel array.
1016 length=(size_t) number_pixels;
1017 pixels=(unsigned char *) AcquireQuantumMemory(length,
1019 if (pixels == (unsigned char *) NULL)
1020 ThrowWriterException(ResourceLimitError,
1021 "MemoryAllocationFailed");
1023 Dump Runlength encoded pixels.
1026 for (y=0; y < (ssize_t) image->rows; y++)
1028 p=GetVirtualPixels(image,0,y,image->columns,1,
1030 if (p == (const Quantum *) NULL)
1032 for (x=0; x < (ssize_t) image->columns; x++)
1034 *q++=(unsigned char) GetPixelIndex(image,p);
1035 p+=GetPixelChannels(image);
1037 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1039 if (progress == MagickFalse)
1042 length=(size_t) (q-pixels);
1043 if (compression == LZWCompression)
1044 status=LZWEncodeImage(image,length,pixels);
1046 status=PackbitsEncodeImage(image,length,pixels);
1047 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1048 if (status == MagickFalse)
1050 (void) CloseBlob(image);
1051 return(MagickFalse);
1058 Dump uncompressed PseudoColor packets.
1060 Ascii85Initialize(image);
1061 for (y=0; y < (ssize_t) image->rows; y++)
1063 p=GetVirtualPixels(image,0,y,image->columns,1,
1065 if (p == (const Quantum *) NULL)
1067 for (x=0; x < (ssize_t) image->columns; x++)
1069 Ascii85Encode(image,(unsigned char) GetPixelIndex(image,p));
1070 p+=GetPixelChannels(image);
1072 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1074 if (progress == MagickFalse)
1077 Ascii85Flush(image);
1082 (void) WriteBlobByte(image,'\n');
1083 length=(size_t) (TellBlob(image)-stop);
1084 stop=TellBlob(image);
1085 offset=SeekBlob(image,start,SEEK_SET);
1087 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
1088 (void) FormatLocaleString(buffer,MaxTextExtent,
1089 "%%%%BeginData:%13ld %s Bytes\n",(long) length,
1090 compression == NoCompression ? "ASCII" : "Binary");
1091 (void) WriteBlobString(image,buffer);
1092 offset=SeekBlob(image,stop,SEEK_SET);
1093 (void) WriteBlobString(image,"%%EndData\n");
1094 if (LocaleCompare(image_info->magick,"PS2") != 0)
1095 (void) WriteBlobString(image,"end\n");
1096 (void) WriteBlobString(image,"%%PageTrailer\n");
1097 if (GetNextImageInList(image) == (Image *) NULL)
1099 image=SyncNextImageInList(image);
1100 status=SetImageProgress(image,SaveImagesTag,scene++,
1101 GetImageListLength(image));
1102 if (status == MagickFalse)
1104 } while (image_info->adjoin != MagickFalse);
1105 (void) WriteBlobString(image,"%%Trailer\n");
1108 (void) FormatLocaleString(buffer,MaxTextExtent,
1109 "%%%%BoundingBox: %.20g %.20g %.20g %.20g\n",ceil(bounds.x1-0.5),
1110 ceil(bounds.y1-0.5),floor(bounds.x2+0.5),floor(bounds.y2+0.5));
1111 (void) WriteBlobString(image,buffer);
1112 (void) FormatLocaleString(buffer,MaxTextExtent,
1113 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,bounds.y1,
1114 bounds.x2,bounds.y2);
1115 (void) WriteBlobString(image,buffer);
1117 (void) WriteBlobString(image,"%%EOF\n");
1118 (void) CloseBlob(image);