blob: 2ef8de31ffc4bb2df07220ad9a9cec6211370144 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
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% John Cristy %
17% July 1992 %
18% %
19% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 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/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"
cristy3ed852e2009-09-05 21:47:34 +000068
69/*
70 Typedef declarations.
71*/
72typedef struct _EPTInfo
73{
cristybb503372010-05-27 20:51:26 +000074 size_t
cristy3ed852e2009-09-05 21:47:34 +000075 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*/
93static MagickBooleanType
cristy1e178e72011-08-28 19:44:34 +000094 WriteEPTImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +000095
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*/
121static 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*/
157static Image *ReadEPTImage(const ImageInfo *image_info,ExceptionInfo *exception)
158{
159 EPTInfo
160 ept_info;
161
162 Image
163 *image;
164
165 ImageInfo
166 *read_info;
167
cristy3ed852e2009-09-05 21:47:34 +0000168 MagickBooleanType
169 status;
170
171 MagickOffsetType
172 offset;
173
cristy202de442011-04-24 18:19:07 +0000174 ssize_t
175 count;
176
cristy3ed852e2009-09-05 21:47:34 +0000177 /*
178 Open image file.
179 */
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);
cristy9950d572011-10-01 18:22:35 +0000187 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000188 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
189 if (status == MagickFalse)
190 {
191 image=DestroyImageList(image);
192 return((Image *) NULL);
193 }
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(
cristy86db8732010-03-02 14:11:33 +0000205 ept_info.postscript_length+1,sizeof(*ept_info.postscript));
cristy3ed852e2009-09-05 21:47:34 +0000206 if (ept_info.postscript == (unsigned char *) NULL)
207 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristy86db8732010-03-02 14:11:33 +0000208 (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,
cristy3ed852e2009-09-05 21:47:34 +0000211 sizeof(*ept_info.tiff));
cristy86db8732010-03-02 14:11:33 +0000212 if (ept_info.tiff == (unsigned char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000213 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristy86db8732010-03-02 14:11:33 +0000214 (void) ResetMagickMemory(ept_info.tiff,0,(ept_info.tiff_length+1)*
215 sizeof(*ept_info.tiff));
cristy3ed852e2009-09-05 21:47:34 +0000216 offset=SeekBlob(image,ept_info.tiff_offset,SEEK_SET);
217 if (offset < 0)
218 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
219 count=ReadBlob(image,ept_info.tiff_length,ept_info.tiff);
220 if (count != (ssize_t) (ept_info.tiff_length))
cristy88bae1c2010-02-22 13:58:51 +0000221 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageWarning,
222 "InsufficientImageDataInFile","`%s'",image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000223 offset=SeekBlob(image,ept_info.postscript_offset,SEEK_SET);
224 if (offset < 0)
225 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
226 count=ReadBlob(image,ept_info.postscript_length,ept_info.postscript);
227 if (count != (ssize_t) (ept_info.postscript_length))
cristy88bae1c2010-02-22 13:58:51 +0000228 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageWarning,
229 "InsufficientImageDataInFile","`%s'",image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000230 (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,
235 exception);
236 if (image == (Image *) NULL)
237 {
238 (void) CopyMagickString(read_info->magick,"TIFF",MaxTextExtent);
239 image=BlobToImage(read_info,ept_info.tiff,ept_info.tiff_length,exception);
240 }
241 read_info=DestroyImageInfo(read_info);
242 if (image != (Image *) NULL)
243 (void) CopyMagickString(image->filename,image_info->filename,MaxTextExtent);
244 ept_info.tiff=(unsigned char *) RelinquishMagickMemory(ept_info.tiff);
245 ept_info.postscript=(unsigned char *) RelinquishMagickMemory(
246 ept_info.postscript);
247 return(image);
248}
249
250/*
251%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
252% %
253% %
254% %
255% R e g i s t e r E P T I m a g e %
256% %
257% %
258% %
259%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
260%
261% RegisterEPTImage() adds attributes for the EPT image format to
262% the list of supported formats. The attributes include the image format
263% tag, a method to read and/or write the format, whether the format
264% supports the saving of more than one frame to the same file or blob,
265% whether the format supports native in-memory I/O, and a brief
266% description of the format.
267%
268% The format of the RegisterEPTImage method is:
269%
cristybb503372010-05-27 20:51:26 +0000270% size_t RegisterEPTImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000271%
272*/
cristybb503372010-05-27 20:51:26 +0000273ModuleExport size_t RegisterEPTImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000274{
275 MagickInfo
276 *entry;
277
278 entry=SetMagickInfo("EPT");
279 entry->decoder=(DecodeImageHandler *) ReadEPTImage;
280 entry->encoder=(EncodeImageHandler *) WriteEPTImage;
281 entry->magick=(IsImageFormatHandler *) IsEPT;
cristyffaf9782011-04-13 19:50:51 +0000282 entry->seekable_stream=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +0000283 entry->adjoin=MagickFalse;
284 entry->blob_support=MagickFalse;
285 entry->description=ConstantString(
286 "Encapsulated PostScript with TIFF preview");
287 entry->module=ConstantString("EPT");
288 (void) RegisterMagickInfo(entry);
289 entry=SetMagickInfo("EPT2");
290 entry->decoder=(DecodeImageHandler *) ReadEPTImage;
291 entry->encoder=(EncodeImageHandler *) WriteEPTImage;
292 entry->magick=(IsImageFormatHandler *) IsEPT;
293 entry->adjoin=MagickFalse;
cristyffaf9782011-04-13 19:50:51 +0000294 entry->seekable_stream=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +0000295 entry->blob_support=MagickFalse;
296 entry->description=ConstantString(
297 "Encapsulated PostScript Level II with TIFF preview");
298 entry->module=ConstantString("EPT");
299 (void) RegisterMagickInfo(entry);
300 entry=SetMagickInfo("EPT3");
301 entry->decoder=(DecodeImageHandler *) ReadEPTImage;
302 entry->encoder=(EncodeImageHandler *) WriteEPTImage;
303 entry->magick=(IsImageFormatHandler *) IsEPT;
cristyffaf9782011-04-13 19:50:51 +0000304 entry->seekable_stream=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +0000305 entry->blob_support=MagickFalse;
306 entry->description=ConstantString(
307 "Encapsulated PostScript Level III with TIFF preview");
308 entry->module=ConstantString("EPT");
309 (void) RegisterMagickInfo(entry);
310 return(MagickImageCoderSignature);
311}
312
313/*
314%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
315% %
316% %
317% %
318% U n r e g i s t e r E P T I m a g e %
319% %
320% %
321% %
322%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
323%
324% UnregisterEPTImage() removes format registrations made by the
325% EPT module from the list of supported formats.
326%
327% The format of the UnregisterEPTImage method is:
328%
329% UnregisterEPTImage(void)
330%
331*/
332ModuleExport void UnregisterEPTImage(void)
333{
334 (void) UnregisterMagickInfo("EPT");
335}
336
337/*
338%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
339% %
340% %
341% %
342% W r i t e E P T I m a g e %
343% %
344% %
345% %
346%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
347%
348% WriteEPTImage() writes an image in the Encapsulated Postscript format
349% with a TIFF preview.
350%
351% The format of the WriteEPTImage method is:
352%
cristy1e178e72011-08-28 19:44:34 +0000353% MagickBooleanType WriteEPTImage(const ImageInfo *image_info,
354% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000355%
356% A description of each parameter follows.
357%
358% o image_info: the image info.
359%
360% o image: The image.
361%
cristy1e178e72011-08-28 19:44:34 +0000362% o exception: return any errors or warnings in this structure.
363%
cristy3ed852e2009-09-05 21:47:34 +0000364*/
cristy1e178e72011-08-28 19:44:34 +0000365static MagickBooleanType WriteEPTImage(const ImageInfo *image_info,Image *image,
366 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000367{
cristy2d6ccc32009-09-25 03:18:25 +0000368 char
369 filename[MaxTextExtent];
370
cristy3ed852e2009-09-05 21:47:34 +0000371 EPTInfo
372 ept_info;
373
374 Image
375 *write_image;
376
377 ImageInfo
378 *write_info;
379
380 MagickBooleanType
381 status;
382
383 /*
384 Write EPT image.
385 */
386 assert(image_info != (const ImageInfo *) NULL);
387 assert(image_info->signature == MagickSignature);
388 assert(image != (Image *) NULL);
389 assert(image->signature == MagickSignature);
390 if (image->debug != MagickFalse)
391 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +0000392 assert(exception != (ExceptionInfo *) NULL);
393 assert(exception->signature == MagickSignature);
cristy1e178e72011-08-28 19:44:34 +0000394 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000395 if (status == MagickFalse)
396 return(status);
cristy1e178e72011-08-28 19:44:34 +0000397 write_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000398 if (write_image == (Image *) NULL)
399 return(MagickFalse);
400 write_info=CloneImageInfo(image_info);
401 (void) CopyMagickString(write_info->magick,"EPS",MaxTextExtent);
402 if (LocaleCompare(image_info->magick,"EPT2") == 0)
403 (void) CopyMagickString(write_info->magick,"EPS2",MaxTextExtent);
404 if (LocaleCompare(image_info->magick,"EPT3") == 0)
405 (void) CopyMagickString(write_info->magick,"EPS3",MaxTextExtent);
406 (void) ResetMagickMemory(&ept_info,0,sizeof(ept_info));
407 ept_info.magick=0xc6d3d0c5ul;
408 ept_info.postscript=(unsigned char *) ImageToBlob(write_info,write_image,
cristy1e178e72011-08-28 19:44:34 +0000409 &ept_info.postscript_length,exception);
cristy3ed852e2009-09-05 21:47:34 +0000410 write_image=DestroyImage(write_image);
411 write_info=DestroyImageInfo(write_info);
412 if (ept_info.postscript == (void *) NULL)
413 return(MagickFalse);
cristy1e178e72011-08-28 19:44:34 +0000414 write_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000415 if (write_image == (Image *) NULL)
416 return(MagickFalse);
417 write_info=CloneImageInfo(image_info);
418 (void) CopyMagickString(write_info->magick,"TIFF",MaxTextExtent);
cristyb51dff52011-05-19 16:55:47 +0000419 (void) FormatLocaleString(filename,MaxTextExtent,"tiff:%s",
cristy3ed852e2009-09-05 21:47:34 +0000420 write_info->filename);
cristy2d6ccc32009-09-25 03:18:25 +0000421 (void) CopyMagickString(write_info->filename,filename,MaxTextExtent);
cristye941a752011-10-15 01:52:48 +0000422 (void) TransformImage(&write_image,(char *) NULL,"512x512>",exception);
cristy3ed852e2009-09-05 21:47:34 +0000423 if ((write_image->storage_class == DirectClass) ||
424 (write_image->colors > 256))
425 {
426 QuantizeInfo
427 quantize_info;
428
429 /*
430 EPT preview requires that the image is colormapped.
431 */
432 GetQuantizeInfo(&quantize_info);
cristy1e178e72011-08-28 19:44:34 +0000433 quantize_info.dither=IsPaletteImage(write_image,exception) == MagickFalse
434 ? MagickTrue : MagickFalse;
cristy018f07f2011-09-04 21:15:19 +0000435 (void) QuantizeImage(&quantize_info,write_image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000436 }
437 write_info->compression=NoCompression;
438 ept_info.tiff=(unsigned char *) ImageToBlob(write_info,write_image,
cristy1e178e72011-08-28 19:44:34 +0000439 &ept_info.tiff_length,exception);
cristy3ed852e2009-09-05 21:47:34 +0000440 write_image=DestroyImage(write_image);
441 write_info=DestroyImageInfo(write_info);
442 if (ept_info.tiff == (void *) NULL)
443 {
444 ept_info.postscript=(unsigned char *) RelinquishMagickMemory(
445 ept_info.postscript);
446 return(MagickFalse);
447 }
448 /*
449 Write EPT image.
450 */
cristy2d6ccc32009-09-25 03:18:25 +0000451 (void) WriteBlobLSBLong(image,(unsigned int) ept_info.magick);
cristy3ed852e2009-09-05 21:47:34 +0000452 (void) WriteBlobLSBLong(image,30);
cristy2d6ccc32009-09-25 03:18:25 +0000453 (void) WriteBlobLSBLong(image,(unsigned int) ept_info.postscript_length);
cristy3ed852e2009-09-05 21:47:34 +0000454 (void) WriteBlobLSBLong(image,0);
455 (void) WriteBlobLSBLong(image,0);
cristy2d6ccc32009-09-25 03:18:25 +0000456 (void) WriteBlobLSBLong(image,(unsigned int) ept_info.postscript_length+30);
457 (void) WriteBlobLSBLong(image,(unsigned int) ept_info.tiff_length);
cristy3ed852e2009-09-05 21:47:34 +0000458 (void) WriteBlobLSBShort(image,0xffff);
459 (void) WriteBlob(image,ept_info.postscript_length,ept_info.postscript);
460 (void) WriteBlob(image,ept_info.tiff_length,ept_info.tiff);
461 /*
462 Relinquish resources.
463 */
464 ept_info.postscript=(unsigned char *) RelinquishMagickMemory(
465 ept_info.postscript);
466 ept_info.tiff=(unsigned char *) RelinquishMagickMemory(ept_info.tiff);
467 (void) CloseBlob(image);
468 return(MagickTrue);
469}