Add Image::fill_rect method

This commit is contained in:
kleonc 2021-09-07 10:27:32 +02:00
parent 7b0458c18f
commit 91595b16e3
4 changed files with 90 additions and 10 deletions

View file

@ -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);

View file

@ -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;