Skip to content

Commit

Permalink
Merge pull request #67 from A-lex-Ra/main
Browse files Browse the repository at this point in the history
rays efficiency improve + refactoring
  • Loading branch information
MihailRis authored Dec 19, 2023
2 parents 082599e + 73abc23 commit aefc9aa
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 137 deletions.
81 changes: 43 additions & 38 deletions src/coders/png.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,36 @@ using std::unique_ptr;
#ifdef LIBPNG
#include <png.h>

// 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 = NULL;
png_infop info_ptr = NULL;
png_structp png_ptr = nullptr;
png_infop info_ptr = nullptr;
uint pixsize = alpha ? 4 : 3;

// Open file for writing (binary mode)
FILE* fp = fopen(filename, "wb");
if (fp == NULL) {
if (fp == nullptr) {
fprintf(stderr, "Could not open file %s for writing\n", filename);
fclose(fp);
return 1;
}

// Initialize write structure
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
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);
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
png_destroy_write_struct(&png_ptr, (png_infopp)nullptr);
return 1;
}

// Initialize info structure
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
if (info_ptr == nullptr) {
fprintf(stderr, "Could not allocate info struct\n");
fclose(fp);
png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
png_destroy_write_struct(&png_ptr, (png_infopp)nullptr);
return 1;

}
Expand All @@ -55,7 +56,7 @@ int _png_write(const char* filename, uint width, uint height, const ubyte* data,
fprintf(stderr, "Error during png creation\n");
fclose(fp);
png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
png_destroy_write_struct(&png_ptr, (png_infopp)nullptr);
return 1;
}

Expand Down Expand Up @@ -85,58 +86,59 @@ int _png_write(const char* filename, uint width, uint height, const ubyte* data,
}

// End write
png_write_end(png_ptr, NULL);
png_write_end(png_ptr, nullptr);

fclose(fp);
png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
png_destroy_write_struct(&png_ptr, (png_infopp)nullptr);
return 0;
}

ImageData* _png_load(const char* file){
FILE *f;
int is_png, bit_depth, color_type, row_bytes;
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;

if (!(f = fopen(file, "r"))) {
if ((f = fopen(file, "r")) == nullptr) {
return nullptr;
}
if (fread(header, 1, 8, f) < 8) {
if (fread(header, 1, 8, f) < 8) { // check of read elements count
fclose(f);
return nullptr;
}

is_png = !png_sig_cmp(header, 0, 8);
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, NULL,
NULL, NULL);
if (!png_ptr) {
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);
if (!info_ptr) {
png_destroy_read_struct( &png_ptr, (png_infopp) NULL,
(png_infopp) NULL );
if (info_ptr == nullptr) {
png_destroy_read_struct( &png_ptr, (png_infopp) nullptr,
(png_infopp) nullptr );
fclose(f);
return nullptr;
}
end_info = png_create_info_struct(png_ptr);
if (!end_info) {
png_destroy_read_struct(&png_ptr, (png_infopp) NULL,
(png_infopp) NULL);
if (end_info == nullptr) {
png_destroy_read_struct(&png_ptr, (png_infopp) nullptr,
(png_infopp) nullptr);
fclose(f);
return nullptr;
}

if (setjmp(png_jmpbuf(png_ptr))) {
if (setjmp(png_jmpbuf(png_ptr)) != 0) {
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
fclose(f);
return nullptr;
Expand All @@ -146,7 +148,7 @@ ImageData* _png_load(const char* file){
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, NULL, NULL, NULL );
&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];
Expand All @@ -156,7 +158,7 @@ ImageData* _png_load(const char* file){
return nullptr;
}
row_pointers = (png_bytepp) malloc( t_height * sizeof(png_bytep) );
if ( !row_pointers ) {
if ( row_pointers == nullptr ) {
png_destroy_read_struct( &png_ptr, &info_ptr, &end_info );
delete[] image_data;
fclose(f);
Expand All @@ -180,6 +182,7 @@ ImageData* _png_load(const char* file){
png_get_color_type( png_ptr, info_ptr ) );
png_destroy_read_struct( &png_ptr, &info_ptr, &end_info );
delete[] image_data;
free( row_pointers );
fclose(f);
return nullptr;
}
Expand All @@ -194,10 +197,12 @@ ImageData* _png_load(const char* file){
#include <stdio.h>
#include <inttypes.h>

static const int SPNG_SUCCESS = 0;
//returns spng result code
int _png_write(const char* filename, uint width, uint height, const ubyte* data, bool alpha) {
int fmt;
int ret = 0;
spng_ctx* ctx = NULL;
spng_ctx* ctx = nullptr;
spng_ihdr ihdr = { 0 };
uint pixsize = alpha ? 4 : 3;

Expand All @@ -212,7 +217,7 @@ int _png_write(const char* filename, uint width, uint height, const ubyte* data,
spng_set_ihdr(ctx, &ihdr);
fmt = SPNG_FMT_PNG;
ret = spng_encode_image(ctx, data, (size_t)width * (size_t)height * pixsize , fmt, SPNG_ENCODE_FINALIZE);
if (ret) {
if (ret != SPNG_SUCCESS) {
printf("spng_encode_image() error: %s\n", spng_strerror(ret));
fflush(stdout);
spng_ctx_free(ctx);
Expand All @@ -222,7 +227,7 @@ int _png_write(const char* filename, uint width, uint height, const ubyte* data,
size_t png_size;
void* png_buf = spng_get_png_buffer(ctx, &png_size, &ret);

if (png_buf == NULL) {
if (png_buf == nullptr) {
printf("spng_get_png_buffer() error: %s\n", spng_strerror(ret));
}
else {
Expand All @@ -235,7 +240,7 @@ int _png_write(const char* filename, uint width, uint height, const ubyte* data,

ImageData* _png_load(const char* file){
int r = 0;
FILE *png;
FILE *png = nullptr;
char *pngbuf = nullptr;
spng_ctx *ctx = nullptr;
unsigned char *out = nullptr;
Expand All @@ -255,7 +260,7 @@ ImageData* _png_load(const char* file){
return nullptr;
}
pngbuf = new char[siz_pngbuf];
if(fread(pngbuf, siz_pngbuf, 1, png) != 1){
if(fread(pngbuf, siz_pngbuf, 1, png) != 1){ //check of read elements count
fclose(png);
delete[] pngbuf;
std::cerr << "fread() failed" << std::endl;
Expand All @@ -269,14 +274,14 @@ ImageData* _png_load(const char* file){
return nullptr;
}
r = spng_set_crc_action(ctx, SPNG_CRC_USE, SPNG_CRC_USE);
if (r){
if (r != SPNG_SUCCESS){
delete[] pngbuf;
spng_ctx_free(ctx);
std::cerr << "spng_set_crc_action() error: " << spng_strerror(r) << std::endl;
return nullptr;
}
r = spng_set_png_buffer(ctx, pngbuf, siz_pngbuf);
if (r){
if (r != SPNG_SUCCESS){
delete[] pngbuf;
spng_ctx_free(ctx);
std::cerr << "spng_set_png_buffer() error: " << spng_strerror(r) << std::endl;
Expand All @@ -285,7 +290,7 @@ ImageData* _png_load(const char* file){

spng_ihdr ihdr;
r = spng_get_ihdr(ctx, &ihdr);
if (r){
if (r != SPNG_SUCCESS){
delete[] pngbuf;
spng_ctx_free(ctx);
std::cerr << "spng_get_ihdr() error: " << spng_strerror(r) << std::endl;
Expand All @@ -306,15 +311,15 @@ ImageData* _png_load(const char* file){

size_t out_size;
r = spng_decoded_image_size(ctx, SPNG_FMT_RGBA8, &out_size);
if (r){
if (r != SPNG_SUCCESS){
delete[] pngbuf;
spng_ctx_free(ctx);
std::cerr << "spng_decoded_image_size() error: " << spng_strerror(r) << std::endl;
return nullptr;
}
out = new unsigned char[out_size];
r = spng_decode_image(ctx, out, out_size, SPNG_FMT_RGBA8, 0);
if (r){
if (r != SPNG_SUCCESS){
delete[] out;
delete[] pngbuf;
spng_ctx_free(ctx);
Expand Down
Loading

0 comments on commit aefc9aa

Please sign in to comment.