Reworked Screenshot saving.

Now OGL doesn't rely on WX for PNG saving.
FlipImageData supports (pixel data len > 3) now.
TextureToPng is now in ImageWrite.cpp/h
Video Common depends on zlib and png.
D3D no longer depends on zlib and png.
This commit is contained in:
Matthew Parlane
2013-11-15 13:00:38 +13:00
parent 2703cae8d3
commit 033ed9477e
10 changed files with 134 additions and 196 deletions

View File

@ -93,15 +93,9 @@
<ClInclude Include="Src\XFBEncoder.h" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\Externals\libpng\png\png.vcxproj">
<Project>{4c9f135b-a85e-430c-bad4-4c67ef5fc12c}</Project>
</ProjectReference>
<ProjectReference Include="..\..\..\..\Externals\wxWidgets3\build\msw\wx_base.vcxproj">
<Project>{1c8436c9-dbaf-42be-83bc-cf3ec9175abe}</Project>
</ProjectReference>
<ProjectReference Include="..\..\..\..\Externals\zlib\zlib.vcxproj">
<Project>{ff213b23-2c26-4214-9f88-85271e557e87}</Project>
</ProjectReference>
<ProjectReference Include="..\..\..\Core\VideoCommon\VideoCommon.vcxproj">
<Project>{3de9ee35-3e91-4f27-a014-2866ad8c3fe3}</Project>
</ProjectReference>

View File

@ -5,96 +5,12 @@
#include "D3DBase.h"
#include "D3DTexture.h"
#include "png.h"
namespace DX11
{
namespace D3D
{
bool TextureToPng(D3D11_MAPPED_SUBRESOURCE &map, const char* filename, int width, int height, bool saveAlpha)
{
bool success = false;
if (map.pData != NULL)
{
FILE *fp = NULL;
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
// Open file for writing (binary mode)
fp = fopen(filename, "wb");
if (fp == NULL) {
PanicAlert("Could not open file %s for writing\n", filename);
goto finalise;
}
// Initialize write structure
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
PanicAlert("Could not allocate write struct\n");
goto finalise;
}
// Initialize info structure
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
PanicAlert("Could not allocate info struct\n");
goto finalise;
}
// Setup Exception handling
if (setjmp(png_jmpbuf(png_ptr))) {
PanicAlert("Error during png creation\n");
goto finalise;
}
png_init_io(png_ptr, fp);
// Write header (8 bit colour depth)
png_set_IHDR(png_ptr, info_ptr, width, height,
8, PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
char title[] = "Dolphin Screenshot";
png_text title_text;
title_text.compression = PNG_TEXT_COMPRESSION_NONE;
title_text.key = "Title";
title_text.text = title;
png_set_text(png_ptr, info_ptr, &title_text, 1);
png_write_info(png_ptr, info_ptr);
// Write image data
for (auto y = 0; y < height; ++y)
{
u8* row_ptr = (u8*)map.pData + y * map.RowPitch;
u8* ptr = row_ptr;
for (UINT x = 0; x < map.RowPitch / 4; ++x)
{
if (!saveAlpha)
ptr[3] = 0xff;
ptr += 4;
}
png_write_row(png_ptr, row_ptr);
}
// End write
png_write_end(png_ptr, NULL);
success = true;
finalise:
if (fp != NULL) fclose(fp);
if (info_ptr != NULL) png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
if (png_ptr != NULL) png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
}
return false;
}
void ReplaceRGBATexture2D(ID3D11Texture2D* pTexture, const u8* buffer, unsigned int width, unsigned int height, unsigned int pitch, unsigned int level, D3D11_USAGE usage)
{
if (usage == D3D11_USAGE_DYNAMIC || usage == D3D11_USAGE_STAGING)

View File

@ -11,7 +11,6 @@ namespace DX11
namespace D3D
{
bool TextureToPng(D3D11_MAPPED_SUBRESOURCE& map, const char* filename, int width, int height, bool saveAlpha = true);
void ReplaceRGBATexture2D(ID3D11Texture2D* pTexture, const u8* buffer, unsigned int width, unsigned int height, unsigned int pitch, unsigned int level, D3D11_USAGE usage);
}

View File

@ -34,6 +34,7 @@
#include "FPSCounter.h"
#include "ConfigManager.h"
#include <strsafe.h>
#include "ImageWrite.h"
namespace DX11
{
@ -693,11 +694,19 @@ bool Renderer::SaveScreenshot(const std::string &filename, const TargetRectangle
D3D11_MAPPED_SUBRESOURCE map;
D3D::context->Map(s_screenshot_texture, 0, D3D11_MAP_READ_WRITE, 0, &map);
// ready to be saved
HRESULT hr = D3D::TextureToPng(map, filename.c_str(), rc.GetWidth(), rc.GetHeight(), false);
bool saved_png = false;
if (map.pData)
{
u8* data = new u8[map.RowPitch * rc.GetHeight()];
memcpy(data, map.pData, map.RowPitch * rc.GetHeight());
saved_png = TextureToPng(data, map.RowPitch, filename.c_str(), rc.GetWidth(), rc.GetHeight(), false);
}
D3D::context->Unmap(s_screenshot_texture, 0);
if (SUCCEEDED(hr))
if (saved_png)
{
OSD::AddMessage(StringFromFormat("Saved %i x %i %s", rc.GetWidth(),
rc.GetHeight(), filename.c_str()));
@ -707,7 +716,7 @@ bool Renderer::SaveScreenshot(const std::string &filename, const TargetRectangle
OSD::AddMessage(StringFromFormat("Error saving %s", filename.c_str()));
}
return SUCCEEDED(hr);
return saved_png;
}
void formatBufferDump(const u8* in, u8* out, int w, int h, int p)

View File

@ -14,6 +14,7 @@
#include "PSTextureEncoder.h"
#include "HW/Memmap.h"
#include "VideoConfig.h"
#include "ImageWrite.h"
namespace DX11
{
@ -54,6 +55,8 @@ bool TextureCache::TCacheEntry::Save(const char filename[], unsigned int level)
HRESULT hr = D3D::device->CreateTexture2D(&desc, NULL, &pNewTexture);
bool saved_png = false;
if (SUCCEEDED(hr) && pNewTexture)
{
D3D::context->CopyResource(pNewTexture, pSurface);
@ -62,13 +65,19 @@ bool TextureCache::TCacheEntry::Save(const char filename[], unsigned int level)
HRESULT hr = D3D::context->Map(pNewTexture, 0, D3D11_MAP_READ_WRITE, 0, &map);
if (SUCCEEDED(hr))
{
hr = D3D::TextureToPng(map, filename, desc.Width, desc.Height);
if (map.pData)
{
u8* data = new u8[map.RowPitch * desc.Height];
memcpy(data, map.pData, map.RowPitch * desc.Height);
saved_png = TextureToPng(data, map.RowPitch, filename, desc.Width, desc.Height);
}
D3D::context->Unmap(pNewTexture, 0);
}
SAFE_RELEASE(pNewTexture);
}
return SUCCEEDED(hr);
return saved_png;
}
void TextureCache::TCacheEntry::Load(unsigned int width, unsigned int height,