2015-05-16 10:32:05 +00:00
|
|
|
/*
|
2015-05-17 14:58:50 +00:00
|
|
|
* Copyright (C) 2015-2015 Gregory hainaut
|
2015-05-16 10:32:05 +00:00
|
|
|
*
|
|
|
|
* This Program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This Program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with GNU Make; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "GSPng.h"
|
2016-02-21 09:59:49 +00:00
|
|
|
#include <zlib.h>
|
|
|
|
#include <png.h>
|
|
|
|
|
|
|
|
struct {
|
|
|
|
int type;
|
|
|
|
int bytes_per_pixel_in;
|
|
|
|
int bytes_per_pixel_out;
|
|
|
|
int channel_bit_depth;
|
|
|
|
const char *extension[2];
|
|
|
|
} static const pixel[GSPng::Format::COUNT] = {
|
|
|
|
{PNG_COLOR_TYPE_RGBA, 4, 4, 8 , {"_full.png", nullptr}}, // RGBA_PNG
|
|
|
|
{PNG_COLOR_TYPE_RGB , 4, 3, 8 , {".png", nullptr}}, // RGB_PNG
|
|
|
|
{PNG_COLOR_TYPE_RGB , 4, 3, 8 , {".png", "_alpha.png"}}, // RGB_A_PNG
|
|
|
|
{PNG_COLOR_TYPE_GRAY, 4, 1, 8 , {"_alpha.png", nullptr}}, // ALPHA_PNG
|
|
|
|
{PNG_COLOR_TYPE_GRAY, 1, 1, 8 , {"_R8I.png", nullptr}}, // R8I_PNG
|
|
|
|
{PNG_COLOR_TYPE_GRAY, 2, 2, 16, {"_R16I.png", nullptr}}, // R16I_PNG
|
|
|
|
{PNG_COLOR_TYPE_GRAY, 4, 2, 16, {"_R32I_lsb.png", "_R32I_msb.png"}}, // R32I_PNG
|
|
|
|
};
|
2015-05-16 10:32:05 +00:00
|
|
|
|
|
|
|
namespace GSPng {
|
|
|
|
|
2017-05-26 15:26:46 +00:00
|
|
|
bool SaveFile(const std::string& file, const Format fmt, const uint8* const image,
|
2016-12-12 17:39:05 +00:00
|
|
|
uint8* const row, const int width, const int height, const int pitch,
|
|
|
|
const int compression, const bool rb_swapped = false, const bool first_image = false)
|
2016-02-21 09:59:49 +00:00
|
|
|
{
|
2016-12-12 17:39:05 +00:00
|
|
|
const int channel_bit_depth = pixel[fmt].channel_bit_depth;
|
|
|
|
const int bytes_per_pixel_in = pixel[fmt].bytes_per_pixel_in;
|
2016-02-21 09:59:49 +00:00
|
|
|
|
2016-08-12 17:13:44 +00:00
|
|
|
const int type = first_image ? pixel[fmt].type : PNG_COLOR_TYPE_GRAY;
|
|
|
|
const int offset = first_image ? 0 : pixel[fmt].bytes_per_pixel_out;
|
|
|
|
const int bytes_per_pixel_out = first_image ? pixel[fmt].bytes_per_pixel_out : bytes_per_pixel_in - offset;
|
|
|
|
|
2016-02-21 09:59:49 +00:00
|
|
|
FILE *fp = fopen(file.c_str(), "wb");
|
|
|
|
if (fp == nullptr)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
|
|
|
|
png_infop info_ptr = nullptr;
|
|
|
|
|
2017-01-11 17:23:34 +00:00
|
|
|
bool success;
|
2016-02-21 09:59:49 +00:00
|
|
|
try {
|
|
|
|
if (png_ptr == nullptr)
|
|
|
|
throw GSDXRecoverableError();
|
|
|
|
|
|
|
|
info_ptr = png_create_info_struct(png_ptr);
|
|
|
|
if (info_ptr == nullptr)
|
|
|
|
throw GSDXRecoverableError();
|
|
|
|
|
|
|
|
if (setjmp(png_jmpbuf(png_ptr)))
|
|
|
|
throw GSDXRecoverableError();
|
|
|
|
|
|
|
|
png_init_io(png_ptr, fp);
|
2016-02-24 21:52:17 +00:00
|
|
|
png_set_compression_level(png_ptr, compression);
|
2016-02-21 09:59:49 +00:00
|
|
|
png_set_IHDR(png_ptr, info_ptr, width, height, channel_bit_depth, type,
|
|
|
|
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
|
|
|
|
png_write_info(png_ptr, info_ptr);
|
|
|
|
|
|
|
|
if (channel_bit_depth > 8)
|
|
|
|
png_set_swap(png_ptr);
|
|
|
|
if (rb_swapped && type != PNG_COLOR_TYPE_GRAY)
|
|
|
|
png_set_bgr(png_ptr);
|
|
|
|
|
2016-12-12 17:39:05 +00:00
|
|
|
for (int y = 0; y < height; ++y) {
|
2016-02-21 09:59:49 +00:00
|
|
|
for (int x = 0; x < width; ++x)
|
|
|
|
for (int i = 0; i < bytes_per_pixel_out; ++i)
|
2016-12-12 17:39:05 +00:00
|
|
|
row[bytes_per_pixel_out * x + i] = image[y * pitch + bytes_per_pixel_in * x + i + offset];
|
2016-02-21 09:59:49 +00:00
|
|
|
png_write_row(png_ptr, row);
|
|
|
|
}
|
|
|
|
png_write_end(png_ptr, nullptr);
|
|
|
|
|
2016-12-12 23:00:43 +00:00
|
|
|
success = true;
|
2016-02-24 21:52:17 +00:00
|
|
|
} catch (GSDXRecoverableError&) {
|
2016-02-21 09:59:49 +00:00
|
|
|
fprintf(stderr, "Failed to write image %s\n", file.c_str());
|
2017-01-11 17:23:34 +00:00
|
|
|
|
|
|
|
success = false;
|
2016-02-21 09:59:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (png_ptr)
|
|
|
|
png_destroy_write_struct(&png_ptr, info_ptr ? &info_ptr : nullptr);
|
|
|
|
fclose(fp);
|
|
|
|
|
2016-12-12 23:00:43 +00:00
|
|
|
return success;
|
2016-02-21 09:59:49 +00:00
|
|
|
}
|
|
|
|
|
2017-05-26 15:26:46 +00:00
|
|
|
bool Save(GSPng::Format fmt, const std::string& file, uint8* image, int w, int h, int pitch, int compression, bool rb_swapped)
|
2015-05-16 10:32:05 +00:00
|
|
|
{
|
|
|
|
std::string root = file;
|
2016-02-21 09:59:49 +00:00
|
|
|
root.replace(file.length() - 4, 4, "");
|
|
|
|
|
|
|
|
ASSERT(fmt >= Format::START && fmt < Format::COUNT);
|
|
|
|
|
2016-12-12 17:39:05 +00:00
|
|
|
if (compression < 0 || compression > Z_BEST_COMPRESSION)
|
|
|
|
compression = Z_BEST_SPEED;
|
|
|
|
|
2016-02-21 09:59:49 +00:00
|
|
|
std::unique_ptr<uint8[]> row(new uint8[pixel[fmt].bytes_per_pixel_out * w]);
|
|
|
|
|
|
|
|
std::string filename = root + pixel[fmt].extension[0];
|
2016-02-24 21:52:17 +00:00
|
|
|
if (!SaveFile(filename, fmt, image, row.get(), w, h, pitch, compression, rb_swapped, true))
|
2016-02-21 09:59:49 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Second image
|
|
|
|
if (pixel[fmt].extension[1] == nullptr)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
filename = root + pixel[fmt].extension[1];
|
2016-02-24 21:52:17 +00:00
|
|
|
return SaveFile(filename, fmt, image, row.get(), w, h, pitch, compression);
|
2015-05-16 10:32:05 +00:00
|
|
|
}
|
2015-05-18 13:40:42 +00:00
|
|
|
|
2017-05-26 15:26:46 +00:00
|
|
|
Transaction::Transaction(GSPng::Format fmt, const std::string& file, const uint8* image, int w, int h, int pitch, int compression)
|
2016-02-24 21:52:17 +00:00
|
|
|
: m_fmt(fmt), m_file(file), m_w(w), m_h(h), m_pitch(pitch), m_compression(compression)
|
2015-05-18 15:23:43 +00:00
|
|
|
{
|
|
|
|
// Note: yes it would be better to use shared pointer
|
2016-02-21 09:59:49 +00:00
|
|
|
m_image = (uint8*)_aligned_malloc(pitch*h, 32);
|
2015-05-18 15:23:43 +00:00
|
|
|
if (m_image)
|
|
|
|
memcpy(m_image, image, pitch*h);
|
|
|
|
}
|
|
|
|
|
|
|
|
Transaction::~Transaction()
|
|
|
|
{
|
|
|
|
if (m_image)
|
|
|
|
_aligned_free(m_image);
|
|
|
|
}
|
|
|
|
|
gsdx: Make GSJobQueue non-inheritable
In the previous code, the threads were created and destroyed in the base
class constructor and destructor, so the threads could potentially be
active while the object is in a partially constructed or destroyed state.
The thread however, relies on a virtual function to process the queue
items, and the vtable might not be in the desired state when the object
is partially constructed or destroyed.
This probably only matters during object destruction - no items are in
the queue during object construction so the virtual function won't be
called, but items may still be queued up when the destructor is called,
so the virtual function can be called. It wasn't an issue because all
uses of the thread explicitly waited for the queues to be empty before
invoking the destructor.
Adjust the constructor to take a std::function parameter, which the
thread will use instead to process queue items, and avoid inheriting
from the GSJobQueue class. This will also eliminate the need to
explicitly wait for all jobs to finish (unless there are other external
factors, of course), which would probably make future code safer.
2016-11-09 01:34:48 +00:00
|
|
|
void Process(std::shared_ptr<Transaction>& item)
|
2015-05-18 15:23:43 +00:00
|
|
|
{
|
2016-02-24 21:52:17 +00:00
|
|
|
Save(item->m_fmt, item->m_file, item->m_image, item->m_w, item->m_h, item->m_pitch, item->m_compression);
|
2015-05-18 15:23:43 +00:00
|
|
|
}
|
2015-05-18 13:40:42 +00:00
|
|
|
|
2015-05-16 10:32:05 +00:00
|
|
|
}
|