1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 % %
4 % %
5 % %
6 % EEEEE PPPP TTTTT %
7 % E P P T %
8 % EEE PPPP T %
9 % E P T %
10 % EEEEE P T %
11 % %
12 % %
13 % Read/Write Encapsulated Postscript Format (with preview). %
14 % %
15 % Software Design %
16 % Cristy %
17 % July 1992 %
18 % %
19 % %
20 % Copyright 1999-2021 ImageMagick Studio LLC, a non-profit organization %
21 % 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 % https://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 */
42 #include "MagickCore/studio.h"
43 #include "MagickCore/blob.h"
44 #include "MagickCore/blob-private.h"
45 #include "MagickCore/color.h"
46 #include "MagickCore/constitute.h"
47 #include "MagickCore/draw.h"
48 #include "MagickCore/exception.h"
49 #include "MagickCore/exception-private.h"
50 #include "MagickCore/delegate.h"
51 #include "MagickCore/geometry.h"
52 #include "MagickCore/histogram.h"
53 #include "MagickCore/image.h"
54 #include "MagickCore/image-private.h"
55 #include "MagickCore/list.h"
56 #include "MagickCore/magick.h"
57 #include "MagickCore/memory_.h"
58 #include "MagickCore/monitor.h"
59 #include "MagickCore/monitor-private.h"
60 #include "MagickCore/quantize.h"
61 #include "MagickCore/resource_.h"
62 #include "MagickCore/resize.h"
63 #include "MagickCore/quantum-private.h"
64 #include "MagickCore/static.h"
65 #include "MagickCore/string_.h"
66 #include "MagickCore/module.h"
67 #include "MagickCore/utility.h"
68
69 /*
70 Typedef declarations.
71 */
72 typedef struct _EPTInfo
73 {
74 size_t
75 magick;
76
77 MagickOffsetType
78 postscript_offset,
79 tiff_offset;
80
81 size_t
82 postscript_length,
83 tiff_length;
84
85 unsigned char
86 *postscript,
87 *tiff;
88 } EPTInfo;
89
90 /*
91 Forward declarations.
92 */
93 static MagickBooleanType
94 WriteEPTImage(const ImageInfo *,Image *,ExceptionInfo *);
95
96 /*
97 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
98 % %
99 % %
100 % %
101 % I s E P T %
102 % %
103 % %
104 % %
105 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106 %
107 % IsEPT() returns MagickTrue if the image format type, identified by the
108 % magick string, is EPT.
109 %
110 % The format of the IsEPT method is:
111 %
112 % MagickBooleanType IsEPT(const unsigned char *magick,const size_t length)
113 %
114 % A description of each parameter follows:
115 %
116 % o magick: compare image format pattern against these bytes.
117 %
118 % o length: Specifies the length of the magick string.
119 %
120 */
IsEPT(const unsigned char * magick,const size_t length)121 static MagickBooleanType IsEPT(const unsigned char *magick,const size_t length)
122 {
123 if (length < 4)
124 return(MagickFalse);
125 if (memcmp(magick,"\305\320\323\306",4) == 0)
126 return(MagickTrue);
127 return(MagickFalse);
128 }
129
130 /*
131 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
132 % %
133 % %
134 % %
135 % R e a d E P T I m a g e %
136 % %
137 % %
138 % %
139 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
140 %
141 % ReadEPTImage() reads a binary Postscript image file and returns it. It
142 % allocates the memory necessary for the new Image structure and returns a
143 % pointer to the new image.
144 %
145 % The format of the ReadEPTImage method is:
146 %
147 % Image *ReadEPTImage(const ImageInfo *image_info,
148 % ExceptionInfo *exception)
149 %
150 % A description of each parameter follows:
151 %
152 % o image_info: the image info.
153 %
154 % o exception: return any errors or warnings in this structure.
155 %
156 */
ReadEPTImage(const ImageInfo * image_info,ExceptionInfo * exception)157 static Image *ReadEPTImage(const ImageInfo *image_info,ExceptionInfo *exception)
158 {
159 const void
160 *postscript_data,
161 *tiff_data;
162
163 EPTInfo
164 ept_info;
165
166 Image
167 *image,
168 *tiff_image;
169
170 ImageInfo
171 *read_info;
172
173 MagickBooleanType
174 status;
175
176 MagickOffsetType
177 offset;
178
179 ssize_t
180 count;
181
182 /*
183 Open image file.
184 */
185 assert(image_info != (const ImageInfo *) NULL);
186 assert(image_info->signature == MagickCoreSignature);
187 if (image_info->debug != MagickFalse)
188 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
189 image_info->filename);
190 assert(exception != (ExceptionInfo *) NULL);
191 assert(exception->signature == MagickCoreSignature);
192 image=AcquireImage(image_info,exception);
193 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
194 if (status == MagickFalse)
195 {
196 image=DestroyImageList(image);
197 return((Image *) NULL);
198 }
199 ept_info.magick=ReadBlobLSBLong(image);
200 if (ept_info.magick != 0xc6d3d0c5ul)
201 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
202 ept_info.postscript_offset=(MagickOffsetType) ReadBlobLSBLong(image);
203 ept_info.postscript_length=ReadBlobLSBLong(image);
204 if ((MagickSizeType) ept_info.postscript_length > GetBlobSize(image))
205 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
206 (void) ReadBlobLSBLong(image);
207 (void) ReadBlobLSBLong(image);
208 ept_info.tiff_offset=(MagickOffsetType) ReadBlobLSBLong(image);
209 ept_info.tiff_length=ReadBlobLSBLong(image);
210 if ((MagickSizeType) ept_info.tiff_length > GetBlobSize(image))
211 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
212 (void) ReadBlobLSBShort(image);
213 ept_info.postscript=(unsigned char *) AcquireQuantumMemory(
214 ept_info.postscript_length+1,sizeof(*ept_info.postscript));
215 if (ept_info.postscript == (unsigned char *) NULL)
216 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
217 (void) memset(ept_info.postscript,0,(ept_info.postscript_length+1)*
218 sizeof(*ept_info.postscript));
219 ept_info.tiff=(unsigned char *) AcquireQuantumMemory(ept_info.tiff_length+1,
220 sizeof(*ept_info.tiff));
221 if (ept_info.tiff == (unsigned char *) NULL)
222 {
223 ept_info.postscript=(unsigned char *) RelinquishMagickMemory(
224 ept_info.postscript);
225 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
226 }
227 (void) memset(ept_info.tiff,0,(ept_info.tiff_length+1)*
228 sizeof(*ept_info.tiff));
229 offset=SeekBlob(image,ept_info.tiff_offset,SEEK_SET);
230 if ((ept_info.tiff_length != 0) && (offset < 30))
231 {
232 ept_info.tiff=(unsigned char *) RelinquishMagickMemory(ept_info.tiff);
233 ept_info.postscript=(unsigned char *) RelinquishMagickMemory(
234 ept_info.postscript);
235 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
236 }
237 tiff_data=ReadBlobStream(image,ept_info.tiff_length,ept_info.tiff,&count);
238 if (count != (ssize_t) (ept_info.tiff_length))
239 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageWarning,
240 "InsufficientImageDataInFile","`%s'",image->filename);
241 offset=SeekBlob(image,ept_info.postscript_offset,SEEK_SET);
242 if ((ept_info.postscript_length != 0) && (offset < 30))
243 {
244 ept_info.tiff=(unsigned char *) RelinquishMagickMemory(ept_info.tiff);
245 ept_info.postscript=(unsigned char *) RelinquishMagickMemory(
246 ept_info.postscript);
247 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
248 }
249 postscript_data=ReadBlobStream(image,ept_info.postscript_length,
250 ept_info.postscript,&count);
251 if (count != (ssize_t) (ept_info.postscript_length))
252 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
253 (void) CloseBlob(image);
254 image=DestroyImage(image);
255 read_info=CloneImageInfo(image_info);
256 read_info->number_scenes=1;
257 read_info->scene=0;
258 (void) CopyMagickString(read_info->magick,"EPS",MagickPathExtent);
259 image=BlobToImage(read_info,postscript_data,ept_info.postscript_length,
260 exception);
261 if (image != (Image *) NULL)
262 {
263 (void) CopyMagickString(image->filename,image_info->filename,
264 MagickPathExtent);
265 (void) CopyMagickString(image->magick,"EPT",MagickPathExtent);
266 }
267 (void) CopyMagickString(read_info->magick,"TIFF",MagickPathExtent);
268 tiff_image=BlobToImage(read_info,tiff_data,ept_info.tiff_length,exception);
269 if (tiff_image != (Image *) NULL)
270 {
271 if (image == (Image *) NULL)
272 image=tiff_image;
273 else
274 AppendImageToList(&image,tiff_image);
275 }
276 read_info=DestroyImageInfo(read_info);
277 ept_info.tiff=(unsigned char *) RelinquishMagickMemory(ept_info.tiff);
278 ept_info.postscript=(unsigned char *) RelinquishMagickMemory(
279 ept_info.postscript);
280 return(GetFirstImageInList(image));
281 }
282
283 /*
284 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
285 % %
286 % %
287 % %
288 % R e g i s t e r E P T I m a g e %
289 % %
290 % %
291 % %
292 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
293 %
294 % RegisterEPTImage() adds attributes for the EPT image format to
295 % the list of supported formats. The attributes include the image format
296 % tag, a method to read and/or write the format, whether the format
297 % supports the saving of more than one frame to the same file or blob,
298 % whether the format supports native in-memory I/O, and a brief
299 % description of the format.
300 %
301 % The format of the RegisterEPTImage method is:
302 %
303 % size_t RegisterEPTImage(void)
304 %
305 */
RegisterEPTImage(void)306 ModuleExport size_t RegisterEPTImage(void)
307 {
308 MagickInfo
309 *entry;
310
311 entry=AcquireMagickInfo("EPT","EPT",
312 "Encapsulated PostScript with TIFF preview");
313 entry->decoder=(DecodeImageHandler *) ReadEPTImage;
314 entry->encoder=(EncodeImageHandler *) WriteEPTImage;
315 entry->magick=(IsImageFormatHandler *) IsEPT;
316 entry->flags|=CoderDecoderSeekableStreamFlag;
317 entry->flags^=CoderAdjoinFlag;
318 entry->flags^=CoderBlobSupportFlag;
319 (void) RegisterMagickInfo(entry);
320 entry=AcquireMagickInfo("EPT","EPT2",
321 "Encapsulated PostScript Level II with TIFF preview");
322 entry->decoder=(DecodeImageHandler *) ReadEPTImage;
323 entry->encoder=(EncodeImageHandler *) WriteEPTImage;
324 entry->magick=(IsImageFormatHandler *) IsEPT;
325 entry->flags^=CoderAdjoinFlag;
326 entry->flags|=CoderDecoderSeekableStreamFlag;
327 entry->flags^=CoderBlobSupportFlag;
328 (void) RegisterMagickInfo(entry);
329 entry=AcquireMagickInfo("EPT","EPT3",
330 "Encapsulated PostScript Level III with TIFF preview");
331 entry->decoder=(DecodeImageHandler *) ReadEPTImage;
332 entry->encoder=(EncodeImageHandler *) WriteEPTImage;
333 entry->magick=(IsImageFormatHandler *) IsEPT;
334 entry->flags|=CoderDecoderSeekableStreamFlag;
335 entry->flags^=CoderBlobSupportFlag;
336 (void) RegisterMagickInfo(entry);
337 return(MagickImageCoderSignature);
338 }
339
340 /*
341 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
342 % %
343 % %
344 % %
345 % U n r e g i s t e r E P T I m a g e %
346 % %
347 % %
348 % %
349 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
350 %
351 % UnregisterEPTImage() removes format registrations made by the
352 % EPT module from the list of supported formats.
353 %
354 % The format of the UnregisterEPTImage method is:
355 %
356 % UnregisterEPTImage(void)
357 %
358 */
UnregisterEPTImage(void)359 ModuleExport void UnregisterEPTImage(void)
360 {
361 (void) UnregisterMagickInfo("EPT");
362 }
363
364 /*
365 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
366 % %
367 % %
368 % %
369 % W r i t e E P T I m a g e %
370 % %
371 % %
372 % %
373 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
374 %
375 % WriteEPTImage() writes an image in the Encapsulated Postscript format
376 % with a TIFF preview.
377 %
378 % The format of the WriteEPTImage method is:
379 %
380 % MagickBooleanType WriteEPTImage(const ImageInfo *image_info,
381 % Image *image,ExceptionInfo *exception)
382 %
383 % A description of each parameter follows.
384 %
385 % o image_info: the image info.
386 %
387 % o image: The image.
388 %
389 % o exception: return any errors or warnings in this structure.
390 %
391 */
WriteEPTImage(const ImageInfo * image_info,Image * image,ExceptionInfo * exception)392 static MagickBooleanType WriteEPTImage(const ImageInfo *image_info,Image *image,
393 ExceptionInfo *exception)
394 {
395 char
396 filename[MagickPathExtent];
397
398 EPTInfo
399 ept_info;
400
401 Image
402 *write_image;
403
404 ImageInfo
405 *write_info;
406
407 MagickBooleanType
408 status;
409
410 /*
411 Write EPT image.
412 */
413 assert(image_info != (const ImageInfo *) NULL);
414 assert(image_info->signature == MagickCoreSignature);
415 assert(image != (Image *) NULL);
416 assert(image->signature == MagickCoreSignature);
417 if (image->debug != MagickFalse)
418 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
419 assert(exception != (ExceptionInfo *) NULL);
420 assert(exception->signature == MagickCoreSignature);
421 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
422 if (status == MagickFalse)
423 return(status);
424 write_image=CloneImage(image,0,0,MagickTrue,exception);
425 if (write_image == (Image *) NULL)
426 return(MagickFalse);
427 write_info=CloneImageInfo(image_info);
428 (void) CopyMagickString(write_info->filename,"EPS:",MagickPathExtent);
429 (void) CopyMagickString(write_info->magick,"EPS",MagickPathExtent);
430 if (LocaleCompare(image_info->magick,"EPT2") == 0)
431 {
432 (void) CopyMagickString(write_info->filename,"EPS2:",MagickPathExtent);
433 (void) CopyMagickString(write_info->magick,"EPS2",MagickPathExtent);
434 }
435 if (LocaleCompare(image_info->magick,"EPT3") == 0)
436 {
437 (void) CopyMagickString(write_info->filename,"EPS3:",MagickPathExtent);
438 (void) CopyMagickString(write_info->magick,"EPS3",MagickPathExtent);
439 }
440 (void) memset(&ept_info,0,sizeof(ept_info));
441 ept_info.magick=0xc6d3d0c5ul;
442 ept_info.postscript=(unsigned char *) ImageToBlob(write_info,write_image,
443 &ept_info.postscript_length,exception);
444 write_image=DestroyImage(write_image);
445 write_info=DestroyImageInfo(write_info);
446 if (ept_info.postscript == (void *) NULL)
447 return(MagickFalse);
448 write_image=CloneImage(image,0,0,MagickTrue,exception);
449 if (write_image == (Image *) NULL)
450 return(MagickFalse);
451 write_info=CloneImageInfo(image_info);
452 (void) CopyMagickString(write_info->magick,"TIFF",MagickPathExtent);
453 (void) FormatLocaleString(filename,MagickPathExtent,"tiff:%s",
454 write_info->filename);
455 (void) CopyMagickString(write_info->filename,filename,MagickPathExtent);
456 if ((write_image->columns > 512) || (write_image->rows > 512))
457 {
458 Image
459 *resize_image;
460
461 resize_image=ResizeImage(write_image,512,512,write_image->filter,
462 exception);
463 if (resize_image != (Image *) NULL)
464 {
465 write_image=DestroyImage(write_image);
466 write_image=resize_image;
467 }
468 }
469 if ((write_image->storage_class == DirectClass) ||
470 (write_image->colors > 256))
471 {
472 QuantizeInfo
473 quantize_info;
474
475 /*
476 EPT preview requires that the image is colormapped.
477 */
478 GetQuantizeInfo(&quantize_info);
479 quantize_info.dither_method=IdentifyPaletteImage(write_image,
480 exception) == MagickFalse ? RiemersmaDitherMethod : NoDitherMethod;
481 (void) QuantizeImage(&quantize_info,write_image,exception);
482 }
483 write_info->compression=NoCompression;
484 ept_info.tiff=(unsigned char *) ImageToBlob(write_info,write_image,
485 &ept_info.tiff_length,exception);
486 write_image=DestroyImage(write_image);
487 write_info=DestroyImageInfo(write_info);
488 if (ept_info.tiff == (void *) NULL)
489 {
490 ept_info.postscript=(unsigned char *) RelinquishMagickMemory(
491 ept_info.postscript);
492 return(MagickFalse);
493 }
494 /*
495 Write EPT image.
496 */
497 (void) WriteBlobLSBLong(image,(unsigned int) ept_info.magick);
498 (void) WriteBlobLSBLong(image,30);
499 (void) WriteBlobLSBLong(image,(unsigned int) ept_info.postscript_length);
500 (void) WriteBlobLSBLong(image,0);
501 (void) WriteBlobLSBLong(image,0);
502 (void) WriteBlobLSBLong(image,(unsigned int) ept_info.postscript_length+30);
503 (void) WriteBlobLSBLong(image,(unsigned int) ept_info.tiff_length);
504 (void) WriteBlobLSBShort(image,0xffff);
505 (void) WriteBlob(image,ept_info.postscript_length,ept_info.postscript);
506 (void) WriteBlob(image,ept_info.tiff_length,ept_info.tiff);
507 /*
508 Relinquish resources.
509 */
510 ept_info.postscript=(unsigned char *) RelinquishMagickMemory(
511 ept_info.postscript);
512 ept_info.tiff=(unsigned char *) RelinquishMagickMemory(ept_info.tiff);
513 (void) CloseBlob(image);
514 return(MagickTrue);
515 }
516