Advertisement
Guest User

Untitled

a guest
Aug 21st, 2014
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.40 KB | None | 0 0
  1. /*********************************
  2.  * ImageResource.h
  3.  * Connor Hilarides
  4.  * Created 2014/08/07
  5.  *********************************/
  6.  
  7. #pragma once
  8.  
  9. #include "Helpers\SharedArray.h"
  10. #include "Helpers\UseDirectX.h"
  11.  
  12. // ----------------------------------------------------------------------------
  13.  
  14. struct ImageResource
  15. {
  16.   enum class Format
  17.   {
  18.     RGB,
  19.     RGBA,
  20.     BGR,
  21.     BGRA,
  22.   };
  23.  
  24.   ImageResource() = default;
  25.   ImageResource(int width, int height, Format format, shared_array<byte> data)
  26.     : width(width), height(height), format(format), data(data)
  27.   {
  28.   }
  29.  
  30.   UINT width, height;
  31.   Format format;
  32.   shared_array<byte> data;
  33.  
  34.   void to32BitColor(byte *destination, size_t dest_size);
  35.   template <size_t dest_size>
  36.   void to32BitColor(byte (&destination)[dest_size])
  37.   {
  38.     to32BitColor(destination, dest_size);
  39.   }
  40.   void to32BitColor(shared_array<byte>& destination)
  41.   {
  42.     to32BitColor(destination, destination.size());
  43.   }
  44.  
  45.   static ImageResource fromFile(const std::wstring& file);
  46.   static ImageResource fromAsset(const std::string& container, const std::string& asset);
  47. };
  48.  
  49. // ----------------------------------------------------------------------------
  50.  
  51.  
  52. /*********************************
  53.  * ImageResource.cpp
  54.  * Connor Hilarides
  55.  * Created 2014/08/07
  56.  *********************************/
  57.  
  58. #include "ImageResource.h"
  59. #include "Engine\Common.h"
  60. #include "Helpers\CriticalSections.h"
  61. #include "Engine\Game.h"
  62. #include "ResourcePack.h"
  63.  
  64. // ----------------------------------------------------------------------------
  65.  
  66. void ImageResource::to32BitColor(byte *destination, size_t dest_size)
  67. {
  68.   switch (format)
  69.   {
  70.  
  71.     case Format::RGB:
  72.       for (size_t i = 0; i < data.size() / 3 && i < dest_size / 4; ++i)
  73.       {
  74.         destination[i * 4 + 0] = data[i * 3 + 0]; // R component
  75.         destination[i * 4 + 1] = data[i * 3 + 1]; // G component
  76.         destination[i * 4 + 2] = data[i * 3 + 2]; // B component
  77.         destination[i * 4 + 3] = 255; // Set alpha channel to white
  78.       }
  79.       break;
  80.  
  81.     case Format::RGBA:
  82.       memcpy_s(destination, dest_size, data, data.size());
  83.       break;
  84.  
  85.     case Format::BGR:
  86.       for (size_t i = 0; i < data.size() / 3 && i < dest_size / 4; ++i)
  87.       {
  88.         destination[i * 4 + 0] = data[i * 3 + 2]; // R component
  89.         destination[i * 4 + 1] = data[i * 3 + 1]; // G component
  90.         destination[i * 4 + 2] = data[i * 3 + 0]; // B component
  91.         destination[i * 4 + 3] = 255; // Set alpha channel to white
  92.       }
  93.       break;
  94.  
  95.     case Format::BGRA:
  96.       for (size_t i = 0; i < data.size() / 4 && i < dest_size / 4; ++i)
  97.       {
  98.         destination[i * 4 + 0] = data[i * 3 + 2]; // R component
  99.         destination[i * 4 + 1] = data[i * 3 + 1]; // G component
  100.         destination[i * 4 + 2] = data[i * 3 + 0]; // B component
  101.         destination[i * 4 + 3] = data[i * 3 + 3]; // A component
  102.       }
  103.       break;
  104.   }
  105. }
  106.  
  107. // ----------------------------------------------------------------------------
  108.  
  109. static IWICImagingFactory *getImagingFactory()
  110. {
  111.   THREAD_EXCLUSIVE_SCOPE;
  112.  
  113.   static IWICImagingFactory *factory = nullptr;
  114.   if (factory)
  115.     return factory;
  116.  
  117.   HRESULT hr = CoCreateInstance(
  118.     CLSID_WICImagingFactory,
  119.     nullptr,
  120.     CLSCTX_INPROC_SERVER,
  121.     IID_IWICImagingFactory,
  122.     reinterpret_cast<LPVOID*>(&factory));
  123.   CHECK_HRESULT(hr);
  124.  
  125.   return factory;
  126. }
  127.  
  128. // ----------------------------------------------------------------------------
  129.  
  130. ImageResource ImageResource::fromFile(const std::wstring& file)
  131. {
  132.   ImageResource image;
  133.  
  134.   auto fileData = fs::file_reader::readAllBytes(file);
  135.  
  136.   IWICImagingFactory *factory = getImagingFactory();
  137.   HRESULT hr;
  138.  
  139.   IWICBitmapDecoder *decoder;
  140.   hr = factory->CreateDecoderFromFilename(
  141.     file.c_str(),
  142.     nullptr,
  143.     GENERIC_READ,
  144.     WICDecodeMetadataCacheOnLoad,
  145.     &decoder);
  146.   CHECK_HRESULT(hr);
  147.   RELEASE_AFTER_SCOPE(decoder);
  148.  
  149.   IWICBitmapFrameDecode *source;
  150.   hr = decoder->GetFrame(0, &source);
  151.   CHECK_HRESULT(hr);
  152.   RELEASE_AFTER_SCOPE(source);
  153.  
  154.   IWICFormatConverter *converter;
  155.   hr = factory->CreateFormatConverter(&converter);
  156.   CHECK_HRESULT(hr);
  157.   RELEASE_AFTER_SCOPE(converter);
  158.  
  159.   hr = converter->Initialize(
  160.     source,
  161.     GUID_WICPixelFormat32bppRGBA,
  162.     WICBitmapDitherTypeNone,
  163.     nullptr,
  164.     0.f,
  165.     WICBitmapPaletteTypeMedianCut);
  166.   CHECK_HRESULT(hr);
  167.  
  168.   image.format = Format::RGBA;
  169.  
  170.   hr = converter->GetSize(&image.width, &image.height);
  171.   CHECK_HRESULT(hr);
  172.  
  173.   image.data = shared_array<byte>(image.width * image.height * 4);
  174.   hr = converter->CopyPixels(
  175.     nullptr,
  176.     image.width * 4,
  177.     static_cast<UINT>(image.data.size()),
  178.     image.data);
  179.   CHECK_HRESULT(hr);
  180.  
  181.   return image;
  182. }
  183.  
  184. // ----------------------------------------------------------------------------
  185.  
  186. ImageResource ImageResource::fromAsset(const std::string& container, const std::string& asset)
  187. {
  188.   auto& pack = GetGame()->Respack;
  189.  
  190.   auto *rescontainer = pack[container];
  191.   RELEASE_AFTER_SCOPE(rescontainer);
  192.  
  193.   auto *resource = rescontainer->getResource(asset);
  194.   RELEASE_AFTER_SCOPE(resource);
  195.  
  196.   if (resource == nullptr)
  197.     throw string_exception(container + " asset '" + asset + "' could not be found!");
  198.  
  199.   auto file = resource->getTempFile();
  200.   return ImageResource::fromFile(file.getPath());
  201. }
  202.  
  203. // ----------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement