private Dimension getScaledDimension ( Dimension originalDimension ) { // check if image has to be rescaled Dimension requestedDimension = getRequestedDimension ( ) ; boolean scaleWidth = ( requestedDimension . getWidth ( ) > 0 && requestedDimension . getWidth ( ) != originalDimension . getWidth ( ) ) ; boolean scaleHeight = ( requestedDimension . getHeight ( ) > 0 && requestedDimension . getHeight ( ) != originalDimension . getHeight ( ) ) ; if ( scaleWidth || scaleHeight ) { long requestedWidth = requestedDimension . getWidth ( ) ; long requestedHeight = requestedDimension . getHeight ( ) ; // calculate missing width/height from ratio if not specified double imageRatio = ( double ) originalDimension . getWidth ( ) / ( double ) originalDimension . getHeight ( ) ; if ( requestedWidth == 0 && requestedHeight > 0 ) { requestedWidth = ( int ) Math . round ( requestedHeight * imageRatio ) ; } else if ( requestedWidth > 0 && requestedHeight == 0 ) { requestedHeight = ( int ) Math . round ( requestedWidth / imageRatio ) ; } // calculate requested ratio double requestedRatio = ( double ) requestedWidth / ( double ) requestedHeight ; // if ratio does not match, or requested width/height is larger than the original image scaling is not possible if ( ! Ratio . matches ( imageRatio , requestedRatio ) || ( originalDimension . getWidth ( ) < requestedWidth ) || ( originalDimension . getHeight ( ) < requestedHeight ) ) { return SCALING_NOT_POSSIBLE_DIMENSION ; } else { return new Dimension ( requestedWidth , requestedHeight ) ; } } return null ; }
Checks if the current binary is an image and has to be scaled . In this case the destination dimension is returned .