Gui support for webp image format Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
DataLife
Posts: 445
Joined: 29 Sep 2013, 19:52

Gui support for webp image format

17 Jul 2019, 17:49

As far as I can determine AutoHotkey Gui's do not support webp image format.

As far as I can determine Windows 10 does not have built-in support for WebP

Does this mean that Autohotkey can not support webp until Windows 10 has built in support for webp ?
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
User avatar
DataLife
Posts: 445
Joined: 29 Sep 2013, 19:52

Re: Gui support for webp image format

17 Jul 2019, 21:31

Upon further research I have found that if you remove the -rwa from the end of the url and use UrlDownLoadToFile to download the file, it can be displayed by an AutoHotkey Gui.

Code: Select all

URLDownloadToFile,https://lh3.googleusercontent.com/BdQ1ngugnmV6uT960KOX0G9av7YJF4MnoEdSgi5xBINEC4YYuOp3Q7RUj7i4Cg0tRQ=w884-rwa,Webp-Image.webp
becomes

Code: Select all

URLDownloadToFile,https://lh3.googleusercontent.com/BdQ1ngugnmV6uT960KOX0G9av7YJF4MnoEdSgi5xBINEC4YYuOp3Q7RUj7i4Cg0tRQ=w884,Webp-Image.webp
I tried many ways to right click and choose Save Image As with Chrome and could not get the downloaded image to display in a GUI.

I have not tried to download the image with any other browsers.
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Gui support for webp image format

18 Jul 2019, 01:33

DataLife wrote:
17 Jul 2019, 21:31
Upon further research I have found that if you remove the -rwa from the end of the url and use UrlDownLoadToFile to download the file, it can be displayed by an AutoHotkey Gui.

I tried many ways to right click and choose Save Image As with Chrome and could not get the downloaded image to display in a GUI.
?
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Gui support for webp image format

18 Jul 2019, 02:26

DataLife wrote:
17 Jul 2019, 21:31
Upon further research I have found that if you remove the -rwa from the end of the url and use UrlDownLoadToFile to download the file, it can be displayed by an AutoHotkey Gui.

Code: Select all

URLDownloadToFile,https://lh3.googleusercontent.com/BdQ1ngugnmV6uT960KOX0G9av7YJF4MnoEdSgi5xBINEC4YYuOp3Q7RUj7i4Cg0tRQ=w884-rwa,Webp-Image.webp
becomes

Code: Select all

URLDownloadToFile,https://lh3.googleusercontent.com/BdQ1ngugnmV6uT960KOX0G9av7YJF4MnoEdSgi5xBINEC4YYuOp3Q7RUj7i4Cg0tRQ=w884,Webp-Image.webp
I tried many ways to right click and choose Save Image As with Chrome and could not get the downloaded image to display in a GUI.

I have not tried to download the image with any other browsers.
You actually download a png file after removing the -rwa from the end of the url.
User avatar
DataLife
Posts: 445
Joined: 29 Sep 2013, 19:52

Re: Gui support for webp image format

18 Jul 2019, 07:23

These are the other possible URL endings that need to be removed to down webp images as png.

-rw
-rw-no
-rwa

Does anyone know if ALL webp image urls will end in one of these 3 possibilities?
Maybe just Google chooses to end their webp images with these and other webpage developers can do what ever they want.
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Gui support for webp image format

18 Jul 2019, 08:11

DataLife wrote:
18 Jul 2019, 07:23
Does anyone know if ALL webp image urls will end in one of these 3 possibilities?
No. Example url:
https://upload.wikimedia.org/wikipedia/commons/b/b2/Vulphere_WebP_OTAGROOVE_demonstration_2.webp
User avatar
DataLife
Posts: 445
Joined: 29 Sep 2013, 19:52

Re: Gui support for webp image format

18 Jul 2019, 14:00

Does this mean that an Autohotkey Gui can not support webp until Windows 10 has built in support for webp ?
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Gui support for webp image format

18 Jul 2019, 15:10

There is a libwebp dll that can decode webp to a hbitmap, then you can use Gui, Add, Picture,, HBITMAP:%handle%. But I'm not good at dllcalls..

There is also a command line tool dwebp.exe, that can decode webp to png file: https://developers.google.com/speed/webp/download
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Gui support for webp image format

18 Jul 2019, 16:26

Using a .NET wrapper Imazen.WebP:

Code: Select all

; #Include, CLR.ahk ;https://www.autohotkey.com/boards/viewtopic.php?t=4633
#NoEnv
SetWorkingDir %A_ScriptDir%

if (A_PtrSize = 8)
	DllCall("SetDllDirectory", "str", "libwebp.x64")

hBitmap := Webp_FileToHbitmap("test.webp")
; hBitmap := Webp_URLToHbitmap("https://www.gstatic.com/webp/gallery/1.webp")

Gui, Add, Pic, , HBITMAP:%hBitmap%
Gui, Show
DllCall("DeleteObject", "Ptr", hBitmap)	
Return

GuiClose:
ExitApp

Webp_URLToHbitmap(URL) {
	whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
	whr.Open("GET", URL, true)
	whr.Send()
	whr.WaitForResponse()

	asm := Clr_LoadLibrary("Imazen.WebP.dll")
	SimpleDecoder := asm.CreateInstance("Imazen.WebP.SimpleDecoder")
	bitmap := SimpleDecoder.DecodeFromBytes(whr.responseBody, whr.responseBody.MaxIndex()+1)
	return bitmap.GetHbitmap()
}

Webp_FileToHbitmap(WebpFile) {
	bytes := File_To_SafeArray(WebpFile)

	asm := Clr_LoadLibrary("Imazen.WebP.dll")
	SimpleDecoder := asm.CreateInstance("Imazen.WebP.SimpleDecoder")
	bitmap := SimpleDecoder.DecodeFromBytes(bytes, bytes.MaxIndex()+1)
	return bitmap.GetHbitmap()
}

File_To_SafeArray(FileName) {
	f := FileOpen(FileName, "r")

	safeArr := ComObjArray(0x11, f.length) ; Create SAFEARRAY = VT_ARRAY|VT_UI1
	pvData := NumGet(ComObjValue(safeArr) + 12 + (A_PtrSize==8 ? 4 : 0)) ; get pvData memeber
	f.RawRead(pvData + 0, f.length) ; read raw data

	f.Close()
	return safeArr
}
Attachments
DisplayWebp.7z
(329.34 KiB) Downloaded 183 times
teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Gui support for webp image format

18 Jul 2019, 20:04

@tmplinshi, interesting!
Managed to decode webp without external ahk-libraries:

Code: Select all

webpUrl := "https://upload.wikimedia.org/wikipedia/commons/b/b2/Vulphere_WebP_OTAGROOVE_demonstration_2.webp"
libwebp32Url := "https://s3.amazonaws.com/resizer-dynamic-downloads/webp/0.5.2/x86/libwebp.dll"
libwebp64Url := "https://s3.amazonaws.com/resizer-dynamic-downloads/webp/0.5.2/x86_64/libwebp.dll"
filePath := A_ScriptDir . "\test.webp"
bitness := A_PtrSize*8
lib := A_ScriptDir . "\libwebp" . bitness . ".dll"

if !FileExist(filePath)
   URLDownloadToFile, % webpUrl, % filePath

if !FileExist(lib)
   URLDownloadToFile, % libwebp%bitness%Url, % lib

hBitmap := HBitmapFromWebP(lib, filePath, width, height)
Gui, Margin, 0, 0
Gui, Add, Pic, w600 h-1, HBITMAP:%hBitmap%
Gui, Show
Return

GuiClose() {
   ExitApp
}

HBitmapFromWebP(libwebp, WebpFilePath, ByRef width, ByRef height) {
   file := FileOpen(WebpFilePath, "r")
   len := file.RawRead(buff, file.Length)
   file.Close()
   if !len
      throw Exception("Failed to read the image file")
   
   if !hLib := DllCall("LoadLibrary", Str, libwebp, Ptr)
      throw Exception("Failed to load library. Error:" . A_LastError)
   
   if !pBits := DllCall(libwebp . "\WebPDecodeRGBA", Ptr, &buff, Ptr, len, IntP, width, IntP, height) {
      DllCall("FreeLibrary", Ptr, hLib)
      throw Exception("Failed to decode the image file")
   }
   
   oGDIp := new GDIp
   pBitmap := oGDIp.CreateBitmapFromScan0(width, height, pBits)
   hBitmap := oGDIp.CreateHBITMAPFromBitmap(pBitmap)
   DllCall(libwebp . "\WebPFree", Ptr, pBits)
   oGDIp.DisposeImage(pBitmap)
   DllCall("FreeLibrary", Ptr, hLib)
   Return hBitmap
}

class GDIp   {
   __New() {
      if !DllCall("GetModuleHandle", Str, "gdiplus", Ptr)
         DllCall("LoadLibrary", Str, "gdiplus")
      VarSetCapacity(si, A_PtrSize = 8 ? 24 : 16, 0), si := Chr(1)
      DllCall("gdiplus\GdiplusStartup", UPtrP, pToken, Ptr, &si, Ptr, 0)
      this.token := pToken
   }
   
   __Delete()  {
      DllCall("gdiplus\GdiplusShutdown", Ptr, this.token)
      if hModule := DllCall("GetModuleHandle", Str, "gdiplus", Ptr)
         DllCall("FreeLibrary", Ptr, hModule)
   }
   
   CreateBitmapFromScan0(Width, Height, pBits, PixelFormat := 0x26200A, Stride := "") {
      if !Stride {
         bpp := (PixelFormat & 0xFF00) >> 8
         Stride := ((Width * bpp + 31) & ~31) >> 3
      }
      DllCall("gdiplus\GdipCreateBitmapFromScan0", Int, Width, Int, Height
                                                 , Int, Stride, Int, PixelFormat
                                                 , Ptr, pBits, PtrP, pBitmap)
      Return pBitmap
   }
   
   CreateHBITMAPFromBitmap(pBitmap, Background=0xffffffff) {
      DllCall("gdiplus\GdipCreateHBITMAPFromBitmap", Ptr, pBitmap, PtrP, hbm, Int, Background)
      return hbm
   }
   
   DisposeImage(pBitmap) {
      return DllCall("gdiplus\GdipDisposeImage", Ptr, pBitmap)
   }
}
Last edited by teadrinker on 19 Jul 2019, 17:26, edited 1 time in total.
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Gui support for webp image format

18 Jul 2019, 23:36

@teadrinker Awesome! Thanks a lot. :thumbup:
User avatar
DataLife
Posts: 445
Joined: 29 Sep 2013, 19:52

Re: Gui support for webp image format

19 Jul 2019, 00:14

All these examples look great but I can not use them.

I distribute my ahk file and prefer not to download dwebp.exe or Dll's to someone else's computer.

I prefer my scripts run properly "out of the box".

I do not want to require the end user to download libraries, dll's, functions and external programs.

If I can not include it in my .ahk file then I prefer not to use it.

I remember when I was a newbie I would attempt to run someone else's code and I would get "Call to nonexistent function". I would go find that function then that particular message would go away and I would get it again referring to a different function. It was very frustrating.

I may have to settle on my ahk script to not support webp image formats as far as it goes with displaying it on a gui.

thanks for all the suggestions. They are interesting.
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Gui support for webp image format

19 Jul 2019, 00:34

hmm, so you distribute one single ahk file? Why not distribute a zip containing the ahk file and dll file?
teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Gui support for webp image format  Topic is solved

19 Jul 2019, 01:00

Just for fun, the code in attachment (since it is too large) will work without any dll and libraries at all.
Attachments
DecodeWebP.ahk
(1.13 MiB) Downloaded 235 times
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: Gui support for webp image format

19 Jul 2019, 01:31

DataLife wrote:
19 Jul 2019, 00:14
All these examples look great but I can not use them.

I distribute my ahk file and prefer not to download dwebp.exe or Dll's to someone else's computer.
libwebp dll is under the MIT license, which is very liberal. A person can do pretty much whatever, including commercially, as long as they give credit and include what is under the MIT license.

dwebp.exe appears to be under the Apache License 2.0. Though this license is a bit more convoluted than the MIT license, it allows personal and commercial use. However, you must also make sure to indicate what is under the Apache License.

While I totally understand that you want an all AHK solution, just saying that as 3rd party tools go, these permissive licenses will usually not cause trouble under most circumstances, even in a business or commercial setting.
User avatar
DataLife
Posts: 445
Joined: 29 Sep 2013, 19:52

Re: Gui support for webp image format

19 Jul 2019, 09:53

teadrinker wrote:
19 Jul 2019, 01:00
Just for fun, the code in attachment (since it is too large) will work without any dll and libraries at all.
This works as expected. Now I can display webp image formats in an Autohotkey Gui with an all AHK solution.

thank you very much
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Gui support for webp image format

06 Aug 2019, 21:26

This script is great. However, I think the colours are not coming out right e.g. blue instead of red.

Anyway, really nice script, thanks so much teadrinker and tmplinshi.

(Btw I've noticed a lot more webp files on the Internet very recently.)
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Gui support for webp image format

06 Aug 2019, 21:44

Ha ha! :lol: You are right.

Code: Select all

webpUrl := "https://upload.wikimedia.org/wikipedia/commons/b/b2/Vulphere_WebP_OTAGROOVE_demonstration_2.webp"
libwebp32Url := "https://s3.amazonaws.com/resizer-dynamic-downloads/webp/0.5.2/x86/libwebp.dll"
libwebp64Url := "https://s3.amazonaws.com/resizer-dynamic-downloads/webp/0.5.2/x86_64/libwebp.dll"
filePath := A_ScriptDir . "\test.webp"
bitness := A_PtrSize*8
lib := A_ScriptDir . "\libwebp" . bitness . ".dll"

if !FileExist(filePath)
   URLDownloadToFile, % webpUrl, % filePath

if !FileExist(lib)
   URLDownloadToFile, % libwebp%bitness%Url, % lib

hBitmap := HBitmapFromWebP(lib, filePath, width, height)
Gui, Margin, 0, 0
Gui, Add, Pic, w600 h-1, HBITMAP:%hBitmap%
Gui, Show
Return

GuiClose() {
   ExitApp
}

HBitmapFromWebP(libwebp, WebpFilePath, ByRef width, ByRef height) {
   file := FileOpen(WebpFilePath, "r")
   len := file.RawRead(buff, file.Length)
   file.Close()
   if !len
      throw Exception("Failed to read the image file")
   
   if !hLib := DllCall("LoadLibrary", Str, libwebp, Ptr)
      throw Exception("Failed to load library. Error:" . A_LastError)
   
   if !pBits := DllCall(libwebp . "\WebPDecodeBGRA", Ptr, &buff, Ptr, len, IntP, width, IntP, height) {
      DllCall("FreeLibrary", Ptr, hLib)
      throw Exception("Failed to decode the image file")
   }
   
   oGDIp := new GDIp
   pBitmap := oGDIp.CreateBitmapFromScan0(width, height, pBits)
   hBitmap := oGDIp.CreateHBITMAPFromBitmap(pBitmap)
   DllCall(libwebp . "\WebPFree", Ptr, pBits)
   oGDIp.DisposeImage(pBitmap)
   DllCall("FreeLibrary", Ptr, hLib)
   Return hBitmap
}

class GDIp   {
   __New() {
      if !DllCall("GetModuleHandle", Str, "gdiplus", Ptr)
         DllCall("LoadLibrary", Str, "gdiplus")
      VarSetCapacity(si, A_PtrSize = 8 ? 24 : 16, 0), si := Chr(1)
      DllCall("gdiplus\GdiplusStartup", UPtrP, pToken, Ptr, &si, Ptr, 0)
      this.token := pToken
   }
   
   __Delete()  {
      DllCall("gdiplus\GdiplusShutdown", Ptr, this.token)
      if hModule := DllCall("GetModuleHandle", Str, "gdiplus", Ptr)
         DllCall("FreeLibrary", Ptr, hModule)
   }
   
   CreateBitmapFromScan0(Width, Height, pBits, PixelFormat := 0x26200A, Stride := "") {
      if !Stride {
         bpp := (PixelFormat & 0xFF00) >> 8
         Stride := ((Width * bpp + 31) & ~31) >> 3
      }
      DllCall("gdiplus\GdipCreateBitmapFromScan0", Int, Width, Int, Height
                                                 , Int, Stride, Int, PixelFormat
                                                 , Ptr, pBits, PtrP, pBitmap)
      Return pBitmap
   }
   
   CreateHBITMAPFromBitmap(pBitmap, Background=0xffffffff) {
      DllCall("gdiplus\GdipCreateHBITMAPFromBitmap", Ptr, pBitmap, PtrP, hbm, Int, Background)
      return hbm
   }
   
   DisposeImage(pBitmap) {
      return DllCall("gdiplus\GdipDisposeImage", Ptr, pBitmap)
   }
}
I mixed up WebPDecodeBGRA and WebPDecodeRGBA, now it's fixed.
Attachments
DecodeWebP_AllInOne.ahk
(1.13 MiB) Downloaded 244 times
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: Gui support for webp image format

06 Aug 2019, 23:32

Good work teadrinker! :bravo:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: FanaticGuru, Google [Bot], Jaroz and 131 guests