blob: e54342ade73ae7b95374c2db66e3642645fc7376 [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;
656 next=ReadImage(read_info,exception);
657 (void) RelinquishUniqueFileResource(read_info->filename);
658 if (next == (Image *) NULL)
659 break;
660 AppendImageToList(&pdf_image,next);
661 }
cristy3ed852e2009-09-05 21:47:34 +0000662 read_info=DestroyImageInfo(read_info);
663 if (pdf_image == (Image *) NULL)
664 {
665 ThrowFileException(exception,DelegateError,"PostscriptDelegateFailed",
666 image_info->filename);
cristyb6cabe82011-03-18 13:25:59 +0000667 image=DestroyImage(image);
cristy3ed852e2009-09-05 21:47:34 +0000668 return((Image *) NULL);
669 }
670 if (LocaleCompare(pdf_image->magick,"BMP") == 0)
671 {
672 Image
673 *cmyk_image;
674
675 cmyk_image=ConsolidateCMYKImages(pdf_image,exception);
676 if (cmyk_image != (Image *) NULL)
677 {
678 pdf_image=DestroyImageList(pdf_image);
679 pdf_image=cmyk_image;
680 }
681 }
682 if (image_info->number_scenes != 0)
683 {
684 Image
685 *clone_image;
686
cristybb503372010-05-27 20:51:26 +0000687 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000688 i;
689
690 /*
691 Add place holder images to meet the subimage specification requirement.
692 */
cristybb503372010-05-27 20:51:26 +0000693 for (i=0; i < (ssize_t) image_info->scene; i++)
cristy3ed852e2009-09-05 21:47:34 +0000694 {
695 clone_image=CloneImage(pdf_image,1,1,MagickTrue,exception);
696 if (clone_image != (Image *) NULL)
697 PrependImageToList(&pdf_image,clone_image);
698 }
699 }
700 do
701 {
702 (void) CopyMagickString(pdf_image->filename,filename,MaxTextExtent);
703 pdf_image->page=page;
704 (void) CloneImageProfiles(pdf_image,image);
705 (void) CloneImageProperties(pdf_image,image);
706 next=SyncNextImageInList(pdf_image);
707 if (next != (Image *) NULL)
708 pdf_image=next;
709 } while (next != (Image *) NULL);
710 image=DestroyImage(image);
711 scene=0;
712 for (next=GetFirstImageInList(pdf_image); next != (Image *) NULL; )
713 {
714 next->scene=scene++;
715 next=GetNextImageInList(next);
716 }
717 return(GetFirstImageInList(pdf_image));
718}
719
720/*
721%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
722% %
723% %
724% %
725% R e g i s t e r P D F I m a g e %
726% %
727% %
728% %
729%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
730%
731% RegisterPDFImage() adds properties for the PDF image format to
732% the list of supported formats. The properties include the image format
733% tag, a method to read and/or write the format, whether the format
734% supports the saving of more than one frame to the same file or blob,
735% whether the format supports native in-memory I/O, and a brief
736% description of the format.
737%
738% The format of the RegisterPDFImage method is:
739%
cristybb503372010-05-27 20:51:26 +0000740% size_t RegisterPDFImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000741%
742*/
cristybb503372010-05-27 20:51:26 +0000743ModuleExport size_t RegisterPDFImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000744{
745 MagickInfo
746 *entry;
747
748 entry=SetMagickInfo("AI");
749 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
750 entry->encoder=(EncodeImageHandler *) WritePDFImage;
751 entry->adjoin=MagickFalse;
752 entry->blob_support=MagickFalse;
753 entry->seekable_stream=MagickTrue;
754 entry->thread_support=EncoderThreadSupport;
755 entry->description=ConstantString("Adobe Illustrator CS2");
756 entry->module=ConstantString("PDF");
757 (void) RegisterMagickInfo(entry);
758 entry=SetMagickInfo("EPDF");
759 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
760 entry->encoder=(EncodeImageHandler *) WritePDFImage;
761 entry->adjoin=MagickFalse;
762 entry->blob_support=MagickFalse;
763 entry->seekable_stream=MagickTrue;
764 entry->thread_support=EncoderThreadSupport;
765 entry->description=ConstantString("Encapsulated Portable Document Format");
766 entry->module=ConstantString("PDF");
767 (void) RegisterMagickInfo(entry);
768 entry=SetMagickInfo("PDF");
769 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
770 entry->encoder=(EncodeImageHandler *) WritePDFImage;
771 entry->magick=(IsImageFormatHandler *) IsPDF;
772 entry->blob_support=MagickFalse;
773 entry->seekable_stream=MagickTrue;
774 entry->thread_support=EncoderThreadSupport;
775 entry->description=ConstantString("Portable Document Format");
776 entry->module=ConstantString("PDF");
777 (void) RegisterMagickInfo(entry);
778 entry=SetMagickInfo("PDFA");
779 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
780 entry->encoder=(EncodeImageHandler *) WritePDFImage;
781 entry->magick=(IsImageFormatHandler *) IsPDF;
782 entry->blob_support=MagickFalse;
783 entry->seekable_stream=MagickTrue;
784 entry->thread_support=EncoderThreadSupport;
785 entry->description=ConstantString("Portable Document Archive Format");
786 entry->module=ConstantString("PDF");
787 (void) RegisterMagickInfo(entry);
788 return(MagickImageCoderSignature);
789}
790
791/*
792%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
793% %
794% %
795% %
796% U n r e g i s t e r P D F I m a g e %
797% %
798% %
799% %
800%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
801%
802% UnregisterPDFImage() removes format registrations made by the
803% PDF module from the list of supported formats.
804%
805% The format of the UnregisterPDFImage method is:
806%
807% UnregisterPDFImage(void)
808%
809*/
810ModuleExport void UnregisterPDFImage(void)
811{
812 (void) UnregisterMagickInfo("AI");
813 (void) UnregisterMagickInfo("EPDF");
814 (void) UnregisterMagickInfo("PDF");
815 (void) UnregisterMagickInfo("PDFA");
816}
817
818/*
819%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
820% %
821% %
822% %
823% W r i t e P D F I m a g e %
824% %
825% %
826% %
827%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
828%
829% WritePDFImage() writes an image in the Portable Document image
830% format.
831%
832% The format of the WritePDFImage method is:
833%
cristy1e178e72011-08-28 19:44:34 +0000834% MagickBooleanType WritePDFImage(const ImageInfo *image_info,
835% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000836%
837% A description of each parameter follows.
838%
839% o image_info: the image info.
840%
841% o image: The image.
842%
cristy1e178e72011-08-28 19:44:34 +0000843% o exception: return any errors or warnings in this structure.
844%
cristy3ed852e2009-09-05 21:47:34 +0000845*/
846
847static inline size_t MagickMax(const size_t x,const size_t y)
848{
849 if (x > y)
850 return(x);
851 return(y);
852}
853
854static inline size_t MagickMin(const size_t x,const size_t y)
855{
856 if (x < y)
857 return(x);
858 return(y);
859}
860
861static char *EscapeParenthesis(const char *text)
862{
863 register char
864 *p;
865
cristybb503372010-05-27 20:51:26 +0000866 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000867 i;
868
cristybb503372010-05-27 20:51:26 +0000869 size_t
cristy3ed852e2009-09-05 21:47:34 +0000870 escapes;
871
cristyaff6d802011-04-26 01:46:31 +0000872 static char
873 buffer[MaxTextExtent];
874
cristy3ed852e2009-09-05 21:47:34 +0000875 escapes=0;
876 p=buffer;
cristybb503372010-05-27 20:51:26 +0000877 for (i=0; i < (ssize_t) MagickMin(strlen(text),(MaxTextExtent-escapes-1)); i++)
cristy3ed852e2009-09-05 21:47:34 +0000878 {
879 if ((text[i] == '(') || (text[i] == ')'))
880 {
881 *p++='\\';
882 escapes++;
883 }
884 *p++=text[i];
885 }
886 *p='\0';
887 return(buffer);
888}
889
cristy47b838c2009-09-19 16:09:30 +0000890static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
cristy018f07f2011-09-04 21:15:19 +0000891 Image *image,Image *inject_image,ExceptionInfo *exception)
cristy47b838c2009-09-19 16:09:30 +0000892{
cristy47b838c2009-09-19 16:09:30 +0000893 Image
cristy80975862009-09-25 14:34:31 +0000894 *group4_image;
cristy47b838c2009-09-19 16:09:30 +0000895
896 ImageInfo
897 *write_info;
898
cristy47b838c2009-09-19 16:09:30 +0000899 MagickBooleanType
900 status;
901
cristy80975862009-09-25 14:34:31 +0000902 size_t
903 length;
cristy47b838c2009-09-19 16:09:30 +0000904
905 unsigned char
cristy80975862009-09-25 14:34:31 +0000906 *group4;
cristy47b838c2009-09-19 16:09:30 +0000907
cristy42751fe2009-10-05 00:15:50 +0000908 status=MagickTrue;
cristy47b838c2009-09-19 16:09:30 +0000909 write_info=CloneImageInfo(image_info);
cristy80975862009-09-25 14:34:31 +0000910 (void) CopyMagickString(write_info->filename,"GROUP4:",MaxTextExtent);
911 (void) CopyMagickString(write_info->magick,"GROUP4",MaxTextExtent);
cristy018f07f2011-09-04 21:15:19 +0000912 group4_image=CloneImage(inject_image,0,0,MagickTrue,exception);
cristy80975862009-09-25 14:34:31 +0000913 if (group4_image == (Image *) NULL)
914 return(MagickFalse);
915 group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
cristy018f07f2011-09-04 21:15:19 +0000916 exception);
cristy80975862009-09-25 14:34:31 +0000917 group4_image=DestroyImage(group4_image);
918 if (group4 == (unsigned char *) NULL)
919 return(MagickFalse);
cristy47b838c2009-09-19 16:09:30 +0000920 write_info=DestroyImageInfo(write_info);
cristy80975862009-09-25 14:34:31 +0000921 if (WriteBlob(image,length,group4) != (ssize_t) length)
922 status=MagickFalse;
923 group4=(unsigned char *) RelinquishMagickMemory(group4);
924 return(status);
cristy47b838c2009-09-19 16:09:30 +0000925}
926
cristy1e178e72011-08-28 19:44:34 +0000927static MagickBooleanType WritePDFImage(const ImageInfo *image_info,Image *image,
928 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000929{
930#define CFormat "/Filter [ /%s ]\n"
931#define ObjectsPerImage 14
932
933 static const char
934 XMPProfile[]=
935 {
936 "<?xpacket begin=\"%s\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n"
937 "<x:xmpmeta xmlns:x=\"adobe:ns:meta/\" x:xmptk=\"Adobe XMP Core 4.0-c316 44.253921, Sun Oct 01 2006 17:08:23\">\n"
938 " <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n"
939 " <rdf:Description rdf:about=\"\"\n"
940 " xmlns:xap=\"http://ns.adobe.com/xap/1.0/\">\n"
941 " <xap:ModifyDate>%s</xap:ModifyDate>\n"
942 " <xap:CreateDate>%s</xap:CreateDate>\n"
943 " <xap:MetadataDate>%s</xap:MetadataDate>\n"
944 " <xap:CreatorTool>%s</xap:CreatorTool>\n"
945 " </rdf:Description>\n"
946 " <rdf:Description rdf:about=\"\"\n"
947 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"
948 " <dc:format>application/pdf</dc:format>\n"
949 " </rdf:Description>\n"
950 " <rdf:Description rdf:about=\"\"\n"
951 " xmlns:xapMM=\"http://ns.adobe.com/xap/1.0/mm/\">\n"
952 " <xapMM:DocumentID>uuid:6ec119d7-7982-4f56-808d-dfe64f5b35cf</xapMM:DocumentID>\n"
953 " <xapMM:InstanceID>uuid:a79b99b4-6235-447f-9f6c-ec18ef7555cb</xapMM:InstanceID>\n"
954 " </rdf:Description>\n"
955 " <rdf:Description rdf:about=\"\"\n"
956 " xmlns:pdf=\"http://ns.adobe.com/pdf/1.3/\">\n"
957 " <pdf:Producer>%s</pdf:Producer>\n"
958 " </rdf:Description>\n"
959 " <rdf:Description rdf:about=\"\"\n"
960 " xmlns:pdfaid=\"http://www.aiim.org/pdfa/ns/id/\">\n"
961 " <pdfaid:part>1</pdfaid:part>\n"
962 " <pdfaid:conformance>B</pdfaid:conformance>\n"
963 " </rdf:Description>\n"
964 " </rdf:RDF>\n"
965 "</x:xmpmeta>\n"
966 "<?xpacket end=\"w\"?>\n"
967 },
968 XMPProfileMagick[4]= { (char) 0xef, (char) 0xbb, (char) 0xbf, (char) 0x00 };
969
970 char
cristy57015272010-12-30 01:16:38 +0000971 basename[MaxTextExtent],
cristy3ed852e2009-09-05 21:47:34 +0000972 buffer[MaxTextExtent],
973 date[MaxTextExtent],
974 **labels,
975 page_geometry[MaxTextExtent];
976
977 CompressionType
978 compression;
979
980 const char
981 *value;
982
983 double
984 pointsize;
985
986 GeometryInfo
987 geometry_info;
988
cristy3ed852e2009-09-05 21:47:34 +0000989 Image
990 *next,
991 *tile_image;
992
993 MagickBooleanType
994 status;
995
996 MagickOffsetType
997 offset,
998 scene,
999 *xref;
1000
1001 MagickSizeType
1002 number_pixels;
1003
1004 MagickStatusType
1005 flags;
1006
1007 PointInfo
1008 delta,
1009 resolution,
1010 scale;
1011
1012 RectangleInfo
1013 geometry,
1014 media_info,
1015 page_info;
1016
cristy4c08aed2011-07-01 19:47:50 +00001017 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +00001018 *p;
1019
1020 register unsigned char
1021 *q;
1022
cristybb503372010-05-27 20:51:26 +00001023 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001024 i,
1025 x;
1026
1027 size_t
cristyaff6d802011-04-26 01:46:31 +00001028 info_id,
1029 length,
1030 object,
1031 pages_id,
1032 root_id,
1033 text_size,
1034 version;
1035
1036 ssize_t
1037 count,
1038 y;
cristy3ed852e2009-09-05 21:47:34 +00001039
1040 struct tm
1041 local_time;
1042
1043 time_t
1044 seconds;
1045
1046 unsigned char
1047 *pixels;
1048
cristy3ed852e2009-09-05 21:47:34 +00001049 /*
1050 Open output image file.
1051 */
1052 assert(image_info != (const ImageInfo *) NULL);
1053 assert(image_info->signature == MagickSignature);
1054 assert(image != (Image *) NULL);
1055 assert(image->signature == MagickSignature);
1056 if (image->debug != MagickFalse)
1057 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +00001058 assert(exception != (ExceptionInfo *) NULL);
1059 assert(exception->signature == MagickSignature);
cristy1e178e72011-08-28 19:44:34 +00001060 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +00001061 if (status == MagickFalse)
1062 return(status);
1063 /*
1064 Allocate X ref memory.
1065 */
1066 xref=(MagickOffsetType *) AcquireQuantumMemory(2048UL,sizeof(*xref));
1067 if (xref == (MagickOffsetType *) NULL)
1068 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1069 (void) ResetMagickMemory(xref,0,2048UL*sizeof(*xref));
1070 /*
1071 Write Info object.
1072 */
1073 object=0;
1074 version=3;
1075 if (image_info->compression == JPEG2000Compression)
cristybb503372010-05-27 20:51:26 +00001076 version=(size_t) MagickMax(version,5);
cristy3ed852e2009-09-05 21:47:34 +00001077 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
1078 if (next->matte != MagickFalse)
cristybb503372010-05-27 20:51:26 +00001079 version=(size_t) MagickMax(version,4);
cristy3ed852e2009-09-05 21:47:34 +00001080 if (LocaleCompare(image_info->magick,"PDFA") == 0)
cristyc04c60e2011-05-31 23:58:45 +00001081 version=(size_t) MagickMax(version,6);
cristyb51dff52011-05-19 16:55:47 +00001082 (void) FormatLocaleString(buffer,MaxTextExtent,"%%PDF-1.%.20g \n",
cristye8c25f92010-06-03 00:53:06 +00001083 (double) version);
cristy3ed852e2009-09-05 21:47:34 +00001084 (void) WriteBlobString(image,buffer);
1085 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1086 (void) WriteBlobString(image,"%âãÏÓ\n");
1087 /*
1088 Write Catalog object.
1089 */
1090 xref[object++]=TellBlob(image);
1091 root_id=object;
cristyb51dff52011-05-19 16:55:47 +00001092 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristye8c25f92010-06-03 00:53:06 +00001093 object);
cristy3ed852e2009-09-05 21:47:34 +00001094 (void) WriteBlobString(image,buffer);
1095 (void) WriteBlobString(image,"<<\n");
1096 if (LocaleCompare(image_info->magick,"PDFA") != 0)
cristyb51dff52011-05-19 16:55:47 +00001097 (void) FormatLocaleString(buffer,MaxTextExtent,"/Pages %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001098 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001099 else
1100 {
cristyb51dff52011-05-19 16:55:47 +00001101 (void) FormatLocaleString(buffer,MaxTextExtent,"/Metadata %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001102 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001103 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001104 (void) FormatLocaleString(buffer,MaxTextExtent,"/Pages %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001105 (double) object+2);
cristy3ed852e2009-09-05 21:47:34 +00001106 }
1107 (void) WriteBlobString(image,buffer);
1108 (void) WriteBlobString(image,"/Type /Catalog\n");
1109 (void) WriteBlobString(image,">>\n");
1110 (void) WriteBlobString(image,"endobj\n");
1111 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1112 {
1113 char
1114 create_date[MaxTextExtent],
1115 modify_date[MaxTextExtent],
1116 timestamp[MaxTextExtent],
1117 xmp_profile[MaxTextExtent];
1118
cristybb503372010-05-27 20:51:26 +00001119 size_t
cristy3ed852e2009-09-05 21:47:34 +00001120 version;
1121
1122 /*
1123 Write XMP object.
1124 */
1125 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001126 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",
cristye8c25f92010-06-03 00:53:06 +00001127 (double) object);
cristy3ed852e2009-09-05 21:47:34 +00001128 (void) WriteBlobString(image,buffer);
1129 (void) WriteBlobString(image,"<<\n");
1130 (void) WriteBlobString(image,"/Subtype /XML\n");
1131 *modify_date='\0';
cristyd15e6592011-10-15 00:13:06 +00001132 value=GetImageProperty(image,"date:modify",exception);
cristy3ed852e2009-09-05 21:47:34 +00001133 if (value != (const char *) NULL)
1134 (void) CopyMagickString(modify_date,value,MaxTextExtent);
1135 *create_date='\0';
cristyd15e6592011-10-15 00:13:06 +00001136 value=GetImageProperty(image,"date:create",exception);
cristy3ed852e2009-09-05 21:47:34 +00001137 if (value != (const char *) NULL)
1138 (void) CopyMagickString(create_date,value,MaxTextExtent);
1139 (void) FormatMagickTime(time((time_t *) NULL),MaxTextExtent,timestamp);
cristyb51dff52011-05-19 16:55:47 +00001140 i=FormatLocaleString(xmp_profile,MaxTextExtent,XMPProfile,
cristy3ed852e2009-09-05 21:47:34 +00001141 XMPProfileMagick,modify_date,create_date,timestamp,
1142 GetMagickVersion(&version),GetMagickVersion(&version));
cristyb51dff52011-05-19 16:55:47 +00001143 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g\n",
cristye8c25f92010-06-03 00:53:06 +00001144 (double) i);
cristy3ed852e2009-09-05 21:47:34 +00001145 (void) WriteBlobString(image,buffer);
1146 (void) WriteBlobString(image,"/Type /Metadata\n");
1147 (void) WriteBlobString(image,">>\nstream\n");
1148 (void) WriteBlobString(image,xmp_profile);
1149 (void) WriteBlobString(image,"endstream\n");
1150 (void) WriteBlobString(image,"endobj\n");
1151 }
1152 /*
1153 Write Pages object.
1154 */
1155 xref[object++]=TellBlob(image);
1156 pages_id=object;
cristyb51dff52011-05-19 16:55:47 +00001157 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001158 object);
cristy3ed852e2009-09-05 21:47:34 +00001159 (void) WriteBlobString(image,buffer);
1160 (void) WriteBlobString(image,"<<\n");
1161 (void) WriteBlobString(image,"/Type /Pages\n");
cristyb51dff52011-05-19 16:55:47 +00001162 (void) FormatLocaleString(buffer,MaxTextExtent,"/Kids [ %.20g 0 R ",
cristye8c25f92010-06-03 00:53:06 +00001163 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001164 (void) WriteBlobString(image,buffer);
cristybb503372010-05-27 20:51:26 +00001165 count=(ssize_t) (pages_id+ObjectsPerImage+1);
cristy3ed852e2009-09-05 21:47:34 +00001166 if (image_info->adjoin != MagickFalse)
1167 {
1168 Image
1169 *kid_image;
1170
1171 /*
1172 Predict page object id's.
1173 */
1174 kid_image=image;
1175 for ( ; GetNextImageInList(kid_image) != (Image *) NULL; count+=ObjectsPerImage)
1176 {
cristyb51dff52011-05-19 16:55:47 +00001177 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 R ",(double)
cristye8c25f92010-06-03 00:53:06 +00001178 count);
cristy3ed852e2009-09-05 21:47:34 +00001179 (void) WriteBlobString(image,buffer);
1180 kid_image=GetNextImageInList(kid_image);
1181 }
1182 xref=(MagickOffsetType *) ResizeQuantumMemory(xref,(size_t) count+2048UL,
1183 sizeof(*xref));
1184 if (xref == (MagickOffsetType *) NULL)
1185 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1186 }
1187 (void) WriteBlobString(image,"]\n");
cristyb51dff52011-05-19 16:55:47 +00001188 (void) FormatLocaleString(buffer,MaxTextExtent,"/Count %.20g\n",
cristye8c25f92010-06-03 00:53:06 +00001189 (double) ((count-pages_id)/ObjectsPerImage));
cristy3ed852e2009-09-05 21:47:34 +00001190 (void) WriteBlobString(image,buffer);
1191 (void) WriteBlobString(image,">>\n");
1192 (void) WriteBlobString(image,"endobj\n");
1193 scene=0;
1194 do
1195 {
1196 compression=image->compression;
1197 if (image_info->compression != UndefinedCompression)
1198 compression=image_info->compression;
1199 switch (compression)
1200 {
1201 case FaxCompression:
1202 case Group4Compression:
1203 {
cristy1e178e72011-08-28 19:44:34 +00001204 if ((IsImageMonochrome(image,exception) == MagickFalse) ||
cristy3ed852e2009-09-05 21:47:34 +00001205 (image->matte != MagickFalse))
1206 compression=RLECompression;
1207 break;
1208 }
1209#if !defined(MAGICKCORE_JPEG_DELEGATE)
1210 case JPEGCompression:
1211 {
1212 compression=RLECompression;
cristy1e178e72011-08-28 19:44:34 +00001213 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00001214 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
1215 image->filename);
1216 break;
1217 }
1218#endif
1219#if !defined(MAGICKCORE_JP2_DELEGATE)
1220 case JPEG2000Compression:
1221 {
1222 compression=RLECompression;
cristy1e178e72011-08-28 19:44:34 +00001223 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00001224 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JP2)",
1225 image->filename);
1226 break;
1227 }
1228#endif
1229#if !defined(MAGICKCORE_ZLIB_DELEGATE)
1230 case ZipCompression:
1231 {
1232 compression=RLECompression;
cristy1e178e72011-08-28 19:44:34 +00001233 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00001234 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZLIB)",
1235 image->filename);
1236 break;
1237 }
1238#endif
1239 case LZWCompression:
1240 {
1241 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1242 compression=RLECompression; /* LZW compression is forbidden */
1243 break;
1244 }
1245 case NoCompression:
1246 {
1247 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1248 compression=RLECompression; /* ASCII 85 compression is forbidden */
1249 break;
1250 }
1251 default:
1252 break;
1253 }
1254 if (compression == JPEG2000Compression)
1255 {
cristy501c5592012-04-18 12:45:09 +00001256 if (IssRGBColorspace(image->colorspace) == MagickFalse)
cristy8d951092012-02-08 18:54:56 +00001257 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00001258 }
1259 /*
1260 Scale relative to dots-per-inch.
1261 */
1262 delta.x=DefaultResolution;
1263 delta.y=DefaultResolution;
cristy2a11bef2011-10-28 18:33:11 +00001264 resolution.x=image->resolution.x;
1265 resolution.y=image->resolution.y;
cristy3ed852e2009-09-05 21:47:34 +00001266 if ((resolution.x == 0.0) || (resolution.y == 0.0))
1267 {
1268 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
1269 resolution.x=geometry_info.rho;
1270 resolution.y=geometry_info.sigma;
1271 if ((flags & SigmaValue) == 0)
1272 resolution.y=resolution.x;
1273 }
1274 if (image_info->density != (char *) NULL)
1275 {
1276 flags=ParseGeometry(image_info->density,&geometry_info);
1277 resolution.x=geometry_info.rho;
1278 resolution.y=geometry_info.sigma;
1279 if ((flags & SigmaValue) == 0)
1280 resolution.y=resolution.x;
1281 }
1282 if (image->units == PixelsPerCentimeterResolution)
1283 {
cristya97426c2011-02-04 01:41:27 +00001284 resolution.x=(double) ((size_t) (100.0*2.54*resolution.x+0.5)/100.0);
1285 resolution.y=(double) ((size_t) (100.0*2.54*resolution.y+0.5)/100.0);
cristy3ed852e2009-09-05 21:47:34 +00001286 }
1287 SetGeometry(image,&geometry);
cristyb51dff52011-05-19 16:55:47 +00001288 (void) FormatLocaleString(page_geometry,MaxTextExtent,"%.20gx%.20g",
cristye8c25f92010-06-03 00:53:06 +00001289 (double) image->columns,(double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001290 if (image_info->page != (char *) NULL)
1291 (void) CopyMagickString(page_geometry,image_info->page,MaxTextExtent);
1292 else
1293 if ((image->page.width != 0) && (image->page.height != 0))
cristyb51dff52011-05-19 16:55:47 +00001294 (void) FormatLocaleString(page_geometry,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00001295 "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,
1296 (double) image->page.height,(double) image->page.x,(double)
1297 image->page.y);
cristy3ed852e2009-09-05 21:47:34 +00001298 else
1299 if ((image->gravity != UndefinedGravity) &&
1300 (LocaleCompare(image_info->magick,"PDF") == 0))
1301 (void) CopyMagickString(page_geometry,PSPageGeometry,MaxTextExtent);
1302 (void) ConcatenateMagickString(page_geometry,">",MaxTextExtent);
1303 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
1304 &geometry.width,&geometry.height);
1305 scale.x=(double) (geometry.width*delta.x)/resolution.x;
cristybb503372010-05-27 20:51:26 +00001306 geometry.width=(size_t) floor(scale.x+0.5);
cristy3ed852e2009-09-05 21:47:34 +00001307 scale.y=(double) (geometry.height*delta.y)/resolution.y;
cristybb503372010-05-27 20:51:26 +00001308 geometry.height=(size_t) floor(scale.y+0.5);
cristy3ed852e2009-09-05 21:47:34 +00001309 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
cristy1e178e72011-08-28 19:44:34 +00001310 (void) ParseGravityGeometry(image,page_geometry,&page_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00001311 if (image->gravity != UndefinedGravity)
1312 {
1313 geometry.x=(-page_info.x);
cristybb503372010-05-27 20:51:26 +00001314 geometry.y=(ssize_t) (media_info.height+page_info.y-image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001315 }
1316 pointsize=12.0;
1317 if (image_info->pointsize != 0.0)
1318 pointsize=image_info->pointsize;
1319 text_size=0;
cristyd15e6592011-10-15 00:13:06 +00001320 value=GetImageProperty(image,"label",exception);
cristy3ed852e2009-09-05 21:47:34 +00001321 if (value != (const char *) NULL)
cristybb503372010-05-27 20:51:26 +00001322 text_size=(size_t) (MultilineCensus(value)*pointsize+12);
cristyda16f162011-02-19 23:52:17 +00001323 (void) text_size;
cristy3ed852e2009-09-05 21:47:34 +00001324 /*
1325 Write Page object.
1326 */
1327 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001328 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001329 object);
cristy3ed852e2009-09-05 21:47:34 +00001330 (void) WriteBlobString(image,buffer);
1331 (void) WriteBlobString(image,"<<\n");
1332 (void) WriteBlobString(image,"/Type /Page\n");
cristyb51dff52011-05-19 16:55:47 +00001333 (void) FormatLocaleString(buffer,MaxTextExtent,"/Parent %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001334 (double) pages_id);
cristy3ed852e2009-09-05 21:47:34 +00001335 (void) WriteBlobString(image,buffer);
1336 (void) WriteBlobString(image,"/Resources <<\n");
1337 labels=(char **) NULL;
cristyd15e6592011-10-15 00:13:06 +00001338 value=GetImageProperty(image,"label",exception);
cristy3ed852e2009-09-05 21:47:34 +00001339 if (value != (const char *) NULL)
1340 labels=StringToList(value);
1341 if (labels != (char **) NULL)
1342 {
cristyb51dff52011-05-19 16:55:47 +00001343 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00001344 "/Font << /F%.20g %.20g 0 R >>\n",(double) image->scene,(double)
1345 object+4);
cristy3ed852e2009-09-05 21:47:34 +00001346 (void) WriteBlobString(image,buffer);
1347 }
cristyb51dff52011-05-19 16:55:47 +00001348 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00001349 "/XObject << /Im%.20g %.20g 0 R >>\n",(double) image->scene,(double)
1350 object+5);
cristy3ed852e2009-09-05 21:47:34 +00001351 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001352 (void) FormatLocaleString(buffer,MaxTextExtent,"/ProcSet %.20g 0 R >>\n",
cristye8c25f92010-06-03 00:53:06 +00001353 (double) object+3);
cristy3ed852e2009-09-05 21:47:34 +00001354 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001355 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00001356 "/MediaBox [0 0 %g %g]\n",72.0*media_info.width/resolution.x,
cristy8cd5b312010-01-07 01:10:24 +00001357 72.0*media_info.height/resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001358 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001359 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00001360 "/CropBox [0 0 %g %g]\n",72.0*media_info.width/resolution.x,
cristy8cd5b312010-01-07 01:10:24 +00001361 72.0*media_info.height/resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001362 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001363 (void) FormatLocaleString(buffer,MaxTextExtent,"/Contents %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001364 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001365 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001366 (void) FormatLocaleString(buffer,MaxTextExtent,"/Thumb %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001367 (double) object+8);
cristy3ed852e2009-09-05 21:47:34 +00001368 (void) WriteBlobString(image,buffer);
1369 (void) WriteBlobString(image,">>\n");
1370 (void) WriteBlobString(image,"endobj\n");
1371 /*
1372 Write Contents object.
1373 */
1374 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001375 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001376 object);
cristy3ed852e2009-09-05 21:47:34 +00001377 (void) WriteBlobString(image,buffer);
1378 (void) WriteBlobString(image,"<<\n");
cristyb51dff52011-05-19 16:55:47 +00001379 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001380 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001381 (void) WriteBlobString(image,buffer);
1382 (void) WriteBlobString(image,">>\n");
1383 (void) WriteBlobString(image,"stream\n");
1384 offset=TellBlob(image);
1385 (void) WriteBlobString(image,"q\n");
1386 if (labels != (char **) NULL)
1387 for (i=0; labels[i] != (char *) NULL; i++)
1388 {
1389 (void) WriteBlobString(image,"BT\n");
cristyb51dff52011-05-19 16:55:47 +00001390 (void) FormatLocaleString(buffer,MaxTextExtent,"/F%.20g %g Tf\n",
cristye8c25f92010-06-03 00:53:06 +00001391 (double) image->scene,pointsize);
cristy3ed852e2009-09-05 21:47:34 +00001392 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001393 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g Td\n",
cristye8c25f92010-06-03 00:53:06 +00001394 (double) geometry.x,(double) (geometry.y+geometry.height+i*pointsize+
1395 12));
cristy3ed852e2009-09-05 21:47:34 +00001396 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001397 (void) FormatLocaleString(buffer,MaxTextExtent,"(%s) Tj\n",labels[i]);
cristy3ed852e2009-09-05 21:47:34 +00001398 (void) WriteBlobString(image,buffer);
1399 (void) WriteBlobString(image,"ET\n");
1400 labels[i]=DestroyString(labels[i]);
1401 }
cristyb51dff52011-05-19 16:55:47 +00001402 (void) FormatLocaleString(buffer,MaxTextExtent,"%g 0 0 %g %.20g %.20g cm\n",
cristye8c25f92010-06-03 00:53:06 +00001403 scale.x,scale.y,(double) geometry.x,(double) geometry.y);
cristy3ed852e2009-09-05 21:47:34 +00001404 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001405 (void) FormatLocaleString(buffer,MaxTextExtent,"/Im%.20g Do\n",
cristye8c25f92010-06-03 00:53:06 +00001406 (double) image->scene);
cristy3ed852e2009-09-05 21:47:34 +00001407 (void) WriteBlobString(image,buffer);
1408 (void) WriteBlobString(image,"Q\n");
1409 offset=TellBlob(image)-offset;
1410 (void) WriteBlobString(image,"endstream\n");
1411 (void) WriteBlobString(image,"endobj\n");
1412 /*
1413 Write Length object.
1414 */
1415 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001416 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001417 object);
cristy3ed852e2009-09-05 21:47:34 +00001418 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001419 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
cristy3ed852e2009-09-05 21:47:34 +00001420 (void) WriteBlobString(image,buffer);
1421 (void) WriteBlobString(image,"endobj\n");
1422 /*
1423 Write Procset object.
1424 */
1425 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001426 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",
cristye8c25f92010-06-03 00:53:06 +00001427 (double) object);
cristy3ed852e2009-09-05 21:47:34 +00001428 (void) WriteBlobString(image,buffer);
1429 if ((image->storage_class == DirectClass) || (image->colors > 256))
1430 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageC",MaxTextExtent);
1431 else
1432 if ((compression == FaxCompression) || (compression == Group4Compression))
1433 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageB",MaxTextExtent);
1434 else
1435 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageI",MaxTextExtent);
1436 (void) WriteBlobString(image,buffer);
1437 (void) WriteBlobString(image," ]\n");
1438 (void) WriteBlobString(image,"endobj\n");
1439 /*
1440 Write Font object.
1441 */
1442 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001443 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",
cristye8c25f92010-06-03 00:53:06 +00001444 (double) object);
cristy3ed852e2009-09-05 21:47:34 +00001445 (void) WriteBlobString(image,buffer);
1446 (void) WriteBlobString(image,"<<\n");
1447 if (labels != (char **) NULL)
1448 {
1449 (void) WriteBlobString(image,"/Type /Font\n");
1450 (void) WriteBlobString(image,"/Subtype /Type1\n");
cristyb51dff52011-05-19 16:55:47 +00001451 (void) FormatLocaleString(buffer,MaxTextExtent,"/Name /F%.20g\n",
cristye8c25f92010-06-03 00:53:06 +00001452 (double) image->scene);
cristy3ed852e2009-09-05 21:47:34 +00001453 (void) WriteBlobString(image,buffer);
1454 (void) WriteBlobString(image,"/BaseFont /Helvetica\n");
1455 (void) WriteBlobString(image,"/Encoding /MacRomanEncoding\n");
1456 labels=(char **) RelinquishMagickMemory(labels);
1457 }
1458 (void) WriteBlobString(image,">>\n");
1459 (void) WriteBlobString(image,"endobj\n");
1460 /*
1461 Write XObject object.
1462 */
1463 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001464 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001465 object);
cristy3ed852e2009-09-05 21:47:34 +00001466 (void) WriteBlobString(image,buffer);
1467 (void) WriteBlobString(image,"<<\n");
1468 (void) WriteBlobString(image,"/Type /XObject\n");
1469 (void) WriteBlobString(image,"/Subtype /Image\n");
cristyb51dff52011-05-19 16:55:47 +00001470 (void) FormatLocaleString(buffer,MaxTextExtent,"/Name /Im%.20g\n",
cristye8c25f92010-06-03 00:53:06 +00001471 (double) image->scene);
cristy3ed852e2009-09-05 21:47:34 +00001472 (void) WriteBlobString(image,buffer);
1473 switch (compression)
1474 {
1475 case NoCompression:
1476 {
cristyb51dff52011-05-19 16:55:47 +00001477 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"ASCII85Decode");
cristy3ed852e2009-09-05 21:47:34 +00001478 break;
1479 }
1480 case JPEGCompression:
1481 {
cristyb51dff52011-05-19 16:55:47 +00001482 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"DCTDecode");
cristy3ed852e2009-09-05 21:47:34 +00001483 if (image->colorspace != CMYKColorspace)
1484 break;
1485 (void) WriteBlobString(image,buffer);
1486 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1487 MaxTextExtent);
1488 break;
1489 }
1490 case JPEG2000Compression:
1491 {
cristyb51dff52011-05-19 16:55:47 +00001492 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"JPXDecode");
cristy3ed852e2009-09-05 21:47:34 +00001493 if (image->colorspace != CMYKColorspace)
1494 break;
1495 (void) WriteBlobString(image,buffer);
1496 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
1497 MaxTextExtent);
1498 break;
1499 }
1500 case LZWCompression:
1501 {
cristyb51dff52011-05-19 16:55:47 +00001502 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"LZWDecode");
cristyfa7becb2009-09-13 02:44:40 +00001503 break;
cristy3ed852e2009-09-05 21:47:34 +00001504 }
1505 case ZipCompression:
1506 {
cristyb51dff52011-05-19 16:55:47 +00001507 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,"FlateDecode");
cristyfa7becb2009-09-13 02:44:40 +00001508 break;
cristy3ed852e2009-09-05 21:47:34 +00001509 }
1510 case FaxCompression:
1511 case Group4Compression:
1512 {
1513 (void) CopyMagickString(buffer,"/Filter [ /CCITTFaxDecode ]\n",
1514 MaxTextExtent);
1515 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001516 (void) FormatLocaleString(buffer,MaxTextExtent,"/DecodeParms [ << "
cristye8c25f92010-06-03 00:53:06 +00001517 "/K %s /BlackIs1 false /Columns %.20g /Rows %.20g >> ]\n",CCITTParam,
1518 (double) image->columns,(double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001519 break;
1520 }
1521 default:
1522 {
cristyb51dff52011-05-19 16:55:47 +00001523 (void) FormatLocaleString(buffer,MaxTextExtent,CFormat,
cristy3ed852e2009-09-05 21:47:34 +00001524 "RunLengthDecode");
1525 break;
1526 }
1527 }
1528 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001529 (void) FormatLocaleString(buffer,MaxTextExtent,"/Width %.20g\n",(double)
cristye8c25f92010-06-03 00:53:06 +00001530 image->columns);
cristy3ed852e2009-09-05 21:47:34 +00001531 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001532 (void) FormatLocaleString(buffer,MaxTextExtent,"/Height %.20g\n",(double)
cristye8c25f92010-06-03 00:53:06 +00001533 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001534 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001535 (void) FormatLocaleString(buffer,MaxTextExtent,"/ColorSpace %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001536 (double) object+2);
cristy3ed852e2009-09-05 21:47:34 +00001537 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001538 (void) FormatLocaleString(buffer,MaxTextExtent,"/BitsPerComponent %d\n",
cristy3ed852e2009-09-05 21:47:34 +00001539 (compression == FaxCompression) || (compression == Group4Compression) ?
1540 1 : 8);
1541 (void) WriteBlobString(image,buffer);
1542 if (image->matte != MagickFalse)
1543 {
cristyb51dff52011-05-19 16:55:47 +00001544 (void) FormatLocaleString(buffer,MaxTextExtent,"/SMask %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001545 (double) object+7);
cristy3ed852e2009-09-05 21:47:34 +00001546 (void) WriteBlobString(image,buffer);
1547 }
cristyb51dff52011-05-19 16:55:47 +00001548 (void) FormatLocaleString(buffer,MaxTextExtent,"/Length %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001549 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001550 (void) WriteBlobString(image,buffer);
1551 (void) WriteBlobString(image,">>\n");
1552 (void) WriteBlobString(image,"stream\n");
1553 offset=TellBlob(image);
1554 number_pixels=(MagickSizeType) image->columns*image->rows;
1555 if ((4*number_pixels) != (MagickSizeType) ((size_t) (4*number_pixels)))
1556 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1557 if ((compression == FaxCompression) || (compression == Group4Compression) ||
1558 ((image_info->type != TrueColorType) &&
cristy1e178e72011-08-28 19:44:34 +00001559 (IsImageGray(image,exception) != MagickFalse)))
cristy3ed852e2009-09-05 21:47:34 +00001560 {
1561 switch (compression)
1562 {
1563 case FaxCompression:
1564 case Group4Compression:
1565 {
1566 if (LocaleCompare(CCITTParam,"0") == 0)
1567 {
cristy018f07f2011-09-04 21:15:19 +00001568 (void) HuffmanEncodeImage(image_info,image,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001569 break;
1570 }
cristy018f07f2011-09-04 21:15:19 +00001571 (void) Huffman2DEncodeImage(image_info,image,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001572 break;
1573 }
1574 case JPEGCompression:
1575 {
cristy1e178e72011-08-28 19:44:34 +00001576 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
cristy3ed852e2009-09-05 21:47:34 +00001577 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00001578 {
1579 (void) CloseBlob(image);
1580 return(MagickFalse);
1581 }
cristy3ed852e2009-09-05 21:47:34 +00001582 break;
1583 }
1584 case JPEG2000Compression:
1585 {
cristy1e178e72011-08-28 19:44:34 +00001586 status=InjectImageBlob(image_info,image,image,"jp2",exception);
cristy3ed852e2009-09-05 21:47:34 +00001587 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00001588 {
1589 (void) CloseBlob(image);
1590 return(MagickFalse);
1591 }
cristy3ed852e2009-09-05 21:47:34 +00001592 break;
1593 }
1594 case RLECompression:
1595 default:
1596 {
1597 /*
1598 Allocate pixel array.
1599 */
1600 length=(size_t) number_pixels;
1601 pixels=(unsigned char *) AcquireQuantumMemory(length,
1602 sizeof(*pixels));
1603 if (pixels == (unsigned char *) NULL)
1604 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1605 /*
1606 Dump Runlength encoded pixels.
1607 */
1608 q=pixels;
cristybb503372010-05-27 20:51:26 +00001609 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001610 {
cristy1e178e72011-08-28 19:44:34 +00001611 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001612 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001613 break;
cristybb503372010-05-27 20:51:26 +00001614 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001615 {
cristy4c08aed2011-07-01 19:47:50 +00001616 *q++=ScaleQuantumToChar(GetPixelIntensity(image,p));
cristyed231572011-07-14 02:18:59 +00001617 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001618 }
1619 if (image->previous == (Image *) NULL)
1620 {
cristyf5c61ba2010-12-17 00:58:04 +00001621 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1622 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001623 if (status == MagickFalse)
1624 break;
1625 }
1626 }
1627#if defined(MAGICKCORE_ZLIB_DELEGATE)
1628 if (compression == ZipCompression)
cristy018f07f2011-09-04 21:15:19 +00001629 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001630 else
1631#endif
1632 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00001633 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001634 else
cristy018f07f2011-09-04 21:15:19 +00001635 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001636 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1637 if (status == MagickFalse)
1638 {
1639 (void) CloseBlob(image);
1640 return(MagickFalse);
1641 }
1642 break;
1643 }
1644 case NoCompression:
1645 {
1646 /*
1647 Dump uncompressed PseudoColor packets.
1648 */
1649 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00001650 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001651 {
cristy1e178e72011-08-28 19:44:34 +00001652 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001653 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001654 break;
cristybb503372010-05-27 20:51:26 +00001655 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001656 {
cristy4c08aed2011-07-01 19:47:50 +00001657 Ascii85Encode(image,ScaleQuantumToChar(
1658 GetPixelIntensity(image,p)));
cristyed231572011-07-14 02:18:59 +00001659 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001660 }
1661 if (image->previous == (Image *) NULL)
1662 {
cristyf5c61ba2010-12-17 00:58:04 +00001663 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1664 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001665 if (status == MagickFalse)
1666 break;
1667 }
1668 }
1669 Ascii85Flush(image);
1670 break;
1671 }
1672 }
1673 }
1674 else
1675 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1676 (compression == JPEGCompression) ||
1677 (compression == JPEG2000Compression))
1678 switch (compression)
1679 {
1680 case JPEGCompression:
1681 {
cristy1e178e72011-08-28 19:44:34 +00001682 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
cristy3ed852e2009-09-05 21:47:34 +00001683 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00001684 {
1685 (void) CloseBlob(image);
1686 return(MagickFalse);
1687 }
cristy3ed852e2009-09-05 21:47:34 +00001688 break;
1689 }
1690 case JPEG2000Compression:
1691 {
cristy1e178e72011-08-28 19:44:34 +00001692 status=InjectImageBlob(image_info,image,image,"jp2",exception);
cristy3ed852e2009-09-05 21:47:34 +00001693 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00001694 {
1695 (void) CloseBlob(image);
1696 return(MagickFalse);
1697 }
cristy3ed852e2009-09-05 21:47:34 +00001698 break;
1699 }
1700 case RLECompression:
1701 default:
1702 {
1703 /*
1704 Allocate pixel array.
1705 */
1706 length=(size_t) number_pixels;
1707 pixels=(unsigned char *) AcquireQuantumMemory(length,
1708 4*sizeof(*pixels));
1709 length*=image->colorspace == CMYKColorspace ? 4UL : 3UL;
1710 if (pixels == (unsigned char *) NULL)
1711 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1712 /*
1713 Dump runoffset encoded pixels.
1714 */
1715 q=pixels;
cristybb503372010-05-27 20:51:26 +00001716 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001717 {
cristy1e178e72011-08-28 19:44:34 +00001718 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001719 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001720 break;
cristybb503372010-05-27 20:51:26 +00001721 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001722 {
cristy4c08aed2011-07-01 19:47:50 +00001723 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1724 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1725 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
cristy3ed852e2009-09-05 21:47:34 +00001726 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00001727 *q++=ScaleQuantumToChar(GetPixelBlack(image,p));
cristyed231572011-07-14 02:18:59 +00001728 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001729 }
1730 if (image->previous == (Image *) NULL)
1731 {
cristyf5c61ba2010-12-17 00:58:04 +00001732 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1733 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001734 if (status == MagickFalse)
1735 break;
1736 }
1737 }
1738#if defined(MAGICKCORE_ZLIB_DELEGATE)
1739 if (compression == ZipCompression)
cristy018f07f2011-09-04 21:15:19 +00001740 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001741 else
1742#endif
1743 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00001744 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001745 else
cristy018f07f2011-09-04 21:15:19 +00001746 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001747 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1748 if (status == MagickFalse)
1749 {
1750 (void) CloseBlob(image);
1751 return(MagickFalse);
1752 }
1753 break;
1754 }
1755 case NoCompression:
1756 {
1757 /*
1758 Dump uncompressed DirectColor packets.
1759 */
1760 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00001761 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001762 {
cristy1e178e72011-08-28 19:44:34 +00001763 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001764 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001765 break;
cristybb503372010-05-27 20:51:26 +00001766 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001767 {
cristy4c08aed2011-07-01 19:47:50 +00001768 Ascii85Encode(image,ScaleQuantumToChar(GetPixelRed(image,p)));
1769 Ascii85Encode(image,ScaleQuantumToChar(GetPixelGreen(image,p)));
1770 Ascii85Encode(image,ScaleQuantumToChar(GetPixelBlue(image,p)));
cristy3ed852e2009-09-05 21:47:34 +00001771 if (image->colorspace == CMYKColorspace)
cristyaff6d802011-04-26 01:46:31 +00001772 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +00001773 GetPixelBlack(image,p)));
cristyed231572011-07-14 02:18:59 +00001774 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001775 }
1776 if (image->previous == (Image *) NULL)
1777 {
cristyf5c61ba2010-12-17 00:58:04 +00001778 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1779 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001780 if (status == MagickFalse)
1781 break;
1782 }
1783 }
1784 Ascii85Flush(image);
1785 break;
1786 }
1787 }
1788 else
1789 {
1790 /*
1791 Dump number of colors and colormap.
1792 */
1793 switch (compression)
1794 {
1795 case RLECompression:
1796 default:
1797 {
1798 /*
1799 Allocate pixel array.
1800 */
1801 length=(size_t) number_pixels;
1802 pixels=(unsigned char *) AcquireQuantumMemory(length,
1803 sizeof(*pixels));
1804 if (pixels == (unsigned char *) NULL)
1805 ThrowWriterException(ResourceLimitError,
1806 "MemoryAllocationFailed");
1807 /*
1808 Dump Runlength encoded pixels.
1809 */
1810 q=pixels;
cristybb503372010-05-27 20:51:26 +00001811 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001812 {
cristy1e178e72011-08-28 19:44:34 +00001813 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001814 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001815 break;
cristybb503372010-05-27 20:51:26 +00001816 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00001817 {
1818 *q++=(unsigned char) GetPixelIndex(image,p);
cristyed231572011-07-14 02:18:59 +00001819 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001820 }
cristy3ed852e2009-09-05 21:47:34 +00001821 if (image->previous == (Image *) NULL)
1822 {
cristyf5c61ba2010-12-17 00:58:04 +00001823 status=SetImageProgress(image,SaveImageTag,
1824 (MagickOffsetType) y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001825 if (status == MagickFalse)
1826 break;
1827 }
1828 }
1829#if defined(MAGICKCORE_ZLIB_DELEGATE)
1830 if (compression == ZipCompression)
cristy018f07f2011-09-04 21:15:19 +00001831 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001832 else
1833#endif
1834 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00001835 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001836 else
cristy018f07f2011-09-04 21:15:19 +00001837 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001838 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1839 if (status == MagickFalse)
1840 {
1841 (void) CloseBlob(image);
1842 return(MagickFalse);
1843 }
1844 break;
1845 }
1846 case NoCompression:
1847 {
1848 /*
1849 Dump uncompressed PseudoColor packets.
1850 */
1851 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00001852 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001853 {
cristy1e178e72011-08-28 19:44:34 +00001854 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001855 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001856 break;
cristybb503372010-05-27 20:51:26 +00001857 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00001858 {
1859 Ascii85Encode(image,(unsigned char) GetPixelIndex(image,p));
cristyed231572011-07-14 02:18:59 +00001860 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001861 }
cristy3ed852e2009-09-05 21:47:34 +00001862 if (image->previous == (Image *) NULL)
1863 {
cristyf5c61ba2010-12-17 00:58:04 +00001864 status=SetImageProgress(image,SaveImageTag,
1865 (MagickOffsetType) y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001866 if (status == MagickFalse)
1867 break;
1868 }
1869 }
1870 Ascii85Flush(image);
1871 break;
1872 }
1873 }
1874 }
1875 offset=TellBlob(image)-offset;
1876 (void) WriteBlobString(image,"\nendstream\n");
1877 (void) WriteBlobString(image,"endobj\n");
1878 /*
1879 Write Length object.
1880 */
1881 xref[object++]=TellBlob(image);
cristyb51dff52011-05-19 16:55:47 +00001882 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001883 object);
cristy3ed852e2009-09-05 21:47:34 +00001884 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001885 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",
cristye8c25f92010-06-03 00:53:06 +00001886 (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);
cristyed231572011-07-14 02:18:59 +00002262 q+=GetPixelChannels(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);
cristyb51dff52011-05-19 16:55:47 +00002418 (void) FormatLocaleString(buffer,MaxTextExtent,"/Width %.20g\n",
cristye8c25f92010-06-03 00:53:06 +00002419 (double) 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 */
2562 offset=TellBlob(image)-xref[0]+10;
2563 (void) WriteBlobString(image,"xref\n");
cristyb51dff52011-05-19 16:55:47 +00002564 (void) FormatLocaleString(buffer,MaxTextExtent,"0 %.20g\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002565 object+1);
cristy3ed852e2009-09-05 21:47:34 +00002566 (void) WriteBlobString(image,buffer);
2567 (void) WriteBlobString(image,"0000000000 65535 f \n");
cristybb503372010-05-27 20:51:26 +00002568 for (i=0; i < (ssize_t) object; i++)
cristy3ed852e2009-09-05 21:47:34 +00002569 {
cristyb51dff52011-05-19 16:55:47 +00002570 (void) FormatLocaleString(buffer,MaxTextExtent,"%010lu 00000 n \n",
cristyf2faecf2010-05-28 19:19:36 +00002571 (unsigned long) xref[i]);
cristy3ed852e2009-09-05 21:47:34 +00002572 (void) WriteBlobString(image,buffer);
2573 }
2574 (void) WriteBlobString(image,"trailer\n");
2575 (void) WriteBlobString(image,"<<\n");
cristyb51dff52011-05-19 16:55:47 +00002576 (void) FormatLocaleString(buffer,MaxTextExtent,"/Size %.20g\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002577 object+1);
cristy3ed852e2009-09-05 21:47:34 +00002578 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002579 (void) FormatLocaleString(buffer,MaxTextExtent,"/Info %.20g 0 R\n",(double)
cristye8c25f92010-06-03 00:53:06 +00002580 info_id);
cristy3ed852e2009-09-05 21:47:34 +00002581 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002582 (void) FormatLocaleString(buffer,MaxTextExtent,"/Root %.20g 0 R\n",(double)
cristye8c25f92010-06-03 00:53:06 +00002583 root_id);
cristy3ed852e2009-09-05 21:47:34 +00002584 (void) WriteBlobString(image,buffer);
2585 (void) WriteBlobString(image,">>\n");
2586 (void) WriteBlobString(image,"startxref\n");
cristyb51dff52011-05-19 16:55:47 +00002587 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) offset);
cristy3ed852e2009-09-05 21:47:34 +00002588 (void) WriteBlobString(image,buffer);
2589 (void) WriteBlobString(image,"%%EOF\n");
2590 xref=(MagickOffsetType *) RelinquishMagickMemory(xref);
2591 (void) CloseBlob(image);
2592 return(MagickTrue);
2593}