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

/**
 * Copies the specified array, truncating or padding with zeros (if
 * necessary) so the copy has the specified length. This method is temporary
 * replace for Arrays.copyOf() until we start to require JDK 1.6.
 * //from  ww w. j  a  va  2 s  .  c o m
 * @param source
 *            the array to be copied
 * @param newLength
 *            the length of the copy to be returned
 * @return a copy of the original array, truncated or padded with zeros to
 *         obtain the specified length
 * @throws NegativeArraySizeException
 *             if <tt>newLength</tt> is negative
 * @throws NullPointerException
 *             if <tt>original</tt> is null
 */
public static byte[] copyOf(byte[] source, int newLength) {
    byte[] result = new byte[newLength];
    System.arraycopy(source, 0, result, 0, Math.min(source.length, newLength));
    return result;
}

From source file:Main.java

public static Bitmap fastblur(Bitmap sentBitmap, int radius) {

    if (sentBitmap == null)
        return null;

    Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);

    if (radius < 1) {
        return (null);
    }//from ww  w  . j  a  va  2s.c  o m

    int w = bitmap.getWidth();
    int h = bitmap.getHeight();

    int[] pix = new int[w * h];
    bitmap.getPixels(pix, 0, w, 0, 0, w, h);

    int wm = w - 1;
    int hm = h - 1;
    int wh = w * h;
    int div = radius + radius + 1;

    int r[] = new int[wh];
    int g[] = new int[wh];
    int b[] = new int[wh];
    int rsum, gsum, bsum, x, y, i, p, yp, yi, yw;
    int vmin[] = new int[Math.max(w, h)];

    int divsum = (div + 1) >> 1;
    divsum *= divsum;
    int dv[] = new int[256 * divsum];
    for (i = 0; i < 256 * divsum; i++) {
        dv[i] = (i / divsum);
    }

    yw = yi = 0;

    int[][] stack = new int[div][3];
    int stackpointer;
    int stackstart;
    int[] sir;
    int rbs;
    int r1 = radius + 1;
    int routsum, goutsum, boutsum;
    int rinsum, ginsum, binsum;

    for (y = 0; y < h; y++) {
        rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
        for (i = -radius; i <= radius; i++) {
            p = pix[yi + Math.min(wm, Math.max(i, 0))];
            sir = stack[i + radius];
            sir[0] = (p & 0xff0000) >> 16;
            sir[1] = (p & 0x00ff00) >> 8;
            sir[2] = (p & 0x0000ff);
            rbs = r1 - Math.abs(i);
            rsum += sir[0] * rbs;
            gsum += sir[1] * rbs;
            bsum += sir[2] * rbs;
            if (i > 0) {
                rinsum += sir[0];
                ginsum += sir[1];
                binsum += sir[2];
            } else {
                routsum += sir[0];
                goutsum += sir[1];
                boutsum += sir[2];
            }
        }
        stackpointer = radius;

        for (x = 0; x < w; x++) {

            r[yi] = dv[rsum];
            g[yi] = dv[gsum];
            b[yi] = dv[bsum];

            rsum -= routsum;
            gsum -= goutsum;
            bsum -= boutsum;

            stackstart = stackpointer - radius + div;
            sir = stack[stackstart % div];

            routsum -= sir[0];
            goutsum -= sir[1];
            boutsum -= sir[2];

            if (y == 0) {
                vmin[x] = Math.min(x + radius + 1, wm);
            }
            p = pix[yw + vmin[x]];

            sir[0] = (p & 0xff0000) >> 16;
            sir[1] = (p & 0x00ff00) >> 8;
            sir[2] = (p & 0x0000ff);

            rinsum += sir[0];
            ginsum += sir[1];
            binsum += sir[2];

            rsum += rinsum;
            gsum += ginsum;
            bsum += binsum;

            stackpointer = (stackpointer + 1) % div;
            sir = stack[(stackpointer) % div];

            routsum += sir[0];
            goutsum += sir[1];
            boutsum += sir[2];

            rinsum -= sir[0];
            ginsum -= sir[1];
            binsum -= sir[2];

            yi++;
        }
        yw += w;
    }
    for (x = 0; x < w; x++) {
        rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
        yp = -radius * w;
        for (i = -radius; i <= radius; i++) {
            yi = Math.max(0, yp) + x;

            sir = stack[i + radius];

            sir[0] = r[yi];
            sir[1] = g[yi];
            sir[2] = b[yi];

            rbs = r1 - Math.abs(i);

            rsum += r[yi] * rbs;
            gsum += g[yi] * rbs;
            bsum += b[yi] * rbs;

            if (i > 0) {
                rinsum += sir[0];
                ginsum += sir[1];
                binsum += sir[2];
            } else {
                routsum += sir[0];
                goutsum += sir[1];
                boutsum += sir[2];
            }

            if (i < hm) {
                yp += w;
            }
        }
        yi = x;
        stackpointer = radius;
        for (y = 0; y < h; y++) {
            // Preserve alpha channel: ( 0xff000000 & pix[yi] )
            pix[yi] = (0xff000000 & pix[yi]) | (dv[rsum] << 16) | (dv[gsum] << 8) | dv[bsum];

            rsum -= routsum;
            gsum -= goutsum;
            bsum -= boutsum;

            stackstart = stackpointer - radius + div;
            sir = stack[stackstart % div];

            routsum -= sir[0];
            goutsum -= sir[1];
            boutsum -= sir[2];

            if (x == 0) {
                vmin[y] = Math.min(y + r1, hm) * w;
            }
            p = x + vmin[y];

            sir[0] = r[p];
            sir[1] = g[p];
            sir[2] = b[p];

            rinsum += sir[0];
            ginsum += sir[1];
            binsum += sir[2];

            rsum += rinsum;
            gsum += ginsum;
            bsum += binsum;

            stackpointer = (stackpointer + 1) % div;
            sir = stack[stackpointer];

            routsum += sir[0];
            goutsum += sir[1];
            boutsum += sir[2];

            rinsum -= sir[0];
            ginsum -= sir[1];
            binsum -= sir[2];

            yi += w;
        }
    }

    bitmap.setPixels(pix, 0, w, 0, 0, w, h);

    return (bitmap);
}

From source file:Main.java

/**
 * <p>Gets the minimum of three <code>float</code> values.</p>
 * /* ww w . ja  v a2  s.  co m*/
 * <p>If any value is <code>NaN</code>, <code>NaN</code> is
 * returned. Infinity is handled.</p>
 *
 * @param a  value 1
 * @param b  value 2
 * @param c  value 3
 * @return  the smallest of the values
 * @see IEEE754rUtils#min(float, float, float) for a version of this method that handles NaN differently
 */
public static float min(float a, float b, float c) {
    return Math.min(Math.min(a, b), c);
}

From source file:Main.java

public static short[] copyOf(short[] original, int newLength) {
    short[] copy = new short[newLength];
    System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
    return copy;/*from  w  ww  . j  av a2  s  .co  m*/
}

From source file:Main.java

/**
 * Call {@code in.read()} repeatedly until either the stream is exhausted or
 * {@code byteCount} bytes have been read.
 *
 * <p>This method reuses the skip buffer but is careful to never use it at
 * the same time that another stream is using it. Otherwise streams that use
 * the caller's buffer for consistency checks like CRC could be clobbered by
 * other threads. A thread-local buffer is also insufficient because some
 * streams may call other streams in their skip() method, also clobbering the
 * buffer./*  www . ja v  a 2 s  . c  o m*/
 */
public static long skipByReading(InputStream in, long byteCount) throws IOException {
    if (byteCount == 0)
        return 0L;

    // acquire the shared skip buffer.
    byte[] buffer = skipBuffer.getAndSet(null);
    if (buffer == null) {
        buffer = new byte[4096];
    }

    long skipped = 0;
    while (skipped < byteCount) {
        int toRead = (int) Math.min(byteCount - skipped, buffer.length);
        int read = in.read(buffer, 0, toRead);
        if (read == -1) {
            break;
        }
        skipped += read;
        if (read < toRead) {
            break;
        }
    }

    // release the shared skip buffer.
    skipBuffer.set(buffer);

    return skipped;
}

From source file:Main.java

/**
 * <p>Gets the minimum of three <code>double</code> values.</p>
 * /*from  ww w  .j  av  a 2s  .co  m*/
 * <p>If any value is <code>NaN</code>, <code>NaN</code> is
 * returned. Infinity is handled.</p>
 * 
 * @param a  value 1
 * @param b  value 2
 * @param c  value 3
 * @return  the smallest of the values
 * @see IEEE754rUtils#min(double, double, double) for a version of this method that handles NaN differently
 */
public static double min(double a, double b, double c) {
    return Math.min(Math.min(a, b), c);
}

From source file:Main.java

/**
 * Returns the contrast ratio between {@code foreground} and {@code background}.
 * {@code background} must be opaque.// www  . j av  a  2s.  c om
 * <p>
 * Formula defined
 * <a href="http://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef">here</a>.
 */
public static double calculateContrast(int foreground, int background) {
    if (Color.alpha(background) != 255) {
        throw new IllegalArgumentException(
                "background can not be translucent: #" + Integer.toHexString(background));
    }
    if (Color.alpha(foreground) < 255) {
        // If the foreground is translucent, composite the foreground over the background
        foreground = compositeColors(foreground, background);
    }

    final double luminance1 = calculateLuminance(foreground) + 0.05;
    final double luminance2 = calculateLuminance(background) + 0.05;

    // Now return the lighter luminance divided by the darker luminance
    return Math.max(luminance1, luminance2) / Math.min(luminance1, luminance2);
}

From source file:net.mindengine.galen.specs.Range.java

public static Range between(double from, double to) {
    return new Range(Math.min(from, to), Math.max(from, to)).withType(RangeType.BETWEEN);
}

From source file:Main.java

public static Bitmap emboss(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Bitmap newBitmap = Bitmap.createBitmap(width, height, Config.RGB_565);

    int pixR = 0;
    int pixG = 0;
    int pixB = 0;

    int pixColor = 0;

    int newR = 0;
    int newG = 0;
    int newB = 0;

    int[] pixels = new int[width * height];
    bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
    int pos = 0;//from w  ww. ja v  a  2s.c  o m
    for (int i = 1, length = height - 1; i < length; i++) {
        for (int k = 1, len = width - 1; k < len; k++) {
            pos = i * width + k;
            pixColor = pixels[pos];

            pixR = Color.red(pixColor);
            pixG = Color.green(pixColor);
            pixB = Color.blue(pixColor);

            pixColor = pixels[pos + 1];
            newR = Color.red(pixColor) - pixR + 127;
            newG = Color.green(pixColor) - pixG + 127;
            newB = Color.blue(pixColor) - pixB + 127;

            newR = Math.min(255, Math.max(0, newR));
            newG = Math.min(255, Math.max(0, newG));
            newB = Math.min(255, Math.max(0, newB));

            pixels[pos] = Color.argb(255, newR, newG, newB);
        }
    }

    newBitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return newBitmap;
}

From source file:net.landora.justintv.JustinTVAPI.java

public static List<JustinArchive> readArchives(String channelName, int offset, int maxNumber) throws Exception {

    String url = String.format("http://api.justin.tv/api/channel/archives/%s.json?offest=%d&limit=%d",
            channelName, offset, Math.min(100, maxNumber));

    InputStream stream = openURL(url);

    ObjectMapper mapper = new ObjectMapper();

    List<JustinArchive> readValue = mapper.readValue(stream, new TypeReference<List<JustinArchive>>() {
    });//from ww w  .j  a v  a 2  s.c  o m

    return readValue;
}