Skip to contentSkip to conversation

raw2rgbpnm cleanups and a fix

PATCH Superseded Sakari Ailus 2 participants 16 messages Mar 17 Media (V4L/DVB)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover raw2rgbpnm cleanups and a fix

Hi folks,

Here are a few cleanups and fixes found in Laurent's review.

Sakari Ailus (7):
Reorder headers alphabetically
Check we have a supported format first before allocating memory Add quotes for "-f help" in help text and do a line wrap Constify progname
More header sorting
Use "stride" in a stride-related error message instead of an internal
name
Fix file size check

 raw2rgbpnm.c | 47 +++++++++++++++++++++++++----------------------
 1 file changed, 25 insertions(+), 22 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH 1/7 Reorder headers alphabetically

---
 raw2rgbpnm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
raw2rgbpnm.c +1 -1
index 24a88feddf00..121f0e0272e4 100644
@@ -26,8 +26,8 @@
 #include <ctype.h>
 #include <getopt.h>
 #include <limits.h>
-#include <stdio.h>
 #include <stdint.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
Signature · 1 line
2.47.3
#2 lore raw

Re: [PATCH 1/7] Reorder headers alphabetically

Quoted 10 lines · Sakari Ailus
---
 raw2rgbpnm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
raw2rgbpnm.c +0 -0
index 24a88feddf00..121f0e0272e4 100644

raw2rgbpnm.c line 33

Quoted 9 lines
raw2rgbpnm.c +1 -1
@@ -26,8 +26,8 @@
 #include <ctype.h>
 #include <getopt.h>
 #include <limits.h>
-#include <stdio.h>
 #include <stdint.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>

How about you select the whole block and use the sort function of your favourite text editor ?

Signature · 3 lines
Regards,

Laurent Pinchart
#3 lore raw

PATCH 2/7 Check we have a supported format first before allocating memory

In reply to Sakari Ailus
---
 raw2rgbpnm.c | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)
raw2rgbpnm.c +14 -14
index 121f0e0272e4..8aa7258218ad 100644
@@ -370,20 +370,7 @@ static void raw_to_rgb(const struct format_info *info,
 		src_width &= ~3;
 
 		const struct format_info *old_info = info;
-		unsigned int new_stride = src_width * 2;
-
-		tmp_src = malloc(new_stride * src_height);
-		if (!tmp_src)
-			error("can't allocate memory for the temporary buffer");
-
-		for (src_y = 0; src_y < src_height; src_y++)
-			for (src_x = 0; src_x < src_width; src_x++)
-				raw_put(16, tmp_src, new_stride, src_x, src_y,
-					raw_get(info->bpp, src, src_stride,
-						src_x, src_y));
-
-		src_stride = new_stride;
-		src = tmp_src;
+		unsigned int unpacked_stride = src_width * 2;
 
 		for (unsigned int i = 0; i < SIZE(v4l2_pix_fmt_str); i++) {
 			if (v4l2_pix_fmt_str[i].fmt == info->compat_fmt) {
@@ -395,6 +382,19 @@ static void raw_to_rgb(const struct format_info *info,
 		if (info == old_info)
 			error("no supported format found for %s",
 			      old_info->name);
+
+		tmp_src = malloc(unpacked_stride * src_height);
+		if (!tmp_src)
+			error("can't allocate memory for the temporary buffer");
+
+		for (src_y = 0; src_y < src_height; src_y++)
+			for (src_x = 0; src_x < src_width; src_x++)
+				raw_put(16, tmp_src, unpacked_stride, src_x, src_y,
+					raw_get(info->bpp, src, src_stride,
+						src_x, src_y));
+
+		src_stride = unpacked_stride;
+		src = tmp_src;
 	}
 	}
 
Signature · 1 line
2.47.3
#4 lore raw

Re: [PATCH 2/7] Check we have a supported format first before allocating memory

Quoted 34 lines · Sakari Ailus
---
 raw2rgbpnm.c | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)
raw2rgbpnm.c +1 -14
index 121f0e0272e4..8aa7258218ad 100644
@@ -370,20 +370,7 @@ static void raw_to_rgb(const struct format_info *info,
 		src_width &= ~3;
 
 		const struct format_info *old_info = info;
-		unsigned int new_stride = src_width * 2;
-
-		tmp_src = malloc(new_stride * src_height);
-		if (!tmp_src)
-			error("can't allocate memory for the temporary buffer");
-
-		for (src_y = 0; src_y < src_height; src_y++)
-			for (src_x = 0; src_x < src_width; src_x++)
-				raw_put(16, tmp_src, new_stride, src_x, src_y,
-					raw_get(info->bpp, src, src_stride,
-						src_x, src_y));
-
-		src_stride = new_stride;
-		src = tmp_src;
+		unsigned int unpacked_stride = src_width * 2;
 
 		for (unsigned int i = 0; i < SIZE(v4l2_pix_fmt_str); i++) {
 			if (v4l2_pix_fmt_str[i].fmt == info->compat_fmt) {
@@ -395,6 +382,19 @@ static void raw_to_rgb(const struct format_info *info,
 		if (info == old_info)
 			error("no supported format found for %s",

raw2rgbpnm.c static void raw_to_rgb(const struct format_info *info, · line 393

Quoted 10 lines
raw2rgbpnm.c +9 -0
 			      old_info->name);
+
+		tmp_src = malloc(unpacked_stride * src_height);
+		if (!tmp_src)
+			error("can't allocate memory for the temporary buffer");
+
+		for (src_y = 0; src_y < src_height; src_y++)
+			for (src_x = 0; src_x < src_width; src_x++)
+				raw_put(16, tmp_src, unpacked_stride, src_x, src_y,
+					raw_get(info->bpp, src, src_stride,

info now points to the format_info for the compat format. I think you should now use old_info->bpp here.

Quoted 7 lines
raw2rgbpnm.c +4 -0
+						src_x, src_y));
+
+		src_stride = unpacked_stride;
+		src = tmp_src;
 	}
 	}
 
Signature · 3 lines
Regards,

Laurent Pinchart
#5 lore raw

Re: [PATCH 2/7] Check we have a supported format first before allocating memory

Quoted 40 lines · Laurent Pinchart
Quoted 39 lines · Sakari Ailus
---
 raw2rgbpnm.c | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)
raw2rgbpnm.c +5 -14
index 121f0e0272e4..8aa7258218ad 100644
@@ -370,20 +370,7 @@ static void raw_to_rgb(const struct format_info *info,
 		src_width &= ~3;
 
 		const struct format_info *old_info = info;
-		unsigned int new_stride = src_width * 2;
-
-		tmp_src = malloc(new_stride * src_height);
-		if (!tmp_src)
-			error("can't allocate memory for the temporary buffer");
-
-		for (src_y = 0; src_y < src_height; src_y++)
-			for (src_x = 0; src_x < src_width; src_x++)
-				raw_put(16, tmp_src, new_stride, src_x, src_y,
-					raw_get(info->bpp, src, src_stride,
-						src_x, src_y));
-
-		src_stride = new_stride;
-		src = tmp_src;
+		unsigned int unpacked_stride = src_width * 2;
 
 		for (unsigned int i = 0; i < SIZE(v4l2_pix_fmt_str); i++) {
 			if (v4l2_pix_fmt_str[i].fmt == info->compat_fmt) {
@@ -395,6 +382,19 @@ static void raw_to_rgb(const struct format_info *info,
 		if (info == old_info)
 			error("no supported format found for %s",
 			      old_info->name);
+
+		tmp_src = malloc(unpacked_stride * src_height);
+		if (!tmp_src)
+			error("can't allocate memory for the temporary buffer");
Quoted 8 lines

raw2rgbpnm.c static void raw_to_rgb(const struct format_info *info, · line 393

Quoted 5 lines
raw2rgbpnm.c +5 -0
+
+		for (src_y = 0; src_y < src_height; src_y++)
+			for (src_x = 0; src_x < src_width; src_x++)
+				raw_put(16, tmp_src, unpacked_stride, src_x, src_y,
+					raw_get(info->bpp, src, src_stride,

info now points to the format_info for the compat format. I think you should now use old_info->bpp here.

Yes, makes sense.

Quoted 13 lines
Quoted 8 lines
raw2rgbpnm.c +4 -0
+						src_x, src_y));
+
+		src_stride = unpacked_stride;
+		src = tmp_src;
 	}
 	}
 
Signature · 3 lines
Regards,

Laurent Pinchart
Signature · 1 line
Sakari Ailus
#6 lore raw

PATCH 3/7 Add quotes for "-f help" in help text and do a line wrap

In reply to Sakari Ailus
---
 raw2rgbpnm.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
raw2rgbpnm.c +3 -2
index 8aa7258218ad..e8d72a68be30 100644
@@ -929,11 +929,12 @@ int main(int argc, char *argv[])
 			       "--brightness, -b <bright> Set brightness (multiplier) to output image\n"
 			       "                          (float, default 1.0)\n"
 			       "--format, -f <format>     Specify input file format format\n"
-			       "                          (-f help for list, default UYVY)\n"
+			       "                          (\"-f help\" for list, default UYVY)\n"
 			       "--help, -h                Show this help\n"
 			       "--high-bits, -g           Use high bits for Bayer RAW 10 data\n"
 			       "--size, -s <XxY>          Specify image size\n"
-			       "--swap-rb, -w             Swap R and B channels\n", progname, argv[0]);
+			       "--swap-rb, -w             Swap R and B channels\n",
+			       progname, argv[0]);
 			exit(0);
 		case 's':
 			if (parse_format(optarg, &width, &height) < 0) {
Signature · 1 line
2.47.3
Reviewed-by on v1 3/7
#7 lore raw

Re: [PATCH 3/7] Add quotes for "-f help" in help text and do a line wrap

Quoted 1 line · Sakari Ailus
Quoted 23 lines
---
 raw2rgbpnm.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
raw2rgbpnm.c +3 -2
index 8aa7258218ad..e8d72a68be30 100644
@@ -929,11 +929,12 @@ int main(int argc, char *argv[])
 			       "--brightness, -b <bright> Set brightness (multiplier) to output image\n"
 			       "                          (float, default 1.0)\n"
 			       "--format, -f <format>     Specify input file format format\n"
-			       "                          (-f help for list, default UYVY)\n"
+			       "                          (\"-f help\" for list, default UYVY)\n"
 			       "--help, -h                Show this help\n"
 			       "--high-bits, -g           Use high bits for Bayer RAW 10 data\n"
 			       "--size, -s <XxY>          Specify image size\n"
-			       "--swap-rb, -w             Swap R and B channels\n", progname, argv[0]);
+			       "--swap-rb, -w             Swap R and B channels\n",
+			       progname, argv[0]);
 			exit(0);
 		case 's':
 			if (parse_format(optarg, &width, &height) < 0) {
Signature · 3 lines
Regards,

Laurent Pinchart
#8 lore raw

PATCH 4/7 Constify progname

In reply to Sakari Ailus
---
 raw2rgbpnm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
raw2rgbpnm.c +1 -1
index e8d72a68be30..215decd65d93 100644
@@ -43,7 +43,7 @@
 #define MAX(a,b)	((a)>(b)?(a):(b))
 #define MIN(a,b)	((a)<(b)?(a):(b))
 
-char *progname = "raw2rgbpnm";
+const char *progname = "raw2rgbpnm";
 
 static int swaprb = 0;
 static int highbits = 0;			/* Bayer RAW10 formats use high bits for data */
Signature · 1 line
2.47.3
Reviewed-by on v1 4/7
#9 lore raw

Re: [PATCH 4/7] Constify progname

Quoted 1 line · Sakari Ailus
Quoted 17 lines
---
 raw2rgbpnm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
raw2rgbpnm.c +1 -1
index e8d72a68be30..215decd65d93 100644
@@ -43,7 +43,7 @@
 #define MAX(a,b)	((a)>(b)?(a):(b))
 #define MIN(a,b)	((a)<(b)?(a):(b))
 
-char *progname = "raw2rgbpnm";
+const char *progname = "raw2rgbpnm";
 
 static int swaprb = 0;
 static int highbits = 0;			/* Bayer RAW10 formats use high bits for data */
Signature · 3 lines
Regards,

Laurent Pinchart
#10 lore raw

PATCH 5/7 More header sorting

In reply to Sakari Ailus
---
 raw2rgbpnm.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
raw2rgbpnm.c +4 -2
index 215decd65d93..ef12be820be6 100644
@@ -29,10 +29,12 @@
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <unistd.h>
 #include <string.h>
-#include <sys/types.h>
+#include <unistd.h>
+
 #include <linux/videodev2.h>
+#include <sys/types.h>
+
 #include "utils.h"
 #include "raw_to_rgb.h"
 #include "yuv_to_rgb.h"
Signature · 1 line
2.47.3
#11 lore raw

Re: [PATCH 5/7] More header sorting

Quoted 1 line · Sakari Ailus

Merged that with 1/7.

Quoted 23 lines
---
 raw2rgbpnm.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
raw2rgbpnm.c +4 -2
index 215decd65d93..ef12be820be6 100644
@@ -29,10 +29,12 @@
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <unistd.h>
 #include <string.h>
-#include <sys/types.h>
+#include <unistd.h>
+
 #include <linux/videodev2.h>
+#include <sys/types.h>
+
 #include "utils.h"
 #include "raw_to_rgb.h"
 #include "yuv_to_rgb.h"
Signature · 3 lines
Regards,

Laurent Pinchart
#12 lore raw

PATCH 6/7 Use "stride" in a stride-related error message instead of an internal name

In reply to Sakari Ailus
---
 raw2rgbpnm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
raw2rgbpnm.c +1 -1
index ef12be820be6..3b92c6ba519b 100644
@@ -177,7 +177,7 @@ static unsigned char *read_raw_data(char *filename, unsigned int *width,
 	line_length = line_length ?: (*width * bpp + 7) / 8;
 	if (!line_length || UINT_MAX / line_length < *height ||
 	    line_length < *width * bpp / 8)
-		error("line_length %u is bad", line_length);
+		error("stride %u is bad", line_length);
 	if (file_size > UINT_MAX)
 		error("too large file");
 	if ((unsigned int)file_size < line_length * *height)
Signature · 1 line
2.47.3
Reviewed-by on v1 6/7
#13 lore raw

Re: [PATCH 6/7] Use "stride" in a stride-related error message instead of an internal name

Quoted 1 line · Sakari Ailus
Quoted 17 lines
---
 raw2rgbpnm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
raw2rgbpnm.c +1 -1
index ef12be820be6..3b92c6ba519b 100644
@@ -177,7 +177,7 @@ static unsigned char *read_raw_data(char *filename, unsigned int *width,
 	line_length = line_length ?: (*width * bpp + 7) / 8;
 	if (!line_length || UINT_MAX / line_length < *height ||
 	    line_length < *width * bpp / 8)
-		error("line_length %u is bad", line_length);
+		error("stride %u is bad", line_length);
 	if (file_size > UINT_MAX)
 		error("too large file");
 	if ((unsigned int)file_size < line_length * *height)
Signature · 3 lines
Regards,

Laurent Pinchart
#14 lore raw

PATCH 7/7 Fix file size check

In reply to Sakari Ailus

The file size was unintentionally multiplied by 8 and that's not right anymore due to changes elsewhere.

Fixes 578f7012a851 ("Improve input validation")
---
 raw2rgbpnm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
raw2rgbpnm.c +1 -1
index 3b92c6ba519b..49b8071b299e 100644
@@ -187,7 +187,7 @@ static unsigned char *read_raw_data(char *filename, unsigned int *width,
 	if (file_size % *height == 0) {
 		padding = file_size / *height - line_length;
 		printf("%u padding bytes detected at end of line\n", padding);
-	} else if ((file_size * 8) % (line_length * *height) != 0) {
+	} else if (file_size % (line_length * *height) != 0) {
 		printf("warning: input size not multiple of frame size\n");
 	}
 
Signature · 1 line
2.47.3
Reviewed-by on v1 7/7
#15 lore raw

Re: [PATCH 7/7] Fix file size check

Quoted 6 lines · Sakari Ailus

The file size was unintentionally multiplied by 8 and that's not right anymore due to changes elsewhere.

Fixes 578f7012a851 ("Improve input validation")
Quoted 17 lines
---
 raw2rgbpnm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
raw2rgbpnm.c +1 -1
index 3b92c6ba519b..49b8071b299e 100644
@@ -187,7 +187,7 @@ static unsigned char *read_raw_data(char *filename, unsigned int *width,
 	if (file_size % *height == 0) {
 		padding = file_size / *height - line_length;
 		printf("%u padding bytes detected at end of line\n", padding);
-	} else if ((file_size * 8) % (line_length * *height) != 0) {
+	} else if (file_size % (line_length * *height) != 0) {
 		printf("warning: input size not multiple of frame size\n");
 	}
 
Signature · 3 lines
Regards,

Laurent Pinchart