Jump to content

monochrome bitmap


bogQ
 Share

Recommended Posts

Here another method using Authenticity's great GDIP.au3 (taken from Image Tool.au3) which

should also work with WinXP:

#include <GDIPlus.au3>

$file = FileOpenDialog("Select an image", "", "Images (*.jpg;*.bmp;*.png;*.gif)")
If @error Then Exit
_GDIPlus_Startup()
$hBitmap = _GDIPlus_BitmapCreateFromFile($file)
$iW = _GDIPlus_ImageGetWidth($hBitmap)
$iH = _GDIPlus_ImageGetHeight($hBitmap)

; Colors luminace
Global Const $GDIP_RLUM = 0.3086
Global Const $GDIP_GLUM = 0.6094
Global Const $GDIP_BLUM = 0.0820

; $tagGDIPCOLORMATRIX
Global Const $tagGDIPCOLORMATRIX = "float m[25];"

$hBMP_2bpp = _GDIPlus_BitmapCreateFromScan0($iW, $iH)
$hContext = _GDIPlus_ImageGetGraphicsContext($hBMP_2bpp)
$hIA = _GDIPlus_ImageAttributesCreate()
_GDIPlus_ImageAttributesSetThreshold($hIA, 0, True, 0.5)
$tBWMatrix = _GDIPlus_ColorMatrixCreateGrayScale()
$pBWMatrix = DllStructGetPtr($tBWMatrix)

_GDIPlus_ImageAttributesSetColorMatrix($hIA, 0, True, $pBWMatrix)
_GDIPlus_GraphicsDrawImageRectRectIA($hContext, $hBitmap, 0, 0, $iW, $iH, 0, 0, $iW, $iH, $hIA)

$fsave = @ScriptDir & "\Test.png"
ConsoleWrite(_GDIPlus_ImageSaveToFile($hBMP_2bpp, $fsave) & @CRLF)

_GDIPlus_ImageAttributesDispose($hIA)
_GDIPlus_BitmapDispose($hBitmap)
_GDIPlus_BitmapDispose($hBMP_2bpp)
_GDIPlus_GraphicsDispose($hContext)
_GDIPlus_ShutDown()
ShellExecute(@ScriptDir & "\Test.png")
Exit

Func _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight, $iStride = 0, $iPixelFormat = 0x0026200A, $pScan0 = 0)
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $iWidth, "int", $iHeight, "int", $iStride, "int", $iPixelFormat, "ptr", $pScan0, "int*", 0)
    If @error Then Return SetError(@error, @extended, 0)
    Return $aResult[6]
EndFunc   ;==>_GDIPlus_BitmapCreateFromScan0

Func _GDIPlus_ImageAttributesCreate()
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateImageAttributes", "int*", 0)
    If @error Then Return SetError(@error, @extended, 0)
    Return $aResult[1]
EndFunc   ;==>_GDIPlus_ImageAttributesCreate

Func _GDIPlus_ImageAttributesDispose($hImageAttributes)
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipDisposeImageAttributes", "hwnd", $hImageAttributes)
    If @error Then Return SetError(@error, @extended, False)
    Return $aResult[0] = 0
EndFunc   ;==>_GDIPlus_ImageAttributesDispose

Func _GDIPlus_ImageAttributesSetThreshold($hImageAttributes, $iColorAdjustType = 0, $fEnable = False, $nThershold = 0)
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipSetImageAttributesThreshold", "hwnd", $hImageAttributes, "int", $iColorAdjustType, "int", $fEnable, "float", $nThershold)
    If @error Then Return SetError(@error, @extended, False)
    Return $aResult[0] = 0
EndFunc   ;==>_GDIPlus_ImageAttributesSetThreshold

Func _GDIPlus_ColorMatrixCreateGrayScale()
    Local $iI, $iJ, $tCM, $aLums[4] = [$GDIP_RLUM, $GDIP_GLUM, $GDIP_BLUM, 0]
    $tCM = DllStructCreate($tagGDIPCOLORMATRIX)
    For $iI = 0 To 3
        For $iJ = 1 To 3
            DllStructSetData($tCM, "m", $aLums[$iI], $iI * 5 + $iJ)
        Next
    Next
    DllStructSetData($tCM, "m", 1, 19)
    DllStructSetData($tCM, "m", 1, 25)
    Return $tCM
EndFunc   ;==>_GDIPlus_ColorMatrixCreateGrayScale

Func _GDIPlus_ImageAttributesSetColorMatrix($hImageAttributes, $iColorAdjustType = 0, $fEnable = False, $pClrMatrix = 0, $pGrayMatrix = 0, $iColorMatrixFlags = 0)
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipSetImageAttributesColorMatrix", "hwnd", $hImageAttributes, "int", $iColorAdjustType, "int", $fEnable, "ptr", $pClrMatrix, "ptr", $pGrayMatrix, "int", $iColorMatrixFlags)
    If @error Then Return SetError(@error, @extended, False)
    Return $aResult[0] = 0
EndFunc   ;==>_GDIPlus_ImageAttributesSetColorMatrix

Func _GDIPlus_GraphicsDrawImageRectRectIA($hGraphics, $hImage, $nSrcX, $nSrcY, $nSrcWidth, $nSrcHeight, $nDstX, $nDstY, $nDstWidth, $nDstHeight, $hImageAttributes = 0, $iUnit = 2)
    Local $aResult = DllCall($ghGDIPDll, "int", "GdipDrawImageRectRect", "hwnd", $hGraphics, "hwnd", $hImage, "float", $nDstX, "float", _
            $nDstY, "float", $nDstWidth, "float", $nDstHeight, "float", $nSrcX, "float", $nSrcY, "float", $nSrcWidth, "float", _
            $nSrcHeight, "int", $iUnit, "hwnd", $hImageAttributes, "int", 0, "int", 0)
    If @error Then Return SetError(@error, @extended, False)
    Return $aResult[0] = 0
EndFunc   ;==>_GDIPlus_GraphicsDrawImageRectRectIA

More information can be found in GDIP.au3!

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...