]> granicus.if.org Git - imagemagick/blob - coders/ept.c
https://github.com/ImageMagick/ImageMagick/issues/453
[imagemagick] / coders / ept.c
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-2017 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://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 \f
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 \f
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 \f
90 /*
91   Forward declarations.
92 */
93 static MagickBooleanType
94   WriteEPTImage(const ImageInfo *,Image *,ExceptionInfo *);
95 \f
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 */
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 \f
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 */
157 static 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
168   MagickBooleanType
169     status;
170
171   MagickOffsetType
172     offset;
173
174   ssize_t
175     count;
176
177   /*
178     Open image file.
179   */
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)
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(
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     {
214       ept_info.postscript=(unsigned char *) RelinquishMagickMemory(
215         ept_info.postscript);
216       ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
217     }
218   (void) ResetMagickMemory(ept_info.tiff,0,(ept_info.tiff_length+1)*
219     sizeof(*ept_info.tiff));
220   offset=SeekBlob(image,ept_info.tiff_offset,SEEK_SET);
221   if ((ept_info.tiff_length != 0) && (offset < 30))
222     {
223       ept_info.tiff=(unsigned char *) RelinquishMagickMemory(ept_info.tiff);
224       ept_info.postscript=(unsigned char *) RelinquishMagickMemory(
225         ept_info.postscript);
226       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
227     }
228   count=ReadBlob(image,ept_info.tiff_length,ept_info.tiff);
229   if (count != (ssize_t) (ept_info.tiff_length))
230     (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageWarning,
231       "InsufficientImageDataInFile","`%s'",image->filename);
232   offset=SeekBlob(image,ept_info.postscript_offset,SEEK_SET);
233   if ((ept_info.postscript_length != 0) && (offset < 30))
234     {
235       ept_info.tiff=(unsigned char *) RelinquishMagickMemory(ept_info.tiff);
236       ept_info.postscript=(unsigned char *) RelinquishMagickMemory(
237         ept_info.postscript);
238       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
239     }
240   count=ReadBlob(image,ept_info.postscript_length,ept_info.postscript);
241   if (count != (ssize_t) (ept_info.postscript_length))
242     (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageWarning,
243       "InsufficientImageDataInFile","`%s'",image->filename);
244   (void) CloseBlob(image);
245   image=DestroyImage(image);
246   read_info=CloneImageInfo(image_info);
247   (void) CopyMagickString(read_info->magick,"EPS",MagickPathExtent);
248   image=BlobToImage(read_info,ept_info.postscript,ept_info.postscript_length,
249     exception);
250   if (image == (Image *) NULL)
251     {
252       (void) CopyMagickString(read_info->magick,"TIFF",MagickPathExtent);
253       image=BlobToImage(read_info,ept_info.tiff,ept_info.tiff_length,exception);
254     }
255   read_info=DestroyImageInfo(read_info);
256   if (image != (Image *) NULL)
257     {
258       (void) CopyMagickString(image->filename,image_info->filename,
259         MagickPathExtent);
260       (void) CopyMagickString(image->magick,"EPT",MagickPathExtent);
261     }
262   ept_info.tiff=(unsigned char *) RelinquishMagickMemory(ept_info.tiff);
263   ept_info.postscript=(unsigned char *) RelinquishMagickMemory(
264     ept_info.postscript);
265   return(image);
266 }
267 \f
268 /*
269 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
270 %                                                                             %
271 %                                                                             %
272 %                                                                             %
273 %   R e g i s t e r E P T I m a g e                                           %
274 %                                                                             %
275 %                                                                             %
276 %                                                                             %
277 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
278 %
279 %  RegisterEPTImage() adds attributes for the EPT image format to
280 %  the list of supported formats.  The attributes include the image format
281 %  tag, a method to read and/or write the format, whether the format
282 %  supports the saving of more than one frame to the same file or blob,
283 %  whether the format supports native in-memory I/O, and a brief
284 %  description of the format.
285 %
286 %  The format of the RegisterEPTImage method is:
287 %
288 %      size_t RegisterEPTImage(void)
289 %
290 */
291 ModuleExport size_t RegisterEPTImage(void)
292 {
293   MagickInfo
294     *entry;
295
296   entry=AcquireMagickInfo("EPT","EPT",
297     "Encapsulated PostScript with TIFF preview");
298   entry->decoder=(DecodeImageHandler *) ReadEPTImage;
299   entry->encoder=(EncodeImageHandler *) WriteEPTImage;
300   entry->magick=(IsImageFormatHandler *) IsEPT;
301   entry->flags|=CoderDecoderSeekableStreamFlag;
302   entry->flags^=CoderAdjoinFlag;
303   entry->flags^=CoderBlobSupportFlag;
304   (void) RegisterMagickInfo(entry);
305   entry=AcquireMagickInfo("EPT","EPT2",
306     "Encapsulated PostScript Level II with TIFF preview");
307   entry->decoder=(DecodeImageHandler *) ReadEPTImage;
308   entry->encoder=(EncodeImageHandler *) WriteEPTImage;
309   entry->magick=(IsImageFormatHandler *) IsEPT;
310   entry->flags^=CoderAdjoinFlag;
311   entry->flags|=CoderDecoderSeekableStreamFlag;
312   entry->flags^=CoderBlobSupportFlag;
313   (void) RegisterMagickInfo(entry);
314   entry=AcquireMagickInfo("EPT","EPT3",
315     "Encapsulated PostScript Level III with TIFF preview");
316   entry->decoder=(DecodeImageHandler *) ReadEPTImage;
317   entry->encoder=(EncodeImageHandler *) WriteEPTImage;
318   entry->magick=(IsImageFormatHandler *) IsEPT;
319   entry->flags|=CoderDecoderSeekableStreamFlag;
320   entry->flags^=CoderBlobSupportFlag;
321   (void) RegisterMagickInfo(entry);
322   return(MagickImageCoderSignature);
323 }
324 \f
325 /*
326 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
327 %                                                                             %
328 %                                                                             %
329 %                                                                             %
330 %   U n r e g i s t e r E P T I m a g e                                       %
331 %                                                                             %
332 %                                                                             %
333 %                                                                             %
334 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
335 %
336 %  UnregisterEPTImage() removes format registrations made by the
337 %  EPT module from the list of supported formats.
338 %
339 %  The format of the UnregisterEPTImage method is:
340 %
341 %      UnregisterEPTImage(void)
342 %
343 */
344 ModuleExport void UnregisterEPTImage(void)
345 {
346   (void) UnregisterMagickInfo("EPT");
347 }
348 \f
349 /*
350 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
351 %                                                                             %
352 %                                                                             %
353 %                                                                             %
354 %   W r i t e E P T I m a g e                                                 %
355 %                                                                             %
356 %                                                                             %
357 %                                                                             %
358 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
359 %
360 %  WriteEPTImage() writes an image in the Encapsulated Postscript format
361 %  with a TIFF preview.
362 %
363 %  The format of the WriteEPTImage method is:
364 %
365 %      MagickBooleanType WriteEPTImage(const ImageInfo *image_info,
366 %        Image *image,ExceptionInfo *exception)
367 %
368 %  A description of each parameter follows.
369 %
370 %    o image_info: the image info.
371 %
372 %    o image:  The image.
373 %
374 %    o exception: return any errors or warnings in this structure.
375 %
376 */
377 static MagickBooleanType WriteEPTImage(const ImageInfo *image_info,Image *image,
378   ExceptionInfo *exception)
379 {
380   char
381      filename[MagickPathExtent];
382
383   EPTInfo
384     ept_info;
385
386   Image
387     *write_image;
388
389   ImageInfo
390     *write_info;
391
392   MagickBooleanType
393     status;
394
395   /*
396     Write EPT image.
397   */
398   assert(image_info != (const ImageInfo *) NULL);
399   assert(image_info->signature == MagickCoreSignature);
400   assert(image != (Image *) NULL);
401   assert(image->signature == MagickCoreSignature);
402   if (image->debug != MagickFalse)
403     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
404   assert(exception != (ExceptionInfo *) NULL);
405   assert(exception->signature == MagickCoreSignature);
406   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
407   if (status == MagickFalse)
408     return(status);
409   write_image=CloneImage(image,0,0,MagickTrue,exception);
410   if (write_image == (Image *) NULL)
411     return(MagickFalse);
412   write_info=CloneImageInfo(image_info);
413   (void) CopyMagickString(write_info->magick,"EPS",MagickPathExtent);
414   if (LocaleCompare(image_info->magick,"EPT2") == 0)
415     (void) CopyMagickString(write_info->magick,"EPS2",MagickPathExtent);
416   if (LocaleCompare(image_info->magick,"EPT3") == 0)
417     (void) CopyMagickString(write_info->magick,"EPS3",MagickPathExtent);
418   (void) ResetMagickMemory(&ept_info,0,sizeof(ept_info));
419   ept_info.magick=0xc6d3d0c5ul;
420   ept_info.postscript=(unsigned char *) ImageToBlob(write_info,write_image,
421     &ept_info.postscript_length,exception);
422   write_image=DestroyImage(write_image);
423   write_info=DestroyImageInfo(write_info);
424   if (ept_info.postscript == (void *) NULL)
425     return(MagickFalse);
426   write_image=CloneImage(image,0,0,MagickTrue,exception);
427   if (write_image == (Image *) NULL)
428     return(MagickFalse);
429   write_info=CloneImageInfo(image_info);
430   *write_info->magick='\0';
431   (void) CopyMagickString(write_info->magick,"TIFF",MagickPathExtent);
432   (void) FormatLocaleString(filename,MagickPathExtent,"tiff:%s",
433     write_info->filename); 
434   (void) CopyMagickString(write_info->filename,filename,MagickPathExtent);
435   if ((write_image->columns > 512) || (write_image->rows > 512))
436     {
437       Image
438         *resize_image;
439
440       resize_image=ResizeImage(write_image,512,512,write_image->filter,
441         exception);
442       if (resize_image != (Image *) NULL)
443         {
444           write_image=DestroyImage(write_image);
445           write_image=resize_image;
446         }
447     }
448   if ((write_image->storage_class == DirectClass) ||
449       (write_image->colors > 256))
450     {
451       QuantizeInfo
452         quantize_info;
453
454       /*
455         EPT preview requires that the image is colormapped.
456       */
457       GetQuantizeInfo(&quantize_info);
458       quantize_info.dither_method=IdentifyPaletteImage(write_image,
459         exception) == MagickFalse ? RiemersmaDitherMethod : NoDitherMethod;
460       (void) QuantizeImage(&quantize_info,write_image,exception);
461     }
462   write_info->compression=NoCompression;
463   ept_info.tiff=(unsigned char *) ImageToBlob(write_info,write_image,
464     &ept_info.tiff_length,exception);
465   write_image=DestroyImage(write_image);
466   write_info=DestroyImageInfo(write_info);
467   if (ept_info.tiff == (void *) NULL)
468     {
469       ept_info.postscript=(unsigned char *) RelinquishMagickMemory(
470         ept_info.postscript);
471       return(MagickFalse);
472     }
473   /*
474     Write EPT image.
475   */
476   (void) WriteBlobLSBLong(image,(unsigned int) ept_info.magick);
477   (void) WriteBlobLSBLong(image,30);
478   (void) WriteBlobLSBLong(image,(unsigned int) ept_info.postscript_length);
479   (void) WriteBlobLSBLong(image,0);
480   (void) WriteBlobLSBLong(image,0);
481   (void) WriteBlobLSBLong(image,(unsigned int) ept_info.postscript_length+30);
482   (void) WriteBlobLSBLong(image,(unsigned int) ept_info.tiff_length);
483   (void) WriteBlobLSBShort(image,0xffff);
484   (void) WriteBlob(image,ept_info.postscript_length,ept_info.postscript);
485   (void) WriteBlob(image,ept_info.tiff_length,ept_info.tiff);
486   /*
487     Relinquish resources.
488   */
489   ept_info.postscript=(unsigned char *) RelinquishMagickMemory(
490     ept_info.postscript);
491   ept_info.tiff=(unsigned char *) RelinquishMagickMemory(ept_info.tiff);
492   (void) CloseBlob(image);
493   return(MagickTrue);
494 }