Reputation: 857
I've the following C code to make the almost white pixels transparent, but i've some difficult to understand why the value is multiplied by 255. Can you tell me why please?
The pointer fish points to a bitmap in the memory; this bitmap has almost white pixels; instead fishp is a pointer which points to the part of the memory where i'd like to store the new bitmap.
int x, y, c;
int pink;
float hue, sat, val;
for (x = 0; x < fish - > w; x++)
for (y = 0; y < fish - > h; y++) {
c = getpixel(fish, x, y);
rgb_to_hsv(getr(c), getg(c), getb(c), &
hue, & sat, & val);
val = val * 255;
if (val >= 240) c = pink;
putpixel(fishp, x, y, c);
}
get_palette(pal);
save_bitmap("fishp.bmp", fishp, pal);
Thank you for your time.
Upvotes: 1
Views: 144
Reputation: 154280
why the value is multiplied by 255?
In the HSV model, the V
part, value has a numeric value in the [0.0 ... 1.0] range.
As many RGB models use primaries with integer values in the [0...255] range, scaling by 255 simply brings the value into a likewise range before the compare with 240.
Alternatively code could have been as below for a similar (and potentially faster) compare.
rgb_to_hsv(getr(c), getg(c), getb(c), &hue, & sat, & val);
// val = val * 255;
// if (val >= 240) c = pink;
if (val >= 240/255.0f) c = pink;
The key is that the threshold level of 240
implies an RGB model with 256 different primary levels and so the compare is done in those units.
Note: pink
is never assigned a value, so code needs work else if (val >= 240) c = pink;
is an issue.
Upvotes: 1