From 717d904bbc20e06e2c6c71613d59e065845ff209 Mon Sep 17 00:00:00 2001 From: wm4 Date: Tue, 25 Dec 2012 22:29:49 +0100 Subject: mp_image: add mp_image_crop() Actually stolen from draw_bmp.c. --- video/mp_image.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'video/mp_image.c') diff --git a/video/mp_image.c b/video/mp_image.c index 4df4d2e17f..256e908198 100644 --- a/video/mp_image.c +++ b/video/mp_image.c @@ -335,6 +335,28 @@ void mp_image_copy_attributes(struct mp_image *dst, struct mp_image *src) } } +// Crop the given image to (x0, y0)-(x1, y1) (bottom/right border exclusive) +// x0/y0 must be naturally aligned. +void mp_image_crop(struct mp_image *img, int x0, int y0, int x1, int y1) +{ + assert(x0 >= 0 && y0 >= 0); + assert(x0 <= x1 && y0 <= y1); + assert(x1 <= img->w && y1 <= img->h); + assert(!(x0 & (img->fmt.align_x - 1))); + assert(!(y0 & (img->fmt.align_y - 1))); + + for (int p = 0; p < img->num_planes; ++p) { + img->planes[p] += (y0 >> img->fmt.ys[p]) * img->stride[p] + + (x0 >> img->fmt.xs[p]) * img->fmt.bpp[p] / 8; + } + mp_image_set_size(img, x1 - x0, y1 - y0); +} + +void mp_image_crop_rc(struct mp_image *img, struct mp_rect rc) +{ + mp_image_crop(img, rc.x0, rc.y0, rc.x1, rc.y1); +} + void mp_image_clear(struct mp_image *mpi, int x0, int y0, int w, int h) { int y; -- cgit v1.2.3