Skip to content

Commit 7c01ef5

Browse files
committed
Wrap .split() result in RawImage
1 parent d4a32a4 commit 7c01ef5

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

src/utils/image.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -663,10 +663,10 @@ export class RawImage {
663663
* For example, splitting an "RGB" image creates three new images each containing a copy of one of the original bands (red, green, blue).
664664
*
665665
* Inspired by PIL's `Image.split()` [function](https://pillow.readthedocs.io/en/latest/reference/Image.html#PIL.Image.Image.split).
666-
* @returns {(Uint8Array|Uint8ClampedArray)[]} An array containing bands.
666+
* @returns {RawImage[]} An array containing bands.
667667
*/
668668
split() {
669-
const { data, channels } = this;
669+
const { data, width, height, channels } = this;
670670

671671
/** @type {typeof Uint8Array | typeof Uint8ClampedArray} */
672672
const data_type = /** @type {any} */(data.constructor);
@@ -685,7 +685,7 @@ export class RawImage {
685685
split_data[j][i] = data[data_offset + j];
686686
}
687687
}
688-
return split_data;
688+
return split_data.map((data) => new RawImage(data, width, height, 1));
689689
}
690690

691691
/**

tests/utils/utils.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ describe("Utilities", () => {
7272
});
7373

7474
it("Can split image into separate channels", async () => {
75-
const image_data = tiny_image.split()
75+
const image_data = tiny_image.split().map(x => x.data);
7676

7777
const target = [
7878
new Uint8Array([0, 3, 1, 4]), // Reds
@@ -84,7 +84,7 @@ describe("Utilities", () => {
8484
});
8585

8686
it("Can splits channels for grayscale", async () => {
87-
const image_data = tiny_image.grayscale().split();
87+
const image_data = tiny_image.grayscale().split().map(x => x.data);
8888
const target = [new Uint8Array([1, 3, 2, 1])];
8989

9090
compare(image_data, target);

0 commit comments

Comments
 (0)