blob: 51f7a8fccc1900c76cfddcc2ba31089fb3cca87c [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 }
500 (void) ParseAbsoluteGeometry(PSPageGeometry,&page);
501 if (image_info->page != (char *) NULL)
502 (void) ParseAbsoluteGeometry(image_info->page,&page);
503 page.width=(size_t) ceil((double) (page.width*image->resolution.x/delta.x)-
504 0.5);
505 page.height=(size_t) ceil((double) (page.height*image->resolution.y/delta.y)-
506 0.5);
cristy3ed852e2009-09-05 21:47:34 +0000507 /*
508 Determine page geometry from the PDF media box.
509 */
510 cmyk=image_info->colorspace == CMYKColorspace ? MagickTrue : MagickFalse;
cristy092ec8d2013-04-26 13:46:22 +0000511 cropbox=IsStringTrue(GetImageOption(image_info,"pdf:use-cropbox"));
cristy72437802015-03-23 00:42:27 +0000512 stop_on_error=IsStringTrue(GetImageOption(image_info,"pdf:stop-on-error"));
cristy092ec8d2013-04-26 13:46:22 +0000513 trimbox=IsStringTrue(GetImageOption(image_info,"pdf:use-trimbox"));
cristy3ed852e2009-09-05 21:47:34 +0000514 count=0;
515 spotcolor=0;
516 (void) ResetMagickMemory(&bounding_box,0,sizeof(bounding_box));
517 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
518 (void) ResetMagickMemory(&hires_bounds,0,sizeof(hires_bounds));
cristy3ed852e2009-09-05 21:47:34 +0000519 (void) ResetMagickMemory(command,0,sizeof(command));
cristy3ed852e2009-09-05 21:47:34 +0000520 angle=0.0;
521 p=command;
522 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
523 {
524 /*
525 Note PDF elements.
526 */
527 if (c == '\n')
528 c=' ';
529 *p++=(char) c;
cristyfd6e87b2011-02-03 18:48:17 +0000530 if ((c != (int) '/') && (c != (int) '%') &&
cristy151b66d2015-04-15 10:50:31 +0000531 ((size_t) (p-command) < (MagickPathExtent-1)))
cristy3ed852e2009-09-05 21:47:34 +0000532 continue;
533 *(--p)='\0';
534 p=command;
535 if (LocaleNCompare(PDFRotate,command,strlen(PDFRotate)) == 0)
536 count=(ssize_t) sscanf(command,"Rotate %lf",&angle);
537 /*
538 Is this a CMYK document?
539 */
cristy0a39a5c2012-06-27 12:51:45 +0000540 if (LocaleNCompare(DefaultCMYK,command,strlen(DefaultCMYK)) == 0)
541 cmyk=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +0000542 if (LocaleNCompare(DeviceCMYK,command,strlen(DeviceCMYK)) == 0)
543 cmyk=MagickTrue;
cristy0a39a5c2012-06-27 12:51:45 +0000544 if (LocaleNCompare(CMYKProcessColor,command,strlen(CMYKProcessColor)) == 0)
545 cmyk=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +0000546 if (LocaleNCompare(SpotColor,command,strlen(SpotColor)) == 0)
547 {
548 char
cristy151b66d2015-04-15 10:50:31 +0000549 name[MagickPathExtent],
550 property[MagickPathExtent],
cristy3ed852e2009-09-05 21:47:34 +0000551 *value;
552
cristy3ed852e2009-09-05 21:47:34 +0000553 /*
554 Note spot names.
555 */
cristy151b66d2015-04-15 10:50:31 +0000556 (void) FormatLocaleString(property,MagickPathExtent,"pdf:SpotColor-%.20g",
cristye8c25f92010-06-03 00:53:06 +0000557 (double) spotcolor++);
cristy3ed852e2009-09-05 21:47:34 +0000558 i=0;
559 for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
560 {
cristy151b66d2015-04-15 10:50:31 +0000561 if ((isspace(c) != 0) || (c == '/') || ((i+1) == MagickPathExtent))
cristy3ed852e2009-09-05 21:47:34 +0000562 break;
563 name[i++]=(char) c;
564 }
565 name[i]='\0';
566 value=AcquireString(name);
567 (void) SubstituteString(&value,"#20"," ");
cristyd15e6592011-10-15 00:13:06 +0000568 (void) SetImageProperty(image,property,value,exception);
cristy3ed852e2009-09-05 21:47:34 +0000569 value=DestroyString(value);
570 continue;
571 }
572 if (LocaleNCompare(PDFVersion,command,strlen(PDFVersion)) == 0)
cristyd15e6592011-10-15 00:13:06 +0000573 (void) SetImageProperty(image,"pdf:Version",command,exception);
cristydeb50be2011-11-24 00:19:33 +0000574 if (image_info->page != (char *) NULL)
575 continue;
cristy3ed852e2009-09-05 21:47:34 +0000576 count=0;
577 if (cropbox != MagickFalse)
578 {
579 if (LocaleNCompare(CropBox,command,strlen(CropBox)) == 0)
580 {
581 /*
582 Note region defined by crop box.
583 */
584 count=(ssize_t) sscanf(command,"CropBox [%lf %lf %lf %lf",
585 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
586 if (count != 4)
587 count=(ssize_t) sscanf(command,"CropBox[%lf %lf %lf %lf",
588 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
589 }
590 }
591 else
592 if (trimbox != MagickFalse)
593 {
594 if (LocaleNCompare(TrimBox,command,strlen(TrimBox)) == 0)
595 {
596 /*
597 Note region defined by trim box.
598 */
599 count=(ssize_t) sscanf(command,"TrimBox [%lf %lf %lf %lf",
600 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
601 if (count != 4)
602 count=(ssize_t) sscanf(command,"TrimBox[%lf %lf %lf %lf",
603 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
604 }
605 }
606 else
607 if (LocaleNCompare(MediaBox,command,strlen(MediaBox)) == 0)
608 {
609 /*
610 Note region defined by media box.
611 */
612 count=(ssize_t) sscanf(command,"MediaBox [%lf %lf %lf %lf",
613 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
614 if (count != 4)
615 count=(ssize_t) sscanf(command,"MediaBox[%lf %lf %lf %lf",
616 &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
617 }
618 if (count != 4)
619 continue;
cristyad29a8c2012-01-25 23:06:43 +0000620 if ((fabs(bounds.x2-bounds.x1) <= fabs(hires_bounds.x2-hires_bounds.x1)) ||
cristyc2038532011-12-29 16:43:05 +0000621 (fabs(bounds.y2-bounds.y1) <= fabs(hires_bounds.y2-hires_bounds.y1)))
622 continue;
cristy48853f12011-11-11 15:47:57 +0000623 hires_bounds=bounds;
cristy3ed852e2009-09-05 21:47:34 +0000624 }
cristy7b0fcf12011-11-24 00:24:07 +0000625 if ((fabs(hires_bounds.x2-hires_bounds.x1) >= MagickEpsilon) &&
cristy614bc082011-11-24 00:49:08 +0000626 (fabs(hires_bounds.y2-hires_bounds.y1) >= MagickEpsilon))
cristy48853f12011-11-11 15:47:57 +0000627 {
628 /*
629 Set PDF render geometry.
630 */
cristy151b66d2015-04-15 10:50:31 +0000631 (void) FormatLocaleString(geometry,MagickPathExtent,"%gx%g%+.15g%+.15g",
cristy48853f12011-11-11 15:47:57 +0000632 hires_bounds.x2-bounds.x1,hires_bounds.y2-hires_bounds.y1,
633 hires_bounds.x1,hires_bounds.y1);
634 (void) SetImageProperty(image,"pdf:HiResBoundingBox",geometry,exception);
cristydeb50be2011-11-24 00:19:33 +0000635 page.width=(size_t) ceil((double) ((hires_bounds.x2-hires_bounds.x1)*
636 image->resolution.x/delta.x)-0.5);
637 page.height=(size_t) ceil((double) ((hires_bounds.y2-hires_bounds.y1)*
638 image->resolution.y/delta.y)-0.5);
cristy48853f12011-11-11 15:47:57 +0000639 }
dirk83c36b12014-02-23 13:07:22 +0000640 fitPage=MagickFalse;
641 option=GetImageOption(image_info,"pdf:fit-page");
642 if (option != (char *) NULL)
643 {
644 char
dirkdb6b52c2015-11-25 23:44:01 +0100645 *page_geometry;
dirk83c36b12014-02-23 13:07:22 +0000646
dirkdb6b52c2015-11-25 23:44:01 +0100647 page_geometry=GetPageGeometry(option);
648 flags=ParseMetaGeometry(page_geometry,&page.x,&page.y,&page.width,
649 &page.height);
dirk83c36b12014-02-23 13:07:22 +0000650 if (flags == NoValue)
651 {
652 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
653 "InvalidGeometry","`%s'",option);
654 image=DestroyImage(image);
655 return((Image *) NULL);
656 }
657 page.width=(size_t) ceil((double) (page.width*image->resolution.x/delta.x)
658 -0.5);
659 page.height=(size_t) ceil((double) (page.height*image->resolution.y/
660 delta.y) -0.5);
dirkdb6b52c2015-11-25 23:44:01 +0100661 page_geometry=DestroyString(page_geometry);
dirk83c36b12014-02-23 13:07:22 +0000662 fitPage=MagickTrue;
663 }
cristy3ed852e2009-09-05 21:47:34 +0000664 (void) CloseBlob(image);
665 if ((fabs(angle) == 90.0) || (fabs(angle) == 270.0))
666 {
cristybb503372010-05-27 20:51:26 +0000667 size_t
cristy3ed852e2009-09-05 21:47:34 +0000668 swap;
669
670 swap=page.width;
671 page.width=page.height;
672 page.height=swap;
673 }
cristy3d9f5ba2012-06-26 13:37:31 +0000674 if (IssRGBCompatibleColorspace(image_info->colorspace) != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000675 cmyk=MagickFalse;
676 /*
677 Create Ghostscript control file.
678 */
679 file=AcquireUniqueFileResource(postscript_filename);
680 if (file == -1)
681 {
682 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
683 image_info->filename);
684 image=DestroyImage(image);
685 return((Image *) NULL);
686 }
687 count=write(file," ",1);
688 file=close(file)-1;
689 /*
690 Render Postscript with the Ghostscript delegate.
691 */
692 if ((image_info->ping != MagickFalse) ||
693 (image_info->monochrome != MagickFalse))
694 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("");
cristy151b66d2015-04-15 10:50:31 +0000708 (void) FormatLocaleString(density,MagickPathExtent,"%gx%g",image->resolution.x,
cristy2a11bef2011-10-28 18:33:11 +0000709 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)
cristy151b66d2015-04-15 10:50:31 +0000720 (void) ConcatenateMagickString(options,"-dPDFSTOPONERROR ",MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +0000721 if (trimbox != MagickFalse)
cristy151b66d2015-04-15 10:50:31 +0000722 (void) ConcatenateMagickString(options,"-dUseTrimBox ",MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +0000723 read_info=CloneImageInfo(image_info);
724 *read_info->magick='\0';
725 if (read_info->number_scenes != 0)
726 {
727 char
cristy151b66d2015-04-15 10:50:31 +0000728 pages[MagickPathExtent];
cristy3ed852e2009-09-05 21:47:34 +0000729
cristy151b66d2015-04-15 10:50:31 +0000730 (void) FormatLocaleString(pages,MagickPathExtent,"-dFirstPage=%.20g "
cristye8c25f92010-06-03 00:53:06 +0000731 "-dLastPage=%.20g",(double) read_info->scene+1,(double)
cristyf2faecf2010-05-28 19:19:36 +0000732 (read_info->scene+read_info->number_scenes));
cristy151b66d2015-04-15 10:50:31 +0000733 (void) ConcatenateMagickString(options,pages,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +0000734 read_info->number_scenes=0;
735 if (read_info->scenes != (char *) NULL)
736 *read_info->scenes='\0';
737 }
cristy092ec8d2013-04-26 13:46:22 +0000738 option=GetImageOption(image_info,"authenticate");
anthony1afdc7a2011-10-05 11:54:28 +0000739 if (option != (const char *) NULL)
cristy151b66d2015-04-15 10:50:31 +0000740 (void) FormatLocaleString(options+strlen(options),MagickPathExtent,
anthony1afdc7a2011-10-05 11:54:28 +0000741 " -sPCLPassword=%s",option);
cristy151b66d2015-04-15 10:50:31 +0000742 (void) CopyMagickString(filename,read_info->filename,MagickPathExtent);
cristya97426c2011-02-04 01:41:27 +0000743 (void) AcquireUniqueFilename(filename);
dirkfd826fc2014-06-04 21:11:30 +0000744 (void) RelinquishUniqueFileResource(filename);
cristy151b66d2015-04-15 10:50:31 +0000745 (void) ConcatenateMagickString(filename,"%d",MagickPathExtent);
746 (void) FormatLocaleString(command,MagickPathExtent,
cristy3ed852e2009-09-05 21:47:34 +0000747 GetDelegateCommands(delegate_info),
748 read_info->antialias != MagickFalse ? 4 : 1,
cristy209d4692014-03-19 12:40:01 +0000749 read_info->antialias != MagickFalse ? 4 : 1,density,options,filename,
cristya97426c2011-02-04 01:41:27 +0000750 postscript_filename,input_filename);
cristy51816562015-04-12 13:18:38 +0000751 options=DestroyString(options);
752 density=DestroyString(density);
cristy60575362014-10-18 13:22:50 +0000753 *message='\0';
754 status=InvokePDFDelegate(read_info->verbose,command,message,exception);
cristy3ed852e2009-09-05 21:47:34 +0000755 (void) RelinquishUniqueFileResource(postscript_filename);
cristy3ed852e2009-09-05 21:47:34 +0000756 (void) RelinquishUniqueFileResource(input_filename);
cristya97426c2011-02-04 01:41:27 +0000757 pdf_image=(Image *) NULL;
758 if (status == MagickFalse)
759 for (i=1; ; i++)
760 {
cristya97426c2011-02-04 01:41:27 +0000761 (void) InterpretImageFilename(image_info,image,filename,(int) i,
cristy6fccee12011-10-20 18:43:18 +0000762 read_info->filename,exception);
cristya97426c2011-02-04 01:41:27 +0000763 if (IsPDFRendered(read_info->filename) == MagickFalse)
764 break;
765 (void) RelinquishUniqueFileResource(read_info->filename);
766 }
767 else
768 for (i=1; ; i++)
769 {
cristya97426c2011-02-04 01:41:27 +0000770 (void) InterpretImageFilename(image_info,image,filename,(int) i,
cristy6fccee12011-10-20 18:43:18 +0000771 read_info->filename,exception);
cristya97426c2011-02-04 01:41:27 +0000772 if (IsPDFRendered(read_info->filename) == MagickFalse)
773 break;
cristyb0ffea22012-06-17 20:22:01 +0000774 read_info->blob=NULL;
775 read_info->length=0;
cristya97426c2011-02-04 01:41:27 +0000776 next=ReadImage(read_info,exception);
777 (void) RelinquishUniqueFileResource(read_info->filename);
778 if (next == (Image *) NULL)
779 break;
780 AppendImageToList(&pdf_image,next);
781 }
cristy3ed852e2009-09-05 21:47:34 +0000782 read_info=DestroyImageInfo(read_info);
783 if (pdf_image == (Image *) NULL)
784 {
cristy60575362014-10-18 13:22:50 +0000785 if (*message != '\0')
786 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
787 "PDFDelegateFailed","`%s'",message);
cristyb6cabe82011-03-18 13:25:59 +0000788 image=DestroyImage(image);
cristy3ed852e2009-09-05 21:47:34 +0000789 return((Image *) NULL);
790 }
791 if (LocaleCompare(pdf_image->magick,"BMP") == 0)
792 {
793 Image
794 *cmyk_image;
795
796 cmyk_image=ConsolidateCMYKImages(pdf_image,exception);
797 if (cmyk_image != (Image *) NULL)
798 {
799 pdf_image=DestroyImageList(pdf_image);
800 pdf_image=cmyk_image;
801 }
802 }
803 if (image_info->number_scenes != 0)
804 {
805 Image
806 *clone_image;
807
cristy3ed852e2009-09-05 21:47:34 +0000808 /*
809 Add place holder images to meet the subimage specification requirement.
810 */
cristybb503372010-05-27 20:51:26 +0000811 for (i=0; i < (ssize_t) image_info->scene; i++)
cristy3ed852e2009-09-05 21:47:34 +0000812 {
813 clone_image=CloneImage(pdf_image,1,1,MagickTrue,exception);
814 if (clone_image != (Image *) NULL)
815 PrependImageToList(&pdf_image,clone_image);
816 }
817 }
818 do
819 {
cristy151b66d2015-04-15 10:50:31 +0000820 (void) CopyMagickString(pdf_image->filename,filename,MagickPathExtent);
821 (void) CopyMagickString(pdf_image->magick,image->magick,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +0000822 pdf_image->page=page;
823 (void) CloneImageProfiles(pdf_image,image);
824 (void) CloneImageProperties(pdf_image,image);
825 next=SyncNextImageInList(pdf_image);
826 if (next != (Image *) NULL)
827 pdf_image=next;
828 } while (next != (Image *) NULL);
829 image=DestroyImage(image);
830 scene=0;
831 for (next=GetFirstImageInList(pdf_image); next != (Image *) NULL; )
832 {
833 next->scene=scene++;
834 next=GetNextImageInList(next);
835 }
836 return(GetFirstImageInList(pdf_image));
837}
838
839/*
840%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
841% %
842% %
843% %
844% R e g i s t e r P D F I m a g e %
845% %
846% %
847% %
848%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
849%
850% RegisterPDFImage() adds properties for the PDF image format to
851% the list of supported formats. The properties include the image format
852% tag, a method to read and/or write the format, whether the format
853% supports the saving of more than one frame to the same file or blob,
854% whether the format supports native in-memory I/O, and a brief
855% description of the format.
856%
857% The format of the RegisterPDFImage method is:
858%
cristybb503372010-05-27 20:51:26 +0000859% size_t RegisterPDFImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000860%
861*/
cristybb503372010-05-27 20:51:26 +0000862ModuleExport size_t RegisterPDFImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000863{
864 MagickInfo
865 *entry;
866
dirk06b627a2015-04-06 18:59:17 +0000867 entry=AcquireMagickInfo("PDF","AI","Adobe Illustrator CS2");
cristy3ed852e2009-09-05 21:47:34 +0000868 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
869 entry->encoder=(EncodeImageHandler *) WritePDFImage;
dirk08e9a112015-02-22 01:51:41 +0000870 entry->flags^=CoderAdjoinFlag;
871 entry->flags^=CoderBlobSupportFlag;
872 entry->flags|=CoderSeekableStreamFlag;
cristy5aefbeb2013-08-09 12:13:32 +0000873 entry->mime_type=ConstantString("application/pdf");
cristy3ed852e2009-09-05 21:47:34 +0000874 (void) RegisterMagickInfo(entry);
dirk06b627a2015-04-06 18:59:17 +0000875 entry=AcquireMagickInfo("PDF","EPDF",
876 "Encapsulated Portable Document Format");
cristy3ed852e2009-09-05 21:47:34 +0000877 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
878 entry->encoder=(EncodeImageHandler *) WritePDFImage;
dirk08e9a112015-02-22 01:51:41 +0000879 entry->flags^=CoderAdjoinFlag;
880 entry->flags^=CoderBlobSupportFlag;
881 entry->flags|=CoderSeekableStreamFlag;
cristy5aefbeb2013-08-09 12:13:32 +0000882 entry->mime_type=ConstantString("application/pdf");
cristy3ed852e2009-09-05 21:47:34 +0000883 (void) RegisterMagickInfo(entry);
dirk06b627a2015-04-06 18:59:17 +0000884 entry=AcquireMagickInfo("PDF","PDF","Portable Document Format");
cristy3ed852e2009-09-05 21:47:34 +0000885 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
886 entry->encoder=(EncodeImageHandler *) WritePDFImage;
887 entry->magick=(IsImageFormatHandler *) IsPDF;
dirk08e9a112015-02-22 01:51:41 +0000888 entry->flags^=CoderBlobSupportFlag;
889 entry->flags|=CoderSeekableStreamFlag;
cristy5aefbeb2013-08-09 12:13:32 +0000890 entry->mime_type=ConstantString("application/pdf");
cristy3ed852e2009-09-05 21:47:34 +0000891 (void) RegisterMagickInfo(entry);
dirk06b627a2015-04-06 18:59:17 +0000892 entry=AcquireMagickInfo("PDF","PDFA","Portable Document Archive Format");
cristy3ed852e2009-09-05 21:47:34 +0000893 entry->decoder=(DecodeImageHandler *) ReadPDFImage;
894 entry->encoder=(EncodeImageHandler *) WritePDFImage;
895 entry->magick=(IsImageFormatHandler *) IsPDF;
dirk08e9a112015-02-22 01:51:41 +0000896 entry->flags^=CoderBlobSupportFlag;
897 entry->flags|=CoderSeekableStreamFlag;
cristy5aefbeb2013-08-09 12:13:32 +0000898 entry->mime_type=ConstantString("application/pdf");
cristy3ed852e2009-09-05 21:47:34 +0000899 (void) RegisterMagickInfo(entry);
900 return(MagickImageCoderSignature);
901}
902
903/*
904%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
905% %
906% %
907% %
908% U n r e g i s t e r P D F I m a g e %
909% %
910% %
911% %
912%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
913%
914% UnregisterPDFImage() removes format registrations made by the
915% PDF module from the list of supported formats.
916%
917% The format of the UnregisterPDFImage method is:
918%
919% UnregisterPDFImage(void)
920%
921*/
922ModuleExport void UnregisterPDFImage(void)
923{
924 (void) UnregisterMagickInfo("AI");
925 (void) UnregisterMagickInfo("EPDF");
926 (void) UnregisterMagickInfo("PDF");
927 (void) UnregisterMagickInfo("PDFA");
928}
929
930/*
931%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
932% %
933% %
934% %
935% W r i t e P D F I m a g e %
936% %
937% %
938% %
939%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
940%
941% WritePDFImage() writes an image in the Portable Document image
942% format.
943%
944% The format of the WritePDFImage method is:
945%
cristy1e178e72011-08-28 19:44:34 +0000946% MagickBooleanType WritePDFImage(const ImageInfo *image_info,
947% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000948%
949% A description of each parameter follows.
950%
951% o image_info: the image info.
952%
953% o image: The image.
954%
cristy1e178e72011-08-28 19:44:34 +0000955% o exception: return any errors or warnings in this structure.
956%
cristy3ed852e2009-09-05 21:47:34 +0000957*/
958
cristy3ed852e2009-09-05 21:47:34 +0000959static char *EscapeParenthesis(const char *text)
960{
961 register char
962 *p;
963
cristybb503372010-05-27 20:51:26 +0000964 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000965 i;
966
cristybb503372010-05-27 20:51:26 +0000967 size_t
cristy3ed852e2009-09-05 21:47:34 +0000968 escapes;
969
cristyaff6d802011-04-26 01:46:31 +0000970 static char
cristy151b66d2015-04-15 10:50:31 +0000971 buffer[MagickPathExtent];
cristyaff6d802011-04-26 01:46:31 +0000972
cristy3ed852e2009-09-05 21:47:34 +0000973 escapes=0;
974 p=buffer;
cristy151b66d2015-04-15 10:50:31 +0000975 for (i=0; i < (ssize_t) MagickMin(strlen(text),(MagickPathExtent-escapes-1)); i++)
cristy3ed852e2009-09-05 21:47:34 +0000976 {
977 if ((text[i] == '(') || (text[i] == ')'))
978 {
979 *p++='\\';
980 escapes++;
981 }
982 *p++=text[i];
983 }
984 *p='\0';
985 return(buffer);
986}
987
Cristyce4a3552015-12-14 13:53:35 -0500988static size_t UTF8ToUTF16(const unsigned char *utf8,wchar_t *utf16)
989{
990 register const unsigned char
991 *p;
992
993 if (utf16 != (wchar_t *) NULL)
994 {
995 register wchar_t
996 *q;
997
998 wchar_t
999 c;
1000
1001 /*
1002 Convert UTF-8 to UTF-16.
1003 */
1004 q=utf16;
1005 for (p=utf8; *p != '\0'; p++)
1006 {
1007 if ((*p & 0x80) == 0)
1008 *q=(*p);
1009 else
1010 if ((*p & 0xE0) == 0xC0)
1011 {
1012 c=(*p);
1013 *q=(c & 0x1F) << 6;
1014 p++;
1015 if ((*p & 0xC0) != 0x80)
1016 return(0);
1017 *q|=(*p & 0x3F);
1018 }
1019 else
1020 if ((*p & 0xF0) == 0xE0)
1021 {
1022 c=(*p);
1023 *q=c << 12;
1024 p++;
1025 if ((*p & 0xC0) != 0x80)
1026 return(0);
1027 c=(*p);
1028 *q|=(c & 0x3F) << 6;
1029 p++;
1030 if ((*p & 0xC0) != 0x80)
1031 return(0);
1032 *q|=(*p & 0x3F);
1033 }
1034 else
1035 return(0);
1036 q++;
1037 }
1038 *q++='\0';
1039 return(q-utf16);
1040 }
1041 /*
1042 Compute UTF-16 string length.
1043 */
1044 for (p=utf8; *p != '\0'; p++)
1045 {
1046 if ((*p & 0x80) == 0)
1047 ;
1048 else
1049 if ((*p & 0xE0) == 0xC0)
1050 {
1051 p++;
1052 if ((*p & 0xC0) != 0x80)
1053 return(0);
1054 }
1055 else
1056 if ((*p & 0xF0) == 0xE0)
1057 {
1058 p++;
1059 if ((*p & 0xC0) != 0x80)
1060 return(0);
1061 p++;
1062 if ((*p & 0xC0) != 0x80)
1063 return(0);
1064 }
1065 else
1066 return(0);
1067 }
1068 return(p-utf8);
1069}
1070
1071static wchar_t *ConvertUTF8ToUTF16(const unsigned char *source,size_t *length)
1072{
1073 wchar_t
1074 *utf16;
1075
1076 *length=UTF8ToUTF16(source,(wchar_t *) NULL);
1077 if (*length == 0)
1078 {
1079 register ssize_t
1080 i;
1081
1082 /*
1083 Not UTF-8, just copy.
1084 */
1085 *length=strlen((const char *) source);
1086 utf16=(wchar_t *) AcquireQuantumMemory(*length+1,sizeof(*utf16));
1087 if (utf16 == (wchar_t *) NULL)
1088 return((wchar_t *) NULL);
1089 for (i=0; i <= (ssize_t) *length; i++)
1090 utf16[i]=source[i];
1091 return(utf16);
1092 }
1093 utf16=(wchar_t *) AcquireQuantumMemory(*length+1,sizeof(*utf16));
1094 if (utf16 == (wchar_t *) NULL)
1095 return((wchar_t *) NULL);
1096 *length=UTF8ToUTF16(source,utf16);
1097 return(utf16);
1098}
1099
cristy47b838c2009-09-19 16:09:30 +00001100static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
cristy018f07f2011-09-04 21:15:19 +00001101 Image *image,Image *inject_image,ExceptionInfo *exception)
cristy47b838c2009-09-19 16:09:30 +00001102{
cristy47b838c2009-09-19 16:09:30 +00001103 Image
cristy80975862009-09-25 14:34:31 +00001104 *group4_image;
cristy47b838c2009-09-19 16:09:30 +00001105
1106 ImageInfo
1107 *write_info;
1108
cristy47b838c2009-09-19 16:09:30 +00001109 MagickBooleanType
1110 status;
1111
cristy80975862009-09-25 14:34:31 +00001112 size_t
1113 length;
cristy47b838c2009-09-19 16:09:30 +00001114
1115 unsigned char
cristy80975862009-09-25 14:34:31 +00001116 *group4;
cristy47b838c2009-09-19 16:09:30 +00001117
cristy42751fe2009-10-05 00:15:50 +00001118 status=MagickTrue;
cristy47b838c2009-09-19 16:09:30 +00001119 write_info=CloneImageInfo(image_info);
cristy151b66d2015-04-15 10:50:31 +00001120 (void) CopyMagickString(write_info->filename,"GROUP4:",MagickPathExtent);
1121 (void) CopyMagickString(write_info->magick,"GROUP4",MagickPathExtent);
cristy018f07f2011-09-04 21:15:19 +00001122 group4_image=CloneImage(inject_image,0,0,MagickTrue,exception);
cristy80975862009-09-25 14:34:31 +00001123 if (group4_image == (Image *) NULL)
1124 return(MagickFalse);
1125 group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
cristy018f07f2011-09-04 21:15:19 +00001126 exception);
cristy80975862009-09-25 14:34:31 +00001127 group4_image=DestroyImage(group4_image);
1128 if (group4 == (unsigned char *) NULL)
1129 return(MagickFalse);
cristy47b838c2009-09-19 16:09:30 +00001130 write_info=DestroyImageInfo(write_info);
cristy80975862009-09-25 14:34:31 +00001131 if (WriteBlob(image,length,group4) != (ssize_t) length)
1132 status=MagickFalse;
1133 group4=(unsigned char *) RelinquishMagickMemory(group4);
1134 return(status);
cristy47b838c2009-09-19 16:09:30 +00001135}
1136
cristy1e178e72011-08-28 19:44:34 +00001137static MagickBooleanType WritePDFImage(const ImageInfo *image_info,Image *image,
1138 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001139{
1140#define CFormat "/Filter [ /%s ]\n"
1141#define ObjectsPerImage 14
1142
dirk93b02b72013-11-16 16:03:36 +00001143DisableMSCWarning(4310)
cristy3ed852e2009-09-05 21:47:34 +00001144 static const char
1145 XMPProfile[]=
1146 {
1147 "<?xpacket begin=\"%s\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n"
1148 "<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"
1149 " <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n"
1150 " <rdf:Description rdf:about=\"\"\n"
1151 " xmlns:xap=\"http://ns.adobe.com/xap/1.0/\">\n"
1152 " <xap:ModifyDate>%s</xap:ModifyDate>\n"
1153 " <xap:CreateDate>%s</xap:CreateDate>\n"
1154 " <xap:MetadataDate>%s</xap:MetadataDate>\n"
1155 " <xap:CreatorTool>%s</xap:CreatorTool>\n"
1156 " </rdf:Description>\n"
1157 " <rdf:Description rdf:about=\"\"\n"
1158 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"
1159 " <dc:format>application/pdf</dc:format>\n"
cristy1b70dc82012-07-01 23:15:05 +00001160 " <dc:title>\n"
1161 " <rdf:Alt>\n"
1162 " <rdf:li xml:lang=\"x-default\">%s</rdf:li>\n"
1163 " </rdf:Alt>\n"
1164 " </dc:title>\n"
cristy3ed852e2009-09-05 21:47:34 +00001165 " </rdf:Description>\n"
1166 " <rdf:Description rdf:about=\"\"\n"
1167 " xmlns:xapMM=\"http://ns.adobe.com/xap/1.0/mm/\">\n"
1168 " <xapMM:DocumentID>uuid:6ec119d7-7982-4f56-808d-dfe64f5b35cf</xapMM:DocumentID>\n"
1169 " <xapMM:InstanceID>uuid:a79b99b4-6235-447f-9f6c-ec18ef7555cb</xapMM:InstanceID>\n"
1170 " </rdf:Description>\n"
1171 " <rdf:Description rdf:about=\"\"\n"
1172 " xmlns:pdf=\"http://ns.adobe.com/pdf/1.3/\">\n"
1173 " <pdf:Producer>%s</pdf:Producer>\n"
1174 " </rdf:Description>\n"
1175 " <rdf:Description rdf:about=\"\"\n"
1176 " xmlns:pdfaid=\"http://www.aiim.org/pdfa/ns/id/\">\n"
cristyba5f54b2014-05-19 22:10:08 +00001177 " <pdfaid:part>3</pdfaid:part>\n"
cristy3ed852e2009-09-05 21:47:34 +00001178 " <pdfaid:conformance>B</pdfaid:conformance>\n"
1179 " </rdf:Description>\n"
1180 " </rdf:RDF>\n"
1181 "</x:xmpmeta>\n"
1182 "<?xpacket end=\"w\"?>\n"
1183 },
1184 XMPProfileMagick[4]= { (char) 0xef, (char) 0xbb, (char) 0xbf, (char) 0x00 };
dirk93b02b72013-11-16 16:03:36 +00001185RestoreMSCWarning
cristy3ed852e2009-09-05 21:47:34 +00001186
1187 char
cristy151b66d2015-04-15 10:50:31 +00001188 basename[MagickPathExtent],
1189 buffer[MagickPathExtent],
1190 date[MagickPathExtent],
cristy3ed852e2009-09-05 21:47:34 +00001191 **labels,
cristy151b66d2015-04-15 10:50:31 +00001192 page_geometry[MagickPathExtent];
cristy3ed852e2009-09-05 21:47:34 +00001193
1194 CompressionType
1195 compression;
1196
1197 const char
1198 *value;
1199
1200 double
1201 pointsize;
1202
1203 GeometryInfo
1204 geometry_info;
1205
cristy3ed852e2009-09-05 21:47:34 +00001206 Image
1207 *next,
1208 *tile_image;
1209
1210 MagickBooleanType
1211 status;
1212
1213 MagickOffsetType
1214 offset,
1215 scene,
1216 *xref;
1217
1218 MagickSizeType
1219 number_pixels;
1220
1221 MagickStatusType
1222 flags;
1223
1224 PointInfo
1225 delta,
1226 resolution,
1227 scale;
1228
1229 RectangleInfo
1230 geometry,
1231 media_info,
1232 page_info;
1233
cristy4c08aed2011-07-01 19:47:50 +00001234 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +00001235 *p;
1236
1237 register unsigned char
1238 *q;
1239
cristybb503372010-05-27 20:51:26 +00001240 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001241 i,
1242 x;
1243
1244 size_t
cristyaff6d802011-04-26 01:46:31 +00001245 info_id,
1246 length,
1247 object,
1248 pages_id,
1249 root_id,
1250 text_size,
1251 version;
1252
1253 ssize_t
1254 count,
1255 y;
cristy3ed852e2009-09-05 21:47:34 +00001256
1257 struct tm
1258 local_time;
1259
1260 time_t
1261 seconds;
1262
1263 unsigned char
1264 *pixels;
1265
Cristyce4a3552015-12-14 13:53:35 -05001266 wchar_t
1267 *utf16;
1268
cristy3ed852e2009-09-05 21:47:34 +00001269 /*
1270 Open output image file.
1271 */
1272 assert(image_info != (const ImageInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +00001273 assert(image_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +00001274 assert(image != (Image *) NULL);
cristye1c94d92015-06-28 12:16:33 +00001275 assert(image->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +00001276 if (image->debug != MagickFalse)
1277 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +00001278 assert(exception != (ExceptionInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +00001279 assert(exception->signature == MagickCoreSignature);
cristy1e178e72011-08-28 19:44:34 +00001280 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +00001281 if (status == MagickFalse)
1282 return(status);
1283 /*
1284 Allocate X ref memory.
1285 */
1286 xref=(MagickOffsetType *) AcquireQuantumMemory(2048UL,sizeof(*xref));
1287 if (xref == (MagickOffsetType *) NULL)
1288 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1289 (void) ResetMagickMemory(xref,0,2048UL*sizeof(*xref));
1290 /*
1291 Write Info object.
1292 */
1293 object=0;
1294 version=3;
1295 if (image_info->compression == JPEG2000Compression)
cristybb503372010-05-27 20:51:26 +00001296 version=(size_t) MagickMax(version,5);
cristy3ed852e2009-09-05 21:47:34 +00001297 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
cristy17f11b02014-12-20 19:37:04 +00001298 if (next->alpha_trait != UndefinedPixelTrait)
cristybb503372010-05-27 20:51:26 +00001299 version=(size_t) MagickMax(version,4);
cristy3ed852e2009-09-05 21:47:34 +00001300 if (LocaleCompare(image_info->magick,"PDFA") == 0)
cristyc04c60e2011-05-31 23:58:45 +00001301 version=(size_t) MagickMax(version,6);
cristy151b66d2015-04-15 10:50:31 +00001302 (void) FormatLocaleString(buffer,MagickPathExtent,"%%PDF-1.%.20g \n",(double)
cristyf40010e2012-05-13 13:52:20 +00001303 version);
cristy3ed852e2009-09-05 21:47:34 +00001304 (void) WriteBlobString(image,buffer);
1305 if (LocaleCompare(image_info->magick,"PDFA") == 0)
cristy42bd8002014-01-26 02:06:27 +00001306 {
1307 (void) WriteBlobByte(image,'%');
1308 (void) WriteBlobByte(image,0xe2);
1309 (void) WriteBlobByte(image,0xe3);
1310 (void) WriteBlobByte(image,0xcf);
1311 (void) WriteBlobByte(image,0xd3);
1312 (void) WriteBlobByte(image,'\n');
1313 }
cristy3ed852e2009-09-05 21:47:34 +00001314 /*
1315 Write Catalog object.
1316 */
1317 xref[object++]=TellBlob(image);
1318 root_id=object;
cristy151b66d2015-04-15 10:50:31 +00001319 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g 0 obj\n",(double)
cristye8c25f92010-06-03 00:53:06 +00001320 object);
cristy3ed852e2009-09-05 21:47:34 +00001321 (void) WriteBlobString(image,buffer);
1322 (void) WriteBlobString(image,"<<\n");
1323 if (LocaleCompare(image_info->magick,"PDFA") != 0)
cristy151b66d2015-04-15 10:50:31 +00001324 (void) FormatLocaleString(buffer,MagickPathExtent,"/Pages %.20g 0 R\n",(double)
cristyf40010e2012-05-13 13:52:20 +00001325 object+1);
cristy3ed852e2009-09-05 21:47:34 +00001326 else
1327 {
cristy151b66d2015-04-15 10:50:31 +00001328 (void) FormatLocaleString(buffer,MagickPathExtent,"/Metadata %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001329 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001330 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00001331 (void) FormatLocaleString(buffer,MagickPathExtent,"/Pages %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001332 (double) object+2);
cristy3ed852e2009-09-05 21:47:34 +00001333 }
1334 (void) WriteBlobString(image,buffer);
1335 (void) WriteBlobString(image,"/Type /Catalog\n");
1336 (void) WriteBlobString(image,">>\n");
1337 (void) WriteBlobString(image,"endobj\n");
cristy1b70dc82012-07-01 23:15:05 +00001338 GetPathComponent(image->filename,BasePath,basename);
cristy3ed852e2009-09-05 21:47:34 +00001339 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1340 {
1341 char
cristy151b66d2015-04-15 10:50:31 +00001342 create_date[MagickPathExtent],
1343 modify_date[MagickPathExtent],
1344 timestamp[MagickPathExtent],
1345 xmp_profile[MagickPathExtent];
cristy3ed852e2009-09-05 21:47:34 +00001346
cristy3ed852e2009-09-05 21:47:34 +00001347 /*
1348 Write XMP object.
1349 */
1350 xref[object++]=TellBlob(image);
cristy151b66d2015-04-15 10:50:31 +00001351 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g 0 obj\n",(double)
cristyf40010e2012-05-13 13:52:20 +00001352 object);
cristy3ed852e2009-09-05 21:47:34 +00001353 (void) WriteBlobString(image,buffer);
1354 (void) WriteBlobString(image,"<<\n");
1355 (void) WriteBlobString(image,"/Subtype /XML\n");
1356 *modify_date='\0';
cristyd15e6592011-10-15 00:13:06 +00001357 value=GetImageProperty(image,"date:modify",exception);
cristy3ed852e2009-09-05 21:47:34 +00001358 if (value != (const char *) NULL)
cristy151b66d2015-04-15 10:50:31 +00001359 (void) CopyMagickString(modify_date,value,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001360 *create_date='\0';
cristyd15e6592011-10-15 00:13:06 +00001361 value=GetImageProperty(image,"date:create",exception);
cristy3ed852e2009-09-05 21:47:34 +00001362 if (value != (const char *) NULL)
cristy151b66d2015-04-15 10:50:31 +00001363 (void) CopyMagickString(create_date,value,MagickPathExtent);
1364 (void) FormatMagickTime(time((time_t *) NULL),MagickPathExtent,timestamp);
1365 i=FormatLocaleString(xmp_profile,MagickPathExtent,XMPProfile,
cristy3ed852e2009-09-05 21:47:34 +00001366 XMPProfileMagick,modify_date,create_date,timestamp,
cristy1b70dc82012-07-01 23:15:05 +00001367 GetMagickVersion(&version),EscapeParenthesis(basename),
1368 GetMagickVersion(&version));
cristy151b66d2015-04-15 10:50:31 +00001369 (void) FormatLocaleString(buffer,MagickPathExtent,"/Length %.20g\n",(double)
cristyf40010e2012-05-13 13:52:20 +00001370 i);
cristy3ed852e2009-09-05 21:47:34 +00001371 (void) WriteBlobString(image,buffer);
1372 (void) WriteBlobString(image,"/Type /Metadata\n");
1373 (void) WriteBlobString(image,">>\nstream\n");
1374 (void) WriteBlobString(image,xmp_profile);
cristy1b70dc82012-07-01 23:15:05 +00001375 (void) WriteBlobString(image,"\nendstream\n");
cristy3ed852e2009-09-05 21:47:34 +00001376 (void) WriteBlobString(image,"endobj\n");
1377 }
1378 /*
1379 Write Pages object.
1380 */
1381 xref[object++]=TellBlob(image);
1382 pages_id=object;
cristy151b66d2015-04-15 10:50:31 +00001383 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001384 object);
cristy3ed852e2009-09-05 21:47:34 +00001385 (void) WriteBlobString(image,buffer);
1386 (void) WriteBlobString(image,"<<\n");
1387 (void) WriteBlobString(image,"/Type /Pages\n");
cristy151b66d2015-04-15 10:50:31 +00001388 (void) FormatLocaleString(buffer,MagickPathExtent,"/Kids [ %.20g 0 R ",(double)
cristyf40010e2012-05-13 13:52:20 +00001389 object+1);
cristy3ed852e2009-09-05 21:47:34 +00001390 (void) WriteBlobString(image,buffer);
cristybb503372010-05-27 20:51:26 +00001391 count=(ssize_t) (pages_id+ObjectsPerImage+1);
cristy3ed852e2009-09-05 21:47:34 +00001392 if (image_info->adjoin != MagickFalse)
1393 {
1394 Image
1395 *kid_image;
1396
1397 /*
1398 Predict page object id's.
1399 */
1400 kid_image=image;
1401 for ( ; GetNextImageInList(kid_image) != (Image *) NULL; count+=ObjectsPerImage)
1402 {
cristy151b66d2015-04-15 10:50:31 +00001403 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g 0 R ",(double)
cristye8c25f92010-06-03 00:53:06 +00001404 count);
cristy3ed852e2009-09-05 21:47:34 +00001405 (void) WriteBlobString(image,buffer);
1406 kid_image=GetNextImageInList(kid_image);
1407 }
1408 xref=(MagickOffsetType *) ResizeQuantumMemory(xref,(size_t) count+2048UL,
1409 sizeof(*xref));
1410 if (xref == (MagickOffsetType *) NULL)
1411 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1412 }
1413 (void) WriteBlobString(image,"]\n");
cristy151b66d2015-04-15 10:50:31 +00001414 (void) FormatLocaleString(buffer,MagickPathExtent,"/Count %.20g\n",(double)
cristyf40010e2012-05-13 13:52:20 +00001415 ((count-pages_id)/ObjectsPerImage));
cristy3ed852e2009-09-05 21:47:34 +00001416 (void) WriteBlobString(image,buffer);
1417 (void) WriteBlobString(image,">>\n");
1418 (void) WriteBlobString(image,"endobj\n");
1419 scene=0;
1420 do
1421 {
cristyaf8d3912014-02-21 14:50:33 +00001422 compression=image->compression;
cristy3ed852e2009-09-05 21:47:34 +00001423 if (image_info->compression != UndefinedCompression)
1424 compression=image_info->compression;
1425 switch (compression)
1426 {
1427 case FaxCompression:
1428 case Group4Compression:
1429 {
dirkf1d85482015-04-06 00:36:00 +00001430 if ((SetImageMonochrome(image,exception) == MagickFalse) ||
cristy17f11b02014-12-20 19:37:04 +00001431 (image->alpha_trait != UndefinedPixelTrait))
cristy3ed852e2009-09-05 21:47:34 +00001432 compression=RLECompression;
1433 break;
1434 }
1435#if !defined(MAGICKCORE_JPEG_DELEGATE)
1436 case JPEGCompression:
1437 {
1438 compression=RLECompression;
cristy1e178e72011-08-28 19:44:34 +00001439 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00001440 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JPEG)",
1441 image->filename);
1442 break;
1443 }
1444#endif
cristyf95d7c12014-04-08 07:35:08 +00001445#if !defined(MAGICKCORE_LIBOPENJP2_DELEGATE)
cristy3ed852e2009-09-05 21:47:34 +00001446 case JPEG2000Compression:
1447 {
1448 compression=RLECompression;
cristy1e178e72011-08-28 19:44:34 +00001449 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00001450 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (JP2)",
1451 image->filename);
1452 break;
1453 }
1454#endif
1455#if !defined(MAGICKCORE_ZLIB_DELEGATE)
1456 case ZipCompression:
1457 {
1458 compression=RLECompression;
cristy1e178e72011-08-28 19:44:34 +00001459 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00001460 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZLIB)",
1461 image->filename);
1462 break;
1463 }
1464#endif
1465 case LZWCompression:
1466 {
1467 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1468 compression=RLECompression; /* LZW compression is forbidden */
1469 break;
1470 }
1471 case NoCompression:
1472 {
1473 if (LocaleCompare(image_info->magick,"PDFA") == 0)
1474 compression=RLECompression; /* ASCII 85 compression is forbidden */
1475 break;
1476 }
1477 default:
1478 break;
1479 }
1480 if (compression == JPEG2000Compression)
cristyaf8d3912014-02-21 14:50:33 +00001481 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00001482 /*
1483 Scale relative to dots-per-inch.
1484 */
1485 delta.x=DefaultResolution;
1486 delta.y=DefaultResolution;
cristy2a11bef2011-10-28 18:33:11 +00001487 resolution.x=image->resolution.x;
1488 resolution.y=image->resolution.y;
cristy3ed852e2009-09-05 21:47:34 +00001489 if ((resolution.x == 0.0) || (resolution.y == 0.0))
1490 {
1491 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
1492 resolution.x=geometry_info.rho;
1493 resolution.y=geometry_info.sigma;
1494 if ((flags & SigmaValue) == 0)
1495 resolution.y=resolution.x;
1496 }
1497 if (image_info->density != (char *) NULL)
1498 {
1499 flags=ParseGeometry(image_info->density,&geometry_info);
1500 resolution.x=geometry_info.rho;
1501 resolution.y=geometry_info.sigma;
1502 if ((flags & SigmaValue) == 0)
1503 resolution.y=resolution.x;
1504 }
1505 if (image->units == PixelsPerCentimeterResolution)
1506 {
cristya97426c2011-02-04 01:41:27 +00001507 resolution.x=(double) ((size_t) (100.0*2.54*resolution.x+0.5)/100.0);
1508 resolution.y=(double) ((size_t) (100.0*2.54*resolution.y+0.5)/100.0);
cristy3ed852e2009-09-05 21:47:34 +00001509 }
1510 SetGeometry(image,&geometry);
cristy151b66d2015-04-15 10:50:31 +00001511 (void) FormatLocaleString(page_geometry,MagickPathExtent,"%.20gx%.20g",(double)
cristyf40010e2012-05-13 13:52:20 +00001512 image->columns,(double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001513 if (image_info->page != (char *) NULL)
cristy151b66d2015-04-15 10:50:31 +00001514 (void) CopyMagickString(page_geometry,image_info->page,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001515 else
1516 if ((image->page.width != 0) && (image->page.height != 0))
cristy151b66d2015-04-15 10:50:31 +00001517 (void) FormatLocaleString(page_geometry,MagickPathExtent,
cristyf40010e2012-05-13 13:52:20 +00001518 "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,(double)
1519 image->page.height,(double) image->page.x,(double) image->page.y);
cristy3ed852e2009-09-05 21:47:34 +00001520 else
1521 if ((image->gravity != UndefinedGravity) &&
1522 (LocaleCompare(image_info->magick,"PDF") == 0))
cristy151b66d2015-04-15 10:50:31 +00001523 (void) CopyMagickString(page_geometry,PSPageGeometry,MagickPathExtent);
1524 (void) ConcatenateMagickString(page_geometry,">",MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001525 (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
1526 &geometry.width,&geometry.height);
1527 scale.x=(double) (geometry.width*delta.x)/resolution.x;
cristybb503372010-05-27 20:51:26 +00001528 geometry.width=(size_t) floor(scale.x+0.5);
cristy3ed852e2009-09-05 21:47:34 +00001529 scale.y=(double) (geometry.height*delta.y)/resolution.y;
cristybb503372010-05-27 20:51:26 +00001530 geometry.height=(size_t) floor(scale.y+0.5);
cristy3ed852e2009-09-05 21:47:34 +00001531 (void) ParseAbsoluteGeometry(page_geometry,&media_info);
cristy1e178e72011-08-28 19:44:34 +00001532 (void) ParseGravityGeometry(image,page_geometry,&page_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00001533 if (image->gravity != UndefinedGravity)
1534 {
1535 geometry.x=(-page_info.x);
cristybb503372010-05-27 20:51:26 +00001536 geometry.y=(ssize_t) (media_info.height+page_info.y-image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001537 }
1538 pointsize=12.0;
1539 if (image_info->pointsize != 0.0)
1540 pointsize=image_info->pointsize;
1541 text_size=0;
cristyd15e6592011-10-15 00:13:06 +00001542 value=GetImageProperty(image,"label",exception);
cristy3ed852e2009-09-05 21:47:34 +00001543 if (value != (const char *) NULL)
cristybb503372010-05-27 20:51:26 +00001544 text_size=(size_t) (MultilineCensus(value)*pointsize+12);
cristyda16f162011-02-19 23:52:17 +00001545 (void) text_size;
cristy3ed852e2009-09-05 21:47:34 +00001546 /*
1547 Write Page object.
1548 */
1549 xref[object++]=TellBlob(image);
cristy151b66d2015-04-15 10:50:31 +00001550 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001551 object);
cristy3ed852e2009-09-05 21:47:34 +00001552 (void) WriteBlobString(image,buffer);
1553 (void) WriteBlobString(image,"<<\n");
1554 (void) WriteBlobString(image,"/Type /Page\n");
cristy151b66d2015-04-15 10:50:31 +00001555 (void) FormatLocaleString(buffer,MagickPathExtent,"/Parent %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001556 (double) pages_id);
cristy3ed852e2009-09-05 21:47:34 +00001557 (void) WriteBlobString(image,buffer);
1558 (void) WriteBlobString(image,"/Resources <<\n");
1559 labels=(char **) NULL;
cristyd15e6592011-10-15 00:13:06 +00001560 value=GetImageProperty(image,"label",exception);
cristy3ed852e2009-09-05 21:47:34 +00001561 if (value != (const char *) NULL)
1562 labels=StringToList(value);
1563 if (labels != (char **) NULL)
1564 {
cristy151b66d2015-04-15 10:50:31 +00001565 (void) FormatLocaleString(buffer,MagickPathExtent,
cristye8c25f92010-06-03 00:53:06 +00001566 "/Font << /F%.20g %.20g 0 R >>\n",(double) image->scene,(double)
1567 object+4);
cristy3ed852e2009-09-05 21:47:34 +00001568 (void) WriteBlobString(image,buffer);
1569 }
cristy151b66d2015-04-15 10:50:31 +00001570 (void) FormatLocaleString(buffer,MagickPathExtent,
cristye8c25f92010-06-03 00:53:06 +00001571 "/XObject << /Im%.20g %.20g 0 R >>\n",(double) image->scene,(double)
1572 object+5);
cristy3ed852e2009-09-05 21:47:34 +00001573 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00001574 (void) FormatLocaleString(buffer,MagickPathExtent,"/ProcSet %.20g 0 R >>\n",
cristye8c25f92010-06-03 00:53:06 +00001575 (double) object+3);
cristy3ed852e2009-09-05 21:47:34 +00001576 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00001577 (void) FormatLocaleString(buffer,MagickPathExtent,
cristye7f51092010-01-17 00:39:37 +00001578 "/MediaBox [0 0 %g %g]\n",72.0*media_info.width/resolution.x,
cristy8cd5b312010-01-07 01:10:24 +00001579 72.0*media_info.height/resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001580 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00001581 (void) FormatLocaleString(buffer,MagickPathExtent,
cristye7f51092010-01-17 00:39:37 +00001582 "/CropBox [0 0 %g %g]\n",72.0*media_info.width/resolution.x,
cristy8cd5b312010-01-07 01:10:24 +00001583 72.0*media_info.height/resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001584 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00001585 (void) FormatLocaleString(buffer,MagickPathExtent,"/Contents %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001586 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001587 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00001588 (void) FormatLocaleString(buffer,MagickPathExtent,"/Thumb %.20g 0 R\n",(double)
cristyf40010e2012-05-13 13:52:20 +00001589 object+8);
cristy3ed852e2009-09-05 21:47:34 +00001590 (void) WriteBlobString(image,buffer);
1591 (void) WriteBlobString(image,">>\n");
1592 (void) WriteBlobString(image,"endobj\n");
1593 /*
1594 Write Contents object.
1595 */
1596 xref[object++]=TellBlob(image);
cristy151b66d2015-04-15 10:50:31 +00001597 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001598 object);
cristy3ed852e2009-09-05 21:47:34 +00001599 (void) WriteBlobString(image,buffer);
1600 (void) WriteBlobString(image,"<<\n");
cristy151b66d2015-04-15 10:50:31 +00001601 (void) FormatLocaleString(buffer,MagickPathExtent,"/Length %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001602 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001603 (void) WriteBlobString(image,buffer);
1604 (void) WriteBlobString(image,">>\n");
1605 (void) WriteBlobString(image,"stream\n");
1606 offset=TellBlob(image);
1607 (void) WriteBlobString(image,"q\n");
1608 if (labels != (char **) NULL)
1609 for (i=0; labels[i] != (char *) NULL; i++)
1610 {
1611 (void) WriteBlobString(image,"BT\n");
cristy151b66d2015-04-15 10:50:31 +00001612 (void) FormatLocaleString(buffer,MagickPathExtent,"/F%.20g %g Tf\n",
cristye8c25f92010-06-03 00:53:06 +00001613 (double) image->scene,pointsize);
cristy3ed852e2009-09-05 21:47:34 +00001614 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00001615 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g %.20g Td\n",
cristye8c25f92010-06-03 00:53:06 +00001616 (double) geometry.x,(double) (geometry.y+geometry.height+i*pointsize+
1617 12));
cristy3ed852e2009-09-05 21:47:34 +00001618 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00001619 (void) FormatLocaleString(buffer,MagickPathExtent,"(%s) Tj\n",labels[i]);
cristy3ed852e2009-09-05 21:47:34 +00001620 (void) WriteBlobString(image,buffer);
1621 (void) WriteBlobString(image,"ET\n");
1622 labels[i]=DestroyString(labels[i]);
1623 }
cristy151b66d2015-04-15 10:50:31 +00001624 (void) FormatLocaleString(buffer,MagickPathExtent,"%g 0 0 %g %.20g %.20g cm\n",
cristye8c25f92010-06-03 00:53:06 +00001625 scale.x,scale.y,(double) geometry.x,(double) geometry.y);
cristy3ed852e2009-09-05 21:47:34 +00001626 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00001627 (void) FormatLocaleString(buffer,MagickPathExtent,"/Im%.20g Do\n",(double)
cristyf40010e2012-05-13 13:52:20 +00001628 image->scene);
cristy3ed852e2009-09-05 21:47:34 +00001629 (void) WriteBlobString(image,buffer);
1630 (void) WriteBlobString(image,"Q\n");
1631 offset=TellBlob(image)-offset;
cristy1b70dc82012-07-01 23:15:05 +00001632 (void) WriteBlobString(image,"\nendstream\n");
cristy3ed852e2009-09-05 21:47:34 +00001633 (void) WriteBlobString(image,"endobj\n");
1634 /*
1635 Write Length object.
1636 */
1637 xref[object++]=TellBlob(image);
cristy151b66d2015-04-15 10:50:31 +00001638 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001639 object);
cristy3ed852e2009-09-05 21:47:34 +00001640 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00001641 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g\n",(double) offset);
cristy3ed852e2009-09-05 21:47:34 +00001642 (void) WriteBlobString(image,buffer);
1643 (void) WriteBlobString(image,"endobj\n");
1644 /*
1645 Write Procset object.
1646 */
1647 xref[object++]=TellBlob(image);
cristy151b66d2015-04-15 10:50:31 +00001648 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g 0 obj\n",(double)
cristyf40010e2012-05-13 13:52:20 +00001649 object);
cristy3ed852e2009-09-05 21:47:34 +00001650 (void) WriteBlobString(image,buffer);
1651 if ((image->storage_class == DirectClass) || (image->colors > 256))
cristy151b66d2015-04-15 10:50:31 +00001652 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageC",MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001653 else
1654 if ((compression == FaxCompression) || (compression == Group4Compression))
cristy151b66d2015-04-15 10:50:31 +00001655 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageB",MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001656 else
cristy151b66d2015-04-15 10:50:31 +00001657 (void) CopyMagickString(buffer,"[ /PDF /Text /ImageI",MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001658 (void) WriteBlobString(image,buffer);
1659 (void) WriteBlobString(image," ]\n");
1660 (void) WriteBlobString(image,"endobj\n");
1661 /*
1662 Write Font object.
1663 */
1664 xref[object++]=TellBlob(image);
cristy151b66d2015-04-15 10:50:31 +00001665 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g 0 obj\n",(double)
cristyf40010e2012-05-13 13:52:20 +00001666 object);
cristy3ed852e2009-09-05 21:47:34 +00001667 (void) WriteBlobString(image,buffer);
1668 (void) WriteBlobString(image,"<<\n");
1669 if (labels != (char **) NULL)
1670 {
1671 (void) WriteBlobString(image,"/Type /Font\n");
1672 (void) WriteBlobString(image,"/Subtype /Type1\n");
cristy151b66d2015-04-15 10:50:31 +00001673 (void) FormatLocaleString(buffer,MagickPathExtent,"/Name /F%.20g\n",
cristye8c25f92010-06-03 00:53:06 +00001674 (double) image->scene);
cristy3ed852e2009-09-05 21:47:34 +00001675 (void) WriteBlobString(image,buffer);
1676 (void) WriteBlobString(image,"/BaseFont /Helvetica\n");
1677 (void) WriteBlobString(image,"/Encoding /MacRomanEncoding\n");
1678 labels=(char **) RelinquishMagickMemory(labels);
1679 }
1680 (void) WriteBlobString(image,">>\n");
1681 (void) WriteBlobString(image,"endobj\n");
1682 /*
1683 Write XObject object.
1684 */
1685 xref[object++]=TellBlob(image);
cristy151b66d2015-04-15 10:50:31 +00001686 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00001687 object);
cristy3ed852e2009-09-05 21:47:34 +00001688 (void) WriteBlobString(image,buffer);
1689 (void) WriteBlobString(image,"<<\n");
1690 (void) WriteBlobString(image,"/Type /XObject\n");
1691 (void) WriteBlobString(image,"/Subtype /Image\n");
cristy151b66d2015-04-15 10:50:31 +00001692 (void) FormatLocaleString(buffer,MagickPathExtent,"/Name /Im%.20g\n",(double)
cristyf40010e2012-05-13 13:52:20 +00001693 image->scene);
cristy3ed852e2009-09-05 21:47:34 +00001694 (void) WriteBlobString(image,buffer);
1695 switch (compression)
1696 {
1697 case NoCompression:
1698 {
cristy151b66d2015-04-15 10:50:31 +00001699 (void) FormatLocaleString(buffer,MagickPathExtent,CFormat,"ASCII85Decode");
cristy3ed852e2009-09-05 21:47:34 +00001700 break;
1701 }
1702 case JPEGCompression:
1703 {
cristy151b66d2015-04-15 10:50:31 +00001704 (void) FormatLocaleString(buffer,MagickPathExtent,CFormat,"DCTDecode");
cristy3ed852e2009-09-05 21:47:34 +00001705 if (image->colorspace != CMYKColorspace)
1706 break;
1707 (void) WriteBlobString(image,buffer);
1708 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
cristy151b66d2015-04-15 10:50:31 +00001709 MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001710 break;
1711 }
1712 case JPEG2000Compression:
1713 {
cristy151b66d2015-04-15 10:50:31 +00001714 (void) FormatLocaleString(buffer,MagickPathExtent,CFormat,"JPXDecode");
cristy3ed852e2009-09-05 21:47:34 +00001715 if (image->colorspace != CMYKColorspace)
1716 break;
1717 (void) WriteBlobString(image,buffer);
1718 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
cristy151b66d2015-04-15 10:50:31 +00001719 MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001720 break;
1721 }
1722 case LZWCompression:
1723 {
cristy151b66d2015-04-15 10:50:31 +00001724 (void) FormatLocaleString(buffer,MagickPathExtent,CFormat,"LZWDecode");
cristyfa7becb2009-09-13 02:44:40 +00001725 break;
cristy3ed852e2009-09-05 21:47:34 +00001726 }
1727 case ZipCompression:
1728 {
cristy151b66d2015-04-15 10:50:31 +00001729 (void) FormatLocaleString(buffer,MagickPathExtent,CFormat,"FlateDecode");
cristyfa7becb2009-09-13 02:44:40 +00001730 break;
cristy3ed852e2009-09-05 21:47:34 +00001731 }
1732 case FaxCompression:
1733 case Group4Compression:
1734 {
1735 (void) CopyMagickString(buffer,"/Filter [ /CCITTFaxDecode ]\n",
cristy151b66d2015-04-15 10:50:31 +00001736 MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001737 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00001738 (void) FormatLocaleString(buffer,MagickPathExtent,"/DecodeParms [ << "
cristye8c25f92010-06-03 00:53:06 +00001739 "/K %s /BlackIs1 false /Columns %.20g /Rows %.20g >> ]\n",CCITTParam,
1740 (double) image->columns,(double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001741 break;
1742 }
1743 default:
1744 {
cristy151b66d2015-04-15 10:50:31 +00001745 (void) FormatLocaleString(buffer,MagickPathExtent,CFormat,
cristy3ed852e2009-09-05 21:47:34 +00001746 "RunLengthDecode");
1747 break;
1748 }
1749 }
1750 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00001751 (void) FormatLocaleString(buffer,MagickPathExtent,"/Width %.20g\n",(double)
cristye8c25f92010-06-03 00:53:06 +00001752 image->columns);
cristy3ed852e2009-09-05 21:47:34 +00001753 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00001754 (void) FormatLocaleString(buffer,MagickPathExtent,"/Height %.20g\n",(double)
cristye8c25f92010-06-03 00:53:06 +00001755 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001756 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00001757 (void) FormatLocaleString(buffer,MagickPathExtent,"/ColorSpace %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001758 (double) object+2);
cristy3ed852e2009-09-05 21:47:34 +00001759 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00001760 (void) FormatLocaleString(buffer,MagickPathExtent,"/BitsPerComponent %d\n",
cristy3ed852e2009-09-05 21:47:34 +00001761 (compression == FaxCompression) || (compression == Group4Compression) ?
1762 1 : 8);
1763 (void) WriteBlobString(image,buffer);
cristy17f11b02014-12-20 19:37:04 +00001764 if (image->alpha_trait != UndefinedPixelTrait)
cristy3ed852e2009-09-05 21:47:34 +00001765 {
cristy151b66d2015-04-15 10:50:31 +00001766 (void) FormatLocaleString(buffer,MagickPathExtent,"/SMask %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001767 (double) object+7);
cristy3ed852e2009-09-05 21:47:34 +00001768 (void) WriteBlobString(image,buffer);
1769 }
cristy151b66d2015-04-15 10:50:31 +00001770 (void) FormatLocaleString(buffer,MagickPathExtent,"/Length %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00001771 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00001772 (void) WriteBlobString(image,buffer);
1773 (void) WriteBlobString(image,">>\n");
1774 (void) WriteBlobString(image,"stream\n");
1775 offset=TellBlob(image);
1776 number_pixels=(MagickSizeType) image->columns*image->rows;
1777 if ((4*number_pixels) != (MagickSizeType) ((size_t) (4*number_pixels)))
1778 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1779 if ((compression == FaxCompression) || (compression == Group4Compression) ||
1780 ((image_info->type != TrueColorType) &&
dirkf1d85482015-04-06 00:36:00 +00001781 (SetImageGray(image,exception) != MagickFalse)))
cristy3ed852e2009-09-05 21:47:34 +00001782 {
1783 switch (compression)
1784 {
1785 case FaxCompression:
1786 case Group4Compression:
1787 {
1788 if (LocaleCompare(CCITTParam,"0") == 0)
1789 {
cristy018f07f2011-09-04 21:15:19 +00001790 (void) HuffmanEncodeImage(image_info,image,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001791 break;
1792 }
cristy018f07f2011-09-04 21:15:19 +00001793 (void) Huffman2DEncodeImage(image_info,image,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001794 break;
1795 }
1796 case JPEGCompression:
1797 {
cristy1e178e72011-08-28 19:44:34 +00001798 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
cristy3ed852e2009-09-05 21:47:34 +00001799 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00001800 {
1801 (void) CloseBlob(image);
1802 return(MagickFalse);
1803 }
cristy3ed852e2009-09-05 21:47:34 +00001804 break;
1805 }
1806 case JPEG2000Compression:
1807 {
cristy1e178e72011-08-28 19:44:34 +00001808 status=InjectImageBlob(image_info,image,image,"jp2",exception);
cristy3ed852e2009-09-05 21:47:34 +00001809 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00001810 {
1811 (void) CloseBlob(image);
1812 return(MagickFalse);
1813 }
cristy3ed852e2009-09-05 21:47:34 +00001814 break;
1815 }
1816 case RLECompression:
1817 default:
1818 {
cristy0553bd52013-06-30 15:53:50 +00001819 MemoryInfo
1820 *pixel_info;
1821
cristy3ed852e2009-09-05 21:47:34 +00001822 /*
1823 Allocate pixel array.
1824 */
1825 length=(size_t) number_pixels;
cristy0553bd52013-06-30 15:53:50 +00001826 pixel_info=AcquireVirtualMemory(length,sizeof(*pixels));
1827 if (pixel_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001828 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
cristy0553bd52013-06-30 15:53:50 +00001829 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +00001830 /*
1831 Dump Runlength encoded pixels.
1832 */
1833 q=pixels;
cristybb503372010-05-27 20:51:26 +00001834 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001835 {
cristy1e178e72011-08-28 19:44:34 +00001836 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001837 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001838 break;
cristybb503372010-05-27 20:51:26 +00001839 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001840 {
cristyd0323222013-04-07 16:13:21 +00001841 *q++=ScaleQuantumToChar(ClampToQuantum(GetPixelLuma(image,p)));
cristyed231572011-07-14 02:18:59 +00001842 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001843 }
1844 if (image->previous == (Image *) NULL)
1845 {
cristyf5c61ba2010-12-17 00:58:04 +00001846 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1847 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001848 if (status == MagickFalse)
1849 break;
1850 }
1851 }
1852#if defined(MAGICKCORE_ZLIB_DELEGATE)
1853 if (compression == ZipCompression)
cristy018f07f2011-09-04 21:15:19 +00001854 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001855 else
1856#endif
1857 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00001858 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001859 else
cristy018f07f2011-09-04 21:15:19 +00001860 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy0553bd52013-06-30 15:53:50 +00001861 pixel_info=RelinquishVirtualMemory(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +00001862 if (status == MagickFalse)
1863 {
1864 (void) CloseBlob(image);
1865 return(MagickFalse);
1866 }
1867 break;
1868 }
1869 case NoCompression:
1870 {
1871 /*
1872 Dump uncompressed PseudoColor packets.
1873 */
1874 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00001875 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001876 {
cristy1e178e72011-08-28 19:44:34 +00001877 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001878 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001879 break;
cristybb503372010-05-27 20:51:26 +00001880 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001881 {
cristyd0323222013-04-07 16:13:21 +00001882 Ascii85Encode(image,ScaleQuantumToChar(ClampToQuantum(
1883 GetPixelLuma(image,p))));
cristyed231572011-07-14 02:18:59 +00001884 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001885 }
1886 if (image->previous == (Image *) NULL)
1887 {
cristyf5c61ba2010-12-17 00:58:04 +00001888 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1889 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001890 if (status == MagickFalse)
1891 break;
1892 }
1893 }
1894 Ascii85Flush(image);
1895 break;
1896 }
1897 }
1898 }
1899 else
1900 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
1901 (compression == JPEGCompression) ||
1902 (compression == JPEG2000Compression))
1903 switch (compression)
1904 {
1905 case JPEGCompression:
1906 {
cristy1e178e72011-08-28 19:44:34 +00001907 status=InjectImageBlob(image_info,image,image,"jpeg",exception);
cristy3ed852e2009-09-05 21:47:34 +00001908 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00001909 {
1910 (void) CloseBlob(image);
1911 return(MagickFalse);
1912 }
cristy3ed852e2009-09-05 21:47:34 +00001913 break;
1914 }
1915 case JPEG2000Compression:
1916 {
cristy1e178e72011-08-28 19:44:34 +00001917 status=InjectImageBlob(image_info,image,image,"jp2",exception);
cristy3ed852e2009-09-05 21:47:34 +00001918 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00001919 {
1920 (void) CloseBlob(image);
1921 return(MagickFalse);
1922 }
cristy3ed852e2009-09-05 21:47:34 +00001923 break;
1924 }
1925 case RLECompression:
1926 default:
1927 {
cristy0553bd52013-06-30 15:53:50 +00001928 MemoryInfo
1929 *pixel_info;
1930
cristy3ed852e2009-09-05 21:47:34 +00001931 /*
1932 Allocate pixel array.
1933 */
1934 length=(size_t) number_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001935 length*=image->colorspace == CMYKColorspace ? 4UL : 3UL;
cristy0553bd52013-06-30 15:53:50 +00001936 pixel_info=AcquireVirtualMemory(length,sizeof(*pixels));
1937 if (pixel_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001938 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
cristy0553bd52013-06-30 15:53:50 +00001939 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +00001940 /*
1941 Dump runoffset encoded pixels.
1942 */
1943 q=pixels;
cristybb503372010-05-27 20:51:26 +00001944 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001945 {
cristy1e178e72011-08-28 19:44:34 +00001946 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001947 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001948 break;
cristybb503372010-05-27 20:51:26 +00001949 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001950 {
cristy4c08aed2011-07-01 19:47:50 +00001951 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1952 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1953 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
cristy3ed852e2009-09-05 21:47:34 +00001954 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00001955 *q++=ScaleQuantumToChar(GetPixelBlack(image,p));
cristyed231572011-07-14 02:18:59 +00001956 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001957 }
1958 if (image->previous == (Image *) NULL)
1959 {
cristyf5c61ba2010-12-17 00:58:04 +00001960 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
1961 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001962 if (status == MagickFalse)
1963 break;
1964 }
1965 }
1966#if defined(MAGICKCORE_ZLIB_DELEGATE)
1967 if (compression == ZipCompression)
cristy018f07f2011-09-04 21:15:19 +00001968 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001969 else
1970#endif
1971 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00001972 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001973 else
cristy018f07f2011-09-04 21:15:19 +00001974 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy0553bd52013-06-30 15:53:50 +00001975 pixel_info=RelinquishVirtualMemory(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +00001976 if (status == MagickFalse)
1977 {
1978 (void) CloseBlob(image);
1979 return(MagickFalse);
1980 }
1981 break;
1982 }
1983 case NoCompression:
1984 {
1985 /*
1986 Dump uncompressed DirectColor packets.
1987 */
1988 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00001989 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001990 {
cristy1e178e72011-08-28 19:44:34 +00001991 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001992 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001993 break;
cristybb503372010-05-27 20:51:26 +00001994 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001995 {
cristy4c08aed2011-07-01 19:47:50 +00001996 Ascii85Encode(image,ScaleQuantumToChar(GetPixelRed(image,p)));
1997 Ascii85Encode(image,ScaleQuantumToChar(GetPixelGreen(image,p)));
1998 Ascii85Encode(image,ScaleQuantumToChar(GetPixelBlue(image,p)));
cristy3ed852e2009-09-05 21:47:34 +00001999 if (image->colorspace == CMYKColorspace)
cristyaff6d802011-04-26 01:46:31 +00002000 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +00002001 GetPixelBlack(image,p)));
cristyed231572011-07-14 02:18:59 +00002002 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002003 }
2004 if (image->previous == (Image *) NULL)
2005 {
cristyf5c61ba2010-12-17 00:58:04 +00002006 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
2007 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00002008 if (status == MagickFalse)
2009 break;
2010 }
2011 }
2012 Ascii85Flush(image);
2013 break;
2014 }
2015 }
2016 else
2017 {
2018 /*
2019 Dump number of colors and colormap.
2020 */
2021 switch (compression)
2022 {
2023 case RLECompression:
2024 default:
2025 {
cristy0553bd52013-06-30 15:53:50 +00002026 MemoryInfo
2027 *pixel_info;
2028
cristy3ed852e2009-09-05 21:47:34 +00002029 /*
2030 Allocate pixel array.
2031 */
2032 length=(size_t) number_pixels;
cristy0553bd52013-06-30 15:53:50 +00002033 pixel_info=AcquireVirtualMemory(length,sizeof(*pixels));
2034 if (pixel_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002035 ThrowWriterException(ResourceLimitError,
2036 "MemoryAllocationFailed");
cristy0553bd52013-06-30 15:53:50 +00002037 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +00002038 /*
2039 Dump Runlength encoded pixels.
2040 */
2041 q=pixels;
cristybb503372010-05-27 20:51:26 +00002042 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002043 {
cristy1e178e72011-08-28 19:44:34 +00002044 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00002045 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002046 break;
cristybb503372010-05-27 20:51:26 +00002047 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00002048 {
2049 *q++=(unsigned char) GetPixelIndex(image,p);
cristyed231572011-07-14 02:18:59 +00002050 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00002051 }
cristy3ed852e2009-09-05 21:47:34 +00002052 if (image->previous == (Image *) NULL)
2053 {
cristyf5c61ba2010-12-17 00:58:04 +00002054 status=SetImageProgress(image,SaveImageTag,
2055 (MagickOffsetType) y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00002056 if (status == MagickFalse)
2057 break;
2058 }
2059 }
2060#if defined(MAGICKCORE_ZLIB_DELEGATE)
2061 if (compression == ZipCompression)
cristy018f07f2011-09-04 21:15:19 +00002062 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002063 else
2064#endif
2065 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00002066 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002067 else
cristy018f07f2011-09-04 21:15:19 +00002068 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy0553bd52013-06-30 15:53:50 +00002069 pixel_info=RelinquishVirtualMemory(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +00002070 if (status == MagickFalse)
2071 {
2072 (void) CloseBlob(image);
2073 return(MagickFalse);
2074 }
2075 break;
2076 }
2077 case NoCompression:
2078 {
2079 /*
2080 Dump uncompressed PseudoColor packets.
2081 */
2082 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00002083 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002084 {
cristy1e178e72011-08-28 19:44:34 +00002085 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00002086 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002087 break;
cristybb503372010-05-27 20:51:26 +00002088 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00002089 {
2090 Ascii85Encode(image,(unsigned char) GetPixelIndex(image,p));
cristyed231572011-07-14 02:18:59 +00002091 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00002092 }
cristy3ed852e2009-09-05 21:47:34 +00002093 if (image->previous == (Image *) NULL)
2094 {
cristyf5c61ba2010-12-17 00:58:04 +00002095 status=SetImageProgress(image,SaveImageTag,
2096 (MagickOffsetType) y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00002097 if (status == MagickFalse)
2098 break;
2099 }
2100 }
2101 Ascii85Flush(image);
2102 break;
2103 }
2104 }
2105 }
2106 offset=TellBlob(image)-offset;
2107 (void) WriteBlobString(image,"\nendstream\n");
2108 (void) WriteBlobString(image,"endobj\n");
2109 /*
2110 Write Length object.
2111 */
2112 xref[object++]=TellBlob(image);
cristy151b66d2015-04-15 10:50:31 +00002113 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002114 object);
cristy3ed852e2009-09-05 21:47:34 +00002115 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00002116 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g\n",(double) offset);
cristy3ed852e2009-09-05 21:47:34 +00002117 (void) WriteBlobString(image,buffer);
2118 (void) WriteBlobString(image,"endobj\n");
2119 /*
2120 Write Colorspace object.
2121 */
2122 xref[object++]=TellBlob(image);
cristy151b66d2015-04-15 10:50:31 +00002123 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002124 object);
cristy3ed852e2009-09-05 21:47:34 +00002125 (void) WriteBlobString(image,buffer);
2126 if (image->colorspace == CMYKColorspace)
cristy151b66d2015-04-15 10:50:31 +00002127 (void) CopyMagickString(buffer,"/DeviceCMYK\n",MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00002128 else
2129 if ((compression == FaxCompression) ||
2130 (compression == Group4Compression) ||
2131 ((image_info->type != TrueColorType) &&
dirkf1d85482015-04-06 00:36:00 +00002132 (SetImageGray(image,exception) != MagickFalse)))
cristy151b66d2015-04-15 10:50:31 +00002133 (void) CopyMagickString(buffer,"/DeviceGray\n",MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00002134 else
2135 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
2136 (compression == JPEGCompression) ||
2137 (compression == JPEG2000Compression))
cristy151b66d2015-04-15 10:50:31 +00002138 (void) CopyMagickString(buffer,"/DeviceRGB\n",MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00002139 else
cristy151b66d2015-04-15 10:50:31 +00002140 (void) FormatLocaleString(buffer,MagickPathExtent,
cristye8c25f92010-06-03 00:53:06 +00002141 "[ /Indexed /DeviceRGB %.20g %.20g 0 R ]\n",(double) image->colors-
2142 1,(double) object+3);
cristy3ed852e2009-09-05 21:47:34 +00002143 (void) WriteBlobString(image,buffer);
2144 (void) WriteBlobString(image,"endobj\n");
2145 /*
2146 Write Thumb object.
2147 */
2148 SetGeometry(image,&geometry);
2149 (void) ParseMetaGeometry("106x106+0+0>",&geometry.x,&geometry.y,
2150 &geometry.width,&geometry.height);
cristy1e178e72011-08-28 19:44:34 +00002151 tile_image=ThumbnailImage(image,geometry.width,geometry.height,exception);
cristy3ed852e2009-09-05 21:47:34 +00002152 if (tile_image == (Image *) NULL)
cristy1e178e72011-08-28 19:44:34 +00002153 return(MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +00002154 xref[object++]=TellBlob(image);
cristy151b66d2015-04-15 10:50:31 +00002155 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002156 object);
cristy3ed852e2009-09-05 21:47:34 +00002157 (void) WriteBlobString(image,buffer);
2158 (void) WriteBlobString(image,"<<\n");
2159 switch (compression)
2160 {
2161 case NoCompression:
2162 {
cristy151b66d2015-04-15 10:50:31 +00002163 (void) FormatLocaleString(buffer,MagickPathExtent,CFormat,"ASCII85Decode");
cristy3ed852e2009-09-05 21:47:34 +00002164 break;
2165 }
2166 case JPEGCompression:
2167 {
cristy151b66d2015-04-15 10:50:31 +00002168 (void) FormatLocaleString(buffer,MagickPathExtent,CFormat,"DCTDecode");
cristy3ed852e2009-09-05 21:47:34 +00002169 if (image->colorspace != CMYKColorspace)
2170 break;
2171 (void) WriteBlobString(image,buffer);
2172 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
cristy151b66d2015-04-15 10:50:31 +00002173 MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00002174 break;
2175 }
2176 case JPEG2000Compression:
2177 {
cristy151b66d2015-04-15 10:50:31 +00002178 (void) FormatLocaleString(buffer,MagickPathExtent,CFormat,"JPXDecode");
cristy3ed852e2009-09-05 21:47:34 +00002179 if (image->colorspace != CMYKColorspace)
2180 break;
2181 (void) WriteBlobString(image,buffer);
2182 (void) CopyMagickString(buffer,"/Decode [1 0 1 0 1 0 1 0]\n",
cristy151b66d2015-04-15 10:50:31 +00002183 MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00002184 break;
2185 }
2186 case LZWCompression:
2187 {
cristy151b66d2015-04-15 10:50:31 +00002188 (void) FormatLocaleString(buffer,MagickPathExtent,CFormat,"LZWDecode");
cristy3ed852e2009-09-05 21:47:34 +00002189 break;
2190 }
2191 case ZipCompression:
2192 {
cristy151b66d2015-04-15 10:50:31 +00002193 (void) FormatLocaleString(buffer,MagickPathExtent,CFormat,"FlateDecode");
cristy3ed852e2009-09-05 21:47:34 +00002194 break;
2195 }
2196 case FaxCompression:
2197 case Group4Compression:
2198 {
2199 (void) CopyMagickString(buffer,"/Filter [ /CCITTFaxDecode ]\n",
cristy151b66d2015-04-15 10:50:31 +00002200 MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00002201 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00002202 (void) FormatLocaleString(buffer,MagickPathExtent,"/DecodeParms [ << "
cristye8c25f92010-06-03 00:53:06 +00002203 "/K %s /BlackIs1 false /Columns %.20g /Rows %.20g >> ]\n",CCITTParam,
2204 (double) tile_image->columns,(double) tile_image->rows);
cristy3ed852e2009-09-05 21:47:34 +00002205 break;
2206 }
2207 default:
2208 {
cristy151b66d2015-04-15 10:50:31 +00002209 (void) FormatLocaleString(buffer,MagickPathExtent,CFormat,
cristy3ed852e2009-09-05 21:47:34 +00002210 "RunLengthDecode");
2211 break;
2212 }
2213 }
2214 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00002215 (void) FormatLocaleString(buffer,MagickPathExtent,"/Width %.20g\n",(double)
cristye8c25f92010-06-03 00:53:06 +00002216 tile_image->columns);
cristy3ed852e2009-09-05 21:47:34 +00002217 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00002218 (void) FormatLocaleString(buffer,MagickPathExtent,"/Height %.20g\n",(double)
cristye8c25f92010-06-03 00:53:06 +00002219 tile_image->rows);
cristy3ed852e2009-09-05 21:47:34 +00002220 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00002221 (void) FormatLocaleString(buffer,MagickPathExtent,"/ColorSpace %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00002222 (double) object-1);
cristy3ed852e2009-09-05 21:47:34 +00002223 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00002224 (void) FormatLocaleString(buffer,MagickPathExtent,"/BitsPerComponent %d\n",
cristy3ed852e2009-09-05 21:47:34 +00002225 (compression == FaxCompression) || (compression == Group4Compression) ?
2226 1 : 8);
2227 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00002228 (void) FormatLocaleString(buffer,MagickPathExtent,"/Length %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00002229 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00002230 (void) WriteBlobString(image,buffer);
2231 (void) WriteBlobString(image,">>\n");
2232 (void) WriteBlobString(image,"stream\n");
2233 offset=TellBlob(image);
2234 number_pixels=(MagickSizeType) tile_image->columns*tile_image->rows;
2235 if ((compression == FaxCompression) ||
2236 (compression == Group4Compression) ||
2237 ((image_info->type != TrueColorType) &&
dirkf1d85482015-04-06 00:36:00 +00002238 (SetImageGray(tile_image,exception) != MagickFalse)))
cristy3ed852e2009-09-05 21:47:34 +00002239 {
2240 switch (compression)
2241 {
2242 case FaxCompression:
2243 case Group4Compression:
2244 {
2245 if (LocaleCompare(CCITTParam,"0") == 0)
2246 {
cristy018f07f2011-09-04 21:15:19 +00002247 (void) HuffmanEncodeImage(image_info,image,tile_image,
2248 exception);
cristy3ed852e2009-09-05 21:47:34 +00002249 break;
2250 }
cristy018f07f2011-09-04 21:15:19 +00002251 (void) Huffman2DEncodeImage(image_info,image,tile_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00002252 break;
2253 }
2254 case JPEGCompression:
2255 {
2256 status=InjectImageBlob(image_info,image,tile_image,"jpeg",
cristy1e178e72011-08-28 19:44:34 +00002257 exception);
cristy3ed852e2009-09-05 21:47:34 +00002258 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00002259 {
2260 (void) CloseBlob(image);
2261 return(MagickFalse);
2262 }
cristy3ed852e2009-09-05 21:47:34 +00002263 break;
2264 }
2265 case JPEG2000Compression:
2266 {
cristy1e178e72011-08-28 19:44:34 +00002267 status=InjectImageBlob(image_info,image,tile_image,"jp2",exception);
cristy3ed852e2009-09-05 21:47:34 +00002268 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00002269 {
2270 (void) CloseBlob(image);
2271 return(MagickFalse);
2272 }
cristy3ed852e2009-09-05 21:47:34 +00002273 break;
2274 }
2275 case RLECompression:
2276 default:
2277 {
cristy0553bd52013-06-30 15:53:50 +00002278 MemoryInfo
2279 *pixel_info;
2280
cristy3ed852e2009-09-05 21:47:34 +00002281 /*
2282 Allocate pixel array.
2283 */
2284 length=(size_t) number_pixels;
cristy0553bd52013-06-30 15:53:50 +00002285 pixel_info=AcquireVirtualMemory(length,sizeof(*pixels));
2286 if (pixel_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002287 {
2288 tile_image=DestroyImage(tile_image);
2289 ThrowWriterException(ResourceLimitError,
2290 "MemoryAllocationFailed");
2291 }
cristy0553bd52013-06-30 15:53:50 +00002292 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +00002293 /*
cristy0553bd52013-06-30 15:53:50 +00002294 Dump runlength encoded pixels.
cristy3ed852e2009-09-05 21:47:34 +00002295 */
2296 q=pixels;
cristybb503372010-05-27 20:51:26 +00002297 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002298 {
2299 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
cristy1e178e72011-08-28 19:44:34 +00002300 exception);
cristy4c08aed2011-07-01 19:47:50 +00002301 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002302 break;
cristybb503372010-05-27 20:51:26 +00002303 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002304 {
cristyd0323222013-04-07 16:13:21 +00002305 *q++=ScaleQuantumToChar(ClampToQuantum(GetPixelLuma(
2306 tile_image,p)));
cristyed231572011-07-14 02:18:59 +00002307 p+=GetPixelChannels(tile_image);
cristy3ed852e2009-09-05 21:47:34 +00002308 }
2309 }
2310#if defined(MAGICKCORE_ZLIB_DELEGATE)
2311 if (compression == ZipCompression)
cristy018f07f2011-09-04 21:15:19 +00002312 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002313 else
2314#endif
2315 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00002316 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002317 else
cristy018f07f2011-09-04 21:15:19 +00002318 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy0553bd52013-06-30 15:53:50 +00002319 pixel_info=RelinquishVirtualMemory(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +00002320 if (status == MagickFalse)
2321 {
2322 (void) CloseBlob(image);
2323 return(MagickFalse);
2324 }
2325 break;
2326 }
2327 case NoCompression:
2328 {
2329 /*
2330 Dump uncompressed PseudoColor packets.
2331 */
2332 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00002333 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002334 {
2335 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
cristy1e178e72011-08-28 19:44:34 +00002336 exception);
cristy4c08aed2011-07-01 19:47:50 +00002337 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002338 break;
cristybb503372010-05-27 20:51:26 +00002339 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002340 {
cristy62773482015-06-27 15:43:59 +00002341 Ascii85Encode(tile_image,ScaleQuantumToChar(ClampToQuantum(
cristyd0323222013-04-07 16:13:21 +00002342 GetPixelLuma(tile_image,p))));
cristyed231572011-07-14 02:18:59 +00002343 p+=GetPixelChannels(tile_image);
cristy3ed852e2009-09-05 21:47:34 +00002344 }
2345 }
2346 Ascii85Flush(image);
2347 break;
2348 }
2349 }
2350 }
2351 else
2352 if ((tile_image->storage_class == DirectClass) ||
2353 (tile_image->colors > 256) || (compression == JPEGCompression) ||
2354 (compression == JPEG2000Compression))
2355 switch (compression)
2356 {
2357 case JPEGCompression:
2358 {
2359 status=InjectImageBlob(image_info,image,tile_image,"jpeg",
cristy1e178e72011-08-28 19:44:34 +00002360 exception);
cristy3ed852e2009-09-05 21:47:34 +00002361 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00002362 {
2363 (void) CloseBlob(image);
2364 return(MagickFalse);
2365 }
cristy3ed852e2009-09-05 21:47:34 +00002366 break;
2367 }
2368 case JPEG2000Compression:
2369 {
cristy1e178e72011-08-28 19:44:34 +00002370 status=InjectImageBlob(image_info,image,tile_image,"jp2",exception);
cristy3ed852e2009-09-05 21:47:34 +00002371 if (status == MagickFalse)
cristy1e178e72011-08-28 19:44:34 +00002372 {
2373 (void) CloseBlob(image);
2374 return(MagickFalse);
2375 }
cristy3ed852e2009-09-05 21:47:34 +00002376 break;
2377 }
2378 case RLECompression:
2379 default:
2380 {
cristy0553bd52013-06-30 15:53:50 +00002381 MemoryInfo
2382 *pixel_info;
2383
cristy3ed852e2009-09-05 21:47:34 +00002384 /*
2385 Allocate pixel array.
2386 */
2387 length=(size_t) number_pixels;
cristy3ed852e2009-09-05 21:47:34 +00002388 length*=tile_image->colorspace == CMYKColorspace ? 4UL : 3UL;
cristy0553bd52013-06-30 15:53:50 +00002389 pixel_info=AcquireVirtualMemory(length,4*sizeof(*pixels));
2390 if (pixel_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002391 {
2392 tile_image=DestroyImage(tile_image);
2393 ThrowWriterException(ResourceLimitError,
2394 "MemoryAllocationFailed");
2395 }
cristy0553bd52013-06-30 15:53:50 +00002396 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +00002397 /*
cristy0553bd52013-06-30 15:53:50 +00002398 Dump runlength encoded pixels.
cristy3ed852e2009-09-05 21:47:34 +00002399 */
2400 q=pixels;
cristybb503372010-05-27 20:51:26 +00002401 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002402 {
2403 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
cristy1e178e72011-08-28 19:44:34 +00002404 exception);
cristy4c08aed2011-07-01 19:47:50 +00002405 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002406 break;
cristybb503372010-05-27 20:51:26 +00002407 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002408 {
cristy4c08aed2011-07-01 19:47:50 +00002409 *q++=ScaleQuantumToChar(GetPixelRed(tile_image,p));
2410 *q++=ScaleQuantumToChar(GetPixelGreen(tile_image,p));
2411 *q++=ScaleQuantumToChar(GetPixelBlue(tile_image,p));
cristy4d0ca342014-05-01 00:42:09 +00002412 if (tile_image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00002413 *q++=ScaleQuantumToChar(GetPixelBlack(tile_image,p));
cristyed231572011-07-14 02:18:59 +00002414 p+=GetPixelChannels(tile_image);
cristy3ed852e2009-09-05 21:47:34 +00002415 }
2416 }
2417#if defined(MAGICKCORE_ZLIB_DELEGATE)
2418 if (compression == ZipCompression)
cristy018f07f2011-09-04 21:15:19 +00002419 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002420 else
2421#endif
2422 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00002423 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002424 else
cristy018f07f2011-09-04 21:15:19 +00002425 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy0553bd52013-06-30 15:53:50 +00002426 pixel_info=RelinquishVirtualMemory(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +00002427 if (status == MagickFalse)
2428 {
2429 (void) CloseBlob(image);
2430 return(MagickFalse);
2431 }
2432 break;
2433 }
2434 case NoCompression:
2435 {
2436 /*
2437 Dump uncompressed DirectColor packets.
2438 */
2439 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00002440 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002441 {
2442 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
cristy1e178e72011-08-28 19:44:34 +00002443 exception);
cristy4c08aed2011-07-01 19:47:50 +00002444 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002445 break;
cristybb503372010-05-27 20:51:26 +00002446 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002447 {
cristyf5c61ba2010-12-17 00:58:04 +00002448 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +00002449 GetPixelRed(tile_image,p)));
cristyf5c61ba2010-12-17 00:58:04 +00002450 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +00002451 GetPixelGreen(tile_image,p)));
cristyf5c61ba2010-12-17 00:58:04 +00002452 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +00002453 GetPixelBlue(tile_image,p)));
cristy3ed852e2009-09-05 21:47:34 +00002454 if (image->colorspace == CMYKColorspace)
cristyaff6d802011-04-26 01:46:31 +00002455 Ascii85Encode(image,ScaleQuantumToChar(
cristy4c08aed2011-07-01 19:47:50 +00002456 GetPixelBlack(tile_image,p)));
cristyed231572011-07-14 02:18:59 +00002457 p+=GetPixelChannels(tile_image);
cristy3ed852e2009-09-05 21:47:34 +00002458 }
2459 }
2460 Ascii85Flush(image);
2461 break;
2462 }
2463 }
2464 else
2465 {
2466 /*
2467 Dump number of colors and colormap.
2468 */
2469 switch (compression)
2470 {
2471 case RLECompression:
2472 default:
2473 {
cristy0553bd52013-06-30 15:53:50 +00002474 MemoryInfo
2475 *pixel_info;
2476
cristy3ed852e2009-09-05 21:47:34 +00002477 /*
2478 Allocate pixel array.
2479 */
2480 length=(size_t) number_pixels;
cristy0553bd52013-06-30 15:53:50 +00002481 pixel_info=AcquireVirtualMemory(length,sizeof(*pixels));
2482 if (pixel_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002483 {
2484 tile_image=DestroyImage(tile_image);
2485 ThrowWriterException(ResourceLimitError,
2486 "MemoryAllocationFailed");
2487 }
cristy0553bd52013-06-30 15:53:50 +00002488 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +00002489 /*
cristy0553bd52013-06-30 15:53:50 +00002490 Dump runlength encoded pixels.
cristy3ed852e2009-09-05 21:47:34 +00002491 */
2492 q=pixels;
cristybb503372010-05-27 20:51:26 +00002493 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002494 {
2495 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
cristy1e178e72011-08-28 19:44:34 +00002496 exception);
cristy4c08aed2011-07-01 19:47:50 +00002497 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002498 break;
cristybb503372010-05-27 20:51:26 +00002499 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00002500 {
2501 *q++=(unsigned char) GetPixelIndex(tile_image,p);
cristy6307d372012-06-17 20:07:34 +00002502 p+=GetPixelChannels(tile_image);
cristy4c08aed2011-07-01 19:47:50 +00002503 }
cristy3ed852e2009-09-05 21:47:34 +00002504 }
2505#if defined(MAGICKCORE_ZLIB_DELEGATE)
2506 if (compression == ZipCompression)
cristy018f07f2011-09-04 21:15:19 +00002507 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002508 else
2509#endif
2510 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00002511 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002512 else
cristy018f07f2011-09-04 21:15:19 +00002513 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy0553bd52013-06-30 15:53:50 +00002514 pixel_info=RelinquishVirtualMemory(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +00002515 if (status == MagickFalse)
2516 {
2517 (void) CloseBlob(image);
2518 return(MagickFalse);
2519 }
2520 break;
2521 }
2522 case NoCompression:
2523 {
2524 /*
2525 Dump uncompressed PseudoColor packets.
2526 */
2527 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00002528 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002529 {
2530 p=GetVirtualPixels(tile_image,0,y,tile_image->columns,1,
cristy1e178e72011-08-28 19:44:34 +00002531 exception);
cristy4c08aed2011-07-01 19:47:50 +00002532 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002533 break;
cristybb503372010-05-27 20:51:26 +00002534 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00002535 {
cristyaff6d802011-04-26 01:46:31 +00002536 Ascii85Encode(image,(unsigned char)
cristy4c08aed2011-07-01 19:47:50 +00002537 GetPixelIndex(tile_image,p));
cristyed231572011-07-14 02:18:59 +00002538 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00002539 }
cristy3ed852e2009-09-05 21:47:34 +00002540 }
2541 Ascii85Flush(image);
2542 break;
2543 }
2544 }
2545 }
2546 tile_image=DestroyImage(tile_image);
2547 offset=TellBlob(image)-offset;
2548 (void) WriteBlobString(image,"\nendstream\n");
2549 (void) WriteBlobString(image,"endobj\n");
2550 /*
2551 Write Length object.
2552 */
2553 xref[object++]=TellBlob(image);
cristy151b66d2015-04-15 10:50:31 +00002554 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002555 object);
cristy3ed852e2009-09-05 21:47:34 +00002556 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00002557 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g\n",(double) offset);
cristy3ed852e2009-09-05 21:47:34 +00002558 (void) WriteBlobString(image,buffer);
2559 (void) WriteBlobString(image,"endobj\n");
2560 xref[object++]=TellBlob(image);
cristy151b66d2015-04-15 10:50:31 +00002561 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002562 object);
cristy3ed852e2009-09-05 21:47:34 +00002563 (void) WriteBlobString(image,buffer);
cristy59427d32013-01-23 19:19:11 +00002564 (void) WriteBlobString(image,"<<\n");
cristy3152b6b2013-01-23 19:21:48 +00002565 if ((image->storage_class == DirectClass) || (image->colors > 256) ||
2566 (compression == FaxCompression) || (compression == Group4Compression))
2567 (void) WriteBlobString(image,">>\n");
2568 else
cristy3ed852e2009-09-05 21:47:34 +00002569 {
2570 /*
2571 Write Colormap object.
2572 */
cristy3ed852e2009-09-05 21:47:34 +00002573 if (compression == NoCompression)
2574 (void) WriteBlobString(image,"/Filter [ /ASCII85Decode ]\n");
cristy151b66d2015-04-15 10:50:31 +00002575 (void) FormatLocaleString(buffer,MagickPathExtent,"/Length %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00002576 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00002577 (void) WriteBlobString(image,buffer);
2578 (void) WriteBlobString(image,">>\n");
2579 (void) WriteBlobString(image,"stream\n");
2580 offset=TellBlob(image);
2581 if (compression == NoCompression)
2582 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00002583 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00002584 {
2585 if (compression == NoCompression)
2586 {
cristyd0323222013-04-07 16:13:21 +00002587 Ascii85Encode(image,ScaleQuantumToChar(ClampToQuantum(
2588 image->colormap[i].red)));
2589 Ascii85Encode(image,ScaleQuantumToChar(ClampToQuantum(
2590 image->colormap[i].green)));
2591 Ascii85Encode(image,ScaleQuantumToChar(ClampToQuantum(
2592 image->colormap[i].blue)));
cristy3ed852e2009-09-05 21:47:34 +00002593 continue;
2594 }
cristyd0323222013-04-07 16:13:21 +00002595 (void) WriteBlobByte(image,ScaleQuantumToChar(
2596 ClampToQuantum(image->colormap[i].red)));
2597 (void) WriteBlobByte(image,ScaleQuantumToChar(
2598 ClampToQuantum(image->colormap[i].green)));
2599 (void) WriteBlobByte(image,ScaleQuantumToChar(
2600 ClampToQuantum(image->colormap[i].blue)));
cristy3ed852e2009-09-05 21:47:34 +00002601 }
2602 if (compression == NoCompression)
2603 Ascii85Flush(image);
2604 offset=TellBlob(image)-offset;
2605 (void) WriteBlobString(image,"\nendstream\n");
2606 }
2607 (void) WriteBlobString(image,"endobj\n");
2608 /*
2609 Write Length object.
2610 */
2611 xref[object++]=TellBlob(image);
cristy151b66d2015-04-15 10:50:31 +00002612 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002613 object);
cristy3ed852e2009-09-05 21:47:34 +00002614 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00002615 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002616 offset);
cristy3ed852e2009-09-05 21:47:34 +00002617 (void) WriteBlobString(image,buffer);
2618 (void) WriteBlobString(image,"endobj\n");
2619 /*
2620 Write softmask object.
2621 */
2622 xref[object++]=TellBlob(image);
cristy151b66d2015-04-15 10:50:31 +00002623 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002624 object);
cristy3ed852e2009-09-05 21:47:34 +00002625 (void) WriteBlobString(image,buffer);
2626 (void) WriteBlobString(image,"<<\n");
cristy17f11b02014-12-20 19:37:04 +00002627 if (image->alpha_trait == UndefinedPixelTrait)
cristy3ed852e2009-09-05 21:47:34 +00002628 (void) WriteBlobString(image,">>\n");
2629 else
2630 {
2631 (void) WriteBlobString(image,"/Type /XObject\n");
2632 (void) WriteBlobString(image,"/Subtype /Image\n");
cristy151b66d2015-04-15 10:50:31 +00002633 (void) FormatLocaleString(buffer,MagickPathExtent,"/Name /Ma%.20g\n",
cristye8c25f92010-06-03 00:53:06 +00002634 (double) image->scene);
cristy3ed852e2009-09-05 21:47:34 +00002635 (void) WriteBlobString(image,buffer);
2636 switch (compression)
2637 {
2638 case NoCompression:
2639 {
cristy151b66d2015-04-15 10:50:31 +00002640 (void) FormatLocaleString(buffer,MagickPathExtent,CFormat,
cristy3ed852e2009-09-05 21:47:34 +00002641 "ASCII85Decode");
2642 break;
2643 }
2644 case LZWCompression:
2645 {
cristy151b66d2015-04-15 10:50:31 +00002646 (void) FormatLocaleString(buffer,MagickPathExtent,CFormat,"LZWDecode");
cristy3ed852e2009-09-05 21:47:34 +00002647 break;
2648 }
2649 case ZipCompression:
2650 {
cristy151b66d2015-04-15 10:50:31 +00002651 (void) FormatLocaleString(buffer,MagickPathExtent,CFormat,
cristy3ed852e2009-09-05 21:47:34 +00002652 "FlateDecode");
2653 break;
2654 }
2655 default:
2656 {
cristy151b66d2015-04-15 10:50:31 +00002657 (void) FormatLocaleString(buffer,MagickPathExtent,CFormat,
cristy3ed852e2009-09-05 21:47:34 +00002658 "RunLengthDecode");
2659 break;
2660 }
2661 }
2662 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00002663 (void) FormatLocaleString(buffer,MagickPathExtent,"/Width %.20g\n",(double)
cristyf40010e2012-05-13 13:52:20 +00002664 image->columns);
cristy3ed852e2009-09-05 21:47:34 +00002665 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00002666 (void) FormatLocaleString(buffer,MagickPathExtent,"/Height %.20g\n",
cristye8c25f92010-06-03 00:53:06 +00002667 (double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +00002668 (void) WriteBlobString(image,buffer);
2669 (void) WriteBlobString(image,"/ColorSpace /DeviceGray\n");
cristy151b66d2015-04-15 10:50:31 +00002670 (void) FormatLocaleString(buffer,MagickPathExtent,"/BitsPerComponent %d\n",
cristy3ed852e2009-09-05 21:47:34 +00002671 (compression == FaxCompression) || (compression == Group4Compression)
2672 ? 1 : 8);
2673 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00002674 (void) FormatLocaleString(buffer,MagickPathExtent,"/Length %.20g 0 R\n",
cristye8c25f92010-06-03 00:53:06 +00002675 (double) object+1);
cristy3ed852e2009-09-05 21:47:34 +00002676 (void) WriteBlobString(image,buffer);
2677 (void) WriteBlobString(image,">>\n");
2678 (void) WriteBlobString(image,"stream\n");
2679 offset=TellBlob(image);
2680 number_pixels=(MagickSizeType) image->columns*image->rows;
2681 switch (compression)
2682 {
2683 case RLECompression:
2684 default:
2685 {
cristy0553bd52013-06-30 15:53:50 +00002686 MemoryInfo
2687 *pixel_info;
2688
cristy3ed852e2009-09-05 21:47:34 +00002689 /*
2690 Allocate pixel array.
2691 */
2692 length=(size_t) number_pixels;
cristy0553bd52013-06-30 15:53:50 +00002693 pixel_info=AcquireVirtualMemory(length,4*sizeof(*pixels));
2694 if (pixel_info == (MemoryInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002695 {
2696 image=DestroyImage(image);
2697 ThrowWriterException(ResourceLimitError,
2698 "MemoryAllocationFailed");
2699 }
cristy0553bd52013-06-30 15:53:50 +00002700 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +00002701 /*
2702 Dump Runlength encoded pixels.
2703 */
2704 q=pixels;
cristybb503372010-05-27 20:51:26 +00002705 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002706 {
cristy1e178e72011-08-28 19:44:34 +00002707 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00002708 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002709 break;
cristybb503372010-05-27 20:51:26 +00002710 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002711 {
cristy4c08aed2011-07-01 19:47:50 +00002712 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
cristyed231572011-07-14 02:18:59 +00002713 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002714 }
2715 }
2716#if defined(MAGICKCORE_ZLIB_DELEGATE)
2717 if (compression == ZipCompression)
cristy018f07f2011-09-04 21:15:19 +00002718 status=ZLIBEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002719 else
2720#endif
2721 if (compression == LZWCompression)
cristy018f07f2011-09-04 21:15:19 +00002722 status=LZWEncodeImage(image,length,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002723 else
cristy018f07f2011-09-04 21:15:19 +00002724 status=PackbitsEncodeImage(image,length,pixels,exception);
cristy0553bd52013-06-30 15:53:50 +00002725 pixel_info=RelinquishVirtualMemory(pixel_info);
cristy3ed852e2009-09-05 21:47:34 +00002726 if (status == MagickFalse)
2727 {
2728 (void) CloseBlob(image);
2729 return(MagickFalse);
2730 }
2731 break;
2732 }
2733 case NoCompression:
2734 {
2735 /*
2736 Dump uncompressed PseudoColor packets.
2737 */
2738 Ascii85Initialize(image);
cristybb503372010-05-27 20:51:26 +00002739 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002740 {
cristy1e178e72011-08-28 19:44:34 +00002741 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00002742 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002743 break;
cristybb503372010-05-27 20:51:26 +00002744 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002745 {
cristy4c08aed2011-07-01 19:47:50 +00002746 Ascii85Encode(image,ScaleQuantumToChar(GetPixelAlpha(image,p)));
cristyed231572011-07-14 02:18:59 +00002747 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002748 }
2749 }
2750 Ascii85Flush(image);
2751 break;
2752 }
2753 }
2754 offset=TellBlob(image)-offset;
2755 (void) WriteBlobString(image,"\nendstream\n");
2756 }
2757 (void) WriteBlobString(image,"endobj\n");
2758 /*
2759 Write Length object.
2760 */
2761 xref[object++]=TellBlob(image);
cristy151b66d2015-04-15 10:50:31 +00002762 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002763 object);
cristy3ed852e2009-09-05 21:47:34 +00002764 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00002765 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g\n",(double) offset);
cristy3ed852e2009-09-05 21:47:34 +00002766 (void) WriteBlobString(image,buffer);
2767 (void) WriteBlobString(image,"endobj\n");
2768 if (GetNextImageInList(image) == (Image *) NULL)
2769 break;
2770 image=SyncNextImageInList(image);
2771 status=SetImageProgress(image,SaveImagesTag,scene++,
2772 GetImageListLength(image));
2773 if (status == MagickFalse)
2774 break;
2775 } while (image_info->adjoin != MagickFalse);
2776 /*
2777 Write Metadata object.
2778 */
2779 xref[object++]=TellBlob(image);
2780 info_id=object;
cristy151b66d2015-04-15 10:50:31 +00002781 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g 0 obj\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002782 object);
cristy3ed852e2009-09-05 21:47:34 +00002783 (void) WriteBlobString(image,buffer);
2784 (void) WriteBlobString(image,"<<\n");
Cristyce4a3552015-12-14 13:53:35 -05002785 utf16=ConvertUTF8ToUTF16((unsigned char *) basename,&length);
2786 if (utf16 != (wchar_t *) NULL)
2787 {
2788 (void) FormatLocaleString(buffer,MaxTextExtent,"/Title (\xfe\xff");
2789 (void) WriteBlobString(image,buffer);
Cristy1196b1a2015-12-17 16:29:01 -05002790 for (i=0; i < (ssize_t) length; i++)
Cristyce4a3552015-12-14 13:53:35 -05002791 WriteBlobMSBShort(image,(unsigned short) utf16[i]);
2792 (void) FormatLocaleString(buffer,MaxTextExtent,")\n");
2793 (void) WriteBlobString(image,buffer);
2794 utf16=(wchar_t *) RelinquishMagickMemory(utf16);
2795 }
cristy3ed852e2009-09-05 21:47:34 +00002796 seconds=time((time_t *) NULL);
2797#if defined(MAGICKCORE_HAVE_LOCALTIME_R)
2798 (void) localtime_r(&seconds,&local_time);
2799#else
2800 (void) memcpy(&local_time,localtime(&seconds),sizeof(local_time));
2801#endif
cristy151b66d2015-04-15 10:50:31 +00002802 (void) FormatLocaleString(date,MagickPathExtent,"D:%04d%02d%02d%02d%02d%02d",
cristy3ed852e2009-09-05 21:47:34 +00002803 local_time.tm_year+1900,local_time.tm_mon+1,local_time.tm_mday,
2804 local_time.tm_hour,local_time.tm_min,local_time.tm_sec);
cristy151b66d2015-04-15 10:50:31 +00002805 (void) FormatLocaleString(buffer,MagickPathExtent,"/CreationDate (%s)\n",date);
cristy3ed852e2009-09-05 21:47:34 +00002806 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00002807 (void) FormatLocaleString(buffer,MagickPathExtent,"/ModDate (%s)\n",date);
cristy3ed852e2009-09-05 21:47:34 +00002808 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00002809 (void) FormatLocaleString(buffer,MagickPathExtent,"/Producer (%s)\n",
cristybb503372010-05-27 20:51:26 +00002810 EscapeParenthesis(GetMagickVersion((size_t *) NULL)));
cristy3ed852e2009-09-05 21:47:34 +00002811 (void) WriteBlobString(image,buffer);
2812 (void) WriteBlobString(image,">>\n");
2813 (void) WriteBlobString(image,"endobj\n");
2814 /*
2815 Write Xref object.
2816 */
cristyf40010e2012-05-13 13:52:20 +00002817 offset=TellBlob(image)-xref[0]+
2818 (LocaleCompare(image_info->magick,"PDFA") == 0 ? 6 : 0)+10;
cristy3ed852e2009-09-05 21:47:34 +00002819 (void) WriteBlobString(image,"xref\n");
cristy151b66d2015-04-15 10:50:31 +00002820 (void) FormatLocaleString(buffer,MagickPathExtent,"0 %.20g\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002821 object+1);
cristy3ed852e2009-09-05 21:47:34 +00002822 (void) WriteBlobString(image,buffer);
2823 (void) WriteBlobString(image,"0000000000 65535 f \n");
cristybb503372010-05-27 20:51:26 +00002824 for (i=0; i < (ssize_t) object; i++)
cristy3ed852e2009-09-05 21:47:34 +00002825 {
cristy151b66d2015-04-15 10:50:31 +00002826 (void) FormatLocaleString(buffer,MagickPathExtent,"%010lu 00000 n \n",
cristyf2faecf2010-05-28 19:19:36 +00002827 (unsigned long) xref[i]);
cristy3ed852e2009-09-05 21:47:34 +00002828 (void) WriteBlobString(image,buffer);
2829 }
2830 (void) WriteBlobString(image,"trailer\n");
2831 (void) WriteBlobString(image,"<<\n");
cristy151b66d2015-04-15 10:50:31 +00002832 (void) FormatLocaleString(buffer,MagickPathExtent,"/Size %.20g\n",(double)
cristyf2faecf2010-05-28 19:19:36 +00002833 object+1);
cristy3ed852e2009-09-05 21:47:34 +00002834 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00002835 (void) FormatLocaleString(buffer,MagickPathExtent,"/Info %.20g 0 R\n",(double)
cristye8c25f92010-06-03 00:53:06 +00002836 info_id);
cristy3ed852e2009-09-05 21:47:34 +00002837 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00002838 (void) FormatLocaleString(buffer,MagickPathExtent,"/Root %.20g 0 R\n",(double)
cristye8c25f92010-06-03 00:53:06 +00002839 root_id);
cristy3ed852e2009-09-05 21:47:34 +00002840 (void) WriteBlobString(image,buffer);
cristy1b70dc82012-07-01 23:15:05 +00002841 (void) SignatureImage(image,exception);
cristy151b66d2015-04-15 10:50:31 +00002842 (void) FormatLocaleString(buffer,MagickPathExtent,"/ID [<%s> <%s>]\n",
cristy1b70dc82012-07-01 23:15:05 +00002843 GetImageProperty(image,"signature",exception),
2844 GetImageProperty(image,"signature",exception));
2845 (void) WriteBlobString(image,buffer);
cristy3ed852e2009-09-05 21:47:34 +00002846 (void) WriteBlobString(image,">>\n");
2847 (void) WriteBlobString(image,"startxref\n");
cristy151b66d2015-04-15 10:50:31 +00002848 (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g\n",(double) offset);
cristy3ed852e2009-09-05 21:47:34 +00002849 (void) WriteBlobString(image,buffer);
2850 (void) WriteBlobString(image,"%%EOF\n");
2851 xref=(MagickOffsetType *) RelinquishMagickMemory(xref);
2852 (void) CloseBlob(image);
2853 return(MagickTrue);
2854}