brightness and contrast

Blitz3D Forums/Blitz3D Beginners Area/brightness and contrast

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

try something like this:

Graphics 1280, 1024, 32, 1
.redo
Locate 0, 0
Cls 
Global imageid$ =Input$("Image name and extension (default extension is jpeg) : ")
If imageid$ = "" Then Goto redo


Global bias$    =Input$("To what nearest number should the rgb's be rounded to? ")
Global bw$      =Input$("Should the output image be black and white? (y/n) ")
If bias=256 Then bias=255

If Instr (imageid$, ".", 1) Then
Else
imageid$=imageid$+".jpg"
EndIf 

Global image=LoadImage ("" + imageid$)

Global save=CreateImage(ImageWidth(image), ImageHeight(image))

DrawImage image, 0, 0
Locate 600, 1000
LockBuffer ImageBuffer(image)
LockBuffer ImageBuffer(save)
;--------------------------------------------------------------------------
For y=0 To ImageHeight (image) - 1
	For x=0 To ImageWidth (image) - 1
	
		pix% = Abs(ReadPixelFast( x,y, ImageBuffer(image) ))
		;pix%=Abs(pix%)
		r=(pix% Shr 16) And 255
		g=(pix% Shr  8) And 255
		b=(pix% Shr  0) And 255


		If bw$="y" Then 


			av = (r + g + b) / 3
			
			r=(round( av, bias))
			g=(round( av, bias))
			b=(round( av, bias))

		Else

			r=(round( r, bias))
			g=(round( g, bias))
			b=(round( b, bias))

		EndIf 
		
		

		pix% = (r Shl 16) + (g Shl 8) + b

		WritePixelFast x, y, pix%, ImageBuffer(save)

		
 
	Next
Next 
;---------------------------------------------------------------------------
UnlockBuffer ImageBuffer(image)
UnlockBuffer ImageBuffer(save)
If ImageHeight(image)*2 > GraphicsHeight() Then 
DrawImage save,  ImageWidth(image), 0
Else
DrawImage save, 0, ImageHeight(image), 0
EndIf 



savename$=Input$("New image filename? (will save as bitmap, supply no name to not save) ") 

If savename$="" Then 
Else
SaveImage (save, savename$+".bmp")
EndIf
FlushKeys()
Repeat 

Text 800,  980, "Press Escape to Exit, Space to reset."
If KeyHit(57) Then Goto redo

Until KeyHit(1)
End

Function Round( Number , N )

   If ( Number Mod N ) >= ( N *.5 )
        Number  = ( Ceil( Number / N ) + 1 ) * N
   Else
        Number = Floor( Number / N ) * N
   EndIf

   Return Number

End Function


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

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)

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

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.

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