Skip to content

Commit

Permalink
refactor(pixel): minor, dedupe kernel normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jun 17, 2024
1 parent b8fd47d commit 4fec4e6
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions packages/pixel/src/convolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,7 @@ export const defKernel = (
const kvars: string[] = [];
const h2 = h >> 1;
const w2 = w >> 1;
if (normalize) {
const scale = 1 / (<number[]>tpl).reduce((acc, x) => acc + x, 0);
tpl = (<number[]>tpl).map((x) => x * scale);
}
if (normalize) tpl = __normalize(<NumericArray>tpl);
for (let y = 0, i = 0; y < h; y++) {
const yy = y - h2;
const row: string[] = [];
Expand Down Expand Up @@ -262,10 +259,7 @@ export const defLargeKernel = (
h: number,
normalize = false
): Fn<FloatBuffer, FnN3> => {
if (normalize) {
const scale = 1 / (<number[]>kernel).reduce((acc, x) => acc + x, 0);
kernel = (<number[]>kernel).map((x) => x * scale);
}
if (normalize) kernel = __normalize(kernel);
return (src) => {
const {
data,
Expand Down Expand Up @@ -299,6 +293,12 @@ export const defLargeKernel = (
};
};

/** @internal */
const __normalize = (kernel: NumericArray) => {
const scale = 1 / (<number[]>kernel).reduce((acc, x) => acc + x, 0);
return (<number[]>kernel).map((x) => x * scale);
};

export const POOL_NEAREST: PoolTemplate = (body, w, h) =>
body[(h >> 1) * w + (w >> 1)];

Expand Down

0 comments on commit 4fec4e6

Please sign in to comment.