Skip to content

Commit

Permalink
feat(pixel): add FloatFormat.range
Browse files Browse the repository at this point in the history
- update all float formats
- update FloatBuffer.clamp/clampChannel/getChannel()
  • Loading branch information
postspectacular committed Jan 9, 2023
1 parent 965af0d commit 0dbac7d
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 4 deletions.
2 changes: 2 additions & 0 deletions packages/pixel/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ export interface FloatFormat extends IABGRConvert<NumericArray> {
shift: IObjectOf<number>;
channels: Lane[];

range: [number, number];

/**
* Maps given value to [0..1] interval. Used in combination with
* {@link IntChannel.setFloat}.
Expand Down
14 changes: 10 additions & 4 deletions packages/pixel/src/float.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IGrid2DMixin } from "@thi.ng/api/mixins/igrid";
import { isNumber } from "@thi.ng/checks/is-number";
import { isString } from "@thi.ng/checks/is-string";
import { assert } from "@thi.ng/errors/assert";
import { clamp01 } from "@thi.ng/math/interval";
import { clamp } from "@thi.ng/math/interval";
import {
isPremultiplied,
postmultiply,
Expand Down Expand Up @@ -248,9 +248,10 @@ export class FloatBuffer
data,
stride: [stride],
} = this;
const [min, max] = this.format.range;
const dest = new Float32Array(this.width * this.height);
for (let i = id, j = 0, n = data.length; i < n; i += stride, j++) {
dest[j] = clamp01(data[i]);
dest[j] = clamp(data[i], min, max);
}
return new FloatBuffer(this.width, this.height, FLOAT_GRAY, dest);
}
Expand Down Expand Up @@ -437,8 +438,9 @@ export class FloatBuffer

clamp() {
const data = this.data;
const [min, max] = this.format.range;
for (let i = data.length; i-- > 0; ) {
data[i] = clamp01(data[i]);
data[i] = clamp(data[i], min, max);
}
return this;
}
Expand All @@ -449,8 +451,12 @@ export class FloatBuffer
data,
stride: [stride],
} = this;
const [min, max] = this.format.range;
for (let i = id, n = data.length; i < n; i += stride) {
data[i] = clamp01(data[i]);
data[i] = clamp(data[i], min, max);
}
}

}
}

Expand Down
1 change: 1 addition & 0 deletions packages/pixel/src/format/float-format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const defFloatFormat = (fmt: FloatFormatSpec) => {
__float: true,
size: chan.length,
shift: chanShift,
range: [0, 1],
getNormalized: (val) => clamp01(val),
};
if (fmt.convert) {
Expand Down
1 change: 1 addition & 0 deletions packages/pixel/src/format/float-gray-range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const FLOAT_GRAY_RANGE = (min: number, max: number): FloatFormat => {
channels: [Lane.RED],
shift: { 3: 0 },
size: 1,
range: [min, max],
getNormalized: (val) => {
return fitClamped(val, min, max, 0, 1);
},
Expand Down
1 change: 1 addition & 0 deletions packages/pixel/src/format/float-norm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const FLOAT_NORMAL: FloatFormat = {
channels: [Lane.RED, Lane.GREEN, Lane.BLUE],
shift: { 3: 0, 2: 8, 1: 16 },
size: 3,
range: [-1, 1],
getNormalized: (val) => clamp01(val * 0.5 + 0.5),
fromABGR: (src) => [
from(src & 0xff),
Expand Down

0 comments on commit 0dbac7d

Please sign in to comment.