MCPcopy Index your code
hub / github.com/tensorflow/tfjs / decodeJpeg

Function decodeJpeg

tfjs-react-native/src/decode_image.ts:34–55  ·  view source on GitHub ↗
(
    contents: Uint8Array, channels: 0|1|3 = 3)

Source from the content-addressed store, hash-verified

32 * @doc {heading: 'Media', subheading: 'Images'}
33 */
34export 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.

Callers

nothing calls this directly

Calls 3

tensor3dFunction · 0.90
getImageTypeFunction · 0.70
decodeMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…