1 /*
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 % Cristy %
17 % July 1992 %
18 % %
19 % %
20 % Copyright 1999-2021 ImageMagick Studio LLC, a non-profit organization %
21 % 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 % https://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 */
42 #include "magick/studio.h"
43 #include "magick/attribute.h"
44 #include "magick/blob.h"
45 #include "magick/blob-private.h"
46 #include "magick/cache.h"
47 #include "magick/color.h"
48 #include "magick/color-private.h"
49 #include "magick/compress.h"
50 #include "magick/constitute.h"
51 #include "magick/draw.h"
52 #include "magick/exception.h"
53 #include "magick/exception-private.h"
54 #include "magick/geometry.h"
55 #include "magick/image.h"
56 #include "magick/image-private.h"
57 #include "magick/list.h"
58 #include "magick/magick.h"
59 #include "magick/memory_.h"
60 #include "magick/module.h"
61 #include "magick/monitor.h"
62 #include "magick/monitor-private.h"
63 #include "magick/monitor-private.h"
64 #include "magick/option.h"
65 #include "magick/pixel-accessor.h"
66 #include "magick/property.h"
67 #include "magick/quantum-private.h"
68 #include "magick/resource_.h"
69 #include "magick/static.h"
70 #include "magick/string_.h"
71 #include "magick/timer-private.h"
72 #include "magick/utility.h"
73
74 /*
75 Define declarations.
76 */
77 #if defined(MAGICKCORE_TIFF_DELEGATE)
78 #define CCITTParam "-1"
79 #else
80 #define CCITTParam "0"
81 #endif
82
83 /*
84 Forward declarations.
85 */
86 static MagickBooleanType
87 WritePS2Image(const ImageInfo *,Image *);
88
89 /*
90 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91 % %
92 % %
93 % %
94 % R e g i s t e r P S 2 I m a g e %
95 % %
96 % %
97 % %
98 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
99 %
100 % RegisterPS2Image() adds properties for the PS2 image format to
101 % the list of supported formats. The properties include the image format
102 % tag, a method to read and/or write the format, whether the format
103 % supports the saving of more than one frame to the same file or blob,
104 % whether the format supports native in-memory I/O, and a brief
105 % description of the format.
106 %
107 % The format of the RegisterPS2Image method is:
108 %
109 % size_t RegisterPS2Image(void)
110 %
111 */
RegisterPS2Image(void)112 ModuleExport size_t RegisterPS2Image(void)
113 {
114 MagickInfo
115 *entry;
116
117 entry=SetMagickInfo("EPS2");
118 entry->encoder=(EncodeImageHandler *) WritePS2Image;
119 entry->adjoin=MagickFalse;
120 entry->seekable_stream=MagickTrue;
121 entry->blob_support=MagickFalse;
122 entry->description=ConstantString("Level II Encapsulated PostScript");
123 entry->mime_type=ConstantString("application/postscript");
124 entry->magick_module=ConstantString("PS2");
125 (void) RegisterMagickInfo(entry);
126 entry=SetMagickInfo("PS2");
127 entry->encoder=(EncodeImageHandler *) WritePS2Image;
128 entry->seekable_stream=MagickTrue;
129 entry->blob_support=MagickFalse;
130 entry->description=ConstantString("Level II PostScript");
131 entry->mime_type=ConstantString("application/postscript");
132 entry->magick_module=ConstantString("PS2");
133 (void) RegisterMagickInfo(entry);
134 return(MagickImageCoderSignature);
135 }
136
137 /*
138 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
139 % %
140 % %
141 % %
142 % U n r e g i s t e r P S 2 I m a g e %
143 % %
144 % %
145 % %
146 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
147 %
148 % UnregisterPS2Image() removes format registrations made by the
149 % PS2 module from the list of supported formats.
150 %
151 % The format of the UnregisterPS2Image method is:
152 %
153 % UnregisterPS2Image(void)
154 %
155 */
UnregisterPS2Image(void)156 ModuleExport void UnregisterPS2Image(void)
157 {
158 (void) UnregisterMagickInfo("EPS2");
159 (void) UnregisterMagickInfo("PS2");
160 }
161
162 /*
163 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
164 % %
165 % %
166 % %
167 % W r i t e P S 2 I m a g e %
168 % %
169 % %
170 % %
171 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
172 %
173 % WritePS2Image translates an image to encapsulated Postscript
174 % Level II for printing. If the supplied geometry is null, the image is
175 % centered on the Postscript page. Otherwise, the image is positioned as
176 % specified by the geometry.
177 %
178 % The format of the WritePS2Image method is:
179 %
180 % MagickBooleanType WritePS2Image(const ImageInfo *image_info,Image *image)
181 %
182 % A description of each parameter follows:
183 %
184 % o image_info: the image info.
185 %
186 % o image: the image.
187 %
188 */
189
Huffman2DEncodeImage(const ImageInfo * image_info,Image * image,Image * inject_image)190 static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
191 Image *image,Image *inject_image)
192 {
193 Image
194 *group4_image;
195
196 ImageInfo
197 *write_info;
198
199 MagickBooleanType
200 status;
201
202 size_t
203 length;
204
205 unsigned char
206 *group4;
207
208 group4_image=CloneImage(inject_image,0,0,MagickTrue,&image->exception);
209 if (group4_image == (Image *) NULL)
210 return(MagickFalse);
211 write_info=CloneImageInfo(image_info);
212 (void) CopyMagickString(write_info->filename,"GROUP4:",MaxTextExtent);
213 (void) CopyMagickString(write_info->magick,"GROUP4",MaxTextExtent);
214 group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
215 &image->exception);
216 write_info=DestroyImageInfo(write_info);
217 group4_image=DestroyImage(group4_image);
218 if (group4 == (unsigned char *) NULL)
219 return(MagickFalse);
220 status=MagickTrue;
221 if (WriteBlob(image,length,group4) != (ssize_t) length)
222 status=MagickFalse;
223 group4=(unsigned char *) RelinquishMagickMemory(group4);
224 return(status);
225 }
226
WritePS2Image(const ImageInfo * image_info,Image * image)227 static MagickBooleanType WritePS2Image(const ImageInfo *image_info,Image *image)
228 {
229 static const char
230 PostscriptProlog[] =
231 "%%%%BeginProlog\n"
232 "%%\n"
233 "%% Display a color image. The image is displayed in color on\n"
234 "%% Postscript viewers or printers that support color, otherwise\n"
235 "%% it is displayed as grayscale.\n"
236 "%%\n"
237 "/DirectClassImage\n"
238 "{\n"
239 " %%\n"
240 " %% Display a DirectClass image.\n"
241 " %%\n"
242 " colorspace 0 eq\n"
243 " {\n"
244 " /DeviceRGB setcolorspace\n"
245 " <<\n"
246 " /ImageType 1\n"
247 " /Width columns\n"
248 " /Height rows\n"
249 " /BitsPerComponent 8\n"
250 " /Decode [0 1 0 1 0 1]\n"
251 " /ImageMatrix [columns 0 0 rows neg 0 rows]\n"
252 " compression 0 gt\n"
253 " { /DataSource pixel_stream %s }\n"
254 " { /DataSource pixel_stream %s } ifelse\n"
255 " >> image\n"
256 " }\n"
257 " {\n"
258 " /DeviceCMYK setcolorspace\n"
259 " <<\n"
260 " /ImageType 1\n"
261 " /Width columns\n"
262 " /Height rows\n"
263 " /BitsPerComponent 8\n"
264 " /Decode [1 0 1 0 1 0 1 0]\n"
265 " /ImageMatrix [columns 0 0 rows neg 0 rows]\n"
266 " compression 0 gt\n"
267 " { /DataSource pixel_stream %s }\n"
268 " { /DataSource pixel_stream %s } ifelse\n"
269 " >> image\n"
270 " } ifelse\n"
271 "} bind def\n"
272 "\n"
273 "/PseudoClassImage\n"
274 "{\n"
275 " %%\n"
276 " %% Display a PseudoClass image.\n"
277 " %%\n"
278 " %% Parameters:\n"
279 " %% colors: number of colors in the colormap.\n"
280 " %%\n"
281 " currentfile buffer readline pop\n"
282 " token pop /colors exch def pop\n"
283 " colors 0 eq\n"
284 " {\n"
285 " %%\n"
286 " %% Image is grayscale.\n"
287 " %%\n"
288 " currentfile buffer readline pop\n"
289 " token pop /bits exch def pop\n"
290 " /DeviceGray setcolorspace\n"
291 " <<\n"
292 " /ImageType 1\n"
293 " /Width columns\n"
294 " /Height rows\n"
295 " /BitsPerComponent bits\n"
296 " /Decode [0 1]\n"
297 " /ImageMatrix [columns 0 0 rows neg 0 rows]\n"
298 " compression 0 gt\n"
299 " { /DataSource pixel_stream %s }\n"
300 " {\n"
301 " /DataSource pixel_stream %s\n"
302 " <<\n"
303 " /K " CCITTParam "\n"
304 " /Columns columns\n"
305 " /Rows rows\n"
306 " >> /CCITTFaxDecode filter\n"
307 " } ifelse\n"
308 " >> image\n"
309 " }\n"
310 " {\n"
311 " %%\n"
312 " %% Parameters:\n"
313 " %% colormap: red, green, blue color packets.\n"
314 " %%\n"
315 " /colormap colors 3 mul string def\n"
316 " currentfile colormap readhexstring pop pop\n"
317 " currentfile buffer readline pop\n"
318 " [ /Indexed /DeviceRGB colors 1 sub colormap ] setcolorspace\n"
319 " <<\n"
320 " /ImageType 1\n"
321 " /Width columns\n"
322 " /Height rows\n"
323 " /BitsPerComponent 8\n"
324 " /Decode [0 255]\n"
325 " /ImageMatrix [columns 0 0 rows neg 0 rows]\n"
326 " compression 0 gt\n"
327 " { /DataSource pixel_stream %s }\n"
328 " { /DataSource pixel_stream %s } ifelse\n"
329 " >> image\n"
330 " } ifelse\n"
331 "} bind def\n"
332 "\n"
333 "/DisplayImage\n"
334 "{\n"
335 " %%\n"
336 " %% Display a DirectClass or PseudoClass image.\n"
337 " %%\n"
338 " %% Parameters:\n"
339 " %% x & y translation.\n"
340 " %% x & y scale.\n"
341 " %% label pointsize.\n"
342 " %% image label.\n"
343 " %% image columns & rows.\n"
344 " %% class: 0-DirectClass or 1-PseudoClass.\n"
345 " %% colorspace: 0-RGB or 1-CMYK.\n"
346 " %% compression: 0-RLECompression or 1-NoCompression.\n"
347 " %% hex color packets.\n"
348 " %%\n"
349 " gsave\n"
350 " /buffer 512 string def\n"
351 " /pixel_stream currentfile def\n"
352 "\n"
353 " currentfile buffer readline pop\n"
354 " token pop /x exch def\n"
355 " token pop /y exch def pop\n"
356 " x y translate\n"
357 " currentfile buffer readline pop\n"
358 " token pop /x exch def\n"
359 " token pop /y exch def pop\n"
360 " currentfile buffer readline pop\n"
361 " token pop /pointsize exch def pop\n",
362 PostscriptEpilog[] =
363 " x y scale\n"
364 " currentfile buffer readline pop\n"
365 " token pop /columns exch def\n"
366 " token pop /rows exch def pop\n"
367 " currentfile buffer readline pop\n"
368 " token pop /class exch def pop\n"
369 " currentfile buffer readline pop\n"
370 " token pop /colorspace exch def pop\n"
371 " currentfile buffer readline pop\n"
372 " token pop /compression exch def pop\n"
373 " class 0 gt { PseudoClassImage } { DirectClassImage } ifelse\n"
374 " grestore\n";
375
376 char
377 buffer[MaxTextExtent],
378 date[MaxTextExtent],
379 page_geometry[MaxTextExtent],
380 **labels;
381
382 CompressionType
383 compression;
384
385 const char
386 *filter,
387 *value;
388
389 double
390 pointsize;
391
392 GeometryInfo
393 geometry_info;
394
395 MagickOffsetType
396 scene,
397 start,
398 stop;
399
400 MagickBooleanType
401 progress,
402 status;
403
404 MagickOffsetType
405 offset;
406
407 MagickSizeType
408 number_pixels;
409
410 MagickStatusType
411 flags;
412
413 PointInfo
414 delta,
415 resolution,
416 scale;
417
418 RectangleInfo
419 geometry,
420 media_info,
421 page_info;
422
423 const IndexPacket
424 *indexes;
425
426 const PixelPacket
427 *p;
428
429 ssize_t
430 x;
431
432 ssize_t
433 i;
434
435 SegmentInfo
436 bounds;
437
438 size_t
439 imageListLength,
440 length,
441 page,
442 text_size;
443
444 ssize_t
445 j,
446 y;
447
448 time_t
449 timer;
450
451 unsigned char
452 *pixels;
453
454 /*
455 Open output image file.
456 */
457 assert(image_info != (const ImageInfo *) NULL);
458 assert(image_info->signature == MagickCoreSignature);
459 assert(image != (Image *) NULL);
460 assert(image->signature == MagickCoreSignature);
461 if (image->debug != MagickFalse)
462 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
463 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
464 if (status == MagickFalse)
465 return(status);
466 compression=image->compression;
467 if (image_info->compression != UndefinedCompression)
468 compression=image_info->compression;
469 switch (compression)
470 {
471 #if !defined(MAGICKCORE_JPEG_DELEGATE)
472 case JPEGCompression:
473 {
474 compression=RLECompression;
475 (void) ThrowMagickException(&image->exception,GetMagickModule(),
476 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
477 image->filename);
478 break;
479 }
480 #endif
481 default:
482 break;
483 }
484 (void) memset(&bounds,0,sizeof(bounds));
485 page=1;
486 scene=0;
487 imageListLength=GetImageListLength(image);
488 do
489 {
490 /*
491 Scale relative to dots-per-inch.
492 */
493 delta.x=DefaultResolution;
494 delta.y=DefaultResolution;
495 resolution.x=image->x_resolution;
496 resolution.y=image->y_resolution;
497 if ((resolution.x == 0.0) || (resolution.y == 0.0))
498 {
499 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
500 if ((flags & RhoValue) != 0)
501 resolution.x=geometry_info.rho;
502 resolution.y=resolution.x;
503 if ((flags & SigmaValue) != 0)
504 resolution.y=geometry_info.sigma;
505 }
506 if (image_info->density != (char *) NULL)
507 {
508 flags=ParseGeometry(image_info->density,&geometry_info);
509 if ((flags & RhoValue) != 0)
510 resolution.x=geometry_info.rho;
511 resolution.y=resolution.x;
512 if ((flags & SigmaValue) != 0)
513 resolution.y=geometry_info.sigma;
514 }
515 if (image->units == PixelsPerCentimeterResolution)
516 {
517 resolution.x=(100.0*2.54*resolution.x+0.5)/100.0;
518 resolution.y=(100.0*2.54*resolution.y+0.5)/100.0;
519 }
520 SetGeometry(image,&geometry);
521 (void) FormatLocaleString(page_geometry,MaxTextExtent,"%.20gx%.20g",
522 (double) image->columns,(double) image->rows);
523 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))
527 (void) FormatLocaleString(page_geometry,MaxTextExtent,
528 "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,(double)
529 image->page.height,(double) image->page.x,(double) image->page.y);
530 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=PerceptibleReciprocal(resolution.x)*geometry.width*delta.x;
538 geometry.width=(size_t) floor(scale.x+0.5);
539 scale.y=PerceptibleReciprocal(resolution.y)*geometry.height*delta.y;
540 geometry.height=(size_t) floor(scale.y+0.5);
541 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
542 (void) ParseGravityGeometry(image,page_geometry,&page_info,
543 &image->exception);
544 if (image->gravity != UndefinedGravity)
545 {
546 geometry.x=(-page_info.x);
547 geometry.y=(ssize_t) (media_info.height+page_info.y-image->rows);
548 }
549 pointsize=12.0;
550 if (image_info->pointsize != 0.0)
551 pointsize=image_info->pointsize;
552 text_size=0;
553 value=GetImageProperty(image,"label");
554 if (value != (const char *) NULL)
555 text_size=(size_t) (MultilineCensus(value)*pointsize+12);
556 if (page == 1)
557 {
558 /*
559 Output Postscript header.
560 */
561 if (LocaleCompare(image_info->magick,"PS2") == 0)
562 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0\n",MaxTextExtent);
563 else
564 (void) CopyMagickString(buffer,"%!PS-Adobe-3.0 EPSF-3.0\n",
565 MaxTextExtent);
566 (void) WriteBlobString(image,buffer);
567 (void) WriteBlobString(image,"%%Creator: (ImageMagick)\n");
568 (void) FormatLocaleString(buffer,MaxTextExtent,"%%%%Title: (%s)\n",
569 image->filename);
570 (void) WriteBlobString(image,buffer);
571 timer=GetMagickTime();
572 (void) FormatMagickTime(timer,MaxTextExtent,date);
573 (void) FormatLocaleString(buffer,MaxTextExtent,
574 "%%%%CreationDate: (%s)\n",date);
575 (void) WriteBlobString(image,buffer);
576 bounds.x1=(double) geometry.x;
577 bounds.y1=(double) geometry.y;
578 bounds.x2=(double) geometry.x+geometry.width;
579 bounds.y2=(double) geometry.y+geometry.height+text_size;
580 if ((image_info->adjoin != MagickFalse) &&
581 (GetNextImageInList(image) != (Image *) NULL))
582 (void) CopyMagickString(buffer,"%%BoundingBox: (atend)\n",
583 MaxTextExtent);
584 else
585 {
586 (void) FormatLocaleString(buffer,MaxTextExtent,
587 "%%%%BoundingBox: %.20g %.20g %.20g %.20g\n",ceil(bounds.x1-0.5),
588 ceil(bounds.y1-0.5),floor(bounds.x2+0.5),floor(bounds.y2+0.5));
589 (void) WriteBlobString(image,buffer);
590 (void) FormatLocaleString(buffer,MaxTextExtent,
591 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,
592 bounds.y1,bounds.x2,bounds.y2);
593 }
594 (void) WriteBlobString(image,buffer);
595 value=GetImageProperty(image,"label");
596 if (value != (const char *) NULL)
597 (void) WriteBlobString(image,
598 "%%DocumentNeededResources: font Helvetica\n");
599 (void) WriteBlobString(image,"%%LanguageLevel: 2\n");
600 if (LocaleCompare(image_info->magick,"PS2") != 0)
601 (void) WriteBlobString(image,"%%Pages: 1\n");
602 else
603 {
604 (void) WriteBlobString(image,"%%Orientation: Portrait\n");
605 (void) WriteBlobString(image,"%%PageOrder: Ascend\n");
606 if (image_info->adjoin == MagickFalse)
607 (void) CopyMagickString(buffer,"%%Pages: 1\n",MaxTextExtent);
608 else
609 (void) FormatLocaleString(buffer,MaxTextExtent,
610 "%%%%Pages: %.20g\n",(double) GetImageListLength(image));
611 (void) WriteBlobString(image,buffer);
612 }
613 if (image->colorspace == CMYKColorspace)
614 (void) WriteBlobString(image,
615 "%%DocumentProcessColors: Cyan Magenta Yellow Black\n");
616 (void) WriteBlobString(image,"%%EndComments\n");
617 (void) WriteBlobString(image,"\n%%BeginDefaults\n");
618 (void) WriteBlobString(image,"%%EndDefaults\n\n");
619 /*
620 Output Postscript commands.
621 */
622 switch (compression)
623 {
624 case NoCompression:
625 {
626 filter="/ASCII85Decode filter";
627 break;
628 }
629 case JPEGCompression:
630 {
631 filter="/DCTDecode filter";
632 break;
633 }
634 case LZWCompression:
635 {
636 filter="/LZWDecode filter";
637 break;
638 }
639 case FaxCompression:
640 case Group4Compression:
641 {
642 filter=" ";
643 break;
644 }
645 default:
646 {
647 filter="/RunLengthDecode filter";
648 break;
649 }
650 }
651 (void) FormatLocaleString(buffer,MagickPathExtent,PostscriptProlog,
652 filter,filter,filter,filter,filter,filter,filter,filter);
653 (void) WriteBlob(image,strlen(buffer),(const unsigned char *) buffer);
654 value=GetImageProperty(image,"label");
655 if (value != (const char *) NULL)
656 {
657 (void) WriteBlobString(image,
658 " /Helvetica findfont pointsize scalefont setfont\n");
659 for (j=(ssize_t) MultilineCensus(value)-1; j >= 0; j--)
660 {
661 (void) WriteBlobString(image," /label 512 string def\n");
662 (void) WriteBlobString(image,
663 " currentfile label readline pop\n");
664 (void) FormatLocaleString(buffer,MaxTextExtent,
665 " 0 y %g add moveto label show pop\n",j*pointsize+12);
666 (void) WriteBlobString(image,buffer);
667 }
668 }
669 (void) WriteBlob(image,sizeof(PostscriptEpilog)-1,
670 (const unsigned char *) PostscriptEpilog);
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 }
676 (void) FormatLocaleString(buffer,MaxTextExtent,"%%%%Page: 1 %.20g\n",
677 (double) page++);
678 (void) WriteBlobString(image,buffer);
679 (void) FormatLocaleString(buffer,MaxTextExtent,
680 "%%%%PageBoundingBox: %.20g %.20g %.20g %.20g\n",(double) geometry.x,
681 (double) geometry.y,geometry.x+(double) geometry.width,geometry.y+(double)
682 (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;
692 value=GetImageProperty(image,"label");
693 if (value != (const char *) NULL)
694 (void) WriteBlobString(image,"%%PageResources: font Helvetica\n");
695 if (LocaleCompare(image_info->magick,"PS2") != 0)
696 (void) WriteBlobString(image,"userdict begin\n");
697 start=TellBlob(image);
698 (void) FormatLocaleString(buffer,MaxTextExtent,
699 "%%%%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 */
707 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n%g %g\n%g\n",
708 (double) geometry.x,(double) geometry.y,scale.x,scale.y,pointsize);
709 (void) WriteBlobString(image,buffer);
710 labels=(char **) NULL;
711 value=GetImageProperty(image,"label");
712 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 {
718 (void) FormatLocaleString(buffer,MaxTextExtent,"%s \n",
719 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) &&
730 (SetImageGray(image,&image->exception) != MagickFalse)))
731 {
732 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n1\n%d\n",
733 (double) image->columns,(double) image->rows,(int)
734 (image->colorspace == CMYKColorspace));
735 (void) WriteBlobString(image,buffer);
736 (void) FormatLocaleString(buffer,MaxTextExtent,"%d\n",
737 (int) ((compression != FaxCompression) &&
738 (compression != Group4Compression)));
739 (void) WriteBlobString(image,buffer);
740 (void) WriteBlobString(image,"0\n");
741 (void) FormatLocaleString(buffer,MaxTextExtent,"%d\n",
742 (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 {
752 (void) HuffmanEncodeImage(image_info,image,image);
753 break;
754 }
755 (void) Huffman2DEncodeImage(image_info,image,image);
756 break;
757 }
758 case JPEGCompression:
759 {
760 status=InjectImageBlob(image_info,image,image,"jpeg",
761 &image->exception);
762 if (status == MagickFalse)
763 ThrowWriterException(CoderError,image->exception.reason);
764 break;
765 }
766 case RLECompression:
767 default:
768 {
769 MemoryInfo
770 *pixel_info;
771
772 unsigned char
773 *q;
774
775 /*
776 Allocate pixel array.
777 */
778 length=(size_t) number_pixels;
779 pixel_info=AcquireVirtualMemory(length,sizeof(*pixels));
780 if (pixel_info == (MemoryInfo *) NULL)
781 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
782 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
783 /*
784 Dump runlength encoded pixels.
785 */
786 q=pixels;
787 for (y=0; y < (ssize_t) image->rows; y++)
788 {
789 p=GetVirtualPixels(image,0,y,image->columns,1,
790 &image->exception);
791 if (p == (const PixelPacket *) NULL)
792 break;
793 for (x=0; x < (ssize_t) image->columns; x++)
794 {
795 *q++=ScaleQuantumToChar(ClampToQuantum(
796 GetPixelLuma(image,p)));
797 p++;
798 }
799 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
800 image->rows);
801 if (progress == MagickFalse)
802 break;
803 }
804 length=(size_t) (q-pixels);
805 if (compression == LZWCompression)
806 status=LZWEncodeImage(image,length,pixels);
807 else
808 status=PackbitsEncodeImage(image,length,pixels);
809 pixel_info=RelinquishVirtualMemory(pixel_info);
810 if (status == MagickFalse)
811 {
812 (void) CloseBlob(image);
813 return(MagickFalse);
814 }
815 break;
816 }
817 case NoCompression:
818 {
819 /*
820 Dump uncompressed PseudoColor packets.
821 */
822 Ascii85Initialize(image);
823 for (y=0; y < (ssize_t) image->rows; y++)
824 {
825 p=GetVirtualPixels(image,0,y,image->columns,1,
826 &image->exception);
827 if (p == (const PixelPacket *) NULL)
828 break;
829 for (x=0; x < (ssize_t) image->columns; x++)
830 {
831 Ascii85Encode(image,ScaleQuantumToChar(ClampToQuantum(
832 GetPixelLuma(image,p))));
833 p++;
834 }
835 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
836 y,image->rows);
837 if (progress == MagickFalse)
838 break;
839 }
840 Ascii85Flush(image);
841 break;
842 }
843 }
844 }
845 else
846 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
847 (compression == JPEGCompression) || (image->matte != MagickFalse))
848 {
849 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n0\n%d\n",
850 (double) image->columns,(double) image->rows,(int)
851 (image->colorspace == CMYKColorspace));
852 (void) WriteBlobString(image,buffer);
853 (void) FormatLocaleString(buffer,MaxTextExtent,"%d\n",
854 (int) (compression == NoCompression));
855 (void) WriteBlobString(image,buffer);
856 switch (compression)
857 {
858 case JPEGCompression:
859 {
860 status=InjectImageBlob(image_info,image,image,"jpeg",
861 &image->exception);
862 if (status == MagickFalse)
863 ThrowWriterException(CoderError,image->exception.reason);
864 break;
865 }
866 case RLECompression:
867 default:
868 {
869 MemoryInfo
870 *pixel_info;
871
872 unsigned char
873 *q;
874
875 /*
876 Allocate pixel array.
877 */
878 length=(size_t) number_pixels;
879 pixel_info=AcquireVirtualMemory(length,4*sizeof(*pixels));
880 if (pixel_info == (MemoryInfo *) NULL)
881 ThrowWriterException(ResourceLimitError,
882 "MemoryAllocationFailed");
883 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
884 /*
885 Dump runlength encoded pixels.
886 */
887 q=pixels;
888 for (y=0; y < (ssize_t) image->rows; y++)
889 {
890 p=GetVirtualPixels(image,0,y,image->columns,1,
891 &image->exception);
892 if (p == (const PixelPacket *) NULL)
893 break;
894 indexes=GetVirtualIndexQueue(image);
895 for (x=0; x < (ssize_t) image->columns; x++)
896 {
897 if ((image->matte != MagickFalse) &&
898 (GetPixelOpacity(p) == (Quantum) TransparentOpacity))
899 {
900 *q++=ScaleQuantumToChar(QuantumRange);
901 *q++=ScaleQuantumToChar(QuantumRange);
902 *q++=ScaleQuantumToChar(QuantumRange);
903 }
904 else
905 if (image->colorspace != CMYKColorspace)
906 {
907 *q++=ScaleQuantumToChar(GetPixelRed(p));
908 *q++=ScaleQuantumToChar(GetPixelGreen(p));
909 *q++=ScaleQuantumToChar(GetPixelBlue(p));
910 }
911 else
912 {
913 *q++=ScaleQuantumToChar(GetPixelRed(p));
914 *q++=ScaleQuantumToChar(GetPixelGreen(p));
915 *q++=ScaleQuantumToChar(GetPixelBlue(p));
916 *q++=ScaleQuantumToChar(GetPixelIndex(
917 indexes+x));
918 }
919 p++;
920 }
921 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
922 y,image->rows);
923 if (progress == MagickFalse)
924 break;
925 }
926 length=(size_t) (q-pixels);
927 if (compression == LZWCompression)
928 status=LZWEncodeImage(image,length,pixels);
929 else
930 status=PackbitsEncodeImage(image,length,pixels);
931 if (status == MagickFalse)
932 {
933 (void) CloseBlob(image);
934 return(MagickFalse);
935 }
936 pixel_info=RelinquishVirtualMemory(pixel_info);
937 break;
938 }
939 case NoCompression:
940 {
941 /*
942 Dump uncompressed DirectColor packets.
943 */
944 Ascii85Initialize(image);
945 for (y=0; y < (ssize_t) image->rows; y++)
946 {
947 p=GetVirtualPixels(image,0,y,image->columns,1,
948 &image->exception);
949 if (p == (const PixelPacket *) NULL)
950 break;
951 indexes=GetVirtualIndexQueue(image);
952 for (x=0; x < (ssize_t) image->columns; x++)
953 {
954 if ((image->matte != MagickFalse) &&
955 (GetPixelOpacity(p) == (Quantum) TransparentOpacity))
956 {
957 Ascii85Encode(image,ScaleQuantumToChar((Quantum)
958 QuantumRange));
959 Ascii85Encode(image,ScaleQuantumToChar((Quantum)
960 QuantumRange));
961 Ascii85Encode(image,ScaleQuantumToChar((Quantum)
962 QuantumRange));
963 }
964 else
965 if (image->colorspace != CMYKColorspace)
966 {
967 Ascii85Encode(image,ScaleQuantumToChar(
968 GetPixelRed(p)));
969 Ascii85Encode(image,ScaleQuantumToChar(
970 GetPixelGreen(p)));
971 Ascii85Encode(image,ScaleQuantumToChar(
972 GetPixelBlue(p)));
973 }
974 else
975 {
976 Ascii85Encode(image,ScaleQuantumToChar(
977 GetPixelRed(p)));
978 Ascii85Encode(image,ScaleQuantumToChar(
979 GetPixelGreen(p)));
980 Ascii85Encode(image,ScaleQuantumToChar(
981 GetPixelBlue(p)));
982 Ascii85Encode(image,ScaleQuantumToChar(
983 GetPixelIndex(indexes+x)));
984 }
985 p++;
986 }
987 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
988 y,image->rows);
989 if (progress == MagickFalse)
990 break;
991 }
992 Ascii85Flush(image);
993 break;
994 }
995 }
996 }
997 else
998 {
999 /*
1000 Dump number of colors and colormap.
1001 */
1002 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n1\n%d\n",
1003 (double) image->columns,(double) image->rows,(int)
1004 (image->colorspace == CMYKColorspace));
1005 (void) WriteBlobString(image,buffer);
1006 (void) FormatLocaleString(buffer,MaxTextExtent,"%d\n",
1007 (int) (compression == NoCompression));
1008 (void) WriteBlobString(image,buffer);
1009 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
1010 image->colors);
1011 (void) WriteBlobString(image,buffer);
1012 for (i=0; i < (ssize_t) image->colors; i++)
1013 {
1014 (void) FormatLocaleString(buffer,MaxTextExtent,"%02X%02X%02X\n",
1015 ScaleQuantumToChar(image->colormap[i].red),
1016 ScaleQuantumToChar(image->colormap[i].green),
1017 ScaleQuantumToChar(image->colormap[i].blue));
1018 (void) WriteBlobString(image,buffer);
1019 }
1020 switch (compression)
1021 {
1022 case RLECompression:
1023 default:
1024 {
1025 MemoryInfo
1026 *pixel_info;
1027
1028 unsigned char
1029 *q;
1030
1031 /*
1032 Allocate pixel array.
1033 */
1034 length=(size_t) number_pixels;
1035 pixel_info=AcquireVirtualMemory(length,sizeof(*pixels));
1036 if (pixel_info == (MemoryInfo *) NULL)
1037 ThrowWriterException(ResourceLimitError,
1038 "MemoryAllocationFailed");
1039 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
1040 /*
1041 Dump runlength encoded pixels.
1042 */
1043 q=pixels;
1044 for (y=0; y < (ssize_t) image->rows; y++)
1045 {
1046 p=GetVirtualPixels(image,0,y,image->columns,1,
1047 &image->exception);
1048 if (p == (const PixelPacket *) NULL)
1049 break;
1050 indexes=GetVirtualIndexQueue(image);
1051 for (x=0; x < (ssize_t) image->columns; x++)
1052 *q++=(unsigned char) ((size_t) GetPixelIndex(indexes+x));
1053 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1054 y,image->rows);
1055 if (progress == MagickFalse)
1056 break;
1057 }
1058 length=(size_t) (q-pixels);
1059 if (compression == LZWCompression)
1060 status=LZWEncodeImage(image,length,pixels);
1061 else
1062 status=PackbitsEncodeImage(image,length,pixels);
1063 pixel_info=RelinquishVirtualMemory(pixel_info);
1064 if (status == MagickFalse)
1065 {
1066 (void) CloseBlob(image);
1067 return(MagickFalse);
1068 }
1069 break;
1070 }
1071 case NoCompression:
1072 {
1073 /*
1074 Dump uncompressed PseudoColor packets.
1075 */
1076 Ascii85Initialize(image);
1077 for (y=0; y < (ssize_t) image->rows; y++)
1078 {
1079 p=GetVirtualPixels(image,0,y,image->columns,1,
1080 &image->exception);
1081 if (p == (const PixelPacket *) NULL)
1082 break;
1083 indexes=GetVirtualIndexQueue(image);
1084 for (x=0; x < (ssize_t) image->columns; x++)
1085 Ascii85Encode(image,(unsigned char) GetPixelIndex(
1086 indexes+x));
1087 progress=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1088 y,image->rows);
1089 if (progress == MagickFalse)
1090 break;
1091 }
1092 Ascii85Flush(image);
1093 break;
1094 }
1095 }
1096 }
1097 (void) WriteBlobByte(image,'\n');
1098 length=(size_t) (TellBlob(image)-stop);
1099 stop=TellBlob(image);
1100 offset=SeekBlob(image,start,SEEK_SET);
1101 if (offset < 0)
1102 ThrowWriterException(CorruptImageError,"ImproperImageHeader");
1103 (void) FormatLocaleString(buffer,MaxTextExtent,
1104 "%%%%BeginData:%13ld %s Bytes\n",(long) length,
1105 compression == NoCompression ? "ASCII" : "Binary");
1106 (void) WriteBlobString(image,buffer);
1107 (void) SeekBlob(image,stop,SEEK_SET);
1108 (void) WriteBlobString(image,"%%EndData\n");
1109 if (LocaleCompare(image_info->magick,"PS2") != 0)
1110 (void) WriteBlobString(image,"end\n");
1111 (void) WriteBlobString(image,"%%PageTrailer\n");
1112 if (GetNextImageInList(image) == (Image *) NULL)
1113 break;
1114 image=SyncNextImageInList(image);
1115 status=SetImageProgress(image,SaveImagesTag,scene++,imageListLength);
1116 if (status == MagickFalse)
1117 break;
1118 } while (image_info->adjoin != MagickFalse);
1119 (void) WriteBlobString(image,"%%Trailer\n");
1120 if (page > 1)
1121 {
1122 (void) FormatLocaleString(buffer,MaxTextExtent,
1123 "%%%%BoundingBox: %.20g %.20g %.20g %.20g\n",ceil(bounds.x1-0.5),
1124 ceil(bounds.y1-0.5),floor(bounds.x2+0.5),floor(bounds.y2+0.5));
1125 (void) WriteBlobString(image,buffer);
1126 (void) FormatLocaleString(buffer,MaxTextExtent,
1127 "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,bounds.y1,
1128 bounds.x2,bounds.y2);
1129 (void) WriteBlobString(image,buffer);
1130 }
1131 (void) WriteBlobString(image,"%%EOF\n");
1132 (void) CloseBlob(image);
1133 return(MagickTrue);
1134 }
1135