blob: a91b763340ebdccc3dd23eb34cf7582c2c646e92 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% PPPP SSSSS 33333 %
7% P P SS 33 %
8% PPPP SSS 333 %
9% P SS 33 %
10% P SSSSS 33333 %
11% %
12% %
13% Write Postscript Level III Format %
14% %
15% Software Design %
16% John Cristy %
17% Lars Ruben Skyum %
18% July 1992 %
19% %
20% %
cristy16af1cb2009-12-11 21:38:29 +000021% Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000022% dedicated to making software imaging solutions freely available. %
23% %
24% You may not use this file except in compliance with the License. You may %
25% obtain a copy of the License at %
26% %
27% http://www.imagemagick.org/script/license.php %
28% %
29% Unless required by applicable law or agreed to in writing, software %
30% distributed under the License is distributed on an "AS IS" BASIS, %
31% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
32% See the License for the specific language governing permissions and %
33% limitations under the License. %
34% %
35%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36%
37%
38*/
39
40/*
41 Include declarations.
42*/
43#include "magick/studio.h"
44#include "magick/blob.h"
45#include "magick/blob-private.h"
46#include "magick/cache.h"
47#include "magick/color.h"
48#include "magick/color-private.h"
49#include "magick/compress.h"
50#include "magick/constitute.h"
51#include "magick/draw.h"
52#include "magick/exception.h"
53#include "magick/exception-private.h"
54#include "magick/geometry.h"
55#include "magick/image.h"
56#include "magick/image-private.h"
57#include "magick/list.h"
58#include "magick/magick.h"
59#include "magick/memory_.h"
60#include "magick/monitor.h"
61#include "magick/monitor-private.h"
62#include "magick/option.h"
63#include "magick/property.h"
64#include "magick/quantum-private.h"
65#include "magick/resource_.h"
66#include "magick/static.h"
67#include "magick/string_.h"
68#include "magick/module.h"
69#include "magick/token.h"
70#include "magick/utility.h"
71#include "magick/module.h"
cristy3ed852e2009-09-05 21:47:34 +000072
73/*
74 Define declarations.
75*/
76#define PS3_NoCompression "0"
77#define PS3_FaxCompression "1"
78#define PS3_JPEGCompression "2"
79#define PS3_LZWCompression "3"
80#define PS3_RLECompression "4"
81#define PS3_ZipCompression "5"
82
83#define PS3_RGBColorspace "0"
84#define PS3_CMYKColorspace "1"
85
86#define PS3_DirectClass "0"
87#define PS3_PseudoClass "1"
cristy80975862009-09-25 14:34:31 +000088
89#if defined(MAGICKCORE_TIFF_DELEGATE)
cristy80975862009-09-25 14:34:31 +000090#define CCITTParam "-1"
91#else
92#define CCITTParam "0"
93#endif
cristy3ed852e2009-09-05 21:47:34 +000094
95/*
96 Forward declarations.
97*/
98static MagickBooleanType
99 WritePS3Image(const ImageInfo *,Image *);
100
101/*
102%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103% %
104% %
105% %
106% R e g i s t e r P S 3 I m a g e %
107% %
108% %
109% %
110%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111%
112% RegisterPS3Image() adds properties for the PS3 image format to the list of
113% supported formats. The properties include the image format tag, a method to
114% read and/or write the format, whether the format supports the saving of more
115% than one frame to the same file or blob, whether the format supports native
116% in-memory I/O, and a brief description of the format.
117%
118% The format of the RegisterPS3Image method is:
119%
120% unsigned long RegisterPS3Image(void)
121%
122*/
123ModuleExport unsigned long RegisterPS3Image(void)
124{
125 MagickInfo
126 *entry;
127
128 entry=SetMagickInfo("EPS3");
129 entry->encoder=(EncodeImageHandler *) WritePS3Image;
130 entry->description=ConstantString("Level III Encapsulated PostScript");
131 entry->module=ConstantString("PS3");
132 (void) RegisterMagickInfo(entry);
133 entry=SetMagickInfo("PS3");
134 entry->encoder=(EncodeImageHandler *) WritePS3Image;
135 entry->description=ConstantString("Level III PostScript");
136 entry->module=ConstantString("PS3");
137 (void) RegisterMagickInfo(entry);
138 return(MagickImageCoderSignature);
139}
140
141/*
142%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143% %
144% %
145% %
146% U n r e g i s t e r P S 3 I m a g e %
147% %
148% %
149% %
150%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151%
152% UnregisterPS3Image() removes format registrations made by the PS3 module
153% from the list of supported formats.
154%
155% The format of the UnregisterPS3Image method is:
156%
157% UnregisterPS3Image(void)
158%
159*/
160ModuleExport void UnregisterPS3Image(void)
161{
162 (void) UnregisterMagickInfo("EPS3");
163 (void) UnregisterMagickInfo("PS3");
164}
165
166/*
167%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
168% %
169% %
170% %
171% W r i t e P S 3 I m a g e %
172% %
173% %
174% %
175%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
176%
177% WritePS3Image() translates an image to encapsulated Postscript Level III
178% for printing. If the supplied geometry is null, the image is centered on
179% the Postscript page. Otherwise, the image is positioned as specified by the
180% geometry.
181%
182% The format of the WritePS3Image method is:
183%
184% MagickBooleanType WritePS3Image(const ImageInfo *image_info,Image *image)
185%
186% A description of each parameter follows:
187%
188% o image_info: Specifies a pointer to a ImageInfo structure.
189%
190% o image: the image.
191%
192*/
193
cristy47b838c2009-09-19 16:09:30 +0000194static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
195 Image *image,Image *inject_image)
196{
cristy47b838c2009-09-19 16:09:30 +0000197 Image
cristy80975862009-09-25 14:34:31 +0000198 *group4_image;
cristy47b838c2009-09-19 16:09:30 +0000199
200 ImageInfo
201 *write_info;
202
cristy47b838c2009-09-19 16:09:30 +0000203 MagickBooleanType
204 status;
205
cristy80975862009-09-25 14:34:31 +0000206 size_t
207 length;
cristy47b838c2009-09-19 16:09:30 +0000208
209 unsigned char
cristy80975862009-09-25 14:34:31 +0000210 *group4;
cristy47b838c2009-09-19 16:09:30 +0000211
cristy42751fe2009-10-05 00:15:50 +0000212 status=MagickTrue;
cristy47b838c2009-09-19 16:09:30 +0000213 write_info=CloneImageInfo(image_info);
cristy80975862009-09-25 14:34:31 +0000214 (void) CopyMagickString(write_info->filename,"GROUP4:",MaxTextExtent);
215 (void) CopyMagickString(write_info->magick,"GROUP4",MaxTextExtent);
216 group4_image=CloneImage(inject_image,0,0,MagickTrue,&image->exception);
217 if (group4_image == (Image *) NULL)
218 return(MagickFalse);
219 group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
220 &image->exception);
221 group4_image=DestroyImage(group4_image);
222 if (group4 == (unsigned char *) NULL)
223 return(MagickFalse);
cristy47b838c2009-09-19 16:09:30 +0000224 write_info=DestroyImageInfo(write_info);
cristy80975862009-09-25 14:34:31 +0000225 if (WriteBlob(image,length,group4) != (ssize_t) length)
226 status=MagickFalse;
227 group4=(unsigned char *) RelinquishMagickMemory(group4);
228 return(status);
cristy47b838c2009-09-19 16:09:30 +0000229}
cristy80975862009-09-25 14:34:31 +0000230
cristy47b838c2009-09-19 16:09:30 +0000231
cristy3ed852e2009-09-05 21:47:34 +0000232static MagickBooleanType SerializeImage(const ImageInfo *image_info,
233 Image *image,unsigned char **pixels,size_t *length)
234{
235 long
236 y;
237
238 MagickBooleanType
239 status;
240
241 register const IndexPacket
242 *indexes;
243
244 register const PixelPacket
245 *p;
246
247 register long
248 x;
249
250 register unsigned char
251 *q;
252
253 assert(image != (Image *) NULL);
254 assert(image->signature == MagickSignature);
255 if (image->debug != MagickFalse)
256 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
257 status=MagickTrue;
258 *length=(image->colorspace == CMYKColorspace ? 4 : 3)*
259 (size_t) image->columns*image->rows;
260 *pixels=(unsigned char *) AcquireQuantumMemory(*length,sizeof(**pixels));
261 if (*pixels == (unsigned char *) NULL)
262 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
263 q=(*pixels);
264 for (y=0; y < (long) image->rows; y++)
265 {
266 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
267 if (p == (const PixelPacket *) NULL)
268 break;
269 indexes=GetVirtualIndexQueue(image);
270 if (image->colorspace != CMYKColorspace)
271 for (x=0; x < (long) image->columns; x++)
272 {
273 *q++=ScaleQuantumToChar(p->red);
274 *q++=ScaleQuantumToChar(p->green);
275 *q++=ScaleQuantumToChar(p->blue);
276 p++;
277 }
278 else
279 for (x=0; x < (long) image->columns; x++)
280 {
281 *q++=ScaleQuantumToChar(p->red);
282 *q++=ScaleQuantumToChar(p->green);
283 *q++=ScaleQuantumToChar(p->blue);
284 *q++=ScaleQuantumToChar(indexes[x]);
285 p++;
286 }
287 if (image->previous == (Image *) NULL)
288 {
289 status=SetImageProgress(image,SaveImageTag,y,image->rows);
290 if (status == MagickFalse)
291 break;
292 }
293 }
294 if (status == MagickFalse)
295 *pixels=(unsigned char *) RelinquishMagickMemory(*pixels);
296 return(status);
297}
298
299static MagickBooleanType SerializeImageChannel(const ImageInfo *image_info,
300 Image *image,unsigned char **pixels,size_t *length)
301{
302 long
303 y;
304
305 MagickBooleanType
306 status;
307
308 register const PixelPacket
309 *p;
310
311 register long
312 x;
313
314 register unsigned char
315 *q;
316
317 unsigned char
318 code,
319 bit;
320
321 unsigned long
322 pack,
323 padded_columns;
324
325 assert(image != (Image *) NULL);
326 assert(image->signature == MagickSignature);
327 if (image->debug != MagickFalse)
328 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
329 status=MagickTrue;
330 pack=IsMonochromeImage(image,&image->exception) == MagickFalse ? 1UL : 8UL;
331 padded_columns=((image->columns+pack-1)/pack)*pack;
332 *length=(size_t) padded_columns*image->rows/pack;
333 *pixels=(unsigned char *) AcquireQuantumMemory(*length,sizeof(**pixels));
334 if (*pixels == (unsigned char *) NULL)
335 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
336 q=(*pixels);
337 for (y=0; y < (long) image->rows; y++)
338 {
339 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
340 if (p == (const PixelPacket *) NULL)
341 break;
342 if (pack == 1)
343 for (x=0; x < (long) image->columns; x++)
344 {
345 *q++=ScaleQuantumToChar(PixelIntensityToQuantum(p));
346 p++;
347 }
348 else
349 {
350 code='\0';
351 for (x=0; x < (long) padded_columns; x++)
352 {
353 bit=(unsigned char) 0x00;
354 if (x < (long) image->columns)
355 bit=(unsigned char) (PixelIntensityToQuantum(p) ==
356 (Quantum) TransparentOpacity ? 0x01 : 0x00);
357 code=(code << 1)+bit;
358 if (((x+1) % pack) == 0)
359 {
360 *q++=code;
361 code='\0';
362 }
363 p++;
364 }
365 }
366 status=SetImageProgress(image,SaveImageTag,y,image->rows);
367 if (status == MagickFalse)
368 break;
369 }
370 if (status == MagickFalse)
371 *pixels=(unsigned char *) RelinquishMagickMemory(*pixels);
372 return(status);
373}
374
375static MagickBooleanType SerializeImageIndexes(const ImageInfo *image_info,
376 Image *image,unsigned char **pixels,size_t *length)
377{
378 long
379 y;
380
381 MagickBooleanType
382 status;
383
384 register const IndexPacket
385 *indexes;
386
387 register const PixelPacket
388 *p;
389
390 register long
391 x;
392
393 register unsigned char
394 *q;
395
396 assert(image != (Image *) NULL);
397 assert(image->signature == MagickSignature);
398 if (image->debug != MagickFalse)
399 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
400 status=MagickTrue;
401 *length=(size_t) image->columns*image->rows;
402 *pixels=(unsigned char *) AcquireQuantumMemory(*length,sizeof(**pixels));
403 if (*pixels == (unsigned char *) NULL)
404 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
405 q=(*pixels);
406 for (y=0; y < (long) image->rows; y++)
407 {
408 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
409 if (p == (const PixelPacket *) NULL)
410 break;
411 indexes=GetVirtualIndexQueue(image);
412 for (x=0; x < (long) image->columns; x++)
413 *q++=(unsigned char) indexes[x];
414 if (image->previous == (Image *) NULL)
415 {
416 status=SetImageProgress(image,SaveImageTag,y,image->rows);
417 if (status == MagickFalse)
418 break;
419 }
420 }
421 if (status == MagickFalse)
422 *pixels=(unsigned char *) RelinquishMagickMemory(*pixels);
423 return(status);
424}
425
426static MagickBooleanType WritePS3MaskImage(const ImageInfo *image_info,
427 Image *image,const CompressionType compression)
428{
429 char
430 buffer[MaxTextExtent];
431
432 Image
433 *mask_image;
434
435 MagickBooleanType
436 status;
437
438 MagickOffsetType
439 offset,
440 start,
441 stop;
442
443 register long
444 i;
445
446 size_t
447 length;
448
449 unsigned char
450 *pixels;
451
452 assert(image_info != (ImageInfo *) NULL);
453 assert(image_info->signature == MagickSignature);
454 assert(image != (Image *) NULL);
455 assert(image->signature == MagickSignature);
456 if (image->debug != MagickFalse)
457 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
458 assert(image->matte != MagickFalse);
459 status=MagickTrue;
460 /*
461 Note BeginData DSC comment for update later.
462 */
463 start=TellBlob(image);
464 (void) FormatMagickString(buffer,MaxTextExtent,
465 "%%%%BeginData:%13ld %s Bytes\n",0L,
466 compression == NoCompression ? "ASCII" : "BINARY");
467 (void) WriteBlobString(image,buffer);
468 stop=TellBlob(image);
469 /*
470 Only lossless compressions for the mask.
471 */
472 switch (compression)
473 {
474 case NoCompression:
475 default:
476 {
477 (void) FormatMagickString(buffer,MaxTextExtent,
478 "currentfile %lu %lu "PS3_NoCompression" ByteStreamDecodeFilter\n",
479 image->columns,image->rows);
480 break;
481 }
482 case FaxCompression:
483 case Group4Compression:
484 {
485 (void) FormatMagickString(buffer,MaxTextExtent,
486 "currentfile %lu %lu "PS3_FaxCompression" ByteStreamDecodeFilter\n",
487 image->columns,image->rows);
488 break;
489 }
490 case LZWCompression:
491 {
492 (void) FormatMagickString(buffer,MaxTextExtent,
493 "currentfile %lu %lu "PS3_LZWCompression" ByteStreamDecodeFilter\n",
494 image->columns,image->rows);
495 break;
496 }
497 case RLECompression:
498 {
499 (void) FormatMagickString(buffer,MaxTextExtent,
500 "currentfile %lu %lu "PS3_RLECompression" ByteStreamDecodeFilter\n",
501 image->columns,image->rows);
502 break;
503 }
504 case ZipCompression:
505 {
506 (void) FormatMagickString(buffer,MaxTextExtent,
507 "currentfile %lu %lu "PS3_ZipCompression" ByteStreamDecodeFilter\n",
508 image->columns,image->rows);
509 break;
510 }
511 }
512 (void) WriteBlobString(image,buffer);
513 (void) WriteBlobString(image,"/ReusableStreamDecode filter\n");
514 mask_image=CloneImage(image,0,0,MagickTrue,&image->exception);
515 if (mask_image == (Image *) NULL)
516 ThrowWriterException(CoderError,image->exception.reason);
517 status=SeparateImageChannel(mask_image,OpacityChannel);
518 if (status == MagickFalse)
519 {
520 mask_image=DestroyImage(mask_image);
521 return(MagickFalse);
522 }
523 (void) SetImageType(mask_image,BilevelType);
524 (void) SetImageType(mask_image,PaletteType);
525 mask_image->matte=MagickFalse;
526 pixels=(unsigned char *) NULL;
527 length=0;
528 switch (compression)
529 {
530 case NoCompression:
531 default:
532 {
533 status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
534 if (status == MagickFalse)
535 break;
536 Ascii85Initialize(image);
537 for (i=0; i < (long) length; i++)
538 Ascii85Encode(image,pixels[i]);
539 Ascii85Flush(image);
540 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
541 break;
542 }
543 case FaxCompression:
544 case Group4Compression:
545 {
546 if ((compression == FaxCompression) ||
547 (LocaleCompare(CCITTParam,"0") == 0))
548 status=HuffmanEncodeImage(image_info,image,mask_image);
549 else
550 status=Huffman2DEncodeImage(image_info,image,mask_image);
551 break;
552 }
553 case LZWCompression:
554 {
555 status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
556 if (status == MagickFalse)
557 break;
558 status=LZWEncodeImage(image,length,pixels);
559 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
560 break;
561 }
562 case RLECompression:
563 {
564 status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
565 if (status == MagickFalse)
566 break;
567 status=PackbitsEncodeImage(image,length,pixels);
568 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
569 break;
570 }
571 case ZipCompression:
572 {
573 status=SerializeImageChannel(image_info,mask_image,&pixels,&length);
574 if (status == MagickFalse)
575 break;
576 status=ZLIBEncodeImage(image,length,pixels);
577 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
578 break;
579 }
580 }
581 mask_image=DestroyImage(mask_image);
582 (void) WriteBlobByte(image,'\n');
583 length=(size_t) (TellBlob(image)-stop);
584 stop=TellBlob(image);
585 offset=SeekBlob(image,start,SEEK_SET);
586 if (offset < 0)
587 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
588 (void) FormatMagickString(buffer,MaxTextExtent,
589 "%%%%BeginData:%13ld %s Bytes\n",(long) length,
590 compression == NoCompression ? "ASCII" : "BINARY");
591 (void) WriteBlobString(image,buffer);
592 offset=SeekBlob(image,stop,SEEK_SET);
593 if (offset < 0)
594 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
595 (void) WriteBlobString(image,"%%EndData\n");
596 (void) WriteBlobString(image, "/mask_stream exch def\n");
597 return(status);
598}
599
600static MagickBooleanType WritePS3Image(const ImageInfo *image_info,Image *image)
601{
602 static const char
603 *PostscriptProlog[]=
604 {
605 "/ByteStreamDecodeFilter",
606 "{",
607 " /z exch def",
608 " /r exch def",
609 " /c exch def",
610 " z "PS3_NoCompression" eq { /ASCII85Decode filter } if",
611 " z "PS3_FaxCompression" eq",
612 " {",
613 " <<",
614 " /K "CCITTParam,
615 " /Columns c",
616 " /Rows r",
617 " >>",
618 " /CCITTFaxDecode filter",
619 " } if",
620 " z "PS3_JPEGCompression" eq { /DCTDecode filter } if",
621 " z "PS3_LZWCompression" eq { /LZWDecode filter } if",
622 " z "PS3_RLECompression" eq { /RunLengthDecode filter } if",
623 " z "PS3_ZipCompression" eq { /FlateDecode filter } if",
624 "} bind def",
625 "",
626 "/DirectClassImageDict",
627 "{",
628 " colorspace "PS3_RGBColorspace" eq",
629 " {",
630 " /DeviceRGB setcolorspace",
631 " <<",
632 " /ImageType 1",
633 " /Width columns",
634 " /Height rows",
635 " /BitsPerComponent 8",
636 " /DataSource pixel_stream",
637 " /MultipleDataSources false",
638 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
639 " /Decode [0 1 0 1 0 1]",
640 " >>",
641 " }",
642 " {",
643 " /DeviceCMYK setcolorspace",
644 " <<",
645 " /ImageType 1",
646 " /Width columns",
647 " /Height rows",
648 " /BitsPerComponent 8",
649 " /DataSource pixel_stream",
650 " /MultipleDataSources false",
651 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
652 " /Decode",
653 " compression "PS3_JPEGCompression" eq",
654 " { [1 0 1 0 1 0 1 0] }",
655 " { [0 1 0 1 0 1 0 1] }",
656 " ifelse",
657 " >>",
658 " }",
659 " ifelse",
660 "} bind def",
661 "",
662 "/PseudoClassImageDict",
663 "{",
664 " % Colors in colormap image.",
665 " currentfile buffer readline pop",
666 " token pop /colors exch def pop",
667 " colors 0 eq",
668 " {",
669 " % Depth of grayscale image.",
670 " currentfile buffer readline pop",
671 " token pop /bits exch def pop",
672 " /DeviceGray setcolorspace",
673 " <<",
674 " /ImageType 1",
675 " /Width columns",
676 " /Height rows",
677 " /BitsPerComponent bits",
678 " /Decode [0 1]",
679 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
680 " /DataSource pixel_stream",
681 " >>",
682 " }",
683 " {",
684 " % RGB colormap.",
685 " /colormap colors 3 mul string def",
686 " compression "PS3_NoCompression" eq",
687 " { currentfile /ASCII85Decode filter colormap readstring pop pop }",
688 " { currentfile colormap readstring pop pop }",
689 " ifelse",
690 " [ /Indexed /DeviceRGB colors 1 sub colormap ] setcolorspace",
691 " <<",
692 " /ImageType 1",
693 " /Width columns",
694 " /Height rows",
695 " /BitsPerComponent 8",
696 " /Decode [0 255]",
697 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
698 " /DataSource pixel_stream",
699 " >>",
700 " }",
701 " ifelse",
702 "} bind def",
703 "",
704 "/NonMaskedImageDict",
705 "{",
706 " class "PS3_PseudoClass" eq",
707 " { PseudoClassImageDict }",
708 " { DirectClassImageDict }",
709 " ifelse",
710 "} bind def",
711 "",
712 "/MaskedImageDict",
713 "{",
714 " <<",
715 " /ImageType 3",
716 " /InterleaveType 3",
717 " /DataDict NonMaskedImageDict",
718 " /MaskDict",
719 " <<",
720 " /ImageType 1",
721 " /Width columns",
722 " /Height rows",
723 " /BitsPerComponent 1",
724 " /DataSource mask_stream",
725 " /MultipleDataSources false",
726 " /ImageMatrix [ columns 0 0 rows neg 0 rows]",
727 " /Decode [ 0 1 ]",
728 " >>",
729 " >>",
730 "} bind def",
731 "",
732 "/ClipImage",
733 "{} def",
734 "",
735 "/DisplayImage",
736 "{",
737 " /buffer 512 string def",
738 " % Translation.",
739 " currentfile buffer readline pop",
740 " token pop /x exch def",
741 " token pop /y exch def pop",
742 " x y translate",
743 " % Image size and font size.",
744 " currentfile buffer readline pop",
745 " token pop /x exch def",
746 " token pop /y exch def pop",
747 " currentfile buffer readline pop",
748 " token pop /pointsize exch def pop",
749 (char *) NULL
750 },
751 *PostscriptEpilog[]=
752 {
753 " x y scale",
754 " % Clipping path.",
755 " currentfile buffer readline pop",
756 " token pop /clipped exch def pop",
757 " % Showpage.",
758 " currentfile buffer readline pop",
759 " token pop /sp exch def pop",
760 " % Image pixel size.",
761 " currentfile buffer readline pop",
762 " token pop /columns exch def",
763 " token pop /rows exch def pop",
764 " % Colorspace (RGB/CMYK).",
765 " currentfile buffer readline pop",
766 " token pop /colorspace exch def pop",
767 " % Transparency.",
768 " currentfile buffer readline pop",
769 " token pop /alpha exch def pop",
770 " % Stencil mask?",
771 " currentfile buffer readline pop",
772 " token pop /stencil exch def pop",
773 " % Image class (direct/pseudo).",
774 " currentfile buffer readline pop",
775 " token pop /class exch def pop",
776 " % Compression type.",
777 " currentfile buffer readline pop",
778 " token pop /compression exch def pop",
779 " % Clip and render.",
780 " /pixel_stream currentfile columns rows compression ByteStreamDecodeFilter def",
781 " clipped { ClipImage } if",
782 " alpha stencil not and",
783 " { MaskedImageDict mask_stream resetfile }",
784 " { NonMaskedImageDict }",
785 " ifelse",
786 " stencil { 0 setgray imagemask } { image } ifelse",
787 " sp { showpage } if",
788 "} bind def",
789 (char *) NULL
790 };
791
792 char
793 buffer[MaxTextExtent],
794 date[MaxTextExtent],
795 **labels,
796 page_geometry[MaxTextExtent];
797
798 CompressionType
799 compression;
800
801 const char
802 *option,
803 **q,
804 *value;
805
806 double
807 pointsize;
808
809 GeometryInfo
810 geometry_info;
811
812 long
813 j;
814
815 MagickBooleanType
816 status;
817
818 MagickOffsetType
819 offset,
820 scene,
821 start,
822 stop;
823
824 MagickStatusType
825 flags;
826
827 PointInfo
828 delta,
829 resolution,
830 scale;
831
832 RectangleInfo
833 geometry,
834 media_info,
835 page_info;
836
837 register long
838 i;
839
840 SegmentInfo
841 bounds;
842
843 size_t
844 length;
845
846 time_t
847 timer;
848
849 unsigned char
850 *pixels;
851
852 unsigned long
853 page,
854 pixel,
855 text_size;
856
857 /*
858 Open output image file.
859 */
860 assert(image_info != (const ImageInfo *) NULL);
861 assert(image_info->signature == MagickSignature);
862 assert(image != (Image *) NULL);
863 assert(image->signature == MagickSignature);
864 if (image->debug != MagickFalse)
865 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
866 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
867 if (status == MagickFalse)
868 return(MagickFalse);
869 compression=image->compression;
870 if (image_info->compression != UndefinedCompression)
871 compression=image_info->compression;
872 switch (compression)
873 {
874 case FaxCompression:
875 case Group4Compression:
876 {
877 if ((IsMonochromeImage(image,&image->exception) == MagickFalse) ||
878 (image->matte != MagickFalse))
879 compression=RLECompression;
880 break;
881 }
882#if !defined(MAGICKCORE_JPEG_DELEGATE)
883 case JPEGCompression:
884 {
885 compression=RLECompression;
886 (void) ThrowMagickException(&image->exception,GetMagickModule(),
887 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
888 image->filename);
889 break;
890 }
891#endif
892#if !defined(MAGICKCORE_ZLIB_DELEGATE)
893 case ZipCompression:
894 {
895 compression=RLECompression;
896 (void) ThrowMagickException(&image->exception,GetMagickModule(),
897 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZLIB)",
898 image->filename);
899 break;
900 }
901#endif
902 default:
903 break;
904 }
905 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
906 page=0;
907 scene=0;
908 do
909 {
910 /*
911 Scale relative to dots-per-inch.
912 */
913 delta.x=DefaultResolution;
914 delta.y=DefaultResolution;
915 resolution.x=image->x_resolution;
916 resolution.y=image->y_resolution;
917 if ((resolution.x == 0.0) || (resolution.y == 0.0))
918 {
919 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
920 resolution.x=geometry_info.rho;
921 resolution.y=geometry_info.sigma;
922 if ((flags & SigmaValue) == 0)
923 resolution.y=resolution.x;
924 }
925 if (image_info->density != (char *) NULL)
926 {
927 flags=ParseGeometry(image_info->density,&geometry_info);
928 resolution.x=geometry_info.rho;
929 resolution.y=geometry_info.sigma;
930 if ((flags & SigmaValue) == 0)
931 resolution.y=resolution.x;
932 }
933 if (image->units == PixelsPerCentimeterResolution)
934 {
935 resolution.x*=2.54;
936 resolution.y*=2.54;
937 }
938 SetGeometry(image,&geometry);
939 (void) FormatMagickString(page_geometry,MaxTextExtent,"%lux%lu",
940 image->columns,image->rows);
941 if (image_info->page != (char *) NULL)
942 (void) CopyMagickString(page_geometry,image_info->page,MaxTextExtent);
943 else
944 if ((image->page.width != 0) && (image->page.height != 0))
945 (void) FormatMagickString(page_geometry,MaxTextExtent,"%lux%lu%+ld%+ld",
946 image->page.width,image->page.height,image->page.x,image->page.y);
947 else
948 if ((image->gravity != UndefinedGravity) &&
949 (LocaleCompare(image_info->magick,"PS") == 0))
950 (void) CopyMagickString(page_geometry,PSPageGeometry,MaxTextExtent);
951 (void) ConcatenateMagickString(page_geometry,">",MaxTextExtent);
952 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
953 &geometry.width,&geometry.height);
954 scale.x=(double) (geometry.width*delta.x)/resolution.x;
955 geometry.width=(unsigned long) (scale.x+0.5);
956 scale.y=(double) (geometry.height*delta.y)/resolution.y;
957 geometry.height=(unsigned long) (scale.y+0.5);
958 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
959 (void) ParseGravityGeometry(image,page_geometry,&page_info,
960 &image->exception);
961 if (image->gravity != UndefinedGravity)
962 {
963 geometry.x=(-page_info.x);
964 geometry.y=(long) (media_info.height+page_info.y-image->rows);
965 }
966 pointsize=12.0;
967 if (image_info->pointsize != 0.0)
968 pointsize=image_info->pointsize;
969 text_size=0;
970 value=GetImageProperty(image,"label");
971 if (value != (const char *) NULL)
972 text_size=(unsigned long) (MultilineCensus(value)*pointsize+12);
973 page++;
974 if (page == 1)
975 {
976 /*
977 Postscript header on the first page.
978 */
979 if (LocaleCompare(image_info->magick,"PS3") == 0)
980 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0\n",MaxTextExtent);
981 else
982 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0 EPSF-3.0\n",
983 MaxTextExtent);
984 (void) WriteBlobString(image,buffer);
985 (void) FormatMagickString(buffer,MaxTextExtent,
986 "%%%%Creator: ImageMagick %s\n",MagickLibVersionText);
987 (void) WriteBlobString(image,buffer);
988 (void) FormatMagickString(buffer,MaxTextExtent,"%%%%Title: %s\n",
989 image->filename);
990 (void) WriteBlobString(image,buffer);
991 timer=time((time_t *) NULL);
992 (void) FormatMagickTime(timer,MaxTextExtent,date);
993 (void) FormatMagickString(buffer,MaxTextExtent,
994 "%%%%CreationDate: %s\n",date);
995 (void) WriteBlobString(image,buffer);
996 bounds.x1=(double) geometry.x;
997 bounds.y1=(double) geometry.y;
998 bounds.x2=(double) geometry.x+scale.x;
999 bounds.y2=(double) geometry.y+scale.y+text_size;
1000 if ((image_info->adjoin != MagickFalse) &&
1001 (GetNextImageInList(image) != (Image *) NULL))
1002 {
1003 (void) WriteBlobString(image,"%%BoundingBox: (atend)\n");
1004 (void) WriteBlobString(image,"%%HiResBoundingBox: (atend)\n");
1005 }
1006 else
1007 {
1008 (void) FormatMagickString(buffer,MaxTextExtent,
1009 "%%%%BoundingBox: %g %g %g %g\n",floor(bounds.x1+0.5),
1010 floor(bounds.y1+0.5),ceil(bounds.x2-0.5),ceil(bounds.y2-0.5));
1011 (void) WriteBlobString(image,buffer);
1012 (void) FormatMagickString(buffer,MaxTextExtent,
1013 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,bounds.y1,
1014 bounds.x2,bounds.y2);
1015 (void) WriteBlobString(image,buffer);
1016 if (image->colorspace == CMYKColorspace)
1017 (void) WriteBlobString(image,
1018 "%%DocumentProcessColors: Cyan Magenta Yellow Black\n");
1019 else
1020 if (IsGrayImage(image,&image->exception) != MagickFalse)
1021 (void) WriteBlobString(image,
1022 "%%DocumentProcessColors: Black\n");
1023 }
1024 /*
1025 Font resources
1026 */
1027 value=GetImageProperty(image,"label");
1028 if (value != (const char *) NULL)
1029 (void) WriteBlobString(image,
1030 "%%DocumentNeededResources: font Helvetica\n");
1031 (void) WriteBlobString(image,"%%LanguageLevel: 3\n");
1032 /*
1033 Pages, orientation and order.
1034 */
1035 if (LocaleCompare(image_info->magick,"PS3") != 0)
1036 (void) WriteBlobString(image,"%%Pages: 1\n");
1037 else
1038 {
1039 (void) WriteBlobString(image,"%%Orientation: Portrait\n");
1040 (void) WriteBlobString(image,"%%PageOrder: Ascend\n");
1041 if (image_info->adjoin == MagickFalse)
1042 (void) CopyMagickString(buffer,"%%Pages: 1\n",MaxTextExtent);
1043 else
1044 (void) FormatMagickString(buffer,MaxTextExtent,"%%%%Pages: %lu\n",
1045 (unsigned long) GetImageListLength(image));
1046 (void) WriteBlobString(image,buffer);
1047 }
1048 (void) WriteBlobString(image,"%%EndComments\n");
1049 /*
1050 The static postscript procedures prolog.
1051 */
1052 (void)WriteBlobString(image,"%%BeginProlog\n");
1053 for (q=PostscriptProlog; *q; q++)
1054 {
1055 (void) WriteBlobString(image,*q);
1056 (void) WriteBlobByte(image,'\n');
1057 }
1058 /*
1059 One label line for each line in label string.
1060 */
1061 value=GetImageProperty(image,"label");
1062 if (value != (const char *) NULL)
1063 {
1064 (void) WriteBlobString(image,"\n %% Labels.\n /Helvetica "
1065 " findfont pointsize scalefont setfont\n");
1066 for (i=(long) MultilineCensus(value)-1; i >= 0; i--)
1067 {
1068 (void) WriteBlobString(image,
1069 " currentfile buffer readline pop token pop\n");
1070 (void) FormatMagickString(buffer,MaxTextExtent,
1071 " 0 y %g add moveto show pop\n",i*pointsize+12);
1072 (void) WriteBlobString(image,buffer);
1073 }
1074 }
1075 /*
1076 The static postscript procedures epilog.
1077 */
1078 for (q=PostscriptEpilog; *q; q++)
1079 {
1080 (void) WriteBlobString(image,*q);
1081 (void) WriteBlobByte(image,'\n');
1082 }
1083 (void)WriteBlobString(image,"%%EndProlog\n");
1084 }
1085 (void) FormatMagickString(buffer,MaxTextExtent,"%%%%Page: 1 %lu\n",page);
1086 (void) WriteBlobString(image,buffer);
1087 /*
1088 Page bounding box.
1089 */
1090 (void) FormatMagickString(buffer,MaxTextExtent,
1091 "%%%%PageBoundingBox: %ld %ld %ld %ld\n",geometry.x,geometry.y,geometry.x+
1092 (long) geometry.width,geometry.y+(long) (geometry.height+text_size));
1093 (void) WriteBlobString(image,buffer);
1094 /*
1095 Page process colors if not RGB.
1096 */
1097 if (image->colorspace == CMYKColorspace)
1098 (void) WriteBlobString(image,
1099 "%%PageProcessColors: Cyan Magenta Yellow Black\n");
1100 else
1101 if (IsGrayImage(image,&image->exception) != MagickFalse)
1102 (void) WriteBlobString(image,"%%PageProcessColors: Black\n");
1103 /*
1104 Adjust document bounding box to bound page bounding box.
1105 */
1106 if ((double) geometry.x < bounds.x1)
1107 bounds.x1=(double) geometry.x;
1108 if ((double) geometry.y < bounds.y1)
1109 bounds.y1=(double) geometry.y;
1110 if ((double) (geometry.x+scale.x) > bounds.x2)
1111 bounds.x2=(double) geometry.x+scale.x;
1112 if ((double) (geometry.y+scale.y+text_size) > bounds.y2)
1113 bounds.y2=(double) geometry.y+scale.y+text_size;
1114 /*
1115 Page font resource if there's a label.
1116 */
1117 value=GetImageProperty(image,"label");
1118 if (value != (const char *) NULL)
1119 (void) WriteBlobString(image,"%%PageResources: font Helvetica\n");
1120 /*
1121 PS clipping path from Photoshop clipping path.
1122 */
1123 if ((image->clip_mask == (Image *) NULL) ||
1124 (LocaleNCompare("8BIM:",image->clip_mask->magick_filename,5) != 0))
1125 (void) WriteBlobString(image,"/ClipImage {} def\n");
1126 else
1127 {
1128 const char
1129 *value;
1130
1131 value=GetImageProperty(image,image->clip_mask->magick_filename);
1132 if (value == (const char *) NULL)
1133 return(MagickFalse);
1134 (void) WriteBlobString(image,value);
1135 (void) WriteBlobByte(image,'\n');
1136 }
1137 /*
1138 Push a dictionary for our own def's if this an EPS.
1139 */
1140 if (LocaleCompare(image_info->magick,"PS3") != 0)
1141 (void) WriteBlobString(image,"userdict begin\n");
1142 /*
1143 Image mask.
1144 */
1145 if ((image->matte != MagickFalse) &&
1146 (WritePS3MaskImage(image_info,image,compression) == MagickFalse))
1147 {
1148 (void) CloseBlob(image);
1149 return(MagickFalse);
1150 }
1151 /*
1152 Remember position of BeginData comment so we can update it.
1153 */
1154 start=TellBlob(image);
1155 (void) FormatMagickString(buffer,MaxTextExtent,
1156 "%%%%BeginData:%13ld %s Bytes\n",0L,
1157 compression == NoCompression ? "ASCII" : "BINARY");
1158 (void) WriteBlobString(image,buffer);
1159 stop=TellBlob(image);
1160 (void) WriteBlobString(image,"DisplayImage\n");
1161 /*
1162 Translate, scale, and font point size.
1163 */
1164 (void) FormatMagickString(buffer,MaxTextExtent,"%ld %ld\n%g %g\n%f\n",
1165 geometry.x,geometry.y,scale.x,scale.y,pointsize);
1166 (void) WriteBlobString(image,buffer);
1167 /*
1168 Output labels.
1169 */
1170 labels=(char **) NULL;
1171 value=GetImageProperty(image,"label");
1172 if (value != (const char *) NULL)
1173 labels=StringToList(value);
1174 if (labels != (char **) NULL)
1175 {
1176 for (i=0; labels[i] != (char *) NULL; i++)
1177 {
1178 if (compression != NoCompression)
1179 {
1180 for (j=0; labels[i][j] != '\0'; j++)
1181 (void) WriteBlobByte(image,(unsigned char) labels[i][j]);
1182 (void) WriteBlobByte(image,'\n');
1183 }
1184 else
1185 {
1186 (void) WriteBlobString(image,"<~");
1187 Ascii85Initialize(image);
1188 for (j=0; labels[i][j] != '\0'; j++)
1189 Ascii85Encode(image,(unsigned char) labels[i][j]);
1190 Ascii85Flush(image);
1191 }
1192 labels[i]=DestroyString(labels[i]);
1193 }
1194 labels=(char **) RelinquishMagickMemory(labels);
1195 }
1196 /*
1197 Photoshop clipping path active?
1198 */
1199 if ((image->clip_mask != (Image *) NULL) &&
1200 (LocaleNCompare("8BIM:",image->clip_mask->magick_filename,5) == 0))
1201 (void) WriteBlobString(image,"true\n");
1202 else
1203 (void) WriteBlobString(image,"false\n");
1204 /*
1205 Showpage for non-EPS.
1206 */
1207 (void) WriteBlobString(image, LocaleCompare(image_info->magick,"PS3") == 0 ?
1208 "true\n" : "false\n");
1209 /*
1210 Image columns, rows, and color space.
1211 */
1212 (void) FormatMagickString(buffer,MaxTextExtent,"%lu %lu\n%s\n",
1213 image->columns,image->rows,image->colorspace == CMYKColorspace ?
1214 PS3_CMYKColorspace : PS3_RGBColorspace);
1215 (void) WriteBlobString(image,buffer);
1216 /*
1217 Masked image?
1218 */
1219 (void) WriteBlobString(image,image->matte != MagickFalse ?
1220 "true\n" : "false\n");
1221 /*
1222 Render with imagemask operator?
1223 */
1224 option=GetImageOption(image_info,"ps3:imagemask");
1225 (void) WriteBlobString(image,((option != (const char *) NULL) &&
1226 (IsMonochromeImage(image,&image->exception) != MagickFalse)) ?
1227 "true\n" : "false\n");
1228 /*
1229 Output pixel data.
1230 */
1231 pixels=(unsigned char *) NULL;
1232 length=0;
1233 if ((image_info->type != TrueColorType) &&
1234 (image_info->type != TrueColorMatteType) &&
1235 (image_info->type != ColorSeparationType) &&
1236 (image_info->type != ColorSeparationMatteType) &&
1237 (image->colorspace != CMYKColorspace) &&
1238 ((IsGrayImage(image,&image->exception) != MagickFalse) ||
1239 (IsMonochromeImage(image,&image->exception) != MagickFalse)))
1240 {
1241 /*
1242 Gray images.
1243 */
1244 (void) WriteBlobString(image,PS3_PseudoClass"\n");
1245 switch (compression)
1246 {
1247 case NoCompression:
1248 default:
1249 {
1250 (void) WriteBlobString(image,PS3_NoCompression"\n");
1251 break;
1252 }
1253 case FaxCompression:
1254 case Group4Compression:
1255 {
1256 (void) WriteBlobString(image,PS3_FaxCompression"\n");
1257 break;
1258 }
1259 case JPEGCompression:
1260 {
1261 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1262 break;
1263 }
1264 case LZWCompression:
1265 {
1266 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1267 break;
1268 }
1269 case RLECompression:
1270 {
1271 (void) WriteBlobString(image,PS3_RLECompression"\n");
1272 break;
1273 }
1274 case ZipCompression:
1275 {
1276 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1277 break;
1278 }
1279 }
1280 /*
1281 Number of colors -- 0 for single component non-color mapped data.
1282 */
1283 (void) WriteBlobString(image,"0\n");
1284 /*
1285 1 bit or 8 bit components?
1286 */
1287 (void) FormatMagickString(buffer,MaxTextExtent,"%d\n",
1288 IsMonochromeImage(image,&image->exception) != MagickFalse ? 1 : 8);
1289 (void) WriteBlobString(image,buffer);
1290 /*
1291 Image data.
1292 */
1293 if (compression == JPEGCompression)
1294 status=InjectImageBlob(image_info,image,image,"jpeg",
1295 &image->exception);
1296 else
1297 if ((compression == FaxCompression) ||
1298 (compression == Group4Compression))
1299 {
1300 if (LocaleCompare(CCITTParam,"0") == 0)
1301 status=HuffmanEncodeImage(image_info,image,image);
1302 else
1303 status=Huffman2DEncodeImage(image_info,image,image);
1304 }
1305 else
1306 {
1307 status=SerializeImageChannel(image_info,image,&pixels,&length);
1308 if (status == MagickFalse)
1309 {
1310 (void) CloseBlob(image);
1311 return(MagickFalse);
1312 }
1313 switch (compression)
1314 {
1315 case NoCompression:
1316 default:
1317 {
1318 Ascii85Initialize(image);
1319 for (i=0; i < (long) length; i++)
1320 Ascii85Encode(image,pixels[i]);
1321 Ascii85Flush(image);
1322 status=MagickTrue;
1323 break;
1324 }
1325 case LZWCompression:
1326 {
1327 status=LZWEncodeImage(image,length,pixels);
1328 break;
1329 }
1330 case RLECompression:
1331 {
1332 status=PackbitsEncodeImage(image,length,pixels);
1333 break;
1334 }
1335 case ZipCompression:
1336 {
1337 status=ZLIBEncodeImage(image,length,pixels);
1338 break;
1339 }
1340 }
1341 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1342 }
1343 }
1344 else
1345 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1346 (compression == JPEGCompression))
1347 {
1348 /*
1349 Truecolor image.
1350 */
1351 (void) WriteBlobString(image,PS3_DirectClass"\n");
1352 switch (compression)
1353 {
1354 case NoCompression:
1355 default:
1356 {
1357 (void) WriteBlobString(image,PS3_NoCompression"\n");
1358 break;
1359 }
1360 case RLECompression:
1361 {
1362 (void) WriteBlobString(image,PS3_RLECompression"\n");
1363 break;
1364 }
1365 case JPEGCompression:
1366 {
1367 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1368 break;
1369 }
1370 case LZWCompression:
1371 {
1372 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1373 break;
1374 }
1375 case ZipCompression:
1376 {
1377 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1378 break;
1379 }
1380 }
1381 /*
1382 Image data.
1383 */
1384 if (compression == JPEGCompression)
1385 status=InjectImageBlob(image_info,image,image,"jpeg",
1386 &image->exception);
1387 else
1388 {
1389 /*
1390 Stream based compressions.
1391 */
1392 status=SerializeImage(image_info,image,&pixels,&length);
1393 if (status == MagickFalse)
1394 {
1395 (void) CloseBlob(image);
1396 return(MagickFalse);
1397 }
1398 switch (compression)
1399 {
1400 case NoCompression:
1401 default:
1402 {
1403 Ascii85Initialize(image);
1404 for (i=0; i < (long) length; i++)
1405 Ascii85Encode(image,pixels[i]);
1406 Ascii85Flush(image);
1407 status=MagickTrue;
1408 break;
1409 }
1410 case RLECompression:
1411 {
1412 status=PackbitsEncodeImage(image,length,pixels);
1413 break;
1414 }
1415 case LZWCompression:
1416 {
1417 status=LZWEncodeImage(image,length,pixels);
1418 break;
1419 }
1420 case ZipCompression:
1421 {
1422 status=ZLIBEncodeImage(image,length,pixels);
1423 break;
1424 }
1425 }
1426 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1427 }
1428 }
1429 else
1430 {
1431 /*
1432 Colormapped images.
1433 */
1434 (void) WriteBlobString(image,PS3_PseudoClass"\n");
1435 switch (compression)
1436 {
1437 case NoCompression:
1438 default:
1439 {
1440 (void) WriteBlobString(image,PS3_NoCompression"\n");
1441 break;
1442 }
1443 case JPEGCompression:
1444 {
1445 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1446 break;
1447 }
1448 case RLECompression:
1449 {
1450 (void) WriteBlobString(image,PS3_RLECompression"\n");
1451 break;
1452 }
1453 case LZWCompression:
1454 {
1455 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1456 break;
1457 }
1458 case ZipCompression:
1459 {
1460 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1461 break;
1462 }
1463 }
1464 /*
1465 Number of colors in color map.
1466 */
1467 (void) FormatMagickString(buffer,MaxTextExtent,"%lu\n",
1468 image->colors);
1469 (void) WriteBlobString(image,buffer);
1470 /*
1471 Color map - uncompressed.
1472 */
1473 if ((compression != NoCompression) &&
1474 (compression != UndefinedCompression))
1475 {
1476 for (i=0; i < (long) image->colors; i++)
1477 {
1478 pixel=ScaleQuantumToChar(image->colormap[i].red);
1479 (void) WriteBlobByte(image,(unsigned char) pixel);
1480 pixel=ScaleQuantumToChar(image->colormap[i].green);
1481 (void) WriteBlobByte(image,(unsigned char) pixel);
1482 pixel=ScaleQuantumToChar(image->colormap[i].blue);
1483 (void) WriteBlobByte(image,(unsigned char) pixel);
1484 }
1485 }
1486 else
1487 {
1488 Ascii85Initialize(image);
1489 for (i=0; i < (long) image->colors; i++)
1490 {
1491 pixel=ScaleQuantumToChar(image->colormap[i].red);
1492 Ascii85Encode(image,(unsigned char) pixel);
1493 pixel=ScaleQuantumToChar(image->colormap[i].green);
1494 Ascii85Encode(image,(unsigned char) pixel);
1495 pixel=ScaleQuantumToChar(image->colormap[i].blue);
1496 Ascii85Encode(image,(unsigned char) pixel);
1497 }
1498 Ascii85Flush(image);
1499 }
1500 status=SerializeImageIndexes(image_info,image,&pixels,&length);
1501 if (status == MagickFalse)
1502 {
1503 (void) CloseBlob(image);
1504 return(MagickFalse);
1505 }
1506 switch (compression)
1507 {
1508 case NoCompression:
1509 default:
1510 {
1511 Ascii85Initialize(image);
1512 for (i=0; i < (long) length; i++)
1513 Ascii85Encode(image,pixels[i]);
1514 Ascii85Flush(image);
1515 status=MagickTrue;
1516 break;
1517 }
1518 case JPEGCompression:
1519 {
1520 status=InjectImageBlob(image_info,image,image,"jpeg",
1521 &image->exception);
1522 break;
1523 }
1524 case RLECompression:
1525 {
1526 status=PackbitsEncodeImage(image,length,pixels);
1527 break;
1528 }
1529 case LZWCompression:
1530 {
1531 status=LZWEncodeImage(image,length,pixels);
1532 break;
1533 }
1534 case ZipCompression:
1535 {
1536 status=ZLIBEncodeImage(image,length,pixels);
1537 break;
1538 }
1539 }
1540 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1541 }
1542 (void) WriteBlobByte(image,'\n');
1543 if (status == MagickFalse)
1544 {
1545 (void) CloseBlob(image);
1546 return(MagickFalse);
1547 }
1548 /*
1549 Update BeginData now that we know the data size.
1550 */
1551 length=(size_t) (TellBlob(image)-stop);
1552 stop=TellBlob(image);
1553 offset=SeekBlob(image,start,SEEK_SET);
1554 if (offset < 0)
1555 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
1556 (void) FormatMagickString(buffer,MaxTextExtent,
1557 "%%%%BeginData:%13ld %s Bytes\n",(long) length,
1558 compression == NoCompression ? "ASCII" : "BINARY");
1559 (void) WriteBlobString(image,buffer);
1560 offset=SeekBlob(image,stop,SEEK_SET);
1561 (void) WriteBlobString(image,"%%EndData\n");
1562 /*
1563 End private dictionary if this an EPS.
1564 */
1565 if (LocaleCompare(image_info->magick,"PS3") != 0)
1566 (void) WriteBlobString(image,"end\n");
1567 (void) WriteBlobString(image,"%%PageTrailer\n");
1568 if (GetNextImageInList(image) == (Image *) NULL)
1569 break;
1570 image=SyncNextImageInList(image);
1571 status=SetImageProgress(image,SaveImagesTag,scene++,
1572 GetImageListLength(image));
1573 if (status == MagickFalse)
1574 break;
1575 } while (image_info->adjoin != MagickFalse);
1576 (void) WriteBlobString(image,"%%Trailer\n");
1577 if (page > 1)
1578 {
1579 (void) FormatMagickString(buffer,MaxTextExtent,
1580 "%%%%BoundingBox: %g %g %g %g\n",floor(bounds.x1+0.5),
1581 floor(bounds.y1+0.5),ceil(bounds.x2-0.5),ceil(bounds.y2-0.5));
1582 (void) WriteBlobString(image,buffer);
1583 (void) FormatMagickString(buffer,MaxTextExtent,
1584 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,bounds.y1,
1585 bounds.x2,bounds.y2);
1586 (void) WriteBlobString(image,buffer);
1587 }
1588 (void) WriteBlobString(image,"%%EOF\n");
1589 (void) CloseBlob(image);
1590 return(MagickTrue);
1591}