ExrLoader.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <ImageLoader/ImageLoaders.h>
  9. #include <Atom/ImageProcessing/ImageObject.h>
  10. #include <QString>
  11. // From OpenEXR third party library
  12. #include <OpenEXR/ImfArray.h>
  13. #include <OpenEXR/ImfFrameBuffer.h>
  14. #include <OpenEXR/ImfChannelList.h>
  15. #include <OpenEXR/ImfHeader.h>
  16. #include <OpenEXR/ImfInputFile.h>
  17. #include <OpenEXR/ImfRgba.h>
  18. #include <OpenEXR/ImfStandardAttributes.h>
  19. #include <OpenEXR/ImfTestFile.h>
  20. #include <OpenEXR/ImfTiledInputFile.h>
  21. // define imf namespace with current library version
  22. #define Imf OPENEXR_IMF_INTERNAL_NAMESPACE
  23. #define ImfMath IMATH_INTERNAL_NAMESPACE
  24. namespace ImageProcessingAtom
  25. {
  26. namespace ExrLoader
  27. {
  28. bool IsExtensionSupported(const char* extension)
  29. {
  30. QString ext = QString(extension).toLower();
  31. // This is the list of file extensions supported by this loader
  32. return ext == "exr";
  33. }
  34. IImageObject* LoadImageFromScanlineFile(const AZStd::string& filename)
  35. {
  36. try
  37. {
  38. Imf::InputFile exrFile(filename.c_str());
  39. if (!exrFile.isComplete())
  40. {
  41. AZ_Error("Image Processing", false, "ExrLoader: uncompleted exr file [%s]", filename.c_str());
  42. return NULL;
  43. }
  44. const Imf::Header& header = exrFile.header();
  45. // Get Channel information for RGBA
  46. const Imf::Channel* channels[4];
  47. channels[0] = header.channels().findChannel("R");
  48. channels[1] = header.channels().findChannel("G");
  49. channels[2] = header.channels().findChannel("B");
  50. channels[3] = header.channels().findChannel("A");
  51. // Initialize pixel format to invalid one
  52. Imf::PixelType pixelType = Imf::NUM_PIXELTYPES;
  53. bool hasChannels = false;
  54. for (int32_t idx = 0; idx < 4; idx++)
  55. {
  56. if (channels[idx])
  57. {
  58. if (hasChannels)
  59. {
  60. if (pixelType != channels[idx]->type)
  61. {
  62. // return null if there are different pixel types in different channels
  63. AZ_Error("Image Processing", false, "load exr file error: image "
  64. "channels have different data types", filename.c_str());
  65. return nullptr;
  66. }
  67. }
  68. else
  69. {
  70. pixelType = channels[idx]->type;
  71. hasChannels = true;
  72. }
  73. }
  74. }
  75. if (!hasChannels)
  76. {
  77. // return null if there are no rgba channels
  78. AZ_Error("Image Processing", false, "load exr file error: exr image doesn't contain "
  79. "any rgba channels", filename.c_str());
  80. return nullptr;
  81. }
  82. // Find the EPixelFormat to matching the format
  83. EPixelFormat format = ePixelFormat_Unknown;
  84. int32_t pixelSize = 0;
  85. if (pixelType == Imf::FLOAT)
  86. {
  87. format = EPixelFormat::ePixelFormat_R32G32B32A32F;
  88. pixelSize = 16;
  89. }
  90. else if (pixelType == Imf::HALF)
  91. {
  92. format = EPixelFormat::ePixelFormat_R16G16B16A16F;
  93. pixelSize = 8;
  94. }
  95. else
  96. {
  97. AZ_Error("Image Processing", false, "load exr file error: unsupported exr pixel format [%d]", pixelType);
  98. return nullptr;
  99. }
  100. // Get the image size
  101. int width, height;
  102. ImfMath::Box2i dw = header.dataWindow();
  103. width = dw.max.x - dw.min.x + 1;
  104. height = dw.max.y - dw.min.y + 1;
  105. // Create IImageObject
  106. IImageObject* newImage = IImageObject::CreateImage(width, height, 1, format);
  107. // Setup Imf FrameBuffer for loading data
  108. char* pixels = new char[width * height * pixelSize];
  109. Imf::FrameBuffer frameBuffer;
  110. size_t xStride = pixelSize;
  111. size_t yStride = pixelSize * width;
  112. int32_t channelPixelSize = pixelSize / 4;
  113. char* base = pixels;
  114. frameBuffer.insert("R",
  115. Imf::Slice(pixelType, base, xStride, yStride));
  116. frameBuffer.insert("G",
  117. Imf::Slice(pixelType, base + channelPixelSize, xStride, yStride));
  118. frameBuffer.insert("B",
  119. Imf::Slice(pixelType, base + channelPixelSize * 2, xStride, yStride));
  120. // Insert A with default value of 1
  121. frameBuffer.insert("A",
  122. Imf::Slice(pixelType, base + channelPixelSize * 3, xStride, yStride, 1, 1, 1.0));
  123. exrFile.setFrameBuffer(frameBuffer);
  124. exrFile.readPixels(0, height - 1);
  125. // save pixel data to newImage's mipmap data buffer
  126. AZ::u32 pitch;
  127. AZ::u8* mem;
  128. newImage->GetImagePointer(0, mem, pitch);
  129. memcpy(mem, base, newImage->GetMipBufSize(0));
  130. delete [] pixels;
  131. return newImage;
  132. }
  133. catch (...)
  134. {
  135. AZ_Error("Image Processing", false, "ExrLoader: load exr file [%s] error", filename.c_str());
  136. return nullptr;
  137. }
  138. }
  139. IImageObject* LoadImageFromFile(const AZStd::string& filename)
  140. {
  141. // In the current implementation it supports load one flat image with one or few of rgba channels.
  142. // It wont handle multi-part, deep image or some arbitrary channels. It also won't handle layers.
  143. // It's often the environment map wasn't saved with "envmap" header, so we are not trying get the information.
  144. // Get exr file feature information
  145. bool isTiled, isDeep, isMultiPart;
  146. bool isExr = Imf::isOpenExrFile(filename.c_str(), isTiled, isDeep, isMultiPart);
  147. if (!isExr)
  148. {
  149. AZ_Error("Image Processing", false, "ExrLoader: file [%s] is not a valid exr file", filename.c_str());
  150. return NULL;
  151. }
  152. if (isDeep || isMultiPart || isTiled)
  153. {
  154. if (isTiled)
  155. {
  156. AZ_Error("Image Processing", false, "ExrLoader doesn't support tiled exr file [%s]", filename.c_str());
  157. }
  158. else
  159. {
  160. AZ_Error("Image Processing", false, "ExrLoader: file [%s] has unsupported deep or multi-part information", filename.c_str());
  161. }
  162. return NULL;
  163. }
  164. return LoadImageFromScanlineFile(filename);
  165. }
  166. }// namespace ExrLoader
  167. } //namespace ImageProcessingAtom
  168. #undef Imf
  169. #undef ImfMath