|
| 1 | +import { |
| 2 | + calculateHfColorGradingSecondaryMask, |
| 3 | + type NormalizedHfColorGradingSecondary, |
| 4 | +} from "@hyperframes/core/color-grading"; |
| 5 | +import { clampNumber } from "../../utils/studioHelpers"; |
| 6 | + |
| 7 | +const BYTE_MAX = 255; |
| 8 | +const SCOPE_SIZE = 256; |
| 9 | + |
| 10 | +export type ColorGradingScopeMode = "histogram" | "waveform" | "parade" | "vectorscope"; |
| 11 | + |
| 12 | +export interface ColorGradingScopeAnalysis { |
| 13 | + width: number; |
| 14 | + histogram: Uint32Array; |
| 15 | + waveform: Uint32Array; |
| 16 | + parade: Uint32Array; |
| 17 | + vectorscope: Uint32Array; |
| 18 | +} |
| 19 | + |
| 20 | +export async function readColorGradingFramePixels(frame: { |
| 21 | + dataUrl: string; |
| 22 | + width: number; |
| 23 | + height: number; |
| 24 | +}): Promise<Uint8ClampedArray | null> { |
| 25 | + const image = new Image(); |
| 26 | + image.src = frame.dataUrl; |
| 27 | + await image.decode(); |
| 28 | + const canvas = document.createElement("canvas"); |
| 29 | + canvas.width = frame.width; |
| 30 | + canvas.height = frame.height; |
| 31 | + const context = canvas.getContext("2d", { willReadFrequently: true }); |
| 32 | + if (!context) return null; |
| 33 | + context.drawImage(image, 0, 0, frame.width, frame.height); |
| 34 | + return context.getImageData(0, 0, frame.width, frame.height).data; |
| 35 | +} |
| 36 | + |
| 37 | +function rec709Luma(red: number, green: number, blue: number): number { |
| 38 | + return red * 0.2126 + green * 0.7152 + blue * 0.0722; |
| 39 | +} |
| 40 | + |
| 41 | +function rgbToHsv(red: number, green: number, blue: number) { |
| 42 | + const max = Math.max(red, green, blue); |
| 43 | + const min = Math.min(red, green, blue); |
| 44 | + const chroma = max - min; |
| 45 | + let hue = 0; |
| 46 | + if (chroma > 0) { |
| 47 | + if (max === red) hue = ((green - blue) / chroma) % 6; |
| 48 | + else if (max === green) hue = (blue - red) / chroma + 2; |
| 49 | + else hue = (red - green) / chroma + 4; |
| 50 | + hue = (hue * 60 + 360) % 360; |
| 51 | + } |
| 52 | + return { |
| 53 | + hue, |
| 54 | + saturation: max === 0 ? 0 : chroma / max, |
| 55 | + value: max, |
| 56 | + }; |
| 57 | +} |
| 58 | + |
| 59 | +export function colorGradingSecondaryMask( |
| 60 | + red: number, |
| 61 | + green: number, |
| 62 | + blue: number, |
| 63 | + key: NormalizedHfColorGradingSecondary["key"], |
| 64 | +): number { |
| 65 | + const hsv = rgbToHsv(red / BYTE_MAX, green / BYTE_MAX, blue / BYTE_MAX); |
| 66 | + return calculateHfColorGradingSecondaryMask( |
| 67 | + hsv.hue, |
| 68 | + hsv.saturation, |
| 69 | + rec709Luma(red / BYTE_MAX, green / BYTE_MAX, blue / BYTE_MAX), |
| 70 | + key, |
| 71 | + ); |
| 72 | +} |
| 73 | + |
| 74 | +export function buildColorGradingSecondaryMatte( |
| 75 | + pixels: Uint8ClampedArray, |
| 76 | + width: number, |
| 77 | + height: number, |
| 78 | + key: NormalizedHfColorGradingSecondary["key"], |
| 79 | +): Uint8ClampedArray { |
| 80 | + const matte = new Uint8ClampedArray(width * height * 4); |
| 81 | + for (let offset = 0; offset < pixels.length; offset += 4) { |
| 82 | + const alpha = pixels[offset + 3] ?? 0; |
| 83 | + const mask = |
| 84 | + alpha === 0 |
| 85 | + ? 0 |
| 86 | + : colorGradingSecondaryMask( |
| 87 | + pixels[offset] ?? 0, |
| 88 | + pixels[offset + 1] ?? 0, |
| 89 | + pixels[offset + 2] ?? 0, |
| 90 | + key, |
| 91 | + ); |
| 92 | + const value = Math.round(mask * BYTE_MAX); |
| 93 | + matte[offset] = value; |
| 94 | + matte[offset + 1] = value; |
| 95 | + matte[offset + 2] = value; |
| 96 | + matte[offset + 3] = alpha; |
| 97 | + } |
| 98 | + return matte; |
| 99 | +} |
| 100 | + |
| 101 | +function pixelOffset(width: number, x: number, y: number): number { |
| 102 | + return (y * width + x) * 4; |
| 103 | +} |
| 104 | + |
| 105 | +function byteIndex(value: number): number { |
| 106 | + return Math.round(clampNumber(value, 0, 1) * BYTE_MAX); |
| 107 | +} |
| 108 | + |
| 109 | +export function analyzeColorGradingFrame( |
| 110 | + pixels: Uint8ClampedArray, |
| 111 | + width: number, |
| 112 | + height: number, |
| 113 | +): ColorGradingScopeAnalysis { |
| 114 | + const histogram = new Uint32Array(SCOPE_SIZE); |
| 115 | + const waveform = new Uint32Array(width * SCOPE_SIZE); |
| 116 | + const parade = new Uint32Array(width * SCOPE_SIZE * 3); |
| 117 | + const vectorscope = new Uint32Array(SCOPE_SIZE * SCOPE_SIZE); |
| 118 | + |
| 119 | + for (let y = 0; y < height; y += 1) { |
| 120 | + for (let x = 0; x < width; x += 1) { |
| 121 | + const offset = pixelOffset(width, x, y); |
| 122 | + const alpha = pixels[offset + 3] ?? 0; |
| 123 | + if (alpha === 0) continue; |
| 124 | + const red = (pixels[offset] ?? 0) / BYTE_MAX; |
| 125 | + const green = (pixels[offset + 1] ?? 0) / BYTE_MAX; |
| 126 | + const blue = (pixels[offset + 2] ?? 0) / BYTE_MAX; |
| 127 | + const normalizedLuma = rec709Luma(red, green, blue); |
| 128 | + const luma = byteIndex(normalizedLuma); |
| 129 | + histogram[luma] += alpha; |
| 130 | + waveform[x * SCOPE_SIZE + (BYTE_MAX - luma)] += alpha; |
| 131 | + parade[x * SCOPE_SIZE + (BYTE_MAX - byteIndex(red))] += alpha; |
| 132 | + parade[width * SCOPE_SIZE + x * SCOPE_SIZE + (BYTE_MAX - byteIndex(green))] += alpha; |
| 133 | + parade[width * SCOPE_SIZE * 2 + x * SCOPE_SIZE + (BYTE_MAX - byteIndex(blue))] += alpha; |
| 134 | + |
| 135 | + const chromaBlue = (blue - normalizedLuma) / 1.8556; |
| 136 | + const chromaRed = (red - normalizedLuma) / 1.5748; |
| 137 | + const vectorX = byteIndex(chromaBlue + 0.5); |
| 138 | + const vectorY = BYTE_MAX - byteIndex(chromaRed + 0.5); |
| 139 | + vectorscope[vectorY * SCOPE_SIZE + vectorX] += alpha; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + return { width, histogram, waveform, parade, vectorscope }; |
| 144 | +} |
| 145 | + |
| 146 | +export function sampleColorGradingSecondary( |
| 147 | + pixels: Uint8ClampedArray, |
| 148 | + width: number, |
| 149 | + height: number, |
| 150 | + x: number, |
| 151 | + y: number, |
| 152 | + radius = 2, |
| 153 | +): NormalizedHfColorGradingSecondary["key"] | null { |
| 154 | + const minX = clampNumber(Math.floor(x) - radius, 0, width - 1); |
| 155 | + const maxX = clampNumber(Math.floor(x) + radius, 0, width - 1); |
| 156 | + const minY = clampNumber(Math.floor(y) - radius, 0, height - 1); |
| 157 | + const maxY = clampNumber(Math.floor(y) + radius, 0, height - 1); |
| 158 | + let hueX = 0; |
| 159 | + let hueY = 0; |
| 160 | + let saturation = 0; |
| 161 | + let luma = 0; |
| 162 | + let weight = 0; |
| 163 | + |
| 164 | + for (let sampleY = minY; sampleY <= maxY; sampleY += 1) { |
| 165 | + for (let sampleX = minX; sampleX <= maxX; sampleX += 1) { |
| 166 | + const offset = pixelOffset(width, sampleX, sampleY); |
| 167 | + const alpha = (pixels[offset + 3] ?? 0) / BYTE_MAX; |
| 168 | + if (alpha === 0) continue; |
| 169 | + const red = (pixels[offset] ?? 0) / BYTE_MAX; |
| 170 | + const green = (pixels[offset + 1] ?? 0) / BYTE_MAX; |
| 171 | + const blue = (pixels[offset + 2] ?? 0) / BYTE_MAX; |
| 172 | + const hsv = rgbToHsv(red, green, blue); |
| 173 | + const hueRadians = (hsv.hue * Math.PI) / 180; |
| 174 | + const hueWeight = hsv.saturation * alpha; |
| 175 | + hueX += Math.cos(hueRadians) * hueWeight; |
| 176 | + hueY += Math.sin(hueRadians) * hueWeight; |
| 177 | + saturation += hsv.saturation * alpha; |
| 178 | + luma += rec709Luma(red, green, blue) * alpha; |
| 179 | + weight += alpha; |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + if (weight === 0) return null; |
| 184 | + const averageHue = ((Math.atan2(hueY, hueX) * 180) / Math.PI + 360) % 360; |
| 185 | + const averageSaturation = saturation / weight; |
| 186 | + const averageLuma = luma / weight; |
| 187 | + const neutralSample = averageSaturation < 0.02; |
| 188 | + return { |
| 189 | + hue: neutralSample |
| 190 | + ? { center: 0, range: 180, softness: 0 } |
| 191 | + : { center: averageHue, range: 18, softness: 12 }, |
| 192 | + saturation: { |
| 193 | + min: clampNumber(averageSaturation - 0.2, 0, 1), |
| 194 | + max: clampNumber(averageSaturation + 0.2, 0, 1), |
| 195 | + softness: 0.1, |
| 196 | + }, |
| 197 | + luma: { |
| 198 | + min: clampNumber(averageLuma - 0.2, 0, 1), |
| 199 | + max: clampNumber(averageLuma + 0.2, 0, 1), |
| 200 | + softness: 0.1, |
| 201 | + }, |
| 202 | + }; |
| 203 | +} |
0 commit comments