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     {
253       ept_info.tiff=(unsigned char *) RelinquishMagickMemory(ept_info.tiff);
254       ept_info.postscript=(unsigned char *) RelinquishMagickMemory(
255         ept_info.postscript);
256       ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
257     }
258   (void) CloseBlob(image);
259   image=DestroyImage(image);
260   read_info=CloneImageInfo(image_info);
261   read_info->number_scenes=1;
262   read_info->scene=0;
263   (void) CopyMagickString(read_info->magick,"EPS",MagickPathExtent);
264   image=BlobToImage(read_info,postscript_data,ept_info.postscript_length,
265     exception);
266   if (image != (Image *) NULL)
267     {
268       (void) CopyMagickString(image->filename,image_info->filename,
269         MagickPathExtent);
270       (void) CopyMagickString(image->magick,"EPT",MagickPathExtent);
271     }
272   (void) CopyMagickString(read_info->magick,"TIFF",MagickPathExtent);
273   tiff_image=BlobToImage(read_info,tiff_data,ept_info.tiff_length,exception);
274   if (tiff_image != (Image *) NULL)
275     {
276       if (image == (Image *) NULL)
277         image=tiff_image;
278       else
279         AppendImageToList(&image,tiff_image);
280     }
281   read_info=DestroyImageInfo(read_info);
282   ept_info.tiff=(unsigned char *) RelinquishMagickMemory(ept_info.tiff);
283   ept_info.postscript=(unsigned char *) RelinquishMagickMemory(
284     ept_info.postscript);
285   return(GetFirstImageInList(image));
286 }
287 
288 /*
289 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
290 %                                                                             %
291 %                                                                             %
292 %                                                                             %
293 %   R e g i s t e r E P T I m a g e                                           %
294 %                                                                             %
295 %                                                                             %
296 %                                                                             %
297 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
298 %
299 %  RegisterEPTImage() adds attributes for the EPT image format to
300 %  the list of supported formats.  The attributes include the image format
301 %  tag, a method to read and/or write the format, whether the format
302 %  supports the saving of more than one frame to the same file or blob,
303 %  whether the format supports native in-memory I/O, and a brief
304 %  description of the format.
305 %
306 %  The format of the RegisterEPTImage method is:
307 %
308 %      size_t RegisterEPTImage(void)
309 %
310 */
RegisterEPTImage(void)311 ModuleExport size_t RegisterEPTImage(void)
312 {
313   MagickInfo
314     *entry;
315 
316   entry=AcquireMagickInfo("EPT","EPT",
317     "Encapsulated PostScript with TIFF preview");
318   entry->decoder=(DecodeImageHandler *) ReadEPTImage;
319   entry->encoder=(EncodeImageHandler *) WriteEPTImage;
320   entry->magick=(IsImageFormatHandler *) IsEPT;
321   entry->flags|=CoderDecoderSeekableStreamFlag;
322   entry->flags^=CoderAdjoinFlag;
323   entry->flags^=CoderBlobSupportFlag;
324   (void) RegisterMagickInfo(entry);
325   entry=AcquireMagickInfo("EPT","EPT2",
326     "Encapsulated PostScript Level II with TIFF preview");
327   entry->decoder=(DecodeImageHandler *) ReadEPTImage;
328   entry->encoder=(EncodeImageHandler *) WriteEPTImage;
329   entry->magick=(IsImageFormatHandler *) IsEPT;
330   entry->flags^=CoderAdjoinFlag;
331   entry->flags|=CoderDecoderSeekableStreamFlag;
332   entry->flags^=CoderBlobSupportFlag;
333   (void) RegisterMagickInfo(entry);
334   entry=AcquireMagickInfo("EPT","EPT3",
335     "Encapsulated PostScript Level III with TIFF preview");
336   entry->decoder=(DecodeImageHandler *) ReadEPTImage;
337   entry->encoder=(EncodeImageHandler *) WriteEPTImage;
338   entry->magick=(IsImageFormatHandler *) IsEPT;
339   entry->flags|=CoderDecoderSeekableStreamFlag;
340   entry->flags^=CoderBlobSupportFlag;
341   (void) RegisterMagickInfo(entry);
342   return(MagickImageCoderSignature);
343 }
344 
345 /*
346 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
347 %                                                                             %
348 %                                                                             %
349 %                                                                             %
350 %   U n r e g i s t e r E P T I m a g e                                       %
351 %                                                                             %
352 %                                                                             %
353 %                                                                             %
354 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
355 %
356 %  UnregisterEPTImage() removes format registrations made by the
357 %  EPT module from the list of supported formats.
358 %
359 %  The format of the UnregisterEPTImage method is:
360 %
361 %      UnregisterEPTImage(void)
362 %
363 */
UnregisterEPTImage(void)364 ModuleExport void UnregisterEPTImage(void)
365 {
366   (void) UnregisterMagickInfo("EPT");
367 }
368 
369 /*
370 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
371 %                                                                             %
372 %                                                                             %
373 %                                                                             %
374 %   W r i t e E P T I m a g e                                                 %
375 %                                                                             %
376 %                                                                             %
377 %                                                                             %
378 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
379 %
380 %  WriteEPTImage() writes an image in the Encapsulated Postscript format
381 %  with a TIFF preview.
382 %
383 %  The format of the WriteEPTImage method is:
384 %
385 %      MagickBooleanType WriteEPTImage(const ImageInfo *image_info,
386 %        Image *image,ExceptionInfo *exception)
387 %
388 %  A description of each parameter follows.
389 %
390 %    o image_info: the image info.
391 %
392 %    o image:  The image.
393 %
394 %    o exception: return any errors or warnings in this structure.
395 %
396 */
WriteEPTImage(const ImageInfo * image_info,Image * image,ExceptionInfo * exception)397 static MagickBooleanType WriteEPTImage(const ImageInfo *image_info,Image *image,
398   ExceptionInfo *exception)
399 {
400   char
401     filename[MagickPathExtent];
402 
403   EPTInfo
404     ept_info;
405 
406   Image
407     *write_image;
408 
409   ImageInfo
410     *write_info;
411 
412   MagickBooleanType
413     status;
414 
415   /*
416     Write EPT image.
417   */
418   assert(image_info != (const ImageInfo *) NULL);
419   assert(image_info->signature == MagickCoreSignature);
420   assert(image != (Image *) NULL);
421   assert(image->signature == MagickCoreSignature);
422   if (image->debug != MagickFalse)
423     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
424   assert(exception != (ExceptionInfo *) NULL);
425   assert(exception->signature == MagickCoreSignature);
426   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
427   if (status == MagickFalse)
428     return(status);
429   write_image=CloneImage(image,0,0,MagickTrue,exception);
430   if (write_image == (Image *) NULL)
431     return(MagickFalse);
432   write_info=CloneImageInfo(image_info);
433   (void) CopyMagickString(write_info->filename,"EPS:",MagickPathExtent);
434   (void) CopyMagickString(write_info->magick,"EPS",MagickPathExtent);
435   if (LocaleCompare(image_info->magick,"EPT2") == 0)
436     {
437       (void) CopyMagickString(write_info->filename,"EPS2:",MagickPathExtent);
438       (void) CopyMagickString(write_info->magick,"EPS2",MagickPathExtent);
439     }
440   if (LocaleCompare(image_info->magick,"EPT3") == 0)
441     {
442       (void) CopyMagickString(write_info->filename,"EPS3:",MagickPathExtent);
443       (void) CopyMagickString(write_info->magick,"EPS3",MagickPathExtent);
444     }
445   (void) memset(&ept_info,0,sizeof(ept_info));
446   ept_info.magick=0xc6d3d0c5ul;
447   ept_info.postscript=(unsigned char *) ImageToBlob(write_info,write_image,
448     &ept_info.postscript_length,exception);
449   write_image=DestroyImage(write_image);
450   write_info=DestroyImageInfo(write_info);
451   if (ept_info.postscript == (void *) NULL)
452     return(MagickFalse);
453   write_image=CloneImage(image,0,0,MagickTrue,exception);
454   if (write_image == (Image *) NULL)
455     return(MagickFalse);
456   write_info=CloneImageInfo(image_info);
457   (void) CopyMagickString(write_info->magick,"TIFF",MagickPathExtent);
458   (void) FormatLocaleString(filename,MagickPathExtent,"tiff:%s",
459     write_info->filename);
460   (void) CopyMagickString(write_info->filename,filename,MagickPathExtent);
461   if ((write_image->columns > 512) || (write_image->rows > 512))
462     {
463       Image
464         *resize_image;
465 
466       resize_image=ResizeImage(write_image,512,512,write_image->filter,
467         exception);
468       if (resize_image != (Image *) NULL)
469         {
470           write_image=DestroyImage(write_image);
471           write_image=resize_image;
472         }
473     }
474   if ((write_image->storage_class == DirectClass) ||
475       (write_image->colors > 256))
476     {
477       QuantizeInfo
478         quantize_info;
479 
480       /*
481         EPT preview requires that the image is colormapped.
482       */
483       GetQuantizeInfo(&quantize_info);
484       quantize_info.dither_method=IdentifyPaletteImage(write_image,
485         exception) == MagickFalse ? RiemersmaDitherMethod : NoDitherMethod;
486       (void) QuantizeImage(&quantize_info,write_image,exception);
487     }
488   write_info->compression=NoCompression;
489   ept_info.tiff=(unsigned char *) ImageToBlob(write_info,write_image,
490     &ept_info.tiff_length,exception);
491   write_image=DestroyImage(write_image);
492   write_info=DestroyImageInfo(write_info);
493   if (ept_info.tiff == (void *) NULL)
494     {
495       ept_info.postscript=(unsigned char *) RelinquishMagickMemory(
496         ept_info.postscript);
497       return(MagickFalse);
498     }
499   /*
500     Write EPT image.
501   */
502   (void) WriteBlobLSBLong(image,(unsigned int) ept_info.magick);
503   (void) WriteBlobLSBLong(image,30);
504   (void) WriteBlobLSBLong(image,(unsigned int) ept_info.postscript_length);
505   (void) WriteBlobLSBLong(image,0);
506   (void) WriteBlobLSBLong(image,0);
507   (void) WriteBlobLSBLong(image,(unsigned int) ept_info.postscript_length+30);
508   (void) WriteBlobLSBLong(image,(unsigned int) ept_info.tiff_length);
509   (void) WriteBlobLSBShort(image,0xffff);
510   (void) WriteBlob(image,ept_info.postscript_length,ept_info.postscript);
511   (void) WriteBlob(image,ept_info.tiff_length,ept_info.tiff);
512   /*
513     Relinquish resources.
514   */
515   ept_info.postscript=(unsigned char *) RelinquishMagickMemory(
516     ept_info.postscript);
517   ept_info.tiff=(unsigned char *) RelinquishMagickMemory(ept_info.tiff);
518   (void) CloseBlob(image);
519   return(MagickTrue);
520 }
521