Browse Source

Set image format from v4l pixfmt

Martijn Braam 1 year ago
parent
commit
c8ed0a0aef
4 changed files with 74 additions and 17 deletions
  1. 5 2
      include/libdng.h
  2. 25 9
      src/libdng.c
  3. 40 6
      src/mode.c
  4. 4 0
      src/mode.h

+ 5 - 2
include/libdng.h

@@ -36,10 +36,13 @@ EXPORT int
 libdng_set_mode_from_name(libdng_info *dng, const char *name);
 
 EXPORT int
-libdng_set_make_model(libdng_info *dng, char *make, char *model);
+libdng_set_mode_from_pixfmt(libdng_info *dng, uint32_t pixfmt);
 
 EXPORT int
-libdng_write(libdng_info *dng, const char *path, unsigned int width, unsigned int height, uint8_t *data,
+libdng_set_make_model(libdng_info *dng, const char *make, const char *model);
+
+EXPORT int
+libdng_write(libdng_info *dng, const char *path, unsigned int width, unsigned int height, const uint8_t *data,
 	size_t length);
 
 #endif //LIBDNG_LIBRARY_H

+ 25 - 9
src/libdng.c

@@ -51,14 +51,8 @@ libdng_new(libdng_info *dng)
 }
 
 int
-libdng_set_mode_from_name(libdng_info *dng, const char *name)
+libdng_set_mode_from_index(libdng_info *dng, int index)
 {
-	int index = dng_mode_from_name(name);
-	if (index == 0) {
-		fprintf(stderr, "Invalid mode '%s'\n", name);
-		return 0;
-	}
-
 	uint32_t cfa = dng_cfa_from_mode(index);
 	dng->cfapattern[0] = (cfa >> 24) & 0xFF;
 	dng->cfapattern[1] = (cfa >> 16) & 0xFF;
@@ -68,7 +62,29 @@ libdng_set_mode_from_name(libdng_info *dng, const char *name)
 }
 
 int
-libdng_set_make_model(libdng_info *dng, char *make, char *model)
+libdng_set_mode_from_pixfmt(libdng_info *dng, uint32_t pixfmt)
+{
+	int index = dng_mode_from_pixfmt(pixfmt);
+	if (index == 0) {
+		fprintf(stderr, "Invalid pixfmt '%d'\n", pixfmt);
+		return 0;
+	}
+	return libdng_set_mode_from_index(dng, index);
+}
+
+int
+libdng_set_mode_from_name(libdng_info *dng, const char *name)
+{
+	int index = dng_mode_from_name(name);
+	if (index == 0) {
+		fprintf(stderr, "Invalid mode '%s'\n", name);
+		return 0;
+	}
+	return libdng_set_mode_from_index(dng, index);
+}
+
+int
+libdng_set_make_model(libdng_info *dng, const char *make, const char *model)
 {
 	if (dng == NULL)
 		return 0;
@@ -108,7 +124,7 @@ libdng_set_datetime_now(libdng_info *dng)
 }
 
 int
-libdng_write(libdng_info *dng, const char *path, unsigned int width, unsigned int height, uint8_t *data, size_t length)
+libdng_write(libdng_info *dng, const char *path, unsigned int width, unsigned int height, const uint8_t *data, size_t length)
 {
 	TIFF *tif = TIFFOpen(path, "w");
 	if (!tif) {

+ 40 - 6
src/mode.c

@@ -1,130 +1,152 @@
 #include <string.h>
+#include <linux/videodev2.h>
 #include "mode.h"
 
 static struct pixelformat pixelformat_lut[] = {
 	{
 		.fourcc = "",
 		.name = "unsupported",
+		.pixfmt = 0,
 		.cfa = CFA_NONE,
 		.bits_per_sample = 0,
 	},
 	{
 		.fourcc = "RGGB",
-		.name = "SRRGB8",
+		.name = "SRGGB8",
+		.pixfmt = V4L2_PIX_FMT_SRGGB8,
 		.cfa = CFA_RGGB,
 		.bits_per_sample = 8,
 	},
 	{
 		.fourcc = "GRBG",
 		.name = "SGRBG8",
+		.pixfmt = V4L2_PIX_FMT_SGRBG8,
 		.cfa = CFA_GRBG,
 		.bits_per_sample = 8,
 	},
 	{
 		.fourcc = "GBRG",
-		.name = "SGBR8",
+		.name = "SGBRG8",
+		.pixfmt = V4L2_PIX_FMT_SGBRG8,
 		.cfa = CFA_GBRG,
 		.bits_per_sample = 8,
 	},
 	{
 		.fourcc = "BGGR",
 		.name = "SBGGR8",
+		.pixfmt = V4L2_PIX_FMT_SBGGR8,
 		.cfa = CFA_BGGR,
 		.bits_per_sample = 8,
 	},
 	{
 		.fourcc = "RG10",
 		.name = "SRGGB10",
+		.pixfmt = V4L2_PIX_FMT_SRGGB10,
 		.cfa = CFA_RGGB,
 		.bits_per_sample = 10,
 	},
 	{
 		.fourcc = "BA10",
 		.name = "SGRBG10",
+		.pixfmt = V4L2_PIX_FMT_SGRBG10,
 		.cfa = CFA_GRBG,
 		.bits_per_sample = 10,
 	},
 	{
 		.fourcc = "GB10",
-		.name = "SGBR10",
+		.name = "SGBRG10",
+		.pixfmt = V4L2_PIX_FMT_SGBRG10,
 		.cfa = CFA_GBRG,
 		.bits_per_sample = 10,
 	},
 	{
 		.fourcc = "BG10",
 		.name = "SBGGR10",
+		.pixfmt = V4L2_PIX_FMT_SBGGR10,
 		.cfa = CFA_BGGR,
 		.bits_per_sample = 10,
 	},
 	{
 		.fourcc = "pRAA",
 		.name = "SRGGB10P",
+		.pixfmt = V4L2_PIX_FMT_SRGGB10P,
 		.cfa = CFA_RGGB,
 		.bits_per_sample = 10,
 	},
 	{
 		.fourcc = "pgAA",
 		.name = "SGRBG10P",
+		.pixfmt = V4L2_PIX_FMT_SGRBG10P,
 		.cfa = CFA_GRBG,
 		.bits_per_sample = 10,
 	},
 	{
 		.fourcc = "pGAA",
-		.name = "SGBR10P",
+		.name = "SGBRG10P",
+		.pixfmt = V4L2_PIX_FMT_SGBRG10P,
 		.cfa = CFA_GBRG,
 		.bits_per_sample = 10,
 	},
 	{
 		.fourcc = "pBAA",
 		.name = "SBGGR10P",
+		.pixfmt = V4L2_PIX_FMT_SBGGR10P,
 		.cfa = CFA_BGGR,
 		.bits_per_sample = 10,
 	},
 	{
 		.fourcc = "RG12",
 		.name = "SRGGB12",
+		.pixfmt = V4L2_PIX_FMT_SRGGB12,
 		.cfa = CFA_RGGB,
 		.bits_per_sample = 12,
 	},
 	{
 		.fourcc = "BA12",
 		.name = "SGRBG12",
+		.pixfmt = V4L2_PIX_FMT_SGRBG12,
 		.cfa = CFA_GRBG,
 		.bits_per_sample = 12,
 	},
 	{
 		.fourcc = "GB12",
-		.name = "SGBR12",
+		.name = "SGBRG12",
+		.pixfmt = V4L2_PIX_FMT_SGBRG12,
 		.cfa = CFA_GBRG,
 		.bits_per_sample = 12,
 	},
 	{
 		.fourcc = "BG12",
 		.name = "SBGGR12",
+		.pixfmt = V4L2_PIX_FMT_SBGGR12,
 		.cfa = CFA_BGGR,
 		.bits_per_sample = 12,
 	},
 	{
 		.fourcc = "RG16",
 		.name = "SRGGB16",
+		.pixfmt = V4L2_PIX_FMT_SRGGB16,
 		.cfa = CFA_RGGB,
 		.bits_per_sample = 16,
 	},
 	{
 		.fourcc = "GR16",
 		.name = "SGRBG16",
+		.pixfmt = V4L2_PIX_FMT_SGRBG16,
 		.cfa = CFA_GRBG,
 		.bits_per_sample = 16,
 	},
 	{
 		.fourcc = "GB16",
-		.name = "SGBR16",
+		.name = "SGBRG16",
+		.pixfmt = V4L2_PIX_FMT_SGBRG16,
 		.cfa = CFA_GBRG,
 		.bits_per_sample = 16,
 	},
 	{
 		.fourcc = "BYR2",
 		.name = "SBGGR16",
+		.pixfmt = V4L2_PIX_FMT_SBGGR16,
 		.cfa = CFA_BGGR,
 		.bits_per_sample = 16,
 	},
@@ -145,6 +167,18 @@ dng_mode_from_name(const char *name)
 	return 0;
 }
 
+int
+dng_mode_from_pixfmt(uint32_t pixfmt)
+{
+	int count = sizeof(pixelformat_lut) / sizeof(pixelformat_lut[0]);
+	for (int i = 0; i < count; i++) {
+		if (pixelformat_lut[i].pixfmt == pixfmt) {
+			return i;
+		}
+	}
+	return 0;
+}
+
 uint32_t
 dng_cfa_from_mode(int index)
 {

+ 4 - 0
src/mode.h

@@ -12,6 +12,7 @@
 struct pixelformat {
 		char *fourcc;
 		char *name;
+		uint32_t pixfmt;
 		uint32_t cfa;
 		int bits_per_sample;
 };
@@ -19,5 +20,8 @@ struct pixelformat {
 int
 dng_mode_from_name(const char *name);
 
+int
+dng_mode_from_pixfmt(uint32_t pixfmt);
+
 uint32_t
 dng_cfa_from_mode(int index);