浏览代码

Fix bytes used calculation for packed bayer formats

Kristian Vos 5 月之前
父节点
当前提交
47ce86c024
共有 2 个文件被更改,包括 22 次插入6 次删除
  1. 3 0
      include/libmegapixels.h
  2. 19 6
      src/mode.c

+ 3 - 0
include/libmegapixels.h

@@ -190,6 +190,9 @@ libmegapixels_mode_equals(libmegapixels_mode *a, libmegapixels_mode *b);
 EXPORT int
 libmegapixels_v4l_pixfmt_to_index(uint32_t pixfmt);
 
+EXPORT int
+libmegapixels_mode_is_packed(int index);
+
 // The AAA API is considered completely unstable and shouldn't be used yet
 
 EXPORT void

+ 19 - 6
src/mode.c

@@ -306,6 +306,8 @@ libmegapixels_format_cfa_pattern(int format)
 	}
 }
 
+// Useful for the calculation: https://www.1stvision.com/cameras/IDS/IDS-manuals/en/basics-raw-bayer-pixel-formats.html
+
 /* Calculates the number of bytes required to store a row of pixels for a given mode and width. */
 uint32_t
 libmegapixels_mode_width_to_bytes(int index, uint32_t width)
@@ -313,13 +315,10 @@ libmegapixels_mode_width_to_bytes(int index, uint32_t width)
 	uint32_t bits_per_pixel = mode_lut[index].bpp;
 	uint64_t bits_per_width = width * (uint64_t) bits_per_pixel;
 	uint64_t remainder = bits_per_width % 8;
-	uint32_t result = 0;
 	if (remainder == 0)
-		result = bits_per_width / 8;
-	else
-		result = (bits_per_width + 8 - remainder) / 8;
+		return bits_per_width / 8;
 
-	return result;
+	return (bits_per_width + 8 - remainder) / 8;
 }
 
 /* Calculates the padding (in bytes) required at the end of each row for a given mode and width. */
@@ -327,7 +326,7 @@ uint32_t
 libmegapixels_mode_width_to_padding(int index, uint32_t width)
 {
 	int remainder = mode_lut[index].bpp % 8;
-	if (remainder == 0)
+	if (remainder == 0 || libmegapixels_mode_is_packed(index))
 		return 0;
 
 	uint32_t padding = ((uint64_t) (8 - remainder) * width) / 8;
@@ -399,6 +398,20 @@ libmegapixels_mode_equals(libmegapixels_mode *a, libmegapixels_mode *b)
 	return 1;
 }
 
+int
+libmegapixels_mode_is_packed(int index)
+{
+	switch (mode_lut[index].v4l_pixel_format) {
+		case V4L2_PIX_FMT_SBGGR10P:
+		case V4L2_PIX_FMT_SGBRG10P:
+		case V4L2_PIX_FMT_SGRBG10P:
+		case V4L2_PIX_FMT_SRGGB10P:
+			return 1;
+		default:
+			return 0;
+	}
+}
+
 int
 mode_snprintf(char *buf, size_t maxlen, libmegapixels_mode *mode)
 {