Jump to content

GDIPlus Performance


TommyDDR
 Share

Recommended Posts

Hello, i'm dealing with GDIPlus and i would like to know if i'm doing something wrong or if it's simply GDIPlus calls speed limits :

#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include "./include/2d.au3"

Opt("GUIOnEventMode", 1)
Opt("MustDeclareVars", 1)

Global $gui
Global $taille[2] = [400, 400]
Global $hBackbuffer = _createCanvas($taille[0], $taille[1])
GUISetOnEvent($GUI_EVENT_CLOSE, quit, $2d_Gui)
Global $pos = 0

While 1
    Local $tim = TimerInit()
    loop()
    ConsoleWrite(TimerDiff($tim) & @CRLF)
WEnd

Func loop()
    Local $width = 2
    For $y = 0 To $taille[1] - 1 Step $width
        For $x = 0 To $taille[0] - 1 Step $width
            Local $r = ($x / $taille[0]) * 255
            Local $b = ($y / $taille[1]) * 255
            Local $color = rgbToRGB($r, Random(0, 255, 1), $b)
            Local $brush = _GDIPlus_BrushCreateSolid($color)
            _GDIPlus_GraphicsFillRect($hBackbuffer, $x, $y, $width, $width, $brush)
            _GDIPlus_BrushDispose($brush)
        Next
    Next
    _show()
EndFunc

Func rgbToRGB($r, $g, $b)
    Return BitOR(BitShift(255, -24), BitShift($r, -16), BitShift($g, -8), $b)
EndFunc

Func quit()
    Exit
EndFunc
#include <GDIPlus.au3>
#include <WindowsConstants.au3>
#include-once

_GDIPlus_Startup()

Global $2d_Gui
Global $2d_hGraphic
Global $2d_hBitmap
Global $2d_hBackBuffer
Global $2d_width
Global $2d_height

OnAutoItExitRegister(_freeRessources)

Func _createCanvas($width, $height)
    $2d_width = $width
    $2d_height = $height
    $2d_Gui = GUICreate("", $width, $height, Default, Default, $WS_POPUP)
    GUISetState(@SW_SHOW, $2d_Gui)
    $2d_hGraphic = _GDIPlus_GraphicsCreateFromHWND($2d_Gui)
    $2d_hBitmap = _GDIPlus_BitmapCreateFromGraphics($width, $height, $2d_hGraphic)
    $2d_hBackBuffer = _GDIPlus_ImageGetGraphicsContext($2d_hBitmap)
    Return $2d_hBackBuffer
EndFunc

Func _show()
    _GDIPlus_GraphicsDrawImageRect($2d_hGraphic, $2d_hBitmap, 0, 0, $2d_width, $2d_height)
EndFunc

Func _freeRessources()
    _GDIPlus_GraphicsDispose($2d_hBackBuffer)
    _GDIPlus_BitmapDispose($2d_hBitmap)
    _GDIPlus_GraphicsDispose($2d_hGraphic)
    _GDIPlus_Shutdown()
EndFunc

I only have 1 image per 2.3sec (40 000 brush created, fill rect, brush diposed per loop)

 

 

Edited by TommyDDR
Code color
_GUIRegisterMsg (Register more than 1 time the same Msg), _Resize_Window (GUICtrlSetResizing for children windows), _GUICtrlSetOnHover (Link a function when mouse go on, left, clic down, clic up, on a control), _InputHeure (Create an input for hour contain), _GUICtrlCalendar (Make a complete calendar), _GUICtrlCreateGraphic3D (Create a 3D graph), _ArrayEx.au3 (Array management), _GUIXViewEx.au3 (List/Tree View management).
Link to comment
Share on other sites

I tested with a different approach, using a struct of 400*400 pixels.  Filling the structure pixel by pixel like you did, and saving the result into file using  _GDIPlus_BitmapCreateFromScan0 /  _GDIPlus_ImageSaveToFile function.  Result 1.2 sec.

Link to comment
Share on other sites

Thank you for your reply.

Can you post the code here ?

_GUIRegisterMsg (Register more than 1 time the same Msg), _Resize_Window (GUICtrlSetResizing for children windows), _GUICtrlSetOnHover (Link a function when mouse go on, left, clic down, clic up, on a control), _InputHeure (Create an input for hour contain), _GUICtrlCalendar (Make a complete calendar), _GUICtrlCreateGraphic3D (Create a 3D graph), _ArrayEx.au3 (Array management), _GUIXViewEx.au3 (List/Tree View management).
Link to comment
Share on other sites

There :

#include <GUIConstants.au3>
#include <Constants.au3>
#include <GDIPlus.au3>

Global $size = 400
Global $__g_tStruct, $__g_tString, $__g_iWidth, $__g_iHeight
_GDIPlus_Startup()

_GetScreen_Initialize($size, $size)

$timer = TimerInit()
loop()
ConsoleWrite(TimerDiff($timer) & @CRLF)

$2d_Gui = GUICreate("", $size, $size, Default, Default, $WS_POPUP)
GUISetState(@SW_SHOW, $2d_Gui)
$2d_hGraphic = _GDIPlus_GraphicsCreateFromHWND($2d_Gui)
$2d_hBitmap =_GetScreen_GetBitMap()
$2d_hBackBuffer = _GDIPlus_ImageGetGraphicsContext($2d_hBitmap)

_GDIPlus_GraphicsDrawImageRect($2d_hGraphic, $2d_hBitmap, 0, 0, $size, $size)

While True
  If GUIGetMsg() = $GUI_EVENT_CLOSE Then ExitLoop
WEnd

_GDIPlus_GraphicsDispose($2d_hBackBuffer)
_GDIPlus_BitmapDispose($2d_hBitmap)
_GDIPlus_GraphicsDispose($2d_hGraphic)

;_GetScreen_SaveFile("Test.jpg")
;ConsoleWrite(TimerDiff($timer) & @CRLF)
_GDIPlus_Shutdown()

Func loop()
  Local $width = 2, $r, $b, $color
  For $y = 0 To $size - 1 Step $width
    $b = Int(($y / $size) * 255)
    For $x = 0 To $size - 1 Step $width
      $r = Int(($x / $size) * 255)
      $color = rgbToRGB($r, Random(0, 255, 1), $b)
      ;ConsoleWrite (Hex($color) & @CRLF)
      _GetScreen_SetPixelEX($x, $y, $color)
      _GetScreen_SetPixelEX($x + 1, $y, $color)
      _GetScreen_SetPixelEX($x, $y + 1, $color)
      _GetScreen_SetPixelEX($x + 1, $y + 1, $color)
    Next
  Next
EndFunc   ;==>loop

Func rgbToRGB($r, $g, $b)
  Return Dec("FF" & Hex($r, 2) & Hex($g, 2) & Hex($b, 2))
EndFunc   ;==>rgbToRGB

Func _GetScreen_SetPixelEX($iX, $iY, $iValue)
  DllStructSetData($__g_tStruct, "color", $iValue, ($iY * $__g_iWidth) + $iX + 1)
EndFunc   ;==>_GetScreen_SetPixel

Func _GetScreen_Initialize($iWidth, $iHeight)
  $__g_iWidth = $iWidth
  $__g_iHeight = $iHeight
  $__g_tStruct = DllStructCreate("int color [" & $iWidth * $iHeight & "]")
EndFunc   ;==>_GetScreen_Initialize

Func _GetScreen_GetBitMap()
  Return _GDIPlus_BitmapCreateFromScan0($__g_iWidth, $__g_iHeight, $GDIP_PXF32ARGB, $__g_iWidth * 4, DllStructGetPtr($__g_tStruct, "color"))
EndFunc   ;==>_GetScreen_GetBitMap

I extracted the _GetScreen_* functions from a personal (unpublished) UDF that I made to fast capture part of the screen.

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