blob: 12b9115beedb133d69d868770c5d8ce56fa4fd64 [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 {
cristyc4c8d132010-01-07 01:58:38 +0000273 *q++=ScaleQuantumToChar(GetRedSample(p));
274 *q++=ScaleQuantumToChar(GetGreenSample(p));
275 *q++=ScaleQuantumToChar(GetBlueSample(p));
cristy3ed852e2009-09-05 21:47:34 +0000276 p++;
277 }
278 else
279 for (x=0; x < (long) image->columns; x++)
280 {
cristyc4c8d132010-01-07 01:58:38 +0000281 *q++=ScaleQuantumToChar(GetRedSample(p));
282 *q++=ScaleQuantumToChar(GetGreenSample(p));
283 *q++=ScaleQuantumToChar(GetBlueSample(p));
cristy3ed852e2009-09-05 21:47:34 +0000284 *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,
cristy8cd5b312010-01-07 01:10:24 +00001009 "%%%%BoundingBox: %.15g %.15g %.15g %.15g\n",floor(bounds.x1+0.5),
cristy3ed852e2009-09-05 21:47:34 +00001010 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,
cristy8cd5b312010-01-07 01:10:24 +00001013 "%%%%HiResBoundingBox: %.15g %.15g %.15g %.15g\n",bounds.x1,
1014 bounds.y1,bounds.x2,bounds.y2);
cristy3ed852e2009-09-05 21:47:34 +00001015 (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,
cristy8cd5b312010-01-07 01:10:24 +00001071 " 0 y %.15g add moveto show pop\n",i*pointsize+12);
cristy3ed852e2009-09-05 21:47:34 +00001072 (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 */
cristy8cd5b312010-01-07 01:10:24 +00001164 (void) FormatMagickString(buffer,MaxTextExtent,
1165 "%ld %ld\n%.15g %.15g\n%.15g\n",geometry.x,geometry.y,scale.x,scale.y,
1166 pointsize);
cristy3ed852e2009-09-05 21:47:34 +00001167 (void) WriteBlobString(image,buffer);
1168 /*
1169 Output labels.
1170 */
1171 labels=(char **) NULL;
1172 value=GetImageProperty(image,"label");
1173 if (value != (const char *) NULL)
1174 labels=StringToList(value);
1175 if (labels != (char **) NULL)
1176 {
1177 for (i=0; labels[i] != (char *) NULL; i++)
1178 {
1179 if (compression != NoCompression)
1180 {
1181 for (j=0; labels[i][j] != '\0'; j++)
1182 (void) WriteBlobByte(image,(unsigned char) labels[i][j]);
1183 (void) WriteBlobByte(image,'\n');
1184 }
1185 else
1186 {
1187 (void) WriteBlobString(image,"<~");
1188 Ascii85Initialize(image);
1189 for (j=0; labels[i][j] != '\0'; j++)
1190 Ascii85Encode(image,(unsigned char) labels[i][j]);
1191 Ascii85Flush(image);
1192 }
1193 labels[i]=DestroyString(labels[i]);
1194 }
1195 labels=(char **) RelinquishMagickMemory(labels);
1196 }
1197 /*
1198 Photoshop clipping path active?
1199 */
1200 if ((image->clip_mask != (Image *) NULL) &&
1201 (LocaleNCompare("8BIM:",image->clip_mask->magick_filename,5) == 0))
1202 (void) WriteBlobString(image,"true\n");
1203 else
1204 (void) WriteBlobString(image,"false\n");
1205 /*
1206 Showpage for non-EPS.
1207 */
1208 (void) WriteBlobString(image, LocaleCompare(image_info->magick,"PS3") == 0 ?
1209 "true\n" : "false\n");
1210 /*
1211 Image columns, rows, and color space.
1212 */
1213 (void) FormatMagickString(buffer,MaxTextExtent,"%lu %lu\n%s\n",
1214 image->columns,image->rows,image->colorspace == CMYKColorspace ?
1215 PS3_CMYKColorspace : PS3_RGBColorspace);
1216 (void) WriteBlobString(image,buffer);
1217 /*
1218 Masked image?
1219 */
1220 (void) WriteBlobString(image,image->matte != MagickFalse ?
1221 "true\n" : "false\n");
1222 /*
1223 Render with imagemask operator?
1224 */
1225 option=GetImageOption(image_info,"ps3:imagemask");
1226 (void) WriteBlobString(image,((option != (const char *) NULL) &&
1227 (IsMonochromeImage(image,&image->exception) != MagickFalse)) ?
1228 "true\n" : "false\n");
1229 /*
1230 Output pixel data.
1231 */
1232 pixels=(unsigned char *) NULL;
1233 length=0;
1234 if ((image_info->type != TrueColorType) &&
1235 (image_info->type != TrueColorMatteType) &&
1236 (image_info->type != ColorSeparationType) &&
1237 (image_info->type != ColorSeparationMatteType) &&
1238 (image->colorspace != CMYKColorspace) &&
1239 ((IsGrayImage(image,&image->exception) != MagickFalse) ||
1240 (IsMonochromeImage(image,&image->exception) != MagickFalse)))
1241 {
1242 /*
1243 Gray images.
1244 */
1245 (void) WriteBlobString(image,PS3_PseudoClass"\n");
1246 switch (compression)
1247 {
1248 case NoCompression:
1249 default:
1250 {
1251 (void) WriteBlobString(image,PS3_NoCompression"\n");
1252 break;
1253 }
1254 case FaxCompression:
1255 case Group4Compression:
1256 {
1257 (void) WriteBlobString(image,PS3_FaxCompression"\n");
1258 break;
1259 }
1260 case JPEGCompression:
1261 {
1262 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1263 break;
1264 }
1265 case LZWCompression:
1266 {
1267 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1268 break;
1269 }
1270 case RLECompression:
1271 {
1272 (void) WriteBlobString(image,PS3_RLECompression"\n");
1273 break;
1274 }
1275 case ZipCompression:
1276 {
1277 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1278 break;
1279 }
1280 }
1281 /*
1282 Number of colors -- 0 for single component non-color mapped data.
1283 */
1284 (void) WriteBlobString(image,"0\n");
1285 /*
1286 1 bit or 8 bit components?
1287 */
1288 (void) FormatMagickString(buffer,MaxTextExtent,"%d\n",
1289 IsMonochromeImage(image,&image->exception) != MagickFalse ? 1 : 8);
1290 (void) WriteBlobString(image,buffer);
1291 /*
1292 Image data.
1293 */
1294 if (compression == JPEGCompression)
1295 status=InjectImageBlob(image_info,image,image,"jpeg",
1296 &image->exception);
1297 else
1298 if ((compression == FaxCompression) ||
1299 (compression == Group4Compression))
1300 {
1301 if (LocaleCompare(CCITTParam,"0") == 0)
1302 status=HuffmanEncodeImage(image_info,image,image);
1303 else
1304 status=Huffman2DEncodeImage(image_info,image,image);
1305 }
1306 else
1307 {
1308 status=SerializeImageChannel(image_info,image,&pixels,&length);
1309 if (status == MagickFalse)
1310 {
1311 (void) CloseBlob(image);
1312 return(MagickFalse);
1313 }
1314 switch (compression)
1315 {
1316 case NoCompression:
1317 default:
1318 {
1319 Ascii85Initialize(image);
1320 for (i=0; i < (long) length; i++)
1321 Ascii85Encode(image,pixels[i]);
1322 Ascii85Flush(image);
1323 status=MagickTrue;
1324 break;
1325 }
1326 case LZWCompression:
1327 {
1328 status=LZWEncodeImage(image,length,pixels);
1329 break;
1330 }
1331 case RLECompression:
1332 {
1333 status=PackbitsEncodeImage(image,length,pixels);
1334 break;
1335 }
1336 case ZipCompression:
1337 {
1338 status=ZLIBEncodeImage(image,length,pixels);
1339 break;
1340 }
1341 }
1342 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1343 }
1344 }
1345 else
1346 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1347 (compression == JPEGCompression))
1348 {
1349 /*
1350 Truecolor image.
1351 */
1352 (void) WriteBlobString(image,PS3_DirectClass"\n");
1353 switch (compression)
1354 {
1355 case NoCompression:
1356 default:
1357 {
1358 (void) WriteBlobString(image,PS3_NoCompression"\n");
1359 break;
1360 }
1361 case RLECompression:
1362 {
1363 (void) WriteBlobString(image,PS3_RLECompression"\n");
1364 break;
1365 }
1366 case JPEGCompression:
1367 {
1368 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1369 break;
1370 }
1371 case LZWCompression:
1372 {
1373 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1374 break;
1375 }
1376 case ZipCompression:
1377 {
1378 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1379 break;
1380 }
1381 }
1382 /*
1383 Image data.
1384 */
1385 if (compression == JPEGCompression)
1386 status=InjectImageBlob(image_info,image,image,"jpeg",
1387 &image->exception);
1388 else
1389 {
1390 /*
1391 Stream based compressions.
1392 */
1393 status=SerializeImage(image_info,image,&pixels,&length);
1394 if (status == MagickFalse)
1395 {
1396 (void) CloseBlob(image);
1397 return(MagickFalse);
1398 }
1399 switch (compression)
1400 {
1401 case NoCompression:
1402 default:
1403 {
1404 Ascii85Initialize(image);
1405 for (i=0; i < (long) length; i++)
1406 Ascii85Encode(image,pixels[i]);
1407 Ascii85Flush(image);
1408 status=MagickTrue;
1409 break;
1410 }
1411 case RLECompression:
1412 {
1413 status=PackbitsEncodeImage(image,length,pixels);
1414 break;
1415 }
1416 case LZWCompression:
1417 {
1418 status=LZWEncodeImage(image,length,pixels);
1419 break;
1420 }
1421 case ZipCompression:
1422 {
1423 status=ZLIBEncodeImage(image,length,pixels);
1424 break;
1425 }
1426 }
1427 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1428 }
1429 }
1430 else
1431 {
1432 /*
1433 Colormapped images.
1434 */
1435 (void) WriteBlobString(image,PS3_PseudoClass"\n");
1436 switch (compression)
1437 {
1438 case NoCompression:
1439 default:
1440 {
1441 (void) WriteBlobString(image,PS3_NoCompression"\n");
1442 break;
1443 }
1444 case JPEGCompression:
1445 {
1446 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1447 break;
1448 }
1449 case RLECompression:
1450 {
1451 (void) WriteBlobString(image,PS3_RLECompression"\n");
1452 break;
1453 }
1454 case LZWCompression:
1455 {
1456 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1457 break;
1458 }
1459 case ZipCompression:
1460 {
1461 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1462 break;
1463 }
1464 }
1465 /*
1466 Number of colors in color map.
1467 */
1468 (void) FormatMagickString(buffer,MaxTextExtent,"%lu\n",
1469 image->colors);
1470 (void) WriteBlobString(image,buffer);
1471 /*
1472 Color map - uncompressed.
1473 */
1474 if ((compression != NoCompression) &&
1475 (compression != UndefinedCompression))
1476 {
1477 for (i=0; i < (long) image->colors; i++)
1478 {
1479 pixel=ScaleQuantumToChar(image->colormap[i].red);
1480 (void) WriteBlobByte(image,(unsigned char) pixel);
1481 pixel=ScaleQuantumToChar(image->colormap[i].green);
1482 (void) WriteBlobByte(image,(unsigned char) pixel);
1483 pixel=ScaleQuantumToChar(image->colormap[i].blue);
1484 (void) WriteBlobByte(image,(unsigned char) pixel);
1485 }
1486 }
1487 else
1488 {
1489 Ascii85Initialize(image);
1490 for (i=0; i < (long) image->colors; i++)
1491 {
1492 pixel=ScaleQuantumToChar(image->colormap[i].red);
1493 Ascii85Encode(image,(unsigned char) pixel);
1494 pixel=ScaleQuantumToChar(image->colormap[i].green);
1495 Ascii85Encode(image,(unsigned char) pixel);
1496 pixel=ScaleQuantumToChar(image->colormap[i].blue);
1497 Ascii85Encode(image,(unsigned char) pixel);
1498 }
1499 Ascii85Flush(image);
1500 }
1501 status=SerializeImageIndexes(image_info,image,&pixels,&length);
1502 if (status == MagickFalse)
1503 {
1504 (void) CloseBlob(image);
1505 return(MagickFalse);
1506 }
1507 switch (compression)
1508 {
1509 case NoCompression:
1510 default:
1511 {
1512 Ascii85Initialize(image);
1513 for (i=0; i < (long) length; i++)
1514 Ascii85Encode(image,pixels[i]);
1515 Ascii85Flush(image);
1516 status=MagickTrue;
1517 break;
1518 }
1519 case JPEGCompression:
1520 {
1521 status=InjectImageBlob(image_info,image,image,"jpeg",
1522 &image->exception);
1523 break;
1524 }
1525 case RLECompression:
1526 {
1527 status=PackbitsEncodeImage(image,length,pixels);
1528 break;
1529 }
1530 case LZWCompression:
1531 {
1532 status=LZWEncodeImage(image,length,pixels);
1533 break;
1534 }
1535 case ZipCompression:
1536 {
1537 status=ZLIBEncodeImage(image,length,pixels);
1538 break;
1539 }
1540 }
1541 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1542 }
1543 (void) WriteBlobByte(image,'\n');
1544 if (status == MagickFalse)
1545 {
1546 (void) CloseBlob(image);
1547 return(MagickFalse);
1548 }
1549 /*
1550 Update BeginData now that we know the data size.
1551 */
1552 length=(size_t) (TellBlob(image)-stop);
1553 stop=TellBlob(image);
1554 offset=SeekBlob(image,start,SEEK_SET);
1555 if (offset < 0)
1556 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
1557 (void) FormatMagickString(buffer,MaxTextExtent,
1558 "%%%%BeginData:%13ld %s Bytes\n",(long) length,
1559 compression == NoCompression ? "ASCII" : "BINARY");
1560 (void) WriteBlobString(image,buffer);
1561 offset=SeekBlob(image,stop,SEEK_SET);
1562 (void) WriteBlobString(image,"%%EndData\n");
1563 /*
1564 End private dictionary if this an EPS.
1565 */
1566 if (LocaleCompare(image_info->magick,"PS3") != 0)
1567 (void) WriteBlobString(image,"end\n");
1568 (void) WriteBlobString(image,"%%PageTrailer\n");
1569 if (GetNextImageInList(image) == (Image *) NULL)
1570 break;
1571 image=SyncNextImageInList(image);
1572 status=SetImageProgress(image,SaveImagesTag,scene++,
1573 GetImageListLength(image));
1574 if (status == MagickFalse)
1575 break;
1576 } while (image_info->adjoin != MagickFalse);
1577 (void) WriteBlobString(image,"%%Trailer\n");
1578 if (page > 1)
1579 {
1580 (void) FormatMagickString(buffer,MaxTextExtent,
cristy8cd5b312010-01-07 01:10:24 +00001581 "%%%%BoundingBox: %.15g %.15g %.15g %.15g\n",floor(bounds.x1+0.5),
cristy3ed852e2009-09-05 21:47:34 +00001582 floor(bounds.y1+0.5),ceil(bounds.x2-0.5),ceil(bounds.y2-0.5));
1583 (void) WriteBlobString(image,buffer);
1584 (void) FormatMagickString(buffer,MaxTextExtent,
cristy8cd5b312010-01-07 01:10:24 +00001585 "%%%%HiResBoundingBox: %.15g %.15g %.15g %.15g\n",bounds.x1,bounds.y1,
cristy3ed852e2009-09-05 21:47:34 +00001586 bounds.x2,bounds.y2);
1587 (void) WriteBlobString(image,buffer);
1588 }
1589 (void) WriteBlobString(image,"%%EOF\n");
1590 (void) CloseBlob(image);
1591 return(MagickTrue);
1592}