blob: f468693063f7dffa11de4bf47b99ca4774811aa4 [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 %
cristyde984cd2013-12-01 14:49:27 +000016% Cristy %
cristy3ed852e2009-09-05 21:47:34 +000017% July 1992 %
18% %
19% %
Cristy7ce65e72015-12-12 18:03:16 -050020% Copyright 1999-2016 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"
cristy76ce6e12013-04-05 14:33:38 +000044#include "MagickCore/artifact.h"
cristy4c08aed2011-07-01 19:47:50 +000045#include "MagickCore/blob.h"
46#include "MagickCore/blob-private.h"
47#include "MagickCore/cache.h"
48#include "MagickCore/color.h"
49#include "MagickCore/color-private.h"
50#include "MagickCore/colorspace.h"
cristy510d06a2011-07-06 23:43:54 +000051#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000052#include "MagickCore/compress.h"
53#include "MagickCore/constitute.h"
54#include "MagickCore/delegate.h"
55#include "MagickCore/delegate-private.h"
56#include "MagickCore/draw.h"
57#include "MagickCore/exception.h"
58#include "MagickCore/exception-private.h"
59#include "MagickCore/geometry.h"
60#include "MagickCore/image.h"
61#include "MagickCore/image-private.h"
62#include "MagickCore/list.h"
63#include "MagickCore/magick.h"
64#include "MagickCore/memory_.h"
65#include "MagickCore/monitor.h"
66#include "MagickCore/monitor-private.h"
cristy1e37e8f2014-02-21 17:05:37 +000067#include "MagickCore/nt-base-private.h"
cristy4c08aed2011-07-01 19:47:50 +000068#include "MagickCore/option.h"
69#include "MagickCore/pixel-accessor.h"
70#include "MagickCore/profile.h"
71#include "MagickCore/property.h"
72#include "MagickCore/quantum-private.h"
73#include "MagickCore/resource_.h"
74#include "MagickCore/resize.h"
cristy1b70dc82012-07-01 23:15:05 +000075#include "MagickCore/signature.h"
cristy4c08aed2011-07-01 19:47:50 +000076#include "MagickCore/static.h"
77#include "MagickCore/string_.h"
78#include "MagickCore/module.h"
cristye40005d2012-03-23 12:18:45 +000079#include "MagickCore/token.h"
cristy4c08aed2011-07-01 19:47:50 +000080#include "MagickCore/transform.h"
81#include "MagickCore/utility.h"
82#include "MagickCore/module.h"
cristy80975862009-09-25 14:34:31 +000083
84/*
85 Define declarations.
86*/
cristy3ed852e2009-09-05 21:47:34 +000087#if defined(MAGICKCORE_TIFF_DELEGATE)
88#define CCITTParam "-1"
89#else
90#define CCITTParam "0"
91#endif
92
93/*
94 Forward declarations.
95*/
96static MagickBooleanType
cristy1e178e72011-08-28 19:44:34 +000097 WritePDFImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +000098
99/*
100%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101% %
102% %
103% %
cristy817f4cd2013-02-06 11:45:42 +0000104% I n v o k e P D F D e l e g a t e %
105% %
106% %
107% %
108%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109%
110% InvokePDFDelegate() executes the PDF interpreter with the specified command.
111%
112% The format of the InvokePDFDelegate method is:
113%
114% MagickBooleanType InvokePDFDelegate(const MagickBooleanType verbose,
115% const char *command,ExceptionInfo *exception)
116%
117% A description of each parameter follows:
118%
119% o verbose: A value other than zero displays the command prior to
120% executing it.
121%
122% o command: the address of a character string containing the command to
123% execute.
124%
125% o exception: return any errors or warnings in this structure.
126%
127*/
dirkfdec0e72014-05-09 18:45:41 +0000128#if defined(MAGICKCORE_GS_DELEGATE) || defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy26842022014-10-05 19:18:23 +0000129static int MagickDLLCall PDFDelegateMessage(void *handle,const char *message,
130 int length)
dirk52784992014-05-03 13:00:18 +0000131{
132 char
133 **messages;
134
cristy26842022014-10-05 19:18:23 +0000135 ssize_t
136 offset;
dirk52784992014-05-03 13:00:18 +0000137
138 offset=0;
cristy26842022014-10-05 19:18:23 +0000139 messages=(char **) handle;
dirk52784992014-05-03 13:00:18 +0000140 if (*messages == (char *) NULL)
cristy26842022014-10-05 19:18:23 +0000141 *messages=(char *) AcquireQuantumMemory(length+1,sizeof(char *));
dirk52784992014-05-03 13:00:18 +0000142 else
143 {
144 offset=strlen(*messages);
cristy26842022014-10-05 19:18:23 +0000145 *messages=(char *) ResizeQuantumMemory(*messages,offset+length+1,
dirk52784992014-05-03 13:00:18 +0000146 sizeof(char *));
147 }
cristy26842022014-10-05 19:18:23 +0000148 (void) memcpy(*messages+offset,message,length);
149 (*messages)[length+offset] ='\0';
150 return(length);
dirk52784992014-05-03 13:00:18 +0000151}
dirkfdec0e72014-05-09 18:45:41 +0000152#endif
dirk52784992014-05-03 13:00:18 +0000153
cristy817f4cd2013-02-06 11:45:42 +0000154static MagickBooleanType InvokePDFDelegate(const MagickBooleanType verbose,
cristy60575362014-10-18 13:22:50 +0000155 const char *command,char *message,ExceptionInfo *exception)
cristy817f4cd2013-02-06 11:45:42 +0000156{
dirkfdec0e72014-05-09 18:45:41 +0000157 int
158 status;
159
160#if defined(MAGICKCORE_GS_DELEGATE) || defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy26842022014-10-05 19:18:23 +0000161#define SetArgsStart(command,args_start) \
dirk52784992014-05-03 13:00:18 +0000162 if (args_start == (const char *) NULL) \
163 { \
cristy26842022014-10-05 19:18:23 +0000164 if (*command != '"') \
dirk52784992014-05-03 13:00:18 +0000165 args_start=strchr(command,' '); \
166 else \
167 { \
168 args_start=strchr(command+1,'"'); \
169 if (args_start != (const char *) NULL) \
170 args_start++; \
171 } \
172 }
173
cristy26842022014-10-05 19:18:23 +0000174#define ExecuteGhostscriptCommand(command,status) \
175{ \
cristy60575362014-10-18 13:22:50 +0000176 status=ExternalDelegateCommand(MagickFalse,verbose,command,message, \
cristydfc19b62014-10-17 22:52:24 +0000177 exception); \
cristy26842022014-10-05 19:18:23 +0000178 if (status == 0) \
179 return(MagickTrue); \
180 if (status < 0) \
dirkf3682e92014-05-03 16:00:28 +0000181 return(MagickFalse); \
cristy26842022014-10-05 19:18:23 +0000182 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError, \
183 "FailedToExecuteCommand","`%s' (%d)",command,status); \
184 return(MagickFalse); \
185}
dirkf3682e92014-05-03 16:00:28 +0000186
cristy817f4cd2013-02-06 11:45:42 +0000187 char
dirk52784992014-05-03 13:00:18 +0000188 **argv,
189 *errors;
cristy817f4cd2013-02-06 11:45:42 +0000190
dirkfdec0e72014-05-09 18:45:41 +0000191 const char
cristy26842022014-10-05 19:18:23 +0000192 *args_start = (const char *) NULL;
dirkfdec0e72014-05-09 18:45:41 +0000193
cristy817f4cd2013-02-06 11:45:42 +0000194 const GhostInfo
195 *ghost_info;
196
197 gs_main_instance
198 *interpreter;
199
dirk6d23b5c2015-01-05 19:52:49 +0000200 gsapi_revision_t
201 revision;
202
cristy817f4cd2013-02-06 11:45:42 +0000203 int
204 argc,
205 code;
206
207 register ssize_t
208 i;
209
210#if defined(MAGICKCORE_WINDOWS_SUPPORT)
211 ghost_info=NTGhostscriptDLLVectors();
212#else
213 GhostInfo
214 ghost_info_struct;
215
216 ghost_info=(&ghost_info_struct);
dirkefd45a02014-05-03 14:00:09 +0000217 (void) ResetMagickMemory(&ghost_info_struct,0,sizeof(ghost_info_struct));
dirk52784992014-05-03 13:00:18 +0000218 ghost_info_struct.delete_instance=(void (*)(gs_main_instance *))
219 gsapi_delete_instance;
220 ghost_info_struct.exit=(int (*)(gs_main_instance *)) gsapi_exit;
cristy817f4cd2013-02-06 11:45:42 +0000221 ghost_info_struct.new_instance=(int (*)(gs_main_instance **,void *))
222 gsapi_new_instance;
223 ghost_info_struct.init_with_args=(int (*)(gs_main_instance *,int,char **))
224 gsapi_init_with_args;
225 ghost_info_struct.run_string=(int (*)(gs_main_instance *,const char *,int,
226 int *)) gsapi_run_string;
dirk52784992014-05-03 13:00:18 +0000227 ghost_info_struct.set_stdio=(int (*)(gs_main_instance *,int(*)(void *,char *,
228 int),int(*)(void *,const char *,int),int(*)(void *, const char *, int)))
229 gsapi_set_stdio;
dirk6d23b5c2015-01-05 19:52:49 +0000230 ghost_info_struct.revision=(int (*)(gsapi_revision_t *,int)) gsapi_revision;
cristy817f4cd2013-02-06 11:45:42 +0000231#endif
232 if (ghost_info == (GhostInfo *) NULL)
dirk4dd75672014-10-12 12:52:42 +0000233 ExecuteGhostscriptCommand(command,status);
dirk6d23b5c2015-01-05 19:52:49 +0000234 if ((ghost_info->revision)(&revision,sizeof(revision)) != 0)
235 revision.revision=0;
cristy817f4cd2013-02-06 11:45:42 +0000236 if (verbose != MagickFalse)
237 {
cristy7c443542015-01-05 23:19:48 +0000238 (void) fprintf(stdout,"[ghostscript library %.2f]",(double)
239 revision.revision/100.0);
cristy26842022014-10-05 19:18:23 +0000240 SetArgsStart(command,args_start);
dirk52784992014-05-03 13:00:18 +0000241 (void) fputs(args_start,stdout);
cristy817f4cd2013-02-06 11:45:42 +0000242 }
dirk52784992014-05-03 13:00:18 +0000243 errors=(char *) NULL;
244 status=(ghost_info->new_instance)(&interpreter,(void *) &errors);
cristy817f4cd2013-02-06 11:45:42 +0000245 if (status < 0)
cristy26842022014-10-05 19:18:23 +0000246 ExecuteGhostscriptCommand(command,status);
cristy817f4cd2013-02-06 11:45:42 +0000247 code=0;
248 argv=StringToArgv(command,&argc);
249 if (argv == (char **) NULL)
dirk85acc9f2015-11-23 21:05:33 +0100250 {
251 (ghost_info->delete_instance)(interpreter);
252 return(MagickFalse);
253 }
dirk52784992014-05-03 13:00:18 +0000254 (void) (ghost_info->set_stdio)(interpreter,(int(MagickDLLCall *)(void *,
255 char *,int)) NULL,PDFDelegateMessage,PDFDelegateMessage);
cristy817f4cd2013-02-06 11:45:42 +0000256 status=(ghost_info->init_with_args)(interpreter,argc-1,argv+1);
257 if (status == 0)
258 status=(ghost_info->run_string)(interpreter,"systemdict /start get exec\n",
259 0,&code);
260 (ghost_info->exit)(interpreter);
261 (ghost_info->delete_instance)(interpreter);
cristy817f4cd2013-02-06 11:45:42 +0000262 for (i=0; i < (ssize_t) argc; i++)
263 argv[i]=DestroyString(argv[i]);
264 argv=(char **) RelinquishMagickMemory(argv);
dirk4dd75672014-10-12 12:52:42 +0000265 if (status != 0)
cristy817f4cd2013-02-06 11:45:42 +0000266 {
cristy26842022014-10-05 19:18:23 +0000267 SetArgsStart(command,args_start);
dirk2267d492014-10-12 18:32:02 +0000268 if (status == -101) /* quit */
cristy151b66d2015-04-15 10:50:31 +0000269 (void) FormatLocaleString(message,MagickPathExtent,
dirk6d23b5c2015-01-05 19:52:49 +0000270 "[ghostscript library %.2f]%s: %s",(double)revision.revision / 100,
271 args_start,errors);
dirk4dd75672014-10-12 12:52:42 +0000272 else
273 {
274 (void) ThrowMagickException(exception,GetMagickModule(),
dirk6d23b5c2015-01-05 19:52:49 +0000275 DelegateError,"PDFDelegateFailed",
276 "`[ghostscript library %.2f]%s': %s",
277 (double)revision.revision / 100,args_start,errors);
dirk4dd75672014-10-12 12:52:42 +0000278 if (errors != (char *) NULL)
279 errors=DestroyString(errors);
280 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
281 "Ghostscript returns status %d, exit code %d",status,code);
282 return(MagickFalse);
283 }
cristy817f4cd2013-02-06 11:45:42 +0000284 }
dirk52784992014-05-03 13:00:18 +0000285 if (errors != (char *) NULL)
286 errors=DestroyString(errors);
cristy817f4cd2013-02-06 11:45:42 +0000287 return(MagickTrue);
288#else
cristy60575362014-10-18 13:22:50 +0000289 status=ExternalDelegateCommand(MagickFalse,verbose,command,message,exception);
cristy817f4cd2013-02-06 11:45:42 +0000290 return(status == 0 ? MagickTrue : MagickFalse);
291#endif
292}
293
294/*
295%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
296% %
297% %
298% %
cristy3ed852e2009-09-05 21:47:34 +0000299% I s P D F %
300% %
301% %
302% %
303%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
304%
305% IsPDF() returns MagickTrue if the image format type, identified by the
306% magick string, is PDF.
307%
308% The format of the IsPDF method is:
309%
310% MagickBooleanType IsPDF(const unsigned char *magick,const size_t offset)
311%
312% A description of each parameter follows:
313%
314% o magick: compare image format pattern against these bytes.
315%
316% o offset: Specifies the offset of the magick string.
317%
318*/
319static MagickBooleanType IsPDF(const unsigned char *magick,const size_t offset)
320{
321 if (offset < 5)
322 return(MagickFalse);
cristy41083a42009-09-07 23:47:59 +0000323 if (LocaleNCompare((const char *) magick,"%PDF-",5) == 0)
cristy3ed852e2009-09-05 21:47:34 +0000324 return(MagickTrue);
325 return(MagickFalse);
326}
327
328/*
329%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
330% %
331% %
332% %
333% R e a d P D F I m a g e %
334% %
335% %
336% %
337%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
338%
339% ReadPDFImage() reads a Portable Document Format image file and
340% returns it. It allocates the memory necessary for the new Image structure
341% and returns a pointer to the new image.
342%
343% The format of the ReadPDFImage method is:
344%
345% Image *ReadPDFImage(const ImageInfo *image_info,ExceptionInfo *exception)
346%
347% A description of each parameter follows:
348%
349% o image_info: the image info.
350%
351% o exception: return any errors or warnings in this structure.
352%
353*/
354
355static MagickBooleanType IsPDFRendered(const char *path)
356{
357 MagickBooleanType
358 status;
359
360 struct stat
361 attributes;
362
363 if ((path == (const char *) NULL) || (*path == '\0'))
364 return(MagickFalse);
365 status=GetPathAttributes(path,&attributes);
366 if ((status != MagickFalse) && S_ISREG(attributes.st_mode) &&
367 (attributes.st_size > 0))
368 return(MagickTrue);
369 return(MagickFalse);
370}
371
372static Image *ReadPDFImage(const ImageInfo *image_info,ExceptionInfo *exception)
373{
cristy0a39a5c2012-06-27 12:51:45 +0000374#define CMYKProcessColor "CMYKProcessColor"
cristy3ed852e2009-09-05 21:47:34 +0000375#define CropBox "CropBox"
cristy0a39a5c2012-06-27 12:51:45 +0000376#define DefaultCMYK "DefaultCMYK"
cristy3ed852e2009-09-05 21:47:34 +0000377#define DeviceCMYK "DeviceCMYK"
378#define MediaBox "MediaBox"
379#define RenderPostscriptText "Rendering Postscript... "
380#define PDFRotate "Rotate"
381#define SpotColor "Separation"
382#define TrimBox "TrimBox"
383#define PDFVersion "PDF-"
384
385 char
cristy151b66d2015-04-15 10:50:31 +0000386 command[MagickPathExtent],
cristy51816562015-04-12 13:18:38 +0000387 *density,
cristy151b66d2015-04-15 10:50:31 +0000388 filename[MagickPathExtent],
389 geometry[MagickPathExtent],
390 input_filename[MagickPathExtent],
391 message[MagickPathExtent],
cristy51816562015-04-12 13:18:38 +0000392 *options,
cristy151b66d2015-04-15 10:50:31 +0000393 postscript_filename[MagickPathExtent];
cristy3ed852e2009-09-05 21:47:34 +0000394
395 const char
396 *option;
397
398 const DelegateInfo
399 *delegate_info;
400
401 double
402 angle;
403
cristydba40d42010-03-25 18:31:50 +0000404 GeometryInfo
405 geometry_info;
406
cristy3ed852e2009-09-05 21:47:34 +0000407 Image
408 *image,
409 *next,
410 *pdf_image;
411
412 ImageInfo
413 *read_info;
414
415 int
cristya97426c2011-02-04 01:41:27 +0000416 c,
cristy3ed852e2009-09-05 21:47:34 +0000417 file;
418
419 MagickBooleanType
420 cmyk,
421 cropbox,
dirk83c36b12014-02-23 13:07:22 +0000422 fitPage,
cristy72437802015-03-23 00:42:27 +0000423 status,
424 stop_on_error,
425 trimbox;
cristy3ed852e2009-09-05 21:47:34 +0000426
cristydba40d42010-03-25 18:31:50 +0000427 MagickStatusType
428 flags;
429
cristy3ed852e2009-09-05 21:47:34 +0000430 PointInfo
431 delta;
432
433 RectangleInfo
434 bounding_box,
435 page;
436
437 register char
438 *p;
439
cristya97426c2011-02-04 01:41:27 +0000440 register ssize_t
441 i;
cristy3ed852e2009-09-05 21:47:34 +0000442
443 SegmentInfo
444 bounds,
445 hires_bounds;
446
cristybb503372010-05-27 20:51:26 +0000447 size_t
cristy3ed852e2009-09-05 21:47:34 +0000448 scene,
449 spotcolor;
450
cristyaff6d802011-04-26 01:46:31 +0000451 ssize_t
452 count;
453
cristy3ed852e2009-09-05 21:47:34 +0000454 assert(image_info != (const ImageInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000455 assert(image_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000456 if (image_info->debug != MagickFalse)
457 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
458 image_info->filename);
459 assert(exception != (ExceptionInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000460 assert(exception->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000461 /*
462 Open image file.
463 */
cristy9950d572011-10-01 18:22:35 +0000464 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000465 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
466 if (status == MagickFalse)
467 {
468 image=DestroyImageList(image);
469 return((Image *) NULL);
470 }
471 status=AcquireUniqueSymbolicLink(image_info->filename,input_filename);
472 if (status == MagickFalse)
473 {
474 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
475 image_info->filename);
476 image=DestroyImageList(image);
477 return((Image *) NULL);
478 }
479 /*
480 Set the page density.
481 */
482 delta.x=DefaultResolution;
483 delta.y=DefaultResolution;
cristy2a11bef2011-10-28 18:33:11 +0000484 if ((image->resolution.x == 0.0) || (image->resolution.y == 0.0))
cristy3ed852e2009-09-05 21:47:34 +0000485 {
cristy3ed852e2009-09-05 21:47:34 +0000486 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
cristy2a11bef2011-10-28 18:33:11 +0000487 image->resolution.x=geometry_info.rho;
488 image->resolution.y=geometry_info.sigma;
cristy3ed852e2009-09-05 21:47:34 +0000489 if ((flags & SigmaValue) == 0)
cristy2a11bef2011-10-28 18:33:11 +0000490 image->resolution.y=image->resolution.x;
cristy3ed852e2009-09-05 21:47:34 +0000491 }
cristydeb50be2011-11-24 00:19:33 +0000492 if (image_info->density != (char *) NULL)
493 {
494 flags=ParseGeometry(image_info->density,&geometry_info);
495 image->resolution.x=geometry_info.rho;
496 image->resolution.y=geometry_info.sigma;
497 if ((flags & SigmaValue) == 0)
498 image->resolution.y=image->resolution.x;
499 }
dirk6d022892016-05-12 23:41:59 +0200500 (void) ResetMagickMemory(&page,0,sizeof(page));
cristydeb50be2011-11-24 00:19:33 +0000501 (void) ParseAbsoluteGeometry(PSPageGeometry,&page);
502 if (image_info->page != (char *) NULL)
503 (void) ParseAbsoluteGeometry(image_info->page,&page);
504 page.width=(size_t) ceil((double) (page.width*image->resolution.x/delta.x)-
505 0.5);
506 page.height=(size_t) ceil((double) (page.height*image->resolution.y/delta.y)-
507 0.5);
cristy3ed852e2009-09-05 21:47:34 +0000508 /*
509 Determine page geometry from the PDF media box.
510 */
511 cmyk=image_info->colorspace == CMYKColorspace ? MagickTrue : MagickFalse;
cristy092ec8d2013-04-26 13:46:22 +0000512 cropbox=IsStringTrue(GetImageOption(image_info,"pdf:use-cropbox"));
cristy72437802015-03-23 00:42:27 +0000513 stop_on_error=IsStringTrue(GetImageOption(image_info,"pdf:stop-on-error"));
cristy092ec8d2013-04-26 13:46:22 +0000514 trimbox=IsStringTrue(GetImageOption(image_info,"pdf:use-trimbox"));
cristy3ed852e2009-09-05 21:47:34 +0000515 count=0;
516 spotcolor=0;
517 (void) ResetMagickMemory(&bounding_box,0,sizeof(bounding_box));
518 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
519 (void) ResetMagickMemory(&hires_bounds,0,sizeof(hires_bounds));
cristy3ed852e2009-09-05 21:47:34 +0000520 (void) ResetMagickMemory(command,0,sizeof(command));
cristy3ed852e2009-09-05 21:47:34 +0000521 angle=0.0;
522 p=command;
523 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
524 {
525 /*
526 Note PDF elements.
527 */
528 if (c == '\n')
529 c=' ';
530 *p++=(char) c;
cristyfd6e87b2011-02-03 18:48:17 +0000531 if ((c != (int) '/') && (c != (int) '%') &&
cristy151b66d2015-04-15 10:50:31 +0000532 ((size_t) (p-command) < (MagickPathExtent-1)))
cristy3ed852e2009-09-05 21:47:34 +0000533 continue;
534 *(--p)='\0';
535 p=command;
536 if (LocaleNCompare(PDFRotate,command,strlen(PDFRotate)) == 0)
537 count=(ssize_t) sscanf(command,"Rotate %lf",&angle);
538 /*
539 Is this a CMYK document?
540 */
cristy0a39a5c2012-06-27 12:51:45 +0000541 if (LocaleNCompare(DefaultCMYK,command,strlen(DefaultCMYK)) == 0)
542 cmyk=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +0000543 if (LocaleNCompare(DeviceCMYK,command,strlen(DeviceCMYK)) == 0)
544 cmyk=MagickTrue;
cristy0a39a5c2012-06-27 12:51:45 +0000545 if (LocaleNCompare(CMYKProcessColor,command,strlen(CMYKProcessColor)) == 0)
546 cmyk=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +0000547 if (LocaleNCompare(SpotColor,command,strlen(SpotColor)) == 0)
548 {
549 char
cristy151b66d2015-04-15 10:50:31 +0000550 name[MagickPathExtent],
551 property[MagickPathExtent],
cristy3ed852e2009-09-05 21:47:34 +0000552 *value;
553
cristy3ed852e2009-09-05 21:47:34 +0000554 /*
555 Note spot names.
556 */
Cristy25701e62016-07-09 16:14:13 -0400557 (void) FormatLocaleString(property,MagickPathExtent,
558 "pdf:SpotColor-%.20g",(double) spotcolor++);
cristy3ed852e2009-09-05 21:47:34 +0000559 i=0;
560 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
561 {
cristy151b66d2015-04-15 10:50:31 +0000562 if ((isspace(c) != 0) || (c == '/') || ((i+1) == MagickPathExtent))
cristy3ed852e2009-09-05 21:47:34 +0000563 break;
564 name[i++]=(char) c;
565 }
566 name[i]='\0';
567 value=AcquireString(name);
568 (void) SubstituteString(&value,"#20"," ");
cristyd15e6592011-10-15 00:13:06 +0000569 (void) SetImageProperty(image,property,value,exception);
cristy3ed852e2009-09-05 21:47:34 +0000570 value=DestroyString(value);
571 continue;
572 }
573 if (LocaleNCompare(PDFVersion,command,strlen(PDFVersion)) == 0)
cristyd15e6592011-10-15 00:13:06 +0000574 (void) SetImageProperty(image,"pdf:Version",command,exception);
cristydeb50be2011-11-24 00:19:33 +0000575 if (image_info->page != (char *) NULL)
576 continue;
cristy3ed852e2009-09-05 21:47:34 +0000577 count=0;
578 if (cropbox != MagickFalse)
579 {
580 if (LocaleNCompare(CropBox,command,strlen(CropBox)) == 0)
581 {
582 /*
583 Note region defined by crop box.
584 */
585 count=(ssize_t) sscanf(command,"CropBox [%lf %lf %lf %lf",
586 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
587 if (count != 4)
588 count=(ssize_t) sscanf(command,"CropBox[%lf %lf %lf %lf",
589 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
590 }
591 }
592 else
593 if (trimbox != MagickFalse)
594 {
595 if (LocaleNCompare(TrimBox,command,strlen(TrimBox)) == 0)
596 {
597 /*
598 Note region defined by trim box.
599 */
600 count=(ssize_t) sscanf(command,"TrimBox [%lf %lf %lf %lf",
601 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
602 if (count != 4)
603 count=(ssize_t) sscanf(command,"TrimBox[%lf %lf %lf %lf",
604 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
605 }
606 }
607 else
608 if (LocaleNCompare(MediaBox,command,strlen(MediaBox)) == 0)
609 {
610 /*
611 Note region defined by media box.
612 */
613 count=(ssize_t) sscanf(command,"MediaBox [%lf %lf %lf %lf",
614 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
615 if (count != 4)
616 count=(ssize_t) sscanf(command,"MediaBox[%lf %lf %lf %lf",
617 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
618 }
619 if (count != 4)
620 continue;
cristyad29a8c2012-01-25 23:06:43 +0000621 if ((fabs(bounds.x2-bounds.x1) <= fabs(hires_bounds.x2-hires_bounds.x1)) ||
cristyc2038532011-12-29 16:43:05 +0000622 (fabs(bounds.y2-bounds.y1) <= fabs(hires_bounds.y2-hires_bounds.y1)))
623 continue;
cristy48853f12011-11-11 15:47:57 +0000624 hires_bounds=bounds;
cristy3ed852e2009-09-05 21:47:34 +0000625 }
Cristy34838f52016-07-09 20:54:46 -0400626 if ((fabs(hires_bounds.x2-hires_bounds.x1) >= MagickEpsilon) &&
cristy614bc082011-11-24 00:49:08 +0000627 (fabs(hires_bounds.y2-hires_bounds.y1) >= MagickEpsilon))
cristy48853f12011-11-11 15:47:57 +0000628 {
629 /*
630 Set PDF render geometry.
631 */
cristy151b66d2015-04-15 10:50:31 +0000632 (void) FormatLocaleString(geometry,MagickPathExtent,"%gx%g%+.15g%+.15g",
cristy48853f12011-11-11 15:47:57 +0000633 hires_bounds.x2-bounds.x1,hires_bounds.y2-hires_bounds.y1,
634 hires_bounds.x1,hires_bounds.y1);
635 (void) SetImageProperty(image,"pdf:HiResBoundingBox",geometry,exception);
cristydeb50be2011-11-24 00:19:33 +0000636 page.width=(size_t) ceil((double) ((hires_bounds.x2-hires_bounds.x1)*
637 image->resolution.x/delta.x)-0.5);
638 page.height=(size_t) ceil((double) ((hires_bounds.y2-hires_bounds.y1)*
639 image->resolution.y/delta.y)-0.5);
cristy48853f12011-11-11 15:47:57 +0000640 }
dirk83c36b12014-02-23 13:07:22 +0000641 fitPage=MagickFalse;
642 option=GetImageOption(image_info,"pdf:fit-page");
643 if (option != (char *) NULL)
644 {
645 char
dirkdb6b52c2015-11-25 23:44:01 +0100646 *page_geometry;
dirk83c36b12014-02-23 13:07:22 +0000647
dirkdb6b52c2015-11-25 23:44:01 +0100648 page_geometry=GetPageGeometry(option);
649 flags=ParseMetaGeometry(page_geometry,&page.x,&page.y,&page.width,
650 &page.height);
dirk15be7972016-07-10 15:24:44 +0200651 page_geometry=DestroyString(page_geometry);
dirk83c36b12014-02-23 13:07:22 +0000652 if (flags == NoValue)
653 {
654 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
655 "InvalidGeometry","`%s'",option);
656 image=DestroyImage(image);
657 return((Image *) NULL);
658 }
659 page.width=(size_t) ceil((double) (page.width*image->resolution.x/delta.x)
660 -0.5);
661 page.height=(size_t) ceil((double) (page.height*image->resolution.y/
662 delta.y) -0.5);
dirk83c36b12014-02-23 13:07:22 +0000663 fitPage=MagickTrue;
664 }
cristy3ed852e2009-09-05 21:47:34 +0000665 (void) CloseBlob(image);
666 if ((fabs(angle) == 90.0) || (fabs(angle) == 270.0))
667 {
cristybb503372010-05-27 20:51:26 +0000668 size_t
cristy3ed852e2009-09-05 21:47:34 +0000669 swap;
670
671 swap=page.width;
672 page.width=page.height;
673 page.height=swap;
674 }
cristy3d9f5ba2012-06-26 13:37:31 +0000675 if (IssRGBCompatibleColorspace(image_info->colorspace) != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000676 cmyk=MagickFalse;
677 /*
678 Create Ghostscript control file.
679 */
680 file=AcquireUniqueFileResource(postscript_filename);
681 if (file == -1)
682 {
683 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
684 image_info->filename);
685 image=DestroyImage(image);
686 return((Image *) NULL);
687 }
688 count=write(file," ",1);
689 file=close(file)-1;
690 /*
691 Render Postscript with the Ghostscript delegate.
692 */
Cristy8de8fb62016-04-08 17:48:07 -0400693 if (image_info->monochrome != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000694 delegate_info=GetDelegateInfo("ps:mono",(char *) NULL,exception);
695 else
696 if (cmyk != MagickFalse)
697 delegate_info=GetDelegateInfo("ps:cmyk",(char *) NULL,exception);
698 else
cristya97426c2011-02-04 01:41:27 +0000699 delegate_info=GetDelegateInfo("ps:alpha",(char *) NULL,exception);
cristy3ed852e2009-09-05 21:47:34 +0000700 if (delegate_info == (const DelegateInfo *) NULL)
701 {
702 (void) RelinquishUniqueFileResource(postscript_filename);
703 image=DestroyImage(image);
704 return((Image *) NULL);
705 }
cristy51816562015-04-12 13:18:38 +0000706 density=AcquireString("");
707 options=AcquireString("");
Cristy8de8fb62016-04-08 17:48:07 -0400708 (void) FormatLocaleString(density,MagickPathExtent,"%gx%g",
709 image->resolution.x,image->resolution.y);
dirk83c36b12014-02-23 13:07:22 +0000710 if ((image_info->page != (char *) NULL) || (fitPage != MagickFalse))
cristy151b66d2015-04-15 10:50:31 +0000711 (void) FormatLocaleString(options,MagickPathExtent,"-g%.20gx%.20g ",(double)
cristybe0063d2013-02-01 01:33:47 +0000712 page.width,(double) page.height);
dirk83c36b12014-02-23 13:07:22 +0000713 if (fitPage != MagickFalse)
cristy151b66d2015-04-15 10:50:31 +0000714 (void) ConcatenateMagickString(options,"-dPSFitPage ",MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +0000715 if (cmyk != MagickFalse)
cristy151b66d2015-04-15 10:50:31 +0000716 (void) ConcatenateMagickString(options,"-dUseCIEColor ",MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +0000717 if (cropbox != MagickFalse)
cristy151b66d2015-04-15 10:50:31 +0000718 (void) ConcatenateMagickString(options,"-dUseCropBox ",MagickPathExtent);
cristy72437802015-03-23 00:42:27 +0000719 if (stop_on_error != MagickFalse)
Cristy8de8fb62016-04-08 17:48:07 -0400720 (void) ConcatenateMagickString(options,"-dPDFSTOPONERROR ",
721 MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +0000722 if (trimbox != MagickFalse)
cristy151b66d2015-04-15 10:50:31 +0000723 (void) ConcatenateMagickString(options,"-dUseTrimBox ",MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +0000724 read_info=CloneImageInfo(image_info);
725 *read_info->magick='\0';
726 if (read_info->number_scenes != 0)
727 {
728 char
cristy151b66d2015-04-15 10:50:31 +0000729 pages[MagickPathExtent];
cristy3ed852e2009-09-05 21:47:34 +0000730
cristy151b66d2015-04-15 10:50:31 +0000731 (void) FormatLocaleString(pages,MagickPathExtent,"-dFirstPage=%.20g "
cristye8c25f92010-06-03 00:53:06 +0000732 "-dLastPage=%.20g",(double) read_info->scene+1,(double)
cristyf2faecf2010-05-28 19:19:36 +0000733 (read_info->scene+read_info->number_scenes));
cristy151b66d2015-04-15 10:50:31 +0000734 (void) ConcatenateMagickString(options,pages,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +0000735 read_info->number_scenes=0;
736 if (read_info->scenes != (char *) NULL)
737 *read_info->scenes='\0';
738 }
cristy151b66d2015-04-15 10:50:31 +0000739 (void) CopyMagickString(filename,read_info->filename,MagickPathExtent);
cristya97426c2011-02-04 01:41:27 +0000740 (void) AcquireUniqueFilename(filename);
dirkfd826fc2014-06-04 21:11:30 +0000741 (void) RelinquishUniqueFileResource(filename);
cristy151b66d2015-04-15 10:50:31 +0000742 (void) ConcatenateMagickString(filename,"%d",MagickPathExtent);
743 (void) FormatLocaleString(command,MagickPathExtent,
cristy3ed852e2009-09-05 21:47:34 +0000744 GetDelegateCommands(delegate_info),
745 read_info->antialias != MagickFalse ? 4 : 1,
cristy209d4692014-03-19 12:40:01 +0000746 read_info->antialias != MagickFalse ? 4 : 1,density,options,filename,
cristya97426c2011-02-04 01:41:27 +0000747 postscript_filename,input_filename);
cristy51816562015-04-12 13:18:38 +0000748 options=DestroyString(options);
749 density=DestroyString(density);
cristy60575362014-10-18 13:22:50 +0000750 *message='\0';
751 status=InvokePDFDelegate(read_info->verbose,command,message,exception);
cristy3ed852e2009-09-05 21:47:34 +0000752 (void) RelinquishUniqueFileResource(postscript_filename);
cristy3ed852e2009-09-05 21:47:34 +0000753 (void) RelinquishUniqueFileResource(input_filename);
cristya97426c2011-02-04 01:41:27 +0000754 pdf_image=(Image *) NULL;
755 if (status == MagickFalse)
756 for (i=1; ; i++)
757 {
cristya97426c2011-02-04 01:41:27 +0000758 (void) InterpretImageFilename(image_info,image,filename,(int) i,
cristy6fccee12011-10-20 18:43:18 +0000759 read_info->filename,exception);
cristya97426c2011-02-04 01:41:27 +0000760 if (IsPDFRendered(read_info->filename) == MagickFalse)
761 break;
762 (void) RelinquishUniqueFileResource(read_info->filename);
763 }
764 else
765 for (i=1; ; i++)
766 {
cristya97426c2011-02-04 01:41:27 +0000767 (void) InterpretImageFilename(image_info,image,filename,(int) i,
cristy6fccee12011-10-20 18:43:18 +0000768 read_info->filename,exception);
cristya97426c2011-02-04 01:41:27 +0000769 if (IsPDFRendered(read_info->filename) == MagickFalse)
770 break;
cristyb0ffea22012-06-17 20:22:01 +0000771 read_info->blob=NULL;
772 read_info->length=0;
cristya97426c2011-02-04 01:41:27 +0000773 next=ReadImage(read_info,exception);
774 (void) RelinquishUniqueFileResource(read_info->filename);
775 if (next == (Image *) NULL)
776 break;
777 AppendImageToList(&pdf_image,next);
778 }
cristy3ed852e2009-09-05 21:47:34 +0000779 read_info=DestroyImageInfo(read_info);
780 if (pdf_image == (Image *) NULL)
781 {
cristy60575362014-10-18 13:22:50 +0000782 if (*message != '\0')
783 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
784 "PDFDelegateFailed","`%s'",message);
cristyb6cabe82011-03-18 13:25:59 +0000785 image=DestroyImage(image);
cristy3ed852e2009-09-05 21:47:34 +0000786 return((Image *) NULL);
787 }
788 if (LocaleCompare(pdf_image->magick,"BMP") == 0)
789 {
790 Image
791 *cmyk_image;
792
793 cmyk_image=ConsolidateCMYKImages(pdf_image,exception);
794 if (cmyk_image != (Image *) NULL)
795 {
796 pdf_image=DestroyImageList(pdf_image);
797 pdf_image=cmyk_image;
798 }
799 }
800 if (image_info->number_scenes != 0)
801 {
802 Image
803 *clone_image;
804
cristy3ed852e2009-09-05 21:47:34 +0000805 /*
806 Add place holder images to meet the subimage specification requirement.
807 */
cristybb503372010-05-27 20:51:26 +0000808 for (i=0; i < (ssize_t) image_info->scene; i++)
cristy3ed852e2009-09-05 21:47:34 +0000809 {
810 clone_image=CloneImage(pdf_image,1,1,MagickTrue,exception);
811 if (clone_image != (Image *) NULL)
812 PrependImageToList(&pdf_image,clone_image);
813 }
814 }
815 do
816 {
cristy151b66d2015-04-15 10:50:31 +0000817 (void) CopyMagickString(pdf_image->filename,filename,MagickPathExtent);
818 (void) CopyMagickString(pdf_image->magick,image->magick,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +0000819 pdf_image->page=page;
820 (void) CloneImageProfiles(pdf_image,image);
821 (void) CloneImageProperties(pdf_image,image);
822 next=SyncNextImageInList(pdf_image);
823 if (next != (Image *) NULL)
824 pdf_image=next;
825 } while (next != (Image *) NULL);
826 image=DestroyImage(image);
827 scene=0;
828 for (next=GetFirstImageInList(pdf_image); next != (Image *) NULL; )
829 {
830 next->scene=scene++;
831 next=GetNextImageInList(next);
832 }
833 return(GetFirstImageInList(pdf_image));
834}
835
836/*
837%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
838% %
839% %
840% %
841% R e g i s t e r P D F I m a g e %
842% %
843% %
844% %
845%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
846%
847% RegisterPDFImage() adds properties for the PDF image format to
848% the list of supported formats. The properties include the image format
849% tag, a method to read and/or write the format, whether the format
850% supports the saving of more than one frame to the same file or blob,
851% whether the format supports native in-memory I/O, and a brief
852% description of the format.
853%
854% The format of the RegisterPDFImage method is:
855%
cristybb503372010-05-27 20:51:26 +0000856% size_t RegisterPDFImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000857%
858*/
cristybb503372010-05-27 20:51:26 +0000859ModuleExport size_t RegisterPDFImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000860{
861 MagickInfo
862 *entry;
863
dirk06b627a2015-04-06 18:59:17 +0000864 entry=AcquireMagickInfo("PDF","AI","Adobe Illustrator CS2");
cristy3ed852e2009-09-05 21:47:34 +0000865 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
866 entry->encoder=(EncodeImageHandler *) WritePDFImage;
dirk08e9a112015-02-22 01:51:41 +0000867 entry->flags^=CoderAdjoinFlag;
868 entry->flags^=CoderBlobSupportFlag;
869 entry->flags|=CoderSeekableStreamFlag;
cristy5aefbeb2013-08-09 12:13:32 +0000870 entry->mime_type=ConstantString("application/pdf");
cristy3ed852e2009-09-05 21:47:34 +0000871 (void) RegisterMagickInfo(entry);
dirk06b627a2015-04-06 18:59:17 +0000872 entry=AcquireMagickInfo("PDF","EPDF",
873 "Encapsulated Portable Document Format");
cristy3ed852e2009-09-05 21:47:34 +0000874 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
875 entry->encoder=(EncodeImageHandler *) WritePDFImage;
dirk08e9a112015-02-22 01:51:41 +0000876 entry->flags^=CoderAdjoinFlag;
877 entry->flags^=CoderBlobSupportFlag;
878 entry->flags|=CoderSeekableStreamFlag;
cristy5aefbeb2013-08-09 12:13:32 +0000879 entry->mime_type=ConstantString("application/pdf");
cristy3ed852e2009-09-05 21:47:34 +0000880 (void) RegisterMagickInfo(entry);
dirk06b627a2015-04-06 18:59:17 +0000881 entry=AcquireMagickInfo("PDF","PDF","Portable Document Format");
cristy3ed852e2009-09-05 21:47:34 +0000882 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
883 entry->encoder=(EncodeImageHandler *) WritePDFImage;
884 entry->magick=(IsImageFormatHandler *) IsPDF;
dirk08e9a112015-02-22 01:51:41 +0000885 entry->flags^=CoderBlobSupportFlag;
886 entry->flags|=CoderSeekableStreamFlag;
cristy5aefbeb2013-08-09 12:13:32 +0000887 entry->mime_type=ConstantString("application/pdf");
cristy3ed852e2009-09-05 21:47:34 +0000888 (void) RegisterMagickInfo(entry);
dirk06b627a2015-04-06 18:59:17 +0000889 entry=AcquireMagickInfo("PDF","PDFA","Portable Document Archive Format");
cristy3ed852e2009-09-05 21:47:34 +0000890 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
891 entry->encoder=(EncodeImageHandler *) WritePDFImage;
892 entry->magick=(IsImageFormatHandler *) IsPDF;
dirk08e9a112015-02-22 01:51:41 +0000893 entry->flags^=CoderBlobSupportFlag;
894 entry->flags|=CoderSeekableStreamFlag;
cristy5aefbeb2013-08-09 12:13:32 +0000895 entry->mime_type=ConstantString("application/pdf");
cristy3ed852e2009-09-05 21:47:34 +0000896 (void) RegisterMagickInfo(entry);
897 return(MagickImageCoderSignature);
898}
899
900/*
901%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
902% %
903% %
904% %
905% U n r e g i s t e r P D F I m a g e %
906% %
907% %
908% %
909%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
910%
911% UnregisterPDFImage() removes format registrations made by the
912% PDF module from the list of supported formats.
913%
914% The format of the UnregisterPDFImage method is:
915%
916% UnregisterPDFImage(void)
917%
918*/
919ModuleExport void UnregisterPDFImage(void)
920{
921 (void) UnregisterMagickInfo("AI");
922 (void) UnregisterMagickInfo("EPDF");
923 (void) UnregisterMagickInfo("PDF");
924 (void) UnregisterMagickInfo("PDFA");
925}
926
927/*
928%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
929% %
930% %
931% %
932% W r i t e P D F I m a g e %
933% %
934% %
935% %
936%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
937%
938% WritePDFImage() writes an image in the Portable Document image
939% format.
940%
941% The format of the WritePDFImage method is:
942%
cristy1e178e72011-08-28 19:44:34 +0000943% MagickBooleanType WritePDFImage(const ImageInfo *image_info,
944% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000945%
946% A description of each parameter follows.
947%
948% o image_info: the image info.
949%
950% o image: The image.
951%
cristy1e178e72011-08-28 19:44:34 +0000952% o exception: return any errors or warnings in this structure.
953%
cristy3ed852e2009-09-05 21:47:34 +0000954*/
955
Cristy34838f52016-07-09 20:54:46 -0400956static char *EscapeParenthesis(const char *source)
cristy3ed852e2009-09-05 21:47:34 +0000957{
Cristy34838f52016-07-09 20:54:46 -0400958 char
959 *destination;
960
cristy3ed852e2009-09-05 21:47:34 +0000961 register char
Cristy34838f52016-07-09 20:54:46 -0400962 *q;
963
964 register const char
cristy3ed852e2009-09-05 21:47:34 +0000965 *p;
966
cristybb503372010-05-27 20:51:26 +0000967 size_t
Cristy34838f52016-07-09 20:54:46 -0400968 length;
cristy3ed852e2009-09-05 21:47:34 +0000969
Cristy34838f52016-07-09 20:54:46 -0400970 assert(source != (const char *) NULL);
971 length=0;
972 for (p=source; *p != '\0'; p++)
cristy3ed852e2009-09-05 21:47:34 +0000973 {
Cristy34838f52016-07-09 20:54:46 -0400974 if ((*p == '\\') || (*p == '(') || (*p == ')'))
cristy3ed852e2009-09-05 21:47:34 +0000975 {
Cristy34838f52016-07-09 20:54:46 -0400976 if (~length < 1)
977 ThrowFatalException(ResourceLimitFatalError,"UnableToEscapeString");
978 length++;
cristy3ed852e2009-09-05 21:47:34 +0000979 }
Cristy34838f52016-07-09 20:54:46 -0400980 length++;
cristy3ed852e2009-09-05 21:47:34 +0000981 }
Cristy34838f52016-07-09 20:54:46 -0400982 destination=(char *) NULL;
983 if (~length >= (MagickPathExtent-1))
984 destination=(char *) AcquireQuantumMemory(length+MagickPathExtent,
985 sizeof(*destination));
986 if (destination == (char *) NULL)
987 ThrowFatalException(ResourceLimitFatalError,"UnableToEscapeString");
988 *destination='\0';
989 q=destination;
990 for (p=source; *p != '\0'; p++)
991 {
992 if ((*p == '\\') || (*p == '(') || (*p == ')'))
993 *q++='\\';
994 *q++=(*p);
995 }
996 *q='\0';
997 return(destination);
cristy3ed852e2009-09-05 21:47:34 +0000998}
999
Cristyce4a3552015-12-14 13:53:35 -05001000static size_t UTF8ToUTF16(const unsigned char *utf8,wchar_t *utf16)
1001{
1002 register const unsigned char
1003 *p;
1004
1005 if (utf16 != (wchar_t *) NULL)
1006 {
1007 register wchar_t
1008 *q;
1009
1010 wchar_t
1011 c;
1012
1013 /*
1014 Convert UTF-8 to UTF-16.
1015 */
1016 q=utf16;
1017 for (p=utf8; *p != '\0'; p++)
1018 {
1019 if ((*p & 0x80) == 0)
1020 *q=(*p);
1021 else
1022 if ((*p & 0xE0) == 0xC0)
1023 {
1024 c=(*p);
1025 *q=(c & 0x1F) << 6;
1026 p++;
1027 if ((*p & 0xC0) != 0x80)
1028 return(0);
1029 *q|=(*p & 0x3F);
1030 }
1031 else
1032 if ((*p & 0xF0) == 0xE0)
1033 {
1034 c=(*p);
1035 *q=c << 12;
1036 p++;
1037 if ((*p & 0xC0) != 0x80)
1038 return(0);
1039 c=(*p);
1040 *q|=(c & 0x3F) << 6;
1041 p++;
1042 if ((*p & 0xC0) != 0x80)
1043 return(0);
1044 *q|=(*p & 0x3F);
1045 }
1046 else
1047 return(0);
1048 q++;
1049 }
1050 *q++='\0';
1051 return(q-utf16);
1052 }
1053 /*
1054 Compute UTF-16 string length.
1055 */
1056 for (p=utf8; *p != '\0'; p++)
1057 {
1058 if ((*p & 0x80) == 0)
1059 ;
1060 else
1061 if ((*p & 0xE0) == 0xC0)
1062 {
1063 p++;
1064 if ((*p & 0xC0) != 0x80)
1065 return(0);
1066 }
1067 else
1068 if ((*p & 0xF0) == 0xE0)
1069 {
1070 p++;
1071 if ((*p & 0xC0) != 0x80)
1072 return(0);
1073 p++;
1074 if ((*p & 0xC0) != 0x80)
1075 return(0);
1076 }
1077 else
1078 return(0);
1079 }
1080 return(p-utf8);
1081}
1082
1083static wchar_t *ConvertUTF8ToUTF16(const unsigned char *source,size_t *length)
1084{
1085 wchar_t
1086 *utf16;
1087
1088 *length=UTF8ToUTF16(source,(wchar_t *) NULL);
1089 if (*length == 0)
1090 {
1091 register ssize_t
1092 i;
1093
1094 /*
1095 Not UTF-8, just copy.
1096 */
1097 *length=strlen((const char *) source);
1098 utf16=(wchar_t *) AcquireQuantumMemory(*length+1,sizeof(*utf16));
1099 if (utf16 == (wchar_t *) NULL)
1100 return((wchar_t *) NULL);
1101 for (i=0; i <= (ssize_t) *length; i++)
1102 utf16[i]=source[i];
1103 return(utf16);
1104 }
1105 utf16=(wchar_t *) AcquireQuantumMemory(*length+1,sizeof(*utf16));
1106 if (utf16 == (wchar_t *) NULL)
1107 return((wchar_t *) NULL);
1108 *length=UTF8ToUTF16(source,utf16);
1109 return(utf16);
1110}
1111
cristy47b838c2009-09-19 16:09:30 +00001112static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
cristy018f07f2011-09-04 21:15:19 +00001113 Image *image,Image *inject_image,ExceptionInfo *exception)
cristy47b838c2009-09-19 16:09:30 +00001114{
cristy47b838c2009-09-19 16:09:30 +00001115 Image
cristy80975862009-09-25 14:34:31 +00001116 *group4_image;
cristy47b838c2009-09-19 16:09:30 +00001117
1118 ImageInfo
1119 *write_info;
1120
cristy47b838c2009-09-19 16:09:30 +00001121 MagickBooleanType
1122 status;
1123
cristy80975862009-09-25 14:34:31 +00001124 size_t
1125 length;
cristy47b838c2009-09-19 16:09:30 +00001126
1127 unsigned char
cristy80975862009-09-25 14:34:31 +00001128 *group4;
cristy47b838c2009-09-19 16:09:30 +00001129
cristy42751fe2009-10-05 00:15:50 +00001130 status=MagickTrue;
cristy47b838c2009-09-19 16:09:30 +00001131 write_info=CloneImageInfo(image_info);
cristy151b66d2015-04-15 10:50:31 +00001132 (void) CopyMagickString(write_info->filename,"GROUP4:",MagickPathExtent);
1133 (void) CopyMagickString(write_info->magick,"GROUP4",MagickPathExtent);
cristy018f07f2011-09-04 21:15:19 +00001134 group4_image=CloneImage(inject_image,0,0,MagickTrue,exception);
cristy80975862009-09-25 14:34:31 +00001135 if (group4_image == (Image *) NULL)
1136 return(MagickFalse);
1137 group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
cristy018f07f2011-09-04 21:15:19 +00001138 exception);
cristy80975862009-09-25 14:34:31 +00001139 group4_image=DestroyImage(group4_image);
1140 if (group4 == (unsigned char *) NULL)
1141 return(MagickFalse);
cristy47b838c2009-09-19 16:09:30 +00001142 write_info=DestroyImageInfo(write_info);
cristy80975862009-09-25 14:34:31 +00001143 if (WriteBlob(image,length,group4) != (ssize_t) length)
1144 status=MagickFalse;
1145 group4=(unsigned char *) RelinquishMagickMemory(group4);
1146 return(status);
cristy47b838c2009-09-19 16:09:30 +00001147}
1148
cristy1e178e72011-08-28 19:44:34 +00001149static MagickBooleanType WritePDFImage(const ImageInfo *image_info,Image *image,
1150 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001151{
1152#define CFormat "/Filter [ /%s ]\n"
1153#define ObjectsPerImage 14
1154
dirk93b02b72013-11-16 16:03:36 +00001155DisableMSCWarning(4310)
cristy3ed852e2009-09-05 21:47:34 +00001156 static const char
1157 XMPProfile[]=
1158 {
1159 "<?xpacket begin=\"%s\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n"
1160 "<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"
1161 " <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n"
1162 " <rdf:Description rdf:about=\"\"\n"
1163 " xmlns:xap=\"http://ns.adobe.com/xap/1.0/\">\n"
1164 " <xap:ModifyDate>%s</xap:ModifyDate>\n"
1165 " <xap:CreateDate>%s</xap:CreateDate>\n"
1166 " <xap:MetadataDate>%s</xap:MetadataDate>\n"
1167 " <xap:CreatorTool>%s</xap:CreatorTool>\n"
1168 " </rdf:Description>\n"
1169 " <rdf:Description rdf:about=\"\"\n"
1170 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"
1171 " <dc:format>application/pdf</dc:format>\n"
cristy1b70dc82012-07-01 23:15:05 +00001172 " <dc:title>\n"
1173 " <rdf:Alt>\n"
1174 " <rdf:li xml:lang=\"x-default\">%s</rdf:li>\n"
1175 " </rdf:Alt>\n"
1176 " </dc:title>\n"
cristy3ed852e2009-09-05 21:47:34 +00001177 " </rdf:Description>\n"
1178 " <rdf:Description rdf:about=\"\"\n"
1179 " xmlns:xapMM=\"http://ns.adobe.com/xap/1.0/mm/\">\n"
1180 " <xapMM:DocumentID>uuid:6ec119d7-7982-4f56-808d-dfe64f5b35cf</xapMM:DocumentID>\n"
1181 " <xapMM:InstanceID>uuid:a79b99b4-6235-447f-9f6c-ec18ef7555cb</xapMM:InstanceID>\n"
1182 " </rdf:Description>\n"
1183 " <rdf:Description rdf:about=\"\"\n"
1184 " xmlns:pdf=\"http://ns.adobe.com/pdf/1.3/\">\n"
1185 " <pdf:Producer>%s</pdf:Producer>\n"
1186 " </rdf:Description>\n"
1187 " <rdf:Description rdf:about=\"\"\n"
1188 " xmlns:pdfaid=\"http://www.aiim.org/pdfa/ns/id/\">\n"
cristyba5f54b2014-05-19 22:10:08 +00001189 " <pdfaid:part>3</pdfaid:part>\n"
cristy3ed852e2009-09-05 21:47:34 +00001190 " <pdfaid:conformance>B</pdfaid:conformance>\n"
1191 " </rdf:Description>\n"
1192 " </rdf:RDF>\n"
1193 "</x:xmpmeta>\n"
1194 "<?xpacket end=\"w\"?>\n"
1195 },
1196 XMPProfileMagick[4]= { (char) 0xef, (char) 0xbb, (char) 0xbf, (char) 0x00 };
dirk93b02b72013-11-16 16:03:36 +00001197RestoreMSCWarning
cristy3ed852e2009-09-05 21:47:34 +00001198
1199 char
cristy151b66d2015-04-15 10:50:31 +00001200 basename[MagickPathExtent],
1201 buffer[MagickPathExtent],
1202 date[MagickPathExtent],
cristy3ed852e2009-09-05 21:47:34 +00001203 **labels,
Cristy519e0052016-10-16 15:28:40 -04001204 page_geometry[MagickPathExtent],
1205 *url;
cristy3ed852e2009-09-05 21:47:34 +00001206
1207 CompressionType
1208 compression;
1209
1210 const char
Cristy07b9f5e2016-02-17 13:13:47 -05001211 *option,
cristy3ed852e2009-09-05 21:47:34 +00001212 *value;
1213
1214 double
1215 pointsize;
1216
1217 GeometryInfo
1218 geometry_info;
1219
cristy3ed852e2009-09-05 21:47:34 +00001220 Image
1221 *next,
1222 *tile_image;
1223
1224 MagickBooleanType
1225 status;
1226
1227 MagickOffsetType
1228 offset,
1229 scene,
1230 *xref;
1231
1232 MagickSizeType
1233 number_pixels;
1234
1235 MagickStatusType
1236 flags;
1237
1238 PointInfo
1239 delta,
1240 resolution,
1241 scale;
1242
1243 RectangleInfo
1244 geometry,
1245 media_info,
1246 page_info;
1247
cristy4c08aed2011-07-01 19:47:50 +00001248 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +00001249 *p;
1250
1251 register unsigned char
1252 *q;
1253
cristybb503372010-05-27 20:51:26 +00001254 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001255 i,
1256 x;
1257
1258 size_t
cristyaff6d802011-04-26 01:46:31 +00001259 info_id,
1260 length,
1261 object,
1262 pages_id,
1263 root_id,
1264 text_size,
1265 version;
1266
1267 ssize_t
1268 count,
1269 y;
cristy3ed852e2009-09-05 21:47:34 +00001270
1271 struct tm
1272 local_time;
1273
1274 time_t
1275 seconds;
1276
1277 unsigned char
1278 *pixels;
1279
Cristyce4a3552015-12-14 13:53:35 -05001280 wchar_t
1281 *utf16;
1282
cristy3ed852e2009-09-05 21:47:34 +00001283 /*
1284 Open output image file.
1285 */
1286 assert(image_info != (const ImageInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +00001287 assert(image_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +00001288 assert(image != (Image *) NULL);
cristye1c94d92015-06-28 12:16:33 +00001289 assert(image->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +00001290 if (image->debug != MagickFalse)
1291 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +00001292 assert(exception != (ExceptionInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +00001293 assert(exception->signature == MagickCoreSignature);
cristy1e178e72011-08-28 19:44:34 +00001294 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +00001295 if (status == MagickFalse)
1296 return(status);
1297 /*
1298 Allocate X ref memory.
1299 */
1300 xref=(MagickOffsetType *) AcquireQuantumMemory(2048UL,sizeof(*xref));
1301 if (xref == (MagickOffsetType *) NULL)
1302 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1303 (void) ResetMagickMemory(xref,0,2048UL*sizeof(*xref));
1304 /*
1305 Write Info object.
1306 */
1307 object=0;
1308 version=3;
1309 if (image_info->compression == JPEG2000Compression)
cristybb503372010-05-27 20:51:26 +00001310 version=(size_t) MagickMax(version,5);
cristy3ed852e2009-09-05 21:47:34 +00001311 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
cristy17f11b02014-12-20 19:37:04 +00001312 if (next->alpha_trait != UndefinedPixelTrait)
cristybb503372010-05-27 20:51:26 +00001313 version=(size_t) MagickMax(version,4);
cristy3ed852e2009-09-05 21:47:34 +00001314 if (LocaleCompare(image_info->magick,"PDFA") == 0)
cristyc04c60e2011-05-31 23:58:45 +00001315 version=(size_t) MagickMax(version,6);
cristy151b66d2015-04-15 10:50:31 +00001316 (void) FormatLocaleString(buffer,MagickPathExtent,"%%PDF-1.%.20g \n",(double)
cristyf40010e2012-05-13 13:52:20 +00001317 version);
cristy3ed852e2009-09-05 21:47:34 +00001318 (void) WriteBlobString(image,buffer);
1319 if (LocaleCompare(image_info->magick,"PDFA") == 0)
cristy42bd8002014-01-26 02:06:27 +00001320 {
1321 (void) WriteBlobByte(image,'%');
1322 (void) WriteBlobByte(image,0xe2);
1323 (void) WriteBlobByte(image,0xe3);
1324 (void) WriteBlobByte(image,0xcf);
1325 (void) WriteBlobByte(image,0xd3);
1326 (void) WriteBlobByte(image,'\n');
1327 }
cristy3ed852e2009-09-05 21:47:34 +00001328 /*
1329 Write Catalog object.
1330 */
1331 xref[object++]=TellBlob(image);
1332 root_id=object;
cristy151b66d2015-04-15 10:50:31 +00001333 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g 0 obj\n",(double)
cristye8c25f92010-06-03 00:53:06 +00001334 object);
cristy3ed852e2009-09-05 21:47:34 +00001335 (void) WriteBlobString(image,buffer);
1336 (void) WriteBlobString(image,"<<\n");
1337 if (LocaleCompare(image_info->magick,"PDFA") != 0)
Cristy07b9f5e2016-02-17 13:13:47 -05001338 (void) FormatLocaleString(buffer,MagickPathExtent,"/Pages %.20g 0 R\n",
1339 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001340 else
1341 {
cristy151b66d2015-04-15 10:50:31 +00001342 (void) FormatLocaleString(buffer,MagickPathExtent,"/Metadata %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001343 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001344 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00001345 (void) FormatLocaleString(buffer,MagickPathExtent,"/Pages %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001346 (double) object+2);
cristy3ed852e2009-09-05 21:47:34 +00001347 }
1348 (void) WriteBlobString(image,buffer);
Cristy07b9f5e2016-02-17 13:13:47 -05001349 (void) WriteBlobString(image,"/Type /Catalog");
1350 option=GetImageOption(image_info,"pdf:page-direction");
1351 if ((option != (const char *) NULL) &&
1352 (LocaleCompare(option,"right-to-left") != MagickFalse))
1353 (void) WriteBlobString(image,"/ViewerPreferences<</PageDirection/R2L>>\n");
1354 (void) WriteBlobString(image,"\n");
cristy3ed852e2009-09-05 21:47:34 +00001355 (void) WriteBlobString(image,">>\n");
1356 (void) WriteBlobString(image,"endobj\n");
cristy1b70dc82012-07-01 23:15:05 +00001357 GetPathComponent(image->filename,BasePath,basename);
cristy3ed852e2009-09-05 21:47:34 +00001358 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1359 {
1360 char
cristy151b66d2015-04-15 10:50:31 +00001361 create_date[MagickPathExtent],
1362 modify_date[MagickPathExtent],
1363 timestamp[MagickPathExtent],
Cristy519e0052016-10-16 15:28:40 -04001364 *url,
cristy151b66d2015-04-15 10:50:31 +00001365 xmp_profile[MagickPathExtent];
cristy3ed852e2009-09-05 21:47:34 +00001366
cristy3ed852e2009-09-05 21:47:34 +00001367 /*
1368 Write XMP object.
1369 */
1370 xref[object++]=TellBlob(image);
cristy151b66d2015-04-15 10:50:31 +00001371 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g 0 obj\n",(double)
cristyf40010e2012-05-13 13:52:20 +00001372 object);
cristy3ed852e2009-09-05 21:47:34 +00001373 (void) WriteBlobString(image,buffer);
1374 (void) WriteBlobString(image,"<<\n");
1375 (void) WriteBlobString(image,"/Subtype /XML\n");
1376 *modify_date='\0';
cristyd15e6592011-10-15 00:13:06 +00001377 value=GetImageProperty(image,"date:modify",exception);
cristy3ed852e2009-09-05 21:47:34 +00001378 if (value != (const char *) NULL)
cristy151b66d2015-04-15 10:50:31 +00001379 (void) CopyMagickString(modify_date,value,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001380 *create_date='\0';
cristyd15e6592011-10-15 00:13:06 +00001381 value=GetImageProperty(image,"date:create",exception);
cristy3ed852e2009-09-05 21:47:34 +00001382 if (value != (const char *) NULL)
cristy151b66d2015-04-15 10:50:31 +00001383 (void) CopyMagickString(create_date,value,MagickPathExtent);
1384 (void) FormatMagickTime(time((time_t *) NULL),MagickPathExtent,timestamp);
Cristy519e0052016-10-16 15:28:40 -04001385 url=GetMagickHomeURL();
cristy151b66d2015-04-15 10:50:31 +00001386 i=FormatLocaleString(xmp_profile,MagickPathExtent,XMPProfile,
Cristy519e0052016-10-16 15:28:40 -04001387 XMPProfileMagick,modify_date,create_date,timestamp,url,
1388 EscapeParenthesis(basename),url);
1389 url=DestroyString(url);
Cristy07b9f5e2016-02-17 13:13:47 -05001390 (void) FormatLocaleString(buffer,MagickPathExtent,"/Length %.20g\n",
1391 (double) i);
cristy3ed852e2009-09-05 21:47:34 +00001392 (void) WriteBlobString(image,buffer);
1393 (void) WriteBlobString(image,"/Type /Metadata\n");
1394 (void) WriteBlobString(image,">>\nstream\n");
1395 (void) WriteBlobString(image,xmp_profile);
cristy1b70dc82012-07-01 23:15:05 +00001396 (void) WriteBlobString(image,"\nendstream\n");
cristy3ed852e2009-09-05 21:47:34 +00001397 (void) WriteBlobString(image,"endobj\n");
1398 }
1399 /*
1400 Write Pages object.
1401 */
1402 xref[object++]=TellBlob(image);
1403 pages_id=object;
cristy151b66d2015-04-15 10:50:31 +00001404 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001405 object);
cristy3ed852e2009-09-05 21:47:34 +00001406 (void) WriteBlobString(image,buffer);
1407 (void) WriteBlobString(image,"<<\n");
1408 (void) WriteBlobString(image,"/Type /Pages\n");
Cristy07b9f5e2016-02-17 13:13:47 -05001409 (void) FormatLocaleString(buffer,MagickPathExtent,"/Kids [ %.20g 0 R ",
1410 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001411 (void) WriteBlobString(image,buffer);
cristybb503372010-05-27 20:51:26 +00001412 count=(ssize_t) (pages_id+ObjectsPerImage+1);
cristy3ed852e2009-09-05 21:47:34 +00001413 if (image_info->adjoin != MagickFalse)
1414 {
1415 Image
1416 *kid_image;
1417
1418 /*
1419 Predict page object id's.
1420 */
1421 kid_image=image;
1422 for ( ; GetNextImageInList(kid_image) != (Image *) NULL; count+=ObjectsPerImage)
1423 {
cristy151b66d2015-04-15 10:50:31 +00001424 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g 0 R ",(double)
cristye8c25f92010-06-03 00:53:06 +00001425 count);
cristy3ed852e2009-09-05 21:47:34 +00001426 (void) WriteBlobString(image,buffer);
1427 kid_image=GetNextImageInList(kid_image);
1428 }
1429 xref=(MagickOffsetType *) ResizeQuantumMemory(xref,(size_t) count+2048UL,
1430 sizeof(*xref));
1431 if (xref == (MagickOffsetType *) NULL)
1432 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1433 }
1434 (void) WriteBlobString(image,"]\n");
cristy151b66d2015-04-15 10:50:31 +00001435 (void) FormatLocaleString(buffer,MagickPathExtent,"/Count %.20g\n",(double)
cristyf40010e2012-05-13 13:52:20 +00001436 ((count-pages_id)/ObjectsPerImage));
cristy3ed852e2009-09-05 21:47:34 +00001437 (void) WriteBlobString(image,buffer);
1438 (void) WriteBlobString(image,">>\n");
1439 (void) WriteBlobString(image,"endobj\n");
1440 scene=0;
1441 do
1442 {
cristyaf8d3912014-02-21 14:50:33 +00001443 compression=image->compression;
cristy3ed852e2009-09-05 21:47:34 +00001444 if (image_info->compression != UndefinedCompression)
1445 compression=image_info->compression;
1446 switch (compression)
1447 {
1448 case FaxCompression:
1449 case Group4Compression:
1450 {
dirkf1d85482015-04-06 00:36:00 +00001451 if ((SetImageMonochrome(image,exception) == MagickFalse) ||
cristy17f11b02014-12-20 19:37:04 +00001452 (image->alpha_trait != UndefinedPixelTrait))
cristy3ed852e2009-09-05 21:47:34 +00001453 compression=RLECompression;
1454 break;
1455 }
1456#if !defined(MAGICKCORE_JPEG_DELEGATE)
1457 case JPEGCompression:
1458 {
1459 compression=RLECompression;
cristy1e178e72011-08-28 19:44:34 +00001460 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00001461 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
1462 image->filename);
1463 break;
1464 }
1465#endif
cristyf95d7c12014-04-08 07:35:08 +00001466#if !defined(MAGICKCORE_LIBOPENJP2_DELEGATE)
cristy3ed852e2009-09-05 21:47:34 +00001467 case JPEG2000Compression:
1468 {
1469 compression=RLECompression;
cristy1e178e72011-08-28 19:44:34 +00001470 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00001471 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JP2)",
1472 image->filename);
1473 break;
1474 }
1475#endif
1476#if !defined(MAGICKCORE_ZLIB_DELEGATE)
1477 case ZipCompression:
1478 {
1479 compression=RLECompression;
cristy1e178e72011-08-28 19:44:34 +00001480 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00001481 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZLIB)",
1482 image->filename);
1483 break;
1484 }
1485#endif
1486 case LZWCompression:
1487 {
1488 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1489 compression=RLECompression; /* LZW compression is forbidden */
1490 break;
1491 }
1492 case NoCompression:
1493 {
1494 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1495 compression=RLECompression; /* ASCII 85 compression is forbidden */
1496 break;
1497 }
1498 default:
1499 break;
1500 }
1501 if (compression == JPEG2000Compression)
cristyaf8d3912014-02-21 14:50:33 +00001502 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00001503 /*
1504 Scale relative to dots-per-inch.
1505 */
1506 delta.x=DefaultResolution;
1507 delta.y=DefaultResolution;
cristy2a11bef2011-10-28 18:33:11 +00001508 resolution.x=image->resolution.x;
1509 resolution.y=image->resolution.y;
cristy3ed852e2009-09-05 21:47:34 +00001510 if ((resolution.x == 0.0) || (resolution.y == 0.0))
1511 {
1512 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
1513 resolution.x=geometry_info.rho;
1514 resolution.y=geometry_info.sigma;
1515 if ((flags & SigmaValue) == 0)
1516 resolution.y=resolution.x;
1517 }
1518 if (image_info->density != (char *) NULL)
1519 {
1520 flags=ParseGeometry(image_info->density,&geometry_info);
1521 resolution.x=geometry_info.rho;
1522 resolution.y=geometry_info.sigma;
1523 if ((flags & SigmaValue) == 0)
1524 resolution.y=resolution.x;
1525 }
1526 if (image->units == PixelsPerCentimeterResolution)
1527 {
cristya97426c2011-02-04 01:41:27 +00001528 resolution.x=(double) ((size_t) (100.0*2.54*resolution.x+0.5)/100.0);
1529 resolution.y=(double) ((size_t) (100.0*2.54*resolution.y+0.5)/100.0);
cristy3ed852e2009-09-05 21:47:34 +00001530 }
1531 SetGeometry(image,&geometry);
Cristy07b9f5e2016-02-17 13:13:47 -05001532 (void) FormatLocaleString(page_geometry,MagickPathExtent,"%.20gx%.20g",
1533 (double) image->columns,(double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001534 if (image_info->page != (char *) NULL)
cristy151b66d2015-04-15 10:50:31 +00001535 (void) CopyMagickString(page_geometry,image_info->page,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001536 else
1537 if ((image->page.width != 0) && (image->page.height != 0))
cristy151b66d2015-04-15 10:50:31 +00001538 (void) FormatLocaleString(page_geometry,MagickPathExtent,
cristyf40010e2012-05-13 13:52:20 +00001539 "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,(double)
1540 image->page.height,(double) image->page.x,(double) image->page.y);
cristy3ed852e2009-09-05 21:47:34 +00001541 else
1542 if ((image->gravity != UndefinedGravity) &&
1543 (LocaleCompare(image_info->magick,"PDF") == 0))
Cristy07b9f5e2016-02-17 13:13:47 -05001544 (void) CopyMagickString(page_geometry,PSPageGeometry,
1545 MagickPathExtent);
cristy151b66d2015-04-15 10:50:31 +00001546 (void) ConcatenateMagickString(page_geometry,">",MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001547 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
1548 &geometry.width,&geometry.height);
1549 scale.x=(double) (geometry.width*delta.x)/resolution.x;
cristybb503372010-05-27 20:51:26 +00001550 geometry.width=(size_t) floor(scale.x+0.5);
cristy3ed852e2009-09-05 21:47:34 +00001551 scale.y=(double) (geometry.height*delta.y)/resolution.y;
cristybb503372010-05-27 20:51:26 +00001552 geometry.height=(size_t) floor(scale.y+0.5);
cristy3ed852e2009-09-05 21:47:34 +00001553 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
cristy1e178e72011-08-28 19:44:34 +00001554 (void) ParseGravityGeometry(image,page_geometry,&page_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00001555 if (image->gravity != UndefinedGravity)
1556 {
1557 geometry.x=(-page_info.x);
cristybb503372010-05-27 20:51:26 +00001558 geometry.y=(ssize_t) (media_info.height+page_info.y-image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001559 }
1560 pointsize=12.0;
1561 if (image_info->pointsize != 0.0)
1562 pointsize=image_info->pointsize;
1563 text_size=0;
cristyd15e6592011-10-15 00:13:06 +00001564 value=GetImageProperty(image,"label",exception);
cristy3ed852e2009-09-05 21:47:34 +00001565 if (value != (const char *) NULL)
cristybb503372010-05-27 20:51:26 +00001566 text_size=(size_t) (MultilineCensus(value)*pointsize+12);
cristyda16f162011-02-19 23:52:17 +00001567 (void) text_size;
cristy3ed852e2009-09-05 21:47:34 +00001568 /*
1569 Write Page object.
1570 */
1571 xref[object++]=TellBlob(image);
cristy151b66d2015-04-15 10:50:31 +00001572 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001573 object);
cristy3ed852e2009-09-05 21:47:34 +00001574 (void) WriteBlobString(image,buffer);
1575 (void) WriteBlobString(image,"<<\n");
1576 (void) WriteBlobString(image,"/Type /Page\n");
cristy151b66d2015-04-15 10:50:31 +00001577 (void) FormatLocaleString(buffer,MagickPathExtent,"/Parent %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001578 (double) pages_id);
cristy3ed852e2009-09-05 21:47:34 +00001579 (void) WriteBlobString(image,buffer);
1580 (void) WriteBlobString(image,"/Resources <<\n");
1581 labels=(char **) NULL;
cristyd15e6592011-10-15 00:13:06 +00001582 value=GetImageProperty(image,"label",exception);
cristy3ed852e2009-09-05 21:47:34 +00001583 if (value != (const char *) NULL)
1584 labels=StringToList(value);
1585 if (labels != (char **) NULL)
1586 {
cristy151b66d2015-04-15 10:50:31 +00001587 (void) FormatLocaleString(buffer,MagickPathExtent,
cristye8c25f92010-06-03 00:53:06 +00001588 "/Font << /F%.20g %.20g 0 R >>\n",(double) image->scene,(double)
1589 object+4);
cristy3ed852e2009-09-05 21:47:34 +00001590 (void) WriteBlobString(image,buffer);
1591 }
cristy151b66d2015-04-15 10:50:31 +00001592 (void) FormatLocaleString(buffer,MagickPathExtent,
cristye8c25f92010-06-03 00:53:06 +00001593 "/XObject << /Im%.20g %.20g 0 R >>\n",(double) image->scene,(double)
1594 object+5);
cristy3ed852e2009-09-05 21:47:34 +00001595 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00001596 (void) FormatLocaleString(buffer,MagickPathExtent,"/ProcSet %.20g 0 R >>\n",
cristye8c25f92010-06-03 00:53:06 +00001597 (double) object+3);
cristy3ed852e2009-09-05 21:47:34 +00001598 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00001599 (void) FormatLocaleString(buffer,MagickPathExtent,
cristye7f51092010-01-17 00:39:37 +00001600 "/MediaBox [0 0 %g %g]\n",72.0*media_info.width/resolution.x,
cristy8cd5b312010-01-07 01:10:24 +00001601 72.0*media_info.height/resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001602 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00001603 (void) FormatLocaleString(buffer,MagickPathExtent,
cristye7f51092010-01-17 00:39:37 +00001604 "/CropBox [0 0 %g %g]\n",72.0*media_info.width/resolution.x,
cristy8cd5b312010-01-07 01:10:24 +00001605 72.0*media_info.height/resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001606 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00001607 (void) FormatLocaleString(buffer,MagickPathExtent,"/Contents %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001608 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001609 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00001610 (void) FormatLocaleString(buffer,MagickPathExtent,"/Thumb %.20g 0 R\n",(double)
cristyf40010e2012-05-13 13:52:20 +00001611 object+8);
cristy3ed852e2009-09-05 21:47:34 +00001612 (void) WriteBlobString(image,buffer);
1613 (void) WriteBlobString(image,">>\n");
1614 (void) WriteBlobString(image,"endobj\n");
1615 /*
1616 Write Contents object.
1617 */
1618 xref[object++]=TellBlob(image);
cristy151b66d2015-04-15 10:50:31 +00001619 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001620 object);
cristy3ed852e2009-09-05 21:47:34 +00001621 (void) WriteBlobString(image,buffer);
1622 (void) WriteBlobString(image,"<<\n");
cristy151b66d2015-04-15 10:50:31 +00001623 (void) FormatLocaleString(buffer,MagickPathExtent,"/Length %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001624 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001625 (void) WriteBlobString(image,buffer);
1626 (void) WriteBlobString(image,">>\n");
1627 (void) WriteBlobString(image,"stream\n");
1628 offset=TellBlob(image);
1629 (void) WriteBlobString(image,"q\n");
1630 if (labels != (char **) NULL)
1631 for (i=0; labels[i] != (char *) NULL; i++)
1632 {
1633 (void) WriteBlobString(image,"BT\n");
cristy151b66d2015-04-15 10:50:31 +00001634 (void) FormatLocaleString(buffer,MagickPathExtent,"/F%.20g %g Tf\n",
cristye8c25f92010-06-03 00:53:06 +00001635 (double) image->scene,pointsize);
cristy3ed852e2009-09-05 21:47:34 +00001636 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00001637 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g %.20g Td\n",
cristye8c25f92010-06-03 00:53:06 +00001638 (double) geometry.x,(double) (geometry.y+geometry.height+i*pointsize+
1639 12));
cristy3ed852e2009-09-05 21:47:34 +00001640 (void) WriteBlobString(image,buffer);
Cristy07b9f5e2016-02-17 13:13:47 -05001641 (void) FormatLocaleString(buffer,MagickPathExtent,"(%s) Tj\n",
1642 labels[i]);
cristy3ed852e2009-09-05 21:47:34 +00001643 (void) WriteBlobString(image,buffer);
1644 (void) WriteBlobString(image,"ET\n");
1645 labels[i]=DestroyString(labels[i]);
1646 }
Cristy07b9f5e2016-02-17 13:13:47 -05001647 (void) FormatLocaleString(buffer,MagickPathExtent,
1648 "%g 0 0 %g %.20g %.20g cm\n",scale.x,scale.y,(double) geometry.x,
1649 (double) geometry.y);
cristy3ed852e2009-09-05 21:47:34 +00001650 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00001651 (void) FormatLocaleString(buffer,MagickPathExtent,"/Im%.20g Do\n",(double)
cristyf40010e2012-05-13 13:52:20 +00001652 image->scene);
cristy3ed852e2009-09-05 21:47:34 +00001653 (void) WriteBlobString(image,buffer);
1654 (void) WriteBlobString(image,"Q\n");
1655 offset=TellBlob(image)-offset;
cristy1b70dc82012-07-01 23:15:05 +00001656 (void) WriteBlobString(image,"\nendstream\n");
cristy3ed852e2009-09-05 21:47:34 +00001657 (void) WriteBlobString(image,"endobj\n");
1658 /*
1659 Write Length object.
1660 */
1661 xref[object++]=TellBlob(image);
cristy151b66d2015-04-15 10:50:31 +00001662 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001663 object);
cristy3ed852e2009-09-05 21:47:34 +00001664 (void) WriteBlobString(image,buffer);
Cristy07b9f5e2016-02-17 13:13:47 -05001665 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g\n",
1666 (double) offset);
cristy3ed852e2009-09-05 21:47:34 +00001667 (void) WriteBlobString(image,buffer);
1668 (void) WriteBlobString(image,"endobj\n");
1669 /*
1670 Write Procset object.
1671 */
1672 xref[object++]=TellBlob(image);
cristy151b66d2015-04-15 10:50:31 +00001673 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g 0 obj\n",(double)
cristyf40010e2012-05-13 13:52:20 +00001674 object);
cristy3ed852e2009-09-05 21:47:34 +00001675 (void) WriteBlobString(image,buffer);
1676 if ((image->storage_class == DirectClass) || (image->colors > 256))
cristy151b66d2015-04-15 10:50:31 +00001677 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageC",MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001678 else
1679 if ((compression == FaxCompression) || (compression == Group4Compression))
cristy151b66d2015-04-15 10:50:31 +00001680 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageB",MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001681 else
cristy151b66d2015-04-15 10:50:31 +00001682 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageI",MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001683 (void) WriteBlobString(image,buffer);
1684 (void) WriteBlobString(image," ]\n");
1685 (void) WriteBlobString(image,"endobj\n");
1686 /*
1687 Write Font object.
1688 */
1689 xref[object++]=TellBlob(image);
cristy151b66d2015-04-15 10:50:31 +00001690 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g 0 obj\n",(double)
cristyf40010e2012-05-13 13:52:20 +00001691 object);
cristy3ed852e2009-09-05 21:47:34 +00001692 (void) WriteBlobString(image,buffer);
1693 (void) WriteBlobString(image,"<<\n");
1694 if (labels != (char **) NULL)
1695 {
1696 (void) WriteBlobString(image,"/Type /Font\n");
1697 (void) WriteBlobString(image,"/Subtype /Type1\n");
cristy151b66d2015-04-15 10:50:31 +00001698 (void) FormatLocaleString(buffer,MagickPathExtent,"/Name /F%.20g\n",
cristye8c25f92010-06-03 00:53:06 +00001699 (double) image->scene);
cristy3ed852e2009-09-05 21:47:34 +00001700 (void) WriteBlobString(image,buffer);
1701 (void) WriteBlobString(image,"/BaseFont /Helvetica\n");
1702 (void) WriteBlobString(image,"/Encoding /MacRomanEncoding\n");
1703 labels=(char **) RelinquishMagickMemory(labels);
1704 }
1705 (void) WriteBlobString(image,">>\n");
1706 (void) WriteBlobString(image,"endobj\n");
1707 /*
1708 Write XObject object.
1709 */
1710 xref[object++]=TellBlob(image);
cristy151b66d2015-04-15 10:50:31 +00001711 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001712 object);
cristy3ed852e2009-09-05 21:47:34 +00001713 (void) WriteBlobString(image,buffer);
1714 (void) WriteBlobString(image,"<<\n");
1715 (void) WriteBlobString(image,"/Type /XObject\n");
1716 (void) WriteBlobString(image,"/Subtype /Image\n");
cristy151b66d2015-04-15 10:50:31 +00001717 (void) FormatLocaleString(buffer,MagickPathExtent,"/Name /Im%.20g\n",(double)
cristyf40010e2012-05-13 13:52:20 +00001718 image->scene);
cristy3ed852e2009-09-05 21:47:34 +00001719 (void) WriteBlobString(image,buffer);
1720 switch (compression)
1721 {
1722 case NoCompression:
1723 {
cristy151b66d2015-04-15 10:50:31 +00001724 (void) FormatLocaleString(buffer,MagickPathExtent,CFormat,"ASCII85Decode");
cristy3ed852e2009-09-05 21:47:34 +00001725 break;
1726 }
1727 case JPEGCompression:
1728 {
cristy151b66d2015-04-15 10:50:31 +00001729 (void) FormatLocaleString(buffer,MagickPathExtent,CFormat,"DCTDecode");
cristy3ed852e2009-09-05 21:47:34 +00001730 if (image->colorspace != CMYKColorspace)
1731 break;
1732 (void) WriteBlobString(image,buffer);
1733 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
cristy151b66d2015-04-15 10:50:31 +00001734 MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001735 break;
1736 }
1737 case JPEG2000Compression:
1738 {
cristy151b66d2015-04-15 10:50:31 +00001739 (void) FormatLocaleString(buffer,MagickPathExtent,CFormat,"JPXDecode");
cristy3ed852e2009-09-05 21:47:34 +00001740 if (image->colorspace != CMYKColorspace)
1741 break;
1742 (void) WriteBlobString(image,buffer);
1743 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
cristy151b66d2015-04-15 10:50:31 +00001744 MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001745 break;
1746 }
1747 case LZWCompression:
1748 {
cristy151b66d2015-04-15 10:50:31 +00001749 (void) FormatLocaleString(buffer,MagickPathExtent,CFormat,"LZWDecode");
cristyfa7becb2009-09-13 02:44:40 +00001750 break;
cristy3ed852e2009-09-05 21:47:34 +00001751 }
1752 case ZipCompression:
1753 {
Cristy07b9f5e2016-02-17 13:13:47 -05001754 (void) FormatLocaleString(buffer,MagickPathExtent,CFormat,
1755 "FlateDecode");
cristyfa7becb2009-09-13 02:44:40 +00001756 break;
cristy3ed852e2009-09-05 21:47:34 +00001757 }
1758 case FaxCompression:
1759 case Group4Compression:
1760 {
1761 (void) CopyMagickString(buffer,"/Filter [ /CCITTFaxDecode ]\n",
cristy151b66d2015-04-15 10:50:31 +00001762 MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001763 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00001764 (void) FormatLocaleString(buffer,MagickPathExtent,"/DecodeParms [ << "
cristye8c25f92010-06-03 00:53:06 +00001765 "/K %s /BlackIs1 false /Columns %.20g /Rows %.20g >> ]\n",CCITTParam,
1766 (double) image->columns,(double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001767 break;
1768 }
1769 default:
1770 {
cristy151b66d2015-04-15 10:50:31 +00001771 (void) FormatLocaleString(buffer,MagickPathExtent,CFormat,
cristy3ed852e2009-09-05 21:47:34 +00001772 "RunLengthDecode");
1773 break;
1774 }
1775 }
1776 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00001777 (void) FormatLocaleString(buffer,MagickPathExtent,"/Width %.20g\n",(double)
cristye8c25f92010-06-03 00:53:06 +00001778 image->columns);
cristy3ed852e2009-09-05 21:47:34 +00001779 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00001780 (void) FormatLocaleString(buffer,MagickPathExtent,"/Height %.20g\n",(double)
cristye8c25f92010-06-03 00:53:06 +00001781 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001782 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00001783 (void) FormatLocaleString(buffer,MagickPathExtent,"/ColorSpace %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001784 (double) object+2);
cristy3ed852e2009-09-05 21:47:34 +00001785 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00001786 (void) FormatLocaleString(buffer,MagickPathExtent,"/BitsPerComponent %d\n",
cristy3ed852e2009-09-05 21:47:34 +00001787 (compression == FaxCompression) || (compression == Group4Compression) ?
1788 1 : 8);
1789 (void) WriteBlobString(image,buffer);
cristy17f11b02014-12-20 19:37:04 +00001790 if (image->alpha_trait != UndefinedPixelTrait)
cristy3ed852e2009-09-05 21:47:34 +00001791 {
cristy151b66d2015-04-15 10:50:31 +00001792 (void) FormatLocaleString(buffer,MagickPathExtent,"/SMask %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001793 (double) object+7);
cristy3ed852e2009-09-05 21:47:34 +00001794 (void) WriteBlobString(image,buffer);
1795 }
cristy151b66d2015-04-15 10:50:31 +00001796 (void) FormatLocaleString(buffer,MagickPathExtent,"/Length %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001797 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001798 (void) WriteBlobString(image,buffer);
1799 (void) WriteBlobString(image,">>\n");
1800 (void) WriteBlobString(image,"stream\n");
1801 offset=TellBlob(image);
1802 number_pixels=(MagickSizeType) image->columns*image->rows;
1803 if ((4*number_pixels) != (MagickSizeType) ((size_t) (4*number_pixels)))
1804 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1805 if ((compression == FaxCompression) || (compression == Group4Compression) ||
1806 ((image_info->type != TrueColorType) &&
dirkf1d85482015-04-06 00:36:00 +00001807 (SetImageGray(image,exception) != MagickFalse)))
cristy3ed852e2009-09-05 21:47:34 +00001808 {
1809 switch (compression)
1810 {
1811 case FaxCompression:
1812 case Group4Compression:
1813 {
1814 if (LocaleCompare(CCITTParam,"0") == 0)
1815 {
cristy018f07f2011-09-04 21:15:19 +00001816 (void) HuffmanEncodeImage(image_info,image,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001817 break;
1818 }
cristy018f07f2011-09-04 21:15:19 +00001819 (void) Huffman2DEncodeImage(image_info,image,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001820 break;
1821 }
1822 case JPEGCompression:
1823 {
cristy1e178e72011-08-28 19:44:34 +00001824 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
cristy3ed852e2009-09-05 21:47:34 +00001825 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00001826 {
1827 (void) CloseBlob(image);
1828 return(MagickFalse);
1829 }
cristy3ed852e2009-09-05 21:47:34 +00001830 break;
1831 }
1832 case JPEG2000Compression:
1833 {
cristy1e178e72011-08-28 19:44:34 +00001834 status=InjectImageBlob(image_info,image,image,"jp2",exception);
cristy3ed852e2009-09-05 21:47:34 +00001835 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00001836 {
1837 (void) CloseBlob(image);
1838 return(MagickFalse);
1839 }
cristy3ed852e2009-09-05 21:47:34 +00001840 break;
1841 }
1842 case RLECompression:
1843 default:
1844 {
cristy0553bd52013-06-30 15:53:50 +00001845 MemoryInfo
1846 *pixel_info;
1847
cristy3ed852e2009-09-05 21:47:34 +00001848 /*
1849 Allocate pixel array.
1850 */
1851 length=(size_t) number_pixels;
cristy0553bd52013-06-30 15:53:50 +00001852 pixel_info=AcquireVirtualMemory(length,sizeof(*pixels));
1853 if (pixel_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001854 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
cristy0553bd52013-06-30 15:53:50 +00001855 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +00001856 /*
1857 Dump Runlength encoded pixels.
1858 */
1859 q=pixels;
cristybb503372010-05-27 20:51:26 +00001860 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001861 {
cristy1e178e72011-08-28 19:44:34 +00001862 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001863 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001864 break;
cristybb503372010-05-27 20:51:26 +00001865 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001866 {
cristyd0323222013-04-07 16:13:21 +00001867 *q++=ScaleQuantumToChar(ClampToQuantum(GetPixelLuma(image,p)));
cristyed231572011-07-14 02:18:59 +00001868 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001869 }
1870 if (image->previous == (Image *) NULL)
1871 {
cristyf5c61ba2010-12-17 00:58:04 +00001872 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1873 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001874 if (status == MagickFalse)
1875 break;
1876 }
1877 }
1878#if defined(MAGICKCORE_ZLIB_DELEGATE)
1879 if (compression == ZipCompression)
cristy018f07f2011-09-04 21:15:19 +00001880 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001881 else
1882#endif
1883 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00001884 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001885 else
cristy018f07f2011-09-04 21:15:19 +00001886 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy0553bd52013-06-30 15:53:50 +00001887 pixel_info=RelinquishVirtualMemory(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +00001888 if (status == MagickFalse)
1889 {
1890 (void) CloseBlob(image);
1891 return(MagickFalse);
1892 }
1893 break;
1894 }
1895 case NoCompression:
1896 {
1897 /*
1898 Dump uncompressed PseudoColor packets.
1899 */
1900 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00001901 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001902 {
cristy1e178e72011-08-28 19:44:34 +00001903 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001904 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001905 break;
cristybb503372010-05-27 20:51:26 +00001906 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001907 {
cristyd0323222013-04-07 16:13:21 +00001908 Ascii85Encode(image,ScaleQuantumToChar(ClampToQuantum(
1909 GetPixelLuma(image,p))));
cristyed231572011-07-14 02:18:59 +00001910 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001911 }
1912 if (image->previous == (Image *) NULL)
1913 {
cristyf5c61ba2010-12-17 00:58:04 +00001914 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1915 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001916 if (status == MagickFalse)
1917 break;
1918 }
1919 }
1920 Ascii85Flush(image);
1921 break;
1922 }
1923 }
1924 }
1925 else
1926 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1927 (compression == JPEGCompression) ||
1928 (compression == JPEG2000Compression))
1929 switch (compression)
1930 {
1931 case JPEGCompression:
1932 {
cristy1e178e72011-08-28 19:44:34 +00001933 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
cristy3ed852e2009-09-05 21:47:34 +00001934 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00001935 {
1936 (void) CloseBlob(image);
1937 return(MagickFalse);
1938 }
cristy3ed852e2009-09-05 21:47:34 +00001939 break;
1940 }
1941 case JPEG2000Compression:
1942 {
cristy1e178e72011-08-28 19:44:34 +00001943 status=InjectImageBlob(image_info,image,image,"jp2",exception);
cristy3ed852e2009-09-05 21:47:34 +00001944 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00001945 {
1946 (void) CloseBlob(image);
1947 return(MagickFalse);
1948 }
cristy3ed852e2009-09-05 21:47:34 +00001949 break;
1950 }
1951 case RLECompression:
1952 default:
1953 {
cristy0553bd52013-06-30 15:53:50 +00001954 MemoryInfo
1955 *pixel_info;
1956
cristy3ed852e2009-09-05 21:47:34 +00001957 /*
1958 Allocate pixel array.
1959 */
1960 length=(size_t) number_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001961 length*=image->colorspace == CMYKColorspace ? 4UL : 3UL;
cristy0553bd52013-06-30 15:53:50 +00001962 pixel_info=AcquireVirtualMemory(length,sizeof(*pixels));
1963 if (pixel_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001964 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
cristy0553bd52013-06-30 15:53:50 +00001965 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +00001966 /*
1967 Dump runoffset encoded pixels.
1968 */
1969 q=pixels;
cristybb503372010-05-27 20:51:26 +00001970 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001971 {
cristy1e178e72011-08-28 19:44:34 +00001972 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001973 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001974 break;
cristybb503372010-05-27 20:51:26 +00001975 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001976 {
cristy4c08aed2011-07-01 19:47:50 +00001977 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1978 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1979 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
cristy3ed852e2009-09-05 21:47:34 +00001980 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00001981 *q++=ScaleQuantumToChar(GetPixelBlack(image,p));
cristyed231572011-07-14 02:18:59 +00001982 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001983 }
1984 if (image->previous == (Image *) NULL)
1985 {
cristyf5c61ba2010-12-17 00:58:04 +00001986 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1987 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001988 if (status == MagickFalse)
1989 break;
1990 }
1991 }
1992#if defined(MAGICKCORE_ZLIB_DELEGATE)
1993 if (compression == ZipCompression)
cristy018f07f2011-09-04 21:15:19 +00001994 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001995 else
1996#endif
1997 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00001998 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001999 else
cristy018f07f2011-09-04 21:15:19 +00002000 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy0553bd52013-06-30 15:53:50 +00002001 pixel_info=RelinquishVirtualMemory(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +00002002 if (status == MagickFalse)
2003 {
2004 (void) CloseBlob(image);
2005 return(MagickFalse);
2006 }
2007 break;
2008 }
2009 case NoCompression:
2010 {
2011 /*
2012 Dump uncompressed DirectColor packets.
2013 */
2014 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00002015 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002016 {
cristy1e178e72011-08-28 19:44:34 +00002017 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00002018 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002019 break;
cristybb503372010-05-27 20:51:26 +00002020 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002021 {
cristy4c08aed2011-07-01 19:47:50 +00002022 Ascii85Encode(image,ScaleQuantumToChar(GetPixelRed(image,p)));
2023 Ascii85Encode(image,ScaleQuantumToChar(GetPixelGreen(image,p)));
2024 Ascii85Encode(image,ScaleQuantumToChar(GetPixelBlue(image,p)));
cristy3ed852e2009-09-05 21:47:34 +00002025 if (image->colorspace == CMYKColorspace)
cristyaff6d802011-04-26 01:46:31 +00002026 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +00002027 GetPixelBlack(image,p)));
cristyed231572011-07-14 02:18:59 +00002028 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002029 }
2030 if (image->previous == (Image *) NULL)
2031 {
cristyf5c61ba2010-12-17 00:58:04 +00002032 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
2033 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00002034 if (status == MagickFalse)
2035 break;
2036 }
2037 }
2038 Ascii85Flush(image);
2039 break;
2040 }
2041 }
2042 else
2043 {
2044 /*
2045 Dump number of colors and colormap.
2046 */
2047 switch (compression)
2048 {
2049 case RLECompression:
2050 default:
2051 {
cristy0553bd52013-06-30 15:53:50 +00002052 MemoryInfo
2053 *pixel_info;
2054
cristy3ed852e2009-09-05 21:47:34 +00002055 /*
2056 Allocate pixel array.
2057 */
2058 length=(size_t) number_pixels;
cristy0553bd52013-06-30 15:53:50 +00002059 pixel_info=AcquireVirtualMemory(length,sizeof(*pixels));
2060 if (pixel_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002061 ThrowWriterException(ResourceLimitError,
2062 "MemoryAllocationFailed");
cristy0553bd52013-06-30 15:53:50 +00002063 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +00002064 /*
2065 Dump Runlength encoded pixels.
2066 */
2067 q=pixels;
cristybb503372010-05-27 20:51:26 +00002068 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002069 {
cristy1e178e72011-08-28 19:44:34 +00002070 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00002071 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002072 break;
cristybb503372010-05-27 20:51:26 +00002073 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00002074 {
2075 *q++=(unsigned char) GetPixelIndex(image,p);
cristyed231572011-07-14 02:18:59 +00002076 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00002077 }
cristy3ed852e2009-09-05 21:47:34 +00002078 if (image->previous == (Image *) NULL)
2079 {
cristyf5c61ba2010-12-17 00:58:04 +00002080 status=SetImageProgress(image,SaveImageTag,
2081 (MagickOffsetType) y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00002082 if (status == MagickFalse)
2083 break;
2084 }
2085 }
2086#if defined(MAGICKCORE_ZLIB_DELEGATE)
2087 if (compression == ZipCompression)
cristy018f07f2011-09-04 21:15:19 +00002088 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002089 else
2090#endif
2091 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00002092 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002093 else
cristy018f07f2011-09-04 21:15:19 +00002094 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy0553bd52013-06-30 15:53:50 +00002095 pixel_info=RelinquishVirtualMemory(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +00002096 if (status == MagickFalse)
2097 {
2098 (void) CloseBlob(image);
2099 return(MagickFalse);
2100 }
2101 break;
2102 }
2103 case NoCompression:
2104 {
2105 /*
2106 Dump uncompressed PseudoColor packets.
2107 */
2108 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00002109 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002110 {
cristy1e178e72011-08-28 19:44:34 +00002111 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00002112 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002113 break;
cristybb503372010-05-27 20:51:26 +00002114 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00002115 {
2116 Ascii85Encode(image,(unsigned char) GetPixelIndex(image,p));
cristyed231572011-07-14 02:18:59 +00002117 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00002118 }
cristy3ed852e2009-09-05 21:47:34 +00002119 if (image->previous == (Image *) NULL)
2120 {
cristyf5c61ba2010-12-17 00:58:04 +00002121 status=SetImageProgress(image,SaveImageTag,
2122 (MagickOffsetType) y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00002123 if (status == MagickFalse)
2124 break;
2125 }
2126 }
2127 Ascii85Flush(image);
2128 break;
2129 }
2130 }
2131 }
2132 offset=TellBlob(image)-offset;
2133 (void) WriteBlobString(image,"\nendstream\n");
2134 (void) WriteBlobString(image,"endobj\n");
2135 /*
2136 Write Length object.
2137 */
2138 xref[object++]=TellBlob(image);
cristy151b66d2015-04-15 10:50:31 +00002139 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002140 object);
cristy3ed852e2009-09-05 21:47:34 +00002141 (void) WriteBlobString(image,buffer);
Cristy07b9f5e2016-02-17 13:13:47 -05002142 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g\n",(double)
2143 offset);
cristy3ed852e2009-09-05 21:47:34 +00002144 (void) WriteBlobString(image,buffer);
2145 (void) WriteBlobString(image,"endobj\n");
2146 /*
2147 Write Colorspace object.
2148 */
2149 xref[object++]=TellBlob(image);
cristy151b66d2015-04-15 10:50:31 +00002150 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002151 object);
cristy3ed852e2009-09-05 21:47:34 +00002152 (void) WriteBlobString(image,buffer);
2153 if (image->colorspace == CMYKColorspace)
cristy151b66d2015-04-15 10:50:31 +00002154 (void) CopyMagickString(buffer,"/DeviceCMYK\n",MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00002155 else
2156 if ((compression == FaxCompression) ||
2157 (compression == Group4Compression) ||
2158 ((image_info->type != TrueColorType) &&
dirkf1d85482015-04-06 00:36:00 +00002159 (SetImageGray(image,exception) != MagickFalse)))
cristy151b66d2015-04-15 10:50:31 +00002160 (void) CopyMagickString(buffer,"/DeviceGray\n",MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00002161 else
2162 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
2163 (compression == JPEGCompression) ||
2164 (compression == JPEG2000Compression))
cristy151b66d2015-04-15 10:50:31 +00002165 (void) CopyMagickString(buffer,"/DeviceRGB\n",MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00002166 else
cristy151b66d2015-04-15 10:50:31 +00002167 (void) FormatLocaleString(buffer,MagickPathExtent,
cristye8c25f92010-06-03 00:53:06 +00002168 "[ /Indexed /DeviceRGB %.20g %.20g 0 R ]\n",(double) image->colors-
2169 1,(double) object+3);
cristy3ed852e2009-09-05 21:47:34 +00002170 (void) WriteBlobString(image,buffer);
2171 (void) WriteBlobString(image,"endobj\n");
2172 /*
2173 Write Thumb object.
2174 */
2175 SetGeometry(image,&geometry);
2176 (void) ParseMetaGeometry("106x106+0+0>",&geometry.x,&geometry.y,
2177 &geometry.width,&geometry.height);
cristy1e178e72011-08-28 19:44:34 +00002178 tile_image=ThumbnailImage(image,geometry.width,geometry.height,exception);
cristy3ed852e2009-09-05 21:47:34 +00002179 if (tile_image == (Image *) NULL)
cristy1e178e72011-08-28 19:44:34 +00002180 return(MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +00002181 xref[object++]=TellBlob(image);
cristy151b66d2015-04-15 10:50:31 +00002182 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002183 object);
cristy3ed852e2009-09-05 21:47:34 +00002184 (void) WriteBlobString(image,buffer);
2185 (void) WriteBlobString(image,"<<\n");
2186 switch (compression)
2187 {
2188 case NoCompression:
2189 {
Cristy07b9f5e2016-02-17 13:13:47 -05002190 (void) FormatLocaleString(buffer,MagickPathExtent,CFormat,
2191 "ASCII85Decode");
cristy3ed852e2009-09-05 21:47:34 +00002192 break;
2193 }
2194 case JPEGCompression:
2195 {
cristy151b66d2015-04-15 10:50:31 +00002196 (void) FormatLocaleString(buffer,MagickPathExtent,CFormat,"DCTDecode");
cristy3ed852e2009-09-05 21:47:34 +00002197 if (image->colorspace != CMYKColorspace)
2198 break;
2199 (void) WriteBlobString(image,buffer);
2200 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
cristy151b66d2015-04-15 10:50:31 +00002201 MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00002202 break;
2203 }
2204 case JPEG2000Compression:
2205 {
cristy151b66d2015-04-15 10:50:31 +00002206 (void) FormatLocaleString(buffer,MagickPathExtent,CFormat,"JPXDecode");
cristy3ed852e2009-09-05 21:47:34 +00002207 if (image->colorspace != CMYKColorspace)
2208 break;
2209 (void) WriteBlobString(image,buffer);
2210 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
cristy151b66d2015-04-15 10:50:31 +00002211 MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00002212 break;
2213 }
2214 case LZWCompression:
2215 {
cristy151b66d2015-04-15 10:50:31 +00002216 (void) FormatLocaleString(buffer,MagickPathExtent,CFormat,"LZWDecode");
cristy3ed852e2009-09-05 21:47:34 +00002217 break;
2218 }
2219 case ZipCompression:
2220 {
Cristy07b9f5e2016-02-17 13:13:47 -05002221 (void) FormatLocaleString(buffer,MagickPathExtent,CFormat,
2222 "FlateDecode");
cristy3ed852e2009-09-05 21:47:34 +00002223 break;
2224 }
2225 case FaxCompression:
2226 case Group4Compression:
2227 {
2228 (void) CopyMagickString(buffer,"/Filter [ /CCITTFaxDecode ]\n",
cristy151b66d2015-04-15 10:50:31 +00002229 MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00002230 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00002231 (void) FormatLocaleString(buffer,MagickPathExtent,"/DecodeParms [ << "
cristye8c25f92010-06-03 00:53:06 +00002232 "/K %s /BlackIs1 false /Columns %.20g /Rows %.20g >> ]\n",CCITTParam,
2233 (double) tile_image->columns,(double) tile_image->rows);
cristy3ed852e2009-09-05 21:47:34 +00002234 break;
2235 }
2236 default:
2237 {
cristy151b66d2015-04-15 10:50:31 +00002238 (void) FormatLocaleString(buffer,MagickPathExtent,CFormat,
cristy3ed852e2009-09-05 21:47:34 +00002239 "RunLengthDecode");
2240 break;
2241 }
2242 }
2243 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00002244 (void) FormatLocaleString(buffer,MagickPathExtent,"/Width %.20g\n",(double)
cristye8c25f92010-06-03 00:53:06 +00002245 tile_image->columns);
cristy3ed852e2009-09-05 21:47:34 +00002246 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00002247 (void) FormatLocaleString(buffer,MagickPathExtent,"/Height %.20g\n",(double)
cristye8c25f92010-06-03 00:53:06 +00002248 tile_image->rows);
cristy3ed852e2009-09-05 21:47:34 +00002249 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00002250 (void) FormatLocaleString(buffer,MagickPathExtent,"/ColorSpace %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00002251 (double) object-1);
cristy3ed852e2009-09-05 21:47:34 +00002252 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00002253 (void) FormatLocaleString(buffer,MagickPathExtent,"/BitsPerComponent %d\n",
cristy3ed852e2009-09-05 21:47:34 +00002254 (compression == FaxCompression) || (compression == Group4Compression) ?
2255 1 : 8);
2256 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00002257 (void) FormatLocaleString(buffer,MagickPathExtent,"/Length %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00002258 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00002259 (void) WriteBlobString(image,buffer);
2260 (void) WriteBlobString(image,">>\n");
2261 (void) WriteBlobString(image,"stream\n");
2262 offset=TellBlob(image);
2263 number_pixels=(MagickSizeType) tile_image->columns*tile_image->rows;
2264 if ((compression == FaxCompression) ||
2265 (compression == Group4Compression) ||
2266 ((image_info->type != TrueColorType) &&
dirkf1d85482015-04-06 00:36:00 +00002267 (SetImageGray(tile_image,exception) != MagickFalse)))
cristy3ed852e2009-09-05 21:47:34 +00002268 {
2269 switch (compression)
2270 {
2271 case FaxCompression:
2272 case Group4Compression:
2273 {
2274 if (LocaleCompare(CCITTParam,"0") == 0)
2275 {
cristy018f07f2011-09-04 21:15:19 +00002276 (void) HuffmanEncodeImage(image_info,image,tile_image,
2277 exception);
cristy3ed852e2009-09-05 21:47:34 +00002278 break;
2279 }
cristy018f07f2011-09-04 21:15:19 +00002280 (void) Huffman2DEncodeImage(image_info,image,tile_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00002281 break;
2282 }
2283 case JPEGCompression:
2284 {
2285 status=InjectImageBlob(image_info,image,tile_image,"jpeg",
cristy1e178e72011-08-28 19:44:34 +00002286 exception);
cristy3ed852e2009-09-05 21:47:34 +00002287 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00002288 {
2289 (void) CloseBlob(image);
2290 return(MagickFalse);
2291 }
cristy3ed852e2009-09-05 21:47:34 +00002292 break;
2293 }
2294 case JPEG2000Compression:
2295 {
cristy1e178e72011-08-28 19:44:34 +00002296 status=InjectImageBlob(image_info,image,tile_image,"jp2",exception);
cristy3ed852e2009-09-05 21:47:34 +00002297 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00002298 {
2299 (void) CloseBlob(image);
2300 return(MagickFalse);
2301 }
cristy3ed852e2009-09-05 21:47:34 +00002302 break;
2303 }
2304 case RLECompression:
2305 default:
2306 {
cristy0553bd52013-06-30 15:53:50 +00002307 MemoryInfo
2308 *pixel_info;
2309
cristy3ed852e2009-09-05 21:47:34 +00002310 /*
2311 Allocate pixel array.
2312 */
2313 length=(size_t) number_pixels;
cristy0553bd52013-06-30 15:53:50 +00002314 pixel_info=AcquireVirtualMemory(length,sizeof(*pixels));
2315 if (pixel_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002316 {
2317 tile_image=DestroyImage(tile_image);
2318 ThrowWriterException(ResourceLimitError,
2319 "MemoryAllocationFailed");
2320 }
cristy0553bd52013-06-30 15:53:50 +00002321 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +00002322 /*
cristy0553bd52013-06-30 15:53:50 +00002323 Dump runlength encoded pixels.
cristy3ed852e2009-09-05 21:47:34 +00002324 */
2325 q=pixels;
cristybb503372010-05-27 20:51:26 +00002326 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002327 {
2328 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
cristy1e178e72011-08-28 19:44:34 +00002329 exception);
cristy4c08aed2011-07-01 19:47:50 +00002330 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002331 break;
cristybb503372010-05-27 20:51:26 +00002332 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002333 {
cristyd0323222013-04-07 16:13:21 +00002334 *q++=ScaleQuantumToChar(ClampToQuantum(GetPixelLuma(
2335 tile_image,p)));
cristyed231572011-07-14 02:18:59 +00002336 p+=GetPixelChannels(tile_image);
cristy3ed852e2009-09-05 21:47:34 +00002337 }
2338 }
2339#if defined(MAGICKCORE_ZLIB_DELEGATE)
2340 if (compression == ZipCompression)
cristy018f07f2011-09-04 21:15:19 +00002341 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002342 else
2343#endif
2344 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00002345 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002346 else
cristy018f07f2011-09-04 21:15:19 +00002347 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy0553bd52013-06-30 15:53:50 +00002348 pixel_info=RelinquishVirtualMemory(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +00002349 if (status == MagickFalse)
2350 {
2351 (void) CloseBlob(image);
2352 return(MagickFalse);
2353 }
2354 break;
2355 }
2356 case NoCompression:
2357 {
2358 /*
2359 Dump uncompressed PseudoColor packets.
2360 */
2361 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00002362 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002363 {
2364 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
cristy1e178e72011-08-28 19:44:34 +00002365 exception);
cristy4c08aed2011-07-01 19:47:50 +00002366 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002367 break;
cristybb503372010-05-27 20:51:26 +00002368 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002369 {
cristy62773482015-06-27 15:43:59 +00002370 Ascii85Encode(tile_image,ScaleQuantumToChar(ClampToQuantum(
cristyd0323222013-04-07 16:13:21 +00002371 GetPixelLuma(tile_image,p))));
cristyed231572011-07-14 02:18:59 +00002372 p+=GetPixelChannels(tile_image);
cristy3ed852e2009-09-05 21:47:34 +00002373 }
2374 }
2375 Ascii85Flush(image);
2376 break;
2377 }
2378 }
2379 }
2380 else
2381 if ((tile_image->storage_class == DirectClass) ||
2382 (tile_image->colors > 256) || (compression == JPEGCompression) ||
2383 (compression == JPEG2000Compression))
2384 switch (compression)
2385 {
2386 case JPEGCompression:
2387 {
2388 status=InjectImageBlob(image_info,image,tile_image,"jpeg",
cristy1e178e72011-08-28 19:44:34 +00002389 exception);
cristy3ed852e2009-09-05 21:47:34 +00002390 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00002391 {
2392 (void) CloseBlob(image);
2393 return(MagickFalse);
2394 }
cristy3ed852e2009-09-05 21:47:34 +00002395 break;
2396 }
2397 case JPEG2000Compression:
2398 {
cristy1e178e72011-08-28 19:44:34 +00002399 status=InjectImageBlob(image_info,image,tile_image,"jp2",exception);
cristy3ed852e2009-09-05 21:47:34 +00002400 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00002401 {
2402 (void) CloseBlob(image);
2403 return(MagickFalse);
2404 }
cristy3ed852e2009-09-05 21:47:34 +00002405 break;
2406 }
2407 case RLECompression:
2408 default:
2409 {
cristy0553bd52013-06-30 15:53:50 +00002410 MemoryInfo
2411 *pixel_info;
2412
cristy3ed852e2009-09-05 21:47:34 +00002413 /*
2414 Allocate pixel array.
2415 */
2416 length=(size_t) number_pixels;
cristy3ed852e2009-09-05 21:47:34 +00002417 length*=tile_image->colorspace == CMYKColorspace ? 4UL : 3UL;
cristy0553bd52013-06-30 15:53:50 +00002418 pixel_info=AcquireVirtualMemory(length,4*sizeof(*pixels));
2419 if (pixel_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002420 {
2421 tile_image=DestroyImage(tile_image);
2422 ThrowWriterException(ResourceLimitError,
2423 "MemoryAllocationFailed");
2424 }
cristy0553bd52013-06-30 15:53:50 +00002425 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +00002426 /*
cristy0553bd52013-06-30 15:53:50 +00002427 Dump runlength encoded pixels.
cristy3ed852e2009-09-05 21:47:34 +00002428 */
2429 q=pixels;
cristybb503372010-05-27 20:51:26 +00002430 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002431 {
2432 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
cristy1e178e72011-08-28 19:44:34 +00002433 exception);
cristy4c08aed2011-07-01 19:47:50 +00002434 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002435 break;
cristybb503372010-05-27 20:51:26 +00002436 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002437 {
cristy4c08aed2011-07-01 19:47:50 +00002438 *q++=ScaleQuantumToChar(GetPixelRed(tile_image,p));
2439 *q++=ScaleQuantumToChar(GetPixelGreen(tile_image,p));
2440 *q++=ScaleQuantumToChar(GetPixelBlue(tile_image,p));
cristy4d0ca342014-05-01 00:42:09 +00002441 if (tile_image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00002442 *q++=ScaleQuantumToChar(GetPixelBlack(tile_image,p));
cristyed231572011-07-14 02:18:59 +00002443 p+=GetPixelChannels(tile_image);
cristy3ed852e2009-09-05 21:47:34 +00002444 }
2445 }
2446#if defined(MAGICKCORE_ZLIB_DELEGATE)
2447 if (compression == ZipCompression)
cristy018f07f2011-09-04 21:15:19 +00002448 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002449 else
2450#endif
2451 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00002452 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002453 else
cristy018f07f2011-09-04 21:15:19 +00002454 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy0553bd52013-06-30 15:53:50 +00002455 pixel_info=RelinquishVirtualMemory(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +00002456 if (status == MagickFalse)
2457 {
2458 (void) CloseBlob(image);
2459 return(MagickFalse);
2460 }
2461 break;
2462 }
2463 case NoCompression:
2464 {
2465 /*
2466 Dump uncompressed DirectColor packets.
2467 */
2468 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00002469 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002470 {
2471 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
cristy1e178e72011-08-28 19:44:34 +00002472 exception);
cristy4c08aed2011-07-01 19:47:50 +00002473 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002474 break;
cristybb503372010-05-27 20:51:26 +00002475 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002476 {
cristyf5c61ba2010-12-17 00:58:04 +00002477 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +00002478 GetPixelRed(tile_image,p)));
cristyf5c61ba2010-12-17 00:58:04 +00002479 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +00002480 GetPixelGreen(tile_image,p)));
cristyf5c61ba2010-12-17 00:58:04 +00002481 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +00002482 GetPixelBlue(tile_image,p)));
cristy3ed852e2009-09-05 21:47:34 +00002483 if (image->colorspace == CMYKColorspace)
cristyaff6d802011-04-26 01:46:31 +00002484 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +00002485 GetPixelBlack(tile_image,p)));
cristyed231572011-07-14 02:18:59 +00002486 p+=GetPixelChannels(tile_image);
cristy3ed852e2009-09-05 21:47:34 +00002487 }
2488 }
2489 Ascii85Flush(image);
2490 break;
2491 }
2492 }
2493 else
2494 {
2495 /*
2496 Dump number of colors and colormap.
2497 */
2498 switch (compression)
2499 {
2500 case RLECompression:
2501 default:
2502 {
cristy0553bd52013-06-30 15:53:50 +00002503 MemoryInfo
2504 *pixel_info;
2505
cristy3ed852e2009-09-05 21:47:34 +00002506 /*
2507 Allocate pixel array.
2508 */
2509 length=(size_t) number_pixels;
cristy0553bd52013-06-30 15:53:50 +00002510 pixel_info=AcquireVirtualMemory(length,sizeof(*pixels));
2511 if (pixel_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002512 {
2513 tile_image=DestroyImage(tile_image);
2514 ThrowWriterException(ResourceLimitError,
2515 "MemoryAllocationFailed");
2516 }
cristy0553bd52013-06-30 15:53:50 +00002517 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +00002518 /*
cristy0553bd52013-06-30 15:53:50 +00002519 Dump runlength encoded pixels.
cristy3ed852e2009-09-05 21:47:34 +00002520 */
2521 q=pixels;
cristybb503372010-05-27 20:51:26 +00002522 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002523 {
2524 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
cristy1e178e72011-08-28 19:44:34 +00002525 exception);
cristy4c08aed2011-07-01 19:47:50 +00002526 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002527 break;
cristybb503372010-05-27 20:51:26 +00002528 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00002529 {
2530 *q++=(unsigned char) GetPixelIndex(tile_image,p);
cristy6307d372012-06-17 20:07:34 +00002531 p+=GetPixelChannels(tile_image);
cristy4c08aed2011-07-01 19:47:50 +00002532 }
cristy3ed852e2009-09-05 21:47:34 +00002533 }
2534#if defined(MAGICKCORE_ZLIB_DELEGATE)
2535 if (compression == ZipCompression)
cristy018f07f2011-09-04 21:15:19 +00002536 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002537 else
2538#endif
2539 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00002540 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002541 else
cristy018f07f2011-09-04 21:15:19 +00002542 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy0553bd52013-06-30 15:53:50 +00002543 pixel_info=RelinquishVirtualMemory(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +00002544 if (status == MagickFalse)
2545 {
2546 (void) CloseBlob(image);
2547 return(MagickFalse);
2548 }
2549 break;
2550 }
2551 case NoCompression:
2552 {
2553 /*
2554 Dump uncompressed PseudoColor packets.
2555 */
2556 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00002557 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002558 {
2559 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
cristy1e178e72011-08-28 19:44:34 +00002560 exception);
cristy4c08aed2011-07-01 19:47:50 +00002561 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002562 break;
cristybb503372010-05-27 20:51:26 +00002563 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00002564 {
cristyaff6d802011-04-26 01:46:31 +00002565 Ascii85Encode(image,(unsigned char)
cristy4c08aed2011-07-01 19:47:50 +00002566 GetPixelIndex(tile_image,p));
cristyed231572011-07-14 02:18:59 +00002567 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00002568 }
cristy3ed852e2009-09-05 21:47:34 +00002569 }
2570 Ascii85Flush(image);
2571 break;
2572 }
2573 }
2574 }
2575 tile_image=DestroyImage(tile_image);
2576 offset=TellBlob(image)-offset;
2577 (void) WriteBlobString(image,"\nendstream\n");
2578 (void) WriteBlobString(image,"endobj\n");
2579 /*
2580 Write Length object.
2581 */
2582 xref[object++]=TellBlob(image);
cristy151b66d2015-04-15 10:50:31 +00002583 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002584 object);
cristy3ed852e2009-09-05 21:47:34 +00002585 (void) WriteBlobString(image,buffer);
Cristy07b9f5e2016-02-17 13:13:47 -05002586 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g\n",(double)
2587 offset);
cristy3ed852e2009-09-05 21:47:34 +00002588 (void) WriteBlobString(image,buffer);
2589 (void) WriteBlobString(image,"endobj\n");
2590 xref[object++]=TellBlob(image);
cristy151b66d2015-04-15 10:50:31 +00002591 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002592 object);
cristy3ed852e2009-09-05 21:47:34 +00002593 (void) WriteBlobString(image,buffer);
cristy59427d32013-01-23 19:19:11 +00002594 (void) WriteBlobString(image,"<<\n");
cristy3152b6b2013-01-23 19:21:48 +00002595 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
2596 (compression == FaxCompression) || (compression == Group4Compression))
2597 (void) WriteBlobString(image,">>\n");
2598 else
cristy3ed852e2009-09-05 21:47:34 +00002599 {
2600 /*
2601 Write Colormap object.
2602 */
cristy3ed852e2009-09-05 21:47:34 +00002603 if (compression == NoCompression)
2604 (void) WriteBlobString(image,"/Filter [ /ASCII85Decode ]\n");
cristy151b66d2015-04-15 10:50:31 +00002605 (void) FormatLocaleString(buffer,MagickPathExtent,"/Length %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00002606 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00002607 (void) WriteBlobString(image,buffer);
2608 (void) WriteBlobString(image,">>\n");
2609 (void) WriteBlobString(image,"stream\n");
2610 offset=TellBlob(image);
2611 if (compression == NoCompression)
2612 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00002613 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00002614 {
2615 if (compression == NoCompression)
2616 {
cristyd0323222013-04-07 16:13:21 +00002617 Ascii85Encode(image,ScaleQuantumToChar(ClampToQuantum(
2618 image->colormap[i].red)));
2619 Ascii85Encode(image,ScaleQuantumToChar(ClampToQuantum(
2620 image->colormap[i].green)));
2621 Ascii85Encode(image,ScaleQuantumToChar(ClampToQuantum(
2622 image->colormap[i].blue)));
cristy3ed852e2009-09-05 21:47:34 +00002623 continue;
2624 }
cristyd0323222013-04-07 16:13:21 +00002625 (void) WriteBlobByte(image,ScaleQuantumToChar(
2626 ClampToQuantum(image->colormap[i].red)));
2627 (void) WriteBlobByte(image,ScaleQuantumToChar(
2628 ClampToQuantum(image->colormap[i].green)));
2629 (void) WriteBlobByte(image,ScaleQuantumToChar(
2630 ClampToQuantum(image->colormap[i].blue)));
cristy3ed852e2009-09-05 21:47:34 +00002631 }
2632 if (compression == NoCompression)
2633 Ascii85Flush(image);
2634 offset=TellBlob(image)-offset;
2635 (void) WriteBlobString(image,"\nendstream\n");
2636 }
2637 (void) WriteBlobString(image,"endobj\n");
2638 /*
2639 Write Length object.
2640 */
2641 xref[object++]=TellBlob(image);
cristy151b66d2015-04-15 10:50:31 +00002642 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002643 object);
cristy3ed852e2009-09-05 21:47:34 +00002644 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00002645 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002646 offset);
cristy3ed852e2009-09-05 21:47:34 +00002647 (void) WriteBlobString(image,buffer);
2648 (void) WriteBlobString(image,"endobj\n");
2649 /*
2650 Write softmask object.
2651 */
2652 xref[object++]=TellBlob(image);
cristy151b66d2015-04-15 10:50:31 +00002653 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002654 object);
cristy3ed852e2009-09-05 21:47:34 +00002655 (void) WriteBlobString(image,buffer);
2656 (void) WriteBlobString(image,"<<\n");
cristy17f11b02014-12-20 19:37:04 +00002657 if (image->alpha_trait == UndefinedPixelTrait)
cristy3ed852e2009-09-05 21:47:34 +00002658 (void) WriteBlobString(image,">>\n");
2659 else
2660 {
2661 (void) WriteBlobString(image,"/Type /XObject\n");
2662 (void) WriteBlobString(image,"/Subtype /Image\n");
cristy151b66d2015-04-15 10:50:31 +00002663 (void) FormatLocaleString(buffer,MagickPathExtent,"/Name /Ma%.20g\n",
cristye8c25f92010-06-03 00:53:06 +00002664 (double) image->scene);
cristy3ed852e2009-09-05 21:47:34 +00002665 (void) WriteBlobString(image,buffer);
2666 switch (compression)
2667 {
2668 case NoCompression:
2669 {
cristy151b66d2015-04-15 10:50:31 +00002670 (void) FormatLocaleString(buffer,MagickPathExtent,CFormat,
cristy3ed852e2009-09-05 21:47:34 +00002671 "ASCII85Decode");
2672 break;
2673 }
2674 case LZWCompression:
2675 {
Cristy07b9f5e2016-02-17 13:13:47 -05002676 (void) FormatLocaleString(buffer,MagickPathExtent,CFormat,
2677 "LZWDecode");
cristy3ed852e2009-09-05 21:47:34 +00002678 break;
2679 }
2680 case ZipCompression:
2681 {
cristy151b66d2015-04-15 10:50:31 +00002682 (void) FormatLocaleString(buffer,MagickPathExtent,CFormat,
cristy3ed852e2009-09-05 21:47:34 +00002683 "FlateDecode");
2684 break;
2685 }
2686 default:
2687 {
cristy151b66d2015-04-15 10:50:31 +00002688 (void) FormatLocaleString(buffer,MagickPathExtent,CFormat,
cristy3ed852e2009-09-05 21:47:34 +00002689 "RunLengthDecode");
2690 break;
2691 }
2692 }
2693 (void) WriteBlobString(image,buffer);
Cristy07b9f5e2016-02-17 13:13:47 -05002694 (void) FormatLocaleString(buffer,MagickPathExtent,"/Width %.20g\n",
2695 (double) image->columns);
cristy3ed852e2009-09-05 21:47:34 +00002696 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00002697 (void) FormatLocaleString(buffer,MagickPathExtent,"/Height %.20g\n",
cristye8c25f92010-06-03 00:53:06 +00002698 (double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +00002699 (void) WriteBlobString(image,buffer);
2700 (void) WriteBlobString(image,"/ColorSpace /DeviceGray\n");
Cristy07b9f5e2016-02-17 13:13:47 -05002701 (void) FormatLocaleString(buffer,MagickPathExtent,
2702 "/BitsPerComponent %d\n",(compression == FaxCompression) ||
2703 (compression == Group4Compression) ? 1 : 8);
cristy3ed852e2009-09-05 21:47:34 +00002704 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00002705 (void) FormatLocaleString(buffer,MagickPathExtent,"/Length %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00002706 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00002707 (void) WriteBlobString(image,buffer);
2708 (void) WriteBlobString(image,">>\n");
2709 (void) WriteBlobString(image,"stream\n");
2710 offset=TellBlob(image);
2711 number_pixels=(MagickSizeType) image->columns*image->rows;
2712 switch (compression)
2713 {
2714 case RLECompression:
2715 default:
2716 {
cristy0553bd52013-06-30 15:53:50 +00002717 MemoryInfo
2718 *pixel_info;
2719
cristy3ed852e2009-09-05 21:47:34 +00002720 /*
2721 Allocate pixel array.
2722 */
2723 length=(size_t) number_pixels;
cristy0553bd52013-06-30 15:53:50 +00002724 pixel_info=AcquireVirtualMemory(length,4*sizeof(*pixels));
2725 if (pixel_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002726 {
2727 image=DestroyImage(image);
2728 ThrowWriterException(ResourceLimitError,
2729 "MemoryAllocationFailed");
2730 }
cristy0553bd52013-06-30 15:53:50 +00002731 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +00002732 /*
2733 Dump Runlength encoded pixels.
2734 */
2735 q=pixels;
cristybb503372010-05-27 20:51:26 +00002736 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002737 {
cristy1e178e72011-08-28 19:44:34 +00002738 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00002739 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002740 break;
cristybb503372010-05-27 20:51:26 +00002741 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002742 {
cristy4c08aed2011-07-01 19:47:50 +00002743 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
cristyed231572011-07-14 02:18:59 +00002744 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002745 }
2746 }
2747#if defined(MAGICKCORE_ZLIB_DELEGATE)
2748 if (compression == ZipCompression)
cristy018f07f2011-09-04 21:15:19 +00002749 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002750 else
2751#endif
2752 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00002753 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002754 else
cristy018f07f2011-09-04 21:15:19 +00002755 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy0553bd52013-06-30 15:53:50 +00002756 pixel_info=RelinquishVirtualMemory(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +00002757 if (status == MagickFalse)
2758 {
2759 (void) CloseBlob(image);
2760 return(MagickFalse);
2761 }
2762 break;
2763 }
2764 case NoCompression:
2765 {
2766 /*
2767 Dump uncompressed PseudoColor packets.
2768 */
2769 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00002770 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002771 {
cristy1e178e72011-08-28 19:44:34 +00002772 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00002773 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002774 break;
cristybb503372010-05-27 20:51:26 +00002775 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002776 {
cristy4c08aed2011-07-01 19:47:50 +00002777 Ascii85Encode(image,ScaleQuantumToChar(GetPixelAlpha(image,p)));
cristyed231572011-07-14 02:18:59 +00002778 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002779 }
2780 }
2781 Ascii85Flush(image);
2782 break;
2783 }
2784 }
2785 offset=TellBlob(image)-offset;
2786 (void) WriteBlobString(image,"\nendstream\n");
2787 }
2788 (void) WriteBlobString(image,"endobj\n");
2789 /*
2790 Write Length object.
2791 */
2792 xref[object++]=TellBlob(image);
cristy151b66d2015-04-15 10:50:31 +00002793 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002794 object);
cristy3ed852e2009-09-05 21:47:34 +00002795 (void) WriteBlobString(image,buffer);
Cristy07b9f5e2016-02-17 13:13:47 -05002796 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g\n",(double)
2797 offset);
cristy3ed852e2009-09-05 21:47:34 +00002798 (void) WriteBlobString(image,buffer);
2799 (void) WriteBlobString(image,"endobj\n");
2800 if (GetNextImageInList(image) == (Image *) NULL)
2801 break;
2802 image=SyncNextImageInList(image);
2803 status=SetImageProgress(image,SaveImagesTag,scene++,
2804 GetImageListLength(image));
2805 if (status == MagickFalse)
2806 break;
2807 } while (image_info->adjoin != MagickFalse);
2808 /*
2809 Write Metadata object.
2810 */
2811 xref[object++]=TellBlob(image);
2812 info_id=object;
cristy151b66d2015-04-15 10:50:31 +00002813 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002814 object);
cristy3ed852e2009-09-05 21:47:34 +00002815 (void) WriteBlobString(image,buffer);
2816 (void) WriteBlobString(image,"<<\n");
Cristyce4a3552015-12-14 13:53:35 -05002817 utf16=ConvertUTF8ToUTF16((unsigned char *) basename,&length);
2818 if (utf16 != (wchar_t *) NULL)
2819 {
Cristya8b2bb22016-03-25 10:39:01 -04002820 (void) FormatLocaleString(buffer,MagickPathExtent,"/Title (\xfe\xff");
Cristyce4a3552015-12-14 13:53:35 -05002821 (void) WriteBlobString(image,buffer);
Cristy1196b1a2015-12-17 16:29:01 -05002822 for (i=0; i < (ssize_t) length; i++)
Cristyce4a3552015-12-14 13:53:35 -05002823 WriteBlobMSBShort(image,(unsigned short) utf16[i]);
Cristya8b2bb22016-03-25 10:39:01 -04002824 (void) FormatLocaleString(buffer,MagickPathExtent,")\n");
Cristyce4a3552015-12-14 13:53:35 -05002825 (void) WriteBlobString(image,buffer);
2826 utf16=(wchar_t *) RelinquishMagickMemory(utf16);
2827 }
cristy3ed852e2009-09-05 21:47:34 +00002828 seconds=time((time_t *) NULL);
2829#if defined(MAGICKCORE_HAVE_LOCALTIME_R)
2830 (void) localtime_r(&seconds,&local_time);
2831#else
2832 (void) memcpy(&local_time,localtime(&seconds),sizeof(local_time));
2833#endif
cristy151b66d2015-04-15 10:50:31 +00002834 (void) FormatLocaleString(date,MagickPathExtent,"D:%04d%02d%02d%02d%02d%02d",
cristy3ed852e2009-09-05 21:47:34 +00002835 local_time.tm_year+1900,local_time.tm_mon+1,local_time.tm_mday,
2836 local_time.tm_hour,local_time.tm_min,local_time.tm_sec);
Cristy07b9f5e2016-02-17 13:13:47 -05002837 (void) FormatLocaleString(buffer,MagickPathExtent,"/CreationDate (%s)\n",
2838 date);
cristy3ed852e2009-09-05 21:47:34 +00002839 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00002840 (void) FormatLocaleString(buffer,MagickPathExtent,"/ModDate (%s)\n",date);
cristy3ed852e2009-09-05 21:47:34 +00002841 (void) WriteBlobString(image,buffer);
Cristy519e0052016-10-16 15:28:40 -04002842 url=GetMagickHomeURL();
cristy151b66d2015-04-15 10:50:31 +00002843 (void) FormatLocaleString(buffer,MagickPathExtent,"/Producer (%s)\n",
Cristy519e0052016-10-16 15:28:40 -04002844 EscapeParenthesis(url));
2845 url=DestroyString(url);
cristy3ed852e2009-09-05 21:47:34 +00002846 (void) WriteBlobString(image,buffer);
2847 (void) WriteBlobString(image,">>\n");
2848 (void) WriteBlobString(image,"endobj\n");
2849 /*
2850 Write Xref object.
2851 */
cristyf40010e2012-05-13 13:52:20 +00002852 offset=TellBlob(image)-xref[0]+
2853 (LocaleCompare(image_info->magick,"PDFA") == 0 ? 6 : 0)+10;
cristy3ed852e2009-09-05 21:47:34 +00002854 (void) WriteBlobString(image,"xref\n");
cristy151b66d2015-04-15 10:50:31 +00002855 (void) FormatLocaleString(buffer,MagickPathExtent,"0 %.20g\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002856 object+1);
cristy3ed852e2009-09-05 21:47:34 +00002857 (void) WriteBlobString(image,buffer);
2858 (void) WriteBlobString(image,"0000000000 65535 f \n");
cristybb503372010-05-27 20:51:26 +00002859 for (i=0; i < (ssize_t) object; i++)
cristy3ed852e2009-09-05 21:47:34 +00002860 {
cristy151b66d2015-04-15 10:50:31 +00002861 (void) FormatLocaleString(buffer,MagickPathExtent,"%010lu 00000 n \n",
cristyf2faecf2010-05-28 19:19:36 +00002862 (unsigned long) xref[i]);
cristy3ed852e2009-09-05 21:47:34 +00002863 (void) WriteBlobString(image,buffer);
2864 }
2865 (void) WriteBlobString(image,"trailer\n");
2866 (void) WriteBlobString(image,"<<\n");
cristy151b66d2015-04-15 10:50:31 +00002867 (void) FormatLocaleString(buffer,MagickPathExtent,"/Size %.20g\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002868 object+1);
cristy3ed852e2009-09-05 21:47:34 +00002869 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00002870 (void) FormatLocaleString(buffer,MagickPathExtent,"/Info %.20g 0 R\n",(double)
cristye8c25f92010-06-03 00:53:06 +00002871 info_id);
cristy3ed852e2009-09-05 21:47:34 +00002872 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00002873 (void) FormatLocaleString(buffer,MagickPathExtent,"/Root %.20g 0 R\n",(double)
cristye8c25f92010-06-03 00:53:06 +00002874 root_id);
cristy3ed852e2009-09-05 21:47:34 +00002875 (void) WriteBlobString(image,buffer);
cristy1b70dc82012-07-01 23:15:05 +00002876 (void) SignatureImage(image,exception);
cristy151b66d2015-04-15 10:50:31 +00002877 (void) FormatLocaleString(buffer,MagickPathExtent,"/ID [<%s> <%s>]\n",
cristy1b70dc82012-07-01 23:15:05 +00002878 GetImageProperty(image,"signature",exception),
2879 GetImageProperty(image,"signature",exception));
2880 (void) WriteBlobString(image,buffer);
cristy3ed852e2009-09-05 21:47:34 +00002881 (void) WriteBlobString(image,">>\n");
2882 (void) WriteBlobString(image,"startxref\n");
cristy151b66d2015-04-15 10:50:31 +00002883 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g\n",(double) offset);
cristy3ed852e2009-09-05 21:47:34 +00002884 (void) WriteBlobString(image,buffer);
2885 (void) WriteBlobString(image,"%%EOF\n");
2886 xref=(MagickOffsetType *) RelinquishMagickMemory(xref);
2887 (void) CloseBlob(image);
2888 return(MagickTrue);
2889}