void D3D11RenderTarget::captureColorBuffer(ColorImageR8G8B8A8 &result)
{
	auto &context = m_graphics->getContext();
    context.CopyResource(m_captureTexture, m_texture);

	result.allocate(m_width, m_height);

    D3D11_MAPPED_SUBRESOURCE resource;
    UINT subresource = D3D11CalcSubresource(0, 0, 0);
    HRESULT hr = context.Map(m_captureTexture, subresource, D3D11_MAP_READ, 0, &resource);
    const BYTE *data = (BYTE *)resource.pData;

    for (UINT y = 0; y < m_height; y++)
    {
        memcpy(&result(0U, y), data + resource.RowPitch * y, m_width * sizeof(ml::vec4uc));
    }

    context.Unmap(m_captureTexture, subresource);
}
  ColorImageR8G8B8A8 LodePNG::load(const std::string &filename)
  {
    if (!ml::util::fileExists(filename))
    {
      std::cout << ("LodePNG::load file not found: " + filename);
      return ColorImageR8G8B8A8();
    }
    std::vector<BYTE> image;
    UINT width, height;

    UINT error = lodepng::decode(image, width, height, filename);

    MLIB_ASSERT_STR(!error, std::string(lodepng_error_text(error)) + ": " + filename);

    ColorImageR8G8B8A8 result;

    if (!error)
    {
        result.allocate(width, height);
        memcpy(result.getPointer(), &image[0], 4 * width * height);
    }

    return result;
  }