Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image.replace_color #19234

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions core/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1868,6 +1868,53 @@ void Image::fill(const Color &c) {
unlock();
}

void Image::replace_color(const Color &p_color_old, const Color &p_color_new) {

if (p_color_old != p_color_new) {

uint32_t pixel_size = get_format_pixel_size(format);

uint8_t colorb_new[16];
uint8_t colorb_old[16];
uint8_t colorb_current[16];

Image new_img(2, 1, 0, format);
new_img.lock();

new_img.set_pixel(0, 0, p_color_new);
new_img.set_pixel(1, 0, p_color_old);

PoolVector<uint8_t>::Read r_dummy = new_img.data.read();
new_img._get_pixelb(0, 0, pixel_size, r_dummy.ptr(), colorb_new);
new_img._get_pixelb(1, 0, pixel_size, r_dummy.ptr(), colorb_old);

new_img.unlock();

lock();

PoolVector<uint8_t>::Read r = data.read();
PoolVector<uint8_t>::Write w = data.write();

bool found = false;
for (int y = 0; y < height; y++) {

for (int x = 0; x < width; x++) {
found = true;
_get_pixelb(x, y, pixel_size, r.ptr(), colorb_current);
for (uint32_t i = 0; i < pixel_size; i++)
if (colorb_current[i] != colorb_old[i]) {
found = false;
}
if (found) {
_put_pixelb(x, y, pixel_size, w.ptr(), colorb_new);
}
}
}

unlock();
}
}

Ref<Image> (*Image::_png_mem_loader_func)(const uint8_t *, int) = NULL;
Ref<Image> (*Image::_jpg_mem_loader_func)(const uint8_t *, int) = NULL;

Expand Down Expand Up @@ -2308,6 +2355,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("replace_color", "color_old", "color_new"), &Image::replace_color);

ClassDB::bind_method(D_METHOD("get_used_rect"), &Image::get_used_rect);
ClassDB::bind_method(D_METHOD("get_rect", "rect"), &Image::get_rect);
Expand Down
1 change: 1 addition & 0 deletions core/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ class Image : public Resource {
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 replace_color(const Color &color_old, const Color &color_new);

Rect2 get_used_rect() const;
Ref<Image> get_rect(const Rect2 &p_area) const;
Expand Down