Page 2 of 2

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

Posted: 05 Sep 2017, 08:05
by jNizM
Should be also possible with ComObjCreate("Msxml2.DOMDocument"), createElement("base64") and dataType := "bin.base64"

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

Posted: 05 Sep 2017, 09:03
by jNizM

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") 
}

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

Posted: 05 Sep 2017, 13:36
by Helgef
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.

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

Posted: 06 Sep 2017, 12:02
by SKAN
@ 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. :)

Base64PNG_to_HICON() - Updated

Posted: 07 May 2020, 14:00
by SKAN
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)

PNG_to_HICON()

Posted: 22 May 2020, 15:57
by SKAN
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

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

Posted: 25 May 2020, 20:29
by SirSocks
Very useful! Thank you!