Jump to content

Picture Effects (Swap, Add, Substract,Change ARGB with Color Matrix)


Malkey
 Share

Recommended Posts

Just play around with it. Discover how different matrices change an image. Or not.

#include <GDIPlus.au3>
    #include <ScreenCapture.au3>
    #include <GUIConstantsEx.au3>
    #include <WindowsConstants.au3>
    #include <INet.au3>
    #include <EditConstants.au3>
    #include <ButtonConstants.au3>
    #include <Constants.au3>
    #include <misc.au3>
    
    ; Colour Matrix Reference Sites (some)
    ; http://www.codeproject.com/KB/GDI-plus/colormatrix.aspx
    ; http://boredzo.org/blog/archives/category/...hics/core-image
    ; http://www.reflectiveimages.com/digitalvelvia.htm
    ; http://www.imagemagick.org/Usage/color/
    ; http://blog.zacharcher.com/2007/10/
    ; Big Thanks Siao
    ;http://www.autoitscript.com/forum/index.php?s=&showtopic=69930&view=findpost&p=513472
    Opt("GUIOnEventMode", 0)
    Opt('MustDeclareVars', 1)
    HotKeySet("{ESC}", "Quit")
    Opt("GUICloseOnESC", 0)
    
    Global $dll = DllOpen("user32.dll")
    Global $iOpacity = 255
    Global Const $STM_SETIMAGE = 0x0172
    ;Local Const $ULW_ALPHA = 2
    ;Global Const $IMAGE_BITMAP = 0
    ;Global Const $WM_LBUTTONDOWN = 0x0201  ; Drag Window 1 of 3 addin
    Global $hBitmap, $hImage, $hImage1, $hGui1, $MatMenuChk = 0, $OrigImageChk = 0
    Global $hGui, $hBitmapG, $hGraphic, $hGraphicG, $num, $hWnd, $hDC
    Global $hBitmapG1, $hGraphic1, $hWnd1, $hDC1, $hGraphicG1, $tColorMatrix
    Global $pSize, $tSize, $pSource, $tSource, $pBlend, $tBlend, $m, $n
    Global $ColMatBut1, $ColMatBut2, $ColMatBut3, $ColMatBut4, $Combo1
    Global $ColMatForm, $comboData, $iX, $iY, $Add2MatrixStr
    Global $ContextMenu, $viewOrigImg, $viewMatrix, $Demos, $PreSetMat, $ChangeColMat, $MenuItemExit
    Global $separator1, $Cell[26], $init = 1
    Global $LastMatrix, $num
    Global $a,$b,$c  ; To use inside matrix in display. e.g. Assign( "a",0.9) = 1;  $a = 0.9
    Global $CA[17][26] = [[ "Identity Martix", 1, 0, 0, 0, 0, _
            0, 1, 0, 0, 0, _
            0, 0, 1, 0, 0, _
            0, 0, 0, 1, 0, _
            0, 0, 0, 0, 1], _
            [ "Gray Shade", 0.3, 0.3, 0.3, 0, 0, 0.59, 0.59, 0.59, 0, 0, _
            0.11, 0.11, 0.11, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1], _
            [ "Pink Shade", 0.4, 0.3, 0.3, 0, 0, 0.7, 0.59, 0.59, 0, 0, _
            0.2, 0.11, 0.11, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1], _
            [ "Yellow Shade", 0.4, 0.4, 0.3, 0, 0, 0.7, 0.7, 0.59, 0, 0, _
            0.2, 0.2, 0.11, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1], _
            [ "Green Shade", 0.3, 0.4, 0.3, 0, 0, 0.59, 0.7, 0.59, 0, 0, _
            0.11, 0.2, 0.11, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1], _
            [ "Blue Shade", 0.3, 0.3, 0.4, 0, 0, 0.59, 0.59, 0.7, 0, 0, _
            0.11, 0.11, 0.2, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1], _
            [ "Gray Shade Alternate", 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, _
            0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0.2, 0.2, 0.2, 0, 1], _
            [ "Brighter", 1.5, 0, 0, 0, 0, 0, 1.5, 0, 0, 0, _
            0, 0, 1.5, 0, 0, 0, 0, 0, 1, 0, 0.05, 0.05, 0.05, 0, 1], _
            [ "Colour Boost", 1.4, 0, 0, 0, 0, 0, 1.4, 0, 0, 0, _
            0, 0, 1.4, 0, 0, 0, 0, 0, 1, 0, -0.2, -0.2, -0.2, 0, 1], _
            [ "Swap Color RGB2GBR", 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, _
            1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1], _
            ["Swap Color RGB2BRG", 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, _
            0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1], _
            ["Swap Color RGB2RBG", 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, _
            0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1], _
            [ "Swap Colors Transparent", 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, _
            1, 0, 0, 0, 0, 0, 0, 0, 0.5, 0, 0, 0, 0, 0, 1], _
            ["Trial insert in array", 0.7, 0, 0, 0, 0, 0, -0.9, 0, 0, 0, _
    0, 0, -0.9, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1], _
            [ "All White", 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, _
            1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 01, 1], _
            [ "All Black", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _
            0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1], _
            [ "Test Martix", 0xff / 0xff + 0.11,                0,   0,  0, 0, _
                                              0, Assign( "a",0.9),   0,  0, 0, _
                                              0,             0, -$a/3,   0, 0, _
                                              0,                0,   0,  1, 0, _
                                              0,                0,   0,  0, 1 ]]
    
    ; In the ReadMat() Function, Execute(GUICtrlRead($Cell[$P])) is used to read Matrix data displayed.
    ; Test Matrix above works
    
    Local $iI, $iSize, $tBits, $tBMI, $hDC
    $LastMatrix = UBound($CA, 1) - 1
    $num = $LastMatrix
    
    ColorMatForm()
    
    _GDIPlus_Startup ()
    Local $ImageSrce, $Button1, $Button2, $Button3, $Button4, $Label1
    $ImageSrce = GUICreate("Source of Image", 204, 221, 351, 250, BitOR($WS_DLGFRAME, $WS_CLIPSIBLINGS))
    GUISetIcon("D:\002.ico")
    GUISetBkColor(0xFFFFE1)
    $Button1 = GUICtrlCreateButton("Screen Capture", 33, 37, 127, 25, 0)
    GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
    GUICtrlSetTip($Button1, "Capture Top Left quarter of desktop")
    $Button2 = GUICtrlCreateButton("Download Image", 34, 77, 127, 25, 0)
    GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
    GUICtrlSetTip($Button2, "If the 'autoit_builder_wall_800x600.jpg' file (122KB) is not in this script directory" & @CRLF & _
            "The file will auto-download from www.autoitscript.com, and be saved to this script directory. ")
    $Button3 = GUICtrlCreateButton("Open File ", 34, 117, 127, 25, 0)
    GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
    GUICtrlSetTip($Button3, "Opens Dialog for selection of an image.")
    $Button4 = GUICtrlCreateButton("Exit", 61, 157, 75, 25, 0)
    GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
    GUICtrlSetBkColor(-1, 0xFF0000)
    $Label1 = GUICtrlCreateLabel("Choose  Image  Source", 7, 4, 192, 24)
    GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
    GUICtrlSetColor(-1, 0x0000FF)
    GUISetState(@SW_SHOW)
    
    While 1
        Local $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $Button4
                Exit
            Case $Button1
                $hBitmap = _ScreenCapture_Capture ("", 0, 0, @DesktopWidth / 2.1, @DesktopHeight / 2.1, False)
                If Not @error Then ExitLoop
            Case $Button2
                If Not FileExists("autoit_builder_wall_800x600.jpg") Then
                    InetGet("http://www.autoitscript.com/autoit3/files/graphics/autoit_builder_wall_800x600.jpg",  _
                    @ScriptDir & "\autoit_builder_wall_800x600.jpg", 1, 0)
                    ;While @InetGetActive
                    ;  TrayTip("Downloading", "Bytes = " & @InetGetBytesRead, 10, 16)
                    ;  Sleep(250)
                    ;Wend
                EndIf
                Local $hImg = _GDIPlus_ImageLoadFromFile (@ScriptDir & "\autoit_builder_wall_800x600.jpg")
                If $hImg = 0 Then SetError(1, 0, 0)
                $hBitmap = _GDIPlus_ImageCreateGDICompatibleHBITMAP($hImg)
                _GDIPlus_ImageDispose ($hImg)
                If Not @error Then ExitLoop
              
            Case $Button3
                Local $Path = FileOpenDialog("Choose Image File", @ScriptDir & "", _
                                    "Images (*.gif;*.png;*.jpg;*.bmp)| All (*.*)")
                If $Path <> "" Then
                    Local $hImg = _GDIPlus_ImageLoadFromFile ($Path)
                    If $hImg = 0 Then SetError(1, 0, 0)
                    Local $hBitmap = _GDIPlus_ImageCreateGDICompatibleHBITMAP($hImg)
                    _GDIPlus_ImageDispose ($hImg)
                    If Not @error Then ExitLoop
                EndIf
        EndSwitch
    WEnd
    GUIDelete($ImageSrce)
    
    Opt("GUIOnEventMode", 1)
    
    ShowaColMatFrm()
    $hImage = _GDIPlus_BitmapCreateFromHBITMAP ($hBitmap)
    $iX = _GDIPlus_ImageGetWidth ($hImage)
    $iY = _GDIPlus_ImageGetHeight ($hImage)
    
    _WinAPI_DeleteObject ($hBitmap)
    $hGui = GUICreate("GDI+", $iX, $iY, 10, 10, $WS_POPUP, $WS_EX_LAYERED) ;BitOR($WS_EX_LAYERED, $WS_EX_TOPMOST))
    GUIRegisterMsg($WM_LBUTTONDOWN, "_WinMove") ; Drag Window 2 of 3 addin
    GUISetState(@SW_HIDE, $hGui) ;@SW_SHOW
    
    $hWnd = _WinAPI_GetDC ($hGui)
    $hDC = _WinAPI_CreateCompatibleDC ($hWnd)
    $hBitmapG = _WinAPI_CreateCompatibleBitmap ($hWnd, $iX, $iY) ; $iWidth, $iHeight)
    _WinAPI_SelectObject ($hDC, $hBitmapG)
    $hGraphicG = _GDIPlus_GraphicsCreateFromHDC ($hDC)
    _GDIPlus_GraphicsDrawImage ($hGraphicG, $hImage, 0, 0)
    $tSize = DllStructCreate($tagSIZE)
    $pSize = DllStructGetPtr($tSize)
    DllStructSetData($tSize, "X", $iX)  ;$iWidth )
    DllStructSetData($tSize, "Y", $iY)  ;$iHeight)
    $tSource = DllStructCreate($tagPOINT)
    $pSource = DllStructGetPtr($tSource)
    $tBlend = DllStructCreate($tagBLENDFUNCTION)
    $pBlend = DllStructGetPtr($tBlend)
    DllStructSetData($tBlend, "Alpha", $iOpacity)
    DllStructSetData($tBlend, "Format", 1)
    _WinAPI_UpdateLayeredWindow ($hGui, $hWnd, 0, $pSize, $hDC, $pSource, 0, $pBlend, $ULW_ALPHA)
    
    $hGui1 = GUICreate("GDI1", $iX, $iY, $iX, 20, $WS_POPUP, $WS_EX_LAYERED) ; BitOR($WS_EX_LAYERED, $WS_EX_TOPMOST))
    $ContextMenu = GUICtrlCreateContextMenu()
    $viewOrigImg = GUICtrlCreateMenuItem("Show Original Image", $ContextMenu)
    GUICtrlSetOnEvent($viewOrigImg, "ShowaOrig")
    GUICtrlSetState($viewOrigImg, $GUI_UNCHECKED)
    $viewMatrix = GUICtrlCreateMenuItem("Show Matrix", $ContextMenu)
    GUICtrlSetOnEvent($viewMatrix, "ShowaColMatFrm")
    GUICtrlSetState($viewMatrix, $GUI_CHECKED)
    $Demos = GUICtrlCreateMenu("Demos", $ContextMenu)
    $PreSetMat = GUICtrlCreateMenuItem("Preset Matrices - Press Crtl key to scroll thru array of matrices.", $Demos)
    GUICtrlSetOnEvent($PreSetMat, "ScrollMatArray")
    $ChangeColMat = GUICtrlCreateMenuItem("Auto Change Colors (Press Shift once) @ Esc once to stop", $Demos)
    GUICtrlSetOnEvent($ChangeColMat, "AutoColChange")
    $separator1 = GUICtrlCreateMenuItem("", $ContextMenu) ; create a separator line
    $MenuItemExit = GUICtrlCreateMenuItem("Exit  (Esc)", $ContextMenu)
    GUICtrlSetOnEvent($MenuItemExit, "Quit")
    
    GUISetState()
    
    $hWnd1 = _WinAPI_GetDC ($hGui1)
    $hDC1 = _WinAPI_CreateCompatibleDC ($hWnd1)
    
    $hImage1 = $hImage
    While 1
        If _IsPressed("11") Or $init = 1 Then   ;Ctrl key and initial run
            ScrollMatArray()
    
        EndIf
        If _IsPressed("A1") Or _IsPressed("A0") Then ; Right Shift and Left Shift keys
            AutoColChange()
    
        EndIf
    
        $init = 0
        Sleep(10)
    WEnd
    DllClose($dll)
    
    _GDIPlus_GraphicsDispose ($hGraphicG)
    _WinAPI_ReleaseDC ($hGui, $hWnd)
    _WinAPI_DeleteObject ($hBitmap)
    _WinAPI_DeleteDC ($hDC)
    _GDIPlus_ImageDispose ($hImage)
    _WinAPI_ReleaseDC ($hGui1, $hWnd1)
    _WinAPI_DeleteDC ($hDC1)
    _GDIPlus_ImageDispose ($hImage1)
    _GDIPlus_Shutdown ()
    
    
    ;  Displays image with the applied matrix
    Func ImageMatrix()
        Local $pSize1, $tSize1, $pSource1, $tSource1, $pBlend1, $tBlend1, $tColorMatrix
        Local $x, $hImgAttrib, $iW = _GDIPlus_ImageGetWidth ($hImage1), _
                $iH = _GDIPlus_ImageGetHeight ($hImage1), $hGraphics, $hBitmap
      
        $hBitmapG1 = _WinAPI_CreateCompatibleBitmap ($hWnd1, $iX, $iY) ; $iWidth, $iHeight)
        _WinAPI_SelectObject ($hDC1, $hBitmapG1)
        $hGraphicG1 = _GDIPlus_GraphicsCreateFromHDC ($hDC1)
        $tColorMatrix = DllStructCreate("float[5];float[5];float[5];float[5];float[5]")
        ;; Loads current Matrix int matrix display Gui
        LoadMat2Display($num)
        ;;Color adjustment  values:
        $x = DllStructSetData($tColorMatrix, 1, $CA[$num][1], 1) * DllStructSetData($tColorMatrix, 1, $CA[$num][2], 2) * _
        DllStructSetData($tColorMatrix, 1, $CA[$num][3], 3) * DllStructSetData($tColorMatrix, 2, $CA[$num][6], 1) * _
        DllStructSetData($tColorMatrix, 2, $CA[$num][7], 2) * DllStructSetData($tColorMatrix, 2, $CA[$num][8], 3) * _
        DllStructSetData($tColorMatrix, 3, $CA[$num][11], 1) * DllStructSetData($tColorMatrix, 3, $CA[$num][12], 2) * _
        DllStructSetData($tColorMatrix, 3, $CA[$num][13], 3) * DllStructSetData($tColorMatrix, 4, $CA[$num][19], 4) * _
        DllStructSetData($tColorMatrix, 5, $CA[$num][21], 1) * DllStructSetData($tColorMatrix, 5, $CA[$num][22], 2) * _
        DllStructSetData($tColorMatrix, 5, $CA[$num][23], 3) * _
        DllStructSetData($tColorMatrix, 5, $CA[$num][24], 4) * DllStructSetData($tColorMatrix, 5, $CA[$num][25], 5)
        ;;create an image attributes object and update its color matrix
        $hImgAttrib = _GDIPlus_ImageAttributesCreate()
        _GDIPlus_ImageAttributesSetColorMatrix($hImgAttrib, 1, DllStructGetPtr($tColorMatrix))
      
        ;;draw original into copy with attributes
        _GDIPlus_GraphicsDrawImageRectRectEx($hGraphicG1, $hImage1, 0, 0, $iW, $iH, 0, 0, $iW, $iH, 2, $hImgAttrib)
      
        $tSize1 = DllStructCreate($tagSIZE)
        $pSize1 = DllStructGetPtr($tSize1)
        DllStructSetData($tSize1, "X", $iX)  ;$iWidth )
        DllStructSetData($tSize1, "Y", $iY)  ;$iHeight)
        $tSource1 = DllStructCreate($tagPOINT)
        $pSource1 = DllStructGetPtr($tSource1)
        $tBlend1 = DllStructCreate($tagBLENDFUNCTION)
        $pBlend1 = DllStructGetPtr($tBlend1)
        DllStructSetData($tBlend1, "Alpha", $iOpacity)
        DllStructSetData($tBlend1, "Format", 1)
        _WinAPI_UpdateLayeredWindow ($hGui1, $hWnd1, 0, $pSize1, $hDC1, $pSource1, 0, $pBlend1, $ULW_ALPHA)
      
        ;;clean up
        _GDIPlus_GraphicsDispose ($hGraphics)
        _GDIPlus_GraphicsDispose ($hGraphicG1)
        _GDIPlus_ImageAttributesDispose($hImgAttrib)
        _WinAPI_DeleteObject ($hBitmap)
        _WinAPI_DeleteObject ($hBitmapG1)
        Return 1
    EndFunc   ;==>ImageMatrix
    
    Func _GDIPlus_ImageAttributesSetColorMatrix($hImgAttrib, $iColorAdjustType, _
            $pColorMatrix = 0, $pGreyMatrix = 0, $iColorMatrixFlags = 0)
        Local $fEnable = 1, $aResult = DllCall($ghGDIPDll, "int", _
                "GdipSetImageAttributesColorMatrix", "ptr", $hImgAttrib, "int", $iColorAdjustType, _
                "int", $fEnable, "ptr", $pColorMatrix, "ptr", $pGreyMatrix, "int", $iColorMatrixFlags)
        Return SetError($aResult[0], 0, $aResult[0] = 0)
    EndFunc   ;==>_GDIPlus_ImageAttributesSetColorMatrix
    
    ;;Creates ImageAttributes object
    Func _GDIPlus_ImageAttributesCreate()
        Local $aResult = DllCall($ghGDIPDll, "int", _
                "GdipCreateImageAttributes", "ptr*", 0)
        Return SetError($aResult[0], 0, $aResult[1])
    EndFunc   ;==>_GDIPlus_ImageAttributesCreate
    
    ;;Deletes ImageAttributes object
    Func _GDIPlus_ImageAttributesDispose($hImgAttrib)
        Local $aResult = DllCall($ghGDIPDll, "int", _
                "GdipDisposeImageAttributes", "ptr", $hImgAttrib)
        Return SetError($aResult[0], 0, $aResult[0] = 0)
    EndFunc   ;==>_GDIPlus_ImageAttributesDispose
    
    ; _GDIPlus_GraphicsDrawImageRectRectEx()
    ; Same as _GDIPlus_GraphicsDrawImageRectRect(),
    ; but adds 1 optional parameter - $hImgAttrib (handle to ImageAttributes object)
    Func _GDIPlus_GraphicsDrawImageRectRectEx($hGraphics, $hImage, $iSrcX, $iSrcY, $iSrcWidth, _
            $iSrcHeight, $iDstX, $iDstY, $iDstWidth, $iDstHeight, $iUnit = 2, $hImgAttrib = 0)
        Local $aResult = DllCall($ghGDIPDll, "int", "GdipDrawImageRectRectI", "hwnd", _
                $hGraphics, "hwnd", $hImage, "int", $iDstX, "int", _
                $iDstY, "int", $iDstWidth, "int", $iDstHeight, "int", $iSrcX, "int", $iSrcY, "int", _
                $iSrcWidth, "int", $iSrcHeight, "int", $iUnit, "ptr", $hImgAttrib, "int", 0, "int", 0)
        Return SetError($aResult[0], 0, $aResult[0] = 0)
    EndFunc   ;==>_GDIPlus_GraphicsDrawImageRectRectEx
    
    ; Demo - Scroll thru Matrix Array
    Func ScrollMatArray()
        $init = 0
        $num = Mod($num + 1, $LastMatrix + 1)
        ;$num = 13
        GUICtrlSetData($Combo1, $CA[$num][0])
        ImageMatrix()
        Return 1
    EndFunc   ;==>ScrollMatArray
    
    ; Demo - Auto Colour Change
    Func AutoColChange()
        If $init = 2 Then
            $init = 0
        Else
            $init = 2
            $num = 0
            LoadMat2Display($num)
            $num = $LastMatrix  
            GUICtrlSetData($Combo1, $CA[$num][0])   
        EndIf
        While $init = 2
            If _IsPressed("11") Or _IsPressed("1B") Then ExitLoop ;CTRL key and Escape key
            $m = Mod($m + 1, 20)
            $n = Mod($n + 1, 24)
            $CA[$LastMatrix][1] = -($m - 8) / 10
            $CA[$LastMatrix][7] = ($m - 10) / 10
            $CA[$LastMatrix][13] = -($n - 12) / 10
            ImageMatrix()
            Sleep(1000)
        WEnd
        $init = 0
        Sleep(800)
        Return 1
    EndFunc   ;==>AutoColChange
    
    ; Toggles show/hide original image
    Func ShowaOrig()
        If $OrigImageChk = 1 Then   
            GUICtrlSetState($viewOrigImg, $GUI_UNCHECKED)
            GUISetState(@SW_HIDE, $hGui)
            $OrigImageChk = 0
        Else
            GUICtrlSetState($viewOrigImg, $GUI_CHECKED)
            GUISetState(@SW_SHOW, $hGui)
            $OrigImageChk = 1
        EndIf
      
        Return 1
    EndFunc   ;==>ShowaOrig
    
    ; Toggles show/hide matrix Gui
    Func ShowaColMatFrm()
        If $MatMenuChk = 1 Then
            GUICtrlSetState($viewMatrix, $GUI_UNCHECKED)
            GUISetState(@SW_HIDE, $ColMatForm)
            $MatMenuChk = 0
        Else
            GUICtrlSetState($viewMatrix, $GUI_CHECKED)
            GUISetState(@SW_SHOW, $ColMatForm)
            $MatMenuChk = 1
        EndIf
        ;
        ;MsgBox(0,"",GUICtrlRead($viewMatrix))
        Return 1
    EndFunc   ;==>ShowaColMatFrm
    
    ;When selection in combo, load corresponding matrix
    Func ComboLoadMatDat()
        Local $dat, $f
        $dat = GUICtrlRead($Combo1)
        For $f = 0 To $LastMatrix
            If $CA[$f][0] = $dat Then $num = $f
        Next
        ;ReadMat($num)
        ;GUICtrlSetData($Combo1, $CA[$num][0])
        ImageMatrix()
        Return 1
    EndFunc   ;==>ComboLoadMatDat
    
    ; Loads current Matrix int matrix display Gui
    Func LoadMat2Display($number = $LastMatrix)
        If $MatMenuChk = 1 Then
            For $P = 1 To 25
                GUICtrlSetData($Cell[$P], $CA[$number][$P])
            Next
        EndIf
        Return 1
    EndFunc   ;==>LoadMat2Display
    
    ;When Apply Matrix is pressed, Stores Matrix's element values to last matrix in array
    Func ApplyColMat()
        $num = $LastMatrix
        ReadMat($num)
        GUICtrlSetData($Combo1, $CA[$num][0])
        ImageMatrix()
        Return 1
    EndFunc   ;==>ApplyColMat
    
    ; Reads Matrix in Gui and enters into last matrix in array of matrices
    Func ReadMat($number = $LastMatrix)
        For $P = 1 To 25
            $CA[$number][$P] = Execute(GUICtrlRead($Cell[$P]))
        Next
        Return 1
    EndFunc   ;==>ReadMat
    
    ; To save matrix data - writes to console. Formatted to copy into $CA matrix array at top of script.
    Func WriteConsole()
        $Add2MatrixStr &= "[" & '"' & "Change to your label" & '"' & ", "
        For $x = 1 To 10
            $Add2MatrixStr &= $CA[$LastMatrix][$x] & ", "
        Next
        $Add2MatrixStr &= "_" & @CRLF
        For $x = 11 To 25
            $Add2MatrixStr &= $CA[$LastMatrix][$x] & ", "
        Next
        $Add2MatrixStr = StringTrimRight($Add2MatrixStr, 2)
        $Add2MatrixStr &= "], _" & @CRLF & @CRLF
        ConsoleWrite($Add2MatrixStr)
        ConsoleWrite("Copy above matrix. Insert between existing matrices in declaration of Global $CA[14][26]" & @CRLF & _
                " in script. Also, increase number of matrices in array. " & @CRLF & _
                "eg. if $CA[14][26] increase to $CA[15][26] Then re-run script. " & @CRLF)
    EndFunc   ;==>WriteConsole
          
    ;Saves generated image to disk
    Func SaveImage()
        Local $ext, $sCLSID
        Local $Path = FileSaveDialog("Save Current Image", @ScriptDir, "Images (*.gif;*.png;*.jpg;*.bmp)| All (*.*)", 16)
        If $Path <> "" Then
            $ext = StringUpper(StringRight($Path, 3))
            If $ext = "GIF"  Or $ext = "PNG"  Or $ext = "JPG"  Or $ext = "BMP"  Then
                $sCLSID = _GDIPlus_EncodersGetCLSID ($ext)
                ;$SaveFlag = 1
                ApplyColMat()
                Local $tColorMatrix, $x, $hImgAttrib, $iW = _GDIPlus_ImageGetWidth ($hImage1)
                Local $iH = _GDIPlus_ImageGetHeight ($hImage1), $hGraphics
                Local $hGraphics2, $hBitmap
                ;;create color matrix data
                $tColorMatrix = DllStructCreate("float[5];float[5];float[5];float[5];float[5]")
                LoadMat2Display($num)
                $x = DllStructSetData($tColorMatrix, 1, $CA[$num][1], 1) * DllStructSetData($tColorMatrix, 1, $CA[$num][2], 2) * _
                DllStructSetData($tColorMatrix, 1, $CA[$num][3], 3) * DllStructSetData($tColorMatrix, 2, $CA[$num][6], 1) * _
                DllStructSetData($tColorMatrix, 2, $CA[$num][7], 2) * DllStructSetData($tColorMatrix, 2, $CA[$num][8], 3) * _
                DllStructSetData($tColorMatrix, 3, $CA[$num][11], 1) * DllStructSetData($tColorMatrix, 3, $CA[$num][12], 2) * _
                DllStructSetData($tColorMatrix, 3, $CA[$num][13], 3) * DllStructSetData($tColorMatrix, 4, $CA[$num][19], 4) * _
                DllStructSetData($tColorMatrix, 5, $CA[$num][21], 1) * DllStructSetData($tColorMatrix, 5, $CA[$num][22], 2) * _
                DllStructSetData($tColorMatrix, 5, $CA[$num][23], 3) * _
                DllStructSetData($tColorMatrix, 5, $CA[$num][24], 4) * DllStructSetData($tColorMatrix, 5, $CA[$num][25], 5)
                ;;create an image attributes object and update its color matrix
                $hImgAttrib = _GDIPlus_ImageAttributesCreate()
                _GDIPlus_ImageAttributesSetColorMatrix($hImgAttrib, 1, DllStructGetPtr($tColorMatrix))
                ;;copy image
                $hGraphics = _GDIPlus_ImageGetGraphicsContext ($hImage1)
                $hBitmap = _GDIPlus_BitmapCreateFromGraphics ($iW, $iH, $hGraphics)
                $hGraphics2 = _GDIPlus_ImageGetGraphicsContext ($hBitmap)
                ;;draw original into copy with attributes
                _GDIPlus_GraphicsDrawImageRectRectEx($hGraphics2, $hImage1, 0, 0, $iW, $iH, 0, 0, $iW, $iH, 2, $hImgAttrib)
                ;;clean up
                _GDIPlus_GraphicsDispose ($hGraphics)
                _GDIPlus_GraphicsDispose ($hGraphics2)
                _GDIPlus_ImageAttributesDispose($hImgAttrib)
                ; Save image
                _GDIPlus_ImageSaveToFileEx ($hBitmap, $Path, $sCLSID)
              
                _GDIPlus_GraphicsDispose ($hGraphics)
                _GDIPlus_GraphicsDispose ($hGraphics2)
                _GDIPlus_ImageAttributesDispose($hImgAttrib)
                _GDIPlus_ImageDispose ($hBitmap)
            EndIf
        EndIf
        Return 1
    EndFunc   ;==>SaveImage
    
    ; If in Auto-generate demo Esc exits that loop. Otherwise, exits script.
    Func Quit()
        Local $e
        If $init = 2 Then  ; For AutoColChange() while loop
            $init = 0
        Else
            $e = MsgBox(1, "Exit", "Press OK to Exit")
            If $e = 1 Then Exit
        EndIf
        Return 1
    EndFunc   ;==>Quit
    
    ; The Gui contains matrix data display, buttons and combobox of pre-defined matrices from array.
    Func ColorMatForm()
        Local $Group1, $Group2, $Group3, $Group4, $Group5, $Group6, $Group7, $Label1, $Label2
        $ColMatForm = GUICreate("Form1", 775, 243, 290, @DesktopHeight * 0.7, BitOR($WS_POPUP, $WS_BORDER))
        $Group1 = GUICtrlCreateGroup("Color Matrix 5x5", 6, 4, 760, 197)
        GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
        $Group2 = GUICtrlCreateGroup(" Red ", 148, 19, 137, 101)
        GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
        GUICtrlSetColor(-1, 0xFF0000)
        $Cell[1] = GUICtrlCreateInput("0xff / 0xff + 0.11", 154, 36, 125, 24, BitOR($ES_RIGHT, $ES_AUTOHSCROLL))
        $Cell[2] = GUICtrlCreateInput("0", 293, 36, 125, 24, BitOR($ES_RIGHT, $ES_AUTOHSCROLL))
        $Cell[3] = GUICtrlCreateInput("0", 430, 36, 125, 24, BitOR($ES_RIGHT, $ES_AUTOHSCROLL))
        GUICtrlCreateGroup("", -99, -99, 1, 1)
        $Group3 = GUICtrlCreateGroup("Green", 288, 19, 137, 101)
        GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
        GUICtrlSetColor(-1, 0x00A000)
        $Cell[4] = GUICtrlCreateInput("0", 564, 36, 125, 24, BitOR($ES_RIGHT, $ES_AUTOHSCROLL))
        GUICtrlSetBkColor(-1, 0xD4D0C8)
        $Cell[5] = GUICtrlCreateInput("0", 692, 36, 49, 24, BitOR($ES_RIGHT, $ES_AUTOHSCROLL))
        GUICtrlSetBkColor(-1, 0xD4D0C8)
        $Cell[6] = GUICtrlCreateInput("0", 154, 64, 125, 24, BitOR($ES_RIGHT, $ES_AUTOHSCROLL))
        GUICtrlCreateGroup("", -99, -99, 1, 1)
        $Group4 = GUICtrlCreateGroup(" Blue ", 425, 19, 133, 101)
        GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
        GUICtrlSetColor(-1, 0x0000FF)
        $Cell[7] = GUICtrlCreateInput("1", 293, 64, 125, 24, BitOR($ES_RIGHT, $ES_AUTOHSCROLL))
        $Cell[8] = GUICtrlCreateInput("0", 430, 64, 125, 24, BitOR($ES_RIGHT, $ES_AUTOHSCROLL))
        $Cell[9] = GUICtrlCreateInput("0", 564, 64, 125, 24, BitOR($ES_RIGHT, $ES_AUTOHSCROLL))
        GUICtrlSetBkColor(-1, 0xD4D0C8)
        GUICtrlCreateGroup("", -99, -99, 1, 1)
        $Group5 = GUICtrlCreateGroup(" Unused ", 558, 19, 191, 101)
        $Cell[10] = GUICtrlCreateInput("0", 692, 64, 49, 24, BitOR($ES_RIGHT, $ES_AUTOHSCROLL))
        GUICtrlSetBkColor(-1, 0xD4D0C8)
        $Cell[11] = GUICtrlCreateInput("0", 154, 92, 125, 24, BitOR($ES_RIGHT, $ES_AUTOHSCROLL))
        $Cell[12] = GUICtrlCreateInput("0", 293, 92, 125, 24, BitOR($ES_RIGHT, $ES_AUTOHSCROLL))
        $Cell[13] = GUICtrlCreateInput("1", 430, 93, 125, 24, BitOR($ES_RIGHT, $ES_AUTOHSCROLL))
        $Cell[14] = GUICtrlCreateInput("0", 564, 93, 125, 24, BitOR($ES_RIGHT, $ES_AUTOHSCROLL))
        GUICtrlSetBkColor(-1, 0xD4D0C8)
        $Cell[15] = GUICtrlCreateInput("0", 693, 93, 49, 24, BitOR($ES_RIGHT, $ES_AUTOHSCROLL))
        GUICtrlSetBkColor(-1, 0xD4D0C8)
        GUICtrlCreateGroup("", -99, -99, 1, 1)
        $Group6 = GUICtrlCreateGroup("", 148, 114, 601, 39)
        $Cell[16] = GUICtrlCreateInput("0", 154, 124, 125, 24, BitOR($ES_RIGHT, $ES_AUTOHSCROLL))
        GUICtrlSetBkColor(-1, 0xD4D0C8)
        $Cell[17] = GUICtrlCreateInput("0", 293, 124, 125, 24, BitOR($ES_RIGHT, $ES_AUTOHSCROLL))
        GUICtrlSetBkColor(-1, 0xD4D0C8)
        $Cell[18] = GUICtrlCreateInput("0", 430, 124, 125, 24, BitOR($ES_RIGHT, $ES_AUTOHSCROLL))
        GUICtrlSetBkColor(-1, 0xD4D0C8)
        $Cell[19] = GUICtrlCreateInput("1", 564, 124, 125, 24, BitOR($ES_RIGHT, $ES_AUTOHSCROLL))
        $Cell[20] = GUICtrlCreateInput("0", 692, 124, 49, 24, BitOR($ES_RIGHT, $ES_AUTOHSCROLL))
        GUICtrlSetBkColor(-1, 0xD4D0C8)
        GUICtrlCreateGroup("", -99, -99, 1, 1)
        $Group7 = GUICtrlCreateGroup("", 148, 146, 601, 39)
        $Cell[21] = GUICtrlCreateInput("0", 154, 156, 125, 24, BitOR($ES_RIGHT, $ES_AUTOHSCROLL))
        $Cell[22] = GUICtrlCreateInput("0", 293, 156, 125, 24, BitOR($ES_RIGHT, $ES_AUTOHSCROLL))
        $Cell[23] = GUICtrlCreateInput("0", 430, 156, 125, 24, BitOR($ES_RIGHT, $ES_AUTOHSCROLL))
        $Cell[24] = GUICtrlCreateInput("0", 564, 156, 125, 24, BitOR($ES_RIGHT, $ES_AUTOHSCROLL))
        $Cell[25] = GUICtrlCreateInput("1", 692, 156, 49, 24, BitOR($ES_RIGHT, $ES_AUTOHSCROLL))
        GUICtrlSetBkColor(-1, 0xD4D0C8)
        GUICtrlCreateGroup("", -99, -99, 1, 1)
        $Label1 = GUICtrlCreateLabel("Alpha Channel", 44, 128, 106, 20)
        GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
        $Label2 = GUICtrlCreateLabel("RGBA Additive Row", 8, 158, 142, 21)
        GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
        For $f = 0 To $LastMatrix
            $comboData &= $CA[$f][0] & "|"
        Next
        $Combo1 = GUICtrlCreateCombo("", 10, 28, 133, 25)
        GUICtrlSetData($Combo1, StringTrimRight($comboData, 1))
        GUICtrlSetOnEvent($Combo1, "ComboLoadMatDat")
        GUICtrlCreateGroup("", -99, -99, 1, 1)
        $ColMatBut1 = GUICtrlCreateButton("Save Current Image", 4, 206, 120, 30, $BS_FLAT)
        GUICtrlSetBkColor(-1, 0x00FFFF)
        GUICtrlSetOnEvent($ColMatBut1, "SaveImage")
        $ColMatBut2 = GUICtrlCreateButton("Write Matrix to Console", 132, 207, 118, 30, $BS_FLAT)
        GUICtrlSetBkColor(-1, 0xFFFF00)
        GUICtrlSetOnEvent($ColMatBut2, "WriteConsole")
        $ColMatBut3 = GUICtrlCreateButton("Apply Matrix ", 260, 207, 125, 30, $BS_FLAT)
        GUICtrlSetBkColor(-1, 0x00FF00)
        GUICtrlSetOnEvent($ColMatBut3, "ApplyColMat")
        $ColMatBut4 = GUICtrlCreateButton("Hide Matrix Display", 393, 206, 100, 30, $BS_FLAT)
        GUICtrlSetBkColor(-1, 0xFF00FF)
        GUICtrlSetOnEvent($ColMatBut4, "ShowaColMatFrm")
        Return 1
    EndFunc   ;==>ColorMatForm
    
    ; Description:: Converts a GDIPlus-Image to GDI-combatible HBITMAP
    ; Parameter(s): $hImg -> GDIplus Image object
    ; Requirement(s):  GDIPlus.au3
    ; Return Value(s): HBITMAP, compatible with ClipBoard
    ; Author(s):       Prog@ndy
    Func _GDIPlus_ImageCreateGDICompatibleHBITMAP($hImg)
        Local $hBitmap2 = _GDIPlus_BitmapCreateHBITMAPFromBitmap ($hImg)
        Local $hBitmap = _WinAPI_CopyImage($hBitmap2, 0, 0, 0, $LR_COPYDELETEORG + $LR_COPYRETURNORG)
        _WinAPI_DeleteObject ($hBitmap2)
        Return $hBitmap
    EndFunc   ;==>_GDIPlus_ImageCreateGDICompatibleHBITMAP
    
    ; Description:: Copies an image, also makes GDIPlus-HBITMAP to GDI32-BITMAP
    ; Parameter(s): $hImg -> HBITMAP Object, GDI or GDIPlus
    ; Requirement(s):  WinAPI.au3
    ; Return Value(s): Succes: Handle to new Bitmap, Error: 0
    ; Author(s):       Prog@ndy
    Func _WinAPI_CopyImage($hImg, $uType = 0, $x = 0, $y = 0, $flags = 0)
        Local $aResult
        $aResult = DllCall("User32.dll", "hwnd", "CopyImage", "hwnd", $hImg, "UINT", $uType, "int", $x, "int", $y, "UINT", $flags)
        ;_WinAPI_Check ("_WinAPI_CopyImage", ($aResult[0] = 0), 0, True)
        Return $aResult[0]
    EndFunc   ;==>_WinAPI_CopyImage
    
    ; Drag Window 3 of 3 addin
    Func _WinMove($hWnd, $Command, $wParam, $lParam)
        If BitAND(WinGetState($hWnd), 32) Then Return $GUI_RUNDEFMSG
        ;DllCall("user32.dll", "long", "SendMessage", "hwnd", $HWnd, "int", $WM_SYSCOMMAND, "int", 0xF009, "int", 0)
        DllCall("user32.dll", "int", "SendMessage", "hWnd", $hWnd, "int", $WM_NCLBUTTONDOWN, "int", $HTCAPTION, "int", 0)
        Return 1
    EndFunc   ;==>_WinMove

To learn more about a Colour Matrix, here are some reference sites.

http://www.codeproject.com/KB/GDI-plus/colormatrix.aspx

http://boredzo.org/blog/archives/category/...hics/core-image

http://www.reflectiveimages.com/digitalvelvia.htm

http://www.imagemagick.org/Usage/color/

http://blog.zacharcher.com/2007/10/

Edit: Added Picture Effects to thread title.

Edited by Malkey
Link to comment
Share on other sites

  • 3 weeks later...

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...