Java tutorial
//package com.java2s; public class Main { private static int computeSampleSize(int width, int height, int desiredWidth, int desiredHeight) { double wr = (double) width / desiredWidth; double hr = (double) height / desiredHeight; double ratio = Math.min(wr, hr); float n = 1.0f; while ((n * 2) <= ratio) { n *= 2; } return (int) n; } }