?
Solved

Need faster image rotate routine

Posted on 2003-02-27
4
Medium Priority
?
631 Views
Last Modified: 2010-08-05
Does anybody know how to speed up this image rotation subroutine?  I've managed to speed it up quite a bit using the DIB functions, but it still is 10 times slower than microsoft photo editor.
The goal is to speed up this routine 4 times or more.
The basic algorithm is:
(1) Takes the image in Picture1(ImageIndex)
    pictureboxcontrolarray and stores in a bytearray
    using GetDIBits function.
(2) Rotates the bytearray color data 90 degrees.  >the slow part<
(3) Puts the bytearray data in picAction
    pictureboxcontrol using SetDIBits function.

'The Code:
'(I have the proper types defined for: BITMAP, BITMAPINFOHEADER, RGBQUAD, and BITMAPINFO.)
'(I have GetDIBits and SetDIBits properly declared.)
Private Const DIB_RGB_COLORS = 0&
Private Const BI_RGB = 0&

Public Sub RotateImage(ImageIndex As Integer, Direction As Byte)
 Dim wid As Long
 Dim hgt As Long
 Dim bitmap_info As BITMAPINFO
 Dim bitmap_info2 As BITMAPINFO
 Dim pixels() As Byte
 Dim pixels2() As Byte
 Dim i As Long
 Dim j As Long
 Dim k As Long
   
    'Setup Picture1() picturebox (image already loaded previously)
    Picture1(ImageIndex).ScaleMode = vbPixels
    Picture1(ImageIndex).AutoRedraw = True
   
    'Prepare the bitmap description
    wid = Picture1(ImageIndex).ScaleWidth
    hgt = Picture1(ImageIndex).ScaleHeight
    With bitmap_info.bmiHeader
        .biSize = 40
        .biWidth = wid
        .biHeight = -hgt 'Use negative height to scan top-down.
        .biPlanes = 1
        .biBitCount = 32 '32-bit color
        .biCompression = BI_RGB
        .biSizeImage = ((((wid * .biBitCount) + 31) \ 32) * 4) * hgt
    End With
   
    'Load the bitmap's data into bytearray
    ReDim pixels(1 To 4, 1 To wid, 1 To hgt)
    GetDIBits Picture1(ImageIndex).hdc, Picture1(ImageIndex).Image, 0, hgt, pixels(1, 1, 1), _
              bitmap_info, DIB_RGB_COLORS

    '-------------------------------------------------------------
    'Rotate bytearray 90 degrees  (this is where I lose speed)
    ReDim pixels2(1 To 4, 1 To hgt, 1 To wid)
    Select Case (Direction)
        Case 1 'cw
            For i = 1 To wid
                For j = 1 To hgt
                    For k = 1 To 4
                        pixels2(k, (hgt - j + 1), (i)) = pixels(k, i, j)
                    Next k
                Next j
            Next i
        Case 2 'ccw
            For i = 1 To wid
                For j = 1 To hgt
                    For k = 1 To 4
                        pixels2(k, (j), (wid - i + 1)) = pixels(k, i, j)
                    Next k
                Next j
            Next i
    End Select
    '-------------------------------------------------------------

    'Setup new header
    With bitmap_info2.bmiHeader
        .biSize = 40
        .biWidth = hgt
        .biHeight = -wid 'Use negative height to scan top-down.
        .biPlanes = 1
        .biBitCount = 32 '32-bit color
        .biCompression = BI_RGB
        .biSizeImage = ((((hgt * .biBitCount) + 31) \ 32) * 4) * wid
    End With

    'Setup picAction picturebox
    With picAction
        .ScaleMode = vbPixels
        .Height = Picture1(ImageIndex).Width
        .Width = Picture1(ImageIndex).Height
    End With

    'Display the results.
    SetDIBits picAction.hdc, picAction.Image, 0, wid, pixels2(1, 1, 1), bitmap_info2, _
              DIB_RGB_COLORS
    picAction.Picture = picAction.Image

End Sub
0
Comment
Question by:J-Man
[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
  • Learn & ask questions
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
Belfry earned 225 total points
ID: 8040279
I guess there are severel reasons why your program is slower than microsoft's photo editor:
1) you are using vb, vb compiler is not very good at optimizing speed, while photo editor might be compiled by c compiler;
2) you are using API, calling API might cost much time, if possible, you can try to read the bitmap information directly, but it's a huge work;
3) there might be some improvements for the loop, i'm not quite sure, but i know looping from 1 to wid might cost more time than looping from wid-1 to 0(that's what my professor of computer science said) 'cause
    for i=1 to wid   'compares i with wid every time
    while
    for i=wid-1 to 0 'wid is used only once

hope these help, enjoy programming!
0
 
LVL 1

Author Comment

by:J-Man
ID: 8045183
Thanks for the comments.

1) I'm basically using MS PhotoEd as a benchmark.  I don't expect to be as fast as photoed, but I presume I can rotate an image faster than my routine currently does (with VB).  I'm hoping for 4x speed boost which would still be at least 3x slower than photoed.
Do you know how much more efficient an application can be expected to run when compiled from VS6-C++ as compared to VS6-VB?  I could go either way, but VB is usually easier for me.

2) As far as I know, using GetDIBits is the fastest way to put a DDB into a DIB-format bytearray.  Much faster than using Point/PSet or GetPixel/SetPixel.  The actual file may be jpeg, gif, metafile, bmp, etc so it won't be in bmp format until loaded into the picturecontrol.
Does anyone know if I could expect faster results trying to put the colordata bytes into a PropertyBag or DataObject since they usually put the data as a bytearray automatically when retrieving?  If so, any help on these two object-types?

3) Good idea.  I often forget about this.  I made the changes, but only see a 2% increase in speed.  Still a long way to go for the goal.  ;-)

0
 

Expert Comment

by:CleanupPing
ID: 8901001
J-Man:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Experts: Post your closing recommendations!  Who deserves points here?
0
 
LVL 1

Author Comment

by:J-Man
ID: 8904172
Sorry guys, I've been thinking about closing this one for a couple weeks now.
I'll accept Belfry's answer because he's totally right even though it wasn't what I wanted to hear.  ;-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month10 days, 10 hours left to enroll

765 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