Image.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // The MIT License(MIT)
  2. //
  3. // Copyright(c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. // this software and associated documentation files(the "Software"), to deal in
  7. // the Software without restriction, including without limitation the rights to
  8. // use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of
  9. // the Software, and to permit persons to whom the Software is furnished to do so,
  10. // subject to the following conditions :
  11. //
  12. // The above copyright notice and this permission notice shall be included in all
  13. // copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR
  18. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #include "Image.h"
  22. #include "DXUtilities.h"
  23. Image::Image()
  24. {
  25. DX::ThrowIfFailed(CoInitialize(nullptr));
  26. DX::ThrowIfFailed(CoCreateInstance(CLSID_WICImagingFactory, nullptr, CLSCTX_INPROC_SERVER, __uuidof(IWICImagingFactory), (LPVOID*)&m_WICFactory));
  27. }
  28. void Image::load(const std::filesystem::path& FilePath)
  29. {
  30. if (FilePath.extension() == ".png")
  31. LoadWIC(FilePath.wstring());
  32. }
  33. void Image::LoadWIC(const std::wstring& filename)
  34. {
  35. wchar_t* wfilename = _wcsdup(filename.c_str());
  36. ComPtr<IWICBitmapDecoder> decoder;
  37. DX::ThrowIfFailed(m_WICFactory->CreateDecoderFromFilename(wfilename, 0, GENERIC_READ, WICDecodeMetadataCacheOnDemand, &decoder));
  38. ComPtr<IWICBitmapFrameDecode> frame;
  39. DX::ThrowIfFailed(decoder->GetFrame(0, &frame));
  40. DX::ThrowIfFailed(frame->GetSize(&m_width, &m_height));
  41. // get bpp
  42. WICPixelFormatGUID pixelFormat;
  43. DX::ThrowIfFailed(frame->GetPixelFormat(&pixelFormat));
  44. ComPtr<IWICBitmapSource> pConvertedFrame;
  45. if (pixelFormat != GUID_WICPixelFormat32bppRGBA)
  46. {
  47. DX::ThrowIfFailed(WICConvertBitmapSource(GUID_WICPixelFormat32bppRGBA, frame.Get(), &pConvertedFrame));
  48. }
  49. else
  50. {
  51. pConvertedFrame = frame;
  52. }
  53. DX::ThrowIfFailed(pConvertedFrame->GetPixelFormat(&pixelFormat));
  54. ComPtr<IWICComponentInfo> cinfo;
  55. DX::ThrowIfFailed(m_WICFactory->CreateComponentInfo(pixelFormat, &cinfo));
  56. ComPtr<IWICPixelFormatInfo> pfinfo;
  57. DX::ThrowIfFailed(cinfo->QueryInterface(__uuidof(IWICPixelFormatInfo), &pfinfo));
  58. DX::ThrowIfFailed(pfinfo->GetBitsPerPixel(&m_bpp));
  59. m_rowPitch = (m_width * m_bpp + 7) / 8;
  60. m_imageSize = m_rowPitch * m_height;
  61. m_data.resize(m_imageSize);
  62. DX::ThrowIfFailed(pConvertedFrame->CopyPixels(0, m_rowPitch, m_imageSize, m_data.data()));
  63. }