+shared_ptr<Image>
+Image::crop (Crop crop, bool aligned) const
+{
+ libdcp::Size cropped_size = size ();
+ cropped_size.width -= crop.left + crop.right;
+ cropped_size.height -= crop.top + crop.bottom;
+
+ shared_ptr<Image> out (new SimpleImage (pixel_format(), cropped_size, aligned));
+
+ for (int c = 0; c < components(); ++c) {
+ int const crop_left_in_bytes = bytes_per_pixel(c) * crop.left;
+ int const cropped_width_in_bytes = bytes_per_pixel(c) * cropped_size.width;
+
+ /* Start of the source line, cropped from the top but not the left */
+ uint8_t* in_p = data()[c] + crop.top * stride()[c];
+ uint8_t* out_p = out->data()[c];
+
+ for (int y = 0; y < out->lines(c); ++y) {
+ memcpy (out_p, in_p + crop_left_in_bytes, cropped_width_in_bytes);
+ in_p += stride()[c];
+ out_p += out->stride()[c];
+ }
+ }
+
+ return out;
+}
+
+/** Blacken a YUV image whose bits per pixel is rounded up to 16 */
+void
+Image::yuv_16_black (uint16_t v)
+{
+ memset (data()[0], 0, lines(0) * stride()[0]);
+ for (int i = 1; i < 3; ++i) {
+ int16_t* p = reinterpret_cast<int16_t*> (data()[i]);
+ for (int y = 0; y < size().height; ++y) {
+ for (int x = 0; x < line_size()[i] / 2; ++x) {
+ p[x] = v;
+ }
+ p += stride()[i] / 2;
+ }
+ }
+}
+
+uint16_t
+Image::swap_16 (uint16_t v)
+{
+ return ((v >> 8) & 0xff) | ((v & 0xff) << 8);
+}
+