Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bit Shift and Bitwise operations to encode RGB values

I would like to encode the RGB color to a single integer value.

Let's say the algorithm for encoding is like this:

int code = (blue * 256 * 256) + (green * 256) + red

How can encode/decode RGB components to/from the code using bit shift and/or bitwise operators?

like image 840
Jan Krakora Avatar asked Oct 09 '13 16:10

Jan Krakora


People also ask

What is bit shifting operations?

Bit shifting is an operation done on all the bits of a binary value in which they are moved by a determined number of places to either the left or right. Bit shifting is used when the operand is being used as a series of bits rather than as a whole.

What does bit shifting by 1 do?

Bitshifting shifts the binary representation of each pixel to the left or to the right by a pre-defined number of positions. Shifting a binary number by one bit is equivalent to multiplying (when shifting to the left) or dividing (when shifting to the right) the number by 2.

What does shifting bits to the right do?

When shifting right with a logical right shift, the least-significant bit is lost and a 0 is inserted on the other end. For positive numbers, a single logical right shift divides a number by 2, throwing out any remainders.


3 Answers

    int blueMask = 0xFF0000, greenMask = 0xFF00, redMask = 0xFF;
    int r = 12, g = 13, b = 14;
    int bgrValue = (b << 16) + (g << 8) + r;
    System.out.println("blue:" + ((bgrValue & blueMask) >> 16));
    System.out.println("red:" + ((bgrValue & redMask)));
    System.out.println("green:" + ((bgrValue & greenMask) >> 8));
like image 50
Dev Blanked Avatar answered Sep 17 '22 09:09

Dev Blanked


If you simply want to/from RGB conversion and don't care how I would suggest using java.awt.Color

int r = 255; //red
int g = 255; //green
int b = 255; //blue
int a = 255; //alpha
Color c = new Color(r,g,b,a);

The using the getRGB method and getRed, getBlue, getGreen methods

int RGB = c.getRGB();
int red = c.getRed();
int blue = c.getBlue();
int green = c.getGreen();

Alternatively you can construct a color object using the Color(r,g,b) constructor, it will have default 255 alpha.

With bit operations (ARGB, 32 bit colorspace). Constructing the RGB color:

int alpha = 255;    
int red = 128;
int green = 128;
int blue = 128;
int RGB = (alpha << 24);
RGB = RGB | (red << 16);
RGB = RGB | (green << 8);
RGB = RGB | (blue);

System.out.println(Integer.toBinaryString(RGB));

Out 11111111100000001000000010000000

Decoding is done as in the link in the comment.

like image 42
arynaq Avatar answered Sep 20 '22 09:09

arynaq


Here is a mock program that I've done up that might assist you. I approached the conversion much like Dev Blanked based off an old program I did, but he answered while I was putting the program together. Since I did the work anyways, figured I'd share in case it helped in any way.

import java.util.Scanner;
import java.math.*;

public class RGB{

public static void main(String[]args){
    Scanner scan = new Scanner(System.in);
    int code; //Code for the color
    int red, green, blue; //Individual colors
    int rMask = 0xFF0000, gMask = 0xFF00, bMask = 0xFF; //Masks for the colors

    //Take input
    System.out.println("Please enter the red color. Range [0, 255] only please.");
    red = scan.nextInt();
    System.out.println("Please enter the green color. Range [0, 255] only please.");
    green = scan.nextInt();
    System.out.println("Please enter the blue color. Range [0, 255] only please.");
    blue = scan.nextInt();

    //Generate code based on Behnil's way.
    code = 0;
    code += (int) (red * Math.pow(2, 16));
    code += (int) (green * Math.pow(2, 8));
    code += (int) (blue * Math.pow(2,0));
    System.out.println("The code is " + code + ".");

    //Clear values
    red = 0;
    green = 0;
    blue = 0;

    //Obtain values.
    red = (code & rMask) >> 16;
    green = (code & gMask) >> 8;
    blue = (code & bMask);

    System.out.println("Your red value is: " + red);
    System.out.println("Your green value is: " + green);
    System.out.println("Your blue value is: " + blue);      
}

}
like image 25
Fiki Avatar answered Sep 20 '22 09:09

Fiki