Skip to content

Commit

Permalink
fix(pixel): add clamping for float->ABGR conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Feb 26, 2021
1 parent 6a31dc3 commit 41540e0
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions packages/pixel/src/format/float-format.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Fn2, FnN2, IObjectOf, NumericArray } from "@thi.ng/api";
import { clamp01 } from "@thi.ng/math";
import { FloatFormat, FloatFormatSpec, Lane } from "../api";
import { luminanceABGR } from "../utils";

Expand All @@ -19,7 +20,7 @@ export const defFloatFormat = (fmt: FloatFormatSpec) => {
return res;
}
const to = (col: NumericArray, i: number) =>
((col[i] * 0xff + 0.5) | 0) << chanShift[chan[i]];
((clamp01(col[i]) * 0xff + 0.5) | 0) << chanShift[chan[i]];
const from: FnN2 = (col, i) => ((col >>> chanShift[chan[i]]) & 0xff) / 0xff;
switch (chan.length) {
case 1:
Expand Down Expand Up @@ -56,7 +57,7 @@ const defConvert1 = (

const defConvert1Gray = (res: FloatFormat) => {
res.toABGR = (col) =>
((((col[0] * 0xff + 0.5) | 0) * 0x010101) | 0xff000000) >>> 0;
((((clamp01(col[0]) * 0xff + 0.5) | 0) * 0x010101) | 0xff000000) >>> 0;
res.fromABGR = (col, out = []) => (
(out[0] = luminanceABGR(col) / 0xff), out
);
Expand Down Expand Up @@ -84,7 +85,7 @@ const defConvert2Gray = (res: FloatFormat, from: FnN2) => {
const gray = ~~(res.channels[0] === Lane.ALPHA);
const alpha = gray ^ 1;
res.toABGR = (col) => {
let out = ((col[gray] * 0xff + 0.5) | 0) * 0x010101;
let out = ((clamp01(col[gray]) * 0xff + 0.5) | 0) * 0x010101;
out |= ((col[alpha] * 0xff + 0.5) | 0) << 24;
return out >>> 0;
};
Expand Down

0 comments on commit 41540e0

Please sign in to comment.