* Split this image into individual bands. This method returns an array of individual image bands from an image. * For example, splitting an "RGB" image creates three new images each containing a copy of one of the original bands (red, green, blue). * * Inspired by PIL's `Image.split()
()
| 698 | * @returns {RawImage[]} An array containing bands. |
| 699 | */ |
| 700 | split() { |
| 701 | const { data, width, height, channels } = this; |
| 702 | |
| 703 | /** @type {typeof Uint8Array | typeof Uint8ClampedArray} */ |
| 704 | const data_type = /** @type {any} */ (data.constructor); |
| 705 | const per_channel_length = data.length / channels; |
| 706 | |
| 707 | // Pre-allocate buffers for each channel |
| 708 | const split_data = Array.from({ length: channels }, () => new data_type(per_channel_length)); |
| 709 | |
| 710 | // Write pixel data |
| 711 | for (let i = 0; i < per_channel_length; ++i) { |
| 712 | const data_offset = channels * i; |
| 713 | for (let j = 0; j < channels; ++j) { |
| 714 | split_data[j][i] = data[data_offset + j]; |
| 715 | } |
| 716 | } |
| 717 | return split_data.map((data) => new RawImage(data, width, height, 1)); |
| 718 | } |
| 719 | |
| 720 | /** |
| 721 | * Helper method to update the image data. |
no test coverage detected