Skip to content

Commit

Permalink
refactor(pixel): rename internals
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Oct 12, 2021
1 parent 043e3eb commit 055e799
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 59 deletions.
8 changes: 4 additions & 4 deletions packages/pixel/src/convolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {
import { ensureChannel } from "./checks";
import { FloatBuffer } from "./float";
import { FLOAT_GRAY } from "./format/float-gray";
import { asIntVec } from "./internal/utils";
import { __asIntVec } from "./internal/utils";
import { range } from "./range";

/**
Expand Down Expand Up @@ -126,9 +126,9 @@ const initConvolve = (src: FloatBuffer, opts: ConvolveOpts) => {
...opts,
};
const size = kernel.size;
const [kw, kh] = asIntVec(size);
const [strideX, strideY] = asIntVec(sampleStride);
const [offsetX, offsetY] = asIntVec(offset);
const [kw, kh] = __asIntVec(size);
const [strideX, strideY] = __asIntVec(sampleStride);
const [offsetX, offsetY] = __asIntVec(offset);
assert(strideX >= 1 && strideY >= 1, `illegal stride: ${sampleStride}`);
const { width, height, stride: srcStride, rowStride } = src;
const dwidth = Math.floor(width / strideX);
Expand Down
8 changes: 4 additions & 4 deletions packages/pixel/src/float.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import type {
import { ensureChannel, ensureSize } from "./checks";
import { defFloatFormat } from "./format/float-format";
import { FLOAT_GRAY } from "./format/float-gray";
import { clampRegion, prepRegions } from "./internal/utils";
import { __clampRegion, __prepRegions } from "./internal/utils";
import { PackedBuffer } from "./packed";
import { defSampler } from "./sample";

Expand Down Expand Up @@ -204,7 +204,7 @@ export class FloatBuffer

blend(op: BlendFnFloat, dest: FloatBuffer, opts?: Partial<BlitOpts>) {
this.ensureFormat(dest);
const { sx, sy, dx, dy, rw, rh } = prepRegions(this, dest, opts);
const { sx, sy, dx, dy, rw, rh } = __prepRegions(this, dest, opts);
if (rw < 1 || rh < 1) return dest;
const sbuf = this.pixels;
const dbuf = dest.pixels;
Expand Down Expand Up @@ -232,7 +232,7 @@ export class FloatBuffer

blit(dest: FloatBuffer, opts?: Partial<BlitOpts>) {
this.ensureFormat(dest);
const { sx, sy, dx, dy, rw, rh } = prepRegions(this, dest, opts);
const { sx, sy, dx, dy, rw, rh } = __prepRegions(this, dest, opts);
if (rw < 1 || rh < 1) return dest;
const sbuf = this.pixels;
const dbuf = dest.pixels;
Expand Down Expand Up @@ -274,7 +274,7 @@ export class FloatBuffer
}

getRegion(x: number, y: number, width: number, height: number) {
const [sx, sy, w, h] = clampRegion(
const [sx, sy, w, h] = __clampRegion(
x,
y,
width,
Expand Down
6 changes: 3 additions & 3 deletions packages/pixel/src/format/float-format.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Fn2, FnN2, IObjectOf, NumericArray } from "@thi.ng/api";
import { clamp01 } from "@thi.ng/math/interval";
import { FloatFormat, FloatFormatSpec, Lane } from "../api";
import { luminanceABGR } from "../internal/utils";
import { __luminanceABGR } from "../internal/utils";

export const defFloatFormat = (fmt: FloatFormatSpec) => {
const chan = fmt.channels;
Expand Down Expand Up @@ -59,7 +59,7 @@ const defConvert1Gray = (res: FloatFormat) => {
res.toABGR = (col) =>
((((clamp01(col[0]) * 0xff + 0.5) | 0) * 0x010101) | 0xff000000) >>> 0;
res.fromABGR = (col, out = []) => (
(out[0] = luminanceABGR(col) / 0xff), out
(out[0] = __luminanceABGR(col) / 0xff), out
);
};

Expand Down Expand Up @@ -90,7 +90,7 @@ const defConvert2Gray = (res: FloatFormat, from: FnN2) => {
return out >>> 0;
};
res.fromABGR = (col, out = []) => {
out[gray] = luminanceABGR(col) / 0xff;
out[gray] = __luminanceABGR(col) / 0xff;
out[alpha] = from(col, alpha);
return out;
};
Expand Down
4 changes: 2 additions & 2 deletions packages/pixel/src/format/gray-alpha16.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Lane } from "../api";
import { luminanceABGR } from "../internal/utils";
import { __luminanceABGR } from "../internal/utils";
import { defPackedFormat } from "./packed-format";

export const GRAY_ALPHA16 = defPackedFormat({
Expand All @@ -10,7 +10,7 @@ export const GRAY_ALPHA16 = defPackedFormat({
{ size: 16, lane: Lane.RED },
],
fromABGR: (x) =>
(((luminanceABGR(x) + 0.5) | 0) * 0x0101) |
(((__luminanceABGR(x) + 0.5) | 0) * 0x0101) |
(((x >>> 8) & 0xff0000) * 0x0101),
toABGR: (x) => ((x & 0xff000000) | (((x >>> 8) & 0xff) * 0x010101)) >>> 0,
});
4 changes: 2 additions & 2 deletions packages/pixel/src/format/gray-alpha8.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Lane } from "../api";
import { luminanceABGR } from "../internal/utils";
import { __luminanceABGR } from "../internal/utils";
import { defPackedFormat } from "./packed-format";

export const GRAY_ALPHA8 = defPackedFormat({
Expand All @@ -10,6 +10,6 @@ export const GRAY_ALPHA8 = defPackedFormat({
{ size: 8, lane: Lane.ALPHA },
{ size: 8, lane: Lane.RED },
],
fromABGR: (x) => luminanceABGR(x) | ((x >>> 16) & 0xff00),
fromABGR: (x) => __luminanceABGR(x) | ((x >>> 16) & 0xff00),
toABGR: (x) => (((x & 0xff00) << 16) | ((x & 0xff) * 0x010101)) >>> 0,
});
4 changes: 2 additions & 2 deletions packages/pixel/src/format/gray16.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Lane } from "../api";
import { luminanceABGR } from "../internal/utils";
import { __luminanceABGR } from "../internal/utils";
import { defPackedFormat } from "./packed-format";

export const GRAY16 = defPackedFormat({
type: "u16",
size: 16,
channels: [{ size: 16, lane: Lane.RED }],
fromABGR: (x) => ((luminanceABGR(x) + 0.5) | 0) * 0x0101,
fromABGR: (x) => ((__luminanceABGR(x) + 0.5) | 0) * 0x0101,
toABGR: (x) => (0xff000000 | ((x >>> 8) * 0x010101)) >>> 0,
});
4 changes: 2 additions & 2 deletions packages/pixel/src/format/gray8.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Lane } from "../api";
import { luminanceABGR } from "../internal/utils";
import { __luminanceABGR } from "../internal/utils";
import { defPackedFormat } from "./packed-format";

export const GRAY8 = defPackedFormat({
type: "u8",
size: 8,
channels: [{ size: 8, lane: Lane.RED }],
fromABGR: (x) => luminanceABGR(x),
fromABGR: (x) => __luminanceABGR(x),
toABGR: (x) => (0xff000000 | ((x & 0xff) * 0x010101)) >>> 0,
});
6 changes: 3 additions & 3 deletions packages/pixel/src/format/packed-format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
PackedFormat,
PackedFormatSpec,
} from "../api";
import { compileFromABGR, compileToABGR } from "../internal/codegen";
import { __compileFromABGR, __compileToABGR } from "../internal/codegen";

const defChannel = (
ch: PackedChannelSpec,
Expand Down Expand Up @@ -52,7 +52,7 @@ export const defPackedFormat = (fmt: PackedFormatSpec): PackedFormat => {
size: fmt.size,
alpha: fmt.alpha || 0,
channels,
fromABGR: fmt.fromABGR || compileFromABGR(channels),
toABGR: fmt.toABGR || compileToABGR(channels, !!fmt.alpha),
fromABGR: fmt.fromABGR || __compileFromABGR(channels),
toABGR: fmt.toABGR || __compileToABGR(channels, !!fmt.alpha),
};
};
12 changes: 6 additions & 6 deletions packages/pixel/src/internal/codegen.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Fn } from "@thi.ng/api";
import type { PackedChannel } from "../api";
import { luminanceABGR } from "./utils";
import { __luminanceABGR } from "./utils";

const compileLShift = (x: string, shift: number) =>
shift > 0
Expand All @@ -14,19 +14,19 @@ const compileRShift = (x: string, shift: number) => compileLShift(x, -shift);
const hex = (x: number) => `0x${x.toString(16)}`;

/** @internal */
export const compileGrayFromABGR = (size: number) => {
export const __compileGrayFromABGR = (size: number) => {
const shift = 8 - size;
const mask = (1 << size) - 1;
return <Fn<number, number>>(
new Function(
"luma",
`return (x) => ${compileRShift("luma(x)", shift)} & ${mask};`
)(luminanceABGR)
)(__luminanceABGR)
);
};

/** @internal */
export const compileGrayToABGR = (size: number) => {
export const __compileGrayToABGR = (size: number) => {
let body: string;
if (size !== 8) {
const mask = (1 << size) - 1;
Expand All @@ -42,7 +42,7 @@ export const compileGrayToABGR = (size: number) => {
};

/** @internal */
export const compileFromABGR = (chans: PackedChannel[]) =>
export const __compileFromABGR = (chans: PackedChannel[]) =>
<Fn<number, number>>new Function(
"x",
"return (" +
Expand All @@ -56,7 +56,7 @@ export const compileFromABGR = (chans: PackedChannel[]) =>
);

/** @internal */
export const compileToABGR = (chans: PackedChannel[], hasAlpha: boolean) => {
export const __compileToABGR = (chans: PackedChannel[], hasAlpha: boolean) => {
const body = chans
.map((ch) => {
if (ch.size !== 8) {
Expand Down
24 changes: 12 additions & 12 deletions packages/pixel/src/internal/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { isNumber } from "@thi.ng/checks/is-number";
import { clamp } from "@thi.ng/math/interval";
import type { BlitOpts, PackedFormat } from "../api";

export const luminanceABGR: FnN = (c) =>
export const __luminanceABGR: FnN = (c) =>
(((c >>> 16) & 0xff) * 29 + ((c >>> 8) & 0xff) * 150 + (c & 0xff) * 76) /
255;

/** @internal */
export const clampRegion = (
export const __clampRegion = (
sx: number,
sy: number,
w: number,
Expand All @@ -28,7 +28,7 @@ export const clampRegion = (
};

/** @internal */
export const prepRegions = (
export const __prepRegions = (
src: { width: number; height: number },
dest: { width: number; height: number },
opts: Partial<BlitOpts> = {}
Expand All @@ -38,15 +38,15 @@ export const prepRegions = (
let sx: number, sy: number;
let dx: number, dy: number;
let rw: number, rh: number;
[sx, sy, rw, rh] = clampRegion(
[sx, sy, rw, rh] = __clampRegion(
opts.sx || 0,
opts.sy || 0,
opts.w || sw,
opts.h || src.height,
sw,
src.height
);
[dx, dy, rw, rh, sx, sy] = clampRegion(
[dx, dy, rw, rh, sx, sy] = __clampRegion(
opts.dx || 0,
opts.dy || 0,
rw,
Expand All @@ -60,7 +60,7 @@ export const prepRegions = (
};

/** @internal */
export const setChannelUni = (
export const __setChannelUni = (
dbuf: UIntArray,
src: number,
set: Fn2<number, number, number>
Expand All @@ -71,7 +71,7 @@ export const setChannelUni = (
};

/** @internal */
export const setChannelSame = (
export const __setChannelSame = (
dbuf: UIntArray,
sbuf: UIntArray,
get: Fn<number, number>,
Expand All @@ -83,7 +83,7 @@ export const setChannelSame = (
};

/** @internal */
export const setChannelConvert = (
export const __setChannelConvert = (
dbuf: UIntArray,
sbuf: UIntArray,
from: Fn<number, number>,
Expand All @@ -96,7 +96,7 @@ export const setChannelConvert = (
}
};

export const transformABGR = (
export const __transformABGR = (
pix: UIntArray,
format: PackedFormat,
fn: Fn<number, number>
Expand All @@ -109,11 +109,11 @@ export const transformABGR = (
};

/** @internal */
export const asVec = (x: number | [number, number]) =>
export const __asVec = (x: number | [number, number]) =>
isNumber(x) ? [x, x] : x;

/** @internal */
export const asIntVec = (x: number | [number, number]) => {
const v = asVec(x);
export const __asIntVec = (x: number | [number, number]) => {
const v = __asVec(x);
return [v[0] | 0, v[1] | 0];
};
4 changes: 2 additions & 2 deletions packages/pixel/src/normal-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ensureChannel } from "./checks";
import { convolveChannel } from "./convolve";
import { FloatBuffer } from "./float";
import { FLOAT_NORMAL } from "./format/float-norm";
import { asVec } from "./internal/utils";
import { __asVec } from "./internal/utils";

/**
* Computes normal map image (aka gradient in X & Y directions and a static Z
Expand Down Expand Up @@ -33,7 +33,7 @@ export const normalMap = (src: FloatBuffer, opts?: Partial<NormalMapOpts>) => {
};
ensureChannel(src.format, channel);
const spec = [-1, ...new Array(step).fill(0), 1];
const [sx, sy] = asVec(scale);
const [sx, sy] = __asVec(scale);
const dest = new FloatBuffer(src.width, src.height, FLOAT_NORMAL);
dest.setChannel(
0,
Expand Down
Loading

0 comments on commit 055e799

Please sign in to comment.