(
contents: Uint8Array, channels: 0|1|3 = 3)
| 32 | * @doc {heading: 'Media', subheading: 'Images'} |
| 33 | */ |
| 34 | export function decodeJpeg( |
| 35 | contents: Uint8Array, channels: 0|1|3 = 3): Tensor3D { |
| 36 | util.assert( |
| 37 | getImageType(contents) === ImageType.JPEG, |
| 38 | () => 'The passed contents are not a valid JPEG image'); |
| 39 | util.assert( |
| 40 | channels === 3, () => 'Only 3 channels is supported at this time'); |
| 41 | const {width, height, data} = jpeg.decode(contents, {useTArray: true}); |
| 42 | // Drop the alpha channel info because jpeg.decode always returns a typedArray |
| 43 | // with 255 |
| 44 | const buffer = new Uint8Array(width * height * 3); |
| 45 | let offset = 0; // offset into original data |
| 46 | for (let i = 0; i < buffer.length; i += 3) { |
| 47 | buffer[i] = data[offset]; |
| 48 | buffer[i + 1] = data[offset + 1]; |
| 49 | buffer[i + 2] = data[offset + 2]; |
| 50 | |
| 51 | offset += 4; |
| 52 | } |
| 53 | |
| 54 | return tensor3d(buffer, [height, width, channels]); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Helper function to get image type based on starting bytes of the image file. |
nothing calls this directly
no test coverage detected
searching dependent graphs…