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