license import * as tf from '@tensorflow/tfjs' ; class MockContext { getImageData ( x , y , width , height ) { const data = new Uint8ClampedArray ( width * height * 4 ) ; for ( let i = 0 ; i < data . length ; ++ i ) { data [ i ] = i + 1 ; } return { data } ; } } class MockCanvas { constructor ( public width , public height ) { } getContext ( type ) { return new MockContext ( ) ; } } describe ( 'tf.browser.fromPixels with polyfills' , ( ) => { it ( 'accepts a canvas-like element' , async ( ) => { const c = new MockCanvas ( 2 , 2 ) ; const t = tf . browser . fromPixels ( c as any ) ; expect ( t . dtype ) . toBe ( 'int32' ) ; expect ( t . shape ) . toEqual ( [ 2 , 2 , 3 ] ) ; tf . test_util . expectArraysEqual ( await t . data ( ) , [ 1 , 2 , 3 , 5 , 6 , 7 , 9 , 10 , 11 , 13 , 14 , 15 ] ) ; } ) ; it ( 'accepts a canvas-like element, numChannels=4' , async ( ) => { const c = new MockCanvas ( 2 , 2 ) ; const t = tf . browser . fromPixels ( c as any , 4 ) ; expect ( t . dtype ) . toBe ( 'int32' ) ; expect ( t . shape ) . toEqual ( [ 2 , 2 , 4 ] ) ; tf . test_util . expectArraysEqual ( await t . data ( ) , [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 ] ) ; } ) ; it ( 'errors when passed a non-canvas object' , ( ) => { const c = 5 ; expect ( ( ) => tf . browser . fromPixels ( c as any ) ) . toThrowError ( /pixels passed to tf\.browser\.fromPixels\(\) must be either an/ ) ; } ) ; } ) ;