Skip to content

Commit

Permalink
feat(pixel): add IntBuffer/FloatBuffer.flipX()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jan 9, 2023
1 parent 0dbac7d commit daa7c32
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/pixel/src/float.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,28 @@ export class FloatBuffer
}
}

flipX() {
const {
data,
width,
height,
stride: [sx, sy],
} = this;
const tmp = new Float32Array(sx);
const w1 = width - 1;
const w2 = width >>> 1;
for (let y = 0; y < height; y++) {
for (
let x = 0, i = y * sy, j = i + w1 * sx;
x < w2;
x++, i += sx, j -= sx
) {
tmp.set(data.subarray(i, i + sx));
data.copyWithin(i, j, j + sx);
data.set(tmp, j);
}
}
return this;
}

/**
Expand Down
11 changes: 11 additions & 0 deletions packages/pixel/src/int.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,17 @@ export class IntBuffer
this.data.fill(x);
}

/**
* Flips image horizontally.
*/
flipX() {
const { data, width } = this;
for (let i = 0; i < data.length; i += width) {
data.subarray(i, i + width).reverse();
}
return this;
}

/**
* Flips image vertically.
*/
Expand Down

0 comments on commit daa7c32

Please sign in to comment.