2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Encapsulated Postscript Format (with preview). %
20 % Copyright 1999-2018 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % https://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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"
72 typedef struct _EPTInfo
93 static MagickBooleanType
94 WriteEPTImage(const ImageInfo *,Image *,ExceptionInfo *);
97 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
107 % IsEPT() returns MagickTrue if the image format type, identified by the
108 % magick string, is EPT.
110 % The format of the IsEPT method is:
112 % MagickBooleanType IsEPT(const unsigned char *magick,const size_t length)
114 % A description of each parameter follows:
116 % o magick: compare image format pattern against these bytes.
118 % o length: Specifies the length of the magick string.
121 static MagickBooleanType IsEPT(const unsigned char *magick,const size_t length)
125 if (memcmp(magick,"\305\320\323\306",4) == 0)
131 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
135 % R e a d E P T I m a g e %
139 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
145 % The format of the ReadEPTImage method is:
147 % Image *ReadEPTImage(const ImageInfo *image_info,
148 % ExceptionInfo *exception)
150 % A description of each parameter follows:
152 % o image_info: the image info.
154 % o exception: return any errors or warnings in this structure.
157 static Image *ReadEPTImage(const ImageInfo *image_info,ExceptionInfo *exception)
180 assert(image_info != (const ImageInfo *) NULL);
181 assert(image_info->signature == MagickCoreSignature);
182 if (image_info->debug != MagickFalse)
183 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
184 image_info->filename);
185 assert(exception != (ExceptionInfo *) NULL);
186 assert(exception->signature == MagickCoreSignature);
187 image=AcquireImage(image_info,exception);
188 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
189 if (status == MagickFalse)
191 image=DestroyImageList(image);
192 return((Image *) NULL);
194 ept_info.magick=ReadBlobLSBLong(image);
195 if (ept_info.magick != 0xc6d3d0c5ul)
196 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
197 ept_info.postscript_offset=(MagickOffsetType) ReadBlobLSBLong(image);
198 ept_info.postscript_length=ReadBlobLSBLong(image);
199 if (ept_info.postscript_length > GetBlobSize(image))
200 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
201 (void) ReadBlobLSBLong(image);
202 (void) ReadBlobLSBLong(image);
203 ept_info.tiff_offset=(MagickOffsetType) ReadBlobLSBLong(image);
204 ept_info.tiff_length=ReadBlobLSBLong(image);
205 if (ept_info.tiff_length > GetBlobSize(image))
206 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
207 (void) ReadBlobLSBShort(image);
208 ept_info.postscript=(unsigned char *) AcquireQuantumMemory(
209 ept_info.postscript_length+1,sizeof(*ept_info.postscript));
210 if (ept_info.postscript == (unsigned char *) NULL)
211 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
212 (void) memset(ept_info.postscript,0,(ept_info.postscript_length+1)*
213 sizeof(*ept_info.postscript));
214 ept_info.tiff=(unsigned char *) AcquireQuantumMemory(ept_info.tiff_length+1,
215 sizeof(*ept_info.tiff));
216 if (ept_info.tiff == (unsigned char *) NULL)
218 ept_info.postscript=(unsigned char *) RelinquishMagickMemory(
219 ept_info.postscript);
220 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
222 (void) memset(ept_info.tiff,0,(ept_info.tiff_length+1)*
223 sizeof(*ept_info.tiff));
224 offset=SeekBlob(image,ept_info.tiff_offset,SEEK_SET);
225 if ((ept_info.tiff_length != 0) && (offset < 30))
227 ept_info.tiff=(unsigned char *) RelinquishMagickMemory(ept_info.tiff);
228 ept_info.postscript=(unsigned char *) RelinquishMagickMemory(
229 ept_info.postscript);
230 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
232 count=ReadBlob(image,ept_info.tiff_length,ept_info.tiff);
233 if (count != (ssize_t) (ept_info.tiff_length))
234 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageWarning,
235 "InsufficientImageDataInFile","`%s'",image->filename);
236 offset=SeekBlob(image,ept_info.postscript_offset,SEEK_SET);
237 if ((ept_info.postscript_length != 0) && (offset < 30))
239 ept_info.tiff=(unsigned char *) RelinquishMagickMemory(ept_info.tiff);
240 ept_info.postscript=(unsigned char *) RelinquishMagickMemory(
241 ept_info.postscript);
242 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
244 count=ReadBlob(image,ept_info.postscript_length,ept_info.postscript);
245 if (count != (ssize_t) (ept_info.postscript_length))
246 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageWarning,
247 "InsufficientImageDataInFile","`%s'",image->filename);
248 (void) CloseBlob(image);
249 image=DestroyImage(image);
250 read_info=CloneImageInfo(image_info);
251 (void) CopyMagickString(read_info->magick,"EPS",MagickPathExtent);
252 image=BlobToImage(read_info,ept_info.postscript,ept_info.postscript_length,
254 if (image == (Image *) NULL)
256 (void) CopyMagickString(read_info->magick,"TIFF",MagickPathExtent);
257 image=BlobToImage(read_info,ept_info.tiff,ept_info.tiff_length,exception);
259 read_info=DestroyImageInfo(read_info);
260 if (image != (Image *) NULL)
262 (void) CopyMagickString(image->filename,image_info->filename,
264 (void) CopyMagickString(image->magick,"EPT",MagickPathExtent);
266 ept_info.tiff=(unsigned char *) RelinquishMagickMemory(ept_info.tiff);
267 ept_info.postscript=(unsigned char *) RelinquishMagickMemory(
268 ept_info.postscript);
273 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
277 % R e g i s t e r E P T I m a g e %
281 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
283 % RegisterEPTImage() adds attributes for the EPT image format to
284 % the list of supported formats. The attributes include the image format
285 % tag, a method to read and/or write the format, whether the format
286 % supports the saving of more than one frame to the same file or blob,
287 % whether the format supports native in-memory I/O, and a brief
288 % description of the format.
290 % The format of the RegisterEPTImage method is:
292 % size_t RegisterEPTImage(void)
295 ModuleExport size_t RegisterEPTImage(void)
300 entry=AcquireMagickInfo("EPT","EPT",
301 "Encapsulated PostScript with TIFF preview");
302 entry->decoder=(DecodeImageHandler *) ReadEPTImage;
303 entry->encoder=(EncodeImageHandler *) WriteEPTImage;
304 entry->magick=(IsImageFormatHandler *) IsEPT;
305 entry->flags|=CoderDecoderSeekableStreamFlag;
306 entry->flags^=CoderAdjoinFlag;
307 entry->flags^=CoderBlobSupportFlag;
308 (void) RegisterMagickInfo(entry);
309 entry=AcquireMagickInfo("EPT","EPT2",
310 "Encapsulated PostScript Level II with TIFF preview");
311 entry->decoder=(DecodeImageHandler *) ReadEPTImage;
312 entry->encoder=(EncodeImageHandler *) WriteEPTImage;
313 entry->magick=(IsImageFormatHandler *) IsEPT;
314 entry->flags^=CoderAdjoinFlag;
315 entry->flags|=CoderDecoderSeekableStreamFlag;
316 entry->flags^=CoderBlobSupportFlag;
317 (void) RegisterMagickInfo(entry);
318 entry=AcquireMagickInfo("EPT","EPT3",
319 "Encapsulated PostScript Level III with TIFF preview");
320 entry->decoder=(DecodeImageHandler *) ReadEPTImage;
321 entry->encoder=(EncodeImageHandler *) WriteEPTImage;
322 entry->magick=(IsImageFormatHandler *) IsEPT;
323 entry->flags|=CoderDecoderSeekableStreamFlag;
324 entry->flags^=CoderBlobSupportFlag;
325 (void) RegisterMagickInfo(entry);
326 return(MagickImageCoderSignature);
330 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
334 % U n r e g i s t e r E P T I m a g e %
338 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
340 % UnregisterEPTImage() removes format registrations made by the
341 % EPT module from the list of supported formats.
343 % The format of the UnregisterEPTImage method is:
345 % UnregisterEPTImage(void)
348 ModuleExport void UnregisterEPTImage(void)
350 (void) UnregisterMagickInfo("EPT");
354 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
358 % W r i t e E P T I m a g e %
362 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
364 % WriteEPTImage() writes an image in the Encapsulated Postscript format
365 % with a TIFF preview.
367 % The format of the WriteEPTImage method is:
369 % MagickBooleanType WriteEPTImage(const ImageInfo *image_info,
370 % Image *image,ExceptionInfo *exception)
372 % A description of each parameter follows.
374 % o image_info: the image info.
376 % o image: The image.
378 % o exception: return any errors or warnings in this structure.
381 static MagickBooleanType WriteEPTImage(const ImageInfo *image_info,Image *image,
382 ExceptionInfo *exception)
385 filename[MagickPathExtent];
402 assert(image_info != (const ImageInfo *) NULL);
403 assert(image_info->signature == MagickCoreSignature);
404 assert(image != (Image *) NULL);
405 assert(image->signature == MagickCoreSignature);
406 if (image->debug != MagickFalse)
407 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
408 assert(exception != (ExceptionInfo *) NULL);
409 assert(exception->signature == MagickCoreSignature);
410 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
411 if (status == MagickFalse)
413 write_image=CloneImage(image,0,0,MagickTrue,exception);
414 if (write_image == (Image *) NULL)
416 write_info=CloneImageInfo(image_info);
417 (void) CopyMagickString(write_info->filename,"EPS:",MagickPathExtent);
418 (void) CopyMagickString(write_info->magick,"EPS",MagickPathExtent);
419 if (LocaleCompare(image_info->magick,"EPT2") == 0)
421 (void) CopyMagickString(write_info->filename,"EPS2:",MagickPathExtent);
422 (void) CopyMagickString(write_info->magick,"EPS2",MagickPathExtent);
424 if (LocaleCompare(image_info->magick,"EPT3") == 0)
426 (void) CopyMagickString(write_info->filename,"EPS3:",MagickPathExtent);
427 (void) CopyMagickString(write_info->magick,"EPS3",MagickPathExtent);
429 (void) memset(&ept_info,0,sizeof(ept_info));
430 ept_info.magick=0xc6d3d0c5ul;
431 ept_info.postscript=(unsigned char *) ImageToBlob(write_info,write_image,
432 &ept_info.postscript_length,exception);
433 write_image=DestroyImage(write_image);
434 write_info=DestroyImageInfo(write_info);
435 if (ept_info.postscript == (void *) NULL)
437 write_image=CloneImage(image,0,0,MagickTrue,exception);
438 if (write_image == (Image *) NULL)
440 write_info=CloneImageInfo(image_info);
441 (void) CopyMagickString(write_info->magick,"TIFF",MagickPathExtent);
442 (void) FormatLocaleString(filename,MagickPathExtent,"tiff:%s",
443 write_info->filename);
444 (void) CopyMagickString(write_info->filename,filename,MagickPathExtent);
445 if ((write_image->columns > 512) || (write_image->rows > 512))
450 resize_image=ResizeImage(write_image,512,512,write_image->filter,
452 if (resize_image != (Image *) NULL)
454 write_image=DestroyImage(write_image);
455 write_image=resize_image;
458 if ((write_image->storage_class == DirectClass) ||
459 (write_image->colors > 256))
465 EPT preview requires that the image is colormapped.
467 GetQuantizeInfo(&quantize_info);
468 quantize_info.dither_method=IdentifyPaletteImage(write_image,
469 exception) == MagickFalse ? RiemersmaDitherMethod : NoDitherMethod;
470 (void) QuantizeImage(&quantize_info,write_image,exception);
472 write_info->compression=NoCompression;
473 ept_info.tiff=(unsigned char *) ImageToBlob(write_info,write_image,
474 &ept_info.tiff_length,exception);
475 write_image=DestroyImage(write_image);
476 write_info=DestroyImageInfo(write_info);
477 if (ept_info.tiff == (void *) NULL)
479 ept_info.postscript=(unsigned char *) RelinquishMagickMemory(
480 ept_info.postscript);
486 (void) WriteBlobLSBLong(image,(unsigned int) ept_info.magick);
487 (void) WriteBlobLSBLong(image,30);
488 (void) WriteBlobLSBLong(image,(unsigned int) ept_info.postscript_length);
489 (void) WriteBlobLSBLong(image,0);
490 (void) WriteBlobLSBLong(image,0);
491 (void) WriteBlobLSBLong(image,(unsigned int) ept_info.postscript_length+30);
492 (void) WriteBlobLSBLong(image,(unsigned int) ept_info.tiff_length);
493 (void) WriteBlobLSBShort(image,0xffff);
494 (void) WriteBlob(image,ept_info.postscript_length,ept_info.postscript);
495 (void) WriteBlob(image,ept_info.tiff_length,ept_info.tiff);
497 Relinquish resources.
499 ept_info.postscript=(unsigned char *) RelinquishMagickMemory(
500 ept_info.postscript);
501 ept_info.tiff=(unsigned char *) RelinquishMagickMemory(ept_info.tiff);
502 (void) CloseBlob(image);