Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include-once
- #include <GDIPlus.au3>
- Func _GDIPlus_FloodFillIter(ByRef $hBitmap, $iX, $iY, $iColorOld, $iColorNew) ;coded by UEZ 2013-01-12
- Local $aResult = DllCall($__g_hGDIPDll, "uint", "GdipGetImageDimension", "handle", $hBitmap, "float*", 0, "float*", 0)
- Local $iW = $aResult[2], $iH = $aResult[3]
- If BitOR($iX < 0, $iY < 0, $iX > $iW - 1, $iY > $iH - 1) Then Return SetError(1, 0, 0)
- Local $x, $y, $aPos, $sPoint, $c = 0
- Local $aStack[16384] ;init stack
- Local $iStack = 1
- $iColorOld = "0x" & Hex($iColorOld, 8)
- Push($aStack, $iX & ";" & $iY, $iStack)
- While $iStack > 1
- $sPoint = $aStack[$iStack - 1]
- Pop($iStack) ;pop
- $aPos = StringRegExp($sPoint, "(\d+);(\d+)", 3)
- $x = $aPos[0] ;StringRegExpReplace($sPoint, "(\d+);\d+", "$1")
- $y = $aPos[1] ;StringRegExpReplace($sPoint, "\d+;(\d+)", "$1")
- If BitOR($x < 0, $y < 0, $x > $iW - 1, $y > $iH - 1) Then ContinueLoop
- $aResult = DllCall($__g_hGDIPDll, "uint", "GdipBitmapGetPixel", "handle", $hBitmap, "int", $x, "int", $y, "uint*", 0)
- If $aResult[4] = $iColorOld Then
- DllCall($__g_hGDIPDll, "uint", "GdipBitmapSetPixel", "handle", $hBitmap, "int", $x, "int", $y, "uint", $iColorNew)
- Push($aStack, $x + 1 & ";" & $y, $iStack)
- Push($aStack, $x & ";" & $y + 1, $iStack)
- Push($aStack, $x - 1 & ";" & $y, $iStack)
- Push($aStack, $x & ";" & $y - 1, $iStack)
- $c += 1
- EndIf
- WEnd
- $aStack = 0
- If Not $c Then Return SetError(2, 0, 0)
- Return 1
- EndFunc ;==>_GDIPlus_FloodFillIter
- Func _GDIPlus_FloodFillIter2(ByRef $hBitmap, $iX, $iY, $iColorOld, $iColorNew) ;coded by UEZ 2013-01-12
- Local $aResult = DllCall($__g_hGDIPDll, "uint", "GdipGetImageDimension", "handle", $hBitmap, "float*", 0, "float*", 0)
- Local $iW = $aResult[2], $iH = $aResult[3]
- If BitOR($iX < 0, $iY < 0, $iX > $iW - 1, $iY > $iH - 1) Then Return SetError(1, 0, 0)
- Local $x, $y, $i = 1
- Local $oD = ObjCreate('Scripting.Dictionary')
- $oD.Add($i, $iX & ";" & $iY) ;push
- $i += 1
- While $oD.Count > 0
- $sPoint = $oD.Item($i - 1)
- $oD.Remove($i - 1) ;pop
- $i -= 1
- $x = Int(StringRegExpReplace($sPoint, "(\d+);\d+", "$1"))
- $y = Int(StringRegExpReplace($sPoint, "\d+;(\d+)", "$1"))
- If BitOR($x < 0, $y < 0, $x > $iW - 1, $y > $iH - 1) Then ContinueLoop
- $aResult = DllCall($__g_hGDIPDll, "uint", "GdipBitmapGetPixel", "handle", $hBitmap, "int", $x, "int", $y, "uint*", 0)
- If $aResult[4] = "0x" & Hex($iColorOld, 8) Then
- DllCall($__g_hGDIPDll, "uint", "GdipBitmapSetPixel", "handle", $hBitmap, "int", $x, "int", $y, "uint", $iColorNew)
- $oD.Add($i, $x + 1 & ";" & $y) ;push
- $i += 1
- $oD.Add($i, $x & ";" & $y + 1) ;push
- $i += 1
- $oD.Add($i, $x - 1 & ";" & $y) ;push
- $i += 1
- $oD.Add($i, $x & ";" & $y - 1) ;push
- $i += 1
- EndIf
- WEnd
- $oD = 0
- Return 1
- EndFunc ;==>_GDIPlus_FloodFillIter2
- Func _GDIPlus_FloodFillIter3(ByRef $hBitmap, $iX, $iY, $iColorOld, $iColorNew, $iTolerance = 0) ;coded by UEZ 2014-07-20
- Local $aResult = DllCall($__g_hGDIPDll, "uint", "GdipGetImageDimension", "handle", $hBitmap, "float*", 0, "float*", 0)
- Local $iW = $aResult[2], $iH = $aResult[3]
- If BitOR($iX < 0, $iY < 0, $iX > $iW - 1, $iY > $iH - 1) Then Return SetError(1, 0, 0)
- $iTolerance = ($iTolerance < 0) ? 0 : $iTolerance > 255 ? 255: $iTolerance
- Local $x, $y, $aPos, $sPoint, $c = 0
- Local $aStack[16384] ;init stack
- Local $iStack = 1
- Push($aStack, $iX & ";" & $iY, $iStack)
- Local $tBitmapData = _GDIPlus_BitmapLockBits($hBitmap, 0, 0, $iW, $iH, BitOR($GDIP_ILMWRITE, $GDIP_ILMREAD), $GDIP_PXF32ARGB)
- Local $iScan0 = DllStructGetData($tBitmapData, "Scan0")
- Local $tPixel = DllStructCreate("int[" & $iW * $iH & "];", $iScan0)
- Local $aPixel, $iRowOffset
- While $iStack > 1
- $sPoint = $aStack[$iStack - 1]
- Pop($iStack) ;pop
- $aPos = StringRegExp($sPoint, "(\d+|-\d+);(\d+|-\d+)", 3)
- $x = $aPos[0] ;StringRegExpReplace($sPoint, "(\d+);\d+", "$1")
- $y = $aPos[1] ;StringRegExpReplace($sPoint, "\d+;(\d+)", "$1")
- If BitOR($x < 0, $y < 0, $x > $iW - 1, $y > $iH - 1) Then ContinueLoop
- $iRowOffset = $y * $iW + 1
- If GetColorTolerance($iColorOld, DllStructGetData($tPixel, 1, $iRowOffset + $x)) <= $iTolerance Then
- DllStructSetData($tPixel, 1, $iColorNew, $iRowOffset + $x)
- Push($aStack, $x + 1 & ";" & $y, $iStack)
- Push($aStack, $x & ";" & $y + 1, $iStack)
- Push($aStack, $x - 1 & ";" & $y, $iStack)
- Push($aStack, $x & ";" & $y - 1, $iStack)
- $c += 1
- EndIf
- WEnd
- _GDIPlus_BitmapUnlockBits($hBitmap, $tBitmapData)
- $aStack = 0
- If Not $c Then Return SetError(2, 0, 0)
- Return 1
- EndFunc ;==>_GDIPlus_FloodFillIter
- Func GetColorTolerance($c1, $c2)
- Local Const $a = (BitShift(BitAND($c1, 0xFF000000), 24) - BitShift(BitAND($c2, 0xFF000000), 24))^2
- Local Const $r = (BitShift(BitAND($c1, 0x00FF0000), 16) - BitShift(BitAND($c2, 0x00FF0000), 16))^2
- Local Const $g = (BitShift(BitAND($c1, 0x0000FF00), 8) - BitShift(BitAND($c2, 0x0000FF00), 8))^2
- Local Const $b = (BitAND($c1, 0x000000FF) - BitAND($c2, 0x000000FF))^2
- Return Floor(Sqrt($a + $r + $g + $b))
- EndFunc
- Func _GDIPlus_FloodFillIter4(ByRef $hBitmap, $iX, $iY, $iColorNew) ;no alpha channel support but fast
- Local $aResult = DllCall($__g_hGDIPDll, "uint", "GdipGetImageDimension", "handle", $hBitmap, "float*", 0, "float*", 0)
- Local $iW = $aResult[2], $iH = $aResult[3]
- If BitOR($iX < 0, $iY < 0, $iX > $iW - 1, $iY > $iH - 1) Then Return SetError(1, 0, 0)
- Local $nCN = Hex($iColorNew, 8)
- If StringLeft($nCN, 2) = "FF" Then
- $nCN = StringTrimLeft($nCN, 2)
- Local Const $hHBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)
- Local Const $hDC = _WinAPI_GetDC(0)
- Local Const $hBitmapDC = _WinAPI_CreateCompatibleDC($hDC)
- Local Const $hObjOld = _WinAPI_SelectObject($hBitmapDC, $hHBitmap)
- Local Const $hBrush = _WinAPI_CreateBrushIndirect($BS_SOLID, "0x" & $nCN)
- Local Const $hObjOld2 = _WinAPI_SelectObject($hBitmapDC, $hBrush)
- ;~ _WinAPI_ExtFloodFill($hBitmapDC, $iX, $iY, "0x" & $nCN)
- DllCall("gdi32.dll", "int", "FloodFill", "int", $hBitmapDC, "int", $iX,"int", $iY, "int", 0)
- _WinAPI_SelectObject($hBitmapDC, $hObjOld2)
- _WinAPI_DeleteObject($hBrush)
- Local Const $hCtxt = _GDIPlus_ImageGetGraphicsContext($hBitmap)
- Local Const $hBmp = _GDIPlus_BitmapCreateFromHBITMAP($hHBitmap)
- _GDIPlus_GraphicsDrawImageRect($hCtxt, $hBmp, 0, 0, $iW, $iH)
- _GDIPlus_GraphicsDispose($hCtxt)
- _GDIPlus_BitmapDispose($hBmp)
- _WinAPI_DeleteObject($hHBitmap)
- _WinAPI_SelectObject($hBitmapDC, $hObjOld)
- _WinAPI_ReleaseDC(0, $hDC)
- Return 1
- EndIf
- Return SetError(2, 0, 0)
- EndFunc
- Func Push(ByRef $aStack, $sVal, ByRef $iStack)
- If $iStack + 2 > UBound($aStack) Then ReDim $aStack[$iStack + 500]
- $aStack[$iStack] = $sVal
- $iStack += 1
- EndFunc ;==>Push
- ;
- Func Pop(ByRef $iStack)
- $iStack -= 1
- EndFunc ;==>Pop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement