code: 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;
    }
docs: Checks if the current binary is an image and has to be scaled. In this case the destination dimension is returned.
  @return Scaled destination or null if no scaling is required. If a destination object with both
  width and height set to -1 is returned, a scaling is required but not possible with the given source
  object.