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 "magick/studio.h"
43 #include "magick/blob.h"
44 #include "magick/blob-private.h"
45 #include "magick/cache.h"
46 #include "magick/exception.h"
47 #include "magick/exception-private.h"
48 #include "magick/image.h"
49 #include "magick/image-private.h"
50 #include "magick/list.h"
51 #include "magick/magick.h"
52 #include "magick/memory_.h"
53 #include "magick/property.h"
54 #include "magick/quantum-private.h"
55 #include "magick/static.h"
56 #include "magick/string_.h"
57 #include "magick/module.h"
58 #include "magick/resource_.h"
59 #include "magick/utility.h"
60 #if defined(MAGICKCORE_OPENEXR_DELEGATE)
61 #include <ImfCRgbaFile.h>
66 static MagickBooleanType
67 WriteEXRImage(const ImageInfo *,Image *);
71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 % IsEXR() returns MagickTrue if the image format type, identified by the
82 % magick string, is EXR.
84 % The format of the IsEXR method is:
86 % MagickBooleanType IsEXR(const unsigned char *magick,const size_t length)
88 % A description of each parameter follows:
90 % o magick: compare image format pattern against these bytes.
92 % o length: Specifies the length of the magick string.
95 static MagickBooleanType IsEXR(const unsigned char *magick,const size_t length)
99 if (memcmp(magick,"\166\057\061\001",4) == 0)
104 #if defined(MAGICKCORE_OPENEXR_DELEGATE)
106 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110 % R e a d E X R I m a g e %
114 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116 % ReadEXRImage reads an image in the high dynamic-range (HDR) file format
117 % developed by Industrial Light & Magic. It allocates the memory necessary
118 % for the new Image structure and returns a pointer to the new image.
120 % The format of the ReadEXRImage method is:
122 % Image *ReadEXRImage(const ImageInfo *image_info,ExceptionInfo *exception)
124 % A description of each parameter follows:
126 % o image_info: the image info.
128 % o exception: return any errors or warnings in this structure.
131 static Image *ReadEXRImage(const ImageInfo *image_info,ExceptionInfo *exception)
169 assert(image_info != (const ImageInfo *) NULL);
170 assert(image_info->signature == MagickSignature);
171 if (image_info->debug != MagickFalse)
172 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
173 image_info->filename);
174 assert(exception != (ExceptionInfo *) NULL);
175 assert(exception->signature == MagickSignature);
176 image=AcquireImage(image_info);
177 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
178 if (status == MagickFalse)
180 image=DestroyImageList(image);
181 return((Image *) NULL);
183 read_info=CloneImageInfo(image_info);
184 if (IsPathAccessible(read_info->filename) == MagickFalse)
186 (void) AcquireUniqueFilename(read_info->filename);
187 (void) ImageToFile(image,read_info->filename,exception);
189 file=ImfOpenInputFile(read_info->filename);
190 if (file == (ImfInputFile *) NULL)
192 ThrowFileException(exception,BlobError,"UnableToOpenBlob",
194 read_info=DestroyImageInfo(read_info);
195 return((Image *) NULL);
197 hdr_info=ImfInputHeader(file);
198 ImfHeaderDataWindow(hdr_info,&min_x,&min_y,&max_x,&max_y);
199 image->columns=max_x-min_x+1UL;
200 image->rows=max_y-min_y+1UL;
201 image->matte=MagickTrue;
202 if (image_info->ping != MagickFalse)
204 (void) ImfCloseInputFile(file);
205 if (LocaleCompare(image_info->filename,read_info->filename) != 0)
206 (void) RelinquishUniqueFileResource(read_info->filename);
207 read_info=DestroyImageInfo(read_info);
208 (void) CloseBlob(image);
209 return(GetFirstImageInList(image));
211 scanline=(ImfRgba *) AcquireQuantumMemory(image->columns,sizeof(*scanline));
212 if (scanline == (ImfRgba *) NULL)
214 (void) ImfCloseInputFile(file);
215 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
217 for (y=0; y < (ssize_t) image->rows; y++)
219 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
220 if (q == (PixelPacket *) NULL)
222 ImfInputSetFrameBuffer(file,scanline-min_x-image->columns*(min_y+y),1,
224 ImfInputReadPixels(file,min_y+y,min_y+y);
225 for (x=0; x < (ssize_t) image->columns; x++)
227 SetRedPixelComponent(q,ClampToQuantum((MagickRealType) QuantumRange*
228 ImfHalfToFloat(scanline[x].r)));
229 SetGreenPixelComponent(q,ClampToQuantum((MagickRealType) QuantumRange*
230 ImfHalfToFloat(scanline[x].g)));
231 SetBluePixelComponent(q,ClampToQuantum((MagickRealType) QuantumRange*
232 ImfHalfToFloat(scanline[x].b)));
233 SetAlphaPixelComponent(q,ClampToQuantum((MagickRealType) QuantumRange*
234 ImfHalfToFloat(scanline[x].a)));
237 if (SyncAuthenticPixels(image,exception) == MagickFalse)
240 scanline=(ImfRgba *) RelinquishMagickMemory(scanline);
241 (void) ImfCloseInputFile(file);
242 if (LocaleCompare(image_info->filename,read_info->filename) != 0)
243 (void) RelinquishUniqueFileResource(read_info->filename);
244 read_info=DestroyImageInfo(read_info);
245 (void) CloseBlob(image);
246 return(GetFirstImageInList(image));
251 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
255 % R e g i s t e r E X R I m a g e %
259 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
261 % RegisterEXRImage() adds properties for the EXR image format
262 % to the list of supported formats. The properties include the image format
263 % tag, a method to read and/or write the format, whether the format
264 % supports the saving of more than one frame to the same file or blob,
265 % whether the format supports native in-memory I/O, and a brief
266 % description of the format.
268 % The format of the RegisterEXRImage method is:
270 % size_t RegisterEXRImage(void)
273 ModuleExport size_t RegisterEXRImage(void)
278 entry=SetMagickInfo("EXR");
279 #if defined(MAGICKCORE_OPENEXR_DELEGATE)
280 entry->decoder=(DecodeImageHandler *) ReadEXRImage;
281 entry->encoder=(EncodeImageHandler *) WriteEXRImage;
283 entry->magick=(IsImageFormatHandler *) IsEXR;
284 entry->adjoin=MagickFalse;
285 entry->description=ConstantString("High Dynamic-range (HDR)");
286 entry->blob_support=MagickFalse;
287 entry->module=ConstantString("EXR");
288 (void) RegisterMagickInfo(entry);
289 return(MagickImageCoderSignature);
293 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
297 % U n r e g i s t e r E X R I m a g e %
301 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
303 % UnregisterEXRImage() removes format registrations made by the
304 % EXR module from the list of supported formats.
306 % The format of the UnregisterEXRImage method is:
308 % UnregisterEXRImage(void)
311 ModuleExport void UnregisterEXRImage(void)
313 (void) UnregisterMagickInfo("EXR");
316 #if defined(MAGICKCORE_OPENEXR_DELEGATE)
318 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
322 % W r i t e E X R I m a g e %
326 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
328 % WriteEXRImage() writes an image to a file the in the high dynamic-range
329 % (HDR) file format developed by Industrial Light & Magic.
331 % The format of the WriteEXRImage method is:
333 % MagickBooleanType WriteEXRImage(const ImageInfo *image_info,Image *image)
335 % A description of each parameter follows.
337 % o image_info: the image info.
339 % o image: The image.
342 static MagickBooleanType WriteEXRImage(const ImageInfo *image_info,Image *image)
365 register const PixelPacket
375 Open output image file.
377 assert(image_info != (const ImageInfo *) NULL);
378 assert(image_info->signature == MagickSignature);
379 assert(image != (Image *) NULL);
380 assert(image->signature == MagickSignature);
381 if (image->debug != MagickFalse)
382 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
383 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
384 if (status == MagickFalse)
386 write_info=CloneImageInfo(image_info);
387 (void) AcquireUniqueFilename(write_info->filename);
388 hdr_info=ImfNewHeader();
389 ImfHeaderSetDataWindow(hdr_info,0,0,(int) image->columns-1,(int)
391 ImfHeaderSetDisplayWindow(hdr_info,0,0,(int) image->columns-1,(int)
393 compression=IMF_NO_COMPRESSION;
394 if (write_info->compression == ZipSCompression)
395 compression=IMF_ZIPS_COMPRESSION;
396 if (write_info->compression == ZipCompression)
397 compression=IMF_ZIP_COMPRESSION;
398 if (write_info->compression == PizCompression)
399 compression=IMF_PIZ_COMPRESSION;
400 if (write_info->compression == Pxr24Compression)
401 compression=IMF_PXR24_COMPRESSION;
402 #if defined(B44Compression)
403 if (write_info->compression == B44Compression)
404 compression=IMF_B44_COMPRESSION;
406 #if defined(B44ACompression)
407 if (write_info->compression == B44ACompression)
408 compression=IMF_B44A_COMPRESSION;
410 ImfHeaderSetCompression(hdr_info,compression);
411 ImfHeaderSetLineOrder(hdr_info,IMF_INCREASING_Y);
412 file=ImfOpenOutputFile(write_info->filename,hdr_info,IMF_WRITE_RGBA);
413 ImfDeleteHeader(hdr_info);
414 if (file == (ImfOutputFile *) NULL)
416 ThrowFileException(&image->exception,BlobError,"UnableToOpenBlob",
418 write_info=DestroyImageInfo(write_info);
421 scanline=(ImfRgba *) AcquireQuantumMemory(image->columns,sizeof(*scanline));
422 if (scanline == (ImfRgba *) NULL)
424 (void) ImfCloseOutputFile(file);
425 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
427 for (y=0; y < (ssize_t) image->rows; y++)
429 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
430 if (p == (const PixelPacket *) NULL)
432 for (x=0; x < (ssize_t) image->columns; x++)
434 ImfFloatToHalf(QuantumScale*GetRedPixelComponent(p),&half_quantum);
435 scanline[x].r=half_quantum;
436 ImfFloatToHalf(QuantumScale*GetGreenPixelComponent(p),&half_quantum);
437 scanline[x].g=half_quantum;
438 ImfFloatToHalf(QuantumScale*GetBluePixelComponent(p),&half_quantum);
439 scanline[x].b=half_quantum;
440 if (image->matte == MagickFalse)
441 ImfFloatToHalf(1.0,&half_quantum);
443 ImfFloatToHalf(1.0-QuantumScale*GetOpacityPixelComponent(p),
445 scanline[x].a=half_quantum;
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);