Sunday, March 09, 2008

grouping the RGB colors

As I mentioned in the image handling utility, ChitraDeepam, I had to club colors together. The need arised due to the vast color range listed out of the given image; whereas the user was expecting to see few basic colors strikingly visible to human eye.

I was thinking about comparing the individual pigment of a particular color, (pigments => red, green, blue), and deciding which band of basic color it, (the given color), will fit into. However this was a complicated logic as we will end up into infinite comparison and may end up where we started.

A simpler logic occurred to me of masking the LSBs (least significant bits). User can have control over how many LSBs to mask. If I make those bits as zero, (for 3 of the pigment in each pixel color) then I am reducing the set of colors. This masking results in distortion of colors. As we are loosing few bits of data, we move from accurate image colors towards inaccurate colors.

Here is the code:

public Color trimColor(Color color, int accuracy)
{

// find the mask to be applied on color
int trimBar = (0xff << accuracy) & 0xff;


int red = color.getRed() & trimBar;
int green = color.getGreen() & trimBar;
int blue = color.getBlue() & trimBar;


return new Color(red, green, blue);
}


I am looking out for reducing the distortion and still achieving the color grouping. A friend had suggested to check with image processing utilities and libs. Seems like there are lots of possibilities for these minor features to have exists for ages. Let me go through.


Update: check the refined approach.

No comments: