Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

Posted on 2004-04-22
Medium Priority
1,466 Views
Hi, i programmed a function to radial blur (zoom) pictures in Vb6 (with a force from zero to 100% and a point of blur), it works well (better quality than photoshop) but it's a little slower.

my function is easy and i would like to know how to accelerate the process to get the same quality faster
i need a vb6 or C++ function.

here's my function :

'cx, cy = Center X and Center Y
'k = Force (0 to 100%)
'Quality = Quality : 1 = Best | 2 = Good | 4 = Medium | 16 = Bad
Public Sub RadialBlurZoom(ByVal cx As Long, ByVal cy As Long, ByVal k As Single, ByVal Quality as Long)

Dim j               As Long
Dim i               As Long
Dim l               As Long
Dim nX              As Long
Dim nY              As Long
Dim dX              As Long
Dim dY              As Long
Dim Alpha           As Single
Dim Hypotenuse      As Long
Dim vMax            As Long
Dim TotalR          As Long
Dim TotalG          As Long
Dim TotalB          As Long
Dim Dividor         As Long
Dim NewBits()       As Byte

'in the form, PicBits is a private array which contains bitmapbits (from GetBitmapBits)

ReDim NewBits(LBound(PicBits) To UBound(PicBits))

If K = 0 Then K = 1
If K > 100 Then K = 100
K = K / 400

'shpSelection is a Shape which contains the rectangle area to be processed
For j = shpSelection.Top + 1 To shpSelection.Top + shpSelection.Height

dY = Abs(j - cY)

For i = shpSelection.Left + 1 To shpSelection.Left + shpSelection.Width

dX = Abs(i - cX)
If dX = 0 Then dX = 1

Alpha = Atn(dY / dX)
Hypotenuse = Sqr(dX * dX + dY * dY)

TotalR = 0
TotalG = 0
TotalB = 0
Dividor = 0

vMax = Hypotenuse * K

For l = -vMax To vMax Step Quality
If i < cX Then
nX = i + (Cos(Alpha) * l)
ElseIf i >= cX Then
nX = i - (Cos(Alpha) * l)
Else
nX = 0
End If
If j < cY Then
nY = j + (Sin(Alpha) * l)
ElseIf j >= cY Then
nY = j - (Sin(Alpha) * l)
Else
nY = 0
End If

'in this form, Matrix is a private array which contains indexes of
'bits in PicBits to retrieve a point with X and Y values.
If nX > 0 And nY > 0 And nX <= UBound(Matrix) And nY <= UBound(Matrix, 2) Then
TotalR = TotalR + PicBits(Matrix(nX, nY))
TotalG = TotalG + PicBits(Matrix(nX, nY) + 1)
TotalB = TotalB + PicBits(Matrix(nX, nY) + 2)
Dividor = Dividor + 1
End If
Next l
If Dividor > 0 Then
NewBits(Matrix(i, j)) = TotalR \ Dividor
NewBits(Matrix(i, j) + 1) = TotalG \ Dividor
NewBits(Matrix(i, j) + 2) = TotalB \ Dividor
Else
NewBits(Matrix(i, j)) = PicBits(Matrix(i, j))
NewBits(Matrix(i, j) + 1) = PicBits(Matrix(i, j) + 1)
NewBits(Matrix(i, j) + 2) = PicBits(Matrix(i, j) + 2)
End If
Next i
Next j

'P is the name of a pictureBox where the picture was loaded
'sets the new bits to P.Image
SetBitmapBits P.Image, UBound(NewBits), NewBits(1)

End Function
0
Question by:ennixo
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 6
• 4
• 2

LVL 17

Expert Comment

ID: 10897278
Maybe I can try. In case You would explain better in words what it should do.
0

LVL 5

Author Comment

ID: 10897610
ok, for each pixel i compute the resulting color (function of its position, the center position and the force).
everything is calculated for one pixel, everything is calculated again for another pixel, etc...

what i would like is a function which won't recompute everything for each pixel but which will use what was already computed (when possible). this way, there will be less computes and it will be a lot faster (i think) !

but i don't really know how to implement this...
0

Expert Comment

ID: 10897632
Hi.

Depending on in what range the arguments (Alpha) for your sine and cosine functions vary, you can try to make your own sine and cosine functions, based on the Taylor series. As far as I know, VB (and other programming languages) actually does the same, but it usually uses very long series. You can choose to truncate the series earlier. For example write your own functions mCos and mSin which use the following truncated Taylor series:

mCos = 1 - (1/2)*x*x + (1/24)*x*x*x*x - (1/720)*x*x*x*x*x*x
mSin = x - (1/6)*x*x*x + (1/120)* x*x*x*x*x - (1/5040) *x*x*x*x*x*x*x

These two series give pretty good values for -pi<Alpha<pi. Try to make the series even shorter and compare the results. I am also interested to know if or how much this solution improves the speed of your code. So please keep me updated if you choose to give it a try.

There are other things you can do, for example bit-shifting instead of multiplication/division by 2, and so on. I know how it works in C but I am not sure how you can do it in VB, probably the same. Ask me later if you.
0

LVL 5

Author Comment

ID: 10897883
you won't believe me because i really thought mCos and mSin would accelerate the radial blur... but for the same image in the same conditions with compiled code :
Cos & Sin : 27.189 sec
mCos & mSin : 30.564 sec
i did not compare the resulting images but mSin and mCos seems to give the same result effect.
perhaps it would be faster with simpler mCos & mSin but i don't really know how to "truncate the series" more.

for bitshifting i didn't found where i could do this (no multiplication or division by 2) and i don't know how to do this in vb.
0

LVL 17

Accepted Solution

mokule earned 2000 total points
ID: 10897889

1. But what function to calculate? I'm not sure what radial blur(zoom) should do.

2. You should not calculate Cos and Sin in this loop but once before entering loop.
For l = -vMax To vMax Step Quality
If i < cX Then
nX = i + (Cos(Alpha) * l)
ElseIf i >= cX Then
nX = i - (Cos(Alpha) * l)
0

LVL 5

Author Comment

ID: 10897967
mokule:
1.
original image :http://auchtre.free.fr/xpws/_flr_cmp_original.jpg
a radial blur (zoom) with photoshop : http://auchtre.free.fr/xpws/_flr_cmp_photoshop.jpg
a radial blur (zoom) with my algorithm : http://auchtre.free.fr/xpws/_flr_cmp_xpws.jpg

that's why i want to be faster, mine is better quality but slower.

2. GREAAAAAAT !!!!!!!
9.935 sec, same image, same conditions !!!

now it's better and faster than photoshop !!!
0

LVL 5

Author Comment

ID: 10898148
but if you want, you can still research to be faster & faster !
0

LVL 17

Expert Comment

ID: 10898186

Well, take it into account.:)
0

Expert Comment

ID: 10900444
hi!

did the quality of the image deteriorate?

these are the more truncated versions (you simply remove the last term in the polynomials)

mCos = 1 - (1/2)*x*x + (1/24)*x*x*x*x
mSin = x - (1/6)*x*x*x + (1/120)* x*x*x*x*x

may be you won't need these now any more, but who knows? may be another time! cheers.

0

LVL 5

Author Comment

ID: 10900796

with
mCos = 1 - (1/2)*x*x + (1/24)*x*x*x*x
mSin = x - (1/6)*x*x*x + (1/120)* x*x*x*x*x
image is a little deteriorated but it is still slower (!!!?)

with
mCos = 1 - (1/2)*x*x
mSin = x - (1/6)*x*x*x
image is deteriarated a lot more but it is faster
but the deterioration can not be a "quality" because the deformation is not the same so the constraint of the effect are not respected...

but thanx ;)
0

LVL 17

Expert Comment

ID: 10901306
mCos = 1 - (1/2)*x*x + (1/24)*x*x*x*x           // 6 multiplications
mSin = x - (1/6)*x*x*x + (1/120)* x*x*x*x*x  // 8 multiplications

It can be optimized as follows. But I don't expect too mauch with it

y = x*x           // 1 multiplication

mCos = 1 - (1/2)*y + (1/24)*y*y
mSin = x - (1/6)*x*y + (1/120)* x*y*y

further

mCos = 1 - (1/2)*y* (1 + (1/12)*y)    // 3 multiplications
mSin = x - (1/6)*x*y*(1 + (1/20)*y)  // 4 multiplications

0

LVL 5

Author Comment

ID: 10901734
i tried this kind of optimizations but the change is smaller than the loss.
Cos & Sin are definitively faster (i think it's because there are Cos & Sin instruction in asm so it is very fast because the processor can compute this, computing mCos & mSin needs a lot more instructions so it is slower)
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
###### Suggested Courses
Course of the Month9 days, 19 hours left to enroll