blob: db9499b3acdcbae9bfc17b84ced8a7753b3925bc [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 */
cristy9950d572011-10-01 18:22:35 +0000380 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000381 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"," ");
cristyd15e6592011-10-15 00:13:06 +0000476 (void) SetImageProperty(image,property,value,exception);
cristy3ed852e2009-09-05 21:47:34 +0000477 value=DestroyString(value);
478 continue;
479 }
480 if (LocaleNCompare(PDFVersion,command,strlen(PDFVersion)) == 0)
cristyd15e6592011-10-15 00:13:06 +0000481 (void) SetImageProperty(image,"pdf:Version",command,exception);
cristy3ed852e2009-09-05 21:47:34 +0000482 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);
cristyd15e6592011-10-15 00:13:06 +0000535 (void) SetImageProperty(image,"pdf:HiResBoundingBox",geometry,
536 exception);
cristybb503372010-05-27 20:51:26 +0000537 page.width=(size_t) floor(bounds.x2-bounds.x1+0.5);
538 page.height=(size_t) floor(bounds.y2-bounds.y1+0.5);
cristy3ed852e2009-09-05 21:47:34 +0000539 hires_bounds=bounds;
540 }
541 }
542 (void) CloseBlob(image);
543 if ((fabs(angle) == 90.0) || (fabs(angle) == 270.0))
544 {
cristybb503372010-05-27 20:51:26 +0000545 size_t
cristy3ed852e2009-09-05 21:47:34 +0000546 swap;
547
548 swap=page.width;
549 page.width=page.height;
550 page.height=swap;
551 }
cristy510d06a2011-07-06 23:43:54 +0000552 if (IsRGBColorspace(image_info->colorspace) != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000553 cmyk=MagickFalse;
554 /*
555 Create Ghostscript control file.
556 */
557 file=AcquireUniqueFileResource(postscript_filename);
558 if (file == -1)
559 {
560 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
561 image_info->filename);
562 image=DestroyImage(image);
563 return((Image *) NULL);
564 }
565 count=write(file," ",1);
566 file=close(file)-1;
567 /*
568 Render Postscript with the Ghostscript delegate.
569 */
570 if ((image_info->ping != MagickFalse) ||
571 (image_info->monochrome != MagickFalse))
572 delegate_info=GetDelegateInfo("ps:mono",(char *) NULL,exception);
573 else
574 if (cmyk != MagickFalse)
575 delegate_info=GetDelegateInfo("ps:cmyk",(char *) NULL,exception);
576 else
cristya97426c2011-02-04 01:41:27 +0000577 delegate_info=GetDelegateInfo("ps:alpha",(char *) NULL,exception);
cristy3ed852e2009-09-05 21:47:34 +0000578 if (delegate_info == (const DelegateInfo *) NULL)
579 {
580 (void) RelinquishUniqueFileResource(postscript_filename);
581 image=DestroyImage(image);
582 return((Image *) NULL);
583 }
584 *options='\0';
cristydba40d42010-03-25 18:31:50 +0000585 if (image_info->density != (char *) NULL)
586 {
587 flags=ParseGeometry(image_info->density,&geometry_info);
588 image->x_resolution=geometry_info.rho;
589 image->y_resolution=geometry_info.sigma;
590 if ((flags & SigmaValue) == 0)
591 image->y_resolution=image->x_resolution;
592 }
cristyb51dff52011-05-19 16:55:47 +0000593 (void) FormatLocaleString(density,MaxTextExtent,"%gx%g",image->x_resolution,
cristyd511bb52011-02-03 18:50:56 +0000594 image->y_resolution);
cristy3ed852e2009-09-05 21:47:34 +0000595 if (image_info->page != (char *) NULL)
596 {
597 (void) ParseAbsoluteGeometry(image_info->page,&page);
cristya97426c2011-02-04 01:41:27 +0000598 page.width=(size_t) floor((double) (page.width*image->x_resolution/
599 delta.x)+0.5);
600 page.height=(size_t) floor((double) (page.height*image->y_resolution/
601 delta.y)+0.5);
cristyb51dff52011-05-19 16:55:47 +0000602 (void) FormatLocaleString(options,MaxTextExtent,"-g%.20gx%.20g ",(double)
cristyd511bb52011-02-03 18:50:56 +0000603 page.width,(double) page.height);
cristy3ed852e2009-09-05 21:47:34 +0000604 }
605 if (cmyk != MagickFalse)
606 (void) ConcatenateMagickString(options,"-dUseCIEColor ",MaxTextExtent);
607 if (cropbox != MagickFalse)
608 (void) ConcatenateMagickString(options,"-dUseCropBox ",MaxTextExtent);
609 if (trimbox != MagickFalse)
610 (void) ConcatenateMagickString(options,"-dUseTrimBox ",MaxTextExtent);
611 read_info=CloneImageInfo(image_info);
612 *read_info->magick='\0';
613 if (read_info->number_scenes != 0)
614 {
615 char
616 pages[MaxTextExtent];
617
cristyb51dff52011-05-19 16:55:47 +0000618 (void) FormatLocaleString(pages,MaxTextExtent,"-dFirstPage=%.20g "
cristye8c25f92010-06-03 00:53:06 +0000619 "-dLastPage=%.20g",(double) read_info->scene+1,(double)
cristyf2faecf2010-05-28 19:19:36 +0000620 (read_info->scene+read_info->number_scenes));
cristy3ed852e2009-09-05 21:47:34 +0000621 (void) ConcatenateMagickString(options,pages,MaxTextExtent);
622 read_info->number_scenes=0;
623 if (read_info->scenes != (char *) NULL)
624 *read_info->scenes='\0';
625 }
anthony1afdc7a2011-10-05 11:54:28 +0000626 option=GetImageOption(read_info,"authenticate");
627 if (option != (const char *) NULL)
cristyb51dff52011-05-19 16:55:47 +0000628 (void) FormatLocaleString(options+strlen(options),MaxTextExtent,
anthony1afdc7a2011-10-05 11:54:28 +0000629 " -sPCLPassword=%s",option);
cristy3ed852e2009-09-05 21:47:34 +0000630 (void) CopyMagickString(filename,read_info->filename,MaxTextExtent);
cristya97426c2011-02-04 01:41:27 +0000631 (void) AcquireUniqueFilename(filename);
632 (void) ConcatenateMagickString(filename,"-%08d",MaxTextExtent);
cristyb51dff52011-05-19 16:55:47 +0000633 (void) FormatLocaleString(command,MaxTextExtent,
cristy3ed852e2009-09-05 21:47:34 +0000634 GetDelegateCommands(delegate_info),
635 read_info->antialias != MagickFalse ? 4 : 1,
cristya97426c2011-02-04 01:41:27 +0000636 read_info->antialias != MagickFalse ? 4 : 1,density,options,filename,
637 postscript_filename,input_filename);
cristydefb3f02009-09-10 02:18:35 +0000638 status=InvokePDFDelegate(read_info->verbose,command,exception);
cristy3ed852e2009-09-05 21:47:34 +0000639 (void) RelinquishUniqueFileResource(postscript_filename);
cristy3ed852e2009-09-05 21:47:34 +0000640 (void) RelinquishUniqueFileResource(input_filename);
cristya97426c2011-02-04 01:41:27 +0000641 pdf_image=(Image *) NULL;
642 if (status == MagickFalse)
643 for (i=1; ; i++)
644 {
cristya97426c2011-02-04 01:41:27 +0000645 (void) InterpretImageFilename(image_info,image,filename,(int) i,
646 read_info->filename);
647 if (IsPDFRendered(read_info->filename) == MagickFalse)
648 break;
649 (void) RelinquishUniqueFileResource(read_info->filename);
650 }
651 else
652 for (i=1; ; i++)
653 {
cristya97426c2011-02-04 01:41:27 +0000654 (void) InterpretImageFilename(image_info,image,filename,(int) i,
655 read_info->filename);
656 if (IsPDFRendered(read_info->filename) == MagickFalse)
657 break;
658 next=ReadImage(read_info,exception);
659 (void) RelinquishUniqueFileResource(read_info->filename);
660 if (next == (Image *) NULL)
661 break;
662 AppendImageToList(&pdf_image,next);
663 }
cristy3ed852e2009-09-05 21:47:34 +0000664 read_info=DestroyImageInfo(read_info);
665 if (pdf_image == (Image *) NULL)
666 {
667 ThrowFileException(exception,DelegateError,"PostscriptDelegateFailed",
668 image_info->filename);
cristyb6cabe82011-03-18 13:25:59 +0000669 image=DestroyImage(image);
cristy3ed852e2009-09-05 21:47:34 +0000670 return((Image *) NULL);
671 }
672 if (LocaleCompare(pdf_image->magick,"BMP") == 0)
673 {
674 Image
675 *cmyk_image;
676
677 cmyk_image=ConsolidateCMYKImages(pdf_image,exception);
678 if (cmyk_image != (Image *) NULL)
679 {
680 pdf_image=DestroyImageList(pdf_image);
681 pdf_image=cmyk_image;
682 }
683 }
684 if (image_info->number_scenes != 0)
685 {
686 Image
687 *clone_image;
688
cristybb503372010-05-27 20:51:26 +0000689 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000690 i;
691
692 /*
693 Add place holder images to meet the subimage specification requirement.
694 */
cristybb503372010-05-27 20:51:26 +0000695 for (i=0; i < (ssize_t) image_info->scene; i++)
cristy3ed852e2009-09-05 21:47:34 +0000696 {
697 clone_image=CloneImage(pdf_image,1,1,MagickTrue,exception);
698 if (clone_image != (Image *) NULL)
699 PrependImageToList(&pdf_image,clone_image);
700 }
701 }
702 do
703 {
704 (void) CopyMagickString(pdf_image->filename,filename,MaxTextExtent);
705 pdf_image->page=page;
706 (void) CloneImageProfiles(pdf_image,image);
707 (void) CloneImageProperties(pdf_image,image);
708 next=SyncNextImageInList(pdf_image);
709 if (next != (Image *) NULL)
710 pdf_image=next;
711 } while (next != (Image *) NULL);
712 image=DestroyImage(image);
713 scene=0;
714 for (next=GetFirstImageInList(pdf_image); next != (Image *) NULL; )
715 {
716 next->scene=scene++;
717 next=GetNextImageInList(next);
718 }
719 return(GetFirstImageInList(pdf_image));
720}
721
722/*
723%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
724% %
725% %
726% %
727% R e g i s t e r P D F I m a g e %
728% %
729% %
730% %
731%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
732%
733% RegisterPDFImage() adds properties for the PDF image format to
734% the list of supported formats. The properties include the image format
735% tag, a method to read and/or write the format, whether the format
736% supports the saving of more than one frame to the same file or blob,
737% whether the format supports native in-memory I/O, and a brief
738% description of the format.
739%
740% The format of the RegisterPDFImage method is:
741%
cristybb503372010-05-27 20:51:26 +0000742% size_t RegisterPDFImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000743%
744*/
cristybb503372010-05-27 20:51:26 +0000745ModuleExport size_t RegisterPDFImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000746{
747 MagickInfo
748 *entry;
749
750 entry=SetMagickInfo("AI");
751 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
752 entry->encoder=(EncodeImageHandler *) WritePDFImage;
753 entry->adjoin=MagickFalse;
754 entry->blob_support=MagickFalse;
755 entry->seekable_stream=MagickTrue;
756 entry->thread_support=EncoderThreadSupport;
757 entry->description=ConstantString("Adobe Illustrator CS2");
758 entry->module=ConstantString("PDF");
759 (void) RegisterMagickInfo(entry);
760 entry=SetMagickInfo("EPDF");
761 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
762 entry->encoder=(EncodeImageHandler *) WritePDFImage;
763 entry->adjoin=MagickFalse;
764 entry->blob_support=MagickFalse;
765 entry->seekable_stream=MagickTrue;
766 entry->thread_support=EncoderThreadSupport;
767 entry->description=ConstantString("Encapsulated Portable Document Format");
768 entry->module=ConstantString("PDF");
769 (void) RegisterMagickInfo(entry);
770 entry=SetMagickInfo("PDF");
771 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
772 entry->encoder=(EncodeImageHandler *) WritePDFImage;
773 entry->magick=(IsImageFormatHandler *) IsPDF;
774 entry->blob_support=MagickFalse;
775 entry->seekable_stream=MagickTrue;
776 entry->thread_support=EncoderThreadSupport;
777 entry->description=ConstantString("Portable Document Format");
778 entry->module=ConstantString("PDF");
779 (void) RegisterMagickInfo(entry);
780 entry=SetMagickInfo("PDFA");
781 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
782 entry->encoder=(EncodeImageHandler *) WritePDFImage;
783 entry->magick=(IsImageFormatHandler *) IsPDF;
784 entry->blob_support=MagickFalse;
785 entry->seekable_stream=MagickTrue;
786 entry->thread_support=EncoderThreadSupport;
787 entry->description=ConstantString("Portable Document Archive Format");
788 entry->module=ConstantString("PDF");
789 (void) RegisterMagickInfo(entry);
790 return(MagickImageCoderSignature);
791}
792
793/*
794%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
795% %
796% %
797% %
798% U n r e g i s t e r P D F I m a g e %
799% %
800% %
801% %
802%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
803%
804% UnregisterPDFImage() removes format registrations made by the
805% PDF module from the list of supported formats.
806%
807% The format of the UnregisterPDFImage method is:
808%
809% UnregisterPDFImage(void)
810%
811*/
812ModuleExport void UnregisterPDFImage(void)
813{
814 (void) UnregisterMagickInfo("AI");
815 (void) UnregisterMagickInfo("EPDF");
816 (void) UnregisterMagickInfo("PDF");
817 (void) UnregisterMagickInfo("PDFA");
818}
819
820/*
821%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
822% %
823% %
824% %
825% W r i t e P D F I m a g e %
826% %
827% %
828% %
829%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
830%
831% WritePDFImage() writes an image in the Portable Document image
832% format.
833%
834% The format of the WritePDFImage method is:
835%
cristy1e178e72011-08-28 19:44:34 +0000836% MagickBooleanType WritePDFImage(const ImageInfo *image_info,
837% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000838%
839% A description of each parameter follows.
840%
841% o image_info: the image info.
842%
843% o image: The image.
844%
cristy1e178e72011-08-28 19:44:34 +0000845% o exception: return any errors or warnings in this structure.
846%
cristy3ed852e2009-09-05 21:47:34 +0000847*/
848
849static inline size_t MagickMax(const size_t x,const size_t y)
850{
851 if (x > y)
852 return(x);
853 return(y);
854}
855
856static inline size_t MagickMin(const size_t x,const size_t y)
857{
858 if (x < y)
859 return(x);
860 return(y);
861}
862
863static char *EscapeParenthesis(const char *text)
864{
865 register char
866 *p;
867
cristybb503372010-05-27 20:51:26 +0000868 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000869 i;
870
cristybb503372010-05-27 20:51:26 +0000871 size_t
cristy3ed852e2009-09-05 21:47:34 +0000872 escapes;
873
cristyaff6d802011-04-26 01:46:31 +0000874 static char
875 buffer[MaxTextExtent];
876
cristy3ed852e2009-09-05 21:47:34 +0000877 escapes=0;
878 p=buffer;
cristybb503372010-05-27 20:51:26 +0000879 for (i=0; i < (ssize_t) MagickMin(strlen(text),(MaxTextExtent-escapes-1)); i++)
cristy3ed852e2009-09-05 21:47:34 +0000880 {
881 if ((text[i] == '(') || (text[i] == ')'))
882 {
883 *p++='\\';
884 escapes++;
885 }
886 *p++=text[i];
887 }
888 *p='\0';
889 return(buffer);
890}
891
cristy47b838c2009-09-19 16:09:30 +0000892static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
cristy018f07f2011-09-04 21:15:19 +0000893 Image *image,Image *inject_image,ExceptionInfo *exception)
cristy47b838c2009-09-19 16:09:30 +0000894{
cristy47b838c2009-09-19 16:09:30 +0000895 Image
cristy80975862009-09-25 14:34:31 +0000896 *group4_image;
cristy47b838c2009-09-19 16:09:30 +0000897
898 ImageInfo
899 *write_info;
900
cristy47b838c2009-09-19 16:09:30 +0000901 MagickBooleanType
902 status;
903
cristy80975862009-09-25 14:34:31 +0000904 size_t
905 length;
cristy47b838c2009-09-19 16:09:30 +0000906
907 unsigned char
cristy80975862009-09-25 14:34:31 +0000908 *group4;
cristy47b838c2009-09-19 16:09:30 +0000909
cristy42751fe2009-10-05 00:15:50 +0000910 status=MagickTrue;
cristy47b838c2009-09-19 16:09:30 +0000911 write_info=CloneImageInfo(image_info);
cristy80975862009-09-25 14:34:31 +0000912 (void) CopyMagickString(write_info->filename,"GROUP4:",MaxTextExtent);
913 (void) CopyMagickString(write_info->magick,"GROUP4",MaxTextExtent);
cristy018f07f2011-09-04 21:15:19 +0000914 group4_image=CloneImage(inject_image,0,0,MagickTrue,exception);
cristy80975862009-09-25 14:34:31 +0000915 if (group4_image == (Image *) NULL)
916 return(MagickFalse);
917 group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
cristy018f07f2011-09-04 21:15:19 +0000918 exception);
cristy80975862009-09-25 14:34:31 +0000919 group4_image=DestroyImage(group4_image);
920 if (group4 == (unsigned char *) NULL)
921 return(MagickFalse);
cristy47b838c2009-09-19 16:09:30 +0000922 write_info=DestroyImageInfo(write_info);
cristy80975862009-09-25 14:34:31 +0000923 if (WriteBlob(image,length,group4) != (ssize_t) length)
924 status=MagickFalse;
925 group4=(unsigned char *) RelinquishMagickMemory(group4);
926 return(status);
cristy47b838c2009-09-19 16:09:30 +0000927}
928
cristy1e178e72011-08-28 19:44:34 +0000929static MagickBooleanType WritePDFImage(const ImageInfo *image_info,Image *image,
930 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000931{
932#define CFormat "/Filter [ /%s ]\n"
933#define ObjectsPerImage 14
934
935 static const char
936 XMPProfile[]=
937 {
938 "<?xpacket begin=\"%s\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n"
939 "<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"
940 " <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n"
941 " <rdf:Description rdf:about=\"\"\n"
942 " xmlns:xap=\"http://ns.adobe.com/xap/1.0/\">\n"
943 " <xap:ModifyDate>%s</xap:ModifyDate>\n"
944 " <xap:CreateDate>%s</xap:CreateDate>\n"
945 " <xap:MetadataDate>%s</xap:MetadataDate>\n"
946 " <xap:CreatorTool>%s</xap:CreatorTool>\n"
947 " </rdf:Description>\n"
948 " <rdf:Description rdf:about=\"\"\n"
949 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"
950 " <dc:format>application/pdf</dc:format>\n"
951 " </rdf:Description>\n"
952 " <rdf:Description rdf:about=\"\"\n"
953 " xmlns:xapMM=\"http://ns.adobe.com/xap/1.0/mm/\">\n"
954 " <xapMM:DocumentID>uuid:6ec119d7-7982-4f56-808d-dfe64f5b35cf</xapMM:DocumentID>\n"
955 " <xapMM:InstanceID>uuid:a79b99b4-6235-447f-9f6c-ec18ef7555cb</xapMM:InstanceID>\n"
956 " </rdf:Description>\n"
957 " <rdf:Description rdf:about=\"\"\n"
958 " xmlns:pdf=\"http://ns.adobe.com/pdf/1.3/\">\n"
959 " <pdf:Producer>%s</pdf:Producer>\n"
960 " </rdf:Description>\n"
961 " <rdf:Description rdf:about=\"\"\n"
962 " xmlns:pdfaid=\"http://www.aiim.org/pdfa/ns/id/\">\n"
963 " <pdfaid:part>1</pdfaid:part>\n"
964 " <pdfaid:conformance>B</pdfaid:conformance>\n"
965 " </rdf:Description>\n"
966 " </rdf:RDF>\n"
967 "</x:xmpmeta>\n"
968 "<?xpacket end=\"w\"?>\n"
969 },
970 XMPProfileMagick[4]= { (char) 0xef, (char) 0xbb, (char) 0xbf, (char) 0x00 };
971
972 char
cristy57015272010-12-30 01:16:38 +0000973 basename[MaxTextExtent],
cristy3ed852e2009-09-05 21:47:34 +0000974 buffer[MaxTextExtent],
975 date[MaxTextExtent],
976 **labels,
977 page_geometry[MaxTextExtent];
978
979 CompressionType
980 compression;
981
982 const char
983 *value;
984
985 double
986 pointsize;
987
988 GeometryInfo
989 geometry_info;
990
cristy3ed852e2009-09-05 21:47:34 +0000991 Image
992 *next,
993 *tile_image;
994
995 MagickBooleanType
996 status;
997
998 MagickOffsetType
999 offset,
1000 scene,
1001 *xref;
1002
1003 MagickSizeType
1004 number_pixels;
1005
1006 MagickStatusType
1007 flags;
1008
1009 PointInfo
1010 delta,
1011 resolution,
1012 scale;
1013
1014 RectangleInfo
1015 geometry,
1016 media_info,
1017 page_info;
1018
cristy4c08aed2011-07-01 19:47:50 +00001019 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +00001020 *p;
1021
1022 register unsigned char
1023 *q;
1024
cristybb503372010-05-27 20:51:26 +00001025 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001026 i,
1027 x;
1028
1029 size_t
cristyaff6d802011-04-26 01:46:31 +00001030 info_id,
1031 length,
1032 object,
1033 pages_id,
1034 root_id,
1035 text_size,
1036 version;
1037
1038 ssize_t
1039 count,
1040 y;
cristy3ed852e2009-09-05 21:47:34 +00001041
1042 struct tm
1043 local_time;
1044
1045 time_t
1046 seconds;
1047
1048 unsigned char
1049 *pixels;
1050
cristy3ed852e2009-09-05 21:47:34 +00001051 /*
1052 Open output image file.
1053 */
1054 assert(image_info != (const ImageInfo *) NULL);
1055 assert(image_info->signature == MagickSignature);
1056 assert(image != (Image *) NULL);
1057 assert(image->signature == MagickSignature);
1058 if (image->debug != MagickFalse)
1059 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +00001060 assert(exception != (ExceptionInfo *) NULL);
1061 assert(exception->signature == MagickSignature);
cristy1e178e72011-08-28 19:44:34 +00001062 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +00001063 if (status == MagickFalse)
1064 return(status);
1065 /*
1066 Allocate X ref memory.
1067 */
1068 xref=(MagickOffsetType *) AcquireQuantumMemory(2048UL,sizeof(*xref));
1069 if (xref == (MagickOffsetType *) NULL)
1070 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1071 (void) ResetMagickMemory(xref,0,2048UL*sizeof(*xref));
1072 /*
1073 Write Info object.
1074 */
1075 object=0;
1076 version=3;
1077 if (image_info->compression == JPEG2000Compression)
cristybb503372010-05-27 20:51:26 +00001078 version=(size_t) MagickMax(version,5);
cristy3ed852e2009-09-05 21:47:34 +00001079 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
1080 if (next->matte != MagickFalse)
cristybb503372010-05-27 20:51:26 +00001081 version=(size_t) MagickMax(version,4);
cristy3ed852e2009-09-05 21:47:34 +00001082 if (LocaleCompare(image_info->magick,"PDFA") == 0)
cristyc04c60e2011-05-31 23:58:45 +00001083 version=(size_t) MagickMax(version,6);
cristyb51dff52011-05-19 16:55:47 +00001084 (void) FormatLocaleString(buffer,MaxTextExtent,"%%PDF-1.%.20g \n",
cristye8c25f92010-06-03 00:53:06 +00001085 (double) version);
cristy3ed852e2009-09-05 21:47:34 +00001086 (void) WriteBlobString(image,buffer);
1087 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1088 (void) WriteBlobString(image,"%âãÏÓ\n");
1089 /*
1090 Write Catalog object.
1091 */
1092 xref[object++]=TellBlob(image);
1093 root_id=object;
cristyb51dff52011-05-19 16:55:47 +00001094 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristye8c25f92010-06-03 00:53:06 +00001095 object);
cristy3ed852e2009-09-05 21:47:34 +00001096 (void) WriteBlobString(image,buffer);
1097 (void) WriteBlobString(image,"<<\n");
1098 if (LocaleCompare(image_info->magick,"PDFA") != 0)
cristyb51dff52011-05-19 16:55:47 +00001099 (void) FormatLocaleString(buffer,MaxTextExtent,"/Pages %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001100 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001101 else
1102 {
cristyb51dff52011-05-19 16:55:47 +00001103 (void) FormatLocaleString(buffer,MaxTextExtent,"/Metadata %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001104 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001105 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001106 (void) FormatLocaleString(buffer,MaxTextExtent,"/Pages %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001107 (double) object+2);
cristy3ed852e2009-09-05 21:47:34 +00001108 }
1109 (void) WriteBlobString(image,buffer);
1110 (void) WriteBlobString(image,"/Type /Catalog\n");
1111 (void) WriteBlobString(image,">>\n");
1112 (void) WriteBlobString(image,"endobj\n");
1113 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1114 {
1115 char
1116 create_date[MaxTextExtent],
1117 modify_date[MaxTextExtent],
1118 timestamp[MaxTextExtent],
1119 xmp_profile[MaxTextExtent];
1120
cristybb503372010-05-27 20:51:26 +00001121 size_t
cristy3ed852e2009-09-05 21:47:34 +00001122 version;
1123
1124 /*
1125 Write XMP object.
1126 */
1127 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001128 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",
cristye8c25f92010-06-03 00:53:06 +00001129 (double) object);
cristy3ed852e2009-09-05 21:47:34 +00001130 (void) WriteBlobString(image,buffer);
1131 (void) WriteBlobString(image,"<<\n");
1132 (void) WriteBlobString(image,"/Subtype /XML\n");
1133 *modify_date='\0';
cristyd15e6592011-10-15 00:13:06 +00001134 value=GetImageProperty(image,"date:modify",exception);
cristy3ed852e2009-09-05 21:47:34 +00001135 if (value != (const char *) NULL)
1136 (void) CopyMagickString(modify_date,value,MaxTextExtent);
1137 *create_date='\0';
cristyd15e6592011-10-15 00:13:06 +00001138 value=GetImageProperty(image,"date:create",exception);
cristy3ed852e2009-09-05 21:47:34 +00001139 if (value != (const char *) NULL)
1140 (void) CopyMagickString(create_date,value,MaxTextExtent);
1141 (void) FormatMagickTime(time((time_t *) NULL),MaxTextExtent,timestamp);
cristyb51dff52011-05-19 16:55:47 +00001142 i=FormatLocaleString(xmp_profile,MaxTextExtent,XMPProfile,
cristy3ed852e2009-09-05 21:47:34 +00001143 XMPProfileMagick,modify_date,create_date,timestamp,
1144 GetMagickVersion(&version),GetMagickVersion(&version));
cristyb51dff52011-05-19 16:55:47 +00001145 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g\n",
cristye8c25f92010-06-03 00:53:06 +00001146 (double) i);
cristy3ed852e2009-09-05 21:47:34 +00001147 (void) WriteBlobString(image,buffer);
1148 (void) WriteBlobString(image,"/Type /Metadata\n");
1149 (void) WriteBlobString(image,">>\nstream\n");
1150 (void) WriteBlobString(image,xmp_profile);
1151 (void) WriteBlobString(image,"endstream\n");
1152 (void) WriteBlobString(image,"endobj\n");
1153 }
1154 /*
1155 Write Pages object.
1156 */
1157 xref[object++]=TellBlob(image);
1158 pages_id=object;
cristyb51dff52011-05-19 16:55:47 +00001159 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001160 object);
cristy3ed852e2009-09-05 21:47:34 +00001161 (void) WriteBlobString(image,buffer);
1162 (void) WriteBlobString(image,"<<\n");
1163 (void) WriteBlobString(image,"/Type /Pages\n");
cristyb51dff52011-05-19 16:55:47 +00001164 (void) FormatLocaleString(buffer,MaxTextExtent,"/Kids [ %.20g 0 R ",
cristye8c25f92010-06-03 00:53:06 +00001165 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001166 (void) WriteBlobString(image,buffer);
cristybb503372010-05-27 20:51:26 +00001167 count=(ssize_t) (pages_id+ObjectsPerImage+1);
cristy3ed852e2009-09-05 21:47:34 +00001168 if (image_info->adjoin != MagickFalse)
1169 {
1170 Image
1171 *kid_image;
1172
1173 /*
1174 Predict page object id's.
1175 */
1176 kid_image=image;
1177 for ( ; GetNextImageInList(kid_image) != (Image *) NULL; count+=ObjectsPerImage)
1178 {
cristyb51dff52011-05-19 16:55:47 +00001179 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 R ",(double)
cristye8c25f92010-06-03 00:53:06 +00001180 count);
cristy3ed852e2009-09-05 21:47:34 +00001181 (void) WriteBlobString(image,buffer);
1182 kid_image=GetNextImageInList(kid_image);
1183 }
1184 xref=(MagickOffsetType *) ResizeQuantumMemory(xref,(size_t) count+2048UL,
1185 sizeof(*xref));
1186 if (xref == (MagickOffsetType *) NULL)
1187 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1188 }
1189 (void) WriteBlobString(image,"]\n");
cristyb51dff52011-05-19 16:55:47 +00001190 (void) FormatLocaleString(buffer,MaxTextExtent,"/Count %.20g\n",
cristye8c25f92010-06-03 00:53:06 +00001191 (double) ((count-pages_id)/ObjectsPerImage));
cristy3ed852e2009-09-05 21:47:34 +00001192 (void) WriteBlobString(image,buffer);
1193 (void) WriteBlobString(image,">>\n");
1194 (void) WriteBlobString(image,"endobj\n");
1195 scene=0;
1196 do
1197 {
1198 compression=image->compression;
1199 if (image_info->compression != UndefinedCompression)
1200 compression=image_info->compression;
1201 switch (compression)
1202 {
1203 case FaxCompression:
1204 case Group4Compression:
1205 {
cristy1e178e72011-08-28 19:44:34 +00001206 if ((IsImageMonochrome(image,exception) == MagickFalse) ||
cristy3ed852e2009-09-05 21:47:34 +00001207 (image->matte != MagickFalse))
1208 compression=RLECompression;
1209 break;
1210 }
1211#if !defined(MAGICKCORE_JPEG_DELEGATE)
1212 case JPEGCompression:
1213 {
1214 compression=RLECompression;
cristy1e178e72011-08-28 19:44:34 +00001215 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00001216 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
1217 image->filename);
1218 break;
1219 }
1220#endif
1221#if !defined(MAGICKCORE_JP2_DELEGATE)
1222 case JPEG2000Compression:
1223 {
1224 compression=RLECompression;
cristy1e178e72011-08-28 19:44:34 +00001225 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00001226 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JP2)",
1227 image->filename);
1228 break;
1229 }
1230#endif
1231#if !defined(MAGICKCORE_ZLIB_DELEGATE)
1232 case ZipCompression:
1233 {
1234 compression=RLECompression;
cristy1e178e72011-08-28 19:44:34 +00001235 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00001236 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZLIB)",
1237 image->filename);
1238 break;
1239 }
1240#endif
1241 case LZWCompression:
1242 {
1243 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1244 compression=RLECompression; /* LZW compression is forbidden */
1245 break;
1246 }
1247 case NoCompression:
1248 {
1249 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1250 compression=RLECompression; /* ASCII 85 compression is forbidden */
1251 break;
1252 }
1253 default:
1254 break;
1255 }
1256 if (compression == JPEG2000Compression)
1257 {
cristy510d06a2011-07-06 23:43:54 +00001258 if (IsRGBColorspace(image->colorspace) == MagickFalse)
cristye941a752011-10-15 01:52:48 +00001259 (void) TransformImageColorspace(image,RGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00001260 }
1261 /*
1262 Scale relative to dots-per-inch.
1263 */
1264 delta.x=DefaultResolution;
1265 delta.y=DefaultResolution;
1266 resolution.x=image->x_resolution;
1267 resolution.y=image->y_resolution;
1268 if ((resolution.x == 0.0) || (resolution.y == 0.0))
1269 {
1270 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
1271 resolution.x=geometry_info.rho;
1272 resolution.y=geometry_info.sigma;
1273 if ((flags & SigmaValue) == 0)
1274 resolution.y=resolution.x;
1275 }
1276 if (image_info->density != (char *) NULL)
1277 {
1278 flags=ParseGeometry(image_info->density,&geometry_info);
1279 resolution.x=geometry_info.rho;
1280 resolution.y=geometry_info.sigma;
1281 if ((flags & SigmaValue) == 0)
1282 resolution.y=resolution.x;
1283 }
1284 if (image->units == PixelsPerCentimeterResolution)
1285 {
cristya97426c2011-02-04 01:41:27 +00001286 resolution.x=(double) ((size_t) (100.0*2.54*resolution.x+0.5)/100.0);
1287 resolution.y=(double) ((size_t) (100.0*2.54*resolution.y+0.5)/100.0);
cristy3ed852e2009-09-05 21:47:34 +00001288 }
1289 SetGeometry(image,&geometry);
cristyb51dff52011-05-19 16:55:47 +00001290 (void) FormatLocaleString(page_geometry,MaxTextExtent,"%.20gx%.20g",
cristye8c25f92010-06-03 00:53:06 +00001291 (double) image->columns,(double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001292 if (image_info->page != (char *) NULL)
1293 (void) CopyMagickString(page_geometry,image_info->page,MaxTextExtent);
1294 else
1295 if ((image->page.width != 0) && (image->page.height != 0))
cristyb51dff52011-05-19 16:55:47 +00001296 (void) FormatLocaleString(page_geometry,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00001297 "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,
1298 (double) image->page.height,(double) image->page.x,(double)
1299 image->page.y);
cristy3ed852e2009-09-05 21:47:34 +00001300 else
1301 if ((image->gravity != UndefinedGravity) &&
1302 (LocaleCompare(image_info->magick,"PDF") == 0))
1303 (void) CopyMagickString(page_geometry,PSPageGeometry,MaxTextExtent);
1304 (void) ConcatenateMagickString(page_geometry,">",MaxTextExtent);
1305 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
1306 &geometry.width,&geometry.height);
1307 scale.x=(double) (geometry.width*delta.x)/resolution.x;
cristybb503372010-05-27 20:51:26 +00001308 geometry.width=(size_t) floor(scale.x+0.5);
cristy3ed852e2009-09-05 21:47:34 +00001309 scale.y=(double) (geometry.height*delta.y)/resolution.y;
cristybb503372010-05-27 20:51:26 +00001310 geometry.height=(size_t) floor(scale.y+0.5);
cristy3ed852e2009-09-05 21:47:34 +00001311 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
cristy1e178e72011-08-28 19:44:34 +00001312 (void) ParseGravityGeometry(image,page_geometry,&page_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00001313 if (image->gravity != UndefinedGravity)
1314 {
1315 geometry.x=(-page_info.x);
cristybb503372010-05-27 20:51:26 +00001316 geometry.y=(ssize_t) (media_info.height+page_info.y-image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001317 }
1318 pointsize=12.0;
1319 if (image_info->pointsize != 0.0)
1320 pointsize=image_info->pointsize;
1321 text_size=0;
cristyd15e6592011-10-15 00:13:06 +00001322 value=GetImageProperty(image,"label",exception);
cristy3ed852e2009-09-05 21:47:34 +00001323 if (value != (const char *) NULL)
cristybb503372010-05-27 20:51:26 +00001324 text_size=(size_t) (MultilineCensus(value)*pointsize+12);
cristyda16f162011-02-19 23:52:17 +00001325 (void) text_size;
cristy3ed852e2009-09-05 21:47:34 +00001326 /*
1327 Write Page object.
1328 */
1329 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001330 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001331 object);
cristy3ed852e2009-09-05 21:47:34 +00001332 (void) WriteBlobString(image,buffer);
1333 (void) WriteBlobString(image,"<<\n");
1334 (void) WriteBlobString(image,"/Type /Page\n");
cristyb51dff52011-05-19 16:55:47 +00001335 (void) FormatLocaleString(buffer,MaxTextExtent,"/Parent %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001336 (double) pages_id);
cristy3ed852e2009-09-05 21:47:34 +00001337 (void) WriteBlobString(image,buffer);
1338 (void) WriteBlobString(image,"/Resources <<\n");
1339 labels=(char **) NULL;
cristyd15e6592011-10-15 00:13:06 +00001340 value=GetImageProperty(image,"label",exception);
cristy3ed852e2009-09-05 21:47:34 +00001341 if (value != (const char *) NULL)
1342 labels=StringToList(value);
1343 if (labels != (char **) NULL)
1344 {
cristyb51dff52011-05-19 16:55:47 +00001345 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00001346 "/Font << /F%.20g %.20g 0 R >>\n",(double) image->scene,(double)
1347 object+4);
cristy3ed852e2009-09-05 21:47:34 +00001348 (void) WriteBlobString(image,buffer);
1349 }
cristyb51dff52011-05-19 16:55:47 +00001350 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00001351 "/XObject << /Im%.20g %.20g 0 R >>\n",(double) image->scene,(double)
1352 object+5);
cristy3ed852e2009-09-05 21:47:34 +00001353 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001354 (void) FormatLocaleString(buffer,MaxTextExtent,"/ProcSet %.20g 0 R >>\n",
cristye8c25f92010-06-03 00:53:06 +00001355 (double) object+3);
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 "/MediaBox [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,
cristye7f51092010-01-17 00:39:37 +00001362 "/CropBox [0 0 %g %g]\n",72.0*media_info.width/resolution.x,
cristy8cd5b312010-01-07 01:10:24 +00001363 72.0*media_info.height/resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001364 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001365 (void) FormatLocaleString(buffer,MaxTextExtent,"/Contents %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001366 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001367 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001368 (void) FormatLocaleString(buffer,MaxTextExtent,"/Thumb %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001369 (double) object+8);
cristy3ed852e2009-09-05 21:47:34 +00001370 (void) WriteBlobString(image,buffer);
1371 (void) WriteBlobString(image,">>\n");
1372 (void) WriteBlobString(image,"endobj\n");
1373 /*
1374 Write Contents object.
1375 */
1376 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001377 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001378 object);
cristy3ed852e2009-09-05 21:47:34 +00001379 (void) WriteBlobString(image,buffer);
1380 (void) WriteBlobString(image,"<<\n");
cristyb51dff52011-05-19 16:55:47 +00001381 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001382 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001383 (void) WriteBlobString(image,buffer);
1384 (void) WriteBlobString(image,">>\n");
1385 (void) WriteBlobString(image,"stream\n");
1386 offset=TellBlob(image);
1387 (void) WriteBlobString(image,"q\n");
1388 if (labels != (char **) NULL)
1389 for (i=0; labels[i] != (char *) NULL; i++)
1390 {
1391 (void) WriteBlobString(image,"BT\n");
cristyb51dff52011-05-19 16:55:47 +00001392 (void) FormatLocaleString(buffer,MaxTextExtent,"/F%.20g %g Tf\n",
cristye8c25f92010-06-03 00:53:06 +00001393 (double) image->scene,pointsize);
cristy3ed852e2009-09-05 21:47:34 +00001394 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001395 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g Td\n",
cristye8c25f92010-06-03 00:53:06 +00001396 (double) geometry.x,(double) (geometry.y+geometry.height+i*pointsize+
1397 12));
cristy3ed852e2009-09-05 21:47:34 +00001398 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001399 (void) FormatLocaleString(buffer,MaxTextExtent,"(%s) Tj\n",labels[i]);
cristy3ed852e2009-09-05 21:47:34 +00001400 (void) WriteBlobString(image,buffer);
1401 (void) WriteBlobString(image,"ET\n");
1402 labels[i]=DestroyString(labels[i]);
1403 }
cristyb51dff52011-05-19 16:55:47 +00001404 (void) FormatLocaleString(buffer,MaxTextExtent,"%g 0 0 %g %.20g %.20g cm\n",
cristye8c25f92010-06-03 00:53:06 +00001405 scale.x,scale.y,(double) geometry.x,(double) geometry.y);
cristy3ed852e2009-09-05 21:47:34 +00001406 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001407 (void) FormatLocaleString(buffer,MaxTextExtent,"/Im%.20g Do\n",
cristye8c25f92010-06-03 00:53:06 +00001408 (double) image->scene);
cristy3ed852e2009-09-05 21:47:34 +00001409 (void) WriteBlobString(image,buffer);
1410 (void) WriteBlobString(image,"Q\n");
1411 offset=TellBlob(image)-offset;
1412 (void) WriteBlobString(image,"endstream\n");
1413 (void) WriteBlobString(image,"endobj\n");
1414 /*
1415 Write Length object.
1416 */
1417 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001418 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001419 object);
cristy3ed852e2009-09-05 21:47:34 +00001420 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001421 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
cristy3ed852e2009-09-05 21:47:34 +00001422 (void) WriteBlobString(image,buffer);
1423 (void) WriteBlobString(image,"endobj\n");
1424 /*
1425 Write Procset object.
1426 */
1427 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001428 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",
cristye8c25f92010-06-03 00:53:06 +00001429 (double) object);
cristy3ed852e2009-09-05 21:47:34 +00001430 (void) WriteBlobString(image,buffer);
1431 if ((image->storage_class == DirectClass) || (image->colors > 256))
1432 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageC",MaxTextExtent);
1433 else
1434 if ((compression == FaxCompression) || (compression == Group4Compression))
1435 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageB",MaxTextExtent);
1436 else
1437 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageI",MaxTextExtent);
1438 (void) WriteBlobString(image,buffer);
1439 (void) WriteBlobString(image," ]\n");
1440 (void) WriteBlobString(image,"endobj\n");
1441 /*
1442 Write Font object.
1443 */
1444 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001445 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",
cristye8c25f92010-06-03 00:53:06 +00001446 (double) object);
cristy3ed852e2009-09-05 21:47:34 +00001447 (void) WriteBlobString(image,buffer);
1448 (void) WriteBlobString(image,"<<\n");
1449 if (labels != (char **) NULL)
1450 {
1451 (void) WriteBlobString(image,"/Type /Font\n");
1452 (void) WriteBlobString(image,"/Subtype /Type1\n");
cristyb51dff52011-05-19 16:55:47 +00001453 (void) FormatLocaleString(buffer,MaxTextExtent,"/Name /F%.20g\n",
cristye8c25f92010-06-03 00:53:06 +00001454 (double) image->scene);
cristy3ed852e2009-09-05 21:47:34 +00001455 (void) WriteBlobString(image,buffer);
1456 (void) WriteBlobString(image,"/BaseFont /Helvetica\n");
1457 (void) WriteBlobString(image,"/Encoding /MacRomanEncoding\n");
1458 labels=(char **) RelinquishMagickMemory(labels);
1459 }
1460 (void) WriteBlobString(image,">>\n");
1461 (void) WriteBlobString(image,"endobj\n");
1462 /*
1463 Write XObject object.
1464 */
1465 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001466 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001467 object);
cristy3ed852e2009-09-05 21:47:34 +00001468 (void) WriteBlobString(image,buffer);
1469 (void) WriteBlobString(image,"<<\n");
1470 (void) WriteBlobString(image,"/Type /XObject\n");
1471 (void) WriteBlobString(image,"/Subtype /Image\n");
cristyb51dff52011-05-19 16:55:47 +00001472 (void) FormatLocaleString(buffer,MaxTextExtent,"/Name /Im%.20g\n",
cristye8c25f92010-06-03 00:53:06 +00001473 (double) image->scene);
cristy3ed852e2009-09-05 21:47:34 +00001474 (void) WriteBlobString(image,buffer);
1475 switch (compression)
1476 {
1477 case NoCompression:
1478 {
cristyb51dff52011-05-19 16:55:47 +00001479 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"ASCII85Decode");
cristy3ed852e2009-09-05 21:47:34 +00001480 break;
1481 }
1482 case JPEGCompression:
1483 {
cristyb51dff52011-05-19 16:55:47 +00001484 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"DCTDecode");
cristy3ed852e2009-09-05 21:47:34 +00001485 if (image->colorspace != CMYKColorspace)
1486 break;
1487 (void) WriteBlobString(image,buffer);
1488 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1489 MaxTextExtent);
1490 break;
1491 }
1492 case JPEG2000Compression:
1493 {
cristyb51dff52011-05-19 16:55:47 +00001494 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"JPXDecode");
cristy3ed852e2009-09-05 21:47:34 +00001495 if (image->colorspace != CMYKColorspace)
1496 break;
1497 (void) WriteBlobString(image,buffer);
1498 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1499 MaxTextExtent);
1500 break;
1501 }
1502 case LZWCompression:
1503 {
cristyb51dff52011-05-19 16:55:47 +00001504 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"LZWDecode");
cristyfa7becb2009-09-13 02:44:40 +00001505 break;
cristy3ed852e2009-09-05 21:47:34 +00001506 }
1507 case ZipCompression:
1508 {
cristyb51dff52011-05-19 16:55:47 +00001509 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"FlateDecode");
cristyfa7becb2009-09-13 02:44:40 +00001510 break;
cristy3ed852e2009-09-05 21:47:34 +00001511 }
1512 case FaxCompression:
1513 case Group4Compression:
1514 {
1515 (void) CopyMagickString(buffer,"/Filter [ /CCITTFaxDecode ]\n",
1516 MaxTextExtent);
1517 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001518 (void) FormatLocaleString(buffer,MaxTextExtent,"/DecodeParms [ << "
cristye8c25f92010-06-03 00:53:06 +00001519 "/K %s /BlackIs1 false /Columns %.20g /Rows %.20g >> ]\n",CCITTParam,
1520 (double) image->columns,(double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001521 break;
1522 }
1523 default:
1524 {
cristyb51dff52011-05-19 16:55:47 +00001525 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
cristy3ed852e2009-09-05 21:47:34 +00001526 "RunLengthDecode");
1527 break;
1528 }
1529 }
1530 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001531 (void) FormatLocaleString(buffer,MaxTextExtent,"/Width %.20g\n",(double)
cristye8c25f92010-06-03 00:53:06 +00001532 image->columns);
cristy3ed852e2009-09-05 21:47:34 +00001533 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001534 (void) FormatLocaleString(buffer,MaxTextExtent,"/Height %.20g\n",(double)
cristye8c25f92010-06-03 00:53:06 +00001535 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001536 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001537 (void) FormatLocaleString(buffer,MaxTextExtent,"/ColorSpace %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001538 (double) object+2);
cristy3ed852e2009-09-05 21:47:34 +00001539 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001540 (void) FormatLocaleString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
cristy3ed852e2009-09-05 21:47:34 +00001541 (compression == FaxCompression) || (compression == Group4Compression) ?
1542 1 : 8);
1543 (void) WriteBlobString(image,buffer);
1544 if (image->matte != MagickFalse)
1545 {
cristyb51dff52011-05-19 16:55:47 +00001546 (void) FormatLocaleString(buffer,MaxTextExtent,"/SMask %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001547 (double) object+7);
cristy3ed852e2009-09-05 21:47:34 +00001548 (void) WriteBlobString(image,buffer);
1549 }
cristyb51dff52011-05-19 16:55:47 +00001550 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001551 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001552 (void) WriteBlobString(image,buffer);
1553 (void) WriteBlobString(image,">>\n");
1554 (void) WriteBlobString(image,"stream\n");
1555 offset=TellBlob(image);
1556 number_pixels=(MagickSizeType) image->columns*image->rows;
1557 if ((4*number_pixels) != (MagickSizeType) ((size_t) (4*number_pixels)))
1558 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1559 if ((compression == FaxCompression) || (compression == Group4Compression) ||
1560 ((image_info->type != TrueColorType) &&
cristy1e178e72011-08-28 19:44:34 +00001561 (IsImageGray(image,exception) != MagickFalse)))
cristy3ed852e2009-09-05 21:47:34 +00001562 {
1563 switch (compression)
1564 {
1565 case FaxCompression:
1566 case Group4Compression:
1567 {
1568 if (LocaleCompare(CCITTParam,"0") == 0)
1569 {
cristy018f07f2011-09-04 21:15:19 +00001570 (void) HuffmanEncodeImage(image_info,image,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001571 break;
1572 }
cristy018f07f2011-09-04 21:15:19 +00001573 (void) Huffman2DEncodeImage(image_info,image,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001574 break;
1575 }
1576 case JPEGCompression:
1577 {
cristy1e178e72011-08-28 19:44:34 +00001578 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
cristy3ed852e2009-09-05 21:47:34 +00001579 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00001580 {
1581 (void) CloseBlob(image);
1582 return(MagickFalse);
1583 }
cristy3ed852e2009-09-05 21:47:34 +00001584 break;
1585 }
1586 case JPEG2000Compression:
1587 {
cristy1e178e72011-08-28 19:44:34 +00001588 status=InjectImageBlob(image_info,image,image,"jp2",exception);
cristy3ed852e2009-09-05 21:47:34 +00001589 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00001590 {
1591 (void) CloseBlob(image);
1592 return(MagickFalse);
1593 }
cristy3ed852e2009-09-05 21:47:34 +00001594 break;
1595 }
1596 case RLECompression:
1597 default:
1598 {
1599 /*
1600 Allocate pixel array.
1601 */
1602 length=(size_t) number_pixels;
1603 pixels=(unsigned char *) AcquireQuantumMemory(length,
1604 sizeof(*pixels));
1605 if (pixels == (unsigned char *) NULL)
1606 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1607 /*
1608 Dump Runlength encoded pixels.
1609 */
1610 q=pixels;
cristybb503372010-05-27 20:51:26 +00001611 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001612 {
cristy1e178e72011-08-28 19:44:34 +00001613 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001614 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001615 break;
cristybb503372010-05-27 20:51:26 +00001616 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001617 {
cristy4c08aed2011-07-01 19:47:50 +00001618 *q++=ScaleQuantumToChar(GetPixelIntensity(image,p));
cristyed231572011-07-14 02:18:59 +00001619 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001620 }
1621 if (image->previous == (Image *) NULL)
1622 {
cristyf5c61ba2010-12-17 00:58:04 +00001623 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1624 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001625 if (status == MagickFalse)
1626 break;
1627 }
1628 }
1629#if defined(MAGICKCORE_ZLIB_DELEGATE)
1630 if (compression == ZipCompression)
cristy018f07f2011-09-04 21:15:19 +00001631 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001632 else
1633#endif
1634 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00001635 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001636 else
cristy018f07f2011-09-04 21:15:19 +00001637 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001638 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1639 if (status == MagickFalse)
1640 {
1641 (void) CloseBlob(image);
1642 return(MagickFalse);
1643 }
1644 break;
1645 }
1646 case NoCompression:
1647 {
1648 /*
1649 Dump uncompressed PseudoColor packets.
1650 */
1651 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00001652 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001653 {
cristy1e178e72011-08-28 19:44:34 +00001654 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001655 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001656 break;
cristybb503372010-05-27 20:51:26 +00001657 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001658 {
cristy4c08aed2011-07-01 19:47:50 +00001659 Ascii85Encode(image,ScaleQuantumToChar(
1660 GetPixelIntensity(image,p)));
cristyed231572011-07-14 02:18:59 +00001661 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001662 }
1663 if (image->previous == (Image *) NULL)
1664 {
cristyf5c61ba2010-12-17 00:58:04 +00001665 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1666 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001667 if (status == MagickFalse)
1668 break;
1669 }
1670 }
1671 Ascii85Flush(image);
1672 break;
1673 }
1674 }
1675 }
1676 else
1677 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1678 (compression == JPEGCompression) ||
1679 (compression == JPEG2000Compression))
1680 switch (compression)
1681 {
1682 case JPEGCompression:
1683 {
cristy1e178e72011-08-28 19:44:34 +00001684 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
cristy3ed852e2009-09-05 21:47:34 +00001685 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00001686 {
1687 (void) CloseBlob(image);
1688 return(MagickFalse);
1689 }
cristy3ed852e2009-09-05 21:47:34 +00001690 break;
1691 }
1692 case JPEG2000Compression:
1693 {
cristy1e178e72011-08-28 19:44:34 +00001694 status=InjectImageBlob(image_info,image,image,"jp2",exception);
cristy3ed852e2009-09-05 21:47:34 +00001695 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00001696 {
1697 (void) CloseBlob(image);
1698 return(MagickFalse);
1699 }
cristy3ed852e2009-09-05 21:47:34 +00001700 break;
1701 }
1702 case RLECompression:
1703 default:
1704 {
1705 /*
1706 Allocate pixel array.
1707 */
1708 length=(size_t) number_pixels;
1709 pixels=(unsigned char *) AcquireQuantumMemory(length,
1710 4*sizeof(*pixels));
1711 length*=image->colorspace == CMYKColorspace ? 4UL : 3UL;
1712 if (pixels == (unsigned char *) NULL)
1713 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1714 /*
1715 Dump runoffset encoded pixels.
1716 */
1717 q=pixels;
cristybb503372010-05-27 20:51:26 +00001718 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001719 {
cristy1e178e72011-08-28 19:44:34 +00001720 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001721 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001722 break;
cristybb503372010-05-27 20:51:26 +00001723 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001724 {
cristy4c08aed2011-07-01 19:47:50 +00001725 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1726 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1727 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
cristy3ed852e2009-09-05 21:47:34 +00001728 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00001729 *q++=ScaleQuantumToChar(GetPixelBlack(image,p));
cristyed231572011-07-14 02:18:59 +00001730 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001731 }
1732 if (image->previous == (Image *) NULL)
1733 {
cristyf5c61ba2010-12-17 00:58:04 +00001734 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1735 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001736 if (status == MagickFalse)
1737 break;
1738 }
1739 }
1740#if defined(MAGICKCORE_ZLIB_DELEGATE)
1741 if (compression == ZipCompression)
cristy018f07f2011-09-04 21:15:19 +00001742 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001743 else
1744#endif
1745 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00001746 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001747 else
cristy018f07f2011-09-04 21:15:19 +00001748 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001749 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1750 if (status == MagickFalse)
1751 {
1752 (void) CloseBlob(image);
1753 return(MagickFalse);
1754 }
1755 break;
1756 }
1757 case NoCompression:
1758 {
1759 /*
1760 Dump uncompressed DirectColor packets.
1761 */
1762 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00001763 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001764 {
cristy1e178e72011-08-28 19:44:34 +00001765 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001766 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001767 break;
cristybb503372010-05-27 20:51:26 +00001768 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001769 {
cristy4c08aed2011-07-01 19:47:50 +00001770 Ascii85Encode(image,ScaleQuantumToChar(GetPixelRed(image,p)));
1771 Ascii85Encode(image,ScaleQuantumToChar(GetPixelGreen(image,p)));
1772 Ascii85Encode(image,ScaleQuantumToChar(GetPixelBlue(image,p)));
cristy3ed852e2009-09-05 21:47:34 +00001773 if (image->colorspace == CMYKColorspace)
cristyaff6d802011-04-26 01:46:31 +00001774 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +00001775 GetPixelBlack(image,p)));
cristyed231572011-07-14 02:18:59 +00001776 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001777 }
1778 if (image->previous == (Image *) NULL)
1779 {
cristyf5c61ba2010-12-17 00:58:04 +00001780 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1781 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001782 if (status == MagickFalse)
1783 break;
1784 }
1785 }
1786 Ascii85Flush(image);
1787 break;
1788 }
1789 }
1790 else
1791 {
1792 /*
1793 Dump number of colors and colormap.
1794 */
1795 switch (compression)
1796 {
1797 case RLECompression:
1798 default:
1799 {
1800 /*
1801 Allocate pixel array.
1802 */
1803 length=(size_t) number_pixels;
1804 pixels=(unsigned char *) AcquireQuantumMemory(length,
1805 sizeof(*pixels));
1806 if (pixels == (unsigned char *) NULL)
1807 ThrowWriterException(ResourceLimitError,
1808 "MemoryAllocationFailed");
1809 /*
1810 Dump Runlength encoded pixels.
1811 */
1812 q=pixels;
cristybb503372010-05-27 20:51:26 +00001813 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001814 {
cristy1e178e72011-08-28 19:44:34 +00001815 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001816 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001817 break;
cristybb503372010-05-27 20:51:26 +00001818 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00001819 {
1820 *q++=(unsigned char) GetPixelIndex(image,p);
cristyed231572011-07-14 02:18:59 +00001821 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001822 }
cristy3ed852e2009-09-05 21:47:34 +00001823 if (image->previous == (Image *) NULL)
1824 {
cristyf5c61ba2010-12-17 00:58:04 +00001825 status=SetImageProgress(image,SaveImageTag,
1826 (MagickOffsetType) y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001827 if (status == MagickFalse)
1828 break;
1829 }
1830 }
1831#if defined(MAGICKCORE_ZLIB_DELEGATE)
1832 if (compression == ZipCompression)
cristy018f07f2011-09-04 21:15:19 +00001833 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001834 else
1835#endif
1836 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00001837 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001838 else
cristy018f07f2011-09-04 21:15:19 +00001839 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001840 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1841 if (status == MagickFalse)
1842 {
1843 (void) CloseBlob(image);
1844 return(MagickFalse);
1845 }
1846 break;
1847 }
1848 case NoCompression:
1849 {
1850 /*
1851 Dump uncompressed PseudoColor packets.
1852 */
1853 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00001854 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001855 {
cristy1e178e72011-08-28 19:44:34 +00001856 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001857 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001858 break;
cristybb503372010-05-27 20:51:26 +00001859 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00001860 {
1861 Ascii85Encode(image,(unsigned char) GetPixelIndex(image,p));
cristyed231572011-07-14 02:18:59 +00001862 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001863 }
cristy3ed852e2009-09-05 21:47:34 +00001864 if (image->previous == (Image *) NULL)
1865 {
cristyf5c61ba2010-12-17 00:58:04 +00001866 status=SetImageProgress(image,SaveImageTag,
1867 (MagickOffsetType) y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001868 if (status == MagickFalse)
1869 break;
1870 }
1871 }
1872 Ascii85Flush(image);
1873 break;
1874 }
1875 }
1876 }
1877 offset=TellBlob(image)-offset;
1878 (void) WriteBlobString(image,"\nendstream\n");
1879 (void) WriteBlobString(image,"endobj\n");
1880 /*
1881 Write Length object.
1882 */
1883 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001884 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001885 object);
cristy3ed852e2009-09-05 21:47:34 +00001886 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001887 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",
cristye8c25f92010-06-03 00:53:06 +00001888 (double) offset);
cristy3ed852e2009-09-05 21:47:34 +00001889 (void) WriteBlobString(image,buffer);
1890 (void) WriteBlobString(image,"endobj\n");
1891 /*
1892 Write Colorspace object.
1893 */
1894 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001895 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001896 object);
cristy3ed852e2009-09-05 21:47:34 +00001897 (void) WriteBlobString(image,buffer);
1898 if (image->colorspace == CMYKColorspace)
1899 (void) CopyMagickString(buffer,"/DeviceCMYK\n",MaxTextExtent);
1900 else
1901 if ((compression == FaxCompression) ||
1902 (compression == Group4Compression) ||
1903 ((image_info->type != TrueColorType) &&
cristy1e178e72011-08-28 19:44:34 +00001904 (IsImageGray(image,exception) != MagickFalse)))
cristy3ed852e2009-09-05 21:47:34 +00001905 (void) CopyMagickString(buffer,"/DeviceGray\n",MaxTextExtent);
1906 else
1907 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1908 (compression == JPEGCompression) ||
1909 (compression == JPEG2000Compression))
1910 (void) CopyMagickString(buffer,"/DeviceRGB\n",MaxTextExtent);
1911 else
cristyb51dff52011-05-19 16:55:47 +00001912 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00001913 "[ /Indexed /DeviceRGB %.20g %.20g 0 R ]\n",(double) image->colors-
1914 1,(double) object+3);
cristy3ed852e2009-09-05 21:47:34 +00001915 (void) WriteBlobString(image,buffer);
1916 (void) WriteBlobString(image,"endobj\n");
1917 /*
1918 Write Thumb object.
1919 */
1920 SetGeometry(image,&geometry);
1921 (void) ParseMetaGeometry("106x106+0+0>",&geometry.x,&geometry.y,
1922 &geometry.width,&geometry.height);
cristy1e178e72011-08-28 19:44:34 +00001923 tile_image=ThumbnailImage(image,geometry.width,geometry.height,exception);
cristy3ed852e2009-09-05 21:47:34 +00001924 if (tile_image == (Image *) NULL)
cristy1e178e72011-08-28 19:44:34 +00001925 return(MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +00001926 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001927 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001928 object);
cristy3ed852e2009-09-05 21:47:34 +00001929 (void) WriteBlobString(image,buffer);
1930 (void) WriteBlobString(image,"<<\n");
1931 switch (compression)
1932 {
1933 case NoCompression:
1934 {
cristyb51dff52011-05-19 16:55:47 +00001935 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"ASCII85Decode");
cristy3ed852e2009-09-05 21:47:34 +00001936 break;
1937 }
1938 case JPEGCompression:
1939 {
cristyb51dff52011-05-19 16:55:47 +00001940 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"DCTDecode");
cristy3ed852e2009-09-05 21:47:34 +00001941 if (image->colorspace != CMYKColorspace)
1942 break;
1943 (void) WriteBlobString(image,buffer);
1944 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1945 MaxTextExtent);
1946 break;
1947 }
1948 case JPEG2000Compression:
1949 {
cristyb51dff52011-05-19 16:55:47 +00001950 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"JPXDecode");
cristy3ed852e2009-09-05 21:47:34 +00001951 if (image->colorspace != CMYKColorspace)
1952 break;
1953 (void) WriteBlobString(image,buffer);
1954 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1955 MaxTextExtent);
1956 break;
1957 }
1958 case LZWCompression:
1959 {
cristyb51dff52011-05-19 16:55:47 +00001960 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"LZWDecode");
cristy3ed852e2009-09-05 21:47:34 +00001961 break;
1962 }
1963 case ZipCompression:
1964 {
cristyb51dff52011-05-19 16:55:47 +00001965 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"FlateDecode");
cristy3ed852e2009-09-05 21:47:34 +00001966 break;
1967 }
1968 case FaxCompression:
1969 case Group4Compression:
1970 {
1971 (void) CopyMagickString(buffer,"/Filter [ /CCITTFaxDecode ]\n",
1972 MaxTextExtent);
1973 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001974 (void) FormatLocaleString(buffer,MaxTextExtent,"/DecodeParms [ << "
cristye8c25f92010-06-03 00:53:06 +00001975 "/K %s /BlackIs1 false /Columns %.20g /Rows %.20g >> ]\n",CCITTParam,
1976 (double) tile_image->columns,(double) tile_image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001977 break;
1978 }
1979 default:
1980 {
cristyb51dff52011-05-19 16:55:47 +00001981 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
cristy3ed852e2009-09-05 21:47:34 +00001982 "RunLengthDecode");
1983 break;
1984 }
1985 }
1986 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001987 (void) FormatLocaleString(buffer,MaxTextExtent,"/Width %.20g\n",(double)
cristye8c25f92010-06-03 00:53:06 +00001988 tile_image->columns);
cristy3ed852e2009-09-05 21:47:34 +00001989 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001990 (void) FormatLocaleString(buffer,MaxTextExtent,"/Height %.20g\n",(double)
cristye8c25f92010-06-03 00:53:06 +00001991 tile_image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001992 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001993 (void) FormatLocaleString(buffer,MaxTextExtent,"/ColorSpace %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001994 (double) object-1);
cristy3ed852e2009-09-05 21:47:34 +00001995 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001996 (void) FormatLocaleString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
cristy3ed852e2009-09-05 21:47:34 +00001997 (compression == FaxCompression) || (compression == Group4Compression) ?
1998 1 : 8);
1999 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002000 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00002001 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00002002 (void) WriteBlobString(image,buffer);
2003 (void) WriteBlobString(image,">>\n");
2004 (void) WriteBlobString(image,"stream\n");
2005 offset=TellBlob(image);
2006 number_pixels=(MagickSizeType) tile_image->columns*tile_image->rows;
2007 if ((compression == FaxCompression) ||
2008 (compression == Group4Compression) ||
2009 ((image_info->type != TrueColorType) &&
cristy1e178e72011-08-28 19:44:34 +00002010 (IsImageGray(tile_image,exception) != MagickFalse)))
cristy3ed852e2009-09-05 21:47:34 +00002011 {
2012 switch (compression)
2013 {
2014 case FaxCompression:
2015 case Group4Compression:
2016 {
2017 if (LocaleCompare(CCITTParam,"0") == 0)
2018 {
cristy018f07f2011-09-04 21:15:19 +00002019 (void) HuffmanEncodeImage(image_info,image,tile_image,
2020 exception);
cristy3ed852e2009-09-05 21:47:34 +00002021 break;
2022 }
cristy018f07f2011-09-04 21:15:19 +00002023 (void) Huffman2DEncodeImage(image_info,image,tile_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00002024 break;
2025 }
2026 case JPEGCompression:
2027 {
2028 status=InjectImageBlob(image_info,image,tile_image,"jpeg",
cristy1e178e72011-08-28 19:44:34 +00002029 exception);
cristy3ed852e2009-09-05 21:47:34 +00002030 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00002031 {
2032 (void) CloseBlob(image);
2033 return(MagickFalse);
2034 }
cristy3ed852e2009-09-05 21:47:34 +00002035 break;
2036 }
2037 case JPEG2000Compression:
2038 {
cristy1e178e72011-08-28 19:44:34 +00002039 status=InjectImageBlob(image_info,image,tile_image,"jp2",exception);
cristy3ed852e2009-09-05 21:47:34 +00002040 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00002041 {
2042 (void) CloseBlob(image);
2043 return(MagickFalse);
2044 }
cristy3ed852e2009-09-05 21:47:34 +00002045 break;
2046 }
2047 case RLECompression:
2048 default:
2049 {
2050 /*
2051 Allocate pixel array.
2052 */
2053 length=(size_t) number_pixels;
2054 pixels=(unsigned char *) AcquireQuantumMemory(length,
2055 sizeof(*pixels));
2056 if (pixels == (unsigned char *) NULL)
2057 {
2058 tile_image=DestroyImage(tile_image);
2059 ThrowWriterException(ResourceLimitError,
2060 "MemoryAllocationFailed");
2061 }
2062 /*
2063 Dump Runlength encoded pixels.
2064 */
2065 q=pixels;
cristybb503372010-05-27 20:51:26 +00002066 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002067 {
2068 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
cristy1e178e72011-08-28 19:44:34 +00002069 exception);
cristy4c08aed2011-07-01 19:47:50 +00002070 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002071 break;
cristybb503372010-05-27 20:51:26 +00002072 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002073 {
cristy4c08aed2011-07-01 19:47:50 +00002074 *q++=ScaleQuantumToChar(GetPixelIntensity(tile_image,p));
cristyed231572011-07-14 02:18:59 +00002075 p+=GetPixelChannels(tile_image);
cristy3ed852e2009-09-05 21:47:34 +00002076 }
2077 }
2078#if defined(MAGICKCORE_ZLIB_DELEGATE)
2079 if (compression == ZipCompression)
cristy018f07f2011-09-04 21:15:19 +00002080 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002081 else
2082#endif
2083 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00002084 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002085 else
cristy018f07f2011-09-04 21:15:19 +00002086 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002087 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2088 if (status == MagickFalse)
2089 {
2090 (void) CloseBlob(image);
2091 return(MagickFalse);
2092 }
2093 break;
2094 }
2095 case NoCompression:
2096 {
2097 /*
2098 Dump uncompressed PseudoColor packets.
2099 */
2100 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00002101 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002102 {
2103 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
cristy1e178e72011-08-28 19:44:34 +00002104 exception);
cristy4c08aed2011-07-01 19:47:50 +00002105 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002106 break;
cristybb503372010-05-27 20:51:26 +00002107 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002108 {
2109 Ascii85Encode(image,
cristy4c08aed2011-07-01 19:47:50 +00002110 ScaleQuantumToChar(GetPixelIntensity(tile_image,p)));
cristyed231572011-07-14 02:18:59 +00002111 p+=GetPixelChannels(tile_image);
cristy3ed852e2009-09-05 21:47:34 +00002112 }
2113 }
2114 Ascii85Flush(image);
2115 break;
2116 }
2117 }
2118 }
2119 else
2120 if ((tile_image->storage_class == DirectClass) ||
2121 (tile_image->colors > 256) || (compression == JPEGCompression) ||
2122 (compression == JPEG2000Compression))
2123 switch (compression)
2124 {
2125 case JPEGCompression:
2126 {
2127 status=InjectImageBlob(image_info,image,tile_image,"jpeg",
cristy1e178e72011-08-28 19:44:34 +00002128 exception);
cristy3ed852e2009-09-05 21:47:34 +00002129 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00002130 {
2131 (void) CloseBlob(image);
2132 return(MagickFalse);
2133 }
cristy3ed852e2009-09-05 21:47:34 +00002134 break;
2135 }
2136 case JPEG2000Compression:
2137 {
cristy1e178e72011-08-28 19:44:34 +00002138 status=InjectImageBlob(image_info,image,tile_image,"jp2",exception);
cristy3ed852e2009-09-05 21:47:34 +00002139 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00002140 {
2141 (void) CloseBlob(image);
2142 return(MagickFalse);
2143 }
cristy3ed852e2009-09-05 21:47:34 +00002144 break;
2145 }
2146 case RLECompression:
2147 default:
2148 {
2149 /*
2150 Allocate pixel array.
2151 */
2152 length=(size_t) number_pixels;
2153 pixels=(unsigned char *) AcquireQuantumMemory(length,4*
2154 sizeof(*pixels));
2155 length*=tile_image->colorspace == CMYKColorspace ? 4UL : 3UL;
2156 if (pixels == (unsigned char *) NULL)
2157 {
2158 tile_image=DestroyImage(tile_image);
2159 ThrowWriterException(ResourceLimitError,
2160 "MemoryAllocationFailed");
2161 }
2162 /*
2163 Dump runoffset encoded pixels.
2164 */
2165 q=pixels;
cristybb503372010-05-27 20:51:26 +00002166 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002167 {
2168 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
cristy1e178e72011-08-28 19:44:34 +00002169 exception);
cristy4c08aed2011-07-01 19:47:50 +00002170 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002171 break;
cristybb503372010-05-27 20:51:26 +00002172 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002173 {
cristy4c08aed2011-07-01 19:47:50 +00002174 *q++=ScaleQuantumToChar(GetPixelRed(tile_image,p));
2175 *q++=ScaleQuantumToChar(GetPixelGreen(tile_image,p));
2176 *q++=ScaleQuantumToChar(GetPixelBlue(tile_image,p));
cristy3ed852e2009-09-05 21:47:34 +00002177 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00002178 *q++=ScaleQuantumToChar(GetPixelBlack(tile_image,p));
cristyed231572011-07-14 02:18:59 +00002179 p+=GetPixelChannels(tile_image);
cristy3ed852e2009-09-05 21:47:34 +00002180 }
2181 }
2182#if defined(MAGICKCORE_ZLIB_DELEGATE)
2183 if (compression == ZipCompression)
cristy018f07f2011-09-04 21:15:19 +00002184 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002185 else
2186#endif
2187 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00002188 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002189 else
cristy018f07f2011-09-04 21:15:19 +00002190 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002191 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2192 if (status == MagickFalse)
2193 {
2194 (void) CloseBlob(image);
2195 return(MagickFalse);
2196 }
2197 break;
2198 }
2199 case NoCompression:
2200 {
2201 /*
2202 Dump uncompressed DirectColor packets.
2203 */
2204 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00002205 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002206 {
2207 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
cristy1e178e72011-08-28 19:44:34 +00002208 exception);
cristy4c08aed2011-07-01 19:47:50 +00002209 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002210 break;
cristybb503372010-05-27 20:51:26 +00002211 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002212 {
cristyf5c61ba2010-12-17 00:58:04 +00002213 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +00002214 GetPixelRed(tile_image,p)));
cristyf5c61ba2010-12-17 00:58:04 +00002215 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +00002216 GetPixelGreen(tile_image,p)));
cristyf5c61ba2010-12-17 00:58:04 +00002217 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +00002218 GetPixelBlue(tile_image,p)));
cristy3ed852e2009-09-05 21:47:34 +00002219 if (image->colorspace == CMYKColorspace)
cristyaff6d802011-04-26 01:46:31 +00002220 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +00002221 GetPixelBlack(tile_image,p)));
cristyed231572011-07-14 02:18:59 +00002222 p+=GetPixelChannels(tile_image);
cristy3ed852e2009-09-05 21:47:34 +00002223 }
2224 }
2225 Ascii85Flush(image);
2226 break;
2227 }
2228 }
2229 else
2230 {
2231 /*
2232 Dump number of colors and colormap.
2233 */
2234 switch (compression)
2235 {
2236 case RLECompression:
2237 default:
2238 {
2239 /*
2240 Allocate pixel array.
2241 */
2242 length=(size_t) number_pixels;
2243 pixels=(unsigned char *) AcquireQuantumMemory(length,
2244 sizeof(*pixels));
2245 if (pixels == (unsigned char *) NULL)
2246 {
2247 tile_image=DestroyImage(tile_image);
2248 ThrowWriterException(ResourceLimitError,
2249 "MemoryAllocationFailed");
2250 }
2251 /*
2252 Dump Runlength encoded pixels.
2253 */
2254 q=pixels;
cristybb503372010-05-27 20:51:26 +00002255 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002256 {
2257 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
cristy1e178e72011-08-28 19:44:34 +00002258 exception);
cristy4c08aed2011-07-01 19:47:50 +00002259 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002260 break;
cristybb503372010-05-27 20:51:26 +00002261 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00002262 {
2263 *q++=(unsigned char) GetPixelIndex(tile_image,p);
cristyed231572011-07-14 02:18:59 +00002264 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00002265 }
cristy3ed852e2009-09-05 21:47:34 +00002266 }
2267#if defined(MAGICKCORE_ZLIB_DELEGATE)
2268 if (compression == ZipCompression)
cristy018f07f2011-09-04 21:15:19 +00002269 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002270 else
2271#endif
2272 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00002273 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002274 else
cristy018f07f2011-09-04 21:15:19 +00002275 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002276 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2277 if (status == MagickFalse)
2278 {
2279 (void) CloseBlob(image);
2280 return(MagickFalse);
2281 }
2282 break;
2283 }
2284 case NoCompression:
2285 {
2286 /*
2287 Dump uncompressed PseudoColor packets.
2288 */
2289 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00002290 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002291 {
2292 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
cristy1e178e72011-08-28 19:44:34 +00002293 exception);
cristy4c08aed2011-07-01 19:47:50 +00002294 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002295 break;
cristybb503372010-05-27 20:51:26 +00002296 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00002297 {
cristyaff6d802011-04-26 01:46:31 +00002298 Ascii85Encode(image,(unsigned char)
cristy4c08aed2011-07-01 19:47:50 +00002299 GetPixelIndex(tile_image,p));
cristyed231572011-07-14 02:18:59 +00002300 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00002301 }
cristy3ed852e2009-09-05 21:47:34 +00002302 }
2303 Ascii85Flush(image);
2304 break;
2305 }
2306 }
2307 }
2308 tile_image=DestroyImage(tile_image);
2309 offset=TellBlob(image)-offset;
2310 (void) WriteBlobString(image,"\nendstream\n");
2311 (void) WriteBlobString(image,"endobj\n");
2312 /*
2313 Write Length object.
2314 */
2315 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00002316 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002317 object);
cristy3ed852e2009-09-05 21:47:34 +00002318 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002319 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
cristy3ed852e2009-09-05 21:47:34 +00002320 (void) WriteBlobString(image,buffer);
2321 (void) WriteBlobString(image,"endobj\n");
2322 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00002323 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002324 object);
cristy3ed852e2009-09-05 21:47:34 +00002325 (void) WriteBlobString(image,buffer);
2326 if ((image->storage_class != DirectClass) && (image->colors <= 256) &&
2327 (compression != FaxCompression) && (compression != Group4Compression))
2328 {
2329 /*
2330 Write Colormap object.
2331 */
2332 (void) WriteBlobString(image,"<<\n");
2333 if (compression == NoCompression)
2334 (void) WriteBlobString(image,"/Filter [ /ASCII85Decode ]\n");
cristyb51dff52011-05-19 16:55:47 +00002335 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00002336 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00002337 (void) WriteBlobString(image,buffer);
2338 (void) WriteBlobString(image,">>\n");
2339 (void) WriteBlobString(image,"stream\n");
2340 offset=TellBlob(image);
2341 if (compression == NoCompression)
2342 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00002343 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00002344 {
2345 if (compression == NoCompression)
2346 {
2347 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].red));
2348 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].green));
2349 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].blue));
2350 continue;
2351 }
2352 (void) WriteBlobByte(image,
2353 ScaleQuantumToChar(image->colormap[i].red));
2354 (void) WriteBlobByte(image,
2355 ScaleQuantumToChar(image->colormap[i].green));
2356 (void) WriteBlobByte(image,
2357 ScaleQuantumToChar(image->colormap[i].blue));
2358 }
2359 if (compression == NoCompression)
2360 Ascii85Flush(image);
2361 offset=TellBlob(image)-offset;
2362 (void) WriteBlobString(image,"\nendstream\n");
2363 }
2364 (void) WriteBlobString(image,"endobj\n");
2365 /*
2366 Write Length object.
2367 */
2368 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00002369 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002370 object);
cristy3ed852e2009-09-05 21:47:34 +00002371 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002372 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002373 offset);
cristy3ed852e2009-09-05 21:47:34 +00002374 (void) WriteBlobString(image,buffer);
2375 (void) WriteBlobString(image,"endobj\n");
2376 /*
2377 Write softmask object.
2378 */
2379 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00002380 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002381 object);
cristy3ed852e2009-09-05 21:47:34 +00002382 (void) WriteBlobString(image,buffer);
2383 (void) WriteBlobString(image,"<<\n");
2384 if (image->matte == MagickFalse)
2385 (void) WriteBlobString(image,">>\n");
2386 else
2387 {
2388 (void) WriteBlobString(image,"/Type /XObject\n");
2389 (void) WriteBlobString(image,"/Subtype /Image\n");
cristyb51dff52011-05-19 16:55:47 +00002390 (void) FormatLocaleString(buffer,MaxTextExtent,"/Name /Ma%.20g\n",
cristye8c25f92010-06-03 00:53:06 +00002391 (double) image->scene);
cristy3ed852e2009-09-05 21:47:34 +00002392 (void) WriteBlobString(image,buffer);
2393 switch (compression)
2394 {
2395 case NoCompression:
2396 {
cristyb51dff52011-05-19 16:55:47 +00002397 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
cristy3ed852e2009-09-05 21:47:34 +00002398 "ASCII85Decode");
2399 break;
2400 }
2401 case LZWCompression:
2402 {
cristyb51dff52011-05-19 16:55:47 +00002403 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"LZWDecode");
cristy3ed852e2009-09-05 21:47:34 +00002404 break;
2405 }
2406 case ZipCompression:
2407 {
cristyb51dff52011-05-19 16:55:47 +00002408 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
cristy3ed852e2009-09-05 21:47:34 +00002409 "FlateDecode");
2410 break;
2411 }
2412 default:
2413 {
cristyb51dff52011-05-19 16:55:47 +00002414 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
cristy3ed852e2009-09-05 21:47:34 +00002415 "RunLengthDecode");
2416 break;
2417 }
2418 }
2419 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002420 (void) FormatLocaleString(buffer,MaxTextExtent,"/Width %.20g\n",
cristye8c25f92010-06-03 00:53:06 +00002421 (double) image->columns);
cristy3ed852e2009-09-05 21:47:34 +00002422 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002423 (void) FormatLocaleString(buffer,MaxTextExtent,"/Height %.20g\n",
cristye8c25f92010-06-03 00:53:06 +00002424 (double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +00002425 (void) WriteBlobString(image,buffer);
2426 (void) WriteBlobString(image,"/ColorSpace /DeviceGray\n");
cristyb51dff52011-05-19 16:55:47 +00002427 (void) FormatLocaleString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
cristy3ed852e2009-09-05 21:47:34 +00002428 (compression == FaxCompression) || (compression == Group4Compression)
2429 ? 1 : 8);
2430 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002431 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00002432 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00002433 (void) WriteBlobString(image,buffer);
2434 (void) WriteBlobString(image,">>\n");
2435 (void) WriteBlobString(image,"stream\n");
2436 offset=TellBlob(image);
2437 number_pixels=(MagickSizeType) image->columns*image->rows;
2438 switch (compression)
2439 {
2440 case RLECompression:
2441 default:
2442 {
2443 /*
2444 Allocate pixel array.
2445 */
2446 length=(size_t) number_pixels;
2447 pixels=(unsigned char *) AcquireQuantumMemory(length,
2448 sizeof(*pixels));
2449 if (pixels == (unsigned char *) NULL)
2450 {
2451 image=DestroyImage(image);
2452 ThrowWriterException(ResourceLimitError,
2453 "MemoryAllocationFailed");
2454 }
2455 /*
2456 Dump Runlength encoded pixels.
2457 */
2458 q=pixels;
cristybb503372010-05-27 20:51:26 +00002459 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002460 {
cristy1e178e72011-08-28 19:44:34 +00002461 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00002462 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002463 break;
cristybb503372010-05-27 20:51:26 +00002464 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002465 {
cristy4c08aed2011-07-01 19:47:50 +00002466 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
cristyed231572011-07-14 02:18:59 +00002467 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002468 }
2469 }
2470#if defined(MAGICKCORE_ZLIB_DELEGATE)
2471 if (compression == ZipCompression)
cristy018f07f2011-09-04 21:15:19 +00002472 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002473 else
2474#endif
2475 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00002476 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002477 else
cristy018f07f2011-09-04 21:15:19 +00002478 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002479 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2480 if (status == MagickFalse)
2481 {
2482 (void) CloseBlob(image);
2483 return(MagickFalse);
2484 }
2485 break;
2486 }
2487 case NoCompression:
2488 {
2489 /*
2490 Dump uncompressed PseudoColor packets.
2491 */
2492 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00002493 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002494 {
cristy1e178e72011-08-28 19:44:34 +00002495 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00002496 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002497 break;
cristybb503372010-05-27 20:51:26 +00002498 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002499 {
cristy4c08aed2011-07-01 19:47:50 +00002500 Ascii85Encode(image,ScaleQuantumToChar(GetPixelAlpha(image,p)));
cristyed231572011-07-14 02:18:59 +00002501 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002502 }
2503 }
2504 Ascii85Flush(image);
2505 break;
2506 }
2507 }
2508 offset=TellBlob(image)-offset;
2509 (void) WriteBlobString(image,"\nendstream\n");
2510 }
2511 (void) WriteBlobString(image,"endobj\n");
2512 /*
2513 Write Length object.
2514 */
2515 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00002516 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002517 object);
cristy3ed852e2009-09-05 21:47:34 +00002518 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002519 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
cristy3ed852e2009-09-05 21:47:34 +00002520 (void) WriteBlobString(image,buffer);
2521 (void) WriteBlobString(image,"endobj\n");
2522 if (GetNextImageInList(image) == (Image *) NULL)
2523 break;
2524 image=SyncNextImageInList(image);
2525 status=SetImageProgress(image,SaveImagesTag,scene++,
2526 GetImageListLength(image));
2527 if (status == MagickFalse)
2528 break;
2529 } while (image_info->adjoin != MagickFalse);
2530 /*
2531 Write Metadata object.
2532 */
2533 xref[object++]=TellBlob(image);
2534 info_id=object;
cristyb51dff52011-05-19 16:55:47 +00002535 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002536 object);
cristy3ed852e2009-09-05 21:47:34 +00002537 (void) WriteBlobString(image,buffer);
2538 (void) WriteBlobString(image,"<<\n");
cristy57015272010-12-30 01:16:38 +00002539 GetPathComponent(image->filename,BasePath,basename);
cristyb51dff52011-05-19 16:55:47 +00002540 (void) FormatLocaleString(buffer,MaxTextExtent,"/Title (%s)\n",
cristy57015272010-12-30 01:16:38 +00002541 EscapeParenthesis(basename));
cristy3ed852e2009-09-05 21:47:34 +00002542 (void) WriteBlobString(image,buffer);
2543 seconds=time((time_t *) NULL);
2544#if defined(MAGICKCORE_HAVE_LOCALTIME_R)
2545 (void) localtime_r(&seconds,&local_time);
2546#else
2547 (void) memcpy(&local_time,localtime(&seconds),sizeof(local_time));
2548#endif
cristyb51dff52011-05-19 16:55:47 +00002549 (void) FormatLocaleString(date,MaxTextExtent,"D:%04d%02d%02d%02d%02d%02d",
cristy3ed852e2009-09-05 21:47:34 +00002550 local_time.tm_year+1900,local_time.tm_mon+1,local_time.tm_mday,
2551 local_time.tm_hour,local_time.tm_min,local_time.tm_sec);
cristyb51dff52011-05-19 16:55:47 +00002552 (void) FormatLocaleString(buffer,MaxTextExtent,"/CreationDate (%s)\n",date);
cristy3ed852e2009-09-05 21:47:34 +00002553 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002554 (void) FormatLocaleString(buffer,MaxTextExtent,"/ModDate (%s)\n",date);
cristy3ed852e2009-09-05 21:47:34 +00002555 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002556 (void) FormatLocaleString(buffer,MaxTextExtent,"/Producer (%s)\n",
cristybb503372010-05-27 20:51:26 +00002557 EscapeParenthesis(GetMagickVersion((size_t *) NULL)));
cristy3ed852e2009-09-05 21:47:34 +00002558 (void) WriteBlobString(image,buffer);
2559 (void) WriteBlobString(image,">>\n");
2560 (void) WriteBlobString(image,"endobj\n");
2561 /*
2562 Write Xref object.
2563 */
2564 offset=TellBlob(image)-xref[0]+10;
2565 (void) WriteBlobString(image,"xref\n");
cristyb51dff52011-05-19 16:55:47 +00002566 (void) FormatLocaleString(buffer,MaxTextExtent,"0 %.20g\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002567 object+1);
cristy3ed852e2009-09-05 21:47:34 +00002568 (void) WriteBlobString(image,buffer);
2569 (void) WriteBlobString(image,"0000000000 65535 f \n");
cristybb503372010-05-27 20:51:26 +00002570 for (i=0; i < (ssize_t) object; i++)
cristy3ed852e2009-09-05 21:47:34 +00002571 {
cristyb51dff52011-05-19 16:55:47 +00002572 (void) FormatLocaleString(buffer,MaxTextExtent,"%010lu 00000 n \n",
cristyf2faecf2010-05-28 19:19:36 +00002573 (unsigned long) xref[i]);
cristy3ed852e2009-09-05 21:47:34 +00002574 (void) WriteBlobString(image,buffer);
2575 }
2576 (void) WriteBlobString(image,"trailer\n");
2577 (void) WriteBlobString(image,"<<\n");
cristyb51dff52011-05-19 16:55:47 +00002578 (void) FormatLocaleString(buffer,MaxTextExtent,"/Size %.20g\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002579 object+1);
cristy3ed852e2009-09-05 21:47:34 +00002580 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002581 (void) FormatLocaleString(buffer,MaxTextExtent,"/Info %.20g 0 R\n",(double)
cristye8c25f92010-06-03 00:53:06 +00002582 info_id);
cristy3ed852e2009-09-05 21:47:34 +00002583 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002584 (void) FormatLocaleString(buffer,MaxTextExtent,"/Root %.20g 0 R\n",(double)
cristye8c25f92010-06-03 00:53:06 +00002585 root_id);
cristy3ed852e2009-09-05 21:47:34 +00002586 (void) WriteBlobString(image,buffer);
2587 (void) WriteBlobString(image,">>\n");
2588 (void) WriteBlobString(image,"startxref\n");
cristyb51dff52011-05-19 16:55:47 +00002589 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
cristy3ed852e2009-09-05 21:47:34 +00002590 (void) WriteBlobString(image,buffer);
2591 (void) WriteBlobString(image,"%%EOF\n");
2592 xref=(MagickOffsetType *) RelinquishMagickMemory(xref);
2593 (void) CloseBlob(image);
2594 return(MagickTrue);
2595}