[libcamera-devel] utils: rkisp1: gen-csc-table: Add support for inverting the CSC
diff mbox series

Message ID 20220927232707.7596-1-laurent.pinchart@ideasonboard.com
State Superseded
Headers show
Series
  • [libcamera-devel] utils: rkisp1: gen-csc-table: Add support for inverting the CSC
Related show

Commit Message

Laurent Pinchart Sept. 27, 2022, 11:27 p.m. UTC
Add a -i/--invert command line argument to invert the YCbCr encoding and
output a YCbCr to RGB matrix.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 utils/rkisp1/gen-csc-table.py | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

Comments

Jacopo Mondi Sept. 28, 2022, 6:25 a.m. UTC | #1
Hi Laurent

On Wed, Sep 28, 2022 at 02:27:07AM +0300, Laurent Pinchart via libcamera-devel wrote:
> Add a -i/--invert command line argument to invert the YCbCr encoding and
> output a YCbCr to RGB matrix.
>
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>

Thanks
  j

> ---
>  utils/rkisp1/gen-csc-table.py | 15 ++++++++++++---
>  1 file changed, 12 insertions(+), 3 deletions(-)
>
> diff --git a/utils/rkisp1/gen-csc-table.py b/utils/rkisp1/gen-csc-table.py
> index 2fb702746421..83aea357966a 100755
> --- a/utils/rkisp1/gen-csc-table.py
> +++ b/utils/rkisp1/gen-csc-table.py
> @@ -7,6 +7,7 @@
>
>  import argparse
>  import enum
> +import numpy as np
>  import sys
>
>
> @@ -63,7 +64,7 @@ class Quantization(enum.Enum):
>      LIMITED = 1
>
>
> -def scale_coeff(coeff, quantization, luma, precision):
> +def scale_coeff(coeff, quantization, luma, precision, invert):
>      """Scale a coefficient to the output range dictated by the quantization and
>      the precision.
>
> @@ -91,6 +92,9 @@ def scale_coeff(coeff, quantization, luma, precision):
>      else:
>          out_range = 240 - 16
>
> +    if invert:
> +        in_range, out_range = out_range, in_range
> +
>      return coeff * out_range / in_range * (1 << precision)
>
>
> @@ -150,6 +154,8 @@ def main(argv):
>          description='Generate color space conversion table coefficients with '
>          'configurable fixed-point precision.'
>      )
> +    parser.add_argument('--invert', '-i', action='store_true',
> +                        help='Invert the color space conversion (YUV -> RGB)')
>      parser.add_argument('--precision', '-p', default='Q1.7',
>                          help='The output fixed point precision in Q notation (sign bit excluded)')
>      parser.add_argument('--quantization', '-q', choices=['full', 'limited'],
> @@ -166,12 +172,15 @@ def main(argv):
>      encoding = encodings[args.encoding]
>      quantization = Quantization[args.quantization.upper()]
>
> +    if args.invert:
> +        encoding = np.linalg.inv(encoding)
> +
>      # Scale and round the encoding coefficients based on the precision and
>      # quantization range.
>      luma = True
>      scaled_coeffs = []
>      for line in encoding:
> -        line = [scale_coeff(coeff, quantization, luma, precision.fractional) for coeff in line]
> +        line = [scale_coeff(coeff, quantization, luma, precision.fractional, args.invert) for coeff in line]
>          scaled_coeffs.append(line)
>          luma = False
>
> @@ -188,7 +197,7 @@ def main(argv):
>      # Print the result as C code.
>      nbits = 1 << (precision.total - 1).bit_length()
>      nbytes = nbits // 4
> -    print(f'static const u{nbits} rgb2yuv_{args.encoding}_{quantization.name.lower()}_coeffs[] = {{')
> +    print(f'static const u{nbits} {"yuv2rgb" if args.invert else "rgb2yuv"}_{args.encoding}_{quantization.name.lower()}_coeffs[] = {{')
>
>      for line in rounded_coeffs:
>          line = [f'0x{coeff:0{nbytes}x}' for coeff in line]
> --
> Regards,
>
> Laurent Pinchart
>

Patch
diff mbox series

diff --git a/utils/rkisp1/gen-csc-table.py b/utils/rkisp1/gen-csc-table.py
index 2fb702746421..83aea357966a 100755
--- a/utils/rkisp1/gen-csc-table.py
+++ b/utils/rkisp1/gen-csc-table.py
@@ -7,6 +7,7 @@ 
 
 import argparse
 import enum
+import numpy as np
 import sys
 
 
@@ -63,7 +64,7 @@  class Quantization(enum.Enum):
     LIMITED = 1
 
 
-def scale_coeff(coeff, quantization, luma, precision):
+def scale_coeff(coeff, quantization, luma, precision, invert):
     """Scale a coefficient to the output range dictated by the quantization and
     the precision.
 
@@ -91,6 +92,9 @@  def scale_coeff(coeff, quantization, luma, precision):
     else:
         out_range = 240 - 16
 
+    if invert:
+        in_range, out_range = out_range, in_range
+
     return coeff * out_range / in_range * (1 << precision)
 
 
@@ -150,6 +154,8 @@  def main(argv):
         description='Generate color space conversion table coefficients with '
         'configurable fixed-point precision.'
     )
+    parser.add_argument('--invert', '-i', action='store_true',
+                        help='Invert the color space conversion (YUV -> RGB)')
     parser.add_argument('--precision', '-p', default='Q1.7',
                         help='The output fixed point precision in Q notation (sign bit excluded)')
     parser.add_argument('--quantization', '-q', choices=['full', 'limited'],
@@ -166,12 +172,15 @@  def main(argv):
     encoding = encodings[args.encoding]
     quantization = Quantization[args.quantization.upper()]
 
+    if args.invert:
+        encoding = np.linalg.inv(encoding)
+
     # Scale and round the encoding coefficients based on the precision and
     # quantization range.
     luma = True
     scaled_coeffs = []
     for line in encoding:
-        line = [scale_coeff(coeff, quantization, luma, precision.fractional) for coeff in line]
+        line = [scale_coeff(coeff, quantization, luma, precision.fractional, args.invert) for coeff in line]
         scaled_coeffs.append(line)
         luma = False
 
@@ -188,7 +197,7 @@  def main(argv):
     # Print the result as C code.
     nbits = 1 << (precision.total - 1).bit_length()
     nbytes = nbits // 4
-    print(f'static const u{nbits} rgb2yuv_{args.encoding}_{quantization.name.lower()}_coeffs[] = {{')
+    print(f'static const u{nbits} {"yuv2rgb" if args.invert else "rgb2yuv"}_{args.encoding}_{quantization.name.lower()}_coeffs[] = {{')
 
     for line in rounded_coeffs:
         line = [f'0x{coeff:0{nbytes}x}' for coeff in line]