blob: 66a1f561a8114038d0054bbaa5c6a58bef8af906 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% PPPP DDDD FFFFF %
7% P P D D F %
8% PPPP D D FFF %
9% P D D F %
10% P DDDD F %
11% %
12% %
13% Read/Write Portable Document Format %
14% %
15% Software Design %
16% John Cristy %
17% July 1992 %
18% %
19% %
cristy7e41fe82010-12-04 23:12:08 +000020% Copyright 1999-2011 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/colorspace.h"
cristy510d06a2011-07-06 23:43:54 +000050#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000051#include "MagickCore/compress.h"
52#include "MagickCore/constitute.h"
53#include "MagickCore/delegate.h"
54#include "MagickCore/delegate-private.h"
55#include "MagickCore/draw.h"
56#include "MagickCore/exception.h"
57#include "MagickCore/exception-private.h"
58#include "MagickCore/geometry.h"
59#include "MagickCore/image.h"
60#include "MagickCore/image-private.h"
61#include "MagickCore/list.h"
62#include "MagickCore/magick.h"
63#include "MagickCore/memory_.h"
64#include "MagickCore/monitor.h"
65#include "MagickCore/monitor-private.h"
66#include "MagickCore/option.h"
67#include "MagickCore/pixel-accessor.h"
68#include "MagickCore/profile.h"
69#include "MagickCore/property.h"
70#include "MagickCore/quantum-private.h"
71#include "MagickCore/resource_.h"
72#include "MagickCore/resize.h"
73#include "MagickCore/static.h"
74#include "MagickCore/string_.h"
75#include "MagickCore/module.h"
76#include "MagickCore/transform.h"
77#include "MagickCore/utility.h"
78#include "MagickCore/module.h"
cristy80975862009-09-25 14:34:31 +000079
80/*
81 Define declarations.
82*/
cristy3ed852e2009-09-05 21:47:34 +000083#if defined(MAGICKCORE_TIFF_DELEGATE)
84#define CCITTParam "-1"
85#else
86#define CCITTParam "0"
87#endif
88
89/*
90 Forward declarations.
91*/
92static MagickBooleanType
cristy1e178e72011-08-28 19:44:34 +000093 WritePDFImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +000094
95/*
96%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
97% %
98% %
99% %
cristydefb3f02009-09-10 02:18:35 +0000100% I n v o k e P D F D e l e g a t e %
cristy3ed852e2009-09-05 21:47:34 +0000101% %
102% %
103% %
104%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105%
cristydefb3f02009-09-10 02:18:35 +0000106% InvokePDFDelegate() executes the PDF interpreter with the specified command.
cristy3ed852e2009-09-05 21:47:34 +0000107%
cristydefb3f02009-09-10 02:18:35 +0000108% The format of the InvokePDFDelegate method is:
cristy3ed852e2009-09-05 21:47:34 +0000109%
cristydefb3f02009-09-10 02:18:35 +0000110% MagickBooleanType InvokePDFDelegate(const MagickBooleanType verbose,
111% const char *command,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000112%
113% A description of each parameter follows:
114%
115% o verbose: A value other than zero displays the command prior to
116% executing it.
117%
118% o command: the address of a character string containing the command to
119% execute.
120%
cristyb32b90a2009-09-07 21:45:48 +0000121% o exception: return any errors or warnings in this structure.
122%
cristy3ed852e2009-09-05 21:47:34 +0000123*/
cristydefb3f02009-09-10 02:18:35 +0000124static MagickBooleanType InvokePDFDelegate(const MagickBooleanType verbose,
125 const char *command,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000126{
cristyb32b90a2009-09-07 21:45:48 +0000127 int
128 status;
129
cristy0157aea2010-04-24 21:12:18 +0000130#if defined(MAGICKCORE_GS_DELEGATE) || defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000131 char
132 **argv;
133
cristydefb3f02009-09-10 02:18:35 +0000134 const GhostInfo
135 *ghost_info;
cristy3ed852e2009-09-05 21:47:34 +0000136
137 gs_main_instance
138 *interpreter;
139
140 int
141 argc,
cristyb32b90a2009-09-07 21:45:48 +0000142 code;
cristy3ed852e2009-09-05 21:47:34 +0000143
cristybb503372010-05-27 20:51:26 +0000144 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000145 i;
146
cristy0157aea2010-04-24 21:12:18 +0000147#if defined(MAGICKCORE_WINDOWS_SUPPORT)
cristydefb3f02009-09-10 02:18:35 +0000148 ghost_info=NTGhostscriptDLLVectors();
cristy3ed852e2009-09-05 21:47:34 +0000149#else
cristydefb3f02009-09-10 02:18:35 +0000150 GhostInfo
151 ghost_info_struct;
cristy3ed852e2009-09-05 21:47:34 +0000152
cristydefb3f02009-09-10 02:18:35 +0000153 ghost_info=(&ghost_info_struct);
154 (void) ResetMagickMemory(&ghost_info,0,sizeof(ghost_info));
155 ghost_info_struct.new_instance=(int (*)(gs_main_instance **,void *))
cristy3ed852e2009-09-05 21:47:34 +0000156 gsapi_new_instance;
cristydefb3f02009-09-10 02:18:35 +0000157 ghost_info_struct.init_with_args=(int (*)(gs_main_instance *,int,char **))
cristy3ed852e2009-09-05 21:47:34 +0000158 gsapi_init_with_args;
cristydefb3f02009-09-10 02:18:35 +0000159 ghost_info_struct.run_string=(int (*)(gs_main_instance *,const char *,int,
160 int *)) gsapi_run_string;
161 ghost_info_struct.delete_instance=(void (*)(gs_main_instance *))
cristy3ed852e2009-09-05 21:47:34 +0000162 gsapi_delete_instance;
cristydefb3f02009-09-10 02:18:35 +0000163 ghost_info_struct.exit=(int (*)(gs_main_instance *)) gsapi_exit;
cristy3ed852e2009-09-05 21:47:34 +0000164#endif
cristydefb3f02009-09-10 02:18:35 +0000165 if (ghost_info == (GhostInfo *) NULL)
cristyb32b90a2009-09-07 21:45:48 +0000166 {
cristy6de4bc22010-01-12 17:10:35 +0000167 status=SystemCommand(MagickFalse,verbose,command,exception);
cristy41083a42009-09-07 23:47:59 +0000168 return(status == 0 ? MagickTrue : MagickFalse);
cristyb32b90a2009-09-07 21:45:48 +0000169 }
cristy3ed852e2009-09-05 21:47:34 +0000170 if (verbose != MagickFalse)
171 {
172 (void) fputs("[ghostscript library]",stdout);
173 (void) fputs(strchr(command,' '),stdout);
174 }
cristydefb3f02009-09-10 02:18:35 +0000175 status=(ghost_info->new_instance)(&interpreter,(void *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000176 if (status < 0)
cristyb32b90a2009-09-07 21:45:48 +0000177 {
cristy6de4bc22010-01-12 17:10:35 +0000178 status=SystemCommand(MagickFalse,verbose,command,exception);
cristy41083a42009-09-07 23:47:59 +0000179 return(status == 0 ? MagickTrue : MagickFalse);
cristyb32b90a2009-09-07 21:45:48 +0000180 }
cristya73c0f82010-12-20 15:57:05 +0000181 code=0;
cristy3ed852e2009-09-05 21:47:34 +0000182 argv=StringToArgv(command,&argc);
cristydefb3f02009-09-10 02:18:35 +0000183 status=(ghost_info->init_with_args)(interpreter,argc-1,argv+1);
cristy3ed852e2009-09-05 21:47:34 +0000184 if (status == 0)
cristydefb3f02009-09-10 02:18:35 +0000185 status=(ghost_info->run_string)(interpreter,"systemdict /start get exec\n",
186 0,&code);
187 (ghost_info->exit)(interpreter);
188 (ghost_info->delete_instance)(interpreter);
cristy0157aea2010-04-24 21:12:18 +0000189#if defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000190 NTGhostscriptUnLoadDLL();
191#endif
cristybb503372010-05-27 20:51:26 +0000192 for (i=0; i < (ssize_t) argc; i++)
cristy3ed852e2009-09-05 21:47:34 +0000193 argv[i]=DestroyString(argv[i]);
194 argv=(char **) RelinquishMagickMemory(argv);
cristy41083a42009-09-07 23:47:59 +0000195 if ((status != 0) && (status != -101))
196 {
197 char
198 *message;
cristyb32b90a2009-09-07 21:45:48 +0000199
cristy41083a42009-09-07 23:47:59 +0000200 message=GetExceptionMessage(errno);
201 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
202 "`%s': %s",command,message);
203 message=DestroyString(message);
204 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
205 "Ghostscript returns status %d, exit code %d",status,code);
206 return(MagickFalse);
207 }
cristy3ed852e2009-09-05 21:47:34 +0000208 return(MagickTrue);
209#else
cristy6de4bc22010-01-12 17:10:35 +0000210 status=SystemCommand(MagickFalse,verbose,command,exception);
cristy41083a42009-09-07 23:47:59 +0000211 return(status == 0 ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000212#endif
213}
214
215/*
216%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
217% %
218% %
219% %
220% I s P D F %
221% %
222% %
223% %
224%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
225%
226% IsPDF() returns MagickTrue if the image format type, identified by the
227% magick string, is PDF.
228%
229% The format of the IsPDF method is:
230%
231% MagickBooleanType IsPDF(const unsigned char *magick,const size_t offset)
232%
233% A description of each parameter follows:
234%
235% o magick: compare image format pattern against these bytes.
236%
237% o offset: Specifies the offset of the magick string.
238%
239*/
240static MagickBooleanType IsPDF(const unsigned char *magick,const size_t offset)
241{
242 if (offset < 5)
243 return(MagickFalse);
cristy41083a42009-09-07 23:47:59 +0000244 if (LocaleNCompare((const char *) magick,"%PDF-",5) == 0)
cristy3ed852e2009-09-05 21:47:34 +0000245 return(MagickTrue);
246 return(MagickFalse);
247}
248
249/*
250%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251% %
252% %
253% %
254% R e a d P D F I m a g e %
255% %
256% %
257% %
258%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
259%
260% ReadPDFImage() reads a Portable Document Format image file and
261% returns it. It allocates the memory necessary for the new Image structure
262% and returns a pointer to the new image.
263%
264% The format of the ReadPDFImage method is:
265%
266% Image *ReadPDFImage(const ImageInfo *image_info,ExceptionInfo *exception)
267%
268% A description of each parameter follows:
269%
270% o image_info: the image info.
271%
272% o exception: return any errors or warnings in this structure.
273%
274*/
275
276static MagickBooleanType IsPDFRendered(const char *path)
277{
278 MagickBooleanType
279 status;
280
281 struct stat
282 attributes;
283
284 if ((path == (const char *) NULL) || (*path == '\0'))
285 return(MagickFalse);
286 status=GetPathAttributes(path,&attributes);
287 if ((status != MagickFalse) && S_ISREG(attributes.st_mode) &&
288 (attributes.st_size > 0))
289 return(MagickTrue);
290 return(MagickFalse);
291}
292
293static Image *ReadPDFImage(const ImageInfo *image_info,ExceptionInfo *exception)
294{
295#define CropBox "CropBox"
296#define DeviceCMYK "DeviceCMYK"
297#define MediaBox "MediaBox"
298#define RenderPostscriptText "Rendering Postscript... "
299#define PDFRotate "Rotate"
300#define SpotColor "Separation"
301#define TrimBox "TrimBox"
302#define PDFVersion "PDF-"
303
304 char
305 command[MaxTextExtent],
306 density[MaxTextExtent],
307 filename[MaxTextExtent],
308 geometry[MaxTextExtent],
309 options[MaxTextExtent],
310 input_filename[MaxTextExtent],
311 postscript_filename[MaxTextExtent];
312
313 const char
314 *option;
315
316 const DelegateInfo
317 *delegate_info;
318
319 double
320 angle;
321
cristydba40d42010-03-25 18:31:50 +0000322 GeometryInfo
323 geometry_info;
324
cristy3ed852e2009-09-05 21:47:34 +0000325 Image
326 *image,
327 *next,
328 *pdf_image;
329
330 ImageInfo
331 *read_info;
332
333 int
cristya97426c2011-02-04 01:41:27 +0000334 c,
cristy3ed852e2009-09-05 21:47:34 +0000335 file;
336
337 MagickBooleanType
338 cmyk,
339 cropbox,
340 trimbox,
341 status;
342
cristydba40d42010-03-25 18:31:50 +0000343 MagickStatusType
344 flags;
345
cristy3ed852e2009-09-05 21:47:34 +0000346 PointInfo
347 delta;
348
349 RectangleInfo
350 bounding_box,
351 page;
352
353 register char
354 *p;
355
cristya97426c2011-02-04 01:41:27 +0000356 register ssize_t
357 i;
cristy3ed852e2009-09-05 21:47:34 +0000358
359 SegmentInfo
360 bounds,
361 hires_bounds;
362
cristybb503372010-05-27 20:51:26 +0000363 size_t
cristy3ed852e2009-09-05 21:47:34 +0000364 scene,
365 spotcolor;
366
cristyaff6d802011-04-26 01:46:31 +0000367 ssize_t
368 count;
369
cristy3ed852e2009-09-05 21:47:34 +0000370 assert(image_info != (const ImageInfo *) NULL);
371 assert(image_info->signature == MagickSignature);
372 if (image_info->debug != MagickFalse)
373 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
374 image_info->filename);
375 assert(exception != (ExceptionInfo *) NULL);
376 assert(exception->signature == MagickSignature);
377 /*
378 Open image file.
379 */
380 image=AcquireImage(image_info);
381 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
382 if (status == MagickFalse)
383 {
384 image=DestroyImageList(image);
385 return((Image *) NULL);
386 }
387 status=AcquireUniqueSymbolicLink(image_info->filename,input_filename);
388 if (status == MagickFalse)
389 {
390 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
391 image_info->filename);
392 image=DestroyImageList(image);
393 return((Image *) NULL);
394 }
395 /*
396 Set the page density.
397 */
398 delta.x=DefaultResolution;
399 delta.y=DefaultResolution;
400 if ((image->x_resolution == 0.0) || (image->y_resolution == 0.0))
401 {
cristy3ed852e2009-09-05 21:47:34 +0000402 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
403 image->x_resolution=geometry_info.rho;
404 image->y_resolution=geometry_info.sigma;
405 if ((flags & SigmaValue) == 0)
406 image->y_resolution=image->x_resolution;
407 }
408 /*
409 Determine page geometry from the PDF media box.
410 */
411 cmyk=image_info->colorspace == CMYKColorspace ? MagickTrue : MagickFalse;
412 cropbox=MagickFalse;
413 option=GetImageOption(image_info,"pdf:use-cropbox");
414 if (option != (const char *) NULL)
415 cropbox=IsMagickTrue(option);
416 trimbox=MagickFalse;
417 option=GetImageOption(image_info,"pdf:use-trimbox");
418 if (option != (const char *) NULL)
419 trimbox=IsMagickTrue(option);
420 count=0;
421 spotcolor=0;
422 (void) ResetMagickMemory(&bounding_box,0,sizeof(bounding_box));
423 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
424 (void) ResetMagickMemory(&hires_bounds,0,sizeof(hires_bounds));
425 (void) ResetMagickMemory(&page,0,sizeof(page));
426 (void) ResetMagickMemory(command,0,sizeof(command));
427 hires_bounds.x2=0.0;
428 hires_bounds.y2=0.0;
429 angle=0.0;
430 p=command;
431 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
432 {
433 /*
434 Note PDF elements.
435 */
436 if (c == '\n')
437 c=' ';
438 *p++=(char) c;
cristyfd6e87b2011-02-03 18:48:17 +0000439 if ((c != (int) '/') && (c != (int) '%') &&
cristy3ed852e2009-09-05 21:47:34 +0000440 ((size_t) (p-command) < (MaxTextExtent-1)))
441 continue;
442 *(--p)='\0';
443 p=command;
444 if (LocaleNCompare(PDFRotate,command,strlen(PDFRotate)) == 0)
445 count=(ssize_t) sscanf(command,"Rotate %lf",&angle);
446 /*
447 Is this a CMYK document?
448 */
449 if (LocaleNCompare(DeviceCMYK,command,strlen(DeviceCMYK)) == 0)
450 cmyk=MagickTrue;
451 if (LocaleNCompare(SpotColor,command,strlen(SpotColor)) == 0)
452 {
453 char
454 name[MaxTextExtent],
455 property[MaxTextExtent],
456 *value;
457
cristybb503372010-05-27 20:51:26 +0000458 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000459 i;
460
461 /*
462 Note spot names.
463 */
cristyb51dff52011-05-19 16:55:47 +0000464 (void) FormatLocaleString(property,MaxTextExtent,"pdf:SpotColor-%.20g",
cristye8c25f92010-06-03 00:53:06 +0000465 (double) spotcolor++);
cristy3ed852e2009-09-05 21:47:34 +0000466 i=0;
467 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
468 {
469 if ((isspace(c) != 0) || (c == '/') || ((i+1) == MaxTextExtent))
470 break;
471 name[i++]=(char) c;
472 }
473 name[i]='\0';
474 value=AcquireString(name);
475 (void) SubstituteString(&value,"#20"," ");
476 (void) SetImageProperty(image,property,value);
477 value=DestroyString(value);
478 continue;
479 }
480 if (LocaleNCompare(PDFVersion,command,strlen(PDFVersion)) == 0)
481 (void) SetImageProperty(image,"pdf:Version",command);
482 count=0;
483 if (cropbox != MagickFalse)
484 {
485 if (LocaleNCompare(CropBox,command,strlen(CropBox)) == 0)
486 {
487 /*
488 Note region defined by crop box.
489 */
490 count=(ssize_t) sscanf(command,"CropBox [%lf %lf %lf %lf",
491 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
492 if (count != 4)
493 count=(ssize_t) sscanf(command,"CropBox[%lf %lf %lf %lf",
494 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
495 }
496 }
497 else
498 if (trimbox != MagickFalse)
499 {
500 if (LocaleNCompare(TrimBox,command,strlen(TrimBox)) == 0)
501 {
502 /*
503 Note region defined by trim box.
504 */
505 count=(ssize_t) sscanf(command,"TrimBox [%lf %lf %lf %lf",
506 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
507 if (count != 4)
508 count=(ssize_t) sscanf(command,"TrimBox[%lf %lf %lf %lf",
509 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
510 }
511 }
512 else
513 if (LocaleNCompare(MediaBox,command,strlen(MediaBox)) == 0)
514 {
515 /*
516 Note region defined by media box.
517 */
518 count=(ssize_t) sscanf(command,"MediaBox [%lf %lf %lf %lf",
519 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
520 if (count != 4)
521 count=(ssize_t) sscanf(command,"MediaBox[%lf %lf %lf %lf",
522 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
523 }
524 if (count != 4)
525 continue;
526 if (((bounds.x2 > hires_bounds.x2) && (bounds.y2 > hires_bounds.y2)) ||
527 ((hires_bounds.x2 == 0.0) && (hires_bounds.y2 == 0.0)))
528 {
529 /*
530 Set PDF render geometry.
531 */
cristyb51dff52011-05-19 16:55:47 +0000532 (void) FormatLocaleString(geometry,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +0000533 "%gx%g%+.15g%+.15g",bounds.x2-bounds.x1,bounds.y2-bounds.y1,
cristy8cd5b312010-01-07 01:10:24 +0000534 bounds.x1,bounds.y1);
cristy3ed852e2009-09-05 21:47:34 +0000535 (void) SetImageProperty(image,"pdf:HiResBoundingBox",geometry);
cristybb503372010-05-27 20:51:26 +0000536 page.width=(size_t) floor(bounds.x2-bounds.x1+0.5);
537 page.height=(size_t) floor(bounds.y2-bounds.y1+0.5);
cristy3ed852e2009-09-05 21:47:34 +0000538 hires_bounds=bounds;
539 }
540 }
541 (void) CloseBlob(image);
542 if ((fabs(angle) == 90.0) || (fabs(angle) == 270.0))
543 {
cristybb503372010-05-27 20:51:26 +0000544 size_t
cristy3ed852e2009-09-05 21:47:34 +0000545 swap;
546
547 swap=page.width;
548 page.width=page.height;
549 page.height=swap;
550 }
cristy510d06a2011-07-06 23:43:54 +0000551 if (IsRGBColorspace(image_info->colorspace) != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000552 cmyk=MagickFalse;
553 /*
554 Create Ghostscript control file.
555 */
556 file=AcquireUniqueFileResource(postscript_filename);
557 if (file == -1)
558 {
559 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
560 image_info->filename);
561 image=DestroyImage(image);
562 return((Image *) NULL);
563 }
564 count=write(file," ",1);
565 file=close(file)-1;
566 /*
567 Render Postscript with the Ghostscript delegate.
568 */
569 if ((image_info->ping != MagickFalse) ||
570 (image_info->monochrome != MagickFalse))
571 delegate_info=GetDelegateInfo("ps:mono",(char *) NULL,exception);
572 else
573 if (cmyk != MagickFalse)
574 delegate_info=GetDelegateInfo("ps:cmyk",(char *) NULL,exception);
575 else
cristya97426c2011-02-04 01:41:27 +0000576 delegate_info=GetDelegateInfo("ps:alpha",(char *) NULL,exception);
cristy3ed852e2009-09-05 21:47:34 +0000577 if (delegate_info == (const DelegateInfo *) NULL)
578 {
579 (void) RelinquishUniqueFileResource(postscript_filename);
580 image=DestroyImage(image);
581 return((Image *) NULL);
582 }
583 *options='\0';
cristydba40d42010-03-25 18:31:50 +0000584 if (image_info->density != (char *) NULL)
585 {
586 flags=ParseGeometry(image_info->density,&geometry_info);
587 image->x_resolution=geometry_info.rho;
588 image->y_resolution=geometry_info.sigma;
589 if ((flags & SigmaValue) == 0)
590 image->y_resolution=image->x_resolution;
591 }
cristyb51dff52011-05-19 16:55:47 +0000592 (void) FormatLocaleString(density,MaxTextExtent,"%gx%g",image->x_resolution,
cristyd511bb52011-02-03 18:50:56 +0000593 image->y_resolution);
cristy3ed852e2009-09-05 21:47:34 +0000594 if (image_info->page != (char *) NULL)
595 {
596 (void) ParseAbsoluteGeometry(image_info->page,&page);
cristya97426c2011-02-04 01:41:27 +0000597 page.width=(size_t) floor((double) (page.width*image->x_resolution/
598 delta.x)+0.5);
599 page.height=(size_t) floor((double) (page.height*image->y_resolution/
600 delta.y)+0.5);
cristyb51dff52011-05-19 16:55:47 +0000601 (void) FormatLocaleString(options,MaxTextExtent,"-g%.20gx%.20g ",(double)
cristyd511bb52011-02-03 18:50:56 +0000602 page.width,(double) page.height);
cristy3ed852e2009-09-05 21:47:34 +0000603 }
604 if (cmyk != MagickFalse)
605 (void) ConcatenateMagickString(options,"-dUseCIEColor ",MaxTextExtent);
606 if (cropbox != MagickFalse)
607 (void) ConcatenateMagickString(options,"-dUseCropBox ",MaxTextExtent);
608 if (trimbox != MagickFalse)
609 (void) ConcatenateMagickString(options,"-dUseTrimBox ",MaxTextExtent);
610 read_info=CloneImageInfo(image_info);
611 *read_info->magick='\0';
612 if (read_info->number_scenes != 0)
613 {
614 char
615 pages[MaxTextExtent];
616
cristyb51dff52011-05-19 16:55:47 +0000617 (void) FormatLocaleString(pages,MaxTextExtent,"-dFirstPage=%.20g "
cristye8c25f92010-06-03 00:53:06 +0000618 "-dLastPage=%.20g",(double) read_info->scene+1,(double)
cristyf2faecf2010-05-28 19:19:36 +0000619 (read_info->scene+read_info->number_scenes));
cristy3ed852e2009-09-05 21:47:34 +0000620 (void) ConcatenateMagickString(options,pages,MaxTextExtent);
621 read_info->number_scenes=0;
622 if (read_info->scenes != (char *) NULL)
623 *read_info->scenes='\0';
624 }
625 if (read_info->authenticate != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +0000626 (void) FormatLocaleString(options+strlen(options),MaxTextExtent,
cristy3ed852e2009-09-05 21:47:34 +0000627 " -sPDFPassword=%s",read_info->authenticate);
628 (void) CopyMagickString(filename,read_info->filename,MaxTextExtent);
cristya97426c2011-02-04 01:41:27 +0000629 (void) AcquireUniqueFilename(filename);
630 (void) ConcatenateMagickString(filename,"-%08d",MaxTextExtent);
cristyb51dff52011-05-19 16:55:47 +0000631 (void) FormatLocaleString(command,MaxTextExtent,
cristy3ed852e2009-09-05 21:47:34 +0000632 GetDelegateCommands(delegate_info),
633 read_info->antialias != MagickFalse ? 4 : 1,
cristya97426c2011-02-04 01:41:27 +0000634 read_info->antialias != MagickFalse ? 4 : 1,density,options,filename,
635 postscript_filename,input_filename);
cristydefb3f02009-09-10 02:18:35 +0000636 status=InvokePDFDelegate(read_info->verbose,command,exception);
cristy3ed852e2009-09-05 21:47:34 +0000637 (void) RelinquishUniqueFileResource(postscript_filename);
cristy3ed852e2009-09-05 21:47:34 +0000638 (void) RelinquishUniqueFileResource(input_filename);
cristya97426c2011-02-04 01:41:27 +0000639 pdf_image=(Image *) NULL;
640 if (status == MagickFalse)
641 for (i=1; ; i++)
642 {
cristya97426c2011-02-04 01:41:27 +0000643 (void) InterpretImageFilename(image_info,image,filename,(int) i,
644 read_info->filename);
645 if (IsPDFRendered(read_info->filename) == MagickFalse)
646 break;
647 (void) RelinquishUniqueFileResource(read_info->filename);
648 }
649 else
650 for (i=1; ; i++)
651 {
cristya97426c2011-02-04 01:41:27 +0000652 (void) InterpretImageFilename(image_info,image,filename,(int) i,
653 read_info->filename);
654 if (IsPDFRendered(read_info->filename) == MagickFalse)
655 break;
656 next=ReadImage(read_info,exception);
657 (void) RelinquishUniqueFileResource(read_info->filename);
658 if (next == (Image *) NULL)
659 break;
660 AppendImageToList(&pdf_image,next);
661 }
cristy3ed852e2009-09-05 21:47:34 +0000662 read_info=DestroyImageInfo(read_info);
663 if (pdf_image == (Image *) NULL)
664 {
665 ThrowFileException(exception,DelegateError,"PostscriptDelegateFailed",
666 image_info->filename);
cristyb6cabe82011-03-18 13:25:59 +0000667 image=DestroyImage(image);
cristy3ed852e2009-09-05 21:47:34 +0000668 return((Image *) NULL);
669 }
670 if (LocaleCompare(pdf_image->magick,"BMP") == 0)
671 {
672 Image
673 *cmyk_image;
674
675 cmyk_image=ConsolidateCMYKImages(pdf_image,exception);
676 if (cmyk_image != (Image *) NULL)
677 {
678 pdf_image=DestroyImageList(pdf_image);
679 pdf_image=cmyk_image;
680 }
681 }
682 if (image_info->number_scenes != 0)
683 {
684 Image
685 *clone_image;
686
cristybb503372010-05-27 20:51:26 +0000687 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000688 i;
689
690 /*
691 Add place holder images to meet the subimage specification requirement.
692 */
cristybb503372010-05-27 20:51:26 +0000693 for (i=0; i < (ssize_t) image_info->scene; i++)
cristy3ed852e2009-09-05 21:47:34 +0000694 {
695 clone_image=CloneImage(pdf_image,1,1,MagickTrue,exception);
696 if (clone_image != (Image *) NULL)
697 PrependImageToList(&pdf_image,clone_image);
698 }
699 }
700 do
701 {
702 (void) CopyMagickString(pdf_image->filename,filename,MaxTextExtent);
703 pdf_image->page=page;
704 (void) CloneImageProfiles(pdf_image,image);
705 (void) CloneImageProperties(pdf_image,image);
706 next=SyncNextImageInList(pdf_image);
707 if (next != (Image *) NULL)
708 pdf_image=next;
709 } while (next != (Image *) NULL);
710 image=DestroyImage(image);
711 scene=0;
712 for (next=GetFirstImageInList(pdf_image); next != (Image *) NULL; )
713 {
714 next->scene=scene++;
715 next=GetNextImageInList(next);
716 }
717 return(GetFirstImageInList(pdf_image));
718}
719
720/*
721%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
722% %
723% %
724% %
725% R e g i s t e r P D F I m a g e %
726% %
727% %
728% %
729%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
730%
731% RegisterPDFImage() adds properties for the PDF image format to
732% the list of supported formats. The properties include the image format
733% tag, a method to read and/or write the format, whether the format
734% supports the saving of more than one frame to the same file or blob,
735% whether the format supports native in-memory I/O, and a brief
736% description of the format.
737%
738% The format of the RegisterPDFImage method is:
739%
cristybb503372010-05-27 20:51:26 +0000740% size_t RegisterPDFImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000741%
742*/
cristybb503372010-05-27 20:51:26 +0000743ModuleExport size_t RegisterPDFImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000744{
745 MagickInfo
746 *entry;
747
748 entry=SetMagickInfo("AI");
749 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
750 entry->encoder=(EncodeImageHandler *) WritePDFImage;
751 entry->adjoin=MagickFalse;
752 entry->blob_support=MagickFalse;
753 entry->seekable_stream=MagickTrue;
754 entry->thread_support=EncoderThreadSupport;
755 entry->description=ConstantString("Adobe Illustrator CS2");
756 entry->module=ConstantString("PDF");
757 (void) RegisterMagickInfo(entry);
758 entry=SetMagickInfo("EPDF");
759 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
760 entry->encoder=(EncodeImageHandler *) WritePDFImage;
761 entry->adjoin=MagickFalse;
762 entry->blob_support=MagickFalse;
763 entry->seekable_stream=MagickTrue;
764 entry->thread_support=EncoderThreadSupport;
765 entry->description=ConstantString("Encapsulated Portable Document Format");
766 entry->module=ConstantString("PDF");
767 (void) RegisterMagickInfo(entry);
768 entry=SetMagickInfo("PDF");
769 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
770 entry->encoder=(EncodeImageHandler *) WritePDFImage;
771 entry->magick=(IsImageFormatHandler *) IsPDF;
772 entry->blob_support=MagickFalse;
773 entry->seekable_stream=MagickTrue;
774 entry->thread_support=EncoderThreadSupport;
775 entry->description=ConstantString("Portable Document Format");
776 entry->module=ConstantString("PDF");
777 (void) RegisterMagickInfo(entry);
778 entry=SetMagickInfo("PDFA");
779 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
780 entry->encoder=(EncodeImageHandler *) WritePDFImage;
781 entry->magick=(IsImageFormatHandler *) IsPDF;
782 entry->blob_support=MagickFalse;
783 entry->seekable_stream=MagickTrue;
784 entry->thread_support=EncoderThreadSupport;
785 entry->description=ConstantString("Portable Document Archive Format");
786 entry->module=ConstantString("PDF");
787 (void) RegisterMagickInfo(entry);
788 return(MagickImageCoderSignature);
789}
790
791/*
792%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
793% %
794% %
795% %
796% U n r e g i s t e r P D F I m a g e %
797% %
798% %
799% %
800%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
801%
802% UnregisterPDFImage() removes format registrations made by the
803% PDF module from the list of supported formats.
804%
805% The format of the UnregisterPDFImage method is:
806%
807% UnregisterPDFImage(void)
808%
809*/
810ModuleExport void UnregisterPDFImage(void)
811{
812 (void) UnregisterMagickInfo("AI");
813 (void) UnregisterMagickInfo("EPDF");
814 (void) UnregisterMagickInfo("PDF");
815 (void) UnregisterMagickInfo("PDFA");
816}
817
818/*
819%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
820% %
821% %
822% %
823% W r i t e P D F I m a g e %
824% %
825% %
826% %
827%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
828%
829% WritePDFImage() writes an image in the Portable Document image
830% format.
831%
832% The format of the WritePDFImage method is:
833%
cristy1e178e72011-08-28 19:44:34 +0000834% MagickBooleanType WritePDFImage(const ImageInfo *image_info,
835% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000836%
837% A description of each parameter follows.
838%
839% o image_info: the image info.
840%
841% o image: The image.
842%
cristy1e178e72011-08-28 19:44:34 +0000843% o exception: return any errors or warnings in this structure.
844%
cristy3ed852e2009-09-05 21:47:34 +0000845*/
846
847static inline size_t MagickMax(const size_t x,const size_t y)
848{
849 if (x > y)
850 return(x);
851 return(y);
852}
853
854static inline size_t MagickMin(const size_t x,const size_t y)
855{
856 if (x < y)
857 return(x);
858 return(y);
859}
860
861static char *EscapeParenthesis(const char *text)
862{
863 register char
864 *p;
865
cristybb503372010-05-27 20:51:26 +0000866 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000867 i;
868
cristybb503372010-05-27 20:51:26 +0000869 size_t
cristy3ed852e2009-09-05 21:47:34 +0000870 escapes;
871
cristyaff6d802011-04-26 01:46:31 +0000872 static char
873 buffer[MaxTextExtent];
874
cristy3ed852e2009-09-05 21:47:34 +0000875 escapes=0;
876 p=buffer;
cristybb503372010-05-27 20:51:26 +0000877 for (i=0; i < (ssize_t) MagickMin(strlen(text),(MaxTextExtent-escapes-1)); i++)
cristy3ed852e2009-09-05 21:47:34 +0000878 {
879 if ((text[i] == '(') || (text[i] == ')'))
880 {
881 *p++='\\';
882 escapes++;
883 }
884 *p++=text[i];
885 }
886 *p='\0';
887 return(buffer);
888}
889
cristy47b838c2009-09-19 16:09:30 +0000890static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
891 Image *image,Image *inject_image)
892{
cristy47b838c2009-09-19 16:09:30 +0000893 Image
cristy80975862009-09-25 14:34:31 +0000894 *group4_image;
cristy47b838c2009-09-19 16:09:30 +0000895
896 ImageInfo
897 *write_info;
898
cristy47b838c2009-09-19 16:09:30 +0000899 MagickBooleanType
900 status;
901
cristy80975862009-09-25 14:34:31 +0000902 size_t
903 length;
cristy47b838c2009-09-19 16:09:30 +0000904
905 unsigned char
cristy80975862009-09-25 14:34:31 +0000906 *group4;
cristy47b838c2009-09-19 16:09:30 +0000907
cristy42751fe2009-10-05 00:15:50 +0000908 status=MagickTrue;
cristy47b838c2009-09-19 16:09:30 +0000909 write_info=CloneImageInfo(image_info);
cristy80975862009-09-25 14:34:31 +0000910 (void) CopyMagickString(write_info->filename,"GROUP4:",MaxTextExtent);
911 (void) CopyMagickString(write_info->magick,"GROUP4",MaxTextExtent);
912 group4_image=CloneImage(inject_image,0,0,MagickTrue,&image->exception);
913 if (group4_image == (Image *) NULL)
914 return(MagickFalse);
915 group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
916 &image->exception);
917 group4_image=DestroyImage(group4_image);
918 if (group4 == (unsigned char *) NULL)
919 return(MagickFalse);
cristy47b838c2009-09-19 16:09:30 +0000920 write_info=DestroyImageInfo(write_info);
cristy80975862009-09-25 14:34:31 +0000921 if (WriteBlob(image,length,group4) != (ssize_t) length)
922 status=MagickFalse;
923 group4=(unsigned char *) RelinquishMagickMemory(group4);
924 return(status);
cristy47b838c2009-09-19 16:09:30 +0000925}
926
cristy1e178e72011-08-28 19:44:34 +0000927static MagickBooleanType WritePDFImage(const ImageInfo *image_info,Image *image,
928 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000929{
930#define CFormat "/Filter [ /%s ]\n"
931#define ObjectsPerImage 14
932
933 static const char
934 XMPProfile[]=
935 {
936 "<?xpacket begin=\"%s\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n"
937 "<x:xmpmeta xmlns:x=\"adobe:ns:meta/\" x:xmptk=\"Adobe XMP Core 4.0-c316 44.253921, Sun Oct 01 2006 17:08:23\">\n"
938 " <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n"
939 " <rdf:Description rdf:about=\"\"\n"
940 " xmlns:xap=\"http://ns.adobe.com/xap/1.0/\">\n"
941 " <xap:ModifyDate>%s</xap:ModifyDate>\n"
942 " <xap:CreateDate>%s</xap:CreateDate>\n"
943 " <xap:MetadataDate>%s</xap:MetadataDate>\n"
944 " <xap:CreatorTool>%s</xap:CreatorTool>\n"
945 " </rdf:Description>\n"
946 " <rdf:Description rdf:about=\"\"\n"
947 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"
948 " <dc:format>application/pdf</dc:format>\n"
949 " </rdf:Description>\n"
950 " <rdf:Description rdf:about=\"\"\n"
951 " xmlns:xapMM=\"http://ns.adobe.com/xap/1.0/mm/\">\n"
952 " <xapMM:DocumentID>uuid:6ec119d7-7982-4f56-808d-dfe64f5b35cf</xapMM:DocumentID>\n"
953 " <xapMM:InstanceID>uuid:a79b99b4-6235-447f-9f6c-ec18ef7555cb</xapMM:InstanceID>\n"
954 " </rdf:Description>\n"
955 " <rdf:Description rdf:about=\"\"\n"
956 " xmlns:pdf=\"http://ns.adobe.com/pdf/1.3/\">\n"
957 " <pdf:Producer>%s</pdf:Producer>\n"
958 " </rdf:Description>\n"
959 " <rdf:Description rdf:about=\"\"\n"
960 " xmlns:pdfaid=\"http://www.aiim.org/pdfa/ns/id/\">\n"
961 " <pdfaid:part>1</pdfaid:part>\n"
962 " <pdfaid:conformance>B</pdfaid:conformance>\n"
963 " </rdf:Description>\n"
964 " </rdf:RDF>\n"
965 "</x:xmpmeta>\n"
966 "<?xpacket end=\"w\"?>\n"
967 },
968 XMPProfileMagick[4]= { (char) 0xef, (char) 0xbb, (char) 0xbf, (char) 0x00 };
969
970 char
cristy57015272010-12-30 01:16:38 +0000971 basename[MaxTextExtent],
cristy3ed852e2009-09-05 21:47:34 +0000972 buffer[MaxTextExtent],
973 date[MaxTextExtent],
974 **labels,
975 page_geometry[MaxTextExtent];
976
977 CompressionType
978 compression;
979
980 const char
981 *value;
982
983 double
984 pointsize;
985
986 GeometryInfo
987 geometry_info;
988
cristy3ed852e2009-09-05 21:47:34 +0000989 Image
990 *next,
991 *tile_image;
992
993 MagickBooleanType
994 status;
995
996 MagickOffsetType
997 offset,
998 scene,
999 *xref;
1000
1001 MagickSizeType
1002 number_pixels;
1003
1004 MagickStatusType
1005 flags;
1006
1007 PointInfo
1008 delta,
1009 resolution,
1010 scale;
1011
1012 RectangleInfo
1013 geometry,
1014 media_info,
1015 page_info;
1016
cristy4c08aed2011-07-01 19:47:50 +00001017 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +00001018 *p;
1019
1020 register unsigned char
1021 *q;
1022
cristybb503372010-05-27 20:51:26 +00001023 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001024 i,
1025 x;
1026
1027 size_t
cristyaff6d802011-04-26 01:46:31 +00001028 info_id,
1029 length,
1030 object,
1031 pages_id,
1032 root_id,
1033 text_size,
1034 version;
1035
1036 ssize_t
1037 count,
1038 y;
cristy3ed852e2009-09-05 21:47:34 +00001039
1040 struct tm
1041 local_time;
1042
1043 time_t
1044 seconds;
1045
1046 unsigned char
1047 *pixels;
1048
cristy3ed852e2009-09-05 21:47:34 +00001049 /*
1050 Open output image file.
1051 */
1052 assert(image_info != (const ImageInfo *) NULL);
1053 assert(image_info->signature == MagickSignature);
1054 assert(image != (Image *) NULL);
1055 assert(image->signature == MagickSignature);
1056 if (image->debug != MagickFalse)
1057 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy1e178e72011-08-28 19:44:34 +00001058 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +00001059 if (status == MagickFalse)
1060 return(status);
1061 /*
1062 Allocate X ref memory.
1063 */
1064 xref=(MagickOffsetType *) AcquireQuantumMemory(2048UL,sizeof(*xref));
1065 if (xref == (MagickOffsetType *) NULL)
1066 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1067 (void) ResetMagickMemory(xref,0,2048UL*sizeof(*xref));
1068 /*
1069 Write Info object.
1070 */
1071 object=0;
1072 version=3;
1073 if (image_info->compression == JPEG2000Compression)
cristybb503372010-05-27 20:51:26 +00001074 version=(size_t) MagickMax(version,5);
cristy3ed852e2009-09-05 21:47:34 +00001075 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
1076 if (next->matte != MagickFalse)
cristybb503372010-05-27 20:51:26 +00001077 version=(size_t) MagickMax(version,4);
cristy3ed852e2009-09-05 21:47:34 +00001078 if (LocaleCompare(image_info->magick,"PDFA") == 0)
cristyc04c60e2011-05-31 23:58:45 +00001079 version=(size_t) MagickMax(version,6);
cristyb51dff52011-05-19 16:55:47 +00001080 (void) FormatLocaleString(buffer,MaxTextExtent,"%%PDF-1.%.20g \n",
cristye8c25f92010-06-03 00:53:06 +00001081 (double) version);
cristy3ed852e2009-09-05 21:47:34 +00001082 (void) WriteBlobString(image,buffer);
1083 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1084 (void) WriteBlobString(image,"%âãÏÓ\n");
1085 /*
1086 Write Catalog object.
1087 */
1088 xref[object++]=TellBlob(image);
1089 root_id=object;
cristyb51dff52011-05-19 16:55:47 +00001090 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristye8c25f92010-06-03 00:53:06 +00001091 object);
cristy3ed852e2009-09-05 21:47:34 +00001092 (void) WriteBlobString(image,buffer);
1093 (void) WriteBlobString(image,"<<\n");
1094 if (LocaleCompare(image_info->magick,"PDFA") != 0)
cristyb51dff52011-05-19 16:55:47 +00001095 (void) FormatLocaleString(buffer,MaxTextExtent,"/Pages %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001096 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001097 else
1098 {
cristyb51dff52011-05-19 16:55:47 +00001099 (void) FormatLocaleString(buffer,MaxTextExtent,"/Metadata %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001100 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001101 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001102 (void) FormatLocaleString(buffer,MaxTextExtent,"/Pages %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001103 (double) object+2);
cristy3ed852e2009-09-05 21:47:34 +00001104 }
1105 (void) WriteBlobString(image,buffer);
1106 (void) WriteBlobString(image,"/Type /Catalog\n");
1107 (void) WriteBlobString(image,">>\n");
1108 (void) WriteBlobString(image,"endobj\n");
1109 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1110 {
1111 char
1112 create_date[MaxTextExtent],
1113 modify_date[MaxTextExtent],
1114 timestamp[MaxTextExtent],
1115 xmp_profile[MaxTextExtent];
1116
cristybb503372010-05-27 20:51:26 +00001117 size_t
cristy3ed852e2009-09-05 21:47:34 +00001118 version;
1119
1120 /*
1121 Write XMP object.
1122 */
1123 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001124 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",
cristye8c25f92010-06-03 00:53:06 +00001125 (double) object);
cristy3ed852e2009-09-05 21:47:34 +00001126 (void) WriteBlobString(image,buffer);
1127 (void) WriteBlobString(image,"<<\n");
1128 (void) WriteBlobString(image,"/Subtype /XML\n");
1129 *modify_date='\0';
1130 value=GetImageProperty(image,"date:modify");
1131 if (value != (const char *) NULL)
1132 (void) CopyMagickString(modify_date,value,MaxTextExtent);
1133 *create_date='\0';
1134 value=GetImageProperty(image,"date:create");
1135 if (value != (const char *) NULL)
1136 (void) CopyMagickString(create_date,value,MaxTextExtent);
1137 (void) FormatMagickTime(time((time_t *) NULL),MaxTextExtent,timestamp);
cristyb51dff52011-05-19 16:55:47 +00001138 i=FormatLocaleString(xmp_profile,MaxTextExtent,XMPProfile,
cristy3ed852e2009-09-05 21:47:34 +00001139 XMPProfileMagick,modify_date,create_date,timestamp,
1140 GetMagickVersion(&version),GetMagickVersion(&version));
cristyb51dff52011-05-19 16:55:47 +00001141 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g\n",
cristye8c25f92010-06-03 00:53:06 +00001142 (double) i);
cristy3ed852e2009-09-05 21:47:34 +00001143 (void) WriteBlobString(image,buffer);
1144 (void) WriteBlobString(image,"/Type /Metadata\n");
1145 (void) WriteBlobString(image,">>\nstream\n");
1146 (void) WriteBlobString(image,xmp_profile);
1147 (void) WriteBlobString(image,"endstream\n");
1148 (void) WriteBlobString(image,"endobj\n");
1149 }
1150 /*
1151 Write Pages object.
1152 */
1153 xref[object++]=TellBlob(image);
1154 pages_id=object;
cristyb51dff52011-05-19 16:55:47 +00001155 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001156 object);
cristy3ed852e2009-09-05 21:47:34 +00001157 (void) WriteBlobString(image,buffer);
1158 (void) WriteBlobString(image,"<<\n");
1159 (void) WriteBlobString(image,"/Type /Pages\n");
cristyb51dff52011-05-19 16:55:47 +00001160 (void) FormatLocaleString(buffer,MaxTextExtent,"/Kids [ %.20g 0 R ",
cristye8c25f92010-06-03 00:53:06 +00001161 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001162 (void) WriteBlobString(image,buffer);
cristybb503372010-05-27 20:51:26 +00001163 count=(ssize_t) (pages_id+ObjectsPerImage+1);
cristy3ed852e2009-09-05 21:47:34 +00001164 if (image_info->adjoin != MagickFalse)
1165 {
1166 Image
1167 *kid_image;
1168
1169 /*
1170 Predict page object id's.
1171 */
1172 kid_image=image;
1173 for ( ; GetNextImageInList(kid_image) != (Image *) NULL; count+=ObjectsPerImage)
1174 {
cristyb51dff52011-05-19 16:55:47 +00001175 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 R ",(double)
cristye8c25f92010-06-03 00:53:06 +00001176 count);
cristy3ed852e2009-09-05 21:47:34 +00001177 (void) WriteBlobString(image,buffer);
1178 kid_image=GetNextImageInList(kid_image);
1179 }
1180 xref=(MagickOffsetType *) ResizeQuantumMemory(xref,(size_t) count+2048UL,
1181 sizeof(*xref));
1182 if (xref == (MagickOffsetType *) NULL)
1183 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1184 }
1185 (void) WriteBlobString(image,"]\n");
cristyb51dff52011-05-19 16:55:47 +00001186 (void) FormatLocaleString(buffer,MaxTextExtent,"/Count %.20g\n",
cristye8c25f92010-06-03 00:53:06 +00001187 (double) ((count-pages_id)/ObjectsPerImage));
cristy3ed852e2009-09-05 21:47:34 +00001188 (void) WriteBlobString(image,buffer);
1189 (void) WriteBlobString(image,">>\n");
1190 (void) WriteBlobString(image,"endobj\n");
1191 scene=0;
1192 do
1193 {
1194 compression=image->compression;
1195 if (image_info->compression != UndefinedCompression)
1196 compression=image_info->compression;
1197 switch (compression)
1198 {
1199 case FaxCompression:
1200 case Group4Compression:
1201 {
cristy1e178e72011-08-28 19:44:34 +00001202 if ((IsImageMonochrome(image,exception) == MagickFalse) ||
cristy3ed852e2009-09-05 21:47:34 +00001203 (image->matte != MagickFalse))
1204 compression=RLECompression;
1205 break;
1206 }
1207#if !defined(MAGICKCORE_JPEG_DELEGATE)
1208 case JPEGCompression:
1209 {
1210 compression=RLECompression;
cristy1e178e72011-08-28 19:44:34 +00001211 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00001212 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
1213 image->filename);
1214 break;
1215 }
1216#endif
1217#if !defined(MAGICKCORE_JP2_DELEGATE)
1218 case JPEG2000Compression:
1219 {
1220 compression=RLECompression;
cristy1e178e72011-08-28 19:44:34 +00001221 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00001222 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JP2)",
1223 image->filename);
1224 break;
1225 }
1226#endif
1227#if !defined(MAGICKCORE_ZLIB_DELEGATE)
1228 case ZipCompression:
1229 {
1230 compression=RLECompression;
cristy1e178e72011-08-28 19:44:34 +00001231 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00001232 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZLIB)",
1233 image->filename);
1234 break;
1235 }
1236#endif
1237 case LZWCompression:
1238 {
1239 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1240 compression=RLECompression; /* LZW compression is forbidden */
1241 break;
1242 }
1243 case NoCompression:
1244 {
1245 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1246 compression=RLECompression; /* ASCII 85 compression is forbidden */
1247 break;
1248 }
1249 default:
1250 break;
1251 }
1252 if (compression == JPEG2000Compression)
1253 {
cristy510d06a2011-07-06 23:43:54 +00001254 if (IsRGBColorspace(image->colorspace) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001255 (void) TransformImageColorspace(image,RGBColorspace);
1256 }
1257 /*
1258 Scale relative to dots-per-inch.
1259 */
1260 delta.x=DefaultResolution;
1261 delta.y=DefaultResolution;
1262 resolution.x=image->x_resolution;
1263 resolution.y=image->y_resolution;
1264 if ((resolution.x == 0.0) || (resolution.y == 0.0))
1265 {
1266 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
1267 resolution.x=geometry_info.rho;
1268 resolution.y=geometry_info.sigma;
1269 if ((flags & SigmaValue) == 0)
1270 resolution.y=resolution.x;
1271 }
1272 if (image_info->density != (char *) NULL)
1273 {
1274 flags=ParseGeometry(image_info->density,&geometry_info);
1275 resolution.x=geometry_info.rho;
1276 resolution.y=geometry_info.sigma;
1277 if ((flags & SigmaValue) == 0)
1278 resolution.y=resolution.x;
1279 }
1280 if (image->units == PixelsPerCentimeterResolution)
1281 {
cristya97426c2011-02-04 01:41:27 +00001282 resolution.x=(double) ((size_t) (100.0*2.54*resolution.x+0.5)/100.0);
1283 resolution.y=(double) ((size_t) (100.0*2.54*resolution.y+0.5)/100.0);
cristy3ed852e2009-09-05 21:47:34 +00001284 }
1285 SetGeometry(image,&geometry);
cristyb51dff52011-05-19 16:55:47 +00001286 (void) FormatLocaleString(page_geometry,MaxTextExtent,"%.20gx%.20g",
cristye8c25f92010-06-03 00:53:06 +00001287 (double) image->columns,(double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001288 if (image_info->page != (char *) NULL)
1289 (void) CopyMagickString(page_geometry,image_info->page,MaxTextExtent);
1290 else
1291 if ((image->page.width != 0) && (image->page.height != 0))
cristyb51dff52011-05-19 16:55:47 +00001292 (void) FormatLocaleString(page_geometry,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00001293 "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,
1294 (double) image->page.height,(double) image->page.x,(double)
1295 image->page.y);
cristy3ed852e2009-09-05 21:47:34 +00001296 else
1297 if ((image->gravity != UndefinedGravity) &&
1298 (LocaleCompare(image_info->magick,"PDF") == 0))
1299 (void) CopyMagickString(page_geometry,PSPageGeometry,MaxTextExtent);
1300 (void) ConcatenateMagickString(page_geometry,">",MaxTextExtent);
1301 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
1302 &geometry.width,&geometry.height);
1303 scale.x=(double) (geometry.width*delta.x)/resolution.x;
cristybb503372010-05-27 20:51:26 +00001304 geometry.width=(size_t) floor(scale.x+0.5);
cristy3ed852e2009-09-05 21:47:34 +00001305 scale.y=(double) (geometry.height*delta.y)/resolution.y;
cristybb503372010-05-27 20:51:26 +00001306 geometry.height=(size_t) floor(scale.y+0.5);
cristy3ed852e2009-09-05 21:47:34 +00001307 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
cristy1e178e72011-08-28 19:44:34 +00001308 (void) ParseGravityGeometry(image,page_geometry,&page_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00001309 if (image->gravity != UndefinedGravity)
1310 {
1311 geometry.x=(-page_info.x);
cristybb503372010-05-27 20:51:26 +00001312 geometry.y=(ssize_t) (media_info.height+page_info.y-image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001313 }
1314 pointsize=12.0;
1315 if (image_info->pointsize != 0.0)
1316 pointsize=image_info->pointsize;
1317 text_size=0;
cristybedb2ab2011-02-27 03:15:58 +00001318 value=GetImageProperty(image,"label");
cristy3ed852e2009-09-05 21:47:34 +00001319 if (value != (const char *) NULL)
cristybb503372010-05-27 20:51:26 +00001320 text_size=(size_t) (MultilineCensus(value)*pointsize+12);
cristyda16f162011-02-19 23:52:17 +00001321 (void) text_size;
cristy3ed852e2009-09-05 21:47:34 +00001322 /*
1323 Write Page object.
1324 */
1325 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001326 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001327 object);
cristy3ed852e2009-09-05 21:47:34 +00001328 (void) WriteBlobString(image,buffer);
1329 (void) WriteBlobString(image,"<<\n");
1330 (void) WriteBlobString(image,"/Type /Page\n");
cristyb51dff52011-05-19 16:55:47 +00001331 (void) FormatLocaleString(buffer,MaxTextExtent,"/Parent %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001332 (double) pages_id);
cristy3ed852e2009-09-05 21:47:34 +00001333 (void) WriteBlobString(image,buffer);
1334 (void) WriteBlobString(image,"/Resources <<\n");
1335 labels=(char **) NULL;
cristybedb2ab2011-02-27 03:15:58 +00001336 value=GetImageProperty(image,"label");
cristy3ed852e2009-09-05 21:47:34 +00001337 if (value != (const char *) NULL)
1338 labels=StringToList(value);
1339 if (labels != (char **) NULL)
1340 {
cristyb51dff52011-05-19 16:55:47 +00001341 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00001342 "/Font << /F%.20g %.20g 0 R >>\n",(double) image->scene,(double)
1343 object+4);
cristy3ed852e2009-09-05 21:47:34 +00001344 (void) WriteBlobString(image,buffer);
1345 }
cristyb51dff52011-05-19 16:55:47 +00001346 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00001347 "/XObject << /Im%.20g %.20g 0 R >>\n",(double) image->scene,(double)
1348 object+5);
cristy3ed852e2009-09-05 21:47:34 +00001349 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001350 (void) FormatLocaleString(buffer,MaxTextExtent,"/ProcSet %.20g 0 R >>\n",
cristye8c25f92010-06-03 00:53:06 +00001351 (double) object+3);
cristy3ed852e2009-09-05 21:47:34 +00001352 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001353 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00001354 "/MediaBox [0 0 %g %g]\n",72.0*media_info.width/resolution.x,
cristy8cd5b312010-01-07 01:10:24 +00001355 72.0*media_info.height/resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001356 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001357 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00001358 "/CropBox [0 0 %g %g]\n",72.0*media_info.width/resolution.x,
cristy8cd5b312010-01-07 01:10:24 +00001359 72.0*media_info.height/resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001360 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001361 (void) FormatLocaleString(buffer,MaxTextExtent,"/Contents %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001362 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001363 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001364 (void) FormatLocaleString(buffer,MaxTextExtent,"/Thumb %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001365 (double) object+8);
cristy3ed852e2009-09-05 21:47:34 +00001366 (void) WriteBlobString(image,buffer);
1367 (void) WriteBlobString(image,">>\n");
1368 (void) WriteBlobString(image,"endobj\n");
1369 /*
1370 Write Contents object.
1371 */
1372 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001373 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001374 object);
cristy3ed852e2009-09-05 21:47:34 +00001375 (void) WriteBlobString(image,buffer);
1376 (void) WriteBlobString(image,"<<\n");
cristyb51dff52011-05-19 16:55:47 +00001377 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001378 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001379 (void) WriteBlobString(image,buffer);
1380 (void) WriteBlobString(image,">>\n");
1381 (void) WriteBlobString(image,"stream\n");
1382 offset=TellBlob(image);
1383 (void) WriteBlobString(image,"q\n");
1384 if (labels != (char **) NULL)
1385 for (i=0; labels[i] != (char *) NULL; i++)
1386 {
1387 (void) WriteBlobString(image,"BT\n");
cristyb51dff52011-05-19 16:55:47 +00001388 (void) FormatLocaleString(buffer,MaxTextExtent,"/F%.20g %g Tf\n",
cristye8c25f92010-06-03 00:53:06 +00001389 (double) image->scene,pointsize);
cristy3ed852e2009-09-05 21:47:34 +00001390 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001391 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g Td\n",
cristye8c25f92010-06-03 00:53:06 +00001392 (double) geometry.x,(double) (geometry.y+geometry.height+i*pointsize+
1393 12));
cristy3ed852e2009-09-05 21:47:34 +00001394 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001395 (void) FormatLocaleString(buffer,MaxTextExtent,"(%s) Tj\n",labels[i]);
cristy3ed852e2009-09-05 21:47:34 +00001396 (void) WriteBlobString(image,buffer);
1397 (void) WriteBlobString(image,"ET\n");
1398 labels[i]=DestroyString(labels[i]);
1399 }
cristyb51dff52011-05-19 16:55:47 +00001400 (void) FormatLocaleString(buffer,MaxTextExtent,"%g 0 0 %g %.20g %.20g cm\n",
cristye8c25f92010-06-03 00:53:06 +00001401 scale.x,scale.y,(double) geometry.x,(double) geometry.y);
cristy3ed852e2009-09-05 21:47:34 +00001402 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001403 (void) FormatLocaleString(buffer,MaxTextExtent,"/Im%.20g Do\n",
cristye8c25f92010-06-03 00:53:06 +00001404 (double) image->scene);
cristy3ed852e2009-09-05 21:47:34 +00001405 (void) WriteBlobString(image,buffer);
1406 (void) WriteBlobString(image,"Q\n");
1407 offset=TellBlob(image)-offset;
1408 (void) WriteBlobString(image,"endstream\n");
1409 (void) WriteBlobString(image,"endobj\n");
1410 /*
1411 Write Length object.
1412 */
1413 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001414 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001415 object);
cristy3ed852e2009-09-05 21:47:34 +00001416 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001417 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
cristy3ed852e2009-09-05 21:47:34 +00001418 (void) WriteBlobString(image,buffer);
1419 (void) WriteBlobString(image,"endobj\n");
1420 /*
1421 Write Procset object.
1422 */
1423 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001424 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",
cristye8c25f92010-06-03 00:53:06 +00001425 (double) object);
cristy3ed852e2009-09-05 21:47:34 +00001426 (void) WriteBlobString(image,buffer);
1427 if ((image->storage_class == DirectClass) || (image->colors > 256))
1428 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageC",MaxTextExtent);
1429 else
1430 if ((compression == FaxCompression) || (compression == Group4Compression))
1431 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageB",MaxTextExtent);
1432 else
1433 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageI",MaxTextExtent);
1434 (void) WriteBlobString(image,buffer);
1435 (void) WriteBlobString(image," ]\n");
1436 (void) WriteBlobString(image,"endobj\n");
1437 /*
1438 Write Font object.
1439 */
1440 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001441 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",
cristye8c25f92010-06-03 00:53:06 +00001442 (double) object);
cristy3ed852e2009-09-05 21:47:34 +00001443 (void) WriteBlobString(image,buffer);
1444 (void) WriteBlobString(image,"<<\n");
1445 if (labels != (char **) NULL)
1446 {
1447 (void) WriteBlobString(image,"/Type /Font\n");
1448 (void) WriteBlobString(image,"/Subtype /Type1\n");
cristyb51dff52011-05-19 16:55:47 +00001449 (void) FormatLocaleString(buffer,MaxTextExtent,"/Name /F%.20g\n",
cristye8c25f92010-06-03 00:53:06 +00001450 (double) image->scene);
cristy3ed852e2009-09-05 21:47:34 +00001451 (void) WriteBlobString(image,buffer);
1452 (void) WriteBlobString(image,"/BaseFont /Helvetica\n");
1453 (void) WriteBlobString(image,"/Encoding /MacRomanEncoding\n");
1454 labels=(char **) RelinquishMagickMemory(labels);
1455 }
1456 (void) WriteBlobString(image,">>\n");
1457 (void) WriteBlobString(image,"endobj\n");
1458 /*
1459 Write XObject object.
1460 */
1461 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001462 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001463 object);
cristy3ed852e2009-09-05 21:47:34 +00001464 (void) WriteBlobString(image,buffer);
1465 (void) WriteBlobString(image,"<<\n");
1466 (void) WriteBlobString(image,"/Type /XObject\n");
1467 (void) WriteBlobString(image,"/Subtype /Image\n");
cristyb51dff52011-05-19 16:55:47 +00001468 (void) FormatLocaleString(buffer,MaxTextExtent,"/Name /Im%.20g\n",
cristye8c25f92010-06-03 00:53:06 +00001469 (double) image->scene);
cristy3ed852e2009-09-05 21:47:34 +00001470 (void) WriteBlobString(image,buffer);
1471 switch (compression)
1472 {
1473 case NoCompression:
1474 {
cristyb51dff52011-05-19 16:55:47 +00001475 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"ASCII85Decode");
cristy3ed852e2009-09-05 21:47:34 +00001476 break;
1477 }
1478 case JPEGCompression:
1479 {
cristyb51dff52011-05-19 16:55:47 +00001480 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"DCTDecode");
cristy3ed852e2009-09-05 21:47:34 +00001481 if (image->colorspace != CMYKColorspace)
1482 break;
1483 (void) WriteBlobString(image,buffer);
1484 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1485 MaxTextExtent);
1486 break;
1487 }
1488 case JPEG2000Compression:
1489 {
cristyb51dff52011-05-19 16:55:47 +00001490 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"JPXDecode");
cristy3ed852e2009-09-05 21:47:34 +00001491 if (image->colorspace != CMYKColorspace)
1492 break;
1493 (void) WriteBlobString(image,buffer);
1494 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1495 MaxTextExtent);
1496 break;
1497 }
1498 case LZWCompression:
1499 {
cristyb51dff52011-05-19 16:55:47 +00001500 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"LZWDecode");
cristyfa7becb2009-09-13 02:44:40 +00001501 break;
cristy3ed852e2009-09-05 21:47:34 +00001502 }
1503 case ZipCompression:
1504 {
cristyb51dff52011-05-19 16:55:47 +00001505 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"FlateDecode");
cristyfa7becb2009-09-13 02:44:40 +00001506 break;
cristy3ed852e2009-09-05 21:47:34 +00001507 }
1508 case FaxCompression:
1509 case Group4Compression:
1510 {
1511 (void) CopyMagickString(buffer,"/Filter [ /CCITTFaxDecode ]\n",
1512 MaxTextExtent);
1513 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001514 (void) FormatLocaleString(buffer,MaxTextExtent,"/DecodeParms [ << "
cristye8c25f92010-06-03 00:53:06 +00001515 "/K %s /BlackIs1 false /Columns %.20g /Rows %.20g >> ]\n",CCITTParam,
1516 (double) image->columns,(double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001517 break;
1518 }
1519 default:
1520 {
cristyb51dff52011-05-19 16:55:47 +00001521 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
cristy3ed852e2009-09-05 21:47:34 +00001522 "RunLengthDecode");
1523 break;
1524 }
1525 }
1526 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001527 (void) FormatLocaleString(buffer,MaxTextExtent,"/Width %.20g\n",(double)
cristye8c25f92010-06-03 00:53:06 +00001528 image->columns);
cristy3ed852e2009-09-05 21:47:34 +00001529 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001530 (void) FormatLocaleString(buffer,MaxTextExtent,"/Height %.20g\n",(double)
cristye8c25f92010-06-03 00:53:06 +00001531 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001532 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001533 (void) FormatLocaleString(buffer,MaxTextExtent,"/ColorSpace %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001534 (double) object+2);
cristy3ed852e2009-09-05 21:47:34 +00001535 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001536 (void) FormatLocaleString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
cristy3ed852e2009-09-05 21:47:34 +00001537 (compression == FaxCompression) || (compression == Group4Compression) ?
1538 1 : 8);
1539 (void) WriteBlobString(image,buffer);
1540 if (image->matte != MagickFalse)
1541 {
cristyb51dff52011-05-19 16:55:47 +00001542 (void) FormatLocaleString(buffer,MaxTextExtent,"/SMask %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001543 (double) object+7);
cristy3ed852e2009-09-05 21:47:34 +00001544 (void) WriteBlobString(image,buffer);
1545 }
cristyb51dff52011-05-19 16:55:47 +00001546 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001547 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001548 (void) WriteBlobString(image,buffer);
1549 (void) WriteBlobString(image,">>\n");
1550 (void) WriteBlobString(image,"stream\n");
1551 offset=TellBlob(image);
1552 number_pixels=(MagickSizeType) image->columns*image->rows;
1553 if ((4*number_pixels) != (MagickSizeType) ((size_t) (4*number_pixels)))
1554 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1555 if ((compression == FaxCompression) || (compression == Group4Compression) ||
1556 ((image_info->type != TrueColorType) &&
cristy1e178e72011-08-28 19:44:34 +00001557 (IsImageGray(image,exception) != MagickFalse)))
cristy3ed852e2009-09-05 21:47:34 +00001558 {
1559 switch (compression)
1560 {
1561 case FaxCompression:
1562 case Group4Compression:
1563 {
1564 if (LocaleCompare(CCITTParam,"0") == 0)
1565 {
1566 (void) HuffmanEncodeImage(image_info,image,image);
1567 break;
1568 }
1569 (void) Huffman2DEncodeImage(image_info,image,image);
1570 break;
1571 }
1572 case JPEGCompression:
1573 {
cristy1e178e72011-08-28 19:44:34 +00001574 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
cristy3ed852e2009-09-05 21:47:34 +00001575 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00001576 {
1577 (void) CloseBlob(image);
1578 return(MagickFalse);
1579 }
cristy3ed852e2009-09-05 21:47:34 +00001580 break;
1581 }
1582 case JPEG2000Compression:
1583 {
cristy1e178e72011-08-28 19:44:34 +00001584 status=InjectImageBlob(image_info,image,image,"jp2",exception);
cristy3ed852e2009-09-05 21:47:34 +00001585 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00001586 {
1587 (void) CloseBlob(image);
1588 return(MagickFalse);
1589 }
cristy3ed852e2009-09-05 21:47:34 +00001590 break;
1591 }
1592 case RLECompression:
1593 default:
1594 {
1595 /*
1596 Allocate pixel array.
1597 */
1598 length=(size_t) number_pixels;
1599 pixels=(unsigned char *) AcquireQuantumMemory(length,
1600 sizeof(*pixels));
1601 if (pixels == (unsigned char *) NULL)
1602 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1603 /*
1604 Dump Runlength encoded pixels.
1605 */
1606 q=pixels;
cristybb503372010-05-27 20:51:26 +00001607 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001608 {
cristy1e178e72011-08-28 19:44:34 +00001609 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001610 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001611 break;
cristybb503372010-05-27 20:51:26 +00001612 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001613 {
cristy4c08aed2011-07-01 19:47:50 +00001614 *q++=ScaleQuantumToChar(GetPixelIntensity(image,p));
cristyed231572011-07-14 02:18:59 +00001615 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001616 }
1617 if (image->previous == (Image *) NULL)
1618 {
cristyf5c61ba2010-12-17 00:58:04 +00001619 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1620 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001621 if (status == MagickFalse)
1622 break;
1623 }
1624 }
1625#if defined(MAGICKCORE_ZLIB_DELEGATE)
1626 if (compression == ZipCompression)
1627 status=ZLIBEncodeImage(image,length,pixels);
1628 else
1629#endif
1630 if (compression == LZWCompression)
1631 status=LZWEncodeImage(image,length,pixels);
1632 else
1633 status=PackbitsEncodeImage(image,length,pixels);
1634 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1635 if (status == MagickFalse)
1636 {
1637 (void) CloseBlob(image);
1638 return(MagickFalse);
1639 }
1640 break;
1641 }
1642 case NoCompression:
1643 {
1644 /*
1645 Dump uncompressed PseudoColor packets.
1646 */
1647 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00001648 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001649 {
cristy1e178e72011-08-28 19:44:34 +00001650 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001651 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001652 break;
cristybb503372010-05-27 20:51:26 +00001653 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001654 {
cristy4c08aed2011-07-01 19:47:50 +00001655 Ascii85Encode(image,ScaleQuantumToChar(
1656 GetPixelIntensity(image,p)));
cristyed231572011-07-14 02:18:59 +00001657 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001658 }
1659 if (image->previous == (Image *) NULL)
1660 {
cristyf5c61ba2010-12-17 00:58:04 +00001661 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1662 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001663 if (status == MagickFalse)
1664 break;
1665 }
1666 }
1667 Ascii85Flush(image);
1668 break;
1669 }
1670 }
1671 }
1672 else
1673 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1674 (compression == JPEGCompression) ||
1675 (compression == JPEG2000Compression))
1676 switch (compression)
1677 {
1678 case JPEGCompression:
1679 {
cristy1e178e72011-08-28 19:44:34 +00001680 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
cristy3ed852e2009-09-05 21:47:34 +00001681 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00001682 {
1683 (void) CloseBlob(image);
1684 return(MagickFalse);
1685 }
cristy3ed852e2009-09-05 21:47:34 +00001686 break;
1687 }
1688 case JPEG2000Compression:
1689 {
cristy1e178e72011-08-28 19:44:34 +00001690 status=InjectImageBlob(image_info,image,image,"jp2",exception);
cristy3ed852e2009-09-05 21:47:34 +00001691 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00001692 {
1693 (void) CloseBlob(image);
1694 return(MagickFalse);
1695 }
cristy3ed852e2009-09-05 21:47:34 +00001696 break;
1697 }
1698 case RLECompression:
1699 default:
1700 {
1701 /*
1702 Allocate pixel array.
1703 */
1704 length=(size_t) number_pixels;
1705 pixels=(unsigned char *) AcquireQuantumMemory(length,
1706 4*sizeof(*pixels));
1707 length*=image->colorspace == CMYKColorspace ? 4UL : 3UL;
1708 if (pixels == (unsigned char *) NULL)
1709 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1710 /*
1711 Dump runoffset encoded pixels.
1712 */
1713 q=pixels;
cristybb503372010-05-27 20:51:26 +00001714 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001715 {
cristy1e178e72011-08-28 19:44:34 +00001716 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001717 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001718 break;
cristybb503372010-05-27 20:51:26 +00001719 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001720 {
cristy4c08aed2011-07-01 19:47:50 +00001721 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1722 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1723 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
cristy3ed852e2009-09-05 21:47:34 +00001724 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00001725 *q++=ScaleQuantumToChar(GetPixelBlack(image,p));
cristyed231572011-07-14 02:18:59 +00001726 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001727 }
1728 if (image->previous == (Image *) NULL)
1729 {
cristyf5c61ba2010-12-17 00:58:04 +00001730 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1731 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001732 if (status == MagickFalse)
1733 break;
1734 }
1735 }
1736#if defined(MAGICKCORE_ZLIB_DELEGATE)
1737 if (compression == ZipCompression)
1738 status=ZLIBEncodeImage(image,length,pixels);
1739 else
1740#endif
1741 if (compression == LZWCompression)
1742 status=LZWEncodeImage(image,length,pixels);
1743 else
1744 status=PackbitsEncodeImage(image,length,pixels);
1745 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1746 if (status == MagickFalse)
1747 {
1748 (void) CloseBlob(image);
1749 return(MagickFalse);
1750 }
1751 break;
1752 }
1753 case NoCompression:
1754 {
1755 /*
1756 Dump uncompressed DirectColor packets.
1757 */
1758 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00001759 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001760 {
cristy1e178e72011-08-28 19:44:34 +00001761 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001762 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001763 break;
cristybb503372010-05-27 20:51:26 +00001764 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001765 {
cristy4c08aed2011-07-01 19:47:50 +00001766 Ascii85Encode(image,ScaleQuantumToChar(GetPixelRed(image,p)));
1767 Ascii85Encode(image,ScaleQuantumToChar(GetPixelGreen(image,p)));
1768 Ascii85Encode(image,ScaleQuantumToChar(GetPixelBlue(image,p)));
cristy3ed852e2009-09-05 21:47:34 +00001769 if (image->colorspace == CMYKColorspace)
cristyaff6d802011-04-26 01:46:31 +00001770 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +00001771 GetPixelBlack(image,p)));
cristyed231572011-07-14 02:18:59 +00001772 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001773 }
1774 if (image->previous == (Image *) NULL)
1775 {
cristyf5c61ba2010-12-17 00:58:04 +00001776 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1777 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001778 if (status == MagickFalse)
1779 break;
1780 }
1781 }
1782 Ascii85Flush(image);
1783 break;
1784 }
1785 }
1786 else
1787 {
1788 /*
1789 Dump number of colors and colormap.
1790 */
1791 switch (compression)
1792 {
1793 case RLECompression:
1794 default:
1795 {
1796 /*
1797 Allocate pixel array.
1798 */
1799 length=(size_t) number_pixels;
1800 pixels=(unsigned char *) AcquireQuantumMemory(length,
1801 sizeof(*pixels));
1802 if (pixels == (unsigned char *) NULL)
1803 ThrowWriterException(ResourceLimitError,
1804 "MemoryAllocationFailed");
1805 /*
1806 Dump Runlength encoded pixels.
1807 */
1808 q=pixels;
cristybb503372010-05-27 20:51:26 +00001809 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001810 {
cristy1e178e72011-08-28 19:44:34 +00001811 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001812 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001813 break;
cristybb503372010-05-27 20:51:26 +00001814 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00001815 {
1816 *q++=(unsigned char) GetPixelIndex(image,p);
cristyed231572011-07-14 02:18:59 +00001817 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001818 }
cristy3ed852e2009-09-05 21:47:34 +00001819 if (image->previous == (Image *) NULL)
1820 {
cristyf5c61ba2010-12-17 00:58:04 +00001821 status=SetImageProgress(image,SaveImageTag,
1822 (MagickOffsetType) y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001823 if (status == MagickFalse)
1824 break;
1825 }
1826 }
1827#if defined(MAGICKCORE_ZLIB_DELEGATE)
1828 if (compression == ZipCompression)
1829 status=ZLIBEncodeImage(image,length,pixels);
1830 else
1831#endif
1832 if (compression == LZWCompression)
1833 status=LZWEncodeImage(image,length,pixels);
1834 else
1835 status=PackbitsEncodeImage(image,length,pixels);
1836 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1837 if (status == MagickFalse)
1838 {
1839 (void) CloseBlob(image);
1840 return(MagickFalse);
1841 }
1842 break;
1843 }
1844 case NoCompression:
1845 {
1846 /*
1847 Dump uncompressed PseudoColor packets.
1848 */
1849 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00001850 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001851 {
cristy1e178e72011-08-28 19:44:34 +00001852 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001853 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001854 break;
cristybb503372010-05-27 20:51:26 +00001855 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00001856 {
1857 Ascii85Encode(image,(unsigned char) GetPixelIndex(image,p));
cristyed231572011-07-14 02:18:59 +00001858 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001859 }
cristy3ed852e2009-09-05 21:47:34 +00001860 if (image->previous == (Image *) NULL)
1861 {
cristyf5c61ba2010-12-17 00:58:04 +00001862 status=SetImageProgress(image,SaveImageTag,
1863 (MagickOffsetType) y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001864 if (status == MagickFalse)
1865 break;
1866 }
1867 }
1868 Ascii85Flush(image);
1869 break;
1870 }
1871 }
1872 }
1873 offset=TellBlob(image)-offset;
1874 (void) WriteBlobString(image,"\nendstream\n");
1875 (void) WriteBlobString(image,"endobj\n");
1876 /*
1877 Write Length object.
1878 */
1879 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001880 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001881 object);
cristy3ed852e2009-09-05 21:47:34 +00001882 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001883 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",
cristye8c25f92010-06-03 00:53:06 +00001884 (double) offset);
cristy3ed852e2009-09-05 21:47:34 +00001885 (void) WriteBlobString(image,buffer);
1886 (void) WriteBlobString(image,"endobj\n");
1887 /*
1888 Write Colorspace object.
1889 */
1890 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001891 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001892 object);
cristy3ed852e2009-09-05 21:47:34 +00001893 (void) WriteBlobString(image,buffer);
1894 if (image->colorspace == CMYKColorspace)
1895 (void) CopyMagickString(buffer,"/DeviceCMYK\n",MaxTextExtent);
1896 else
1897 if ((compression == FaxCompression) ||
1898 (compression == Group4Compression) ||
1899 ((image_info->type != TrueColorType) &&
cristy1e178e72011-08-28 19:44:34 +00001900 (IsImageGray(image,exception) != MagickFalse)))
cristy3ed852e2009-09-05 21:47:34 +00001901 (void) CopyMagickString(buffer,"/DeviceGray\n",MaxTextExtent);
1902 else
1903 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1904 (compression == JPEGCompression) ||
1905 (compression == JPEG2000Compression))
1906 (void) CopyMagickString(buffer,"/DeviceRGB\n",MaxTextExtent);
1907 else
cristyb51dff52011-05-19 16:55:47 +00001908 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00001909 "[ /Indexed /DeviceRGB %.20g %.20g 0 R ]\n",(double) image->colors-
1910 1,(double) object+3);
cristy3ed852e2009-09-05 21:47:34 +00001911 (void) WriteBlobString(image,buffer);
1912 (void) WriteBlobString(image,"endobj\n");
1913 /*
1914 Write Thumb object.
1915 */
1916 SetGeometry(image,&geometry);
1917 (void) ParseMetaGeometry("106x106+0+0>",&geometry.x,&geometry.y,
1918 &geometry.width,&geometry.height);
cristy1e178e72011-08-28 19:44:34 +00001919 tile_image=ThumbnailImage(image,geometry.width,geometry.height,exception);
cristy3ed852e2009-09-05 21:47:34 +00001920 if (tile_image == (Image *) NULL)
cristy1e178e72011-08-28 19:44:34 +00001921 return(MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +00001922 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001923 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001924 object);
cristy3ed852e2009-09-05 21:47:34 +00001925 (void) WriteBlobString(image,buffer);
1926 (void) WriteBlobString(image,"<<\n");
1927 switch (compression)
1928 {
1929 case NoCompression:
1930 {
cristyb51dff52011-05-19 16:55:47 +00001931 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"ASCII85Decode");
cristy3ed852e2009-09-05 21:47:34 +00001932 break;
1933 }
1934 case JPEGCompression:
1935 {
cristyb51dff52011-05-19 16:55:47 +00001936 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"DCTDecode");
cristy3ed852e2009-09-05 21:47:34 +00001937 if (image->colorspace != CMYKColorspace)
1938 break;
1939 (void) WriteBlobString(image,buffer);
1940 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1941 MaxTextExtent);
1942 break;
1943 }
1944 case JPEG2000Compression:
1945 {
cristyb51dff52011-05-19 16:55:47 +00001946 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"JPXDecode");
cristy3ed852e2009-09-05 21:47:34 +00001947 if (image->colorspace != CMYKColorspace)
1948 break;
1949 (void) WriteBlobString(image,buffer);
1950 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1951 MaxTextExtent);
1952 break;
1953 }
1954 case LZWCompression:
1955 {
cristyb51dff52011-05-19 16:55:47 +00001956 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"LZWDecode");
cristy3ed852e2009-09-05 21:47:34 +00001957 break;
1958 }
1959 case ZipCompression:
1960 {
cristyb51dff52011-05-19 16:55:47 +00001961 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"FlateDecode");
cristy3ed852e2009-09-05 21:47:34 +00001962 break;
1963 }
1964 case FaxCompression:
1965 case Group4Compression:
1966 {
1967 (void) CopyMagickString(buffer,"/Filter [ /CCITTFaxDecode ]\n",
1968 MaxTextExtent);
1969 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001970 (void) FormatLocaleString(buffer,MaxTextExtent,"/DecodeParms [ << "
cristye8c25f92010-06-03 00:53:06 +00001971 "/K %s /BlackIs1 false /Columns %.20g /Rows %.20g >> ]\n",CCITTParam,
1972 (double) tile_image->columns,(double) tile_image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001973 break;
1974 }
1975 default:
1976 {
cristyb51dff52011-05-19 16:55:47 +00001977 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
cristy3ed852e2009-09-05 21:47:34 +00001978 "RunLengthDecode");
1979 break;
1980 }
1981 }
1982 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001983 (void) FormatLocaleString(buffer,MaxTextExtent,"/Width %.20g\n",(double)
cristye8c25f92010-06-03 00:53:06 +00001984 tile_image->columns);
cristy3ed852e2009-09-05 21:47:34 +00001985 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001986 (void) FormatLocaleString(buffer,MaxTextExtent,"/Height %.20g\n",(double)
cristye8c25f92010-06-03 00:53:06 +00001987 tile_image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001988 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001989 (void) FormatLocaleString(buffer,MaxTextExtent,"/ColorSpace %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001990 (double) object-1);
cristy3ed852e2009-09-05 21:47:34 +00001991 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001992 (void) FormatLocaleString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
cristy3ed852e2009-09-05 21:47:34 +00001993 (compression == FaxCompression) || (compression == Group4Compression) ?
1994 1 : 8);
1995 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001996 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001997 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001998 (void) WriteBlobString(image,buffer);
1999 (void) WriteBlobString(image,">>\n");
2000 (void) WriteBlobString(image,"stream\n");
2001 offset=TellBlob(image);
2002 number_pixels=(MagickSizeType) tile_image->columns*tile_image->rows;
2003 if ((compression == FaxCompression) ||
2004 (compression == Group4Compression) ||
2005 ((image_info->type != TrueColorType) &&
cristy1e178e72011-08-28 19:44:34 +00002006 (IsImageGray(tile_image,exception) != MagickFalse)))
cristy3ed852e2009-09-05 21:47:34 +00002007 {
2008 switch (compression)
2009 {
2010 case FaxCompression:
2011 case Group4Compression:
2012 {
2013 if (LocaleCompare(CCITTParam,"0") == 0)
2014 {
2015 (void) HuffmanEncodeImage(image_info,image,tile_image);
2016 break;
2017 }
2018 (void) Huffman2DEncodeImage(image_info,image,tile_image);
2019 break;
2020 }
2021 case JPEGCompression:
2022 {
2023 status=InjectImageBlob(image_info,image,tile_image,"jpeg",
cristy1e178e72011-08-28 19:44:34 +00002024 exception);
cristy3ed852e2009-09-05 21:47:34 +00002025 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00002026 {
2027 (void) CloseBlob(image);
2028 return(MagickFalse);
2029 }
cristy3ed852e2009-09-05 21:47:34 +00002030 break;
2031 }
2032 case JPEG2000Compression:
2033 {
cristy1e178e72011-08-28 19:44:34 +00002034 status=InjectImageBlob(image_info,image,tile_image,"jp2",exception);
cristy3ed852e2009-09-05 21:47:34 +00002035 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00002036 {
2037 (void) CloseBlob(image);
2038 return(MagickFalse);
2039 }
cristy3ed852e2009-09-05 21:47:34 +00002040 break;
2041 }
2042 case RLECompression:
2043 default:
2044 {
2045 /*
2046 Allocate pixel array.
2047 */
2048 length=(size_t) number_pixels;
2049 pixels=(unsigned char *) AcquireQuantumMemory(length,
2050 sizeof(*pixels));
2051 if (pixels == (unsigned char *) NULL)
2052 {
2053 tile_image=DestroyImage(tile_image);
2054 ThrowWriterException(ResourceLimitError,
2055 "MemoryAllocationFailed");
2056 }
2057 /*
2058 Dump Runlength encoded pixels.
2059 */
2060 q=pixels;
cristybb503372010-05-27 20:51:26 +00002061 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002062 {
2063 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
cristy1e178e72011-08-28 19:44:34 +00002064 exception);
cristy4c08aed2011-07-01 19:47:50 +00002065 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002066 break;
cristybb503372010-05-27 20:51:26 +00002067 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002068 {
cristy4c08aed2011-07-01 19:47:50 +00002069 *q++=ScaleQuantumToChar(GetPixelIntensity(tile_image,p));
cristyed231572011-07-14 02:18:59 +00002070 p+=GetPixelChannels(tile_image);
cristy3ed852e2009-09-05 21:47:34 +00002071 }
2072 }
2073#if defined(MAGICKCORE_ZLIB_DELEGATE)
2074 if (compression == ZipCompression)
2075 status=ZLIBEncodeImage(image,length,pixels);
2076 else
2077#endif
2078 if (compression == LZWCompression)
2079 status=LZWEncodeImage(image,length,pixels);
2080 else
2081 status=PackbitsEncodeImage(image,length,pixels);
2082 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2083 if (status == MagickFalse)
2084 {
2085 (void) CloseBlob(image);
2086 return(MagickFalse);
2087 }
2088 break;
2089 }
2090 case NoCompression:
2091 {
2092 /*
2093 Dump uncompressed PseudoColor packets.
2094 */
2095 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00002096 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002097 {
2098 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
cristy1e178e72011-08-28 19:44:34 +00002099 exception);
cristy4c08aed2011-07-01 19:47:50 +00002100 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002101 break;
cristybb503372010-05-27 20:51:26 +00002102 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002103 {
2104 Ascii85Encode(image,
cristy4c08aed2011-07-01 19:47:50 +00002105 ScaleQuantumToChar(GetPixelIntensity(tile_image,p)));
cristyed231572011-07-14 02:18:59 +00002106 p+=GetPixelChannels(tile_image);
cristy3ed852e2009-09-05 21:47:34 +00002107 }
2108 }
2109 Ascii85Flush(image);
2110 break;
2111 }
2112 }
2113 }
2114 else
2115 if ((tile_image->storage_class == DirectClass) ||
2116 (tile_image->colors > 256) || (compression == JPEGCompression) ||
2117 (compression == JPEG2000Compression))
2118 switch (compression)
2119 {
2120 case JPEGCompression:
2121 {
2122 status=InjectImageBlob(image_info,image,tile_image,"jpeg",
cristy1e178e72011-08-28 19:44:34 +00002123 exception);
cristy3ed852e2009-09-05 21:47:34 +00002124 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00002125 {
2126 (void) CloseBlob(image);
2127 return(MagickFalse);
2128 }
cristy3ed852e2009-09-05 21:47:34 +00002129 break;
2130 }
2131 case JPEG2000Compression:
2132 {
cristy1e178e72011-08-28 19:44:34 +00002133 status=InjectImageBlob(image_info,image,tile_image,"jp2",exception);
cristy3ed852e2009-09-05 21:47:34 +00002134 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00002135 {
2136 (void) CloseBlob(image);
2137 return(MagickFalse);
2138 }
cristy3ed852e2009-09-05 21:47:34 +00002139 break;
2140 }
2141 case RLECompression:
2142 default:
2143 {
2144 /*
2145 Allocate pixel array.
2146 */
2147 length=(size_t) number_pixels;
2148 pixels=(unsigned char *) AcquireQuantumMemory(length,4*
2149 sizeof(*pixels));
2150 length*=tile_image->colorspace == CMYKColorspace ? 4UL : 3UL;
2151 if (pixels == (unsigned char *) NULL)
2152 {
2153 tile_image=DestroyImage(tile_image);
2154 ThrowWriterException(ResourceLimitError,
2155 "MemoryAllocationFailed");
2156 }
2157 /*
2158 Dump runoffset encoded pixels.
2159 */
2160 q=pixels;
cristybb503372010-05-27 20:51:26 +00002161 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002162 {
2163 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
cristy1e178e72011-08-28 19:44:34 +00002164 exception);
cristy4c08aed2011-07-01 19:47:50 +00002165 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002166 break;
cristybb503372010-05-27 20:51:26 +00002167 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002168 {
cristy4c08aed2011-07-01 19:47:50 +00002169 *q++=ScaleQuantumToChar(GetPixelRed(tile_image,p));
2170 *q++=ScaleQuantumToChar(GetPixelGreen(tile_image,p));
2171 *q++=ScaleQuantumToChar(GetPixelBlue(tile_image,p));
cristy3ed852e2009-09-05 21:47:34 +00002172 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00002173 *q++=ScaleQuantumToChar(GetPixelBlack(tile_image,p));
cristyed231572011-07-14 02:18:59 +00002174 p+=GetPixelChannels(tile_image);
cristy3ed852e2009-09-05 21:47:34 +00002175 }
2176 }
2177#if defined(MAGICKCORE_ZLIB_DELEGATE)
2178 if (compression == ZipCompression)
2179 status=ZLIBEncodeImage(image,length,pixels);
2180 else
2181#endif
2182 if (compression == LZWCompression)
2183 status=LZWEncodeImage(image,length,pixels);
2184 else
2185 status=PackbitsEncodeImage(image,length,pixels);
2186 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2187 if (status == MagickFalse)
2188 {
2189 (void) CloseBlob(image);
2190 return(MagickFalse);
2191 }
2192 break;
2193 }
2194 case NoCompression:
2195 {
2196 /*
2197 Dump uncompressed DirectColor packets.
2198 */
2199 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00002200 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002201 {
2202 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
cristy1e178e72011-08-28 19:44:34 +00002203 exception);
cristy4c08aed2011-07-01 19:47:50 +00002204 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002205 break;
cristybb503372010-05-27 20:51:26 +00002206 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002207 {
cristyf5c61ba2010-12-17 00:58:04 +00002208 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +00002209 GetPixelRed(tile_image,p)));
cristyf5c61ba2010-12-17 00:58:04 +00002210 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +00002211 GetPixelGreen(tile_image,p)));
cristyf5c61ba2010-12-17 00:58:04 +00002212 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +00002213 GetPixelBlue(tile_image,p)));
cristy3ed852e2009-09-05 21:47:34 +00002214 if (image->colorspace == CMYKColorspace)
cristyaff6d802011-04-26 01:46:31 +00002215 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +00002216 GetPixelBlack(tile_image,p)));
cristyed231572011-07-14 02:18:59 +00002217 p+=GetPixelChannels(tile_image);
cristy3ed852e2009-09-05 21:47:34 +00002218 }
2219 }
2220 Ascii85Flush(image);
2221 break;
2222 }
2223 }
2224 else
2225 {
2226 /*
2227 Dump number of colors and colormap.
2228 */
2229 switch (compression)
2230 {
2231 case RLECompression:
2232 default:
2233 {
2234 /*
2235 Allocate pixel array.
2236 */
2237 length=(size_t) number_pixels;
2238 pixels=(unsigned char *) AcquireQuantumMemory(length,
2239 sizeof(*pixels));
2240 if (pixels == (unsigned char *) NULL)
2241 {
2242 tile_image=DestroyImage(tile_image);
2243 ThrowWriterException(ResourceLimitError,
2244 "MemoryAllocationFailed");
2245 }
2246 /*
2247 Dump Runlength encoded pixels.
2248 */
2249 q=pixels;
cristybb503372010-05-27 20:51:26 +00002250 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002251 {
2252 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
cristy1e178e72011-08-28 19:44:34 +00002253 exception);
cristy4c08aed2011-07-01 19:47:50 +00002254 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002255 break;
cristybb503372010-05-27 20:51:26 +00002256 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00002257 {
2258 *q++=(unsigned char) GetPixelIndex(tile_image,p);
cristyed231572011-07-14 02:18:59 +00002259 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00002260 }
cristy3ed852e2009-09-05 21:47:34 +00002261 }
2262#if defined(MAGICKCORE_ZLIB_DELEGATE)
2263 if (compression == ZipCompression)
2264 status=ZLIBEncodeImage(image,length,pixels);
2265 else
2266#endif
2267 if (compression == LZWCompression)
2268 status=LZWEncodeImage(image,length,pixels);
2269 else
2270 status=PackbitsEncodeImage(image,length,pixels);
2271 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2272 if (status == MagickFalse)
2273 {
2274 (void) CloseBlob(image);
2275 return(MagickFalse);
2276 }
2277 break;
2278 }
2279 case NoCompression:
2280 {
2281 /*
2282 Dump uncompressed PseudoColor packets.
2283 */
2284 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00002285 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002286 {
2287 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
cristy1e178e72011-08-28 19:44:34 +00002288 exception);
cristy4c08aed2011-07-01 19:47:50 +00002289 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002290 break;
cristybb503372010-05-27 20:51:26 +00002291 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00002292 {
cristyaff6d802011-04-26 01:46:31 +00002293 Ascii85Encode(image,(unsigned char)
cristy4c08aed2011-07-01 19:47:50 +00002294 GetPixelIndex(tile_image,p));
cristyed231572011-07-14 02:18:59 +00002295 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00002296 }
cristy3ed852e2009-09-05 21:47:34 +00002297 }
2298 Ascii85Flush(image);
2299 break;
2300 }
2301 }
2302 }
2303 tile_image=DestroyImage(tile_image);
2304 offset=TellBlob(image)-offset;
2305 (void) WriteBlobString(image,"\nendstream\n");
2306 (void) WriteBlobString(image,"endobj\n");
2307 /*
2308 Write Length object.
2309 */
2310 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00002311 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002312 object);
cristy3ed852e2009-09-05 21:47:34 +00002313 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002314 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
cristy3ed852e2009-09-05 21:47:34 +00002315 (void) WriteBlobString(image,buffer);
2316 (void) WriteBlobString(image,"endobj\n");
2317 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00002318 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002319 object);
cristy3ed852e2009-09-05 21:47:34 +00002320 (void) WriteBlobString(image,buffer);
2321 if ((image->storage_class != DirectClass) && (image->colors <= 256) &&
2322 (compression != FaxCompression) && (compression != Group4Compression))
2323 {
2324 /*
2325 Write Colormap object.
2326 */
2327 (void) WriteBlobString(image,"<<\n");
2328 if (compression == NoCompression)
2329 (void) WriteBlobString(image,"/Filter [ /ASCII85Decode ]\n");
cristyb51dff52011-05-19 16:55:47 +00002330 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00002331 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00002332 (void) WriteBlobString(image,buffer);
2333 (void) WriteBlobString(image,">>\n");
2334 (void) WriteBlobString(image,"stream\n");
2335 offset=TellBlob(image);
2336 if (compression == NoCompression)
2337 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00002338 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00002339 {
2340 if (compression == NoCompression)
2341 {
2342 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].red));
2343 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].green));
2344 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].blue));
2345 continue;
2346 }
2347 (void) WriteBlobByte(image,
2348 ScaleQuantumToChar(image->colormap[i].red));
2349 (void) WriteBlobByte(image,
2350 ScaleQuantumToChar(image->colormap[i].green));
2351 (void) WriteBlobByte(image,
2352 ScaleQuantumToChar(image->colormap[i].blue));
2353 }
2354 if (compression == NoCompression)
2355 Ascii85Flush(image);
2356 offset=TellBlob(image)-offset;
2357 (void) WriteBlobString(image,"\nendstream\n");
2358 }
2359 (void) WriteBlobString(image,"endobj\n");
2360 /*
2361 Write Length object.
2362 */
2363 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00002364 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002365 object);
cristy3ed852e2009-09-05 21:47:34 +00002366 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002367 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002368 offset);
cristy3ed852e2009-09-05 21:47:34 +00002369 (void) WriteBlobString(image,buffer);
2370 (void) WriteBlobString(image,"endobj\n");
2371 /*
2372 Write softmask object.
2373 */
2374 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00002375 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002376 object);
cristy3ed852e2009-09-05 21:47:34 +00002377 (void) WriteBlobString(image,buffer);
2378 (void) WriteBlobString(image,"<<\n");
2379 if (image->matte == MagickFalse)
2380 (void) WriteBlobString(image,">>\n");
2381 else
2382 {
2383 (void) WriteBlobString(image,"/Type /XObject\n");
2384 (void) WriteBlobString(image,"/Subtype /Image\n");
cristyb51dff52011-05-19 16:55:47 +00002385 (void) FormatLocaleString(buffer,MaxTextExtent,"/Name /Ma%.20g\n",
cristye8c25f92010-06-03 00:53:06 +00002386 (double) image->scene);
cristy3ed852e2009-09-05 21:47:34 +00002387 (void) WriteBlobString(image,buffer);
2388 switch (compression)
2389 {
2390 case NoCompression:
2391 {
cristyb51dff52011-05-19 16:55:47 +00002392 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
cristy3ed852e2009-09-05 21:47:34 +00002393 "ASCII85Decode");
2394 break;
2395 }
2396 case LZWCompression:
2397 {
cristyb51dff52011-05-19 16:55:47 +00002398 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"LZWDecode");
cristy3ed852e2009-09-05 21:47:34 +00002399 break;
2400 }
2401 case ZipCompression:
2402 {
cristyb51dff52011-05-19 16:55:47 +00002403 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
cristy3ed852e2009-09-05 21:47:34 +00002404 "FlateDecode");
2405 break;
2406 }
2407 default:
2408 {
cristyb51dff52011-05-19 16:55:47 +00002409 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
cristy3ed852e2009-09-05 21:47:34 +00002410 "RunLengthDecode");
2411 break;
2412 }
2413 }
2414 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002415 (void) FormatLocaleString(buffer,MaxTextExtent,"/Width %.20g\n",
cristye8c25f92010-06-03 00:53:06 +00002416 (double) image->columns);
cristy3ed852e2009-09-05 21:47:34 +00002417 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002418 (void) FormatLocaleString(buffer,MaxTextExtent,"/Height %.20g\n",
cristye8c25f92010-06-03 00:53:06 +00002419 (double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +00002420 (void) WriteBlobString(image,buffer);
2421 (void) WriteBlobString(image,"/ColorSpace /DeviceGray\n");
cristyb51dff52011-05-19 16:55:47 +00002422 (void) FormatLocaleString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
cristy3ed852e2009-09-05 21:47:34 +00002423 (compression == FaxCompression) || (compression == Group4Compression)
2424 ? 1 : 8);
2425 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002426 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00002427 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00002428 (void) WriteBlobString(image,buffer);
2429 (void) WriteBlobString(image,">>\n");
2430 (void) WriteBlobString(image,"stream\n");
2431 offset=TellBlob(image);
2432 number_pixels=(MagickSizeType) image->columns*image->rows;
2433 switch (compression)
2434 {
2435 case RLECompression:
2436 default:
2437 {
2438 /*
2439 Allocate pixel array.
2440 */
2441 length=(size_t) number_pixels;
2442 pixels=(unsigned char *) AcquireQuantumMemory(length,
2443 sizeof(*pixels));
2444 if (pixels == (unsigned char *) NULL)
2445 {
2446 image=DestroyImage(image);
2447 ThrowWriterException(ResourceLimitError,
2448 "MemoryAllocationFailed");
2449 }
2450 /*
2451 Dump Runlength encoded pixels.
2452 */
2453 q=pixels;
cristybb503372010-05-27 20:51:26 +00002454 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002455 {
cristy1e178e72011-08-28 19:44:34 +00002456 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00002457 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002458 break;
cristybb503372010-05-27 20:51:26 +00002459 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002460 {
cristy4c08aed2011-07-01 19:47:50 +00002461 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
cristyed231572011-07-14 02:18:59 +00002462 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002463 }
2464 }
2465#if defined(MAGICKCORE_ZLIB_DELEGATE)
2466 if (compression == ZipCompression)
2467 status=ZLIBEncodeImage(image,length,pixels);
2468 else
2469#endif
2470 if (compression == LZWCompression)
2471 status=LZWEncodeImage(image,length,pixels);
2472 else
2473 status=PackbitsEncodeImage(image,length,pixels);
2474 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2475 if (status == MagickFalse)
2476 {
2477 (void) CloseBlob(image);
2478 return(MagickFalse);
2479 }
2480 break;
2481 }
2482 case NoCompression:
2483 {
2484 /*
2485 Dump uncompressed PseudoColor packets.
2486 */
2487 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00002488 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002489 {
cristy1e178e72011-08-28 19:44:34 +00002490 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00002491 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002492 break;
cristybb503372010-05-27 20:51:26 +00002493 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002494 {
cristy4c08aed2011-07-01 19:47:50 +00002495 Ascii85Encode(image,ScaleQuantumToChar(GetPixelAlpha(image,p)));
cristyed231572011-07-14 02:18:59 +00002496 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002497 }
2498 }
2499 Ascii85Flush(image);
2500 break;
2501 }
2502 }
2503 offset=TellBlob(image)-offset;
2504 (void) WriteBlobString(image,"\nendstream\n");
2505 }
2506 (void) WriteBlobString(image,"endobj\n");
2507 /*
2508 Write Length object.
2509 */
2510 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00002511 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002512 object);
cristy3ed852e2009-09-05 21:47:34 +00002513 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002514 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
cristy3ed852e2009-09-05 21:47:34 +00002515 (void) WriteBlobString(image,buffer);
2516 (void) WriteBlobString(image,"endobj\n");
2517 if (GetNextImageInList(image) == (Image *) NULL)
2518 break;
2519 image=SyncNextImageInList(image);
2520 status=SetImageProgress(image,SaveImagesTag,scene++,
2521 GetImageListLength(image));
2522 if (status == MagickFalse)
2523 break;
2524 } while (image_info->adjoin != MagickFalse);
2525 /*
2526 Write Metadata object.
2527 */
2528 xref[object++]=TellBlob(image);
2529 info_id=object;
cristyb51dff52011-05-19 16:55:47 +00002530 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002531 object);
cristy3ed852e2009-09-05 21:47:34 +00002532 (void) WriteBlobString(image,buffer);
2533 (void) WriteBlobString(image,"<<\n");
cristy57015272010-12-30 01:16:38 +00002534 GetPathComponent(image->filename,BasePath,basename);
cristyb51dff52011-05-19 16:55:47 +00002535 (void) FormatLocaleString(buffer,MaxTextExtent,"/Title (%s)\n",
cristy57015272010-12-30 01:16:38 +00002536 EscapeParenthesis(basename));
cristy3ed852e2009-09-05 21:47:34 +00002537 (void) WriteBlobString(image,buffer);
2538 seconds=time((time_t *) NULL);
2539#if defined(MAGICKCORE_HAVE_LOCALTIME_R)
2540 (void) localtime_r(&seconds,&local_time);
2541#else
2542 (void) memcpy(&local_time,localtime(&seconds),sizeof(local_time));
2543#endif
cristyb51dff52011-05-19 16:55:47 +00002544 (void) FormatLocaleString(date,MaxTextExtent,"D:%04d%02d%02d%02d%02d%02d",
cristy3ed852e2009-09-05 21:47:34 +00002545 local_time.tm_year+1900,local_time.tm_mon+1,local_time.tm_mday,
2546 local_time.tm_hour,local_time.tm_min,local_time.tm_sec);
cristyb51dff52011-05-19 16:55:47 +00002547 (void) FormatLocaleString(buffer,MaxTextExtent,"/CreationDate (%s)\n",date);
cristy3ed852e2009-09-05 21:47:34 +00002548 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002549 (void) FormatLocaleString(buffer,MaxTextExtent,"/ModDate (%s)\n",date);
cristy3ed852e2009-09-05 21:47:34 +00002550 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002551 (void) FormatLocaleString(buffer,MaxTextExtent,"/Producer (%s)\n",
cristybb503372010-05-27 20:51:26 +00002552 EscapeParenthesis(GetMagickVersion((size_t *) NULL)));
cristy3ed852e2009-09-05 21:47:34 +00002553 (void) WriteBlobString(image,buffer);
2554 (void) WriteBlobString(image,">>\n");
2555 (void) WriteBlobString(image,"endobj\n");
2556 /*
2557 Write Xref object.
2558 */
2559 offset=TellBlob(image)-xref[0]+10;
2560 (void) WriteBlobString(image,"xref\n");
cristyb51dff52011-05-19 16:55:47 +00002561 (void) FormatLocaleString(buffer,MaxTextExtent,"0 %.20g\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002562 object+1);
cristy3ed852e2009-09-05 21:47:34 +00002563 (void) WriteBlobString(image,buffer);
2564 (void) WriteBlobString(image,"0000000000 65535 f \n");
cristybb503372010-05-27 20:51:26 +00002565 for (i=0; i < (ssize_t) object; i++)
cristy3ed852e2009-09-05 21:47:34 +00002566 {
cristyb51dff52011-05-19 16:55:47 +00002567 (void) FormatLocaleString(buffer,MaxTextExtent,"%010lu 00000 n \n",
cristyf2faecf2010-05-28 19:19:36 +00002568 (unsigned long) xref[i]);
cristy3ed852e2009-09-05 21:47:34 +00002569 (void) WriteBlobString(image,buffer);
2570 }
2571 (void) WriteBlobString(image,"trailer\n");
2572 (void) WriteBlobString(image,"<<\n");
cristyb51dff52011-05-19 16:55:47 +00002573 (void) FormatLocaleString(buffer,MaxTextExtent,"/Size %.20g\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002574 object+1);
cristy3ed852e2009-09-05 21:47:34 +00002575 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002576 (void) FormatLocaleString(buffer,MaxTextExtent,"/Info %.20g 0 R\n",(double)
cristye8c25f92010-06-03 00:53:06 +00002577 info_id);
cristy3ed852e2009-09-05 21:47:34 +00002578 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002579 (void) FormatLocaleString(buffer,MaxTextExtent,"/Root %.20g 0 R\n",(double)
cristye8c25f92010-06-03 00:53:06 +00002580 root_id);
cristy3ed852e2009-09-05 21:47:34 +00002581 (void) WriteBlobString(image,buffer);
2582 (void) WriteBlobString(image,">>\n");
2583 (void) WriteBlobString(image,"startxref\n");
cristyb51dff52011-05-19 16:55:47 +00002584 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
cristy3ed852e2009-09-05 21:47:34 +00002585 (void) WriteBlobString(image,buffer);
2586 (void) WriteBlobString(image,"%%EOF\n");
2587 xref=(MagickOffsetType *) RelinquishMagickMemory(xref);
2588 (void) CloseBlob(image);
2589 return(MagickTrue);
2590}