Jump to content
Beege

BmpSearch - Search for Bitmap within Bitmap - Assembly Version

Recommended Posts

Beege

Heres a function for searching for a bitmap within another bitmap. The heart of it is written assembly (source included) and working pretty quick I feel. I have included an example which is pretty basic and should be easily enough for anyone to get the concept. 

You will be given a small blue window that will take a screencapture of that size:

set.PNG

 

It will then take a full screenshot and highlight all locations that it found

find.PNG

Please let me know if you have any issues or questions. Thanks!

BmpSearch.zip

 

GAMERS - Asking for help with ANY kind of game automation is against the forum rules. DON'T DO IT.

  • Like 2

Share this post


Link to post
Share on other sites
Beege

Thanks! :) 

Share this post


Link to post
Share on other sites
kvm2001

Maybe you can add the ability to use mask and toloerance in your real cool lib? It'd make this a really invaluable.

Share this post


Link to post
Share on other sites
junkew

nice.

See another one I wrote in AU3 

As I assume ASM will be much faster you could enhance it with some of the things I did in that AU3 version

  • differentiate the colordepth (B/W, 4,8,16,32, n colors)
  • find using line by line of the image searched (=search multiple bits at 1 call)

Share this post


Link to post
Share on other sites
Sw4rM

Hello Beege, I impress with this UDF that it can search bitmap within another bitmap very fast. But it isn't work when I search 1x1 bitmap size (single pixel).

Please set these variables to 1
$hBMP_Width = 1
$hBMP_Height = 1

How can i fix it?

Best Regards

 

#include <ScreenCapture.au3>
#include <Array.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <BmpSearch.au3>

_Example()

Func _Example()
    local $hBMP_Width = 200
    local $hBMP_Height = 200

    local $FindX = 100
    local $FindY = 100

    _GDIPlus_Startup()

    local $WinHandle = _WinAPI_GetDesktopWindow()
    local $hDDC = _WinAPI_GetDC($WinHandle)
    local $hCDC = _WinAPI_CreateCompatibleDC($hDDC)
    local $hBMP = _WinAPI_CreateCompatibleBitmap($hDDC, $hBMP_Width, $hBMP_Height)

    _WinAPI_SelectObject($hCDC, $hBMP)
    _WinAPI_BitBlt($hCDC, 0, 0, $hBMP_Width, $hBMP_Height, $hDDC, $FindX, $FindY, $SRCCOPY)

    Local $BMP = _GDIPlus_BitmapCreateFromHBITMAP($hBMP)

    $hGUI = GUICreate("hFind", 400, 300)
    GUISetState(@SW_SHOW)

    ; Draw bitmap to GUI

    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
    _GDIPlus_GraphicsDrawImage($hGraphic, $BMP, 0, 0)

    MsgBox($MB_TOPMOST, 'TEST', "Press OK")
    Sleep(500)
    GUIDelete($hGUI)

    Local $hSource = _ScreenCapture_Capture('')

    Local $aCords = _BmpSearch($hSource, $hBMP)
    If Not @error Then
        $iTime = @extended / 1000

        If $aCords[0][0] > 100 Then
            _ArrayDisplay($aCords)
        Else
            ;highligh matches
            Local $aGUI[$aCords[0][0] + 1]
            For $i = 1 To $aCords[0][0]
                ConsoleWrite($aCords[$i][0] & ',' & $aCords[$i][1] & ',' & $aCords[$i][2] & ',' & $aCords[$i][3] & @LF)
                $aGUI[$i] = GUICreate("", $aCords[$i][0], $aCords[$i][1], $aCords[$i][2], $aCords[$i][3], $WS_POPUPWINDOW, $WS_EX_CONTROLPARENT)
                GUISetBkColor(0xFFFF00, $aGUI[$i])
                WinSetTrans($aGUI[$i], '', 150)
                GUISetState(@SW_SHOW)
            Next

            MsgBox(0, 'Found', $aCords[0][0] & ' matches found. Time = ' & $iTime & ' ms')

            For $i = 1 To UBound($aGUI) - 1
                GUIDelete($aGUI[$i])
            Next
        EndIf

    Else
        MsgBox(0, 'NO MATCHES', 'No Matches Found!')
    EndIf

    _WinAPI_DeleteObject($hSource)
    _WinAPI_ReleaseDC($WinHandle, $hDDC)
    _WinAPI_DeleteDC($hCDC)
    _WinAPI_DeleteObject($hBMP)
    _GDIPlus_ImageDispose($BMP)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_Shutdown()

EndFunc   ;==>_Example

 

Share this post


Link to post
Share on other sites
InnI
18 hours ago, Sw4rM said:

How can i fix it?

In BmpSearch.au3 replace function _FindFirstDiff with this

;Find first pixel that is diffrent than ....the first pixel
Func _FindFirstDiff($tPix)

    ;####### (BinaryStrLen = 106) ########################################################################################################################
    Static Local $Opcode = '0xC80000008B5D0C8B1383C3048B4D103913750C83C304E2F7B800000000EB118B5508FF338F028B451029C883C002EB00C9C20C00'
    Static Local $aMemBuff = DllCall("kernel32.dll", "ptr", "VirtualAlloc", "ptr", 0, "ulong_ptr", BinaryLen($Opcode), "dword", 4096, "dword", 64)
    Static Local $tMem = DllStructCreate('byte[' & BinaryLen($Opcode) & ']', $aMemBuff[0])
    Static Local $fSet = DllStructSetData($tMem, 1, $Opcode)
    ;#####################################################################################################################################################

    Local $iMaxLoops = (DllStructGetSize($tPix) / 4) - 1
    If $iMaxLoops Then
        Return DllCallAddress('dword', DllStructGetPtr($tMem), 'dword*', 0, 'struct*', $tPix, 'dword', $iMaxLoops)
    Else
        Local $aRet[] = [1, DllStructGetData($tPix, 1, 1)]
        Return $aRet
    EndIf
EndFunc   ;==>_FindFirstDiff

 

  • Like 1

Share this post


Link to post
Share on other sites
jmaruca

Hi Beege,

This looks quite good and it works on Windows 10 x64.  Would it be possible to do some sort of search for a particular icon / button then double-click on it?.  I have been eagerly searching for a solution to select certain buttons on a Citrix Published application; would be really great, many thanks.

Share this post


Link to post
Share on other sites
Sw4rM

@InnI Thank you so much, It works perfectly :).

Share this post


Link to post
Share on other sites
Beege
On 10/28/2018 at 9:39 AM, Sw4rM said:

Hello Beege, I impress with this UDF that it can search bitmap within another bitmap very fast. But it isn't work when I search 1x1 bitmap size (single pixel).

Please set these variables to 1
$hBMP_Width = 1
$hBMP_Height = 1

How can i fix it?

 

18 hours ago, InnI said:

In BmpSearch.au3 replace function _FindFirstDiff with this

Will that fix searching for a single row in general? 1x5 for example. Honestly I never even thought of that or a single vertical row. All three I think would fail with my code because im looking for 4 corners as a pre-check

  • Like 1

Share this post


Link to post
Share on other sites
Beege
19 hours ago, jmaruca said:

Hi Beege,

This looks quite good and it works on Windows 10 x64.  Would it be possible to do some sort of search for a particular icon / button then double-click on it?.  I have been eagerly searching for a solution to select certain buttons on a Citrix Published application; would be really great, many thanks.

Yes that should be possible. At one point in time I was making a capture tool that could be used to capture a small pic like your describing, and then encode the bmp into a string and wrap it up into a function that you could call for just for simply that - look for this picture and click here within that pic. I have not tried this on windows 10 yet but give this a shot and see if you have any luck getting it to generate. If works it will generate a function that has the picture of the icon you captured and will move the mouse to that location. I have the code to click the button commented out for testing. 

Edit: a little about how the tool works - after you capture an image, it will show you the image back zoomed in. This is so you can capture where you want the mouse to go. So capture one is the image you are looking for - capture 2 is where you want to click within that image. The idea here was to help in situations where you have multiple buttons on the screen that look the same. This way I can more easily say for example - find this picture, but click the top left corner. 

BmpSearch_with_Capture.zip

Edited by Beege

Share this post


Link to post
Share on other sites
MaximusCZ

It seems OP script doesnt find results when its shown on negative coordinates. ie if primary monitor is on right side, whole space of left monitor have neagtive x coord.

Is there a way to make it search on all monitors?

Share this post


Link to post
Share on other sites
InnI
8 hours ago, Beege said:

All three I think would fail with my code because im looking for 4 corners as a pre-check

Your function works good up to two pixels dimensions picture: 1x2 or 2x1. With my fix it works with 1x1 picture too (in fact returns array of pixels). But there is some trouble in algorithm on right side of image. Try this example

#include <WinAPIGdi.au3>
#include <BmpSearch.au3>
#include <Array.au3>

$hWnd = GUICreate("")

$hSource = _WinAPI_CreateSolidBitmap($hWnd, 0x00FFFFFF, 3, 3)
$hFind = _WinAPI_CreateSolidBitmap($hWnd, 0x00FFFFFF, 2, 2)

$aData = _BmpSearch($hSource, $hFind)
If @error Then Exit MsgBox(0, "", "Not found")

_ArrayDisplay($aData)

; result

; 5         <= must be 4
; 2 2 0 0
; 2 2 1 0
; 2 2 2 0   <= error
; 2 2 0 1
; 2 2 1 1

In white square 3x3 function finds 5 white squares 2x2, but must find 4. Algorithm does not stop on right side and continues to search on next row from left side. The result is a picture in which the left side of $hFind is found on the right side of $hSource, and the right side of $hFind on the left side of $hSource.

Also I think will be better to ignore alpha channel. Some functions return alpha channel as 0x00 (gdi32) but other as 0xFF (GDI+) and identical bitmaps will not be found.

And you need to add error checking after all _WinAPI_GetBitmapDimension functions.

Share this post


Link to post
Share on other sites
Beege
12 hours ago, MaximusCZ said:

It seems OP script doesnt find results when its shown on negative coordinates. ie if primary monitor is on right side, whole space of left monitor have neagtive x coord.

Is there a way to make it search on all monitors?

Correct.. The OP script example does not search all monitors. Remember, this function is for searching bitmaps, not the monitors. If you need to search your other monitors, your script needs to take screenshots of the other screens and feed them into the _Bmpsearch()

Share this post


Link to post
Share on other sites
Beege
7 hours ago, InnI said:

Your function works good up to two pixels dimensions picture: 1x2 or 2x1. With my fix it works with 1x1 picture too (in fact returns array of pixels). But there is some trouble in algorithm on right side of image. Try this example

Thanks Innl, Ill be rethinking some of these scenarios pointed out. Post that ex function :)

Share this post


Link to post
Share on other sites
MaximusCZ
9 hours ago, Beege said:

Correct.. The OP script example does not search all monitors. Remember, this function is for searching bitmaps, not the monitors. If you need to search your other monitors, your script needs to take screenshots of the other screens and feed them into the _Bmpsearch()

Thank you, elegant solution that just didnt cross my mind.

Also, its interesting that when I work with example, I select an area on screen and let it find, it reports the output on same coords, but size is -1 in ever direction than given example:

 

First line is marked area, second line is first area found.

 

btw, Are you thinking about adding some tolerance in variation? Sometimes (in some programs regulary) you cant find same bitmap twice because of very tiny variance in pixel colors

bmpsearch.png

Share this post


Link to post
Share on other sites
InnI
12 hours ago, Beege said:

Post that ex function

I used _BmpSearch quite often. And I made some changes to improve usability:

  • now it is not a UDF, but a function
  • now the function accepts not only gdi32 hBitmap, but also GDI+ hImage or simply path to image file
  • now there is no problem with the alpha channel
  • now you can search for a 1x1 pixel image and get array of pixels
  • now the search time is returned to the $aCoords[0][1] in milliseconds
  • added error checking
#include <GDIPlus.au3>

#pragma compile(x64, false)

; #FUNCTION# ====================================================================================================================
; Name          : _BmpSearchEx
; Description   : Searches for Bitmap in a Bitmap
; Syntax        : _BmpSearchEx($vPic1, $vPic2, $iMax = 5000)
; Parameters    : $vPic1   - Handle to bitmap (gdi32 or GDI+) to search or path to image file
;               : $vPic2   - Handle to bitmap (gdi32 or GDI+) to find or path to image file
;               : $iMax    - Max matches to find
; Return values : Success: Returns a 2d array with the following format:
;               :          $aCoords[0][0]  = Total matches found (0 if not found)
;               :          $aCoords[0][1]  = Time of search in ms
;               :          $aCoords[$i][1] = Width of bitmap
;               :          $aCoords[$i][2] = Height of bitmap
;               :          $aCoords[$i][3] = X coordinate
;               :          $aCoords[$i][4] = Y coordinate
;               : Failure: Returns 0 and sets @error:
;               :          @error = 1 - file $vPic1 not found
;               :          @error = 2 - file $vPic2 not found
;               :          @error = 3 - $vPic1 is not a bitmap
;               :          @error = 4 - $vPic2 is not a bitmap
;               :          @error = 5 - error decode opcode
; Author        : Brian J Christy (Beege)
; Modified      : InnI
; ===============================================================================================================================
Func _BmpSearchEx($vPic1, $vPic2, $iMax = 5000)
  If $iMax < 1 Then $iMax = 5000
  Local $hImg1, $hImg2, $iErr1, $iErr2, $iTime = TimerInit()
  _GDIPlus_Startup()
  If IsString($vPic1) Then
    If Not FileExists($vPic1) Then Return SetError(1, _GDIPlus_Shutdown(), 0)
    $hImg1 = _GDIPlus_BitmapCreateFromFile($vPic1)
  Else
    $hImg1 = _GDIPlus_BitmapCreateFromHBITMAP($vPic1)
    $iErr1 = @error
  EndIf
  If IsString($vPic2) Then
    If Not FileExists($vPic2) Then Return SetError(2, _GDIPlus_Shutdown(), 0)
    $hImg2 = _GDIPlus_BitmapCreateFromFile($vPic2)
  Else
    $hImg2 = _GDIPlus_BitmapCreateFromHBITMAP($vPic2)
    $iErr2 = @error
  EndIf
  $hSource = ($iErr1) ? _GDIPlus_BitmapCreateHBITMAPFromBitmap($vPic1) : _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImg1)
  _GDIPlus_BitmapDispose($hImg1)
  $hFind = ($iErr2) ? _GDIPlus_BitmapCreateHBITMAPFromBitmap($vPic2) : _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImg2)
  _GDIPlus_BitmapDispose($hImg2)
  _GDIPlus_Shutdown()
  Static Local $aMemBuff, $tMem, $fStartup = True
  If $fStartup Then
    ;####### (BinaryStrLen = 490) #### (Base64StrLen = 328 )#####################################################################
    Local $Opcode = 'yBAAAFCNRfyJRfSNRfiJRfBYx0X8AAAAAItVDP8yj0X4i10Ii0UYKdiZuQQAAAD38YnBi0X4OQN0CoPDBOL36akAAACDfSgAdB1TA10oO10YD4OVAAAAi1UkORN1A1vrBluDwwTrvVOLVSyLRTADGjtdGHd3iwg5C3UhA1oEi0gEO10Yd2Y5C3USA1oIi0gIO10Yc1c5' & _
        'C3UDW+sGW4PDBOuCi1UUid6LfQyLTRCJ2AHIO0UYczfzp4P5AHcLSoP6AHQNA3Uc6+KDwwTpVP///4tFIIkYg0UgBIPDBP9F/ItVNDlV/HQG6Tj///9bi0X8ycIwAA=='
    Local $aDecode = DllCall("Crypt32.dll", "bool", "CryptStringToBinary", "str", $Opcode, "dword", 0, "dword", 1, "struct*", DllStructCreate("byte[254]"), "dword*", 254, "ptr", 0, "ptr", 0)
    If @error Or (Not $aDecode[0]) Then Return SetError(5, 0, 0)
    $Opcode = BinaryMid(DllStructGetData($aDecode[4], 1), 1, $aDecode[5])
    $aMemBuff = DllCall("kernel32.dll", "ptr", "VirtualAlloc", "ptr", 0, "ulong_ptr", BinaryLen($Opcode), "dword", 4096, "dword", 64)
    $tMem = DllStructCreate('byte[' & BinaryLen($Opcode) & ']', $aMemBuff[0])
    DllStructSetData($tMem, 1, $Opcode)
    ;############################################################################################################################
    $fStartup = False
  EndIf
  Local $tSizeSource = _WinAPI_GetBitmapDimension($hSource)
  If @error Then Return SetError(3, 0, 0)
  Local $tSizeFind = _WinAPI_GetBitmapDimension($hFind)
  If @error Then Return SetError(4, 0, 0)
  Local $iRowInc = ($tSizeSource.X - $tSizeFind.X) * 4
  Local $tSource = DllStructCreate('dword[' & ($tSizeSource.X * $tSizeSource.Y) & ']')
  _WinAPI_GetBitmapBits($hSource, DllStructGetSize($tSource), DllStructGetPtr($tSource))
  Local $tFind = DllStructCreate('dword[' & ($tSizeFind.X * $tSizeFind.Y) & ']')
  _WinAPI_GetBitmapBits($hFind, DllStructGetSize($tFind), DllStructGetPtr($tFind))
  ;####### (BinaryStrLen = 106) #################################################################################################
  Static Local $Opcode_ = '0xC80000008B5D0C8B1383C3048B4D103913750C83C304E2F7B800000000EB118B5508FF338F028B451029C883C002EB00C9C20C00'
  Static Local $aMemBuff_ = DllCall("kernel32.dll", "ptr", "VirtualAlloc", "ptr", 0, "ulong_ptr", BinaryLen($Opcode_), "dword", 4096, "dword", 64)
  Static Local $tMem_ = DllStructCreate('byte[' & BinaryLen($Opcode_) & ']', $aMemBuff_[0])
  DllStructSetData($tMem_, 1, $Opcode_)
  ;##############################################################################################################################
  Local $iFirstDiffIdx, $iFirstDiffPix, $aFirstDiffCoords[2], $iMaxLoops = (DllStructGetSize($tFind) / 4) - 1
  If $iMaxLoops Then
    Local $aFD = DllCallAddress('dword', DllStructGetPtr($tMem_), 'dword*', 0, 'struct*', $tFind, 'dword', $iMaxLoops)
    $iFirstDiffIdx = $aFD[0]
    $iFirstDiffPix = $aFD[1]
  Else
    $iFirstDiffIdx = 1
    $iFirstDiffPix = DllStructGetData($tFind, 1, 1)
  EndIf
  $aFirstDiffCoords[1] = Int(($iFirstDiffIdx - 1) / $tSizeFind.X)
  $aFirstDiffCoords[0] = ($iFirstDiffIdx - 1) - ($aFirstDiffCoords[1] * $tSizeFind.X)
  Local $iFirst_Diff_Inc = (($aFirstDiffCoords[1] * $tSizeSource.X) + $aFirstDiffCoords[0]) * 4
  If $iFirst_Diff_Inc < 0 Then $iFirst_Diff_Inc = 0
  Local $tCornerPixs = DllStructCreate('dword[3]')
  DllStructSetData($tCornerPixs, 1, DllStructGetData($tFind, 1, $tSizeFind.X), 1)
  DllStructSetData($tCornerPixs, 1, DllStructGetData($tFind, 1, $tSizeFind.X * ($tSizeFind.Y - 1) + 1), 2)
  DllStructSetData($tCornerPixs, 1, DllStructGetData($tFind, 1, $tSizeFind.X * $tSizeFind.Y), 3)
  Local $tCornerInc = DllStructCreate('dword[3]')
  DllStructSetData($tCornerInc, 1, ($tSizeFind.X - 1) * 4, 1)
  DllStructSetData($tCornerInc, 1, (($tSizeSource.X - $tSizeFind.X) + $tSizeSource.X * ($tSizeFind.Y - 2) + 1) * 4, 2)
  DllStructSetData($tCornerInc, 1, ($tSizeFind.X - 1) * 4, 3)
  Local $pStart = DllStructGetPtr($tSource)
  Local $iEndAddress = Int($pStart + DllStructGetSize($tSource))
  Local $tFound = DllStructCreate('dword[' & $iMax & ']')
  Local $ret = DllCallAddress('dword', DllStructGetPtr($tMem), 'struct*', $tSource, 'struct*', $tFind, _
      'dword', $tSizeFind.X, 'dword', $tSizeFind.Y, _
      'dword', $iEndAddress, 'dword', $iRowInc, 'struct*', $tFound, _
      'dword', $iFirstDiffPix, 'dword', $iFirst_Diff_Inc, _
      'struct*', $tCornerInc, 'struct*', $tCornerPixs, _
      'dword', $iMax)
  Local $aCoords[$ret[0] + 1][4] = [[$ret[0], Round(TimerDiff($iTime))]]
  If Not $ret[0] Then Return SetError(0, 0, $aCoords)
  For $i = 1 To $ret[0]
    $iFoundIndex = ((DllStructGetData($tFound, 1, $i) - $pStart) / 4) + 1
    $aCoords[$i][3] = Int(($iFoundIndex - 1) / $tSizeSource.X)
    $aCoords[$i][2] = ($iFoundIndex - 1) - ($aCoords[$i][3] * $tSizeSource.X)
    $aCoords[$i][0] = $tSizeFind.X
    $aCoords[$i][1] = $tSizeFind.Y
  Next
  $aCoords[0][1] = Round(TimerDiff($iTime))
  Return SetError(0, 0, $aCoords)
EndFunc  ;==>_BmpSearchEx

 

Edited by InnI

Share this post


Link to post
Share on other sites
Beege
3 hours ago, MaximusCZ said:

Also, its interesting that when I work with example, I select an area on screen and let it find, it reports the output on same coords, but size is -1 in ever direction than given example:

Sorry Im not following the example. The size is off by -1?

 

3 hours ago, MaximusCZ said:

btw, Are you thinking about adding some tolerance in variation? Sometimes (in some programs regulary) you cant find same bitmap twice because of very tiny variance in pixel colors

Yes I would love to add some extra functionality like that. If you or anyone can demonstrate the kinda math we would be talking about with the pixel values, even in straight autoit, that would be helpful. 

Share this post


Link to post
Share on other sites
MaximusCZ
13 minutes ago, Beege said:

Sorry Im not following the example. The size is off by -1

In the screen you can see console output. First line (10,12..) is output of where I place the blue mark. Then it searches, and finds that exact spot, but marks the sie 11,13

Share this post


Link to post
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

  • Similar Content

    • UEZ
      By UEZ
      Here a little tool to display the histogram of an image (inspired by this thread):
      )

      ;#AutoIt3Wrapper_Icon=Histogram1.ico #AutoIt3Wrapper_Compile_Both=y ;#AutoIt3Wrapper_Res_Field=CompanyName|UEZ Software Development ;#AutoIt3Wrapper_Res_Field=ProductName|GDI+ Show Histogram ;#AutoIt3Wrapper_Res_Field=ProductVersion|%AutoItVer% #AutoIt3Wrapper_Res_Fileversion=0.9.8.0 ;#AutoIt3Wrapper_Res_Field=Build|2015-07-10 #AutoIt3Wrapper_Res_LegalCopyright=UEZ Software Development #AutoIt3Wrapper_Res_Language=1033 ;#AutoIt3Wrapper_Res_Field=URL|http://www.autoitscript.com/forum/index.php?showtopic=147777 #AutoIt3Wrapper_Run_Au3Stripper=y #Au3Stripper_Parameters=/so /pe /rm #AutoIt3Wrapper_Run_After=del /f /q "%scriptdir%\%scriptfile%_stripped.au3" #AutoIt3Wrapper_UseUpx=y #AutoIt3Wrapper_UPX_Parameters=--best --lzma #AutoIt3Wrapper_Run_After=upx.exe --best --lzma "GDI+ Show Histogram_x64.exe" #AutoIt3Wrapper_UseX64=n #include <Constants.au3> #include <GUIConstantsEx.au3> #include <GDIPlus.au3> #include <WindowsConstants.au3> Opt("MustDeclareVars", 1) Opt("MouseCoordMode", 2) _GDIPlus_Startup() If @error Then Exit MsgBox(0x40010, "Error", "GDI+ Show Histogram can run on operating systems Vista or newer only!") Global $ghGDIPDll = $__g_hGDIPDll, $aRes Global Const $hFullScreen = WinGetHandle("[TITLE:Program Manager;CLASS:Progman]") Global Const $aFullScreen = WinGetPos($hFullScreen) Global Enum $HistogramFormatARGB, $HistogramFormatPARGB, $HistogramFormatRGB, $HistogramFormatGray, _ ;http://msdn.microsoft.com/en-us/library/windows/desktop/ms534129(v=vs.85).aspx $HistogramFormatB, $HistogramFormatG, $HistogramFormatR, $HistogramFormatA Global $tStructChannel0 = DllStructCreate("uint channel0[256]") Global $pStructChannel0 = DllStructGetPtr($tStructChannel0) Global $tStructChannel1 = DllStructCreate("uint channel1[256]") Global $pStructChannel1 = DllStructGetPtr($tStructChannel1) Global $tStructChannel2 = DllStructCreate("uint channel2[256]") Global $pStructChannel2 = DllStructGetPtr($tStructChannel2) Global $tStructChannel3 = DllStructCreate("uint channel3[256]") Global $pStructChannel3 = DllStructGetPtr($tStructChannel3) Global $s, $sFile, $hImage, $bImgLoaded = False, $aLuminosity[257], $aCRGB[257] Global Const $STM_SETIMAGE = 0x0172, $iLineJoinBevel = 1, $frY = 0.299, $fgY = 0.587, $fbY = 0.114 Global Const $hGUI = GUICreate("GDI+ Show Histogram v0.98 beta by UEZ build 2015-07-13", 468, 310, -1, -1, Default, Default) GUISetBkColor(0xFFFFFF, $hGUI) Global Const $sLabel_Titel_Txt = "GDI+ Show Histogram" Global Const $iPosX_Label = 20, $iPosY_Label = 12 Global Const $iLabel_Titel_Shadow = GUICtrlCreateLabel($sLabel_Titel_Txt, $iPosX_Label, $iPosY_Label, 429, 57) GUICtrlSetFont(-1, 30, 400, 0, "Comic Sans MS", 4) GUICtrlSetColor(-1, 0xC0C0D8) Global Const $iLabel_Titel = GUICtrlCreateLabel($sLabel_Titel_Txt, $iPosX_Label - 4, $iPosY_Label - 4, 429, 57) GUICtrlSetFont(-1, 30, 400, 0, "Comic Sans MS", 4) GUICtrlSetColor(-1, 0x0000C0) GUICtrlSetBkColor(-1, -2) Global Const $iPic_Histogram = GUICtrlCreatePic("", 20, 90, 255, 127, -1, $WS_EX_STATICEDGE) GUICtrlSetCursor(-1, 14) Global Const $iPic_ColorGradient = GUICtrlCreatePic("", 20, 220, 255, 24, -1, $WS_EX_STATICEDGE) Global Const $iGroup = GUICtrlCreateGroup("Channel", 290, 84, 160, 164) Global Const $iRadio_L = GUICtrlCreateRadio("Luminosit&y (gray)", 300, 100, 110, 20) GUICtrlSetTip(-1, $frY & "*R, " & $fgY & "*G, " & $fbY & "*B") GUICtrlSetState($iRadio_L, $GUI_CHECKED) Global Const $iRadio_C = GUICtrlCreateRadio("&cRGB", 300, 130, 60, 20) GUICtrlSetTip(-1, "Combined average of RGB values") Global Const $iRadio_R = GUICtrlCreateRadio("&Red", 300, 160, 60, 20) Global Const $iRadio_G = GUICtrlCreateRadio("&Green", 300, 190, 60, 20) Global Const $iRadio_B = GUICtrlCreateRadio("&Blue", 300, 220, 60, 20) GUICtrlCreateGroup("", -99, -99, 1, 1) Global Const $iButton_Save = GUICtrlCreateButton("&Save", 209, 265, 70, 30) GUICtrlSetTip(-1, "Click button to save all histogram images") GUICtrlSetState(-1, $GUI_DISABLE) Global Const $iButton_Load = GUICtrlCreateButton("&Load", 290, 265, 70, 30) GUICtrlSetTip(-1, "Click button to load an image") Global Const $iButton_Exit = GUICtrlCreateButton("E&xit", 390, 265, 60, 30) Global $sLabelCredit_Txt = "coded by UEZ'13" Global $aLabelCredit_Pos[StringLen($sLabelCredit_Txt)][4], $u, $iFS = 6.5, $iY = 280 For $u = 0 To UBound($aLabelCredit_Pos) - 1 $aLabelCredit_Pos[$u][0] = StringMid($sLabelCredit_Txt, $u + 1, 1) $aLabelCredit_Pos[$u][1] = 20 + $u * ($iFS - 0.25) $aLabelCredit_Pos[$u][2] = $iY $aLabelCredit_Pos[$u][3] = GUICtrlCreateLabel($aLabelCredit_Pos[$u][0], $aLabelCredit_Pos[$u][1], $aLabelCredit_Pos[$u][2], $iFS, $iFS * 1.8) GUICtrlSetFont(-1, $iFS, 400, 0, "Comic Sans MS", 2) GUICtrlSetColor(-1, 0x006000) Next Global $aLabelCredit_AnimPos[300] For $u = 0 To UBound($aLabelCredit_AnimPos) - 1 $aLabelCredit_AnimPos[$u] = $iY Next Global Const $fLimes = 2.2, $fStep = 0.1 Global $iPT = 0, $z = Int((UBound($aLabelCredit_AnimPos) - ($fLimes * $fStep * 100)) / 2) For $u = 0 To $fLimes Step $fStep $aLabelCredit_AnimPos[$z] = $iY + Sin($iPT * 4) * 10 $z += 1 $iPT += 0.075 Next Global $ihGui_PreviewSize = 256, $iBGColor = 0xF0F0F0 Global $hGui_Preview = GUICreate("", $ihGui_PreviewSize, $ihGui_PreviewSize + 58, -1, -1, $WS_POPUP + $WS_BORDER, $WS_EX_TOPMOST, $hGUI) Global $iPic_Preview = GUICtrlCreatePic("", 0, 0, $ihGui_PreviewSize, $ihGui_PreviewSize) Global $idLabel_Info = GUICtrlCreateLabel("", 0, $ihGui_PreviewSize, $ihGui_PreviewSize * 2, 58) GUICtrlSetFont(-1, 8.5, 400, 0, "Arial", 5) GUICtrlSetColor(-1, $iBGColor) GUICtrlSetBkColor(-1, 0x333333) Global Const $hTexture_Checkerboard = _GDIPlus_CreateCheckerboardTexture(8, 0xFFFBFBFB, 0xFFFFFFFF) Global $hHBmp_L = _GDIPlus_CreateColorGradient("L", 256, 24) Global $hHBmp_C = _GDIPlus_CreateColorGradient("C", 256, 24) Global $hHBmp_R = _GDIPlus_CreateColorGradient("R", 256, 24) Global $hHBmp_G = _GDIPlus_CreateColorGradient("G", 256, 24) Global $hHBmp_B = _GDIPlus_CreateColorGradient("B", 256, 24) Global Const $hHBmp_HL = _GDIPlus_BitmapCreateFromScan0(256, 127) Global Const $hCtxt_HL = _GDIPlus_ImageGetGraphicsContext($hHBmp_HL) Global Const $hHBmp_HC = _GDIPlus_BitmapCloneArea($hHBmp_HL, 0, 0, 256, 127, 0x0026200A) Global Const $hCtxt_HC = _GDIPlus_ImageGetGraphicsContext($hHBmp_HC) Global Const $hHBmp_HR = _GDIPlus_BitmapCloneArea($hHBmp_HL, 0, 0, 256, 127, 0x0026200A) Global Const $hCtxt_HR = _GDIPlus_ImageGetGraphicsContext($hHBmp_HR) Global Const $hHBmp_HG = _GDIPlus_BitmapCloneArea($hHBmp_HL, 0, 0, 256, 127, 0x0026200A) Global Const $hCtxt_HG = _GDIPlus_ImageGetGraphicsContext($hHBmp_HG) Global Const $hHBmp_HB = _GDIPlus_BitmapCloneArea($hHBmp_HL, 0, 0, 256, 127, 0x0026200A) Global Const $hCtxt_HB = _GDIPlus_ImageGetGraphicsContext($hHBmp_HB) _GDIPlus_GraphicsClear($hCtxt_HL, 0xFFFFFFFF) _hBmpToPicControl($iPic_ColorGradient, $hHBmp_L) Global $hB_H = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hHBmp_HL) _hBmpToPicControl($iPic_Histogram, $hB_H, 1) GUISetState(@SW_HIDE, $hGui_Preview) GUISetState(@SW_SHOW, $hGUI) ControlFocus($hGUI, "", $iButton_Load) Global $hPen_H = _GDIPlus_PenCreate() $s = "L" Global $iDummyL = GUICtrlCreateDummy(), $iDummyC = GUICtrlCreateDummy(), $iDummyR = GUICtrlCreateDummy(), $iDummyG = GUICtrlCreateDummy(), $iDummyB = GUICtrlCreateDummy() Global $iHistogramFormat, $hHBmp_Preview, $aPosCtrl, $aPosWin, $aMouseInfo, $aPos_hWnd, $imp, $iVal, $iMpos_oldx, $iMpos_oldy, $sType, $iPaletteType, $iPixelFormat Global $fAverage_L, $fAverage_cRGB, $iAverage_R, $iAverage_B, $iAverage_G, $o = 0 Global $bShow = False, $bHide = False, $bIsAlpha = False, $bIsGray = False Global Const $iEntries = 256 Global Const $tagCOLORPALETTE = "uint Flags;" & _ ; Palette flags "uint Count;" & _ ; Number of color entries "uint ARGB[" & $iEntries & "];" ; Palette color AdlibRegister("AnimateLabel", 60) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE, $iButton_Exit AdlibUnRegister("AnimateLabel") GUIDelete($hGui_Preview) GUIDelete($hGUI) _WinAPI_DeleteObject($hHBmp_L) _WinAPI_DeleteObject($hHBmp_C) _WinAPI_DeleteObject($hHBmp_L) _WinAPI_DeleteObject($hHBmp_R) _WinAPI_DeleteObject($hHBmp_G) _WinAPI_DeleteObject($hHBmp_B) If $hHBmp_Preview Then _WinAPI_DeleteObject($hHBmp_Preview) _GDIPlus_PenDispose($hPen_H) _GDIPlus_BrushDispose($hTexture_Checkerboard) _GDIPlus_GraphicsDispose($hCtxt_HL) _GDIPlus_GraphicsDispose($hCtxt_HC) _GDIPlus_GraphicsDispose($hCtxt_HR) _GDIPlus_GraphicsDispose($hCtxt_HG) _GDIPlus_GraphicsDispose($hCtxt_HB) _GDIPlus_BitmapDispose($hHBmp_HL) _GDIPlus_BitmapDispose($hHBmp_HC) _GDIPlus_BitmapDispose($hHBmp_HR) _GDIPlus_BitmapDispose($hHBmp_HG) _GDIPlus_BitmapDispose($hHBmp_HB) _GDIPlus_Shutdown() $tStructChannel0 = 0 $tStructChannel1 = 0 $tStructChannel2 = 0 $tStructChannel3 = 0 Exit Case $iRadio_L, $iDummyL If $s <> "L" Then _hBmpToPicControl($iPic_ColorGradient, $hHBmp_L) If $bImgLoaded Then $hB_H = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hHBmp_HL) _hBmpToPicControl($iPic_Histogram, $hB_H, 1) EndIf $s = "L" EndIf Case $iRadio_C, $iDummyC If $s <> "C" Then _hBmpToPicControl($iPic_ColorGradient, $hHBmp_C) If $bImgLoaded Then $hB_H = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hHBmp_HC) _hBmpToPicControl($iPic_Histogram, $hB_H, 1) EndIf $s = "C" EndIf Case $iRadio_R, $iDummyR If $s <> "R" Then _hBmpToPicControl($iPic_ColorGradient, $hHBmp_R) If $bImgLoaded Then $hB_H = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hHBmp_HR) _hBmpToPicControl($iPic_Histogram, $hB_H, 1) EndIf $s = "R" EndIf Case $iRadio_G, $iDummyG If $s <> "G" Then _hBmpToPicControl($iPic_ColorGradient, $hHBmp_G) If $bImgLoaded Then $hB_H = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hHBmp_HG) _hBmpToPicControl($iPic_Histogram, $hB_H, 1) EndIf $s = "G" EndIf Case $iRadio_B, $iDummyB If $s <> "B" Then _hBmpToPicControl($iPic_ColorGradient, $hHBmp_B) If $bImgLoaded Then $hB_H = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hHBmp_HB) _hBmpToPicControl($iPic_Histogram, $hB_H, 1) EndIf $s = "B" EndIf Case $iButton_Load $sFile = FileOpenDialog("Select an image", "", "Images (*.jpg;*.png;*.bmp;*.gif)", 3) If @error Then ContinueCase If $bImgLoaded Then _GDIPlus_ImageDispose($hImage) $hImage = _GDIPlus_ImageLoadFromFile($sFile) If @error Then MsgBox(0x40010, "Error", "Unable to load image", 30) ContinueCase EndIf GUICtrlSetState($iButton_Save, $GUI_ENABLE) $aRes = _GDIPlus_ImageGetFlags($hImage) If BitAND($aRes[0], $GDIP_IMAGEFLAGS_HASALPHA) Then $bIsAlpha = True Else $bIsAlpha = False EndIf If BitAND($aRes[0], $GDIP_IMAGEFLAGS_COLORSPACE_GRAY) Then $bIsGray = True Else $bIsGray = False EndIf $hHBmp_Preview = _GetImage($sFile, $ihGui_PreviewSize, $iBGColor) _hBmpToPicControl($iPic_Preview, $hHBmp_Preview, 1) If Not $bIsAlpha Then $pStructChannel3 = 0 $iHistogramFormat = $HistogramFormatRGB Else $pStructChannel3 = DllStructGetPtr($tStructChannel3) $iHistogramFormat = $HistogramFormatARGB EndIf _GDIPlus_BitmapGetHistogram($hImage, $iHistogramFormat, _GDIPlus_BitmapGetHistogramSize($iHistogramFormat), $pStructChannel0, $pStructChannel1, $pStructChannel2, $pStructChannel3) _GDIPlus_CreateHistogram() $bImgLoaded = True If BitAND(GUICtrlRead($iRadio_L), $GUI_CHECKED) Then $s = "" GUICtrlSendToDummy($iDummyL) ElseIf BitAND(GUICtrlRead($iRadio_C), $GUI_CHECKED) Then $s = "" GUICtrlSendToDummy($iDummyC) ElseIf BitAND(GUICtrlRead($iRadio_R), $GUI_CHECKED) Then $s = "" GUICtrlSendToDummy($iDummyR) ElseIf BitAND(GUICtrlRead($iRadio_G), $GUI_CHECKED) Then $s = "" GUICtrlSendToDummy($iDummyG) Else $s = "" GUICtrlSendToDummy($iDummyB) EndIf Case $iButton_Save If $sFile = "" Then ContinueLoop $sType = "png" $o += _GDIPlus_ImageSaveToFile($hHBmp_HL, StringRegExpReplace($sFile, "(.*)\..*", "$1") & "_Luminosity." & $sType) $o += _GDIPlus_ImageSaveToFile($hHBmp_HC, StringRegExpReplace($sFile, "(.*)\..*", "$1") & "_cRGB." & $sType) $o += _GDIPlus_ImageSaveToFile($hHBmp_HR, StringRegExpReplace($sFile, "(.*)\..*", "$1") & "_Red." & $sType) $o += _GDIPlus_ImageSaveToFile($hHBmp_HG, StringRegExpReplace($sFile, "(.*)\..*", "$1") & "_Green." & $sType) $o += _GDIPlus_ImageSaveToFile($hHBmp_HB, StringRegExpReplace($sFile, "(.*)\..*", "$1") & "_Blue." & $sType) If $o = 5 Then MsgBox(64 + 262144, "Information", "Images properly saved to " & StringRegExpReplace($sFile, "(.*)\\.*", "$1"), 30, $hGUI) Else MsgBox(16 + 262144, "Error", "One or more images could not saved to " & StringRegExpReplace($sFile, "(.*)\\.*", "$1") & ".", 30, $hGUI) EndIf EndSwitch If WinActive($hGUI) Then $aMouseInfo = GUIGetCursorInfo($hGUI) Switch $aMouseInfo[4] Case $iPic_Histogram If $bImgLoaded Then $aPos_hWnd = WinGetPos($hGUI) $imp = Max(1, Min(256, -19 + $aMouseInfo[0])) Switch $s Case "L" $iVal = $aLuminosity[$imp] Case "C" $iVal = $aCRGB[$imp] Case "R" $iVal = DllStructGetData($tStructChannel0, "channel0", $imp) Case "G" $iVal = DllStructGetData($tStructChannel1, "channel1", $imp) Case "B" $iVal = DllStructGetData($tStructChannel2, "channel2", $imp) EndSwitch If $iMpos_oldx <> $aMouseInfo[0] Or $iMpos_oldy <> $aMouseInfo[1] Then ToolTip("Index: " & $imp & ", Value: " & Round($iVal, 0) & ", Average: " & Round($fAverage_L, 2), $aPos_hWnd[0] + $aMouseInfo[0] - 60, $aMouseInfo[1] + $aPos_hWnd[1] - 20) $iMpos_oldx = $aMouseInfo[0] $iMpos_oldy = $aMouseInfo[1] EndIf ShowPreview($iPic_Histogram) $bShow = True $bHide = False EndIf Case Else If Not $bHide Then GUISetState(@SW_HIDE, $hGui_Preview) $bHide = True ToolTip("") EndIf $bShow = False EndSwitch EndIf WEnd Func AnimateLabel() Local Static $v = Int((UBound($aLabelCredit_AnimPos) - ($fLimes * $fStep * 100)) / 2) Local $u For $u = 0 To UBound($aLabelCredit_Pos) - 1 GUICtrlSetPos($aLabelCredit_Pos[$u][3], $aLabelCredit_Pos[$u][1], $aLabelCredit_AnimPos[$v - $u]) Next If $v - $u < 0 Then $v = UBound($aLabelCredit_AnimPos) - 1 $v -= 1 EndFunc ;==>AnimateLabel Func Min($a, $b) If $a < $b Then Return $a Return $b EndFunc ;==>Min Func Max($a, $b) If $a > $b Then Return $a Return $b EndFunc ;==>Max Func ShowPreview($iCtrl) $aPosWin = WinGetPos($hGUI) $aPosCtrl = ControlGetPos($hGUI, "", $iCtrl) Local $iNewX, $iNewX1 = $aPosWin[0] - $ihGui_PreviewSize + 10, $iNewX2 = $aPosWin[0] + $aPosWin[2] - 10 $iNewX = $iNewX1 If $iNewX1 < $aFullScreen[0] Then $iNewX = $iNewX2 EndIf WinMove($hGui_Preview, "", $iNewX, $aPosWin[1] + 50) GUISetState(@SW_SHOWNOACTIVATE, $hGui_Preview) EndFunc ;==>ShowPreview Func _GetImage($sFile, $iWH, $iBkClr = 0xFFFFFF, $bCheckerboard_Bg = True) Local $hBmp1, $hBitmap, $hGraphic, $hImage, $iW, $iH, $aGS, $hBmp2, $aFTS $aFTS = FileGetTime($sFile) If @error Then Return SetError(1, 0, 0) $hBmp1 = _WinAPI_CreateBitmap($iWH, $iWH, 1, 32) $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hBmp1) $hGraphic = _GDIPlus_ImageGetGraphicsContext($hBitmap) _WinAPI_DeleteObject($hBmp1) $hImage = _GDIPlus_ImageLoadFromFile($sFile) $iW = _GDIPlus_ImageGetWidth($hImage) $iH = _GDIPlus_ImageGetHeight($hImage) GUICtrlSetData($idLabel_Info, StringRegExpReplace($sFile, ".*\\(.*)", "$1") & @LF & Round(FileGetSize($sFile) / 1024, 0) & " kb (" & $iW & " x " & $iH & ")" & @LF & $aFTS[0] & "/" & $aFTS[1] & "/" & $aFTS[2] & " " & $aFTS[3] & ":" & $aFTS[4] & ":" & $aFTS[5] & @LF & "Alpha: " & $bIsAlpha & ", Gray: " & $bIsGray) $aGS = _GetScale($iW, $iH, $iWH) Switch $bCheckerboard_Bg Case True ;~ _GDIPlus_GraphicsFillRect($hGraphic, 0, 0, $iWH, $iWH, $hTexture_Checkerboard) _GDIPlus_GraphicsClear($hGraphic, BitOR(0xFF000000, $iBkClr)) _GDIPlus_GraphicsFillRect($hGraphic, $aGS[0], $aGS[1], $aGS[2], $aGS[3], $hTexture_Checkerboard) Case False _GDIPlus_GraphicsClear($hGraphic, BitOR(0xFF000000, $iBkClr)) EndSwitch _GDIPlus_GraphicsDrawImageRect($hGraphic, $hImage, $aGS[0], $aGS[1], $aGS[2], $aGS[3]) _GDIPlus_ImageDispose($hImage) _GDIPlus_GraphicsDispose($hGraphic) $hBmp2 = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap) _GDIPlus_BitmapDispose($hBitmap) Return $hBmp2 EndFunc ;==>_GetImage Func _GetScale($iW, $iH, $iWH) Local $aRet[4] If $iW <= $iWH And $iH <= $iWH Then $aRet[2] = $iW $aRet[3] = $iH $aRet[0] = ($iWH - $aRet[2]) / 2 $aRet[1] = ($iWH - $aRet[3]) / 2 ElseIf $iW > $iH Then $aRet[2] = $iWH $aRet[3] = $iH / ($iW / $iWH) $aRet[0] = 0 $aRet[1] = ($iWH - $aRet[3]) / 2 ElseIf $iW < $iH Then $aRet[2] = $iW / ($iH / $iWH) $aRet[3] = $iWH $aRet[0] = ($iWH - $aRet[2]) / 2 $aRet[1] = 0 ElseIf $iW = $iH Then $aRet[2] = $iWH $aRet[3] = $iWH $aRet[0] = 0 $aRet[1] = 0 EndIf Return $aRet EndFunc ;==>_GetScale Func _hBmpToPicControl($iCID, ByRef $hBmp, $iFlag = 0) Local $hOldBmp $hOldBmp = GUICtrlSendMsg($iCID, $STM_SETIMAGE, $IMAGE_BITMAP, $hBmp) If $hOldBmp Then _WinAPI_DeleteObject($hOldBmp) If $iFlag Then _WinAPI_DeleteObject($hBmp) EndFunc ;==>_hBmpToPicControl Func _GDIPlus_CreateHistogram($sRounding = "Ceiling") Local $iMaxL, $iMaxC, $iMaxR, $iMaxG, $iMaxB, $a, $r, $g, $b, $yR, $yG, $yB, $iRedAv, $iGreenAv, $iBlueAv For $i = 1 + $bIsAlpha To 256 If $bIsAlpha Then ;~ ConsoleWrite("Alpha: " & DllStructGetData($tStructChannel0, "channel0", $i) & @CRLF) $r = DllStructGetData($tStructChannel1, "channel1", $i) $yR = $r * $frY If DllStructGetData($tStructChannel1 , "channel1", $i) > $iMaxR Then $iMaxR = $r $g = DllStructGetData($tStructChannel2, "channel2", $i) $yG = $g * $fgY If DllStructGetData($tStructChannel2 , "channel2", $i) > $iMaxG Then $iMaxG = $g $b = DllStructGetData($tStructChannel3, "channel3", $i) $yB = $b * $fbY If DllStructGetData($tStructChannel3 , "channel3", $i) > $iMaxB Then $iMaxB = $b $fAverage_cRGB += $r + $g + $b $fAverage_L += $yR + $yG + $yB Else $r = DllStructGetData($tStructChannel0, "channel0", $i) $yR = $r * $frY If DllStructGetData($tStructChannel0 , "channel0", $i) > $iMaxR Then $iMaxR = $r $g = DllStructGetData($tStructChannel1, "channel1", $i) $yG = $g * $fgY If DllStructGetData($tStructChannel1 , "channel1", $i) > $iMaxG Then $iMaxG = $g $b = DllStructGetData($tStructChannel2, "channel2", $i) $yB = $b * $fbY If DllStructGetData($tStructChannel2 , "channel2", $i) > $iMaxB Then $iMaxB = $b EndIf $fAverage_cRGB += $r + $g + $b $fAverage_L += $yR + $yG + $yB $aLuminosity[$i] = $yR + $yG + $yB If $aLuminosity[$i] > $iMaxL Then $iMaxL = $aLuminosity[$i] $aCRGB[$i] = ($r + $g + $b) / 3 If $aCRGB[$i] > $iMaxC Then $iMaxC = $aCRGB[$i] Next $fAverage_cRGB /= 256 * 3 $fAverage_L /= 256 _GDIPlus_GraphicsClear($hCtxt_HL, 0xFFFFFFFF) _GDIPlus_GraphicsClear($hCtxt_HC, 0xFFFFFFFF) _GDIPlus_GraphicsClear($hCtxt_HR, 0xFFFFFFFF) _GDIPlus_GraphicsClear($hCtxt_HG, 0xFFFFFFFF) _GDIPlus_GraphicsClear($hCtxt_HB, 0xFFFFFFFF) Local $iDL = 1, $iDC = 1, $iDR = 1, $iDG = 1, $iDB = 1 Local $sRoundings = "Round,Ceiling,Floor," If Not StringInStr($sRoundings, $sRounding) Then $sRounding = "Ceiling" If $iMaxL > 128 Then ;~ $iDL = Ceiling($iMaxL / 128) $iDL = Execute($sRounding & "(" & $iMaxL / 128 & ")") Else $iDL = 1 / (128 / $iMaxL) EndIf If $iMaxC > 128 Then ;~ $iDC = Ceiling($iMaxC / 128) $iDC = Execute($sRounding & "(" & $iMaxC / 128 & ")") Else $iDC = 1 / (128 / $iMaxC) EndIf If $iMaxR > 128 Then ;~ $iDR = Ceiling($iMaxR / 128) $iDR = Execute($sRounding & "(" & $iMaxR / 128 & ")") Else $iDR = 1 / (128 / $iMaxR) EndIf If $iMaxG > 128 Then ;~ $iDG = Ceiling($iMaxG / 128) $iDG = Execute($sRounding & "(" & $iMaxG / 128 & ")") Else $iDG = 1 / (128 / $iMaxG) EndIf If $iMaxB > 128 Then ;~ $iDB = Ceiling($iMaxB / 128) $iDB = Execute($sRounding & "(" & $iMaxB / 128 & ")") Else $iDB = 1 / (128 / $iMaxB) EndIf For $i = 1 To 256 _GDIPlus_PenSetColor($hPen_H, 0xFF000000) _GDIPlus_GraphicsDrawLine($hCtxt_HL, $i - 1, 127, $i - 1, 127 - (Int($aLuminosity[$i] / $iDL)), $hPen_H) _GDIPlus_PenSetColor($hPen_H, 0xFF000000) _GDIPlus_GraphicsDrawLine($hCtxt_HC, $i - 1, 127, $i - 1, 127 - (Int($aCRGB[$i] / $iDC)), $hPen_H) _GDIPlus_PenSetColor($hPen_H, 0xFFB00000) If $bIsAlpha Then _GDIPlus_GraphicsDrawLine($hCtxt_HR, $i - 1, 127, $i - 1, 127 - (Int(DllStructGetData($tStructChannel1, "channel1", $i) / $iDR)), $hPen_H) _GDIPlus_PenSetColor($hPen_H, 0xFF00B000) _GDIPlus_GraphicsDrawLine($hCtxt_HG, $i - 1, 127, $i - 1, 127 - (Int(DllStructGetData($tStructChannel2, "channel2", $i) / $iDG)), $hPen_H) _GDIPlus_PenSetColor($hPen_H, 0xFF0000B0) _GDIPlus_GraphicsDrawLine($hCtxt_HB, $i - 1, 127, $i - 1, 127 - (Int(DllStructGetData($tStructChannel3, "channel3", $i) / $iDB)), $hPen_H) Else _GDIPlus_GraphicsDrawLine($hCtxt_HR, $i - 1, 127, $i - 1, 127 - (Int(DllStructGetData($tStructChannel0, "channel0", $i) / $iDR)), $hPen_H) _GDIPlus_PenSetColor($hPen_H, 0xFF00B000) _GDIPlus_GraphicsDrawLine($hCtxt_HG, $i - 1, 127, $i - 1, 127 - (Int(DllStructGetData($tStructChannel1, "channel1", $i) / $iDG)), $hPen_H) _GDIPlus_PenSetColor($hPen_H, 0xFF0000B0) _GDIPlus_GraphicsDrawLine($hCtxt_HB, $i - 1, 127, $i - 1, 127 - (Int(DllStructGetData($tStructChannel2, "channel2", $i) / $iDB)), $hPen_H) EndIf Next EndFunc ;==>_GDIPlus_CreateHistogram Func _GDIPlus_CreateColorGradient($sColorChannel, $iW, $iH, $bHBitmap = True) Local $hBitmap = _GDIPlus_BitmapCreateFromScan0($iW, $iH) Local $hCtxt = _GDIPlus_ImageGetGraphicsContext($hBitmap) ;~ _GDIPlus_GraphicsSetSmoothingMode($hCtxt, 2) Local $hPen = _GDIPlus_PenCreate() _GDIPlus_PenSetLineJoin($hPen, $iLineJoinBevel) Local $i, $c For $i = 0 To $iW $c = Hex($i, 2) Switch $sColorChannel Case "L" _GDIPlus_PenSetColor($hPen, "0xFF" & $c & $c & $c) Case "C" _GDIPlus_PenSetColor($hPen, "0xFF" & $c & $c & $c) Case "R" _GDIPlus_PenSetColor($hPen, "0xFF" & $c & "0000") Case "G" _GDIPlus_PenSetColor($hPen, "0xFF00" & $c & "00") Case "B" _GDIPlus_PenSetColor($hPen, "0xFF0000" & $c) EndSwitch _GDIPlus_GraphicsDrawLine($hCtxt, $i, 0, $i, $iH, $hPen) Next _GDIPlus_GraphicsDispose($hCtxt) _GDIPlus_PenDispose($hPen) If $bHBitmap Then Local $hHBmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap) _GDIPlus_BitmapDispose($hBitmap) Return $hHBmp EndIf Return $hBitmap EndFunc ;==>_GDIPlus_CreateColorGradient Func _GDIPlus_CreateCheckerboardTexture($iSquareSize = 80, $iCol1 = 0xFFECECEC, $iCol2 = 0xFFDFDFDF) Local $hPatternBitmap = _GDIPlus_BitmapCreateFromScan0(2 * $iSquareSize, 2 * $iSquareSize) Local $hCtxt = _GDIPlus_ImageGetGraphicsContext($hPatternBitmap) Local $hBrush0 = _GDIPlus_BrushCreateSolid($iCol1) Local $hBrush1 = _GDIPlus_BrushCreateSolid($iCol2) _GDIPlus_GraphicsFillRect($hCtxt, 0, 0, $iSquareSize, $iSquareSize, $hBrush0) _GDIPlus_GraphicsFillRect($hCtxt, $iSquareSize, 0, $iSquareSize, $iSquareSize, $hBrush1) _GDIPlus_GraphicsFillRect($hCtxt, 0, $iSquareSize, $iSquareSize, $iSquareSize, $hBrush1) _GDIPlus_GraphicsFillRect($hCtxt, $iSquareSize, $iSquareSize, $iSquareSize, $iSquareSize, $hBrush0) Local $hTexture = _GDIPlus_TextureCreate($hPatternBitmap) _GDIPlus_GraphicsDispose($hCtxt) _GDIPlus_BitmapDispose($hPatternBitmap) _GDIPlus_BitmapDispose($hBrush0) _GDIPlus_BitmapDispose($hBrush1) Return $hTexture EndFunc ;==>_CreatePatternTexture  
      GdipBitmapGetHistogram(), GdipBitmapGetHistogramSize() and 2 more GDI+ functions requires GDI+ v1.1 which means this will only run on Vista or higher operating systems.
      I hope the values are correct especially the value for luminosity.
      Br,
      UEZ
    • Xandy
      By Xandy
      I have a bunch of SDL_Surfaces loaded into memory.  I want to list them in a ComboBox.
      Using this code, I can load images to the combobox from file but not from existing SDL_Surface memory
      CODE ISN"T MEANT TO RUN
      ; Create combobox pic list example $aControl[$iControl_id][$eControl_data] = _GUICtrlComboBoxEx_Create($gui, $data_value, $data_x, $data_y, $data_w, $data_h, $CBS_DROPDOWNLIST) ; Image List for ComboboxEx Local $hImage = _GUIImageList_Create($gTile_w, $gTile_h, 6) ; Fill hImage with SDL_Surfaces stored in memory: aNPC_surf[scale][type][way][frame] For $i = 0 To 1 ; This works but only from file: ;_GUIImageList_AddBitmap($hImage, $gFolder_graphics & $gaWorld_info[$player.iWorld_cur][$eWi_filename] & "Tiles\" & $i & ".bmp") ; I've broken up a sprite sheet and want to insert into combobox from memory ;_GUIImageList_Add($hImage, $aNPC_surf[1][1][1][$i]) _GUIImageList_Add($hImage, _GDIPlus_BitmapCreateFromMemory($aNPC_surf[1][1][1][$i]), True) ;_GDIPlus_BitmapCreateFromMemory($aNPC_surf[1][1][1][$i]) ; Add the index number to combobox item _GUICtrlComboBoxEx_AddString($aControl[$eNPC_iPic_type][$eControl_data], $i, $i, $i) Next ; Set hImage list to combobox control _GUICtrlComboBoxEx_SetImageList($aControl[$eNPC_iPic_type][$eControl_data], $hImage) Anyone know if I can convert from SDL_Surface to hBitmap?  Maybe I'm doing something else wrong.
       
      I've seen hBitmap converted to SDL_Surface but I don't really understand it yet: 
       
      My full script can be found here:
       
    • Daka
      By Daka
      I developing simple script for one program to do the automatic clicks.
      Now I developed on xp and win 7, but on win 10 gives me error.

      But the point for me to make it clear and not having basic mistake
      with choosing x64 and x86 system.
      So if I choose x64 system, does it going to work on x86?
      when compiling do I need to check or uncheck x64 compatibilities?
      Right now I'm installing new autoit v3 full install and it ask me what version to do like:
      - use native x64 tools by default
      - Use x86 tools by default (recommanded for compatibility)
      I assume I should use x86, because this is for 32bit machines?

      I know those are the basic questions of building the application for windows or any OS,
      but I didn't do any of this and if someone can give me information, just to do point,
      use always that or this, it would be great!

       
    • SimTheNo1
      By SimTheNo1
      I am busy with building a solution for change monitoring of VOIP call program and to be properly automated means among others need for some limited OCR functionality. Current works of others are way too much overkill for this case what makes the need to build it myself. But to do it properly I significantly have to increase my knowledge about digital graphics management.
      For now I already have discovered here and there some mind blowing  Autoit miracles what can be achieved with Windows own possibilities to manipulate that what is output to the monitor. And as far I can judge there are 2 options to process graphics without use of any external libraries like ImageMagick, FreeImage and so on. These are:
       WinAPI  GDIPlus It is for me quite obvious to have various holes in general understanding of graphics and it is once more very clear what advantages gives consistent general study in 1 or another official institutions like University.  Cause there you are introduced into certain domain of knowledge in a way which usually has been perfected over long period of time. So you are not overloaded by stuff which requires a certain amount of information to be initially clear for you. For example, before starting to solve physics you first learn to read, count and so on and then move to subjects like physics.
      Though in my case opportunity to study in such educational system I had only for 8 years, from my 7th to 15th year of age in the country that was falling apart now Ukraine but used to be USSR, was all what it was. After have immigrated to Netherlands possibilities to study further haven't occur. And this therefor causes often various implications when going deep in that or another field of practical knowledge acquiring for any needed physical result, like programming to perform enormous amount of tasks. In this particular case automating VOIP call program.
      Anyway, right now I think the best direction to move is to concentrate on as basic as possible image management and if someone would maybe explain in general what is a pixel will definitely help. Particularly I am very curious about how to do picture manipulations in Autoit. Especially would help a lot to produce eventually following functions:
      createImage($imageFileName, $width, $height, $color) readImagePixel($imageFileName, $x, $y)  writeImagePixel($imageFileName, $x, $y) I do not know exactly how image is handled in computer but preferably above mentioned functions should deal with so far possible origin of graphics creation on computer. But nevertheless I definitely would love to hear any proposition for solution.
      The problem with explaining screenshots:
       VOIPConnect full Window  Part of Window with control to monitor for changes  Exact location of area where actual changes occur and have to be processed  It comes down to a rectangle of approximately 51 pixel wide and 7 pixel high. In fact if I get to learn as far as to be able exactly read, write and compare 2 images consistently across different computers I could narrow down then even further the area to watch as little as a square of 2-5 pixels wide. 
      To finish here is last detail about particularly no need for ultra fast solution at all. This because it is needed only once when it is first run on a new computer and to have to wait few minutes while it is being set instead of just a few seconds make no sence.
      This is it and what I too think to do beside this very particular case is to purify out beautiful generic Autoit functions for core image manipulation by using WinAPI or/and GDIPlus. 
    • UritOR
      By UritOR
      Hi all,
      I'm trying to find how to draw an image (create a label in mspaint) and save it as .bmp. And set it to be a wallpaper
      I also need to know what wallpaper is use now, for restore it when the script is closed.
      What is the best way to do it? Do I need Gdi+ for it?  I use this code (which I found here in forum) to set a .bmp file as wallpaper:
      Func _ChangeWallpaper($sFile,$iType) ; Changes the wallpaper to $sFilename using $iType as: ; 1 Tiled ; 2 Centered ; 3 Stretched ; any other value (usually 0) unchanged ; ; Returns ; 0 if everything is allright. ; -1 if $sFile does not exist. @error is set to 1 ; -2 if £sFile is not a .bmp file. @error is set to 2 If Not FileExists($sFile) Then SetError(1) Return -1 EndIf If StringTrimLeft($sFile,StringInStr($sFile,'.',0,-1)) <> 'bmp' Then SetError(2) Return -2 EndIf Select Case $iType = 1 RegWrite('HKCU\Control Panel\Desktop','TileWallpaper','reg_sz','1') RegWrite('HKCU\Control Panel\Desktop','WallpaperStyle','reg_sz','0') Case $iType = 2 RegWrite('HKCU\Control Panel\Desktop','TileWallpaper','reg_sz','0') RegWrite('HKCU\Control Panel\Desktop','WallpaperStyle','reg_sz','0') Case $iType = 3 RegWrite('HKCU\Control Panel\Desktop','TileWallpaper','reg_sz','0') RegWrite('HKCU\Control Panel\Desktop','WallpaperStyle','reg_sz','2') Case Else EndSelect RegWrite('HKCU\Control Panel\Desktop','Wallpaper','reg_sz',$sFile) DllCall("User32.dll","int","SystemParametersInfo","int",20,"int",0,"str",$sFile,"int",0) Return 0 EndFunc  
      The flow that I understand should be:
      1) Check what wallpaper is in use now. I can find the path to it by going to registry  key named "wallpaper" here :[HKEY_CURRENT_USER\Control Panel\Desktop]
      2) Create a file mspaint
      3) Create a label with its properties (size,color) in this file
      3) Save this file in some temporary directory
      4) Set a created file as a wallpaper using the above code
      5) When the script is closed restore the old wallpaper.
       
      So again, my question is how to create a .bmp file with label in it?
×