Base64PNG_to_HICON() : Uses native PNG Decompression (requires WIN VISTA and later)

Post your working scripts, libraries and tools
User avatar
jNizM
Posts: 2665
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: Base64PNG_to_HICON() : Uses native PNG Decompression (requires WIN VISTA and later)

05 Sep 2017, 08:05

Should be also possible with ComObjCreate("Msxml2.DOMDocument"), createElement("base64") and dataType := "bin.base64"
[AHK] 1.1.32.00 x64 Unicode | [WIN] 10 Pro (Version 2004) x64 | [GitHub] Profile
Donations are appreciated if I could help you
User avatar
jNizM
Posts: 2665
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: Base64PNG_to_HICON() : Uses native PNG Decompression (requires WIN VISTA and later)

05 Sep 2017, 09:03

Code: Select all

hICON := b64Decode(PNG10x10, 48, 48)

b64Decode(b64, w := 16, h := 16) ; borrowed from uberi
{
    static CharSet := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
    VarSetCapacity(bin, Ceil(((len := StrLen(b64)) / 4) * 3), 0), i := 1, p := 0
    loop % len // 4 {
        b := ((InStr(CharSet, SubStr(b64, i,     1), true) - 1) << 0x12)
           | ((InStr(CharSet, SubStr(b64, i + 1, 1), true) - 1) << 0x0c)
           | ((InStr(CharSet, SubStr(b64, i + 2, 1), true) - 1) << 0x06)
           | ((InStr(CharSet, SubStr(b64, i + 3, 1), true) - 1)        )
        NumPut((b >> 0x10) | (((b >> 0x08) & 0xff) << 0x08) | ((b & 0xff) << 0x10), bin, p, "uint")
        i += 4, p += 3
    }
    if (len & 3) {
        b := ((InStr(CharSet, SubStr(b64, i, 1), true) - 1) << 0x12) | ((InStr(CharSet, SubStr(b64, i + 1, 1), true) - 1) << 0x0c)
        NumPut(b >> 0x10, bin, p, "uchar")
        if (len & 1) {
            b |= ((InStr(CharSet, SubStr(b64, i + 2, 1), true) - 1) << 0x06)
            NumPut((b >> 0x08) & 0xff, bin, p + 1, "uchar")
        }
    }
    return DllCall("CreateIconFromResourceEx", "ptr", &bin, "uint", len, "int", true, "uint", 0x30000, "int", W, "int", H, "uint", 0, "uptr") 
}
[AHK] 1.1.32.00 x64 Unicode | [WIN] 10 Pro (Version 2004) x64 | [GitHub] Profile
Donations are appreciated if I could help you
Helgef
Posts: 4464
Joined: 17 Jul 2016, 01:02
Contact:

Re: Base64PNG_to_HICON() : Uses native PNG Decompression (requires WIN VISTA and later)

05 Sep 2017, 13:36

SKAN wrote: Its my policy. I will specifically mention, if my function is not meant for XP.
:salute:
SKAN wrote:A perfect one-liner to avoid the additional DllCall() would be a good improvement for that function.
round(strlen(B64)/4*3) or round(strlen(strReplace( B64,"=","=",e))/4*3-e), either will work, I prefer the first. I wouldn't do the floor, I'd worry about potentally rounding down exactSize-0.0000000000x to exactSize-1 when there is no padding.
I guess you eliminate the size calculation dllcall for the Base64Enc function too, nChars:=ceil(nBytes/4*3)+linebreaks+spaces. :?:

@ jNizM, hi and interesting code :wave: (My guess is your nick is the most copy-pasted on the forum, I can't remeber how to spell it at least :D )

Cheers.
User avatar
SKAN
Posts: 853
Joined: 29 Sep 2013, 16:58

Re: Base64PNG_to_HICON() : Uses native PNG Decompression (requires WIN VISTA and later)

06 Sep 2017, 12:02

@ jNizM
Thanks for sharing :)
PS: I miss uberi.
Helgef wrote:round(strlen(B64)/4*3) or round(strlen(strReplace( B64,"=","=",e))/4*3-e), either will work, I prefer the first.
Thank you. I will test it and reply. :)
User avatar
SKAN
Posts: 853
Joined: 29 Sep 2013, 16:58

Base64PNG_to_HICON() - Updated

07 May 2020, 14:00

I wrote:
Helgef wrote:round(strlen(B64)/4*3) or round(strlen(strReplace( B64,"=","=",e))/4*3-e), either will work, I prefer the first.
Thank you. I will test it and reply. :)
I tested well. round(strlen(strReplace( B64,"=","=",e))/4*3-e) works well :thumbup: round(strlen(B64)/4*3) doesn't.

I've updated the code with this slightly modified floor version from @jeeswg : bytes := floor(strlen(RTrim( B64,"="))*3/4)
User avatar
SKAN
Posts: 853
Joined: 29 Sep 2013, 16:58

PNG_to_HICON()

22 May 2020, 15:57

I was searching for something else and landed on this ask-for-help topic
LoadPicture to use it as icon by @john_c

So, here is the modified version of Base64PNG_to_HICON() which
loads ICON from a PNG file.

Code: Select all

PNG_to_HICON(File, W:=0, H:=0) { ; By SKAN 
FileRead, Bin ,*c %File%
Return DllCall("CreateIconFromResourceEx", "Ptr",&Bin, "UInt",VarSetCapacity(Bin)
             , "Int",True, "UInt",0x30000, "Int",W, "Int",H, "UInt",0, "UPtr")             
}

; DEMO

#NoEnv
#Warn
#SingleInstance, Force
SetWorkingDir %A_ScriptDir%

UrlDownloadToFile, https://www.autohotkey.com/assets/images/ahk-logo-no-text241x78-180.png, ahk.png
Gui, Add, Picture,, % "HICON:" PNG_to_HICON("ahk.png")
Gui, Show
Return

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 23 guests