?
Solved

Need faster image rotate routine

Posted on 2003-02-27
4
Medium Priority
?
632 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
  • 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

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

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…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month8 days, 11 hours left to enroll

621 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