2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 03:09:55 +00:00
|
|
|
// Refer to the license.txt file included.
|
2008-12-08 05:25:12 +00:00
|
|
|
|
|
|
|
#include <list>
|
2014-06-03 05:08:54 +00:00
|
|
|
#include <string>
|
2008-12-08 05:25:12 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2013-11-15 00:00:38 +00:00
|
|
|
#include "png.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/FileUtil.h"
|
2015-09-26 21:13:07 +00:00
|
|
|
#include "Common/MsgHandler.h"
|
|
|
|
#include "Common/Logging/Log.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/ImageWrite.h"
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2014-06-03 05:08:54 +00:00
|
|
|
bool SaveData(const std::string& filename, const char* data)
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2013-03-01 01:33:39 +00:00
|
|
|
std::ofstream f;
|
|
|
|
OpenFStream(f, filename, std::ios::binary);
|
2011-03-11 10:21:46 +00:00
|
|
|
f << data;
|
|
|
|
|
|
|
|
return true;
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
2013-11-15 00:00:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
TextureToPng
|
|
|
|
|
|
|
|
Inputs:
|
|
|
|
data : This is an array of RGBA with 8 bits per channel. 4 bytes for each pixel.
|
|
|
|
row_stride: Determines the amount of bytes per row of pixels.
|
|
|
|
*/
|
2014-02-23 22:03:39 +00:00
|
|
|
bool TextureToPng(u8* data, int row_stride, const std::string& filename, int width, int height, bool saveAlpha)
|
2013-11-15 00:00:38 +00:00
|
|
|
{
|
|
|
|
bool success = false;
|
|
|
|
|
|
|
|
if (!data)
|
|
|
|
return false;
|
|
|
|
|
2013-11-15 00:05:03 +00:00
|
|
|
char title[] = "Dolphin Screenshot";
|
2013-11-15 04:58:57 +00:00
|
|
|
char title_key[] = "Title";
|
2014-03-09 20:14:26 +00:00
|
|
|
png_structp png_ptr = nullptr;
|
|
|
|
png_infop info_ptr = nullptr;
|
2013-11-15 00:05:03 +00:00
|
|
|
|
2013-11-15 00:00:38 +00:00
|
|
|
// Open file for writing (binary mode)
|
2013-11-16 22:12:07 +00:00
|
|
|
File::IOFile fp(filename, "wb");
|
2014-08-30 20:51:27 +00:00
|
|
|
if (!fp.IsOpen())
|
|
|
|
{
|
2015-03-15 15:42:22 +00:00
|
|
|
PanicAlertT("Screenshot failed: Could not open file \"%s\" (error %d)", filename.c_str(), errno);
|
2013-11-15 00:00:38 +00:00
|
|
|
goto finalise;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize write structure
|
2014-03-09 20:14:26 +00:00
|
|
|
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
|
2014-08-30 20:51:27 +00:00
|
|
|
if (png_ptr == nullptr)
|
|
|
|
{
|
2015-03-15 15:42:22 +00:00
|
|
|
PanicAlert("Screenshot failed: Could not allocate write struct");
|
2013-11-15 00:00:38 +00:00
|
|
|
goto finalise;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize info structure
|
2013-11-15 00:05:03 +00:00
|
|
|
info_ptr = png_create_info_struct(png_ptr);
|
2014-08-30 20:51:27 +00:00
|
|
|
if (info_ptr == nullptr)
|
|
|
|
{
|
2015-03-15 15:42:22 +00:00
|
|
|
PanicAlert("Screenshot failed: Could not allocate info struct");
|
2013-11-15 00:00:38 +00:00
|
|
|
goto finalise;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup Exception handling
|
2014-08-30 20:51:27 +00:00
|
|
|
if (setjmp(png_jmpbuf(png_ptr)))
|
|
|
|
{
|
2015-03-15 15:42:22 +00:00
|
|
|
PanicAlert("Screenshot failed: Error during PNG creation");
|
2013-11-15 00:00:38 +00:00
|
|
|
goto finalise;
|
|
|
|
}
|
|
|
|
|
2013-11-16 22:12:07 +00:00
|
|
|
png_init_io(png_ptr, fp.GetHandle());
|
2013-11-15 00:00:38 +00:00
|
|
|
|
2014-11-14 02:28:27 +00:00
|
|
|
// Write header (8 bit color depth)
|
2013-11-15 00:00:38 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
png_text title_text;
|
|
|
|
title_text.compression = PNG_TEXT_COMPRESSION_NONE;
|
2013-11-15 04:58:57 +00:00
|
|
|
title_text.key = title_key;
|
2013-11-15 00:00:38 +00:00
|
|
|
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
|
2014-03-09 20:14:26 +00:00
|
|
|
png_write_end(png_ptr, nullptr);
|
2013-11-15 00:00:38 +00:00
|
|
|
|
|
|
|
success = true;
|
|
|
|
|
|
|
|
finalise:
|
2014-03-09 20:14:26 +00:00
|
|
|
if (info_ptr != nullptr) png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
|
|
|
|
if (png_ptr != nullptr) png_destroy_write_struct(&png_ptr, (png_infopp)nullptr);
|
2013-11-15 00:00:38 +00:00
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|