Add guess_crop_by_alpha().
authorCarl Hetherington <cth@carlh.net>
Fri, 6 Dec 2024 17:35:14 +0000 (18:35 +0100)
committerCarl Hetherington <cth@carlh.net>
Thu, 26 Dec 2024 16:20:25 +0000 (17:20 +0100)
src/lib/guess_crop.cc
src/lib/guess_crop.h

index d82db18241aff124e1773201a691b48209c7d52f..7089d245d4f652b0ba3b695bcad0819a9b8cd39c 100644 (file)
@@ -149,3 +149,29 @@ guess_crop_by_brightness(shared_ptr<const Film> film, shared_ptr<const Content>
        return crop;
 }
 
+
+Crop
+guess_crop_by_alpha(shared_ptr<const Image> image)
+{
+       std::function<bool (int, int, int, bool)> image_in_line = [image](int start_x, int start_y, int pixels, bool rows) {
+               switch (image->pixel_format()) {
+               case AV_PIX_FMT_RGBA:
+               {
+                       int const increment = rows ? 3 : image->stride()[0];
+                       uint8_t const* data = image->data()[0] + start_x * std::lround(image->bytes_per_pixel(0)) + start_y * image->stride()[0];
+                       for (int p = 0; p < pixels; ++p) {
+                               if (data[3]) {
+                                       return true;
+                               }
+                               data += increment;
+                       }
+                       return false;
+               }
+               default:
+                       throw PixelFormatError("guess_crop_by_alpha()", image->pixel_format());
+               }
+       };
+
+       return guess_crop(image, image_in_line);
+}
+
index 9585ed92a14ef00e751f82d81b97b011d670eaaa..7c500faf07fec11d7bf1e1012b8b388bc0f09e02 100644 (file)
@@ -31,4 +31,5 @@ class Image;
 
 Crop guess_crop_by_brightness(std::shared_ptr<const Image> image, double threshold);
 Crop guess_crop_by_brightness(std::shared_ptr<const Film> fillm, std::shared_ptr<const Content> content, double threshold, dcpomatic::ContentTime position);
+Crop guess_crop_by_alpha(std::shared_ptr<const Image> image);