Example usage for java.lang Math min

List of usage examples for java.lang Math min

Introduction

In this page you can find the example usage for java.lang Math min.

Prototype

@HotSpotIntrinsicCandidate
public static double min(double a, double b) 

Source Link

Document

Returns the smaller of two double values.

Usage

From source file:Main.java

public static int getSampleSize(BitmapFactory.Options options, int maxWidth, int maxHeight) {
    // raw height and width of image
    int rawWidth = options.outWidth;
    int rawHeight = options.outHeight;

    // calculate best sample size
    int inSampleSize = 0;
    if (rawHeight > maxHeight || rawWidth > maxWidth) {
        float ratioWidth = (float) rawWidth / maxWidth;
        float ratioHeight = (float) rawHeight / maxHeight;
        inSampleSize = (int) Math.min(ratioHeight, ratioWidth);
    }/*from  w  w w. j a v a2  s.  c o m*/
    inSampleSize = Math.max(1, inSampleSize);

    return inSampleSize;
}

From source file:Main.java

/**
 * Get top value of the bounding rectangle of the given points.
 *//* w  ww  .  ja  va  2  s  .co  m*/
static float getRectTop(float[] points) {
    return Math.min(Math.min(Math.min(points[1], points[3]), points[5]), points[7]);
}

From source file:Main.java

public static float calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    float inSampleSize = 1;

    if (height >= reqHeight || width >= reqWidth) {
        if (width > height) {
            inSampleSize = height / (float) reqHeight;
        } else {/*from  www  .  j a  v  a  2  s.c  o  m*/
            inSampleSize = width / (float) reqWidth;
        }

    } else {
        inSampleSize = Math.min((float) width / reqWidth, (float) height / reqHeight);
    }
    return inSampleSize;
}

From source file:Main.java

/**
 * NOTE: Arithmetic operations in primitive types may lead to arithmetic overflow. To retain
 * precision, BigDecimal objects are used.
 *
 * @param rgb/* w  w w.  ja  v a  2 s  .c  o  m*/
 * @return
 */
public static int[] convertRGB_8_8_8_To_HSB_32_32_32(int[] rgb) {
    int[] hsb = new int[3];
    int maxChroma = Math.max(Math.max(rgb[0], rgb[1]), rgb[2]);
    int minChroma = Math.min(Math.min(rgb[0], rgb[1]), rgb[2]);
    int diff = maxChroma - minChroma;

    // Hue
    BigDecimal hue;
    if (diff == 0) {
        hue = BigDecimal.ZERO;
    } else if (maxChroma == rgb[0]) {
        float tmp = (rgb[1] - rgb[2]) / (float) diff;
        if (tmp < 0) {
            tmp += 6 * Math.ceil(-tmp / 6.0);
        } else {
            tmp -= 6 * Math.floor(tmp / 6.0);
        }
        hue = BigDecimal.valueOf(tmp);
    } else if (maxChroma == rgb[1]) {
        hue = BigDecimal.valueOf((rgb[2] - rgb[0]) / (float) diff + 2);
    } else {
        hue = BigDecimal.valueOf((rgb[0] - rgb[1]) / (float) diff + 4);
    }
    // [0, 360] -> [0, 0xffffffff]
    hue = hue.multiply(BigDecimal.valueOf(0xffffffffL));
    hue = hue.divide(BigDecimal.valueOf(6), RoundingMode.FLOOR);
    hsb[0] = ByteBuffer.allocate(8).putLong(hue.longValue()).getInt(4);

    // Saturation
    if (maxChroma == 0) {
        hsb[1] = 0;
    } else {
        // [0, 1] -> [0, 0xffffffff]
        BigDecimal sat = BigDecimal.valueOf(diff);
        sat = sat.multiply(BigDecimal.valueOf(0xffffffffL));
        sat = sat.divide(BigDecimal.valueOf(maxChroma), RoundingMode.FLOOR);
        hsb[1] = ByteBuffer.allocate(8).putLong(sat.longValue()).getInt(4);
    }

    // Brightness
    // [0, 255] -> [0, 0xffffffff]
    BigDecimal brightness = BigDecimal.valueOf(maxChroma);
    brightness = brightness.multiply(BigDecimal.valueOf(0xffffffffL));
    brightness = brightness.divide(BigDecimal.valueOf(0xffL), RoundingMode.FLOOR);
    hsb[2] = ByteBuffer.allocate(8).putLong(brightness.longValue()).getInt(4);

    return hsb;
}

From source file:Main.java

/**
 * Creates an animation to fade the dialog opacity from 0 to 1.
 */// ww  w .  j  a  va  2s  .  co  m
public static void fadeIn(final JDialog dialog) {
    final Timer timer = new Timer(10, null);
    timer.setRepeats(true);
    timer.addActionListener(new ActionListener() {
        private float opacity = 0;

        @Override
        public void actionPerformed(ActionEvent e) {
            opacity += 0.15f;
            dialog.setOpacity(Math.min(opacity, 1));
            if (opacity >= 1)
                timer.stop();
        }
    });

    dialog.setOpacity(0);
    timer.start();
    dialog.setVisible(true);
}

From source file:Main.java

/**
 * Clamp a value to be within the provided range.
 * @param value the value to clamp/*from   w  ww  .  j  ava2  s.  c o  m*/
 * @param low the low end of the range
 * @param high the high end of the range
 * @return the clamped value
 */
public static double clamp(double value, double low, double high) {
    return Math.min(Math.max(value, low), high);
}

From source file:Main.java

/**
 * Get a rectangle for the given 4 points (x0,y0,x1,y1,x2,y2,x3,y3) by finding the min/max 2 points that
 * contains the given 4 points and is a stright rectangle.
 *///from w w w . ja  va2s.  c o  m
static Rect getRectFromPoints(float[] points, int imageWidth, int imageHeight, boolean fixAspectRatio,
        int aspectRatioX, int aspectRatioY) {
    int left = Math.round(Math.max(0, getRectLeft(points)));
    int top = Math.round(Math.max(0, getRectTop(points)));
    int right = Math.round(Math.min(imageWidth, getRectRight(points)));
    int bottom = Math.round(Math.min(imageHeight, getRectBottom(points)));

    Rect rect = new Rect(left, top, right, bottom);
    if (fixAspectRatio) {
        fixRectForAspectRatio(rect, aspectRatioX, aspectRatioY);
    }

    return rect;
}

From source file:Main.java

public static void fadeIn(final Dialog win) {
    if (!win.isUndecorated()) {
        return;/*from  w  w  w. j a v  a2 s .co m*/
    }
    final Timer timer = new Timer(30, null);
    timer.setRepeats(true);
    timer.addActionListener(new ActionListener() {
        private float opacity = 0;

        @Override
        public void actionPerformed(ActionEvent e) {
            opacity += 0.05f;
            win.setOpacity(Math.min(opacity, 1f));
            if (opacity >= 1) {
                timer.stop();
            }
        }
    });
    win.setOpacity(0);
    timer.start();
    win.setVisible(true);
}

From source file:Main.java

public static Bitmap getScaledBitmap(String picturePath, int width, int height) {
    // Get the dimensions of the bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(picturePath, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    // Determine how much to scale down the image
    int scaleFactor;

    if (width > 0 && height > 0) {
        scaleFactor = Math.min(photoW / width, photoH / height);
    } else {//from w  w  w .j av  a2  s.  c  om
        scaleFactor = 1;
    }

    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;

    return BitmapFactory.decodeFile(picturePath, bmOptions);
}

From source file:Main.java

public static Bitmap scaleBitmap(int height, int width, String pathImage) {
    Bitmap desBitmap = null;//from  w w  w  .  jav  a 2s.  com
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(pathImage, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    // Determine how much to scale down the image
    int scaleFactor = Math.min(photoW / width, photoH / height);

    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor << 1;
    bmOptions.inPurgeable = true;

    Bitmap bitmap = BitmapFactory.decodeFile(pathImage, bmOptions);

    Matrix mtx = new Matrix();
    mtx.postRotate(90);
    // Rotating Bitmap
    Bitmap rotatedBMP = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), mtx, true);

    if (rotatedBMP != bitmap) {
        bitmap.recycle();
    }

    return rotatedBMP;
}