[2/2] apps: common: dng_writer: Fix RAW10 and RAW12 packing on BE machines
diff mbox series

Message ID 20240628213535.6906-3-laurent.pinchart@ideasonboard.com
State Accepted
Commit e3310749f5abb8003c85f7bdf01ce14b9e4b1fab
Headers show
Series
  • apps: common: dng_write: Endianness fixes
Related show

Commit Message

Laurent Pinchart June 28, 2024, 9:35 p.m. UTC
The 16-bit padded raw 10 and raw 12 formats are stored in memory in
little endian order, regardless of the machine's endianness. Read pixel
data as uint8_t values and hardcode bit shifting to little endian to fix
scanline packing.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 src/apps/common/dng_writer.cpp | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

Comments

Umang Jain July 1, 2024, 4:47 a.m. UTC | #1
Hi Laurent,

Thank you for the patch.

On 29/06/24 3:05 am, Laurent Pinchart wrote:
> The 16-bit padded raw 10 and raw 12 formats are stored in memory in
> little endian order, regardless of the machine's endianness. Read pixel
> data as uint8_t values and hardcode bit shifting to little endian to fix
> scanline packing.
>
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

Reviewed-by: Umang Jain <umang.jain@ideasonboard.com>

> ---
>   src/apps/common/dng_writer.cpp | 24 ++++++++++++------------
>   1 file changed, 12 insertions(+), 12 deletions(-)
>
> diff --git a/src/apps/common/dng_writer.cpp b/src/apps/common/dng_writer.cpp
> index 50db5eb33c83..355433b08d68 100644
> --- a/src/apps/common/dng_writer.cpp
> +++ b/src/apps/common/dng_writer.cpp
> @@ -139,29 +139,29 @@ void packScanlineRaw8(void *output, const void *input, unsigned int width)
>   
>   void packScanlineRaw10(void *output, const void *input, unsigned int width)
>   {
> -	const uint16_t *in = static_cast<const uint16_t *>(input);
> +	const uint8_t *in = static_cast<const uint8_t *>(input);
>   	uint8_t *out = static_cast<uint8_t *>(output);
>   
>   	for (unsigned int i = 0; i < width; i += 4) {
> -		*out++ = (in[0] & 0x3fc) >> 2;
> -		*out++ = (in[0] & 0x003) << 6 | (in[1] & 0x3f0) >> 4;
> -		*out++ = (in[1] & 0x00f) << 4 | (in[2] & 0x3c0) >> 6;
> -		*out++ = (in[2] & 0x03f) << 2 | (in[3] & 0x300) >> 8;
> -		*out++ = (in[3] & 0x0ff);
> -		in += 4;
> +		*out++ = in[1] << 6 | in[0] >> 2;
> +		*out++ = in[0] << 6 | (in[3] & 0x03) << 4 | in[2] >> 4;
> +		*out++ = in[2] << 4 | (in[5] & 0x03) << 2 | in[4] >> 6;
> +		*out++ = in[4] << 2 | (in[7] & 0x03) << 0;
> +		*out++ = in[6];
> +		in += 8;
>   	}
>   }
>   
>   void packScanlineRaw12(void *output, const void *input, unsigned int width)
>   {
> -	const uint16_t *in = static_cast<const uint16_t *>(input);
> +	const uint8_t *in = static_cast<const uint8_t *>(input);
>   	uint8_t *out = static_cast<uint8_t *>(output);
>   
>   	for (unsigned int i = 0; i < width; i += 2) {
> -		*out++ = (in[0] & 0xff0) >> 4;
> -		*out++ = (in[0] & 0x00f) << 4 | (in[1] & 0xf00) >> 8;
> -		*out++ = (in[1] & 0x0ff);
> -		in += 2;
> +		*out++ = in[1] << 4 | in[0] >> 4;
> +		*out++ = in[0] << 4 | (in[3] & 0x0f);
> +		*out++ = in[2];
> +		in += 4;
>   	}
>   }
>
Stefan Klug July 1, 2024, 7:48 a.m. UTC | #2
Hi Laurent,

Thank you for the patch.

On Sat, Jun 29, 2024 at 12:35:35AM +0300, Laurent Pinchart wrote:
> The 16-bit padded raw 10 and raw 12 formats are stored in memory in
> little endian order, regardless of the machine's endianness. Read pixel
> data as uint8_t values and hardcode bit shifting to little endian to fix
> scanline packing.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

Yep, now it's correct :-)

Reviewed-by: Stefan Klug <stefan.klug@ideasonboard.com> 

> ---
>  src/apps/common/dng_writer.cpp | 24 ++++++++++++------------
>  1 file changed, 12 insertions(+), 12 deletions(-)
> 
> diff --git a/src/apps/common/dng_writer.cpp b/src/apps/common/dng_writer.cpp
> index 50db5eb33c83..355433b08d68 100644
> --- a/src/apps/common/dng_writer.cpp
> +++ b/src/apps/common/dng_writer.cpp
> @@ -139,29 +139,29 @@ void packScanlineRaw8(void *output, const void *input, unsigned int width)
>  
>  void packScanlineRaw10(void *output, const void *input, unsigned int width)
>  {
> -	const uint16_t *in = static_cast<const uint16_t *>(input);
> +	const uint8_t *in = static_cast<const uint8_t *>(input);
>  	uint8_t *out = static_cast<uint8_t *>(output);
>  
>  	for (unsigned int i = 0; i < width; i += 4) {
> -		*out++ = (in[0] & 0x3fc) >> 2;
> -		*out++ = (in[0] & 0x003) << 6 | (in[1] & 0x3f0) >> 4;
> -		*out++ = (in[1] & 0x00f) << 4 | (in[2] & 0x3c0) >> 6;
> -		*out++ = (in[2] & 0x03f) << 2 | (in[3] & 0x300) >> 8;
> -		*out++ = (in[3] & 0x0ff);
> -		in += 4;
> +		*out++ = in[1] << 6 | in[0] >> 2;
> +		*out++ = in[0] << 6 | (in[3] & 0x03) << 4 | in[2] >> 4;
> +		*out++ = in[2] << 4 | (in[5] & 0x03) << 2 | in[4] >> 6;
> +		*out++ = in[4] << 2 | (in[7] & 0x03) << 0;
> +		*out++ = in[6];
> +		in += 8;
>  	}
>  }
>  
>  void packScanlineRaw12(void *output, const void *input, unsigned int width)
>  {
> -	const uint16_t *in = static_cast<const uint16_t *>(input);
> +	const uint8_t *in = static_cast<const uint8_t *>(input);
>  	uint8_t *out = static_cast<uint8_t *>(output);
>  
>  	for (unsigned int i = 0; i < width; i += 2) {
> -		*out++ = (in[0] & 0xff0) >> 4;
> -		*out++ = (in[0] & 0x00f) << 4 | (in[1] & 0xf00) >> 8;
> -		*out++ = (in[1] & 0x0ff);
> -		in += 2;
> +		*out++ = in[1] << 4 | in[0] >> 4;
> +		*out++ = in[0] << 4 | (in[3] & 0x0f);
> +		*out++ = in[2];
> +		in += 4;
>  	}
>  }
>  
> -- 
> Regards,
> 
> Laurent Pinchart
>

Patch
diff mbox series

diff --git a/src/apps/common/dng_writer.cpp b/src/apps/common/dng_writer.cpp
index 50db5eb33c83..355433b08d68 100644
--- a/src/apps/common/dng_writer.cpp
+++ b/src/apps/common/dng_writer.cpp
@@ -139,29 +139,29 @@  void packScanlineRaw8(void *output, const void *input, unsigned int width)
 
 void packScanlineRaw10(void *output, const void *input, unsigned int width)
 {
-	const uint16_t *in = static_cast<const uint16_t *>(input);
+	const uint8_t *in = static_cast<const uint8_t *>(input);
 	uint8_t *out = static_cast<uint8_t *>(output);
 
 	for (unsigned int i = 0; i < width; i += 4) {
-		*out++ = (in[0] & 0x3fc) >> 2;
-		*out++ = (in[0] & 0x003) << 6 | (in[1] & 0x3f0) >> 4;
-		*out++ = (in[1] & 0x00f) << 4 | (in[2] & 0x3c0) >> 6;
-		*out++ = (in[2] & 0x03f) << 2 | (in[3] & 0x300) >> 8;
-		*out++ = (in[3] & 0x0ff);
-		in += 4;
+		*out++ = in[1] << 6 | in[0] >> 2;
+		*out++ = in[0] << 6 | (in[3] & 0x03) << 4 | in[2] >> 4;
+		*out++ = in[2] << 4 | (in[5] & 0x03) << 2 | in[4] >> 6;
+		*out++ = in[4] << 2 | (in[7] & 0x03) << 0;
+		*out++ = in[6];
+		in += 8;
 	}
 }
 
 void packScanlineRaw12(void *output, const void *input, unsigned int width)
 {
-	const uint16_t *in = static_cast<const uint16_t *>(input);
+	const uint8_t *in = static_cast<const uint8_t *>(input);
 	uint8_t *out = static_cast<uint8_t *>(output);
 
 	for (unsigned int i = 0; i < width; i += 2) {
-		*out++ = (in[0] & 0xff0) >> 4;
-		*out++ = (in[0] & 0x00f) << 4 | (in[1] & 0xf00) >> 8;
-		*out++ = (in[1] & 0x0ff);
-		in += 2;
+		*out++ = in[1] << 4 | in[0] >> 4;
+		*out++ = in[0] << 4 | (in[3] & 0x0f);
+		*out++ = in[2];
+		in += 4;
 	}
 }