2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Encapsulated Postscript Format (with preview). %
20 % Copyright 1999-2015 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 % http://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/quantum-private.h"
63 #include "MagickCore/static.h"
64 #include "MagickCore/string_.h"
65 #include "MagickCore/module.h"
66 #include "MagickCore/transform.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 == MagickSignature);
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 == MagickSignature);
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 (void) ReadBlobLSBLong(image);
200 (void) ReadBlobLSBLong(image);
201 ept_info.tiff_offset=(MagickOffsetType) ReadBlobLSBLong(image);
202 ept_info.tiff_length=ReadBlobLSBLong(image);
203 (void) ReadBlobLSBShort(image);
204 ept_info.postscript=(unsigned char *) AcquireQuantumMemory(
205 ept_info.postscript_length+1,sizeof(*ept_info.postscript));
206 if (ept_info.postscript == (unsigned char *) NULL)
207 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
208 (void) ResetMagickMemory(ept_info.postscript,0,(ept_info.postscript_length+1)*
209 sizeof(*ept_info.postscript));
210 ept_info.tiff=(unsigned char *) AcquireQuantumMemory(ept_info.tiff_length+1,
211 sizeof(*ept_info.tiff));
212 if (ept_info.tiff == (unsigned char *) NULL)
213 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
214 (void) ResetMagickMemory(ept_info.tiff,0,(ept_info.tiff_length+1)*
215 sizeof(*ept_info.tiff));
216 offset=SeekBlob(image,ept_info.tiff_offset,SEEK_SET);
218 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
219 count=ReadBlob(image,ept_info.tiff_length,ept_info.tiff);
220 if (count != (ssize_t) (ept_info.tiff_length))
221 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageWarning,
222 "InsufficientImageDataInFile","`%s'",image->filename);
223 offset=SeekBlob(image,ept_info.postscript_offset,SEEK_SET);
225 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
226 count=ReadBlob(image,ept_info.postscript_length,ept_info.postscript);
227 if (count != (ssize_t) (ept_info.postscript_length))
228 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageWarning,
229 "InsufficientImageDataInFile","`%s'",image->filename);
230 (void) CloseBlob(image);
231 image=DestroyImage(image);
232 read_info=CloneImageInfo(image_info);
233 (void) CopyMagickString(read_info->magick,"EPS",MaxTextExtent);
234 image=BlobToImage(read_info,ept_info.postscript,ept_info.postscript_length,
236 if (image == (Image *) NULL)
238 (void) CopyMagickString(read_info->magick,"TIFF",MaxTextExtent);
239 image=BlobToImage(read_info,ept_info.tiff,ept_info.tiff_length,exception);
241 read_info=DestroyImageInfo(read_info);
242 if (image != (Image *) NULL)
244 (void) CopyMagickString(image->filename,image_info->filename,
246 (void) CopyMagickString(image->magick,"EPT",MaxTextExtent);
248 ept_info.tiff=(unsigned char *) RelinquishMagickMemory(ept_info.tiff);
249 ept_info.postscript=(unsigned char *) RelinquishMagickMemory(
250 ept_info.postscript);
255 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
259 % R e g i s t e r E P T I m a g e %
263 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
265 % RegisterEPTImage() adds attributes for the EPT image format to
266 % the list of supported formats. The attributes include the image format
267 % tag, a method to read and/or write the format, whether the format
268 % supports the saving of more than one frame to the same file or blob,
269 % whether the format supports native in-memory I/O, and a brief
270 % description of the format.
272 % The format of the RegisterEPTImage method is:
274 % size_t RegisterEPTImage(void)
277 ModuleExport size_t RegisterEPTImage(void)
282 entry=SetMagickInfo("EPT");
283 entry->decoder=(DecodeImageHandler *) ReadEPTImage;
284 entry->encoder=(EncodeImageHandler *) WriteEPTImage;
285 entry->magick=(IsImageFormatHandler *) IsEPT;
286 entry->flags|=SeekableStream;
287 entry->flags^=Adjoin;
288 entry->flags^=BlobSupport;
289 entry->description=ConstantString(
290 "Encapsulated PostScript with TIFF preview");
291 entry->module=ConstantString("EPT");
292 (void) RegisterMagickInfo(entry);
293 entry=SetMagickInfo("EPT2");
294 entry->decoder=(DecodeImageHandler *) ReadEPTImage;
295 entry->encoder=(EncodeImageHandler *) WriteEPTImage;
296 entry->magick=(IsImageFormatHandler *) IsEPT;
297 entry->flags^=Adjoin;
298 entry->flags|=SeekableStream;
299 entry->flags^=BlobSupport;
300 entry->description=ConstantString(
301 "Encapsulated PostScript Level II with TIFF preview");
302 entry->module=ConstantString("EPT");
303 (void) RegisterMagickInfo(entry);
304 entry=SetMagickInfo("EPT3");
305 entry->decoder=(DecodeImageHandler *) ReadEPTImage;
306 entry->encoder=(EncodeImageHandler *) WriteEPTImage;
307 entry->magick=(IsImageFormatHandler *) IsEPT;
308 entry->flags|=SeekableStream;
309 entry->flags^=BlobSupport;
310 entry->description=ConstantString(
311 "Encapsulated PostScript Level III with TIFF preview");
312 entry->module=ConstantString("EPT");
313 (void) RegisterMagickInfo(entry);
314 return(MagickImageCoderSignature);
318 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
322 % U n r e g i s t e r E P T I m a g e %
326 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
328 % UnregisterEPTImage() removes format registrations made by the
329 % EPT module from the list of supported formats.
331 % The format of the UnregisterEPTImage method is:
333 % UnregisterEPTImage(void)
336 ModuleExport void UnregisterEPTImage(void)
338 (void) UnregisterMagickInfo("EPT");
342 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
346 % W r i t e E P T I m a g e %
350 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
352 % WriteEPTImage() writes an image in the Encapsulated Postscript format
353 % with a TIFF preview.
355 % The format of the WriteEPTImage method is:
357 % MagickBooleanType WriteEPTImage(const ImageInfo *image_info,
358 % Image *image,ExceptionInfo *exception)
360 % A description of each parameter follows.
362 % o image_info: the image info.
364 % o image: The image.
366 % o exception: return any errors or warnings in this structure.
369 static MagickBooleanType WriteEPTImage(const ImageInfo *image_info,Image *image,
370 ExceptionInfo *exception)
373 filename[MaxTextExtent];
390 assert(image_info != (const ImageInfo *) NULL);
391 assert(image_info->signature == MagickSignature);
392 assert(image != (Image *) NULL);
393 assert(image->signature == MagickSignature);
394 if (image->debug != MagickFalse)
395 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
396 assert(exception != (ExceptionInfo *) NULL);
397 assert(exception->signature == MagickSignature);
398 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
399 if (status == MagickFalse)
401 write_image=CloneImage(image,0,0,MagickTrue,exception);
402 if (write_image == (Image *) NULL)
404 write_info=CloneImageInfo(image_info);
405 (void) CopyMagickString(write_info->magick,"EPS",MaxTextExtent);
406 if (LocaleCompare(image_info->magick,"EPT2") == 0)
407 (void) CopyMagickString(write_info->magick,"EPS2",MaxTextExtent);
408 if (LocaleCompare(image_info->magick,"EPT3") == 0)
409 (void) CopyMagickString(write_info->magick,"EPS3",MaxTextExtent);
410 (void) ResetMagickMemory(&ept_info,0,sizeof(ept_info));
411 ept_info.magick=0xc6d3d0c5ul;
412 ept_info.postscript=(unsigned char *) ImageToBlob(write_info,write_image,
413 &ept_info.postscript_length,exception);
414 write_image=DestroyImage(write_image);
415 write_info=DestroyImageInfo(write_info);
416 if (ept_info.postscript == (void *) NULL)
418 write_image=CloneImage(image,0,0,MagickTrue,exception);
419 if (write_image == (Image *) NULL)
421 write_info=CloneImageInfo(image_info);
422 (void) CopyMagickString(write_info->magick,"TIFF",MaxTextExtent);
423 (void) FormatLocaleString(filename,MaxTextExtent,"tiff:%s",
424 write_info->filename);
425 (void) CopyMagickString(write_info->filename,filename,MaxTextExtent);
426 (void) TransformImage(&write_image,(char *) NULL,"512x512>",exception);
427 if ((write_image->storage_class == DirectClass) ||
428 (write_image->colors > 256))
434 EPT preview requires that the image is colormapped.
436 GetQuantizeInfo(&quantize_info);
437 quantize_info.dither_method=IsPaletteImage(write_image,exception) ==
438 MagickFalse ? RiemersmaDitherMethod : NoDitherMethod;
439 (void) QuantizeImage(&quantize_info,write_image,exception);
441 write_info->compression=NoCompression;
442 ept_info.tiff=(unsigned char *) ImageToBlob(write_info,write_image,
443 &ept_info.tiff_length,exception);
444 write_image=DestroyImage(write_image);
445 write_info=DestroyImageInfo(write_info);
446 if (ept_info.tiff == (void *) NULL)
448 ept_info.postscript=(unsigned char *) RelinquishMagickMemory(
449 ept_info.postscript);
455 (void) WriteBlobLSBLong(image,(unsigned int) ept_info.magick);
456 (void) WriteBlobLSBLong(image,30);
457 (void) WriteBlobLSBLong(image,(unsigned int) ept_info.postscript_length);
458 (void) WriteBlobLSBLong(image,0);
459 (void) WriteBlobLSBLong(image,0);
460 (void) WriteBlobLSBLong(image,(unsigned int) ept_info.postscript_length+30);
461 (void) WriteBlobLSBLong(image,(unsigned int) ept_info.tiff_length);
462 (void) WriteBlobLSBShort(image,0xffff);
463 (void) WriteBlob(image,ept_info.postscript_length,ept_info.postscript);
464 (void) WriteBlob(image,ept_info.tiff_length,ept_info.tiff);
466 Relinquish resources.
468 ept_info.postscript=(unsigned char *) RelinquishMagickMemory(
469 ept_info.postscript);
470 ept_info.tiff=(unsigned char *) RelinquishMagickMemory(ept_info.tiff);
471 (void) CloseBlob(image);