From 4f446c8b9e042e489fc4b252c36643d12c39729d Mon Sep 17 00:00:00 2001 From: kleonc <9283098+kleonc@users.noreply.github.com> Date: Tue, 7 Sep 2021 10:31:30 +0200 Subject: [PATCH] Add Image::fill_rect method --- core/image.cpp | 39 ++++++++++++++++++++++++++++++++++++--- core/image.h | 3 ++- doc/classes/Image.xml | 10 +++++++++- 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/core/image.cpp b/core/image.cpp index e70de14f82d..6dca11552ff 100644 --- a/core/image.cpp +++ b/core/image.cpp @@ -2454,7 +2454,7 @@ void Image::blend_rect_mask(const Ref &p_src, const Ref &p_mask, c unlock(); } -void Image::fill(const Color &c) { +void Image::fill(const Color &p_color) { ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot fill in compressed or custom image formats."); lock(); @@ -2464,8 +2464,8 @@ void Image::fill(const Color &c) { int pixel_size = get_format_pixel_size(format); - // put first pixel with the format-aware API - set_pixel(0, 0, c); + // Put first pixel with the format-aware API. + set_pixel(0, 0, p_color); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { @@ -2480,6 +2480,38 @@ void Image::fill(const Color &c) { unlock(); } +void Image::fill_rect(const Rect2 &p_rect, const Color &p_color) { + ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot fill rect in compressed or custom image formats."); + + Rect2i r = Rect2i(0, 0, width, height).clip(p_rect.abs()); + if (r.has_no_area()) { + return; + } + + lock(); + + PoolVector::Write wp = data.write(); + uint8_t *dst_data_ptr = wp.ptr(); + + int pixel_size = get_format_pixel_size(format); + + // Put first pixel with the format-aware API. + set_pixelv(r.position, p_color); + const uint8_t *rect_first_pixel_ptr = &dst_data_ptr[(r.position.y * width + r.position.x) * pixel_size]; + + for (int y = r.position.y; y < r.position.y + r.size.y; y++) { + for (int x = r.position.x; x < r.position.x + r.size.x; x++) { + uint8_t *dst = &dst_data_ptr[(y * width + x) * pixel_size]; + + for (int k = 0; k < pixel_size; k++) { + dst[k] = rect_first_pixel_ptr[k]; + } + } + } + + unlock(); +} + ImageMemLoadFunc Image::_png_mem_loader_func = nullptr; ImageMemLoadFunc Image::_jpg_mem_loader_func = nullptr; ImageMemLoadFunc Image::_webp_mem_loader_func = nullptr; @@ -2903,6 +2935,7 @@ void Image::_bind_methods() { ClassDB::bind_method(D_METHOD("blend_rect", "src", "src_rect", "dst"), &Image::blend_rect); ClassDB::bind_method(D_METHOD("blend_rect_mask", "src", "mask", "src_rect", "dst"), &Image::blend_rect_mask); ClassDB::bind_method(D_METHOD("fill", "color"), &Image::fill); + 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); diff --git a/core/image.h b/core/image.h index 9e7c5c281ec..85b415f75f8 100644 --- a/core/image.h +++ b/core/image.h @@ -324,7 +324,8 @@ public: void blit_rect_mask(const Ref &p_src, const Ref &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest); void blend_rect(const Ref &p_src, const Rect2 &p_src_rect, const Point2 &p_dest); void blend_rect_mask(const Ref &p_src, const Ref &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest); - void fill(const Color &c); + void fill(const Color &p_color); + void fill_rect(const Rect2 &p_rect, const Color &p_color); Rect2 get_used_rect() const; Ref get_rect(const Rect2 &p_area) const; diff --git a/doc/classes/Image.xml b/doc/classes/Image.xml index 32dee0b0959..51fd67ca32a 100644 --- a/doc/classes/Image.xml +++ b/doc/classes/Image.xml @@ -137,7 +137,15 @@ - Fills the image with a given [Color]. + Fills the image with [code]color[/code]. + + + + + + + + Fills [code]rect[/code] with [code]color[/code].