Add Image::crop().
authorCarl Hetherington <cth@carlh.net>
Fri, 6 Dec 2024 20:23:44 +0000 (21:23 +0100)
committerCarl Hetherington <cth@carlh.net>
Thu, 26 Dec 2024 16:20:25 +0000 (17:20 +0100)
src/lib/image.cc
src/lib/image.h

index f1586be4ddd46f3ffc4988c499b4f5cc5c60e74f..80ef92d061416432bb5311d44ec73362345366aa 100644 (file)
@@ -60,9 +60,11 @@ using std::list;
 using std::make_shared;
 using std::max;
 using std::min;
+using std::pair;
 using std::runtime_error;
 using std::shared_ptr;
 using std::string;
+using std::vector;
 using dcp::Size;
 
 
@@ -1706,3 +1708,23 @@ Image::video_range_to_full_range ()
        }
 }
 
+
+shared_ptr<Image>
+Image::crop(Crop crop) const
+{
+       vector<uint8_t*> scale_in_data;
+       dcp::Size cropped_size;
+       std::tie(scale_in_data, cropped_size) = crop_source_pointers(crop);
+
+       auto out = make_shared<Image>(_pixel_format, cropped_size, _alignment);
+
+       for (int c = 0; c < planes(); ++c) {
+               auto const out_data = out->data()[c];
+               for (int y = 0; y < cropped_size.height; ++y) {
+                       memcpy(out_data + y * out->stride()[c], scale_in_data[c] + y * stride()[c], cropped_size.width * bytes_per_pixel(c));
+               }
+       }
+
+       return out;
+}
+
index ce4382a42e5ca8c3b9c5d8bac48a709697ac1d09..548ebf7171d5fd7aef4c6ea18a1fa64242c87b15 100644 (file)
@@ -84,6 +84,8 @@ public:
                bool fast
                ) const;
 
+       std::shared_ptr<Image> crop(Crop crop) const;
+
        void make_black ();
        void make_transparent ();
        void alpha_blend (std::shared_ptr<const Image> image, Position<int> pos);