diff options
Diffstat (limited to 'modules/tinyexr/image_saver_tinyexr.cpp')
-rw-r--r-- | modules/tinyexr/image_saver_tinyexr.cpp | 47 |
1 files changed, 24 insertions, 23 deletions
diff --git a/modules/tinyexr/image_saver_tinyexr.cpp b/modules/tinyexr/image_saver_tinyexr.cpp index e1d42d3217..05080289bd 100644 --- a/modules/tinyexr/image_saver_tinyexr.cpp +++ b/modules/tinyexr/image_saver_tinyexr.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -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,7 +135,7 @@ static int get_channel_count(Image::Format p_format) { case Image::FORMAT_RGBA8: return 4; default: - CRASH_NOW(); + return -1; } } @@ -158,7 +159,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,35 +174,39 @@ 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]; @@ -211,8 +216,8 @@ Error save_exr(const String &p_path, const Ref<Image> &p_img, bool p_grayscale) 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]; @@ -222,8 +227,8 @@ Error save_exr(const String &p_path, const Ref<Image> &p_img, bool p_grayscale) 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 +240,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,10 +267,6 @@ 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; - // TODO DEBUG REMOVE - for (int i = 0; i < 4; ++i) { - print_line(String("requested_pixel_types{0}: {1}").format(varray(i, requested_pixel_types[i]))); - } CharString utf8_filename = p_path.utf8(); const char *err; |