Example usage for java.lang Math max

List of usage examples for java.lang Math max

Introduction

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

Prototype

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

Source Link

Document

Returns the greater of two double values.

Usage

From source file:Main.java

public static BufferedImage int2image(int[][] scene) {
    int maxValue = -1;
    int minValue = Integer.MAX_VALUE;
    for (int y = 0; y < scene.length; y++) {
        for (int x = 0; x < scene[y].length; x++) {
            maxValue = Math.max(maxValue, scene[y][x]);
            minValue = Math.min(minValue, scene[y][x]);
        }//from  w  w  w  .j a  v  a 2s .co  m
    }

    if (maxValue == minValue)
        maxValue = minValue + 1;
    final double scale = 255.0 / (maxValue - minValue);

    BufferedImage image = new BufferedImage(scene[0].length, scene.length, BufferedImage.TYPE_INT_RGB);
    for (int y = 0; y < scene.length; y++) {
        for (int x = 0; x < scene[y].length; x++) {
            final int c = (int) (scale * (scene[y][x] - minValue));
            image.setRGB(x, y, c << 16 | c << 8 | c);
        }
    }

    return image;
}

From source file:Main.java

public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    int h = options.outHeight;
    int w = options.outWidth;
    int inSampleSize = 0;
    if (h > reqHeight || w > reqWidth) {
        float ratioW = (float) w / reqWidth;
        float ratioH = (float) h / reqHeight;
        inSampleSize = (int) Math.min(ratioH, ratioW);
    }//  w  w  w.  j av a 2s  .  c o m
    inSampleSize = Math.max(1, inSampleSize);
    return inSampleSize;
}

From source file:Main.java

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

    if (height > reqHeight || width > reqWidth) {
        //https://github.com/square/picasso/blob/master/picasso/src/main/java/com/squareup/picasso/RequestHandler.java#L161
        if (reqHeight == 0) {
            inSampleSize = (int) Math.floor((float) width / (float) reqWidth);
        } else if (reqWidth == 0) {
            inSampleSize = (int) Math.floor((float) height / (float) reqHeight);
        } else {//from  w w w . jav a2  s . c  om
            int heightRatio = (int) Math.floor((float) height / (float) reqHeight);
            int widthRatio = (int) Math.floor((float) width / (float) reqWidth);
            inSampleSize = Math.max(heightRatio, widthRatio);
        }
    }
    return inSampleSize;
}

From source file:Main.java

/**
 * Create a color integer value with specified alpha.
 * This may be useful to change alpha value of background color.
 *
 * @param alpha     alpha value from 0.0f to 1.0f.
 * @param baseColor base color. alpha value will be ignored.
 * @return a color with alpha made from base color
 *//*  w  w w  .  ja va  2 s. c om*/
public static int getColorWithAlpha(float alpha, int baseColor) {
    int a = Math.min(255, Math.max(0, (int) (alpha * 255))) << 24;
    int rgb = 0x00ffffff & baseColor;
    return a + rgb;
}

From source file:Main.java

public static int[] getExtremeCoords(List<int[]> positions, boolean min) {
    if (positions.size() > 0) {
        int[] selectedPos = positions.get(0).clone();

        for (int n = 1; n < positions.size(); n++) {
            int[] position = positions.get(n);

            for (int i = 0; i < selectedPos.length; i++) {
                selectedPos[i] = min ? Math.min(position[i], selectedPos[i])
                        : Math.max(position[i], selectedPos[i]);
            }//ww w. j a  v a  2s  . co m
        }

        return selectedPos;
    }

    return null;
}

From source file:Main.java

/**
 * Returns the amount of storage currently available in the cache.
 *
 * @param dir The cache directory path name.
 * @return The amount of storage available in bytes.
 *///from   w ww. java  2 s.c  o  m
public static long calculateDiskCacheSize(File dir) {
    long size = MIN_DISK_CACHE_SIZE;

    try {
        StatFs statFs = new StatFs(dir.getAbsolutePath());
        long available = statFs.getBlockCountLong() * statFs.getBlockSizeLong();
        // Target 2% of the total space.
        size = available * MAX_DISK_CACHE_AS_PERCENT / 100;
    } catch (IllegalArgumentException ignored) {
    }

    // Bound inside min/max size for disk cache.
    return Math.max(Math.min(size, MAX_DISK_CACHE_SIZE), MIN_DISK_CACHE_SIZE);
}

From source file:Main.java

/**
 * Calculates how many months are in given period. If one of the dates only partially covers the month, it still counts as full month.
 *
 * @param start Start of the period.//w  w  w  .j a  v  a 2s  .c o m
 * @param end   End of the period.
 * @return Number of days in given period. If {@code end < start}, returns -1.
 */
public static int getMonthCountInPeriod(long start, long end) {
    if (end < start)
        return -1;

    final Calendar cal = Calendar.getInstance();
    final int monthCountInYear = cal.getMaximum(Calendar.MONTH);

    cal.setTimeInMillis(start);
    final int startYear = cal.get(Calendar.YEAR);
    final int startMonth = cal.get(Calendar.MONTH) + 1;

    cal.setTimeInMillis(end);
    final int endYear = cal.get(Calendar.YEAR);
    final int endMonth = cal.get(Calendar.MONTH) + 1;

    int monthsCount;

    if (startYear != endYear) {
        monthsCount = monthCountInYear * Math.max(0, endYear - startYear - 1);
        monthsCount += monthCountInYear - startMonth + 1;
        monthsCount += endMonth;
    } else {
        monthsCount = endMonth - startMonth + 1;
    }

    return monthsCount;
}

From source file:Main.java

public static Bitmap resizeDownAndCropCenter(Bitmap bitmap, int size, boolean recycle) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    int minSide = Math.min(w, h);
    if (w == h && minSide <= size)
        return bitmap;
    size = Math.min(size, minSide);

    float scale = Math.max((float) size / bitmap.getWidth(), (float) size / bitmap.getHeight());
    Bitmap target = Bitmap.createBitmap(size, size, getConfig(bitmap));
    int width = Math.round(scale * bitmap.getWidth());
    int height = Math.round(scale * bitmap.getHeight());
    Canvas canvas = new Canvas(target);
    canvas.translate((size - width) / 2f, (size - height) / 2f);
    canvas.scale(scale, scale);//  ww w.  j av  a2 s .c  o  m
    Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    if (recycle)
        bitmap.recycle();
    return target;
}

From source file:Main.java

public static Bitmap getThumbnail(Context context, String url) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//from w ww.ja  v  a2  s .c o  m
    BitmapFactory.decodeFile(url, options);

    float w = options.outWidth;
    float h = options.outHeight;
    float max = Math.max(w, h);

    BitmapFactory.Options bounds = new BitmapFactory.Options();
    bounds.inPurgeable = true;
    bounds.inInputShareable = true;
    bounds.inJustDecodeBounds = false;

    float size = max / 800;
    if (size > 1) {
        bounds.inSampleSize = (int) size;
    }
    return BitmapFactory.decodeFile(url, bounds);
}

From source file:Main.java

/**
 * decode the image bitmap according to specified size
 * //w w  w .  j  av  a2  s . c o  m
 * @param f
 * @param maxSize
 * @return
 */
public static Bitmap decodeFile(File f, int maxSize) {
    Bitmap b = null;
    try {
        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;

        FileInputStream fis = new FileInputStream(f);
        BitmapFactory.decodeStream(fis, null, o);
        fis.close();

        int scale = 1;
        if (o.outHeight > maxSize || o.outWidth > maxSize) {
            scale = (int) Math.pow(2, (int) Math
                    .round(Math.log(maxSize / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
        }

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        fis = new FileInputStream(f);
        b = BitmapFactory.decodeStream(fis, null, o2);
        fis.close();
    } catch (IOException e) {
    }
    return b;
}