Jump to content

Optimization help


Recommended Posts

How I can use that formula? Please explain, I need for some my own colors ;p

Since that post in May, 2008, I have continued to use the colour matrix when needed.

I when through my posts and found these examples which may give you insight into how the colour matrix works.

http://www.autoitscript.com/forum/index.ph...c=71513&hl=

This is the best one to play around with to see the different effects or colours that an image can be transformed into.

It has reference sites near the top of the script. The codeproject, I remember was informative.

http://www.autoitscript.com/forum/index.ph...c=81686&hl=

Here, the _GDIPlus_GraphicsDrawImageRectRectTrans() func made the use of a colour matrix easier to use. The example I have is poor to ordinary.

The function here uses only the transparent ability in cell 4,4 of the matrix.

http://www.autoitscript.com/forum/index.ph...c=82940&hl=

Using the _GDIPlus_GraphicsDrawImageRectRectTrans() func again, but a much better example. Still only using the transparency ability.

http://www.autoitscript.com/forum/index.ph...mp;#entry623855

This uses an expanded _GDIPlus_GraphicsDrawImageRectRectTrans() func . Incorporates the 5th row of the matrix cells 5, 1 and 5, 2 and 5, 3.

The fifth row adds or extracts red, green, blue colours from the image.

This is directed at n3nE request. Posted here to share with all.

To Spiff59

Once I realized it slowly transforms into the shaded colour, I think it is an impressive example. Nice effect..

Link to comment
Share on other sites

To Spiff59

Once I realized it slowly transforms into the shaded colour, I think it is an impressive example. Nice effect..

I did reduce the Step increment in the WinSettrans loop to make the "show" last longer.

I guess this would make a better demo script:

#include <GDIPlus.au3>
#include <ScreenCapture.au3>
#include <GUIConstantsEX.au3>
#include <WindowsConstants.au3>
Global Const $STM_SETIMAGE = 0x0172
Global Const $IMAGE_BITMAP = 0

HotKeySet("!{TAB}","dummy")
Global $hBitmap, $hImage1, $hImage2, $oldBitmap
Global $hGui, $hPic
; colormatrix values                      RED  GREEN  BLUE   R   G   B   R   G   B
Global $huetable[10][10] = [["Grey",      .299, .299, .299, .587, .587, .587, .114, .114, .114], _ 
                           ["Red" ,   .400, .299, .299, .700, .587, .587, .200, .114, .114], _ 
                           ["Purple",   .400, .299, .400, .700, .587, .700, .200, .114, .200], _ 
                           ["Blue",   .299, .299, .400, .587, .587, .700, .114, .114, .200], _ 
                           ["Yellow",   .400, .400, .299, .700, .700, .587, .200, .200, .114], _ 
                           ["Green",     .299, .400, .299, .587, .700, .587, .114, .200, .114], _ 
                           ["Aqua",   .299, .400, .400, .587, .700, .700, .114, .200, .200], _ 
                           ["MS Sepia",  .393, .349, .272, .769, .686, .534, .189, .168, .131], _ 
                           ["FadeToBlack", .000, .000, .000, .000, .000, .000, .000, .000, .000], _   
                           ["test" ,      .700, .299, .700, 1, .587, 1, .500, .114, .500]] 
Global $speedtable[9] = [.25, .5, .75, 1, 2, 3, 4, 5, 6] 
Global $hue = 0, $alpha = .8, $speed = 4
Global $test, $Control_GUI,$huedown, $hueup, $labelhue,$alphadown, $alphaup, $labelalpha, $speeddown, $speedup, $labelspeed


; MAIN -------------------------------------------------------------------------
_GDIPlus_Startup()
$hBitmap = _ScreenCapture_Capture("", 0, 0, -1, -1, False)
$hImage1 = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap)
_WinAPI_DeleteObject($hBitmap)

Fade()
Sleep(250)
ControlGUI()

While 1
    $msg = GUIGetMsg()
;   If Not WinActive("GDI+") Then WinActivate("GDI+")
    Switch $msg
        Case $huedown
            If $hue > 0 then $hue -= 1
            GUICtrlSetData($labelhue, $huetable[$hue][0])
        Case $hueup
            If $hue < 9 then $hue += 1      
            GUICtrlSetData($labelhue, $huetable[$hue][0])
        Case $alphadown
            If $alpha > 0 then $alpha -= .1
            GUICtrlSetData($labelalpha, $alpha)
        Case $alphaup
            If $alpha < 1 then $alpha += .1     
            GUICtrlSetData($labelalpha, $alpha)
        Case $speeddown
            If $speed > 0 then $speed -= 1
            GUICtrlSetData($labelspeed, $speedtable[$speed])
        Case $speedup
            If $speed < 8 then $speed += 1
            GUICtrlSetData($labelspeed, $speedtable[$speed])
        Case $test
            GUIDelete($hGui)
            GUIDelete($Control_GUI)
            Sleep(250)
            Fade()
            Sleep(250)
            ControlGUI()
        Case $GUI_EVENT_CLOSE
            _GDIPlus_ImageDispose($hImage1)
            _GDIPlus_Shutdown()
            Exit
    EndSwitch
WEnd
Exit

;--------------------------------------------------------------------------------------------
Func ControlGUI()
;   Local $huedown, $hueup, $labelhue
$Control_GUI = GUICreate("GDI+ 'Locked screen' Fade Demo", 300, 120, -1, -1, Default,$WS_EX_TOPMOST)
GUISetFont(8.5,600)
$huedown = GuiCtrlCreateButton("<", 10, 10, 22, 22)
$hueup = GuiCtrlCreateButton(">", 34, 10, 22, 22)
GUICtrlCreateLabel("Color:",70,15,60,20)
$labelhue = GUICtrlCreateLabel($huetable[$hue][0],135,15,80,20)
$alphadown = GuiCtrlCreateButton("<", 10, 35, 22, 22)
$alphaup = GuiCtrlCreateButton(">", 34, 35, 22, 22)
GUICtrlCreateLabel("Alpha:",70,40,60,20)
$labelalpha = GUICtrlCreateLabel($alpha,135,40,70,20)
$speeddown = GuiCtrlCreateButton("<", 10, 60, 22, 22)
$speedup = GuiCtrlCreateButton(">", 34, 60, 22, 22)
GUICtrlCreateLabel("Speed:",70,65,60,20)
$labelspeed = GUICtrlCreateLabel($speedtable[$speed],135,65,70,20)
$test = GuiCtrlCreateButton("TEST", 100, 90, 100, 22)
GUISetState()
EndFunc         

;--------------------------------------------------------------------------------------------
Func Fade()
    $hImage2 = _GDIPlus_ImageGreyscale($hImage1)
    $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage2, 0x00000000)
    $hGui = GUICreate("Fade", @DesktopWidth, @DesktopHeight, 0, 0, $WS_POPUP, $WS_EX_TOOLWINDOW)
;   $hGui = GUICreate("Fade", @DesktopWidth, @DesktopHeight, 0, 0, $WS_POPUP, BitOR($WS_EX_TOPMOST, $WS_EX_TOOLWINDOW))
    $hPic = GUICtrlCreatePic("", 0, 0, @DesktopWidth, @DesktopHeight)
    $hPic = GUICtrlGetHandle($hPic)

    If IsHWnd($hPic) Then
        $oldBitmap = DllCall("user32.dll", "hwnd", "SendMessage", "hwnd", _
           $hPic, "int", $STM_SETIMAGE, "int", $IMAGE_BITMAP, "int", $hBitmap)
;       If $oldBitmap[0] Then
;           _WinAPI_DeleteObject($oldBitmap[0]); not needed in this usage,
        ; but necessary to prevent resource leaks when SendMessage used to
        ; replace existing image.
;       EndIf
    EndIf
    _GDIPlus_ImageDispose($hImage2)
    WinSetTrans($hGui, "", 0)
    GUISetState()
    For $i = 1 To 255 Step $speedtable[$speed]
        WinSetTrans($hGui, "", $i)
    Next
Endfunc
    
Func Dummy()
EndFunc

Func Quit()
    _GDIPlus_ImageDispose($hImage1)
    _GDIPlus_Shutdown()
    Exit
EndFunc  ;==>Quit

;--------------------------------------------------------------------------------------------
;; _GDIPlus_ImageGreyscale()
;;  Creates a greyscale copy of Image object and returns its handle.
;;  To destroy it, use _GDIPlus_ImageDispose() or _GDIPlus_BitmapDispose()
Func _GDIPlus_ImageGreyscale(Const ByRef $hImage)
    Local $tColorMatrix, $y, $hImgAttrib, $iW = _GDIPlus_ImageGetWidth($hImage), _
            $iH = _GDIPlus_ImageGetHeight($hImage), $hGraphics, $hGraphics2, $hBitmap
 ;;create color matrix data
    $tColorMatrix = DllStructCreate("float[5];float[5];float[5];float[5];float[5]")
    Local $y = 0
    For $x = 1 to 3
        For $z = 1 to 3
            $y +=1
            DllStructSetData($tColorMatrix, $x, $huetable[$hue][$y], $z)
        Next
    Next
    DllStructSetData($tColorMatrix, 4, $alpha, 4); Alpha?
    DllStructSetData($tColorMatrix, 5, 1.0, 5); dummy for internal addition

 ;;create an image attributes object and update its color matrix
    $hImgAttrib = _GDIPlus_ImageAttributesCreate()
    _GDIPlus_ImageAttributesSetColorMatrix($hImgAttrib, 1, _
            DllStructGetPtr($tColorMatrix))
 ;;copy image
    $hGraphics = _GDIPlus_ImageGetGraphicsContext($hImage)
    $hBitmap = _GDIPlus_BitmapCreateFromGraphics($iW, $iH, $hGraphics)
    $hGraphics2 = _GDIPlus_ImageGetGraphicsContext($hBitmap)
 ;;draw original into copy with attributes
    _GDIPlus_GraphicsDrawImageRectRectEx($hGraphics2, $hImage, _
            0, 0, $iW, $iH, 0, 0, $iW, $iH, 2, $hImgAttrib)
 ;;clean up
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_GraphicsDispose($hGraphics2)
    _GDIPlus_ImageAttributesDispose($hImgAttrib)

    Return $hBitmap
EndFunc;==>_GDIPlus_ImageGreyscale

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

EDIT: Changed GUI title to reflect this is a 'locked screen' fade technique. Adjusted speed table (10 was ludicrous speed!)

Edited by Spiff59
Link to comment
Share on other sites

Most of the congrats are due to Siao, and Paulie, and Rover and Malkey.

I just created the colormatrix table, added some new values to it and then put together a front-end with the hue, alpha and speed adjustments. I am alreasy using this effect in one of my production scripts, just to add some eye-candy. It's a script where I don't want users doing anything until it's complete, and this effectively does that with a little more flare than my prior script version. It's just bells-and-whistles, but users seem to like that stuff.

Link to comment
Share on other sites

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