PNG to Base64 string
|
12-22-2018, 03:24 AM
Post: #1
|
|||
|
|||
PNG to Base64 string
Considering that this:
Code: AFiles("SomeName"):=G0; My question is, has anyone explored this on the Prime? I've used PNG<>Base64 converters online to be able to create HTML pages without external links to images. I'm about to research the method to convert between the formats, but maybe there's an easy method that someone is aware of? |
|||
12-22-2018, 08:30 PM
Post: #2
|
|||
|
|||
RE: PNG to Base64 string
I had some time this morning to investigate this PNG to Base64 business further.
Although I now have something that works, it is not pretty or fast, and certainly not optimized. Here it is for anyone interested, I will try to improve on it when I have more time. Code: BinaryString(); I confirmed the results with https://codebeautify.org/base64-to-image-converter Pretty neat! It appears that the bulk of the time is spent formatting the binary strings, padding with 0's, etc. |
|||
12-27-2018, 05:09 AM
Post: #3
|
|||
|
|||
RE: PNG to Base64 string
Here is a small sample program that actually creates the HTML file in the current app's AFiles. I've made the screen capture simple so the speed is pretty good for demonstration purposes. With more complex screen content of course it gets more involved and slower.
Code: WriteBase64(); I'll follow up in the next post about the challenge to make this program faster. |
|||
12-27-2018, 05:27 AM
Post: #4
|
|||
|
|||
RE: PNG to Base64 string
To demonstrate how a PNG to Base64 is decoded and then encoded, I'll use a basic block of 3 bytes of data expressed in decimal format as {137, 80, 78}.
Step 1: Convert these bytes to binary format using SETBASE({137,80,78},1). The result is {#10001001b,#1010000b,#1001110b} Step 2: Format as string, making sure each string is 8 chars long, left-padding with zeroes where required, resulting in: 10001001 01010000 01001110 Step 3: Combine and then break into 6-bit chunks: 100010010101000001001110 to 100010 010101 000001 001110 Step 4: Format as binary integers to get: {#100010b,#10101b,#1b,#1110b} Step 5: Format as decimal integers SETBASE({#100010b,#10101b,#1b,#1110b},3) to get: {#34d,#21d,#1d,#14d} Step6: Look up the nth character in the Base64 alphabet (see https://en.wikipedia.org/wiki/Base64) for each integer to get: iVBO And so you iterate over each three byte block to compile the string, and in the end deal with any fractional remainders. For anyone more familiar with these things; is there a quicker and simpler way to get from {137, 80, 78} to {34, 21, 1, 14} on the HP Prime? Any suggestions would be appreciated. |
|||
12-27-2018, 06:16 AM
Post: #5
|
|||
|
|||
RE: PNG to Base64 string
Do you actually need Step 5?
— Ian Abbott |
|||
12-27-2018, 08:23 AM
(This post was last modified: 12-27-2018 09:31 AM by Didier Lachieze.)
Post: #6
|
|||
|
|||
RE: PNG to Base64 string
(12-27-2018 05:27 AM)Jacob Wall Wrote: For anyone more familiar with these things; is there a quicker and simpler way to get from {137, 80, 78} to {34, 21, 1, 14} on the HP Prime? Any suggestions would be appreciated. Here is how I would do it : Code: L0:={137,80,78}; L1:={}; L1 will return {34,21,1,14}, but you can also do the lookup and build the Base64 string directly in the loop rather than building L1. |
|||
12-27-2018, 06:35 PM
Post: #7
|
|||
|
|||
RE: PNG to Base64 string | |||
12-27-2018, 06:47 PM
(This post was last modified: 12-27-2018 09:09 PM by Jacob Wall.)
Post: #8
|
|||
|
|||
RE: PNG to Base64 string
(12-27-2018 08:23 AM)Didier Lachieze Wrote: Here is how I would do it : Interesting, this makes sense. I don't work with binary enough to have figured that one out easily. I implemented this method and compared performance to original version, there is a significant improvement. Using a fairly typical screen resulting in a ~26KB file; my times were:
I experimented reading 3 bytes at a time with AFilesB, but that proved to be much slower than reading in the maximum number of bytes into a local variable, then processing it 3 bytes per iteration. Here's the latest version: Code: WriteBase64(); EDIT: Removed duplicate <body> tag |
|||
12-27-2018, 08:41 PM
Post: #9
|
|||
|
|||
RE: PNG to Base64 string
I didn't try your latest version nor examined it thoroughly, but in htmlcontent:="..." you have <body> two times.
Prime G2, 15C CE |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 2 Guest(s)