Skip to content

Commit

Permalink
feat(pixel): update/improve FloatBuffer.as() single channel conversions
Browse files Browse the repository at this point in the history
- check if both source & dest formats are single channel
- if so, convert directly via getNormalized() and avoid intermediate
  (lossy) conversion via ABGR
- using only scalar access also faster than per-pixel subarrays
  • Loading branch information
postspectacular committed Jan 7, 2023
1 parent eedb24f commit 0146075
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions packages/pixel/src/float.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,17 @@ export class FloatBuffer
} = this;
const dest = new IntBuffer(width, height, fmt);
const dpixels = dest.data;
for (let i = 0, j = 0, n = data.length; i < n; i += stride, j++) {
dpixels[j] = fmt.fromABGR(
sfmt.toABGR(data.subarray(i, i + stride))
);
if (sfmt.size === 1 && fmt.channels.length === 1) {
const setFloat = fmt.channels[0].setFloat;
for (let i = 0, j = 0, n = data.length; i < n; i += stride, j++) {
dpixels[j] = setFloat(0, sfmt.getNormalized(data[i]));
}
} else {
for (let i = 0, j = 0, n = data.length; i < n; i += stride, j++) {
dpixels[j] = fmt.fromABGR(
sfmt.toABGR(data.subarray(i, i + stride))
);
}
}
return dest;
}
Expand Down

0 comments on commit 0146075

Please sign in to comment.