2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write High Dynamic-Range (HDR) Image File Format %
20 % Copyright 1999-2017 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/option.h"
54 #include "MagickCore/pixel-accessor.h"
55 #include "MagickCore/property.h"
56 #include "MagickCore/quantum-private.h"
57 #include "MagickCore/static.h"
58 #include "MagickCore/string_.h"
59 #include "MagickCore/module.h"
60 #include "MagickCore/resource_.h"
61 #include "MagickCore/utility.h"
62 #if defined(MAGICKCORE_OPENEXR_DELEGATE)
63 #include <ImfCRgbaFile.h>
66 Typedef declaractions.
68 typedef struct _ExrWindow
80 static MagickBooleanType
81 WriteEXRImage(const ImageInfo *,Image *,ExceptionInfo *);
85 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
95 % IsEXR() returns MagickTrue if the image format type, identified by the
96 % magick string, is EXR.
98 % The format of the IsEXR method is:
100 % MagickBooleanType IsEXR(const unsigned char *magick,const size_t length)
102 % A description of each parameter follows:
104 % o magick: compare image format pattern against these bytes.
106 % o length: Specifies the length of the magick string.
109 static MagickBooleanType IsEXR(const unsigned char *magick,const size_t length)
113 if (memcmp(magick,"\166\057\061\001",4) == 0)
118 #if defined(MAGICKCORE_OPENEXR_DELEGATE)
120 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
124 % R e a d E X R I m a g e %
128 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130 % ReadEXRImage reads an image in the high dynamic-range (HDR) file format
131 % developed by Industrial Light & Magic. It allocates the memory necessary
132 % for the new Image structure and returns a pointer to the new image.
134 % The format of the ReadEXRImage method is:
136 % Image *ReadEXRImage(const ImageInfo *image_info,ExceptionInfo *exception)
138 % A description of each parameter follows:
140 % o image_info: the image info.
142 % o exception: return any errors or warnings in this structure.
145 static Image *ReadEXRImage(const ImageInfo *image_info,ExceptionInfo *exception)
182 assert(image_info != (const ImageInfo *) NULL);
183 assert(image_info->signature == MagickCoreSignature);
184 if (image_info->debug != MagickFalse)
185 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
186 image_info->filename);
187 assert(exception != (ExceptionInfo *) NULL);
188 assert(exception->signature == MagickCoreSignature);
189 image=AcquireImage(image_info,exception);
190 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
191 if (status == MagickFalse)
193 image=DestroyImageList(image);
194 return((Image *) NULL);
196 read_info=CloneImageInfo(image_info);
197 if (IsPathAccessible(read_info->filename) == MagickFalse)
199 (void) AcquireUniqueFilename(read_info->filename);
200 (void) ImageToFile(image,read_info->filename,exception);
202 file=ImfOpenInputFile(read_info->filename);
203 if (file == (ImfInputFile *) NULL)
205 ThrowFileException(exception,BlobError,"UnableToOpenBlob",
207 if (LocaleCompare(image_info->filename,read_info->filename) != 0)
208 (void) RelinquishUniqueFileResource(read_info->filename);
209 read_info=DestroyImageInfo(read_info);
210 return((Image *) NULL);
212 hdr_info=ImfInputHeader(file);
213 ImfHeaderDisplayWindow(hdr_info,&display_window.min_x,&display_window.min_y,
214 &display_window.max_x,&display_window.max_y);
215 image->columns=display_window.max_x-display_window.min_x+1UL;
216 image->rows=display_window.max_y-display_window.min_y+1UL;
217 image->alpha_trait=BlendPixelTrait;
218 SetImageColorspace(image,RGBColorspace,exception);
220 if (image_info->ping != MagickFalse)
222 (void) ImfCloseInputFile(file);
223 if (LocaleCompare(image_info->filename,read_info->filename) != 0)
224 (void) RelinquishUniqueFileResource(read_info->filename);
225 read_info=DestroyImageInfo(read_info);
226 (void) CloseBlob(image);
227 return(GetFirstImageInList(image));
229 status=SetImageExtent(image,image->columns,image->rows,exception);
230 if (status == MagickFalse)
231 return(DestroyImageList(image));
232 ImfHeaderDataWindow(hdr_info,&data_window.min_x,&data_window.min_y,
233 &data_window.max_x,&data_window.max_y);
234 columns=(ssize_t) data_window.max_x-data_window.min_x+1UL;
235 if ((display_window.min_x > data_window.max_x) ||
236 (display_window.min_x+(int) image->columns <= data_window.min_x))
237 scanline=(ImfRgba *) NULL;
240 scanline=(ImfRgba *) AcquireQuantumMemory(columns,sizeof(*scanline));
241 if (scanline == (ImfRgba *) NULL)
243 (void) ImfCloseInputFile(file);
244 if (LocaleCompare(image_info->filename,read_info->filename) != 0)
245 (void) RelinquishUniqueFileResource(read_info->filename);
246 read_info=DestroyImageInfo(read_info);
247 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
250 for (y=0; y < (ssize_t) image->rows; y++)
255 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
256 if (q == (Quantum *) NULL)
258 yy=display_window.min_y+y;
259 if ((yy < data_window.min_y) || (yy > data_window.max_y) ||
260 (scanline == (ImfRgba *) NULL))
262 for (x=0; x < (ssize_t) image->columns; x++)
264 SetPixelViaPixelInfo(image,&image->background_color,q);
265 q+=GetPixelChannels(image);
269 ResetMagickMemory(scanline,0,columns*sizeof(*scanline));
270 ImfInputSetFrameBuffer(file,scanline-data_window.min_x-columns*yy,1,
272 ImfInputReadPixels(file,yy,yy);
273 for (x=0; x < (ssize_t) image->columns; x++)
278 xx=display_window.min_x+((int) x-data_window.min_x);
279 if ((xx < 0) || (display_window.min_x+(int) x > data_window.max_x))
280 SetPixelViaPixelInfo(image,&image->background_color,q);
283 SetPixelRed(image,ClampToQuantum((MagickRealType) QuantumRange*
284 ImfHalfToFloat(scanline[xx].r)),q);
285 SetPixelGreen(image,ClampToQuantum((MagickRealType) QuantumRange*
286 ImfHalfToFloat(scanline[xx].g)),q);
287 SetPixelBlue(image,ClampToQuantum((MagickRealType) QuantumRange*
288 ImfHalfToFloat(scanline[xx].b)),q);
289 SetPixelAlpha(image,ClampToQuantum((MagickRealType) QuantumRange*
290 ImfHalfToFloat(scanline[xx].a)),q);
292 q+=GetPixelChannels(image);
294 if (SyncAuthenticPixels(image,exception) == MagickFalse)
297 scanline=(ImfRgba *) RelinquishMagickMemory(scanline);
298 (void) ImfCloseInputFile(file);
299 if (LocaleCompare(image_info->filename,read_info->filename) != 0)
300 (void) RelinquishUniqueFileResource(read_info->filename);
301 read_info=DestroyImageInfo(read_info);
302 (void) CloseBlob(image);
303 return(GetFirstImageInList(image));
308 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
312 % R e g i s t e r E X R I m a g e %
316 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
318 % RegisterEXRImage() adds properties for the EXR image format
319 % to the list of supported formats. The properties include the image format
320 % tag, a method to read and/or write the format, whether the format
321 % supports the saving of more than one frame to the same file or blob,
322 % whether the format supports native in-memory I/O, and a brief
323 % description of the format.
325 % The format of the RegisterEXRImage method is:
327 % size_t RegisterEXRImage(void)
330 ModuleExport size_t RegisterEXRImage(void)
335 entry=AcquireMagickInfo("EXR","EXR","High Dynamic-range (HDR)");
336 #if defined(MAGICKCORE_OPENEXR_DELEGATE)
337 entry->decoder=(DecodeImageHandler *) ReadEXRImage;
338 entry->encoder=(EncodeImageHandler *) WriteEXRImage;
340 entry->magick=(IsImageFormatHandler *) IsEXR;
341 entry->flags^=CoderAdjoinFlag;
342 entry->flags^=CoderBlobSupportFlag;
343 (void) RegisterMagickInfo(entry);
344 return(MagickImageCoderSignature);
348 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
352 % U n r e g i s t e r E X R I m a g e %
356 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
358 % UnregisterEXRImage() removes format registrations made by the
359 % EXR module from the list of supported formats.
361 % The format of the UnregisterEXRImage method is:
363 % UnregisterEXRImage(void)
366 ModuleExport void UnregisterEXRImage(void)
368 (void) UnregisterMagickInfo("EXR");
371 #if defined(MAGICKCORE_OPENEXR_DELEGATE)
373 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
377 % W r i t e E X R I m a g e %
381 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
383 % WriteEXRImage() writes an image to a file the in the high dynamic-range
384 % (HDR) file format developed by Industrial Light & Magic.
386 % The format of the WriteEXRImage method is:
388 % MagickBooleanType WriteEXRImage(const ImageInfo *image_info,
389 % Image *image,ExceptionInfo *exception)
391 % A description of each parameter follows.
393 % o image_info: the image info.
395 % o image: The image.
397 % o exception: return any errors or warnings in this structure.
400 static MagickBooleanType WriteEXRImage(const ImageInfo *image_info,Image *image,
401 ExceptionInfo *exception)
430 register const Quantum
440 Open output image file.
442 assert(image_info != (const ImageInfo *) NULL);
443 assert(image_info->signature == MagickCoreSignature);
444 assert(image != (Image *) NULL);
445 assert(image->signature == MagickCoreSignature);
446 if (image->debug != MagickFalse)
447 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
448 assert(exception != (ExceptionInfo *) NULL);
449 assert(exception->signature == MagickCoreSignature);
450 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
451 if (status == MagickFalse)
453 (void) SetImageColorspace(image,RGBColorspace,exception);
454 write_info=CloneImageInfo(image_info);
455 (void) AcquireUniqueFilename(write_info->filename);
456 hdr_info=ImfNewHeader();
457 ImfHeaderSetDataWindow(hdr_info,0,0,(int) image->columns-1,(int)
459 ImfHeaderSetDisplayWindow(hdr_info,0,0,(int) image->columns-1,(int)
461 compression=IMF_NO_COMPRESSION;
462 if (write_info->compression == ZipSCompression)
463 compression=IMF_ZIPS_COMPRESSION;
464 if (write_info->compression == ZipCompression)
465 compression=IMF_ZIP_COMPRESSION;
466 if (write_info->compression == PizCompression)
467 compression=IMF_PIZ_COMPRESSION;
468 if (write_info->compression == Pxr24Compression)
469 compression=IMF_PXR24_COMPRESSION;
470 #if defined(B44Compression)
471 if (write_info->compression == B44Compression)
472 compression=IMF_B44_COMPRESSION;
474 #if defined(B44ACompression)
475 if (write_info->compression == B44ACompression)
476 compression=IMF_B44A_COMPRESSION;
479 value=GetImageOption(image_info,"exr:color-type");
480 if (value != (const char *) NULL)
482 if (LocaleCompare(value,"RGB") == 0)
483 channels=IMF_WRITE_RGB;
484 else if (LocaleCompare(value,"RGBA") == 0)
485 channels=IMF_WRITE_RGBA;
486 else if (LocaleCompare(value,"YC") == 0)
487 channels=IMF_WRITE_YC;
488 else if (LocaleCompare(value,"YCA") == 0)
489 channels=IMF_WRITE_YCA;
490 else if (LocaleCompare(value,"Y") == 0)
491 channels=IMF_WRITE_Y;
492 else if (LocaleCompare(value,"YA") == 0)
493 channels=IMF_WRITE_YA;
494 else if (LocaleCompare(value,"R") == 0)
495 channels=IMF_WRITE_R;
496 else if (LocaleCompare(value,"G") == 0)
497 channels=IMF_WRITE_G;
498 else if (LocaleCompare(value,"B") == 0)
499 channels=IMF_WRITE_B;
500 else if (LocaleCompare(value,"A") == 0)
501 channels=IMF_WRITE_A;
503 (void) ThrowMagickException(exception,GetMagickModule(),CoderWarning,
504 "ignoring invalid defined exr:color-type","=%s",value);
506 sampling_factor=(const char *) NULL;
508 if (image_info->sampling_factor != (char *) NULL)
509 sampling_factor=image_info->sampling_factor;
510 if (sampling_factor != NULL)
513 Sampling factors, valid values are 1x1 or 2x2.
515 if (sscanf(sampling_factor,"%d:%d:%d",factors,factors+1,factors+2) == 3)
517 if ((factors[0] == factors[1]) && (factors[1] == factors[2]))
520 if ((factors[0] == (2*factors[1])) && (factors[2] == 0))
524 if (sscanf(sampling_factor,"%dx%d",factors,factors+1) == 2)
526 if (factors[0] != factors[1])
529 if ((factors[0] != 1) && (factors[0] != 2))
530 (void) ThrowMagickException(exception,GetMagickModule(),CoderWarning,
531 "ignoring sampling-factor","=%s",sampling_factor);
532 else if (channels != 0)
535 Cross check given color type and subsampling.
537 factors[1]=((channels == IMF_WRITE_YCA) ||
538 (channels == IMF_WRITE_YC)) ? 2 : 1;
539 if (factors[0] != factors[1])
540 (void) ThrowMagickException(exception,GetMagickModule(),
541 CoderWarning,"sampling-factor and color type mismatch","=%s",
548 If no color type given, select it now.
551 channels=image->alpha_trait != UndefinedPixelTrait ? IMF_WRITE_YCA :
554 channels=image->alpha_trait != UndefinedPixelTrait ? IMF_WRITE_RGBA :
557 ImfHeaderSetCompression(hdr_info,compression);
558 ImfHeaderSetLineOrder(hdr_info,IMF_INCREASING_Y);
559 file=ImfOpenOutputFile(write_info->filename,hdr_info,channels);
560 ImfDeleteHeader(hdr_info);
561 if (file == (ImfOutputFile *) NULL)
563 (void) RelinquishUniqueFileResource(write_info->filename);
564 write_info=DestroyImageInfo(write_info);
565 ThrowFileException(exception,BlobError,"UnableToOpenBlob",
569 scanline=(ImfRgba *) AcquireQuantumMemory(image->columns,sizeof(*scanline));
570 if (scanline == (ImfRgba *) NULL)
572 (void) ImfCloseOutputFile(file);
573 (void) RelinquishUniqueFileResource(write_info->filename);
574 write_info=DestroyImageInfo(write_info);
575 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
577 ResetMagickMemory(scanline,0,image->columns*sizeof(*scanline));
578 for (y=0; y < (ssize_t) image->rows; y++)
580 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
581 if (p == (const Quantum *) NULL)
583 for (x=0; x < (ssize_t) image->columns; x++)
585 ImfFloatToHalf(QuantumScale*GetPixelRed(image,p),&half_quantum);
586 scanline[x].r=half_quantum;
587 ImfFloatToHalf(QuantumScale*GetPixelGreen(image,p),&half_quantum);
588 scanline[x].g=half_quantum;
589 ImfFloatToHalf(QuantumScale*GetPixelBlue(image,p),&half_quantum);
590 scanline[x].b=half_quantum;
591 if (image->alpha_trait == UndefinedPixelTrait)
592 ImfFloatToHalf(1.0,&half_quantum);
594 ImfFloatToHalf(QuantumScale*GetPixelAlpha(image,p),&half_quantum);
595 scanline[x].a=half_quantum;
596 p+=GetPixelChannels(image);
598 ImfOutputSetFrameBuffer(file,scanline-(y*image->columns),1,image->columns);
599 ImfOutputWritePixels(file,1);
601 (void) ImfCloseOutputFile(file);
602 scanline=(ImfRgba *) RelinquishMagickMemory(scanline);
603 (void) FileToImage(image,write_info->filename,exception);
604 (void) RelinquishUniqueFileResource(write_info->filename);
605 write_info=DestroyImageInfo(write_info);
606 (void) CloseBlob(image);