blob: 2c6427e018981d18788d908ed1f7b3fd86155a97 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% PPPP CCCC L %
7% P P C L %
8% PPPP C L %
9% P C L %
10% P CCCC LLLLL %
11% %
12% %
13% Read/Write HP PCL Printer Format %
14% %
15% Software Design %
16% John Cristy %
17% July 1992 %
18% %
19% %
cristy16af1cb2009-12-11 21:38:29 +000020% Copyright 1999-2010 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*/
42#include "magick/studio.h"
43#include "magick/property.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/colorspace.h"
50#include "magick/constitute.h"
51#include "magick/delegate.h"
52#include "magick/draw.h"
53#include "magick/exception.h"
54#include "magick/exception-private.h"
55#include "magick/geometry.h"
56#include "magick/image.h"
57#include "magick/image-private.h"
58#include "magick/list.h"
59#include "magick/magick.h"
60#include "magick/memory_.h"
61#include "magick/monitor.h"
62#include "magick/monitor-private.h"
cristy6ccaa8a2009-09-28 12:49:48 +000063#include "magick/option.h"
cristy3ed852e2009-09-05 21:47:34 +000064#include "magick/profile.h"
65#include "magick/resource_.h"
66#include "magick/quantum-private.h"
67#include "magick/static.h"
68#include "magick/string_.h"
69#include "magick/module.h"
70#include "magick/token.h"
71#include "magick/transform.h"
72#include "magick/utility.h"
73
74/*
75 Forward declarations.
76*/
77static MagickBooleanType
78 WritePCLImage(const ImageInfo *,Image *);
79
80/*
81%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82% %
83% %
84% %
85% I s P C L %
86% %
87% %
88% %
89%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90%
91% IsPCL() returns MagickTrue if the image format type, identified by the
92% magick string, is PCL.
93%
94% The format of the IsPCL method is:
95%
96% MagickBooleanType IsPCL(const unsigned char *magick,const size_t length)
97%
98% A description of each parameter follows:
99%
100% o magick: compare image format pattern against these bytes.
101%
102% o length: Specifies the length of the magick string.
103%
104*/
105static MagickBooleanType IsPCL(const unsigned char *magick,const size_t length)
106{
107 if (length < 4)
108 return(MagickFalse);
109 if (memcmp(magick,"\033E\033",3) == 0)
110 return(MagickTrue);
111 if (memcmp(magick,"\033E\033&",4) == 0)
112 return(MagickFalse);
113 return(MagickFalse);
114}
115
116/*
117%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
118% %
119% %
120% %
121% R e a d P C L I m a g e %
122% %
123% %
124% %
125%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
126%
127% ReadPCLImage() reads a Printer Control Language image file and returns it.
128% It allocates the memory necessary for the new Image structure and returns a
129% pointer to the new image.
130%
131% The format of the ReadPCLImage method is:
132%
133% Image *ReadPCLImage(const ImageInfo *image_info,ExceptionInfo *exception)
134%
135% A description of each parameter follows:
136%
137% o image_info: the image info.
138%
139% o exception: return any errors or warnings in this structure.
140%
141*/
142static Image *ReadPCLImage(const ImageInfo *image_info,ExceptionInfo *exception)
143{
144#define CropBox "CropBox"
145#define DeviceCMYK "DeviceCMYK"
146#define MediaBox "MediaBox"
147#define RenderPCLText " Rendering PCL... "
148
149 char
150 command[MaxTextExtent],
151 density[MaxTextExtent],
152 filename[MaxTextExtent],
153 geometry[MaxTextExtent],
154 options[MaxTextExtent],
155 input_filename[MaxTextExtent];
156
157 const DelegateInfo
158 *delegate_info;
159
160 Image
161 *image,
162 *next_image;
163
164 ImageInfo
165 *read_info;
166
167 MagickBooleanType
168 cmyk,
169 status;
170
171 PointInfo
172 delta;
173
174 RectangleInfo
175 bounding_box,
176 page;
177
178 register char
179 *p;
180
cristybb503372010-05-27 20:51:26 +0000181 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000182 c;
183
184 SegmentInfo
185 bounds;
186
187 ssize_t
188 count;
189
cristybb503372010-05-27 20:51:26 +0000190 size_t
cristy3ed852e2009-09-05 21:47:34 +0000191 height,
192 width;
193
194 assert(image_info != (const ImageInfo *) NULL);
195 assert(image_info->signature == MagickSignature);
196 if (image_info->debug != MagickFalse)
197 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
198 image_info->filename);
199 assert(exception != (ExceptionInfo *) NULL);
200 assert(exception->signature == MagickSignature);
201 /*
202 Open image file.
203 */
204 image=AcquireImage(image_info);
205 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
206 if (status == MagickFalse)
207 {
208 image=DestroyImageList(image);
209 return((Image *) NULL);
210 }
211 status=AcquireUniqueSymbolicLink(image_info->filename,input_filename);
212 if (status == MagickFalse)
213 {
214 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
215 image_info->filename);
216 image=DestroyImageList(image);
217 return((Image *) NULL);
218 }
219 /*
220 Set the page density.
221 */
222 delta.x=DefaultResolution;
223 delta.y=DefaultResolution;
224 if ((image->x_resolution == 0.0) || (image->y_resolution == 0.0))
225 {
226 GeometryInfo
227 geometry_info;
228
229 MagickStatusType
230 flags;
231
232 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
233 image->x_resolution=geometry_info.rho;
234 image->y_resolution=geometry_info.sigma;
235 if ((flags & SigmaValue) == 0)
236 image->y_resolution=image->x_resolution;
237 }
238 /*
239 Determine page geometry from the PCL media box.
240 */
241 cmyk=image->colorspace == CMYKColorspace ? MagickTrue : MagickFalse;
242 count=0;
243 (void) ResetMagickMemory(&bounding_box,0,sizeof(bounding_box));
244 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
245 (void) ResetMagickMemory(&page,0,sizeof(page));
246 (void) ResetMagickMemory(command,0,sizeof(command));
247 p=command;
248 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
249 {
250 if (image_info->page != (char *) NULL)
251 continue;
252 /*
253 Note PCL elements.
254 */
255 *p++=(char) c;
256 if ((c != (int) '/') && (c != '\n') &&
257 ((size_t) (p-command) < (MaxTextExtent-1)))
258 continue;
259 *p='\0';
260 p=command;
261 /*
262 Is this a CMYK document?
263 */
264 if (LocaleNCompare(DeviceCMYK,command,strlen(DeviceCMYK)) == 0)
265 cmyk=MagickTrue;
266 if (LocaleNCompare(CropBox,command,strlen(CropBox)) == 0)
267 {
268 /*
269 Note region defined by crop box.
270 */
271 count=(ssize_t) sscanf(command,"CropBox [%lf %lf %lf %lf",
272 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
273 if (count != 4)
274 count=(ssize_t) sscanf(command,"CropBox[%lf %lf %lf %lf",
275 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
276 }
277 if (LocaleNCompare(MediaBox,command,strlen(MediaBox)) == 0)
278 {
279 /*
280 Note region defined by media box.
281 */
282 count=(ssize_t) sscanf(command,"MediaBox [%lf %lf %lf %lf",
283 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
284 if (count != 4)
285 count=(ssize_t) sscanf(command,"MediaBox[%lf %lf %lf %lf",
286 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
287 }
288 if (count != 4)
289 continue;
290 /*
291 Set PCL render geometry.
292 */
cristybb503372010-05-27 20:51:26 +0000293 width=(size_t) floor(bounds.x2-bounds.x1+0.5);
294 height=(size_t) floor(bounds.y2-bounds.y1+0.5);
cristy3ed852e2009-09-05 21:47:34 +0000295 if (width > page.width)
296 page.width=width;
297 if (height > page.height)
298 page.height=height;
299 }
300 (void) CloseBlob(image);
301 /*
302 Render PCL with the GhostPCL delegate.
303 */
304 if ((page.width == 0) || (page.height == 0))
305 (void) ParseAbsoluteGeometry(PSPageGeometry,&page);
306 if (image_info->page != (char *) NULL)
307 (void) ParseAbsoluteGeometry(image_info->page,&page);
cristye8c25f92010-06-03 00:53:06 +0000308 (void) FormatMagickString(geometry,MaxTextExtent,"%.20gx%.20g",(double)
309 page.width,(double) page.height);
cristy3ed852e2009-09-05 21:47:34 +0000310 if (image_info->monochrome != MagickFalse)
311 delegate_info=GetDelegateInfo("pcl:mono",(char *) NULL,exception);
312 else
313 if (cmyk != MagickFalse)
314 delegate_info=GetDelegateInfo("pcl:cmyk",(char *) NULL,exception);
315 else
316 delegate_info=GetDelegateInfo("pcl:color",(char *) NULL,exception);
317 if (delegate_info == (const DelegateInfo *) NULL)
318 return((Image *) NULL);
319 *options='\0';
320 if ((page.width == 0) || (page.height == 0))
321 (void) ParseAbsoluteGeometry(PSPageGeometry,&page);
322 if (image_info->page != (char *) NULL)
323 (void) ParseAbsoluteGeometry(image_info->page,&page);
cristye7f51092010-01-17 00:39:37 +0000324 (void) FormatMagickString(density,MaxTextExtent,"%gx%g",
cristy3ed852e2009-09-05 21:47:34 +0000325 image->x_resolution,image->y_resolution);
cristybb503372010-05-27 20:51:26 +0000326 page.width=(size_t) floor(page.width*image->x_resolution/delta.x+0.5);
327 page.height=(size_t) floor(page.height*image->y_resolution/delta.y+
cristy06609ee2010-03-17 20:21:27 +0000328 0.5);
cristye8c25f92010-06-03 00:53:06 +0000329 (void) FormatMagickString(options,MaxTextExtent,"-g%.20gx%.20g ",(double)
330 page.width,(double) page.height);
cristy3ed852e2009-09-05 21:47:34 +0000331 image=DestroyImage(image);
332 read_info=CloneImageInfo(image_info);
333 *read_info->magick='\0';
334 if (read_info->number_scenes != 0)
335 {
336 if (read_info->number_scenes != 1)
cristye8c25f92010-06-03 00:53:06 +0000337 (void) FormatMagickString(options,MaxTextExtent,"-dLastPage=%.20g",
338 (double) (read_info->scene+read_info->number_scenes));
cristy3ed852e2009-09-05 21:47:34 +0000339 else
340 (void) FormatMagickString(options,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +0000341 "-dFirstPage=%.20g -dLastPage=%.20g",(double) read_info->scene+1,
342 (double) (read_info->scene+read_info->number_scenes));
cristy3ed852e2009-09-05 21:47:34 +0000343 read_info->number_scenes=0;
344 if (read_info->scenes != (char *) NULL)
345 *read_info->scenes='\0';
346 }
347 if (read_info->authenticate != (char *) NULL)
348 (void) FormatMagickString(options+strlen(options),MaxTextExtent,
349 " -sPCLPassword=%s",read_info->authenticate);
350 (void) CopyMagickString(filename,read_info->filename,MaxTextExtent);
351 (void) AcquireUniqueFilename(read_info->filename);
352 (void) FormatMagickString(command,MaxTextExtent,
353 GetDelegateCommands(delegate_info),
354 read_info->antialias != MagickFalse ? 4 : 1,
355 read_info->antialias != MagickFalse ? 4 : 1,density,options,
356 read_info->filename,input_filename);
cristy6de4bc22010-01-12 17:10:35 +0000357 status=SystemCommand(MagickFalse,read_info->verbose,command,exception) != 0 ?
358 MagickTrue : MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000359 image=ReadImage(read_info,exception);
360 (void) RelinquishUniqueFileResource(read_info->filename);
361 (void) RelinquishUniqueFileResource(input_filename);
362 read_info=DestroyImageInfo(read_info);
363 if (image == (Image *) NULL)
364 ThrowReaderException(DelegateError,"PCLDelegateFailed");
365 if (LocaleCompare(image->magick,"BMP") == 0)
366 {
367 Image
368 *cmyk_image;
369
370 cmyk_image=ConsolidateCMYKImages(image,&image->exception);
371 if (cmyk_image != (Image *) NULL)
372 {
373 image=DestroyImageList(image);
374 image=cmyk_image;
375 }
376 }
377 do
378 {
379 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
380 image->page=page;
381 next_image=SyncNextImageInList(image);
382 if (next_image != (Image *) NULL)
383 image=next_image;
384 } while (next_image != (Image *) NULL);
385 return(GetFirstImageInList(image));
386}
387
388/*
389%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
390% %
391% %
392% %
393% R e g i s t e r P C L I m a g e %
394% %
395% %
396% %
397%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
398%
399% RegisterPCLImage() adds attributes for the PCL image format to
400% the list of supported formats. The attributes include the image format
401% tag, a method to read and/or write the format, whether the format
402% supports the saving of more than one frame to the i file or blob,
403% whether the format supports native in-memory I/O, and a brief
404% description of the format.
405%
406% The format of the RegisterPCLImage method is:
407%
cristybb503372010-05-27 20:51:26 +0000408% size_t RegisterPCLImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000409%
410*/
cristybb503372010-05-27 20:51:26 +0000411ModuleExport size_t RegisterPCLImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000412{
413 MagickInfo
414 *entry;
415
416 entry=SetMagickInfo("PCL");
417 entry->decoder=(DecodeImageHandler *) ReadPCLImage;
418 entry->encoder=(EncodeImageHandler *) WritePCLImage;
419 entry->magick=(IsImageFormatHandler *) IsPCL;
420 entry->blob_support=MagickFalse;
421 entry->seekable_stream=MagickTrue;
422 entry->thread_support=EncoderThreadSupport;
423 entry->description=ConstantString("Printer Control Language");
424 entry->module=ConstantString("PCL");
425 (void) RegisterMagickInfo(entry);
426 return(MagickImageCoderSignature);
427}
428
429/*
430%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
431% %
432% %
433% %
434% U n r e g i s t e r P C L I m a g e %
435% %
436% %
437% %
438%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
439%
440% UnregisterPCLImage() removes format registrations made by the PCL module
441% from the list of supported formats.
442%
443% The format of the UnregisterPCLImage method is:
444%
445% UnregisterPCLImage(void)
446%
447*/
448ModuleExport void UnregisterPCLImage(void)
449{
450 (void) UnregisterMagickInfo("PCL");
451}
452
453/*
454%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
455% %
456% %
457% %
458% W r i t e P C L I m a g e %
459% %
460% %
461% %
462%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
463%
464% WritePCLImage() writes an image in the Page Control Language encoded
465% image format.
466%
467% The format of the WritePCLImage method is:
468%
469% MagickBooleanType WritePCLImage(const ImageInfo *image_info,Image *image)
470%
471% A description of each parameter follows.
472%
473% o image_info: the image info.
474%
475% o image: The image.
476%
477*/
478
479static size_t PCLDeltaCompressImage(const size_t length,
480 const unsigned char *previous_pixels,const unsigned char *pixels,
481 unsigned char *compress_pixels)
482{
483 int
484 delta,
485 j,
486 replacement;
487
cristybb503372010-05-27 20:51:26 +0000488 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000489 i,
490 x;
491
492 register unsigned char
493 *q;
494
495 q=compress_pixels;
cristybb503372010-05-27 20:51:26 +0000496 for (x=0; x < (ssize_t) length; )
cristy3ed852e2009-09-05 21:47:34 +0000497 {
498 j=0;
cristybb503372010-05-27 20:51:26 +0000499 for (i=0; x < (ssize_t) length; x++)
cristy3ed852e2009-09-05 21:47:34 +0000500 {
501 if (*pixels++ != *previous_pixels++)
502 {
503 i=1;
504 break;
505 }
506 j++;
507 }
cristybb503372010-05-27 20:51:26 +0000508 for ( ; x < (ssize_t) length; x++)
cristy3ed852e2009-09-05 21:47:34 +0000509 {
510 if (*pixels == *previous_pixels)
511 break;
512 i++;
513 previous_pixels++;
514 pixels++;
515 }
516 if (i == 0)
517 break;
518 replacement=j >= 31 ? 31 : j;
519 j-=replacement;
520 delta=i >= 8 ? 8 : i;
521 *q++=(unsigned char) (((delta-1) << 5) | replacement);
522 if (replacement == 31)
523 {
524 for (replacement=255; j != 0; )
525 {
526 if (replacement > j)
527 replacement=j;
528 *q++=(unsigned char) replacement;
529 j-=replacement;
530 }
531 if (replacement == 255)
532 *q++='\0';
533 }
534 for (pixels-=i; i != 0; )
535 {
536 for (i-=delta; delta != 0; delta--)
537 *q++=(*pixels++);
538 if (i == 0)
539 break;
540 delta=i;
541 if (i >= 8)
542 delta=8;
543 *q++=(unsigned char) ((delta-1) << 5);
544 }
545 }
546 return((size_t) (q-compress_pixels));
547}
548
549static size_t PCLPackbitsCompressImage(const size_t length,
550 const unsigned char *pixels,unsigned char *compress_pixels)
551{
552 int
553 count;
554
cristybb503372010-05-27 20:51:26 +0000555 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000556 j;
557
cristybb503372010-05-27 20:51:26 +0000558 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000559 x;
560
561 register unsigned char
562 *q;
563
564 unsigned char
565 packbits[128];
566
567 /*
568 Compress pixels with Packbits encoding.
569 */
570 q=compress_pixels;
cristybb503372010-05-27 20:51:26 +0000571 for (x=(ssize_t) length; x != 0; )
cristy3ed852e2009-09-05 21:47:34 +0000572 {
573 switch (x)
574 {
575 case 1:
576 {
577 x--;
578 *q++=0;
579 *q++=(*pixels);
580 break;
581 }
582 case 2:
583 {
584 x-=2;
585 *q++=1;
586 *q++=(*pixels);
587 *q++=pixels[1];
588 break;
589 }
590 case 3:
591 {
592 x-=3;
593 if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
594 {
595 *q++=(unsigned char) ((256-3)+1);
596 *q++=(*pixels);
597 break;
598 }
599 *q++=2;
600 *q++=(*pixels);
601 *q++=pixels[1];
602 *q++=pixels[2];
603 break;
604 }
605 default:
606 {
607 if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
608 {
609 /*
610 Packed run.
611 */
612 count=3;
cristybb503372010-05-27 20:51:26 +0000613 while (((ssize_t) count < x) && (*pixels == *(pixels+count)))
cristy3ed852e2009-09-05 21:47:34 +0000614 {
615 count++;
616 if (count >= 127)
617 break;
618 }
619 x-=count;
620 *q++=(unsigned char) ((256-count)+1);
621 *q++=(*pixels);
622 pixels+=count;
623 break;
624 }
625 /*
626 Literal run.
627 */
628 count=0;
629 while ((*(pixels+count) != *(pixels+count+1)) ||
630 (*(pixels+count+1) != *(pixels+count+2)))
631 {
632 packbits[count+1]=pixels[count];
633 count++;
cristybb503372010-05-27 20:51:26 +0000634 if (((ssize_t) count >= (x-3)) || (count >= 127))
cristy3ed852e2009-09-05 21:47:34 +0000635 break;
636 }
637 x-=count;
638 *packbits=(unsigned char) (count-1);
cristybb503372010-05-27 20:51:26 +0000639 for (j=0; j <= (ssize_t) count; j++)
cristy3ed852e2009-09-05 21:47:34 +0000640 *q++=packbits[j];
641 pixels+=count;
642 break;
643 }
644 }
645 }
646 *q++=128; /* EOD marker */
647 return((size_t) (q-compress_pixels));
648}
649
650static MagickBooleanType WritePCLImage(const ImageInfo *image_info,Image *image)
651{
652 char
653 buffer[MaxTextExtent];
654
cristy688f07b2009-09-27 15:19:13 +0000655 const char
656 *option;
657
cristybb503372010-05-27 20:51:26 +0000658 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000659 y;
660
661 MagickBooleanType
662 status;
663
664 MagickOffsetType
665 scene;
666
667 register const IndexPacket
668 *indexes;
669
670 register const PixelPacket
671 *p;
672
cristybb503372010-05-27 20:51:26 +0000673 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000674 i,
675 x;
676
677 register unsigned char
678 *q;
679
680 size_t
681 length,
682 packets;
683
684 unsigned char
685 bits_per_pixel,
686 *compress_pixels,
687 *pixels,
688 *previous_pixels;
689
cristybb503372010-05-27 20:51:26 +0000690 size_t
cristy3ed852e2009-09-05 21:47:34 +0000691 density;
692
693 /*
694 Open output image file.
695 */
696 assert(image_info != (const ImageInfo *) NULL);
697 assert(image_info->signature == MagickSignature);
698 assert(image != (Image *) NULL);
699 assert(image->signature == MagickSignature);
700 if (image->debug != MagickFalse)
701 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
702 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
703 if (status == MagickFalse)
704 return(status);
705 density=75;
706 if (image_info->density != (char *) NULL)
707 {
708 GeometryInfo
709 geometry;
710
711 (void) ParseGeometry(image_info->density,&geometry);
cristybb503372010-05-27 20:51:26 +0000712 density=(size_t) geometry.rho;
cristy3ed852e2009-09-05 21:47:34 +0000713 }
714 scene=0;
715 do
716 {
717 if (image->colorspace != RGBColorspace)
718 (void) TransformImageColorspace(image,RGBColorspace);
719 /*
720 Initialize the printer.
721 */
722 (void) WriteBlobString(image,"\033E"); /* printer reset */
723 (void) WriteBlobString(image,"\033*r3F"); /* set presentation mode */
cristye8c25f92010-06-03 00:53:06 +0000724 (void) FormatMagickString(buffer,MaxTextExtent,"\033*r%.20gs%.20gT",
725 (double) image->columns,(double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000726 (void) WriteBlobString(image,buffer);
cristye8c25f92010-06-03 00:53:06 +0000727 (void) FormatMagickString(buffer,MaxTextExtent,"\033*t%.20gR",(double)
728 density);
cristy3ed852e2009-09-05 21:47:34 +0000729 (void) WriteBlobString(image,buffer);
730 (void) WriteBlobString(image,"\033&l0E"); /* top margin 0 */
731 if (IsMonochromeImage(image,&image->exception) != MagickFalse)
732 {
733 /*
734 Monochrome image.
735 */
736 bits_per_pixel=1;
737 (void) WriteBlobString(image,"\033*v6W"); /* set color mode... */
738 (void) WriteBlobByte(image,0); /* RGB */
739 (void) WriteBlobByte(image,1); /* indexed by pixel */
740 (void) WriteBlobByte(image,bits_per_pixel); /* bits per index */
741 (void) WriteBlobByte(image,8); /* bits per red component */
742 (void) WriteBlobByte(image,8); /* bits per green component */
743 (void) WriteBlobByte(image,8); /* bits per blue component */
744 (void) FormatMagickString(buffer,MaxTextExtent,"\033*v0a0b0c0I");
745 (void) WriteBlobString(image,buffer);
746 (void) FormatMagickString(buffer,MaxTextExtent,"\033*v1a1b1c1I");
747 (void) WriteBlobString(image,buffer);
748 }
749 else
750 if (image->storage_class == DirectClass)
751 {
752 /*
753 DirectClass image.
754 */
755 bits_per_pixel=24;
756 (void) WriteBlobString(image,"\033*v6W"); /* set color mode */
757 (void) WriteBlobByte(image,0); /* RGB */
758 (void) WriteBlobByte(image,3); /* direct by pixel */
759 (void) WriteBlobByte(image,0); /* bits per index (ignored) */
760 (void) WriteBlobByte(image,8); /* bits per red component */
761 (void) WriteBlobByte(image,8); /* bits per green component */
762 (void) WriteBlobByte(image,8); /* bits per blue component */
763 }
764 else
765 {
766 /*
767 Colormapped image.
768 */
769 bits_per_pixel=8;
770 (void) WriteBlobString(image,"\033*v6W"); /* set color mode... */
771 (void) WriteBlobByte(image,0); /* RGB */
772 (void) WriteBlobByte(image,1); /* indexed by pixel */
773 (void) WriteBlobByte(image,bits_per_pixel); /* bits per index */
774 (void) WriteBlobByte(image,8); /* bits per red component */
775 (void) WriteBlobByte(image,8); /* bits per green component */
776 (void) WriteBlobByte(image,8); /* bits per blue component */
cristybb503372010-05-27 20:51:26 +0000777 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000778 {
779 (void) FormatMagickString(buffer,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +0000780 "\033*v%da%db%dc%.20gI",
781 ScaleQuantumToChar(image->colormap[i].red),
cristy3ed852e2009-09-05 21:47:34 +0000782 ScaleQuantumToChar(image->colormap[i].green),
cristye8c25f92010-06-03 00:53:06 +0000783 ScaleQuantumToChar(image->colormap[i].blue),(double) i);
cristy3ed852e2009-09-05 21:47:34 +0000784 (void) WriteBlobString(image,buffer);
785 }
786 for ( ; i < (1L << bits_per_pixel); i++)
787 {
cristye8c25f92010-06-03 00:53:06 +0000788 (void) FormatMagickString(buffer,MaxTextExtent,"\033*v%.20gI",
789 (double) i);
cristy3ed852e2009-09-05 21:47:34 +0000790 (void) WriteBlobString(image,buffer);
791 }
792 }
cristy688f07b2009-09-27 15:19:13 +0000793 option=GetImageOption(image_info,"pcl:fit-to-page");
794 if ((option != (const char *) NULL) &&
795 (IsMagickTrue(option) != MagickFalse))
796 (void) WriteBlobString(image,"\033*r3A");
797 else
798 (void) WriteBlobString(image,"\033*r1A"); /* start raster graphics */
cristy3ed852e2009-09-05 21:47:34 +0000799 (void) WriteBlobString(image,"\033*b0Y"); /* set y offset */
800 length=(image->columns*bits_per_pixel+7)/8;
801 pixels=(unsigned char *) AcquireQuantumMemory(length,sizeof(*pixels));
802 if (pixels == (unsigned char *) NULL)
803 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
804 compress_pixels=(unsigned char *) NULL;
805 previous_pixels=(unsigned char *) NULL;
806 switch (image->compression)
807 {
808 case NoCompression:
809 {
810 (void) FormatMagickString(buffer,MaxTextExtent,"\033*b0M");
811 (void) WriteBlobString(image,buffer);
812 break;
813 }
814 case RLECompression:
815 {
816 compress_pixels=(unsigned char *) AcquireQuantumMemory(length+256,
817 sizeof(*compress_pixels));
818 if (compress_pixels == (unsigned char *) NULL)
819 {
820 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
821 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
822 }
823 (void) FormatMagickString(buffer,MaxTextExtent,"\033*b2M");
824 (void) WriteBlobString(image,buffer);
825 break;
826 }
827 default:
828 {
829 compress_pixels=(unsigned char *) AcquireQuantumMemory(length+
830 (length >> 3),sizeof(*compress_pixels));
831 if (compress_pixels == (unsigned char *) NULL)
832 {
833 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
834 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
835 }
836 previous_pixels=(unsigned char *) AcquireQuantumMemory(length,
837 sizeof(*previous_pixels));
838 if (previous_pixels == (unsigned char *) NULL)
839 {
840 compress_pixels=(unsigned char *) RelinquishMagickMemory(
841 compress_pixels);
842 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
843 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
844 }
845 (void) FormatMagickString(buffer,MaxTextExtent,"\033*b3M");
846 (void) WriteBlobString(image,buffer);
847 break;
848 }
849 }
cristybb503372010-05-27 20:51:26 +0000850 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000851 {
852 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
853 if (p == (const PixelPacket *) NULL)
854 break;
855 indexes=GetAuthenticIndexQueue(image);
856 q=pixels;
857 switch (bits_per_pixel)
858 {
859 case 1:
860 {
861 register unsigned char
862 bit,
863 byte;
864
865 /*
866 Monochrome image.
867 */
868 bit=0;
869 byte=0;
cristybb503372010-05-27 20:51:26 +0000870 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000871 {
872 byte<<=1;
873 if (PixelIntensity(p) >= ((MagickRealType) QuantumRange/2.0))
874 byte|=0x01;
875 bit++;
876 if (bit == 8)
877 {
878 *q++=byte;
879 bit=0;
880 byte=0;
881 }
882 p++;
883 }
884 if (bit != 0)
885 *q++=byte << (8-bit);
886 break;
887 }
888 case 8:
889 {
890 /*
891 Colormapped image.
892 */
cristybb503372010-05-27 20:51:26 +0000893 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000894 *q++=(unsigned char) indexes[x];
895 break;
896 }
897 case 24:
898 case 32:
899 {
900 /*
901 Truecolor image.
902 */
cristybb503372010-05-27 20:51:26 +0000903 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000904 {
cristyce70c172010-01-07 17:15:30 +0000905 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
906 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
907 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
cristy3ed852e2009-09-05 21:47:34 +0000908 p++;
909 }
910 break;
911 }
912 }
913 switch (image->compression)
914 {
915 case NoCompression:
916 {
cristye8c25f92010-06-03 00:53:06 +0000917 (void) FormatMagickString(buffer,MaxTextExtent,"\033*b%.20gW",
918 (double) length);
cristy3ed852e2009-09-05 21:47:34 +0000919 (void) WriteBlobString(image,buffer);
920 (void) WriteBlob(image,length,pixels);
921 break;
922 }
923 case RLECompression:
924 {
925 packets=PCLPackbitsCompressImage(length,pixels,
926 compress_pixels);
cristye8c25f92010-06-03 00:53:06 +0000927 (void) FormatMagickString(buffer,MaxTextExtent,"\033*b%.20gW",
928 (double) packets);
cristy3ed852e2009-09-05 21:47:34 +0000929 (void) WriteBlobString(image,buffer);
930 (void) WriteBlob(image,packets,compress_pixels);
931 break;
932 }
933 default:
934 {
935 if (y == 0)
cristybb503372010-05-27 20:51:26 +0000936 for (i=0; i < (ssize_t) length; i++)
cristy3ed852e2009-09-05 21:47:34 +0000937 previous_pixels[i]=(~pixels[i]);
938 packets=PCLDeltaCompressImage(length,previous_pixels,pixels,
939 compress_pixels);
cristye8c25f92010-06-03 00:53:06 +0000940 (void) FormatMagickString(buffer,MaxTextExtent,"\033*b%.20gW",
941 (double) packets);
cristy3ed852e2009-09-05 21:47:34 +0000942 (void) WriteBlobString(image,buffer);
943 (void) WriteBlob(image,packets,compress_pixels);
944 (void) CopyMagickMemory(previous_pixels,pixels,length*
945 sizeof(*pixels));
946 break;
947 }
948 }
949 }
cristy8bc68ee2009-09-24 18:39:29 +0000950 (void) WriteBlobString(image,"\033*rB"); /* end graphics */
cristy3ed852e2009-09-05 21:47:34 +0000951 switch (image->compression)
952 {
953 case NoCompression:
954 break;
955 case RLECompression:
956 {
957 compress_pixels=(unsigned char *) RelinquishMagickMemory(
958 compress_pixels);
959 break;
960 }
961 default:
962 {
963 previous_pixels=(unsigned char *) RelinquishMagickMemory(
964 previous_pixels);
965 compress_pixels=(unsigned char *) RelinquishMagickMemory(
966 compress_pixels);
967 break;
968 }
969 }
970 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
971 if (GetNextImageInList(image) == (Image *) NULL)
972 break;
973 image=SyncNextImageInList(image);
974 status=SetImageProgress(image,SaveImagesTag,scene++,
975 GetImageListLength(image));
976 if (status == MagickFalse)
977 break;
978 } while (image_info->adjoin != MagickFalse);
979 (void) WriteBlobString(image,"\033E");
980 (void) CloseBlob(image);
981 return(MagickTrue);
982}