blob: f90d6da7806b613ec586f2b62fb1b28e1f23de84 [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% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38
39/*
40 Include declarations.
41*/
cristy4c08aed2011-07-01 19:47:50 +000042#include "MagickCore/studio.h"
43#include "MagickCore/attribute.h"
44#include "MagickCore/blob.h"
45#include "MagickCore/blob-private.h"
46#include "MagickCore/cache.h"
47#include "MagickCore/color.h"
48#include "MagickCore/color-private.h"
49#include "MagickCore/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;
cristy2a11bef2011-10-28 18:33:11 +0000400 if ((image->resolution.x == 0.0) || (image->resolution.y == 0.0))
cristy3ed852e2009-09-05 21:47:34 +0000401 {
cristy3ed852e2009-09-05 21:47:34 +0000402 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
cristy2a11bef2011-10-28 18:33:11 +0000403 image->resolution.x=geometry_info.rho;
404 image->resolution.y=geometry_info.sigma;
cristy3ed852e2009-09-05 21:47:34 +0000405 if ((flags & SigmaValue) == 0)
cristy2a11bef2011-10-28 18:33:11 +0000406 image->resolution.y=image->resolution.x;
cristy3ed852e2009-09-05 21:47:34 +0000407 }
cristydeb50be2011-11-24 00:19:33 +0000408 if (image_info->density != (char *) NULL)
409 {
410 flags=ParseGeometry(image_info->density,&geometry_info);
411 image->resolution.x=geometry_info.rho;
412 image->resolution.y=geometry_info.sigma;
413 if ((flags & SigmaValue) == 0)
414 image->resolution.y=image->resolution.x;
415 }
416 (void) ParseAbsoluteGeometry(PSPageGeometry,&page);
417 if (image_info->page != (char *) NULL)
418 (void) ParseAbsoluteGeometry(image_info->page,&page);
419 page.width=(size_t) ceil((double) (page.width*image->resolution.x/delta.x)-
420 0.5);
421 page.height=(size_t) ceil((double) (page.height*image->resolution.y/delta.y)-
422 0.5);
cristy3ed852e2009-09-05 21:47:34 +0000423 /*
424 Determine page geometry from the PDF media box.
425 */
426 cmyk=image_info->colorspace == CMYKColorspace ? MagickTrue : MagickFalse;
427 cropbox=MagickFalse;
428 option=GetImageOption(image_info,"pdf:use-cropbox");
429 if (option != (const char *) NULL)
430 cropbox=IsMagickTrue(option);
431 trimbox=MagickFalse;
432 option=GetImageOption(image_info,"pdf:use-trimbox");
433 if (option != (const char *) NULL)
434 trimbox=IsMagickTrue(option);
435 count=0;
436 spotcolor=0;
437 (void) ResetMagickMemory(&bounding_box,0,sizeof(bounding_box));
438 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
439 (void) ResetMagickMemory(&hires_bounds,0,sizeof(hires_bounds));
cristy3ed852e2009-09-05 21:47:34 +0000440 (void) ResetMagickMemory(command,0,sizeof(command));
cristy3ed852e2009-09-05 21:47:34 +0000441 angle=0.0;
442 p=command;
443 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
444 {
445 /*
446 Note PDF elements.
447 */
448 if (c == '\n')
449 c=' ';
450 *p++=(char) c;
cristyfd6e87b2011-02-03 18:48:17 +0000451 if ((c != (int) '/') && (c != (int) '%') &&
cristy3ed852e2009-09-05 21:47:34 +0000452 ((size_t) (p-command) < (MaxTextExtent-1)))
453 continue;
454 *(--p)='\0';
455 p=command;
456 if (LocaleNCompare(PDFRotate,command,strlen(PDFRotate)) == 0)
457 count=(ssize_t) sscanf(command,"Rotate %lf",&angle);
458 /*
459 Is this a CMYK document?
460 */
461 if (LocaleNCompare(DeviceCMYK,command,strlen(DeviceCMYK)) == 0)
462 cmyk=MagickTrue;
463 if (LocaleNCompare(SpotColor,command,strlen(SpotColor)) == 0)
464 {
465 char
466 name[MaxTextExtent],
467 property[MaxTextExtent],
468 *value;
469
cristybb503372010-05-27 20:51:26 +0000470 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000471 i;
472
473 /*
474 Note spot names.
475 */
cristyb51dff52011-05-19 16:55:47 +0000476 (void) FormatLocaleString(property,MaxTextExtent,"pdf:SpotColor-%.20g",
cristye8c25f92010-06-03 00:53:06 +0000477 (double) spotcolor++);
cristy3ed852e2009-09-05 21:47:34 +0000478 i=0;
479 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
480 {
481 if ((isspace(c) != 0) || (c == '/') || ((i+1) == MaxTextExtent))
482 break;
483 name[i++]=(char) c;
484 }
485 name[i]='\0';
486 value=AcquireString(name);
487 (void) SubstituteString(&value,"#20"," ");
cristyd15e6592011-10-15 00:13:06 +0000488 (void) SetImageProperty(image,property,value,exception);
cristy3ed852e2009-09-05 21:47:34 +0000489 value=DestroyString(value);
490 continue;
491 }
492 if (LocaleNCompare(PDFVersion,command,strlen(PDFVersion)) == 0)
cristyd15e6592011-10-15 00:13:06 +0000493 (void) SetImageProperty(image,"pdf:Version",command,exception);
cristydeb50be2011-11-24 00:19:33 +0000494 if (image_info->page != (char *) NULL)
495 continue;
cristy3ed852e2009-09-05 21:47:34 +0000496 count=0;
497 if (cropbox != MagickFalse)
498 {
499 if (LocaleNCompare(CropBox,command,strlen(CropBox)) == 0)
500 {
501 /*
502 Note region defined by crop box.
503 */
504 count=(ssize_t) sscanf(command,"CropBox [%lf %lf %lf %lf",
505 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
506 if (count != 4)
507 count=(ssize_t) sscanf(command,"CropBox[%lf %lf %lf %lf",
508 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
509 }
510 }
511 else
512 if (trimbox != MagickFalse)
513 {
514 if (LocaleNCompare(TrimBox,command,strlen(TrimBox)) == 0)
515 {
516 /*
517 Note region defined by trim box.
518 */
519 count=(ssize_t) sscanf(command,"TrimBox [%lf %lf %lf %lf",
520 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
521 if (count != 4)
522 count=(ssize_t) sscanf(command,"TrimBox[%lf %lf %lf %lf",
523 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
524 }
525 }
526 else
527 if (LocaleNCompare(MediaBox,command,strlen(MediaBox)) == 0)
528 {
529 /*
530 Note region defined by media box.
531 */
532 count=(ssize_t) sscanf(command,"MediaBox [%lf %lf %lf %lf",
533 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
534 if (count != 4)
535 count=(ssize_t) sscanf(command,"MediaBox[%lf %lf %lf %lf",
536 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
537 }
538 if (count != 4)
539 continue;
cristyad29a8c2012-01-25 23:06:43 +0000540 if ((fabs(bounds.x2-bounds.x1) <= fabs(hires_bounds.x2-hires_bounds.x1)) ||
cristyc2038532011-12-29 16:43:05 +0000541 (fabs(bounds.y2-bounds.y1) <= fabs(hires_bounds.y2-hires_bounds.y1)))
542 continue;
cristy48853f12011-11-11 15:47:57 +0000543 hires_bounds=bounds;
cristy3ed852e2009-09-05 21:47:34 +0000544 }
cristy7b0fcf12011-11-24 00:24:07 +0000545 if ((fabs(hires_bounds.x2-hires_bounds.x1) >= MagickEpsilon) &&
cristy614bc082011-11-24 00:49:08 +0000546 (fabs(hires_bounds.y2-hires_bounds.y1) >= MagickEpsilon))
cristy48853f12011-11-11 15:47:57 +0000547 {
548 /*
549 Set PDF render geometry.
550 */
551 (void) FormatLocaleString(geometry,MaxTextExtent,"%gx%g%+.15g%+.15g",
552 hires_bounds.x2-bounds.x1,hires_bounds.y2-hires_bounds.y1,
553 hires_bounds.x1,hires_bounds.y1);
554 (void) SetImageProperty(image,"pdf:HiResBoundingBox",geometry,exception);
cristydeb50be2011-11-24 00:19:33 +0000555 page.width=(size_t) ceil((double) ((hires_bounds.x2-hires_bounds.x1)*
556 image->resolution.x/delta.x)-0.5);
557 page.height=(size_t) ceil((double) ((hires_bounds.y2-hires_bounds.y1)*
558 image->resolution.y/delta.y)-0.5);
cristy48853f12011-11-11 15:47:57 +0000559 }
cristy3ed852e2009-09-05 21:47:34 +0000560 (void) CloseBlob(image);
561 if ((fabs(angle) == 90.0) || (fabs(angle) == 270.0))
562 {
cristybb503372010-05-27 20:51:26 +0000563 size_t
cristy3ed852e2009-09-05 21:47:34 +0000564 swap;
565
566 swap=page.width;
567 page.width=page.height;
568 page.height=swap;
569 }
cristy510d06a2011-07-06 23:43:54 +0000570 if (IsRGBColorspace(image_info->colorspace) != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000571 cmyk=MagickFalse;
572 /*
573 Create Ghostscript control file.
574 */
575 file=AcquireUniqueFileResource(postscript_filename);
576 if (file == -1)
577 {
578 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
579 image_info->filename);
580 image=DestroyImage(image);
581 return((Image *) NULL);
582 }
583 count=write(file," ",1);
584 file=close(file)-1;
585 /*
586 Render Postscript with the Ghostscript delegate.
587 */
588 if ((image_info->ping != MagickFalse) ||
589 (image_info->monochrome != MagickFalse))
590 delegate_info=GetDelegateInfo("ps:mono",(char *) NULL,exception);
591 else
592 if (cmyk != MagickFalse)
593 delegate_info=GetDelegateInfo("ps:cmyk",(char *) NULL,exception);
594 else
cristya97426c2011-02-04 01:41:27 +0000595 delegate_info=GetDelegateInfo("ps:alpha",(char *) NULL,exception);
cristy3ed852e2009-09-05 21:47:34 +0000596 if (delegate_info == (const DelegateInfo *) NULL)
597 {
598 (void) RelinquishUniqueFileResource(postscript_filename);
599 image=DestroyImage(image);
600 return((Image *) NULL);
601 }
602 *options='\0';
cristy2a11bef2011-10-28 18:33:11 +0000603 (void) FormatLocaleString(density,MaxTextExtent,"%gx%g",image->resolution.x,
604 image->resolution.y);
cristydeb50be2011-11-24 00:19:33 +0000605 (void) FormatLocaleString(options,MaxTextExtent,"-g%.20gx%.20g ",(double)
606 page.width,(double) page.height);
cristy3ed852e2009-09-05 21:47:34 +0000607 if (cmyk != MagickFalse)
608 (void) ConcatenateMagickString(options,"-dUseCIEColor ",MaxTextExtent);
609 if (cropbox != MagickFalse)
610 (void) ConcatenateMagickString(options,"-dUseCropBox ",MaxTextExtent);
611 if (trimbox != MagickFalse)
612 (void) ConcatenateMagickString(options,"-dUseTrimBox ",MaxTextExtent);
613 read_info=CloneImageInfo(image_info);
614 *read_info->magick='\0';
615 if (read_info->number_scenes != 0)
616 {
617 char
618 pages[MaxTextExtent];
619
cristyb51dff52011-05-19 16:55:47 +0000620 (void) FormatLocaleString(pages,MaxTextExtent,"-dFirstPage=%.20g "
cristye8c25f92010-06-03 00:53:06 +0000621 "-dLastPage=%.20g",(double) read_info->scene+1,(double)
cristyf2faecf2010-05-28 19:19:36 +0000622 (read_info->scene+read_info->number_scenes));
cristy3ed852e2009-09-05 21:47:34 +0000623 (void) ConcatenateMagickString(options,pages,MaxTextExtent);
624 read_info->number_scenes=0;
625 if (read_info->scenes != (char *) NULL)
626 *read_info->scenes='\0';
627 }
anthony1afdc7a2011-10-05 11:54:28 +0000628 option=GetImageOption(read_info,"authenticate");
629 if (option != (const char *) NULL)
cristyb51dff52011-05-19 16:55:47 +0000630 (void) FormatLocaleString(options+strlen(options),MaxTextExtent,
anthony1afdc7a2011-10-05 11:54:28 +0000631 " -sPCLPassword=%s",option);
cristy3ed852e2009-09-05 21:47:34 +0000632 (void) CopyMagickString(filename,read_info->filename,MaxTextExtent);
cristya97426c2011-02-04 01:41:27 +0000633 (void) AcquireUniqueFilename(filename);
634 (void) ConcatenateMagickString(filename,"-%08d",MaxTextExtent);
cristyb51dff52011-05-19 16:55:47 +0000635 (void) FormatLocaleString(command,MaxTextExtent,
cristy3ed852e2009-09-05 21:47:34 +0000636 GetDelegateCommands(delegate_info),
637 read_info->antialias != MagickFalse ? 4 : 1,
cristya97426c2011-02-04 01:41:27 +0000638 read_info->antialias != MagickFalse ? 4 : 1,density,options,filename,
639 postscript_filename,input_filename);
cristydefb3f02009-09-10 02:18:35 +0000640 status=InvokePDFDelegate(read_info->verbose,command,exception);
cristy3ed852e2009-09-05 21:47:34 +0000641 (void) RelinquishUniqueFileResource(postscript_filename);
cristy3ed852e2009-09-05 21:47:34 +0000642 (void) RelinquishUniqueFileResource(input_filename);
cristya97426c2011-02-04 01:41:27 +0000643 pdf_image=(Image *) NULL;
644 if (status == MagickFalse)
645 for (i=1; ; i++)
646 {
cristya97426c2011-02-04 01:41:27 +0000647 (void) InterpretImageFilename(image_info,image,filename,(int) i,
cristy6fccee12011-10-20 18:43:18 +0000648 read_info->filename,exception);
cristya97426c2011-02-04 01:41:27 +0000649 if (IsPDFRendered(read_info->filename) == MagickFalse)
650 break;
651 (void) RelinquishUniqueFileResource(read_info->filename);
652 }
653 else
654 for (i=1; ; i++)
655 {
cristya97426c2011-02-04 01:41:27 +0000656 (void) InterpretImageFilename(image_info,image,filename,(int) i,
cristy6fccee12011-10-20 18:43:18 +0000657 read_info->filename,exception);
cristya97426c2011-02-04 01:41:27 +0000658 if (IsPDFRendered(read_info->filename) == MagickFalse)
659 break;
660 next=ReadImage(read_info,exception);
661 (void) RelinquishUniqueFileResource(read_info->filename);
662 if (next == (Image *) NULL)
663 break;
664 AppendImageToList(&pdf_image,next);
665 }
cristy3ed852e2009-09-05 21:47:34 +0000666 read_info=DestroyImageInfo(read_info);
667 if (pdf_image == (Image *) NULL)
668 {
669 ThrowFileException(exception,DelegateError,"PostscriptDelegateFailed",
670 image_info->filename);
cristyb6cabe82011-03-18 13:25:59 +0000671 image=DestroyImage(image);
cristy3ed852e2009-09-05 21:47:34 +0000672 return((Image *) NULL);
673 }
674 if (LocaleCompare(pdf_image->magick,"BMP") == 0)
675 {
676 Image
677 *cmyk_image;
678
679 cmyk_image=ConsolidateCMYKImages(pdf_image,exception);
680 if (cmyk_image != (Image *) NULL)
681 {
682 pdf_image=DestroyImageList(pdf_image);
683 pdf_image=cmyk_image;
684 }
685 }
686 if (image_info->number_scenes != 0)
687 {
688 Image
689 *clone_image;
690
cristybb503372010-05-27 20:51:26 +0000691 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000692 i;
693
694 /*
695 Add place holder images to meet the subimage specification requirement.
696 */
cristybb503372010-05-27 20:51:26 +0000697 for (i=0; i < (ssize_t) image_info->scene; i++)
cristy3ed852e2009-09-05 21:47:34 +0000698 {
699 clone_image=CloneImage(pdf_image,1,1,MagickTrue,exception);
700 if (clone_image != (Image *) NULL)
701 PrependImageToList(&pdf_image,clone_image);
702 }
703 }
704 do
705 {
706 (void) CopyMagickString(pdf_image->filename,filename,MaxTextExtent);
707 pdf_image->page=page;
708 (void) CloneImageProfiles(pdf_image,image);
709 (void) CloneImageProperties(pdf_image,image);
710 next=SyncNextImageInList(pdf_image);
711 if (next != (Image *) NULL)
712 pdf_image=next;
713 } while (next != (Image *) NULL);
714 image=DestroyImage(image);
715 scene=0;
716 for (next=GetFirstImageInList(pdf_image); next != (Image *) NULL; )
717 {
718 next->scene=scene++;
719 next=GetNextImageInList(next);
720 }
721 return(GetFirstImageInList(pdf_image));
722}
723
724/*
725%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
726% %
727% %
728% %
729% R e g i s t e r P D F I m a g e %
730% %
731% %
732% %
733%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
734%
735% RegisterPDFImage() adds properties for the PDF image format to
736% the list of supported formats. The properties include the image format
737% tag, a method to read and/or write the format, whether the format
738% supports the saving of more than one frame to the same file or blob,
739% whether the format supports native in-memory I/O, and a brief
740% description of the format.
741%
742% The format of the RegisterPDFImage method is:
743%
cristybb503372010-05-27 20:51:26 +0000744% size_t RegisterPDFImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000745%
746*/
cristybb503372010-05-27 20:51:26 +0000747ModuleExport size_t RegisterPDFImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000748{
749 MagickInfo
750 *entry;
751
752 entry=SetMagickInfo("AI");
753 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
754 entry->encoder=(EncodeImageHandler *) WritePDFImage;
755 entry->adjoin=MagickFalse;
756 entry->blob_support=MagickFalse;
757 entry->seekable_stream=MagickTrue;
758 entry->thread_support=EncoderThreadSupport;
759 entry->description=ConstantString("Adobe Illustrator CS2");
760 entry->module=ConstantString("PDF");
761 (void) RegisterMagickInfo(entry);
762 entry=SetMagickInfo("EPDF");
763 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
764 entry->encoder=(EncodeImageHandler *) WritePDFImage;
765 entry->adjoin=MagickFalse;
766 entry->blob_support=MagickFalse;
767 entry->seekable_stream=MagickTrue;
768 entry->thread_support=EncoderThreadSupport;
769 entry->description=ConstantString("Encapsulated Portable Document Format");
770 entry->module=ConstantString("PDF");
771 (void) RegisterMagickInfo(entry);
772 entry=SetMagickInfo("PDF");
773 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
774 entry->encoder=(EncodeImageHandler *) WritePDFImage;
775 entry->magick=(IsImageFormatHandler *) IsPDF;
776 entry->blob_support=MagickFalse;
777 entry->seekable_stream=MagickTrue;
778 entry->thread_support=EncoderThreadSupport;
779 entry->description=ConstantString("Portable Document Format");
780 entry->module=ConstantString("PDF");
781 (void) RegisterMagickInfo(entry);
782 entry=SetMagickInfo("PDFA");
783 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
784 entry->encoder=(EncodeImageHandler *) WritePDFImage;
785 entry->magick=(IsImageFormatHandler *) IsPDF;
786 entry->blob_support=MagickFalse;
787 entry->seekable_stream=MagickTrue;
788 entry->thread_support=EncoderThreadSupport;
789 entry->description=ConstantString("Portable Document Archive Format");
790 entry->module=ConstantString("PDF");
791 (void) RegisterMagickInfo(entry);
792 return(MagickImageCoderSignature);
793}
794
795/*
796%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
797% %
798% %
799% %
800% U n r e g i s t e r P D F I m a g e %
801% %
802% %
803% %
804%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
805%
806% UnregisterPDFImage() removes format registrations made by the
807% PDF module from the list of supported formats.
808%
809% The format of the UnregisterPDFImage method is:
810%
811% UnregisterPDFImage(void)
812%
813*/
814ModuleExport void UnregisterPDFImage(void)
815{
816 (void) UnregisterMagickInfo("AI");
817 (void) UnregisterMagickInfo("EPDF");
818 (void) UnregisterMagickInfo("PDF");
819 (void) UnregisterMagickInfo("PDFA");
820}
821
822/*
823%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
824% %
825% %
826% %
827% W r i t e P D F I m a g e %
828% %
829% %
830% %
831%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
832%
833% WritePDFImage() writes an image in the Portable Document image
834% format.
835%
836% The format of the WritePDFImage method is:
837%
cristy1e178e72011-08-28 19:44:34 +0000838% MagickBooleanType WritePDFImage(const ImageInfo *image_info,
839% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000840%
841% A description of each parameter follows.
842%
843% o image_info: the image info.
844%
845% o image: The image.
846%
cristy1e178e72011-08-28 19:44:34 +0000847% o exception: return any errors or warnings in this structure.
848%
cristy3ed852e2009-09-05 21:47:34 +0000849*/
850
851static inline size_t MagickMax(const size_t x,const size_t y)
852{
853 if (x > y)
854 return(x);
855 return(y);
856}
857
858static inline size_t MagickMin(const size_t x,const size_t y)
859{
860 if (x < y)
861 return(x);
862 return(y);
863}
864
865static char *EscapeParenthesis(const char *text)
866{
867 register char
868 *p;
869
cristybb503372010-05-27 20:51:26 +0000870 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000871 i;
872
cristybb503372010-05-27 20:51:26 +0000873 size_t
cristy3ed852e2009-09-05 21:47:34 +0000874 escapes;
875
cristyaff6d802011-04-26 01:46:31 +0000876 static char
877 buffer[MaxTextExtent];
878
cristy3ed852e2009-09-05 21:47:34 +0000879 escapes=0;
880 p=buffer;
cristybb503372010-05-27 20:51:26 +0000881 for (i=0; i < (ssize_t) MagickMin(strlen(text),(MaxTextExtent-escapes-1)); i++)
cristy3ed852e2009-09-05 21:47:34 +0000882 {
883 if ((text[i] == '(') || (text[i] == ')'))
884 {
885 *p++='\\';
886 escapes++;
887 }
888 *p++=text[i];
889 }
890 *p='\0';
891 return(buffer);
892}
893
cristy47b838c2009-09-19 16:09:30 +0000894static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
cristy018f07f2011-09-04 21:15:19 +0000895 Image *image,Image *inject_image,ExceptionInfo *exception)
cristy47b838c2009-09-19 16:09:30 +0000896{
cristy47b838c2009-09-19 16:09:30 +0000897 Image
cristy80975862009-09-25 14:34:31 +0000898 *group4_image;
cristy47b838c2009-09-19 16:09:30 +0000899
900 ImageInfo
901 *write_info;
902
cristy47b838c2009-09-19 16:09:30 +0000903 MagickBooleanType
904 status;
905
cristy80975862009-09-25 14:34:31 +0000906 size_t
907 length;
cristy47b838c2009-09-19 16:09:30 +0000908
909 unsigned char
cristy80975862009-09-25 14:34:31 +0000910 *group4;
cristy47b838c2009-09-19 16:09:30 +0000911
cristy42751fe2009-10-05 00:15:50 +0000912 status=MagickTrue;
cristy47b838c2009-09-19 16:09:30 +0000913 write_info=CloneImageInfo(image_info);
cristy80975862009-09-25 14:34:31 +0000914 (void) CopyMagickString(write_info->filename,"GROUP4:",MaxTextExtent);
915 (void) CopyMagickString(write_info->magick,"GROUP4",MaxTextExtent);
cristy018f07f2011-09-04 21:15:19 +0000916 group4_image=CloneImage(inject_image,0,0,MagickTrue,exception);
cristy80975862009-09-25 14:34:31 +0000917 if (group4_image == (Image *) NULL)
918 return(MagickFalse);
919 group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
cristy018f07f2011-09-04 21:15:19 +0000920 exception);
cristy80975862009-09-25 14:34:31 +0000921 group4_image=DestroyImage(group4_image);
922 if (group4 == (unsigned char *) NULL)
923 return(MagickFalse);
cristy47b838c2009-09-19 16:09:30 +0000924 write_info=DestroyImageInfo(write_info);
cristy80975862009-09-25 14:34:31 +0000925 if (WriteBlob(image,length,group4) != (ssize_t) length)
926 status=MagickFalse;
927 group4=(unsigned char *) RelinquishMagickMemory(group4);
928 return(status);
cristy47b838c2009-09-19 16:09:30 +0000929}
930
cristy1e178e72011-08-28 19:44:34 +0000931static MagickBooleanType WritePDFImage(const ImageInfo *image_info,Image *image,
932 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000933{
934#define CFormat "/Filter [ /%s ]\n"
935#define ObjectsPerImage 14
936
937 static const char
938 XMPProfile[]=
939 {
940 "<?xpacket begin=\"%s\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n"
941 "<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"
942 " <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n"
943 " <rdf:Description rdf:about=\"\"\n"
944 " xmlns:xap=\"http://ns.adobe.com/xap/1.0/\">\n"
945 " <xap:ModifyDate>%s</xap:ModifyDate>\n"
946 " <xap:CreateDate>%s</xap:CreateDate>\n"
947 " <xap:MetadataDate>%s</xap:MetadataDate>\n"
948 " <xap:CreatorTool>%s</xap:CreatorTool>\n"
949 " </rdf:Description>\n"
950 " <rdf:Description rdf:about=\"\"\n"
951 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"
952 " <dc:format>application/pdf</dc:format>\n"
953 " </rdf:Description>\n"
954 " <rdf:Description rdf:about=\"\"\n"
955 " xmlns:xapMM=\"http://ns.adobe.com/xap/1.0/mm/\">\n"
956 " <xapMM:DocumentID>uuid:6ec119d7-7982-4f56-808d-dfe64f5b35cf</xapMM:DocumentID>\n"
957 " <xapMM:InstanceID>uuid:a79b99b4-6235-447f-9f6c-ec18ef7555cb</xapMM:InstanceID>\n"
958 " </rdf:Description>\n"
959 " <rdf:Description rdf:about=\"\"\n"
960 " xmlns:pdf=\"http://ns.adobe.com/pdf/1.3/\">\n"
961 " <pdf:Producer>%s</pdf:Producer>\n"
962 " </rdf:Description>\n"
963 " <rdf:Description rdf:about=\"\"\n"
964 " xmlns:pdfaid=\"http://www.aiim.org/pdfa/ns/id/\">\n"
965 " <pdfaid:part>1</pdfaid:part>\n"
966 " <pdfaid:conformance>B</pdfaid:conformance>\n"
967 " </rdf:Description>\n"
968 " </rdf:RDF>\n"
969 "</x:xmpmeta>\n"
970 "<?xpacket end=\"w\"?>\n"
971 },
972 XMPProfileMagick[4]= { (char) 0xef, (char) 0xbb, (char) 0xbf, (char) 0x00 };
973
974 char
cristy57015272010-12-30 01:16:38 +0000975 basename[MaxTextExtent],
cristy3ed852e2009-09-05 21:47:34 +0000976 buffer[MaxTextExtent],
977 date[MaxTextExtent],
978 **labels,
979 page_geometry[MaxTextExtent];
980
981 CompressionType
982 compression;
983
984 const char
985 *value;
986
987 double
988 pointsize;
989
990 GeometryInfo
991 geometry_info;
992
cristy3ed852e2009-09-05 21:47:34 +0000993 Image
994 *next,
995 *tile_image;
996
997 MagickBooleanType
998 status;
999
1000 MagickOffsetType
1001 offset,
1002 scene,
1003 *xref;
1004
1005 MagickSizeType
1006 number_pixels;
1007
1008 MagickStatusType
1009 flags;
1010
1011 PointInfo
1012 delta,
1013 resolution,
1014 scale;
1015
1016 RectangleInfo
1017 geometry,
1018 media_info,
1019 page_info;
1020
cristy4c08aed2011-07-01 19:47:50 +00001021 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +00001022 *p;
1023
1024 register unsigned char
1025 *q;
1026
cristybb503372010-05-27 20:51:26 +00001027 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001028 i,
1029 x;
1030
1031 size_t
cristyaff6d802011-04-26 01:46:31 +00001032 info_id,
1033 length,
1034 object,
1035 pages_id,
1036 root_id,
1037 text_size,
1038 version;
1039
1040 ssize_t
1041 count,
1042 y;
cristy3ed852e2009-09-05 21:47:34 +00001043
1044 struct tm
1045 local_time;
1046
1047 time_t
1048 seconds;
1049
1050 unsigned char
1051 *pixels;
1052
cristy3ed852e2009-09-05 21:47:34 +00001053 /*
1054 Open output image file.
1055 */
1056 assert(image_info != (const ImageInfo *) NULL);
1057 assert(image_info->signature == MagickSignature);
1058 assert(image != (Image *) NULL);
1059 assert(image->signature == MagickSignature);
1060 if (image->debug != MagickFalse)
1061 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +00001062 assert(exception != (ExceptionInfo *) NULL);
1063 assert(exception->signature == MagickSignature);
cristy1e178e72011-08-28 19:44:34 +00001064 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +00001065 if (status == MagickFalse)
1066 return(status);
1067 /*
1068 Allocate X ref memory.
1069 */
1070 xref=(MagickOffsetType *) AcquireQuantumMemory(2048UL,sizeof(*xref));
1071 if (xref == (MagickOffsetType *) NULL)
1072 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1073 (void) ResetMagickMemory(xref,0,2048UL*sizeof(*xref));
1074 /*
1075 Write Info object.
1076 */
1077 object=0;
1078 version=3;
1079 if (image_info->compression == JPEG2000Compression)
cristybb503372010-05-27 20:51:26 +00001080 version=(size_t) MagickMax(version,5);
cristy3ed852e2009-09-05 21:47:34 +00001081 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
1082 if (next->matte != MagickFalse)
cristybb503372010-05-27 20:51:26 +00001083 version=(size_t) MagickMax(version,4);
cristy3ed852e2009-09-05 21:47:34 +00001084 if (LocaleCompare(image_info->magick,"PDFA") == 0)
cristyc04c60e2011-05-31 23:58:45 +00001085 version=(size_t) MagickMax(version,6);
cristyb51dff52011-05-19 16:55:47 +00001086 (void) FormatLocaleString(buffer,MaxTextExtent,"%%PDF-1.%.20g \n",
cristye8c25f92010-06-03 00:53:06 +00001087 (double) version);
cristy3ed852e2009-09-05 21:47:34 +00001088 (void) WriteBlobString(image,buffer);
1089 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1090 (void) WriteBlobString(image,"%âãÏÓ\n");
1091 /*
1092 Write Catalog object.
1093 */
1094 xref[object++]=TellBlob(image);
1095 root_id=object;
cristyb51dff52011-05-19 16:55:47 +00001096 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristye8c25f92010-06-03 00:53:06 +00001097 object);
cristy3ed852e2009-09-05 21:47:34 +00001098 (void) WriteBlobString(image,buffer);
1099 (void) WriteBlobString(image,"<<\n");
1100 if (LocaleCompare(image_info->magick,"PDFA") != 0)
cristyb51dff52011-05-19 16:55:47 +00001101 (void) FormatLocaleString(buffer,MaxTextExtent,"/Pages %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001102 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001103 else
1104 {
cristyb51dff52011-05-19 16:55:47 +00001105 (void) FormatLocaleString(buffer,MaxTextExtent,"/Metadata %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001106 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001107 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001108 (void) FormatLocaleString(buffer,MaxTextExtent,"/Pages %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001109 (double) object+2);
cristy3ed852e2009-09-05 21:47:34 +00001110 }
1111 (void) WriteBlobString(image,buffer);
1112 (void) WriteBlobString(image,"/Type /Catalog\n");
1113 (void) WriteBlobString(image,">>\n");
1114 (void) WriteBlobString(image,"endobj\n");
1115 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1116 {
1117 char
1118 create_date[MaxTextExtent],
1119 modify_date[MaxTextExtent],
1120 timestamp[MaxTextExtent],
1121 xmp_profile[MaxTextExtent];
1122
cristybb503372010-05-27 20:51:26 +00001123 size_t
cristy3ed852e2009-09-05 21:47:34 +00001124 version;
1125
1126 /*
1127 Write XMP object.
1128 */
1129 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001130 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",
cristye8c25f92010-06-03 00:53:06 +00001131 (double) object);
cristy3ed852e2009-09-05 21:47:34 +00001132 (void) WriteBlobString(image,buffer);
1133 (void) WriteBlobString(image,"<<\n");
1134 (void) WriteBlobString(image,"/Subtype /XML\n");
1135 *modify_date='\0';
cristyd15e6592011-10-15 00:13:06 +00001136 value=GetImageProperty(image,"date:modify",exception);
cristy3ed852e2009-09-05 21:47:34 +00001137 if (value != (const char *) NULL)
1138 (void) CopyMagickString(modify_date,value,MaxTextExtent);
1139 *create_date='\0';
cristyd15e6592011-10-15 00:13:06 +00001140 value=GetImageProperty(image,"date:create",exception);
cristy3ed852e2009-09-05 21:47:34 +00001141 if (value != (const char *) NULL)
1142 (void) CopyMagickString(create_date,value,MaxTextExtent);
1143 (void) FormatMagickTime(time((time_t *) NULL),MaxTextExtent,timestamp);
cristyb51dff52011-05-19 16:55:47 +00001144 i=FormatLocaleString(xmp_profile,MaxTextExtent,XMPProfile,
cristy3ed852e2009-09-05 21:47:34 +00001145 XMPProfileMagick,modify_date,create_date,timestamp,
1146 GetMagickVersion(&version),GetMagickVersion(&version));
cristyb51dff52011-05-19 16:55:47 +00001147 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g\n",
cristye8c25f92010-06-03 00:53:06 +00001148 (double) i);
cristy3ed852e2009-09-05 21:47:34 +00001149 (void) WriteBlobString(image,buffer);
1150 (void) WriteBlobString(image,"/Type /Metadata\n");
1151 (void) WriteBlobString(image,">>\nstream\n");
1152 (void) WriteBlobString(image,xmp_profile);
1153 (void) WriteBlobString(image,"endstream\n");
1154 (void) WriteBlobString(image,"endobj\n");
1155 }
1156 /*
1157 Write Pages object.
1158 */
1159 xref[object++]=TellBlob(image);
1160 pages_id=object;
cristyb51dff52011-05-19 16:55:47 +00001161 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001162 object);
cristy3ed852e2009-09-05 21:47:34 +00001163 (void) WriteBlobString(image,buffer);
1164 (void) WriteBlobString(image,"<<\n");
1165 (void) WriteBlobString(image,"/Type /Pages\n");
cristyb51dff52011-05-19 16:55:47 +00001166 (void) FormatLocaleString(buffer,MaxTextExtent,"/Kids [ %.20g 0 R ",
cristye8c25f92010-06-03 00:53:06 +00001167 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001168 (void) WriteBlobString(image,buffer);
cristybb503372010-05-27 20:51:26 +00001169 count=(ssize_t) (pages_id+ObjectsPerImage+1);
cristy3ed852e2009-09-05 21:47:34 +00001170 if (image_info->adjoin != MagickFalse)
1171 {
1172 Image
1173 *kid_image;
1174
1175 /*
1176 Predict page object id's.
1177 */
1178 kid_image=image;
1179 for ( ; GetNextImageInList(kid_image) != (Image *) NULL; count+=ObjectsPerImage)
1180 {
cristyb51dff52011-05-19 16:55:47 +00001181 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 R ",(double)
cristye8c25f92010-06-03 00:53:06 +00001182 count);
cristy3ed852e2009-09-05 21:47:34 +00001183 (void) WriteBlobString(image,buffer);
1184 kid_image=GetNextImageInList(kid_image);
1185 }
1186 xref=(MagickOffsetType *) ResizeQuantumMemory(xref,(size_t) count+2048UL,
1187 sizeof(*xref));
1188 if (xref == (MagickOffsetType *) NULL)
1189 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1190 }
1191 (void) WriteBlobString(image,"]\n");
cristyb51dff52011-05-19 16:55:47 +00001192 (void) FormatLocaleString(buffer,MaxTextExtent,"/Count %.20g\n",
cristye8c25f92010-06-03 00:53:06 +00001193 (double) ((count-pages_id)/ObjectsPerImage));
cristy3ed852e2009-09-05 21:47:34 +00001194 (void) WriteBlobString(image,buffer);
1195 (void) WriteBlobString(image,">>\n");
1196 (void) WriteBlobString(image,"endobj\n");
1197 scene=0;
1198 do
1199 {
1200 compression=image->compression;
1201 if (image_info->compression != UndefinedCompression)
1202 compression=image_info->compression;
1203 switch (compression)
1204 {
1205 case FaxCompression:
1206 case Group4Compression:
1207 {
cristy1e178e72011-08-28 19:44:34 +00001208 if ((IsImageMonochrome(image,exception) == MagickFalse) ||
cristy3ed852e2009-09-05 21:47:34 +00001209 (image->matte != MagickFalse))
1210 compression=RLECompression;
1211 break;
1212 }
1213#if !defined(MAGICKCORE_JPEG_DELEGATE)
1214 case JPEGCompression:
1215 {
1216 compression=RLECompression;
cristy1e178e72011-08-28 19:44:34 +00001217 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00001218 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
1219 image->filename);
1220 break;
1221 }
1222#endif
1223#if !defined(MAGICKCORE_JP2_DELEGATE)
1224 case JPEG2000Compression:
1225 {
1226 compression=RLECompression;
cristy1e178e72011-08-28 19:44:34 +00001227 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00001228 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JP2)",
1229 image->filename);
1230 break;
1231 }
1232#endif
1233#if !defined(MAGICKCORE_ZLIB_DELEGATE)
1234 case ZipCompression:
1235 {
1236 compression=RLECompression;
cristy1e178e72011-08-28 19:44:34 +00001237 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00001238 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZLIB)",
1239 image->filename);
1240 break;
1241 }
1242#endif
1243 case LZWCompression:
1244 {
1245 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1246 compression=RLECompression; /* LZW compression is forbidden */
1247 break;
1248 }
1249 case NoCompression:
1250 {
1251 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1252 compression=RLECompression; /* ASCII 85 compression is forbidden */
1253 break;
1254 }
1255 default:
1256 break;
1257 }
1258 if (compression == JPEG2000Compression)
1259 {
cristy510d06a2011-07-06 23:43:54 +00001260 if (IsRGBColorspace(image->colorspace) == MagickFalse)
cristye941a752011-10-15 01:52:48 +00001261 (void) TransformImageColorspace(image,RGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00001262 }
1263 /*
1264 Scale relative to dots-per-inch.
1265 */
1266 delta.x=DefaultResolution;
1267 delta.y=DefaultResolution;
cristy2a11bef2011-10-28 18:33:11 +00001268 resolution.x=image->resolution.x;
1269 resolution.y=image->resolution.y;
cristy3ed852e2009-09-05 21:47:34 +00001270 if ((resolution.x == 0.0) || (resolution.y == 0.0))
1271 {
1272 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
1273 resolution.x=geometry_info.rho;
1274 resolution.y=geometry_info.sigma;
1275 if ((flags & SigmaValue) == 0)
1276 resolution.y=resolution.x;
1277 }
1278 if (image_info->density != (char *) NULL)
1279 {
1280 flags=ParseGeometry(image_info->density,&geometry_info);
1281 resolution.x=geometry_info.rho;
1282 resolution.y=geometry_info.sigma;
1283 if ((flags & SigmaValue) == 0)
1284 resolution.y=resolution.x;
1285 }
1286 if (image->units == PixelsPerCentimeterResolution)
1287 {
cristya97426c2011-02-04 01:41:27 +00001288 resolution.x=(double) ((size_t) (100.0*2.54*resolution.x+0.5)/100.0);
1289 resolution.y=(double) ((size_t) (100.0*2.54*resolution.y+0.5)/100.0);
cristy3ed852e2009-09-05 21:47:34 +00001290 }
1291 SetGeometry(image,&geometry);
cristyb51dff52011-05-19 16:55:47 +00001292 (void) FormatLocaleString(page_geometry,MaxTextExtent,"%.20gx%.20g",
cristye8c25f92010-06-03 00:53:06 +00001293 (double) image->columns,(double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001294 if (image_info->page != (char *) NULL)
1295 (void) CopyMagickString(page_geometry,image_info->page,MaxTextExtent);
1296 else
1297 if ((image->page.width != 0) && (image->page.height != 0))
cristyb51dff52011-05-19 16:55:47 +00001298 (void) FormatLocaleString(page_geometry,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00001299 "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,
1300 (double) image->page.height,(double) image->page.x,(double)
1301 image->page.y);
cristy3ed852e2009-09-05 21:47:34 +00001302 else
1303 if ((image->gravity != UndefinedGravity) &&
1304 (LocaleCompare(image_info->magick,"PDF") == 0))
1305 (void) CopyMagickString(page_geometry,PSPageGeometry,MaxTextExtent);
1306 (void) ConcatenateMagickString(page_geometry,">",MaxTextExtent);
1307 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
1308 &geometry.width,&geometry.height);
1309 scale.x=(double) (geometry.width*delta.x)/resolution.x;
cristybb503372010-05-27 20:51:26 +00001310 geometry.width=(size_t) floor(scale.x+0.5);
cristy3ed852e2009-09-05 21:47:34 +00001311 scale.y=(double) (geometry.height*delta.y)/resolution.y;
cristybb503372010-05-27 20:51:26 +00001312 geometry.height=(size_t) floor(scale.y+0.5);
cristy3ed852e2009-09-05 21:47:34 +00001313 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
cristy1e178e72011-08-28 19:44:34 +00001314 (void) ParseGravityGeometry(image,page_geometry,&page_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00001315 if (image->gravity != UndefinedGravity)
1316 {
1317 geometry.x=(-page_info.x);
cristybb503372010-05-27 20:51:26 +00001318 geometry.y=(ssize_t) (media_info.height+page_info.y-image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001319 }
1320 pointsize=12.0;
1321 if (image_info->pointsize != 0.0)
1322 pointsize=image_info->pointsize;
1323 text_size=0;
cristyd15e6592011-10-15 00:13:06 +00001324 value=GetImageProperty(image,"label",exception);
cristy3ed852e2009-09-05 21:47:34 +00001325 if (value != (const char *) NULL)
cristybb503372010-05-27 20:51:26 +00001326 text_size=(size_t) (MultilineCensus(value)*pointsize+12);
cristyda16f162011-02-19 23:52:17 +00001327 (void) text_size;
cristy3ed852e2009-09-05 21:47:34 +00001328 /*
1329 Write Page object.
1330 */
1331 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001332 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001333 object);
cristy3ed852e2009-09-05 21:47:34 +00001334 (void) WriteBlobString(image,buffer);
1335 (void) WriteBlobString(image,"<<\n");
1336 (void) WriteBlobString(image,"/Type /Page\n");
cristyb51dff52011-05-19 16:55:47 +00001337 (void) FormatLocaleString(buffer,MaxTextExtent,"/Parent %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001338 (double) pages_id);
cristy3ed852e2009-09-05 21:47:34 +00001339 (void) WriteBlobString(image,buffer);
1340 (void) WriteBlobString(image,"/Resources <<\n");
1341 labels=(char **) NULL;
cristyd15e6592011-10-15 00:13:06 +00001342 value=GetImageProperty(image,"label",exception);
cristy3ed852e2009-09-05 21:47:34 +00001343 if (value != (const char *) NULL)
1344 labels=StringToList(value);
1345 if (labels != (char **) NULL)
1346 {
cristyb51dff52011-05-19 16:55:47 +00001347 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00001348 "/Font << /F%.20g %.20g 0 R >>\n",(double) image->scene,(double)
1349 object+4);
cristy3ed852e2009-09-05 21:47:34 +00001350 (void) WriteBlobString(image,buffer);
1351 }
cristyb51dff52011-05-19 16:55:47 +00001352 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00001353 "/XObject << /Im%.20g %.20g 0 R >>\n",(double) image->scene,(double)
1354 object+5);
cristy3ed852e2009-09-05 21:47:34 +00001355 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001356 (void) FormatLocaleString(buffer,MaxTextExtent,"/ProcSet %.20g 0 R >>\n",
cristye8c25f92010-06-03 00:53:06 +00001357 (double) object+3);
cristy3ed852e2009-09-05 21:47:34 +00001358 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001359 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00001360 "/MediaBox [0 0 %g %g]\n",72.0*media_info.width/resolution.x,
cristy8cd5b312010-01-07 01:10:24 +00001361 72.0*media_info.height/resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001362 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001363 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00001364 "/CropBox [0 0 %g %g]\n",72.0*media_info.width/resolution.x,
cristy8cd5b312010-01-07 01:10:24 +00001365 72.0*media_info.height/resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001366 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001367 (void) FormatLocaleString(buffer,MaxTextExtent,"/Contents %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001368 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001369 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001370 (void) FormatLocaleString(buffer,MaxTextExtent,"/Thumb %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001371 (double) object+8);
cristy3ed852e2009-09-05 21:47:34 +00001372 (void) WriteBlobString(image,buffer);
1373 (void) WriteBlobString(image,">>\n");
1374 (void) WriteBlobString(image,"endobj\n");
1375 /*
1376 Write Contents object.
1377 */
1378 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001379 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001380 object);
cristy3ed852e2009-09-05 21:47:34 +00001381 (void) WriteBlobString(image,buffer);
1382 (void) WriteBlobString(image,"<<\n");
cristyb51dff52011-05-19 16:55:47 +00001383 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001384 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001385 (void) WriteBlobString(image,buffer);
1386 (void) WriteBlobString(image,">>\n");
1387 (void) WriteBlobString(image,"stream\n");
1388 offset=TellBlob(image);
1389 (void) WriteBlobString(image,"q\n");
1390 if (labels != (char **) NULL)
1391 for (i=0; labels[i] != (char *) NULL; i++)
1392 {
1393 (void) WriteBlobString(image,"BT\n");
cristyb51dff52011-05-19 16:55:47 +00001394 (void) FormatLocaleString(buffer,MaxTextExtent,"/F%.20g %g Tf\n",
cristye8c25f92010-06-03 00:53:06 +00001395 (double) image->scene,pointsize);
cristy3ed852e2009-09-05 21:47:34 +00001396 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001397 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g Td\n",
cristye8c25f92010-06-03 00:53:06 +00001398 (double) geometry.x,(double) (geometry.y+geometry.height+i*pointsize+
1399 12));
cristy3ed852e2009-09-05 21:47:34 +00001400 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001401 (void) FormatLocaleString(buffer,MaxTextExtent,"(%s) Tj\n",labels[i]);
cristy3ed852e2009-09-05 21:47:34 +00001402 (void) WriteBlobString(image,buffer);
1403 (void) WriteBlobString(image,"ET\n");
1404 labels[i]=DestroyString(labels[i]);
1405 }
cristyb51dff52011-05-19 16:55:47 +00001406 (void) FormatLocaleString(buffer,MaxTextExtent,"%g 0 0 %g %.20g %.20g cm\n",
cristye8c25f92010-06-03 00:53:06 +00001407 scale.x,scale.y,(double) geometry.x,(double) geometry.y);
cristy3ed852e2009-09-05 21:47:34 +00001408 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001409 (void) FormatLocaleString(buffer,MaxTextExtent,"/Im%.20g Do\n",
cristye8c25f92010-06-03 00:53:06 +00001410 (double) image->scene);
cristy3ed852e2009-09-05 21:47:34 +00001411 (void) WriteBlobString(image,buffer);
1412 (void) WriteBlobString(image,"Q\n");
1413 offset=TellBlob(image)-offset;
1414 (void) WriteBlobString(image,"endstream\n");
1415 (void) WriteBlobString(image,"endobj\n");
1416 /*
1417 Write Length object.
1418 */
1419 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001420 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001421 object);
cristy3ed852e2009-09-05 21:47:34 +00001422 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001423 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
cristy3ed852e2009-09-05 21:47:34 +00001424 (void) WriteBlobString(image,buffer);
1425 (void) WriteBlobString(image,"endobj\n");
1426 /*
1427 Write Procset object.
1428 */
1429 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001430 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",
cristye8c25f92010-06-03 00:53:06 +00001431 (double) object);
cristy3ed852e2009-09-05 21:47:34 +00001432 (void) WriteBlobString(image,buffer);
1433 if ((image->storage_class == DirectClass) || (image->colors > 256))
1434 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageC",MaxTextExtent);
1435 else
1436 if ((compression == FaxCompression) || (compression == Group4Compression))
1437 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageB",MaxTextExtent);
1438 else
1439 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageI",MaxTextExtent);
1440 (void) WriteBlobString(image,buffer);
1441 (void) WriteBlobString(image," ]\n");
1442 (void) WriteBlobString(image,"endobj\n");
1443 /*
1444 Write Font object.
1445 */
1446 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001447 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",
cristye8c25f92010-06-03 00:53:06 +00001448 (double) object);
cristy3ed852e2009-09-05 21:47:34 +00001449 (void) WriteBlobString(image,buffer);
1450 (void) WriteBlobString(image,"<<\n");
1451 if (labels != (char **) NULL)
1452 {
1453 (void) WriteBlobString(image,"/Type /Font\n");
1454 (void) WriteBlobString(image,"/Subtype /Type1\n");
cristyb51dff52011-05-19 16:55:47 +00001455 (void) FormatLocaleString(buffer,MaxTextExtent,"/Name /F%.20g\n",
cristye8c25f92010-06-03 00:53:06 +00001456 (double) image->scene);
cristy3ed852e2009-09-05 21:47:34 +00001457 (void) WriteBlobString(image,buffer);
1458 (void) WriteBlobString(image,"/BaseFont /Helvetica\n");
1459 (void) WriteBlobString(image,"/Encoding /MacRomanEncoding\n");
1460 labels=(char **) RelinquishMagickMemory(labels);
1461 }
1462 (void) WriteBlobString(image,">>\n");
1463 (void) WriteBlobString(image,"endobj\n");
1464 /*
1465 Write XObject object.
1466 */
1467 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001468 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001469 object);
cristy3ed852e2009-09-05 21:47:34 +00001470 (void) WriteBlobString(image,buffer);
1471 (void) WriteBlobString(image,"<<\n");
1472 (void) WriteBlobString(image,"/Type /XObject\n");
1473 (void) WriteBlobString(image,"/Subtype /Image\n");
cristyb51dff52011-05-19 16:55:47 +00001474 (void) FormatLocaleString(buffer,MaxTextExtent,"/Name /Im%.20g\n",
cristye8c25f92010-06-03 00:53:06 +00001475 (double) image->scene);
cristy3ed852e2009-09-05 21:47:34 +00001476 (void) WriteBlobString(image,buffer);
1477 switch (compression)
1478 {
1479 case NoCompression:
1480 {
cristyb51dff52011-05-19 16:55:47 +00001481 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"ASCII85Decode");
cristy3ed852e2009-09-05 21:47:34 +00001482 break;
1483 }
1484 case JPEGCompression:
1485 {
cristyb51dff52011-05-19 16:55:47 +00001486 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"DCTDecode");
cristy3ed852e2009-09-05 21:47:34 +00001487 if (image->colorspace != CMYKColorspace)
1488 break;
1489 (void) WriteBlobString(image,buffer);
1490 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1491 MaxTextExtent);
1492 break;
1493 }
1494 case JPEG2000Compression:
1495 {
cristyb51dff52011-05-19 16:55:47 +00001496 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"JPXDecode");
cristy3ed852e2009-09-05 21:47:34 +00001497 if (image->colorspace != CMYKColorspace)
1498 break;
1499 (void) WriteBlobString(image,buffer);
1500 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1501 MaxTextExtent);
1502 break;
1503 }
1504 case LZWCompression:
1505 {
cristyb51dff52011-05-19 16:55:47 +00001506 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"LZWDecode");
cristyfa7becb2009-09-13 02:44:40 +00001507 break;
cristy3ed852e2009-09-05 21:47:34 +00001508 }
1509 case ZipCompression:
1510 {
cristyb51dff52011-05-19 16:55:47 +00001511 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"FlateDecode");
cristyfa7becb2009-09-13 02:44:40 +00001512 break;
cristy3ed852e2009-09-05 21:47:34 +00001513 }
1514 case FaxCompression:
1515 case Group4Compression:
1516 {
1517 (void) CopyMagickString(buffer,"/Filter [ /CCITTFaxDecode ]\n",
1518 MaxTextExtent);
1519 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001520 (void) FormatLocaleString(buffer,MaxTextExtent,"/DecodeParms [ << "
cristye8c25f92010-06-03 00:53:06 +00001521 "/K %s /BlackIs1 false /Columns %.20g /Rows %.20g >> ]\n",CCITTParam,
1522 (double) image->columns,(double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001523 break;
1524 }
1525 default:
1526 {
cristyb51dff52011-05-19 16:55:47 +00001527 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
cristy3ed852e2009-09-05 21:47:34 +00001528 "RunLengthDecode");
1529 break;
1530 }
1531 }
1532 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001533 (void) FormatLocaleString(buffer,MaxTextExtent,"/Width %.20g\n",(double)
cristye8c25f92010-06-03 00:53:06 +00001534 image->columns);
cristy3ed852e2009-09-05 21:47:34 +00001535 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001536 (void) FormatLocaleString(buffer,MaxTextExtent,"/Height %.20g\n",(double)
cristye8c25f92010-06-03 00:53:06 +00001537 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001538 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001539 (void) FormatLocaleString(buffer,MaxTextExtent,"/ColorSpace %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001540 (double) object+2);
cristy3ed852e2009-09-05 21:47:34 +00001541 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001542 (void) FormatLocaleString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
cristy3ed852e2009-09-05 21:47:34 +00001543 (compression == FaxCompression) || (compression == Group4Compression) ?
1544 1 : 8);
1545 (void) WriteBlobString(image,buffer);
1546 if (image->matte != MagickFalse)
1547 {
cristyb51dff52011-05-19 16:55:47 +00001548 (void) FormatLocaleString(buffer,MaxTextExtent,"/SMask %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001549 (double) object+7);
cristy3ed852e2009-09-05 21:47:34 +00001550 (void) WriteBlobString(image,buffer);
1551 }
cristyb51dff52011-05-19 16:55:47 +00001552 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001553 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001554 (void) WriteBlobString(image,buffer);
1555 (void) WriteBlobString(image,">>\n");
1556 (void) WriteBlobString(image,"stream\n");
1557 offset=TellBlob(image);
1558 number_pixels=(MagickSizeType) image->columns*image->rows;
1559 if ((4*number_pixels) != (MagickSizeType) ((size_t) (4*number_pixels)))
1560 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1561 if ((compression == FaxCompression) || (compression == Group4Compression) ||
1562 ((image_info->type != TrueColorType) &&
cristy1e178e72011-08-28 19:44:34 +00001563 (IsImageGray(image,exception) != MagickFalse)))
cristy3ed852e2009-09-05 21:47:34 +00001564 {
1565 switch (compression)
1566 {
1567 case FaxCompression:
1568 case Group4Compression:
1569 {
1570 if (LocaleCompare(CCITTParam,"0") == 0)
1571 {
cristy018f07f2011-09-04 21:15:19 +00001572 (void) HuffmanEncodeImage(image_info,image,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001573 break;
1574 }
cristy018f07f2011-09-04 21:15:19 +00001575 (void) Huffman2DEncodeImage(image_info,image,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001576 break;
1577 }
1578 case JPEGCompression:
1579 {
cristy1e178e72011-08-28 19:44:34 +00001580 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
cristy3ed852e2009-09-05 21:47:34 +00001581 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00001582 {
1583 (void) CloseBlob(image);
1584 return(MagickFalse);
1585 }
cristy3ed852e2009-09-05 21:47:34 +00001586 break;
1587 }
1588 case JPEG2000Compression:
1589 {
cristy1e178e72011-08-28 19:44:34 +00001590 status=InjectImageBlob(image_info,image,image,"jp2",exception);
cristy3ed852e2009-09-05 21:47:34 +00001591 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00001592 {
1593 (void) CloseBlob(image);
1594 return(MagickFalse);
1595 }
cristy3ed852e2009-09-05 21:47:34 +00001596 break;
1597 }
1598 case RLECompression:
1599 default:
1600 {
1601 /*
1602 Allocate pixel array.
1603 */
1604 length=(size_t) number_pixels;
1605 pixels=(unsigned char *) AcquireQuantumMemory(length,
1606 sizeof(*pixels));
1607 if (pixels == (unsigned char *) NULL)
1608 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1609 /*
1610 Dump Runlength encoded pixels.
1611 */
1612 q=pixels;
cristybb503372010-05-27 20:51:26 +00001613 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001614 {
cristy1e178e72011-08-28 19:44:34 +00001615 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001616 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001617 break;
cristybb503372010-05-27 20:51:26 +00001618 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001619 {
cristy4c08aed2011-07-01 19:47:50 +00001620 *q++=ScaleQuantumToChar(GetPixelIntensity(image,p));
cristyed231572011-07-14 02:18:59 +00001621 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001622 }
1623 if (image->previous == (Image *) NULL)
1624 {
cristyf5c61ba2010-12-17 00:58:04 +00001625 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1626 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001627 if (status == MagickFalse)
1628 break;
1629 }
1630 }
1631#if defined(MAGICKCORE_ZLIB_DELEGATE)
1632 if (compression == ZipCompression)
cristy018f07f2011-09-04 21:15:19 +00001633 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001634 else
1635#endif
1636 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00001637 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001638 else
cristy018f07f2011-09-04 21:15:19 +00001639 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001640 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1641 if (status == MagickFalse)
1642 {
1643 (void) CloseBlob(image);
1644 return(MagickFalse);
1645 }
1646 break;
1647 }
1648 case NoCompression:
1649 {
1650 /*
1651 Dump uncompressed PseudoColor packets.
1652 */
1653 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00001654 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001655 {
cristy1e178e72011-08-28 19:44:34 +00001656 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001657 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001658 break;
cristybb503372010-05-27 20:51:26 +00001659 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001660 {
cristy4c08aed2011-07-01 19:47:50 +00001661 Ascii85Encode(image,ScaleQuantumToChar(
1662 GetPixelIntensity(image,p)));
cristyed231572011-07-14 02:18:59 +00001663 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001664 }
1665 if (image->previous == (Image *) NULL)
1666 {
cristyf5c61ba2010-12-17 00:58:04 +00001667 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1668 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001669 if (status == MagickFalse)
1670 break;
1671 }
1672 }
1673 Ascii85Flush(image);
1674 break;
1675 }
1676 }
1677 }
1678 else
1679 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1680 (compression == JPEGCompression) ||
1681 (compression == JPEG2000Compression))
1682 switch (compression)
1683 {
1684 case JPEGCompression:
1685 {
cristy1e178e72011-08-28 19:44:34 +00001686 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
cristy3ed852e2009-09-05 21:47:34 +00001687 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00001688 {
1689 (void) CloseBlob(image);
1690 return(MagickFalse);
1691 }
cristy3ed852e2009-09-05 21:47:34 +00001692 break;
1693 }
1694 case JPEG2000Compression:
1695 {
cristy1e178e72011-08-28 19:44:34 +00001696 status=InjectImageBlob(image_info,image,image,"jp2",exception);
cristy3ed852e2009-09-05 21:47:34 +00001697 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00001698 {
1699 (void) CloseBlob(image);
1700 return(MagickFalse);
1701 }
cristy3ed852e2009-09-05 21:47:34 +00001702 break;
1703 }
1704 case RLECompression:
1705 default:
1706 {
1707 /*
1708 Allocate pixel array.
1709 */
1710 length=(size_t) number_pixels;
1711 pixels=(unsigned char *) AcquireQuantumMemory(length,
1712 4*sizeof(*pixels));
1713 length*=image->colorspace == CMYKColorspace ? 4UL : 3UL;
1714 if (pixels == (unsigned char *) NULL)
1715 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1716 /*
1717 Dump runoffset encoded pixels.
1718 */
1719 q=pixels;
cristybb503372010-05-27 20:51:26 +00001720 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001721 {
cristy1e178e72011-08-28 19:44:34 +00001722 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001723 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001724 break;
cristybb503372010-05-27 20:51:26 +00001725 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001726 {
cristy4c08aed2011-07-01 19:47:50 +00001727 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1728 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1729 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
cristy3ed852e2009-09-05 21:47:34 +00001730 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00001731 *q++=ScaleQuantumToChar(GetPixelBlack(image,p));
cristyed231572011-07-14 02:18:59 +00001732 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001733 }
1734 if (image->previous == (Image *) NULL)
1735 {
cristyf5c61ba2010-12-17 00:58:04 +00001736 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1737 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001738 if (status == MagickFalse)
1739 break;
1740 }
1741 }
1742#if defined(MAGICKCORE_ZLIB_DELEGATE)
1743 if (compression == ZipCompression)
cristy018f07f2011-09-04 21:15:19 +00001744 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001745 else
1746#endif
1747 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00001748 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001749 else
cristy018f07f2011-09-04 21:15:19 +00001750 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001751 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1752 if (status == MagickFalse)
1753 {
1754 (void) CloseBlob(image);
1755 return(MagickFalse);
1756 }
1757 break;
1758 }
1759 case NoCompression:
1760 {
1761 /*
1762 Dump uncompressed DirectColor packets.
1763 */
1764 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00001765 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001766 {
cristy1e178e72011-08-28 19:44:34 +00001767 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001768 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001769 break;
cristybb503372010-05-27 20:51:26 +00001770 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001771 {
cristy4c08aed2011-07-01 19:47:50 +00001772 Ascii85Encode(image,ScaleQuantumToChar(GetPixelRed(image,p)));
1773 Ascii85Encode(image,ScaleQuantumToChar(GetPixelGreen(image,p)));
1774 Ascii85Encode(image,ScaleQuantumToChar(GetPixelBlue(image,p)));
cristy3ed852e2009-09-05 21:47:34 +00001775 if (image->colorspace == CMYKColorspace)
cristyaff6d802011-04-26 01:46:31 +00001776 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +00001777 GetPixelBlack(image,p)));
cristyed231572011-07-14 02:18:59 +00001778 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001779 }
1780 if (image->previous == (Image *) NULL)
1781 {
cristyf5c61ba2010-12-17 00:58:04 +00001782 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1783 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001784 if (status == MagickFalse)
1785 break;
1786 }
1787 }
1788 Ascii85Flush(image);
1789 break;
1790 }
1791 }
1792 else
1793 {
1794 /*
1795 Dump number of colors and colormap.
1796 */
1797 switch (compression)
1798 {
1799 case RLECompression:
1800 default:
1801 {
1802 /*
1803 Allocate pixel array.
1804 */
1805 length=(size_t) number_pixels;
1806 pixels=(unsigned char *) AcquireQuantumMemory(length,
1807 sizeof(*pixels));
1808 if (pixels == (unsigned char *) NULL)
1809 ThrowWriterException(ResourceLimitError,
1810 "MemoryAllocationFailed");
1811 /*
1812 Dump Runlength encoded pixels.
1813 */
1814 q=pixels;
cristybb503372010-05-27 20:51:26 +00001815 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001816 {
cristy1e178e72011-08-28 19:44:34 +00001817 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001818 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001819 break;
cristybb503372010-05-27 20:51:26 +00001820 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00001821 {
1822 *q++=(unsigned char) GetPixelIndex(image,p);
cristyed231572011-07-14 02:18:59 +00001823 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001824 }
cristy3ed852e2009-09-05 21:47:34 +00001825 if (image->previous == (Image *) NULL)
1826 {
cristyf5c61ba2010-12-17 00:58:04 +00001827 status=SetImageProgress(image,SaveImageTag,
1828 (MagickOffsetType) y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001829 if (status == MagickFalse)
1830 break;
1831 }
1832 }
1833#if defined(MAGICKCORE_ZLIB_DELEGATE)
1834 if (compression == ZipCompression)
cristy018f07f2011-09-04 21:15:19 +00001835 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001836 else
1837#endif
1838 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00001839 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001840 else
cristy018f07f2011-09-04 21:15:19 +00001841 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001842 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1843 if (status == MagickFalse)
1844 {
1845 (void) CloseBlob(image);
1846 return(MagickFalse);
1847 }
1848 break;
1849 }
1850 case NoCompression:
1851 {
1852 /*
1853 Dump uncompressed PseudoColor packets.
1854 */
1855 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00001856 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001857 {
cristy1e178e72011-08-28 19:44:34 +00001858 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001859 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001860 break;
cristybb503372010-05-27 20:51:26 +00001861 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00001862 {
1863 Ascii85Encode(image,(unsigned char) GetPixelIndex(image,p));
cristyed231572011-07-14 02:18:59 +00001864 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001865 }
cristy3ed852e2009-09-05 21:47:34 +00001866 if (image->previous == (Image *) NULL)
1867 {
cristyf5c61ba2010-12-17 00:58:04 +00001868 status=SetImageProgress(image,SaveImageTag,
1869 (MagickOffsetType) y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001870 if (status == MagickFalse)
1871 break;
1872 }
1873 }
1874 Ascii85Flush(image);
1875 break;
1876 }
1877 }
1878 }
1879 offset=TellBlob(image)-offset;
1880 (void) WriteBlobString(image,"\nendstream\n");
1881 (void) WriteBlobString(image,"endobj\n");
1882 /*
1883 Write Length object.
1884 */
1885 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001886 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001887 object);
cristy3ed852e2009-09-05 21:47:34 +00001888 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001889 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",
cristye8c25f92010-06-03 00:53:06 +00001890 (double) offset);
cristy3ed852e2009-09-05 21:47:34 +00001891 (void) WriteBlobString(image,buffer);
1892 (void) WriteBlobString(image,"endobj\n");
1893 /*
1894 Write Colorspace object.
1895 */
1896 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001897 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001898 object);
cristy3ed852e2009-09-05 21:47:34 +00001899 (void) WriteBlobString(image,buffer);
1900 if (image->colorspace == CMYKColorspace)
1901 (void) CopyMagickString(buffer,"/DeviceCMYK\n",MaxTextExtent);
1902 else
1903 if ((compression == FaxCompression) ||
1904 (compression == Group4Compression) ||
1905 ((image_info->type != TrueColorType) &&
cristy1e178e72011-08-28 19:44:34 +00001906 (IsImageGray(image,exception) != MagickFalse)))
cristy3ed852e2009-09-05 21:47:34 +00001907 (void) CopyMagickString(buffer,"/DeviceGray\n",MaxTextExtent);
1908 else
1909 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1910 (compression == JPEGCompression) ||
1911 (compression == JPEG2000Compression))
1912 (void) CopyMagickString(buffer,"/DeviceRGB\n",MaxTextExtent);
1913 else
cristyb51dff52011-05-19 16:55:47 +00001914 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00001915 "[ /Indexed /DeviceRGB %.20g %.20g 0 R ]\n",(double) image->colors-
1916 1,(double) object+3);
cristy3ed852e2009-09-05 21:47:34 +00001917 (void) WriteBlobString(image,buffer);
1918 (void) WriteBlobString(image,"endobj\n");
1919 /*
1920 Write Thumb object.
1921 */
1922 SetGeometry(image,&geometry);
1923 (void) ParseMetaGeometry("106x106+0+0>",&geometry.x,&geometry.y,
1924 &geometry.width,&geometry.height);
cristy1e178e72011-08-28 19:44:34 +00001925 tile_image=ThumbnailImage(image,geometry.width,geometry.height,exception);
cristy3ed852e2009-09-05 21:47:34 +00001926 if (tile_image == (Image *) NULL)
cristy1e178e72011-08-28 19:44:34 +00001927 return(MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +00001928 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001929 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001930 object);
cristy3ed852e2009-09-05 21:47:34 +00001931 (void) WriteBlobString(image,buffer);
1932 (void) WriteBlobString(image,"<<\n");
1933 switch (compression)
1934 {
1935 case NoCompression:
1936 {
cristyb51dff52011-05-19 16:55:47 +00001937 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"ASCII85Decode");
cristy3ed852e2009-09-05 21:47:34 +00001938 break;
1939 }
1940 case JPEGCompression:
1941 {
cristyb51dff52011-05-19 16:55:47 +00001942 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"DCTDecode");
cristy3ed852e2009-09-05 21:47:34 +00001943 if (image->colorspace != CMYKColorspace)
1944 break;
1945 (void) WriteBlobString(image,buffer);
1946 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1947 MaxTextExtent);
1948 break;
1949 }
1950 case JPEG2000Compression:
1951 {
cristyb51dff52011-05-19 16:55:47 +00001952 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"JPXDecode");
cristy3ed852e2009-09-05 21:47:34 +00001953 if (image->colorspace != CMYKColorspace)
1954 break;
1955 (void) WriteBlobString(image,buffer);
1956 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1957 MaxTextExtent);
1958 break;
1959 }
1960 case LZWCompression:
1961 {
cristyb51dff52011-05-19 16:55:47 +00001962 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"LZWDecode");
cristy3ed852e2009-09-05 21:47:34 +00001963 break;
1964 }
1965 case ZipCompression:
1966 {
cristyb51dff52011-05-19 16:55:47 +00001967 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"FlateDecode");
cristy3ed852e2009-09-05 21:47:34 +00001968 break;
1969 }
1970 case FaxCompression:
1971 case Group4Compression:
1972 {
1973 (void) CopyMagickString(buffer,"/Filter [ /CCITTFaxDecode ]\n",
1974 MaxTextExtent);
1975 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001976 (void) FormatLocaleString(buffer,MaxTextExtent,"/DecodeParms [ << "
cristye8c25f92010-06-03 00:53:06 +00001977 "/K %s /BlackIs1 false /Columns %.20g /Rows %.20g >> ]\n",CCITTParam,
1978 (double) tile_image->columns,(double) tile_image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001979 break;
1980 }
1981 default:
1982 {
cristyb51dff52011-05-19 16:55:47 +00001983 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
cristy3ed852e2009-09-05 21:47:34 +00001984 "RunLengthDecode");
1985 break;
1986 }
1987 }
1988 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001989 (void) FormatLocaleString(buffer,MaxTextExtent,"/Width %.20g\n",(double)
cristye8c25f92010-06-03 00:53:06 +00001990 tile_image->columns);
cristy3ed852e2009-09-05 21:47:34 +00001991 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001992 (void) FormatLocaleString(buffer,MaxTextExtent,"/Height %.20g\n",(double)
cristye8c25f92010-06-03 00:53:06 +00001993 tile_image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001994 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001995 (void) FormatLocaleString(buffer,MaxTextExtent,"/ColorSpace %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001996 (double) object-1);
cristy3ed852e2009-09-05 21:47:34 +00001997 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001998 (void) FormatLocaleString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
cristy3ed852e2009-09-05 21:47:34 +00001999 (compression == FaxCompression) || (compression == Group4Compression) ?
2000 1 : 8);
2001 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002002 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00002003 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00002004 (void) WriteBlobString(image,buffer);
2005 (void) WriteBlobString(image,">>\n");
2006 (void) WriteBlobString(image,"stream\n");
2007 offset=TellBlob(image);
2008 number_pixels=(MagickSizeType) tile_image->columns*tile_image->rows;
2009 if ((compression == FaxCompression) ||
2010 (compression == Group4Compression) ||
2011 ((image_info->type != TrueColorType) &&
cristy1e178e72011-08-28 19:44:34 +00002012 (IsImageGray(tile_image,exception) != MagickFalse)))
cristy3ed852e2009-09-05 21:47:34 +00002013 {
2014 switch (compression)
2015 {
2016 case FaxCompression:
2017 case Group4Compression:
2018 {
2019 if (LocaleCompare(CCITTParam,"0") == 0)
2020 {
cristy018f07f2011-09-04 21:15:19 +00002021 (void) HuffmanEncodeImage(image_info,image,tile_image,
2022 exception);
cristy3ed852e2009-09-05 21:47:34 +00002023 break;
2024 }
cristy018f07f2011-09-04 21:15:19 +00002025 (void) Huffman2DEncodeImage(image_info,image,tile_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00002026 break;
2027 }
2028 case JPEGCompression:
2029 {
2030 status=InjectImageBlob(image_info,image,tile_image,"jpeg",
cristy1e178e72011-08-28 19:44:34 +00002031 exception);
cristy3ed852e2009-09-05 21:47:34 +00002032 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00002033 {
2034 (void) CloseBlob(image);
2035 return(MagickFalse);
2036 }
cristy3ed852e2009-09-05 21:47:34 +00002037 break;
2038 }
2039 case JPEG2000Compression:
2040 {
cristy1e178e72011-08-28 19:44:34 +00002041 status=InjectImageBlob(image_info,image,tile_image,"jp2",exception);
cristy3ed852e2009-09-05 21:47:34 +00002042 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00002043 {
2044 (void) CloseBlob(image);
2045 return(MagickFalse);
2046 }
cristy3ed852e2009-09-05 21:47:34 +00002047 break;
2048 }
2049 case RLECompression:
2050 default:
2051 {
2052 /*
2053 Allocate pixel array.
2054 */
2055 length=(size_t) number_pixels;
2056 pixels=(unsigned char *) AcquireQuantumMemory(length,
2057 sizeof(*pixels));
2058 if (pixels == (unsigned char *) NULL)
2059 {
2060 tile_image=DestroyImage(tile_image);
2061 ThrowWriterException(ResourceLimitError,
2062 "MemoryAllocationFailed");
2063 }
2064 /*
2065 Dump Runlength encoded pixels.
2066 */
2067 q=pixels;
cristybb503372010-05-27 20:51:26 +00002068 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002069 {
2070 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
cristy1e178e72011-08-28 19:44:34 +00002071 exception);
cristy4c08aed2011-07-01 19:47:50 +00002072 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002073 break;
cristybb503372010-05-27 20:51:26 +00002074 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002075 {
cristy4c08aed2011-07-01 19:47:50 +00002076 *q++=ScaleQuantumToChar(GetPixelIntensity(tile_image,p));
cristyed231572011-07-14 02:18:59 +00002077 p+=GetPixelChannels(tile_image);
cristy3ed852e2009-09-05 21:47:34 +00002078 }
2079 }
2080#if defined(MAGICKCORE_ZLIB_DELEGATE)
2081 if (compression == ZipCompression)
cristy018f07f2011-09-04 21:15:19 +00002082 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002083 else
2084#endif
2085 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00002086 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002087 else
cristy018f07f2011-09-04 21:15:19 +00002088 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002089 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2090 if (status == MagickFalse)
2091 {
2092 (void) CloseBlob(image);
2093 return(MagickFalse);
2094 }
2095 break;
2096 }
2097 case NoCompression:
2098 {
2099 /*
2100 Dump uncompressed PseudoColor packets.
2101 */
2102 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00002103 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002104 {
2105 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
cristy1e178e72011-08-28 19:44:34 +00002106 exception);
cristy4c08aed2011-07-01 19:47:50 +00002107 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002108 break;
cristybb503372010-05-27 20:51:26 +00002109 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002110 {
2111 Ascii85Encode(image,
cristy4c08aed2011-07-01 19:47:50 +00002112 ScaleQuantumToChar(GetPixelIntensity(tile_image,p)));
cristyed231572011-07-14 02:18:59 +00002113 p+=GetPixelChannels(tile_image);
cristy3ed852e2009-09-05 21:47:34 +00002114 }
2115 }
2116 Ascii85Flush(image);
2117 break;
2118 }
2119 }
2120 }
2121 else
2122 if ((tile_image->storage_class == DirectClass) ||
2123 (tile_image->colors > 256) || (compression == JPEGCompression) ||
2124 (compression == JPEG2000Compression))
2125 switch (compression)
2126 {
2127 case JPEGCompression:
2128 {
2129 status=InjectImageBlob(image_info,image,tile_image,"jpeg",
cristy1e178e72011-08-28 19:44:34 +00002130 exception);
cristy3ed852e2009-09-05 21:47:34 +00002131 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00002132 {
2133 (void) CloseBlob(image);
2134 return(MagickFalse);
2135 }
cristy3ed852e2009-09-05 21:47:34 +00002136 break;
2137 }
2138 case JPEG2000Compression:
2139 {
cristy1e178e72011-08-28 19:44:34 +00002140 status=InjectImageBlob(image_info,image,tile_image,"jp2",exception);
cristy3ed852e2009-09-05 21:47:34 +00002141 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00002142 {
2143 (void) CloseBlob(image);
2144 return(MagickFalse);
2145 }
cristy3ed852e2009-09-05 21:47:34 +00002146 break;
2147 }
2148 case RLECompression:
2149 default:
2150 {
2151 /*
2152 Allocate pixel array.
2153 */
2154 length=(size_t) number_pixels;
2155 pixels=(unsigned char *) AcquireQuantumMemory(length,4*
2156 sizeof(*pixels));
2157 length*=tile_image->colorspace == CMYKColorspace ? 4UL : 3UL;
2158 if (pixels == (unsigned char *) NULL)
2159 {
2160 tile_image=DestroyImage(tile_image);
2161 ThrowWriterException(ResourceLimitError,
2162 "MemoryAllocationFailed");
2163 }
2164 /*
2165 Dump runoffset encoded pixels.
2166 */
2167 q=pixels;
cristybb503372010-05-27 20:51:26 +00002168 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002169 {
2170 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
cristy1e178e72011-08-28 19:44:34 +00002171 exception);
cristy4c08aed2011-07-01 19:47:50 +00002172 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002173 break;
cristybb503372010-05-27 20:51:26 +00002174 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002175 {
cristy4c08aed2011-07-01 19:47:50 +00002176 *q++=ScaleQuantumToChar(GetPixelRed(tile_image,p));
2177 *q++=ScaleQuantumToChar(GetPixelGreen(tile_image,p));
2178 *q++=ScaleQuantumToChar(GetPixelBlue(tile_image,p));
cristy3ed852e2009-09-05 21:47:34 +00002179 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00002180 *q++=ScaleQuantumToChar(GetPixelBlack(tile_image,p));
cristyed231572011-07-14 02:18:59 +00002181 p+=GetPixelChannels(tile_image);
cristy3ed852e2009-09-05 21:47:34 +00002182 }
2183 }
2184#if defined(MAGICKCORE_ZLIB_DELEGATE)
2185 if (compression == ZipCompression)
cristy018f07f2011-09-04 21:15:19 +00002186 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002187 else
2188#endif
2189 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00002190 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002191 else
cristy018f07f2011-09-04 21:15:19 +00002192 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002193 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2194 if (status == MagickFalse)
2195 {
2196 (void) CloseBlob(image);
2197 return(MagickFalse);
2198 }
2199 break;
2200 }
2201 case NoCompression:
2202 {
2203 /*
2204 Dump uncompressed DirectColor packets.
2205 */
2206 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00002207 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002208 {
2209 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
cristy1e178e72011-08-28 19:44:34 +00002210 exception);
cristy4c08aed2011-07-01 19:47:50 +00002211 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002212 break;
cristybb503372010-05-27 20:51:26 +00002213 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002214 {
cristyf5c61ba2010-12-17 00:58:04 +00002215 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +00002216 GetPixelRed(tile_image,p)));
cristyf5c61ba2010-12-17 00:58:04 +00002217 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +00002218 GetPixelGreen(tile_image,p)));
cristyf5c61ba2010-12-17 00:58:04 +00002219 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +00002220 GetPixelBlue(tile_image,p)));
cristy3ed852e2009-09-05 21:47:34 +00002221 if (image->colorspace == CMYKColorspace)
cristyaff6d802011-04-26 01:46:31 +00002222 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +00002223 GetPixelBlack(tile_image,p)));
cristyed231572011-07-14 02:18:59 +00002224 p+=GetPixelChannels(tile_image);
cristy3ed852e2009-09-05 21:47:34 +00002225 }
2226 }
2227 Ascii85Flush(image);
2228 break;
2229 }
2230 }
2231 else
2232 {
2233 /*
2234 Dump number of colors and colormap.
2235 */
2236 switch (compression)
2237 {
2238 case RLECompression:
2239 default:
2240 {
2241 /*
2242 Allocate pixel array.
2243 */
2244 length=(size_t) number_pixels;
2245 pixels=(unsigned char *) AcquireQuantumMemory(length,
2246 sizeof(*pixels));
2247 if (pixels == (unsigned char *) NULL)
2248 {
2249 tile_image=DestroyImage(tile_image);
2250 ThrowWriterException(ResourceLimitError,
2251 "MemoryAllocationFailed");
2252 }
2253 /*
2254 Dump Runlength encoded pixels.
2255 */
2256 q=pixels;
cristybb503372010-05-27 20:51:26 +00002257 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002258 {
2259 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
cristy1e178e72011-08-28 19:44:34 +00002260 exception);
cristy4c08aed2011-07-01 19:47:50 +00002261 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002262 break;
cristybb503372010-05-27 20:51:26 +00002263 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00002264 {
2265 *q++=(unsigned char) GetPixelIndex(tile_image,p);
cristyed231572011-07-14 02:18:59 +00002266 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00002267 }
cristy3ed852e2009-09-05 21:47:34 +00002268 }
2269#if defined(MAGICKCORE_ZLIB_DELEGATE)
2270 if (compression == ZipCompression)
cristy018f07f2011-09-04 21:15:19 +00002271 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002272 else
2273#endif
2274 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00002275 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002276 else
cristy018f07f2011-09-04 21:15:19 +00002277 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002278 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2279 if (status == MagickFalse)
2280 {
2281 (void) CloseBlob(image);
2282 return(MagickFalse);
2283 }
2284 break;
2285 }
2286 case NoCompression:
2287 {
2288 /*
2289 Dump uncompressed PseudoColor packets.
2290 */
2291 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00002292 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002293 {
2294 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
cristy1e178e72011-08-28 19:44:34 +00002295 exception);
cristy4c08aed2011-07-01 19:47:50 +00002296 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002297 break;
cristybb503372010-05-27 20:51:26 +00002298 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00002299 {
cristyaff6d802011-04-26 01:46:31 +00002300 Ascii85Encode(image,(unsigned char)
cristy4c08aed2011-07-01 19:47:50 +00002301 GetPixelIndex(tile_image,p));
cristyed231572011-07-14 02:18:59 +00002302 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00002303 }
cristy3ed852e2009-09-05 21:47:34 +00002304 }
2305 Ascii85Flush(image);
2306 break;
2307 }
2308 }
2309 }
2310 tile_image=DestroyImage(tile_image);
2311 offset=TellBlob(image)-offset;
2312 (void) WriteBlobString(image,"\nendstream\n");
2313 (void) WriteBlobString(image,"endobj\n");
2314 /*
2315 Write Length object.
2316 */
2317 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00002318 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002319 object);
cristy3ed852e2009-09-05 21:47:34 +00002320 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002321 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
cristy3ed852e2009-09-05 21:47:34 +00002322 (void) WriteBlobString(image,buffer);
2323 (void) WriteBlobString(image,"endobj\n");
2324 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00002325 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002326 object);
cristy3ed852e2009-09-05 21:47:34 +00002327 (void) WriteBlobString(image,buffer);
2328 if ((image->storage_class != DirectClass) && (image->colors <= 256) &&
2329 (compression != FaxCompression) && (compression != Group4Compression))
2330 {
2331 /*
2332 Write Colormap object.
2333 */
2334 (void) WriteBlobString(image,"<<\n");
2335 if (compression == NoCompression)
2336 (void) WriteBlobString(image,"/Filter [ /ASCII85Decode ]\n");
cristyb51dff52011-05-19 16:55:47 +00002337 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00002338 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00002339 (void) WriteBlobString(image,buffer);
2340 (void) WriteBlobString(image,">>\n");
2341 (void) WriteBlobString(image,"stream\n");
2342 offset=TellBlob(image);
2343 if (compression == NoCompression)
2344 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00002345 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00002346 {
2347 if (compression == NoCompression)
2348 {
2349 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].red));
2350 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].green));
2351 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].blue));
2352 continue;
2353 }
2354 (void) WriteBlobByte(image,
2355 ScaleQuantumToChar(image->colormap[i].red));
2356 (void) WriteBlobByte(image,
2357 ScaleQuantumToChar(image->colormap[i].green));
2358 (void) WriteBlobByte(image,
2359 ScaleQuantumToChar(image->colormap[i].blue));
2360 }
2361 if (compression == NoCompression)
2362 Ascii85Flush(image);
2363 offset=TellBlob(image)-offset;
2364 (void) WriteBlobString(image,"\nendstream\n");
2365 }
2366 (void) WriteBlobString(image,"endobj\n");
2367 /*
2368 Write Length object.
2369 */
2370 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00002371 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002372 object);
cristy3ed852e2009-09-05 21:47:34 +00002373 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002374 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002375 offset);
cristy3ed852e2009-09-05 21:47:34 +00002376 (void) WriteBlobString(image,buffer);
2377 (void) WriteBlobString(image,"endobj\n");
2378 /*
2379 Write softmask object.
2380 */
2381 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00002382 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002383 object);
cristy3ed852e2009-09-05 21:47:34 +00002384 (void) WriteBlobString(image,buffer);
2385 (void) WriteBlobString(image,"<<\n");
2386 if (image->matte == MagickFalse)
2387 (void) WriteBlobString(image,">>\n");
2388 else
2389 {
2390 (void) WriteBlobString(image,"/Type /XObject\n");
2391 (void) WriteBlobString(image,"/Subtype /Image\n");
cristyb51dff52011-05-19 16:55:47 +00002392 (void) FormatLocaleString(buffer,MaxTextExtent,"/Name /Ma%.20g\n",
cristye8c25f92010-06-03 00:53:06 +00002393 (double) image->scene);
cristy3ed852e2009-09-05 21:47:34 +00002394 (void) WriteBlobString(image,buffer);
2395 switch (compression)
2396 {
2397 case NoCompression:
2398 {
cristyb51dff52011-05-19 16:55:47 +00002399 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
cristy3ed852e2009-09-05 21:47:34 +00002400 "ASCII85Decode");
2401 break;
2402 }
2403 case LZWCompression:
2404 {
cristyb51dff52011-05-19 16:55:47 +00002405 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"LZWDecode");
cristy3ed852e2009-09-05 21:47:34 +00002406 break;
2407 }
2408 case ZipCompression:
2409 {
cristyb51dff52011-05-19 16:55:47 +00002410 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
cristy3ed852e2009-09-05 21:47:34 +00002411 "FlateDecode");
2412 break;
2413 }
2414 default:
2415 {
cristyb51dff52011-05-19 16:55:47 +00002416 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
cristy3ed852e2009-09-05 21:47:34 +00002417 "RunLengthDecode");
2418 break;
2419 }
2420 }
2421 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002422 (void) FormatLocaleString(buffer,MaxTextExtent,"/Width %.20g\n",
cristye8c25f92010-06-03 00:53:06 +00002423 (double) image->columns);
cristy3ed852e2009-09-05 21:47:34 +00002424 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002425 (void) FormatLocaleString(buffer,MaxTextExtent,"/Height %.20g\n",
cristye8c25f92010-06-03 00:53:06 +00002426 (double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +00002427 (void) WriteBlobString(image,buffer);
2428 (void) WriteBlobString(image,"/ColorSpace /DeviceGray\n");
cristyb51dff52011-05-19 16:55:47 +00002429 (void) FormatLocaleString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
cristy3ed852e2009-09-05 21:47:34 +00002430 (compression == FaxCompression) || (compression == Group4Compression)
2431 ? 1 : 8);
2432 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002433 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00002434 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00002435 (void) WriteBlobString(image,buffer);
2436 (void) WriteBlobString(image,">>\n");
2437 (void) WriteBlobString(image,"stream\n");
2438 offset=TellBlob(image);
2439 number_pixels=(MagickSizeType) image->columns*image->rows;
2440 switch (compression)
2441 {
2442 case RLECompression:
2443 default:
2444 {
2445 /*
2446 Allocate pixel array.
2447 */
2448 length=(size_t) number_pixels;
2449 pixels=(unsigned char *) AcquireQuantumMemory(length,
2450 sizeof(*pixels));
2451 if (pixels == (unsigned char *) NULL)
2452 {
2453 image=DestroyImage(image);
2454 ThrowWriterException(ResourceLimitError,
2455 "MemoryAllocationFailed");
2456 }
2457 /*
2458 Dump Runlength encoded pixels.
2459 */
2460 q=pixels;
cristybb503372010-05-27 20:51:26 +00002461 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002462 {
cristy1e178e72011-08-28 19:44:34 +00002463 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00002464 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002465 break;
cristybb503372010-05-27 20:51:26 +00002466 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002467 {
cristy4c08aed2011-07-01 19:47:50 +00002468 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
cristyed231572011-07-14 02:18:59 +00002469 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002470 }
2471 }
2472#if defined(MAGICKCORE_ZLIB_DELEGATE)
2473 if (compression == ZipCompression)
cristy018f07f2011-09-04 21:15:19 +00002474 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002475 else
2476#endif
2477 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00002478 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002479 else
cristy018f07f2011-09-04 21:15:19 +00002480 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002481 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2482 if (status == MagickFalse)
2483 {
2484 (void) CloseBlob(image);
2485 return(MagickFalse);
2486 }
2487 break;
2488 }
2489 case NoCompression:
2490 {
2491 /*
2492 Dump uncompressed PseudoColor packets.
2493 */
2494 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00002495 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002496 {
cristy1e178e72011-08-28 19:44:34 +00002497 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00002498 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002499 break;
cristybb503372010-05-27 20:51:26 +00002500 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002501 {
cristy4c08aed2011-07-01 19:47:50 +00002502 Ascii85Encode(image,ScaleQuantumToChar(GetPixelAlpha(image,p)));
cristyed231572011-07-14 02:18:59 +00002503 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002504 }
2505 }
2506 Ascii85Flush(image);
2507 break;
2508 }
2509 }
2510 offset=TellBlob(image)-offset;
2511 (void) WriteBlobString(image,"\nendstream\n");
2512 }
2513 (void) WriteBlobString(image,"endobj\n");
2514 /*
2515 Write Length object.
2516 */
2517 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00002518 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002519 object);
cristy3ed852e2009-09-05 21:47:34 +00002520 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002521 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
cristy3ed852e2009-09-05 21:47:34 +00002522 (void) WriteBlobString(image,buffer);
2523 (void) WriteBlobString(image,"endobj\n");
2524 if (GetNextImageInList(image) == (Image *) NULL)
2525 break;
2526 image=SyncNextImageInList(image);
2527 status=SetImageProgress(image,SaveImagesTag,scene++,
2528 GetImageListLength(image));
2529 if (status == MagickFalse)
2530 break;
2531 } while (image_info->adjoin != MagickFalse);
2532 /*
2533 Write Metadata object.
2534 */
2535 xref[object++]=TellBlob(image);
2536 info_id=object;
cristyb51dff52011-05-19 16:55:47 +00002537 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002538 object);
cristy3ed852e2009-09-05 21:47:34 +00002539 (void) WriteBlobString(image,buffer);
2540 (void) WriteBlobString(image,"<<\n");
cristy57015272010-12-30 01:16:38 +00002541 GetPathComponent(image->filename,BasePath,basename);
cristyb51dff52011-05-19 16:55:47 +00002542 (void) FormatLocaleString(buffer,MaxTextExtent,"/Title (%s)\n",
cristy57015272010-12-30 01:16:38 +00002543 EscapeParenthesis(basename));
cristy3ed852e2009-09-05 21:47:34 +00002544 (void) WriteBlobString(image,buffer);
2545 seconds=time((time_t *) NULL);
2546#if defined(MAGICKCORE_HAVE_LOCALTIME_R)
2547 (void) localtime_r(&seconds,&local_time);
2548#else
2549 (void) memcpy(&local_time,localtime(&seconds),sizeof(local_time));
2550#endif
cristyb51dff52011-05-19 16:55:47 +00002551 (void) FormatLocaleString(date,MaxTextExtent,"D:%04d%02d%02d%02d%02d%02d",
cristy3ed852e2009-09-05 21:47:34 +00002552 local_time.tm_year+1900,local_time.tm_mon+1,local_time.tm_mday,
2553 local_time.tm_hour,local_time.tm_min,local_time.tm_sec);
cristyb51dff52011-05-19 16:55:47 +00002554 (void) FormatLocaleString(buffer,MaxTextExtent,"/CreationDate (%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,"/ModDate (%s)\n",date);
cristy3ed852e2009-09-05 21:47:34 +00002557 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002558 (void) FormatLocaleString(buffer,MaxTextExtent,"/Producer (%s)\n",
cristybb503372010-05-27 20:51:26 +00002559 EscapeParenthesis(GetMagickVersion((size_t *) NULL)));
cristy3ed852e2009-09-05 21:47:34 +00002560 (void) WriteBlobString(image,buffer);
2561 (void) WriteBlobString(image,">>\n");
2562 (void) WriteBlobString(image,"endobj\n");
2563 /*
2564 Write Xref object.
2565 */
2566 offset=TellBlob(image)-xref[0]+10;
2567 (void) WriteBlobString(image,"xref\n");
cristyb51dff52011-05-19 16:55:47 +00002568 (void) FormatLocaleString(buffer,MaxTextExtent,"0 %.20g\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002569 object+1);
cristy3ed852e2009-09-05 21:47:34 +00002570 (void) WriteBlobString(image,buffer);
2571 (void) WriteBlobString(image,"0000000000 65535 f \n");
cristybb503372010-05-27 20:51:26 +00002572 for (i=0; i < (ssize_t) object; i++)
cristy3ed852e2009-09-05 21:47:34 +00002573 {
cristyb51dff52011-05-19 16:55:47 +00002574 (void) FormatLocaleString(buffer,MaxTextExtent,"%010lu 00000 n \n",
cristyf2faecf2010-05-28 19:19:36 +00002575 (unsigned long) xref[i]);
cristy3ed852e2009-09-05 21:47:34 +00002576 (void) WriteBlobString(image,buffer);
2577 }
2578 (void) WriteBlobString(image,"trailer\n");
2579 (void) WriteBlobString(image,"<<\n");
cristyb51dff52011-05-19 16:55:47 +00002580 (void) FormatLocaleString(buffer,MaxTextExtent,"/Size %.20g\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002581 object+1);
cristy3ed852e2009-09-05 21:47:34 +00002582 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002583 (void) FormatLocaleString(buffer,MaxTextExtent,"/Info %.20g 0 R\n",(double)
cristye8c25f92010-06-03 00:53:06 +00002584 info_id);
cristy3ed852e2009-09-05 21:47:34 +00002585 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002586 (void) FormatLocaleString(buffer,MaxTextExtent,"/Root %.20g 0 R\n",(double)
cristye8c25f92010-06-03 00:53:06 +00002587 root_id);
cristy3ed852e2009-09-05 21:47:34 +00002588 (void) WriteBlobString(image,buffer);
2589 (void) WriteBlobString(image,">>\n");
2590 (void) WriteBlobString(image,"startxref\n");
cristyb51dff52011-05-19 16:55:47 +00002591 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
cristy3ed852e2009-09-05 21:47:34 +00002592 (void) WriteBlobString(image,buffer);
2593 (void) WriteBlobString(image,"%%EOF\n");
2594 xref=(MagickOffsetType *) RelinquishMagickMemory(xref);
2595 (void) CloseBlob(image);
2596 return(MagickTrue);
2597}