blob: 53d6fad0a9ddf79a0f444d51b34d8f4e310d624a [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% PPPP SSSSS 22222 %
7% P P SS 22 %
8% PPPP SSS 222 %
9% P SS 22 %
10% P SSSSS 22222 %
11% %
12% %
13% Write Postscript Level II Format %
14% %
15% Software Design %
16% John Cristy %
17% July 1992 %
18% %
19% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38
39/*
40 Include declarations.
41*/
cristy4c08aed2011-07-01 19:47:50 +000042#include "MagickCore/studio.h"
43#include "MagickCore/attribute.h"
44#include "MagickCore/blob.h"
45#include "MagickCore/blob-private.h"
46#include "MagickCore/cache.h"
47#include "MagickCore/color.h"
48#include "MagickCore/color-private.h"
49#include "MagickCore/compress.h"
50#include "MagickCore/constitute.h"
51#include "MagickCore/draw.h"
52#include "MagickCore/exception.h"
53#include "MagickCore/exception-private.h"
54#include "MagickCore/geometry.h"
55#include "MagickCore/image.h"
56#include "MagickCore/image-private.h"
57#include "MagickCore/list.h"
58#include "MagickCore/magick.h"
59#include "MagickCore/memory_.h"
60#include "MagickCore/monitor.h"
61#include "MagickCore/monitor-private.h"
62#include "MagickCore/monitor-private.h"
63#include "MagickCore/option.h"
64#include "MagickCore/pixel-accessor.h"
65#include "MagickCore/property.h"
66#include "MagickCore/quantum-private.h"
67#include "MagickCore/resource_.h"
68#include "MagickCore/static.h"
69#include "MagickCore/string_.h"
70#include "MagickCore/module.h"
71#include "MagickCore/utility.h"
cristy80975862009-09-25 14:34:31 +000072
73/*
74 Define declarations.
75*/
cristy3ed852e2009-09-05 21:47:34 +000076#if defined(MAGICKCORE_TIFF_DELEGATE)
77#define CCITTParam "-1"
78#else
79#define CCITTParam "0"
80#endif
81
82/*
83 Forward declarations.
84*/
85static MagickBooleanType
cristy1e178e72011-08-28 19:44:34 +000086 WritePS2Image(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +000087
88/*
89%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90% %
91% %
92% %
93% R e g i s t e r P S 2 I m a g e %
94% %
95% %
96% %
97%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
98%
99% RegisterPS2Image() adds properties for the PS2 image format to
100% the list of supported formats. The properties include the image format
101% tag, a method to read and/or write the format, whether the format
102% supports the saving of more than one frame to the same file or blob,
103% whether the format supports native in-memory I/O, and a brief
104% description of the format.
105%
106% The format of the RegisterPS2Image method is:
107%
cristybb503372010-05-27 20:51:26 +0000108% size_t RegisterPS2Image(void)
cristy3ed852e2009-09-05 21:47:34 +0000109%
110*/
cristybb503372010-05-27 20:51:26 +0000111ModuleExport size_t RegisterPS2Image(void)
cristy3ed852e2009-09-05 21:47:34 +0000112{
113 MagickInfo
114 *entry;
115
116 entry=SetMagickInfo("EPS2");
117 entry->encoder=(EncodeImageHandler *) WritePS2Image;
118 entry->adjoin=MagickFalse;
119 entry->seekable_stream=MagickTrue;
120 entry->description=ConstantString("Level II Encapsulated PostScript");
121 entry->module=ConstantString("PS2");
122 (void) RegisterMagickInfo(entry);
123 entry=SetMagickInfo("PS2");
124 entry->encoder=(EncodeImageHandler *) WritePS2Image;
125 entry->seekable_stream=MagickTrue;
126 entry->description=ConstantString("Level II PostScript");
127 entry->module=ConstantString("PS2");
128 (void) RegisterMagickInfo(entry);
129 return(MagickImageCoderSignature);
130}
131
132/*
133%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
134% %
135% %
136% %
137% U n r e g i s t e r P S 2 I m a g e %
138% %
139% %
140% %
141%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142%
143% UnregisterPS2Image() removes format registrations made by the
144% PS2 module from the list of supported formats.
145%
146% The format of the UnregisterPS2Image method is:
147%
148% UnregisterPS2Image(void)
149%
150*/
151ModuleExport void UnregisterPS2Image(void)
152{
153 (void) UnregisterMagickInfo("EPS2");
154 (void) UnregisterMagickInfo("PS2");
155}
156
157/*
158%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
159% %
160% %
161% %
162% W r i t e P S 2 I m a g e %
163% %
164% %
165% %
166%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
167%
168% WritePS2Image translates an image to encapsulated Postscript
169% Level II for printing. If the supplied geometry is null, the image is
170% centered on the Postscript page. Otherwise, the image is positioned as
171% specified by the geometry.
172%
173% The format of the WritePS2Image method is:
174%
cristy1e178e72011-08-28 19:44:34 +0000175% MagickBooleanType WritePS2Image(const ImageInfo *image_info,
176% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000177%
178% A description of each parameter follows:
179%
180% o image_info: the image info.
181%
182% o image: the image.
183%
cristy1e178e72011-08-28 19:44:34 +0000184% o exception: return any errors or warnings in this structure.
185%
cristy3ed852e2009-09-05 21:47:34 +0000186*/
cristy47b838c2009-09-19 16:09:30 +0000187
188static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
cristy018f07f2011-09-04 21:15:19 +0000189 Image *image,Image *inject_image,ExceptionInfo *exception)
cristy47b838c2009-09-19 16:09:30 +0000190{
cristy47b838c2009-09-19 16:09:30 +0000191 Image
cristy80975862009-09-25 14:34:31 +0000192 *group4_image;
cristy47b838c2009-09-19 16:09:30 +0000193
194 ImageInfo
195 *write_info;
196
cristy47b838c2009-09-19 16:09:30 +0000197 MagickBooleanType
198 status;
199
cristy80975862009-09-25 14:34:31 +0000200 size_t
201 length;
cristy47b838c2009-09-19 16:09:30 +0000202
203 unsigned char
cristy80975862009-09-25 14:34:31 +0000204 *group4;
cristy47b838c2009-09-19 16:09:30 +0000205
cristy42751fe2009-10-05 00:15:50 +0000206 status=MagickTrue;
cristy47b838c2009-09-19 16:09:30 +0000207 write_info=CloneImageInfo(image_info);
cristy80975862009-09-25 14:34:31 +0000208 (void) CopyMagickString(write_info->filename,"GROUP4:",MaxTextExtent);
209 (void) CopyMagickString(write_info->magick,"GROUP4",MaxTextExtent);
cristy018f07f2011-09-04 21:15:19 +0000210 group4_image=CloneImage(inject_image,0,0,MagickTrue,exception);
cristy80975862009-09-25 14:34:31 +0000211 if (group4_image == (Image *) NULL)
212 return(MagickFalse);
213 group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
cristy018f07f2011-09-04 21:15:19 +0000214 exception);
cristy80975862009-09-25 14:34:31 +0000215 group4_image=DestroyImage(group4_image);
216 if (group4 == (unsigned char *) NULL)
217 return(MagickFalse);
cristy47b838c2009-09-19 16:09:30 +0000218 write_info=DestroyImageInfo(write_info);
cristy80975862009-09-25 14:34:31 +0000219 if (WriteBlob(image,length,group4) != (ssize_t) length)
220 status=MagickFalse;
221 group4=(unsigned char *) RelinquishMagickMemory(group4);
222 return(status);
cristy47b838c2009-09-19 16:09:30 +0000223}
224
cristy1e178e72011-08-28 19:44:34 +0000225static MagickBooleanType WritePS2Image(const ImageInfo *image_info,Image *image,
226 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000227{
228 static const char
229 *PostscriptProlog[]=
230 {
231 "%%%%BeginProlog",
232 "%%",
233 "%% Display a color image. The image is displayed in color on",
234 "%% Postscript viewers or printers that support color, otherwise",
235 "%% it is displayed as grayscale.",
236 "%%",
237 "/DirectClassImage",
238 "{",
239 " %%",
240 " %% Display a DirectClass image.",
241 " %%",
242 " colorspace 0 eq",
243 " {",
244 " /DeviceRGB setcolorspace",
245 " <<",
246 " /ImageType 1",
247 " /Width columns",
248 " /Height rows",
249 " /BitsPerComponent 8",
250 " /Decode [0 1 0 1 0 1]",
251 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
252 " compression 0 gt",
cristy80975862009-09-25 14:34:31 +0000253 " { /DataSource pixel_stream %s }",
254 " { /DataSource pixel_stream %s } ifelse",
cristy3ed852e2009-09-05 21:47:34 +0000255 " >> image",
256 " }",
257 " {",
258 " /DeviceCMYK setcolorspace",
259 " <<",
260 " /ImageType 1",
261 " /Width columns",
262 " /Height rows",
263 " /BitsPerComponent 8",
264 " /Decode [1 0 1 0 1 0 1 0]",
265 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
266 " compression 0 gt",
cristy80975862009-09-25 14:34:31 +0000267 " { /DataSource pixel_stream %s }",
268 " { /DataSource pixel_stream %s } ifelse",
cristy3ed852e2009-09-05 21:47:34 +0000269 " >> image",
270 " } ifelse",
271 "} bind def",
272 "",
273 "/PseudoClassImage",
274 "{",
275 " %%",
276 " %% Display a PseudoClass image.",
277 " %%",
278 " %% Parameters:",
279 " %% colors: number of colors in the colormap.",
280 " %%",
281 " currentfile buffer readline pop",
282 " token pop /colors exch def pop",
283 " colors 0 eq",
284 " {",
285 " %%",
286 " %% Image is grayscale.",
287 " %%",
288 " currentfile buffer readline pop",
289 " token pop /bits exch def pop",
290 " /DeviceGray setcolorspace",
291 " <<",
292 " /ImageType 1",
293 " /Width columns",
294 " /Height rows",
295 " /BitsPerComponent bits",
296 " /Decode [0 1]",
297 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
298 " compression 0 gt",
cristy80975862009-09-25 14:34:31 +0000299 " { /DataSource pixel_stream %s }",
cristy3ed852e2009-09-05 21:47:34 +0000300 " {",
cristy80975862009-09-25 14:34:31 +0000301 " /DataSource pixel_stream %s",
cristy3ed852e2009-09-05 21:47:34 +0000302 " <<",
303 " /K "CCITTParam,
304 " /Columns columns",
305 " /Rows rows",
306 " >> /CCITTFaxDecode filter",
307 " } ifelse",
308 " >> image",
309 " }",
310 " {",
311 " %%",
312 " %% Parameters:",
313 " %% colormap: red, green, blue color packets.",
314 " %%",
315 " /colormap colors 3 mul string def",
316 " currentfile colormap readhexstring pop pop",
317 " currentfile buffer readline pop",
318 " [ /Indexed /DeviceRGB colors 1 sub colormap ] setcolorspace",
319 " <<",
320 " /ImageType 1",
321 " /Width columns",
322 " /Height rows",
323 " /BitsPerComponent 8",
324 " /Decode [0 255]",
325 " /ImageMatrix [columns 0 0 rows neg 0 rows]",
326 " compression 0 gt",
cristy80975862009-09-25 14:34:31 +0000327 " { /DataSource pixel_stream %s }",
328 " { /DataSource pixel_stream %s } ifelse",
cristy3ed852e2009-09-05 21:47:34 +0000329 " >> image",
330 " } ifelse",
331 "} bind def",
332 "",
333 "/DisplayImage",
334 "{",
335 " %%",
336 " %% Display a DirectClass or PseudoClass image.",
337 " %%",
338 " %% Parameters:",
339 " %% x & y translation.",
340 " %% x & y scale.",
341 " %% label pointsize.",
342 " %% image label.",
343 " %% image columns & rows.",
344 " %% class: 0-DirectClass or 1-PseudoClass.",
345 " %% colorspace: 0-RGB or 1-CMYK.",
346 " %% compression: 0-RLECompression or 1-NoCompression.",
347 " %% hex color packets.",
348 " %%",
349 " gsave",
350 " /buffer 512 string def",
351 " /pixel_stream currentfile def",
352 "",
353 " currentfile buffer readline pop",
354 " token pop /x exch def",
355 " token pop /y exch def pop",
356 " x y translate",
357 " currentfile buffer readline pop",
358 " token pop /x exch def",
359 " token pop /y exch def pop",
360 " currentfile buffer readline pop",
361 " token pop /pointsize exch def pop",
362 " /Helvetica findfont pointsize scalefont setfont",
363 (char *) NULL
364 },
365 *PostscriptEpilog[]=
366 {
367 " x y scale",
368 " currentfile buffer readline pop",
369 " token pop /columns exch def",
370 " token pop /rows exch def pop",
371 " currentfile buffer readline pop",
372 " token pop /class exch def pop",
373 " currentfile buffer readline pop",
374 " token pop /colorspace exch def pop",
375 " currentfile buffer readline pop",
376 " token pop /compression exch def pop",
377 " class 0 gt { PseudoClassImage } { DirectClassImage } ifelse",
378 (char *) NULL
379 };
380
381 char
382 buffer[MaxTextExtent],
383 date[MaxTextExtent],
384 page_geometry[MaxTextExtent],
385 **labels;
386
387 CompressionType
388 compression;
389
390 const char
391 **q,
392 *value;
393
394 double
395 pointsize;
396
397 GeometryInfo
398 geometry_info;
399
cristy3ed852e2009-09-05 21:47:34 +0000400 MagickOffsetType
401 scene,
402 start,
403 stop;
404
405 MagickBooleanType
406 progress,
407 status;
408
409 MagickOffsetType
410 offset;
411
412 MagickSizeType
413 number_pixels;
414
415 MagickStatusType
416 flags;
417
418 PointInfo
419 delta,
420 resolution,
421 scale;
422
423 RectangleInfo
424 geometry,
425 media_info,
426 page_info;
427
cristy4c08aed2011-07-01 19:47:50 +0000428 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000429 *p;
430
cristybb503372010-05-27 20:51:26 +0000431 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000432 x;
433
cristybb503372010-05-27 20:51:26 +0000434 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000435 i;
436
437 SegmentInfo
438 bounds;
439
440 size_t
cristy802d3642011-04-27 02:02:41 +0000441 length,
442 page,
443 text_size;
444
445 ssize_t
446 j,
447 y;
cristy3ed852e2009-09-05 21:47:34 +0000448
449 time_t
450 timer;
451
452 unsigned char
453 *pixels;
454
cristy3ed852e2009-09-05 21:47:34 +0000455 /*
456 Open output image file.
457 */
458 assert(image_info != (const ImageInfo *) NULL);
459 assert(image_info->signature == MagickSignature);
460 assert(image != (Image *) NULL);
461 assert(image->signature == MagickSignature);
462 if (image->debug != MagickFalse)
463 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +0000464 assert(exception != (ExceptionInfo *) NULL);
465 assert(exception->signature == MagickSignature);
cristy1e178e72011-08-28 19:44:34 +0000466 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000467 if (status == MagickFalse)
468 return(status);
469 compression=image->compression;
470 if (image_info->compression != UndefinedCompression)
471 compression=image_info->compression;
472 switch (compression)
473 {
474#if !defined(MAGICKCORE_JPEG_DELEGATE)
475 case JPEGCompression:
476 {
477 compression=RLECompression;
cristy1e178e72011-08-28 19:44:34 +0000478 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +0000479 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
480 image->filename);
481 break;
482 }
483#endif
484 default:
485 break;
486 }
487 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
488 page=1;
489 scene=0;
490 do
491 {
492 /*
493 Scale relative to dots-per-inch.
494 */
495 delta.x=DefaultResolution;
496 delta.y=DefaultResolution;
cristy2a11bef2011-10-28 18:33:11 +0000497 resolution.x=image->resolution.x;
498 resolution.y=image->resolution.y;
cristy3ed852e2009-09-05 21:47:34 +0000499 if ((resolution.x == 0.0) || (resolution.y == 0.0))
500 {
501 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
502 resolution.x=geometry_info.rho;
503 resolution.y=geometry_info.sigma;
504 if ((flags & SigmaValue) == 0)
505 resolution.y=resolution.x;
506 }
507 if (image_info->density != (char *) NULL)
508 {
509 flags=ParseGeometry(image_info->density,&geometry_info);
510 resolution.x=geometry_info.rho;
511 resolution.y=geometry_info.sigma;
512 if ((flags & SigmaValue) == 0)
513 resolution.y=resolution.x;
514 }
515 if (image->units == PixelsPerCentimeterResolution)
516 {
cristybb503372010-05-27 20:51:26 +0000517 resolution.x=(size_t) (100.0*2.54*resolution.x+0.5)/100.0;
518 resolution.y=(size_t) (100.0*2.54*resolution.y+0.5)/100.0;
cristy3ed852e2009-09-05 21:47:34 +0000519 }
520 SetGeometry(image,&geometry);
cristyb51dff52011-05-19 16:55:47 +0000521 (void) FormatLocaleString(page_geometry,MaxTextExtent,"%.20gx%.20g",
cristye8c25f92010-06-03 00:53:06 +0000522 (double) image->columns,(double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000523 if (image_info->page != (char *) NULL)
524 (void) CopyMagickString(page_geometry,image_info->page,MaxTextExtent);
525 else
526 if ((image->page.width != 0) && (image->page.height != 0))
cristyb51dff52011-05-19 16:55:47 +0000527 (void) FormatLocaleString(page_geometry,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +0000528 "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,(double)
529 image->page.height,(double) image->page.x,(double) image->page.y);
cristy3ed852e2009-09-05 21:47:34 +0000530 else
531 if ((image->gravity != UndefinedGravity) &&
532 (LocaleCompare(image_info->magick,"PS") == 0))
533 (void) CopyMagickString(page_geometry,PSPageGeometry,MaxTextExtent);
534 (void) ConcatenateMagickString(page_geometry,">",MaxTextExtent);
535 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
536 &geometry.width,&geometry.height);
537 scale.x=(double) (geometry.width*delta.x)/resolution.x;
cristybb503372010-05-27 20:51:26 +0000538 geometry.width=(size_t) floor(scale.x+0.5);
cristy3ed852e2009-09-05 21:47:34 +0000539 scale.y=(double) (geometry.height*delta.y)/resolution.y;
cristybb503372010-05-27 20:51:26 +0000540 geometry.height=(size_t) floor(scale.y+0.5);
cristy3ed852e2009-09-05 21:47:34 +0000541 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
cristy1e178e72011-08-28 19:44:34 +0000542 (void) ParseGravityGeometry(image,page_geometry,&page_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000543 if (image->gravity != UndefinedGravity)
544 {
545 geometry.x=(-page_info.x);
cristybb503372010-05-27 20:51:26 +0000546 geometry.y=(ssize_t) (media_info.height+page_info.y-image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000547 }
548 pointsize=12.0;
549 if (image_info->pointsize != 0.0)
550 pointsize=image_info->pointsize;
551 text_size=0;
cristyd15e6592011-10-15 00:13:06 +0000552 value=GetImageProperty(image,"label",exception);
cristy3ed852e2009-09-05 21:47:34 +0000553 if (value != (const char *) NULL)
cristybb503372010-05-27 20:51:26 +0000554 text_size=(size_t) (MultilineCensus(value)*pointsize+12);
cristy3ed852e2009-09-05 21:47:34 +0000555 if (page == 1)
556 {
557 /*
558 Output Postscript header.
559 */
560 if (LocaleCompare(image_info->magick,"PS2") == 0)
561 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0\n",MaxTextExtent);
562 else
563 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0 EPSF-3.0\n",
564 MaxTextExtent);
565 (void) WriteBlobString(image,buffer);
566 (void) WriteBlobString(image,"%%Creator: (ImageMagick)\n");
cristyb51dff52011-05-19 16:55:47 +0000567 (void) FormatLocaleString(buffer,MaxTextExtent,"%%%%Title: (%s)\n",
cristy3ed852e2009-09-05 21:47:34 +0000568 image->filename);
569 (void) WriteBlobString(image,buffer);
570 timer=time((time_t *) NULL);
571 (void) FormatMagickTime(timer,MaxTextExtent,date);
cristyb51dff52011-05-19 16:55:47 +0000572 (void) FormatLocaleString(buffer,MaxTextExtent,
cristy3ed852e2009-09-05 21:47:34 +0000573 "%%%%CreationDate: (%s)\n",date);
574 (void) WriteBlobString(image,buffer);
575 bounds.x1=(double) geometry.x;
576 bounds.y1=(double) geometry.y;
577 bounds.x2=(double) geometry.x+geometry.width;
578 bounds.y2=(double) geometry.y+geometry.height+text_size;
579 if ((image_info->adjoin != MagickFalse) &&
580 (GetNextImageInList(image) != (Image *) NULL))
581 (void) CopyMagickString(buffer,"%%BoundingBox: (atend)\n",
582 MaxTextExtent);
583 else
584 {
cristyb51dff52011-05-19 16:55:47 +0000585 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +0000586 "%%%%BoundingBox: %.20g %.20g %.20g %.20g\n",ceil(bounds.x1-0.5),
587 ceil(bounds.y1-0.5),floor(bounds.x2+0.5),floor(bounds.y2+0.5));
cristy3ed852e2009-09-05 21:47:34 +0000588 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +0000589 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +0000590 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,
cristy8cd5b312010-01-07 01:10:24 +0000591 bounds.y1,bounds.x2,bounds.y2);
cristy3ed852e2009-09-05 21:47:34 +0000592 }
593 (void) WriteBlobString(image,buffer);
cristyd15e6592011-10-15 00:13:06 +0000594 value=GetImageProperty(image,"label",exception);
cristy3ed852e2009-09-05 21:47:34 +0000595 if (value != (const char *) NULL)
596 (void) WriteBlobString(image,
597 "%%DocumentNeededResources: font Helvetica\n");
598 (void) WriteBlobString(image,"%%LanguageLevel: 2\n");
599 if (LocaleCompare(image_info->magick,"PS2") != 0)
600 (void) WriteBlobString(image,"%%Pages: 1\n");
601 else
602 {
603 (void) WriteBlobString(image,"%%Orientation: Portrait\n");
604 (void) WriteBlobString(image,"%%PageOrder: Ascend\n");
605 if (image_info->adjoin == MagickFalse)
606 (void) CopyMagickString(buffer,"%%Pages: 1\n",MaxTextExtent);
607 else
cristyb51dff52011-05-19 16:55:47 +0000608 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +0000609 "%%%%Pages: %.20g\n",(double) GetImageListLength(image));
cristy3ed852e2009-09-05 21:47:34 +0000610 (void) WriteBlobString(image,buffer);
611 }
612 (void) WriteBlobString(image,"%%EndComments\n");
613 (void) WriteBlobString(image,"\n%%BeginDefaults\n");
614 (void) WriteBlobString(image,"%%EndDefaults\n\n");
615 /*
616 Output Postscript commands.
617 */
618 for (q=PostscriptProlog; *q; q++)
619 {
620 switch (compression)
621 {
622 case NoCompression:
623 {
cristyb51dff52011-05-19 16:55:47 +0000624 (void) FormatLocaleString(buffer,MaxTextExtent,*q,
cristy80975862009-09-25 14:34:31 +0000625 "/ASCII85Decode filter");
cristy3ed852e2009-09-05 21:47:34 +0000626 break;
627 }
628 case JPEGCompression:
629 {
cristyb51dff52011-05-19 16:55:47 +0000630 (void) FormatLocaleString(buffer,MaxTextExtent,*q,
cristy80975862009-09-25 14:34:31 +0000631 "/DCTDecode filter");
cristy3ed852e2009-09-05 21:47:34 +0000632 break;
633 }
634 case LZWCompression:
635 {
cristyb51dff52011-05-19 16:55:47 +0000636 (void) FormatLocaleString(buffer,MaxTextExtent,*q,
cristy80975862009-09-25 14:34:31 +0000637 "/LZWDecode filter");
cristy3ed852e2009-09-05 21:47:34 +0000638 break;
639 }
640 case FaxCompression:
641 case Group4Compression:
642 {
cristyb51dff52011-05-19 16:55:47 +0000643 (void) FormatLocaleString(buffer,MaxTextExtent,*q," ");
cristy3ed852e2009-09-05 21:47:34 +0000644 break;
645 }
646 default:
647 {
cristyb51dff52011-05-19 16:55:47 +0000648 (void) FormatLocaleString(buffer,MaxTextExtent,*q,
cristy80975862009-09-25 14:34:31 +0000649 "/RunLengthDecode filter");
cristy3ed852e2009-09-05 21:47:34 +0000650 break;
651 }
652 }
653 (void) WriteBlobString(image,buffer);
654 (void) WriteBlobByte(image,'\n');
655 }
cristyd15e6592011-10-15 00:13:06 +0000656 value=GetImageProperty(image,"label",exception);
cristy3ed852e2009-09-05 21:47:34 +0000657 if (value != (const char *) NULL)
cristybb503372010-05-27 20:51:26 +0000658 for (j=(ssize_t) MultilineCensus(value)-1; j >= 0; j--)
cristy3ed852e2009-09-05 21:47:34 +0000659 {
660 (void) WriteBlobString(image," /label 512 string def\n");
661 (void) WriteBlobString(image," currentfile label readline pop\n");
cristyb51dff52011-05-19 16:55:47 +0000662 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +0000663 " 0 y %g add moveto label show pop\n",j*pointsize+12);
cristy3ed852e2009-09-05 21:47:34 +0000664 (void) WriteBlobString(image,buffer);
665 }
666 for (q=PostscriptEpilog; *q; q++)
667 {
cristyb51dff52011-05-19 16:55:47 +0000668 (void) FormatLocaleString(buffer,MaxTextExtent,"%s\n",*q);
cristy3ed852e2009-09-05 21:47:34 +0000669 (void) WriteBlobString(image,buffer);
670 }
671 if (LocaleCompare(image_info->magick,"PS2") == 0)
672 (void) WriteBlobString(image," showpage\n");
673 (void) WriteBlobString(image,"} bind def\n");
674 (void) WriteBlobString(image,"%%EndProlog\n");
675 }
cristyb51dff52011-05-19 16:55:47 +0000676 (void) FormatLocaleString(buffer,MaxTextExtent,"%%%%Page: 1 %.20g\n",
cristye8c25f92010-06-03 00:53:06 +0000677 (double) page++);
cristy3ed852e2009-09-05 21:47:34 +0000678 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +0000679 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +0000680 "%%%%PageBoundingBox: %.20g %.20g %.20g %.20g\n",(double) geometry.x,
681 (double) geometry.y,geometry.x+(double) geometry.width,geometry.y+(double)
cristy3ed852e2009-09-05 21:47:34 +0000682 (geometry.height+text_size));
683 (void) WriteBlobString(image,buffer);
684 if ((double) geometry.x < bounds.x1)
685 bounds.x1=(double) geometry.x;
686 if ((double) geometry.y < bounds.y1)
687 bounds.y1=(double) geometry.y;
688 if ((double) (geometry.x+geometry.width-1) > bounds.x2)
689 bounds.x2=(double) geometry.x+geometry.width-1;
690 if ((double) (geometry.y+(geometry.height+text_size)-1) > bounds.y2)
691 bounds.y2=(double) geometry.y+(geometry.height+text_size)-1;
cristyd15e6592011-10-15 00:13:06 +0000692 value=GetImageProperty(image,"label",exception);
cristy3ed852e2009-09-05 21:47:34 +0000693 if (value != (const char *) NULL)
694 (void) WriteBlobString(image,"%%PageResources: font Times-Roman\n");
695 if (LocaleCompare(image_info->magick,"PS2") != 0)
696 (void) WriteBlobString(image,"userdict begin\n");
697 start=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +0000698 (void) FormatLocaleString(buffer,MaxTextExtent,
cristy3ed852e2009-09-05 21:47:34 +0000699 "%%%%BeginData:%13ld %s Bytes\n",0L,
700 compression == NoCompression ? "ASCII" : "Binary");
701 (void) WriteBlobString(image,buffer);
702 stop=TellBlob(image);
703 (void) WriteBlobString(image,"DisplayImage\n");
704 /*
705 Output image data.
706 */
cristyb51dff52011-05-19 16:55:47 +0000707 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n%g %g\n%g\n",
cristye8c25f92010-06-03 00:53:06 +0000708 (double) geometry.x,(double) geometry.y,scale.x,scale.y,pointsize);
cristy3ed852e2009-09-05 21:47:34 +0000709 (void) WriteBlobString(image,buffer);
710 labels=(char **) NULL;
cristyd15e6592011-10-15 00:13:06 +0000711 value=GetImageProperty(image,"label",exception);
cristy3ed852e2009-09-05 21:47:34 +0000712 if (value != (const char *) NULL)
713 labels=StringToList(value);
714 if (labels != (char **) NULL)
715 {
716 for (i=0; labels[i] != (char *) NULL; i++)
717 {
cristyb51dff52011-05-19 16:55:47 +0000718 (void) FormatLocaleString(buffer,MaxTextExtent,"%s \n",
cristy3ed852e2009-09-05 21:47:34 +0000719 labels[i]);
720 (void) WriteBlobString(image,buffer);
721 labels[i]=DestroyString(labels[i]);
722 }
723 labels=(char **) RelinquishMagickMemory(labels);
724 }
725 number_pixels=(MagickSizeType) image->columns*image->rows;
726 if (number_pixels != (MagickSizeType) ((size_t) number_pixels))
727 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
728 if ((compression == FaxCompression) || (compression == Group4Compression) ||
729 ((image_info->type != TrueColorType) &&
cristy1e178e72011-08-28 19:44:34 +0000730 (IsImageGray(image,exception) != MagickFalse)))
cristy3ed852e2009-09-05 21:47:34 +0000731 {
cristyb51dff52011-05-19 16:55:47 +0000732 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n1\n%d\n",
cristye8c25f92010-06-03 00:53:06 +0000733 (double) image->columns,(double) image->rows,(int)
cristy3ed852e2009-09-05 21:47:34 +0000734 (image->colorspace == CMYKColorspace));
735 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +0000736 (void) FormatLocaleString(buffer,MaxTextExtent,"%d\n",
cristy3ed852e2009-09-05 21:47:34 +0000737 (int) ((compression != FaxCompression) &&
738 (compression != Group4Compression)));
739 (void) WriteBlobString(image,buffer);
740 (void) WriteBlobString(image,"0\n");
cristyb51dff52011-05-19 16:55:47 +0000741 (void) FormatLocaleString(buffer,MaxTextExtent,"%d\n",
cristy3ed852e2009-09-05 21:47:34 +0000742 (compression == FaxCompression) ||
743 (compression == Group4Compression) ? 1 : 8);
744 (void) WriteBlobString(image,buffer);
745 switch (compression)
746 {
747 case FaxCompression:
748 case Group4Compression:
749 {
750 if (LocaleCompare(CCITTParam,"0") == 0)
751 {
cristy018f07f2011-09-04 21:15:19 +0000752 (void) HuffmanEncodeImage(image_info,image,image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000753 break;
754 }
cristy018f07f2011-09-04 21:15:19 +0000755 (void) Huffman2DEncodeImage(image_info,image,image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000756 break;
757 }
758 case JPEGCompression:
759 {
cristy1e178e72011-08-28 19:44:34 +0000760 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
cristy3ed852e2009-09-05 21:47:34 +0000761 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +0000762 {
763 (void) CloseBlob(image);
764 return(MagickFalse);
765 }
cristy3ed852e2009-09-05 21:47:34 +0000766 break;
767 }
768 case RLECompression:
769 default:
770 {
771 register unsigned char
772 *q;
773
774 /*
775 Allocate pixel array.
776 */
777 length=(size_t) number_pixels;
778 pixels=(unsigned char *) AcquireQuantumMemory(length,
779 sizeof(*pixels));
780 if (pixels == (unsigned char *) NULL)
781 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
782 /*
783 Dump Runlength encoded pixels.
784 */
785 q=pixels;
cristybb503372010-05-27 20:51:26 +0000786 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000787 {
cristy1e178e72011-08-28 19:44:34 +0000788 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000789 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000790 break;
cristybb503372010-05-27 20:51:26 +0000791 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000792 {
cristy4c08aed2011-07-01 19:47:50 +0000793 *q++=ScaleQuantumToChar(GetPixelIntensity(image,p));
cristyed231572011-07-14 02:18:59 +0000794 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000795 }
cristy802d3642011-04-27 02:02:41 +0000796 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
797 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000798 if (progress == MagickFalse)
799 break;
800 }
801 length=(size_t) (q-pixels);
802 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +0000803 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +0000804 else
cristy018f07f2011-09-04 21:15:19 +0000805 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +0000806 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
807 if (status == MagickFalse)
808 {
809 (void) CloseBlob(image);
810 return(MagickFalse);
811 }
812 break;
813 }
814 case NoCompression:
815 {
816 /*
817 Dump uncompressed PseudoColor packets.
818 */
819 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +0000820 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000821 {
cristy1e178e72011-08-28 19:44:34 +0000822 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000823 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000824 break;
cristybb503372010-05-27 20:51:26 +0000825 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000826 {
cristy802d3642011-04-27 02:02:41 +0000827 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +0000828 GetPixelIntensity(image,p)));
cristyed231572011-07-14 02:18:59 +0000829 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000830 }
cristye8c25f92010-06-03 00:53:06 +0000831 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
832 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000833 if (progress == MagickFalse)
834 break;
835 }
836 Ascii85Flush(image);
837 break;
838 }
839 }
840 }
841 else
842 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
843 (compression == JPEGCompression) || (image->matte != MagickFalse))
844 {
cristyb51dff52011-05-19 16:55:47 +0000845 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n0\n%d\n",
cristye8c25f92010-06-03 00:53:06 +0000846 (double) image->columns,(double) image->rows,(int)
cristy3ed852e2009-09-05 21:47:34 +0000847 (image->colorspace == CMYKColorspace));
848 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +0000849 (void) FormatLocaleString(buffer,MaxTextExtent,"%d\n",
cristy3ed852e2009-09-05 21:47:34 +0000850 (int) (compression == NoCompression));
851 (void) WriteBlobString(image,buffer);
852 switch (compression)
853 {
854 case JPEGCompression:
855 {
cristy1e178e72011-08-28 19:44:34 +0000856 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
cristy3ed852e2009-09-05 21:47:34 +0000857 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +0000858 {
859 (void) CloseBlob(image);
860 return(MagickFalse);
861 }
cristy3ed852e2009-09-05 21:47:34 +0000862 break;
863 }
864 case RLECompression:
865 default:
866 {
867 register unsigned char
868 *q;
869
870 /*
871 Allocate pixel array.
872 */
873 length=(size_t) number_pixels;
874 pixels=(unsigned char *) AcquireQuantumMemory(length,
875 4*sizeof(*pixels));
876 if (pixels == (unsigned char *) NULL)
877 ThrowWriterException(ResourceLimitError,
878 "MemoryAllocationFailed");
879 /*
880 Dump Packbit encoded pixels.
881 */
882 q=pixels;
cristybb503372010-05-27 20:51:26 +0000883 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000884 {
cristy1e178e72011-08-28 19:44:34 +0000885 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000886 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000887 break;
cristybb503372010-05-27 20:51:26 +0000888 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000889 {
890 if ((image->matte != MagickFalse) &&
cristy4c08aed2011-07-01 19:47:50 +0000891 (GetPixelAlpha(image,p) == (Quantum) TransparentAlpha))
cristy3ed852e2009-09-05 21:47:34 +0000892 {
893 *q++=ScaleQuantumToChar((Quantum) QuantumRange);
894 *q++=ScaleQuantumToChar((Quantum) QuantumRange);
895 *q++=ScaleQuantumToChar((Quantum) QuantumRange);
896 }
897 else
898 if (image->colorspace != CMYKColorspace)
899 {
cristy4c08aed2011-07-01 19:47:50 +0000900 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
901 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
902 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
cristy3ed852e2009-09-05 21:47:34 +0000903 }
904 else
905 {
cristy4c08aed2011-07-01 19:47:50 +0000906 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
907 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
908 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
909 *q++=ScaleQuantumToChar(GetPixelBlack(image,p));
cristy3ed852e2009-09-05 21:47:34 +0000910 }
cristyed231572011-07-14 02:18:59 +0000911 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000912 }
cristy802d3642011-04-27 02:02:41 +0000913 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
914 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000915 if (progress == MagickFalse)
916 break;
917 }
918 length=(size_t) (q-pixels);
919 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +0000920 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +0000921 else
cristy018f07f2011-09-04 21:15:19 +0000922 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +0000923 if (status == MagickFalse)
924 {
925 (void) CloseBlob(image);
926 return(MagickFalse);
927 }
928 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
929 break;
930 }
931 case NoCompression:
932 {
933 /*
934 Dump uncompressed DirectColor packets.
935 */
936 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +0000937 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000938 {
cristy1e178e72011-08-28 19:44:34 +0000939 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000940 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000941 break;
cristybb503372010-05-27 20:51:26 +0000942 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000943 {
944 if ((image->matte != MagickFalse) &&
cristy4c08aed2011-07-01 19:47:50 +0000945 (GetPixelAlpha(image,p) == (Quantum) TransparentAlpha))
cristy3ed852e2009-09-05 21:47:34 +0000946 {
947 Ascii85Encode(image,ScaleQuantumToChar((Quantum)
948 QuantumRange));
949 Ascii85Encode(image,ScaleQuantumToChar((Quantum)
950 QuantumRange));
951 Ascii85Encode(image,ScaleQuantumToChar((Quantum)
952 QuantumRange));
953 }
954 else
955 if (image->colorspace != CMYKColorspace)
956 {
cristy802d3642011-04-27 02:02:41 +0000957 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +0000958 GetPixelRed(image,p)));
cristy802d3642011-04-27 02:02:41 +0000959 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +0000960 GetPixelGreen(image,p)));
cristy802d3642011-04-27 02:02:41 +0000961 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +0000962 GetPixelBlue(image,p)));
cristy3ed852e2009-09-05 21:47:34 +0000963 }
964 else
965 {
cristy802d3642011-04-27 02:02:41 +0000966 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +0000967 GetPixelRed(image,p)));
cristy802d3642011-04-27 02:02:41 +0000968 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +0000969 GetPixelGreen(image,p)));
cristy802d3642011-04-27 02:02:41 +0000970 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +0000971 GetPixelBlue(image,p)));
cristy9fff7b42011-04-29 01:09:31 +0000972 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +0000973 GetPixelBlack(image,p)));
cristy3ed852e2009-09-05 21:47:34 +0000974 }
cristyed231572011-07-14 02:18:59 +0000975 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000976 }
cristy802d3642011-04-27 02:02:41 +0000977 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
978 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000979 if (progress == MagickFalse)
980 break;
981 }
982 Ascii85Flush(image);
983 break;
984 }
985 }
986 }
987 else
988 {
989 /*
990 Dump number of colors and colormap.
991 */
cristyb51dff52011-05-19 16:55:47 +0000992 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n1\n%d\n",
cristye8c25f92010-06-03 00:53:06 +0000993 (double) image->columns,(double) image->rows,(int)
cristy3ed852e2009-09-05 21:47:34 +0000994 (image->colorspace == CMYKColorspace));
995 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +0000996 (void) FormatLocaleString(buffer,MaxTextExtent,"%d\n",
cristy3ed852e2009-09-05 21:47:34 +0000997 (int) (compression == NoCompression));
998 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +0000999 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001000 image->colors);
cristy3ed852e2009-09-05 21:47:34 +00001001 (void) WriteBlobString(image,buffer);
cristybb503372010-05-27 20:51:26 +00001002 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001003 {
cristyb51dff52011-05-19 16:55:47 +00001004 (void) FormatLocaleString(buffer,MaxTextExtent,"%02X%02X%02X\n",
cristy3ed852e2009-09-05 21:47:34 +00001005 ScaleQuantumToChar(image->colormap[i].red),
1006 ScaleQuantumToChar(image->colormap[i].green),
1007 ScaleQuantumToChar(image->colormap[i].blue));
1008 (void) WriteBlobString(image,buffer);
1009 }
1010 switch (compression)
1011 {
1012 case RLECompression:
1013 default:
1014 {
1015 register unsigned char
1016 *q;
1017
1018 /*
1019 Allocate pixel array.
1020 */
1021 length=(size_t) number_pixels;
1022 pixels=(unsigned char *) AcquireQuantumMemory(length,
1023 sizeof(*pixels));
1024 if (pixels == (unsigned char *) NULL)
1025 ThrowWriterException(ResourceLimitError,
1026 "MemoryAllocationFailed");
1027 /*
1028 Dump Runlength encoded pixels.
1029 */
1030 q=pixels;
cristybb503372010-05-27 20:51:26 +00001031 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001032 {
cristy1e178e72011-08-28 19:44:34 +00001033 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001034 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001035 break;
cristybb503372010-05-27 20:51:26 +00001036 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00001037 {
1038 *q++=(unsigned char) GetPixelIndex(image,p);
cristyed231572011-07-14 02:18:59 +00001039 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001040 }
cristy802d3642011-04-27 02:02:41 +00001041 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1042 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001043 if (progress == MagickFalse)
1044 break;
1045 }
1046 length=(size_t) (q-pixels);
1047 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00001048 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001049 else
cristy018f07f2011-09-04 21:15:19 +00001050 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001051 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1052 if (status == MagickFalse)
1053 {
1054 (void) CloseBlob(image);
1055 return(MagickFalse);
1056 }
1057 break;
1058 }
1059 case NoCompression:
1060 {
1061 /*
1062 Dump uncompressed PseudoColor packets.
1063 */
1064 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00001065 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001066 {
cristy1e178e72011-08-28 19:44:34 +00001067 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001068 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001069 break;
cristybb503372010-05-27 20:51:26 +00001070 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00001071 {
1072 Ascii85Encode(image,(unsigned char) GetPixelIndex(image,p));
cristyed231572011-07-14 02:18:59 +00001073 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001074 }
cristy802d3642011-04-27 02:02:41 +00001075 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1076 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001077 if (progress == MagickFalse)
1078 break;
1079 }
1080 Ascii85Flush(image);
1081 break;
1082 }
1083 }
1084 }
1085 (void) WriteBlobByte(image,'\n');
1086 length=(size_t) (TellBlob(image)-stop);
1087 stop=TellBlob(image);
1088 offset=SeekBlob(image,start,SEEK_SET);
1089 if (offset < 0)
1090 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
cristyb51dff52011-05-19 16:55:47 +00001091 (void) FormatLocaleString(buffer,MaxTextExtent,
cristyf2faecf2010-05-28 19:19:36 +00001092 "%%%%BeginData:%13ld %s Bytes\n",(long) length,
cristy3ed852e2009-09-05 21:47:34 +00001093 compression == NoCompression ? "ASCII" : "Binary");
1094 (void) WriteBlobString(image,buffer);
1095 offset=SeekBlob(image,stop,SEEK_SET);
1096 (void) WriteBlobString(image,"%%EndData\n");
1097 if (LocaleCompare(image_info->magick,"PS2") != 0)
1098 (void) WriteBlobString(image,"end\n");
1099 (void) WriteBlobString(image,"%%PageTrailer\n");
1100 if (GetNextImageInList(image) == (Image *) NULL)
1101 break;
1102 image=SyncNextImageInList(image);
1103 status=SetImageProgress(image,SaveImagesTag,scene++,
1104 GetImageListLength(image));
1105 if (status == MagickFalse)
1106 break;
1107 } while (image_info->adjoin != MagickFalse);
1108 (void) WriteBlobString(image,"%%Trailer\n");
1109 if (page > 1)
1110 {
cristyb51dff52011-05-19 16:55:47 +00001111 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00001112 "%%%%BoundingBox: %.20g %.20g %.20g %.20g\n",ceil(bounds.x1-0.5),
1113 ceil(bounds.y1-0.5),floor(bounds.x2+0.5),floor(bounds.y2+0.5));
cristy3ed852e2009-09-05 21:47:34 +00001114 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001115 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00001116 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,bounds.y1,
cristy8cd5b312010-01-07 01:10:24 +00001117 bounds.x2,bounds.y2);
cristy3ed852e2009-09-05 21:47:34 +00001118 (void) WriteBlobString(image,buffer);
1119 }
1120 (void) WriteBlobString(image,"%%EOF\n");
1121 (void) CloseBlob(image);
1122 return(MagickTrue);
1123}