Radial Blur Zoom

Posted on 2004-04-22
Last Modified: 2012-08-13
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)
                  nX = 0
            End If
            If j < cY Then
                  nY = j + (Sin(Alpha) * l)
            ElseIf j >= cY Then
                  nY = j - (Sin(Alpha) * l)
                  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
                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
Question by:ennixo
  • 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.

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...

Expert Comment

ID: 10897632

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.
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.


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.
LVL 17

Accepted Solution

mokule earned 500 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)

Author Comment

ID: 10897967
original image :
a radial blur (zoom) with photoshop :
a radial blur (zoom) with my algorithm :

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 !!!

Author Comment

ID: 10898148
even if you didn't do what i asked, i was so glad that i accepted your answer...
but if you want, you can still research to be faster & faster !
LVL 17

Expert Comment

ID: 10898186

Well, take it into account.:)

Expert Comment

ID: 10900444

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.


Author Comment

ID: 10900796

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 (!!!?)

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 ;)
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


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


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)

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question