diff options
Diffstat (limited to 'core/io/image.cpp')
-rw-r--r-- | core/io/image.cpp | 108 |
1 files changed, 65 insertions, 43 deletions
diff --git a/core/io/image.cpp b/core/io/image.cpp index 812bfa8263..65addaf964 100644 --- a/core/io/image.cpp +++ b/core/io/image.cpp @@ -444,7 +444,7 @@ static void _convert(int p_width, int p_height, const uint8_t *p_src, uint8_t *p uint8_t rgba[4] = { 0, 0, 0, 255 }; - if (read_gray) { + if constexpr (read_gray) { rgba[0] = rofs[0]; rgba[1] = rofs[0]; rgba[2] = rofs[0]; @@ -454,11 +454,11 @@ static void _convert(int p_width, int p_height, const uint8_t *p_src, uint8_t *p } } - if (read_alpha || write_alpha) { + if constexpr (read_alpha || write_alpha) { rgba[3] = read_alpha ? rofs[read_bytes] : 255; } - if (write_gray) { + if constexpr (write_gray) { //TODO: not correct grayscale, should use fixed point version of actual weights wofs[0] = uint8_t((uint16_t(rgba[0]) + uint16_t(rgba[1]) + uint16_t(rgba[2])) / 3); } else { @@ -467,7 +467,7 @@ static void _convert(int p_width, int p_height, const uint8_t *p_src, uint8_t *p } } - if (write_alpha) { + if constexpr (write_alpha) { wofs[write_bytes] = rgba[3]; } } @@ -640,7 +640,7 @@ static void _scale_cubic(const uint8_t *__restrict p_src, uint8_t *__restrict p_ double xfac = (double)width / p_dst_width; double yfac = (double)height / p_dst_height; // coordinates of source points and coefficients - double ox, oy, dx, dy, k1, k2; + double ox, oy, dx, dy; int ox1, oy1, ox2, oy2; // destination pixel values // width and height decreased by 1 @@ -671,7 +671,7 @@ static void _scale_cubic(const uint8_t *__restrict p_src, uint8_t *__restrict p_ for (int n = -1; n < 3; n++) { // get Y coefficient - k1 = _bicubic_interp_kernel(dy - (double)n); + [[maybe_unused]] double k1 = _bicubic_interp_kernel(dy - (double)n); oy2 = oy1 + n; if (oy2 < 0) { @@ -683,7 +683,7 @@ static void _scale_cubic(const uint8_t *__restrict p_src, uint8_t *__restrict p_ for (int m = -1; m < 3; m++) { // get X coefficient - k2 = k1 * _bicubic_interp_kernel((double)m - dx); + [[maybe_unused]] double k2 = k1 * _bicubic_interp_kernel((double)m - dx); ox2 = ox1 + m; if (ox2 < 0) { @@ -697,7 +697,7 @@ static void _scale_cubic(const uint8_t *__restrict p_src, uint8_t *__restrict p_ const T *__restrict p = ((T *)p_src) + (oy2 * p_src_width + ox2) * CC; for (int i = 0; i < CC; i++) { - if (sizeof(T) == 2) { //half float + if constexpr (sizeof(T) == 2) { //half float color[i] = Math::half_to_float(p[i]); } else { color[i] += p[i] * k2; @@ -707,9 +707,9 @@ static void _scale_cubic(const uint8_t *__restrict p_src, uint8_t *__restrict p_ } for (int i = 0; i < CC; i++) { - if (sizeof(T) == 1) { //byte + if constexpr (sizeof(T) == 1) { //byte dst[i] = CLAMP(Math::fast_ftoi(color[i]), 0, 255); - } else if (sizeof(T) == 2) { //half float + } else if constexpr (sizeof(T) == 2) { //half float dst[i] = Math::make_half_float(color[i]); } else { dst[i] = color[i]; @@ -758,7 +758,7 @@ static void _scale_bilinear(const uint8_t *__restrict p_src, uint8_t *__restrict src_xofs_right *= CC; for (uint32_t l = 0; l < CC; l++) { - if (sizeof(T) == 1) { //uint8 + if constexpr (sizeof(T) == 1) { //uint8 uint32_t p00 = p_src[y_ofs_up + src_xofs_left + l] << FRAC_BITS; uint32_t p10 = p_src[y_ofs_up + src_xofs_right + l] << FRAC_BITS; uint32_t p01 = p_src[y_ofs_down + src_xofs_left + l] << FRAC_BITS; @@ -769,7 +769,7 @@ static void _scale_bilinear(const uint8_t *__restrict p_src, uint8_t *__restrict uint32_t interp = interp_up + (((interp_down - interp_up) * src_yofs_frac) >> FRAC_BITS); interp >>= FRAC_BITS; p_dst[i * p_dst_width * CC + j * CC + l] = uint8_t(interp); - } else if (sizeof(T) == 2) { //half float + } else if constexpr (sizeof(T) == 2) { //half float float xofs_frac = float(src_xofs_frac) / (1 << FRAC_BITS); float yofs_frac = float(src_yofs_frac) / (1 << FRAC_BITS); @@ -786,7 +786,7 @@ static void _scale_bilinear(const uint8_t *__restrict p_src, uint8_t *__restrict float interp = interp_up + ((interp_down - interp_up) * yofs_frac); dst[i * p_dst_width * CC + j * CC + l] = Math::make_half_float(interp); - } else if (sizeof(T) == 4) { //float + } else if constexpr (sizeof(T) == 4) { //float float xofs_frac = float(src_xofs_frac) / (1 << FRAC_BITS); float yofs_frac = float(src_yofs_frac) / (1 << FRAC_BITS); @@ -877,7 +877,7 @@ static void _scale_lanczos(const uint8_t *__restrict p_src, uint8_t *__restrict const T *__restrict src_data = ((const T *)p_src) + (buffer_y * src_width + target_x) * CC; for (uint32_t i = 0; i < CC; i++) { - if (sizeof(T) == 2) { //half float + if constexpr (sizeof(T) == 2) { //half float pixel[i] += Math::half_to_float(src_data[i]) * lanczos_val; } else { pixel[i] += src_data[i] * lanczos_val; @@ -934,9 +934,9 @@ static void _scale_lanczos(const uint8_t *__restrict p_src, uint8_t *__restrict for (uint32_t i = 0; i < CC; i++) { pixel[i] /= weight; - if (sizeof(T) == 1) { //byte + if constexpr (sizeof(T) == 1) { //byte dst_data[i] = CLAMP(Math::fast_ftoi(pixel[i]), 0, 255); - } else if (sizeof(T) == 2) { //half float + } else if constexpr (sizeof(T) == 2) { //half float dst_data[i] = Math::make_half_float(pixel[i]); } else { // float dst_data[i] = pixel[i]; @@ -982,7 +982,7 @@ void Image::resize_to_po2(bool p_square, Interpolation p_interpolation) { } void Image::resize(int p_width, int p_height, Interpolation p_interpolation) { - ERR_FAIL_COND_MSG(data.size() == 0, "Cannot resize image before creating it, use create() or create_from_data() first."); + ERR_FAIL_COND_MSG(data.size() == 0, "Cannot resize image before creating it, use set_data() first."); ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot resize in compressed or custom image formats."); bool mipmap_aware = p_interpolation == INTERPOLATE_TRILINEAR /* || p_interpolation == INTERPOLATE_TRICUBIC */; @@ -1017,7 +1017,7 @@ void Image::resize(int p_width, int p_height, Interpolation p_interpolation) { } bool interpolate_mipmaps = mipmap_aware && mip1 != mip2; if (interpolate_mipmaps) { - dst2.create(p_width, p_height, false, format); + dst2.initialize_data(p_width, p_height, false, format); } bool had_mipmaps = mipmaps; @@ -2016,9 +2016,7 @@ Error Image::generate_mipmap_roughness(RoughnessChannel p_roughness_channel, con uint8_t* wr = imgdata.ptrw(); memcpy(wr.ptr(), ptr, size); wr = uint8_t*(); - Ref<Image> im; - im.instantiate(); - im->create(w, h, false, format, imgdata); + Ref<Image> im = Image::create_from_data(w, h, false, format, imgdata); im->save_png("res://mipmap_" + itos(i) + ".png"); } #endif @@ -2051,7 +2049,25 @@ Vector<uint8_t> Image::get_data() const { return data; } -void Image::create(int p_width, int p_height, bool p_use_mipmaps, Format p_format) { +Ref<Image> Image::create_empty(int p_width, int p_height, bool p_use_mipmaps, Format p_format) { + Ref<Image> image; + image.instantiate(); + image->initialize_data(p_width, p_height, p_use_mipmaps, p_format); + return image; +} + +Ref<Image> Image::create_from_data(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const Vector<uint8_t> &p_data) { + Ref<Image> image; + image.instantiate(); + image->initialize_data(p_width, p_height, p_use_mipmaps, p_format, p_data); + return image; +} + +void Image::set_data(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const Vector<uint8_t> &p_data) { + initialize_data(p_width, p_height, p_use_mipmaps, p_format, p_data); +} + +void Image::initialize_data(int p_width, int p_height, bool p_use_mipmaps, Format p_format) { ERR_FAIL_COND_MSG(p_width <= 0, "The Image width specified (" + itos(p_width) + " pixels) must be greater than 0 pixels."); ERR_FAIL_COND_MSG(p_height <= 0, "The Image height specified (" + itos(p_height) + " pixels) must be greater than 0 pixels."); ERR_FAIL_COND_MSG(p_width > MAX_WIDTH, @@ -2077,7 +2093,7 @@ void Image::create(int p_width, int p_height, bool p_use_mipmaps, Format p_forma format = p_format; } -void Image::create(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const Vector<uint8_t> &p_data) { +void Image::initialize_data(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const Vector<uint8_t> &p_data) { ERR_FAIL_COND_MSG(p_width <= 0, "The Image width specified (" + itos(p_width) + " pixels) must be greater than 0 pixels."); ERR_FAIL_COND_MSG(p_height <= 0, "The Image height specified (" + itos(p_height) + " pixels) must be greater than 0 pixels."); ERR_FAIL_COND_MSG(p_width > MAX_WIDTH, @@ -2115,7 +2131,7 @@ void Image::create(int p_width, int p_height, bool p_use_mipmaps, Format p_forma mipmaps = p_use_mipmaps; } -void Image::create(const char **p_xpm) { +void Image::initialize_data(const char **p_xpm) { int size_width = 0; int size_height = 0; int pixelchars = 0; @@ -2230,7 +2246,7 @@ void Image::create(const char **p_xpm) { } if (line == colormap_size) { status = READING_PIXELS; - create(size_width, size_height, false, has_alpha ? FORMAT_RGBA8 : FORMAT_RGB8); + initialize_data(size_width, size_height, false, has_alpha ? FORMAT_RGBA8 : FORMAT_RGB8); data_write = data.ptrw(); pixel_size = has_alpha ? 4 : 3; } @@ -2559,7 +2575,7 @@ Image::Image(const char **p_xpm) { mipmaps = false; format = FORMAT_L8; - create(p_xpm); + initialize_data(p_xpm); } Image::Image(int p_width, int p_height, bool p_use_mipmaps, Format p_format) { @@ -2568,7 +2584,7 @@ Image::Image(int p_width, int p_height, bool p_use_mipmaps, Format p_format) { mipmaps = p_use_mipmaps; format = FORMAT_L8; - create(p_width, p_height, p_use_mipmaps, p_format); + initialize_data(p_width, p_height, p_use_mipmaps, p_format); } Image::Image(int p_width, int p_height, bool p_mipmaps, Format p_format, const Vector<uint8_t> &p_data) { @@ -2577,7 +2593,7 @@ Image::Image(int p_width, int p_height, bool p_mipmaps, Format p_format, const V mipmaps = p_mipmaps; format = FORMAT_L8; - create(p_width, p_height, p_mipmaps, p_format, p_data); + initialize_data(p_width, p_height, p_mipmaps, p_format, p_data); } Rect2i Image::get_used_rect() const { @@ -2620,9 +2636,9 @@ Rect2i Image::get_used_rect() const { } } -Ref<Image> Image::get_rect(const Rect2i &p_area) const { - Ref<Image> img = memnew(Image(p_area.size.x, p_area.size.y, mipmaps, format)); - img->blit_rect(Ref<Image>((Image *)this), p_area, Point2i(0, 0)); +Ref<Image> Image::get_region(const Rect2i &p_region) const { + Ref<Image> img = memnew(Image(p_region.size.x, p_region.size.y, mipmaps, format)); + img->blit_rect(Ref<Image>((Image *)this), p_region, Point2i(0, 0)); return img; } @@ -2846,6 +2862,9 @@ void Image::_repeat_pixel_over_subsequent_memory(uint8_t *p_pixel, int p_pixel_s } void Image::fill(const Color &p_color) { + if (data.size() == 0) { + return; + } ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot fill in compressed or custom image formats."); uint8_t *dst_data_ptr = data.ptrw(); @@ -2859,6 +2878,9 @@ void Image::fill(const Color &p_color) { } void Image::fill_rect(const Rect2i &p_rect, const Color &p_color) { + if (data.size() == 0) { + return; + } ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot fill rect in compressed or custom image formats."); Rect2i r = Rect2i(0, 0, width, height).intersection(p_rect.abs()); @@ -2931,7 +2953,7 @@ void Image::_set_data(const Dictionary &p_data) { ERR_FAIL_COND(ddformat == FORMAT_MAX); - create(dwidth, dheight, dmipmaps, ddformat, ddata); + initialize_data(dwidth, dheight, dmipmaps, ddformat, ddata); } Dictionary Image::_get_data() const { @@ -3294,8 +3316,9 @@ void Image::_bind_methods() { ClassDB::bind_method(D_METHOD("generate_mipmaps", "renormalize"), &Image::generate_mipmaps, DEFVAL(false)); ClassDB::bind_method(D_METHOD("clear_mipmaps"), &Image::clear_mipmaps); - ClassDB::bind_method(D_METHOD("create", "width", "height", "use_mipmaps", "format"), &Image::create_empty); - ClassDB::bind_method(D_METHOD("create_from_data", "width", "height", "use_mipmaps", "format", "data"), &Image::create_from_data); + ClassDB::bind_static_method("Image", D_METHOD("create", "width", "height", "use_mipmaps", "format"), &Image::create_empty); + ClassDB::bind_static_method("Image", D_METHOD("create_from_data", "width", "height", "use_mipmaps", "format", "data"), &Image::create_from_data); + ClassDB::bind_method(D_METHOD("set_data", "width", "height", "use_mipmaps", "format", "data"), &Image::set_data); ClassDB::bind_method(D_METHOD("is_empty"), &Image::is_empty); @@ -3339,7 +3362,7 @@ void Image::_bind_methods() { ClassDB::bind_method(D_METHOD("fill_rect", "rect", "color"), &Image::fill_rect); ClassDB::bind_method(D_METHOD("get_used_rect"), &Image::get_used_rect); - ClassDB::bind_method(D_METHOD("get_rect", "rect"), &Image::get_rect); + ClassDB::bind_method(D_METHOD("get_region", "region"), &Image::get_region); ClassDB::bind_method(D_METHOD("copy_from", "src"), &Image::copy_internals_from); @@ -3460,9 +3483,7 @@ Ref<Image> Image::rgbe_to_srgb() { ERR_FAIL_COND_V(format != FORMAT_RGBE9995, Ref<Image>()); - Ref<Image> new_image; - new_image.instantiate(); - new_image->create(width, height, false, Image::FORMAT_RGB8); + Ref<Image> new_image = create_empty(width, height, false, Image::FORMAT_RGB8); for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { @@ -3503,6 +3524,7 @@ Ref<Image> Image::get_image_from_mipmap(int p_mipamp) const { void Image::bump_map_to_normal_map(float bump_scale) { ERR_FAIL_COND(!_can_modify(format)); + clear_mipmaps(); convert(Image::FORMAT_RF); Vector<uint8_t> result_image; //rgba output @@ -3869,15 +3891,15 @@ Dictionary Image::compute_image_metrics(const Ref<Image> p_compared_image, bool double image_metric_max, image_metric_mean, image_metric_mean_squared, image_metric_root_mean_squared, image_metric_peak_snr = 0.0; const bool average_component_error = true; - const uint32_t width = MIN(compared_image->get_width(), source_image->get_width()); - const uint32_t height = MIN(compared_image->get_height(), source_image->get_height()); + const uint32_t w = MIN(compared_image->get_width(), source_image->get_width()); + const uint32_t h = MIN(compared_image->get_height(), source_image->get_height()); // Histogram approach originally due to Charles Bloom. double hist[256]; memset(hist, 0, sizeof(hist)); - for (uint32_t y = 0; y < height; y++) { - for (uint32_t x = 0; x < width; x++) { + for (uint32_t y = 0; y < h; y++) { + for (uint32_t x = 0; x < w; x++) { const Color color_a = compared_image->get_pixel(x, y); const Color color_b = source_image->get_pixel(x, y); @@ -3922,7 +3944,7 @@ Dictionary Image::compute_image_metrics(const Ref<Image> p_compared_image, bool } // See http://richg42.blogspot.com/2016/09/how-to-compute-psnr-from-old-berkeley.html - double total_values = width * height; + double total_values = w * h; if (average_component_error) { total_values *= 4; |