license import { Tensor , Tensor3D , Tensor4D , tidy , util } from '@tensorflow/tfjs' ; import { ensureTensorflowBackend , nodeBackend } from './nodejs_kernel_backend' ; export 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 ; } } param image param format param quality param progressive param optimizeSize param chromaDownsampling param densityUnit param xDensity param yDensity param xmpMetadata returns doc export async function encodeJpeg ( image , format = '' , quality = 95 , progressive = false , optimizeSize = false , chromaDownsampling = true , densityUnit = 'in' , xDensity = 300 , yDensity = 300 , xmpMetadata = '' ) { ensureTensorflowBackend ( ) ; const backendEncodeImage = ( imageData ) => nodeBackend ( ) . encodeJpeg ( imageData , image . shape , format , quality , progressive , optimizeSize , chromaDownsampling , densityUnit , xDensity , yDensity , xmpMetadata ) ; return encodeImage ( image , backendEncodeImage ) ; } param image param compression returns doc export async function encodePng ( image , compression = 1 ) { ensureTensorflowBackend ( ) ; const backendEncodeImage = ( imageData ) => nodeBackend ( ) . encodePng ( imageData , image . shape , compression ) ; return encodeImage ( image , backendEncodeImage ) ; } async function encodeImage ( image , backendEncodeImage ) { const encodedDataTensor = backendEncodeImage ( new Uint8Array ( await image . data ( ) ) ) ; const encodedPngData = ( await encodedDataTensor . data ( ) ) [ 0 ] as any as Uint8Array ; encodedDataTensor . dispose ( ) ; return encodedPngData ; } /**
 * Helper function to get image type based on starting bytes of the image file.
 */ export 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 (BMP, JPEG, PNG, or GIF), but got unsupported ' + 'image type' ) ; } }