Added graphics/ImageData class

This commit is contained in:
MihailRis
2023-11-12 03:26:06 +03:00
parent a91fbe7166
commit ffd3e3f852
9 changed files with 110 additions and 61 deletions
+31 -54
View File
@@ -2,6 +2,8 @@
#include <iostream>
#include <GL/glew.h>
#include "../graphics/ImageData.h"
#include "../graphics/Texture.h"
#ifndef _WIN32
@@ -11,7 +13,7 @@
#ifdef LIBPNG
#include <png.h>
int _png_load(const char* file, int* width, int* height){
ImageData* _png_load(const char* file, int* width, int* height){
FILE *f;
int is_png, bit_depth, color_type, row_bytes;
png_infop info_ptr, end_info;
@@ -19,42 +21,40 @@ int _png_load(const char* file, int* width, int* height){
png_byte header[8], *image_data;
png_bytepp row_pointers;
png_structp png_ptr;
GLuint texture;
int alpha;
if ( !( f = fopen(file, "r" ) ) ) {
return 0;
if (!( f = fopen(file, "r" ) ) ) {
return nullptr;
}
fread( header, 1, 8, f );
is_png = !png_sig_cmp( header, 0, 8 );
if ( !is_png ) {
fclose( f );
return 0;
return nullptr;
}
png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING, NULL,
NULL, NULL );
if ( !png_ptr ) {
fclose( f );
return 0;
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 );
fclose( f );
return 0;
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 );
fclose( f );
return 0;
return nullptr;
}
if ( setjmp( png_jmpbuf( png_ptr ) ) ) {
png_destroy_read_struct( &png_ptr, &info_ptr, &end_info );
fclose( f );
return 0;
return nullptr;
}
png_init_io( png_ptr, f );
png_set_sig_bytes( png_ptr, 8 );
@@ -65,56 +65,43 @@ int _png_load(const char* file, int* width, int* height){
*height = t_height;
png_read_update_info( png_ptr, info_ptr );
row_bytes = png_get_rowbytes( png_ptr, info_ptr );
image_data = (png_bytep) malloc( row_bytes * t_height * sizeof(png_byte) );
if ( !image_data ) {
image_data = new png_byte[row_bytes * t_height];
if (!image_data) {
png_destroy_read_struct( &png_ptr, &info_ptr, &end_info );
fclose( f );
return 0;
fclose(f);
return nullptr;
}
row_pointers = (png_bytepp) malloc( t_height * sizeof(png_bytep) );
if ( !row_pointers ) {
png_destroy_read_struct( &png_ptr, &info_ptr, &end_info );
free( image_data );
fclose( f );
return 0;
delete[] image_data;
fclose(f);
return nullptr;
}
for (unsigned int i = 0; i < t_height; ++i ) {
row_pointers[t_height - 1 - i] = image_data + i * row_bytes;
}
png_read_image( png_ptr, row_pointers );
png_read_image(png_ptr, row_pointers);
ImageFormat format;
switch ( png_get_color_type( png_ptr, info_ptr ) ) {
case PNG_COLOR_TYPE_RGBA:
alpha = GL_RGBA;
format = ImageFormat::rgba8888;
break;
case PNG_COLOR_TYPE_RGB:
alpha = GL_RGB;
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 );
return 0;
return nullptr;
}
// configure second post-processing framebuffer
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, t_width, t_height, 0,
alpha, GL_UNSIGNED_BYTE, (GLvoid *) image_data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
ImageData* image = new ImageData(format, *width, *height, (void*)image_data);
png_destroy_read_struct( &png_ptr, &info_ptr, &end_info );
free( image_data );
free( row_pointers );
fclose( f );
return texture;
return image;
}
#else
#include <spng.h>
@@ -205,18 +192,8 @@ int _png_load(const char* file, int* pwidth, int* pheight){
delete[] out;
unsigned int texture;
int alpha = GL_RGBA;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ihdr.width, ihdr.height, 0,
alpha, GL_UNSIGNED_BYTE, (GLvoid *) flipped);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glBindTexture(GL_TEXTURE_2D, 0);
ImageData* image = new ImageData(ImageFormat::rgba8888, ihdr.width, ihdr.height, (void*)flipped);
pwidth[0] = ihdr.width;
pheight[0] = ihdr.height;
@@ -224,17 +201,17 @@ int _png_load(const char* file, int* pwidth, int* pheight){
spng_ctx_free(ctx);
delete[] pngbuf;
return texture;
return image;
}
#endif
Texture* png::load_texture(std::string filename){
int width, height;
GLuint texture = _png_load(filename.c_str(), &width, &height);
if (texture == 0){
std::cerr << "Could not load texture " << filename << std::endl;
ImageData* image = _png_load(filename.c_str(), &width, &height);
if (image == nullptr){
std::cerr << "Could not load image " << filename << std::endl;
return nullptr;
}
return new Texture(texture, width, height);
return Texture::from(image);
}