By
UEZ
I wrote a dll and the wrapper to convert non-animated WebP images to a GDI/GDI+ format / encode any GDI+ supported image to WebP format for use in Autoit.
What is WebP?
You can find more information about WebP and WebP tools / source codes here: https://developers.google.com/speed/webp
Maybe useful for one or the other... 🙂
WebP.au3:
;Version 0.3.0 build 2022-05-07 beta
#include-once
#include <GDIPlus.au3>
Enum $WEBP_PRESET_DEFAULT = 0, _ ; default preset.
$WEBP_PRESET_PICTURE, _ ; digital picture, like portrait, inner shot
$WEBP_PRESET_PHOTO, _ ; outdoor photograph, with natural lighting
$WEBP_PRESET_DRAWING, _ ; hand or line drawing, with high-contrast details
$WEBP_PRESET_ICON, _ ; small-sized colorful images
$WEBP_PRESET_TEXT ; text-like
; #FUNCTION# ====================================================================================================================
; Name ..........: WebP_Ver
; Description ...: Displays the DLL version information
; Syntax ........: WebP_Ver([$sPath2DLL = ""])
; Parameters ....: $sPath2DLL - [optional] a string value. Default is "". Path to WebP dll if not in script dir
; Return values .: None
; Author ........: UEZ
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func WebP_Ver($sPath2DLL = "")
Local $sDLL = Path2DLL($sPath2DLL)
If Not FileExists($sDLL) Then Return SetError(1, 0, 0) ;DLL not found
DllCall($sDLL, "none", "WebP_DLL_Version")
Return True
EndFunc ;==>WebP_Ver
; #FUNCTION# ====================================================================================================================
; Name ..........: WebP_BitmapGetInfo
; Description ...: Gets some rudimentary information about the WebP image
; Syntax ........: WebP_BitmapGetInfo($sFilename[, $sPath2DLL = ""])
; Parameters ....: $sFilename - file to load
; $sPath2DLL - [optional] a string value. Default is "". Path to WebP dll if not in script dir
; Return values .: Struct
; Author ........: UEZ
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........: https://developers.google.com/speed/webp
; Example .......: No
; ===============================================================================================================================
Func WebP_BitmapGetInfo($sFilename, $sPath2DLL = "")
Local $sDLL = Path2DLL($sPath2DLL)
If Not FileExists($sDLL) Then Return SetError(1, 0, 0) ;DLL not found
If Not FileExists($sFilename) Then Return SetError(2, 0, 0) ;file not found
Local $iFileSize = FileGetSize($sFilename), $nBytes
Local $tBuffer = DllStructCreate("struct;byte bin[" & $iFileSize & "];endstruct")
Local Const $hFile = _WinAPI_CreateFile($sFilename, 2, 2)
_WinAPI_ReadFile($hFile, $tBuffer, $iFileSize, $nBytes)
_WinAPI_CloseHandle($hFile)
If Int(BinaryMid($tBuffer.bin, 1, 4)) <> 1179011410 Or Int(BinaryMid($tBuffer.bin, 9, 6)) <> 88331643929943 Then Return SetError(3, 0, 0) ;header must contain RIFF and WEBPVP
Local $tWebPBitstreamFeatures = DllStructCreate("struct;long width; long height; long has_alpha; long has_animation; long format; ulong pad[5];endstruct")
Local $iReturn = DllCall($sDLL, "long", "WebP_BitmapGetInfo", "struct*", $tBuffer, "uint", $iFileSize, "struct*", $tWebPBitstreamFeatures)
If $iReturn = 0 Then Return SetError(4, 0, 0)
Return $tWebPBitstreamFeatures
EndFunc ;==>WebP_BitmapGetInfo
; #FUNCTION# ====================================================================================================================
; Name ..........: WebP_BitmapCreateGDIp
; Description ...: Converts (decodes) a WebP image from disk to a GDI / GDI+ bitmap handle
; Syntax ........: WebP_BitmasFilename[, $bGDIImage = False[, $sPath2DLL = ""]])
; Parameters ....: $sFilename - file to load
; $bGDIImage - [optional] a boolean value. Default is False (GDIPlus bitmap handle). If True then output is GDI bitmap handle
; $sPath2DLL - [optional] a string value. Default is "". Path to WebP dll if not in script dir
; Return values .: GDI / GDIPlus bitmap handle
; Author ........: UEZ
; Modified ......:
; Remarks .......: Currently only WebP images are supported - no animated WebP images yet!
; Related .......:
; Link ..........: https://developers.google.com/speed/webp
; Example .......: No
; ===============================================================================================================================
Func WebP_BitmapCreateGDIp($sFilename, $bGDIImage = False, $sPath2DLL = "")
Local $sDLL = Path2DLL($sPath2DLL)
If Not FileExists($sDLL) Then Return SetError(1, 0, 0) ;DLL not found
If Not FileExists($sFilename) Then Return SetError(2, 0, 0) ;file not found
Local $iFileSize = FileGetSize($sFilename), $nBytes
Local $tBuffer = DllStructCreate("byte bin[" & $iFileSize & "]")
Local Const $hFile = _WinAPI_CreateFile($sFilename, 2, 2)
_WinAPI_ReadFile($hFile, $tBuffer, $iFileSize, $nBytes)
_WinAPI_CloseHandle($hFile)
If Int(BinaryMid($tBuffer.bin, 1, 4)) <> 1179011410 Or Int(BinaryMid($tBuffer.bin, 9, 6)) <> 88331643929943 Then Return SetError(3, 0, 0) ;header must contain RIFF and WEBPVP
Local Const $hBitmap = DllCall($sDLL, "ptr", "WebP_BitmapCreateGDIp", "struct*", $tBuffer, "uint", $iFileSize, "boolean", $bGDIImage)[0]
If $hBitmap = 0 Then Return SetError(4, 0, 0)
Return $hBitmap
EndFunc ;==>WebP_BitmapCreateGDIp
; #FUNCTION# ====================================================================================================================
; Name ..........: WebP_BitmapCreateGDIpFromMem
; Description ...: Converts (decodes) a WebP image from memory to a GDI / GDI+ bitmap handle
; Syntax ........: WebP_BitmapCreateGDIpFromMem($tBuffer[, $iBufferSize = 0[, $bGDIImage = False[, $sPath2DLL = ""]]])
; Parameters ....: $tBuffer - a dll struct with WebP binary data as content or pointer to the memory data
; $iBufferSize - the size of the binary data (file size)
; $bGDIImage - [optional] a boolean value. Default is False (GDIPlus bitmap handle). If True then output is GDI bitmap handle
; $sPath2DLL - [optional] a string value. Default is "". Path to WebP dll if not in script dir
; Return values .: GDI / GDIPlus bitmap handle
; Author ........: UEZ
; Modified ......:
; Remarks .......: Currently only WebP images are supported - no animated WebP images yet!
; Related .......:
; Link ..........: https://developers.google.com/speed/webp
; Example .......: No
; ===============================================================================================================================
Func WebP_BitmapCreateGDIpFromMem($tBuffer, $iBufferSize = 0, $bGDIImage = False, $sPath2DLL = "")
Local $sDLL = Path2DLL($sPath2DLL)
If Not FileExists($sDLL) Then Return SetError(1, 0, 0) ;DLL not found
If $iBufferSize = 0 Then Return SetError(2, 0, 0)
Local $binMem
If IsPtr($tBuffer) Then
Local $tMem = DllStructCreate("byte bin[" & $iBufferSize & "]", $tBuffer)
$binMem = $tMem.bin
Else
$binMem = DllStructGetData($tBuffer, 1)
EndIf
If Int(BinaryMid($binMem, 1, 4)) <> 1179011410 Or Int(BinaryMid($binMem, 9, 6)) <> 88331643929943 Then Return SetError(3, 0, 0) ;header must contain RIFF and WEBPVP
Local Const $hBitmap = DllCall($sDLL, "ptr", "WebP_BitmapCreateGDIp", "struct*", $tBuffer, "uint", $iBufferSize, "bool", $bGDIImage)[0]
If $hBitmap = 0 Then Return SetError(4, 0, 0)
Return $hBitmap
EndFunc ;==>WebP_BitmapCreateGDIpFromMem
; #FUNCTION# ====================================================================================================================
; Name ..........: WebP_CreateWebPLossySimpleFromBitmap
; Description ...: Converts a bitmap to WebP lossy image and save it to HD
; Syntax ........: WebP_CreateWebPLossySimpleFromBitmap($sFilename, $hBitmap[, $iQuality = 75[, $sPath2DLL = ""]])
; Parameters ....: $sFilename - file to load
; $hBitmap - GDIPlus bitmap handle
; $iQuality - [optional] an integer value. Default is 75. Valid range is 0 - 100.
; $sPath2DLL - [optional] a string value. Default is "". Path to WebP dll if not in script dir.
; Return values .: 0 for failure, 1 for success.
; Author ........: UEZ
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........: https://developers.google.com/speed/webp
; Example .......: No
; ===============================================================================================================================
Func WebP_CreateWebPLossySimpleFromBitmap($sFilename, $hBitmap, $iQuality = 75, $sPath2DLL = "")
If $sFilename = "" Then Return SetError(1, 0, 0)
Local $sDLL = Path2DLL($sPath2DLL)
If Not FileExists($sDLL) Then Return SetError(2, 0, 0) ;DLL not found
Local $iReturn = DllCall($sDLL, "long", "WebP_CreateWebPLossySimpleFromBitmap", "str", $sFilename, "ptr", $hBitmap, "float", $iQuality)[0]
If $iReturn = 0 Then Return SetError(3, 0, 0)
Return 1
EndFunc ;==>WebP_CreateWebPLossySimpleFromBitmap
; #FUNCTION# ====================================================================================================================
; Name ..........: WebP_CreateWebPLosslessSimpleFromBitmap
; Description ...: Converts a bitmap to WebP lossless image and save it to HD
; Syntax ........: WebP_CreateWebPLosslessSimpleFromBitmap($sFilename, $hBitmap[, $sPath2DLL = ""])
; Parameters ....: $sFilename - file to load
; $hBitmap - GDIPlus bitmap handle
; $sPath2DLL - [optional] a string value. Default is "". Path to WebP dll if not in script dir.
; Return values .: 0 for failure, 1 for success.
; Author ........: UEZ
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........: https://developers.google.com/speed/webp
; Example .......: No
; ===============================================================================================================================
Func WebP_CreateWebPLosslessSimpleFromBitmap($sFilename, $hBitmap, $sPath2DLL = "")
If $sFilename = "" Then Return SetError(1, 0, 0)
Local $sDLL = Path2DLL($sPath2DLL)
If Not FileExists($sDLL) Then Return SetError(2, 0, 0) ;DLL not found
Local $iReturn = DllCall($sDLL, "long", "WebP_CreateWebPLosslessSimpleFromBitmap", "str", $sFilename, "ptr", $hBitmap)[0]
If $iReturn = 0 Then Return SetError(3, 0, 0)
Return 1
EndFunc ;==>WebP_CreateWebPLosslessSimpleFromBitmap
; #FUNCTION# ====================================================================================================================
; Name ..........: WebP_CreateWebPAdvancedFromBitmap
; Description ...: Converts a bitmap to WebP lossy / lossless image and save it to HD
; Syntax ........: WebP_CreateWebPAdvancedFromBitmap($sFilename, $hBitmap[, $WebPPreset = $WEBP_PRESET_DEFAULT[, $lossless = 0[,
; $quality = 75.0[, $method = 4[, $sns_strength = 50[, $filter_sharpness = 0[, $filter_strength = 60[,
; $pass = 1[, $level = 6[, $sPath2DLL = ""]]]]]]]]]])
; Parameters ....: $sFilename - file to load
; $hBitmap - GDIPlus bitmap handle
; $WebPPreset - [optional] an unknown value. Default is $WEBP_PRESET_DEFAULT.
; $lossless - [optional] an unknown value. Default is 0. 0 for lossy encoding / 1 for lossless..
; $quality - [optional] an unknown value. Default is 75.0. Valid range is 0 - 100.
; $method - [optional] a map. Default is 4. Valid range is 0 - 6 (0=fast, 6=slower-better).
; $sns_strength - [optional] a string value. Default is 50. Spatial Noise Shaping. 0=off, 100=maximum
; $filter_sharpness - [optional] a floating point value. Default is 0. Range: [0 = off .. 7 = least sharp]
; $filter_strength - [optional] a floating point value. Default is 60. Range: [0 = off .. 100 = strongest]
; $pass - [optional] a pointer value. Default is 1. Number of entropy-analysis passes (in [1..10]).
; $level - [optional] an unknown value. Default is 6. Between 0 (fastest, lowest compression) and 9 (slower, best compression) only valid for lossless = 1!
; $near_lossless - [optional] an unknown value. Default is 100. Near lossless encoding [0 = max loss .. 100 = off (default)].
; $alpha_compression - [optional] an unknown value. Default is 1. Algorithm for encoding the alpha plane (0 = none,1 = compressed with WebP lossless). Default is 1.
; $alpha_filtering - [optional] an unknown value. Default is 1. Predictive filtering method for alpha plane.0: none, 1: fast, 2: best. Default if 1.
; $alpha_quality - [optional] an unknown value. Default is 100. Between 0 (smallest size) and 100 (lossless). Default is 100.
; $target_size - [optional] an unknown value. Default is 0. If non-zero, set the desired target size in bytes.
; $NoSave - [optional] an unknown value. Default is False.
; $pMem - [optional] a string value. Default is Null. If $NoSave = True then the pointer to the memory which holds the data will be returned.
; $sPath2DLL - [optional] a string value. Default is "". Path to WebP dll if not in script dir.
; Return values .: negative value for failure, 1 for success or the struct with information (pointers, size) if $NoSave = True
; Author ........: UEZ
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........: https://developers.google.com/speed/webp
; Example .......: No
; ===============================================================================================================================
Func WebP_CreateWebPAdvancedFromBitmap($sFilename, $hBitmap, $WebPPreset = $WEBP_PRESET_DEFAULT, $lossless = 0, $quality = 75.0, $method = 4, $sns_strength = 50, _
$filter_sharpness = 0, $filter_strength = 60, $pass = 1, $level = 6, $near_lossless = 100, $alpha_compression = 1, $alpha_filtering = 1, $alpha_quality = 100, _
$target_size = 0, $NoSave = False, $sPath2DLL = "")
If $sFilename = "" And Not $NoSave Then Return SetError(1, 0, 0)
Local $sDLL = Path2DLL($sPath2DLL)
If Not FileExists($sDLL) Then Return SetError(2, 0, 0) ;DLL not found
Local $tMem = DllStructCreate("struct;ptr pPic; ptr pWriter; ptr pMemData; uint memsize;endstruct")
Local $iReturn = DllCall($sDLL, "long", "WebP_CreateWebPAdvancedFromBitmap", _
"str", $sFilename, _ ;Webp filename
"ptr", $hBitmap, _ ;handle to GDI+ bitmap
"long", $WebPPreset, _ ;WebPPreset
"long", $lossless, _ ;lossless
"float", $quality, _ ;quality
"long", $method, _ ;method
"long", $sns_strength, _ ;sns_strength
"long", $filter_sharpness, _ ;filter_sharpness
"long", $filter_strength, _ ;filter_strength
"long", $pass, _ ;pass
"long", $level, _ ;level
"long", $near_lossless, _ ;near_lossless
"long", $alpha_compression, _ ;alpha_compression
"long", $alpha_filtering, _ ;alpha_filtering
"long", $alpha_quality, _ ;alpha_quality
"long", $target_size, _ ;target_size
"bool", $NoSave, _ ;
"struct*", $tMem)[0]
If $iReturn < 0 Then Return SetError(3, 0, $iReturn)
If $NoSave And $tMem.memsize = 0 Then SetError(4, 0, 0)
Return $NoSave ? $tMem : $iReturn
EndFunc ;==>WebP_CreateWebPAdvancedFromBitmap
Func WebP_FreeUp(ByRef $tMem, $sPath2DLL = "")
Local $sDLL = Path2DLL($sPath2DLL)
If Not FileExists($sDLL) Then Return SetError(2, 0, 0) ;DLL not found
Local $iReturn = DllCall($sDLL, "long", "WebP_FreeUp", "struct*", $tMem)[0]
Return $iReturn
EndFunc
; #INTERNAL_USE_ONLY#============================================================================================================
; Name...........: Path2DLL
; Description ...: Return the path to the _WebP_x??.dll
; Author ........: UEZ
; Modified.......:
; Remarks .......: This function is used internally by WebP.au3
; ===============================================================================================================================
Func Path2DLL($sPath2DLL = "")
Return $sPath2DLL ? $sPath2DLL : @ScriptDir & (@AutoItX64 ? "\_WebP_x64.dll" : "\_WebP_x86.dll")
EndFunc ;==>Path2DLL
WebP Advanced Encoder GUI:
;Coded by UEZ build 2022-05-15
#AutoIt3Wrapper_UseX64=y
#AutoIt3Wrapper_Res_HiDpi=y
#AutoIt3Wrapper_Version=p
#AutoIt3Wrapper_Run_Au3Stripper=y
#Au3Stripper_Parameters=/so /pe ;/rm
#AutoIt3Wrapper_Run_After=del /f /q "%scriptdir%\%scriptfile%_stripped.au3"
#include <Array.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <SliderConstants.au3>
#include <StaticConstants.au3>
#include <WinAPISysWin.au3>
#include <WindowsConstants.au3>
#include "WebP.au3"
_GDIPlus_Startup()
#Region GUI
Global Const $SC_DRAGMOVE = 0xF012
Global Const $hGUI = GUICreate("WAE GUI v0.80 Beta", 322, 615, @DesktopWidth - 330, -1, -1, BitOR($WS_EX_ACCEPTFILES, $WS_EX_APPWINDOW))
GUISetFont(10, 400, 0, "Arial Narrow")
Global Const $Title = GUICtrlCreateLabel("WebP Advanced Encoder GUI", 5, 8, 310, 41)
GUICtrlSetFont(-1, 21, 400, 0, "Arial Narrow")
Global Const $icLoad = GUICtrlCreateIcon(@SystemDir & "\shell32.dll", -127, 8, 60, 32, 32, BitOR($GUI_SS_DEFAULT_ICON, $WS_BORDER))
GUICtrlSetTip(-1, "Load a GDI+ supported image")
Global Const $icSave = GUICtrlCreateIcon(@SystemDir & "\shell32.dll", -259, 56, 60, 32, 32, BitOR($GUI_SS_DEFAULT_ICON, $WS_BORDER))
GUICtrlSetTip(-1, "Save compressed image in WebP format.")
GUICtrlCreateLabel("", 0, 106, 324, 2, $SS_ETCHEDHORZ)
Global Const $lbPresets = GUICtrlCreateLabel("Presets", 10, 125, 39, 20)
Global Const $cbPreset = GUICtrlCreateCombo("Default", 120, 120, 145, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
GUICtrlSetData(-1, "Picture|Photo|Graph|Text")
Global Const $chkbLossless = GUICtrlCreateCheckbox("Lossless", 120, 152, 97, 17)
GUICtrlSetTip(-1, "Enable lossless compression. Default: lossy.")
Global Const $lbEncoding = GUICtrlCreateLabel("Encoding", 10, 152, 48, 20)
Global Const $lbQuality = GUICtrlCreateLabel("Quality", 10, 176, 36, 20)
Global Const $slQuality = GUICtrlCreateSlider(116, 176, 158, 21, BitOR($GUI_SS_DEFAULT_SLIDER, $TBS_ENABLESELRANGE))
GUICtrlSetLimit(-1, 100, 0)
GUICtrlSetData(-1, 75)
GUICtrlSetTip(-1, "Between 0 and 100. 0 gives the smallest size and 100 the largest.")
Global Const $ipQuality = GUICtrlCreateInput("", 274, 172, 28, 24, BitOR($GUI_SS_DEFAULT_INPUT, $ES_CENTER, $ES_READONLY))
GUICtrlSetData(-1, GUICtrlRead($slQuality))
Global Const $lbMethod = GUICtrlCreateLabel("Method", 10, 210, 39, 20)
Global Const $slMethod = GUICtrlCreateSlider(116, 210, 158, 21, BitOR($GUI_SS_DEFAULT_SLIDER, $TBS_ENABLESELRANGE))
GUICtrlSetLimit(-1, 6, 0)
GUICtrlSetData(-1, 4)
GUICtrlSetTip(-1, "Quality/speed trade-off (0=fast, 6=slower-better.")
Global Const $ipMethod = GUICtrlCreateInput("", 274, 206, 28, 24, BitOR($GUI_SS_DEFAULT_INPUT, $ES_CENTER, $ES_READONLY))
GUICtrlSetData(-1, GUICtrlRead($slMethod))
Global Const $lbSNS_Strength = GUICtrlCreateLabel("SNS-Strength", 10, 242, 66, 20)
Global Const $slSNS_Strength = GUICtrlCreateSlider(116, 244, 158, 21, BitOR($GUI_SS_DEFAULT_SLIDER, $TBS_ENABLESELRANGE))
GUICtrlSetLimit(-1, 100, 0)
GUICtrlSetData(-1, 50)
GUICtrlSetTip(-1, "Spatial Noise Shaping. 0=off, 100=maximum.")
Global Const $ipSSN = GUICtrlCreateInput("", 274, 240, 28, 24, BitOR($GUI_SS_DEFAULT_INPUT, $ES_CENTER, $ES_READONLY))
GUICtrlSetData(-1, GUICtrlRead($slSNS_Strength))
Global Const $lbFilterSharpness = GUICtrlCreateLabel("Filter Sharpness", 10, 274, 81, 20)
Global Const $slFilter_Sharpness = GUICtrlCreateSlider(116, 278, 158, 21, BitOR($GUI_SS_DEFAULT_SLIDER, $TBS_ENABLESELRANGE))
GUICtrlSetLimit(-1, 7, 0)
GUICtrlSetTip(-1, "Range: [0 = off .. 7 = least sharp].")
Global Const $ipFilter_Sharpness = GUICtrlCreateInput("", 274, 274, 28, 24, BitOR($GUI_SS_DEFAULT_INPUT, $ES_CENTER, $ES_READONLY))
GUICtrlSetData(-1, GUICtrlRead($slFilter_Sharpness))
Global Const $lbFilter_Strength = GUICtrlCreateLabel("Filter Strenght", 010, 304, 69, 20)
Global Const $slFilter_Strength = GUICtrlCreateSlider(116, 312, 158, 21, BitOR($GUI_SS_DEFAULT_SLIDER, $TBS_ENABLESELRANGE))
GUICtrlSetLimit(-1, 100, 0)
GUICtrlSetData(-1, 60)
GUICtrlSetTip(-1, "Range: [0 = off .. 100 = strongest]")
Global Const $ipFilter_Strength = GUICtrlCreateInput("", 274, 308, 28, 24, BitOR($GUI_SS_DEFAULT_INPUT, $ES_CENTER, $ES_READONLY))
GUICtrlSetData(-1, GUICtrlRead($slFilter_Strength))
Global Const $lbPass = GUICtrlCreateLabel("Pass", 10, 344, 27, 20)
Global Const $slPass = GUICtrlCreateSlider(116, 346, 158, 21, BitOR($GUI_SS_DEFAULT_SLIDER, $TBS_ENABLESELRANGE))
GUICtrlSetLimit(-1, 10, 1)
GUICtrlSetData(-1, 6)
GUICtrlSetTip(-1, "Number of entropy-analysis passes (in [1..10]).")
Global Const $ipPass = GUICtrlCreateInput("", 274, 342, 28, 24, BitOR($GUI_SS_DEFAULT_INPUT, $ES_CENTER, $ES_READONLY))
GUICtrlSetData(-1, GUICtrlRead($slPass))
Global Const $lbLevel = GUICtrlCreateLabel("Level", 10, 377, 30, 20)
Global Const $slLevel = GUICtrlCreateSlider(116, 380, 158, 21, BitOR($GUI_SS_DEFAULT_SLIDER, $TBS_ENABLESELRANGE))
GUICtrlSetLimit(-1, 9, 0)
GUICtrlSetData(-1, 6)
GUICtrlSetTip(-1, "Switch on lossless compression mode with the specified level between 0 and 9, with level 0 being the fastest, 9 being the slowest.")
Global Const $ipLevel = GUICtrlCreateInput("", 274, 376, 28, 24, BitOR($GUI_SS_DEFAULT_INPUT, $ES_CENTER, $ES_READONLY))
GUICtrlSetData(-1, GUICtrlRead($slLevel))
Global Const $lbAlpha_Compression = GUICtrlCreateLabel("Alpha Compression", 10, 410, 96, 20)
Global Const $chkbAlpha_Compression = GUICtrlCreateCheckbox("Enable", 120, 410, 97, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
Global Const $lbAlpha_Filtering = GUICtrlCreateLabel("Alpha Filtering", 10, 444, 71, 20)
Global Const $slAlpha_Filtering = GUICtrlCreateSlider(114, 448, 158, 21, BitOR($GUI_SS_DEFAULT_SLIDER, $TBS_ENABLESELRANGE))
GUICtrlSetLimit(-1, 2, 0)
GUICtrlSetData(-1, 1)
GUICtrlSetTip(-1, "Predictive filtering method for alpha plane. 0: none, 1: fast, 2: best. Default if 1.")
Global Const $ipAlpha_Filtering = GUICtrlCreateInput("", 274, 444, 28, 24, BitOR($GUI_SS_DEFAULT_INPUT, $ES_CENTER, $ES_READONLY))
GUICtrlSetData(-1, GUICtrlRead($slAlpha_Filtering))
Global Const $lbAlpha_Quality = GUICtrlCreateLabel("Alpha Quality", 8, 482, 66, 20)
Global Const $slAlpha_Quality = GUICtrlCreateSlider(114, 482, 158, 21, BitOR($GUI_SS_DEFAULT_SLIDER, $TBS_ENABLESELRANGE))
GUICtrlSetLimit(-1, 100, 0)
GUICtrlSetData(-1, 100)
GUICtrlSetTip(-1, "Between 0 (smallest size) and 100 (lossless).")
Global Const $ipAlpha_Quality = GUICtrlCreateInput("", 274, 478, 28, 24, BitOR($GUI_SS_DEFAULT_INPUT, $ES_CENTER, $ES_READONLY))
GUICtrlSetData(-1, GUICtrlRead($slAlpha_Quality))
Global Const $lbTarget_Size = GUICtrlCreateLabel("Target Size", 10, 518, 58, 20)
Global Const $ipTarget_Size = GUICtrlCreateInput("0", 120, 516, 121, 24, $ES_NUMBER)
GUICtrlSetTip(-1, "If non-zero, set the desired target size in bytes (lossy mode only!).")
Global Const $btnShow = GUICtrlCreateButton("Show Original Image", 10, 552, 123, 25)
GUICtrlSetTip(-1, "Press lmb and hold it to display original image.")
Global Const $btnApply = GUICtrlCreateButton("Apply Settings", 188, 552, 123, 25)
Global Const $StatusBar = _GUICtrlStatusBar_Create($hGUI)
_GUICtrlStatusBar_SetText($StatusBar, "Welcome to 'WebP Advanced Encoder GUI' ٩(●̮̮̃•̃)۶")
Global Const $hGUI_Image = GUICreate("", 0, 0, -1, -1, $WS_EX_TOOLWINDOW, $WS_EX_APPWINDOW, $hGUI)
Global Const $iPic_WebP = GUICtrlCreatePic("", 0, 0, 0, 0)
Global Const $dw = _WinAPI_GetSystemMetrics($SM_CXDLGFRAME), $dh = _WinAPI_GetSystemMetrics($SM_CYDLGFRAME) + _WinAPI_GetSystemMetrics($SM_CYSIZE) + 1
GUISetState(@SW_SHOW, $hGUI)
GUISetState(@SW_HIDE, $hGUI_Image)
Global Const $iDummy_Load = GUICtrlCreateDummy()
Global $sFile, $hImage, $hImage_GDI, $hHBitmap, $pMemData, $pMemData_Size, $tMem, $mp, $sFileSave, $hFile, $nBytes, $nBytes
GUIRegisterMsg($WM_DROPFILES, "WM_DROPFILES")
GUIRegisterMsg($WM_LBUTTONDOWN, "WM_LBUTTONDOWN")
#EndRegion GUI
While 1
$mp = GUIGetCursorInfo($hGUI)
If $hImage And $mp[2] And $mp[4] = $btnShow Then
_WinAPI_DeleteObject(GUICtrlSendMsg($iPic_WebP, $STM_SETIMAGE, $IMAGE_BITMAP, $hImage_GDI))
While $mp[2]
$mp = GUIGetCursorInfo($hGUI)
Sleep(10)
WEnd
_WinAPI_DeleteObject(GUICtrlSendMsg($iPic_WebP, $STM_SETIMAGE, $IMAGE_BITMAP, $hHBitmap))
EndIf
$aGUIGetMsg = GUIGetMsg(1)
Switch $aGUIGetMsg[1]
Case $hGUI
Switch $aGUIGetMsg[0]
Case $GUI_EVENT_CLOSE
If IsDllStruct($tMem) Then WebP_FreeUp($tMem)
If $hImage Then _GDIPlus_ImageDispose($hImage)
If $hHBitmap Then _WinAPI_DeleteObject($hHBitmap)
If $hImage_GDI Then _WinAPI_DeleteObject($hImage_GDI)
If $hGUI_Image Then GUIDelete($hGUI_Image)
_GDIPlus_Shutdown()
GUIDelete($hGUI_Image)
GUIDelete($hGUI)
Exit
Case $btnApply
If $hImage Then CompressAndDisplay($hImage)
Case $icLoad
$sFile = FileOpenDialog("Select an image to compress", "", "Images (*.jpg;*.bmp;*.png;*.gif;*.tif;*webp)")
If @error Then ContinueLoop
LoadImage($sFile)
Case $iDummy_Load
LoadImage($sFile)
Case $icSave
If $hImage Then
$sFileSave = FileSaveDialog("Save WebP Image", "", "WebP Image (*.webp)", BitOR($FD_PATHMUSTEXIST, $FD_PROMPTOVERWRITE), "", $hGUI)
If @error Then ContinueLoop
$hFile = _WinAPI_CreateFile($sFileSave, 1)
$iResult = _WinAPI_WriteFile($hFile, $tMem.pMemData, $tMem.memsize, $nBytes)
_WinAPI_CloseHandle($hFile)
If Not $iResult Then
MsgBox($MB_ICONERROR, "ERROR", "Unable to save WebP image to disk!", 30, $hGUI)
Else
MsgBox($MB_ICONINFORMATION, "Information", "WebP image successfully save to disk", 10, $hGUI)
EndIf
EndIf
Case $slAlpha_Filtering
GUICtrlSetData($ipAlpha_Filtering, GUICtrlRead($slAlpha_Filtering))
Case $slAlpha_Quality
GUICtrlSetData($ipAlpha_Quality, GUICtrlRead($slAlpha_Quality))
Case $slFilter_Sharpness
GUICtrlSetData($ipFilter_Sharpness, GUICtrlRead($slFilter_Sharpness))
Case $slFilter_Strength
GUICtrlSetData($ipFilter_Strength, GUICtrlRead($slFilter_Strength))
Case $slLevel
GUICtrlSetData($ipLevel, GUICtrlRead($slLevel))
Case $slMethod
GUICtrlSetData($ipMethod, GUICtrlRead($slMethod))
Case $slPass
GUICtrlSetData($ipPass, GUICtrlRead($slPass))
Case $slQuality
GUICtrlSetData($ipQuality, GUICtrlRead($slQuality))
Case $slSNS_Strength
GUICtrlSetData($ipSSN, GUICtrlRead($slSNS_Strength))
EndSwitch
Case $hGUI_Image
Switch $aGUIGetMsg[0]
Case $GUI_EVENT_CLOSE
EndSwitch
EndSwitch
WEnd
Func LoadImage($sFile)
If StringRight($sFile, 5) = ".webp" Then
If $hImage Then _GDIPlus_ImageDispose($hImage)
If $hImage_GDI Then _WinAPI_DeleteObject($hImage_GDI)
$hImage = WebP_BitmapCreateGDIp($sFile)
If @error Or $hImage = 0 Then
Return MsgBox($MB_ICONERROR, "ERROR", "Unable to decode WebP image!", 30, $hGUI)
EndIf
Else
If $hImage Then _GDIPlus_ImageDispose($hImage)
If $hImage_GDI Then _WinAPI_DeleteObject($hImage_GDI)
$hImage = _GDIPlus_ImageLoadFromFile($sFile)
If @error Or $hImage = 0 Then
Return MsgBox($MB_ICONERROR, "ERROR", "Unknown image format!", 30, $hGUI)
EndIf
EndIf
$hImage_GDI = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
CompressAndDisplay($hImage)
EndFunc ;==>LoadImage
Func CompressAndDisplay($hImage)
Local Const $aDim = _GDIPlus_ImageGetDimension($hImage)
If $hHBitmap Then _WinAPI_DeleteObject($hHBitmap)
$pMemData = 0
$pMemData_Size = 0
Local $iPreset = 0
Switch GUICtrlRead($cbPreset)
Case "Default"
$iPreset = 0
Case "Picture"
$iPreset = 1
Case "Photo"
$iPreset = 2
Case "Graph"
$iPreset = 3
Case "Text"
$iPreset = 4
EndSwitch
If IsDllStruct($tMem) Then WebP_FreeUp($tMem)
Local $fTimer = TimerInit(), $end
$tMem = WebP_CreateWebPAdvancedFromBitmap("", $hImage, _
$iPreset, _
BitAND(GUICtrlRead($chkbLossless), $GUI_CHECKED), _
GUICtrlRead($slQuality), _
GUICtrlRead($slMethod), _
GUICtrlRead($slSNS_Strength), _
GUICtrlRead($slFilter_Sharpness), _
GUICtrlRead($slFilter_Strength), _
GUICtrlRead($slPass), _
GUICtrlRead($slLevel), _
100, _ ;$near_lossless (0 - 100)
BitAND(GUICtrlRead($chkbAlpha_Compression), $GUI_CHECKED), _
GUICtrlRead($slAlpha_Filtering), _
GUICtrlRead($slAlpha_Quality), _
GUICtrlRead($ipTarget_Size), _
True) ;hold the compressed image in memory only, no save to HD!
$end = TimerDiff($fTimer)
_GUICtrlStatusBar_SetText($StatusBar, "Compressed size: " & Round($tMem.memsize / 1024, 2) & " kb / encoded in " & Round($end, 2) & " ms.")
$hHBitmap = WebP_BitmapCreateGDIpFromMem($tMem.pMemData, $tMem.memsize, True)
If @error Then Return MsgBox($MB_ICONERROR, "ERROR", "Unable to compress image", 30, $hGUI)
WinMove($hGUI_Image, "", (@DesktopWidth - $aDim[0]) / 2, (@DesktopHeight - $aDim[1]) / 2, _
$aDim[0] + $dw, _
$aDim[1] + $dh)
WinSetTitle($hGUI_Image, "", StringRegExpReplace($sFile, ".+\\(.*)", "$1") & " / " & Round(FileGetSize($sFile) / 1024, 2) & " kb")
GUICtrlSetPos($iPic_WebP, 0, 0, $aDim[0] - 1, $aDim[1] - 1)
_WinAPI_DeleteObject(GUICtrlSendMsg($iPic_WebP, $STM_SETIMAGE, $IMAGE_BITMAP, $hHBitmap))
If WinGetState($hGUI_Image) <> $WIN_STATE_VISIBLE Then WinSetState($hGUI_Image, "", @SW_SHOW)
WinActivate($hGUI)
EndFunc ;==>CompressAndDisplay
Func WM_DROPFILES($hWnd, $iMsg, $wParam, $lParam)
Local $i = 1
Local $aFileList = _WinAPI_DragQueryFileEx($wParam)
Do
If StringInStr(FileGetAttrib($aFileList[$i]), "D") Then
_ArrayDelete($aFileList, $i)
Else
$i += 1
EndIf
Until $i = UBound($aFileList)
$aFileList[0] = UBound($aFileList) - 1
$sFile = $aFileList[1]
_WinAPI_DragFinish($wParam)
;~ GUICtrlSendToDummy($iDummy_Load)
LoadImage($sFile)
Return 0
EndFunc ;==>WM_DROPFILES#
Func WM_LBUTTONDOWN($hWnd, $iMsg, $wParam, $lParam)
_SendMessage($hWnd, $WM_SYSCOMMAND, $SC_DRAGMOVE, 0)
EndFunc ;==>_WM_LBUTTONDOWN
Download 7-Zip archive for the UDF, DLLs and examples!
WebP v0.3.0 build 2022-05-07 beta.7z