blob: 7e61d8936a301d111a78bd2de04845b314ebb5ec [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
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% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% 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
39/*
40 Include declarations.
41*/
cristy4c08aed2011-07-01 19:47:50 +000042#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"
cristy3ed852e2009-09-05 21:47:34 +000061#if defined(MAGICKCORE_OPENEXR_DELEGATE)
62#include <ImfCRgbaFile.h>
63
64/*
65 Forward declarations.
66*/
67static MagickBooleanType
cristy1e178e72011-08-28 19:44:34 +000068 WriteEXRImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +000069#endif
70
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*/
96static 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
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*/
132static 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
cristy202de442011-04-24 18:19:07 +0000155 MagickBooleanType
156 status;
cristy3ed852e2009-09-05 21:47:34 +0000157
cristybb503372010-05-27 20:51:26 +0000158 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000159 x;
160
cristy4c08aed2011-07-01 19:47:50 +0000161 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000162 *q;
163
cristy202de442011-04-24 18:19:07 +0000164 ssize_t
165 y;
cristy3ed852e2009-09-05 21:47:34 +0000166
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);
cristy9950d572011-10-01 18:22:35 +0000177 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000178 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);
cristy1ff6c472012-03-21 11:39:04 +0000199 ImfHeaderDisplayWindow(hdr_info,&min_x,&min_y,&max_x,&max_y);
cristy3ed852e2009-09-05 21:47:34 +0000200 image->columns=max_x-min_x+1UL;
201 image->rows=max_y-min_y+1UL;
202 image->matte=MagickTrue;
cristye2c4f182012-05-12 14:11:53 +0000203 SetImageColorspace(image,RGBColorspace,exception);
cristy65147832012-05-01 10:57:51 +0000204 image->gamma=1.0;
cristy3ed852e2009-09-05 21:47:34 +0000205 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 }
cristybb503372010-05-27 20:51:26 +0000220 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000221 {
222 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000223 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000224 break;
225 ImfInputSetFrameBuffer(file,scanline-min_x-image->columns*(min_y+y),1,
226 image->columns);
227 ImfInputReadPixels(file,min_y+y,min_y+y);
cristybb503372010-05-27 20:51:26 +0000228 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000229 {
cristy4c08aed2011-07-01 19:47:50 +0000230 SetPixelRed(image,ClampToQuantum((MagickRealType) QuantumRange*
231 ImfHalfToFloat(scanline[x].r)),q);
232 SetPixelGreen(image,ClampToQuantum((MagickRealType) QuantumRange*
233 ImfHalfToFloat(scanline[x].g)),q);
234 SetPixelBlue(image,ClampToQuantum((MagickRealType) QuantumRange*
235 ImfHalfToFloat(scanline[x].b)),q);
236 SetPixelAlpha(image,ClampToQuantum((MagickRealType) QuantumRange*
237 ImfHalfToFloat(scanline[x].a)),q);
cristyed231572011-07-14 02:18:59 +0000238 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000239 }
240 if (SyncAuthenticPixels(image,exception) == MagickFalse)
241 break;
242 }
243 scanline=(ImfRgba *) RelinquishMagickMemory(scanline);
244 (void) ImfCloseInputFile(file);
245 if (LocaleCompare(image_info->filename,read_info->filename) != 0)
246 (void) RelinquishUniqueFileResource(read_info->filename);
247 read_info=DestroyImageInfo(read_info);
248 (void) CloseBlob(image);
249 return(GetFirstImageInList(image));
250}
251#endif
252
253/*
254%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
255% %
256% %
257% %
258% R e g i s t e r E X R I m a g e %
259% %
260% %
261% %
262%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
263%
264% RegisterEXRImage() adds properties for the EXR image format
265% to the list of supported formats. The properties include the image format
266% tag, a method to read and/or write the format, whether the format
267% supports the saving of more than one frame to the same file or blob,
268% whether the format supports native in-memory I/O, and a brief
269% description of the format.
270%
271% The format of the RegisterEXRImage method is:
272%
cristybb503372010-05-27 20:51:26 +0000273% size_t RegisterEXRImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000274%
275*/
cristybb503372010-05-27 20:51:26 +0000276ModuleExport size_t RegisterEXRImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000277{
278 MagickInfo
279 *entry;
280
281 entry=SetMagickInfo("EXR");
282#if defined(MAGICKCORE_OPENEXR_DELEGATE)
283 entry->decoder=(DecodeImageHandler *) ReadEXRImage;
284 entry->encoder=(EncodeImageHandler *) WriteEXRImage;
285#endif
286 entry->magick=(IsImageFormatHandler *) IsEXR;
287 entry->adjoin=MagickFalse;
288 entry->description=ConstantString("High Dynamic-range (HDR)");
289 entry->blob_support=MagickFalse;
290 entry->module=ConstantString("EXR");
291 (void) RegisterMagickInfo(entry);
292 return(MagickImageCoderSignature);
293}
294
295/*
296%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
297% %
298% %
299% %
300% U n r e g i s t e r E X R I m a g e %
301% %
302% %
303% %
304%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
305%
306% UnregisterEXRImage() removes format registrations made by the
307% EXR module from the list of supported formats.
308%
309% The format of the UnregisterEXRImage method is:
310%
311% UnregisterEXRImage(void)
312%
313*/
314ModuleExport void UnregisterEXRImage(void)
315{
316 (void) UnregisterMagickInfo("EXR");
317}
318
319#if defined(MAGICKCORE_OPENEXR_DELEGATE)
320/*
321%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
322% %
323% %
324% %
325% W r i t e E X R I m a g e %
326% %
327% %
328% %
329%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
330%
331% WriteEXRImage() writes an image to a file the in the high dynamic-range
332% (HDR) file format developed by Industrial Light & Magic.
333%
334% The format of the WriteEXRImage method is:
335%
cristy1e178e72011-08-28 19:44:34 +0000336% MagickBooleanType WriteEXRImage(const ImageInfo *image_info,
337% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000338%
339% A description of each parameter follows.
340%
341% o image_info: the image info.
342%
343% o image: The image.
344%
cristy1e178e72011-08-28 19:44:34 +0000345% o exception: return any errors or warnings in this structure.
346%
cristy3ed852e2009-09-05 21:47:34 +0000347*/
cristy1e178e72011-08-28 19:44:34 +0000348static MagickBooleanType WriteEXRImage(const ImageInfo *image_info,Image *image,
349 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000350{
351 ImageInfo
352 *write_info;
353
354 ImfHalf
355 half_quantum;
356
357 ImfHeader
358 *hdr_info;
359
360 ImfOutputFile
361 *file;
362
363 ImfRgba
364 *scanline;
365
366 int
367 compression;
368
cristy3ed852e2009-09-05 21:47:34 +0000369 MagickBooleanType
370 status;
371
cristy4c08aed2011-07-01 19:47:50 +0000372 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000373 *p;
374
cristybb503372010-05-27 20:51:26 +0000375 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000376 x;
377
cristy202de442011-04-24 18:19:07 +0000378 ssize_t
379 y;
380
cristy3ed852e2009-09-05 21:47:34 +0000381 /*
382 Open output image file.
383 */
384 assert(image_info != (const ImageInfo *) NULL);
385 assert(image_info->signature == MagickSignature);
386 assert(image != (Image *) NULL);
387 assert(image->signature == MagickSignature);
388 if (image->debug != MagickFalse)
389 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +0000390 assert(exception != (ExceptionInfo *) NULL);
391 assert(exception->signature == MagickSignature);
cristy1e178e72011-08-28 19:44:34 +0000392 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000393 if (status == MagickFalse)
394 return(status);
cristyd74fdd02012-05-04 10:35:02 +0000395 (void) SetImageColorspace(image,RGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000396 write_info=CloneImageInfo(image_info);
397 (void) AcquireUniqueFilename(write_info->filename);
398 hdr_info=ImfNewHeader();
399 ImfHeaderSetDataWindow(hdr_info,0,0,(int) image->columns-1,(int)
400 image->rows-1);
401 ImfHeaderSetDisplayWindow(hdr_info,0,0,(int) image->columns-1,(int)
402 image->rows-1);
403 compression=IMF_NO_COMPRESSION;
404 if (write_info->compression == ZipSCompression)
405 compression=IMF_ZIPS_COMPRESSION;
406 if (write_info->compression == ZipCompression)
407 compression=IMF_ZIP_COMPRESSION;
408 if (write_info->compression == PizCompression)
409 compression=IMF_PIZ_COMPRESSION;
410 if (write_info->compression == Pxr24Compression)
411 compression=IMF_PXR24_COMPRESSION;
412#if defined(B44Compression)
413 if (write_info->compression == B44Compression)
414 compression=IMF_B44_COMPRESSION;
415#endif
416#if defined(B44ACompression)
417 if (write_info->compression == B44ACompression)
418 compression=IMF_B44A_COMPRESSION;
419#endif
420 ImfHeaderSetCompression(hdr_info,compression);
421 ImfHeaderSetLineOrder(hdr_info,IMF_INCREASING_Y);
422 file=ImfOpenOutputFile(write_info->filename,hdr_info,IMF_WRITE_RGBA);
423 ImfDeleteHeader(hdr_info);
424 if (file == (ImfOutputFile *) NULL)
425 {
cristy1e178e72011-08-28 19:44:34 +0000426 ThrowFileException(exception,BlobError,"UnableToOpenBlob",
cristy3ed852e2009-09-05 21:47:34 +0000427 ImfErrorMessage());
428 write_info=DestroyImageInfo(write_info);
429 return(MagickFalse);
430 }
431 scanline=(ImfRgba *) AcquireQuantumMemory(image->columns,sizeof(*scanline));
432 if (scanline == (ImfRgba *) NULL)
433 {
434 (void) ImfCloseOutputFile(file);
435 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
436 }
cristybb503372010-05-27 20:51:26 +0000437 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000438 {
cristy1e178e72011-08-28 19:44:34 +0000439 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000440 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000441 break;
cristybb503372010-05-27 20:51:26 +0000442 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000443 {
cristy4c08aed2011-07-01 19:47:50 +0000444 ImfFloatToHalf(QuantumScale*GetPixelRed(image,p),&half_quantum);
cristy3ed852e2009-09-05 21:47:34 +0000445 scanline[x].r=half_quantum;
cristy4c08aed2011-07-01 19:47:50 +0000446 ImfFloatToHalf(QuantumScale*GetPixelGreen(image,p),&half_quantum);
cristy3ed852e2009-09-05 21:47:34 +0000447 scanline[x].g=half_quantum;
cristy4c08aed2011-07-01 19:47:50 +0000448 ImfFloatToHalf(QuantumScale*GetPixelBlue(image,p),&half_quantum);
cristy3ed852e2009-09-05 21:47:34 +0000449 scanline[x].b=half_quantum;
450 if (image->matte == MagickFalse)
451 ImfFloatToHalf(1.0,&half_quantum);
452 else
cristy4c08aed2011-07-01 19:47:50 +0000453 ImfFloatToHalf(QuantumScale*GetPixelAlpha(image,p),&half_quantum);
cristy3ed852e2009-09-05 21:47:34 +0000454 scanline[x].a=half_quantum;
cristyed231572011-07-14 02:18:59 +0000455 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000456 }
457 ImfOutputSetFrameBuffer(file,scanline-(y*image->columns),1,image->columns);
458 ImfOutputWritePixels(file,1);
459 }
460 (void) ImfCloseOutputFile(file);
461 scanline=(ImfRgba *) RelinquishMagickMemory(scanline);
cristyc82a27b2011-10-21 01:07:16 +0000462 (void) FileToImage(image,write_info->filename,exception);
cristy3ed852e2009-09-05 21:47:34 +0000463 (void) RelinquishUniqueFileResource(write_info->filename);
464 write_info=DestroyImageInfo(write_info);
465 (void) CloseBlob(image);
466 return(MagickTrue);
467}
468#endif