Ejemplo n.º 1
0
int main(int argc, const char * argv[])
{
    std::string filename = "../data/ye_high2.png";
    if (argc > 1) {
        filename = argv[1];
    }

    ColorImageR8G8B8A8	   image = LodePNG::load(filename);
	ColorImageR32G32B32A32 imageR32(image.getWidth(), image.getHeight());
	for (unsigned int y = 0; y < image.getHeight(); y++) {
		for (unsigned int x = 0; x < image.getWidth(); x++) {
			imageR32(x,y) = image(x,y);
		}
	}
	

    CombinedSolverParameters params;
    params.nonLinearIter = 7;
    params.linearIter = 10;

    CombinedSolver solver(imageR32, params);

	solver.solveAll();

    ColorImageR32G32B32A32* res = solver.getAlbedo();
	ColorImageR8G8B8A8 out(res->getWidth(), res->getHeight());
	for (unsigned int y = 0; y < res->getHeight(); y++) {
		for (unsigned int x = 0; x < res->getWidth(); x++) {
			unsigned char r = math::round(math::clamp(255.0f*(*res)(x, y).x, 0.0f, 255.0f));
			unsigned char g = math::round(math::clamp(255.0f*(*res)(x, y).y, 0.0f, 255.0f));
			unsigned char b = math::round(math::clamp(255.0f*(*res)(x, y).z, 0.0f, 255.0f));
			out(x, y) = vec4uc(r, g, b,255);
		}
	}
	LodePNG::save(out, "outputAlbedo.png");

    res = solver.getShading();
	ColorImageR8G8B8A8 out2(res->getWidth(), res->getHeight());
	for (unsigned int y = 0; y < res->getHeight(); y++) {
		for (unsigned int x = 0; x < res->getWidth(); x++) {
			unsigned char r = math::round(255.0f*math::clamp((*res)(x, y).x, 0.0f, 255.0f));
			unsigned char g = math::round(255.0f*math::clamp((*res)(x, y).y, 0.0f, 255.0f));
			unsigned char b = math::round(255.0f*math::clamp((*res)(x, y).z, 0.0f, 255.0f));
			out2(x, y) = vec4uc(r, g, b, 255);
		}
	}
	LodePNG::save(out2, "outputShading.png");
	return 0;
}
  void LodePNG::save(const ColorImageR8G8B8A8 &image, const std::string &filename)
  {
    const UINT pixelCount = image.getWidth()*image.getHeight();

    //
    // images should be saved with no transparency, which unfortunately requires us to make a copy of the bitmap data.
    //
    RGBColor *copy = new RGBColor[pixelCount];
    memcpy(copy, image.getPointer(), pixelCount * 4);
    for (UINT i = 0; i < pixelCount; i++)
      copy[i].a = 255;

    lodepng::encode(filename, (const BYTE *)copy, image.getWidth(), image.getHeight(), LodePNGColorType::LCT_RGBA);
    delete[] copy;
  }
  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;
  }
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);
}