Page 1 of 1

## Reducin g 32bit image colors by manipulating alpha component Rate Topic: 1 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=341917&amp;s=1c30b17be9ad0f5fde5201b9b4a4f15e&md5check=' + ipb.vars['secure_hash'], cur_rating: 5, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ricardosms

• D.I.C Regular

Reputation: 74
• Posts: 301
• Joined: 02-April 10

Posted 09 March 2014 - 12:06 PM

Color Reduction

Hey everybody.

I was playing with image transparency and by accident I found that by changing the alpha value on a 32 bit image to a lower value than 255, writing it back to a bitmap in memory and then converting the image back to full opacity (A=255), the resulting image would have a reduced amount of colors.

The amount of colors depends on the value of alpha that you gave it on the first conversion step. As you increase alpha from zero, the amount of colors will increase.

Increased to about 25, I can't visually see a difference with the original, but when manipulating it further, it will show more and more colors as you increase alpha. If you use alpha=0, your resulting image will be all black (Zero/One Color?).

The following is one of my sample images:

I processed this image increasing the alpha value and counted the colors.

I am showing a table and a chart here, where I list the value of alpha I used and the resulting color count.

The chart graph shows a curve (I would guess X^3) for a range and then it changes up to almost a straight line, but I couldn't determine the formula with coeficients. I think it straightens because there are no more colors on the sample. The samples I used are are limited to small images with lots colors, but not all the possibilities. In order to accomodate 16 million colors (RGB=256*256*256), you would have to have a bitmap of at least 16 MB. Besides, my color counting algorithm becomes extremely slow when I increase the size of the image.

My guess is that the approximate formula would be something like this:

Colors = A*X^n + B

A: could be 3 or maybe 6
n: I believe is 3
B: could be 2 ( for black and white).

The following image shows two edge detection results for the initial image.

For the one on the left:

A. I converted the image changing Alpha to 20.
B. Converted resulting image changing Alpha to 255.
C. Used one of my routines to get edge detection

For the one on the right I only used the edge detection routine. All the setting were the same. Please note that when there is a gradient there isn't an edge.

Here is another example:

[/inline]

[/inline]

You could see how the resulting images change with the values of Alpha.

I used lockbits to set the alpha value. The use of a color matrix didn't give me a good result. By dividing colors on blocks and truncating the modulus, neither gives me a good quality.

```            For y = 0 To img.Height * img.Width * 4 - 4 Step 4
'           This is the array of the color component of each pixel
rgbval(y + A) = 255 'or any value, except 0
rgbval(y + R) = rgbval(y + R)
rgbval(y + G) = rgbval(y + G)
rgbval(y + B)/>/> = rgbval(y + B)/>/>
Next y

```

I hope this would be interesting to some of you, and maybe you would experiment for yourselves.