93 changes: 93 additions & 0 deletions Source/Core/VideoCommon/Src/ImageWrite.cpp
Expand Up @@ -5,6 +5,7 @@
#include <list>
#include <vector>

#include "png.h"
#include "ImageWrite.h"
#include "FileUtil.h"

Expand Down Expand Up @@ -62,3 +63,95 @@ bool SaveData(const char* filename, const char* data)

return true;
}


/*
TextureToPng
Inputs:
data : This is an array of RGBA with 8 bits per channel. 4 bytes for each pixel.
data is a newly allocated memory and must have delete[] run on it before returning.
row_stride: Determines the amount of bytes per row of pixels.
*/
bool TextureToPng(u8* data, int row_stride, const char* filename, int width, int height, bool saveAlpha)
{
bool success = false;

if (!data)
return false;

// Open file for writing (binary mode)
FILE *fp = fopen(filename, "wb");
if (fp == NULL) {
PanicAlert("Screenshot failed: Could not open file %s\n", filename);
goto finalise;
}

// Initialize write structure
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
PanicAlert("Screenshot failed: Could not allocate write struct\n");
goto finalise;

}

// Initialize info structure
png_infop info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
PanicAlert("Screenshot failed: Could not allocate info struct\n");
goto finalise;
}

// Setup Exception handling
if (setjmp(png_jmpbuf(png_ptr))) {
PanicAlert("Screenshot failed: 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*)data + y * row_stride;
u8* ptr = row_ptr;
for (auto x = 0; x < row_stride / 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);

// Our duty to delete the inputted data.
delete[] data;

return success;
}
1 change: 1 addition & 0 deletions Source/Core/VideoCommon/Src/ImageWrite.h
Expand Up @@ -9,6 +9,7 @@

bool SaveTGA(const char* filename, int width, int height, void* pdata);
bool SaveData(const char* filename, const char* pdata);
bool TextureToPng(u8* data, int row_stride, const char* filename, int width, int height, bool saveAlpha = true);

#endif // _IMAGEWRITE_H

6 changes: 6 additions & 0 deletions Source/Core/VideoCommon/VideoCommon.vcxproj
Expand Up @@ -152,9 +152,15 @@
<ProjectReference Include="..\..\..\Externals\CLRun\clrun\CLRun.vcxproj">
<Project>{aa862e5e-a993-497a-b6a0-0e8e94b10050}</Project>
</ProjectReference>
<ProjectReference Include="..\..\..\Externals\libpng\png\png.vcxproj">
<Project>{4c9f135b-a85e-430c-bad4-4c67ef5fc12c}</Project>
</ProjectReference>
<ProjectReference Include="..\..\..\Externals\SOIL\SOIL.vcxproj">
<Project>{b441cc62-877e-4b3f-93e0-0de80544f705}</Project>
</ProjectReference>
<ProjectReference Include="..\..\..\Externals\zlib\zlib.vcxproj">
<Project>{ff213b23-2c26-4214-9f88-85271e557e87}</Project>
</ProjectReference>
<ProjectReference Include="..\Common\Common.vcxproj">
<Project>{2e6c348c-c75c-4d94-8d1e-9c1fcbf3efe4}</Project>
</ProjectReference>
Expand Down