blob: 2a68d92c101e4cf12add5fcc6ada85df4ccf7b32 [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% %
cristy45ef08f2012-12-07 13:13:34 +000021% Copyright 1999-2013 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*/
cristy4c08aed2011-07-01 19:47:50 +000043#include "MagickCore/studio.h"
cristy76ce6e12013-04-05 14:33:38 +000044#include "MagickCore/artifact.h"
cristy4c08aed2011-07-01 19:47:50 +000045#include "MagickCore/attribute.h"
46#include "MagickCore/blob.h"
47#include "MagickCore/blob-private.h"
48#include "MagickCore/cache.h"
cristy500d6b92012-03-05 19:12:49 +000049#include "MagickCore/channel.h"
cristy4c08aed2011-07-01 19:47:50 +000050#include "MagickCore/color.h"
51#include "MagickCore/color-private.h"
52#include "MagickCore/compress.h"
53#include "MagickCore/constitute.h"
54#include "MagickCore/draw.h"
55#include "MagickCore/exception.h"
56#include "MagickCore/exception-private.h"
57#include "MagickCore/geometry.h"
58#include "MagickCore/image.h"
59#include "MagickCore/image-private.h"
60#include "MagickCore/list.h"
61#include "MagickCore/magick.h"
62#include "MagickCore/memory_.h"
63#include "MagickCore/monitor.h"
64#include "MagickCore/monitor-private.h"
65#include "MagickCore/option.h"
66#include "MagickCore/pixel-accessor.h"
67#include "MagickCore/property.h"
68#include "MagickCore/quantum-private.h"
69#include "MagickCore/resource_.h"
70#include "MagickCore/static.h"
71#include "MagickCore/string_.h"
72#include "MagickCore/module.h"
73#include "MagickCore/token.h"
74#include "MagickCore/utility.h"
75#include "MagickCore/module.h"
cristy3ed852e2009-09-05 21:47:34 +000076
77/*
78 Define declarations.
79*/
80#define PS3_NoCompression "0"
81#define PS3_FaxCompression "1"
82#define PS3_JPEGCompression "2"
83#define PS3_LZWCompression "3"
84#define PS3_RLECompression "4"
85#define PS3_ZipCompression "5"
86
87#define PS3_RGBColorspace "0"
88#define PS3_CMYKColorspace "1"
89
90#define PS3_DirectClass "0"
91#define PS3_PseudoClass "1"
cristy80975862009-09-25 14:34:31 +000092
93#if defined(MAGICKCORE_TIFF_DELEGATE)
cristy80975862009-09-25 14:34:31 +000094#define CCITTParam "-1"
95#else
96#define CCITTParam "0"
97#endif
cristy3ed852e2009-09-05 21:47:34 +000098
99/*
100 Forward declarations.
101*/
102static MagickBooleanType
cristy1e178e72011-08-28 19:44:34 +0000103 WritePS3Image(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +0000104
105/*
106%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
107% %
108% %
109% %
110% R e g i s t e r P S 3 I m a g e %
111% %
112% %
113% %
114%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115%
116% RegisterPS3Image() adds properties for the PS3 image format to the list of
117% supported formats. The properties include the image format tag, a method to
118% read and/or write the format, whether the format supports the saving of more
119% than one frame to the same file or blob, whether the format supports native
120% in-memory I/O, and a brief description of the format.
121%
122% The format of the RegisterPS3Image method is:
123%
cristybb503372010-05-27 20:51:26 +0000124% size_t RegisterPS3Image(void)
cristy3ed852e2009-09-05 21:47:34 +0000125%
126*/
cristybb503372010-05-27 20:51:26 +0000127ModuleExport size_t RegisterPS3Image(void)
cristy3ed852e2009-09-05 21:47:34 +0000128{
129 MagickInfo
130 *entry;
131
132 entry=SetMagickInfo("EPS3");
133 entry->encoder=(EncodeImageHandler *) WritePS3Image;
134 entry->description=ConstantString("Level III Encapsulated PostScript");
135 entry->module=ConstantString("PS3");
cristyffaf9782011-04-13 19:50:51 +0000136 entry->seekable_stream=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +0000137 (void) RegisterMagickInfo(entry);
138 entry=SetMagickInfo("PS3");
139 entry->encoder=(EncodeImageHandler *) WritePS3Image;
140 entry->description=ConstantString("Level III PostScript");
141 entry->module=ConstantString("PS3");
cristyffaf9782011-04-13 19:50:51 +0000142 entry->seekable_stream=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +0000143 (void) RegisterMagickInfo(entry);
144 return(MagickImageCoderSignature);
145}
146
147/*
148%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149% %
150% %
151% %
152% U n r e g i s t e r P S 3 I m a g e %
153% %
154% %
155% %
156%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
157%
158% UnregisterPS3Image() removes format registrations made by the PS3 module
159% from the list of supported formats.
160%
161% The format of the UnregisterPS3Image method is:
162%
163% UnregisterPS3Image(void)
164%
165*/
166ModuleExport void UnregisterPS3Image(void)
167{
168 (void) UnregisterMagickInfo("EPS3");
169 (void) UnregisterMagickInfo("PS3");
170}
171
172/*
173%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
174% %
175% %
176% %
177% W r i t e P S 3 I m a g e %
178% %
179% %
180% %
181%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
182%
183% WritePS3Image() translates an image to encapsulated Postscript Level III
184% for printing. If the supplied geometry is null, the image is centered on
185% the Postscript page. Otherwise, the image is positioned as specified by the
186% geometry.
187%
188% The format of the WritePS3Image method is:
189%
cristy1e178e72011-08-28 19:44:34 +0000190% MagickBooleanType WritePS3Image(const ImageInfo *image_info,
191% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000192%
193% A description of each parameter follows:
194%
195% o image_info: Specifies a pointer to a ImageInfo structure.
196%
197% o image: the image.
198%
cristy1e178e72011-08-28 19:44:34 +0000199% o exception: return any errors or warnings in this structure.
200%
cristy3ed852e2009-09-05 21:47:34 +0000201*/
202
cristy47b838c2009-09-19 16:09:30 +0000203static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
cristy018f07f2011-09-04 21:15:19 +0000204 Image *image,Image *inject_image,ExceptionInfo *exception)
cristy47b838c2009-09-19 16:09:30 +0000205{
cristy47b838c2009-09-19 16:09:30 +0000206 Image
cristy80975862009-09-25 14:34:31 +0000207 *group4_image;
cristy47b838c2009-09-19 16:09:30 +0000208
209 ImageInfo
210 *write_info;
211
cristy47b838c2009-09-19 16:09:30 +0000212 MagickBooleanType
213 status;
214
cristy80975862009-09-25 14:34:31 +0000215 size_t
216 length;
cristy47b838c2009-09-19 16:09:30 +0000217
218 unsigned char
cristy80975862009-09-25 14:34:31 +0000219 *group4;
cristy47b838c2009-09-19 16:09:30 +0000220
cristy42751fe2009-10-05 00:15:50 +0000221 status=MagickTrue;
cristy47b838c2009-09-19 16:09:30 +0000222 write_info=CloneImageInfo(image_info);
cristy80975862009-09-25 14:34:31 +0000223 (void) CopyMagickString(write_info->filename,"GROUP4:",MaxTextExtent);
224 (void) CopyMagickString(write_info->magick,"GROUP4",MaxTextExtent);
cristy018f07f2011-09-04 21:15:19 +0000225 group4_image=CloneImage(inject_image,0,0,MagickTrue,exception);
cristy80975862009-09-25 14:34:31 +0000226 if (group4_image == (Image *) NULL)
227 return(MagickFalse);
228 group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
cristy018f07f2011-09-04 21:15:19 +0000229 exception);
cristy80975862009-09-25 14:34:31 +0000230 group4_image=DestroyImage(group4_image);
231 if (group4 == (unsigned char *) NULL)
232 return(MagickFalse);
cristy47b838c2009-09-19 16:09:30 +0000233 write_info=DestroyImageInfo(write_info);
cristy80975862009-09-25 14:34:31 +0000234 if (WriteBlob(image,length,group4) != (ssize_t) length)
235 status=MagickFalse;
236 group4=(unsigned char *) RelinquishMagickMemory(group4);
237 return(status);
cristy47b838c2009-09-19 16:09:30 +0000238}
cristy80975862009-09-25 14:34:31 +0000239
cristy3ed852e2009-09-05 21:47:34 +0000240static MagickBooleanType SerializeImage(const ImageInfo *image_info,
cristyb64823d2013-06-30 20:58:24 +0000241 Image *image,MemoryInfo **pixel_info,size_t *length,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000242{
cristy3ed852e2009-09-05 21:47:34 +0000243 MagickBooleanType
244 status;
245
cristy4c08aed2011-07-01 19:47:50 +0000246 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000247 *p;
248
cristybb503372010-05-27 20:51:26 +0000249 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000250 x;
251
252 register unsigned char
253 *q;
254
cristy802d3642011-04-27 02:02:41 +0000255 ssize_t
256 y;
257
cristy3ed852e2009-09-05 21:47:34 +0000258 assert(image != (Image *) NULL);
259 assert(image->signature == MagickSignature);
260 if (image->debug != MagickFalse)
261 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
262 status=MagickTrue;
cristy802d3642011-04-27 02:02:41 +0000263 *length=(image->colorspace == CMYKColorspace ? 4 : 3)*(size_t)
264 image->columns*image->rows;
cristyb64823d2013-06-30 20:58:24 +0000265 *pixel_info=AcquireVirtualMemory(*length,sizeof(*q));
266 if (*pixel_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000267 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
cristyb64823d2013-06-30 20:58:24 +0000268 q=(unsigned char *) GetVirtualMemoryBlob(*pixel_info);
cristybb503372010-05-27 20:51:26 +0000269 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000270 {
cristyc82a27b2011-10-21 01:07:16 +0000271 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000272 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000273 break;
cristy3ed852e2009-09-05 21:47:34 +0000274 if (image->colorspace != CMYKColorspace)
cristybb503372010-05-27 20:51:26 +0000275 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000276 {
cristy4c08aed2011-07-01 19:47:50 +0000277 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
278 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
279 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
cristyed231572011-07-14 02:18:59 +0000280 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000281 }
282 else
cristybb503372010-05-27 20:51:26 +0000283 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000284 {
cristy4c08aed2011-07-01 19:47:50 +0000285 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
286 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
287 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
288 *q++=ScaleQuantumToChar(GetPixelBlack(image,p));
cristyed231572011-07-14 02:18:59 +0000289 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000290 }
291 if (image->previous == (Image *) NULL)
292 {
cristycee97112010-05-28 00:44:52 +0000293 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
cristy802d3642011-04-27 02:02:41 +0000294 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000295 if (status == MagickFalse)
296 break;
297 }
298 }
299 if (status == MagickFalse)
cristyb64823d2013-06-30 20:58:24 +0000300 *pixel_info=RelinquishVirtualMemory(*pixel_info);
cristy3ed852e2009-09-05 21:47:34 +0000301 return(status);
302}
303
304static MagickBooleanType SerializeImageChannel(const ImageInfo *image_info,
cristyb64823d2013-06-30 20:58:24 +0000305 Image *image,MemoryInfo **pixel_info,size_t *length,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000306{
cristy3ed852e2009-09-05 21:47:34 +0000307 MagickBooleanType
308 status;
309
cristy4c08aed2011-07-01 19:47:50 +0000310 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000311 *p;
312
cristybb503372010-05-27 20:51:26 +0000313 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000314 x;
315
316 register unsigned char
317 *q;
318
cristybb503372010-05-27 20:51:26 +0000319 size_t
cristy3ed852e2009-09-05 21:47:34 +0000320 pack,
321 padded_columns;
322
cristy802d3642011-04-27 02:02:41 +0000323 ssize_t
324 y;
325
326 unsigned char
327 code,
328 bit;
329
cristy3ed852e2009-09-05 21:47:34 +0000330 assert(image != (Image *) NULL);
331 assert(image->signature == MagickSignature);
332 if (image->debug != MagickFalse)
333 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
334 status=MagickTrue;
cristyc82a27b2011-10-21 01:07:16 +0000335 pack=IsImageMonochrome(image,exception) == MagickFalse ? 1UL : 8UL;
cristy3ed852e2009-09-05 21:47:34 +0000336 padded_columns=((image->columns+pack-1)/pack)*pack;
337 *length=(size_t) padded_columns*image->rows/pack;
cristyb64823d2013-06-30 20:58:24 +0000338 *pixel_info=AcquireVirtualMemory(*length,sizeof(*q));
339 if (*pixel_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000340 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
cristyb64823d2013-06-30 20:58:24 +0000341 q=(unsigned char *) GetVirtualMemoryBlob(*pixel_info);
cristybb503372010-05-27 20:51:26 +0000342 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000343 {
cristy018f07f2011-09-04 21:15:19 +0000344 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000345 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000346 break;
347 if (pack == 1)
cristybb503372010-05-27 20:51:26 +0000348 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000349 {
cristyd0323222013-04-07 16:13:21 +0000350 *q++=ScaleQuantumToChar(ClampToQuantum(GetPixelLuma(image,p)));
cristyed231572011-07-14 02:18:59 +0000351 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000352 }
353 else
354 {
355 code='\0';
cristybb503372010-05-27 20:51:26 +0000356 for (x=0; x < (ssize_t) padded_columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000357 {
358 bit=(unsigned char) 0x00;
cristybb503372010-05-27 20:51:26 +0000359 if (x < (ssize_t) image->columns)
cristyd0323222013-04-07 16:13:21 +0000360 bit=(unsigned char) (GetPixelLuma(image,p) == TransparentAlpha ?
361 0x01 : 0x00);
cristy3ed852e2009-09-05 21:47:34 +0000362 code=(code << 1)+bit;
363 if (((x+1) % pack) == 0)
364 {
365 *q++=code;
366 code='\0';
367 }
cristyed231572011-07-14 02:18:59 +0000368 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000369 }
370 }
cristycee97112010-05-28 00:44:52 +0000371 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
cristy802d3642011-04-27 02:02:41 +0000372 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000373 if (status == MagickFalse)
374 break;
375 }
376 if (status == MagickFalse)
cristyb64823d2013-06-30 20:58:24 +0000377 *pixel_info=RelinquishVirtualMemory(*pixel_info);
cristy3ed852e2009-09-05 21:47:34 +0000378 return(status);
379}
380
381static MagickBooleanType SerializeImageIndexes(const ImageInfo *image_info,
cristyb64823d2013-06-30 20:58:24 +0000382 Image *image,MemoryInfo **pixel_info,size_t *length,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000383{
cristy3ed852e2009-09-05 21:47:34 +0000384 MagickBooleanType
385 status;
386
cristy4c08aed2011-07-01 19:47:50 +0000387 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000388 *p;
389
cristybb503372010-05-27 20:51:26 +0000390 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000391 x;
392
393 register unsigned char
394 *q;
395
cristy802d3642011-04-27 02:02:41 +0000396 ssize_t
397 y;
398
cristy3ed852e2009-09-05 21:47:34 +0000399 assert(image != (Image *) NULL);
400 assert(image->signature == MagickSignature);
401 if (image->debug != MagickFalse)
402 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
403 status=MagickTrue;
404 *length=(size_t) image->columns*image->rows;
cristyb64823d2013-06-30 20:58:24 +0000405 *pixel_info=AcquireVirtualMemory(*length,sizeof(*q));
406 if (*pixel_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000407 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
cristyb64823d2013-06-30 20:58:24 +0000408 q=(unsigned char *) GetVirtualMemoryBlob(*pixel_info);
cristybb503372010-05-27 20:51:26 +0000409 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000410 {
cristy018f07f2011-09-04 21:15:19 +0000411 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000412 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000413 break;
cristybb503372010-05-27 20:51:26 +0000414 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +0000415 {
416 *q++=(unsigned char) GetPixelIndex(image,p);
cristyed231572011-07-14 02:18:59 +0000417 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +0000418 }
cristy3ed852e2009-09-05 21:47:34 +0000419 if (image->previous == (Image *) NULL)
420 {
cristycee97112010-05-28 00:44:52 +0000421 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
cristy802d3642011-04-27 02:02:41 +0000422 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000423 if (status == MagickFalse)
424 break;
425 }
426 }
427 if (status == MagickFalse)
cristyb64823d2013-06-30 20:58:24 +0000428 *pixel_info=RelinquishVirtualMemory(*pixel_info);
cristy3ed852e2009-09-05 21:47:34 +0000429 return(status);
430}
431
432static MagickBooleanType WritePS3MaskImage(const ImageInfo *image_info,
cristy018f07f2011-09-04 21:15:19 +0000433 Image *image,const CompressionType compression,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000434{
435 char
436 buffer[MaxTextExtent];
437
438 Image
439 *mask_image;
440
441 MagickBooleanType
442 status;
443
444 MagickOffsetType
445 offset,
446 start,
447 stop;
448
cristyb64823d2013-06-30 20:58:24 +0000449 MemoryInfo
450 *pixel_info;
451
cristybb503372010-05-27 20:51:26 +0000452 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000453 i;
454
455 size_t
456 length;
457
458 unsigned char
459 *pixels;
460
461 assert(image_info != (ImageInfo *) NULL);
462 assert(image_info->signature == MagickSignature);
463 assert(image != (Image *) NULL);
464 assert(image->signature == MagickSignature);
465 if (image->debug != MagickFalse)
466 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy8a46d822012-08-28 23:32:39 +0000467 assert(image->alpha_trait == BlendPixelTrait);
cristy3ed852e2009-09-05 21:47:34 +0000468 status=MagickTrue;
469 /*
470 Note BeginData DSC comment for update later.
471 */
472 start=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +0000473 (void) FormatLocaleString(buffer,MaxTextExtent,
cristy802d3642011-04-27 02:02:41 +0000474 "%%%%BeginData:%13ld %s Bytes\n",0L,compression == NoCompression ?
475 "ASCII" : "BINARY");
cristy3ed852e2009-09-05 21:47:34 +0000476 (void) WriteBlobString(image,buffer);
477 stop=TellBlob(image);
478 /*
479 Only lossless compressions for the mask.
480 */
481 switch (compression)
482 {
483 case NoCompression:
484 default:
485 {
cristyb51dff52011-05-19 16:55:47 +0000486 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +0000487 "currentfile %.20g %.20g "PS3_NoCompression" ByteStreamDecodeFilter\n",
488 (double) image->columns,(double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000489 break;
490 }
491 case FaxCompression:
492 case Group4Compression:
493 {
cristyb51dff52011-05-19 16:55:47 +0000494 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +0000495 "currentfile %.20g %.20g "PS3_FaxCompression" ByteStreamDecodeFilter\n",
496 (double) image->columns,(double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000497 break;
498 }
499 case LZWCompression:
500 {
cristyb51dff52011-05-19 16:55:47 +0000501 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +0000502 "currentfile %.20g %.20g "PS3_LZWCompression" ByteStreamDecodeFilter\n",
503 (double) image->columns,(double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000504 break;
505 }
506 case RLECompression:
507 {
cristyb51dff52011-05-19 16:55:47 +0000508 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +0000509 "currentfile %.20g %.20g "PS3_RLECompression" ByteStreamDecodeFilter\n",
510 (double) image->columns,(double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000511 break;
512 }
513 case ZipCompression:
514 {
cristyb51dff52011-05-19 16:55:47 +0000515 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +0000516 "currentfile %.20g %.20g "PS3_ZipCompression" ByteStreamDecodeFilter\n",
517 (double) image->columns,(double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000518 break;
519 }
520 }
521 (void) WriteBlobString(image,buffer);
522 (void) WriteBlobString(image,"/ReusableStreamDecode filter\n");
cristydbeca022012-01-11 17:07:34 +0000523 mask_image=SeparateImage(image,AlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +0000524 if (mask_image == (Image *) NULL)
cristyc82a27b2011-10-21 01:07:16 +0000525 ThrowWriterException(CoderError,exception->reason);
cristy018f07f2011-09-04 21:15:19 +0000526 (void) SetImageType(mask_image,BilevelType,exception);
527 (void) SetImageType(mask_image,PaletteType,exception);
cristy8a46d822012-08-28 23:32:39 +0000528 mask_image->alpha_trait=UndefinedPixelTrait;
cristy3ed852e2009-09-05 21:47:34 +0000529 pixels=(unsigned char *) NULL;
530 length=0;
531 switch (compression)
532 {
533 case NoCompression:
534 default:
535 {
cristyb64823d2013-06-30 20:58:24 +0000536 status=SerializeImageChannel(image_info,mask_image,&pixel_info,&length,
cristy018f07f2011-09-04 21:15:19 +0000537 exception);
cristy3ed852e2009-09-05 21:47:34 +0000538 if (status == MagickFalse)
539 break;
540 Ascii85Initialize(image);
cristyb64823d2013-06-30 20:58:24 +0000541 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
cristybb503372010-05-27 20:51:26 +0000542 for (i=0; i < (ssize_t) length; i++)
cristy3ed852e2009-09-05 21:47:34 +0000543 Ascii85Encode(image,pixels[i]);
544 Ascii85Flush(image);
cristyb64823d2013-06-30 20:58:24 +0000545 pixel_info=RelinquishVirtualMemory(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +0000546 break;
547 }
548 case FaxCompression:
549 case Group4Compression:
550 {
551 if ((compression == FaxCompression) ||
552 (LocaleCompare(CCITTParam,"0") == 0))
cristy018f07f2011-09-04 21:15:19 +0000553 status=HuffmanEncodeImage(image_info,image,mask_image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000554 else
cristy018f07f2011-09-04 21:15:19 +0000555 status=Huffman2DEncodeImage(image_info,image,mask_image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000556 break;
557 }
558 case LZWCompression:
559 {
cristyb64823d2013-06-30 20:58:24 +0000560 status=SerializeImageChannel(image_info,mask_image,&pixel_info,&length,
cristy018f07f2011-09-04 21:15:19 +0000561 exception);
cristy3ed852e2009-09-05 21:47:34 +0000562 if (status == MagickFalse)
563 break;
cristyb64823d2013-06-30 20:58:24 +0000564 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
cristy018f07f2011-09-04 21:15:19 +0000565 status=LZWEncodeImage(image,length,pixels,exception);
cristyb64823d2013-06-30 20:58:24 +0000566 pixel_info=RelinquishVirtualMemory(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +0000567 break;
568 }
569 case RLECompression:
570 {
cristyb64823d2013-06-30 20:58:24 +0000571 status=SerializeImageChannel(image_info,mask_image,&pixel_info,&length,
cristy018f07f2011-09-04 21:15:19 +0000572 exception);
cristy3ed852e2009-09-05 21:47:34 +0000573 if (status == MagickFalse)
574 break;
cristyb64823d2013-06-30 20:58:24 +0000575 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
cristy018f07f2011-09-04 21:15:19 +0000576 status=PackbitsEncodeImage(image,length,pixels,exception);
cristyb64823d2013-06-30 20:58:24 +0000577 pixel_info=RelinquishVirtualMemory(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +0000578 break;
579 }
580 case ZipCompression:
581 {
cristyb64823d2013-06-30 20:58:24 +0000582 status=SerializeImageChannel(image_info,mask_image,&pixel_info,&length,
cristy018f07f2011-09-04 21:15:19 +0000583 exception);
cristy3ed852e2009-09-05 21:47:34 +0000584 if (status == MagickFalse)
585 break;
cristyb64823d2013-06-30 20:58:24 +0000586 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
cristy018f07f2011-09-04 21:15:19 +0000587 status=ZLIBEncodeImage(image,length,pixels,exception);
cristyb64823d2013-06-30 20:58:24 +0000588 pixel_info=RelinquishVirtualMemory(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +0000589 break;
590 }
591 }
592 mask_image=DestroyImage(mask_image);
593 (void) WriteBlobByte(image,'\n');
594 length=(size_t) (TellBlob(image)-stop);
595 stop=TellBlob(image);
596 offset=SeekBlob(image,start,SEEK_SET);
597 if (offset < 0)
598 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
cristyb51dff52011-05-19 16:55:47 +0000599 (void) FormatLocaleString(buffer,MaxTextExtent,
cristyf2faecf2010-05-28 19:19:36 +0000600 "%%%%BeginData:%13ld %s Bytes\n",(long) length,
cristy3ed852e2009-09-05 21:47:34 +0000601 compression == NoCompression ? "ASCII" : "BINARY");
602 (void) WriteBlobString(image,buffer);
603 offset=SeekBlob(image,stop,SEEK_SET);
604 if (offset < 0)
605 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
606 (void) WriteBlobString(image,"%%EndData\n");
607 (void) WriteBlobString(image, "/mask_stream exch def\n");
608 return(status);
609}
610
cristy1e178e72011-08-28 19:44:34 +0000611static MagickBooleanType WritePS3Image(const ImageInfo *image_info,Image *image,
612 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000613{
614 static const char
615 *PostscriptProlog[]=
616 {
617 "/ByteStreamDecodeFilter",
618 "{",
619 " /z exch def",
620 " /r exch def",
621 " /c exch def",
622 " z "PS3_NoCompression" eq { /ASCII85Decode filter } if",
623 " z "PS3_FaxCompression" eq",
624 " {",
625 " <<",
626 " /K "CCITTParam,
627 " /Columns c",
628 " /Rows r",
629 " >>",
630 " /CCITTFaxDecode filter",
631 " } if",
632 " z "PS3_JPEGCompression" eq { /DCTDecode filter } if",
633 " z "PS3_LZWCompression" eq { /LZWDecode filter } if",
634 " z "PS3_RLECompression" eq { /RunLengthDecode filter } if",
635 " z "PS3_ZipCompression" eq { /FlateDecode filter } if",
636 "} bind def",
637 "",
638 "/DirectClassImageDict",
639 "{",
640 " colorspace "PS3_RGBColorspace" eq",
641 " {",
642 " /DeviceRGB setcolorspace",
643 " <<",
644 " /ImageType 1",
645 " /Width columns",
646 " /Height rows",
647 " /BitsPerComponent 8",
648 " /DataSource pixel_stream",
649 " /MultipleDataSources false",
650 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
651 " /Decode [0 1 0 1 0 1]",
652 " >>",
653 " }",
654 " {",
655 " /DeviceCMYK setcolorspace",
656 " <<",
657 " /ImageType 1",
658 " /Width columns",
659 " /Height rows",
660 " /BitsPerComponent 8",
661 " /DataSource pixel_stream",
662 " /MultipleDataSources false",
663 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
664 " /Decode",
665 " compression "PS3_JPEGCompression" eq",
666 " { [1 0 1 0 1 0 1 0] }",
667 " { [0 1 0 1 0 1 0 1] }",
668 " ifelse",
669 " >>",
670 " }",
671 " ifelse",
672 "} bind def",
673 "",
674 "/PseudoClassImageDict",
675 "{",
676 " % Colors in colormap image.",
677 " currentfile buffer readline pop",
678 " token pop /colors exch def pop",
679 " colors 0 eq",
680 " {",
681 " % Depth of grayscale image.",
682 " currentfile buffer readline pop",
683 " token pop /bits exch def pop",
684 " /DeviceGray setcolorspace",
685 " <<",
686 " /ImageType 1",
687 " /Width columns",
688 " /Height rows",
689 " /BitsPerComponent bits",
690 " /Decode [0 1]",
691 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
692 " /DataSource pixel_stream",
693 " >>",
694 " }",
695 " {",
696 " % RGB colormap.",
697 " /colormap colors 3 mul string def",
698 " compression "PS3_NoCompression" eq",
699 " { currentfile /ASCII85Decode filter colormap readstring pop pop }",
700 " { currentfile colormap readstring pop pop }",
701 " ifelse",
702 " [ /Indexed /DeviceRGB colors 1 sub colormap ] setcolorspace",
703 " <<",
704 " /ImageType 1",
705 " /Width columns",
706 " /Height rows",
707 " /BitsPerComponent 8",
708 " /Decode [0 255]",
709 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
710 " /DataSource pixel_stream",
711 " >>",
712 " }",
713 " ifelse",
714 "} bind def",
715 "",
716 "/NonMaskedImageDict",
717 "{",
718 " class "PS3_PseudoClass" eq",
719 " { PseudoClassImageDict }",
720 " { DirectClassImageDict }",
721 " ifelse",
722 "} bind def",
723 "",
724 "/MaskedImageDict",
725 "{",
726 " <<",
727 " /ImageType 3",
728 " /InterleaveType 3",
729 " /DataDict NonMaskedImageDict",
730 " /MaskDict",
731 " <<",
732 " /ImageType 1",
733 " /Width columns",
734 " /Height rows",
735 " /BitsPerComponent 1",
736 " /DataSource mask_stream",
737 " /MultipleDataSources false",
738 " /ImageMatrix [ columns 0 0 rows neg 0 rows]",
739 " /Decode [ 0 1 ]",
740 " >>",
741 " >>",
742 "} bind def",
743 "",
744 "/ClipImage",
745 "{} def",
746 "",
747 "/DisplayImage",
748 "{",
cristy2cde86a2012-07-28 23:19:42 +0000749 " gsave",
cristy3ed852e2009-09-05 21:47:34 +0000750 " /buffer 512 string def",
751 " % Translation.",
752 " currentfile buffer readline pop",
753 " token pop /x exch def",
754 " token pop /y exch def pop",
755 " x y translate",
756 " % Image size and font size.",
757 " currentfile buffer readline pop",
758 " token pop /x exch def",
759 " token pop /y exch def pop",
760 " currentfile buffer readline pop",
761 " token pop /pointsize exch def pop",
762 (char *) NULL
763 },
764 *PostscriptEpilog[]=
765 {
766 " x y scale",
767 " % Clipping path.",
768 " currentfile buffer readline pop",
769 " token pop /clipped exch def pop",
770 " % Showpage.",
771 " currentfile buffer readline pop",
772 " token pop /sp exch def pop",
773 " % Image pixel size.",
774 " currentfile buffer readline pop",
775 " token pop /columns exch def",
776 " token pop /rows exch def pop",
777 " % Colorspace (RGB/CMYK).",
778 " currentfile buffer readline pop",
779 " token pop /colorspace exch def pop",
780 " % Transparency.",
781 " currentfile buffer readline pop",
782 " token pop /alpha exch def pop",
783 " % Stencil mask?",
784 " currentfile buffer readline pop",
785 " token pop /stencil exch def pop",
786 " % Image class (direct/pseudo).",
787 " currentfile buffer readline pop",
788 " token pop /class exch def pop",
789 " % Compression type.",
790 " currentfile buffer readline pop",
791 " token pop /compression exch def pop",
792 " % Clip and render.",
793 " /pixel_stream currentfile columns rows compression ByteStreamDecodeFilter def",
794 " clipped { ClipImage } if",
795 " alpha stencil not and",
796 " { MaskedImageDict mask_stream resetfile }",
797 " { NonMaskedImageDict }",
798 " ifelse",
799 " stencil { 0 setgray imagemask } { image } ifelse",
cristy2cde86a2012-07-28 23:19:42 +0000800 " grestore",
cristy3ed852e2009-09-05 21:47:34 +0000801 " sp { showpage } if",
802 "} bind def",
803 (char *) NULL
804 };
805
806 char
807 buffer[MaxTextExtent],
808 date[MaxTextExtent],
809 **labels,
810 page_geometry[MaxTextExtent];
811
812 CompressionType
813 compression;
814
815 const char
816 *option,
817 **q,
818 *value;
819
820 double
821 pointsize;
822
823 GeometryInfo
824 geometry_info;
825
cristy3ed852e2009-09-05 21:47:34 +0000826 MagickBooleanType
827 status;
828
829 MagickOffsetType
830 offset,
831 scene,
832 start,
833 stop;
834
835 MagickStatusType
836 flags;
837
cristyb64823d2013-06-30 20:58:24 +0000838 MemoryInfo
839 *pixel_info;
840
cristy3ed852e2009-09-05 21:47:34 +0000841 PointInfo
842 delta,
843 resolution,
844 scale;
845
846 RectangleInfo
847 geometry,
848 media_info,
849 page_info;
850
cristybb503372010-05-27 20:51:26 +0000851 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000852 i;
853
854 SegmentInfo
855 bounds;
856
857 size_t
cristy802d3642011-04-27 02:02:41 +0000858 length,
859 page,
860 pixel,
861 text_size;
862
863 ssize_t
864 j;
cristy3ed852e2009-09-05 21:47:34 +0000865
866 time_t
867 timer;
868
869 unsigned char
870 *pixels;
871
cristy3ed852e2009-09-05 21:47:34 +0000872 /*
873 Open output image file.
874 */
875 assert(image_info != (const ImageInfo *) NULL);
876 assert(image_info->signature == MagickSignature);
877 assert(image != (Image *) NULL);
878 assert(image->signature == MagickSignature);
879 if (image->debug != MagickFalse)
880 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +0000881 assert(exception != (ExceptionInfo *) NULL);
882 assert(exception->signature == MagickSignature);
cristy1e178e72011-08-28 19:44:34 +0000883 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000884 if (status == MagickFalse)
885 return(MagickFalse);
886 compression=image->compression;
887 if (image_info->compression != UndefinedCompression)
888 compression=image_info->compression;
889 switch (compression)
890 {
891 case FaxCompression:
892 case Group4Compression:
893 {
cristy1e178e72011-08-28 19:44:34 +0000894 if ((IsImageMonochrome(image,exception) == MagickFalse) ||
cristy8a46d822012-08-28 23:32:39 +0000895 (image->alpha_trait == BlendPixelTrait))
cristy3ed852e2009-09-05 21:47:34 +0000896 compression=RLECompression;
897 break;
898 }
899#if !defined(MAGICKCORE_JPEG_DELEGATE)
900 case JPEGCompression:
901 {
902 compression=RLECompression;
cristy1e178e72011-08-28 19:44:34 +0000903 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +0000904 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
905 image->filename);
906 break;
907 }
908#endif
909#if !defined(MAGICKCORE_ZLIB_DELEGATE)
910 case ZipCompression:
911 {
912 compression=RLECompression;
cristy1e178e72011-08-28 19:44:34 +0000913 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +0000914 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZLIB)",
915 image->filename);
916 break;
917 }
918#endif
919 default:
920 break;
921 }
922 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
923 page=0;
924 scene=0;
925 do
926 {
927 /*
928 Scale relative to dots-per-inch.
929 */
930 delta.x=DefaultResolution;
931 delta.y=DefaultResolution;
cristy2a11bef2011-10-28 18:33:11 +0000932 resolution.x=image->resolution.x;
933 resolution.y=image->resolution.y;
cristy3ed852e2009-09-05 21:47:34 +0000934 if ((resolution.x == 0.0) || (resolution.y == 0.0))
935 {
936 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
937 resolution.x=geometry_info.rho;
938 resolution.y=geometry_info.sigma;
939 if ((flags & SigmaValue) == 0)
940 resolution.y=resolution.x;
941 }
942 if (image_info->density != (char *) NULL)
943 {
944 flags=ParseGeometry(image_info->density,&geometry_info);
945 resolution.x=geometry_info.rho;
946 resolution.y=geometry_info.sigma;
947 if ((flags & SigmaValue) == 0)
948 resolution.y=resolution.x;
949 }
950 if (image->units == PixelsPerCentimeterResolution)
951 {
cristybb503372010-05-27 20:51:26 +0000952 resolution.x=(size_t) (100.0*2.54*resolution.x+0.5)/100.0;
953 resolution.y=(size_t) (100.0*2.54*resolution.y+0.5)/100.0;
cristy3ed852e2009-09-05 21:47:34 +0000954 }
955 SetGeometry(image,&geometry);
cristyb51dff52011-05-19 16:55:47 +0000956 (void) FormatLocaleString(page_geometry,MaxTextExtent,"%.20gx%.20g",
cristye8c25f92010-06-03 00:53:06 +0000957 (double) image->columns,(double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000958 if (image_info->page != (char *) NULL)
959 (void) CopyMagickString(page_geometry,image_info->page,MaxTextExtent);
960 else
961 if ((image->page.width != 0) && (image->page.height != 0))
cristyb51dff52011-05-19 16:55:47 +0000962 (void) FormatLocaleString(page_geometry,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +0000963 "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,(double)
964 image->page.height,(double) image->page.x,(double) image->page.y);
cristy3ed852e2009-09-05 21:47:34 +0000965 else
966 if ((image->gravity != UndefinedGravity) &&
967 (LocaleCompare(image_info->magick,"PS") == 0))
968 (void) CopyMagickString(page_geometry,PSPageGeometry,MaxTextExtent);
969 (void) ConcatenateMagickString(page_geometry,">",MaxTextExtent);
970 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
971 &geometry.width,&geometry.height);
972 scale.x=(double) (geometry.width*delta.x)/resolution.x;
cristybb503372010-05-27 20:51:26 +0000973 geometry.width=(size_t) floor(scale.x+0.5);
cristy3ed852e2009-09-05 21:47:34 +0000974 scale.y=(double) (geometry.height*delta.y)/resolution.y;
cristybb503372010-05-27 20:51:26 +0000975 geometry.height=(size_t) floor(scale.y+0.5);
cristy3ed852e2009-09-05 21:47:34 +0000976 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
cristy1e178e72011-08-28 19:44:34 +0000977 (void) ParseGravityGeometry(image,page_geometry,&page_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000978 if (image->gravity != UndefinedGravity)
979 {
980 geometry.x=(-page_info.x);
cristybb503372010-05-27 20:51:26 +0000981 geometry.y=(ssize_t) (media_info.height+page_info.y-image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000982 }
983 pointsize=12.0;
984 if (image_info->pointsize != 0.0)
985 pointsize=image_info->pointsize;
986 text_size=0;
cristyd15e6592011-10-15 00:13:06 +0000987 value=GetImageProperty(image,"label",exception);
cristy3ed852e2009-09-05 21:47:34 +0000988 if (value != (const char *) NULL)
cristybb503372010-05-27 20:51:26 +0000989 text_size=(size_t) (MultilineCensus(value)*pointsize+12);
cristy3ed852e2009-09-05 21:47:34 +0000990 page++;
991 if (page == 1)
992 {
993 /*
994 Postscript header on the first page.
995 */
996 if (LocaleCompare(image_info->magick,"PS3") == 0)
997 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0\n",MaxTextExtent);
998 else
999 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0 EPSF-3.0\n",
1000 MaxTextExtent);
1001 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001002 (void) FormatLocaleString(buffer,MaxTextExtent,
cristy3ed852e2009-09-05 21:47:34 +00001003 "%%%%Creator: ImageMagick %s\n",MagickLibVersionText);
1004 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001005 (void) FormatLocaleString(buffer,MaxTextExtent,"%%%%Title: %s\n",
cristy3ed852e2009-09-05 21:47:34 +00001006 image->filename);
1007 (void) WriteBlobString(image,buffer);
1008 timer=time((time_t *) NULL);
1009 (void) FormatMagickTime(timer,MaxTextExtent,date);
cristyb51dff52011-05-19 16:55:47 +00001010 (void) FormatLocaleString(buffer,MaxTextExtent,
cristy3ed852e2009-09-05 21:47:34 +00001011 "%%%%CreationDate: %s\n",date);
1012 (void) WriteBlobString(image,buffer);
1013 bounds.x1=(double) geometry.x;
1014 bounds.y1=(double) geometry.y;
1015 bounds.x2=(double) geometry.x+scale.x;
1016 bounds.y2=(double) geometry.y+scale.y+text_size;
1017 if ((image_info->adjoin != MagickFalse) &&
1018 (GetNextImageInList(image) != (Image *) NULL))
1019 {
1020 (void) WriteBlobString(image,"%%BoundingBox: (atend)\n");
1021 (void) WriteBlobString(image,"%%HiResBoundingBox: (atend)\n");
1022 }
1023 else
1024 {
cristyb51dff52011-05-19 16:55:47 +00001025 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye4cc1a12012-09-03 23:04:46 +00001026 "%%%%BoundingBox: %g %g %g %g\n",ceil(bounds.x1-0.5),
cristy8071c472012-09-24 12:41:06 +00001027 ceil(bounds.y1-0.5),floor(bounds.x2+0.5),floor(bounds.y2+0.5));
cristy3ed852e2009-09-05 21:47:34 +00001028 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001029 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00001030 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,
cristy8cd5b312010-01-07 01:10:24 +00001031 bounds.y1,bounds.x2,bounds.y2);
cristy3ed852e2009-09-05 21:47:34 +00001032 (void) WriteBlobString(image,buffer);
1033 if (image->colorspace == CMYKColorspace)
1034 (void) WriteBlobString(image,
1035 "%%DocumentProcessColors: Cyan Magenta Yellow Black\n");
1036 else
cristy1e178e72011-08-28 19:44:34 +00001037 if (IsImageGray(image,exception) != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001038 (void) WriteBlobString(image,
1039 "%%DocumentProcessColors: Black\n");
1040 }
1041 /*
1042 Font resources
1043 */
cristyd15e6592011-10-15 00:13:06 +00001044 value=GetImageProperty(image,"label",exception);
cristy3ed852e2009-09-05 21:47:34 +00001045 if (value != (const char *) NULL)
1046 (void) WriteBlobString(image,
1047 "%%DocumentNeededResources: font Helvetica\n");
1048 (void) WriteBlobString(image,"%%LanguageLevel: 3\n");
1049 /*
1050 Pages, orientation and order.
1051 */
1052 if (LocaleCompare(image_info->magick,"PS3") != 0)
1053 (void) WriteBlobString(image,"%%Pages: 1\n");
1054 else
1055 {
1056 (void) WriteBlobString(image,"%%Orientation: Portrait\n");
1057 (void) WriteBlobString(image,"%%PageOrder: Ascend\n");
1058 if (image_info->adjoin == MagickFalse)
1059 (void) CopyMagickString(buffer,"%%Pages: 1\n",MaxTextExtent);
1060 else
cristyb51dff52011-05-19 16:55:47 +00001061 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00001062 "%%%%Pages: %.20g\n",(double) GetImageListLength(image));
cristy3ed852e2009-09-05 21:47:34 +00001063 (void) WriteBlobString(image,buffer);
1064 }
1065 (void) WriteBlobString(image,"%%EndComments\n");
1066 /*
1067 The static postscript procedures prolog.
1068 */
1069 (void)WriteBlobString(image,"%%BeginProlog\n");
1070 for (q=PostscriptProlog; *q; q++)
1071 {
1072 (void) WriteBlobString(image,*q);
1073 (void) WriteBlobByte(image,'\n');
1074 }
1075 /*
1076 One label line for each line in label string.
1077 */
cristyd15e6592011-10-15 00:13:06 +00001078 value=GetImageProperty(image,"label",exception);
cristy3ed852e2009-09-05 21:47:34 +00001079 if (value != (const char *) NULL)
1080 {
1081 (void) WriteBlobString(image,"\n %% Labels.\n /Helvetica "
1082 " findfont pointsize scalefont setfont\n");
cristybb503372010-05-27 20:51:26 +00001083 for (i=(ssize_t) MultilineCensus(value)-1; i >= 0; i--)
cristy3ed852e2009-09-05 21:47:34 +00001084 {
1085 (void) WriteBlobString(image,
1086 " currentfile buffer readline pop token pop\n");
cristyb51dff52011-05-19 16:55:47 +00001087 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00001088 " 0 y %g add moveto show pop\n",i*pointsize+12);
cristy3ed852e2009-09-05 21:47:34 +00001089 (void) WriteBlobString(image,buffer);
1090 }
1091 }
1092 /*
1093 The static postscript procedures epilog.
1094 */
1095 for (q=PostscriptEpilog; *q; q++)
1096 {
1097 (void) WriteBlobString(image,*q);
1098 (void) WriteBlobByte(image,'\n');
1099 }
1100 (void)WriteBlobString(image,"%%EndProlog\n");
1101 }
cristyb51dff52011-05-19 16:55:47 +00001102 (void) FormatLocaleString(buffer,MaxTextExtent,"%%%%Page: 1 %.20g\n",
cristye8c25f92010-06-03 00:53:06 +00001103 (double) page);
cristy3ed852e2009-09-05 21:47:34 +00001104 (void) WriteBlobString(image,buffer);
1105 /*
1106 Page bounding box.
1107 */
cristyb51dff52011-05-19 16:55:47 +00001108 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00001109 "%%%%PageBoundingBox: %.20g %.20g %.20g %.20g\n",(double) geometry.x,
1110 (double) geometry.y,geometry.x+(double) geometry.width,geometry.y+
1111 (double) (geometry.height+text_size));
cristy3ed852e2009-09-05 21:47:34 +00001112 (void) WriteBlobString(image,buffer);
1113 /*
1114 Page process colors if not RGB.
1115 */
1116 if (image->colorspace == CMYKColorspace)
1117 (void) WriteBlobString(image,
1118 "%%PageProcessColors: Cyan Magenta Yellow Black\n");
1119 else
cristy1e178e72011-08-28 19:44:34 +00001120 if (IsImageGray(image,exception) != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001121 (void) WriteBlobString(image,"%%PageProcessColors: Black\n");
1122 /*
1123 Adjust document bounding box to bound page bounding box.
1124 */
1125 if ((double) geometry.x < bounds.x1)
1126 bounds.x1=(double) geometry.x;
1127 if ((double) geometry.y < bounds.y1)
1128 bounds.y1=(double) geometry.y;
1129 if ((double) (geometry.x+scale.x) > bounds.x2)
1130 bounds.x2=(double) geometry.x+scale.x;
1131 if ((double) (geometry.y+scale.y+text_size) > bounds.y2)
1132 bounds.y2=(double) geometry.y+scale.y+text_size;
1133 /*
1134 Page font resource if there's a label.
1135 */
cristyd15e6592011-10-15 00:13:06 +00001136 value=GetImageProperty(image,"label",exception);
cristy3ed852e2009-09-05 21:47:34 +00001137 if (value != (const char *) NULL)
1138 (void) WriteBlobString(image,"%%PageResources: font Helvetica\n");
1139 /*
1140 PS clipping path from Photoshop clipping path.
1141 */
cristy883fde12013-04-08 00:50:13 +00001142 if ((image->read_mask != MagickFalse) ||
cristy10a6c612012-01-29 21:41:05 +00001143 (LocaleNCompare("8BIM:",image->magick_filename,5) != 0))
cristy3ed852e2009-09-05 21:47:34 +00001144 (void) WriteBlobString(image,"/ClipImage {} def\n");
1145 else
1146 {
1147 const char
1148 *value;
1149
cristy10a6c612012-01-29 21:41:05 +00001150 value=GetImageProperty(image,image->magick_filename,exception);
cristy3ed852e2009-09-05 21:47:34 +00001151 if (value == (const char *) NULL)
1152 return(MagickFalse);
1153 (void) WriteBlobString(image,value);
1154 (void) WriteBlobByte(image,'\n');
1155 }
1156 /*
1157 Push a dictionary for our own def's if this an EPS.
1158 */
1159 if (LocaleCompare(image_info->magick,"PS3") != 0)
1160 (void) WriteBlobString(image,"userdict begin\n");
1161 /*
1162 Image mask.
1163 */
cristy8a46d822012-08-28 23:32:39 +00001164 if ((image->alpha_trait == BlendPixelTrait) &&
cristy018f07f2011-09-04 21:15:19 +00001165 (WritePS3MaskImage(image_info,image,compression,exception) == MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00001166 {
1167 (void) CloseBlob(image);
1168 return(MagickFalse);
1169 }
1170 /*
1171 Remember position of BeginData comment so we can update it.
1172 */
1173 start=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001174 (void) FormatLocaleString(buffer,MaxTextExtent,
cristy3ed852e2009-09-05 21:47:34 +00001175 "%%%%BeginData:%13ld %s Bytes\n",0L,
1176 compression == NoCompression ? "ASCII" : "BINARY");
1177 (void) WriteBlobString(image,buffer);
1178 stop=TellBlob(image);
1179 (void) WriteBlobString(image,"DisplayImage\n");
1180 /*
1181 Translate, scale, and font point size.
1182 */
cristyb51dff52011-05-19 16:55:47 +00001183 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n%g %g\n%g\n",
cristye8c25f92010-06-03 00:53:06 +00001184 (double) geometry.x,(double) geometry.y,scale.x,scale.y,pointsize);
cristy3ed852e2009-09-05 21:47:34 +00001185 (void) WriteBlobString(image,buffer);
1186 /*
1187 Output labels.
1188 */
1189 labels=(char **) NULL;
cristyd15e6592011-10-15 00:13:06 +00001190 value=GetImageProperty(image,"label",exception);
cristy3ed852e2009-09-05 21:47:34 +00001191 if (value != (const char *) NULL)
1192 labels=StringToList(value);
1193 if (labels != (char **) NULL)
1194 {
1195 for (i=0; labels[i] != (char *) NULL; i++)
1196 {
1197 if (compression != NoCompression)
1198 {
1199 for (j=0; labels[i][j] != '\0'; j++)
1200 (void) WriteBlobByte(image,(unsigned char) labels[i][j]);
1201 (void) WriteBlobByte(image,'\n');
1202 }
1203 else
1204 {
1205 (void) WriteBlobString(image,"<~");
1206 Ascii85Initialize(image);
1207 for (j=0; labels[i][j] != '\0'; j++)
1208 Ascii85Encode(image,(unsigned char) labels[i][j]);
1209 Ascii85Flush(image);
1210 }
1211 labels[i]=DestroyString(labels[i]);
1212 }
1213 labels=(char **) RelinquishMagickMemory(labels);
1214 }
1215 /*
1216 Photoshop clipping path active?
1217 */
cristy883fde12013-04-08 00:50:13 +00001218 if ((image->read_mask != MagickFalse) &&
cristy10a6c612012-01-29 21:41:05 +00001219 (LocaleNCompare("8BIM:",image->magick_filename,5) == 0))
cristy3ed852e2009-09-05 21:47:34 +00001220 (void) WriteBlobString(image,"true\n");
1221 else
1222 (void) WriteBlobString(image,"false\n");
1223 /*
1224 Showpage for non-EPS.
1225 */
1226 (void) WriteBlobString(image, LocaleCompare(image_info->magick,"PS3") == 0 ?
1227 "true\n" : "false\n");
1228 /*
1229 Image columns, rows, and color space.
1230 */
cristyb51dff52011-05-19 16:55:47 +00001231 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n%s\n",
cristy802d3642011-04-27 02:02:41 +00001232 (double) image->columns,(double) image->rows,image->colorspace ==
1233 CMYKColorspace ? PS3_CMYKColorspace : PS3_RGBColorspace);
cristy3ed852e2009-09-05 21:47:34 +00001234 (void) WriteBlobString(image,buffer);
1235 /*
1236 Masked image?
1237 */
cristy8a46d822012-08-28 23:32:39 +00001238 (void) WriteBlobString(image,image->alpha_trait == BlendPixelTrait ?
cristy3ed852e2009-09-05 21:47:34 +00001239 "true\n" : "false\n");
1240 /*
1241 Render with imagemask operator?
1242 */
cristy092ec8d2013-04-26 13:46:22 +00001243 option=GetImageOption(image_info,"ps3:imagemask");
cristy3ed852e2009-09-05 21:47:34 +00001244 (void) WriteBlobString(image,((option != (const char *) NULL) &&
cristy1e178e72011-08-28 19:44:34 +00001245 (IsImageMonochrome(image,exception) != MagickFalse)) ?
cristy3ed852e2009-09-05 21:47:34 +00001246 "true\n" : "false\n");
1247 /*
1248 Output pixel data.
1249 */
1250 pixels=(unsigned char *) NULL;
1251 length=0;
1252 if ((image_info->type != TrueColorType) &&
1253 (image_info->type != TrueColorMatteType) &&
1254 (image_info->type != ColorSeparationType) &&
1255 (image_info->type != ColorSeparationMatteType) &&
1256 (image->colorspace != CMYKColorspace) &&
cristy1e178e72011-08-28 19:44:34 +00001257 ((IsImageGray(image,exception) != MagickFalse) ||
1258 (IsImageMonochrome(image,exception) != MagickFalse)))
cristy3ed852e2009-09-05 21:47:34 +00001259 {
1260 /*
1261 Gray images.
1262 */
1263 (void) WriteBlobString(image,PS3_PseudoClass"\n");
1264 switch (compression)
1265 {
1266 case NoCompression:
1267 default:
1268 {
1269 (void) WriteBlobString(image,PS3_NoCompression"\n");
1270 break;
1271 }
1272 case FaxCompression:
1273 case Group4Compression:
1274 {
1275 (void) WriteBlobString(image,PS3_FaxCompression"\n");
1276 break;
1277 }
1278 case JPEGCompression:
1279 {
1280 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1281 break;
1282 }
1283 case LZWCompression:
1284 {
1285 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1286 break;
1287 }
1288 case RLECompression:
1289 {
1290 (void) WriteBlobString(image,PS3_RLECompression"\n");
1291 break;
1292 }
1293 case ZipCompression:
1294 {
1295 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1296 break;
1297 }
1298 }
1299 /*
1300 Number of colors -- 0 for single component non-color mapped data.
1301 */
1302 (void) WriteBlobString(image,"0\n");
1303 /*
1304 1 bit or 8 bit components?
1305 */
cristyb51dff52011-05-19 16:55:47 +00001306 (void) FormatLocaleString(buffer,MaxTextExtent,"%d\n",
cristy1e178e72011-08-28 19:44:34 +00001307 IsImageMonochrome(image,exception) != MagickFalse ? 1 : 8);
cristy3ed852e2009-09-05 21:47:34 +00001308 (void) WriteBlobString(image,buffer);
1309 /*
1310 Image data.
1311 */
1312 if (compression == JPEGCompression)
cristy1e178e72011-08-28 19:44:34 +00001313 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
cristy3ed852e2009-09-05 21:47:34 +00001314 else
1315 if ((compression == FaxCompression) ||
1316 (compression == Group4Compression))
1317 {
1318 if (LocaleCompare(CCITTParam,"0") == 0)
cristy018f07f2011-09-04 21:15:19 +00001319 status=HuffmanEncodeImage(image_info,image,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001320 else
cristy018f07f2011-09-04 21:15:19 +00001321 status=Huffman2DEncodeImage(image_info,image,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001322 }
1323 else
1324 {
cristyb64823d2013-06-30 20:58:24 +00001325 status=SerializeImageChannel(image_info,image,&pixel_info,&length,
cristy018f07f2011-09-04 21:15:19 +00001326 exception);
cristy3ed852e2009-09-05 21:47:34 +00001327 if (status == MagickFalse)
1328 {
1329 (void) CloseBlob(image);
1330 return(MagickFalse);
1331 }
cristyb64823d2013-06-30 20:58:24 +00001332 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +00001333 switch (compression)
1334 {
1335 case NoCompression:
1336 default:
1337 {
1338 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00001339 for (i=0; i < (ssize_t) length; i++)
cristy3ed852e2009-09-05 21:47:34 +00001340 Ascii85Encode(image,pixels[i]);
1341 Ascii85Flush(image);
1342 status=MagickTrue;
1343 break;
1344 }
1345 case LZWCompression:
1346 {
cristy018f07f2011-09-04 21:15:19 +00001347 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001348 break;
1349 }
1350 case RLECompression:
1351 {
cristy018f07f2011-09-04 21:15:19 +00001352 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001353 break;
1354 }
1355 case ZipCompression:
1356 {
cristy018f07f2011-09-04 21:15:19 +00001357 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001358 break;
1359 }
1360 }
cristyb64823d2013-06-30 20:58:24 +00001361 pixel_info=RelinquishVirtualMemory(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +00001362 }
1363 }
1364 else
1365 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1366 (compression == JPEGCompression))
1367 {
1368 /*
1369 Truecolor image.
1370 */
1371 (void) WriteBlobString(image,PS3_DirectClass"\n");
1372 switch (compression)
1373 {
1374 case NoCompression:
1375 default:
1376 {
1377 (void) WriteBlobString(image,PS3_NoCompression"\n");
1378 break;
1379 }
1380 case RLECompression:
1381 {
1382 (void) WriteBlobString(image,PS3_RLECompression"\n");
1383 break;
1384 }
1385 case JPEGCompression:
1386 {
1387 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1388 break;
1389 }
1390 case LZWCompression:
1391 {
1392 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1393 break;
1394 }
1395 case ZipCompression:
1396 {
1397 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1398 break;
1399 }
1400 }
1401 /*
1402 Image data.
1403 */
1404 if (compression == JPEGCompression)
cristy1e178e72011-08-28 19:44:34 +00001405 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
cristy3ed852e2009-09-05 21:47:34 +00001406 else
1407 {
1408 /*
1409 Stream based compressions.
1410 */
cristyb64823d2013-06-30 20:58:24 +00001411 status=SerializeImage(image_info,image,&pixel_info,&length,
1412 exception);
cristy3ed852e2009-09-05 21:47:34 +00001413 if (status == MagickFalse)
1414 {
1415 (void) CloseBlob(image);
1416 return(MagickFalse);
1417 }
cristyb64823d2013-06-30 20:58:24 +00001418 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +00001419 switch (compression)
1420 {
1421 case NoCompression:
1422 default:
1423 {
1424 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00001425 for (i=0; i < (ssize_t) length; i++)
cristy3ed852e2009-09-05 21:47:34 +00001426 Ascii85Encode(image,pixels[i]);
1427 Ascii85Flush(image);
1428 status=MagickTrue;
1429 break;
1430 }
1431 case RLECompression:
1432 {
cristy018f07f2011-09-04 21:15:19 +00001433 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001434 break;
1435 }
1436 case LZWCompression:
1437 {
cristy018f07f2011-09-04 21:15:19 +00001438 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001439 break;
1440 }
1441 case ZipCompression:
1442 {
cristy018f07f2011-09-04 21:15:19 +00001443 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001444 break;
1445 }
1446 }
cristyb64823d2013-06-30 20:58:24 +00001447 pixel_info=RelinquishVirtualMemory(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +00001448 }
1449 }
1450 else
1451 {
1452 /*
1453 Colormapped images.
1454 */
1455 (void) WriteBlobString(image,PS3_PseudoClass"\n");
1456 switch (compression)
1457 {
1458 case NoCompression:
1459 default:
1460 {
1461 (void) WriteBlobString(image,PS3_NoCompression"\n");
1462 break;
1463 }
1464 case JPEGCompression:
1465 {
1466 (void) WriteBlobString(image,PS3_JPEGCompression"\n");
1467 break;
1468 }
1469 case RLECompression:
1470 {
1471 (void) WriteBlobString(image,PS3_RLECompression"\n");
1472 break;
1473 }
1474 case LZWCompression:
1475 {
1476 (void) WriteBlobString(image,PS3_LZWCompression"\n");
1477 break;
1478 }
1479 case ZipCompression:
1480 {
1481 (void) WriteBlobString(image,PS3_ZipCompression"\n");
1482 break;
1483 }
1484 }
1485 /*
1486 Number of colors in color map.
1487 */
cristyb51dff52011-05-19 16:55:47 +00001488 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",
cristye8c25f92010-06-03 00:53:06 +00001489 (double) image->colors);
cristy3ed852e2009-09-05 21:47:34 +00001490 (void) WriteBlobString(image,buffer);
1491 /*
1492 Color map - uncompressed.
1493 */
1494 if ((compression != NoCompression) &&
1495 (compression != UndefinedCompression))
1496 {
cristybb503372010-05-27 20:51:26 +00001497 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001498 {
1499 pixel=ScaleQuantumToChar(image->colormap[i].red);
1500 (void) WriteBlobByte(image,(unsigned char) pixel);
1501 pixel=ScaleQuantumToChar(image->colormap[i].green);
1502 (void) WriteBlobByte(image,(unsigned char) pixel);
1503 pixel=ScaleQuantumToChar(image->colormap[i].blue);
1504 (void) WriteBlobByte(image,(unsigned char) pixel);
1505 }
1506 }
1507 else
1508 {
1509 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00001510 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001511 {
1512 pixel=ScaleQuantumToChar(image->colormap[i].red);
1513 Ascii85Encode(image,(unsigned char) pixel);
1514 pixel=ScaleQuantumToChar(image->colormap[i].green);
1515 Ascii85Encode(image,(unsigned char) pixel);
1516 pixel=ScaleQuantumToChar(image->colormap[i].blue);
1517 Ascii85Encode(image,(unsigned char) pixel);
1518 }
1519 Ascii85Flush(image);
1520 }
cristyb64823d2013-06-30 20:58:24 +00001521 status=SerializeImageIndexes(image_info,image,&pixel_info,&length,
cristy018f07f2011-09-04 21:15:19 +00001522 exception);
cristy3ed852e2009-09-05 21:47:34 +00001523 if (status == MagickFalse)
1524 {
1525 (void) CloseBlob(image);
1526 return(MagickFalse);
1527 }
cristyb64823d2013-06-30 20:58:24 +00001528 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +00001529 switch (compression)
1530 {
1531 case NoCompression:
1532 default:
1533 {
1534 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00001535 for (i=0; i < (ssize_t) length; i++)
cristy3ed852e2009-09-05 21:47:34 +00001536 Ascii85Encode(image,pixels[i]);
1537 Ascii85Flush(image);
1538 status=MagickTrue;
1539 break;
1540 }
1541 case JPEGCompression:
1542 {
cristy1e178e72011-08-28 19:44:34 +00001543 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
cristy3ed852e2009-09-05 21:47:34 +00001544 break;
1545 }
1546 case RLECompression:
1547 {
cristy018f07f2011-09-04 21:15:19 +00001548 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001549 break;
1550 }
1551 case LZWCompression:
1552 {
cristy018f07f2011-09-04 21:15:19 +00001553 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001554 break;
1555 }
1556 case ZipCompression:
1557 {
cristy018f07f2011-09-04 21:15:19 +00001558 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001559 break;
1560 }
1561 }
cristyb64823d2013-06-30 20:58:24 +00001562 pixel_info=RelinquishVirtualMemory(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +00001563 }
1564 (void) WriteBlobByte(image,'\n');
1565 if (status == MagickFalse)
1566 {
1567 (void) CloseBlob(image);
1568 return(MagickFalse);
1569 }
1570 /*
1571 Update BeginData now that we know the data size.
1572 */
1573 length=(size_t) (TellBlob(image)-stop);
1574 stop=TellBlob(image);
1575 offset=SeekBlob(image,start,SEEK_SET);
1576 if (offset < 0)
1577 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
cristyb51dff52011-05-19 16:55:47 +00001578 (void) FormatLocaleString(buffer,MaxTextExtent,
cristyf2faecf2010-05-28 19:19:36 +00001579 "%%%%BeginData:%13ld %s Bytes\n",(long) length,
cristy3ed852e2009-09-05 21:47:34 +00001580 compression == NoCompression ? "ASCII" : "BINARY");
1581 (void) WriteBlobString(image,buffer);
1582 offset=SeekBlob(image,stop,SEEK_SET);
1583 (void) WriteBlobString(image,"%%EndData\n");
1584 /*
1585 End private dictionary if this an EPS.
1586 */
1587 if (LocaleCompare(image_info->magick,"PS3") != 0)
1588 (void) WriteBlobString(image,"end\n");
1589 (void) WriteBlobString(image,"%%PageTrailer\n");
1590 if (GetNextImageInList(image) == (Image *) NULL)
1591 break;
1592 image=SyncNextImageInList(image);
1593 status=SetImageProgress(image,SaveImagesTag,scene++,
1594 GetImageListLength(image));
1595 if (status == MagickFalse)
1596 break;
1597 } while (image_info->adjoin != MagickFalse);
1598 (void) WriteBlobString(image,"%%Trailer\n");
1599 if (page > 1)
1600 {
cristyb51dff52011-05-19 16:55:47 +00001601 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye4cc1a12012-09-03 23:04:46 +00001602 "%%%%BoundingBox: %g %g %g %g\n",ceil(bounds.x1-0.5),
cristy8071c472012-09-24 12:41:06 +00001603 ceil(bounds.y1-0.5),floor(bounds.x2+0.5),floor(bounds.y2+0.5));
cristy3ed852e2009-09-05 21:47:34 +00001604 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001605 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye4cc1a12012-09-03 23:04:46 +00001606 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,bounds.y1,bounds.x2,
1607 bounds.y2);
cristy3ed852e2009-09-05 21:47:34 +00001608 (void) WriteBlobString(image,buffer);
1609 }
1610 (void) WriteBlobString(image,"%%EOF\n");
1611 (void) CloseBlob(image);
1612 return(MagickTrue);
1613}