const _upsampleBilinear = ( { image , padOneWidth , padOneHeight } ) => { const { width , height , data } = image ; const dstWidth = image . width * 2 + ( padOneWidth ? 1 : 0 ) ; const dstHeight = image . height * 2 + ( padOneHeight ? 1 : 0 ) ; const temp = new Float32Array ( dstWidth * dstHeight ) ; for ( let i = 0 ; i < width ; i ++ ) { for ( let j = 0 ; j < height ; j ++ ) { const v = 0.25 * data [ j * width + i ] ; const ii = Math . floor ( i / 2 ) ; const jj = Math . floor ( j / 2 ) ; const pos = Math . floor ( j / 2 ) * dstWidth + Math . floor ( i / 2 ) ; temp [ pos ] += v ; temp [ pos + 1 ] += v ; temp [ pos + dstWidth ] += v ; temp [ pos + dstWidth + 1 ] += v ; } } return { data : temp , width : dstWidth , height : dstHeight } ; } ; export const upsampleBilinear = ( { image , padOneWidth , padOneHeight } ) => { const { width , height , data } = image ; const dstWidth = image . width * 2 + ( padOneWidth ? 1 : 0 ) ; const dstHeight = image . height * 2 + ( padOneHeight ? 1 : 0 ) ; const temp = new Float32Array ( dstWidth * dstHeight ) ; for ( let i = 0 ; i < dstWidth ; i ++ ) { const si = 0.5 * i - 0.25 ; let si0 = Math . floor ( si ) ; let si1 = Math . ceil ( si ) ; if ( si0 < 0 ) si0 = 0 ; if ( si1 >= width ) si1 = width - 1 ; for ( let j = 0 ; j < dstHeight ; j ++ ) { const sj = 0.5 * j - 0.25 ; let sj0 = Math . floor ( sj ) ; let sj1 = Math . ceil ( sj ) ; if ( sj0 < 0 ) sj0 = 0 ; if ( sj1 >= height ) sj1 = height - 1 ; const value = ( si1 - si ) * ( sj1 - sj ) * data [ sj0 * width + si0 ] + ( si1 - si ) * ( sj - sj0 ) * data [ sj1 * width + si0 ] + ( si - si0 ) * ( sj1 - sj ) * data [ sj0 * width + si1 ] + ( si - si0 ) * ( sj - sj0 ) * data [ sj1 * width + si1 ] ; temp [ j * dstWidth + i ] = value ; } } return { data : temp , width : dstWidth , height : dstHeight } ; } ; export const downsampleBilinear = ( { image } ) => { const { data , width , height } = image ; const dstWidth = Math . floor ( width / 2 ) ; const dstHeight = Math . floor ( height / 2 ) ; const temp = new Float32Array ( dstWidth * dstHeight ) ; const offsets = [ 0 , 1 , width , width + 1 ] ; for ( let j = 0 ; j < dstHeight ; j ++ ) { for ( let i = 0 ; i < dstWidth ; i ++ ) { let srcPos = j * 2 * width + i * 2 ; let value = 0.0 ; for ( let d = 0 ; d < offsets . length ; d ++ ) { value += data [ srcPos + offsets [ d ] ] ; } value *= 0.25 ; temp [ j * dstWidth + i ] = value ; } } return { data : temp , width : dstWidth , height : dstHeight } ; } ; export const resize = ( { image , ratio } ) => { const width = Math . round ( image . width * ratio ) ; const height = Math . round ( image . height * ratio ) ; const imageData = new Uint8Array ( width * height ) ; for ( let i = 0 ; i < width ; i ++ ) { let si1 = Math . round ( 1.0 * i / ratio ) ; let si2 = Math . round ( 1.0 * ( i + 1 ) / ratio ) - 1 ; if ( si2 >= image . width ) si2 = image . width - 1 ; for ( let j = 0 ; j < height ; j ++ ) { let sj1 = Math . round ( 1.0 * j / ratio ) ; let sj2 = Math . round ( 1.0 * ( j + 1 ) / ratio ) - 1 ; if ( sj2 >= image . height ) sj2 = image . height - 1 ; let sum = 0 ; let count = 0 ; for ( let ii = si1 ; ii <= si2 ; ii ++ ) { for ( let jj = sj1 ; jj <= sj2 ; jj ++ ) { sum += ( 1.0 * image . data [ jj * image . width + ii ] ) ; count += 1 ; } } imageData [ j * width + i ] = Math . floor ( sum / count ) ; } } return { data : imageData , width : width , height : height } ; } ;