Page 1 of 2

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

Posted: 03 Sep 2017, 14:06
by SKAN
Demo script for Base64PNG_to_HICON()
Note: You need to pass original file-size as parameter 2.

Code: Select all

#NoEnv
#SingleInstance, Force
#NoTrayIcon
Menu, Tray, UseErrorLevel

PNG10x10 := "iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKAgMAAADwXCcuAAAACVBMVEXOgwD///90SQCsNO1SAA"
          . "AAH0lEQVQI12MAARFWBgYx1gYwFg0F0ksb4HwupgaQEgBelQU38+DeFgAAAABJRU5ErkJggg=="
hICON := Base64PNG_to_HICON( PNG10x10, 109, 48, 48 )  ; Create a HICON for Tray

Menu, Tray, Icon, HICON:*%hICON%                      ; AHK makes a copy of HICON when * is used
Menu, Tray, Icon
DllCall( "DestroyIcon", "Ptr",hICON  )                ; Destroy original HICON


PNG256x256 := "iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAMAAABrrFhUAAAAflBMVEXOgwD///+AUQDz5NSTXQDj3NSliWe7dwCnagDGtaPnx6Pbp2eG"
. "VQDt1rz6+PXRjSTJgADCewCycQCeZADkwJbhuIf58ur06t/qzrDesHirbQDw3ci0nILYn1TVlj+KYSSiZwCYYQCOWgDVyby+q5acfFSTbz/u4dTc08jN"
. "v7D3Mcn0AAACq0lEQVR42uzaXW/aMBSA4WMn4JAQyAff0A5o123//w/OkSallUblSDm4qO9759zYfo4vI0RERERERERERERERERERERERB97Kva5L3lX"
. "6deroljKXVoWxcpvWCbv2vkP++JJdFvud8nCfFZSrlQP8bwqE/NZiyTfa82hOJqgNrkotd6YoI6FKFSa4LYqM1huTXCljN7aGIX9dSbgW8vYJWZIopAZ"
. "UgIAAADEBHCuigvwy9VRAawvbQ91NICJP8A8zZoqIkDXPIsG8K+Liwngu1ZRAXxtXADbxgawTVwAGx0gBQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
. "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADgI8BDBQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD6"
. "AFOFHgrAKgQAAAAAAAAAADwegBuphwX4ln+KAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
. "AAAAAAAAAAAAAAAAAAAAAPA1AY5mQAsNgIUZ0O/RAQozoJkGQG4GNB0dQNbhE/hjNQBkF/4CT3Z8AFmutkGbv/y0OgDyvNuYgLavP6wGQGdQ5GVy+xCT"
. "yezU3V4LoDNY50lyG3/yMpt2t1cB6EunvtOsr1u/2RuJQm9T36zv1S/7m+sD2CGJQva/AQDAnQAudkBzUWhuB3SRsXN2QJkolNkBORm9JnwCZ1HpHP4C"
. "G1GoOlyDNm9rUao+Bw3heqhEqcplbXr7EGmaNbWoVjdZmt7GT9vMVaKf8zVZn/PVcsdq58v6Ds5XCRERERER/W0PDgkAAAAABP1/bfQEAAAAAAAL2VmK"
. "C7LwdTIAAAAASUVORK5CYII="
hICON := Base64PNG_to_HICON( PNG256x256, 878, 256, 256 )

Gui, Margin, 20, 20
Gui, Add, Picture,, HICON:%hICON%
Gui, Show,, Base64PNG_to_HICON() DEMO 
Return ;     // end of auto-execcute section //


;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Base64PNG_to_HICON( B64, nBytes, W:=16, H:=16 ) {
Local Bin, BLen, hICON:=0                     
  VarSetCapacity( Bin,nBytes,0 ), BLen := StrLen(B64)
  If DllCall( "Crypt32.dll\CryptStringToBinary", "Str",B64, "UInt",BLen, "UInt",0x1
            , "Ptr",&Bin, "UIntP",nBytes, "Int",0, "Int",0 )
     hICON := DllCall( "CreateIconFromResourceEx", "Ptr",&Bin, "UInt",nBytes, "Int",True
                     , "UInt",0x30000, "Int",W, "Int",H, "UInt",0, "UPtr" )            
Return hICON
}
Original images used in above demo are attached below:

Re: Base64 to HICON : Native PNG Decompression (requires WIN VISTA and later)

Posted: 03 Sep 2017, 14:57
by Helgef
Nice! Thanks for sharing.
Cheers.
Spoiler

Re: Base64 to HICON : Native PNG Decompression (requires WIN VISTA and later)

Posted: 04 Sep 2017, 07:32
by SKAN
Helgef wrote:Nice! Thanks for sharing.
Thanks! .. and thanks for the snippet! :)

Include script icon in script.

Posted: 04 Sep 2017, 07:33
by SKAN
Re-posting example script from ask for help topic:

Code: Select all

; Ask for help topic: https://autohotkey.com/boards/viewtopic.php?t=36640

#NoEnv
#SingleInstance, Force
#NoTrayIcon
Menu, Tray, UseErrorLevel
hICON := Base64toHICON()               ; Create a HICON
Menu, Tray, Icon, HICON:*%hICON%       ; AHK makes a copy of HICON when * is used
Menu, Tray, Icon
DllCall( "DestroyIcon", "Ptr",hICON )  ; Destroy original HICON

Gui, Show, w400 h200, TrayIcon with in-script Base64 PNG image raw data
Return 

Base64toHICON() { ; 16x16 PNG image (236 bytes), requires WIN Vista and later
Local B64 := "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAs0lEQVQ4jbXTO27CUBCF4e9a7rIJECugZ"
. "QlZQhoUSmihg8rpqKEOTZbAEihhBUjsxBSWEZYY2Qrwl/fM49zRTDoWYIwVBrpxRoFdjik2HRNrBvjFR455/TpclreI0"
. "09qZATaPEPvUYu/Q9z+TutlSHFoKyl7IhnkkbDel5HU4H0OFp/N0USOwgLwNWp38PQXXlKg27gfU+a4oE+1otEGBtolH"
. "Yt/HVPNLMMWE9WJduWMb2yvxO4l4nkelNAAAAAASUVORK5CYII=",       Bin, Blen, nBytes:=236, hICON:=0                     
  
  VarSetCapacity( Bin,nBytes,0 ), BLen := StrLen(B64)
  If DllCall( "Crypt32.dll\CryptStringToBinary", "Str",B64, "UInt",BLen, "UInt",0x1
            , "Ptr",&Bin, "UIntP",nBytes, "Int",0, "Int",0 )
     hICON := DllCall( "CreateIconFromResourceEx", "Ptr",&Bin, "UInt",nBytes, "Int",True
                     , "UInt",0x30000, "Int",16, "Int",16, "UInt",0, "UPtr" )            
Return hICON
}

Re: Base64 to HICON : Native PNG Decompression (requires WIN VISTA and later)

Posted: 04 Sep 2017, 08:22
by SpeedMaster
Great script! Thanks ! :dance:
example: with Bart Simpson :lol:

Code: Select all

#NoEnv
#SingleInstance, Force
#NoTrayIcon
Menu, Tray, UseErrorLevel

PNG16x16 := "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAAVFBMVEUAAABzXBdkUBVwWhYmHQtNPhR1Yi8xJwlSQiNmTx0tJxVjQk5QME6XeR6SdR3/zDN/Zhm/mSb///+/v79/TH8/Jj8/Mwy/cr9/ckw/DAz/mf9/f3/ie28vAAAAD3RSTlMAv3+/v38/v3+/vz8/Pz/96/+UAAAAeElEQVQY012M2xKDIAwFQfDSa05CwNr+/38WFHSGfcvOyZrM7KwdrHVvUwEzcSbNTVAFvRg6wa8muo87iMFZ2OOegnyCBD4TS5CfiOBqJuwLhruaKA206JRKkQlLFSPRsbmZZkaPslm9OfEKrF/VR8vEuG0aVePzD5ayCTRkTSbeAAAAAElFTkSuQmCC"
hICON := Base64PNG_to_HICON( PNG16x16 )  ; Create a HICON for Tray

Menu, Tray, Icon, HICON:*%hICON%                      ; AHK makes a copy of HICON when * is used
Menu, Tray, Icon
DllCall( "DestroyIcon", "Ptr",hICON  )                ; Destroy original HICON


PNG256x256 := "iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAMAAABrrFhUAAAC8VBMVEUAAAAvLzBPUFAVFRcbHB0KCgw/P0ADAgQDAwYDBAUBAQMDBAUNDQ8UExUfHyADAwUDAwYFBQgEBAYJCQwHBwomJicMDA8RERIDAwQhIiMJCAwODQoODQv/2Q//NhjevQz///8h0v/eLxSa3g2z/w8ctd7dvAsBAQEGBgSIiIj+2Am6/w8MDAflxAz/4A//+Z94eHjd3d3oxgyh5w3/75bhwAz61g6d4w3/8JuNj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256x256)

Gui, Margin, 20, 20
Gui, Add, Picture,gname, HICON:%hICON%
Gui, Show,, Base64PNG_to_HICON() DEMO 
Return ;     // end of auto-execcute section //

name:
msgbox, Bart Simpson
return

guiclose:
ExitApp
return


;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Base64PNG_to_HICON( B64, nBytes:="", W:="", H:="" ) {
Local Bin, BLen, hICON:=0  
if !nBytes
nBytes := ceil(strlen(strReplace( B64,"=","=",e))/4*3)-e
 
  VarSetCapacity( Bin,nBytes,0 ), BLen := StrLen(B64)
  If DllCall( "Crypt32.dll\CryptStringToBinary", "Str",B64, "UInt",BLen, "UInt",0x1
            , "Ptr",&Bin, "UIntP",nBytes, "Int",0, "Int",0 )
     hICON := DllCall( "CreateIconFromResourceEx", "Ptr",&Bin, "UInt",nBytes, "Int",True
                     , "UInt",0x30000, "Int",W, "Int",H, "UInt",0, "UPtr" )            
Return hICON
}

Re: Base64 to HICON : Native PNG Decompression (requires WIN VISTA and later)

Posted: 04 Sep 2017, 08:33
by SKAN
SpeedMaster wrote:Great script! Thanks ! :dance:
example: with Bart Simpson :lol:
Thanks. Very nice!
BTW, why have you commented out API DestroyIcon()?

Re: Base64 to HICON : Native PNG Decompression (requires WIN VISTA and later)

Posted: 04 Sep 2017, 09:46
by SKAN
I have refined Base64ToHICON() version of SpeedMaster
Using a very tiny PNG 10x10 (109 bytes) resized to 48x48 icon.

Code: Select all

#NoEnv
#SingleInstance, Force
#NoTrayIcon
Menu, Tray, UseErrorLevel

PNG10x10 := "iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKAgMAAADwXCcuAAAACVBMVEXOgwD///90SQCsNO1SAA"
          . "AAH0lEQVQI12MAARFWBgYx1gYwFg0F0ksb4HwupgaQEgBelQU38+DeFgAAAABJRU5ErkJggg=="
hICON := Base64toHICON( PNG10x10, 109, 48, 48 )  ; Create a HICON

Menu, Tray, Icon, HICON:*%hICON%                 ; AHK makes a copy of HICON when * is used
Menu, Tray, Icon
DllCall( "DestroyIcon", "Ptr",hICON  )           ; Destroy original HICON

Gui, Show, w400 h200, TrayIcon with in-script Base64 PNG image raw data
Return 


Base64toHICON( B64, nBytes, W:=16, H:=16 ) {
Local Bin, BLen, hICON:=0                     
  VarSetCapacity( Bin,nBytes,0 ), BLen := StrLen(B64)
  If DllCall( "Crypt32.dll\CryptStringToBinary", "Str",B64, "UInt",BLen, "UInt",0x1
            , "Ptr",&Bin, "UIntP",nBytes, "Int",0, "Int",0 )
     hICON := DllCall( "CreateIconFromResourceEx", "Ptr",&Bin, "UInt",nBytes, "Int",True
                     , "UInt",0x30000, "Int",W, "Int",H, "UInt",0, "UPtr" )            
Return hICON
}

Re: Base64 to HICON : Native PNG Decompression (requires WIN VISTA and later)

Posted: 04 Sep 2017, 10:04
by SpeedMaster
SKAN wrote:BTW, why have you commented out API DestroyIcon()?
Oops, sorry :roll:
The script did not work the first time. :cry:
I thought maybe it was because of that. :roll:
After a some tests, I finally found that it was necessary to specify the exact file size. :!:
Base64toHICON(icon,bytes)

Re: Base64 to HICON : Native PNG Decompression (requires WIN VISTA and later)

Posted: 04 Sep 2017, 10:05
by Helgef
wikipedia wrote:The ratio of output bytes to input bytes is 4:3 (33% overhead). Specifically, given an input of n bytes, the output will be {\displaystyle 4\lceil n/3\rceil } 4 \lceil n/3 \rceil bytes long, including padding characters.
So bytes := ceil(strlen(strReplace( B64,"="))/4*3) :?: Edit: Maybe needs ceil. Lol, I didn't see the ciel symbol in the wiki article but noticed the \lceil \rceil from the copy-paste. Edit2: ceil symbol, ⌈ ⌉ (Send {u+2308}{u+2309} :) )

Re: Base64 to HICON : Native PNG Decompression (requires WIN VISTA and later)

Posted: 04 Sep 2017, 11:44
by SpeedMaster
Helgef wrote: bytes := ceil(strlen(strReplace( B64,"="))/4*3)
Thanks, Helgef. :thumbup:
I updated my previous post with this formula with new formula.
It seems to work well. So no need to specify the file size.

Re: Base64 to HICON : Native PNG Decompression (requires WIN VISTA and later)

Posted: 04 Sep 2017, 13:34
by jeeswg
hex (uses 16 characters):
n bytes, need 2*n characters
[256] -> [16][16]
[256][256] -> [16][16][16][16]
[256][256][256] -> [16][16][16][16][16][16]

[256] -> [aaaa][aaaa]
[256][256] -> [aaaa][aaaa][bbbb][bbbb]
[256][256][256] -> [aaaa][aaaa][bbbb][bbbb][cccc][cccc]

base64 (uses 64 characters):
n bytes, need Ceil((4/3)*n) characters [note: 4/3 = 1.333333...]
[256] -> [64][4|(16)]
[256][256] -> [64][4|16][16|(4)]
[256][256][256] -> [64][4|16][16|4][64]

[256] -> [aaaaaa][aa|####]
[256][256] -> [aaaaaa][aa|bbbb][bbbb|##]
[256][256][256] -> [aaaaaa][aa|bbbb][bbbb|cc][cccccc]

n bytes -> Ceil((4/3)*n) characters [excluding =]
n characters [excluding =] -> Floor((3/4)*n) bytes

vCharsExclEquals := Ceil((4/3)*vSizeData)
vSizeData := Floor((3/4)*vCharsExclEquals)

Re: Base64 to HICON : Native PNG Decompression (requires WIN VISTA and later)

Posted: 04 Sep 2017, 15:18
by SKAN
CryptStringToBinary() returns correct required size even when output is 1 byte.
Helgef wrote:So bytes := ceil(strlen(strReplace( B64,"="))/4*3)
Doesn't seem to give consistent results.

Here is my test script:

Code: Select all

#NoEnv
#SingleInstance, Force


FileRead, Bin, *c %A_AhkPath%
Loop 300 {

  B64 := Base64Enc( Bin, A_Index, 400, 0 )
  bytes := ceil(strlen(strReplace( B64,"="))/4*3)

MsgBox % "[" B64 "]`nActual size: " A_Index "`nYour formula yeilds: "  bytes 
}  

Base64Enc( ByRef Bin, nBytes, LineLength := 64, LeadingSpaces := 0 ) { ; By SKAN / 18-Aug-2017
Local Rqd := 0, B64, B := "", N := 0 - LineLength + 1  ; CRYPT_STRING_BASE64 := 0x1
  DllCall( "Crypt32.dll\CryptBinaryToString", "Ptr",&Bin ,"UInt",nBytes, "UInt",0x1, "Ptr",0,   "UIntP",Rqd )
  VarSetCapacity( B64, Rqd * ( A_Isunicode ? 2 : 1 ), 0 )
  DllCall( "Crypt32.dll\CryptBinaryToString", "Ptr",&Bin, "UInt",nBytes, "UInt",0x1, "Str",B64, "UIntP",Rqd )
  If ( LineLength = 64 and ! LeadingSpaces )
    Return B64
  B64 := StrReplace( B64, "`r`n" )        
  Loop % Ceil( StrLen(B64) / LineLength )
    B .= Format("{1:" LeadingSpaces "s}","" ) . SubStr( B64, N += LineLength, LineLength ) . "`n" 
Return RTrim( B,"`n" )    
}


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

Posted: 04 Sep 2017, 16:00
by jeeswg
This formula works for me using that test, the original calculation appears to be doing the inverse of what it should be doing. [EDIT: The approximate inverse, not the exact inverse.] Also see my post above.

Code: Select all

bytes := floor(strlen(strReplace( B64,"="))*3/4)
I suppose there could be some potential confusion, in that you might want to know how many bytes the string would need if stored in an ANSI plaintext file. Hence I've tried to be extra clear in my examples in a post above.

In the code in this post, 'bytes' refers to the size of the original binary data.

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

Posted: 04 Sep 2017, 16:10
by SKAN
jeeswg wrote:

Code: Select all

bytes := floor(strlen(strReplace( B64,"="))*3/4)
Yes! :bravo: :D

I think RTrim() would be faster than StrReplace() : bytes := floor(strlen(RTrim( B64,"="))*3/4)

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

Posted: 04 Sep 2017, 16:52
by SpeedMaster
According to the function found in this post :
https://stackoverflow.com/questions/133 ... alculation
result = (Math.ceil(base64String.length() / 4) * 3 ) - padding

transcribed into ahk: :think:

Code: Select all

  strReplace( B64,"=","=",padding)
  bytes := (ceil(strlen(B64)/4)*3)-padding

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

Posted: 04 Sep 2017, 17:37
by Helgef
I didn't read carefully enough, my original proposal is ofc not right, it (the quote, not the code) can be useful to go from binary length to string length. To go from string length to binary length, I see this quote just below my original wiki-quote,
wikipedia wrote:When decoding Base64 text, four characters are typically converted back to three bytes. The only exceptions are when padding characters exist. A single '=' indicates that the four characters will decode to only two bytes, while '==' indicates that the four characters will decode to only a single byte
source
So,

Code: Select all

reqBytes(B64){
	local e,l
	strreplace(B64,"=","=",e),strreplace(B64,"`n","`n",l)
	return (strlen(B64)-l) / 4 * 3 - (e == 1 ? 1 : (e == 2 ? 2 : 0))
}
I adjust the string length if there are linebreaks `n, beacuse I saw you added some. And because I saw you removed some, note CryptBinaryToStringhas flag CRYPT_STRING_NOCRLF=0x40000000 for omitting linebreaks.

Cheers.
@ SpeedMaster, Edit: A new day, new eyes, that it is correct :lol:

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

Posted: 04 Sep 2017, 18:35
by SKAN
Helgef wrote:I adjust the string length if there are linebreaks `n, beacuse I saw you added some.
It doesn't matter when JOIN option of string concatenation is in effect.
And because I saw you removed some, note CryptBinaryToStringhas flag CRYPT_STRING_NOCRLF=0x40000000 for omitting linebreaks.
The flag is not supported in XP. RAW HEX is also not supported in XP, and hence Base64 is now my preferred format.

:)

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

Posted: 05 Sep 2017, 02:19
by Helgef
Ok. For the record, strlen(strReplace( B64,"=","=",e))/4*3-e is the exact formula, short of float errors, so you could round it. But I prefer strlen(B64)/4*3 for readabillity, it will always be either the exact required size, 1 or 2 bytes to much. So it will always work and be fastest. Again, maybe round it too, because of float errors.

@ jeeswg, the EDIT:floor version works because you remove 1 or 2 characters and floor(1 * 4 / 3=1.333333)=1 and floor(2 * 4 / 3=2.666667)=2, not because floor is the inverse of ceil (which it is isn't).

@ SKAN XP ? :o :lol: It is probably the best we'll ever see though, in terms of reliabillity and overall performance.

Finally, sorry for derailing the thread.

Cheers.

Edit: @ jeeswg, I meant the floor version, not the ceil version,

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

Posted: 05 Sep 2017, 04:41
by SpeedMaster
Helgef wrote:Ok. For the record, strlen(strReplace( B64,"=","=",e))/4*3-e is the exact formula
Cheers.
Thanks, Helgef :thumbup:
I updated my previous post (Bart Simpson example) with the new formula.
nBytes will be automatically calculated if they are not specified. 8-)

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

Posted: 05 Sep 2017, 07:01
by SKAN
Helgef wrote:@ SKAN XP ? :o :lol: It is probably the best we'll ever see though, in terms of reliabillity and overall performance.
AutoHotkey 1.1 works from Win2k, I guess
While writing a whole app with XP support might not be a great idea, at least standalone functions ( like my Base encoder/decoder ) should be compatible with XP.
Its my policy. I will specifically mention, if my function is not meant for XP.
Helgef wrote:Finally, sorry for derailing the thread.
Not at all.
The conversation is important to me.
While the "textbook" method is to call CryptStringToBinary() twice, once to ascertain the length and a consecutive call to decode base64,
I have avoided it here as Crypt32.dll will be loaded and freed twice.

My Base64Dec() does it. A perfect one-liner to avoid the additional DllCall() would be a good improvement for that function.

Thanks to all of you for the feedback.