public inbox for linux-media@vger.kernel.org
 help / color / mirror / Atom feed
From: Sakari Ailus <sakari.ailus@linux.intel.com>
To: linux-media@vger.kernel.org
Cc: Jacopo Mondi <jacopo.mondi@ideasonboard.com>,
	Mehdi Djait <mehdi.djait@linux.intel.com>,
	Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>,
	laurent.pinchart@ideasonboard.com
Subject: [raw2rgbpnm PATCH v4 08/11] Add --stride (-S) option for setting stride
Date: Tue, 10 Mar 2026 10:46:14 +0200	[thread overview]
Message-ID: <20260310084615.1183141-9-sakari.ailus@linux.intel.com> (raw)
In-Reply-To: <20260310084615.1183141-1-sakari.ailus@linux.intel.com>

Being able to set stride explicitly is useful in some cases.

Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
---
 raw2rgbpnm.c | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)

diff --git a/raw2rgbpnm.c b/raw2rgbpnm.c
index 0e0a5b1e63e9..9ec9f06de052 100644
--- a/raw2rgbpnm.c
+++ b/raw2rgbpnm.c
@@ -139,11 +139,11 @@ static const struct format_info *get_format_info(__u32 f)
 }
 
 /* Read and return image data at given width, height and format information. */
-static unsigned char *read_raw_data(char *filename, int width, int height,
+static unsigned char *read_raw_data(char *filename, unsigned int width,
+				    unsigned int height, unsigned int line_length,
 				    const struct format_info *info)
 {
 	/* Get file size */
-	unsigned int line_length;
 	unsigned int padding = 0;
 	unsigned char *b = NULL;
 	unsigned int i;
@@ -174,9 +174,9 @@ static unsigned char *read_raw_data(char *filename, int width, int height,
 	}
 
 	/* Read data */
-	b = xalloc((width*height*bpp+7)/8);
+	b = xalloc(line_length * height);
 	if (padding == 0) {
-		r = fread(b, (width*height*bpp+7)/8, 1, f);
+		r = fread(b, line_length * height, 1, f);
 		if (r != 1)
 			error("fread");
 	} else {
@@ -327,10 +327,11 @@ static int raw_layout_to_grbg(const struct format_info *info, unsigned char *src
 }
 
 static void raw_to_rgb(const struct format_info *info,
-		       unsigned char *src, int src_width, int src_height, unsigned char *rgb)
+		       unsigned char *src, int src_width, int src_height,
+		       unsigned int stride, unsigned char *rgb)
 {
 	unsigned char *tmp_src = NULL;
-	unsigned int src_stride = src_width * info->bpp / 8;
+	unsigned int src_stride = stride ?: src_width * info->bpp / 8;
 	unsigned int rgb_stride = src_width * 3;
 	unsigned char *src_luma, *src_chroma;
 	unsigned char *src_cb, *src_cr;
@@ -844,6 +845,7 @@ static struct option options[] = {
 	{ "help", no_argument, NULL, 'h', },
 	{ "high-bits", no_argument, NULL, 'g', },
 	{ "size", required_argument, NULL, 's', },
+	{ "stride", required_argument, NULL, 'S', },
 	{ "swap-rb", no_argument, NULL, 'w', },
 	{ 0 },
 };
@@ -855,13 +857,14 @@ int main(int argc, char *argv[])
 	char *file_in = NULL, *file_out = NULL;
 	int format = V4L2_PIX_FMT_UYVY;
 	const struct format_info *info;
+	unsigned int stride = 0;
 	int r;
 	char *algorithm_name = NULL;
 	int height = -1;
 	int width = -1;
 
 	for (;;) {
-		int c = getopt_long(argc, argv, "a:b:f:ghs:w", options, NULL);
+		int c = getopt_long(argc, argv, "a:b:f:ghs:S:w", options, NULL);
 		if (c==-1) break;
 		switch (c) {
 		case 'a':
@@ -920,6 +923,9 @@ int main(int argc, char *argv[])
 				exit(0);
 			}
 			break;
+		case 'S':
+			stride = strtoul(optarg, NULL, 10);
+			break;
 		case 'w':
 			swaprb = 1;
 			break;
@@ -943,12 +949,12 @@ int main(int argc, char *argv[])
 	}
 
 	/* Read, convert, and save image */
-	src = read_raw_data(file_in, width, height, info);
+	src = read_raw_data(file_in, width, height, stride, info);
 	printf("Image size: %ix%i, bytes per pixel: %i, format: %s\n",
 	       width, height, info->bpp, info->name);
 	dst = xalloc(width*height*3);
 
-	raw_to_rgb(info, src, width, height, dst);
+	raw_to_rgb(info, src, width, height, stride, dst);
 	printf("Writing to file `%s'...\n", file_out);
 	f = fopen(file_out, "wb");
 	if (!f) error("file open failed");
-- 
2.47.3


  parent reply	other threads:[~2026-03-10  8:45 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-10  8:46 [raw2rgbpnm PATCH v4 00/11] 10-bit packed raw support, other fixes and improvements Sakari Ailus
2026-03-10  8:46 ` [raw2rgbpnm PATCH v4 01/11] Add explicit switch fallthrough notation Sakari Ailus
2026-03-10  8:46 ` [raw2rgbpnm PATCH v4 02/11] Add compiler options to avoid warnings Sakari Ailus
2026-03-16 14:28   ` Laurent Pinchart
2026-03-10  8:46 ` [raw2rgbpnm PATCH v4 03/11] Add 10-bit CSI-2 packed format support Sakari Ailus
2026-03-16 14:38   ` Laurent Pinchart
2026-03-16 19:00     ` Sakari Ailus
2026-03-10  8:46 ` [raw2rgbpnm PATCH v4 04/11] Support long options and improve help text Sakari Ailus
2026-03-16 14:41   ` Laurent Pinchart
2026-03-17  9:49     ` Sakari Ailus
2026-03-17 10:49       ` Laurent Pinchart
2026-03-17 13:17         ` Sakari Ailus
2026-03-10  8:46 ` [raw2rgbpnm PATCH v4 05/11] Add "help" for listing formats and de-Bayering algos, document it Sakari Ailus
2026-03-16 14:44   ` Laurent Pinchart
2026-03-10  8:46 ` [raw2rgbpnm PATCH v4 06/11] The program has been called raw2rgbpnm, not yuv_to_rgbpnm awhile now Sakari Ailus
2026-03-16 14:46   ` Laurent Pinchart
2026-03-10  8:46 ` [raw2rgbpnm PATCH v4 07/11] Arrange headers alphabetically Sakari Ailus
2026-03-16 14:47   ` Laurent Pinchart
2026-03-10  8:46 ` Sakari Ailus [this message]
2026-03-16 14:50   ` [raw2rgbpnm PATCH v4 08/11] Add --stride (-S) option for setting stride Laurent Pinchart
2026-03-10  8:46 ` [raw2rgbpnm PATCH v4 09/11] Improve input validation Sakari Ailus
2026-03-16 14:54   ` Laurent Pinchart

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260310084615.1183141-9-sakari.ailus@linux.intel.com \
    --to=sakari.ailus@linux.intel.com \
    --cc=jacopo.mondi@ideasonboard.com \
    --cc=laurent.pinchart@ideasonboard.com \
    --cc=linux-media@vger.kernel.org \
    --cc=mehdi.djait@linux.intel.com \
    --cc=tomi.valkeinen@ideasonboard.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox