Implemented saving images.

This commit is contained in:
Relintai 2024-01-13 21:42:17 +01:00
parent ad6e3b57e1
commit 9752cd9069
6 changed files with 141 additions and 4 deletions

View File

@ -43,6 +43,21 @@ void GameScene::input_event(const Ref<InputEvent> &event) {
}
}
if (k->get_physical_scancode() == KEY_K) {
if (pressed) {
image->save_png("test_image_save.png");
image->save_bmp("test_image_save.bmp");
image->save_tga("test_image_save.tga");
image->save_jpg("test_image_save.jpg", 70);
Ref<Image> fimage = image->duplicate();
fimage->convert(Image::FORMAT_RGBAF);
fimage->save_hdr("test_image_save.hdr");
ERR_PRINT("Test images Saved!");
}
}
return;
}

View File

@ -8,9 +8,9 @@
#include "core/error_macros.h"
#include "core/hash_map.h"
#include "math.h"
#include "core/memory.h"
#include "core/vector3.h"
#include "math.h"
#include <memory.h>
#include <stdio.h>
@ -20,8 +20,8 @@
#define STB_SPRINTF_NOUNALIGNED // stb_sprintf
#include "3rd_stb_image.h"
#include "3rd_stb_image_write.h"
//{{FILE:3rd_stb_image_write.h}}
//---
#undef freelist
//--STRIP
@ -1643,6 +1643,113 @@ void Image::create(const char **p_xpm) {
line++;
}
}
Error Image::save_png(const String &file_name) {
ERR_FAIL_COND_V(format >= FORMAT_RF, ERR_UNAVAILABLE);
if (width == 0 || height == 0) {
return FAILED;
}
int pfs = get_format_pixel_size(format);
write_lock = true;
int ret = stbi_write_png(file_name.utf8().get_data(), width, height, pfs, data.ptr(), 0);
write_lock = false;
if (ret == 0) {
return FAILED;
}
return OK;
}
Error Image::save_bmp(const String &file_name) {
ERR_FAIL_COND_V(format >= FORMAT_RF, ERR_UNAVAILABLE);
if (width == 0 || height == 0) {
return FAILED;
}
int pfs = get_format_pixel_size(format);
write_lock = true;
int ret = stbi_write_bmp(file_name.utf8().get_data(), width, height, pfs, data.ptr());
write_lock = false;
if (ret == 0) {
return FAILED;
}
return OK;
}
Error Image::save_tga(const String &file_name) {
ERR_FAIL_COND_V(format >= FORMAT_RF, ERR_UNAVAILABLE);
if (width == 0 || height == 0) {
return FAILED;
}
int pfs = get_format_pixel_size(format);
write_lock = true;
int ret = stbi_write_tga(file_name.utf8().get_data(), width, height, pfs, data.ptr());
write_lock = false;
if (ret == 0) {
return FAILED;
}
return OK;
}
Error Image::save_jpg(const String &file_name, const int quality) {
ERR_FAIL_COND_V(format >= FORMAT_RF, ERR_UNAVAILABLE);
if (width == 0 || height == 0) {
return FAILED;
}
int pfs = get_format_pixel_size(format);
write_lock = true;
int ret = stbi_write_jpg(file_name.utf8().get_data(), width, height, pfs, data.ptr(), quality);
write_lock = false;
if (ret == 0) {
return FAILED;
}
return OK;
}
Error Image::save_hdr(const String &file_name) {
ERR_FAIL_COND_V(format < FORMAT_RF, ERR_UNAVAILABLE);
if (width == 0 || height == 0) {
return FAILED;
}
int pfs = get_format_pixel_size(format) / 4;
write_lock = true;
int ret = stbi_write_hdr(file_name.utf8().get_data(), width, height, pfs, ((float *)data.ptr()));
write_lock = false;
if (ret == 0) {
return FAILED;
}
return OK;
}
#define DETECT_ALPHA_MAX_THRESHOLD 254
#define DETECT_ALPHA_MIN_THRESHOLD 2
@ -2684,3 +2791,8 @@ Image::Image() {
Image::~Image() {
write_lock = false;
}
#undef DETECT_ALPHA_MAX_THRESHOLD
#undef DETECT_ALPHA_MIN_THRESHOLD
#undef DETECT_ALPHA
#undef DETECT_NON_ALPHA

View File

@ -12,9 +12,9 @@
#include "core/color.h"
#include "core/rect2.h"
#include "core/rect2i.h"
#include "object/reference.h"
#include "core/vector.h"
#include "core/vector2i.h"
#include "object/reference.h"
//--STRIP
class Image : public Reference {
@ -153,6 +153,14 @@ public:
void create(const char **p_xpm);
//Save
Error save_png(const String &file_name);
Error save_bmp(const String &file_name);
Error save_tga(const String &file_name);
Error save_jpg(const String &file_name, const int quality);
Error save_hdr(const String &file_name);
/**
* returns true when the image is empty (0,0) in size
*/

View File

@ -21,7 +21,8 @@
{{FILE:sfw/render_core/3rd_stb_image.h}}
//{//{//FILE:sfw/render_core/stb_image_write.h}}
{{FILE:sfw/render_core/3rd_stb_image_write.h}}
//---
#undef freelist

View File

@ -571,6 +571,7 @@
//#include "core/memory.h"
//#include "core/vector3.h"
//#include "3rd_stb_image.h"
//#include "3rd_stb_image_write.h"
//--STRIP
{{FILE:sfw/render_core/image.cpp}}
//--STRIP