Jump to content

_GDIPlus_DrawImagePoints function


Malkey
 Share

Recommended Posts

Example use of GdipDrawImagePoints function. Input is :-

the handle of the graphics to display,

image handle (from image file),

three points (top left cnr, top right cnr and bottom left crn).

This is ideal for matrix manipulation. Plus, it can replace _GDIPlus_MatrixRotate, ShearMatrix, and ScaleMatrix functions, depending on where the three points are placed relative to each other.

_GDIPlus_DrawImagePoints.au3 file follows. A simple example

#include <GDIPlus.au3>
#include <ScreenCapture.au3>


_Main()

Func _Main()
    Local $hBitmap1, $hBitmap2, $hImage1, $hImage2, $hGraphic

    ; Initialize GDI+ library
    _GDIPlus_Startup()

    ; Capture full screen
    $hBitmap1 = _ScreenCapture_Capture("")
    $hImage1 = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap1)

    ; Capture screen region
    $hBitmap2 = _ScreenCapture_Capture("", 0, 0, 400, 300)
    $hImage2 = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap2)

    ; Draw one image in another
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage1)

    _GDIPlus_DrawImagePoints($hGraphic, $hImage2, 100, 100, 600, 170, 130, 570)

    ; Draw a frame around the inserted image
    _GDIPlus_GraphicsDrawRect($hGraphic, 100, 100, 400, 300)

    ; Save resultant image
    _GDIPlus_ImageSaveToFile($hImage1, @MyDocumentsDir & "\GDIPlus_Image.jpg")

    ; Clean up resources
    _GDIPlus_ImageDispose($hImage1)
    _GDIPlus_ImageDispose($hImage2)
    _WinAPI_DeleteObject($hBitmap1)
    _WinAPI_DeleteObject($hBitmap2)

    ; Shut down GDI+ library
    _GDIPlus_Shutdown()
    ShellExecute(@MyDocumentsDir & "\GDIPlus_Image.jpg")
EndFunc ;==>_Main

The inserted image should be skewed, and enlarged.

Other GDIPlus functions:-

GdipShearMatrix

GdipScaleMatrix

GdipSetMatrixElements

GdipGetMatrixElements

GdipMultiplyMatrix (not tested),

GdipInvertMatrix (not tested) .

When testing, this format was used,

$m11 = 0

$m12 = 1

$m21 = 0

$m22 = 1

$dx =100

$dy = 50

$hMatrix = _GDIPlus_MatrixCreate()

_GDIPlus_ShearMatrix($hMatrix, .5, -1)

_GDIPlus_ScaleMatrix($hMatrix, 3, 1)

;_GDIPlus_SetMatrixElements($hMatrix, $m11, $m12, $m21, $m22, $dx, $dy)

;_GDIPlus_MatrixRotate($hMatrix, 45, "False")

_GDIPlus_GraphicsSetTransform($hGraphic , $hMatrix)

$elem = _GDIPlus_GetMatrixElements ($hMatrix)

ConsoleWrite("$elem[0]=" & _WinAPI_IntToFloat($elem[0]) & " $elem[1]=" & _WinAPI_IntToFloat($elem[1]) & _

" $elem[2]=" & _WinAPI_IntToFloat($elem[2]) & " $elem[3]=" & _WinAPI_IntToFloat($elem[3]) & " $elem[4]=" _

& _WinAPI_IntToFloat($elem[4]) & " $elem[5]=" & _WinAPI_IntToFloat($elem[5]) & @CRLF)

The following is th DllCall functions mentioned above.

CODE#include <GDIPlus.au3>

;Matrix::SetElements Method

;The SetElements method sets the elements of this matrix.

;Parameters

; m11 [in] Real number that specifies the element in the first row, first column.

; m12 [in] Real number that specifies the element in the first row, second column.

; m21 [in] Real number that specifies the element in the second row, first column.

; m22 [in] Real number that specifies the element in the second row, second column.

; dx [in] Real number that specifies the element in the third row, first column.

; dy [in] Real number that specifies the element in the third row, second column.

Func _GDIPlus_SetMatrixElements($hMatrix, $nm11, $nm12, $nm21, $nm22, $ndx, $ndy)

Local $im11, $im12, $im21, $im22, $idx, $idy, $aResult

$im11 = _WinAPI_FloatToInt($nm11)

$im12 = _WinAPI_FloatToInt($nm12)

$im21 = _WinAPI_FloatToInt($nm21)

$im22 = _WinAPI_FloatToInt($nm22)

$idx = _WinAPI_FloatToInt($ndx)

$idy = _WinAPI_FloatToInt($ndy)

$aResult = DllCall($ghGDIPDll, "int", "GdipSetMatrixElements", "hwnd", $hMatrix, "int", $im11, "int", $im12, _

"int", $im21, "int", $im22, "int", $idx, "int", $idy)

Return SetError($aResult[0], 0, $aResult[0] = 0)

EndFunc ;==>_GDIPlus_SetMatrixElements

; GdipShearMatrix(

; GpMatrix *matrix,

; REAL shearX,

; REAL shearY,

; GpMatrixOrder order

; );

Func _GDIPlus_ShearMatrix($hMatrix, $nshearX, $nshearY, $fAppend = False)

Local $ishearX, $ishearY, $aResult

$ishearX = _WinAPI_FloatToInt($nshearX)

$ishearY = _WinAPI_FloatToInt($nshearY)

$aResult = DllCall($ghGDIPDll, "int", "GdipShearMatrix", "hwnd", $hMatrix, "int", $ishearX, "int", $ishearY, "int", $fAppend)

Return SetError($aResult[0], 0, $aResult[0] = 0)

EndFunc ;==>_GDIPlus_ShearMatrix

; GdipMultiplyMatrix(

; GpMatrix *matrix,

; GpMatrix* matrix2,

; GpMatrixOrder order

; );

Func _GDIPlus_MultiplyMatrix($hMatrix, $hMatrix2, $fAppend = False)

Local $aResult

$aResult = DllCall($ghGDIPDll, "int", "GdipMultiplyMatrix", "hwnd", $hMatrix, "hwnd", $hMatrix2, "int", $fAppend)

Return SetError($aResult[0], 0, $aResult[0] = 0)

EndFunc ;==>_GDIPlus_MultiplyMatrix

; GdipInvertMatrix(

; GpMatrix *matrix

; );

Func _GDIPlus_InvertMatrix($hMatrix)

Local $aResult

$aResult = DllCall($ghGDIPDll, "int", "GdipInvertMatrix", "hwnd", $hMatrix)

Return SetError($aResult[0], 0, $aResult[0] = 0)

EndFunc ;==>_GDIPlus_InvertMatrix

; GdipScaleMatrix(

; GpMatrix *matrix,

; REAL scaleX,

; REAL scaleY,

; GpMatrixOrder order

; );

Func _GDIPlus_ScaleMatrix($hMatrix, $nscaleX, $nscaleY, $fAppend = False)

Local $iscaleX, $iscaleY, $aResult

$iscaleX = _WinAPI_FloatToInt($nscaleX)

$iscaleY = _WinAPI_FloatToInt($nscaleY)

$aResult = DllCall($ghGDIPDll, "int", "GdipScaleMatrix", "hwnd", $hMatrix, "int", $iscaleX, "int", $iscaleY, "int", $fAppend)

Return SetError($aResult[0], 0, $aResult[0] = 0)

EndFunc ;==>_GDIPlus_ScaleMatrix

; Description Gets the elements of this matrix. The elements are placed in an array in the order m11, m12, m21, m22, m31, m32,

; where mij denotes the element in row i, column j.

; DECLARE FUNCTION GdipGetMatrixElements ( _

; BYVAL matrix AS DWORD, _

; BYREF matrixOut AS SINGLE _

; ) AS LONG

;Parameters

;matrix [in] Pointer to the Matrix object.

;matrixOut [out] Pointer to an array that receives the matrix elements. The size of the array should be 6 sizeof(REAL), i.e. 24.

Func _GDIPlus_GetMatrixElements ($hMatrix)

Local $iElem, $pElem, $tElem, $aElem[6]

$tElem = DllStructCreate("int;int;int;int;int;int;int;int")

$pElem = DllStructGetPtr($tElem)

$iElem = DllStructGetSize($tElem)

DllStructSetData($tElem, 1, $iElem)

DllCall($ghGDIPDll, "none", "GdipGetMatrixElements", "hwnd", $hMatrix, "ptr", $pElem)

$aElem[0] = _WinAPI_IntToFloat(DllStructGetData($tElem, 1))

$aElem[1] = _WinAPI_IntToFloat(DllStructGetData($tElem, 2))

$aElem[2] = _WinAPI_IntToFloat(DllStructGetData($tElem, 3))

$aElem[3] = _WinAPI_IntToFloat(DllStructGetData($tElem, 4))

$aElem[4] = _WinAPI_IntToFloat(DllStructGetData($tElem, 5))

$aElem[5] = _WinAPI_IntToFloat(DllStructGetData($tElem, 6))

Return $aElem

EndFunc ;==>__GDIPlus_GetMatrixElements

I put the ShearMatrix, and ScaleMatrix functions and others here so that if anyone searching the forums for them should find them. If I had found them in my forum search, I would have used them. My rejection of them would have been much faster.

Edited by Malkey
Link to comment
Share on other sites

A less simple example of the _GDIPlus_DrawImagePoints function plus matrix multiipication. Note the similarity to

http://www.autoitscript.com/forum/index.ph...st&p=495801

Drag Left mouse up or down Rotates around the X-axis.

Drag Left mouse left or right Rotates around the Y-axis.

About line 10, the image file path, and file name can be altered if needed.

The flicker is caused by the "GUISetBkColor(0xFFFFF0)" which erases the previous image with vertical movement of the mouse. Horizontal movement of the dragging mouse only, does not erase the previous image.

If too annoying, comment it out at line 43. And set the background color to suit the image.

#include <GUIConstants.au3>
#include <GuiConstantsEx.au3>
#include <GDIPlus.au3>
#include <Misc.au3>

Global Const $nPI = 3.1415926535897932384626433832795
Global $user32_dll = DllOpen("user32.dll")
Global $fact = 200, $angYaxis = 0, $angXaxis = 0
Global $pts[3][3] = [[ -$fact / 2, -$fact / 2, 0], [$fact / 2, -$fact / 2, 0], [ -$fact / 2, $fact / 2, 0]]

Global $Path_Name_Image_Fife = "C:\Program Files\AutoIt3\Examples\GUI\logo4.gif"

Global $hGUI
Local $hGraph, $mouseDiffX, $mouseDiffY, $msg
$pts = MoveCenterPlus()
$hGUI = GUICreate("3d Display", $fact * 2, $fact * 2)
GUISetBkColor(0xFFFFF0)
GUISetState()
Draw2D()
While 1 ; ESC key
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    If _IsPressed("01", $user32_dll) Then ; Virtual-Key Code (0x01) Left mouse button
    $angYaxis = 0
    $mouseDiffX = 0
    $MouseStartPos = MouseGetPos()
    While _IsPressed("01", $user32_dll) ; Virtual-Key Code (0x01) Left mouse button
    $pos = MouseGetPos()
    $mouseDiffX = Int(($pos[0] - $MouseStartPos[0]) / 10)
    $mouseDiffY = Int(($pos[1] - $MouseStartPos[1]) / 10)
    If Abs($mouseDiffX) > 0 Then
    $angXaxis = Mod($mouseDiffX, 360)
    $pts = MoveCenterMinus()
    $pts = rotateYaxis(-$angXaxis * $nPI / 180)
    $pts = MoveCenterPlus()
    ;GUISetBkColor(0xFFFFF0)
    Draw2D()
    EndIf
    If Abs($mouseDiffY) > 0 Then
    $angYaxis = Mod($mouseDiffY, 360)
    $pts = MoveCenterMinus()
    $pts = rotateXaxis($angYaxis * $nPI / 180)
    $pts = MoveCenterPlus()
    GUISetBkColor(0xFFFFF0)
    Draw2D()
    EndIf
    ;ConsoleWrite("$angYaxis=" & $angYaxis & "  $angXaxis=" & $angXaxis & @CRLF)
    Sleep(50)
    WEnd
    EndIf ; mouse BTN 1 pressed??
    Sleep(10)
WEnd

Func rotateYaxis($ang)
    Local $RotYMat[3][3] = [[ Cos($ang), 0, Sin($ang) ], [ 0, 1, 0 ], [ -Sin($ang), 0, Cos($ang) ]]
    $pts = ArrayProduct($pts, $RotYMat)
    Return $pts
EndFunc ;==>rotateYaxis

Func rotateXaxis($ang)
    Local $RotXMat[3][3] = [[1, 0, 0], [0, Cos($ang), -Sin($ang) ], [0, Sin($ang), Cos($ang) ]]
    $pts = ArrayProduct($pts, $RotXMat)
    Return $pts
EndFunc ;==>rotateXaxis

Func MoveCenterPlus()   ; Moves origin to center of GUI window to display rotation.
    Local $MatAdd[1][3] = [[$fact, $fact, 0]]
    $pts = matrixplusmatrix($pts, $MatAdd)
    Return $pts
EndFunc ;==>MoveCenterPlus

Func MoveCenterMinus()  ; Moves origin to top left corner for rotation calculation
    Local $MatMinus[1][3] = [[ -$fact, -$fact, 0]]
    $pts = matrixplusmatrix($pts, $MatMinus)
    Return $pts
EndFunc ;==>MoveCenterMinus

Func Draw2D()
    Local $hBitmap, $hGraphic, $hImage

    ; Initialize GDI+ library
    _GDIPlus_Startup ()

    ; Draw bitmap to GUI
    $hBitmap = _GDIPlus_BitmapCreateFromFile ($Path_Name_Image_Fife)
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND ($hGUI)
    _GDIPlus_DrawImagePoints($hGraphic, $hBitmap, $pts[0][0], $pts[0][1], $pts[1][0], $pts[1][1], $pts[2][0], $pts[2][1])

    ; Clean up resources
    _GDIPlus_GraphicsDispose ($hGraphic)
    _GDIPlus_ImageDispose ($hBitmap)
    _WinAPI_DeleteObject ($hBitmap)
    ;_GDIPlus_MatrixDispose($hMatrix)

    ; Shut down GDI+ library
    _GDIPlus_ShutDown ()

EndFunc ;==>Draw2D


;==========================================================================
; Function: ArrayProduct
;==========================================================================
Func ArrayProduct($avA, $avB, $m = 0)
    ; Check for parameter errors.
    If IsArray($avA) = 0 Or IsArray($avB) = 0 Then Return SetError(1, 1, 0) ; both must be arrays
    If UBound($avA, 0) <> 2 Or UBound($avB, 0) <> 2 Then Return SetError(1, 2, 0) ; both must be 2D arrays
    If UBound($avA, 2) <> UBound($avB) Then Return SetError(1, 3, 0) ; depth must match

    ; Create return array
    Local $iRows = UBound($avA), $iCols = UBound($avB, 2), $iDepth = UBound($avA, 2)
    Local $avRET[$iRows][$iCols]

    ; Calculate values
    For $r = 0 To $iRows - 1
    For $c = 0 To $iCols - 1
    $x = 0
    For $z = 0 To $iDepth - 1
    $x += ($avA[$r][$z] * $avB[$z][$c])
    Next
    $avRET[$r][$c] = $x
    Next
    Next
    Return $avRET
EndFunc ;==>ArrayProduct


; matrix $matr1 plus matrix $matr2
Func matrixplusmatrix($matr1, $matr2)
    If UBound($matr1, 2) <> UBound($matr2) And UBound($matr2, 2) <> UBound($matr1) Then
    SetError(0)
    Return -1
    EndIf
    Local $r = UBound($matr1), $cols = UBound($matr1, 2)
    Local $m[$r][$cols]
    For $i = 0 To $r - 1
    For $j = 0 To $cols - 1
    $m[$i][$j] = $matr1[$i][$j] + $matr2[0][$j]
    Next
    Next
    Return $m
EndFunc ;==>matrixplusmatrix

Just another example.

Edited by Malkey
Link to comment
Share on other sites

Hi,

I like it. Thanks for sharing.

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

  • 1 year later...

Here is another example using the _GDIPlus_DrawImagePoints function.

Each of the three points that are used in the _GDIPlus_DrawImagePoints function are able to be dragged. This distorts the image.

Using the context menu (right click on the GUI), an image can be loaded, saved, flipped, and rotated.

Enjoy.

#include <GDIPlus.au3>
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <misc.au3>
#include <GuiMenu.au3>
#include <Array.au3>
#include <Math.au3>

Global $hGui, $hGraphicGUI, $hBMPBuff, $hImage
Global $iPath = @ProgramFilesDir & "\AutoIt3\Examples\GUI\logo4.gif"    ; <== Initial image
Global $dll = DllOpen("user32.dll")
Global $GuiSize = @DesktopWidth
Global $msg, $exit, $moveAll, $viewPoints, $pointsVisible = 1
Global $PicturePoints[4][2] = [[3, 0],[200, 200],[369, 200],[200, 268]]
Global $PolyPoints[5][2], $Rect = DllStructCreate($tagRECT), $Save = False
Global $FileNameSave = @DesktopCommonDir & "\Poly.png"
Global $hBrushRed, $hBrushGreen, $hBrush, $hPen, $hGraphic

Opt("GUIOnEventMode", 1)

Pict3Points()

Func Pict3Points()
    Local $0nPoint, $mouseDiffX, $mouseDiffY, $MouseStartPos, $pos
    $hGui = GUICreate("Points", $GuiSize / 2, $GuiSize / 2, 0, 0, $WS_SIZEBOX);,0x2000000, $WS_EX_TOPMOST)
    GUISetOnEvent($GUI_EVENT_CLOSE, "quitclk")

    ;GUIRegisterMsg($WM_LBUTTONDOWN, "_WinMove")    ; Drag Window 2 of 3 addin
    $ContextMenu = GUICtrlCreateContextMenu()
    $viewPoints = GUICtrlCreateMenuItem("Show points", $ContextMenu)
    GUICtrlSetState($viewPoints, $GUI_CHECKED)
    GUICtrlSetOnEvent($viewPoints, "ShowaPoints")
    $OpenImage = GUICtrlCreateMenuItem("Open/Load Image", $ContextMenu)
    GUICtrlSetOnEvent($OpenImage, "OpenImage")
    $SaveImage = GUICtrlCreateMenu("Save Image", $ContextMenu)
    $SaveImageAsSet = GUICtrlCreateMenuItem("Save as " & $FileNameSave, $SaveImage)
    GUICtrlSetOnEvent($SaveImageAsSet, "SaveImage")
    $SaveImageAs = GUICtrlCreateMenuItem("Save as Other", $SaveImage)
    GUICtrlSetOnEvent($SaveImageAs, "SaveImageAS")
    $Currentpoints = GUICtrlCreateMenu("Points to Console", $ContextMenu)
    $SunMenu1 = GUICtrlCreateMenuItem("Write current points to Console", $Currentpoints)
    GUICtrlSetOnEvent($SunMenu1, "PicturePoints2Console")
    $SunMenu2 = GUICtrlCreateMenuItem("Write current polygon to Console", $Currentpoints)
    GUICtrlSetOnEvent($SunMenu2, "PolyPoints2Console")
    $SetOpts = GUICtrlCreateMenu("Flip/FlipUp", $ContextMenu)
    $Flip = GUICtrlCreateMenuItem("Flip Image", $SetOpts)
    GUICtrlSetOnEvent($Flip, "Flip")
    $FlipUp = GUICtrlCreateMenuItem("Flip-Up Image", $SetOpts)
    GUICtrlSetOnEvent($FlipUp, "FlipUp")
    $Rotate = GUICtrlCreateMenuItem("Rotate 90deg", $SetOpts)
    GUICtrlSetOnEvent($Rotate, "Rotate90")
    $MakePt1_90deg = GUICtrlCreateMenuItem("Make Point1 90deg", $SetOpts)
    GUICtrlSetOnEvent($MakePt1_90deg, "MakePt1_90deg")
    $separator1 = GUICtrlCreateMenuItem("", $ContextMenu) ; create a separator line
    $MenuItemExit = GUICtrlCreateMenuItem("Exit", $ContextMenu)
    GUICtrlSetOnEvent($MenuItemExit, "quitclk")

    _GDIPlus_Startup()
    $hGraphicGUI = _GDIPlus_GraphicsCreateFromHWND($hGui)
    $hBMPBuff = _GDIPlus_BitmapCreateFromGraphics($GuiSize, $GuiSize, $hGraphicGUI)
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hBMPBuff)
    $hImage = _GDIPlus_ImageLoadFromFile($iPath)
    $hBrushRed = _GDIPlus_BrushCreateSolid(0x8fff0000) ;red
    $hBrushGreen = _GDIPlus_BrushCreateSolid(0x8f00ff00) ;Green
    $hBrush = _GDIPlus_BrushCreateSolid(0xFFFFFFCC)
    $hPen = _GDIPlus_PenCreate(0x00FFFF00, 5)

    GUISetState()
    Draw(0)
    GUIRegisterMsg(0x0085, "MY_PAINT")
    GUIRegisterMsg(0x000F, "MY_PAINT")
    _WinAPI_RedrawWindow($hGui, 0, 0, 2)
    Do
        If _IsPressed("01", $dll) Then ; Virtual-Key Code (0x01) Left mouse button
            $MouseStartPos = GUIGetCursorInfo($hGui) ; MouseGetPos()
            $0nPoint = CheckOverPoint($MouseStartPos[0], $MouseStartPos[1])
            $mouseDiffX = 0
            If $0nPoint > 0 Then
                ToolTip("Point:" & $0nPoint & " X:" & $PicturePoints[$0nPoint][0] & _
                        " Y:" & $PicturePoints[$0nPoint][1], ClientToScrn($hGui, $PicturePoints[$0nPoint][0], -1) - 30, _
                        ClientToScrn($hGui, -1, $PicturePoints[$0nPoint][1]) - 25)
                $moveAll = 1
                Draw($0nPoint)
                While _IsPressed("01", $dll) ; Virtual-Key Code (0x01) Left mouse button
                    $pos = GUIGetCursorInfo($hGui) ; MouseGetPos()
                    $mouseDiffX = Int(($pos[0] - $MouseStartPos[0]))
                    $mouseDiffY = Int(($pos[1] - $MouseStartPos[1]))
                    If Abs($mouseDiffX) > 0 Or Abs($mouseDiffY) > 0 Then
                        $PicturePoints[$0nPoint][0] = $pos[0]
                        $PicturePoints[$0nPoint][1] = $pos[1]
                        Draw($0nPoint)
                        ToolTip("Point:" & $0nPoint & " X:" & $PicturePoints[$0nPoint][0] & _
                                " Y:" & $PicturePoints[$0nPoint][1], ClientToScrn($hGui, $PicturePoints[$0nPoint][0], -1) - 30, _
                                ClientToScrn($hGui, -1, $PicturePoints[$0nPoint][1]) - 25)
                        Sleep(200)
                    EndIf
                    Sleep(20)
                WEnd
                ToolTip("")
            Else
                $moveAll = 0
                Draw(0)
            EndIf
        EndIf ; mouse BTN 1 pressed??
        If _IsPressed("1B", $dll) Then $exit = 1; ESC key
        Sleep(20)
    Until $exit = 1
    DllClose($dll)
EndFunc ;==>Pict3Points


;Check if mouse pointer is over a red/green spot
Func CheckOverPoint($MouseX, $MouseY)
    Local $x, $retValue = 0
    For $x = 1 To $PicturePoints[0][0]
        If $MouseX < $PicturePoints[$x][0] + 5 And $MouseX > $PicturePoints[$x][0] - 5 And _
                $MouseY < $PicturePoints[$x][1] + 5 And $MouseY > $PicturePoints[$x][1] - 5 Then
            $retValue = $x
        EndIf
    Next
    Return $retValue
EndFunc ;==>CheckOverPoint

Func Draw($pt = 0)
    $Rect = PolyPts()
    _GDIPlus_GraphicsClear($hGraphic, 0x00FFFFFF)
    _GDIPlus_DrawImagePoints($hGraphic, $hImage, $PicturePoints[1][0], $PicturePoints[1][1], $PicturePoints[2][0], $PicturePoints[2][1], $PicturePoints[3][0], $PicturePoints[3][1])
    If $pointsVisible = 1 Then
        For $x = 1 To $PicturePoints[0][0]
            If $x = $pt Then
                _GDIPlus_GraphicsFillEllipse($hGraphic, $PicturePoints[$x][0] - 5, $PicturePoints[$x][1] - 5, 10, 10, $hBrushGreen)
            Else
                _GDIPlus_GraphicsFillEllipse($hGraphic, $PicturePoints[$x][0] - 5, $PicturePoints[$x][1] - 5, 10, 10, $hBrushRed) ;this is moveable
            EndIf
            _GDIPlus_GraphicsDrawEllipse($hGraphic, $PicturePoints[$x][0] - 5, $PicturePoints[$x][1] - 5, 10, 10)
        Next
    EndIf
    _GDIPlus_GraphicsDrawPolygon($hGraphic, $PolyPoints, $hPen)

    _WinAPI_RedrawWindow($hGui, $Rect, 0, 0x02) ;PAINT the window Flag $RDW_INTERNALPAINT = 0x0002

    If $Save Then
        $Bmp = _GDIPlus_BitmapCloneArea($hBMPBuff, DllStructGetData($Rect, "Left"), _
                DllStructGetData($Rect, "Top"), DllStructGetData($Rect, "Right") - DllStructGetData($Rect, "Left"), _
                DllStructGetData($Rect, "Bottom") - DllStructGetData($Rect, "Top"), $GDIP_PXF32ARGB)
        _GDIPlus_ImageSaveToFile($Bmp, $FileNameSave)
        ShellExecute($FileNameSave)
        _WinAPI_DeleteObject($Bmp)
        $Save = False
    EndIf
    Return
EndFunc ;==>Draw

;Called from context menu check / uncheck Show Points
Func ShowaPoints()
    If $pointsVisible = 1 Then
        GUICtrlSetState($viewPoints, $GUI_UNCHECKED)
        $pointsVisible = 0
    Else
        GUICtrlSetState($viewPoints, $GUI_CHECKED)
        $pointsVisible = 1
    EndIf
    Draw(0)
EndFunc ;==>ShowaPoints

Func Flip() ; Based on the 3 points used in _GDIPlus_DrawImagePoints()
    Local $CentX = $PicturePoints[3][0] + ($PicturePoints[2][0] - $PicturePoints[3][0]) / 2
    Local $CentY = $PicturePoints[2][1] + ($PicturePoints[3][1] - $PicturePoints[2][1]) / 2

    $PicturePoints[1][0] = ($CentX - $PicturePoints[1][0]) + $CentX
    $PicturePoints[2][0] = $CentX - ($PicturePoints[2][0] - $CentX)
    $PicturePoints[3][0] = ($CentX - $PicturePoints[3][0]) + $CentX
    Draw()
    Return
EndFunc ;==>Flip

Func Rotate90()
    ; Left Hand Axis Rule - Put left hand in front of monitor with thumb pointing down,
    ; pointer finger pointing to the right, and middle finger pointing into monitor.
    ; Thumb is positive y-axis direction, pointer finger is positive x-axis direction, and middle finger is z-axis.
    ; If middle finger, positive z-axis was away from monitor, then it would have been the Right Hand Rule.
    ;TRANSFORMATION MATRICES
    ;$ang = $andDeg * 4 * ATan(1) / 180
    ;Local $Rotate_about_x_axis[3][3] = [[1, 0, 0],[0, Cos($ang), -Sin($ang)],[0, Sin($ang), Cos($ang)]] ; Pointer finger
    ;Local $Rotate_about_y_axis[3][3] = [[Cos($ang), 0, Sin($ang)],[0, 1, 0],[-Sin($ang), 0, Cos($ang)]] ; Thumb
    ;Local $Rotate_about_z_axis[3][3] = [[Cos($ang), -Sin($ang), 0],[Sin($ang), Cos($ang), 0],[0, 0, 1]] ; Middle finger

    ; $Rotate_about_z_axis_90deg[3][3] = [[Cos(90), -Sin(90), 0],[Sin(90), Cos(90), 0],[0, 0, 1]]
    ; The above line is equivalent to the next line because Sin(90) = 1 , and, Cos(90) = 0.
    Local $RotZ90[3][3] = [[0, -1, 0],[1, 0, 0],[0, 0, 1]]

    ;ConsoleWrite("Sin(90) = " & sin(90*4*ATan(1)/180) & @CRLF) ; Check that Sin(90) = 1.

    ;Find $CentX, $CentX - the centre of image. (diagonal/2)
    Local $CentX = $PicturePoints[3][0] + ($PicturePoints[2][0] - $PicturePoints[3][0]) / 2
    Local $CentY = $PicturePoints[2][1] + ($PicturePoints[3][1] - $PicturePoints[2][1]) / 2

    PolyPts() ; Update $PolyPoints array
    ;Convert the $PolyPoints array to accept 3D point coordinates. i.e. x, y, z values per point.
    Local $aPPts[5][3] = [[$PolyPoints[0][0], $PolyPoints[0][1], 0], _
            [$PolyPoints[1][0], $PolyPoints[1][1], 0], _
            [$PolyPoints[2][0], $PolyPoints[2][1], 0], _
            [$PolyPoints[3][0], $PolyPoints[3][1], 0], _
            [$PolyPoints[4][0], $PolyPoints[4][1], 0]]

    ; Take $CentX and $CentY off each polygon point. Equivalent to making centre ot image the origin, point (0, 0).
    Local $aPPtsMod = ArrayXYZplusScalarPoint($aPPts, -$CentX, -$CentY, 0)

    ; _ArrayProduct produces another array of polygon points which have been rotated 90deg about x-axis, around the origin.
    Local $TempPtsBe = _ArrayProduct($aPPtsMod, $RotZ90)

    ; Add $CentX and $CentY to all polygon points. This moves the polygon back to its original location.
    Local $TempPts = ArrayXYZplusScalarPoint($TempPtsBe, $CentX, $CentY)

    ;Extract the 3 points used in _GDIPlus_DrawImagePoints() from the image's rotated polygon points.
    $PicturePoints[1][0] = $TempPts[1][0]
    $PicturePoints[1][1] = $TempPts[1][1]
    $PicturePoints[2][0] = $TempPts[2][0]
    $PicturePoints[2][1] = $TempPts[2][1]
    $PicturePoints[3][0] = $TempPts[4][0]
    $PicturePoints[3][1] = $TempPts[4][1]

    Draw() ; Re-draw image
    Return
EndFunc ;==>Rotate90

Func FlipUp() ; Based on the 3 points used in _GDIPlus_DrawImagePoints()
    PolyPts()
    $PicturePoints[1][0] = $PolyPoints[4][0]
    $PicturePoints[1][1] = $PolyPoints[4][1]
    $PicturePoints[2][0] = $PolyPoints[3][0]
    $PicturePoints[2][1] = $PolyPoints[3][1]
    $PicturePoints[3][0] = $PolyPoints[1][0]
    $PicturePoints[3][1] = $PolyPoints[1][1]
    ;_ArrayDisplay($TempPts)
    Draw()
    Return
EndFunc ;==>FlipUp

Func ArrayXYZplusScalarPoint($pts, $x = 0, $y = 0, $z = 0)
    Dim $aRET[UBound($pts)][3]
    For $r = 0 To UBound($pts) - 1
        $aRET[$r][0] = $pts[$r][0] + $x
        $aRET[$r][1] = $pts[$r][1] + $y
        $aRET[$r][2] = $pts[$r][2] + $z
    Next
    $aRET[0][0] = $pts[0][0]
    Return $aRET
EndFunc ;==>ArrayXYZplusScalarPoint

Func _ArrayProduct($avA, $avB)
    ; Check for parameter errors.
    If IsArray($avA) = 0 Or IsArray($avB) = 0 Then Return SetError(1, 1, 0) ; both must be arrays
    If UBound($avA, 0) <> 2 Or UBound($avB, 0) <> 2 Then Return SetError(1, 2, 0) ; both must be 2D arrays
    If UBound($avA, 2) <> UBound($avB) Then Return SetError(1, 3, 0) ; depth must match
    ; Create return array
    Local $iRows = UBound($avA), $iCols = UBound($avB, 2), $iDepth = UBound($avA, 2)
    Local $avRET[$iRows][$iCols]
    ; Calculate values
    For $r = 0 To $iRows - 1
        For $c = 0 To $iCols - 1
            $x = 0
            For $z = 0 To $iDepth - 1
                $x += ($avA[$r][$z] * $avB[$z][$c])
            Next
            $avRET[$r][$c] = $x
        Next
    Next
    Return $avRET
EndFunc ;==>_ArrayProduct

Func MakePt1_90deg() ; Based on the 3 points used in _GDIPlus_DrawImagePoints()
    $PicturePoints[2][1] = $PicturePoints[1][1]
    $PicturePoints[3][0] = $PicturePoints[1][0]
    Draw()
    Return
EndFunc ;==>MakePt1_90deg

Func PolyPts()
    $PolyPoints[0][0] = 4
    $PolyPoints[1][0] = $PicturePoints[1][0]
    $PolyPoints[1][1] = $PicturePoints[1][1]
    $PolyPoints[2][0] = $PicturePoints[2][0]
    $PolyPoints[2][1] = $PicturePoints[2][1]
    $PolyPoints[3][0] = $PicturePoints[2][0] + $PicturePoints[3][0] - $PicturePoints[1][0]
    $PolyPoints[3][1] = $PicturePoints[2][1] + $PicturePoints[3][1] - $PicturePoints[1][1]
    $PolyPoints[4][0] = $PicturePoints[3][0]
    $PolyPoints[4][1] = $PicturePoints[3][1]
    DllStructSetData($Rect, "Left", _Min(_Min($PolyPoints[1][0], $PolyPoints[2][0]), _Min($PolyPoints[3][0], $PolyPoints[4][0])))
    DllStructSetData($Rect, "Top", _Min(_Min($PolyPoints[1][1], $PolyPoints[2][1]), _Min($PolyPoints[3][1], $PolyPoints[4][1])))
    DllStructSetData($Rect, "Right", _Max(_Max($PolyPoints[1][0], $PolyPoints[2][0]), _Max($PolyPoints[3][0], $PolyPoints[4][0])))
    DllStructSetData($Rect, "Bottom", _Max(_Max($PolyPoints[1][1], $PolyPoints[2][1]), _Max($PolyPoints[3][1], $PolyPoints[4][1])))
    ;_ArrayDisplay($PolyPoints)
    Return $Rect
EndFunc ;==>PolyPts

Func PolyPoints2Console()
    PolyPts()
    ConsoleWrite("Global $PolyPoints[" & $PolyPoints[0][0] + 1 & "][2] = [[" & $PolyPoints[0][0] & ",0]")
    For $x = 1 To $PolyPoints[0][0]
        ConsoleWrite(",[" & $PolyPoints[$x][0] & "," & $PolyPoints[$x][1] & "]")
    Next
    ConsoleWrite("]" & @CRLF)
EndFunc ;==>PolyPoints2Console

;Called from context menu Write Current points to console
Func PicturePoints2Console()
    ConsoleWrite("Global $PicturePoints[" & $PicturePoints[0][0] + 1 & "][2] = [[" & $PicturePoints[0][0] & ",0]")
    For $x = 1 To $PicturePoints[0][0]
        ConsoleWrite(",[" & $PicturePoints[$x][0] & "," & $PicturePoints[$x][1] & "]")
    Next
    ConsoleWrite("]" & @CRLF)
EndFunc ;==>PicturePoints2Console


Func SaveImageAs()
    Local $var = FileSaveDialog("Choose a name.", @DesktopCommonDir, "Images (*.png)", 2, "Poly.png")
    If @error Then
        Return
    Else
        $FileNameSave = $var
        $Save = True
        Draw()
    EndIf
EndFunc ;==>SaveImageAs

Func SaveImage()
    $Save = True
    Draw()
EndFunc ;==>SaveImage

Func OpenImage()
    Local $var = FileOpenDialog("Choose a name.", @DesktopCommonDir, "Images (*.png)", 2, "Poly.png")
    If @error Then
        Return
    Else
        $iPath = $var
        $hImage = _GDIPlus_ImageLoadFromFile($iPath)
        Draw()
    EndIf
EndFunc ;==>OpenImage

; Modified _WinAPI_ClientToScreen() from include file WinAPI.au3.
Func ClientToScrn($hWnd, $x = 0, $y = 0)
    Local $aResult, $tPoint = DllStructCreate("int X;int Y")
    DllStructSetData($tPoint, "X", $x)
    DllStructSetData($tPoint, "Y", $y)
    $aResult = DllCall("User32.dll", "int", "ClientToScreen", "hwnd", $hWnd, "ptr", DllStructGetPtr($tPoint))
    If @error Then Return SetError(@error, 0, False)
    If $x = 0 Or $x = -1 Then
        $ret = DllStructGetData($tPoint, "Y")
    Else
        $ret = DllStructGetData($tPoint, "X")
    EndIf
    Return $ret
EndFunc ;==>ClientToScrn

; Called from context menu Exit script.
Func quitclk()
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_PenDispose($hPen)
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_BrushDispose($hBrushRed)
    _GDIPlus_BrushDispose($hBrushGreen)
    _WinAPI_DeleteObject($hBMPBuff)
    _GDIPlus_GraphicsDispose($hGraphicGUI)
    _GDIPlus_Shutdown()
    $exit = 1 ; Chr(27) ;
EndFunc ;==>quitclk

;Func to redraw the BMP on PAINT MSG
Func MY_PAINT($hWnd, $msg, $wParam, $lParam)
    If $hWnd = $hGui Then
        _WinAPI_RedrawWindow($hGui, "", "", BitOR($RDW_INVALIDATE, $RDW_ERASE, $RDW_UPDATENOW))
        _GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff, 0, 0)
    EndIf
EndFunc ;==>MY_PAINT
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...