]> granicus.if.org Git - imagemagick/blob - coders/exr.c
ce914f24b8d4e54a20ad4ac325d6f0cc3af763e5
[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 %                                John Cristy                                  %
17 %                                 April 2007                                  %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2014 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       read_info=DestroyImageInfo(read_info);
196       return((Image *) NULL);
197     }
198   hdr_info=ImfInputHeader(file);
199   ImfHeaderDisplayWindow(hdr_info,&min_x,&min_y,&max_x,&max_y);
200   image->columns=max_x-min_x+1UL;
201   image->rows=max_y-min_y+1UL;
202   image->alpha_trait=BlendPixelTrait;
203   SetImageColorspace(image,RGBColorspace,exception);
204   image->gamma=1.0;
205   if (image_info->ping != MagickFalse)
206     {
207       (void) ImfCloseInputFile(file);
208       if (LocaleCompare(image_info->filename,read_info->filename) != 0)
209         (void) RelinquishUniqueFileResource(read_info->filename);
210       read_info=DestroyImageInfo(read_info);
211       (void) CloseBlob(image);
212       return(GetFirstImageInList(image));
213     }
214   scanline=(ImfRgba *) AcquireQuantumMemory(image->columns,sizeof(*scanline));
215   if (scanline == (ImfRgba *) NULL)
216     {
217       (void) ImfCloseInputFile(file);
218       ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
219     }
220   for (y=0; y < (ssize_t) image->rows; y++)
221   {
222     q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
223     if (q == (Quantum *) NULL)
224       break;
225     ResetMagickMemory(scanline,0,image->columns*sizeof(*scanline));
226     ImfInputSetFrameBuffer(file,scanline-min_x-image->columns*(min_y+y),1,
227       image->columns);
228     ImfInputReadPixels(file,min_y+y,min_y+y);
229     for (x=0; x < (ssize_t) image->columns; x++)
230     {
231       SetPixelRed(image,ClampToQuantum(QuantumRange*
232         ImfHalfToFloat(scanline[x].r)),q);
233       SetPixelGreen(image,ClampToQuantum(QuantumRange*
234         ImfHalfToFloat(scanline[x].g)),q);
235       SetPixelBlue(image,ClampToQuantum(QuantumRange*
236         ImfHalfToFloat(scanline[x].b)),q);
237       SetPixelAlpha(image,ClampToQuantum(QuantumRange*
238         ImfHalfToFloat(scanline[x].a)),q);
239       q+=GetPixelChannels(image);
240     }
241     if (SyncAuthenticPixels(image,exception) == MagickFalse)
242       break;
243   }
244   scanline=(ImfRgba *) RelinquishMagickMemory(scanline);
245   (void) ImfCloseInputFile(file);
246   if (LocaleCompare(image_info->filename,read_info->filename) != 0)
247     (void) RelinquishUniqueFileResource(read_info->filename);
248   read_info=DestroyImageInfo(read_info);
249   (void) CloseBlob(image);
250   return(GetFirstImageInList(image));
251 }
252 #endif
253 \f
254 /*
255 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
256 %                                                                             %
257 %                                                                             %
258 %                                                                             %
259 %   R e g i s t e r E X R I m a g e                                           %
260 %                                                                             %
261 %                                                                             %
262 %                                                                             %
263 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
264 %
265 %  RegisterEXRImage() adds properties for the EXR image format
266 %  to the list of supported formats.  The properties include the image format
267 %  tag, a method to read and/or write the format, whether the format
268 %  supports the saving of more than one frame to the same file or blob,
269 %  whether the format supports native in-memory I/O, and a brief
270 %  description of the format.
271 %
272 %  The format of the RegisterEXRImage method is:
273 %
274 %      size_t RegisterEXRImage(void)
275 %
276 */
277 ModuleExport size_t RegisterEXRImage(void)
278 {
279   MagickInfo
280     *entry;
281
282   entry=SetMagickInfo("EXR");
283 #if defined(MAGICKCORE_OPENEXR_DELEGATE)
284   entry->decoder=(DecodeImageHandler *) ReadEXRImage;
285   entry->encoder=(EncodeImageHandler *) WriteEXRImage;
286 #endif
287   entry->magick=(IsImageFormatHandler *) IsEXR;
288   entry->adjoin=MagickFalse;
289   entry->description=ConstantString("High Dynamic-range (HDR)");
290   entry->blob_support=MagickFalse;
291   entry->module=ConstantString("EXR");
292   (void) RegisterMagickInfo(entry);
293   return(MagickImageCoderSignature);
294 }
295 \f
296 /*
297 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
298 %                                                                             %
299 %                                                                             %
300 %                                                                             %
301 %   U n r e g i s t e r E X R I m a g e                                       %
302 %                                                                             %
303 %                                                                             %
304 %                                                                             %
305 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
306 %
307 %  UnregisterEXRImage() removes format registrations made by the
308 %  EXR module from the list of supported formats.
309 %
310 %  The format of the UnregisterEXRImage method is:
311 %
312 %      UnregisterEXRImage(void)
313 %
314 */
315 ModuleExport void UnregisterEXRImage(void)
316 {
317   (void) UnregisterMagickInfo("EXR");
318 }
319 \f
320 #if defined(MAGICKCORE_OPENEXR_DELEGATE)
321 /*
322 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
323 %                                                                             %
324 %                                                                             %
325 %                                                                             %
326 %   W r i t e E X R I m a g e                                                 %
327 %                                                                             %
328 %                                                                             %
329 %                                                                             %
330 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
331 %
332 %  WriteEXRImage() writes an image to a file the in the high dynamic-range
333 %  (HDR) file format developed by Industrial Light & Magic.
334 %
335 %  The format of the WriteEXRImage method is:
336 %
337 %      MagickBooleanType WriteEXRImage(const ImageInfo *image_info,
338 %        Image *image,ExceptionInfo *exception)
339 %
340 %  A description of each parameter follows.
341 %
342 %    o image_info: the image info.
343 %
344 %    o image:  The image.
345 %
346 %    o exception: return any errors or warnings in this structure.
347 %
348 */
349 static MagickBooleanType WriteEXRImage(const ImageInfo *image_info,Image *image,
350   ExceptionInfo *exception)
351 {
352   ImageInfo
353     *write_info;
354
355   ImfHalf
356     half_quantum;
357
358   ImfHeader
359     *hdr_info;
360
361   ImfOutputFile
362     *file;
363
364   ImfRgba
365     *scanline;
366
367   int
368     compression;
369
370   MagickBooleanType
371     status;
372
373   register const Quantum
374     *p;
375
376   register ssize_t
377     x;
378
379   ssize_t
380     y;
381
382   /*
383     Open output image file.
384   */
385   assert(image_info != (const ImageInfo *) NULL);
386   assert(image_info->signature == MagickSignature);
387   assert(image != (Image *) NULL);
388   assert(image->signature == MagickSignature);
389   if (image->debug != MagickFalse)
390     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
391   assert(exception != (ExceptionInfo *) NULL);
392   assert(exception->signature == MagickSignature);
393   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
394   if (status == MagickFalse)
395     return(status);
396   (void) SetImageColorspace(image,RGBColorspace,exception);
397   write_info=CloneImageInfo(image_info);
398   (void) AcquireUniqueFilename(write_info->filename);
399   hdr_info=ImfNewHeader();
400   ImfHeaderSetDataWindow(hdr_info,0,0,(int) image->columns-1,(int)
401     image->rows-1);
402   ImfHeaderSetDisplayWindow(hdr_info,0,0,(int) image->columns-1,(int)
403     image->rows-1);
404   compression=IMF_NO_COMPRESSION;
405   if (write_info->compression == ZipSCompression)
406     compression=IMF_ZIPS_COMPRESSION;
407   if (write_info->compression == ZipCompression)
408     compression=IMF_ZIP_COMPRESSION;
409   if (write_info->compression == PizCompression)
410     compression=IMF_PIZ_COMPRESSION;
411   if (write_info->compression == Pxr24Compression)
412     compression=IMF_PXR24_COMPRESSION;
413 #if defined(B44Compression)
414   if (write_info->compression == B44Compression)
415     compression=IMF_B44_COMPRESSION;
416 #endif
417 #if defined(B44ACompression)
418   if (write_info->compression == B44ACompression)
419     compression=IMF_B44A_COMPRESSION;
420 #endif
421   ImfHeaderSetCompression(hdr_info,compression);
422   ImfHeaderSetLineOrder(hdr_info,IMF_INCREASING_Y);
423   file=ImfOpenOutputFile(write_info->filename,hdr_info,IMF_WRITE_RGBA);
424   ImfDeleteHeader(hdr_info);
425   if (file == (ImfOutputFile *) NULL)
426     {
427       ThrowFileException(exception,BlobError,"UnableToOpenBlob",
428         ImfErrorMessage());
429       write_info=DestroyImageInfo(write_info);
430       return(MagickFalse);
431     }
432   scanline=(ImfRgba *) AcquireQuantumMemory(image->columns,sizeof(*scanline));
433   if (scanline == (ImfRgba *) NULL)
434     {
435       (void) ImfCloseOutputFile(file);
436       ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
437     }
438   ResetMagickMemory(scanline,0,image->columns*sizeof(*scanline));
439   for (y=0; y < (ssize_t) image->rows; y++)
440   {
441     p=GetVirtualPixels(image,0,y,image->columns,1,exception);
442     if (p == (const Quantum *) NULL)
443       break;
444     for (x=0; x < (ssize_t) image->columns; x++)
445     {
446       ImfFloatToHalf(QuantumScale*GetPixelRed(image,p),&half_quantum);
447       scanline[x].r=half_quantum;
448       ImfFloatToHalf(QuantumScale*GetPixelGreen(image,p),&half_quantum);
449       scanline[x].g=half_quantum;
450       ImfFloatToHalf(QuantumScale*GetPixelBlue(image,p),&half_quantum);
451       scanline[x].b=half_quantum;
452       if (image->alpha_trait != BlendPixelTrait)
453         ImfFloatToHalf(1.0,&half_quantum);
454       else
455         ImfFloatToHalf(QuantumScale*GetPixelAlpha(image,p),&half_quantum);
456       scanline[x].a=half_quantum;
457       p+=GetPixelChannels(image);
458     }
459     ImfOutputSetFrameBuffer(file,scanline-(y*image->columns),1,image->columns);
460     ImfOutputWritePixels(file,1);
461   }
462   (void) ImfCloseOutputFile(file);
463   scanline=(ImfRgba *) RelinquishMagickMemory(scanline);
464   (void) FileToImage(image,write_info->filename,exception);
465   (void) RelinquishUniqueFileResource(write_info->filename);
466   write_info=DestroyImageInfo(write_info);
467   (void) CloseBlob(image);
468   return(MagickTrue);
469 }
470 #endif