From 2384a876626c04c0223d89b56592a26073eebfc4 Mon Sep 17 00:00:00 2001 From: Julian Waller Date: Thu, 15 Aug 2024 22:44:18 +0100 Subject: [PATCH] wip: add more constants --- src/lib/converters/colorConstants.ts | 12 +++++++++--- src/lib/converters/yuv422ToRgba.ts | 3 +-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/lib/converters/colorConstants.ts b/src/lib/converters/colorConstants.ts index 67990ed3..bddb8bec 100644 --- a/src/lib/converters/colorConstants.ts +++ b/src/lib/converters/colorConstants.ts @@ -13,6 +13,9 @@ export interface ColorConvertConstants { readonly YOffset: number readonly CbCrOffset: number + readonly KRKRioKG: number + readonly KBKBioKG: number + readonly KRoKBi: number readonly KGoKBi: number readonly KBoKRi: number @@ -20,10 +23,10 @@ export interface ColorConvertConstants { } function createColorConvertConstants(KR: number, KB: number): ColorConvertConstants { - const KG = 1 - KR - KB + const KG = 1 - KR - KB // 0.7152 - const KRi = 1 - KR - const KBi = 1 - KB + const KRi = 1 - KR // 0.7874 -> 1.5748 + const KBi = 1 - KB // 0.9278 -> 1.8556 const YRange = 219 const CbCrRange = 224 @@ -44,6 +47,9 @@ function createColorConvertConstants(KR: number, KB: number): ColorConvertConsta YOffset: 16 << 8, CbCrOffset: 128 << 8, + KRKRioKG: (KR * KRi * 2) / KG, + KBKBioKG: (KB * KBi * 2) / KG, + KRoKBi: (KR / KBi) * HalfCbCrRange, KGoKBi: (KG / KBi) * HalfCbCrRange, KBoKRi: (KB / KRi) * HalfCbCrRange, diff --git a/src/lib/converters/yuv422ToRgba.ts b/src/lib/converters/yuv422ToRgba.ts index 38ac5552..e66fd56d 100644 --- a/src/lib/converters/yuv422ToRgba.ts +++ b/src/lib/converters/yuv422ToRgba.ts @@ -33,8 +33,7 @@ export function convertYUV422ToRGBA(width: number, height: number, data: Buffer) a10: number ): [r: number, g: number, b: number, a: number] => { const r = clamp(Math.round(y8 + constants.KRi * cr8)) - // TODO - use more constants - const g = clamp(Math.round(y8 - ((0.2126 * 1.5748) / 0.7152) * cr8 - ((0.0722 * 1.8556) / 0.7152) * cb8)) + const g = clamp(Math.round(y8 - constants.KRKRioKG * cr8 - constants.KBKBioKG * cb8)) const b = clamp(Math.round(y8 + constants.KBi * cb8)) const a = Math.round(a10)