Trouble with GrayScale Filter
BlitzPlus Forums/BlitzPlus Programming/Trouble with GrayScale Filter
| ||
I was trying to write a greyscale filter. I thought I was going in the right direction with it, but the program isn't doing what I had expected. I'm trying to only show luminosity levels on the picture in backbuffer(). Any help would be appreciated. Thanks, Ben |
| ||
Guess I should post the code too. Graphics 1024, 768 XOffset = 20 YOffset = 20 ImageFile$ = "C:\Blitz Work\Small Picture 009.jpg" SetBuffer BackBuffer() Image = LoadImage(ImageFile) DrawBlock Image,XOffset,YOffset LockBuffer BackBuffer() For XIndex = 1 To ImageWidth(Image) For YIndex = 1 To ImageHeight(Image) PixelColor = ReadPixelFast(XIndex,YIndex) And $FFFFFF Red = PixelColor And $FF0000 Green = PixelColor And $00FF00 Blue = PixelColor And $0000FF MaxColor = Maximum(Red,Green,Blue) MaxColor = MaxColor + MaxColor Shl 8 + MaxColor Shl 16 WritePixelFast(XOffset,YOffset,MaxColor,BackBuffer()) Next If KeyDown(1) Then End Next UnlockBuffer BackBuffer() Flip Function Maximum(Red,Green,Blue) If Red > Green And blue Return red ElseIf Green > Red And Blue Return Green Else Return Blue EndIf End Function While Not KeyDown(1) Wend |
| ||
WritePixelFast(XOffset,YOffset,MaxColor,BackBuffer()) Red = (PixelColor And $FF0000) Shr 16 Green = (PixelColor And $00FF00) Shr 8 If Red > (Green And blue) Return red ElseIf Green > (Red And Blue) |
| ||
Good Point! I'll try that. That's what the problem was. Thanks, I guess I need to eat now or something. I've noticed this program runs really slow on a 1000 x 650 picture. Any idea how to speed it up? Thanks, Ben |
| ||
You can't replace If Red>Green And Red>Blue with If Red > (Green And blue), And is a binary operator as in 4 And 3 gives 0. Also you could calculate luminance as 0.299 Red + 0.587 Green + 0.114 Blue for better results |
| ||
Yeah, I was kind of hungry when I wrote it. So I didn't catch some stupid mistakes. Is multiplication faster than the bit masking and shifting? |
| ||
-Snip- (Incorrect code sample. See below for corrected version) |
| ||
Wow! That code is so Cool!! It looks a lot better than what I was doing, and I have no idea why. Is there any theory that I can have on it? Or any sites or books I can check out to help me understand this better? Thanks, Ben |
| ||
Oops! Looks like I cut out a little too much while cleaning up my code: the percentage flag in the code above no longer works at all! Let me correct this, and repost. |
| ||
OK, here is the corrected function, including a fader loop as an example:Graphics 640,480,16,2 img%=LoadImage("x:\monkey6.jpg") SetBuffer BackBuffer() DrawImage img%,0,0 For t=0 To 100 Step 10 DrawImage img%,0,0 monochrome (t) Flip Next Delay 2000 For t=100 To 0 Step -10 DrawImage img%,0,0 monochrome (t) Flip Next WaitKey() End Function Monochrome(perct#) SetBuffer BackBuffer() LockBuffer For y=0 To 479 For x=0 To 639 temp1=ReadPixel(x,y) orgb=(temp1 And $FF) orgg=(temp1 And $FF00) Shr 8 orgr=(temp1 And $FF0000) Shr 16 desb=((orgr*0.299)+(orgg*0.587)+(orgb*0.114)) desr=orgr*(1-(perct#/100))+desb*(perct#/100) desg=orgg*(1-(perct#/100))+desb*(perct#/100) desb=orgb*(1-(perct#/100))+desb*(perct#/100) WritePixel x,y,desb+(desg Shl 8)+(desr Shl 16) Next Next UnlockBuffer End Function Unfortunately it isn't quite fast enough to look great as a fade, I think the effect is better to switch from full color to B&W in a single step rather than using the fade example listed above -- but it does illustrate how the function works. Does anyone here happen to have a *fast* cross-fader? Generating a pure B&W image and doing a *crossfade* would have the exact same effect after all... But probably much smoother, if the crossfader is fast enough. |
| ||
Ok, this is actually kind of funny: in the process of doing some more 'cleanups' to the monochrome code above, I accidentally messed up the declaration of the variables holding the target variables in the code... Which had a a very interesting side effect: This made it lose all its monochrome abilities, but instead introduced a feedback loop into the pixel declarations, leading to an actual pixel BLURRING instead. ...Yes, I actually *accidentally* wrote a horizontal blur function. I still can't believe it. After that, vertical Blur was nothing more than swapping the 'x' and 'y' loops... |
| ||
Great stuff there XLSIOR & welldone guys :) Really cool image effects :) |
| ||
Just in case anyone is interested: I just uploaded the blur code to the Code Archives forum as well. |