blob: f07658208c7eb1d0edc6344e5ef622d14afb266f [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"
cristye40005d2012-03-23 12:18:45 +000076#include "MagickCore/token.h"
cristy4c08aed2011-07-01 19:47:50 +000077#include "MagickCore/transform.h"
78#include "MagickCore/utility.h"
79#include "MagickCore/module.h"
cristy80975862009-09-25 14:34:31 +000080
81/*
82 Define declarations.
83*/
cristy3ed852e2009-09-05 21:47:34 +000084#if defined(MAGICKCORE_TIFF_DELEGATE)
85#define CCITTParam "-1"
86#else
87#define CCITTParam "0"
88#endif
89
90/*
91 Forward declarations.
92*/
93static MagickBooleanType
cristy1e178e72011-08-28 19:44:34 +000094 WritePDFImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +000095
96/*
97%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
98% %
99% %
100% %
cristydefb3f02009-09-10 02:18:35 +0000101% I n v o k e P D F D e l e g a t e %
cristy3ed852e2009-09-05 21:47:34 +0000102% %
103% %
104% %
105%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106%
cristydefb3f02009-09-10 02:18:35 +0000107% InvokePDFDelegate() executes the PDF interpreter with the specified command.
cristy3ed852e2009-09-05 21:47:34 +0000108%
cristydefb3f02009-09-10 02:18:35 +0000109% The format of the InvokePDFDelegate method is:
cristy3ed852e2009-09-05 21:47:34 +0000110%
cristydefb3f02009-09-10 02:18:35 +0000111% MagickBooleanType InvokePDFDelegate(const MagickBooleanType verbose,
112% const char *command,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000113%
114% A description of each parameter follows:
115%
116% o verbose: A value other than zero displays the command prior to
117% executing it.
118%
119% o command: the address of a character string containing the command to
120% execute.
121%
cristyb32b90a2009-09-07 21:45:48 +0000122% o exception: return any errors or warnings in this structure.
123%
cristy3ed852e2009-09-05 21:47:34 +0000124*/
cristydefb3f02009-09-10 02:18:35 +0000125static MagickBooleanType InvokePDFDelegate(const MagickBooleanType verbose,
126 const char *command,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000127{
cristyb32b90a2009-09-07 21:45:48 +0000128 int
129 status;
130
cristy0157aea2010-04-24 21:12:18 +0000131#if defined(MAGICKCORE_GS_DELEGATE) || defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000132 char
133 **argv;
134
cristydefb3f02009-09-10 02:18:35 +0000135 const GhostInfo
136 *ghost_info;
cristy3ed852e2009-09-05 21:47:34 +0000137
138 gs_main_instance
139 *interpreter;
140
141 int
142 argc,
cristyb32b90a2009-09-07 21:45:48 +0000143 code;
cristy3ed852e2009-09-05 21:47:34 +0000144
cristybb503372010-05-27 20:51:26 +0000145 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000146 i;
147
cristy0157aea2010-04-24 21:12:18 +0000148#if defined(MAGICKCORE_WINDOWS_SUPPORT)
cristydefb3f02009-09-10 02:18:35 +0000149 ghost_info=NTGhostscriptDLLVectors();
cristy3ed852e2009-09-05 21:47:34 +0000150#else
cristydefb3f02009-09-10 02:18:35 +0000151 GhostInfo
152 ghost_info_struct;
cristy3ed852e2009-09-05 21:47:34 +0000153
cristydefb3f02009-09-10 02:18:35 +0000154 ghost_info=(&ghost_info_struct);
155 (void) ResetMagickMemory(&ghost_info,0,sizeof(ghost_info));
156 ghost_info_struct.new_instance=(int (*)(gs_main_instance **,void *))
cristy3ed852e2009-09-05 21:47:34 +0000157 gsapi_new_instance;
cristydefb3f02009-09-10 02:18:35 +0000158 ghost_info_struct.init_with_args=(int (*)(gs_main_instance *,int,char **))
cristy3ed852e2009-09-05 21:47:34 +0000159 gsapi_init_with_args;
cristydefb3f02009-09-10 02:18:35 +0000160 ghost_info_struct.run_string=(int (*)(gs_main_instance *,const char *,int,
161 int *)) gsapi_run_string;
162 ghost_info_struct.delete_instance=(void (*)(gs_main_instance *))
cristy3ed852e2009-09-05 21:47:34 +0000163 gsapi_delete_instance;
cristydefb3f02009-09-10 02:18:35 +0000164 ghost_info_struct.exit=(int (*)(gs_main_instance *)) gsapi_exit;
cristy3ed852e2009-09-05 21:47:34 +0000165#endif
cristydefb3f02009-09-10 02:18:35 +0000166 if (ghost_info == (GhostInfo *) NULL)
cristyb32b90a2009-09-07 21:45:48 +0000167 {
cristy6de4bc22010-01-12 17:10:35 +0000168 status=SystemCommand(MagickFalse,verbose,command,exception);
cristy41083a42009-09-07 23:47:59 +0000169 return(status == 0 ? MagickTrue : MagickFalse);
cristyb32b90a2009-09-07 21:45:48 +0000170 }
cristy3ed852e2009-09-05 21:47:34 +0000171 if (verbose != MagickFalse)
172 {
173 (void) fputs("[ghostscript library]",stdout);
174 (void) fputs(strchr(command,' '),stdout);
175 }
cristydefb3f02009-09-10 02:18:35 +0000176 status=(ghost_info->new_instance)(&interpreter,(void *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000177 if (status < 0)
cristyb32b90a2009-09-07 21:45:48 +0000178 {
cristy6de4bc22010-01-12 17:10:35 +0000179 status=SystemCommand(MagickFalse,verbose,command,exception);
cristy41083a42009-09-07 23:47:59 +0000180 return(status == 0 ? MagickTrue : MagickFalse);
cristyb32b90a2009-09-07 21:45:48 +0000181 }
cristya73c0f82010-12-20 15:57:05 +0000182 code=0;
cristy3ed852e2009-09-05 21:47:34 +0000183 argv=StringToArgv(command,&argc);
cristydefb3f02009-09-10 02:18:35 +0000184 status=(ghost_info->init_with_args)(interpreter,argc-1,argv+1);
cristy3ed852e2009-09-05 21:47:34 +0000185 if (status == 0)
cristydefb3f02009-09-10 02:18:35 +0000186 status=(ghost_info->run_string)(interpreter,"systemdict /start get exec\n",
187 0,&code);
188 (ghost_info->exit)(interpreter);
189 (ghost_info->delete_instance)(interpreter);
cristy0157aea2010-04-24 21:12:18 +0000190#if defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000191 NTGhostscriptUnLoadDLL();
192#endif
cristybb503372010-05-27 20:51:26 +0000193 for (i=0; i < (ssize_t) argc; i++)
cristy3ed852e2009-09-05 21:47:34 +0000194 argv[i]=DestroyString(argv[i]);
195 argv=(char **) RelinquishMagickMemory(argv);
cristy41083a42009-09-07 23:47:59 +0000196 if ((status != 0) && (status != -101))
197 {
198 char
199 *message;
cristyb32b90a2009-09-07 21:45:48 +0000200
cristy41083a42009-09-07 23:47:59 +0000201 message=GetExceptionMessage(errno);
202 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
203 "`%s': %s",command,message);
204 message=DestroyString(message);
205 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
206 "Ghostscript returns status %d, exit code %d",status,code);
207 return(MagickFalse);
208 }
cristy3ed852e2009-09-05 21:47:34 +0000209 return(MagickTrue);
210#else
cristy6de4bc22010-01-12 17:10:35 +0000211 status=SystemCommand(MagickFalse,verbose,command,exception);
cristy41083a42009-09-07 23:47:59 +0000212 return(status == 0 ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000213#endif
214}
215
216/*
217%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
218% %
219% %
220% %
221% I s P D F %
222% %
223% %
224% %
225%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
226%
227% IsPDF() returns MagickTrue if the image format type, identified by the
228% magick string, is PDF.
229%
230% The format of the IsPDF method is:
231%
232% MagickBooleanType IsPDF(const unsigned char *magick,const size_t offset)
233%
234% A description of each parameter follows:
235%
236% o magick: compare image format pattern against these bytes.
237%
238% o offset: Specifies the offset of the magick string.
239%
240*/
241static MagickBooleanType IsPDF(const unsigned char *magick,const size_t offset)
242{
243 if (offset < 5)
244 return(MagickFalse);
cristy41083a42009-09-07 23:47:59 +0000245 if (LocaleNCompare((const char *) magick,"%PDF-",5) == 0)
cristy3ed852e2009-09-05 21:47:34 +0000246 return(MagickTrue);
247 return(MagickFalse);
248}
249
250/*
251%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
252% %
253% %
254% %
255% R e a d P D F I m a g e %
256% %
257% %
258% %
259%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
260%
261% ReadPDFImage() reads a Portable Document Format image file and
262% returns it. It allocates the memory necessary for the new Image structure
263% and returns a pointer to the new image.
264%
265% The format of the ReadPDFImage method is:
266%
267% Image *ReadPDFImage(const ImageInfo *image_info,ExceptionInfo *exception)
268%
269% A description of each parameter follows:
270%
271% o image_info: the image info.
272%
273% o exception: return any errors or warnings in this structure.
274%
275*/
276
277static MagickBooleanType IsPDFRendered(const char *path)
278{
279 MagickBooleanType
280 status;
281
282 struct stat
283 attributes;
284
285 if ((path == (const char *) NULL) || (*path == '\0'))
286 return(MagickFalse);
287 status=GetPathAttributes(path,&attributes);
288 if ((status != MagickFalse) && S_ISREG(attributes.st_mode) &&
289 (attributes.st_size > 0))
290 return(MagickTrue);
291 return(MagickFalse);
292}
293
294static Image *ReadPDFImage(const ImageInfo *image_info,ExceptionInfo *exception)
295{
296#define CropBox "CropBox"
297#define DeviceCMYK "DeviceCMYK"
298#define MediaBox "MediaBox"
299#define RenderPostscriptText "Rendering Postscript... "
300#define PDFRotate "Rotate"
301#define SpotColor "Separation"
302#define TrimBox "TrimBox"
303#define PDFVersion "PDF-"
304
305 char
306 command[MaxTextExtent],
307 density[MaxTextExtent],
308 filename[MaxTextExtent],
309 geometry[MaxTextExtent],
310 options[MaxTextExtent],
311 input_filename[MaxTextExtent],
312 postscript_filename[MaxTextExtent];
313
314 const char
315 *option;
316
317 const DelegateInfo
318 *delegate_info;
319
320 double
321 angle;
322
cristydba40d42010-03-25 18:31:50 +0000323 GeometryInfo
324 geometry_info;
325
cristy3ed852e2009-09-05 21:47:34 +0000326 Image
327 *image,
328 *next,
329 *pdf_image;
330
331 ImageInfo
332 *read_info;
333
334 int
cristya97426c2011-02-04 01:41:27 +0000335 c,
cristy3ed852e2009-09-05 21:47:34 +0000336 file;
337
338 MagickBooleanType
339 cmyk,
340 cropbox,
341 trimbox,
342 status;
343
cristydba40d42010-03-25 18:31:50 +0000344 MagickStatusType
345 flags;
346
cristy3ed852e2009-09-05 21:47:34 +0000347 PointInfo
348 delta;
349
350 RectangleInfo
351 bounding_box,
352 page;
353
354 register char
355 *p;
356
cristya97426c2011-02-04 01:41:27 +0000357 register ssize_t
358 i;
cristy3ed852e2009-09-05 21:47:34 +0000359
360 SegmentInfo
361 bounds,
362 hires_bounds;
363
cristybb503372010-05-27 20:51:26 +0000364 size_t
cristy3ed852e2009-09-05 21:47:34 +0000365 scene,
366 spotcolor;
367
cristyaff6d802011-04-26 01:46:31 +0000368 ssize_t
369 count;
370
cristy3ed852e2009-09-05 21:47:34 +0000371 assert(image_info != (const ImageInfo *) NULL);
372 assert(image_info->signature == MagickSignature);
373 if (image_info->debug != MagickFalse)
374 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
375 image_info->filename);
376 assert(exception != (ExceptionInfo *) NULL);
377 assert(exception->signature == MagickSignature);
378 /*
379 Open image file.
380 */
cristy9950d572011-10-01 18:22:35 +0000381 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000382 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
383 if (status == MagickFalse)
384 {
385 image=DestroyImageList(image);
386 return((Image *) NULL);
387 }
388 status=AcquireUniqueSymbolicLink(image_info->filename,input_filename);
389 if (status == MagickFalse)
390 {
391 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
392 image_info->filename);
393 image=DestroyImageList(image);
394 return((Image *) NULL);
395 }
396 /*
397 Set the page density.
398 */
399 delta.x=DefaultResolution;
400 delta.y=DefaultResolution;
cristy2a11bef2011-10-28 18:33:11 +0000401 if ((image->resolution.x == 0.0) || (image->resolution.y == 0.0))
cristy3ed852e2009-09-05 21:47:34 +0000402 {
cristy3ed852e2009-09-05 21:47:34 +0000403 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
cristy2a11bef2011-10-28 18:33:11 +0000404 image->resolution.x=geometry_info.rho;
405 image->resolution.y=geometry_info.sigma;
cristy3ed852e2009-09-05 21:47:34 +0000406 if ((flags & SigmaValue) == 0)
cristy2a11bef2011-10-28 18:33:11 +0000407 image->resolution.y=image->resolution.x;
cristy3ed852e2009-09-05 21:47:34 +0000408 }
cristydeb50be2011-11-24 00:19:33 +0000409 if (image_info->density != (char *) NULL)
410 {
411 flags=ParseGeometry(image_info->density,&geometry_info);
412 image->resolution.x=geometry_info.rho;
413 image->resolution.y=geometry_info.sigma;
414 if ((flags & SigmaValue) == 0)
415 image->resolution.y=image->resolution.x;
416 }
417 (void) ParseAbsoluteGeometry(PSPageGeometry,&page);
418 if (image_info->page != (char *) NULL)
419 (void) ParseAbsoluteGeometry(image_info->page,&page);
420 page.width=(size_t) ceil((double) (page.width*image->resolution.x/delta.x)-
421 0.5);
422 page.height=(size_t) ceil((double) (page.height*image->resolution.y/delta.y)-
423 0.5);
cristy3ed852e2009-09-05 21:47:34 +0000424 /*
425 Determine page geometry from the PDF media box.
426 */
427 cmyk=image_info->colorspace == CMYKColorspace ? MagickTrue : MagickFalse;
anthony6f201312012-03-30 04:08:15 +0000428 cropbox=IsStringTrue(GetImageOption(image_info,"pdf:use-cropbox"));
429 trimbox=IsStringTrue(GetImageOption(image_info,"pdf:use-trimbox"));
cristy3ed852e2009-09-05 21:47:34 +0000430 count=0;
431 spotcolor=0;
432 (void) ResetMagickMemory(&bounding_box,0,sizeof(bounding_box));
433 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
434 (void) ResetMagickMemory(&hires_bounds,0,sizeof(hires_bounds));
cristy3ed852e2009-09-05 21:47:34 +0000435 (void) ResetMagickMemory(command,0,sizeof(command));
cristy3ed852e2009-09-05 21:47:34 +0000436 angle=0.0;
437 p=command;
438 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
439 {
440 /*
441 Note PDF elements.
442 */
443 if (c == '\n')
444 c=' ';
445 *p++=(char) c;
cristyfd6e87b2011-02-03 18:48:17 +0000446 if ((c != (int) '/') && (c != (int) '%') &&
cristy3ed852e2009-09-05 21:47:34 +0000447 ((size_t) (p-command) < (MaxTextExtent-1)))
448 continue;
449 *(--p)='\0';
450 p=command;
451 if (LocaleNCompare(PDFRotate,command,strlen(PDFRotate)) == 0)
452 count=(ssize_t) sscanf(command,"Rotate %lf",&angle);
453 /*
454 Is this a CMYK document?
455 */
456 if (LocaleNCompare(DeviceCMYK,command,strlen(DeviceCMYK)) == 0)
457 cmyk=MagickTrue;
458 if (LocaleNCompare(SpotColor,command,strlen(SpotColor)) == 0)
459 {
460 char
461 name[MaxTextExtent],
462 property[MaxTextExtent],
463 *value;
464
cristybb503372010-05-27 20:51:26 +0000465 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000466 i;
467
468 /*
469 Note spot names.
470 */
cristyb51dff52011-05-19 16:55:47 +0000471 (void) FormatLocaleString(property,MaxTextExtent,"pdf:SpotColor-%.20g",
cristye8c25f92010-06-03 00:53:06 +0000472 (double) spotcolor++);
cristy3ed852e2009-09-05 21:47:34 +0000473 i=0;
474 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
475 {
476 if ((isspace(c) != 0) || (c == '/') || ((i+1) == MaxTextExtent))
477 break;
478 name[i++]=(char) c;
479 }
480 name[i]='\0';
481 value=AcquireString(name);
482 (void) SubstituteString(&value,"#20"," ");
cristyd15e6592011-10-15 00:13:06 +0000483 (void) SetImageProperty(image,property,value,exception);
cristy3ed852e2009-09-05 21:47:34 +0000484 value=DestroyString(value);
485 continue;
486 }
487 if (LocaleNCompare(PDFVersion,command,strlen(PDFVersion)) == 0)
cristyd15e6592011-10-15 00:13:06 +0000488 (void) SetImageProperty(image,"pdf:Version",command,exception);
cristydeb50be2011-11-24 00:19:33 +0000489 if (image_info->page != (char *) NULL)
490 continue;
cristy3ed852e2009-09-05 21:47:34 +0000491 count=0;
492 if (cropbox != MagickFalse)
493 {
494 if (LocaleNCompare(CropBox,command,strlen(CropBox)) == 0)
495 {
496 /*
497 Note region defined by crop box.
498 */
499 count=(ssize_t) sscanf(command,"CropBox [%lf %lf %lf %lf",
500 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
501 if (count != 4)
502 count=(ssize_t) sscanf(command,"CropBox[%lf %lf %lf %lf",
503 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
504 }
505 }
506 else
507 if (trimbox != MagickFalse)
508 {
509 if (LocaleNCompare(TrimBox,command,strlen(TrimBox)) == 0)
510 {
511 /*
512 Note region defined by trim box.
513 */
514 count=(ssize_t) sscanf(command,"TrimBox [%lf %lf %lf %lf",
515 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
516 if (count != 4)
517 count=(ssize_t) sscanf(command,"TrimBox[%lf %lf %lf %lf",
518 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
519 }
520 }
521 else
522 if (LocaleNCompare(MediaBox,command,strlen(MediaBox)) == 0)
523 {
524 /*
525 Note region defined by media box.
526 */
527 count=(ssize_t) sscanf(command,"MediaBox [%lf %lf %lf %lf",
528 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
529 if (count != 4)
530 count=(ssize_t) sscanf(command,"MediaBox[%lf %lf %lf %lf",
531 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
532 }
533 if (count != 4)
534 continue;
cristyad29a8c2012-01-25 23:06:43 +0000535 if ((fabs(bounds.x2-bounds.x1) <= fabs(hires_bounds.x2-hires_bounds.x1)) ||
cristyc2038532011-12-29 16:43:05 +0000536 (fabs(bounds.y2-bounds.y1) <= fabs(hires_bounds.y2-hires_bounds.y1)))
537 continue;
cristy48853f12011-11-11 15:47:57 +0000538 hires_bounds=bounds;
cristy3ed852e2009-09-05 21:47:34 +0000539 }
cristy7b0fcf12011-11-24 00:24:07 +0000540 if ((fabs(hires_bounds.x2-hires_bounds.x1) >= MagickEpsilon) &&
cristy614bc082011-11-24 00:49:08 +0000541 (fabs(hires_bounds.y2-hires_bounds.y1) >= MagickEpsilon))
cristy48853f12011-11-11 15:47:57 +0000542 {
543 /*
544 Set PDF render geometry.
545 */
546 (void) FormatLocaleString(geometry,MaxTextExtent,"%gx%g%+.15g%+.15g",
547 hires_bounds.x2-bounds.x1,hires_bounds.y2-hires_bounds.y1,
548 hires_bounds.x1,hires_bounds.y1);
549 (void) SetImageProperty(image,"pdf:HiResBoundingBox",geometry,exception);
cristydeb50be2011-11-24 00:19:33 +0000550 page.width=(size_t) ceil((double) ((hires_bounds.x2-hires_bounds.x1)*
551 image->resolution.x/delta.x)-0.5);
552 page.height=(size_t) ceil((double) ((hires_bounds.y2-hires_bounds.y1)*
553 image->resolution.y/delta.y)-0.5);
cristy48853f12011-11-11 15:47:57 +0000554 }
cristy3ed852e2009-09-05 21:47:34 +0000555 (void) CloseBlob(image);
556 if ((fabs(angle) == 90.0) || (fabs(angle) == 270.0))
557 {
cristybb503372010-05-27 20:51:26 +0000558 size_t
cristy3ed852e2009-09-05 21:47:34 +0000559 swap;
560
561 swap=page.width;
562 page.width=page.height;
563 page.height=swap;
564 }
cristy501c5592012-04-18 12:45:09 +0000565 if (IssRGBColorspace(image_info->colorspace) != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000566 cmyk=MagickFalse;
567 /*
568 Create Ghostscript control file.
569 */
570 file=AcquireUniqueFileResource(postscript_filename);
571 if (file == -1)
572 {
573 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
574 image_info->filename);
575 image=DestroyImage(image);
576 return((Image *) NULL);
577 }
578 count=write(file," ",1);
579 file=close(file)-1;
580 /*
581 Render Postscript with the Ghostscript delegate.
582 */
583 if ((image_info->ping != MagickFalse) ||
584 (image_info->monochrome != MagickFalse))
585 delegate_info=GetDelegateInfo("ps:mono",(char *) NULL,exception);
586 else
587 if (cmyk != MagickFalse)
588 delegate_info=GetDelegateInfo("ps:cmyk",(char *) NULL,exception);
589 else
cristya97426c2011-02-04 01:41:27 +0000590 delegate_info=GetDelegateInfo("ps:alpha",(char *) NULL,exception);
cristy3ed852e2009-09-05 21:47:34 +0000591 if (delegate_info == (const DelegateInfo *) NULL)
592 {
593 (void) RelinquishUniqueFileResource(postscript_filename);
594 image=DestroyImage(image);
595 return((Image *) NULL);
596 }
597 *options='\0';
cristy2a11bef2011-10-28 18:33:11 +0000598 (void) FormatLocaleString(density,MaxTextExtent,"%gx%g",image->resolution.x,
599 image->resolution.y);
cristyf58e2732012-02-13 15:50:15 +0000600 if (image_info->page != (char *) NULL)
601 (void) FormatLocaleString(options,MaxTextExtent,"-g%.20gx%.20g ",(double)
602 page.width,(double) page.height);
cristy3ed852e2009-09-05 21:47:34 +0000603 if (cmyk != MagickFalse)
604 (void) ConcatenateMagickString(options,"-dUseCIEColor ",MaxTextExtent);
605 if (cropbox != MagickFalse)
606 (void) ConcatenateMagickString(options,"-dUseCropBox ",MaxTextExtent);
607 if (trimbox != MagickFalse)
608 (void) ConcatenateMagickString(options,"-dUseTrimBox ",MaxTextExtent);
609 read_info=CloneImageInfo(image_info);
610 *read_info->magick='\0';
611 if (read_info->number_scenes != 0)
612 {
613 char
614 pages[MaxTextExtent];
615
cristyb51dff52011-05-19 16:55:47 +0000616 (void) FormatLocaleString(pages,MaxTextExtent,"-dFirstPage=%.20g "
cristye8c25f92010-06-03 00:53:06 +0000617 "-dLastPage=%.20g",(double) read_info->scene+1,(double)
cristyf2faecf2010-05-28 19:19:36 +0000618 (read_info->scene+read_info->number_scenes));
cristy3ed852e2009-09-05 21:47:34 +0000619 (void) ConcatenateMagickString(options,pages,MaxTextExtent);
620 read_info->number_scenes=0;
621 if (read_info->scenes != (char *) NULL)
622 *read_info->scenes='\0';
623 }
anthony1afdc7a2011-10-05 11:54:28 +0000624 option=GetImageOption(read_info,"authenticate");
625 if (option != (const char *) NULL)
cristyb51dff52011-05-19 16:55:47 +0000626 (void) FormatLocaleString(options+strlen(options),MaxTextExtent,
anthony1afdc7a2011-10-05 11:54:28 +0000627 " -sPCLPassword=%s",option);
cristy3ed852e2009-09-05 21:47:34 +0000628 (void) CopyMagickString(filename,read_info->filename,MaxTextExtent);
cristya97426c2011-02-04 01:41:27 +0000629 (void) AcquireUniqueFilename(filename);
630 (void) ConcatenateMagickString(filename,"-%08d",MaxTextExtent);
cristyb51dff52011-05-19 16:55:47 +0000631 (void) FormatLocaleString(command,MaxTextExtent,
cristy3ed852e2009-09-05 21:47:34 +0000632 GetDelegateCommands(delegate_info),
633 read_info->antialias != MagickFalse ? 4 : 1,
cristya97426c2011-02-04 01:41:27 +0000634 read_info->antialias != MagickFalse ? 4 : 1,density,options,filename,
635 postscript_filename,input_filename);
cristydefb3f02009-09-10 02:18:35 +0000636 status=InvokePDFDelegate(read_info->verbose,command,exception);
cristy3ed852e2009-09-05 21:47:34 +0000637 (void) RelinquishUniqueFileResource(postscript_filename);
cristy3ed852e2009-09-05 21:47:34 +0000638 (void) RelinquishUniqueFileResource(input_filename);
cristya97426c2011-02-04 01:41:27 +0000639 pdf_image=(Image *) NULL;
640 if (status == MagickFalse)
641 for (i=1; ; i++)
642 {
cristya97426c2011-02-04 01:41:27 +0000643 (void) InterpretImageFilename(image_info,image,filename,(int) i,
cristy6fccee12011-10-20 18:43:18 +0000644 read_info->filename,exception);
cristya97426c2011-02-04 01:41:27 +0000645 if (IsPDFRendered(read_info->filename) == MagickFalse)
646 break;
647 (void) RelinquishUniqueFileResource(read_info->filename);
648 }
649 else
650 for (i=1; ; i++)
651 {
cristya97426c2011-02-04 01:41:27 +0000652 (void) InterpretImageFilename(image_info,image,filename,(int) i,
cristy6fccee12011-10-20 18:43:18 +0000653 read_info->filename,exception);
cristya97426c2011-02-04 01:41:27 +0000654 if (IsPDFRendered(read_info->filename) == MagickFalse)
655 break;
cristyb0ffea22012-06-17 20:22:01 +0000656 read_info->blob=NULL;
657 read_info->length=0;
cristya97426c2011-02-04 01:41:27 +0000658 next=ReadImage(read_info,exception);
659 (void) RelinquishUniqueFileResource(read_info->filename);
660 if (next == (Image *) NULL)
661 break;
662 AppendImageToList(&pdf_image,next);
663 }
cristy3ed852e2009-09-05 21:47:34 +0000664 read_info=DestroyImageInfo(read_info);
665 if (pdf_image == (Image *) NULL)
666 {
667 ThrowFileException(exception,DelegateError,"PostscriptDelegateFailed",
668 image_info->filename);
cristyb6cabe82011-03-18 13:25:59 +0000669 image=DestroyImage(image);
cristy3ed852e2009-09-05 21:47:34 +0000670 return((Image *) NULL);
671 }
672 if (LocaleCompare(pdf_image->magick,"BMP") == 0)
673 {
674 Image
675 *cmyk_image;
676
677 cmyk_image=ConsolidateCMYKImages(pdf_image,exception);
678 if (cmyk_image != (Image *) NULL)
679 {
680 pdf_image=DestroyImageList(pdf_image);
681 pdf_image=cmyk_image;
682 }
683 }
684 if (image_info->number_scenes != 0)
685 {
686 Image
687 *clone_image;
688
cristybb503372010-05-27 20:51:26 +0000689 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000690 i;
691
692 /*
693 Add place holder images to meet the subimage specification requirement.
694 */
cristybb503372010-05-27 20:51:26 +0000695 for (i=0; i < (ssize_t) image_info->scene; i++)
cristy3ed852e2009-09-05 21:47:34 +0000696 {
697 clone_image=CloneImage(pdf_image,1,1,MagickTrue,exception);
698 if (clone_image != (Image *) NULL)
699 PrependImageToList(&pdf_image,clone_image);
700 }
701 }
702 do
703 {
704 (void) CopyMagickString(pdf_image->filename,filename,MaxTextExtent);
705 pdf_image->page=page;
706 (void) CloneImageProfiles(pdf_image,image);
707 (void) CloneImageProperties(pdf_image,image);
708 next=SyncNextImageInList(pdf_image);
709 if (next != (Image *) NULL)
710 pdf_image=next;
711 } while (next != (Image *) NULL);
712 image=DestroyImage(image);
713 scene=0;
714 for (next=GetFirstImageInList(pdf_image); next != (Image *) NULL; )
715 {
716 next->scene=scene++;
717 next=GetNextImageInList(next);
718 }
719 return(GetFirstImageInList(pdf_image));
720}
721
722/*
723%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
724% %
725% %
726% %
727% R e g i s t e r P D F I m a g e %
728% %
729% %
730% %
731%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
732%
733% RegisterPDFImage() adds properties for the PDF image format to
734% the list of supported formats. The properties include the image format
735% tag, a method to read and/or write the format, whether the format
736% supports the saving of more than one frame to the same file or blob,
737% whether the format supports native in-memory I/O, and a brief
738% description of the format.
739%
740% The format of the RegisterPDFImage method is:
741%
cristybb503372010-05-27 20:51:26 +0000742% size_t RegisterPDFImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000743%
744*/
cristybb503372010-05-27 20:51:26 +0000745ModuleExport size_t RegisterPDFImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000746{
747 MagickInfo
748 *entry;
749
750 entry=SetMagickInfo("AI");
751 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
752 entry->encoder=(EncodeImageHandler *) WritePDFImage;
753 entry->adjoin=MagickFalse;
754 entry->blob_support=MagickFalse;
755 entry->seekable_stream=MagickTrue;
756 entry->thread_support=EncoderThreadSupport;
757 entry->description=ConstantString("Adobe Illustrator CS2");
758 entry->module=ConstantString("PDF");
759 (void) RegisterMagickInfo(entry);
760 entry=SetMagickInfo("EPDF");
761 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
762 entry->encoder=(EncodeImageHandler *) WritePDFImage;
763 entry->adjoin=MagickFalse;
764 entry->blob_support=MagickFalse;
765 entry->seekable_stream=MagickTrue;
766 entry->thread_support=EncoderThreadSupport;
767 entry->description=ConstantString("Encapsulated Portable Document Format");
768 entry->module=ConstantString("PDF");
769 (void) RegisterMagickInfo(entry);
770 entry=SetMagickInfo("PDF");
771 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
772 entry->encoder=(EncodeImageHandler *) WritePDFImage;
773 entry->magick=(IsImageFormatHandler *) IsPDF;
774 entry->blob_support=MagickFalse;
775 entry->seekable_stream=MagickTrue;
776 entry->thread_support=EncoderThreadSupport;
777 entry->description=ConstantString("Portable Document Format");
778 entry->module=ConstantString("PDF");
779 (void) RegisterMagickInfo(entry);
780 entry=SetMagickInfo("PDFA");
781 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
782 entry->encoder=(EncodeImageHandler *) WritePDFImage;
783 entry->magick=(IsImageFormatHandler *) IsPDF;
784 entry->blob_support=MagickFalse;
785 entry->seekable_stream=MagickTrue;
786 entry->thread_support=EncoderThreadSupport;
787 entry->description=ConstantString("Portable Document Archive Format");
788 entry->module=ConstantString("PDF");
789 (void) RegisterMagickInfo(entry);
790 return(MagickImageCoderSignature);
791}
792
793/*
794%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
795% %
796% %
797% %
798% U n r e g i s t e r P D F I m a g e %
799% %
800% %
801% %
802%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
803%
804% UnregisterPDFImage() removes format registrations made by the
805% PDF module from the list of supported formats.
806%
807% The format of the UnregisterPDFImage method is:
808%
809% UnregisterPDFImage(void)
810%
811*/
812ModuleExport void UnregisterPDFImage(void)
813{
814 (void) UnregisterMagickInfo("AI");
815 (void) UnregisterMagickInfo("EPDF");
816 (void) UnregisterMagickInfo("PDF");
817 (void) UnregisterMagickInfo("PDFA");
818}
819
820/*
821%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
822% %
823% %
824% %
825% W r i t e P D F I m a g e %
826% %
827% %
828% %
829%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
830%
831% WritePDFImage() writes an image in the Portable Document image
832% format.
833%
834% The format of the WritePDFImage method is:
835%
cristy1e178e72011-08-28 19:44:34 +0000836% MagickBooleanType WritePDFImage(const ImageInfo *image_info,
837% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000838%
839% A description of each parameter follows.
840%
841% o image_info: the image info.
842%
843% o image: The image.
844%
cristy1e178e72011-08-28 19:44:34 +0000845% o exception: return any errors or warnings in this structure.
846%
cristy3ed852e2009-09-05 21:47:34 +0000847*/
848
849static inline size_t MagickMax(const size_t x,const size_t y)
850{
851 if (x > y)
852 return(x);
853 return(y);
854}
855
856static inline size_t MagickMin(const size_t x,const size_t y)
857{
858 if (x < y)
859 return(x);
860 return(y);
861}
862
863static char *EscapeParenthesis(const char *text)
864{
865 register char
866 *p;
867
cristybb503372010-05-27 20:51:26 +0000868 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000869 i;
870
cristybb503372010-05-27 20:51:26 +0000871 size_t
cristy3ed852e2009-09-05 21:47:34 +0000872 escapes;
873
cristyaff6d802011-04-26 01:46:31 +0000874 static char
875 buffer[MaxTextExtent];
876
cristy3ed852e2009-09-05 21:47:34 +0000877 escapes=0;
878 p=buffer;
cristybb503372010-05-27 20:51:26 +0000879 for (i=0; i < (ssize_t) MagickMin(strlen(text),(MaxTextExtent-escapes-1)); i++)
cristy3ed852e2009-09-05 21:47:34 +0000880 {
881 if ((text[i] == '(') || (text[i] == ')'))
882 {
883 *p++='\\';
884 escapes++;
885 }
886 *p++=text[i];
887 }
888 *p='\0';
889 return(buffer);
890}
891
cristy47b838c2009-09-19 16:09:30 +0000892static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
cristy018f07f2011-09-04 21:15:19 +0000893 Image *image,Image *inject_image,ExceptionInfo *exception)
cristy47b838c2009-09-19 16:09:30 +0000894{
cristy47b838c2009-09-19 16:09:30 +0000895 Image
cristy80975862009-09-25 14:34:31 +0000896 *group4_image;
cristy47b838c2009-09-19 16:09:30 +0000897
898 ImageInfo
899 *write_info;
900
cristy47b838c2009-09-19 16:09:30 +0000901 MagickBooleanType
902 status;
903
cristy80975862009-09-25 14:34:31 +0000904 size_t
905 length;
cristy47b838c2009-09-19 16:09:30 +0000906
907 unsigned char
cristy80975862009-09-25 14:34:31 +0000908 *group4;
cristy47b838c2009-09-19 16:09:30 +0000909
cristy42751fe2009-10-05 00:15:50 +0000910 status=MagickTrue;
cristy47b838c2009-09-19 16:09:30 +0000911 write_info=CloneImageInfo(image_info);
cristy80975862009-09-25 14:34:31 +0000912 (void) CopyMagickString(write_info->filename,"GROUP4:",MaxTextExtent);
913 (void) CopyMagickString(write_info->magick,"GROUP4",MaxTextExtent);
cristy018f07f2011-09-04 21:15:19 +0000914 group4_image=CloneImage(inject_image,0,0,MagickTrue,exception);
cristy80975862009-09-25 14:34:31 +0000915 if (group4_image == (Image *) NULL)
916 return(MagickFalse);
917 group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
cristy018f07f2011-09-04 21:15:19 +0000918 exception);
cristy80975862009-09-25 14:34:31 +0000919 group4_image=DestroyImage(group4_image);
920 if (group4 == (unsigned char *) NULL)
921 return(MagickFalse);
cristy47b838c2009-09-19 16:09:30 +0000922 write_info=DestroyImageInfo(write_info);
cristy80975862009-09-25 14:34:31 +0000923 if (WriteBlob(image,length,group4) != (ssize_t) length)
924 status=MagickFalse;
925 group4=(unsigned char *) RelinquishMagickMemory(group4);
926 return(status);
cristy47b838c2009-09-19 16:09:30 +0000927}
928
cristy1e178e72011-08-28 19:44:34 +0000929static MagickBooleanType WritePDFImage(const ImageInfo *image_info,Image *image,
930 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000931{
932#define CFormat "/Filter [ /%s ]\n"
933#define ObjectsPerImage 14
934
935 static const char
936 XMPProfile[]=
937 {
938 "<?xpacket begin=\"%s\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n"
939 "<x:xmpmeta xmlns:x=\"adobe:ns:meta/\" x:xmptk=\"Adobe XMP Core 4.0-c316 44.253921, Sun Oct 01 2006 17:08:23\">\n"
940 " <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n"
941 " <rdf:Description rdf:about=\"\"\n"
942 " xmlns:xap=\"http://ns.adobe.com/xap/1.0/\">\n"
943 " <xap:ModifyDate>%s</xap:ModifyDate>\n"
944 " <xap:CreateDate>%s</xap:CreateDate>\n"
945 " <xap:MetadataDate>%s</xap:MetadataDate>\n"
946 " <xap:CreatorTool>%s</xap:CreatorTool>\n"
947 " </rdf:Description>\n"
948 " <rdf:Description rdf:about=\"\"\n"
949 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"
950 " <dc:format>application/pdf</dc:format>\n"
951 " </rdf:Description>\n"
952 " <rdf:Description rdf:about=\"\"\n"
953 " xmlns:xapMM=\"http://ns.adobe.com/xap/1.0/mm/\">\n"
954 " <xapMM:DocumentID>uuid:6ec119d7-7982-4f56-808d-dfe64f5b35cf</xapMM:DocumentID>\n"
955 " <xapMM:InstanceID>uuid:a79b99b4-6235-447f-9f6c-ec18ef7555cb</xapMM:InstanceID>\n"
956 " </rdf:Description>\n"
957 " <rdf:Description rdf:about=\"\"\n"
958 " xmlns:pdf=\"http://ns.adobe.com/pdf/1.3/\">\n"
959 " <pdf:Producer>%s</pdf:Producer>\n"
960 " </rdf:Description>\n"
961 " <rdf:Description rdf:about=\"\"\n"
962 " xmlns:pdfaid=\"http://www.aiim.org/pdfa/ns/id/\">\n"
963 " <pdfaid:part>1</pdfaid:part>\n"
964 " <pdfaid:conformance>B</pdfaid:conformance>\n"
965 " </rdf:Description>\n"
966 " </rdf:RDF>\n"
967 "</x:xmpmeta>\n"
968 "<?xpacket end=\"w\"?>\n"
969 },
970 XMPProfileMagick[4]= { (char) 0xef, (char) 0xbb, (char) 0xbf, (char) 0x00 };
971
972 char
cristy57015272010-12-30 01:16:38 +0000973 basename[MaxTextExtent],
cristy3ed852e2009-09-05 21:47:34 +0000974 buffer[MaxTextExtent],
975 date[MaxTextExtent],
976 **labels,
977 page_geometry[MaxTextExtent];
978
979 CompressionType
980 compression;
981
982 const char
983 *value;
984
985 double
986 pointsize;
987
988 GeometryInfo
989 geometry_info;
990
cristy3ed852e2009-09-05 21:47:34 +0000991 Image
992 *next,
993 *tile_image;
994
995 MagickBooleanType
996 status;
997
998 MagickOffsetType
999 offset,
1000 scene,
1001 *xref;
1002
1003 MagickSizeType
1004 number_pixels;
1005
1006 MagickStatusType
1007 flags;
1008
1009 PointInfo
1010 delta,
1011 resolution,
1012 scale;
1013
1014 RectangleInfo
1015 geometry,
1016 media_info,
1017 page_info;
1018
cristy4c08aed2011-07-01 19:47:50 +00001019 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +00001020 *p;
1021
1022 register unsigned char
1023 *q;
1024
cristybb503372010-05-27 20:51:26 +00001025 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001026 i,
1027 x;
1028
1029 size_t
cristyaff6d802011-04-26 01:46:31 +00001030 info_id,
1031 length,
1032 object,
1033 pages_id,
1034 root_id,
1035 text_size,
1036 version;
1037
1038 ssize_t
1039 count,
1040 y;
cristy3ed852e2009-09-05 21:47:34 +00001041
1042 struct tm
1043 local_time;
1044
1045 time_t
1046 seconds;
1047
1048 unsigned char
1049 *pixels;
1050
cristy3ed852e2009-09-05 21:47:34 +00001051 /*
1052 Open output image file.
1053 */
1054 assert(image_info != (const ImageInfo *) NULL);
1055 assert(image_info->signature == MagickSignature);
1056 assert(image != (Image *) NULL);
1057 assert(image->signature == MagickSignature);
1058 if (image->debug != MagickFalse)
1059 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +00001060 assert(exception != (ExceptionInfo *) NULL);
1061 assert(exception->signature == MagickSignature);
cristy1e178e72011-08-28 19:44:34 +00001062 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +00001063 if (status == MagickFalse)
1064 return(status);
1065 /*
1066 Allocate X ref memory.
1067 */
1068 xref=(MagickOffsetType *) AcquireQuantumMemory(2048UL,sizeof(*xref));
1069 if (xref == (MagickOffsetType *) NULL)
1070 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1071 (void) ResetMagickMemory(xref,0,2048UL*sizeof(*xref));
1072 /*
1073 Write Info object.
1074 */
1075 object=0;
1076 version=3;
1077 if (image_info->compression == JPEG2000Compression)
cristybb503372010-05-27 20:51:26 +00001078 version=(size_t) MagickMax(version,5);
cristy3ed852e2009-09-05 21:47:34 +00001079 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
1080 if (next->matte != MagickFalse)
cristybb503372010-05-27 20:51:26 +00001081 version=(size_t) MagickMax(version,4);
cristy3ed852e2009-09-05 21:47:34 +00001082 if (LocaleCompare(image_info->magick,"PDFA") == 0)
cristyc04c60e2011-05-31 23:58:45 +00001083 version=(size_t) MagickMax(version,6);
cristyf40010e2012-05-13 13:52:20 +00001084 (void) FormatLocaleString(buffer,MaxTextExtent,"%%PDF-1.%.20g \n",(double)
1085 version);
cristy3ed852e2009-09-05 21:47:34 +00001086 (void) WriteBlobString(image,buffer);
1087 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1088 (void) WriteBlobString(image,"%âãÏÓ\n");
1089 /*
1090 Write Catalog object.
1091 */
1092 xref[object++]=TellBlob(image);
1093 root_id=object;
cristyb51dff52011-05-19 16:55:47 +00001094 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristye8c25f92010-06-03 00:53:06 +00001095 object);
cristy3ed852e2009-09-05 21:47:34 +00001096 (void) WriteBlobString(image,buffer);
1097 (void) WriteBlobString(image,"<<\n");
1098 if (LocaleCompare(image_info->magick,"PDFA") != 0)
cristyf40010e2012-05-13 13:52:20 +00001099 (void) FormatLocaleString(buffer,MaxTextExtent,"/Pages %.20g 0 R\n",(double)
1100 object+1);
cristy3ed852e2009-09-05 21:47:34 +00001101 else
1102 {
cristyb51dff52011-05-19 16:55:47 +00001103 (void) FormatLocaleString(buffer,MaxTextExtent,"/Metadata %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001104 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001105 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001106 (void) FormatLocaleString(buffer,MaxTextExtent,"/Pages %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001107 (double) object+2);
cristy3ed852e2009-09-05 21:47:34 +00001108 }
1109 (void) WriteBlobString(image,buffer);
1110 (void) WriteBlobString(image,"/Type /Catalog\n");
1111 (void) WriteBlobString(image,">>\n");
1112 (void) WriteBlobString(image,"endobj\n");
1113 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1114 {
1115 char
1116 create_date[MaxTextExtent],
1117 modify_date[MaxTextExtent],
1118 timestamp[MaxTextExtent],
1119 xmp_profile[MaxTextExtent];
1120
cristybb503372010-05-27 20:51:26 +00001121 size_t
cristy3ed852e2009-09-05 21:47:34 +00001122 version;
1123
1124 /*
1125 Write XMP object.
1126 */
1127 xref[object++]=TellBlob(image);
cristyf40010e2012-05-13 13:52:20 +00001128 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1129 object);
cristy3ed852e2009-09-05 21:47:34 +00001130 (void) WriteBlobString(image,buffer);
1131 (void) WriteBlobString(image,"<<\n");
1132 (void) WriteBlobString(image,"/Subtype /XML\n");
1133 *modify_date='\0';
cristyd15e6592011-10-15 00:13:06 +00001134 value=GetImageProperty(image,"date:modify",exception);
cristy3ed852e2009-09-05 21:47:34 +00001135 if (value != (const char *) NULL)
1136 (void) CopyMagickString(modify_date,value,MaxTextExtent);
1137 *create_date='\0';
cristyd15e6592011-10-15 00:13:06 +00001138 value=GetImageProperty(image,"date:create",exception);
cristy3ed852e2009-09-05 21:47:34 +00001139 if (value != (const char *) NULL)
1140 (void) CopyMagickString(create_date,value,MaxTextExtent);
1141 (void) FormatMagickTime(time((time_t *) NULL),MaxTextExtent,timestamp);
cristyb51dff52011-05-19 16:55:47 +00001142 i=FormatLocaleString(xmp_profile,MaxTextExtent,XMPProfile,
cristy3ed852e2009-09-05 21:47:34 +00001143 XMPProfileMagick,modify_date,create_date,timestamp,
1144 GetMagickVersion(&version),GetMagickVersion(&version));
cristyf40010e2012-05-13 13:52:20 +00001145 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g\n",(double)
1146 i);
cristy3ed852e2009-09-05 21:47:34 +00001147 (void) WriteBlobString(image,buffer);
1148 (void) WriteBlobString(image,"/Type /Metadata\n");
1149 (void) WriteBlobString(image,">>\nstream\n");
1150 (void) WriteBlobString(image,xmp_profile);
1151 (void) WriteBlobString(image,"endstream\n");
1152 (void) WriteBlobString(image,"endobj\n");
1153 }
1154 /*
1155 Write Pages object.
1156 */
1157 xref[object++]=TellBlob(image);
1158 pages_id=object;
cristyb51dff52011-05-19 16:55:47 +00001159 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001160 object);
cristy3ed852e2009-09-05 21:47:34 +00001161 (void) WriteBlobString(image,buffer);
1162 (void) WriteBlobString(image,"<<\n");
1163 (void) WriteBlobString(image,"/Type /Pages\n");
cristyf40010e2012-05-13 13:52:20 +00001164 (void) FormatLocaleString(buffer,MaxTextExtent,"/Kids [ %.20g 0 R ",(double)
1165 object+1);
cristy3ed852e2009-09-05 21:47:34 +00001166 (void) WriteBlobString(image,buffer);
cristybb503372010-05-27 20:51:26 +00001167 count=(ssize_t) (pages_id+ObjectsPerImage+1);
cristy3ed852e2009-09-05 21:47:34 +00001168 if (image_info->adjoin != MagickFalse)
1169 {
1170 Image
1171 *kid_image;
1172
1173 /*
1174 Predict page object id's.
1175 */
1176 kid_image=image;
1177 for ( ; GetNextImageInList(kid_image) != (Image *) NULL; count+=ObjectsPerImage)
1178 {
cristyb51dff52011-05-19 16:55:47 +00001179 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 R ",(double)
cristye8c25f92010-06-03 00:53:06 +00001180 count);
cristy3ed852e2009-09-05 21:47:34 +00001181 (void) WriteBlobString(image,buffer);
1182 kid_image=GetNextImageInList(kid_image);
1183 }
1184 xref=(MagickOffsetType *) ResizeQuantumMemory(xref,(size_t) count+2048UL,
1185 sizeof(*xref));
1186 if (xref == (MagickOffsetType *) NULL)
1187 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1188 }
1189 (void) WriteBlobString(image,"]\n");
cristyf40010e2012-05-13 13:52:20 +00001190 (void) FormatLocaleString(buffer,MaxTextExtent,"/Count %.20g\n",(double)
1191 ((count-pages_id)/ObjectsPerImage));
cristy3ed852e2009-09-05 21:47:34 +00001192 (void) WriteBlobString(image,buffer);
1193 (void) WriteBlobString(image,">>\n");
1194 (void) WriteBlobString(image,"endobj\n");
1195 scene=0;
1196 do
1197 {
1198 compression=image->compression;
1199 if (image_info->compression != UndefinedCompression)
1200 compression=image_info->compression;
1201 switch (compression)
1202 {
1203 case FaxCompression:
1204 case Group4Compression:
1205 {
cristy1e178e72011-08-28 19:44:34 +00001206 if ((IsImageMonochrome(image,exception) == MagickFalse) ||
cristy3ed852e2009-09-05 21:47:34 +00001207 (image->matte != MagickFalse))
1208 compression=RLECompression;
1209 break;
1210 }
1211#if !defined(MAGICKCORE_JPEG_DELEGATE)
1212 case JPEGCompression:
1213 {
1214 compression=RLECompression;
cristy1e178e72011-08-28 19:44:34 +00001215 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00001216 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
1217 image->filename);
1218 break;
1219 }
1220#endif
1221#if !defined(MAGICKCORE_JP2_DELEGATE)
1222 case JPEG2000Compression:
1223 {
1224 compression=RLECompression;
cristy1e178e72011-08-28 19:44:34 +00001225 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00001226 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JP2)",
1227 image->filename);
1228 break;
1229 }
1230#endif
1231#if !defined(MAGICKCORE_ZLIB_DELEGATE)
1232 case ZipCompression:
1233 {
1234 compression=RLECompression;
cristy1e178e72011-08-28 19:44:34 +00001235 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00001236 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZLIB)",
1237 image->filename);
1238 break;
1239 }
1240#endif
1241 case LZWCompression:
1242 {
1243 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1244 compression=RLECompression; /* LZW compression is forbidden */
1245 break;
1246 }
1247 case NoCompression:
1248 {
1249 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1250 compression=RLECompression; /* ASCII 85 compression is forbidden */
1251 break;
1252 }
1253 default:
1254 break;
1255 }
1256 if (compression == JPEG2000Compression)
1257 {
cristy501c5592012-04-18 12:45:09 +00001258 if (IssRGBColorspace(image->colorspace) == MagickFalse)
cristy8d951092012-02-08 18:54:56 +00001259 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00001260 }
1261 /*
1262 Scale relative to dots-per-inch.
1263 */
1264 delta.x=DefaultResolution;
1265 delta.y=DefaultResolution;
cristy2a11bef2011-10-28 18:33:11 +00001266 resolution.x=image->resolution.x;
1267 resolution.y=image->resolution.y;
cristy3ed852e2009-09-05 21:47:34 +00001268 if ((resolution.x == 0.0) || (resolution.y == 0.0))
1269 {
1270 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
1271 resolution.x=geometry_info.rho;
1272 resolution.y=geometry_info.sigma;
1273 if ((flags & SigmaValue) == 0)
1274 resolution.y=resolution.x;
1275 }
1276 if (image_info->density != (char *) NULL)
1277 {
1278 flags=ParseGeometry(image_info->density,&geometry_info);
1279 resolution.x=geometry_info.rho;
1280 resolution.y=geometry_info.sigma;
1281 if ((flags & SigmaValue) == 0)
1282 resolution.y=resolution.x;
1283 }
1284 if (image->units == PixelsPerCentimeterResolution)
1285 {
cristya97426c2011-02-04 01:41:27 +00001286 resolution.x=(double) ((size_t) (100.0*2.54*resolution.x+0.5)/100.0);
1287 resolution.y=(double) ((size_t) (100.0*2.54*resolution.y+0.5)/100.0);
cristy3ed852e2009-09-05 21:47:34 +00001288 }
1289 SetGeometry(image,&geometry);
cristyf40010e2012-05-13 13:52:20 +00001290 (void) FormatLocaleString(page_geometry,MaxTextExtent,"%.20gx%.20g",(double)
1291 image->columns,(double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001292 if (image_info->page != (char *) NULL)
1293 (void) CopyMagickString(page_geometry,image_info->page,MaxTextExtent);
1294 else
1295 if ((image->page.width != 0) && (image->page.height != 0))
cristyb51dff52011-05-19 16:55:47 +00001296 (void) FormatLocaleString(page_geometry,MaxTextExtent,
cristyf40010e2012-05-13 13:52:20 +00001297 "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,(double)
1298 image->page.height,(double) image->page.x,(double) image->page.y);
cristy3ed852e2009-09-05 21:47:34 +00001299 else
1300 if ((image->gravity != UndefinedGravity) &&
1301 (LocaleCompare(image_info->magick,"PDF") == 0))
1302 (void) CopyMagickString(page_geometry,PSPageGeometry,MaxTextExtent);
1303 (void) ConcatenateMagickString(page_geometry,">",MaxTextExtent);
1304 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
1305 &geometry.width,&geometry.height);
1306 scale.x=(double) (geometry.width*delta.x)/resolution.x;
cristybb503372010-05-27 20:51:26 +00001307 geometry.width=(size_t) floor(scale.x+0.5);
cristy3ed852e2009-09-05 21:47:34 +00001308 scale.y=(double) (geometry.height*delta.y)/resolution.y;
cristybb503372010-05-27 20:51:26 +00001309 geometry.height=(size_t) floor(scale.y+0.5);
cristy3ed852e2009-09-05 21:47:34 +00001310 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
cristy1e178e72011-08-28 19:44:34 +00001311 (void) ParseGravityGeometry(image,page_geometry,&page_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00001312 if (image->gravity != UndefinedGravity)
1313 {
1314 geometry.x=(-page_info.x);
cristybb503372010-05-27 20:51:26 +00001315 geometry.y=(ssize_t) (media_info.height+page_info.y-image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001316 }
1317 pointsize=12.0;
1318 if (image_info->pointsize != 0.0)
1319 pointsize=image_info->pointsize;
1320 text_size=0;
cristyd15e6592011-10-15 00:13:06 +00001321 value=GetImageProperty(image,"label",exception);
cristy3ed852e2009-09-05 21:47:34 +00001322 if (value != (const char *) NULL)
cristybb503372010-05-27 20:51:26 +00001323 text_size=(size_t) (MultilineCensus(value)*pointsize+12);
cristyda16f162011-02-19 23:52:17 +00001324 (void) text_size;
cristy3ed852e2009-09-05 21:47:34 +00001325 /*
1326 Write Page object.
1327 */
1328 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001329 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001330 object);
cristy3ed852e2009-09-05 21:47:34 +00001331 (void) WriteBlobString(image,buffer);
1332 (void) WriteBlobString(image,"<<\n");
1333 (void) WriteBlobString(image,"/Type /Page\n");
cristyb51dff52011-05-19 16:55:47 +00001334 (void) FormatLocaleString(buffer,MaxTextExtent,"/Parent %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001335 (double) pages_id);
cristy3ed852e2009-09-05 21:47:34 +00001336 (void) WriteBlobString(image,buffer);
1337 (void) WriteBlobString(image,"/Resources <<\n");
1338 labels=(char **) NULL;
cristyd15e6592011-10-15 00:13:06 +00001339 value=GetImageProperty(image,"label",exception);
cristy3ed852e2009-09-05 21:47:34 +00001340 if (value != (const char *) NULL)
1341 labels=StringToList(value);
1342 if (labels != (char **) NULL)
1343 {
cristyb51dff52011-05-19 16:55:47 +00001344 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00001345 "/Font << /F%.20g %.20g 0 R >>\n",(double) image->scene,(double)
1346 object+4);
cristy3ed852e2009-09-05 21:47:34 +00001347 (void) WriteBlobString(image,buffer);
1348 }
cristyb51dff52011-05-19 16:55:47 +00001349 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00001350 "/XObject << /Im%.20g %.20g 0 R >>\n",(double) image->scene,(double)
1351 object+5);
cristy3ed852e2009-09-05 21:47:34 +00001352 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001353 (void) FormatLocaleString(buffer,MaxTextExtent,"/ProcSet %.20g 0 R >>\n",
cristye8c25f92010-06-03 00:53:06 +00001354 (double) object+3);
cristy3ed852e2009-09-05 21:47:34 +00001355 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001356 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00001357 "/MediaBox [0 0 %g %g]\n",72.0*media_info.width/resolution.x,
cristy8cd5b312010-01-07 01:10:24 +00001358 72.0*media_info.height/resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001359 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001360 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00001361 "/CropBox [0 0 %g %g]\n",72.0*media_info.width/resolution.x,
cristy8cd5b312010-01-07 01:10:24 +00001362 72.0*media_info.height/resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001363 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001364 (void) FormatLocaleString(buffer,MaxTextExtent,"/Contents %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001365 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001366 (void) WriteBlobString(image,buffer);
cristyf40010e2012-05-13 13:52:20 +00001367 (void) FormatLocaleString(buffer,MaxTextExtent,"/Thumb %.20g 0 R\n",(double)
1368 object+8);
cristy3ed852e2009-09-05 21:47:34 +00001369 (void) WriteBlobString(image,buffer);
1370 (void) WriteBlobString(image,">>\n");
1371 (void) WriteBlobString(image,"endobj\n");
1372 /*
1373 Write Contents object.
1374 */
1375 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001376 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001377 object);
cristy3ed852e2009-09-05 21:47:34 +00001378 (void) WriteBlobString(image,buffer);
1379 (void) WriteBlobString(image,"<<\n");
cristyb51dff52011-05-19 16:55:47 +00001380 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001381 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001382 (void) WriteBlobString(image,buffer);
1383 (void) WriteBlobString(image,">>\n");
1384 (void) WriteBlobString(image,"stream\n");
1385 offset=TellBlob(image);
1386 (void) WriteBlobString(image,"q\n");
1387 if (labels != (char **) NULL)
1388 for (i=0; labels[i] != (char *) NULL; i++)
1389 {
1390 (void) WriteBlobString(image,"BT\n");
cristyb51dff52011-05-19 16:55:47 +00001391 (void) FormatLocaleString(buffer,MaxTextExtent,"/F%.20g %g Tf\n",
cristye8c25f92010-06-03 00:53:06 +00001392 (double) image->scene,pointsize);
cristy3ed852e2009-09-05 21:47:34 +00001393 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001394 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g Td\n",
cristye8c25f92010-06-03 00:53:06 +00001395 (double) geometry.x,(double) (geometry.y+geometry.height+i*pointsize+
1396 12));
cristy3ed852e2009-09-05 21:47:34 +00001397 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001398 (void) FormatLocaleString(buffer,MaxTextExtent,"(%s) Tj\n",labels[i]);
cristy3ed852e2009-09-05 21:47:34 +00001399 (void) WriteBlobString(image,buffer);
1400 (void) WriteBlobString(image,"ET\n");
1401 labels[i]=DestroyString(labels[i]);
1402 }
cristyb51dff52011-05-19 16:55:47 +00001403 (void) FormatLocaleString(buffer,MaxTextExtent,"%g 0 0 %g %.20g %.20g cm\n",
cristye8c25f92010-06-03 00:53:06 +00001404 scale.x,scale.y,(double) geometry.x,(double) geometry.y);
cristy3ed852e2009-09-05 21:47:34 +00001405 (void) WriteBlobString(image,buffer);
cristyf40010e2012-05-13 13:52:20 +00001406 (void) FormatLocaleString(buffer,MaxTextExtent,"/Im%.20g Do\n",(double)
1407 image->scene);
cristy3ed852e2009-09-05 21:47:34 +00001408 (void) WriteBlobString(image,buffer);
1409 (void) WriteBlobString(image,"Q\n");
1410 offset=TellBlob(image)-offset;
1411 (void) WriteBlobString(image,"endstream\n");
1412 (void) WriteBlobString(image,"endobj\n");
1413 /*
1414 Write Length object.
1415 */
1416 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001417 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001418 object);
cristy3ed852e2009-09-05 21:47:34 +00001419 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001420 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
cristy3ed852e2009-09-05 21:47:34 +00001421 (void) WriteBlobString(image,buffer);
1422 (void) WriteBlobString(image,"endobj\n");
1423 /*
1424 Write Procset object.
1425 */
1426 xref[object++]=TellBlob(image);
cristyf40010e2012-05-13 13:52:20 +00001427 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1428 object);
cristy3ed852e2009-09-05 21:47:34 +00001429 (void) WriteBlobString(image,buffer);
1430 if ((image->storage_class == DirectClass) || (image->colors > 256))
1431 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageC",MaxTextExtent);
1432 else
1433 if ((compression == FaxCompression) || (compression == Group4Compression))
1434 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageB",MaxTextExtent);
1435 else
1436 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageI",MaxTextExtent);
1437 (void) WriteBlobString(image,buffer);
1438 (void) WriteBlobString(image," ]\n");
1439 (void) WriteBlobString(image,"endobj\n");
1440 /*
1441 Write Font object.
1442 */
1443 xref[object++]=TellBlob(image);
cristyf40010e2012-05-13 13:52:20 +00001444 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
1445 object);
cristy3ed852e2009-09-05 21:47:34 +00001446 (void) WriteBlobString(image,buffer);
1447 (void) WriteBlobString(image,"<<\n");
1448 if (labels != (char **) NULL)
1449 {
1450 (void) WriteBlobString(image,"/Type /Font\n");
1451 (void) WriteBlobString(image,"/Subtype /Type1\n");
cristyb51dff52011-05-19 16:55:47 +00001452 (void) FormatLocaleString(buffer,MaxTextExtent,"/Name /F%.20g\n",
cristye8c25f92010-06-03 00:53:06 +00001453 (double) image->scene);
cristy3ed852e2009-09-05 21:47:34 +00001454 (void) WriteBlobString(image,buffer);
1455 (void) WriteBlobString(image,"/BaseFont /Helvetica\n");
1456 (void) WriteBlobString(image,"/Encoding /MacRomanEncoding\n");
1457 labels=(char **) RelinquishMagickMemory(labels);
1458 }
1459 (void) WriteBlobString(image,">>\n");
1460 (void) WriteBlobString(image,"endobj\n");
1461 /*
1462 Write XObject object.
1463 */
1464 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001465 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001466 object);
cristy3ed852e2009-09-05 21:47:34 +00001467 (void) WriteBlobString(image,buffer);
1468 (void) WriteBlobString(image,"<<\n");
1469 (void) WriteBlobString(image,"/Type /XObject\n");
1470 (void) WriteBlobString(image,"/Subtype /Image\n");
cristyf40010e2012-05-13 13:52:20 +00001471 (void) FormatLocaleString(buffer,MaxTextExtent,"/Name /Im%.20g\n",(double)
1472 image->scene);
cristy3ed852e2009-09-05 21:47:34 +00001473 (void) WriteBlobString(image,buffer);
1474 switch (compression)
1475 {
1476 case NoCompression:
1477 {
cristyb51dff52011-05-19 16:55:47 +00001478 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"ASCII85Decode");
cristy3ed852e2009-09-05 21:47:34 +00001479 break;
1480 }
1481 case JPEGCompression:
1482 {
cristyb51dff52011-05-19 16:55:47 +00001483 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"DCTDecode");
cristy3ed852e2009-09-05 21:47:34 +00001484 if (image->colorspace != CMYKColorspace)
1485 break;
1486 (void) WriteBlobString(image,buffer);
1487 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1488 MaxTextExtent);
1489 break;
1490 }
1491 case JPEG2000Compression:
1492 {
cristyb51dff52011-05-19 16:55:47 +00001493 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"JPXDecode");
cristy3ed852e2009-09-05 21:47:34 +00001494 if (image->colorspace != CMYKColorspace)
1495 break;
1496 (void) WriteBlobString(image,buffer);
1497 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1498 MaxTextExtent);
1499 break;
1500 }
1501 case LZWCompression:
1502 {
cristyb51dff52011-05-19 16:55:47 +00001503 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"LZWDecode");
cristyfa7becb2009-09-13 02:44:40 +00001504 break;
cristy3ed852e2009-09-05 21:47:34 +00001505 }
1506 case ZipCompression:
1507 {
cristyb51dff52011-05-19 16:55:47 +00001508 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"FlateDecode");
cristyfa7becb2009-09-13 02:44:40 +00001509 break;
cristy3ed852e2009-09-05 21:47:34 +00001510 }
1511 case FaxCompression:
1512 case Group4Compression:
1513 {
1514 (void) CopyMagickString(buffer,"/Filter [ /CCITTFaxDecode ]\n",
1515 MaxTextExtent);
1516 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001517 (void) FormatLocaleString(buffer,MaxTextExtent,"/DecodeParms [ << "
cristye8c25f92010-06-03 00:53:06 +00001518 "/K %s /BlackIs1 false /Columns %.20g /Rows %.20g >> ]\n",CCITTParam,
1519 (double) image->columns,(double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001520 break;
1521 }
1522 default:
1523 {
cristyb51dff52011-05-19 16:55:47 +00001524 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
cristy3ed852e2009-09-05 21:47:34 +00001525 "RunLengthDecode");
1526 break;
1527 }
1528 }
1529 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001530 (void) FormatLocaleString(buffer,MaxTextExtent,"/Width %.20g\n",(double)
cristye8c25f92010-06-03 00:53:06 +00001531 image->columns);
cristy3ed852e2009-09-05 21:47:34 +00001532 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001533 (void) FormatLocaleString(buffer,MaxTextExtent,"/Height %.20g\n",(double)
cristye8c25f92010-06-03 00:53:06 +00001534 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001535 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001536 (void) FormatLocaleString(buffer,MaxTextExtent,"/ColorSpace %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001537 (double) object+2);
cristy3ed852e2009-09-05 21:47:34 +00001538 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001539 (void) FormatLocaleString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
cristy3ed852e2009-09-05 21:47:34 +00001540 (compression == FaxCompression) || (compression == Group4Compression) ?
1541 1 : 8);
1542 (void) WriteBlobString(image,buffer);
1543 if (image->matte != MagickFalse)
1544 {
cristyb51dff52011-05-19 16:55:47 +00001545 (void) FormatLocaleString(buffer,MaxTextExtent,"/SMask %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001546 (double) object+7);
cristy3ed852e2009-09-05 21:47:34 +00001547 (void) WriteBlobString(image,buffer);
1548 }
cristyb51dff52011-05-19 16:55:47 +00001549 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001550 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001551 (void) WriteBlobString(image,buffer);
1552 (void) WriteBlobString(image,">>\n");
1553 (void) WriteBlobString(image,"stream\n");
1554 offset=TellBlob(image);
1555 number_pixels=(MagickSizeType) image->columns*image->rows;
1556 if ((4*number_pixels) != (MagickSizeType) ((size_t) (4*number_pixels)))
1557 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1558 if ((compression == FaxCompression) || (compression == Group4Compression) ||
1559 ((image_info->type != TrueColorType) &&
cristy1e178e72011-08-28 19:44:34 +00001560 (IsImageGray(image,exception) != MagickFalse)))
cristy3ed852e2009-09-05 21:47:34 +00001561 {
1562 switch (compression)
1563 {
1564 case FaxCompression:
1565 case Group4Compression:
1566 {
1567 if (LocaleCompare(CCITTParam,"0") == 0)
1568 {
cristy018f07f2011-09-04 21:15:19 +00001569 (void) HuffmanEncodeImage(image_info,image,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001570 break;
1571 }
cristy018f07f2011-09-04 21:15:19 +00001572 (void) Huffman2DEncodeImage(image_info,image,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001573 break;
1574 }
1575 case JPEGCompression:
1576 {
cristy1e178e72011-08-28 19:44:34 +00001577 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
cristy3ed852e2009-09-05 21:47:34 +00001578 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00001579 {
1580 (void) CloseBlob(image);
1581 return(MagickFalse);
1582 }
cristy3ed852e2009-09-05 21:47:34 +00001583 break;
1584 }
1585 case JPEG2000Compression:
1586 {
cristy1e178e72011-08-28 19:44:34 +00001587 status=InjectImageBlob(image_info,image,image,"jp2",exception);
cristy3ed852e2009-09-05 21:47:34 +00001588 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00001589 {
1590 (void) CloseBlob(image);
1591 return(MagickFalse);
1592 }
cristy3ed852e2009-09-05 21:47:34 +00001593 break;
1594 }
1595 case RLECompression:
1596 default:
1597 {
1598 /*
1599 Allocate pixel array.
1600 */
1601 length=(size_t) number_pixels;
1602 pixels=(unsigned char *) AcquireQuantumMemory(length,
1603 sizeof(*pixels));
1604 if (pixels == (unsigned char *) NULL)
1605 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1606 /*
1607 Dump Runlength encoded pixels.
1608 */
1609 q=pixels;
cristybb503372010-05-27 20:51:26 +00001610 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001611 {
cristy1e178e72011-08-28 19:44:34 +00001612 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001613 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001614 break;
cristybb503372010-05-27 20:51:26 +00001615 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001616 {
cristy4c08aed2011-07-01 19:47:50 +00001617 *q++=ScaleQuantumToChar(GetPixelIntensity(image,p));
cristyed231572011-07-14 02:18:59 +00001618 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001619 }
1620 if (image->previous == (Image *) NULL)
1621 {
cristyf5c61ba2010-12-17 00:58:04 +00001622 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1623 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001624 if (status == MagickFalse)
1625 break;
1626 }
1627 }
1628#if defined(MAGICKCORE_ZLIB_DELEGATE)
1629 if (compression == ZipCompression)
cristy018f07f2011-09-04 21:15:19 +00001630 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001631 else
1632#endif
1633 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00001634 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001635 else
cristy018f07f2011-09-04 21:15:19 +00001636 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001637 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1638 if (status == MagickFalse)
1639 {
1640 (void) CloseBlob(image);
1641 return(MagickFalse);
1642 }
1643 break;
1644 }
1645 case NoCompression:
1646 {
1647 /*
1648 Dump uncompressed PseudoColor packets.
1649 */
1650 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00001651 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001652 {
cristy1e178e72011-08-28 19:44:34 +00001653 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001654 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001655 break;
cristybb503372010-05-27 20:51:26 +00001656 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001657 {
cristy4c08aed2011-07-01 19:47:50 +00001658 Ascii85Encode(image,ScaleQuantumToChar(
1659 GetPixelIntensity(image,p)));
cristyed231572011-07-14 02:18:59 +00001660 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001661 }
1662 if (image->previous == (Image *) NULL)
1663 {
cristyf5c61ba2010-12-17 00:58:04 +00001664 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1665 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001666 if (status == MagickFalse)
1667 break;
1668 }
1669 }
1670 Ascii85Flush(image);
1671 break;
1672 }
1673 }
1674 }
1675 else
1676 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1677 (compression == JPEGCompression) ||
1678 (compression == JPEG2000Compression))
1679 switch (compression)
1680 {
1681 case JPEGCompression:
1682 {
cristy1e178e72011-08-28 19:44:34 +00001683 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
cristy3ed852e2009-09-05 21:47:34 +00001684 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00001685 {
1686 (void) CloseBlob(image);
1687 return(MagickFalse);
1688 }
cristy3ed852e2009-09-05 21:47:34 +00001689 break;
1690 }
1691 case JPEG2000Compression:
1692 {
cristy1e178e72011-08-28 19:44:34 +00001693 status=InjectImageBlob(image_info,image,image,"jp2",exception);
cristy3ed852e2009-09-05 21:47:34 +00001694 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00001695 {
1696 (void) CloseBlob(image);
1697 return(MagickFalse);
1698 }
cristy3ed852e2009-09-05 21:47:34 +00001699 break;
1700 }
1701 case RLECompression:
1702 default:
1703 {
1704 /*
1705 Allocate pixel array.
1706 */
1707 length=(size_t) number_pixels;
1708 pixels=(unsigned char *) AcquireQuantumMemory(length,
1709 4*sizeof(*pixels));
1710 length*=image->colorspace == CMYKColorspace ? 4UL : 3UL;
1711 if (pixels == (unsigned char *) NULL)
1712 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1713 /*
1714 Dump runoffset encoded pixels.
1715 */
1716 q=pixels;
cristybb503372010-05-27 20:51:26 +00001717 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001718 {
cristy1e178e72011-08-28 19:44:34 +00001719 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001720 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001721 break;
cristybb503372010-05-27 20:51:26 +00001722 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001723 {
cristy4c08aed2011-07-01 19:47:50 +00001724 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1725 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1726 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
cristy3ed852e2009-09-05 21:47:34 +00001727 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00001728 *q++=ScaleQuantumToChar(GetPixelBlack(image,p));
cristyed231572011-07-14 02:18:59 +00001729 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001730 }
1731 if (image->previous == (Image *) NULL)
1732 {
cristyf5c61ba2010-12-17 00:58:04 +00001733 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1734 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001735 if (status == MagickFalse)
1736 break;
1737 }
1738 }
1739#if defined(MAGICKCORE_ZLIB_DELEGATE)
1740 if (compression == ZipCompression)
cristy018f07f2011-09-04 21:15:19 +00001741 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001742 else
1743#endif
1744 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00001745 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001746 else
cristy018f07f2011-09-04 21:15:19 +00001747 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001748 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1749 if (status == MagickFalse)
1750 {
1751 (void) CloseBlob(image);
1752 return(MagickFalse);
1753 }
1754 break;
1755 }
1756 case NoCompression:
1757 {
1758 /*
1759 Dump uncompressed DirectColor packets.
1760 */
1761 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00001762 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001763 {
cristy1e178e72011-08-28 19:44:34 +00001764 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001765 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001766 break;
cristybb503372010-05-27 20:51:26 +00001767 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001768 {
cristy4c08aed2011-07-01 19:47:50 +00001769 Ascii85Encode(image,ScaleQuantumToChar(GetPixelRed(image,p)));
1770 Ascii85Encode(image,ScaleQuantumToChar(GetPixelGreen(image,p)));
1771 Ascii85Encode(image,ScaleQuantumToChar(GetPixelBlue(image,p)));
cristy3ed852e2009-09-05 21:47:34 +00001772 if (image->colorspace == CMYKColorspace)
cristyaff6d802011-04-26 01:46:31 +00001773 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +00001774 GetPixelBlack(image,p)));
cristyed231572011-07-14 02:18:59 +00001775 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001776 }
1777 if (image->previous == (Image *) NULL)
1778 {
cristyf5c61ba2010-12-17 00:58:04 +00001779 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1780 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001781 if (status == MagickFalse)
1782 break;
1783 }
1784 }
1785 Ascii85Flush(image);
1786 break;
1787 }
1788 }
1789 else
1790 {
1791 /*
1792 Dump number of colors and colormap.
1793 */
1794 switch (compression)
1795 {
1796 case RLECompression:
1797 default:
1798 {
1799 /*
1800 Allocate pixel array.
1801 */
1802 length=(size_t) number_pixels;
1803 pixels=(unsigned char *) AcquireQuantumMemory(length,
1804 sizeof(*pixels));
1805 if (pixels == (unsigned char *) NULL)
1806 ThrowWriterException(ResourceLimitError,
1807 "MemoryAllocationFailed");
1808 /*
1809 Dump Runlength encoded pixels.
1810 */
1811 q=pixels;
cristybb503372010-05-27 20:51:26 +00001812 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001813 {
cristy1e178e72011-08-28 19:44:34 +00001814 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001815 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001816 break;
cristybb503372010-05-27 20:51:26 +00001817 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00001818 {
1819 *q++=(unsigned char) GetPixelIndex(image,p);
cristyed231572011-07-14 02:18:59 +00001820 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001821 }
cristy3ed852e2009-09-05 21:47:34 +00001822 if (image->previous == (Image *) NULL)
1823 {
cristyf5c61ba2010-12-17 00:58:04 +00001824 status=SetImageProgress(image,SaveImageTag,
1825 (MagickOffsetType) y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001826 if (status == MagickFalse)
1827 break;
1828 }
1829 }
1830#if defined(MAGICKCORE_ZLIB_DELEGATE)
1831 if (compression == ZipCompression)
cristy018f07f2011-09-04 21:15:19 +00001832 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001833 else
1834#endif
1835 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00001836 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001837 else
cristy018f07f2011-09-04 21:15:19 +00001838 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001839 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1840 if (status == MagickFalse)
1841 {
1842 (void) CloseBlob(image);
1843 return(MagickFalse);
1844 }
1845 break;
1846 }
1847 case NoCompression:
1848 {
1849 /*
1850 Dump uncompressed PseudoColor packets.
1851 */
1852 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00001853 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001854 {
cristy1e178e72011-08-28 19:44:34 +00001855 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001856 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001857 break;
cristybb503372010-05-27 20:51:26 +00001858 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00001859 {
1860 Ascii85Encode(image,(unsigned char) GetPixelIndex(image,p));
cristyed231572011-07-14 02:18:59 +00001861 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001862 }
cristy3ed852e2009-09-05 21:47:34 +00001863 if (image->previous == (Image *) NULL)
1864 {
cristyf5c61ba2010-12-17 00:58:04 +00001865 status=SetImageProgress(image,SaveImageTag,
1866 (MagickOffsetType) y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001867 if (status == MagickFalse)
1868 break;
1869 }
1870 }
1871 Ascii85Flush(image);
1872 break;
1873 }
1874 }
1875 }
1876 offset=TellBlob(image)-offset;
1877 (void) WriteBlobString(image,"\nendstream\n");
1878 (void) WriteBlobString(image,"endobj\n");
1879 /*
1880 Write Length object.
1881 */
1882 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001883 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001884 object);
cristy3ed852e2009-09-05 21:47:34 +00001885 (void) WriteBlobString(image,buffer);
cristyf40010e2012-05-13 13:52:20 +00001886 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
cristy3ed852e2009-09-05 21:47:34 +00001887 (void) WriteBlobString(image,buffer);
1888 (void) WriteBlobString(image,"endobj\n");
1889 /*
1890 Write Colorspace object.
1891 */
1892 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001893 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001894 object);
cristy3ed852e2009-09-05 21:47:34 +00001895 (void) WriteBlobString(image,buffer);
1896 if (image->colorspace == CMYKColorspace)
1897 (void) CopyMagickString(buffer,"/DeviceCMYK\n",MaxTextExtent);
1898 else
1899 if ((compression == FaxCompression) ||
1900 (compression == Group4Compression) ||
1901 ((image_info->type != TrueColorType) &&
cristy1e178e72011-08-28 19:44:34 +00001902 (IsImageGray(image,exception) != MagickFalse)))
cristy3ed852e2009-09-05 21:47:34 +00001903 (void) CopyMagickString(buffer,"/DeviceGray\n",MaxTextExtent);
1904 else
1905 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1906 (compression == JPEGCompression) ||
1907 (compression == JPEG2000Compression))
1908 (void) CopyMagickString(buffer,"/DeviceRGB\n",MaxTextExtent);
1909 else
cristyb51dff52011-05-19 16:55:47 +00001910 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00001911 "[ /Indexed /DeviceRGB %.20g %.20g 0 R ]\n",(double) image->colors-
1912 1,(double) object+3);
cristy3ed852e2009-09-05 21:47:34 +00001913 (void) WriteBlobString(image,buffer);
1914 (void) WriteBlobString(image,"endobj\n");
1915 /*
1916 Write Thumb object.
1917 */
1918 SetGeometry(image,&geometry);
1919 (void) ParseMetaGeometry("106x106+0+0>",&geometry.x,&geometry.y,
1920 &geometry.width,&geometry.height);
cristy1e178e72011-08-28 19:44:34 +00001921 tile_image=ThumbnailImage(image,geometry.width,geometry.height,exception);
cristy3ed852e2009-09-05 21:47:34 +00001922 if (tile_image == (Image *) NULL)
cristy1e178e72011-08-28 19:44:34 +00001923 return(MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +00001924 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001925 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001926 object);
cristy3ed852e2009-09-05 21:47:34 +00001927 (void) WriteBlobString(image,buffer);
1928 (void) WriteBlobString(image,"<<\n");
1929 switch (compression)
1930 {
1931 case NoCompression:
1932 {
cristyb51dff52011-05-19 16:55:47 +00001933 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"ASCII85Decode");
cristy3ed852e2009-09-05 21:47:34 +00001934 break;
1935 }
1936 case JPEGCompression:
1937 {
cristyb51dff52011-05-19 16:55:47 +00001938 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"DCTDecode");
cristy3ed852e2009-09-05 21:47:34 +00001939 if (image->colorspace != CMYKColorspace)
1940 break;
1941 (void) WriteBlobString(image,buffer);
1942 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1943 MaxTextExtent);
1944 break;
1945 }
1946 case JPEG2000Compression:
1947 {
cristyb51dff52011-05-19 16:55:47 +00001948 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"JPXDecode");
cristy3ed852e2009-09-05 21:47:34 +00001949 if (image->colorspace != CMYKColorspace)
1950 break;
1951 (void) WriteBlobString(image,buffer);
1952 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1953 MaxTextExtent);
1954 break;
1955 }
1956 case LZWCompression:
1957 {
cristyb51dff52011-05-19 16:55:47 +00001958 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"LZWDecode");
cristy3ed852e2009-09-05 21:47:34 +00001959 break;
1960 }
1961 case ZipCompression:
1962 {
cristyb51dff52011-05-19 16:55:47 +00001963 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"FlateDecode");
cristy3ed852e2009-09-05 21:47:34 +00001964 break;
1965 }
1966 case FaxCompression:
1967 case Group4Compression:
1968 {
1969 (void) CopyMagickString(buffer,"/Filter [ /CCITTFaxDecode ]\n",
1970 MaxTextExtent);
1971 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001972 (void) FormatLocaleString(buffer,MaxTextExtent,"/DecodeParms [ << "
cristye8c25f92010-06-03 00:53:06 +00001973 "/K %s /BlackIs1 false /Columns %.20g /Rows %.20g >> ]\n",CCITTParam,
1974 (double) tile_image->columns,(double) tile_image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001975 break;
1976 }
1977 default:
1978 {
cristyb51dff52011-05-19 16:55:47 +00001979 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
cristy3ed852e2009-09-05 21:47:34 +00001980 "RunLengthDecode");
1981 break;
1982 }
1983 }
1984 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001985 (void) FormatLocaleString(buffer,MaxTextExtent,"/Width %.20g\n",(double)
cristye8c25f92010-06-03 00:53:06 +00001986 tile_image->columns);
cristy3ed852e2009-09-05 21:47:34 +00001987 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001988 (void) FormatLocaleString(buffer,MaxTextExtent,"/Height %.20g\n",(double)
cristye8c25f92010-06-03 00:53:06 +00001989 tile_image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001990 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001991 (void) FormatLocaleString(buffer,MaxTextExtent,"/ColorSpace %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001992 (double) object-1);
cristy3ed852e2009-09-05 21:47:34 +00001993 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001994 (void) FormatLocaleString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
cristy3ed852e2009-09-05 21:47:34 +00001995 (compression == FaxCompression) || (compression == Group4Compression) ?
1996 1 : 8);
1997 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001998 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001999 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00002000 (void) WriteBlobString(image,buffer);
2001 (void) WriteBlobString(image,">>\n");
2002 (void) WriteBlobString(image,"stream\n");
2003 offset=TellBlob(image);
2004 number_pixels=(MagickSizeType) tile_image->columns*tile_image->rows;
2005 if ((compression == FaxCompression) ||
2006 (compression == Group4Compression) ||
2007 ((image_info->type != TrueColorType) &&
cristy1e178e72011-08-28 19:44:34 +00002008 (IsImageGray(tile_image,exception) != MagickFalse)))
cristy3ed852e2009-09-05 21:47:34 +00002009 {
2010 switch (compression)
2011 {
2012 case FaxCompression:
2013 case Group4Compression:
2014 {
2015 if (LocaleCompare(CCITTParam,"0") == 0)
2016 {
cristy018f07f2011-09-04 21:15:19 +00002017 (void) HuffmanEncodeImage(image_info,image,tile_image,
2018 exception);
cristy3ed852e2009-09-05 21:47:34 +00002019 break;
2020 }
cristy018f07f2011-09-04 21:15:19 +00002021 (void) Huffman2DEncodeImage(image_info,image,tile_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00002022 break;
2023 }
2024 case JPEGCompression:
2025 {
2026 status=InjectImageBlob(image_info,image,tile_image,"jpeg",
cristy1e178e72011-08-28 19:44:34 +00002027 exception);
cristy3ed852e2009-09-05 21:47:34 +00002028 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00002029 {
2030 (void) CloseBlob(image);
2031 return(MagickFalse);
2032 }
cristy3ed852e2009-09-05 21:47:34 +00002033 break;
2034 }
2035 case JPEG2000Compression:
2036 {
cristy1e178e72011-08-28 19:44:34 +00002037 status=InjectImageBlob(image_info,image,tile_image,"jp2",exception);
cristy3ed852e2009-09-05 21:47:34 +00002038 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00002039 {
2040 (void) CloseBlob(image);
2041 return(MagickFalse);
2042 }
cristy3ed852e2009-09-05 21:47:34 +00002043 break;
2044 }
2045 case RLECompression:
2046 default:
2047 {
2048 /*
2049 Allocate pixel array.
2050 */
2051 length=(size_t) number_pixels;
2052 pixels=(unsigned char *) AcquireQuantumMemory(length,
2053 sizeof(*pixels));
2054 if (pixels == (unsigned char *) NULL)
2055 {
2056 tile_image=DestroyImage(tile_image);
2057 ThrowWriterException(ResourceLimitError,
2058 "MemoryAllocationFailed");
2059 }
2060 /*
2061 Dump Runlength encoded pixels.
2062 */
2063 q=pixels;
cristybb503372010-05-27 20:51:26 +00002064 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002065 {
2066 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
cristy1e178e72011-08-28 19:44:34 +00002067 exception);
cristy4c08aed2011-07-01 19:47:50 +00002068 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002069 break;
cristybb503372010-05-27 20:51:26 +00002070 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002071 {
cristy4c08aed2011-07-01 19:47:50 +00002072 *q++=ScaleQuantumToChar(GetPixelIntensity(tile_image,p));
cristyed231572011-07-14 02:18:59 +00002073 p+=GetPixelChannels(tile_image);
cristy3ed852e2009-09-05 21:47:34 +00002074 }
2075 }
2076#if defined(MAGICKCORE_ZLIB_DELEGATE)
2077 if (compression == ZipCompression)
cristy018f07f2011-09-04 21:15:19 +00002078 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002079 else
2080#endif
2081 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00002082 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002083 else
cristy018f07f2011-09-04 21:15:19 +00002084 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002085 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2086 if (status == MagickFalse)
2087 {
2088 (void) CloseBlob(image);
2089 return(MagickFalse);
2090 }
2091 break;
2092 }
2093 case NoCompression:
2094 {
2095 /*
2096 Dump uncompressed PseudoColor packets.
2097 */
2098 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00002099 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002100 {
2101 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
cristy1e178e72011-08-28 19:44:34 +00002102 exception);
cristy4c08aed2011-07-01 19:47:50 +00002103 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002104 break;
cristybb503372010-05-27 20:51:26 +00002105 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002106 {
2107 Ascii85Encode(image,
cristy4c08aed2011-07-01 19:47:50 +00002108 ScaleQuantumToChar(GetPixelIntensity(tile_image,p)));
cristyed231572011-07-14 02:18:59 +00002109 p+=GetPixelChannels(tile_image);
cristy3ed852e2009-09-05 21:47:34 +00002110 }
2111 }
2112 Ascii85Flush(image);
2113 break;
2114 }
2115 }
2116 }
2117 else
2118 if ((tile_image->storage_class == DirectClass) ||
2119 (tile_image->colors > 256) || (compression == JPEGCompression) ||
2120 (compression == JPEG2000Compression))
2121 switch (compression)
2122 {
2123 case JPEGCompression:
2124 {
2125 status=InjectImageBlob(image_info,image,tile_image,"jpeg",
cristy1e178e72011-08-28 19:44:34 +00002126 exception);
cristy3ed852e2009-09-05 21:47:34 +00002127 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00002128 {
2129 (void) CloseBlob(image);
2130 return(MagickFalse);
2131 }
cristy3ed852e2009-09-05 21:47:34 +00002132 break;
2133 }
2134 case JPEG2000Compression:
2135 {
cristy1e178e72011-08-28 19:44:34 +00002136 status=InjectImageBlob(image_info,image,tile_image,"jp2",exception);
cristy3ed852e2009-09-05 21:47:34 +00002137 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00002138 {
2139 (void) CloseBlob(image);
2140 return(MagickFalse);
2141 }
cristy3ed852e2009-09-05 21:47:34 +00002142 break;
2143 }
2144 case RLECompression:
2145 default:
2146 {
2147 /*
2148 Allocate pixel array.
2149 */
2150 length=(size_t) number_pixels;
2151 pixels=(unsigned char *) AcquireQuantumMemory(length,4*
2152 sizeof(*pixels));
2153 length*=tile_image->colorspace == CMYKColorspace ? 4UL : 3UL;
2154 if (pixels == (unsigned char *) NULL)
2155 {
2156 tile_image=DestroyImage(tile_image);
2157 ThrowWriterException(ResourceLimitError,
2158 "MemoryAllocationFailed");
2159 }
2160 /*
2161 Dump runoffset encoded pixels.
2162 */
2163 q=pixels;
cristybb503372010-05-27 20:51:26 +00002164 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002165 {
2166 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
cristy1e178e72011-08-28 19:44:34 +00002167 exception);
cristy4c08aed2011-07-01 19:47:50 +00002168 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002169 break;
cristybb503372010-05-27 20:51:26 +00002170 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002171 {
cristy4c08aed2011-07-01 19:47:50 +00002172 *q++=ScaleQuantumToChar(GetPixelRed(tile_image,p));
2173 *q++=ScaleQuantumToChar(GetPixelGreen(tile_image,p));
2174 *q++=ScaleQuantumToChar(GetPixelBlue(tile_image,p));
cristy3ed852e2009-09-05 21:47:34 +00002175 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00002176 *q++=ScaleQuantumToChar(GetPixelBlack(tile_image,p));
cristyed231572011-07-14 02:18:59 +00002177 p+=GetPixelChannels(tile_image);
cristy3ed852e2009-09-05 21:47:34 +00002178 }
2179 }
2180#if defined(MAGICKCORE_ZLIB_DELEGATE)
2181 if (compression == ZipCompression)
cristy018f07f2011-09-04 21:15:19 +00002182 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002183 else
2184#endif
2185 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00002186 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002187 else
cristy018f07f2011-09-04 21:15:19 +00002188 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002189 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2190 if (status == MagickFalse)
2191 {
2192 (void) CloseBlob(image);
2193 return(MagickFalse);
2194 }
2195 break;
2196 }
2197 case NoCompression:
2198 {
2199 /*
2200 Dump uncompressed DirectColor packets.
2201 */
2202 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00002203 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002204 {
2205 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
cristy1e178e72011-08-28 19:44:34 +00002206 exception);
cristy4c08aed2011-07-01 19:47:50 +00002207 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002208 break;
cristybb503372010-05-27 20:51:26 +00002209 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002210 {
cristyf5c61ba2010-12-17 00:58:04 +00002211 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +00002212 GetPixelRed(tile_image,p)));
cristyf5c61ba2010-12-17 00:58:04 +00002213 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +00002214 GetPixelGreen(tile_image,p)));
cristyf5c61ba2010-12-17 00:58:04 +00002215 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +00002216 GetPixelBlue(tile_image,p)));
cristy3ed852e2009-09-05 21:47:34 +00002217 if (image->colorspace == CMYKColorspace)
cristyaff6d802011-04-26 01:46:31 +00002218 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +00002219 GetPixelBlack(tile_image,p)));
cristyed231572011-07-14 02:18:59 +00002220 p+=GetPixelChannels(tile_image);
cristy3ed852e2009-09-05 21:47:34 +00002221 }
2222 }
2223 Ascii85Flush(image);
2224 break;
2225 }
2226 }
2227 else
2228 {
2229 /*
2230 Dump number of colors and colormap.
2231 */
2232 switch (compression)
2233 {
2234 case RLECompression:
2235 default:
2236 {
2237 /*
2238 Allocate pixel array.
2239 */
2240 length=(size_t) number_pixels;
2241 pixels=(unsigned char *) AcquireQuantumMemory(length,
2242 sizeof(*pixels));
2243 if (pixels == (unsigned char *) NULL)
2244 {
2245 tile_image=DestroyImage(tile_image);
2246 ThrowWriterException(ResourceLimitError,
2247 "MemoryAllocationFailed");
2248 }
2249 /*
2250 Dump Runlength encoded pixels.
2251 */
2252 q=pixels;
cristybb503372010-05-27 20:51:26 +00002253 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002254 {
2255 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
cristy1e178e72011-08-28 19:44:34 +00002256 exception);
cristy4c08aed2011-07-01 19:47:50 +00002257 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002258 break;
cristybb503372010-05-27 20:51:26 +00002259 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00002260 {
2261 *q++=(unsigned char) GetPixelIndex(tile_image,p);
cristy6307d372012-06-17 20:07:34 +00002262 p+=GetPixelChannels(tile_image);
cristy4c08aed2011-07-01 19:47:50 +00002263 }
cristy3ed852e2009-09-05 21:47:34 +00002264 }
2265#if defined(MAGICKCORE_ZLIB_DELEGATE)
2266 if (compression == ZipCompression)
cristy018f07f2011-09-04 21:15:19 +00002267 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002268 else
2269#endif
2270 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00002271 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002272 else
cristy018f07f2011-09-04 21:15:19 +00002273 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002274 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2275 if (status == MagickFalse)
2276 {
2277 (void) CloseBlob(image);
2278 return(MagickFalse);
2279 }
2280 break;
2281 }
2282 case NoCompression:
2283 {
2284 /*
2285 Dump uncompressed PseudoColor packets.
2286 */
2287 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00002288 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002289 {
2290 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
cristy1e178e72011-08-28 19:44:34 +00002291 exception);
cristy4c08aed2011-07-01 19:47:50 +00002292 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002293 break;
cristybb503372010-05-27 20:51:26 +00002294 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00002295 {
cristyaff6d802011-04-26 01:46:31 +00002296 Ascii85Encode(image,(unsigned char)
cristy4c08aed2011-07-01 19:47:50 +00002297 GetPixelIndex(tile_image,p));
cristyed231572011-07-14 02:18:59 +00002298 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00002299 }
cristy3ed852e2009-09-05 21:47:34 +00002300 }
2301 Ascii85Flush(image);
2302 break;
2303 }
2304 }
2305 }
2306 tile_image=DestroyImage(tile_image);
2307 offset=TellBlob(image)-offset;
2308 (void) WriteBlobString(image,"\nendstream\n");
2309 (void) WriteBlobString(image,"endobj\n");
2310 /*
2311 Write Length object.
2312 */
2313 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00002314 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002315 object);
cristy3ed852e2009-09-05 21:47:34 +00002316 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002317 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
cristy3ed852e2009-09-05 21:47:34 +00002318 (void) WriteBlobString(image,buffer);
2319 (void) WriteBlobString(image,"endobj\n");
2320 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00002321 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002322 object);
cristy3ed852e2009-09-05 21:47:34 +00002323 (void) WriteBlobString(image,buffer);
2324 if ((image->storage_class != DirectClass) && (image->colors <= 256) &&
2325 (compression != FaxCompression) && (compression != Group4Compression))
2326 {
2327 /*
2328 Write Colormap object.
2329 */
2330 (void) WriteBlobString(image,"<<\n");
2331 if (compression == NoCompression)
2332 (void) WriteBlobString(image,"/Filter [ /ASCII85Decode ]\n");
cristyb51dff52011-05-19 16:55:47 +00002333 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00002334 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00002335 (void) WriteBlobString(image,buffer);
2336 (void) WriteBlobString(image,">>\n");
2337 (void) WriteBlobString(image,"stream\n");
2338 offset=TellBlob(image);
2339 if (compression == NoCompression)
2340 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00002341 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00002342 {
2343 if (compression == NoCompression)
2344 {
2345 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].red));
2346 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].green));
2347 Ascii85Encode(image,ScaleQuantumToChar(image->colormap[i].blue));
2348 continue;
2349 }
2350 (void) WriteBlobByte(image,
2351 ScaleQuantumToChar(image->colormap[i].red));
2352 (void) WriteBlobByte(image,
2353 ScaleQuantumToChar(image->colormap[i].green));
2354 (void) WriteBlobByte(image,
2355 ScaleQuantumToChar(image->colormap[i].blue));
2356 }
2357 if (compression == NoCompression)
2358 Ascii85Flush(image);
2359 offset=TellBlob(image)-offset;
2360 (void) WriteBlobString(image,"\nendstream\n");
2361 }
2362 (void) WriteBlobString(image,"endobj\n");
2363 /*
2364 Write Length object.
2365 */
2366 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00002367 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002368 object);
cristy3ed852e2009-09-05 21:47:34 +00002369 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002370 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002371 offset);
cristy3ed852e2009-09-05 21:47:34 +00002372 (void) WriteBlobString(image,buffer);
2373 (void) WriteBlobString(image,"endobj\n");
2374 /*
2375 Write softmask object.
2376 */
2377 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00002378 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002379 object);
cristy3ed852e2009-09-05 21:47:34 +00002380 (void) WriteBlobString(image,buffer);
2381 (void) WriteBlobString(image,"<<\n");
2382 if (image->matte == MagickFalse)
2383 (void) WriteBlobString(image,">>\n");
2384 else
2385 {
2386 (void) WriteBlobString(image,"/Type /XObject\n");
2387 (void) WriteBlobString(image,"/Subtype /Image\n");
cristyb51dff52011-05-19 16:55:47 +00002388 (void) FormatLocaleString(buffer,MaxTextExtent,"/Name /Ma%.20g\n",
cristye8c25f92010-06-03 00:53:06 +00002389 (double) image->scene);
cristy3ed852e2009-09-05 21:47:34 +00002390 (void) WriteBlobString(image,buffer);
2391 switch (compression)
2392 {
2393 case NoCompression:
2394 {
cristyb51dff52011-05-19 16:55:47 +00002395 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
cristy3ed852e2009-09-05 21:47:34 +00002396 "ASCII85Decode");
2397 break;
2398 }
2399 case LZWCompression:
2400 {
cristyb51dff52011-05-19 16:55:47 +00002401 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"LZWDecode");
cristy3ed852e2009-09-05 21:47:34 +00002402 break;
2403 }
2404 case ZipCompression:
2405 {
cristyb51dff52011-05-19 16:55:47 +00002406 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
cristy3ed852e2009-09-05 21:47:34 +00002407 "FlateDecode");
2408 break;
2409 }
2410 default:
2411 {
cristyb51dff52011-05-19 16:55:47 +00002412 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
cristy3ed852e2009-09-05 21:47:34 +00002413 "RunLengthDecode");
2414 break;
2415 }
2416 }
2417 (void) WriteBlobString(image,buffer);
cristyf40010e2012-05-13 13:52:20 +00002418 (void) FormatLocaleString(buffer,MaxTextExtent,"/Width %.20g\n",(double)
2419 image->columns);
cristy3ed852e2009-09-05 21:47:34 +00002420 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002421 (void) FormatLocaleString(buffer,MaxTextExtent,"/Height %.20g\n",
cristye8c25f92010-06-03 00:53:06 +00002422 (double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +00002423 (void) WriteBlobString(image,buffer);
2424 (void) WriteBlobString(image,"/ColorSpace /DeviceGray\n");
cristyb51dff52011-05-19 16:55:47 +00002425 (void) FormatLocaleString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
cristy3ed852e2009-09-05 21:47:34 +00002426 (compression == FaxCompression) || (compression == Group4Compression)
2427 ? 1 : 8);
2428 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002429 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00002430 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00002431 (void) WriteBlobString(image,buffer);
2432 (void) WriteBlobString(image,">>\n");
2433 (void) WriteBlobString(image,"stream\n");
2434 offset=TellBlob(image);
2435 number_pixels=(MagickSizeType) image->columns*image->rows;
2436 switch (compression)
2437 {
2438 case RLECompression:
2439 default:
2440 {
2441 /*
2442 Allocate pixel array.
2443 */
2444 length=(size_t) number_pixels;
2445 pixels=(unsigned char *) AcquireQuantumMemory(length,
2446 sizeof(*pixels));
2447 if (pixels == (unsigned char *) NULL)
2448 {
2449 image=DestroyImage(image);
2450 ThrowWriterException(ResourceLimitError,
2451 "MemoryAllocationFailed");
2452 }
2453 /*
2454 Dump Runlength encoded pixels.
2455 */
2456 q=pixels;
cristybb503372010-05-27 20:51:26 +00002457 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002458 {
cristy1e178e72011-08-28 19:44:34 +00002459 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00002460 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002461 break;
cristybb503372010-05-27 20:51:26 +00002462 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002463 {
cristy4c08aed2011-07-01 19:47:50 +00002464 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
cristyed231572011-07-14 02:18:59 +00002465 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002466 }
2467 }
2468#if defined(MAGICKCORE_ZLIB_DELEGATE)
2469 if (compression == ZipCompression)
cristy018f07f2011-09-04 21:15:19 +00002470 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002471 else
2472#endif
2473 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00002474 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002475 else
cristy018f07f2011-09-04 21:15:19 +00002476 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002477 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2478 if (status == MagickFalse)
2479 {
2480 (void) CloseBlob(image);
2481 return(MagickFalse);
2482 }
2483 break;
2484 }
2485 case NoCompression:
2486 {
2487 /*
2488 Dump uncompressed PseudoColor packets.
2489 */
2490 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00002491 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002492 {
cristy1e178e72011-08-28 19:44:34 +00002493 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00002494 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002495 break;
cristybb503372010-05-27 20:51:26 +00002496 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002497 {
cristy4c08aed2011-07-01 19:47:50 +00002498 Ascii85Encode(image,ScaleQuantumToChar(GetPixelAlpha(image,p)));
cristyed231572011-07-14 02:18:59 +00002499 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002500 }
2501 }
2502 Ascii85Flush(image);
2503 break;
2504 }
2505 }
2506 offset=TellBlob(image)-offset;
2507 (void) WriteBlobString(image,"\nendstream\n");
2508 }
2509 (void) WriteBlobString(image,"endobj\n");
2510 /*
2511 Write Length object.
2512 */
2513 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00002514 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002515 object);
cristy3ed852e2009-09-05 21:47:34 +00002516 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002517 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
cristy3ed852e2009-09-05 21:47:34 +00002518 (void) WriteBlobString(image,buffer);
2519 (void) WriteBlobString(image,"endobj\n");
2520 if (GetNextImageInList(image) == (Image *) NULL)
2521 break;
2522 image=SyncNextImageInList(image);
2523 status=SetImageProgress(image,SaveImagesTag,scene++,
2524 GetImageListLength(image));
2525 if (status == MagickFalse)
2526 break;
2527 } while (image_info->adjoin != MagickFalse);
2528 /*
2529 Write Metadata object.
2530 */
2531 xref[object++]=TellBlob(image);
2532 info_id=object;
cristyb51dff52011-05-19 16:55:47 +00002533 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002534 object);
cristy3ed852e2009-09-05 21:47:34 +00002535 (void) WriteBlobString(image,buffer);
2536 (void) WriteBlobString(image,"<<\n");
cristy57015272010-12-30 01:16:38 +00002537 GetPathComponent(image->filename,BasePath,basename);
cristyb51dff52011-05-19 16:55:47 +00002538 (void) FormatLocaleString(buffer,MaxTextExtent,"/Title (%s)\n",
cristy57015272010-12-30 01:16:38 +00002539 EscapeParenthesis(basename));
cristy3ed852e2009-09-05 21:47:34 +00002540 (void) WriteBlobString(image,buffer);
2541 seconds=time((time_t *) NULL);
2542#if defined(MAGICKCORE_HAVE_LOCALTIME_R)
2543 (void) localtime_r(&seconds,&local_time);
2544#else
2545 (void) memcpy(&local_time,localtime(&seconds),sizeof(local_time));
2546#endif
cristyb51dff52011-05-19 16:55:47 +00002547 (void) FormatLocaleString(date,MaxTextExtent,"D:%04d%02d%02d%02d%02d%02d",
cristy3ed852e2009-09-05 21:47:34 +00002548 local_time.tm_year+1900,local_time.tm_mon+1,local_time.tm_mday,
2549 local_time.tm_hour,local_time.tm_min,local_time.tm_sec);
cristyb51dff52011-05-19 16:55:47 +00002550 (void) FormatLocaleString(buffer,MaxTextExtent,"/CreationDate (%s)\n",date);
cristy3ed852e2009-09-05 21:47:34 +00002551 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002552 (void) FormatLocaleString(buffer,MaxTextExtent,"/ModDate (%s)\n",date);
cristy3ed852e2009-09-05 21:47:34 +00002553 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002554 (void) FormatLocaleString(buffer,MaxTextExtent,"/Producer (%s)\n",
cristybb503372010-05-27 20:51:26 +00002555 EscapeParenthesis(GetMagickVersion((size_t *) NULL)));
cristy3ed852e2009-09-05 21:47:34 +00002556 (void) WriteBlobString(image,buffer);
2557 (void) WriteBlobString(image,">>\n");
2558 (void) WriteBlobString(image,"endobj\n");
2559 /*
2560 Write Xref object.
2561 */
cristyf40010e2012-05-13 13:52:20 +00002562 offset=TellBlob(image)-xref[0]+
2563 (LocaleCompare(image_info->magick,"PDFA") == 0 ? 6 : 0)+10;
cristy3ed852e2009-09-05 21:47:34 +00002564 (void) WriteBlobString(image,"xref\n");
cristyb51dff52011-05-19 16:55:47 +00002565 (void) FormatLocaleString(buffer,MaxTextExtent,"0 %.20g\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002566 object+1);
cristy3ed852e2009-09-05 21:47:34 +00002567 (void) WriteBlobString(image,buffer);
2568 (void) WriteBlobString(image,"0000000000 65535 f \n");
cristybb503372010-05-27 20:51:26 +00002569 for (i=0; i < (ssize_t) object; i++)
cristy3ed852e2009-09-05 21:47:34 +00002570 {
cristyb51dff52011-05-19 16:55:47 +00002571 (void) FormatLocaleString(buffer,MaxTextExtent,"%010lu 00000 n \n",
cristyf2faecf2010-05-28 19:19:36 +00002572 (unsigned long) xref[i]);
cristy3ed852e2009-09-05 21:47:34 +00002573 (void) WriteBlobString(image,buffer);
2574 }
2575 (void) WriteBlobString(image,"trailer\n");
2576 (void) WriteBlobString(image,"<<\n");
cristyb51dff52011-05-19 16:55:47 +00002577 (void) FormatLocaleString(buffer,MaxTextExtent,"/Size %.20g\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002578 object+1);
cristy3ed852e2009-09-05 21:47:34 +00002579 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002580 (void) FormatLocaleString(buffer,MaxTextExtent,"/Info %.20g 0 R\n",(double)
cristye8c25f92010-06-03 00:53:06 +00002581 info_id);
cristy3ed852e2009-09-05 21:47:34 +00002582 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002583 (void) FormatLocaleString(buffer,MaxTextExtent,"/Root %.20g 0 R\n",(double)
cristye8c25f92010-06-03 00:53:06 +00002584 root_id);
cristy3ed852e2009-09-05 21:47:34 +00002585 (void) WriteBlobString(image,buffer);
2586 (void) WriteBlobString(image,">>\n");
2587 (void) WriteBlobString(image,"startxref\n");
cristyb51dff52011-05-19 16:55:47 +00002588 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
cristy3ed852e2009-09-05 21:47:34 +00002589 (void) WriteBlobString(image,buffer);
2590 (void) WriteBlobString(image,"%%EOF\n");
2591 xref=(MagickOffsetType *) RelinquishMagickMemory(xref);
2592 (void) CloseBlob(image);
2593 return(MagickTrue);
2594}