Sign in to follow this  
Followers 0
qwert

Need help on ColorMatrix usage

2 posts in this topic

I've been trying to get a handle on using GDI+ to adjust the brightness, contrast and saturation of images.  I've been able to get adjustments to work individually, but not together.  After several attempts at it, I've realized I may be taking a wrong approach.

Here's what I have, so far:

;
;           Simple slider to control image brightness
;
#include <GUIConstantsEx.au3>
#include <ScreenCapture.au3>

Example()

Func Example()
    _GDIPlus_Startup()  ; initialize GDI+

    Local Const $iWidth = 720, $iHeight = 600

    Local $hHBmp = _ScreenCapture_Capture("", 0, 0, $iWidth, $iHeight) ;create a GDI bitmap by capturing an area on desktop
    Local $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hHBmp) ;convert GDI to GDI+ bitmap
    _WinAPI_DeleteObject($hHBmp) ;release GDI bitmap resource because not needed anymore

    Local $hGUI = GUICreate("GDI+ Image Brightness Control", $iWidth, $iHeight + 100) ;create a test GUI
    GUISetState(@SW_SHOW)

$label = GUICtrlCreateLabel( "Brightness", 20, 630, 200, 20)
$slider = GUICtrlCreateSlider(20, 650, 200, 25)
$label2 = GUICtrlCreateLabel( "Contrast", 260, 630, 200, 20)
$slider2 = GUICtrlCreateSlider(260, 650, 200, 25)
GUICtrlSetData(-1, 50)
$label3 = GUICtrlCreateLabel( "Saturation", 500, 630, 200, 20)
$slider3 = GUICtrlCreateSlider(500, 650, 200, 25)
GUICtrlSetData(-1, 100)

    Local $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGUI) ;create a graphics object from a window handle
    Local $hIA = _GDIPlus_ImageAttributesCreate() ;create an ImageAttribute object

    Local ;
;            Simple slider to control image brightness
;
#include <GUIConstantsEx.au3>
#include <ScreenCapture.au3>

Example()

Func Example()
    _GDIPlus_Startup()     ; initialize GDI+

    Local Const $iWidth = 720, $iHeight = 600

    Local $hHBmp = _ScreenCapture_Capture("", 0, 0, $iWidth, $iHeight) ;create a GDI bitmap by capturing an area on desktop
    Local $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hHBmp) ;convert GDI to GDI+ bitmap
    _WinAPI_DeleteObject($hHBmp) ;release GDI bitmap resource because not needed anymore

    Local $hGUI = GUICreate("GDI+ Image Brightness Control", $iWidth, $iHeight + 100) ;create a test GUI
    GUISetState(@SW_SHOW)

$label = GUICtrlCreateLabel( "Brightness", 20, 630, 200, 20)
$slider = GUICtrlCreateSlider(20, 650, 200, 25)
$label2 = GUICtrlCreateLabel( "Contrast", 260, 630, 200, 20)
$slider2 = GUICtrlCreateSlider(260, 650, 200, 25)
GUICtrlSetData(-1, 50)
$label3 = GUICtrlCreateLabel( "Saturation", 500, 630, 200, 20)
$slider3 = GUICtrlCreateSlider(500, 650, 200, 25)
GUICtrlSetData(-1, 100)

    Local $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGUI) ;create a graphics object from a window handle
    Local $hIA = _GDIPlus_ImageAttributesCreate() ;create an ImageAttribute object

    Local $tColorMatrix = _GDIPlus_ColorMatrixCreate() ;create color matrix
MsgBox(0, "Should this return something?", Hex($tColorMatrix), 0)

    Local $tColorMatrix = _GDIPlus_ColorMatrixCreateTranslate(.75, .75, .75) ;use translation color matrix to change the image brightness to dim
    _GDIPlus_ImageAttributesSetColorMatrix($hIA, 0, True, $tColorMatrix) ;adapt the modified color matrix
    _GDIPlus_GraphicsDrawImageRectRect($hGraphics, $hBitmap, 0, 0, $iWidth, $iHeight, 0, 0, $iWidth, $iHeight, $hIA) ;draw the bitmap while applying the color adjustment
Sleep(1000)

    Local $tColorMatrix = _GDIPlus_ColorMatrixCreateTranslate(0, 0, 0) ;use translation color matrix to change the image brightness back to normal
    _GDIPlus_ImageAttributesSetColorMatrix($hIA, 0, True, $tColorMatrix) ;adapt the modified color matrix
    _GDIPlus_GraphicsDrawImageRectRect($hGraphics, $hBitmap, 0, 0, $iWidth, $iHeight, 0, 0, $iWidth, $iHeight, $hIA) ;draw the bitmap while applying the color adjustment
Sleep(1000)

; now use slider to control brightness
    Do
     $bright = GUICtrlRead($slider) /100
     $contrast = GUICtrlRead($slider2) /100
     $saturate = GUICtrlRead($slider3) /100
; brightness
    Local  $tColorMatrix = _GDIPlus_ColorMatrixCreateTranslate($bright, $bright, $bright)        ;this works
;    DllStructSetData($tColorMatrix, "m", $bright, 0)
;    DllStructSetData($tColorMatrix, "m", $bright, 1)                                            ;these don't.   Should they have?
;    DllStructSetData($tColorMatrix, "m", $bright, 2)
    _GDIPlus_ImageAttributesSetColorMatrix($hIA, 0, True, $tColorMatrix)                         ;apply the first modified color matrix
    _GDIPlus_GraphicsDrawImageRectRect($hGraphics, $hBitmap, 0, 0, $iWidth, $iHeight, 0, 0, $iWidth, $iHeight, $hIA) ;draw the bitmap while applying the color adjustment
; contrast ... doesn't work unless others are disabled <<<<<<<<<<<<
;    Local $cColorMatrix = _GDIPlus_ColorMatrixCreateScale($contrast, $contrast, $contrast)     ;create the scale factors
;    _GDIPlus_ImageAttributesSetColorMatrix($hIA, 0, True, $cColorMatrix)                     ;apply the second modified color matrix
;    _GDIPlus_GraphicsDrawImageRectRect($hGraphics, $hBitmap, 0, 0, $iWidth, $iHeight, 0, 0, $iWidth, $iHeight, $hIA) ;draw the bitmap while applying the color adjustment
; saturation ... doesn't work unless others are disabled <<<<<<<<<<<
;    Local  $sColorMatrix = _GDIPlus_ColorMatrixCreateSaturation($saturate)                     ;create saturation color matrix
;    _GDIPlus_ImageAttributesSetColorMatrix($hIA, 0, True, $sColorMatrix)                     ;apply the third modified color matrix
;    _GDIPlus_GraphicsDrawImageRectRect($hGraphics, $hBitmap, 0, 0, $iWidth, $iHeight, 0, 0, $iWidth, $iHeight, $hIA) ;draw the bitmap while applying the color adjustment
;
    Sleep(10)
    Until GUIGetMsg() = $GUI_EVENT_CLOSE

    ;cleanup GDI+ resources
    _GDIPlus_ImageAttributesDispose($hIA)
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_Shutdown()
    GUIDelete($hGUI)
EndFunc   ;==>Example

 = _GDIPlus_ColorMatrixCreate() ;create color matrix
MsgBox(0, "Should this return something?", Hex($tColorMatrix), 0)

    Local $tColorMatrix = _GDIPlus_ColorMatrixCreateTranslate(.75, .75, .75) ;use translation color matrix to change the image brightness to dim
    _GDIPlus_ImageAttributesSetColorMatrix($hIA, 0, True, $tColorMatrix) ;adapt the modified color matrix
    _GDIPlus_GraphicsDrawImageRectRect($hGraphics, $hBitmap, 0, 0, $iWidth, $iHeight, 0, 0, $iWidth, $iHeight, $hIA) ;draw the bitmap while applying the color adjustment
Sleep(1000)

    Local $tColorMatrix = _GDIPlus_ColorMatrixCreateTranslate(0, 0, 0) ;use translation color matrix to change the image brightness back to normal
    _GDIPlus_ImageAttributesSetColorMatrix($hIA, 0, True, $tColorMatrix) ;adapt the modified color matrix
    _GDIPlus_GraphicsDrawImageRectRect($hGraphics, $hBitmap, 0, 0, $iWidth, $iHeight, 0, 0, $iWidth, $iHeight, $hIA) ;draw the bitmap while applying the color adjustment
Sleep(1000)

; now use slider to control brightness
    Do
     $bright = GUICtrlRead($slider) /100
     $contrast = GUICtrlRead($slider2) /100
     $saturate = GUICtrlRead($slider3) /100
; brightness
    Local  $tColorMatrix = _GDIPlus_ColorMatrixCreateTranslate($bright, $bright, $bright)       ;this works
;   DllStructSetData($tColorMatrix, "m", $bright, 0)
;   DllStructSetData($tColorMatrix, "m", $bright, 1)                                            ;these don't.   Should they have?
;   DllStructSetData($tColorMatrix, "m", $bright, 2)
    _GDIPlus_ImageAttributesSetColorMatrix($hIA, 0, True, $tColorMatrix)                         ;apply the first modified color matrix
    _GDIPlus_GraphicsDrawImageRectRect($hGraphics, $hBitmap, 0, 0, $iWidth, $iHeight, 0, 0, $iWidth, $iHeight, $hIA) ;draw the bitmap while applying the color adjustment
; contrast ... doesn't work unless others are disabled <<<<<<<<<<<<
;   Local $cColorMatrix = _GDIPlus_ColorMatrixCreateScale($contrast, $contrast, $contrast)  ;create the scale factors
;   _GDIPlus_ImageAttributesSetColorMatrix($hIA, 0, True, $cColorMatrix)                    ;apply the second modified color matrix
;   _GDIPlus_GraphicsDrawImageRectRect($hGraphics, $hBitmap, 0, 0, $iWidth, $iHeight, 0, 0, $iWidth, $iHeight, $hIA) ;draw the bitmap while applying the color adjustment
; saturation ... doesn't work unless others are disabled <<<<<<<<<<<
;   Local  $sColorMatrix = _GDIPlus_ColorMatrixCreateSaturation($saturate)                  ;create saturation color matrix
;   _GDIPlus_ImageAttributesSetColorMatrix($hIA, 0, True, $sColorMatrix)                    ;apply the third modified color matrix
;   _GDIPlus_GraphicsDrawImageRectRect($hGraphics, $hBitmap, 0, 0, $iWidth, $iHeight, 0, 0, $iWidth, $iHeight, $hIA) ;draw the bitmap while applying the color adjustment
;
    Sleep(10)
    Until GUIGetMsg() = $GUI_EVENT_CLOSE

    ;cleanup GDI+ resources
    _GDIPlus_ImageAttributesDispose($hIA)
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_Shutdown()
    GUIDelete($hGUI)
EndFunc   ;==>Example

The problems I have are:

1) only one of the adjusts will work at a time ... so I only have brightness enabled in my example

2) contrast doesn't work as expected.  It seems to be an overlay effect.

As a point of confusion for me, I don't understand what is being returned in the first $tColorMatrix statement.  I thought it was a pointer to the color matrix, itself.  But (apparently) it's a null value.

As a reference, here's the best explanation I've found of the matrix, itself:  http://docs.rainmeter.net/tips/colormatrix-guide

Thanks in advance for any help.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Check out these links: '?do=embed' frameborder='0' data-embedContent>> or '?do=embed' frameborder='0' data-embedContent>>

 

 

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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
Sign in to follow this  
Followers 0