Переглянути джерело

Revert padding calculation changes, fix width bytes calculation for unpacked bayer formats, update tests

Kristian Vos 5 місяців тому
батько
коміт
7ef4f52d18
2 змінених файлів з 13 додано та 10 видалено
  1. 11 8
      src/mode.c
  2. 2 2
      tests/check_mode.c

+ 11 - 8
src/mode.c

@@ -307,12 +307,14 @@ 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. */
+// Note that BayerGB12 is packed according to that document, but this is likely a mistake
 uint32_t
 libmegapixels_mode_width_to_bytes(int index, uint32_t width)
 {
 	uint32_t bits_per_pixel = mode_lut[index].bpp;
+	// For bpp like 10/12/14, for formats that are not packed, we want to set bpp to something like 16
+	if (bits_per_pixel % 8 != 0 && !libmegapixels_mode_is_packed(index))
+		bits_per_pixel = bits_per_pixel + 8 - (bits_per_pixel % 8);
 	uint64_t bits_per_width = width * (uint64_t) bits_per_pixel;
 	uint64_t remainder = bits_per_width % 8;
 	if (remainder == 0)
@@ -321,17 +323,18 @@ libmegapixels_mode_width_to_bytes(int index, uint32_t width)
 	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. */
 uint32_t
 libmegapixels_mode_width_to_padding(int index, uint32_t width)
 {
-	int remainder = mode_lut[index].bpp % 8;
-	if (remainder == 0 || libmegapixels_mode_is_packed(index))
+	if (mode_lut[index].bpp == 8) {
 		return 0;
+	}
+	uint64_t bytes_per_width = libmegapixels_mode_width_to_bytes(index, width);
+	uint64_t remainder = bytes_per_width % 8;
+	if (remainder == 0)
+		return remainder;
 
-	uint32_t padding = ((uint64_t) (8 - remainder) * width) / 8;
-
-	return padding;
+	return 8 - remainder;
 }
 
 uint32_t

+ 2 - 2
tests/check_mode.c

@@ -40,14 +40,14 @@ TEST bytesused_for_rggb8(void)
 
 TEST bytesused_for_rggb10(void)
 {
-	CHECK_CALL(check_bytesused(V4L2_PIX_FMT_SRGGB10, 4208, 3120, 5260, 3156, 26257920));
+	CHECK_CALL(check_bytesused(V4L2_PIX_FMT_SRGGB10, 4208, 3120, 8416, 0, 26257920));
 
 	PASS();
 }
 
 TEST bytesused_for_rggb10p(void)
 {
-	CHECK_CALL(check_bytesused(V4L2_PIX_FMT_SRGGB10P, 2592, 1944, 3240, 0, 6298560));
+	CHECK_CALL(check_bytesused(V4L2_PIX_FMT_SRGGB10P, 4208, 3120, 5260, 4, 16423680));
 
 	PASS();
 }