Fix image loading on windows.

This commit is contained in:
Relintai 2024-01-18 12:00:08 +01:00
parent f72f2a57df
commit 4be1205010
3 changed files with 25 additions and 8 deletions

1
.gitignore vendored
View File

@ -4,6 +4,7 @@ bin/
obj/
venv/
.cache/
.vs/
build/

View File

@ -54,6 +54,10 @@ void GameScene::input_event(const Ref<InputEvent> &event) {
fimage->convert(Image::FORMAT_RGBAF);
fimage->save_hdr("test_image_save.hdr");
Ref<Image> timg = texture->get_data();
timg->save_png("timg.png");
ERR_PRINT("Test images Saved!");
}
}
@ -300,8 +304,8 @@ void GameScene::render_obj() {
//camera->set_camera_transform(t);
//rot += 0.01;
Ref<Image> d = texture->get_data();
texture->create_from_image(d);
//Ref<Image> d = texture->get_data();
//texture->create_from_image(d);
camera->bind();

View File

@ -10,6 +10,7 @@
#include "core/hash_map.h"
#include "core/memory.h"
#include "core/vector3.h"
#include "core/file_access.h"
#include "math.h"
#include <memory.h>
#include <stdio.h>
@ -18,6 +19,7 @@
#define STB_IMAGE_WRITE_IMPLEMENTATION // stbi_write
#define STB_SPRINTF_IMPLEMENTATION // stb_sprintf
#define STB_SPRINTF_NOUNALIGNED // stb_sprintf
//#define STBI_WRITE_NO_STDIO
//#define STBI_WINDOWS_UTF8
//#define STBIW_WINDOWS_UTF8
@ -1411,9 +1413,10 @@ void Image::load_from_file(const String &file_name, Format p_format) {
int img_n = 4;
FILE *fp = fopen(file_name.utf8().get_data(), "r");
Error err;
Vector<uint8_t> file_data = FileAccess::get_file_as_array(file_name, &err);
ERR_FAIL_COND_MSG(!fp, "Couldn't open file! " + file_name);
ERR_FAIL_COND(err != OK);
//case FORMAT_RF:
//case FORMAT_RGF:
@ -1425,8 +1428,7 @@ void Image::load_from_file(const String &file_name, Format p_format) {
int y;
int n;
stbi_uc *pixels = stbi_load_from_file(fp, &x, &y, &n, img_n);
fclose(fp);
stbi_uc *pixels = stbi_load_from_memory(file_data.ptr(), file_data.size(), &x, &y, &n, img_n);
ERR_FAIL_COND_MSG(!pixels, "Couldn't load image! " + file_name);
@ -1659,14 +1661,24 @@ Error Image::save_png(const String &file_name) {
write_lock = true;
int ret = stbi_write_png(file_name.utf8().get_data(), width, height, pfs, data.ptr(), 0);
int out_length = 0;
uint8_t *ret_arr = stbi_write_png_to_mem(data.ptr(), 0, width, height, pfs, &out_length);
write_lock = false;
if (ret == 0) {
if (!ret_arr || out_length == 0) {
return FAILED;
}
FileAccess* f = FileAccess::open(file_name, FileAccess::WRITE);
if (!f) {
return FAILED;
}
f->store_buffer(ret_arr, out_length);
return OK;
}
Error Image::save_bmp(const String &file_name) {