2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write High Dynamic-Range (HDR) Image File Format %
20 % Copyright 1999-2011 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 *);
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);
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 ImfHeaderDataWindow(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->matte=MagickTrue;
203 if (image_info->ping != MagickFalse)
205 (void) ImfCloseInputFile(file);
206 if (LocaleCompare(image_info->filename,read_info->filename) != 0)
207 (void) RelinquishUniqueFileResource(read_info->filename);
208 read_info=DestroyImageInfo(read_info);
209 (void) CloseBlob(image);
210 return(GetFirstImageInList(image));
212 scanline=(ImfRgba *) AcquireQuantumMemory(image->columns,sizeof(*scanline));
213 if (scanline == (ImfRgba *) NULL)
215 (void) ImfCloseInputFile(file);
216 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
218 for (y=0; y < (ssize_t) image->rows; y++)
220 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
221 if (q == (const Quantum *) NULL)
223 ImfInputSetFrameBuffer(file,scanline-min_x-image->columns*(min_y+y),1,
225 ImfInputReadPixels(file,min_y+y,min_y+y);
226 for (x=0; x < (ssize_t) image->columns; x++)
228 SetPixelRed(image,ClampToQuantum((MagickRealType) QuantumRange*
229 ImfHalfToFloat(scanline[x].r)),q);
230 SetPixelGreen(image,ClampToQuantum((MagickRealType) QuantumRange*
231 ImfHalfToFloat(scanline[x].g)),q);
232 SetPixelBlue(image,ClampToQuantum((MagickRealType) QuantumRange*
233 ImfHalfToFloat(scanline[x].b)),q);
234 SetPixelAlpha(image,ClampToQuantum((MagickRealType) QuantumRange*
235 ImfHalfToFloat(scanline[x].a)),q);
236 q+=GetPixelChannels(image);
238 if (SyncAuthenticPixels(image,exception) == MagickFalse)
241 scanline=(ImfRgba *) RelinquishMagickMemory(scanline);
242 (void) ImfCloseInputFile(file);
243 if (LocaleCompare(image_info->filename,read_info->filename) != 0)
244 (void) RelinquishUniqueFileResource(read_info->filename);
245 read_info=DestroyImageInfo(read_info);
246 (void) CloseBlob(image);
247 return(GetFirstImageInList(image));
252 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
256 % R e g i s t e r E X R I m a g e %
260 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
262 % RegisterEXRImage() adds properties for the EXR image format
263 % to the list of supported formats. The properties include the image format
264 % tag, a method to read and/or write the format, whether the format
265 % supports the saving of more than one frame to the same file or blob,
266 % whether the format supports native in-memory I/O, and a brief
267 % description of the format.
269 % The format of the RegisterEXRImage method is:
271 % size_t RegisterEXRImage(void)
274 ModuleExport size_t RegisterEXRImage(void)
279 entry=SetMagickInfo("EXR");
280 #if defined(MAGICKCORE_OPENEXR_DELEGATE)
281 entry->decoder=(DecodeImageHandler *) ReadEXRImage;
282 entry->encoder=(EncodeImageHandler *) WriteEXRImage;
284 entry->magick=(IsImageFormatHandler *) IsEXR;
285 entry->adjoin=MagickFalse;
286 entry->description=ConstantString("High Dynamic-range (HDR)");
287 entry->blob_support=MagickFalse;
288 entry->module=ConstantString("EXR");
289 (void) RegisterMagickInfo(entry);
290 return(MagickImageCoderSignature);
294 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
298 % U n r e g i s t e r E X R I m a g e %
302 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
304 % UnregisterEXRImage() removes format registrations made by the
305 % EXR module from the list of supported formats.
307 % The format of the UnregisterEXRImage method is:
309 % UnregisterEXRImage(void)
312 ModuleExport void UnregisterEXRImage(void)
314 (void) UnregisterMagickInfo("EXR");
317 #if defined(MAGICKCORE_OPENEXR_DELEGATE)
319 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
323 % W r i t e E X R I m a g e %
327 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
329 % WriteEXRImage() writes an image to a file the in the high dynamic-range
330 % (HDR) file format developed by Industrial Light & Magic.
332 % The format of the WriteEXRImage method is:
334 % MagickBooleanType WriteEXRImage(const ImageInfo *image_info,Image *image)
336 % A description of each parameter follows.
338 % o image_info: the image info.
340 % o image: The image.
343 static MagickBooleanType WriteEXRImage(const ImageInfo *image_info,Image *image)
366 register const Quantum
376 Open output image file.
378 assert(image_info != (const ImageInfo *) NULL);
379 assert(image_info->signature == MagickSignature);
380 assert(image != (Image *) NULL);
381 assert(image->signature == MagickSignature);
382 if (image->debug != MagickFalse)
383 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
384 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
385 if (status == MagickFalse)
387 write_info=CloneImageInfo(image_info);
388 (void) AcquireUniqueFilename(write_info->filename);
389 hdr_info=ImfNewHeader();
390 ImfHeaderSetDataWindow(hdr_info,0,0,(int) image->columns-1,(int)
392 ImfHeaderSetDisplayWindow(hdr_info,0,0,(int) image->columns-1,(int)
394 compression=IMF_NO_COMPRESSION;
395 if (write_info->compression == ZipSCompression)
396 compression=IMF_ZIPS_COMPRESSION;
397 if (write_info->compression == ZipCompression)
398 compression=IMF_ZIP_COMPRESSION;
399 if (write_info->compression == PizCompression)
400 compression=IMF_PIZ_COMPRESSION;
401 if (write_info->compression == Pxr24Compression)
402 compression=IMF_PXR24_COMPRESSION;
403 #if defined(B44Compression)
404 if (write_info->compression == B44Compression)
405 compression=IMF_B44_COMPRESSION;
407 #if defined(B44ACompression)
408 if (write_info->compression == B44ACompression)
409 compression=IMF_B44A_COMPRESSION;
411 ImfHeaderSetCompression(hdr_info,compression);
412 ImfHeaderSetLineOrder(hdr_info,IMF_INCREASING_Y);
413 file=ImfOpenOutputFile(write_info->filename,hdr_info,IMF_WRITE_RGBA);
414 ImfDeleteHeader(hdr_info);
415 if (file == (ImfOutputFile *) NULL)
417 ThrowFileException(&image->exception,BlobError,"UnableToOpenBlob",
419 write_info=DestroyImageInfo(write_info);
422 scanline=(ImfRgba *) AcquireQuantumMemory(image->columns,sizeof(*scanline));
423 if (scanline == (ImfRgba *) NULL)
425 (void) ImfCloseOutputFile(file);
426 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
428 for (y=0; y < (ssize_t) image->rows; y++)
430 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
431 if (p == (const Quantum *) NULL)
433 for (x=0; x < (ssize_t) image->columns; x++)
435 ImfFloatToHalf(QuantumScale*GetPixelRed(image,p),&half_quantum);
436 scanline[x].r=half_quantum;
437 ImfFloatToHalf(QuantumScale*GetPixelGreen(image,p),&half_quantum);
438 scanline[x].g=half_quantum;
439 ImfFloatToHalf(QuantumScale*GetPixelBlue(image,p),&half_quantum);
440 scanline[x].b=half_quantum;
441 if (image->matte == MagickFalse)
442 ImfFloatToHalf(1.0,&half_quantum);
444 ImfFloatToHalf(QuantumScale*GetPixelAlpha(image,p),&half_quantum);
445 scanline[x].a=half_quantum;
446 p+=GetPixelChannels(image);
448 ImfOutputSetFrameBuffer(file,scanline-(y*image->columns),1,image->columns);
449 ImfOutputWritePixels(file,1);
451 (void) ImfCloseOutputFile(file);
452 scanline=(ImfRgba *) RelinquishMagickMemory(scanline);
453 (void) FileToImage(image,write_info->filename);
454 (void) RelinquishUniqueFileResource(write_info->filename);
455 write_info=DestroyImageInfo(write_info);
456 (void) CloseBlob(image);