#include <GDIPlus.au3>
#include <GuiConstants.au3>
#include <GDIP.au3>
#include <ComboConstants.au3>
#include <WindowsConstants.au3>
#Include <GuiComboBox.au3>

Global $hBitmap, $Precision = 5, $hBrush, $hGraphic, $hBitmap2, $hBuffer, $Color,$Form,$Char

_GDIPlus_Startup()

$hGui = GUICreate("Bild-Wandler", 600, 100, 192, 124)
$hLoad = GUICtrlCreateButton("Lade Bild", 0, 0, 100, 50)
$hConvert = GUICtrlCreateButton("Konvertiere", 100, 0, 100, 50)
$hSave = GUICtrlCreateButton("Speichern", 200, 0, 100, 50)

GUICtrlCreateLabel("Präzision:", 300, 0, 100, 25)
$hPrecCombo = GuiCtrlCreateCombo("Mittel", 300, 25, 100, 25, $CBS_DROPDOWNLIST)
GuiCtrlSetData(-1, "Niedrig|Genau|Sehr Genau")
Global $ComboPrecData[4] = [12,18,8,5]

GUICtrlCreateLabel("Form:", 400, 0, 100, 25)
$hFormCombo = GuiCtrlCreateCombo("Kreis", 400, 25, 100, 25, $CBS_DROPDOWNLIST)
GuiCtrlSetData(-1, "Quadrat|Eigener Buchstabe|ASCII")
Global $hFormat = _GDIPlus_StringFormatCreate ()
Global $hFamily = _GDIPlus_FontFamilyCreate ("Arial")

GUICtrlCreateLabel("Farbe:", 500, 0, 100, 25)
$hColorCombo = GuiCtrlCreateCombo("Schwarz", 500, 25, 100, 25, $CBS_DROPDOWNLIST)
GuiCtrlSetData(-1, "Blau|Grün|Rot|Gelb")
Global $ComboColorData[5] = [0xFF000000,0xFF0000FF,0xFF00FF00,0xFFFF0000,0xFFFFFF00]

$hProgress = GuiCtrlCreateProgress(0, 50, 600, 50)
GUISetState(@SW_SHOW)

GuiCtrlSetState($hConvert, $GUI_DISABLE)
GuiCtrlSetState($hSave, $GUI_DISABLE)
$hGui2 = GuiCreate("Anzeige", 100, 100, -1, -1, $WS_BORDER, -1, $hGui)
$hBrush = _GDIPlus_BrushCreateSolid(0xFF000000)

While 1
	$nMsg = GuiGetMsg($hGui)
	Switch $nMsg
		Case $GUI_EVENT_CLOSE
			_GDIPlus_GraphicsDispose($hGraphic)
			_GDIPlus_BitmapDispose($hBitmap)
			_GDIPlus_BitmapDispose($hBitmap2)
			_GDIPlus_BrushDispose($hBrush)
			Exit
		Case $hLoad
			$sOpen = FileOpenDialog("Wähle Bild", @ScriptDir, "Bild (*.bmp;*.jpg;*.png)")
			$hBitmap = _GDIPlus_BitmapCreateFromFile($sOpen)
			GuiCtrlSetState($hSave, $GUI_DISABLE)
			If $hBitmap = 0 Then
				MsgBox(0, "Fehler", "Fehler beim Laden des Bilds")
				GuiCtrlSetState($hConvert, $GUI_DISABLE)
			Else
				$iHeight = _GDIPlus_ImageGetHeight($hBitmap)
				$iWidth = _GDIPlus_ImageGetWidth($hBitmap)
				WinMove("Anzeige", "", Default, Default, $iWidth, $iHeight)
				GuiSetState(@SW_SHOW, $hGui2)
				$hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGui2)
				_GDIPlus_GraphicsDrawImage($hGraphic, $hBitmap, 0, 0)
				GuiCtrlSetState($hConvert, $GUI_ENABLE)
			EndIf
		Case $hConvert
			_GDIPlus_GraphicsClear($hGraphic, 0xFFFFFFFF)
			$hBitmap2 = _GDIPlus_BitmapCreateFromGraphics($iWidth, $iHeight, $hGraphic)
			$hBuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap2)
			_GDIPlus_GraphicsSetSmoothingMode($hBuffer, 2)
			_GDIPlus_GraphicsClear($hBuffer, 0xFFFFFFFF)
			GuiCtrlSetstate($hProgress, 0)
			$Precision = $ComboPrecData[_GUICtrlComboBox_GetCurSel($hPrecCombo)]
			_GDIPlus_BrushSetSolidColor($hBrush,$ComboColorData[_GUICtrlComboBox_GetCurSel($hColorCombo)])
			$Form = _GUICtrlComboBox_GetCurSel($hFormCombo)
			If $Form = 2 Then
					$Char = StringLeft(InputBox("Eigener Buchstabe","Bitte geben Sie einen Buchstaben ein!"),1)
			EndIf
			If $Form = 3 Then
					$Char = Chr(InputBox("ASCII","Bitte geben Sie einen ASCII Code zwischen 0 und 255 ein!"))
			EndIf
			For $i = 0 to $iWidth/$Precision - 1
				For $j = 0 to $iHeight/$Precision - 1
					_Draw($Form,_GetGrey($i * $Precision, $j * $Precision, $Precision, $Precision))
				Next
				GuiCtrlSetData($hProgress, ($i+1) * 100 / ($iWidth/$Precision))
			Next
			_GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap2, 0, 0, $iWidth, $iHeight)
			GuiCtrlSetData($hProgress, 100)
			GuiCtrlSetState($hSave, $GUI_ENABLE)
		Case $hSave
			$sSave = FileSaveDialog("Speichern", @ScriptDir, "Bild (*.bmp;*.jpg;*.png)")
			If Not $sSave = "" Then
				Local $Format = StringRight($sSave, 4)
				If ($Format <> ".bmp") And ($Format <> ".jpg") And ($Format <> ".png") Then $sSave &= ".bmp"
				_GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap2, 0, 0, $iWidth, $iHeight)
				If _GDIPlus_ImageSaveToFile($hBitmap2, $sSave) Then
					MsgBox(0, "Speichern", "Erfolgreich gespeichert!")
				Else
					MsgBox(0, "Speichern", "Speichern fehlgeschlagen!")
				Endif
			EndIf
	EndSwitch
WEnd

Func _GetGrey($iX, $iY, $Width, $Height)
	Local $PixCol, $Count = 0
	For $i = $iX to $Width + $iX - 1
		For $j = $iY to $Height + $iY - 1
			$PixCol = StringTrimLeft(Hex(_GDIPlus_BitmapGetPixel($hBitmap, $i, $j)), 2)
			$Count += Dec(StringLeft($PixCol, 2)) + Dec(StringMid($PixCol, 3, 2)) + Dec(StringRight($PixCol, 2))
		Next
	Next
	Return $Count / ($Width*$Height) / 3
EndFunc

Func _Draw($Form,$Grey)
	Switch $Form
		Case 0
			_GDIPlus_GraphicsFillEllipse($hBuffer, $i * $Precision + 0.5 * ($precision - ((255 - $Grey) / (255 / $Precision))), $j * $precision + 0.5 * ($precision - ((255 - $Grey) / (255 / $Precision))), (255 - $Grey) / (255 / $Precision), (255 - $Grey) / (255 / $Precision), $hBrush)
		Case 1
			_GDIPlus_GraphicsFillRect($hBuffer, $i * $Precision + 0.5 * ($precision - ((255 - $Grey) / (255 / $Precision))), $j * $precision + 0.5 * ($precision - ((255 - $Grey) / (255 / $Precision))), (255 - $Grey) / (255 / $Precision), (255 - $Grey) / (255 / $Precision), $hBrush)
		Case 2 or 3
			$hFont = _GDIPlus_FontCreate ($hFamily,(255 - $Grey) / (255 / $Precision), 0,2)
			$tLayout = _GDIPlus_RectFCreate ($i * $Precision + 0.5 * ($precision - ((255 - $Grey) / (255 / $Precision))), $j * $precision + 0.5 * ($precision - ((255 - $Grey) / (255 / $Precision))), (255 - $Grey) / (255 / $Precision), (255 - $Grey) / (255 / $Precision))
			_GDIPlus_GraphicsDrawStringEx ($hBuffer, $Char, $hFont, $tLayout, $hFormat, $hBrush)
	EndSwitch
EndFunc