From patchwork Fri Feb 10 13:30:06 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 9566765 X-Patchwork-Delegate: geert@linux-m68k.org Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id AB79B601EA for ; Fri, 10 Feb 2017 13:30:21 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9814228588 for ; Fri, 10 Feb 2017 13:30:21 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8CEE52858C; Fri, 10 Feb 2017 13:30:21 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1FADF28588 for ; Fri, 10 Feb 2017 13:30:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751847AbdBJNaU (ORCPT ); Fri, 10 Feb 2017 08:30:20 -0500 Received: from mail.kernel.org ([198.145.29.136]:41820 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751767AbdBJNaU (ORCPT ); Fri, 10 Feb 2017 08:30:20 -0500 Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id C073C2034A; Fri, 10 Feb 2017 13:30:18 +0000 (UTC) Received: from CookieMonster.cookiemonster.local (cpc89242-aztw30-2-0-cust488.18-1.cable.virginm.net [86.31.129.233]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id C6E172035B; Fri, 10 Feb 2017 13:30:16 +0000 (UTC) From: Kieran Bingham To: laurent.pinchart@ideasonboard.com, linux-renesas-soc@vger.kernel.org, kieran.bingham@ideasonboard.com Subject: [PATCH v2 2/4] gen-image: Implement option to parse an input crop Date: Fri, 10 Feb 2017 13:30:06 +0000 Message-Id: X-Mailer: git-send-email 2.7.4 In-Reply-To: References: In-Reply-To: References: X-Virus-Scanned: ClamAV using ClamSMTP Sender: linux-renesas-soc-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-renesas-soc@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Kieran Bingham Allow the user to specify an input crop in the form (X,Y)/WxH Signed-off-by: Kieran Bingham --- src/gen-image.c | 132 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 132 insertions(+) diff --git a/src/gen-image.c b/src/gen-image.c index 31d42e0211db..088e8b26f648 100644 --- a/src/gen-image.c +++ b/src/gen-image.c @@ -95,6 +95,13 @@ struct format_info { struct format_yuv_info yuv; }; +struct image_rect { + int left; + int top; + unsigned int width; + unsigned int height; +}; + struct image { const struct format_info *format; unsigned int width; @@ -127,6 +134,8 @@ struct options { bool rotate; unsigned int compose; struct params params; + bool crop; + struct image_rect inputcrop; }; /* ----------------------------------------------------------------------------- @@ -1076,6 +1085,25 @@ static void image_flip(const struct image *input, struct image *output, } /* ----------------------------------------------------------------------------- + * Image Cropping + */ + +static void image_crop(const struct image *input, const struct image *output, + const struct image_rect *crop) +{ + unsigned int offset = (crop->top * input->width + crop->left) * 3; + const uint8_t *idata = input->data + offset; + uint8_t *odata = output->data; + unsigned int y; + + for (y = 0; y < output->height; ++y) { + memcpy(odata, idata, output->width * 3); + odata += output->width * 3; + idata += input->width * 3; + } +} + +/* ----------------------------------------------------------------------------- * Look Up Table */ @@ -1363,6 +1391,21 @@ static int process(const struct options *options) input = rgb; } + if (options->crop) { + struct image *cropped; + + cropped = image_new(input->format, options->inputcrop.width, + options->inputcrop.height); + if (!cropped) { + ret = -ENOMEM; + goto done; + } + + image_crop(input, cropped, &options->inputcrop); + image_delete(input); + input = cropped; + } + /* Scale */ if (options->output_width && options->output_height) { output_width = options->output_width; @@ -1596,6 +1639,7 @@ static void usage(const char *argv0) printf(" or percentages ([0%% - 100%%]). Defaults to 1.0\n"); printf("-c, --compose n Compose n copies of the image offset by (50,50) over a black background\n"); printf("-C, --no-chroma-average Disable chroma averaging for odd pixels on output\n"); + printf(" --crop (X,Y)/WxH Crop the input image\n"); printf("-e, --encoding enc Set the YCbCr encoding method. Valid values are\n"); printf(" BT.601, REC.709, BT.2020 and SMPTE240M\n"); printf("-f, --format format Set the output image format\n"); @@ -1628,11 +1672,13 @@ static void list_formats(void) #define OPT_HFLIP 256 #define OPT_VFLIP 257 +#define OPT_CROP 260 static struct option opts[] = { {"alpha", 1, 0, 'a'}, {"clu", 1, 0, 'L'}, {"compose", 1, 0, 'c'}, + {"crop", 1, 0, OPT_CROP}, {"encoding", 1, 0, 'e'}, {"format", 1, 0, 'f'}, {"help", 0, 0, 'h'}, @@ -1649,6 +1695,84 @@ static struct option opts[] = { {0, 0, 0, 0} }; +static int parse_crop(struct image_rect *crop, char *optarg, char **endp) +{ + /* (X,Y)/WxH */ + char *endptr = optarg; + + if (*endptr != '(') { + printf("Invalid crop argument\n"); + *endp = endptr; + return 1; + } + + crop->left = strtol(endptr + 1, &endptr, 10); + if (*endptr != ',') { + printf("Invalid crop position\n"); + *endp = endptr; + return 1; + } + + if (crop->left < 0) { + printf("Invalid negative crop\n"); + *endp = endptr - 1; + return 1; + } + + crop->top = strtol(endptr + 1, &endptr, 10); + if (*endptr != ')') { + printf("Invalid crop position\n"); + *endp = endptr; + return 1; + } + + if (crop->top < 0) { + printf("Invalid negative crop\n"); + *endp = endptr - 1; + return 1; + } + + endptr++; + + if (*endptr != '/') { + printf("Invalid crop argument\n"); + *endp = endptr; + return 1; + } + + crop->width = strtol(endptr + 1, &endptr, 10); + if (*endptr != 'x') { + printf("Invalid crop size\n"); + *endp = endptr; + return 1; + } + + crop->height = strtol(endptr + 1, &endptr, 10); + if (*endptr != 0) { + printf("Invalid crop size\n"); + *endp = endptr; + return 1; + } + + return 0; +} + +void print_streampos(const char *p, const char *end) +{ + int pos; + + pos = end - p + 1; + + if (pos < 0) + pos = 0; + if (pos > (int) strlen(p)) + pos = strlen(p); + + printf("\n"); + printf(" %s\n", p); + printf(" %*s\n", pos, "^"); +} + static int parse_args(struct options *options, int argc, char *argv[]) { char *endptr; @@ -1809,6 +1933,14 @@ static int parse_args(struct options *options, int argc, char *argv[]) options->vflip = true; break; + case OPT_CROP: + if (parse_crop(&options->inputcrop, optarg, &endptr)) { + print_streampos(optarg, endptr); + return 1; + } + options->crop = true; + break; + default: printf("Invalid option -%c\n", c); printf("Run %s -h for help.\n", argv[0]);