UNPKG

@thi.ng/pixel

Version:

Typedarray integer & float pixel buffers w/ customizable formats, blitting, drawing, convolution

59 lines (58 loc) 1.72 kB
import { __luminanceABGR } from "./utils.js"; const __compileLShift = (x, shift) => shift > 0 ? `(${x} << ${shift})` : shift < 0 ? `(${x} >>> ${-shift})` : `${x}`; const __compileRShift = (x, shift) => __compileLShift(x, -shift); const __hex = (x) => `0x${x.toString(16)}`; const __compileGrayFromABGR = (size) => { const shift = 8 - size; const mask = (1 << size) - 1; return new Function( "luma", `return (x) => ${__compileRShift("luma(x)", shift)} & ${mask};` )(__luminanceABGR); }; const __compileGrayToABGR = (size) => { let body; if (size !== 8) { const mask = (1 << size) - 1; const scale = 255 / mask; body = `(((x & ${mask}) * ${scale}) | 0)`; } else { body = "x"; } return new Function("x", `return 0xff000000 | (${body} * 0x010101);`); }; const __compileFromABGR = (chans) => new Function( "x", "return (" + chans.map((ch) => { const shift = ch.abgrShift + (8 - ch.size); return `(${__compileRShift("x", shift)} & ${__hex( ch.maskA )})`; }).join(" | ") + ") >>> 0;" ); const __compileToABGR = (chans, hasAlpha) => { const body = chans.map((ch) => { if (ch.size !== 8) { const mask = ch.mask0; const scale = 255 / mask; const inner = __compileRShift("x", ch.shift); return __compileLShift( `((${inner} & ${mask}) * ${scale})`, 24 - ch.lane * 8 ); } else { return __compileLShift( `(x & ${__hex(ch.maskA)})`, ch.abgrShift ); } }).join(" | "); const alpha = !hasAlpha ? `0xff000000 | ` : ""; return new Function("x", `return (${alpha}${body}) >>> 0;`); }; export { __compileFromABGR, __compileGrayFromABGR, __compileGrayToABGR, __compileToABGR };