Announcement

Collapse
No announcement yet.

Paul Image Rotation Code

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Paul Image Rotation Code

    Hi Paul,

    I found some old code you wrote to rotate an image. It was short and sweet which is why I am bit embarassed that I cannot convert it for my needs. Your code is below. I basically need to rotate a portion of an image within a larger image.

    The total image is just an rgbtriple that is 320x240. I want to rotate a box in the center of that image around its own center within the larger image. This box is 176 pixels on each side. Its upper left coordinate is then 44,16 and its lower right is 220,192.

    I am trying to understand how you picked your constants and after trying several variations I thought I would just ask if you had any suggestions.

    Code:
    #COMPILE EXE
    #DIM ALL
    
    %xWindowSize = 480
    %yWindowSize = 480
    
    %xRotateOffset=140
    %yRotateOffset=140
    
    FUNCTION PBMAIN () AS LONG
    LOCAL hInitialBMP,hWindow, hThread AS DWORD
    LOCAL InitialBMPstring, RotatedBMPstring AS STRING
    LOCAL x,y,a,xSize,ySize,t1,t2, Oldx,Oldy, QuitAll, lResult AS LONG
    LOCAL Angle,sina, cosa AS EXT
    LOCAL file AS STRING
    LOCAL initial(), rotated() AS LONG
    
    
    file$="rose.bmp"
    x = FREEFILE
    OPEN file$ FOR BINARY AS x
    GET #x, 19, xSize
    GET #x, 23, ySize
    CLOSE x
    
    
    GRAPHIC WINDOW "Rotation test",200,200, %xWindowSize,%yWindowSize TO hWindow
    GRAPHIC ATTACH hWindow,0
    GRAPHIC GET BITS TO RotatedBMPstring
    GRAPHIC DETACH
    
    GRAPHIC BITMAP LOAD file$, xSize&, ySize&  TO hInitialBMP
    GRAPHIC ATTACH hInitialBMP,0
    GRAPHIC GET BITS TO InitialBMPstring
    GRAPHIC DETACH
    
    THREAD CREATE RedrawGraphics (hWindow) TO hThread
    
    DIM initial&(1 TO xSize&,1 TO ySize&) AT STRPTR(InitialBMPstring)+8
    DIM rotated&(1 TO %xWindowSize,1 TO %yWindowSize) AT STRPTR(RotatedBMPstring)+8
    
    
    
    LOCAL tc, ts AS EXT
    
    t1&=TIMER*100
    DO
    FOR a& = 0 TO 360    'degrees
    
    angle##=a&/(180/3.141592653589)
    cosa=COS(angle##)
    sina=SIN(angle##)
    
         FOR x&=1 TO %xWindowSize
             tc = (x& - %xRotateOffset) *cosa  + %xRotateOffset  -40
             ts = -(x& - %xRotateOffset)*sina + %yRotateOffset  -70
             FOR y& = 1 TO %yWindowSize
    
                oldx& =tc + (y& - %yRotateOffset)*sina
                oldy& =(y& - %yRotateOffset) *cosa + ts
    
    
                IF oldx&>0 AND oldx& < xSize&  AND oldy&>0 AND oldy&< ySize& THEN
                     Rotated&(x&,y&)   =  Initial&(oldx& ,oldy&)
                ELSE
                     Rotated&(x&,y&)   =  %WHITE
    
                END IF
    
    
             NEXT
         NEXT
         
         SLEEP 500
    
    
    
    GRAPHIC ATTACH hWindow,0 ,REDRAW
    GRAPHIC SET BITS RotatedBMPstring
    GRAPHIC DETACH
    
    SLEEP 0
    NEXT
    LOOP
    
    t2&=TIMER*100
    'PRINT "Time Taken = "(t2&-t1&)/100
    'PRINT "Frames per second ="; 360/((t2&-t1&)/100 )
    
    QuitAll& =1
    
    THREAD CLOSE hThread TO lResult
    MSGBOX "dfg"
    
    END FUNCTION
    
    
    
    'This is a flag intended to cause all otherwise non-terminating threads to quit
    GLOBAL QuitAll&
    
    'This routine and the main program must use GRAPHIC ATTACH WindowID,0,REDRAW but the main program shouldn't
    'issue a GRAPHIC REDRAW command in normal use. Leave that up to this thread.
    FUNCTION RedrawGraphics(BYVAL WinID AS DWORD) AS DWORD
    
    GRAPHIC ATTACH WinID,0,REDRAW
    
    DO
    
        GRAPHIC REDRAW
        SLEEP 1
    LOOP UNTIL QuitAll&
    
    END FUNCTION

  • #2
    Kevin,
    maybe this version of the code will help. The constants are all set at the start.

    Paul.
    Code:
    'PBCC5.01 program
    #COMPILE EXE
    #BREAK ON
    #DIM ALL
    
    
    %xWindowSize = 450   'the size of the window
    %yWindowSize = 350
    
    %xImageSize = 320    'the size of the image
    %yImageSize = 240
    
    %xImagePos = 50     'the image position within in the window (top left corner co-ordinate)
    %yImagePos = 50
    
    %xRotateStart = 44   'top left point in image of part of image to rotate
    %yRotateStart = 16
    
    %xRotateSize = 176   'the size of the rotatable part of the image
    %yRotateSize = 176
    
    %xRotateOffset = %xImagePos + %xRotateSize / 2 + %xRotateStart   'the point in the window to rotate about
    %yRotateOffset = %yImagePos + %yRotateSize / 2 + %yRotateStart
    
    
    FUNCTION PBMAIN () AS LONG
    LOCAL hInitialBMP,hWindow, hThread AS DWORD
    LOCAL InitialBMPstring, RotatedBMPstring AS STRING
    LOCAL x,y,a,xSize,ySize,t1,t2, Oldx,Oldy, QuitAll, lResult AS LONG
    LOCAL Angle,sina, cosa AS EXT
    LOCAL file AS STRING
    LOCAL initial(), rotated() AS LONG
    
    
    xSize = %xImageSize
    ySize = %yImageSize
    
    GRAPHIC WINDOW "Rotation test",20,20, %xWindowSize,%yWindowSize TO hWindow
    GRAPHIC ATTACH hWindow,0
    GRAPHIC GET BITS TO RotatedBMPstring
    GRAPHIC DETACH
    
    GRAPHIC BITMAP NEW xSize&, ySize& TO hInitialBMP
    GRAPHIC ATTACH hInitialBMP,0
    'draw the image to rotate
    FOR x = 1 TO MIN(%xImageSize, %yImageSize)/2
        GRAPHIC BOX (x,x) - (%xImageSize - x, %yImageSize - x),,x*2
    NEXT
          
        
    GRAPHIC GET BITS TO InitialBMPstring
    GRAPHIC DETACH
    
    THREAD CREATE RedrawGraphics (hWindow) TO hThread
    
    DIM initial&(1 TO xSize&,1 TO ySize&) AT STRPTR(InitialBMPstring)+8
    DIM rotated&(1 TO %xWindowSize,1 TO %yWindowSize) AT STRPTR(RotatedBMPstring)+8
    
    
    
    LOCAL tc, ts AS EXT
    
    t1&=TIMER*100
        
    FOR a& = 0 TO 360    'degrees
    
    angle##=a&/(180/3.141592653589)
    cosa=COS(angle##)
    sina=SIN(angle##)
    
    FOR x&=1 TO %xWindowSize
        tc = (x& - %xRotateOffset) *cosa + %xRotateOffset  - %xImagePos
        ts = -(x& - %xRotateOffset)*sina + %yRotateOffset  - %yImagePos
        FOR y& = 1 TO %yWindowSize
    
           oldx& =tc + (y& - %yRotateOffset)*sina
           oldy& =(y& - %yRotateOffset) *cosa + ts
           
           IF (x& > %xImagePos) AND (x& < (%xImagePos + xSize&))  AND (y& > %yImagePos) AND (y& < (%yImagePos + ySize&)) THEN
               'point lies within the original image, so copy it unrotated
               Rotated&(x& ,y&)  =  Initial&(x& - %xImagePos ,y& - %yImagePos)
           
    ClipImage:
           
           ELSE
               'point lies outside the original image so fill it in with white
               Rotated&(x&,y&) =  %WHITE
           END IF
           
    NoClipImage:
           'if you want the rotated image to clip at the edge of the original image then move this IF-THEN block to ClipImage
           'now, rotate the part that needs to be rotated
           IF oldx& > %xRotateStart AND oldx& < (%xRotateStart + %xRotateSize) AND oldy& > %yRotateStart AND oldy& < (%yRotateStart +  %yRotateSize) THEN
               'point lies within area of image to rotate
               Rotated&(x&,y&)   =  Initial&(oldx& ,oldy&)
           END IF
           
        NEXT
        
    NEXT
    
    
    GRAPHIC ATTACH hWindow,0 ,REDRAW
    GRAPHIC SET BITS RotatedBMPstring
    GRAPHIC DETACH
    
    SLEEP 0
    NEXT
    
    t2&=TIMER*100
    PRINT "Time Taken = "(t2&-t1&)/100
    PRINT "Frames per second ="; 360/((t2&-t1&)/100 )
    
    QuitAll& =1
    
    THREAD CLOSE hThread TO lResult
    WAITKEY$
    
    END FUNCTION
    
    
    
    'This is a flag intended to cause all otherwise non-terminating threads to quit
    GLOBAL QuitAll&
    
    'This routine and the main program must use GRAPHIC ATTACH WindowID,0,REDRAW but the main program shouldn't
    'issue a GRAPHIC REDRAW command in normal use. Leave that up to this thread.
    FUNCTION RedrawGraphics(BYVAL WinID AS DWORD) AS DWORD
    
    GRAPHIC ATTACH WinID,0,REDRAW
    
    DO
    
        GRAPHIC REDRAW
        SLEEP 1
    LOOP UNTIL QuitAll&
    
    END FUNCTION

    Comment


    • #3
      Paul,

      Thanks Paul!

      I wanted to send you a personal email but do not see your email listed...are you willing to share it?

      Best,
      Kevin

      Comment


      • #4
        Kevin,
        no need to e-mail, just buy me a pint next time we meet.

        Paul.

        Comment


        • #5
          well, i was going to send you a gift card from amazon which would require your email...but if you want to remain "anon", then that is OK...

          Comment

          Working...
          X