From 1f077d2b791a059ccef4069dca07ead990bd568e Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Fri, 6 Dec 2024 21:23:44 +0100 Subject: Add Image::crop(). --- src/lib/image.cc | 22 ++++++++++++++++++++++ src/lib/image.h | 2 ++ 2 files changed, 24 insertions(+) (limited to 'src/lib') diff --git a/src/lib/image.cc b/src/lib/image.cc index f1586be4d..80ef92d06 100644 --- a/src/lib/image.cc +++ b/src/lib/image.cc @@ -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::crop(Crop crop) const +{ + vector scale_in_data; + dcp::Size cropped_size; + std::tie(scale_in_data, cropped_size) = crop_source_pointers(crop); + + auto out = make_shared(_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; +} + diff --git a/src/lib/image.h b/src/lib/image.h index ce4382a42..548ebf717 100644 --- a/src/lib/image.h +++ b/src/lib/image.h @@ -84,6 +84,8 @@ public: bool fast ) const; + std::shared_ptr crop(Crop crop) const; + void make_black (); void make_transparent (); void alpha_blend (std::shared_ptr image, Position pos); -- cgit v1.2.3