From 61a85a446cdc8c9010af2f16ccfe96380d186930 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Fri, 6 Dec 2024 18:35:06 +0100 Subject: Extract method to get crop with a predicate. --- src/lib/guess_crop.cc | 75 ++++++++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 34 deletions(-) diff --git a/src/lib/guess_crop.cc b/src/lib/guess_crop.cc index 14ef9a9ed..d82db1824 100644 --- a/src/lib/guess_crop.cc +++ b/src/lib/guess_crop.cc @@ -31,13 +31,50 @@ using std::shared_ptr; using namespace dcpomatic; -Crop -guess_crop_by_brightness(shared_ptr image, double threshold) +static Crop +guess_crop(shared_ptr image, std::function predicate) { auto const width = image->size().width; auto const height = image->size().height; - auto image_in_line = [image, threshold](int start_x, int start_y, int pixels, bool rows) { + auto crop = Crop{}; + + for (auto y = 0; y < height; ++y) { + if (predicate(0, y, width, true)) { + crop.top = y; + break; + } + } + + for (auto y = height - 1; y >= 0; --y) { + if (predicate(0, y, width, true)) { + crop.bottom = height - 1 - y; + break; + } + } + + for (auto x = 0; x < width; ++x) { + if (predicate(x, 0, height, false)) { + crop.left = x; + break; + } + } + + for (auto x = width - 1; x >= 0; --x) { + if (predicate(x, 0, height, false)) { + crop.right = width - 1 - x; + break; + } + } + + return crop; +} + + +Crop +guess_crop_by_brightness(shared_ptr image, double threshold) +{ + std::function image_in_line = [image, threshold](int start_x, int start_y, int pixels, bool rows) { double brightest = 0; switch (image->pixel_format()) { @@ -79,37 +116,7 @@ guess_crop_by_brightness(shared_ptr image, double threshold) return brightest > threshold; }; - auto crop = Crop{}; - - for (auto y = 0; y < height; ++y) { - if (image_in_line(0, y, width, true)) { - crop.top = y; - break; - } - } - - for (auto y = height - 1; y >= 0; --y) { - if (image_in_line(0, y, width, true)) { - crop.bottom = height - 1 - y; - break; - } - } - - for (auto x = 0; x < width; ++x) { - if (image_in_line(x, 0, height, false)) { - crop.left = x; - break; - } - } - - for (auto x = width - 1; x >= 0; --x) { - if (image_in_line(x, 0, height, false)) { - crop.right = width - 1 - x; - break; - } - } - - return crop; + return guess_crop(image, image_in_line); } -- cgit v1.2.3