Add Image::fill_rect method

This commit is contained in:
kleonc 2021-09-07 10:31:30 +02:00
parent 902911e3af
commit 4f446c8b9e
3 changed files with 47 additions and 5 deletions

View file

@ -2454,7 +2454,7 @@ void Image::blend_rect_mask(const Ref<Image> &p_src, const Ref<Image> &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<uint8_t>::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);

View file

@ -324,7 +324,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;

View file

@ -137,7 +137,15 @@
<return type="void" />
<argument index="0" name="color" type="Color" />
<description>
Fills the image with a given [Color].
Fills the image with [code]color[/code].
</description>
</method>
<method name="fill_rect">
<return type="void" />
<argument index="0" name="rect" type="Rect2" />
<argument index="1" name="color" type="Color" />
<description>
Fills [code]rect[/code] with [code]color[/code].
</description>
</method>
<method name="fix_alpha_edges">