T clipInput ( T input , T output ) { double ratioInput = input . width / ( double ) input . height ; double ratioOutput = output . width / ( double ) output . height ; T a = input ; if ( ratioInput > ratioOutput ) { // clip the width int width = input . height * output . width / output . height ; int x0 = ( input . width - width ) / 2 ; int x1 = x0 + width ; clipped = input . subimage ( x0 , 0 , x1 , input . height , clipped ) ; a = clipped ; } else if ( ratioInput < ratioOutput ) { // clip the height int height = input . width * output . height / output . width ; int y0 = ( input . height - height ) / 2 ; int y1 = y0 + height ; clipped = input . subimage ( 0 , y0 , input . width , y1 , clipped ) ; a = clipped ; } return a ; }
Clip the input image to ensure a constant aspect ratio