Java RGB Color Convert To RGB2YCbCr(int r, int g, int b, boolean useBT601)

Here you can find the source of RGB2YCbCr(int r, int g, int b, boolean useBT601)

Description

Convert RGB color info to YCBCr

License

Apache License

Parameter

Parameter Description
r 8 bit red component
g 8 bit green component
b 8 bit blue component

Return

Integer array with luminance (Y), chrominance blue (Cb), chrominance red (Cr) (in this order)

Declaration

public static int[] RGB2YCbCr(int r, int g, int b, boolean useBT601) 

Method Source Code

//package com.java2s;
/*/*from   w  w w .  j  a v  a 2s  .co  m*/
 * Copyright 2012 Volker Oth (0xdeadbeef) / Miklos Juhasz (mjuhasz)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Convert RGB color info to YCBCr
     * @param r 8 bit red component
     * @param g 8 bit green component
     * @param b 8 bit blue component
     * @return Integer array with luminance (Y), chrominance blue (Cb), chrominance red (Cr) (in this order)
     */
    public static int[] RGB2YCbCr(int r, int g, int b, boolean useBT601) {
        int[] yCbCr = new int[3];
        double y, cb, cr;

        if (useBT601) {
            /* BT.601 for RGB 0..255 (PC) -> YCbCr 16..235 */
            y = r * 0.299 * 219 / 255 + g * 0.587 * 219 / 255 + b * 0.114 * 219 / 255;
            cb = -r * 0.168736 * 224 / 255 - g * 0.331264 * 224 / 255 + b * 0.5 * 224 / 255;
            cr = r * 0.5 * 224 / 255 - g * 0.418688 * 224 / 255 - b * 0.081312 * 224 / 255;
        } else {
            /* BT.709 for RGB 0..255 (PC) -> YCbCr 16..235 */
            y = r * 0.2126 * 219 / 255 + g * 0.7152 * 219 / 255 + b * 0.0722 * 219 / 255;
            cb = -r * 0.2126 / 1.8556 * 224 / 255 - g * 0.7152 / 1.8556 * 224 / 255 + b * 0.5 * 224 / 255;
            cr = r * 0.5 * 224 / 255 - g * 0.7152 / 1.5748 * 224 / 255 - b * 0.0722 / 1.5748 * 224 / 255;
        }
        yCbCr[0] = 16 + (int) (y + 0.5);
        yCbCr[1] = 128 + (int) (cb + 0.5);
        yCbCr[2] = 128 + (int) (cr + 0.5);
        for (int i = 0; i < 3; i++) {
            if (yCbCr[i] < 16) {
                yCbCr[i] = 16;
            } else {
                if (i == 0) {
                    if (yCbCr[i] > 235)
                        yCbCr[i] = 235;
                } else {
                    if (yCbCr[i] > 240)
                        yCbCr[i] = 240;
                }
            }
        }
        return yCbCr;
    }
}

Related

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