Jump to content

Maths help GdiPlus and Rotation


Recommended Posts

Hello

I'm trying to get a section of a jpg (shown on the attached image with a red box) the angle is 18 degrees.

I need to create a new bitmap of the red section but rotated -18 degrees so that the new bitmap is a straightened version of the rotated crop.

I'm assuming that I need to copy the area marked in Green do a -18degree rotation and past it on to the new blank bitmap with a minus X and minus Y value

This is where I suck at Maths and need some help.

I know the below code will not fully work on it's own but the whole script is rather long and complicated.

The ? values are where it's all going wrong I think, I can't seem to get my head around where the rotate is occurring and from what points.

Please help!

Thanks

$rotate = 18



$original = _GDIPlus_ImageLoadFromFile("testfile.jpg")

$bmp = _GDIPlus_BitmapCreateFromScan0($WidthOfRedBox,$HeightOfRedBox)
$gr = _GDIPlus_ImageGetGraphicsContext($bmp)



$m = _GDIPlus_MatrixCreate()



_GDIPlus_MatrixTranslate($m,?, ?)
_GDIPlus_MatrixRotate($m, -$rotate)
_GDIPlus_GraphicsSetTransform($gr, $m)

_GDIPlus_GraphicsDrawImageRectRect($gr, $original, $GreenBoxX, $GreenBoxY, $GreenBoxW, $GreenBoxH, ?,?,?,?)

_GDIPlus_MatrixDispose($m)



_GDIPlus_ImageSaveToFile($bmp,@DesktopDir & "\output.jpg")

post-7154-0-75672000-1395144203_thumb.jp

Link to comment
Share on other sites

  • Moderators

Hi, ChrisL. Just curious, with your long history on the forum, why you didn't post this in GH & S? You would definitely get more eyes on your problem over there.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

  • Moderators

Not a problem, just thinking how to get the most eyes on your problem. Might be worth PMing a Mod to ask them to move it :)

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Try this:

;coded by UEZ 2014
#include <Array.au3>
#include <GDIPlus.au3>

_GDIPlus_Startup()
;p1: 134, 47    (x1, y1)
;p2: 391, 132   (x2, y2)
;p3: 7, 434     (x3, y3)
;p4: 265, 516   (x4, y4)
;
; p1          p2
; +-----------+
; |           |
; |           |
; |           |
; +-----------+
; p4          p3

Global $hImage = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\Test.jpg")
Global $hBitmap_Result = _GDIPlus_ImageCropAndRotate($hImage, 134, 47, 391, 132, 265, 516, 7, 434, -18)
_GDIPlus_ImageSaveToFile($hBitmap_Result, @ScriptDir & "\Result.png")
_GDIPlus_BitmapDispose($hBitmap_Result)
_GDIPlus_Shutdown()
ShellExecute(@ScriptDir & "\Result.png")

Func _GDIPlus_ImageCropAndRotate($hImage, $iX1, $iY1, $iX2, $iY2, $iX3, $iY3, $iX4, $iY4, $fAngle) ;coded by UEZ build 2014-18-03
    Local $aCoordinatesX[4] = [$iX1, $iX2, $iX3, $iX4], $aCoordinatesY[4] = [$iY1, $iY2, $iY3, $iY4]
    _ArraySort($aCoordinatesX)
    _ArraySort($aCoordinatesY)
    Local Const $iW = $aCoordinatesX[3] - $aCoordinatesX[0], $iH = $aCoordinatesY[3] - $aCoordinatesY[0]
    Local Const $hBitmap_Mask = _GDIPlus_BitmapCreateFromScan0($iW, $iH)
    Local Const $hGfx_Mask = _GDIPlus_ImageGetGraphicsContext($hBitmap_Mask)
    Local Const $hPath_Mask = _GDIPlus_PathCreate(), $hBrush = _GDIPlus_BrushCreateSolid(0xFFFFFF00)
    _GDIPlus_PathAddLine($hPath_Mask, $iX1 - $aCoordinatesX[0], $iY1 - $aCoordinatesY[0], $iX2 - $aCoordinatesX[0], $iY2 - $aCoordinatesY[0])
    _GDIPlus_PathAddLine($hPath_Mask, $iX2 - $aCoordinatesX[0], $iY2 - $aCoordinatesY[0], $iX3 - $aCoordinatesX[0], $iY3 - $aCoordinatesY[0])
    _GDIPlus_PathAddLine($hPath_Mask, $iX3 - $aCoordinatesX[0], $iY3 - $aCoordinatesY[0], $iX4 - $aCoordinatesX[0], $iY4 - $aCoordinatesY[0])
    _GDIPlus_PathAddLine($hPath_Mask, $iX4 - $aCoordinatesX[0], $iY4 - $aCoordinatesY[0], $iX1 - $aCoordinatesX[0], $iY1 - $aCoordinatesY[0])
    _GDIPlus_GraphicsClear($hGfx_Mask, 0xFF000000)
    _GDIPlus_GraphicsFillPath($hGfx_Mask, $hPath_Mask, $hBrush)

    Local Const $hBitmap = _GDIPlus_BitmapCreateFromScan0($iW, $iH)
    Local Const $hGfx = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    Local Const $iWidth = _GDIPlus_ImageGetWidth($hImage), $iHeight = _GDIPlus_ImageGetHeight($hImage)
    _GDIPlus_GraphicsDrawImageRect($hGfx, $hImage,  -$aCoordinatesX[0], -$aCoordinatesY[0], $iWidth, $iHeight)

    Local Const $hIA = _GDIPlus_ImageAttributesCreate()
    Local $aRemapTable[2][2]
    $aRemapTable[0][0] = 1
    $aRemapTable[1][0] = 0xFFFFFF00
    $aRemapTable[1][1] = 0x00000000
    _GDIPlus_ImageAttributesSetRemapTable($hIA, 1, True, $aRemapTable)
    _GDIPlus_GraphicsDrawImageRectRect($hGfx, $hBitmap_Mask, 0, 0, $iW, $iH, 0, 0, $iW, $iH, $hIA)

    Local $iSize
    If $iW < $iH Then
        $iSize = $iH * 1.1
    Else
        $iSize = $iW * 1.1
    EndIf

    Local Const $hBitmap_Rotate = _GDIPlus_BitmapCreateFromScan0($iSize, $iSize)
    Local Const $hGfx_Rotate = _GDIPlus_ImageGetGraphicsContext($hBitmap_Rotate)
    _GDIPlus_GraphicsClear($hGfx_Rotate, 0xFF000000)

    Local Const $hMatrix = _GDIPlus_MatrixCreate()
    _GDIPlus_MatrixTranslate($hMatrix, $iSize / 2, $iSize / 2)
    _GDIPlus_MatrixRotate($hMatrix, $fAngle)
    _GDIPlus_GraphicsSetTransform($hGfx_Rotate, $hMatrix)
    _GDIPlus_GraphicsDrawImageRect($hGfx_Rotate, $hBitmap,  -$iW / 2, -$iH / 2, $iW, $iH)

    _GDIPlus_MatrixDispose($hMatrix)
    _GDIPlus_ImageAttributesDispose($hIA)
    _GDIPlus_PathDispose($hPath_Mask)
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_GraphicsDispose($hGfx_Mask)
    _GDIPlus_GraphicsDispose($hGfx_Rotate)
    _GDIPlus_GraphicsDispose($hGfx)
    _GDIPlus_BitmapDispose($hBitmap_Mask)
    _GDIPlus_BitmapDispose($hBitmap)
    Return $hBitmap_Rotate
EndFunc


Func _GDIPlus_ImageAttributesSetRemapTable($hImageAttributes, $iColorAdjustType = 0, $fEnable = False, $aColorMap = 0)
    Local $iI, $iCount, $tColorMap, $pColorMap, $aResult
    If IsArray($aColorMap) Then
        $iCount = $aColorMap[0][0]
        $tColorMap = DllStructCreate("uint[" & $iCount * 2 & "]")
        $pColorMap = DllStructGetPtr($tColorMap)
        For $iI = 1 To $iCount
            DllStructSetData($tColorMap, 1, $aColorMap[$iI][0], ($iI - 1) * 2 + 1)
            DllStructSetData($tColorMap, 1, $aColorMap[$iI][1], ($iI - 1) * 2 + 2)
        Next
        $aResult = DllCall($ghGDIPDll, "int", "GdipSetImageAttributesRemapTable", "handle", $hImageAttributes, "int", $iColorAdjustType, "int", $fEnable, "int", $iCount, "ptr", $pColorMap)
    Else
        $aResult = DllCall($ghGDIPDll, "int", "GdipSetImageAttributesRemapTable", "handle", $hImageAttributes, "int", $iColorAdjustType, "int", $fEnable, "int", 0, "ptr", 0)
    EndIf
    If @error Then Return SetError(@error, @extended, False)
    Return $aResult[0] = 0
EndFunc   ;==>_GDIPlus_ImageAttributesSetRemapTable

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

I modified the code in case that you are rotating the cropped image to any angle.

 

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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