Skip to content

Commit

Permalink
Utilities: Give imgcmp a --threshold flag
Browse files Browse the repository at this point in the history
  • Loading branch information
nico committed Jan 31, 2025
1 parent e3edc65 commit 53e39b2
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions Userland/Utilities/imgcmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@ static ErrorOr<void> save_image(NonnullRefPtr<Gfx::Bitmap> bitmap, StringView ou
return Gfx::WebPWriter::encode(*buffered_stream, *bitmap);
}

static bool are_pixels_equal(Gfx::Color first_pixel, Gfx::Color second_pixel)
static bool are_pixels_equal(Gfx::Color first_pixel, Gfx::Color second_pixel, int threshold)
{
return first_pixel == second_pixel;
return abs(first_pixel.red() - second_pixel.red()) <= threshold
&& abs(first_pixel.green() - second_pixel.green()) <= threshold
&& abs(first_pixel.blue() - second_pixel.blue()) <= threshold
&& abs(first_pixel.alpha() - second_pixel.alpha()) <= threshold;
}

static ErrorOr<NonnullRefPtr<Gfx::Bitmap>> make_diff_image(NonnullRefPtr<Gfx::Bitmap> first_image, NonnullRefPtr<Gfx::Bitmap> second_image)
static ErrorOr<NonnullRefPtr<Gfx::Bitmap>> make_diff_image(NonnullRefPtr<Gfx::Bitmap> first_image, NonnullRefPtr<Gfx::Bitmap> second_image, int threshold)
{
VERIFY(first_image->size() == second_image->size());

Expand All @@ -52,7 +55,7 @@ static ErrorOr<NonnullRefPtr<Gfx::Bitmap>> make_diff_image(NonnullRefPtr<Gfx::Bi
for (int x = 0; x < first_image->width(); ++x) {
auto first_pixel = first_image->get_pixel<Gfx::StorageFormat::BGRA8888>(x, y);
auto second_pixel = second_image->get_pixel<Gfx::StorageFormat::BGRA8888>(x, y);
if (are_pixels_equal(first_pixel, second_pixel)) {
if (are_pixels_equal(first_pixel, second_pixel, threshold)) {
diff_image->set_pixel(x, y, first_pixel.interpolate(Gfx::Color::White, 0.5f));
} else {
diff_image->set_pixel(x, y, Gfx::Color::Red);
Expand All @@ -70,6 +73,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
StringView write_diff_image_path;
args_parser.add_option(write_diff_image_path, "Write image that highlights differing pixels", "write-diff-image", {}, "FILE");

int threshold = 0;
args_parser.add_option(threshold, "Only consider pixels further apart than this (in any channel) as distinct (default: 0)", "threshold", {}, "THRESHOLD");

StringView first_image_path;
args_parser.add_positional_argument(first_image_path, "Path to first input image", "FILE1");

Expand All @@ -87,15 +93,15 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
}

if (!write_diff_image_path.is_empty()) {
auto diff_image = TRY(make_diff_image(*first_image, *second_image));
auto diff_image = TRY(make_diff_image(*first_image, *second_image, threshold));
TRY(save_image(diff_image, write_diff_image_path));
}

for (int y = 0; y < first_image->physical_height(); ++y) {
for (int x = 0; x < first_image->physical_width(); ++x) {
auto first_pixel = first_image->get_pixel(x, y);
auto second_pixel = second_image->get_pixel(x, y);
if (!are_pixels_equal(first_pixel, second_pixel)) {
if (!are_pixels_equal(first_pixel, second_pixel, threshold)) {
warnln("different pixel at ({}, {}), {} vs {}", x, y, first_pixel, second_pixel);
return 1;
}
Expand Down

0 comments on commit 53e39b2

Please sign in to comment.