Jump to content

Simple Color Effects (like black&white or sephia) using GDI+?


tip
 Share

Recommended Posts

Hi all,

I was wondering if it is possible to create simple color effects with gdi+. In my case, I'm trying to make an image a little bit grayer to make it feel like it's been "disabled". I tinkered a bit and wrote something with pixelgetcolor and manually altering each pixels color but not suprisingly it works way too slow.

Any help is appreciated.

Thanks in advance.

Regards

Tip

[center]MsgBox_Tipped: Eye candy msgboxes/inputboxes/loginboxes. | CreateBlankBox: Semi-transparent layers with borders and rounded corners.[/center]

Link to comment
Share on other sites

thanks to siao:

;link to post: http://www.autoitscript.com/forum/topic/69930-optimization-help/page__view__findpost__p__513472
#include <GDIPlus.au3>

_GDIPlus_Startup()
$sLoadImage = @ScriptDir&"\test.jpg"
$sFinalImage = @ScriptDir&"\test_grey.png"
$hImage1 = _GDIPlus_ImageLoadFromFile($sLoadImage)
$hImage2 = _GDIPlus_ImageGreyscale($hImage1)
_GDIPlus_ImageSaveToFile($hImage2, $sFinalImage)

_GDIPlus_ImageDispose($hImage1)
_GDIPlus_ImageDispose($hImage2)
ShellExecute($sFinalImage)
_GDIPlus_Shutdown()
Exit

Func _GDIPlus_ImageGreyscale(Const ByRef $hImage)
Local $tColorMatrix, $x, $hImgAttrib
Local $iW = _GDIPlus_ImageGetWidth($hImage)
Local $iH = _GDIPlus_ImageGetHeight($hImage)
Local $hGraphics, $hGraphics2, $hBitmap
;;create color matrix data
$tColorMatrix = DllStructCreate("float[5];float[5];float[5];float[5];float[5]")
;greyscale values:
$x = DllStructSetData($tColorMatrix, 1, 0.3, 1) * DllStructSetData($tColorMatrix, 1, 0.3, 2) * DllStructSetData($tColorMatrix, 1, 0.3, 3) * _
DllStructSetData($tColorMatrix, 2, 0.59, 1) * DllStructSetData($tColorMatrix, 2, 0.59, 2) * DllStructSetData($tColorMatrix, 2, 0.59, 3) * _
DllStructSetData($tColorMatrix, 3, 0.11, 1) * DllStructSetData($tColorMatrix, 3, 0.11, 2) * DllStructSetData($tColorMatrix, 3, 0.11, 3) * _
DllStructSetData($tColorMatrix, 4, 1.0, 4) * _
DllStructSetData($tColorMatrix, 5, 1.0, 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($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, $pGrayMatrix = 0, $iColorMatrixFlags = 0)
Local $fEnable = 1, $aResult = DllCall($ghGDIPDll, "int", "GdipSetImageAttributesColorMatrix", "ptr", $hImgAttrib, "int", $iColorAdjustType, _
"int", $fEnable, "ptr", $pColorMatrix, "ptr", $pGrayMatrix, "int", $iColorMatrixFlags)
Return SetError($aResult[0], 0, $aResult[0] = 0)
EndFunc ;==>_GDIPlus_ImageAttributesSetColorMatrix

Func _GDIPlus_ImageAttributesCreate()
Local $aResult = DllCall($ghGDIPDll, "int", "GdipCreateImageAttributes", "ptr*", 0)
Return SetError($aResult[0], 0, $aResult[1])
EndFunc ;==>_GDIPlus_ImageAttributesCreate

Func _GDIPlus_ImageAttributesDispose($hImgAttrib)
Local $aResult = DllCall($ghGDIPDll, "int", "GdipDisposeImageAttributes", "ptr", $hImgAttrib)
Return SetError($aResult[0], 0, $aResult[0] = 0)
EndFunc ;==>_GDIPlus_ImageAttributesDispose

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

Hi,

That's a nice Grey Scale example taietel.

Another way if your not after grey scale and you still want some color but a grey or any other color overlay.

#include <GDIPlus.au3>
 
Global $sLoadImage, $sFinalImage, $hImage, $hGraphics, $hBrush
 
_GDIPlus_Startup()
$sLoadImage = @ScriptDir&"\test.jpg"
$sFinalImage = @ScriptDir&"\test_grey.jpg"
$hImage = _GDIPlus_ImageLoadFromFile($sLoadImage)
$hGraphics = _GDIPlus_ImageGetGraphicsContext($hImage)
$hBrush = _GDIPlus_BrushCreateSolid(0xC0C0C0C0) ; Grey
_GDIPlus_GraphicsFillRect($hGraphics, 0, 0, _GDIPlus_ImageGetWidth($hImage), _GDIPlus_ImageGetHeight($hImage), $hBrush)
_GDIPlus_BrushDispose($hBrush)
_GDIPlus_GraphicsDispose($hGraphics)
_GDIPlus_ImageSaveToFile($hImage, $sFinalImage)
_GDIPlus_ImageDispose($hImage)
_GDIPlus_Shutdown()
 
ShellExecute($sFinalImage)

Cheers

Edited by smashly
Link to comment
Share on other sites

Ok, here is what I've came up with:

#FUNCTION# ======================================================================================================================================================
; Name...........: _GDIPlus_ImageColorOverlay
; Description ...: Creates overlay effect using GDI+ library.
; Syntax.........:  _GDIPlus_ImageColorOverlay($i_tip_GDIBmp,$i_tip_Color,$i_tip_Alpha, $i_WinAPIBMP = False)
; Parameters ....: $i_tip_GDIBmp - Bitmap Handle.
;                 $i_tip_Color - Color of the effect.
;                 $i_tip_Alpha - Desired alpha value. This can be also considered as the "opacity" value of the effect.
;                 $i_WinAPIBMP - [Optinal] If false, function returns bitmap handle. (Ready to be used in GDI+ functions)
;                 | If true, function returns hbitmap handle.(Ready to be used in WinAPI functions)
; Return values .: Success - Returns (h)bitmap handle.
; Author ........: Tip
;
; Remarks .......: Thanks to smashly for sharing the _GDIPlus_BrushCreateSolid/_GDIPlus_GraphicsFillRect routine.
;==================================================================================================================================================================
Func _GDIPlus_ImageColorOverlay($i_tip_GDIBmp,$i_tip_Color,$i_tip_Alpha, $i_WinAPIBMP = False)
    Local $i_tip_ARBG,$hGraphics,$hBrush
    If $i_tip_Alpha < 0 Then $i_tip_Alpha = 0
    If $i_tip_Alpha > 255 Then $i_tip_Alpha = 255
 
    $hGraphics = _GDIPlus_ImageGetGraphicsContext($i_tip_GDIBmp)
    If @error Then Return SetError(1,0,-1)
 
    $i_tip_ARBG = "0x" & Hex($i_tip_Alpha,2) & Hex($i_tip_Color,6)
 
    $hBrush = _GDIPlus_BrushCreateSolid($i_tip_ARBG)
    _GDIPlus_GraphicsFillRect($hGraphics, 0, 0, _GDIPlus_ImageGetWidth($i_tip_GDIBmp), _GDIPlus_ImageGetHeight($i_tip_GDIBmp), $hBrush)
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_GraphicsDispose($hGraphics)
 
    If $i_WinAPIBMP = True Then
        Local $hHBITMAP = _GDIPlus_BitmapCreateHBITMAPFromBitmap($i_tip_GDIBmp) ;convert GDI+ bitmap to WinAPI bitmap
        _GDIPlus_BitmapDispose($i_tip_GDIBmp)
        Return $hHBITMAP
    Else
        Return $i_tip_GDIBmp
    EndIf
EndFunc

Regards,

Tip

Edited by tip

[center]MsgBox_Tipped: Eye candy msgboxes/inputboxes/loginboxes. | CreateBlankBox: Semi-transparent layers with borders and rounded corners.[/center]

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