- Offizieller Beitrag
Ich möchte gern ermitteln, wie viele verschiedene Farben in einem Bild verwendet werden.
Dazu habe ich mir eine Funktion geschrieben, die alle Pixel durchgeht und in einem Dictionary die Farben speichert.
Das funktioniert auch recht gut, aber das dauert zu lange. Seht ihr vielleicht noch eine Möglichkeit, das schneller hinzukriegen?
AutoIt
#include <GDIPlus.au3>
Global $sImagefile = @ScriptDir & '\test.jpg'
_GDIPlus_Startup()
Global $hImage = _GDIPlus_BitmapCreateFromFile($sImagefile)
$iTimer = TimerInit()
$iCount = _GetUniqueColors($hImage)
ConsoleWrite('Zeit: ' & Int(TimerDiff($iTimer)) & ' ms' & @CR)
ConsoleWrite('Anzahl der Farben: ' & $iCount & @CR)
_GDIPlus_BitmapDispose($hImage)
_GDIPlus_Shutdown()
Exit
Func _GetUniqueColors(ByRef $hImage)
Local $iW, $iH, $tBitmapData, $pScan0, $tPixel, $iOffset, $iColor, $oUnique = ObjCreate('Scripting.Dictionary')
$iW = _GDIPlus_ImageGetWidth($hImage)
$iH = _GDIPlus_ImageGetHeight($hImage)
$tBitmapData = _GDIPlus_BitmapLockBits($hImage, 0, 0, $iW, $iH, $GDIP_ILMREAD, $GDIP_PXF32ARGB)
$pScan0 = DllStructGetData($tBitmapData, 'Scan0')
$tPixel = DllStructCreate('uint[' & $iW * $iH & '];', $pScan0)
ConsoleWrite('$iW * $iH = ' & $iW * $iH & ' px' & @CRLF)
For $iY = 0 To $iH - 1
$iOffset = $iY * $iW + 1
For $iX = 0 To $iW - 1
$iColor = Hex(DllStructGetData($tPixel, 1, $iOffset + $iX), 8)
$oUnique.Item($iColor) = 0
Next
Next
_GDIPlus_BitmapUnlockBits($hImage, $tBitmapData)
$tPixel = 0
Return $oUnique.Count
EndFunc
Alles anzeigen
Als Ergebnis bekomme ich:
Fast 5 Sekunden für so ein kleines Bild dauert mir zu lange.