| Server IP : 217.154.3.148 / Your IP : 216.73.216.90 Web Server : Apache System : Linux blissful-wright.217-154-3-148.plesk.page 6.8.0-124-generic #124-Ubuntu SMP PREEMPT_DYNAMIC Tue May 26 13:00:45 UTC 2026 x86_64 User : gracious-boyd_hfhh1h8vo9n ( 10000) PHP Version : 8.3.31 Disable Function : opcache_get_status MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /var/www/vhosts/gracious-boyd.217-154-3-148.plesk.page/nextjs/lib/render/ |
Upload File : |
import { boxBlur } from '@/utils/maskOps'
export type MapU8 = { data: Uint8Array; width: number; height: number }
export function lumaFromRgba(rgba: Uint8ClampedArray, width: number, height: number): Uint8Array {
const out = new Uint8Array(width * height)
for (let i = 0, j = 0; i < out.length; i++, j += 4) {
const r = rgba[j]
const g = rgba[j + 1]
const b = rgba[j + 2]
out[i] = Math.max(0, Math.min(255, Math.round(0.299 * r + 0.587 * g + 0.114 * b)))
}
return out
}
export function normalizeU8(data: Uint8Array, mask?: Uint8Array | null): Uint8Array {
let min = 255
let max = 0
for (let i = 0; i < data.length; i++) {
if (mask && !mask[i]) continue
const v = data[i]
if (v < min) min = v
if (v > max) max = v
}
const range = Math.max(1, max - min)
const out = new Uint8Array(data.length)
for (let i = 0; i < data.length; i++) {
if (mask && !mask[i]) {
out[i] = 0
continue
}
out[i] = Math.max(0, Math.min(255, Math.round(((data[i] - min) * 255) / range)))
}
return out
}
/**
* Builds a smooth light/brightness map from the original image:
* - luma
* - strong blur
* - normalize to 0..255
* Values outside `tilesMask` are set to 0.
*/
export function buildLightMap(opts: {
rgba: Uint8ClampedArray
width: number
height: number
tilesMask?: Uint8Array | null
blurRadiusPx?: number
}): MapU8 {
const width = Math.max(1, Math.round(opts.width))
const height = Math.max(1, Math.round(opts.height))
const tilesMask = opts.tilesMask ?? null
const blurRadius = Math.max(0, Math.round(opts.blurRadiusPx ?? 40))
const gray = lumaFromRgba(opts.rgba, width, height)
if (!blurRadius) {
return { data: normalizeU8(gray, tilesMask), width, height }
}
const blurred = boxBlur(gray, width, height, blurRadius)
const blurredU8 = new Uint8Array(width * height)
for (let i = 0; i < blurredU8.length; i++) {
const v = blurred[i]
blurredU8[i] = Math.max(0, Math.min(255, Math.round(Number.isFinite(v) ? v : gray[i])))
}
const norm = normalizeU8(blurredU8, tilesMask)
return { data: norm, width, height }
}