Search the Community
Showing results for tags 'Gausian'.
-
This function allows you to blur the specified GDI+ bitmap with different force of blur (see screenshots, 1 - source image, 2 - blurring image with L=5, 3 - blurring image with L=10). The source bitmap may be of any color depth, the output bitmap is always 32 bits-per-pixel with alpha channel. The function creates a few parallel threads (up to 8), that significantly reduces the time consumption. It's worth noting that the blurring is performed for all channels, including the alpha channel. The following shows the basic algorithm for a simple blur of bitmap written in AutoIt. Unfortunately, it is not possible to solve using AutoIt because the processing of even a small picture may require too much time. For example, blurring image 100x100 can last a few seconds. To solve this problem, I compiled the time-consuming part of code (main loop) to machine code. This solved the problem of the time consumption but I wanted to get better results, especially for large images (more than 1000x1000) and large blur radius (10+). Then I divided the source image into several parts and started the blurring of each part in a separate thread. Now it's become quite good. See below for a complete example written for Autoit 3.3.12.0. ; Scans the source bitmap pixel by pixel For $Yi = 0 To $H - 1 For $Xi = To $W - 1 $A = 0 $R = 0 $G = 0 $B = 0 $Count = 0 ; Calculates the average value for each color channel of source bitmap (including alpha chennel) For $Yj = $Yi - $L To $Yi + $L For $Xj = $Xi - $L To $Xi + $L If ($Xj >= 0) And ($Xj < $W) And ($Yj >= 0) And ($Yj < $H) Then $ARGB = DllStructCreate('BYTE B;BYTE G;BYTE R;BYTE A', $pBitsSrc + ($Xj + $Yj * $W) * 4) $A += $ARGB.A $R += $ARGB.R $G += $ARGB.G $B += $ARGB.B $Count += 1 EndIf Next Next ; Writes the calculated color to the corresponding pixel of destination bitmap $ARGB = DllStructCreate('BYTE B;BYTE G;BYTE R;BYTE A', $pBitsDst + ($Xj + $Yj * $W) * 4) $ARGB.A = $A / $Count $ARGB.R = $R / $Count $ARGB.G = $G / $Count $ARGB.B = $B / $Count Next Next #Include <GDIPlus.au3> If StringRegExpReplace(@AutoItVersion, '(?<!\d)(\d)(?!\d)', '0\1') < '03.03.12.00' Then MsgBox(16, 'Error', 'Require AutoIt 3.3.12.0 or later.') EndIf $sFile = @ScriptDir & '\Birds.png' _GDIPlus_Startup() $hBitmap = _GDIPlus_BitmapCreateFromFile($sFile) $hBlur = _GDIPlus_BitmapCreateBlurBitmap($hBitmap, 5, 1) _GDIPlus_ImageSaveToFile($hBlur, StringRegExpReplace($sFile, '(\.[^\.]+)', '_Blur\1')) _GDIPlus_BitmapDispose($hBlur) _GDIPlus_BitmapDispose($hBitmap) _GDIPlus_Shutdown() Func _GDIPlus_BitmapCreateBlurBitmap($hBitmap, $iRadius, $fAccurate = False) Local $tData[2], $hThread, $iThread, $tParams, $bProc, $tProc, $pProc, $aSize, $aHeight, $iLength, $hResult, $aResult $aSize = DllCall($__g_hGDIPDll, 'uint', 'GdipGetImageDimension', 'handle', $hBitmap, 'float*', 0, 'float*', 0) If (@Error) Or ($aSize[0]) Then Return 0 EndIf For $i = 2 To 3 If $iRadius > $aSize[$i] Then $iRadius = $aSize[$i] EndIf Next If $iRadius < 1 Then Return 0 EndIf $hResult = _GDIPlus_BitmapCreateFromScan0($aSize[2], $aSize[3], $GDIP_PXF32ARGB) $tData[0] = _GDIPlus_BitmapLockBits($hBitmap, 0, 0, $aSize[2], $aSize[3], $GDIP_ILMREAD, $GDIP_PXF32ARGB) $tData[1] = _GDIPlus_BitmapLockBits($hResult, 0, 0, $aSize[2], $aSize[3], $GDIP_ILMWRITE, $GDIP_PXF32ARGB) If @AutoItX64 Then $bProc = Binary('0x48894C24085541574156415548C7C00A0000004883EC0848C704240000000048FFC875EF4883EC28488BAC24A000000048837D000074054831C0EB0748C7C0010000004821C00F855E010000488BAC24A000000048837D080074054831C0EB0748C7C0010000004821C00F8527010000488BAC24A0000000837D180074054831C0EB0748C7C0010000004821C00F85F1000000488BAC24A0000000837D1C0074054831C0EB0748C7C0010000004821C00F85BB000000488BAC24A0000000837D200074054831C0EB0748C7C0010000004821C00F8585000000488BAC24A0000000837D240074054831C0EB0748C7C0010000004821C07553488BAC24A0000000837D280074054831C0EB0748C7C0010000004821C07521488BAC24A0000000837D2C0074054831C0EB0748C7C0010000004821C07502EB0948C7C001000000EB034831C04821C07502EB0948C7C001000000EB034831C04821C07502EB0948C7C001000000EB034831C04821C07502EB0948C7C001000000EB034831C04821C07502EB0948C7C001000000EB034831C04821C07502EB0948C7C001000000EB034831C04821C07502EB0948C7C001000000EB034831C04821C0740B4831C04863C0E946030000488BAC24A00000004863451450584889442428488BAC24A00000004C637D14488BAC24A00000004863451C4901C749FFCF4C3B7C24280F8CFB020000488BAC24A00000004863451050584889442430488BAC24A00000004C637D10488BAC24A0000000486345184901C749FFCF4C3B7C24300F8CB402000048C74424380000000048C74424400000000048C74424480000000048C74424500000000048C7442458000000004C8B7C2428488BAC24A0000000486345284929C74C897C24604C8B7C24604C8B742428488BAC24A0000000486345284901C64D39F70F8F840100004C8B7C2430488BAC24A0000000486345284929C74C897C24684C8B7C24684C8B742430488BAC24A0000000486345284901C64D39F70F8F2B0100004C8B7C24684D21FF7C614C8B7C2468488BAC24A0000000486345204939C77D3A4C8B7C24604D21FF7C1F4C8B7C2460488BAC24A0000000486345244939C77D0948C7C001000000EB034831C04821C0740948C7C001000000EB034831C04821C0740948C7C001000000EB034831C04821C00F8496000000488BAC24A00000004C8B7D004C8B7424684C8B6C2460488BAC24A0000000486345204C0FAFE84D01EE49C1E6024D01F74C897C24704C8B7C2438488B6C2470480FB645034901C74C897C24384C8B7C2440488B6C2470480FB645024901C74C897C24404C8B7C2448488B6C2470480FB645014901C74C897C24484C8B7C2450488B6C2470480FB645004901C74C897C245048FF4424584C8B7C2468488BAC24A00000004863452C4901C74C897C2468E9B3FEFFFF4C8B7C2460488BAC24A00000004863452C4901C74C897C2460E95AFEFFFF488BAC24A00000004C8B7D084C8B7424304C8B6C2428488BAC24A0000000486345204C0FAFE84D01EE49C1E6024D01F74C897C24704C8B7C2438FF7424584C89F859489948F7F94989C74C89F850488B6C2478588845034C8B7C2440FF7424584C89F859489948F7F94989C74C89F850488B6C2478588845024C8B7C2448FF7424584C89F859489948F7F94989C74C89F850488B6C2478588845014C8B7C2450FF7424584C89F859489948F7F94989C74C89F850488B6C24785888450048FF4424300F8123FDFFFF48FF4424280F81DCFCFFFF48C7C0010000004863C0EB034831C04883C478415D415E415F5DC3') Else $bProc = Binary('0x55535756BA0A00000083EC04C70424000000004A75F38B6C243C837D0000740431C0EB05B80100000021C00F85090100008B6C243C837D0400740431C0EB05B80100000021C00F85DF0000008B6C243C837D1000740431C0EB05B80100000021C00F85B50000008B6C243C837D1400740431C0EB05B80100000021C00F858B0000008B6C243C837D1800740431C0EB05B80100000021C075658B6C243C837D1C00740431C0EB05B80100000021C0753F8B6C243C837D2000740431C0EB05B80100000021C075198B6C243C837D2400740431C0EB05B80100000021C07502EB07B801000000EB0231C021C07502EB07B801000000EB0231C021C07502EB07B801000000EB0231C021C07502EB07B801000000EB0231C021C07502EB07B801000000EB0231C021C07502EB07B801000000EB0231C021C07502EB07B801000000EB0231C021C0740731C0E9400200008B6C243C8B450C8904248B6C243C8B5D0C8B6C243C035D144B3B1C240F8C150200008B6C243C8B4508894424048B6C243C8B5D088B6C243C035D104B3B5C24040F8CE8010000C744240800000000C744240C00000000C744241000000000C744241400000000C7442418000000008B1C248B6C243C2B5D20895C241C8B5C241C8B3C248B6C243C037D2039FB0F8F0D0100008B5C24048B6C243C2B5D20895C24208B5C24208B7C24048B6C243C037D2039FB0F8FD30000008B5C242021DB7C438B5C24208B6C243C3B5D187D298B5C241C21DB7C148B5C241C8B6C243C3B5D1C7D07B801000000EB0231C021C07407B801000000EB0231C021C07407B801000000EB0231C021C0746E8B6C243C8B5D008B7C24208B74241C8B6C243C0FAF751801F7C1E70201FB895C24248B5C24088B6C24240FB6450301C3895C24088B5C240C8B6C24240FB6450201C3895C240C8B5C24108B6C24240FB6450101C3895C24108B5C24148B6C24240FB6450001C3895C2414FF4424188B5C24208B6C243C035D24895C2420E916FFFFFF8B5C241C8B6C243C035D24895C241CE9DDFEFFFF8B6C243C8B5D048B7C24048B34248B6C243C0FAF751801F7C1E70201FB895C24248B5C2408FF74241889D85999F7F989C3538B6C2428588845038B5C240CFF74241889D85999F7F989C3538B6C2428588845028B5C2410FF74241889D85999F7F989C3538B6C2428588845018B5C2414FF74241889D85999F7F989C3538B6C242858884500FF4424040F81FFFDFFFFFF04240F81D3FDFFFFB801000000EB0231C083C4285E5F5B5DC20400') EndIf $iLength = BinaryLen($bProc) $pProc = DllCall('kernel32.dll', 'ptr', 'VirtualAlloc', 'ptr', 0, 'ulong_ptr', $iLength, 'dword', 0x1000, 'dword', 0x0040) $tProc = DllStructCreate('byte[' & $iLength & ']', $pProc[0]) DllStructSetData($tProc, 1, $bProc) $iThread = 8 If $iThread > $aSize[3] Then $iThread = $aSize[3] EndIf Dim $aHeight[$iThread] Dim $tParams[$iThread] Dim $hThread[$iThread] If $iThread = 1 Then $aHeight[0] = $aSize[3] Else $aHeight[0] = Floor($aSize[3] / $iThread) $aHeight[$iThread - 1] = $aSize[3] - $aHeight[0] * ($iThread - 1) For $i = 1 To $iThread - 2 $aHeight[$i] = $aHeight[0] Next EndIf $iLength = 0 For $i = 0 To $iThread - 1 $tParams[$i] = DllStructCreate('ptr;ptr;uint;uint;uint;uint;uint;uint;uint;uint') DllStructSetData($tParams[$i], 1, $tData[0].Scan0) DllStructSetData($tParams[$i], 2, $tData[1].Scan0) DllStructSetData($tParams[$i], 3, 0) DllStructSetData($tParams[$i], 4, $iLength) DllStructSetData($tParams[$i], 5, $aSize[2]) DllStructSetData($tParams[$i], 6, $aHeight[$i]) DllStructSetData($tParams[$i], 7, $aSize[2]) DllStructSetData($tParams[$i], 8, $aSize[3]) DllStructSetData($tParams[$i], 9, $iRadius) If $fAccurate Then DllStructSetData($tParams[$i],10, 1) Else DllStructSetData($tParams[$i],10, 2) EndIf $iLength+= $aHeight[$i] $aResult = DllCall('kernel32.dll', 'handle', 'CreateThread', 'ptr', 0, 'dword_ptr', 0, 'ptr', $pProc[0], 'struct*', $tParams[$i], 'dword', 0, 'ptr', 0) If (Not @Error) And ($aResult[0]) Then $hThread[$i] = $aResult[0] Else $hThread[$i] = 0 EndIf Next While 1 $iLength = 0 For $i = 0 To $iThread - 1 If $hThread[$i] Then $aResult = DllCall('kernel32.dll', 'bool', 'GetExitCodeThread', 'handle', $hThread[$i], 'dword*', 0) If (@Error) Or (Not $aResult[0]) Or ($aResult[2] <> 259) Then DllCall('kernel32.dll', 'bool', 'CloseHandle', 'handle', $hThread[$i]) $hThread[$i] = 0 Else $iLength += 1 EndIf EndIf Next If Not $iLength Then ExitLoop EndIf WEnd $aResult = DllCall('kernel32.dll', 'int', 'VirtualFree', 'ptr', $pProc[0], 'ulong_ptr', 0, 'dword', 0x4000) If (@Error) Or (Not $aResult[0]) Then ; Nothing EndIf _GDIPlus_BitmapUnlockBits($hResult, $tData[1]) _GDIPlus_BitmapUnlockBits($hBitmap, $tData[0]) Return $hResult EndFunc ;==>_GDIPlus_BitmapCreateBlurBitmap Function + Example Previous downloads: 17 GDIPlus_BitmapCreateBlurBitmap.zip