Minor refactor
This commit is contained in:
parent
e1420a2c57
commit
5bd78c09ad
@ -8,8 +8,6 @@
|
||||
#include "../graphics/Texture.h"
|
||||
#include "../files/files.h"
|
||||
|
||||
using std::unique_ptr;
|
||||
|
||||
#ifndef _WIN32
|
||||
#define LIBPNG
|
||||
#endif
|
||||
@ -19,8 +17,6 @@ using std::unique_ptr;
|
||||
|
||||
// returns 0 if all-right, 1 otherwise
|
||||
int _png_write(const char* filename, uint width, uint height, const ubyte* data, bool alpha) {
|
||||
png_structp png_ptr = nullptr;
|
||||
png_infop info_ptr = nullptr;
|
||||
uint pixsize = alpha ? 4 : 3;
|
||||
|
||||
// Open file for writing (binary mode)
|
||||
@ -32,7 +28,7 @@ int _png_write(const char* filename, uint width, uint height, const ubyte* data,
|
||||
}
|
||||
|
||||
// Initialize write structure
|
||||
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
|
||||
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
|
||||
if (png_ptr == nullptr) {
|
||||
fprintf(stderr, "Could not allocate write struct\n");
|
||||
fclose(fp);
|
||||
@ -41,7 +37,7 @@ int _png_write(const char* filename, uint width, uint height, const ubyte* data,
|
||||
}
|
||||
|
||||
// Initialize info structure
|
||||
info_ptr = png_create_info_struct(png_ptr);
|
||||
png_infop info_ptr = png_create_info_struct(png_ptr);
|
||||
if (info_ptr == nullptr) {
|
||||
fprintf(stderr, "Could not allocate info struct\n");
|
||||
fclose(fp);
|
||||
@ -73,7 +69,7 @@ int _png_write(const char* filename, uint width, uint height, const ubyte* data,
|
||||
|
||||
png_write_info(png_ptr, info_ptr);
|
||||
|
||||
unique_ptr<png_byte[]> row(new png_byte[pixsize * width]);
|
||||
std::unique_ptr<png_byte[]> row(new png_byte[pixsize * width]);
|
||||
|
||||
// Write image data
|
||||
for (uint y = 0; y < height; y++) {
|
||||
@ -95,15 +91,11 @@ int _png_write(const char* filename, uint width, uint height, const ubyte* data,
|
||||
}
|
||||
|
||||
ImageData* _png_load(const char* file){
|
||||
FILE *f = nullptr;
|
||||
bool is_png = false;
|
||||
int bit_depth, color_type, row_bytes;
|
||||
png_infop info_ptr, end_info;
|
||||
png_uint_32 t_width, t_height;
|
||||
png_byte header[8], *image_data;
|
||||
png_bytepp row_pointers;
|
||||
png_structp png_ptr;
|
||||
png_byte header[8];
|
||||
|
||||
FILE *f = nullptr;
|
||||
if ((f = fopen(file, "r")) == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
@ -112,28 +104,28 @@ ImageData* _png_load(const char* file){
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
is_png = (png_sig_cmp(header, 0, 8) == 0);
|
||||
bool is_png = (png_sig_cmp(header, 0, 8) == 0);
|
||||
if (!is_png) {
|
||||
fclose(f);
|
||||
return nullptr;
|
||||
}
|
||||
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr,
|
||||
png_struct* png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr,
|
||||
nullptr, nullptr);
|
||||
if (png_ptr == nullptr) {
|
||||
fclose(f);
|
||||
return nullptr;
|
||||
}
|
||||
info_ptr = png_create_info_struct(png_ptr);
|
||||
png_info* info_ptr = png_create_info_struct(png_ptr);
|
||||
if (info_ptr == nullptr) {
|
||||
png_destroy_read_struct( &png_ptr, (png_infopp) nullptr,
|
||||
(png_infopp) nullptr );
|
||||
png_destroy_read_struct(&png_ptr, (png_info**) nullptr,
|
||||
(png_info**) nullptr);
|
||||
fclose(f);
|
||||
return nullptr;
|
||||
}
|
||||
end_info = png_create_info_struct(png_ptr);
|
||||
png_info* end_info = png_create_info_struct(png_ptr);
|
||||
if (end_info == nullptr) {
|
||||
png_destroy_read_struct(&png_ptr, (png_infopp) nullptr,
|
||||
(png_infopp) nullptr);
|
||||
png_destroy_read_struct(&png_ptr, (png_info**) nullptr,
|
||||
(png_info**) nullptr);
|
||||
fclose(f);
|
||||
return nullptr;
|
||||
}
|
||||
@ -144,33 +136,34 @@ ImageData* _png_load(const char* file){
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
png_init_io( png_ptr, f );
|
||||
png_set_sig_bytes( png_ptr, 8 );
|
||||
png_read_info( png_ptr, info_ptr );
|
||||
png_get_IHDR( png_ptr, info_ptr, &t_width, &t_height, &bit_depth,
|
||||
&color_type, nullptr, nullptr, nullptr );
|
||||
png_read_update_info( png_ptr, info_ptr );
|
||||
row_bytes = png_get_rowbytes( png_ptr, info_ptr );
|
||||
image_data = new png_byte[row_bytes * t_height];
|
||||
png_init_io(png_ptr, f);
|
||||
png_set_sig_bytes(png_ptr, 8);
|
||||
png_read_info(png_ptr, info_ptr);
|
||||
png_get_IHDR(png_ptr, info_ptr, &t_width, &t_height, &bit_depth,
|
||||
&color_type, nullptr, nullptr, nullptr);
|
||||
png_read_update_info(png_ptr, info_ptr);
|
||||
row_bytes = png_get_rowbytes(png_ptr, info_ptr);
|
||||
|
||||
std::unique_ptr<png_byte[]> image_data (new png_byte[row_bytes * t_height]);
|
||||
if (!image_data) {
|
||||
png_destroy_read_struct( &png_ptr, &info_ptr, &end_info );
|
||||
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
|
||||
fclose(f);
|
||||
return nullptr;
|
||||
}
|
||||
row_pointers = (png_bytepp) malloc( t_height * sizeof(png_bytep) );
|
||||
if ( row_pointers == nullptr ) {
|
||||
png_destroy_read_struct( &png_ptr, &info_ptr, &end_info );
|
||||
delete[] image_data;
|
||||
|
||||
std::unique_ptr<png_byte*[]> row_pointers (new png_byte*[t_height]);
|
||||
if (row_pointers == nullptr) {
|
||||
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
|
||||
fclose(f);
|
||||
return nullptr;
|
||||
}
|
||||
for (uint i = 0; i < t_height; ++i ) {
|
||||
row_pointers[t_height - 1 - i] = image_data + i * row_bytes;
|
||||
row_pointers[t_height - 1 - i] = image_data.get() + i * row_bytes;
|
||||
}
|
||||
png_read_image(png_ptr, row_pointers);
|
||||
png_read_image(png_ptr, row_pointers.get());
|
||||
|
||||
ImageFormat format;
|
||||
switch ( png_get_color_type( png_ptr, info_ptr ) ) {
|
||||
switch (png_get_color_type(png_ptr, info_ptr)) {
|
||||
case PNG_COLOR_TYPE_RGBA:
|
||||
format = ImageFormat::rgba8888;
|
||||
break;
|
||||
@ -178,18 +171,15 @@ ImageData* _png_load(const char* file){
|
||||
format = ImageFormat::rgb888;
|
||||
break;
|
||||
default:
|
||||
printf( "Color type %d not supported!\n",
|
||||
png_get_color_type( png_ptr, info_ptr ) );
|
||||
png_destroy_read_struct( &png_ptr, &info_ptr, &end_info );
|
||||
delete[] image_data;
|
||||
free( row_pointers );
|
||||
printf("Color type %d not supported!\n",
|
||||
png_get_color_type(png_ptr, info_ptr));
|
||||
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
|
||||
fclose(f);
|
||||
return nullptr;
|
||||
}
|
||||
ImageData* image = new ImageData(format, t_width, t_height, (void*)image_data);
|
||||
ImageData* image = new ImageData(format, t_width, t_height, (void*)image_data.release());
|
||||
png_destroy_read_struct( &png_ptr, &info_ptr, &end_info );
|
||||
free( row_pointers );
|
||||
fclose( f );
|
||||
fclose(f);
|
||||
return image;
|
||||
}
|
||||
#else
|
||||
@ -335,7 +325,7 @@ ImageData* _png_load(const char* file){
|
||||
flipped[(ihdr.height-i-1)*rowsize+j] = out[i*rowsize+j];
|
||||
}
|
||||
}
|
||||
delete[] out; // <- finally delete out
|
||||
delete[] out; // <- finally delete out // no, delete spng usage
|
||||
|
||||
ImageData* image = new ImageData(ImageFormat::rgba8888, ihdr.width, ihdr.height, (void*)flipped);
|
||||
|
||||
@ -356,7 +346,7 @@ ImageData* png::load_image(std::string filename) {
|
||||
}
|
||||
|
||||
Texture* png::load_texture(std::string filename) {
|
||||
unique_ptr<ImageData> image (_png_load(filename.c_str()));
|
||||
std::unique_ptr<ImageData> image (_png_load(filename.c_str()));
|
||||
if (image == nullptr){
|
||||
std::cerr << "Could not load texture " << filename << std::endl;
|
||||
return nullptr;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user