Shop OBEX P1 Docs P2 Docs Learn Events
On-the-fly Binary-to-Base64 Data Converter — Parallax Forums

On-the-fly Binary-to-Base64 Data Converter

Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
edited 2011-04-20 10:55 in Accessories
The Spinneret seems to exhibit difficulties serving images when separate file references are embedded in the HTML. This is due to some browsers trying to open additional sockets for downloading the images, which the W5100 chip ignores (at least as it's been programmed to date). One work-around to this uses Javascript to force the images to load one at a time, beginning after all the HTML has been loaded. Another option is to encode the images inline in base64 format so that they become part of the HTML and don't require being loaded separately.

Attached is an object and demo that does base64 conversion on-the-fly. The advantage of on-the-fly conversion is that the base64 data can be fed to the W5100 line-by-line, rather than having to buffer an entire file's worth in RAM. Here is a listing of the demo program:
CON

   _clkmode       = xtal1 + pll16x
   _xinfreq       = 5_000_000

OBJ

  sio   :       "FullDuplexSerial"
  b64   :       "base64"

PUB Start | i, row

  sio.start(31, 30, 0, 9600)
  waitcnt(cnt + clkfreq)
  sio.str(@html)
  sio.str(b64.img(string("gif")))
  repeat i from 0 to @image_end - @image - 1
    sio.str(b64.out(image[i]))
  sio.str(b64.end)
  sio.str(@html_end)  

DAT

html          byte      "<html><body>Propeller Forum Icon: ",0
html_end      byte      "</body></html>",0

image         file      "propeller.gif"
image_end

Here is the output it produces:
<html><body>Propeller Forum Icon: <img src="data:image/gif;base64,
R0lGODlhJQAjAPcAAAICAgEBAQUFBQ4ODgoKChAQECA5PnYqJmc5PXg1NRJGOQB5
OlFeKUxcPlpoOzo+ck89RlA8X2s+RCRGWiRDVjJEVTpPUBlGaBZSbRBOdyVMYCxT
bFlCS0lFUlRRXmRDS3NEQmxNV0VMYExRYk5WalJXa1NaalhccmdYZ2Nfc11ieV9l
emxld4kvKogyL4QyMYs1NI07O4c5OZw4MpM8Oaw2L6UyK7Q2Lrs3L6s4Mt0+Ls46
MdI6MeQ+NOk/NO9ANfRBNvxCN/9EOIU/QYhIRn6GHQCKQkaZf2+8dXHEf4SLHYyU
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SlRolDw8hwnBjGKm7vfDI+YjysCIIdXuYY2
uggOMkFxiH0TIw2rWEZ9DCAAAiDAAArAxz768Y+ADGQfB0CAgAAAOw==" />
</body></html>

When sent to a browser, this is what the output looks like:

attachment.php?attachmentid=80391&d=1303321873

The advantage of using base64 is offset somewhat by the 33% more bytes the server has to send when encoded this way. However, by doing on-the-fly conversion, the the internal image storage requirements do not increase.

-Phil
Sign In or Register to comment.