2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write High Dynamic-Range (HDR) Image File Format %
20 % Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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>
67 static MagickBooleanType
68 WriteEXRImage(const ImageInfo *,Image *,ExceptionInfo *);
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 % IsEXR() returns MagickTrue if the image format type, identified by the
83 % magick string, is EXR.
85 % The format of the IsEXR method is:
87 % MagickBooleanType IsEXR(const unsigned char *magick,const size_t length)
89 % A description of each parameter follows:
91 % o magick: compare image format pattern against these bytes.
93 % o length: Specifies the length of the magick string.
96 static MagickBooleanType IsEXR(const unsigned char *magick,const size_t length)
100 if (memcmp(magick,"\166\057\061\001",4) == 0)
105 #if defined(MAGICKCORE_OPENEXR_DELEGATE)
107 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111 % R e a d E X R I m a g e %
115 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
121 % The format of the ReadEXRImage method is:
123 % Image *ReadEXRImage(const ImageInfo *image_info,ExceptionInfo *exception)
125 % A description of each parameter follows:
127 % o image_info: the image info.
129 % o exception: return any errors or warnings in this structure.
132 static Image *ReadEXRImage(const ImageInfo *image_info,ExceptionInfo *exception)
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)
181 image=DestroyImageList(image);
182 return((Image *) NULL);
184 read_info=CloneImageInfo(image_info);
185 if (IsPathAccessible(read_info->filename) == MagickFalse)
187 (void) AcquireUniqueFilename(read_info->filename);
188 (void) ImageToFile(image,read_info->filename,exception);
190 file=ImfOpenInputFile(read_info->filename);
191 if (file == (ImfInputFile *) NULL)
193 ThrowFileException(exception,BlobError,"UnableToOpenBlob",
195 read_info=DestroyImageInfo(read_info);
196 return((Image *) NULL);
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);
205 if (image_info->ping != MagickFalse)
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));
214 scanline=(ImfRgba *) AcquireQuantumMemory(image->columns,sizeof(*scanline));
215 if (scanline == (ImfRgba *) NULL)
217 (void) ImfCloseInputFile(file);
218 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
220 for (y=0; y < (ssize_t) image->rows; y++)
222 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
223 if (q == (Quantum *) NULL)
225 ResetMagickMemory(scanline,0,image->columns*sizeof(*scanline));
226 ImfInputSetFrameBuffer(file,scanline-min_x-image->columns*(min_y+y),1,
228 ImfInputReadPixels(file,min_y+y,min_y+y);
229 for (x=0; x < (ssize_t) image->columns; x++)
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);
241 if (SyncAuthenticPixels(image,exception) == MagickFalse)
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));
255 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
259 % R e g i s t e r E X R I m a g e %
263 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
272 % The format of the RegisterEXRImage method is:
274 % size_t RegisterEXRImage(void)
277 ModuleExport size_t RegisterEXRImage(void)
282 entry=SetMagickInfo("EXR");
283 #if defined(MAGICKCORE_OPENEXR_DELEGATE)
284 entry->decoder=(DecodeImageHandler *) ReadEXRImage;
285 entry->encoder=(EncodeImageHandler *) WriteEXRImage;
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);
297 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
301 % U n r e g i s t e r E X R I m a g e %
305 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
307 % UnregisterEXRImage() removes format registrations made by the
308 % EXR module from the list of supported formats.
310 % The format of the UnregisterEXRImage method is:
312 % UnregisterEXRImage(void)
315 ModuleExport void UnregisterEXRImage(void)
317 (void) UnregisterMagickInfo("EXR");
320 #if defined(MAGICKCORE_OPENEXR_DELEGATE)
322 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
326 % W r i t e E X R I m a g e %
330 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
332 % WriteEXRImage() writes an image to a file the in the high dynamic-range
333 % (HDR) file format developed by Industrial Light & Magic.
335 % The format of the WriteEXRImage method is:
337 % MagickBooleanType WriteEXRImage(const ImageInfo *image_info,
338 % Image *image,ExceptionInfo *exception)
340 % A description of each parameter follows.
342 % o image_info: the image info.
344 % o image: The image.
346 % o exception: return any errors or warnings in this structure.
349 static MagickBooleanType WriteEXRImage(const ImageInfo *image_info,Image *image,
350 ExceptionInfo *exception)
373 register const Quantum
383 Open output image file.
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)
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)
402 ImfHeaderSetDisplayWindow(hdr_info,0,0,(int) image->columns-1,(int)
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;
417 #if defined(B44ACompression)
418 if (write_info->compression == B44ACompression)
419 compression=IMF_B44A_COMPRESSION;
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)
427 ThrowFileException(exception,BlobError,"UnableToOpenBlob",
429 write_info=DestroyImageInfo(write_info);
432 scanline=(ImfRgba *) AcquireQuantumMemory(image->columns,sizeof(*scanline));
433 if (scanline == (ImfRgba *) NULL)
435 (void) ImfCloseOutputFile(file);
436 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
438 ResetMagickMemory(scanline,0,image->columns*sizeof(*scanline));
439 for (y=0; y < (ssize_t) image->rows; y++)
441 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
442 if (p == (const Quantum *) NULL)
444 for (x=0; x < (ssize_t) image->columns; x++)
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);
455 ImfFloatToHalf(QuantumScale*GetPixelAlpha(image,p),&half_quantum);
456 scanline[x].a=half_quantum;
457 p+=GetPixelChannels(image);
459 ImfOutputSetFrameBuffer(file,scanline-(y*image->columns),1,image->columns);
460 ImfOutputWritePixels(file,1);
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);