Author Topic: Blur image[BB2D]  (Read 10535 times)

0 Members and 1 Guest are viewing this topic.

Offline Paul

  • Pentium
  • *****
  • Posts: 1490
  • Karma: 47
    • View Profile
Blur image[BB2D]
« on: December 03, 2006 »
Hi,

I've been trying to make a blur image code but it isn't very fast anyone got any tips?

Here's the code

Code: [Select]
;Example
Graphics 800,600

Dim col(800,600),image(40)
SetBuffer BackBuffer()
image(0)=LoadImage("a-800-600.jpg")
For i=1 To 2
Print i+ " of 2"
image(i)=blurimage(image(0),i,i-1)
;api_Beep(100,100)
Next

While Not KeyHit(1)
a=MouseZ()
If a<0 Then a=0
If image(a+b)=0
Text 0,0,"Loading..."
Flip
image(a+b)=blurimage(image(0),a+b,0)
;api_Beep(100,100)
EndIf
DrawImage(image(a+b),0,0)
Text 580,0,MouseZ()
Flip 0
Cls
Wend


;function
Function blur(image,blur,update=1)
Local x,y,ux,uy,newimage,R,G,B,tmp,bl


If update=0
LockBuffer ImageBuffer(image)
For x=0 To ImageWidth(image)
For y=0  To ImageHeight(image)
col(x,y)=ReadPixelFast(x,y,ImageBuffer(image))
Next
Next
UnlockBuffer ImageBuffer(image)
EndIf


newimage=CreateImage(ImageWidth(image),ImageHeight(image))
LockBuffer ImageBuffer(newimage)
For x=0  To ImageWidth(image)
For y=0  To ImageHeight(image)
R=0
G=0
B=0
tmp=0
For ux=x-blur To x+blur Step 1
For uy=y-blur To y+blur Step 1
If ux=>0 And uy=>0 And ux=<ImageWidth(image) And uy=<ImageHeight(image)
R=R+(col(ux,uy) Shr 16 And $ff)
G=G+((col(ux,uy)Shr 8) And $ff)
B=B+(col(ux,uy) And $ff)
tmp=tmp+1
EndIf
Next
Next
R=R/tmp;((blur*2)+1)^2
G=G/tmp;((blur*2)+1)^2
B=B/tmp;((blur*2)+1)^2
bl=(R Shl 16)+(G Shl 8)+B
WritePixelFast(x,y,bl,ImageBuffer(newimage))
Next
Next
UnlockBuffer ImageBuffer(newimage)
Return newimage
End Function
« Last Edit: July 21, 2007 by Shockwave »
I will bite you - http://s5.bitefight.se/c.php?uid=31059
Challenge Trophies Won:

Offline MrP

  • Atari ST
  • ***
  • Posts: 176
  • Karma: 18
    • View Profile
Re: Blur image
« Reply #1 on: December 03, 2006 »
I Think You'll probably find that its the read and write pixel fast's that are causing your speed issue... The performance of those commands really sucks in Blitz... If Blitz had fast per pixel commands it'd be a hard language to beat, but unfortunately you cant have everything.....

Offline Paul

  • Pentium
  • *****
  • Posts: 1490
  • Karma: 47
    • View Profile
Re: Blur image
« Reply #2 on: December 03, 2006 »
If i draw the whole screen with writepixel (1024*768) without any math then i get almost 30 fps, and this is allot slower. So the math is slow to.
I will bite you - http://s5.bitefight.se/c.php?uid=31059
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17427
  • Karma: 499
  • evil/good
    • View Profile
    • My Homepage
Re: Blur image
« Reply #3 on: December 03, 2006 »
Blitz is too slow to blur a high resolution like this, if you were talking 640 * 480 or even 320*240 then you'd have a good chance.
BB3D would be able to do a good job using the 3D commands if you have that?
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Paul

  • Pentium
  • *****
  • Posts: 1490
  • Karma: 47
    • View Profile
Re: Blur image
« Reply #4 on: December 03, 2006 »
Ok. How would i do that?
can't think of anything ???
I will bite you - http://s5.bitefight.se/c.php?uid=31059
Challenge Trophies Won:

Offline Paul

  • Pentium
  • *****
  • Posts: 1490
  • Karma: 47
    • View Profile
Re: Blur image
« Reply #5 on: December 03, 2006 »
Here's the image, .exe and scource.  Bluring one and maybe 2 levels work pretty well buy going futher takes time.
I will bite you - http://s5.bitefight.se/c.php?uid=31059
Challenge Trophies Won:

Offline MrP

  • Atari ST
  • ***
  • Posts: 176
  • Karma: 18
    • View Profile
Re: Blur image
« Reply #6 on: December 03, 2006 »
render the screen you wish to blur to  a texture, usually a lower resolution texture of about 256x256 wil do it. You can go higher but thats more expensive.....

render that texture back on top of your original image several times, offset by a couple of pixels to the left right up and down with a lowish alpha value so you can see through it some. When they are all combined your original image will appear to be blurred. The more times you overlay the alpha texture, for example doing it eight times rather than four will give you a better final image, but you will have to find a trade off between quality and frame rate....

This is a very basic explanation of what your supposed to do, but I hope it helps some..... I'll see if I can dig out my old fake HDR code I did in blitz 3d. In the meantime see if you can get something down in code.....

Offline Paul

  • Pentium
  • *****
  • Posts: 1490
  • Karma: 47
    • View Profile
Re: Blur image
« Reply #7 on: December 03, 2006 »
Thx again

Haven't got time to try it now though. I have to do some homework
I will bite you - http://s5.bitefight.se/c.php?uid=31059
Challenge Trophies Won:

Offline Paul

  • Pentium
  • *****
  • Posts: 1490
  • Karma: 47
    • View Profile
Re: Blur image
« Reply #8 on: December 04, 2006 »
How do i render the screen to a texture and how do i render the texture on top of my image?

I'm not used to using the 3d commands.
I will bite you - http://s5.bitefight.se/c.php?uid=31059
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Blur image
« Reply #9 on: December 04, 2006 »
For your code, a couple of things you could do to possibly speed it up:

Take the bounds checking out of the inner loop.

There's no need to shift each color down and then back up although you will need to mask each color before they're combined back together (maybe OR would be faster than + to combine them but i'm not totally sure if that's still the case).

Use shift right instead of dividing if you can.

Use a 1 dimensional buffer.

As for the blitz3d stuff, I've dabbled a little but not sure how you'd do that.
« Last Edit: December 04, 2006 by Stonemonkey »

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: Blur image
« Reply #10 on: December 04, 2006 »
I think the commands for B3D are set gamma.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Paul

  • Pentium
  • *****
  • Posts: 1490
  • Karma: 47
    • View Profile
Re: Blur image
« Reply #11 on: December 04, 2006 »
Set Gamma?
Isn't that just to ajust the brightness levels of the whole screen?

Quote
Take the bounds checking out of the inner loop.
Worked verry vell thx :)
I will bite you - http://s5.bitefight.se/c.php?uid=31059
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: Blur image
« Reply #12 on: December 04, 2006 »
Sorry dude, ignore me - I'll get my coat. ;)
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Paul

  • Pentium
  • *****
  • Posts: 1490
  • Karma: 47
    • View Profile
Re: Blur image
« Reply #13 on: December 05, 2006 »
Sorry,
Didn't mean to sound nasty :(
I will bite you - http://s5.bitefight.se/c.php?uid=31059
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: Blur image
« Reply #14 on: December 05, 2006 »
I'll try and whip you up a quicker example dude unless somebody else beats me to it.
As your blurring an image, I'll take it that your after an Average Blur?
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Paul

  • Pentium
  • *****
  • Posts: 1490
  • Karma: 47
    • View Profile
Re: Blur image
« Reply #15 on: December 06, 2006 »
Yep, think thats what i want:)
Ty for helping me  :P
I will bite you - http://s5.bitefight.se/c.php?uid=31059
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: Blur image
« Reply #16 on: December 06, 2006 »
No worries mate.

Looking through and trying your original routine you posted, theres not anything wrong with it. It does the job nicely, and it ran pretty quick on my machine too with an 800x600 image.

But you will get faster per pixel effects in lower resolutions / smaller image sizes. I've not changed a busting lot from the way you were cleverly working out the blur and avoiding the blur fade effect; which I kept getting from my spin on things.

The following routine has 3 different sized images that you can toggle by press + / -, to change the blur rate use the right and left mouse. Using MouseZ was causing a few errors in your original, so I opted for mouse buttons instead. From this you'll be able to see the difference in blurring different sized images.

Code: [Select]
;
; Ever so slightly adapted Blur Example.
; By Clyde For Paul.
;
Const XRES=800
Const YRES=600

Graphics XRES,YRES,32,0
SetBuffer BackBuffer()

Const MAXIMAGES=3


Dim ImageData( XRES, YRES, MAXIMAGES )

Dim ImageW ( MAXIMAGES ), ImageH ( MAXIMAGES )
Dim CenterX ( MAXIMAGES ), CenterY ( MAXIMAGES )

Dim BlurredImage( MAXIMAGES )

Global BlurRate=0
Global ImageNum

;
; Prepare Images & Data.
;
LoadImages( "Media\a-800-600.jpg" )
LoadImages( "Media\a-800-600.jpg",1,640,480 )
LoadImages( "Media\a-800-600.jpg",2,320,240 )

For create=0 To MaxImages-1
BlurImage( 0,Create )
Next

While Not KeyHit(1)
Cls

If BlurRate<000 Then BlurRate=000

;
; Right Mouse Button.
;
If MouseHit(1) Then

BlurRate=BlurRate+1


BlurImage( BlurRate, ImageNum )

End If

;
; Left Mouse Button.
;
If MouseHit(2) Then

BlurRate=BlurRate+1

If BlurRate>100 Then BlurRate=100


BlurImage( BlurRate, ImageNum )

End If

;
; Minus Key.
;
If KeyHit(12) Then

ImageNum=ImageNum-1

If ImageNum<0 Then ImageNum=0

BlurImage( BlurRate, ImageNum )

End If

;
; Plus Key.
;
If KeyHit(13) Then

ImageNum=ImageNum+1

If ImageNum>MAXIMAGES-1 Then ImageNum=MAXIMAGES-1

BlurImage( BlurRate, ImageNum )
End If


;
; Draw The image with Blurring.
;
DrawImage BlurredImage( ImageNum ), CenterX( ImageNum ), CenterY( ImageNum )

;
; Display Blur Rating.
;
Color 0,0,0 
Text CenterX( ImageNum )+1, CenterY( ImageNum )+1, "Blur Rate:"+BlurRate+" ImageNum:"+ImageNum

Flip

Wend

;
; Free Images From Memory.
;
For Free=0 To MAXIMAGES-1

FreeImage BlurredImage( Free )

Next

End


Function LoadImages( FileName$, Num=0, SizeX=XRES, SizeY=YRES )

Local Image

If FileType( FileName$ )=1

Image=LoadImage(FileName$)

ResizeImage Image,SizeX,SizeY

ImageW( Num )=ImageWidth ( Image )
ImageH( Num )=ImageHeight( Image )

CenterX( Num )=(( XRES/2 ) - ( ImageW( Num )/2))
CenterY( Num )=(( YRES/2 ) - ( ImageH( Num )/2))

BlurredImage( Num )=CreateImage( ImageW( Num ), ImageH( Num ))

LockBuffer ImageBuffer( Image )

For y=0 To ImageH( Num )-1
For x=0 To ImageW( Num )-1

ImageData(x,y,Num)=ReadPixelFast(x,y,ImageBuffer( Image )) And $FFFFFF

Next
Next

UnlockBuffer ImageBuffer( Image )
FreeImage Image

Else
RuntimeError("File: "+FileName$+" not present.")
End If

End Function


Function BlurImage( Rate=0, ImageNum=0 )

Local x,y,ux,uy,R,G,B,tmp,col

LockBuffer ImageBuffer( BlurredImage( ImageNum ))
For y=0 To ImageH( ImageNum )-1
For x=0 To ImageW( ImageNum )-1

R =0
G =0
B =0
tmp =0

For uy=y-Rate To y+Rate
For ux=x-Rate To x+Rate

If ux=>0 And ux=<ImageW( ImageNum )-1 And uy=>0 And uy=<ImageH( ImageNum )-1

R=R+(( ImageData( ux, uy, ImageNum ) Shr 16) And $FF)
G=G+(( ImageData( ux, uy, ImageNum ) Shr 8 ) And $FF)
B=B+(( ImageData( ux, uy, ImageNum ) Shr 0 ) And $FF)

tmp=tmp+1

EndIf

Next
Next

R=R/tmp
G=G/tmp
B=B/tmp

Col=(R Shl 16) Or (G Shl 8) Or B

If Col<>0 Then

WritePixelFast x, y, Col, ImageBuffer( BlurredImage( ImageNum ))

End If

Next
Next
UnlockBuffer ImageBuffer( BlurredImage( ImageNum ))

End Function

Also a good tip, with Blitz it has a plus side and can easily load in png's without the need for a huge amount of code / or external dlls, like Freebasic would need; so if you can save as them then I'd advise you to use them as they will make for much smaller media for when it comes to distributing later.

Also try out upx ( which there's a few posts about it here - try the search for them, if you cant find them then I'll take alook for them ) on your executables as they'll be greatly reduced.

Your doing great guns Paul dude, and I hope my slightly changed routine helps you with this and gives you some ideas for future stuff.

Cheers and all the very best,
Clyde.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Paul

  • Pentium
  • *****
  • Posts: 1490
  • Karma: 47
    • View Profile
Re: Blur image
« Reply #17 on: December 06, 2006 »
THX allot!! :)

Just wondering something though, is your routine faster? and how do you do the 3d stuff that shokwave was talking about?

I'm trying to speed it up more and have devised a test for comparing 1d arrays to 2d, look at the difference!

Code: [Select]
Dim col(614400),colo(800,600)
LockBuffer BackBuffer()
WaitKey()
time=MilliSecs()
For t=0 To 100
For i=0 To 614400
y=i Shr 10
x=i-(y Shl 10)
WritePixelFast(x,y,col(i),BackBuffer())
Next
Next
time1=MilliSecs()-time
;WaitKey()
time=MilliSecs()
For t=0 To 100
For x=0 To 799
For y=0 To 599
WritePixelFast(x,y,colo(x,y),BackBuffer())
Next
Next
Next
time2=MilliSecs()-time
UnlockBuffer BackBuffer()
Print time1
Print time2Â  
WaitKey()

[Edit - [ Code ] Tags added. ]
« Last Edit: December 07, 2006 by Clyde »
I will bite you - http://s5.bitefight.se/c.php?uid=31059
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Blur image
« Reply #18 on: December 06, 2006 »
You have the 1d array doing 614400 pixels when it should only be doing 800x600 = 480000.

Jim
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: Blur image
« Reply #19 on: December 07, 2006 »
My routine isnt faster then your original, it's just setup slightly different but does exactly the same as you had.
As for the B3D bluring, as I havent used that side of blitz for quite sometime, I'd have to look into it for you.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won: