Skip to contentSkip to conversation

Add support for multi plane formats with strides

PATCH New Niklas Söderlund 1 participant 4 messages Mar 12, 2025 Media (V4L/DVB)

lore
Share & tools
lore
#0 lore raw

cover Add support for multi plane formats with strides

Hi Sakari,

This series extends raw2rgbpnm to correctly handle NV16 (and friends) source frames where the image stride do not exactly match the source width. Without this fix the utility process the source frame into a output frame that is corrupted without printing any warnings. This could lead to someone (not me obviously...) spending time debugging issues around this and slowly go mad ;-)

The first two patches prepare for adding support for handling multi planer formats by removing odd features which would be hard to rework to support it. Once the cleanup is done the new feature is straight forward.

Patch 1/3 remove support for reading multiple frames from a single file. This feature might have been useful at some point. But in 2012 a type in refactoring lead to it being disabled and impossible to use. I recently fixed this as I noticed the typo in while exploring another area of the tool. I think we can assume if nobody noticed the feature was gone for 13+ years there are few users of it?

Patch 2/3 removes support from guessing from a small list of sizes if no size was given as a CLI argument. This too might have been useful at some point, but the list of resolutions to guess are not kept up-to-date. Few modern standard resolution are part of the list, while really odd ones are. Anyone remember the standard resolution 2592x1968 commented as '5 MP + a bit extra' ? ;-) As guessing stuff is dangerous remove this feature. This feature could possibly be reworked to be kept around if there truly is a user of it.

I have done good tests of this centered around NV16 and rudamentary tests around NV12. Apart from the removed features I have not spotted any regressions and after this multi planer formats with stride > width works as expected.

Niklas Söderlund (3):
Remove support for reading multiple frames from single file Remove guessing of source image size
Add support for multi plane formats with stride

 raw2rgbpnm.c | 345 +++++++++++++++++++++++----------------------------
 1 file changed, 156 insertions(+), 189 deletions(-)
Signature · 1 line
2.48.1
#1 lore raw

PATCH 1/3 Remove support for reading multiple frames from single file

The argument to enable parsing multiple frames from a single file (-n) was accidentally removed in 2012 in commit [1], and only fixed in 2025 by commit [2].

This feature makes it harder to fix other issues found in raw2rgbpnm and since it have been dead for ~13 years without anybody noticing likely have no users, remove it instead of trying to rework it.

1. Commit 5b6e56e54a93 ("Sort option names alphabetically and rename -r to -f")
2. Commit b0a28987d2e3 ("Add missing option to getopt()")

---
Hello,

For what it's worth when I found and fixed this issue in [2] I was working on something else and the missing n to optarg poped out. I have no use-case for this feature so please don't count me fixing this in Jan as somebody is using this feature.

---
 raw2rgbpnm.c | 55 ++++++++++++++++------------------------------------
 1 file changed, 17 insertions(+), 38 deletions(-)
raw2rgbpnm.c +17 -38
index 4cdef563e3c0..e5ed0af99070 100644
@@ -146,17 +146,14 @@ static const int resolutions[][2] = {
 /* Read and return raw image data at given bits per pixel (bpp) depth.
  * size should be set correctly before calling this function.
  * If set to {-1,-1}, try to guess image file resolution.
- * If framenum is set to nonnegative value, assume that input file contains
- * multiple frames and return the given frame. In that case frame size must be given.
  */
-static unsigned char *read_raw_data(char *filename, int framenum, int size[2], int bpp)
+static unsigned char *read_raw_data(char *filename, int size[2], int bpp)
 {
 	/* Get file size */
 	unsigned int line_length;
 	unsigned int padding = 0;
 	unsigned char *b = NULL;
 	unsigned int i;
-	int offset;
 	FILE *f = fopen(filename, "rb");
 	if (!f) error("fopen failed");
 	int r = fseek(f, 0, SEEK_END);
@@ -168,7 +165,6 @@ static unsigned char *read_raw_data(char *filename, int framenum, int size[2], i
 
 	/* Check image resolution */
 	if (size[0]<=0 || size[1]<=0) {
-		if (framenum>=0) error("can not automatically detect frame size with multiple frames");
 		for (i=0; i<SIZE(resolutions); i++)
 			if (resolutions[i][0]*resolutions[i][1]*bpp==file_size*8) break;
 		if (i >= SIZE(resolutions)) error("can't guess raw image file resolution");
@@ -176,9 +172,9 @@ static unsigned char *read_raw_data(char *filename, int framenum, int size[2], i
 		size[1] = resolutions[i][1];
 	}
 
-	if (framenum<0 && (file_size*8 < size[0]*size[1]*bpp)) error("out of input data");
-	if (framenum<0 && (file_size*8 > size[0]*size[1]*bpp)) printf("warning: too large image file\n");
-	if (framenum < 0 && (file_size % size[1] == 0)) {
+	if (file_size*8 < size[0]*size[1]*bpp) error("out of input data");
+	if (file_size*8 > size[0]*size[1]*bpp) printf("warning: too large image file\n");
+	if (file_size % size[1] == 0) {
 		line_length = size[0] * bpp / 8;
 		padding = file_size / size[1] - line_length;
 		printf("%u padding bytes detected at end of line\n", padding);
@@ -186,14 +182,6 @@ static unsigned char *read_raw_data(char *filename, int framenum, int size[2], i
 		printf("warning: input size not multiple of frame size\n");
 	}
 
-	/* Go to the correct position in the file */
-	if (framenum>=0) printf("Reading frame %i...\n", framenum);
-	if (framenum<0) framenum = 0;
-	offset = framenum*size[0]*size[1]*bpp/8;
-	r = fseek(f, offset, SEEK_SET);
-	if (r!=0) error("fseek");
-	if ((file_size-offset)*8 < size[0]*size[1]*bpp) goto out;
-
 	/* Read data */
 	b = xalloc((size[0]*size[1]*bpp+7)/8);
 	if (padding == 0) {
@@ -210,7 +198,7 @@ static unsigned char *read_raw_data(char *filename, int framenum, int size[2], i
 				error("fseek");
 		}
 	}
-out:	fclose(f);
+	fclose(f);
 	return b;
 }
 
@@ -693,15 +681,13 @@ int main(int argc, char *argv[])
 	int size[2] = {-1,-1};
 	unsigned char *src, *dst;
 	char *file_in = NULL, *file_out = NULL;
-	char multi_file_out[NAME_MAX];
 	int format = V4L2_PIX_FMT_UYVY;
 	const struct format_info *info;
 	int r;
 	char *algorithm_name = NULL;
-	int n = 0, multiple = 0;
 
 	for (;;) {
-		int c = getopt(argc, argv, "a:b:f:ghns:w");
+		int c = getopt(argc, argv, "a:b:f:ghs:w");
 		if (c==-1) break;
 		switch (c) {
 		case 'a':
@@ -747,13 +733,9 @@ int main(int argc, char *argv[])
 			       "-f <format>   Specify input file format format (-f ? for list, default UYVY)\n"
 			       "-g            Use high bits for Bayer RAW 10 data\n"
 			       "-h            Show this help\n"
-			       "-n            Assume multiple input frames, extract several PNM files\n"
 			       "-s <XxY>      Specify image size\n"
 			       "-w            Swap R and B channels\n", progname, argv[0]);
 			exit(0);
-		case 'n':
-			multiple = 1;
-			break;
 		case 's':
 			if (parse_format(optarg, &size[0], &size[1]) < 0) {
 				error("bad size");
@@ -780,23 +762,20 @@ int main(int argc, char *argv[])
 	}
 
 	/* Read, convert, and save image */
-	src = read_raw_data(file_in, multiple ? 0 : -1, size, info->bpp);
+	src = read_raw_data(file_in, size, info->bpp);
 	printf("Image size: %ix%i, bytes per pixel: %i, format: %s\n", size[0], size[1],
 		info->bpp, info->name);
 	dst = xalloc(size[0]*size[1]*3);
-	do {
-		raw_to_rgb(info, src, size, dst);
-		sprintf(multi_file_out, "%s-%03i.pnm", file_out, n);
-		printf("Writing to file `%s'...\n", multiple ? multi_file_out : file_out);
-		f = fopen(multiple ? multi_file_out : file_out, "wb");
-		if (!f) error("file open failed");
-		fprintf(f, "P6\n%i %i\n255\n", size[0], size[1]);
-		r = fwrite(dst, size[0]*size[1]*3, 1, f);
-		if (r!=1) error("write failed");
-		fclose(f);
-		if (!multiple) break;
-		src = read_raw_data(file_in, ++n, size, info->bpp);
-	} while (src != NULL);
+
+	raw_to_rgb(info, src, size, dst);
+	printf("Writing to file `%s'...\n", file_out);
+	f = fopen(file_out, "wb");
+	if (!f) error("file open failed");
+	fprintf(f, "P6\n%i %i\n255\n", size[0], size[1]);
+	r = fwrite(dst, size[0]*size[1]*3, 1, f);
+	if (r!=1) error("write failed");
+	fclose(f);
+
 	free(src);
 	free(dst);
 	return 0;
Signature · 1 line
2.48.1
#2 lore raw

PATCH 2/3 Remove guessing of source image size

In reply to Niklas Söderlund

Remove the feature to guess a frame size from file size. This is an odd feature and the list of resolutions to guess from contains entries for '5 MP + a bit extra', which don't feel like a good standard resolution.

This feature however block trying to make the function calls stricter by making it explicit which functions can, and can not, modify the frame size. Remove the guessing and tighten up the all function calls using the source image size.

---
Hello,

This patch could possibly be reworked to keep the guessing, while still allowing for a stricter interface. But I'm in a cleaning mode and this guessing feels wrong. But if somebody is actually using it I would be happy to do the work.

---
 raw2rgbpnm.c | 195 +++++++++++++++++++++++----------------------------
 1 file changed, 87 insertions(+), 108 deletions(-)
raw2rgbpnm.c +87 -108
index e5ed0af99070..d8d4bb198498 100644
@@ -130,24 +130,8 @@ static const struct format_info *get_format_info(__u32 f)
 	return NULL;
 }
 
-static const int resolutions[][2] = {
-	{ 176, 144 },		/* QCIF */
-	{ 320, 240 },		/* QVGA */
-	{ 352, 288 },		/* CIF */
-	{ 640, 480 },		/* VGA */
-	{ 720, 576 },		/* PAL D1 */
-	{ 768, 576 },		/* 1:1 aspect PAL D1 */
-	{ 1920, 1440 },		/* 3VGA */
-	{ 2560, 1920 },		/* 4VGA */
-	{ 2592, 1944 },		/* 5 MP */
-	{ 2592, 1968 },		/* 5 MP + a bit extra */
-};
-
-/* Read and return raw image data at given bits per pixel (bpp) depth.
- * size should be set correctly before calling this function.
- * If set to {-1,-1}, try to guess image file resolution.
- */
-static unsigned char *read_raw_data(char *filename, int size[2], int bpp)
+/* Read and return raw image data at given bits per pixel (bpp) depth. */
+static unsigned char *read_raw_data(char *filename, int width, int height, int bpp)
 {
 	/* Get file size */
 	unsigned int line_length;
@@ -163,33 +147,24 @@ static unsigned char *read_raw_data(char *filename, int size[2], int bpp)
 	r = fseek(f, 0, SEEK_SET);
 	if (r!=0) error("fseek");
 
-	/* Check image resolution */
-	if (size[0]<=0 || size[1]<=0) {
-		for (i=0; i<SIZE(resolutions); i++)
-			if (resolutions[i][0]*resolutions[i][1]*bpp==file_size*8) break;
-		if (i >= SIZE(resolutions)) error("can't guess raw image file resolution");
-		size[0] = resolutions[i][0];
-		size[1] = resolutions[i][1];
-	}
-
-	if (file_size*8 < size[0]*size[1]*bpp) error("out of input data");
-	if (file_size*8 > size[0]*size[1]*bpp) printf("warning: too large image file\n");
-	if (file_size % size[1] == 0) {
-		line_length = size[0] * bpp / 8;
-		padding = file_size / size[1] - line_length;
+	if (file_size*8 < width*height*bpp) error("out of input data");
+	if (file_size*8 > width*height*bpp) printf("warning: too large image file\n");
+	if (file_size % height == 0) {
+		line_length = width * bpp / 8;
+		padding = file_size / height - line_length;
 		printf("%u padding bytes detected at end of line\n", padding);
-	} else if ((file_size * 8) % (size[0] * size[1] * bpp) != 0) {
+	} else if ((file_size * 8) % (width * height * bpp) != 0) {
 		printf("warning: input size not multiple of frame size\n");
 	}
 
 	/* Read data */
-	b = xalloc((size[0]*size[1]*bpp+7)/8);
+	b = xalloc((width*height*bpp+7)/8);
 	if (padding == 0) {
-		r = fread(b, (size[0]*size[1]*bpp+7)/8, 1, f);
+		r = fread(b, (width*height*bpp+7)/8, 1, f);
 		if (r != 1)
 			error("fread");
 	} else {
-		for (i = 0; i < (unsigned int)size[1]; ++i) {
+		for (i = 0; i < (unsigned int)height; ++i) {
 			r = fread(b + i * line_length, line_length, 1, f);
 			if (r != 1)
 				error("fread");
@@ -203,10 +178,10 @@ static unsigned char *read_raw_data(char *filename, int size[2], int bpp)
 }
 
 static void raw_to_rgb(const struct format_info *info,
-		       unsigned char *src, int src_size[2], unsigned char *rgb)
+		       unsigned char *src, int src_width, int src_height, unsigned char *rgb)
 {
-	unsigned int src_stride = src_size[0] * info->bpp / 8;
-	unsigned int rgb_stride = src_size[0] * 3;
+	unsigned int src_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;
 	unsigned char *buf;
@@ -229,8 +204,8 @@ static void raw_to_rgb(const struct format_info *info,
 		cb_pos = info->cb_pos;
 		cr_pos = info->cr_pos;
 
-		for (src_y = 0, dst_y = 0; dst_y < src_size[1]; src_y++, dst_y++) {
-			for (src_x = 0, dst_x = 0; dst_x < src_size[0]; ) {
+		for (src_y = 0, dst_y = 0; dst_y < src_height; src_y++, dst_y++) {
+			for (src_x = 0, dst_x = 0; dst_x < src_width; ) {
 				cb = src[src_y*src_stride + src_x*4 + cb_pos];
 				cr = src[src_y*src_stride + src_x*4 + cr_pos];
 
@@ -264,8 +239,8 @@ static void raw_to_rgb(const struct format_info *info,
 		cb_pos = info->cb_pos;
 		cr_pos = info->cr_pos;
 
-		for (src_y = 0, dst_y = 0; dst_y < src_size[1]; src_y++, dst_y++) {
-			for (src_x = 0, dst_x = 0; dst_x < src_size[0]; ) {
+		for (src_y = 0, dst_y = 0; dst_y < src_height; src_y++, dst_y++) {
+			for (src_x = 0, dst_x = 0; dst_x < src_width; ) {
 				a  = src[src_y*src_stride + src_x*4 + y_pos];
 				cb = src[src_y*src_stride + src_x*4 + cb_pos];
 				cr = src[src_y*src_stride + src_x*4 + cr_pos];
@@ -287,13 +262,13 @@ static void raw_to_rgb(const struct format_info *info,
 		/* fallthrough */
 	case V4L2_PIX_FMT_NV12:
 		src_luma = src;
-		src_chroma = &src[src_size[0] * src_size[1]];
+		src_chroma = &src[src_width * src_height];
 		src_stride = src_stride * 8 / 12;
 
-		for (src_y = 0, dst_y = 0; dst_y < src_size[1]; src_y++, dst_y++) {
+		for (src_y = 0, dst_y = 0; dst_y < src_height; src_y++, dst_y++) {
 			cr = 0;
 
-			for (dst_x = 0, src_x = 0; dst_x < src_size[0]; ) {
+			for (dst_x = 0, src_x = 0; dst_x < src_width; ) {
 				a  = src_luma[dst_y*src_stride + dst_x];
 				cb = src_chroma[(dst_y/2)*src_stride + dst_x + 1 - color_pos];
 				yuv_to_rgb(a,cb,cr, &r, &g, &b);
@@ -318,14 +293,14 @@ static void raw_to_rgb(const struct format_info *info,
 	case V4L2_PIX_FMT_NV16:
 	case V4L2_PIX_FMT_NV61:
 		src_luma = src;
-		src_chroma = &src[src_size[0] * src_size[1]];
+		src_chroma = &src[src_width * src_height];
 		src_stride = src_stride * 8 / 16;
 
 		cb_pos = info->cb_pos;
 		cr_pos = info->cr_pos;
 
-		for (src_y = 0, dst_y = 0; dst_y < src_size[1]; src_y++, dst_y++) {
-			for (dst_x = 0, src_x = 0; dst_x < src_size[0]; ) {
+		for (src_y = 0, dst_y = 0; dst_y < src_height; src_y++, dst_y++) {
+			for (dst_x = 0, src_x = 0; dst_x < src_width; ) {
 				cb = src_chroma[dst_y*src_stride + dst_x + cb_pos];
 				cr = src_chroma[dst_y*src_stride + dst_x + cr_pos];
 
@@ -350,12 +325,12 @@ static void raw_to_rgb(const struct format_info *info,
 
 	case V4L2_PIX_FMT_YUV411P:
 		src_luma = src;
-		src_cb = &src[src_size[0] * src_size[1]];
-		src_cr = &src[src_size[0] * src_size[1] / 4 * 5];
+		src_cb = &src[src_width * src_height];
+		src_cr = &src[src_width * src_height / 4 * 5];
 		src_stride = src_stride * 8 / 12;
 
-		for (src_y = 0, dst_y = 0; dst_y < src_size[1]; src_y++, dst_y++) {
-			for (dst_x = 0, src_x = 0; dst_x < src_size[0]; ) {
+		for (src_y = 0, dst_y = 0; dst_y < src_height; src_y++, dst_y++) {
+			for (dst_x = 0, src_x = 0; dst_x < src_width; ) {
 				a  = src_luma[dst_y*src_stride + dst_x];
 				cb = src_cb[dst_y*src_stride/4 + dst_x/4];
 				cr = src_cr[dst_y*src_stride/4 + dst_x/4];
@@ -374,16 +349,16 @@ static void raw_to_rgb(const struct format_info *info,
 	case V4L2_PIX_FMT_YVU420:
 		src_luma = src;
 		if (info->cb_pos == 0) {
-			src_cb = &src[src_size[0] * src_size[1]];
-			src_cr = &src[src_size[0] * src_size[1] / 4 * 5];
+			src_cb = &src[src_width * src_height];
+			src_cr = &src[src_width * src_height / 4 * 5];
 		} else {
-			src_cr = &src[src_size[0] * src_size[1]];
-			src_cb = &src[src_size[0] * src_size[1] / 4 * 5];
+			src_cr = &src[src_width * src_height];
+			src_cb = &src[src_width * src_height / 4 * 5];
 		}
 		src_stride = src_stride * 8 / 12;
 
-		for (src_y = 0, dst_y = 0; dst_y < src_size[1]; src_y++, dst_y++) {
-			for (dst_x = 0, src_x = 0; dst_x < src_size[0]; ) {
+		for (src_y = 0, dst_y = 0; dst_y < src_height; src_y++, dst_y++) {
+			for (dst_x = 0, src_x = 0; dst_x < src_width; ) {
 				a  = src_luma[dst_y*src_stride + dst_x];
 				cb = src_cb[(dst_y/2)*src_stride/2 + dst_x/2];
 				cr = src_cr[(dst_y/2)*src_stride/2 + dst_x/2];
@@ -402,16 +377,16 @@ static void raw_to_rgb(const struct format_info *info,
 	case V4L2_PIX_FMT_YVU422M:
 		src_luma = src;
 		if (info->cb_pos == 0) {
-			src_cb = &src[src_size[0] * src_size[1]];
-			src_cr = &src[src_size[0] * src_size[1] / 2 * 3];
+			src_cb = &src[src_width * src_height];
+			src_cr = &src[src_width * src_height / 2 * 3];
 		} else {
-			src_cr = &src[src_size[0] * src_size[1]];
-			src_cb = &src[src_size[0] * src_size[1] / 2 * 3];
+			src_cr = &src[src_width * src_height];
+			src_cb = &src[src_width * src_height / 2 * 3];
 		}
 		src_stride = src_stride * 8 / 16;
 
-		for (src_y = 0, dst_y = 0; dst_y < src_size[1]; src_y++, dst_y++) {
-			for (dst_x = 0, src_x = 0; dst_x < src_size[0]; ) {
+		for (src_y = 0, dst_y = 0; dst_y < src_height; src_y++, dst_y++) {
+			for (dst_x = 0, src_x = 0; dst_x < src_width; ) {
 				a  = src_luma[dst_y*src_stride + dst_x];
 				cb = src_cb[dst_y*src_stride/2 + dst_x/2];
 				cr = src_cr[dst_y*src_stride/2 + dst_x/2];
@@ -430,16 +405,16 @@ static void raw_to_rgb(const struct format_info *info,
 	case V4L2_PIX_FMT_YVU444M:
 		src_luma = src;
 		if (info->cb_pos == 0) {
-			src_cb = &src[src_size[0] * src_size[1]];
-			src_cr = &src[src_size[0] * src_size[1] * 2];
+			src_cb = &src[src_width * src_height];
+			src_cr = &src[src_width * src_height * 2];
 		} else {
-			src_cr = &src[src_size[0] * src_size[1]];
-			src_cb = &src[src_size[0] * src_size[1] * 2];
+			src_cr = &src[src_width * src_height];
+			src_cb = &src[src_width * src_height * 2];
 		}
 		src_stride = src_stride * 8 / 24;
 
-		for (src_y = 0, dst_y = 0; dst_y < src_size[1]; src_y++, dst_y++) {
-			for (dst_x = 0, src_x = 0; dst_x < src_size[0]; ) {
+		for (src_y = 0, dst_y = 0; dst_y < src_height; src_y++, dst_y++) {
+			for (dst_x = 0, src_x = 0; dst_x < src_width; ) {
 				a  = src_luma[dst_y*src_stride + dst_x];
 				cb = src_cb[dst_y*src_stride + dst_x];
 				cr = src_cr[dst_y*src_stride + dst_x];
@@ -458,8 +433,8 @@ static void raw_to_rgb(const struct format_info *info,
 	case V4L2_PIX_FMT_Y12:
 	case V4L2_PIX_FMT_Y10:
 		shift = info->bpc - 8;
-		for (src_y = 0, dst_y = 0; dst_y < src_size[1]; src_y++, dst_y++) {
-			for (src_x = 0, dst_x = 0; dst_x < src_size[0]; ) {
+		for (src_y = 0, dst_y = 0; dst_y < src_height; src_y++, dst_y++) {
+			for (src_x = 0, dst_x = 0; dst_x < src_width; ) {
 				a = (src[src_y*src_stride + src_x*2+0] |
 				     (src[src_y*src_stride + src_x*2+1] << 8)) >> shift;
 				rgb[dst_y*rgb_stride+3*dst_x+0] = a;
@@ -472,8 +447,8 @@ static void raw_to_rgb(const struct format_info *info,
 		break;
 
 	case V4L2_PIX_FMT_GREY:
-		for (src_y = 0, dst_y = 0; dst_y < src_size[1]; src_y++, dst_y++) {
-			for (src_x = 0, dst_x = 0; dst_x < src_size[0]; ) {
+		for (src_y = 0, dst_y = 0; dst_y < src_height; src_y++, dst_y++) {
+			for (src_x = 0, dst_x = 0; dst_x < src_width; ) {
 				a = src[src_y*src_stride + src_x];
 				rgb[dst_y*rgb_stride+3*dst_x+0] = a;
 				rgb[dst_y*rgb_stride+3*dst_x+1] = a;
@@ -504,8 +479,8 @@ static void raw_to_rgb(const struct format_info *info,
 	case V4L2_PIX_FMT_SGRBG10:
 		shift = info->bpc - 10;
 
-		for (dst_y=0; dst_y<src_size[1]; dst_y++) {
-			for (dst_x=0; dst_x<src_size[0]; dst_x++) {
+		for (dst_y=0; dst_y<src_height; dst_y++) {
+			for (dst_x=0; dst_x<src_width; dst_x++) {
 				unsigned short *p = (unsigned short *)&(src[src_stride*dst_y+dst_x*2]);
 				int v = *p;
 				if (highbits)
@@ -522,12 +497,12 @@ static void raw_to_rgb(const struct format_info *info,
 			}
 		}
 
-		buf = malloc(src_size[0] * src_size[1] * 3);
+		buf = malloc(src_width * src_height * 3);
 		if (buf==NULL) error("out of memory");
-		qc_imag_bay2rgb10(src, src_stride, buf, src_size[0]*3, src_size[0], src_size[1], 3);
-		for (dst_y=0; dst_y<src_size[1]; dst_y++) {
-			for (dst_x=0; dst_x<src_size[0]; dst_x++) {
-				unsigned char *p = buf + src_size[0]*3*dst_y + dst_x*3;
+		qc_imag_bay2rgb10(src, src_stride, buf, src_width*3, src_width, src_height, 3);
+		for (dst_y=0; dst_y<src_height; dst_y++) {
+			for (dst_x=0; dst_x<src_width; dst_x++) {
+				unsigned char *p = buf + src_width*3*dst_y + dst_x*3;
 				rgb[dst_y*rgb_stride+3*dst_x+0] = swaprb ? p[2] : p[0];
 				rgb[dst_y*rgb_stride+3*dst_x+1] = p[1];
 				rgb[dst_y*rgb_stride+3*dst_x+2] = swaprb ? p[0] : p[2];
@@ -543,12 +518,12 @@ static void raw_to_rgb(const struct format_info *info,
 		/* fallthrough */
 	case V4L2_PIX_FMT_SGRBG8:
 		/* FIXME: only SGRBG8 handled properly: color phase is ignored. */
-		buf = malloc(src_size[0] * src_size[1] * 3);
+		buf = malloc(src_width * src_height * 3);
 		if (buf==NULL) error("out of memory");
-		qc_imag_bay2rgb8(src, src_stride, buf, src_size[0]*3, src_size[0], src_size[1], 3);
-		for (dst_y=0; dst_y<src_size[1]; dst_y++) {
-			for (dst_x=0; dst_x<src_size[0]; dst_x++) {
-				unsigned char *p = buf + src_size[0]*3*dst_y + dst_x*3;
+		qc_imag_bay2rgb8(src, src_stride, buf, src_width*3, src_width, src_height, 3);
+		for (dst_y=0; dst_y<src_height; dst_y++) {
+			for (dst_x=0; dst_x<src_width; dst_x++) {
+				unsigned char *p = buf + src_width*3*dst_y + dst_x*3;
 				rgb[dst_y*rgb_stride+3*dst_x+0] = swaprb ? p[2] : p[0];
 				rgb[dst_y*rgb_stride+3*dst_x+1] = p[1];
 				rgb[dst_y*rgb_stride+3*dst_x+2] = swaprb ? p[0] : p[2];
@@ -558,8 +533,8 @@ static void raw_to_rgb(const struct format_info *info,
 		break;
 
 	case V4L2_PIX_FMT_RGB332:
-		for (src_y = 0, dst_y = 0; dst_y < src_size[1]; src_y++, dst_y++) {
-			for (src_x = 0, dst_x = 0; dst_x < src_size[0]; ) {
+		for (src_y = 0, dst_y = 0; dst_y < src_height; src_y++, dst_y++) {
+			for (src_x = 0, dst_x = 0; dst_x < src_width; ) {
 				pixel = src[dst_y*src_stride + dst_x];
 				r = (pixel << 0) & 0xe0;
 				g = (pixel << 3) & 0xe0;
@@ -574,8 +549,8 @@ static void raw_to_rgb(const struct format_info *info,
 		break;
 
 	case V4L2_PIX_FMT_RGB555:
-		for (src_y = 0, dst_y = 0; dst_y < src_size[1]; src_y++, dst_y++) {
-			for (src_x = 0, dst_x = 0; dst_x < src_size[0]; ) {
+		for (src_y = 0, dst_y = 0; dst_y < src_height; src_y++, dst_y++) {
+			for (src_x = 0, dst_x = 0; dst_x < src_width; ) {
 				pixel = (src[dst_y*src_stride + dst_x*2 + 1] << 8)
 				      | src[dst_y*src_stride + dst_x*2 + 0];
 				r = (pixel >> 7) & 0xf8;
@@ -591,8 +566,8 @@ static void raw_to_rgb(const struct format_info *info,
 		break;
 
 	case V4L2_PIX_FMT_RGB565:
-		for (src_y = 0, dst_y = 0; dst_y < src_size[1]; src_y++, dst_y++) {
-			for (src_x = 0, dst_x = 0; dst_x < src_size[0]; ) {
+		for (src_y = 0, dst_y = 0; dst_y < src_height; src_y++, dst_y++) {
+			for (src_x = 0, dst_x = 0; dst_x < src_width; ) {
 				pixel = (src[dst_y*src_stride + dst_x*2 + 1] << 8)
 				      | src[dst_y*src_stride + dst_x*2 + 0];
 				r = (pixel >> 8) & 0xf8;
@@ -611,8 +586,8 @@ static void raw_to_rgb(const struct format_info *info,
 		swaprb = !swaprb;
 		/* fallthrough */
 	case V4L2_PIX_FMT_RGB24:
-		for (src_y = 0, dst_y = 0; dst_y < src_size[1]; src_y++, dst_y++) {
-			for (src_x = 0, dst_x = 0; dst_x < src_size[0]; ) {
+		for (src_y = 0, dst_y = 0; dst_y < src_height; src_y++, dst_y++) {
+			for (src_x = 0, dst_x = 0; dst_x < src_width; ) {
 				r = src[dst_y*src_stride + dst_x*3 + 0];
 				g = src[dst_y*src_stride + dst_x*3 + 1];
 				b = src[dst_y*src_stride + dst_x*3 + 2];
@@ -626,8 +601,8 @@ static void raw_to_rgb(const struct format_info *info,
 		break;
 
 	case V4L2_PIX_FMT_BGR32:
-		for (src_y = 0, dst_y = 0; dst_y < src_size[1]; src_y++, dst_y++) {
-			for (src_x = 0, dst_x = 0; dst_x < src_size[0]; ) {
+		for (src_y = 0, dst_y = 0; dst_y < src_height; src_y++, dst_y++) {
+			for (src_x = 0, dst_x = 0; dst_x < src_width; ) {
 				r = src[dst_y*src_stride + dst_x*4 + 2];
 				g = src[dst_y*src_stride + dst_x*4 + 1];
 				b = src[dst_y*src_stride + dst_x*4 + 0];
@@ -641,8 +616,8 @@ static void raw_to_rgb(const struct format_info *info,
 		break;
 
 	case V4L2_PIX_FMT_RGB32:
-		for (src_y = 0, dst_y = 0; dst_y < src_size[1]; src_y++, dst_y++) {
-			for (src_x = 0, dst_x = 0; dst_x < src_size[0]; ) {
+		for (src_y = 0, dst_y = 0; dst_y < src_height; src_y++, dst_y++) {
+			for (src_x = 0, dst_x = 0; dst_x < src_width; ) {
 				r = src[dst_y*src_stride + dst_x*4 + 1];
 				g = src[dst_y*src_stride + dst_x*4 + 2];
 				b = src[dst_y*src_stride + dst_x*4 + 3];
@@ -678,13 +653,14 @@ static int parse_format(const char *p, int *w, int *h)
 int main(int argc, char *argv[])
 {
 	FILE *f;
-	int size[2] = {-1,-1};
 	unsigned char *src, *dst;
 	char *file_in = NULL, *file_out = NULL;
 	int format = V4L2_PIX_FMT_UYVY;
 	const struct format_info *info;
 	int r;
 	char *algorithm_name = NULL;
+	int height = -1;
+	int width = -1;
 
 	for (;;) {
 		int c = getopt(argc, argv, "a:b:f:ghs:w");
@@ -737,7 +713,7 @@ int main(int argc, char *argv[])
 			       "-w            Swap R and B channels\n", progname, argv[0]);
 			exit(0);
 		case 's':
-			if (parse_format(optarg, &size[0], &size[1]) < 0) {
+			if (parse_format(optarg, &width, &height) < 0) {
 				error("bad size");
 				exit(0);
 			}
@@ -750,6 +726,9 @@ int main(int argc, char *argv[])
 		}
 	}
 
+	if (width < 0 || height < 0)
+		error("specify image size");
+
 	if (algorithm_name != NULL) qc_set_algorithm(algorithm_name);
 	if (argc-optind != 2) error("give input and output files");
 	file_in  = argv[optind++];
@@ -762,17 +741,17 @@ int main(int argc, char *argv[])
 	}
 
 	/* Read, convert, and save image */
-	src = read_raw_data(file_in, size, info->bpp);
-	printf("Image size: %ix%i, bytes per pixel: %i, format: %s\n", size[0], size[1],
-		info->bpp, info->name);
-	dst = xalloc(size[0]*size[1]*3);
+	src = read_raw_data(file_in, width, height, info->bpp);
+	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, size, dst);
+	raw_to_rgb(info, src, width, height, dst);
 	printf("Writing to file `%s'...\n", file_out);
 	f = fopen(file_out, "wb");
 	if (!f) error("file open failed");
-	fprintf(f, "P6\n%i %i\n255\n", size[0], size[1]);
-	r = fwrite(dst, size[0]*size[1]*3, 1, f);
+	fprintf(f, "P6\n%i %i\n255\n", width, height);
+	r = fwrite(dst, width*height*3, 1, f);
 	if (r!=1) error("write failed");
 	fclose(f);
 
Signature · 1 line
2.48.1
#3 lore raw

PATCH 3/3 Add support for multi plane formats with stride

In reply to Niklas Söderlund

Parsing NV16 that have a stride that is larger then the image width results in incorrect frames, and no warning message being printed.

Fix this by extending the format information structure with how many (concatenated) planes an image is constructed of. And by extending the raw reader to take this information into account.

With this change it is possible to parse NV12, NV21, NV16, and NV61 frames from disk with a stride larger then the image width and have correct frames produced.

---
 raw2rgbpnm.c | 121 +++++++++++++++++++++++++++------------------------
 1 file changed, 65 insertions(+), 56 deletions(-)
raw2rgbpnm.c +65 -56
index d8d4bb198498..5838bc7347e9 100644
@@ -55,60 +55,61 @@ static const struct format_info {
 	unsigned int y_pos;
 	unsigned int cb_pos;
 	unsigned int cr_pos;
+	unsigned int planes;
 } v4l2_pix_fmt_str[] = {
-	{ V4L2_PIX_FMT_RGB332,	8,  0,  "RGB332 (8 RGB-3-3-2)", 0, 0, 0 },
-	{ V4L2_PIX_FMT_RGB555,	16, 5,  "RGB555 (16 RGB-5-5-5)", 0, 0, 0 },
-	{ V4L2_PIX_FMT_RGB565,	16, 0,  "RGB565 (16 RGB-5-6-5)", 0, 0, 0 },
-	{ V4L2_PIX_FMT_BGR24,	24, 8,  "BGR24 (24 BGR-8-8-8)", 0, 0, 0 },
-	{ V4L2_PIX_FMT_RGB24,	24, 8,  "RGB24 (24 RGB-8-8-8)", 0, 0, 0 },
-	{ V4L2_PIX_FMT_BGR32,	32, 8,  "BGR32 (32 BGR-8-8-8-8)", 0, 0, 0 },
-	{ V4L2_PIX_FMT_RGB32,	32, 8,  "RGB32 (32 RGB-8-8-8-8)", 0, 0, 0 },
-	{ V4L2_PIX_FMT_GREY,	8,  8,  "GREY (8 Greyscale)", 0, 0, 0 },
-	{ V4L2_PIX_FMT_Y10,	16, 10, "Y10 (10 Greyscale)", 0, 0, 0 },
-	{ V4L2_PIX_FMT_Y12,	16, 12, "Y12 (12 Greyscale)", 0, 0, 0 },
-	{ V4L2_PIX_FMT_Y16,	16, 16, "Y16 (16 Greyscale)", 0, 0, 0 },
-	{ V4L2_PIX_FMT_UYVY,	16, 8,  "UYVY (16 YUV 4:2:2)", 1, 0, 2 },
-	{ V4L2_PIX_FMT_VYUY,	16, 8,  "VYUY (16 YUV 4:2:2)", 1, 2, 0 },
-	{ V4L2_PIX_FMT_YUYV,	16, 8,  "YUYV (16 YUV 4:2:2)", 0, 1, 3 },
-	{ V4L2_PIX_FMT_YVYU,	16, 8,  "YVYU (16 YUV 4:2:2)", 0, 3, 1 },
-	{ V4L2_PIX_FMT_YUV32,	32, 8,  "YUV32 (32-bit A/XYUV 8-8-8-8)", 1, 2, 3 },
-	{ V4L2_PIX_FMT_AYUV32,	32, 8,  "AYUV32 (32-bit AYUV 8-8-8-8)", 1, 2, 3 },
-	{ V4L2_PIX_FMT_XYUV32,	32, 8,  "XYUV32 (32-bit XYUV 8-8-8-8)", 1, 2, 3 },
-	{ V4L2_PIX_FMT_VUYA32,	32, 8,  "VUYA32 (32-bit VUYA 8-8-8-8)", 2, 1, 0 },
-	{ V4L2_PIX_FMT_VUYX32,	32, 8,  "VUYX32 (32-bit VUYX 8-8-8-8)", 2, 1, 0 },
-	{ V4L2_PIX_FMT_YUVA32,	32, 8,  "YUVA32 (32-bit YUVA 8-8-8-8)", 0, 1, 2 },
-	{ V4L2_PIX_FMT_YUVX32,	32, 8,  "YUVX32 (32-bit YUVX 8-8-8-8)", 0, 1, 2 },
-	{ V4L2_PIX_FMT_YUV411P,	12, 8,  "YUV411P (12 YUV 4:1:1 planar)", 0, 0, 1 },
-	{ V4L2_PIX_FMT_YUV420,	12, 8,  "YUV420P (12 YUV 4:2:0 planar)", 0, 0, 1 },
-	{ V4L2_PIX_FMT_YVU420,	12, 8,  "YVU420P (12 YVU 4:2:2 planar)", 0, 1, 0 },
-	{ V4L2_PIX_FMT_YUV422P,	16, 8,  "YUV422P (16 YUV 4:2:2 planar)", 0, 0, 1 },
-	{ V4L2_PIX_FMT_YVU422M,	16, 8,  "YVU422P (16 YVU 4:2:2 planar)", 0, 1, 0 },
-	{ V4L2_PIX_FMT_YUV444M,	24, 8,  "YUV444P (24 YUV 4:4:4 planar)", 0, 0, 1 },
-	{ V4L2_PIX_FMT_YVU444M,	24, 8,  "YVU444P (24 YVU 4:4:4 planar)", 0, 1, 0 },
-	{ V4L2_PIX_FMT_NV12,	12, 8,  "NV12 (12 Y/CbCr 4:2:0)", 0, 0, 1 },
-	{ V4L2_PIX_FMT_NV21,	12, 8,  "NV21 (12 Y/CrCb 4:2:0)", 0, 1, 0 },
-	{ V4L2_PIX_FMT_NV16,	16, 8,  "NV16 (16 Y/CbCr 4:2:2)", 0, 0, 1 },
-	{ V4L2_PIX_FMT_NV61,	16, 8,  "NV61 (16 Y/CrCb 4:2:2)", 0, 1, 0 },
-	{ V4L2_PIX_FMT_SBGGR8,	8,  8,  "SBGGR8 (8 BGBG.. GRGR..)", 0, 0, 0 },
-	{ V4L2_PIX_FMT_SGBRG8,	8,  8,  "SGBRG8 (8 GBGB.. RGRG..)", 0, 0, 0 },
-	{ V4L2_PIX_FMT_SGRBG8,	8,  8,  "SGRBG8 (8 GRGR.. BGBG..)", 0, 0, 0 },
-	{ V4L2_PIX_FMT_SRGGB8,	8,  8,  "SRGGB8 (8 RGRG.. GBGB..)", 0, 0, 0 },
-	{ V4L2_PIX_FMT_SBGGR10,	16, 10, "SBGGR10 (10 BGBG.. GRGR..)", 0, 0, 0 },
-	{ V4L2_PIX_FMT_SGBRG10,	16, 10, "SGBRG10 (10 GBGB.. RGRG..)", 0, 0, 0 },
-	{ V4L2_PIX_FMT_SGRBG10,	16, 10, "SGRBG10 (10 GRGR.. BGBG..)", 0, 0, 0 },
-	{ V4L2_PIX_FMT_SRGGB10,	16, 10, "SRGGB10 (10 RGRG.. GBGB..)", 0, 0, 0 },
-	{ V4L2_PIX_FMT_SBGGR12,	16, 12, "SBGGR12 (12 BGBG.. GRGR..)", 0, 0, 0 },
-	{ V4L2_PIX_FMT_SGBRG12,	16, 12, "SGBRG12 (12 GBGB.. RGRG..)", 0, 0, 0 },
-	{ V4L2_PIX_FMT_SGRBG12,	16, 12, "SGRBG12 (12 GRGR.. BGBG..)", 0, 0, 0 },
-	{ V4L2_PIX_FMT_SRGGB12,	16, 12, "SRGGB12 (12 RGRG.. GBGB..)", 0, 0, 0 },
-	{ V4L2_PIX_FMT_SBGGR14,	16, 14, "SBGGR14 (14 BGBG.. GRGR..)", 0, 0, 0 },
-	{ V4L2_PIX_FMT_SGBRG14,	16, 14, "SGBRG14 (14 GBGB.. RGRG..)", 0, 0, 0 },
-	{ V4L2_PIX_FMT_SGRBG14,	16, 14, "SGRBG14 (14 GRGR.. BGBG..)", 0, 0, 0 },
-	{ V4L2_PIX_FMT_SRGGB14,	16, 14, "SRGGB14 (14 RGRG.. GBGB..)", 0, 0, 0 },
-	{ V4L2_PIX_FMT_SBGGR16,	16, 16, "SBGGR16 (16 BGBG.. GRGR..)", 0, 0, 0 },
-	{ V4L2_PIX_FMT_SGBRG16,	16, 16, "SGBRG16 (16 GBGB.. RGRG..)", 0, 0, 0 },
-	{ V4L2_PIX_FMT_SGRBG16,	16, 16, "SGRBG16 (16 GRGR.. BGBG..)", 0, 0, 0 },
-	{ V4L2_PIX_FMT_SRGGB16,	16, 16, "SRGGB16 (16 RGRG.. GBGB..)", 0, 0, 0 },
+	{ V4L2_PIX_FMT_RGB332,	8,  0,  "RGB332 (8 RGB-3-3-2)", 0, 0, 0, 1 },
+	{ V4L2_PIX_FMT_RGB555,	16, 5,  "RGB555 (16 RGB-5-5-5)", 0, 0, 0, 1 },
+	{ V4L2_PIX_FMT_RGB565,	16, 0,  "RGB565 (16 RGB-5-6-5)", 0, 0, 0, 1 },
+	{ V4L2_PIX_FMT_BGR24,	24, 8,  "BGR24 (24 BGR-8-8-8)", 0, 0, 0, 1 },
+	{ V4L2_PIX_FMT_RGB24,	24, 8,  "RGB24 (24 RGB-8-8-8)", 0, 0, 0, 1 },
+	{ V4L2_PIX_FMT_BGR32,	32, 8,  "BGR32 (32 BGR-8-8-8-8)", 0, 0, 0, 1 },
+	{ V4L2_PIX_FMT_RGB32,	32, 8,  "RGB32 (32 RGB-8-8-8-8)", 0, 0, 0, 1 },
+	{ V4L2_PIX_FMT_GREY,	8,  8,  "GREY (8 Greyscale)", 0, 0, 0, 1 },
+	{ V4L2_PIX_FMT_Y10,	16, 10, "Y10 (10 Greyscale)", 0, 0, 0, 1 },
+	{ V4L2_PIX_FMT_Y12,	16, 12, "Y12 (12 Greyscale)", 0, 0, 0, 1 },
+	{ V4L2_PIX_FMT_Y16,	16, 16, "Y16 (16 Greyscale)", 0, 0, 0, 1 },
+	{ V4L2_PIX_FMT_UYVY,	16, 8,  "UYVY (16 YUV 4:2:2)", 1, 0, 2, 1 },
+	{ V4L2_PIX_FMT_VYUY,	16, 8,  "VYUY (16 YUV 4:2:2)", 1, 2, 0, 1 },
+	{ V4L2_PIX_FMT_YUYV,	16, 8,  "YUYV (16 YUV 4:2:2)", 0, 1, 3, 1 },
+	{ V4L2_PIX_FMT_YVYU,	16, 8,  "YVYU (16 YUV 4:2:2)", 0, 3, 1, 1 },
+	{ V4L2_PIX_FMT_YUV32,	32, 8,  "YUV32 (32-bit A/XYUV 8-8-8-8)", 1, 2, 3, 1 },
+	{ V4L2_PIX_FMT_AYUV32,	32, 8,  "AYUV32 (32-bit AYUV 8-8-8-8)", 1, 2, 3, 1 },
+	{ V4L2_PIX_FMT_XYUV32,	32, 8,  "XYUV32 (32-bit XYUV 8-8-8-8)", 1, 2, 3, 1 },
+	{ V4L2_PIX_FMT_VUYA32,	32, 8,  "VUYA32 (32-bit VUYA 8-8-8-8)", 2, 1, 0, 1 },
+	{ V4L2_PIX_FMT_VUYX32,	32, 8,  "VUYX32 (32-bit VUYX 8-8-8-8)", 2, 1, 0, 1 },
+	{ V4L2_PIX_FMT_YUVA32,	32, 8,  "YUVA32 (32-bit YUVA 8-8-8-8)", 0, 1, 2, 1 },
+	{ V4L2_PIX_FMT_YUVX32,	32, 8,  "YUVX32 (32-bit YUVX 8-8-8-8)", 0, 1, 2, 1 },
+	{ V4L2_PIX_FMT_YUV411P,	12, 8,  "YUV411P (12 YUV 4:1:1 planar)", 0, 0, 1, 1 },
+	{ V4L2_PIX_FMT_YUV420,	12, 8,  "YUV420P (12 YUV 4:2:0 planar)", 0, 0, 1, 1 },
+	{ V4L2_PIX_FMT_YVU420,	12, 8,  "YVU420P (12 YVU 4:2:2 planar)", 0, 1, 0, 1 },
+	{ V4L2_PIX_FMT_YUV422P,	16, 8,  "YUV422P (16 YUV 4:2:2 planar)", 0, 0, 1, 1 },
+	{ V4L2_PIX_FMT_YVU422M,	16, 8,  "YVU422P (16 YVU 4:2:2 planar)", 0, 1, 0, 1 },
+	{ V4L2_PIX_FMT_YUV444M,	24, 8,  "YUV444P (24 YUV 4:4:4 planar)", 0, 0, 1, 1 },
+	{ V4L2_PIX_FMT_YVU444M,	24, 8,  "YVU444P (24 YVU 4:4:4 planar)", 0, 1, 0, 1 },
+	{ V4L2_PIX_FMT_NV12,	12, 8,  "NV12 (12 Y/CbCr 4:2:0)", 0, 0, 1, 2 },
+	{ V4L2_PIX_FMT_NV21,	12, 8,  "NV21 (12 Y/CrCb 4:2:0)", 0, 1, 0, 2 },
+	{ V4L2_PIX_FMT_NV16,	16, 8,  "NV16 (16 Y/CbCr 4:2:2)", 0, 0, 1, 2 },
+	{ V4L2_PIX_FMT_NV61,	16, 8,  "NV61 (16 Y/CrCb 4:2:2)", 0, 1, 0, 2 },
+	{ V4L2_PIX_FMT_SBGGR8,	8,  8,  "SBGGR8 (8 BGBG.. GRGR..)", 0, 0, 0, 1 },
+	{ V4L2_PIX_FMT_SGBRG8,	8,  8,  "SGBRG8 (8 GBGB.. RGRG..)", 0, 0, 0, 1 },
+	{ V4L2_PIX_FMT_SGRBG8,	8,  8,  "SGRBG8 (8 GRGR.. BGBG..)", 0, 0, 0, 1 },
+	{ V4L2_PIX_FMT_SRGGB8,	8,  8,  "SRGGB8 (8 RGRG.. GBGB..)", 0, 0, 0, 1 },
+	{ V4L2_PIX_FMT_SBGGR10,	16, 10, "SBGGR10 (10 BGBG.. GRGR..)", 0, 0, 0, 1 },
+	{ V4L2_PIX_FMT_SGBRG10,	16, 10, "SGBRG10 (10 GBGB.. RGRG..)", 0, 0, 0, 1 },
+	{ V4L2_PIX_FMT_SGRBG10,	16, 10, "SGRBG10 (10 GRGR.. BGBG..)", 0, 0, 0, 1 },
+	{ V4L2_PIX_FMT_SRGGB10,	16, 10, "SRGGB10 (10 RGRG.. GBGB..)", 0, 0, 0, 1 },
+	{ V4L2_PIX_FMT_SBGGR12,	16, 12, "SBGGR12 (12 BGBG.. GRGR..)", 0, 0, 0, 1 },
+	{ V4L2_PIX_FMT_SGBRG12,	16, 12, "SGBRG12 (12 GBGB.. RGRG..)", 0, 0, 0, 1 },
+	{ V4L2_PIX_FMT_SGRBG12,	16, 12, "SGRBG12 (12 GRGR.. BGBG..)", 0, 0, 0, 1 },
+	{ V4L2_PIX_FMT_SRGGB12,	16, 12, "SRGGB12 (12 RGRG.. GBGB..)", 0, 0, 0, 1 },
+	{ V4L2_PIX_FMT_SBGGR14,	16, 14, "SBGGR14 (14 BGBG.. GRGR..)", 0, 0, 0, 1 },
+	{ V4L2_PIX_FMT_SGBRG14,	16, 14, "SGBRG14 (14 GBGB.. RGRG..)", 0, 0, 0, 1 },
+	{ V4L2_PIX_FMT_SGRBG14,	16, 14, "SGRBG14 (14 GRGR.. BGBG..)", 0, 0, 0, 1 },
+	{ V4L2_PIX_FMT_SRGGB14,	16, 14, "SRGGB14 (14 RGRG.. GBGB..)", 0, 0, 0, 1 },
+	{ V4L2_PIX_FMT_SBGGR16,	16, 16, "SBGGR16 (16 BGBG.. GRGR..)", 0, 0, 0, 1 },
+	{ V4L2_PIX_FMT_SGBRG16,	16, 16, "SGBRG16 (16 GBGB.. RGRG..)", 0, 0, 0, 1 },
+	{ V4L2_PIX_FMT_SGRBG16,	16, 16, "SGRBG16 (16 GRGR.. BGBG..)", 0, 0, 0, 1 },
+	{ V4L2_PIX_FMT_SRGGB16,	16, 16, "SRGGB16 (16 RGRG.. GBGB..)", 0, 0, 0, 1 },
 };
 
 static void *xalloc(int size)
@@ -130,14 +131,17 @@ static const struct format_info *get_format_info(__u32 f)
 	return NULL;
 }
 
-/* Read and return raw image data at given bits per pixel (bpp) depth. */
-static unsigned char *read_raw_data(char *filename, int width, int height, int bpp)
+/* Read and return image data at given width, height and format information. */
+static unsigned char *read_raw_data(char *filename, int width, int height,
+				    const struct format_info *info)
 {
 	/* Get file size */
 	unsigned int line_length;
 	unsigned int padding = 0;
 	unsigned char *b = NULL;
 	unsigned int i;
+	int bpp = info->bpp;
+
 	FILE *f = fopen(filename, "rb");
 	if (!f) error("fopen failed");
 	int r = fseek(f, 0, SEEK_END);
@@ -147,6 +151,11 @@ static unsigned char *read_raw_data(char *filename, int width, int height, int b
 	r = fseek(f, 0, SEEK_SET);
 	if (r!=0) error("fseek");
 
+	if (info->planes > 1) {
+		bpp = info->bpc;
+		height *= info->planes;
+	}
+
 	if (file_size*8 < width*height*bpp) error("out of input data");
 	if (file_size*8 > width*height*bpp) printf("warning: too large image file\n");
 	if (file_size % height == 0) {
@@ -741,7 +750,7 @@ int main(int argc, char *argv[])
 	}
 
 	/* Read, convert, and save image */
-	src = read_raw_data(file_in, width, height, info->bpp);
+	src = read_raw_data(file_in, width, height, info);
 	printf("Image size: %ix%i, bytes per pixel: %i, format: %s\n",
 	       width, height, info->bpp, info->name);
 	dst = xalloc(width*height*3);
Signature · 1 line
2.48.1