#Include <APIConstants.au3>
#Include <Array.au3>
#Include <GDIPlus.au3>
#Include <WinAPIEx.au3>
$W = 195
$H = 105
$C = 15
Dim $Matrix[$H / $C][$W / $C]
Dim $HLS[3]
_GDIPlus_Startup()
$hImage = _GDIPlus_ImageLoadFromFile(@ScriptDir & '\195x105x15.jpg')
$hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
_GDIPlus_ImageDispose($hImage)
_GDIPlus_Shutdown()
$hSrcDC = _WinAPI_CreateCompatibleDC(0)
$hSrcSv = _WinAPI_SelectObject($hSrcDC, $hBitmap)
$hDstDC = _WinAPI_CreateCompatibleDC(0)
$hDib = _WinAPI_CreateDIB(1, 1)
$hDstSv = _WinAPI_SelectObject($hDstDC, $hDib)
_WinAPI_SetStretchBltMode($hDstDC, $HALFTONE)
For $y = 0 To $H / $C - 1
For $x = 0 To $W / $C - 1
_WinAPI_StretchBlt($hDstDC, 0, 0, 1, 1, $hSrcDC, $x * $C, $y * $C, $C, $C, $SRCCOPY)
$RGB = _WinAPI_GetPixel($hDstDC, 0, 0)
_WinAPI_ColorRGBToHLS($RGB, $HLS[0], $HLS[1], $HLS[2])
$Matrix[$y][$x] = $HLS[1]
; Visual representation
$hDC = _WinAPI_GetDC(0)
$tRect = _WinAPI_CreateRectEx($x * $C, $y * $C, $C, $C)
$hBrush = _WinAPI_CreateSolidBrush(_WinAPI_SwitchColor($RGB))
_WinAPI_FillRect($hDC, DllStructGetPtr($tRect), $hBrush)
_WinAPI_DeleteObject($hBrush)
_WinAPI_ReleaseDC(0, $hDC)
Next
Next
_WinAPI_SelectObject($hSrcDC, $hSrcSv)
_WinAPI_DeleteObject($hBitmap)
_WinAPI_DeleteDC($hSrcDC)
_WinAPI_SelectObject($hDstDC, $hDstSv)
_WinAPI_DeleteObject($hDib)
_WinAPI_DeleteDC($hDstDC)
_ArrayDisplay($Matrix, 'Luminance')
$Min = 240
$Max = 0
For $y = 0 To $H / $C - 1
For $x = 0 To $W / $C - 1
If $Min > $Matrix[$y][$x] Then
$Min = $Matrix[$y][$x]
EndIf
If $Max < $Matrix[$y][$x] Then
$Max = $Matrix[$y][$x]
EndIf
Next
Next
$Q = Round(($Min + $Max) / 2)
For $y = 0 To $H / $C - 1
For $x = 0 To $W / $C - 1
$Matrix[$y][$x] = Number($Matrix[$y][$x] < $Q)
Next
Next
_ArrayDisplay($Matrix, 'Matrix')
_WinAPI_InvalidateRect(ControlGetHandle('[CLASS:Progman;TITLE:Program Manager]', '', 'SysListView321'))