summaryrefslogtreecommitdiff
path: root/modules/tinyexr/image_saver_tinyexr.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'modules/tinyexr/image_saver_tinyexr.cpp')
-rw-r--r--modules/tinyexr/image_saver_tinyexr.cpp60
1 files changed, 34 insertions, 26 deletions
diff --git a/modules/tinyexr/image_saver_tinyexr.cpp b/modules/tinyexr/image_saver_tinyexr.cpp
index 17f920746f..420619bd5f 100644
--- a/modules/tinyexr/image_saver_tinyexr.cpp
+++ b/modules/tinyexr/image_saver_tinyexr.cpp
@@ -58,7 +58,8 @@ static bool is_supported_format(Image::Format p_format) {
enum SrcPixelType {
SRC_FLOAT,
SRC_HALF,
- SRC_BYTE
+ SRC_BYTE,
+ SRC_UNSUPPORTED
};
static SrcPixelType get_source_pixel_type(Image::Format p_format) {
@@ -79,7 +80,7 @@ static SrcPixelType get_source_pixel_type(Image::Format p_format) {
case Image::FORMAT_RGBA8:
return SRC_BYTE;
default:
- CRASH_NOW();
+ return SRC_UNSUPPORTED;
}
}
@@ -101,7 +102,7 @@ static int get_target_pixel_type(Image::Format p_format) {
case Image::FORMAT_RGBA8:
return TINYEXR_PIXELTYPE_HALF;
default:
- CRASH_NOW();
+ return -1;
}
}
@@ -112,7 +113,7 @@ static int get_pixel_type_size(int p_pixel_type) {
case TINYEXR_PIXELTYPE_FLOAT:
return 4;
}
- CRASH_NOW();
+ return -1;
}
static int get_channel_count(Image::Format p_format) {
@@ -134,12 +135,11 @@ static int get_channel_count(Image::Format p_format) {
case Image::FORMAT_RGBA8:
return 4;
default:
- CRASH_NOW();
+ return -1;
}
}
Error save_exr(const String &p_path, const Ref<Image> &p_img, bool p_grayscale) {
-
Image::Format format = p_img->get_format();
if (!is_supported_format(format)) {
@@ -158,7 +158,7 @@ Error save_exr(const String &p_path, const Ref<Image> &p_img, bool p_grayscale)
// Godot does not support more than 4 channels,
// so we can preallocate header infos on the stack and use only the subset we need
- PoolByteArray channels[max_channels];
+ PackedByteArray channels[max_channels];
unsigned char *channels_ptrs[max_channels];
EXRChannelInfo channel_infos[max_channels];
int pixel_types[max_channels];
@@ -173,57 +173,57 @@ Error save_exr(const String &p_path, const Ref<Image> &p_img, bool p_grayscale)
};
int channel_count = get_channel_count(format);
+ ERR_FAIL_COND_V(channel_count < 0, ERR_UNAVAILABLE);
ERR_FAIL_COND_V(p_grayscale && channel_count != 1, ERR_INVALID_PARAMETER);
int target_pixel_type = get_target_pixel_type(format);
+ ERR_FAIL_COND_V(target_pixel_type < 0, ERR_UNAVAILABLE);
int target_pixel_type_size = get_pixel_type_size(target_pixel_type);
+ ERR_FAIL_COND_V(target_pixel_type_size < 0, ERR_UNAVAILABLE);
SrcPixelType src_pixel_type = get_source_pixel_type(format);
+ ERR_FAIL_COND_V(src_pixel_type == SRC_UNSUPPORTED, ERR_UNAVAILABLE);
const int pixel_count = p_img->get_width() * p_img->get_height();
const int *channel_mapping = channel_mappings[channel_count - 1];
{
- PoolByteArray src_data = p_img->get_data();
- PoolByteArray::Read src_r = src_data.read();
+ PackedByteArray src_data = p_img->get_data();
+ const uint8_t *src_r = src_data.ptr();
for (int channel_index = 0; channel_index < channel_count; ++channel_index) {
-
// De-interleave channels
- PoolByteArray &dst = channels[channel_index];
+ PackedByteArray &dst = channels[channel_index];
dst.resize(pixel_count * target_pixel_type_size);
- PoolByteArray::Write dst_w = dst.write();
+ uint8_t *dst_w = dst.ptrw();
if (src_pixel_type == SRC_FLOAT && target_pixel_type == TINYEXR_PIXELTYPE_FLOAT) {
-
// Note: we don't save mipmaps
CRASH_COND(src_data.size() < pixel_count * channel_count * target_pixel_type_size);
- const float *src_rp = (float *)src_r.ptr();
- float *dst_wp = (float *)dst_w.ptr();
+ const float *src_rp = (float *)src_r;
+ float *dst_wp = (float *)dst_w;
for (int i = 0; i < pixel_count; ++i) {
dst_wp[i] = src_rp[channel_index + i * channel_count];
}
} else if (src_pixel_type == SRC_HALF && target_pixel_type == TINYEXR_PIXELTYPE_HALF) {
-
CRASH_COND(src_data.size() < pixel_count * channel_count * target_pixel_type_size);
- const uint16_t *src_rp = (uint16_t *)src_r.ptr();
- uint16_t *dst_wp = (uint16_t *)dst_w.ptr();
+ const uint16_t *src_rp = (uint16_t *)src_r;
+ uint16_t *dst_wp = (uint16_t *)dst_w;
for (int i = 0; i < pixel_count; ++i) {
dst_wp[i] = src_rp[channel_index + i * channel_count];
}
} else if (src_pixel_type == SRC_BYTE && target_pixel_type == TINYEXR_PIXELTYPE_HALF) {
-
CRASH_COND(src_data.size() < pixel_count * channel_count);
- const uint8_t *src_rp = (uint8_t *)src_r.ptr();
- uint16_t *dst_wp = (uint16_t *)dst_w.ptr();
+ const uint8_t *src_rp = (uint8_t *)src_r;
+ uint16_t *dst_wp = (uint16_t *)dst_w;
for (int i = 0; i < pixel_count; ++i) {
dst_wp[i] = Math::make_half_float(src_rp[channel_index + i * channel_count] / 255.f);
@@ -235,7 +235,7 @@ Error save_exr(const String &p_path, const Ref<Image> &p_img, bool p_grayscale)
int remapped_index = channel_mapping[channel_index];
- channels_ptrs[remapped_index] = dst_w.ptr();
+ channels_ptrs[remapped_index] = dst_w;
// No conversion
pixel_types[remapped_index] = target_pixel_type;
@@ -262,13 +262,21 @@ Error save_exr(const String &p_path, const Ref<Image> &p_img, bool p_grayscale)
header.channels = channel_infos;
header.pixel_types = pixel_types;
header.requested_pixel_types = requested_pixel_types;
+ header.compression_type = TINYEXR_COMPRESSIONTYPE_PIZ;
+
+ unsigned char *mem = nullptr;
+ const char *err = nullptr;
+
+ size_t bytes = SaveEXRImageToMemory(&image, &header, &mem, &err);
- CharString utf8_filename = p_path.utf8();
- const char *err;
- int ret = SaveEXRImageToFile(&image, &header, utf8_filename.ptr(), &err);
- if (ret != TINYEXR_SUCCESS) {
+ if (bytes == 0) {
print_error(String("Saving EXR failed. Error: {0}").format(varray(err)));
return ERR_FILE_CANT_WRITE;
+ } else {
+ FileAccessRef ref = FileAccess::open(p_path, FileAccess::WRITE);
+ ERR_FAIL_COND_V(!ref, ERR_FILE_CANT_WRITE);
+ ref->store_buffer(mem, bytes);
+ free(mem);
}
return OK;