Night - Day Filter

BlitzPlus Forums/BlitzPlus Programming/Night - Day Filter

FBEpyon(Posted 2004) [#1]
Hello,

Im trying to come up with a way to make a filter that changes the sprite darkness like so that I can have the effect of night to day in my game im making.
It would nice if this function or command was realtime..

Does anyone have any suggestions??

Function FB_DarkenImage ( image, d )

	For ix = 0 To ImageWidth( image ) - 1
		For iy = 0 To ImageHeight( image ) - 1
			Local rbgc = ReadPixel( ix, iy, ImageBuffer(image) )
			If rbgc = $FFFF00FF + d
				WritePixel ix,iy,($0),ImageBuffer(image)		
			Else
				WritePixel ix,iy,( rbgc + d ), ImageBuffer(image)
			EndIf
		Next
	Next
	Return image

End Function 


all this is doing is change the tint or the blue :( I can't seem to make a function that changes the pixels brightness

also if there was a way to keep the mask :( that would be nice :(


MSW(Posted 2004) [#2]
Just check if the color matches the mask color...if so don't change it.

As for brightness and other specific color effects you will need to pull the red, greeen, and blue componets out of the rbgc variable ...something like this (if useing 32-bit color):

Alpha% = )rgbc AND $FF000000) SHR 24
Red% = (rgbc AND $00FF0000) SHR 16
Green% = (rgbc AND $0000FF00) SHR 8
Blue% = rgbc AND $000000FF

then if you want to reduce the brightness by say 25%

Red = Red * .75
Green = Green * .75
Blue = Blue * .75

Then just stick them all back together...course it can help to make sure this new color isn't the same as the mask color...so if the mask color is black (red = 0, green = 0, blue = 0) then just doing something like Blue = Blue * .75 + 1 would work without the need for a IF/THEN check...

anyway to load them all back into rgbc:

rgbc = (Alpha SHL 24) + (Red SHL 16) + (Green SHL 8) + Blue

If that is to complicated, and you really don't want to mess with bitshifting or need this to run in real time then you can use getcolor (and the getred(), getgreen(), getblue() ) functions along with the basic color setting function and Plot to do this (maybe when the graphics are loading...or as as a utility to precolor the graphics then save them under a different file name to be used for "night time" instead of the default images.


FBEpyon(Posted 2004) [#3]
Okay I don't get how it all works togeather can you rewrite the code to use the Red, Blue, Green thing
NeverMind thanks it worked :P

is there way to make 0 full bright and 1 full darkness in my code..??

Function FB_DarkenImage ( image, d#=1 )

	For ix = 0 To ImageWidth( image ) - 1
		For iy = 0 To ImageHeight( image ) - 1
			Local rbgc = ReadPixel( ix, iy, ImageBuffer(image) )
			Alpha% = (rbgc And $FF000000) Shr 24 
			Red% = (rbgc And $00FF0000) Shr 16 
			Green% = (rbgc And $0000FF00) Shr 8 
			Blue% = rbgc And $000000FF
			Red = Red * d#
			Green = Green * d#
			Blue = Blue * d#
			rbgc = (Alpha Shl 24) + (Red Shl 16) + (Green Shl 8) + Blue 
			If rbgc = $FFFF00FF
				WritePixel ix,iy,($0),ImageBuffer(image)		
			Else
				WritePixel ix,iy,(rbgc), ImageBuffer(image)
			EndIf
		Next
	Next
	
	Return image

End Function 


[edit]
How would I also lighten that image using this code basicly im trying to make a code depending on % the images brightness will be changed :) can you see what needs to be added


MSW(Posted 2004) [#4]
Okay...


Function FB_DarkenImage ( image, d%=100 )

     ;convert number into a fraction for multiplication
     percent# = d / 100

	For ix = 0 To ImageWidth( image ) - 1
		For iy = 0 To ImageHeight( image ) - 1
			
                Local rbgc = ReadPixel( ix, iy, ImageBuffer(image) )
		
                ;check if the pixel is a mask color...if so exit to the next pixel...else proceed
                If rbgc = $FFFF00FF then
                        exit
                else
 
                       	Alpha% = (rbgc And $FF000000) Shr 24 
	        	Red% = (rbgc And $00FF0000) Shr 16 
		        Green% = (rbgc And $0000FF00) Shr 8 
		        Blue% = rbgc And $000000FF
			
                        
                        ;modify the color values, MOD ing them by 256 inorder to keep them in the range of 0 to 255
                        Red = (Red * percent) MOD 256
			Green = (Green * percent) MOD 256
			Blue = (Blue * percent) MOD 256
			     
                             ;make sure this modified color isn't the same as the mask color, if so change it else it will be masked
                             If red = 255 and blue = 255 and green = 0 then green = 1

                        
                        ;rebuild the rbgc value and writepixel it to the image
                        rbgc = (Alpha Shl 24) + (Red Shl 16) + (Green Shl 8) + Blue 
			
                      	WritePixel ix,iy,($0),ImageBuffer(image)		
		


                EndIf
		Next
	Next
	
	Return image

End Function 


the D# value works as an percentage in this..if you want the image to be 75% as brite you just call the function like so:

FB_Darkenimage (myimage, 75)

if you want to increase the brightness by 30% then call it like:

FB_Darkenimage (myimage, 130)

Note however then there is no constraining of the values...erm...that is to say if the pixel is fully white (red=255, green=255, blue=255) and you try to increase the brightness it can make the pixel come out very dark grey...if you don't want that to happen then try this instead:



Function coloradjustImage ( image, R% = 100, G% = 100, B% = 100 )

     ;convert number into a fraction for multiplication
     percentred# = R% / 100
     percentgreen# = G% / 100
     percentblue# = B% / 100

	For ix = 0 To ImageWidth( image ) - 1
		For iy = 0 To ImageHeight( image ) - 1
			
                Local rbgc = ReadPixel( ix, iy, ImageBuffer(image) )
		
                ;check if the pixel is a mask color...if so exit to the next pixel...else proceed
                If rbgc = $FFFF00FF then
                        exit
                else
 
                       	Alpha% = (rbgc And $FF000000) Shr 24 
	        	Red% = (rbgc And $00FF0000) Shr 16 
		        Green% = (rbgc And $0000FF00) Shr 8 
		        Blue% = rbgc And $000000FF
			
                        
                        ;modify the color values

                        Red = (Red * percentred)
                            if Red > 255 then Red = 255
                            if Red < 0 then Red = 0 
			
                        Green = (Green * percent)
			    if Green > 255 then Green = 255
                            if Green < 0 then Green = 0
 
                        Blue = (Blue * percent)
			    if Blue > 255 then Blue = 255
                            if Blue < 0 then Blue = 0
 
                             ;make sure this modified color isn't the same as the mask color, if so change it else it will be masked
                             If red = 255 and blue = 255 and green = 0 then green = 1

                        
                        ;rebuild the rbgc value and writepixel it to the image
                        rbgc = (Alpha Shl 24) + (Red Shl 16) + (Green Shl 8) + Blue 
			
                      	WritePixel ix,iy,($0),ImageBuffer(image)		
		


                EndIf
		Next
	Next
	
	Return image

End Function 



works pretty much the same but you can adjust each color seperately...say you wanted to increase the red inorder to use the image on a fire level or somesuch so you would call it like so:

coloradjustimage (myimage, 130,100,95)

which would increase the red by 30% (or set the red to 130%)...the green would stay the same...and the blue is reduced by 5% (or set to 95% of it's current color value)...


MSW(Posted 2004) [#5]
Opps...that writepixel in BOTH the above function examples shoud read:

WritePixel ix, iy, rbgc, ImageBuffer(image)


FBEpyon(Posted 2004) [#6]
hmm that code just made the image completely dark :(

so the d/100 doesn't seem to be right :(


kochOn(Posted 2004) [#7]
Here is a realtime function to draw lighten or darken images( it may works but not tested ) :

function DrawLightenImage(img,x,y,light#,dst=0)
w=ImageWidth()
h=ImageHeight()

if dst=0 then dst=GraphicsBuffer()

LockBuffer(dst)
lockbuffer(imageBuffer(img))
for j=0 to h-1
for i=0 to w-1
p=ReadPixelFast(i,j,ImageBuffer(img))
if p<>$FF000000 then
red=p shr 16 and 255
green=p shr 8 and 255
blue=p and 255
red=float(red)*light
green=float(green)*light
blue=float(blue)*light
p=blue Or (green Shl 8) Or (red Shl 16) Or (255 Shl 24)
WritePixelFast(x+i,y+j,p,dst)
endif
next
next
unlockbuffer(ImageBuffer(img))
UnlockBuffer(dst)
end function


MSW(Posted 2004) [#8]
Okay...corrected some errors...this has been tested, works fine...problem was with the divide by 100...copy and paste :)

Function coloradjustImage ( image, R% = 100, G% = 100, B% = 100 )

     ;convert number into a fraction for multiplication
     percentred# = R% * .01
     percentgreen# = G% * .01
     percentblue# = B% * .01

	For ix = 0 To ImageWidth( image ) - 1
		For iy = 0 To ImageHeight( image ) - 1
			
                rbgc% = ReadPixel( ix, iy, ImageBuffer(image) )
		
                ;check if the pixel is a mask color...if so exit to the next pixel...else proceed
                If rbgc = $FFFF00FF Then
                        Exit
                Else
 
                       	Alpha% = (rbgc And $FF000000) Shr 24 
	        	Red% = (rbgc And $00FF0000) Shr 16 
		        Green% = (rbgc And $0000FF00) Shr 8 
		        Blue% = rbgc And $000000FF
			
                        
                        ;modify the color values

                        Red = (Red * percentred)
                            If Red > 255 Then Red = 255
                            If Red < 0 Then Red = 0 
			
                        Green = (Green * percentgreen)
			                If Green > 255 Then Green = 255
                            If Green < 0 Then Green = 0
 
                        Blue = (Blue * percentblue)
			                If Blue > 255 Then Blue = 255
                            If Blue < 0 Then Blue = 0
 
                             ;make sure this modified color isn't the same as the mask color, if so change it else it will be masked
                             If red = 255 And blue = 255 And green = 0 Then green = 1

                        
                        ;rebuild the rbgc value and writepixel it to the image
                        rbgc = (Alpha Shl 24) Or (Red Shl 16) Or (Green Shl 8) + Blue 
			
                      	WritePixel ix,iy,rbgc,ImageBuffer(image)		
		


                EndIf
		Next
	Next
	
	Return image

SetBuffer BackBuffer()

End Function



Mr Brine(Posted 2004) [#9]
Of course if your not bothered about using windows mode ie full screen mode only, you could use the old gamma channels to simulate your light and dark. The only thing is its a full screen fade, you cant fade individual areas or other buffers. Here is a link to a small demo plus the gamma fader function:

http://www.blitzbasic.com/codearcs/codearcs.php?code=1109

Mr Brine