summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorkleonc <9283098+kleonc@users.noreply.github.com>2021-09-07 10:27:32 +0200
committerkleonc <9283098+kleonc@users.noreply.github.com>2021-11-23 18:06:23 +0100
commit91595b16e3e1f60bdc80c3268cadfc5c00470a7b (patch)
tree28aba73b87c553df2f3e3006b1a3098e0bfcd3eb /core
parent7b0458c18f21cebc5ab1e6739e30e8ed36f0273f (diff)
Add Image::fill_rect method
Diffstat (limited to 'core')
-rw-r--r--core/io/image.cpp37
-rw-r--r--core/io/image.h3
2 files changed, 34 insertions, 6 deletions
diff --git a/core/io/image.cpp b/core/io/image.cpp
index b82e6637b4..2c74229125 100644
--- a/core/io/image.cpp
+++ b/core/io/image.cpp
@@ -2697,16 +2697,15 @@ void Image::blend_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, c
}
}
-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.");
- uint8_t *wp = data.ptrw();
- uint8_t *dst_data_ptr = wp;
+ uint8_t *dst_data_ptr = data.ptrw();
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_color_at_ofs(dst_data_ptr, 0, p_color);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
@@ -2719,6 +2718,33 @@ void Image::fill(const Color &c) {
}
}
+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).intersection(p_rect.abs());
+ if (r.has_no_area()) {
+ return;
+ }
+
+ uint8_t *dst_data_ptr = data.ptrw();
+
+ int pixel_size = get_format_pixel_size(format);
+
+ // Put first pixel with the format-aware API.
+ uint8_t *rect_first_pixel_ptr = &dst_data_ptr[(r.position.y * width + r.position.x) * pixel_size];
+ _set_color_at_ofs(rect_first_pixel_ptr, 0, p_color);
+
+ 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];
+ }
+ }
+ }
+}
+
ImageMemLoadFunc Image::_png_mem_loader_func = nullptr;
ImageMemLoadFunc Image::_jpg_mem_loader_func = nullptr;
ImageMemLoadFunc Image::_webp_mem_loader_func = nullptr;
@@ -3160,6 +3186,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/io/image.h b/core/io/image.h
index d31a065aa7..b6c8fe5abd 100644
--- a/core/io/image.h
+++ b/core/io/image.h
@@ -362,7 +362,8 @@ public:
void blit_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest);
void blend_rect(const Ref<Image> &p_src, const Rect2 &p_src_rect, const Point2 &p_dest);
void blend_rect_mask(const Ref<Image> &p_src, const Ref<Image> &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<Image> get_rect(const Rect2 &p_area) const;