diff options
Diffstat (limited to 'core/io/image.cpp')
-rw-r--r-- | core/io/image.cpp | 47 |
1 files changed, 29 insertions, 18 deletions
diff --git a/core/io/image.cpp b/core/io/image.cpp index 7326563f18..15d0182dfc 100644 --- a/core/io/image.cpp +++ b/core/io/image.cpp @@ -1930,8 +1930,7 @@ Error Image::generate_mipmaps(bool p_renormalize) { } Error Image::generate_mipmap_roughness(RoughnessChannel p_roughness_channel, const Ref<Image> &p_normal_map) { - Vector<double> normal_sat_vec; //summed area table - double *normal_sat = nullptr; //summed area table for normal map + LocalVector<double> normal_sat_vec; //summed area table int normal_w = 0, normal_h = 0; ERR_FAIL_COND_V_MSG(p_normal_map.is_null() || p_normal_map->is_empty(), ERR_INVALID_PARAMETER, "Must provide a valid normal map for roughness mipmaps"); @@ -1945,8 +1944,7 @@ Error Image::generate_mipmap_roughness(RoughnessChannel p_roughness_channel, con normal_h = nm->get_height(); normal_sat_vec.resize(normal_w * normal_h * 3); - - normal_sat = normal_sat_vec.ptrw(); + double *normal_sat = normal_sat_vec.ptr(); //create summed area table @@ -2021,24 +2019,26 @@ Error Image::generate_mipmap_roughness(RoughnessChannel p_roughness_channel, con avg[2] += normal_sat[tofs + 2]; } - if (from_y > 0) { + if (from_y > 0 && to_x > 0) { uint32_t tofs = ((from_y - 1) * normal_w + to_x) * 3; avg[0] -= normal_sat[tofs + 0]; avg[1] -= normal_sat[tofs + 1]; avg[2] -= normal_sat[tofs + 2]; } - if (from_x > 0) { + if (from_x > 0 && to_y > 0) { uint32_t tofs = (to_y * normal_w + (from_x - 1)) * 3; avg[0] -= normal_sat[tofs + 0]; avg[1] -= normal_sat[tofs + 1]; avg[2] -= normal_sat[tofs + 2]; } - uint32_t tofs = (to_y * normal_w + to_x) * 3; - avg[0] += normal_sat[tofs + 0]; - avg[1] += normal_sat[tofs + 1]; - avg[2] += normal_sat[tofs + 2]; + if (to_y > 0 && to_x > 0) { + uint32_t tofs = (to_y * normal_w + to_x) * 3; + avg[0] += normal_sat[tofs + 0]; + avg[1] += normal_sat[tofs + 1]; + avg[2] += normal_sat[tofs + 2]; + } double div = double(size_x * size_y); Vector3 vec(avg[0] / div, avg[1] / div, avg[2] / div); @@ -2356,7 +2356,7 @@ void Image::initialize_data(const char **p_xpm) { } Color *colorptr = colormap.getptr(pixelstr); - ERR_FAIL_COND(!colorptr); + ERR_FAIL_NULL(colorptr); uint8_t pixel[4]; for (uint32_t i = 0; i < pixel_size; i++) { pixel[i] = CLAMP((*colorptr)[i] * 255, 0, 255); @@ -2646,23 +2646,23 @@ Error Image::compress_from_channels(CompressMode p_mode, UsedChannels p_channels switch (p_mode) { case COMPRESS_S3TC: { - ERR_FAIL_COND_V(!_image_compress_bc_func, ERR_UNAVAILABLE); + ERR_FAIL_NULL_V(_image_compress_bc_func, ERR_UNAVAILABLE); _image_compress_bc_func(this, p_channels); } break; case COMPRESS_ETC: { - ERR_FAIL_COND_V(!_image_compress_etc1_func, ERR_UNAVAILABLE); + ERR_FAIL_NULL_V(_image_compress_etc1_func, ERR_UNAVAILABLE); _image_compress_etc1_func(this); } break; case COMPRESS_ETC2: { - ERR_FAIL_COND_V(!_image_compress_etc2_func, ERR_UNAVAILABLE); + ERR_FAIL_NULL_V(_image_compress_etc2_func, ERR_UNAVAILABLE); _image_compress_etc2_func(this, p_channels); } break; case COMPRESS_BPTC: { - ERR_FAIL_COND_V(!_image_compress_bptc_func, ERR_UNAVAILABLE); + ERR_FAIL_NULL_V(_image_compress_bptc_func, ERR_UNAVAILABLE); _image_compress_bptc_func(this, p_channels); } break; case COMPRESS_ASTC: { - ERR_FAIL_COND_V(!_image_compress_astc_func, ERR_UNAVAILABLE); + ERR_FAIL_NULL_V(_image_compress_astc_func, ERR_UNAVAILABLE); _image_compress_astc_func(this, p_astc_format); } break; case COMPRESS_MAX: { @@ -3017,6 +3017,7 @@ ImageMemLoadFunc Image::_webp_mem_loader_func = nullptr; ImageMemLoadFunc Image::_tga_mem_loader_func = nullptr; ImageMemLoadFunc Image::_bmp_mem_loader_func = nullptr; ScalableImageMemLoadFunc Image::_svg_scalable_mem_loader_func = nullptr; +ImageMemLoadFunc Image::_ktx_mem_loader_func = nullptr; void (*Image::_image_compress_bc_func)(Image *, Image::UsedChannels) = nullptr; void (*Image::_image_compress_bptc_func)(Image *, Image::UsedChannels) = nullptr; @@ -3411,6 +3412,7 @@ void Image::_bind_methods() { ClassDB::bind_method(D_METHOD("convert", "format"), &Image::convert); + ClassDB::bind_method(D_METHOD("get_mipmap_count"), &Image::get_mipmap_count); ClassDB::bind_method(D_METHOD("get_mipmap_offset", "mipmap"), &Image::get_mipmap_offset); ClassDB::bind_method(D_METHOD("resize_to_po2", "square", "interpolation"), &Image::resize_to_po2, DEFVAL(false), DEFVAL(INTERPOLATE_BILINEAR)); @@ -3488,6 +3490,7 @@ void Image::_bind_methods() { ClassDB::bind_method(D_METHOD("load_webp_from_buffer", "buffer"), &Image::load_webp_from_buffer); ClassDB::bind_method(D_METHOD("load_tga_from_buffer", "buffer"), &Image::load_tga_from_buffer); ClassDB::bind_method(D_METHOD("load_bmp_from_buffer", "buffer"), &Image::load_bmp_from_buffer); + ClassDB::bind_method(D_METHOD("load_ktx_from_buffer", "buffer"), &Image::load_ktx_from_buffer); ClassDB::bind_method(D_METHOD("load_svg_from_buffer", "buffer", "scale"), &Image::load_svg_from_buffer, DEFVAL(1.0)); ClassDB::bind_method(D_METHOD("load_svg_from_string", "svg_str", "scale"), &Image::load_svg_from_string, DEFVAL(1.0)); @@ -3653,7 +3656,7 @@ void Image::bump_map_to_normal_map(float bump_scale) { const uint8_t *rp = data.ptr(); uint8_t *wp = result_image.ptrw(); - ERR_FAIL_COND(!rp); + ERR_FAIL_NULL(rp); unsigned char *write_ptr = wp; float *read_ptr = (float *)rp; @@ -3863,6 +3866,14 @@ Error Image::load_svg_from_string(const String &p_svg_str, float scale) { return load_svg_from_buffer(p_svg_str.to_utf8_buffer(), scale); } +Error Image::load_ktx_from_buffer(const Vector<uint8_t> &p_array) { + ERR_FAIL_NULL_V_MSG( + _ktx_mem_loader_func, + ERR_UNAVAILABLE, + "The KTX module isn't enabled. Recompile the Godot editor or export template binary with the `module_ktx_enabled=yes` SCons option."); + return _load_from_buffer(p_array, _ktx_mem_loader_func); +} + void Image::convert_rg_to_ra_rgba8() { ERR_FAIL_COND(format != FORMAT_RGBA8); ERR_FAIL_COND(!data.size()); @@ -3906,7 +3917,7 @@ Error Image::_load_from_buffer(const Vector<uint8_t> &p_array, ImageMemLoadFunc int buffer_size = p_array.size(); ERR_FAIL_COND_V(buffer_size == 0, ERR_INVALID_PARAMETER); - ERR_FAIL_COND_V(!p_loader, ERR_INVALID_PARAMETER); + ERR_FAIL_NULL_V(p_loader, ERR_INVALID_PARAMETER); const uint8_t *r = p_array.ptr(); |