Skip to contentSkip to conversation

raw2rgbpnm cleanups and a fix

PATCH New Sakari Ailus 1 participant 7 messages Mar 17 Media (V4L/DVB)

lore
Share & tools
lore
#0 lore raw

v2 cover raw2rgbpnm cleanups and a fix

Hi folks,

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

since v1:

- Use old_info->bpp field in reading the original buffer.

- Squash two header sorting patches plus swap two lines.

Sakari Ailus (6):
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
Use "stride" in a stride-related error message instead of an internal
name
Fix file size check

 raw2rgbpnm.c | 49 ++++++++++++++++++++++++++-----------------------
 1 file changed, 26 insertions(+), 23 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH v2 1/6 Reorder headers alphabetically

---
 raw2rgbpnm.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
raw2rgbpnm.c +6 -4
index 24a88feddf00..007ee7a37636 100644
@@ -26,15 +26,17 @@
 #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>
-#include <sys/types.h>
+#include <unistd.h>
+
 #include <linux/videodev2.h>
-#include "utils.h"
+#include <sys/types.h>
+
 #include "raw_to_rgb.h"
+#include "utils.h"
 #include "yuv_to_rgb.h"
 
 #define DEFAULT_BGR 0
Signature · 1 line
2.47.3
#2 lore raw

PATCH v2 2/6 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 007ee7a37636..96466a5a1d44 100644
@@ -372,20 +372,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) {
@@ -397,6 +384,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(old_info->bpp, src, src_stride,
+						src_x, src_y));
+
+		src_stride = unpacked_stride;
+		src = tmp_src;
 	}
 	}
 
Signature · 1 line
2.47.3
#3 lore raw

PATCH v2 3/6 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 96466a5a1d44..1f00787e80d7 100644
@@ -931,11 +931,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
#4 lore raw

PATCH v2 4/6 Constify progname

In reply to Sakari Ailus
---
 raw2rgbpnm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
raw2rgbpnm.c +1 -1
index 1f00787e80d7..bdcc8dae1db9 100644
@@ -45,7 +45,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
#5 lore raw

PATCH v2 5/6 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 bdcc8dae1db9..ce02c0ec011f 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
#6 lore raw

PATCH v2 6/6 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 ce02c0ec011f..afbe47764de4 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