* Convert an image represented as a typed array to a JIMP object. * * @param {Float32Array} imageData * * @returns {Promise[Jimp]} Jimp object representing image.
(imageData)
| 126 | * @returns {Promise[Jimp]} Jimp object representing image. |
| 127 | */ |
| 128 | async function arrayToJimp(imageData) { |
| 129 | const bufferLen = IMAGE_HEIGHT * IMAGE_WIDTH * 4; |
| 130 | const buffer = new Uint8Array(bufferLen); |
| 131 | |
| 132 | let index = 0; |
| 133 | for (let i = 0; i < IMAGE_HEIGHT; ++i) { |
| 134 | for (let j = 0; j < IMAGE_WIDTH; ++j) { |
| 135 | const inIndex = (i * IMAGE_WIDTH + j); |
| 136 | const val = imageData[inIndex] * 255; |
| 137 | buffer.set([Math.floor(val)], index++); |
| 138 | buffer.set([Math.floor(val)], index++); |
| 139 | buffer.set([Math.floor(val)], index++); |
| 140 | buffer.set([255], index++); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | return new Promise((resolve, reject) => { |
| 145 | new jimp( |
| 146 | {data: buffer, width: IMAGE_WIDTH, height: IMAGE_HEIGHT}, |
| 147 | (err, img) => { |
| 148 | if (err) { |
| 149 | reject(err); |
| 150 | } else { |
| 151 | resolve(img); |
| 152 | } |
| 153 | }); |
| 154 | }); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Preview an image on the console. |