Java RGB Color Convert To convertRgbToByteArray(int[] rgb)

Here you can find the source of convertRgbToByteArray(int[] rgb)

Description

Converts the given int[] RGB array into a byte[] array without preserving the alpa channel (each RGB pixel is in the format 0xAARRGGBB).

License

Open Source License

Parameter

Parameter Description
rgb the original integer array

Return

the corresponding byte array with a length of rgb.length * 3.

Declaration

public static byte[] convertRgbToByteArray(int[] rgb) 

Method Source Code

//package com.java2s;
/*//from  ww w. j ava  2  s . co m
 * Created on Jul 23, 2007 at 10:53:12 AM.
 * 
 * Copyright (c) 2010 Robert Virkus / Enough Software
 *
 * This file is part of J2ME Polish.
 *
 * J2ME Polish is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * J2ME Polish is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with J2ME Polish; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Commercial licenses are also available, please
 * refer to the accompanying LICENSE.txt or visit
 * http://www.j2mepolish.org for details.
 */

public class Main {
    /**
     * Converts the given int[] RGB array into a byte[] array without preserving the alpa channel (each RGB pixel is in the format 0xAARRGGBB).
     * @param rgb the original integer array
     * @return the corresponding byte array with a length of rgb.length * 3.
     */
    public static byte[] convertRgbToByteArray(int[] rgb) {
        return convertRgbToByteArray(rgb, 0, rgb.length);
    }

    /**
     * Converts the given int[] RGB array into a byte[] array without preserving the alpa channel (each RGB pixel is in the format 0xAARRGGBB).
     * @param rgb the original integer array
     * @param offset the start index of the first pixel
     * @param len the number of pixels
     * @return the corresponding byte array with a length of rgb.length * 3.
     */
    public static byte[] convertRgbToByteArray(int[] rgb, int offset, int len) {
        byte[] data = new byte[len * 3];
        int j = 0;
        for (int i = offset; i < offset + len; i++) {
            int v = rgb[i];
            data[j + 0] = (byte) ((v >>> 16) & 0xFF);
            data[j + 1] = (byte) ((v >>> 8) & 0xFF);
            //data[j+2] = (byte)((v >>>  0) & 0xFF);
            data[j + 2] = (byte) (v & 0xFF);
            j += 3;
        }
        return data;
    }
}

Related

  1. convertRgb888To565(int rgb888)
  2. convertRgbaToArgb(int rgba)
  3. convertRGBtoHSL(int r, int g, int b)
  4. convertRGBtoHSV(float[] rgb, float[] hsv)
  5. convertRGBtoYIQ(float[] rgb, float[] yiq)
  6. rgb24FloatArray(float[] array, int rgb24)