brightness and contrast

Blitz3D Forums/Blitz3D Beginners Area/brightness and contrast

orion one(Posted 2007) [#1]
hi guys, i am an old newbie in blitz3D in that i got my hands in it a few years ago and had to sto pusing it so i have to start from scratch again... i need to draw a picture from bytes on a disk file, and then adjust brightness and/or contrast; how can i do that just like all the free image browsers do? i get flickering in all my attemps to do it smoothly. please help and thanks in advance


chwaga(Posted 2007) [#2]
try something like this:



and adjust the RGB's to whatever effect you want :)


jfk EO-11110(Posted 2007) [#3]
Not sure if rounding as in chagwas sample will do the job. It's however a useful example on how to handle RGB colors.

Brightness can be altered easily. Simply extract r,g and b rom rgb, then add or subtract a certain value to/from each channel and make sure it will be remain within 0 to 255. Then reassemble rgb.

Assuming you have taken a 24bit color value named rgb from somewhere:
brighten=30
r=(rgb and $FF0000) shr 16
g=(rgb and $FF00) shr 8
b=rgb and $FF

r=r+brighten
if r<0 then r=0
if r>255 then r=255

; do the same with g and b here

rgb2=(r shl 16) or (g shl 8) or b

Now rgb2 is the brightened rgb.

Contrast is a little harder. you may do it this way:

first extract r,g and b as before. Then subtract 128 from each channel, then mutiply it by a contrast alternation parameter, then add 128 again, then reassemble rgb. Something like this:
cp#=1.2
r=(rgb and $FF0000) shr 16
g=(rgb and $FF00) shr 8
b=rgb and $FF

r2#=r-128
g2#=g-128
b2#=b-128

r2=r2*cp
g2=g2*cp
b2=b2*cp

r=r2+128
g=g2+128
b=b2+128

if r<0 then r=0
if r>255 then r=255
; do the same with g and b here

rgb2=(r shl 16) or (g shl 8) or b

Now rgb2 is rgb with altered contrast.

Instead of using 128 as the "center" for contrast changes you may also use an other number, probably the images average brightness.


I'm pretty sure there are other methods that are better, more elegant and/or correct. Browse the code archives! (Graphics)


orion one(Posted 2007) [#4]
thank you guys for your nice fast replys. however i have to admit my post was not so clear; i deal with CT-SCAN images coded in 256 shades of grey only. i try your codes tonight and will browse the code archives again asap. thanks again


jfk EO-11110(Posted 2007) [#5]
256 shades of grey is most likely saved with 8 Bits per pixel. So you can use my sample and simply handle only one channel (eg. blue may now be grey)

Depending on the format you want to save the image you probably have to convert it from 8 bit greyscale to 24 bit grayscale, that's easy, simply say red=grey, green=grey, blue =grey, then make a 24bit color value out of them (the last line of my 2nd sample). After drawing the pixels to an imagebuffer you may then use SaveBuffer to save the new image as a BMP.


b32(Posted 2007) [#6]
I just saw the freeimage decls in the archive. It seems to have functions for adjusting brightness/contrast as well:
http://www.blitzbasic.com/codearcs/codearcs.php?code=1732