From 714d6f7fc8bfffa37c09dfc2c4251b1041935a24 Mon Sep 17 00:00:00 2001 From: Karsten Schmidt Date: Tue, 2 Nov 2021 00:21:09 +0100 Subject: [PATCH] feat(pixel): add unsafe getters/setters - update IPixelBuffer and all impls --- packages/pixel/src/api.ts | 16 ++++++++++++++++ packages/pixel/src/float.ts | 20 ++++++++++++++------ packages/pixel/src/packed.ts | 9 +++++++++ 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/packages/pixel/src/api.ts b/packages/pixel/src/api.ts index ed16990946..5d552c5571 100644 --- a/packages/pixel/src/api.ts +++ b/packages/pixel/src/api.ts @@ -210,6 +210,14 @@ export interface IPixelBuffer { */ getAt(x: number, y: number): P; + /** + * Non-boundschecked version of {@link IPixelBuffer.getAt}. + * + * @param x + * @param y + */ + getAtUnsafe(x: number, y: number): P; + /** * Writes pixel value at given position. Has no effect if outside of * the defined region. @@ -220,6 +228,14 @@ export interface IPixelBuffer { */ setAt(x: number, y: number, col: P): this; + /** + * Non-boundschecked version of {@link IPixelBuffer.setAt}. + * + * @param x + * @param y + */ + setAtUnsafe(x: number, y: number, col: P): this; + /** * Extracts region as new pixel buffer in same format. * diff --git a/packages/pixel/src/float.ts b/packages/pixel/src/float.ts index 76bf9ab410..cc4a61aa7d 100644 --- a/packages/pixel/src/float.ts +++ b/packages/pixel/src/float.ts @@ -132,12 +132,15 @@ export class FloatBuffer } getAt(x: number, y: number) { - const { width, stride } = this; - if (x >= 0 && x < width && y >= 0 && y < this.height) { - const idx = (x | 0) * stride + (y | 0) * this.rowStride; - return this.pixels.subarray(idx, idx + stride); - } - return this.__empty; + return x >= 0 && x < this.width && y >= 0 && y < this.height + ? this.getAtUnsafe(x, y) + : this.__empty; + } + + getAtUnsafe(x: number, y: number) { + const stride = this.stride; + const idx = (x | 0) * stride + (y | 0) * this.rowStride; + return this.pixels.subarray(idx, idx + stride); } setAt(x: number, y: number, col: NumericArray) { @@ -152,6 +155,11 @@ export class FloatBuffer return this; } + setAtUnsafe(x: number, y: number, col: NumericArray) { + this.pixels.set(col, (x | 0) * this.stride + (y | 0) * this.rowStride); + return this; + } + getChannelAt(x: number, y: number, id: number) { ensureChannel(this.format, id); const { width, stride } = this; diff --git a/packages/pixel/src/packed.ts b/packages/pixel/src/packed.ts index 107a509dea..6420b0eca8 100644 --- a/packages/pixel/src/packed.ts +++ b/packages/pixel/src/packed.ts @@ -165,6 +165,10 @@ export class PackedBuffer : 0; } + getAtUnsafe(x: number, y: number) { + return this.pixels[(x | 0) + (y | 0) * this.width]; + } + setAt(x: number, y: number, col: number) { x >= 0 && x < this.width && @@ -174,6 +178,11 @@ export class PackedBuffer return this; } + setAtUnsafe(x: number, y: number, col: number) { + this.pixels[(x | 0) + (y | 0) * this.width] = col; + return this; + } + getChannelAt(x: number, y: number, id: number, normalized = false) { const chan = ensureChannel(this.format, id); const col = this.getAt(x, y);