Skip to content

Commit

Permalink
feat(pixel): add unsafe getters/setters
Browse files Browse the repository at this point in the history
- update IPixelBuffer and all impls
  • Loading branch information
postspectacular committed Nov 1, 2021
1 parent 611f0da commit 714d6f7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
16 changes: 16 additions & 0 deletions packages/pixel/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,14 @@ export interface IPixelBuffer<T extends TypedArray = TypedArray, P = any> {
*/
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.
Expand All @@ -220,6 +228,14 @@ export interface IPixelBuffer<T extends TypedArray = TypedArray, P = any> {
*/
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.
*
Expand Down
20 changes: 14 additions & 6 deletions packages/pixel/src/float.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions packages/pixel/src/packed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand All @@ -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 = <PackedChannel>ensureChannel(this.format, id);
const col = this.getAt(x, y);
Expand Down

0 comments on commit 714d6f7

Please sign in to comment.