Java RGB Color Convert To rgb2grayscale(int[] rgb, int imageWidth, int imageHeight)

Here you can find the source of rgb2grayscale(int[] rgb, int imageWidth, int imageHeight)

Description

rgbgrayscale

License

Open Source License

Declaration

public static float[][] rgb2grayscale(int[] rgb, int imageWidth, int imageHeight) 

Method Source Code

//package com.java2s;
/**/*from   www.j a  va  2  s.co m*/
 * Copyright (c) 2014-2015 by Wen Yu.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Any modifications to this file must keep this entire header intact.
 *
 * Change History - most recent changes go on top of previous changes
 *
 * IMGUtils.java
 *
 * Who   Date       Description
 * ====  =========  ==============================================================
 * WY    03Feb2015  Added createThumbnail() to create a thumbnail from an image
 * WY    27Jan2015  Added createThumbnail8BIM() to wrap a BufferedImage to _8BIM
 * WY    22Jan2015  Factored out guessImageType(byte[])
 * WY    24Dec2014  Rename CMYK2RGB() to iccp2rgbRaster()
 * WY    17Dec2014  Bug fix for rgb2bilevelDither() to bypass transparent pixels
 * WY    03Dec2014  Bug fix for getRGB() to fall back to BufferedImage.getRGB()
 * WY    26Nov2014  Changed rgb2bilevel() to take into account transparency
 * WY    03Nov2014  Added CMYK2RGB() to convert CMYK raster to RGB raster
 * WY    22Sep2014  Added guessImageType() to auto detect image type
 * WY    13Aug2014  Added RGB2YCCK_Inverted() to support YCCK JPEG
 * WY    05May2014  Added getRGB() and getRGB2() to replace BufferedImage.getRGB()
 */

public class Main {
    public static byte[] rgb2grayscale(int[] rgb) {
        byte[] grayscale = new byte[rgb.length];

        for (int i = 0; i < rgb.length; i++) {
            grayscale[i] = (byte) (((rgb[i] >> 16) & 0xff) * 0.2126 + ((rgb[i] >> 8) & 0xff) * 0.7152
                    + (rgb[i] & 0xff) * 0.0722);
        }

        return grayscale;
    }

    public static float[][] rgb2grayscale(int[] rgb, int imageWidth, int imageHeight) {
        float[][] grayscale = new float[imageHeight][imageWidth];

        for (int i = 0, index = 0; i < imageHeight; i++) {
            for (int j = 0; j < imageWidth; j++, index++) {
                grayscale[i][j] = (float) (((rgb[index] >> 16) & 0xff) * 0.2126
                        + ((rgb[index] >> 8) & 0xff) * 0.7152 + (rgb[index] & 0xff) * 0.0722 - 128.0);
            }
        }

        return grayscale;
    }
}

Related

  1. convertRGBtoYIQ(float[] rgb, float[] yiq)
  2. rgb24FloatArray(float[] array, int rgb24)
  3. rgb2arr(final int color, final int[] argbarr)
  4. rgb2bilevel(int[] rgb)
  5. rgb2gray(int r, int g, int b)
  6. rgb2grayscaleA(int[] rgb)
  7. rgb2Hex(int rgb)
  8. rgb2hex(int[] rgb)
  9. rgb2hsl(int colour)