memory-safety improved
This commit is contained in:
@@ -63,7 +63,7 @@ bool assetload::atlas(Assets* assets,
|
|||||||
if (builder.has(name)) {
|
if (builder.has(name)) {
|
||||||
continue; // skip duplicates
|
continue; // skip duplicates
|
||||||
}
|
}
|
||||||
std::unique_ptr<ImageData> image (png::load_image(file.string()));
|
std::unique_ptr<ImageData> image (png::load_image(file.string()));//but what if load_image return nullptr?
|
||||||
image->fixAlphaColor();
|
image->fixAlphaColor();
|
||||||
builder.add(name, image.release());
|
builder.add(name, image.release());
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-4
@@ -176,6 +176,7 @@ bool load_wav_file_header(std::ifstream& file,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// after that user must free returned memory by himself!
|
||||||
char* load_wav(const std::string& filename,
|
char* load_wav(const std::string& filename,
|
||||||
std::uint8_t& channels,
|
std::uint8_t& channels,
|
||||||
std::int32_t& sampleRate,
|
std::int32_t& sampleRate,
|
||||||
@@ -192,8 +193,13 @@ char* load_wav(const std::string& filename,
|
|||||||
}
|
}
|
||||||
|
|
||||||
char* data = new char[size];
|
char* data = new char[size];
|
||||||
|
try {
|
||||||
in.read(data, size);
|
in.read(data, size);
|
||||||
|
return data;
|
||||||
return data;
|
}
|
||||||
|
catch (const std::exception&) {
|
||||||
|
delete[] data;
|
||||||
|
std::cerr << "ERROR: Could not load wav data of \"" << filename << "\"" << std::endl;
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+48
-27
@@ -179,6 +179,8 @@ ImageData* _png_load(const char* file){
|
|||||||
printf( "Color type %d not supported!\n",
|
printf( "Color type %d not supported!\n",
|
||||||
png_get_color_type( png_ptr, info_ptr ) );
|
png_get_color_type( png_ptr, info_ptr ) );
|
||||||
png_destroy_read_struct( &png_ptr, &info_ptr, &end_info );
|
png_destroy_read_struct( &png_ptr, &info_ptr, &end_info );
|
||||||
|
delete[] image_data;
|
||||||
|
fclose(f);
|
||||||
return nullptr;
|
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);
|
||||||
@@ -241,67 +243,83 @@ ImageData* _png_load(const char* file){
|
|||||||
png = fopen(file, "rb");
|
png = fopen(file, "rb");
|
||||||
if (png == nullptr){
|
if (png == nullptr){
|
||||||
std::cerr << "could not to open file " << file << std::endl;
|
std::cerr << "could not to open file " << file << std::endl;
|
||||||
return 0;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
fseek(png, 0, SEEK_END);
|
fseek(png, 0, SEEK_END);
|
||||||
long siz_pngbuf = ftell(png);
|
long siz_pngbuf = ftell(png);
|
||||||
rewind(png);
|
rewind(png);
|
||||||
if(siz_pngbuf < 1) {
|
if(siz_pngbuf < 1) {
|
||||||
|
fclose(png);
|
||||||
std::cerr << "could not to read file " << file << std::endl;
|
std::cerr << "could not to read file " << file << std::endl;
|
||||||
return 0;
|
return nullptr;
|
||||||
}
|
}
|
||||||
pngbuf = new char[siz_pngbuf];
|
pngbuf = new char[siz_pngbuf];
|
||||||
if(fread(pngbuf, siz_pngbuf, 1, png) != 1){
|
if(fread(pngbuf, siz_pngbuf, 1, png) != 1){
|
||||||
|
fclose(png);
|
||||||
|
delete[] pngbuf;
|
||||||
std::cerr << "fread() failed" << std::endl;
|
std::cerr << "fread() failed" << std::endl;
|
||||||
return 0;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
fclose(png); // <- finally closing file
|
||||||
ctx = spng_ctx_new(0);
|
ctx = spng_ctx_new(0);
|
||||||
if (ctx == nullptr){
|
if (ctx == nullptr){
|
||||||
|
delete[] pngbuf;
|
||||||
std::cerr << "spng_ctx_new() failed" << std::endl;
|
std::cerr << "spng_ctx_new() failed" << std::endl;
|
||||||
return 0;
|
return nullptr;
|
||||||
}
|
}
|
||||||
r = spng_set_crc_action(ctx, SPNG_CRC_USE, SPNG_CRC_USE);
|
r = spng_set_crc_action(ctx, SPNG_CRC_USE, SPNG_CRC_USE);
|
||||||
if (r){
|
if (r){
|
||||||
|
delete[] pngbuf;
|
||||||
|
spng_ctx_free(ctx);
|
||||||
std::cerr << "spng_set_crc_action() error: " << spng_strerror(r) << std::endl;
|
std::cerr << "spng_set_crc_action() error: " << spng_strerror(r) << std::endl;
|
||||||
return 0;
|
return nullptr;
|
||||||
}
|
}
|
||||||
r = spng_set_png_buffer(ctx, pngbuf, siz_pngbuf);
|
r = spng_set_png_buffer(ctx, pngbuf, siz_pngbuf);
|
||||||
if (r){
|
if (r){
|
||||||
|
delete[] pngbuf;
|
||||||
|
spng_ctx_free(ctx);
|
||||||
std::cerr << "spng_set_png_buffer() error: " << spng_strerror(r) << std::endl;
|
std::cerr << "spng_set_png_buffer() error: " << spng_strerror(r) << std::endl;
|
||||||
return 0;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
spng_ihdr ihdr;
|
spng_ihdr ihdr;
|
||||||
r = spng_get_ihdr(ctx, &ihdr);
|
r = spng_get_ihdr(ctx, &ihdr);
|
||||||
if (r){
|
if (r){
|
||||||
|
delete[] pngbuf;
|
||||||
|
spng_ctx_free(ctx);
|
||||||
std::cerr << "spng_get_ihdr() error: " << spng_strerror(r) << std::endl;
|
std::cerr << "spng_get_ihdr() error: " << spng_strerror(r) << std::endl;
|
||||||
return 0;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
//// Unused "something"
|
||||||
const char *clr_type_str;
|
//const char *clr_type_str;
|
||||||
if(ihdr.color_type == SPNG_COLOR_TYPE_GRAYSCALE)
|
//if(ihdr.color_type == SPNG_COLOR_TYPE_GRAYSCALE)
|
||||||
clr_type_str = "grayscale";
|
// clr_type_str = "grayscale";
|
||||||
else if(ihdr.color_type == SPNG_COLOR_TYPE_TRUECOLOR)
|
//else if(ihdr.color_type == SPNG_COLOR_TYPE_TRUECOLOR)
|
||||||
clr_type_str = "truecolor";
|
// clr_type_str = "truecolor";
|
||||||
else if(ihdr.color_type == SPNG_COLOR_TYPE_INDEXED)
|
//else if(ihdr.color_type == SPNG_COLOR_TYPE_INDEXED)
|
||||||
clr_type_str = "indexed color";
|
// clr_type_str = "indexed color";
|
||||||
else if(ihdr.color_type == SPNG_COLOR_TYPE_GRAYSCALE_ALPHA)
|
//else if(ihdr.color_type == SPNG_COLOR_TYPE_GRAYSCALE_ALPHA)
|
||||||
clr_type_str = "grayscale with alpha";
|
// clr_type_str = "grayscale with alpha";
|
||||||
else
|
//else
|
||||||
clr_type_str = "truecolor with alpha";
|
// clr_type_str = "truecolor with alpha";
|
||||||
|
|
||||||
size_t out_size;
|
size_t out_size;
|
||||||
r = spng_decoded_image_size(ctx, SPNG_FMT_RGBA8, &out_size);
|
r = spng_decoded_image_size(ctx, SPNG_FMT_RGBA8, &out_size);
|
||||||
if (r){
|
if (r){
|
||||||
|
delete[] pngbuf;
|
||||||
|
spng_ctx_free(ctx);
|
||||||
std::cerr << "spng_decoded_image_size() error: " << spng_strerror(r) << std::endl;
|
std::cerr << "spng_decoded_image_size() error: " << spng_strerror(r) << std::endl;
|
||||||
return 0;
|
return nullptr;
|
||||||
}
|
}
|
||||||
out = new unsigned char[out_size];
|
out = new unsigned char[out_size];
|
||||||
r = spng_decode_image(ctx, out, out_size, SPNG_FMT_RGBA8, 0);
|
r = spng_decode_image(ctx, out, out_size, SPNG_FMT_RGBA8, 0);
|
||||||
if (r){
|
if (r){
|
||||||
|
delete[] out;
|
||||||
|
delete[] pngbuf;
|
||||||
|
spng_ctx_free(ctx);
|
||||||
std::cerr << "spng_decode_image() error: " << spng_strerror(r) << std::endl;
|
std::cerr << "spng_decode_image() error: " << spng_strerror(r) << std::endl;
|
||||||
return 0;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char* flipped = new unsigned char[out_size];
|
unsigned char* flipped = new unsigned char[out_size];
|
||||||
@@ -312,27 +330,30 @@ ImageData* _png_load(const char* file){
|
|||||||
flipped[(ihdr.height-i-1)*rowsize+j] = out[i*rowsize+j];
|
flipped[(ihdr.height-i-1)*rowsize+j] = out[i*rowsize+j];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
delete[] out;
|
delete[] out; // <- finally delete out
|
||||||
|
|
||||||
unsigned int texture;
|
|
||||||
|
|
||||||
ImageData* image = new ImageData(ImageFormat::rgba8888, ihdr.width, ihdr.height, (void*)flipped);
|
ImageData* image = new ImageData(ImageFormat::rgba8888, ihdr.width, ihdr.height, (void*)flipped);
|
||||||
|
|
||||||
spng_ctx_free(ctx);
|
|
||||||
delete[] pngbuf;
|
delete[] pngbuf;
|
||||||
|
spng_ctx_free(ctx);
|
||||||
|
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
ImageData* png::load_image(std::string filename) {
|
ImageData* png::load_image(std::string filename) {
|
||||||
return _png_load(filename.c_str());
|
ImageData* image (_png_load(filename.c_str()));
|
||||||
|
if (image == nullptr) {
|
||||||
|
std::cerr << "Could not load image " << filename << std::endl;
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
Texture* png::load_texture(std::string filename) {
|
Texture* png::load_texture(std::string filename) {
|
||||||
unique_ptr<ImageData> image (_png_load(filename.c_str()));
|
unique_ptr<ImageData> image (_png_load(filename.c_str()));
|
||||||
if (image == nullptr){
|
if (image == nullptr){
|
||||||
std::cerr << "Could not load image " << filename << std::endl;
|
std::cerr << "Could not load texture " << filename << std::endl;
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
return Texture::from(image.get());
|
return Texture::from(image.get());
|
||||||
|
|||||||
Reference in New Issue
Block a user