From 4d404127bf265529df60a36f380daa765d84f226 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Wed, 11 Sep 2024 23:41:32 +1000 Subject: [PATCH] Hoist some JS cos calls up one loop level --- js/thumbhash.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/js/thumbhash.js b/js/thumbhash.js index 369b118..f7cf441 100644 --- a/js/thumbhash.js +++ b/js/thumbhash.js @@ -139,14 +139,16 @@ export function thumbHashToRGBA(hash) { let h = round(ratio > 1 ? 32 / ratio : 32) let rgba = new Uint8Array(w * h * 4), fx = [], fy = [] for (let y = 0, i = 0; y < h; y++) { + // Precompute the fy coefficients + for (let cy = 0, n = max(ly, hasAlpha ? 5 : 3); cy < n; cy++) + fy[cy] = cos(PI / h * (y + 0.5) * cy) + for (let x = 0; x < w; x++, i += 4) { let l = l_dc, p = p_dc, q = q_dc, a = a_dc - // Precompute the coefficients + // Precompute the fx coefficients for (let cx = 0, n = max(lx, hasAlpha ? 5 : 3); cx < n; cx++) fx[cx] = cos(PI / w * (x + 0.5) * cx) - for (let cy = 0, n = max(ly, hasAlpha ? 5 : 3); cy < n; cy++) - fy[cy] = cos(PI / h * (y + 0.5) * cy) // Decode L for (let cy = 0, j = 0; cy < ly; cy++)