Java RGB Color Convert To RGB2YCbCr(int[] rgb, float[][] Y, float[][] Cb, float[][] Cr, int imageWidth, int imageHeight)

Here you can find the source of RGB2YCbCr(int[] rgb, float[][] Y, float[][] Cb, float[][] Cr, int imageWidth, int imageHeight)

Description

RGBY Cb Cr

License

Open Source License

Declaration

public static void RGB2YCbCr(int[] rgb, float[][] Y, float[][] Cb, float[][] Cr, int imageWidth,
            int imageHeight) 

Method Source Code

//package com.java2s;
/**/*  w  w  w .j  a  v a 2s . 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[] RGB2YCbCr(int[] rgb) {
        int red, green, blue, index = 0;
        byte[] ycbcr = new byte[rgb.length * 3];

        for (int i = 0; i < rgb.length; i++) {
            red = ((rgb[i] >> 16) & 0xff);
            green = ((rgb[i] >> 8) & 0xff);
            blue = (rgb[i] & 0xff);
            ycbcr[index++] = (byte) (0.299f * red + 0.587f * green + 0.114f * blue);
            ycbcr[index++] = (byte) (-0.1687f * red - 0.3313f * green + 0.5f * blue + 128.0f);
            ycbcr[index++] = (byte) (0.5f * red - 0.4187f * green - 0.0813f * blue + 128.f);
        }

        return ycbcr;
    }

    public static void RGB2YCbCr(int[] rgb, float[][] Y, float[][] Cb, float[][] Cr, int imageWidth,
            int imageHeight) {
        // TODO: Add down-sampling
        int red, green, blue, index = 0;
        //
        for (int i = 0; i < imageHeight; i++) {
            for (int j = 0; j < imageWidth; j++) {
                red = ((rgb[index] >> 16) & 0xff);
                green = ((rgb[index] >> 8) & 0xff);
                blue = (rgb[index++] & 0xff);
                Y[i][j] = (0.299f * red + 0.587f * green + 0.114f * blue) - 128.0f;
                Cb[i][j] = -0.1687f * red - 0.3313f * green + 0.5f * blue;
                Cr[i][j] = 0.5f * red - 0.4187f * green - 0.0813f * blue;
            }
        }
    }

    public static void RGB2YCbCr(int[][] red, int[][] green, int[][] blue, float[][] Y, float[][] Cb, float[][] Cr,
            int imageWidth, int imageHeight) {
        //
        for (int i = 0; i < imageHeight; i++) {
            for (int j = 0; j < imageWidth; j++) {
                Y[i][j] = 0.299f * red[i][j] + 0.587f * green[i][j] + 0.114f * blue[i][j] - 128.0f;
                Cb[i][j] = -0.1687f * red[i][j] - 0.3313f * green[i][j] + 0.5f * blue[i][j];
                Cr[i][j] = 0.5f * red[i][j] - 0.4187f * green[i][j] - 0.0813f * blue[i][j];
            }
        }
    }
}

Related

  1. rgb2lab(int R, int G, int B)
  2. rgb2luv(double[] rgb, double[] luv)
  3. rgb2xyz(double[] RGB)
  4. rgb2xyz(double[] rgb, double[] xyz)
  5. RGB2YCbCr(int r, int g, int b, boolean useBT601)
  6. rgb2yuv(float r, float g, float b, float[] yuv)
  7. rgb565ToRGB(short pixel, byte[] rgb)
  8. rgb8ToPixel(byte[] nrgb)
  9. rgb8ToRgbRBXG(byte[] rgb)