Skip to content

Commit

Permalink
wip: add more constants
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Aug 15, 2024
1 parent ebfd65c commit 2384a87
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
12 changes: 9 additions & 3 deletions src/lib/converters/colorConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,20 @@ export interface ColorConvertConstants {
readonly YOffset: number
readonly CbCrOffset: number

readonly KRKRioKG: number
readonly KBKBioKG: number

readonly KRoKBi: number
readonly KGoKBi: number
readonly KBoKRi: number
readonly KGoKRi: number
}

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
Expand All @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions src/lib/converters/yuv422ToRgba.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 2384a87

Please sign in to comment.