license import { Tensor3D , Tensor4D , tidy , util } from '@tensorflow/tfjs-core' ; import { ensureTensorflowBackend , nodeBackend } from './ops/op_utils' ; enum ImageType { JPEG = 'jpeg' , PNG = 'png' , GIF = 'gif' , BMP = 'BMP' } param contents param channels param ratio param fancyUpscaling param tryRecoverTruncated param acceptableFraction param dctMethod returns doc export function decodeJpeg ( contents , channels = 0 , ratio = 1 , fancyUpscaling = true , tryRecoverTruncated = false , acceptableFraction = 1 , dctMethod = '' ) { ensureTensorflowBackend ( ) ; return tidy ( ( ) => { return nodeBackend ( ) . decodeJpeg ( contents , channels , ratio , fancyUpscaling , tryRecoverTruncated , acceptableFraction , dctMethod ) . toInt ( ) ; } ) ; } param contents param channels param dtype returns doc export function decodePng ( contents , channels = 0 , dtype = 'int32' ) { util . assert ( dtype === 'int32' , ( ) => 'decodeImage could only return Tensor of type `int32` for now.' ) ; ensureTensorflowBackend ( ) ; return tidy ( ( ) => { return nodeBackend ( ) . decodePng ( contents , channels ) . toInt ( ) ; } ) ; } param contents param channels returns doc export function decodeBmp ( contents , channels = 0 ) { ensureTensorflowBackend ( ) ; return tidy ( ( ) => { return nodeBackend ( ) . decodeBmp ( contents , channels ) . toInt ( ) ; } ) ; } param contents returns doc export function decodeGif ( contents ) { ensureTensorflowBackend ( ) ; return tidy ( ( ) => { return nodeBackend ( ) . decodeGif ( contents ) . toInt ( ) ; } ) ; } param content param channels param dtype param expandAnimations returns doc export function decodeImage ( content , channels = 0 , dtype = 'int32' , expandAnimations = true ) { util . assert ( dtype === 'int32' , ( ) => 'decodeImage could only return Tensor of type `int32` for now.' ) ; const imageType = getImageType ( content ) ; switch ( imageType ) { case ImageType . JPEG : return decodeJpeg ( content , channels ) ; case ImageType . PNG : return decodePng ( content , channels ) ; case ImageType . GIF : return tidy ( ( ) => { const img = decodeGif ( content ) ; return expandAnimations ? img : img . slice ( 0 , 1 ) . squeeze ( [ 0 ] ) ; } ) ; case ImageType . BMP : return decodeBmp ( content , channels ) ; default : return null ; } } /**
 * Helper function to get image type based on starting bytes of the image file.
 */ function getImageType ( content ) { if ( content . length > 3 && content [ 0 ] === 255 && content [ 1 ] === 216 && content [ 2 ] === 255 ) { return ImageType . JPEG ; } else if ( content . length > 4 && content [ 0 ] === 71 && content [ 1 ] === 73 && content [ 2 ] === 70 && content [ 3 ] === 56 ) { return ImageType . GIF ; } else if ( content . length > 8 && content [ 0 ] === 137 && content [ 1 ] === 80 && content [ 2 ] === 78 && content [ 3 ] === 71 && content [ 4 ] === 13 && content [ 5 ] === 10 && content [ 6 ] === 26 && content [ 7 ] === 10 ) { return ImageType . PNG ; } else if ( content . length > 3 && content [ 0 ] === 66 && content [ 1 ] === 77 ) { return ImageType . BMP ; } else { throw new Error ( 'Expected image (JPEG, PNG, or GIF), but got unsupported image type' ) ; } }