]> granicus.if.org Git - imagemagick/blob - coders/exr.c
Changed several booleans in MagickInfo to a flag.
[imagemagick] / coders / exr.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                            EEEEE  X   X  RRRR                               %
7 %                            E       X X   R   R                              %
8 %                            EEE      X    RRRR                               %
9 %                            E       X X   R R                                %
10 %                            EEEEE  X   X  R  R                               %
11 %                                                                             %
12 %                                                                             %
13 %            Read/Write High Dynamic-Range (HDR) Image File Format            %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                   Cristy                                    %
17 %                                 April 2007                                  %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2015 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 %    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 \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/cache.h"
46 #include "MagickCore/exception.h"
47 #include "MagickCore/exception-private.h"
48 #include "MagickCore/image.h"
49 #include "MagickCore/image-private.h"
50 #include "MagickCore/list.h"
51 #include "MagickCore/magick.h"
52 #include "MagickCore/memory_.h"
53 #include "MagickCore/pixel-accessor.h"
54 #include "MagickCore/property.h"
55 #include "MagickCore/quantum-private.h"
56 #include "MagickCore/static.h"
57 #include "MagickCore/string_.h"
58 #include "MagickCore/module.h"
59 #include "MagickCore/resource_.h"
60 #include "MagickCore/utility.h"
61 #if defined(MAGICKCORE_OPENEXR_DELEGATE)
62 #include <ImfCRgbaFile.h>
63 \f
64 /*
65   Forward declarations.
66 */
67 static MagickBooleanType
68   WriteEXRImage(const ImageInfo *,Image *,ExceptionInfo *);
69 #endif
70 \f
71 /*
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73 %                                                                             %
74 %                                                                             %
75 %                                                                             %
76 %   I s E X R                                                                 %
77 %                                                                             %
78 %                                                                             %
79 %                                                                             %
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 %
82 %  IsEXR() returns MagickTrue if the image format type, identified by the
83 %  magick string, is EXR.
84 %
85 %  The format of the IsEXR method is:
86 %
87 %      MagickBooleanType IsEXR(const unsigned char *magick,const size_t length)
88 %
89 %  A description of each parameter follows:
90 %
91 %    o magick: compare image format pattern against these bytes.
92 %
93 %    o length: Specifies the length of the magick string.
94 %
95 */
96 static MagickBooleanType IsEXR(const unsigned char *magick,const size_t length)
97 {
98   if (length < 4)
99     return(MagickFalse);
100   if (memcmp(magick,"\166\057\061\001",4) == 0)
101     return(MagickTrue);
102   return(MagickFalse);
103 }
104 \f
105 #if defined(MAGICKCORE_OPENEXR_DELEGATE)
106 /*
107 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108 %                                                                             %
109 %                                                                             %
110 %                                                                             %
111 %   R e a d E X R I m a g e                                                   %
112 %                                                                             %
113 %                                                                             %
114 %                                                                             %
115 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116 %
117 %  ReadEXRImage reads an image in the high dynamic-range (HDR) file format
118 %  developed by Industrial Light & Magic.  It allocates the memory necessary
119 %  for the new Image structure and returns a pointer to the new image.
120 %
121 %  The format of the ReadEXRImage method is:
122 %
123 %      Image *ReadEXRImage(const ImageInfo *image_info,ExceptionInfo *exception)
124 %
125 %  A description of each parameter follows:
126 %
127 %    o image_info: the image info.
128 %
129 %    o exception: return any errors or warnings in this structure.
130 %
131 */
132 static Image *ReadEXRImage(const ImageInfo *image_info,ExceptionInfo *exception)
133 {
134   const ImfHeader
135     *hdr_info;
136
137   Image
138     *image;
139
140   ImageInfo
141     *read_info;
142
143   ImfInputFile
144     *file;
145
146   ImfRgba
147     *scanline;
148
149   int
150     max_x,
151     max_y,
152     min_x,
153     min_y;
154
155   MagickBooleanType
156     status;
157
158   register ssize_t
159     x;
160
161   register Quantum
162     *q;
163
164   ssize_t
165     y;
166
167   /*
168     Open image.
169   */
170   assert(image_info != (const ImageInfo *) NULL);
171   assert(image_info->signature == MagickSignature);
172   if (image_info->debug != MagickFalse)
173     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
174       image_info->filename);
175   assert(exception != (ExceptionInfo *) NULL);
176   assert(exception->signature == MagickSignature);
177   image=AcquireImage(image_info,exception);
178   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
179   if (status == MagickFalse)
180     {
181       image=DestroyImageList(image);
182       return((Image *) NULL);
183     }
184   read_info=CloneImageInfo(image_info);
185   if (IsPathAccessible(read_info->filename) == MagickFalse)
186     {
187       (void) AcquireUniqueFilename(read_info->filename);
188       (void) ImageToFile(image,read_info->filename,exception);
189     }
190   file=ImfOpenInputFile(read_info->filename);
191   if (file == (ImfInputFile *) NULL)
192     {
193       ThrowFileException(exception,BlobError,"UnableToOpenBlob",
194         ImfErrorMessage());
195       if (LocaleCompare(image_info->filename,read_info->filename) != 0)
196         (void) RelinquishUniqueFileResource(read_info->filename);
197       read_info=DestroyImageInfo(read_info);
198       return((Image *) NULL);
199     }
200   hdr_info=ImfInputHeader(file);
201   ImfHeaderDisplayWindow(hdr_info,&min_x,&min_y,&max_x,&max_y);
202   image->columns=max_x-min_x+1UL;
203   image->rows=max_y-min_y+1UL;
204   image->alpha_trait=BlendPixelTrait;
205   SetImageColorspace(image,RGBColorspace,exception);
206   image->gamma=1.0;
207   if (image_info->ping != MagickFalse)
208     {
209       (void) ImfCloseInputFile(file);
210       if (LocaleCompare(image_info->filename,read_info->filename) != 0)
211         (void) RelinquishUniqueFileResource(read_info->filename);
212       read_info=DestroyImageInfo(read_info);
213       (void) CloseBlob(image);
214       return(GetFirstImageInList(image));
215     }
216   status=SetImageExtent(image,image->columns,image->rows,exception);
217   if (status == MagickFalse)
218     return(DestroyImageList(image));
219   scanline=(ImfRgba *) AcquireQuantumMemory(image->columns,sizeof(*scanline));
220   if (scanline == (ImfRgba *) NULL)
221     {
222       (void) ImfCloseInputFile(file);
223       if (LocaleCompare(image_info->filename,read_info->filename) != 0)
224         (void) RelinquishUniqueFileResource(read_info->filename);
225       read_info=DestroyImageInfo(read_info);
226       ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
227     }
228   for (y=0; y < (ssize_t) image->rows; y++)
229   {
230     q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
231     if (q == (Quantum *) NULL)
232       break;
233     ResetMagickMemory(scanline,0,image->columns*sizeof(*scanline));
234     ImfInputSetFrameBuffer(file,scanline-min_x-image->columns*(min_y+y),1,
235       image->columns);
236     ImfInputReadPixels(file,min_y+y,min_y+y);
237     for (x=0; x < (ssize_t) image->columns; x++)
238     {
239       SetPixelRed(image,ClampToQuantum(QuantumRange*
240         ImfHalfToFloat(scanline[x].r)),q);
241       SetPixelGreen(image,ClampToQuantum(QuantumRange*
242         ImfHalfToFloat(scanline[x].g)),q);
243       SetPixelBlue(image,ClampToQuantum(QuantumRange*
244         ImfHalfToFloat(scanline[x].b)),q);
245       SetPixelAlpha(image,ClampToQuantum(QuantumRange*
246         ImfHalfToFloat(scanline[x].a)),q);
247       q+=GetPixelChannels(image);
248     }
249     if (SyncAuthenticPixels(image,exception) == MagickFalse)
250       break;
251   }
252   scanline=(ImfRgba *) RelinquishMagickMemory(scanline);
253   (void) ImfCloseInputFile(file);
254   if (LocaleCompare(image_info->filename,read_info->filename) != 0)
255     (void) RelinquishUniqueFileResource(read_info->filename);
256   read_info=DestroyImageInfo(read_info);
257   (void) CloseBlob(image);
258   return(GetFirstImageInList(image));
259 }
260 #endif
261 \f
262 /*
263 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
264 %                                                                             %
265 %                                                                             %
266 %                                                                             %
267 %   R e g i s t e r E X R I m a g e                                           %
268 %                                                                             %
269 %                                                                             %
270 %                                                                             %
271 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
272 %
273 %  RegisterEXRImage() adds properties for the EXR image format
274 %  to the list of supported formats.  The properties include the image format
275 %  tag, a method to read and/or write the format, whether the format
276 %  supports the saving of more than one frame to the same file or blob,
277 %  whether the format supports native in-memory I/O, and a brief
278 %  description of the format.
279 %
280 %  The format of the RegisterEXRImage method is:
281 %
282 %      size_t RegisterEXRImage(void)
283 %
284 */
285 ModuleExport size_t RegisterEXRImage(void)
286 {
287   MagickInfo
288     *entry;
289
290   entry=SetMagickInfo("EXR");
291 #if defined(MAGICKCORE_OPENEXR_DELEGATE)
292   entry->decoder=(DecodeImageHandler *) ReadEXRImage;
293   entry->encoder=(EncodeImageHandler *) WriteEXRImage;
294 #endif
295   entry->magick=(IsImageFormatHandler *) IsEXR;
296   entry->flags^=Adjoin;
297   entry->description=ConstantString("High Dynamic-range (HDR)");
298   entry->flags^=BlobSupport;
299   entry->module=ConstantString("EXR");
300   (void) RegisterMagickInfo(entry);
301   return(MagickImageCoderSignature);
302 }
303 \f
304 /*
305 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
306 %                                                                             %
307 %                                                                             %
308 %                                                                             %
309 %   U n r e g i s t e r E X R I m a g e                                       %
310 %                                                                             %
311 %                                                                             %
312 %                                                                             %
313 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
314 %
315 %  UnregisterEXRImage() removes format registrations made by the
316 %  EXR module from the list of supported formats.
317 %
318 %  The format of the UnregisterEXRImage method is:
319 %
320 %      UnregisterEXRImage(void)
321 %
322 */
323 ModuleExport void UnregisterEXRImage(void)
324 {
325   (void) UnregisterMagickInfo("EXR");
326 }
327 \f
328 #if defined(MAGICKCORE_OPENEXR_DELEGATE)
329 /*
330 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
331 %                                                                             %
332 %                                                                             %
333 %                                                                             %
334 %   W r i t e E X R I m a g e                                                 %
335 %                                                                             %
336 %                                                                             %
337 %                                                                             %
338 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
339 %
340 %  WriteEXRImage() writes an image to a file the in the high dynamic-range
341 %  (HDR) file format developed by Industrial Light & Magic.
342 %
343 %  The format of the WriteEXRImage method is:
344 %
345 %      MagickBooleanType WriteEXRImage(const ImageInfo *image_info,
346 %        Image *image,ExceptionInfo *exception)
347 %
348 %  A description of each parameter follows.
349 %
350 %    o image_info: the image info.
351 %
352 %    o image:  The image.
353 %
354 %    o exception: return any errors or warnings in this structure.
355 %
356 */
357 static MagickBooleanType WriteEXRImage(const ImageInfo *image_info,Image *image,
358   ExceptionInfo *exception)
359 {
360   ImageInfo
361     *write_info;
362
363   ImfHalf
364     half_quantum;
365
366   ImfHeader
367     *hdr_info;
368
369   ImfOutputFile
370     *file;
371
372   ImfRgba
373     *scanline;
374
375   int
376     compression;
377
378   MagickBooleanType
379     status;
380
381   register const Quantum
382     *p;
383
384   register ssize_t
385     x;
386
387   ssize_t
388     y;
389
390   /*
391     Open output image file.
392   */
393   assert(image_info != (const ImageInfo *) NULL);
394   assert(image_info->signature == MagickSignature);
395   assert(image != (Image *) NULL);
396   assert(image->signature == MagickSignature);
397   if (image->debug != MagickFalse)
398     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
399   assert(exception != (ExceptionInfo *) NULL);
400   assert(exception->signature == MagickSignature);
401   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
402   if (status == MagickFalse)
403     return(status);
404   (void) SetImageColorspace(image,RGBColorspace,exception);
405   write_info=CloneImageInfo(image_info);
406   (void) AcquireUniqueFilename(write_info->filename);
407   hdr_info=ImfNewHeader();
408   ImfHeaderSetDataWindow(hdr_info,0,0,(int) image->columns-1,(int)
409     image->rows-1);
410   ImfHeaderSetDisplayWindow(hdr_info,0,0,(int) image->columns-1,(int)
411     image->rows-1);
412   compression=IMF_NO_COMPRESSION;
413   if (write_info->compression == ZipSCompression)
414     compression=IMF_ZIPS_COMPRESSION;
415   if (write_info->compression == ZipCompression)
416     compression=IMF_ZIP_COMPRESSION;
417   if (write_info->compression == PizCompression)
418     compression=IMF_PIZ_COMPRESSION;
419   if (write_info->compression == Pxr24Compression)
420     compression=IMF_PXR24_COMPRESSION;
421 #if defined(B44Compression)
422   if (write_info->compression == B44Compression)
423     compression=IMF_B44_COMPRESSION;
424 #endif
425 #if defined(B44ACompression)
426   if (write_info->compression == B44ACompression)
427     compression=IMF_B44A_COMPRESSION;
428 #endif
429   ImfHeaderSetCompression(hdr_info,compression);
430   ImfHeaderSetLineOrder(hdr_info,IMF_INCREASING_Y);
431   file=ImfOpenOutputFile(write_info->filename,hdr_info,IMF_WRITE_RGBA);
432   ImfDeleteHeader(hdr_info);
433   if (file == (ImfOutputFile *) NULL)
434     {
435       (void) RelinquishUniqueFileResource(write_info->filename);
436       write_info=DestroyImageInfo(write_info);
437       ThrowFileException(exception,BlobError,"UnableToOpenBlob",
438         ImfErrorMessage());
439       return(MagickFalse);
440     }
441   scanline=(ImfRgba *) AcquireQuantumMemory(image->columns,sizeof(*scanline));
442   if (scanline == (ImfRgba *) NULL)
443     {
444       (void) ImfCloseOutputFile(file);
445       (void) RelinquishUniqueFileResource(write_info->filename);
446       write_info=DestroyImageInfo(write_info);
447       ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
448     }
449   ResetMagickMemory(scanline,0,image->columns*sizeof(*scanline));
450   for (y=0; y < (ssize_t) image->rows; y++)
451   {
452     p=GetVirtualPixels(image,0,y,image->columns,1,exception);
453     if (p == (const Quantum *) NULL)
454       break;
455     for (x=0; x < (ssize_t) image->columns; x++)
456     {
457       ImfFloatToHalf(QuantumScale*GetPixelRed(image,p),&half_quantum);
458       scanline[x].r=half_quantum;
459       ImfFloatToHalf(QuantumScale*GetPixelGreen(image,p),&half_quantum);
460       scanline[x].g=half_quantum;
461       ImfFloatToHalf(QuantumScale*GetPixelBlue(image,p),&half_quantum);
462       scanline[x].b=half_quantum;
463       if (image->alpha_trait == UndefinedPixelTrait)
464         ImfFloatToHalf(1.0,&half_quantum);
465       else
466         ImfFloatToHalf(QuantumScale*GetPixelAlpha(image,p),&half_quantum);
467       scanline[x].a=half_quantum;
468       p+=GetPixelChannels(image);
469     }
470     ImfOutputSetFrameBuffer(file,scanline-(y*image->columns),1,image->columns);
471     ImfOutputWritePixels(file,1);
472   }
473   (void) ImfCloseOutputFile(file);
474   scanline=(ImfRgba *) RelinquishMagickMemory(scanline);
475   (void) FileToImage(image,write_info->filename,exception);
476   (void) RelinquishUniqueFileResource(write_info->filename);
477   write_info=DestroyImageInfo(write_info);
478   (void) CloseBlob(image);
479   return(MagickTrue);
480 }
481 #endif