Image import (or "do you like my fez?")

Managed to import an image without hackery - code to come, but feast your eyeballs on this:

https://twitter.com/#!/bortels/status/144248682827616259/photo/1

Things I learned:

  1. print() is really slow

  2. my base64 stuff doesn’t work

  3. string.gsub does not modify in place (that had me stuck for a few hours)

  4. I look DAMN GOOD in a fez. But - I knew that.

  5. Image data is really big.

Amazing work, Bortels! That’s pretty surprising, I didn’t expect anyone to come up with an image loader :slight_smile:

John suggested that when you copy an image to the clipboard, it should paste into Codea as a base64 encoded string. So you could copy and paste images straight in from Safari or elsewhere.

W00T! This is crazy! How did you do that? Is it taken from camera or photo app?

You mean the end has come for the worst image format ever made?

I was going to name and patent it cdm with a .cdm extention.

cdm = comma delimited mess

Code: http://bortels.posterous.com/tomfez

I also made a quick perl program (at work, alas) to convert the jpeg to the hex format in the source code.

I couldn’t make your “copy image and paste” suggestion above work. Wah. It sounds a lot easier than the juggle I did.

The image is originally from photobooth - I resized it to a more managable 64x64 pixels, and used netpbm to convert it to a nice ascii format I could then munge into hex (to avoid the Codea spinnies) and import. See the code above for the import routine - it’s fairly straightforward. When we get some sort of binary conduit (sockets! sockets! sockets!) I’ll write (port, really, almost certainly) a jpeg (and gif and png) decoder over. Pity - most “lua” code to do that sort of thing are bindings to C libraries…

I was going to hold off (Bee’s filled poly code is about 80% done - I had to swap my data representation halfway thru), but figured I’d post what I had :slight_smile:

Awesome.

@Bortels Sorry I meant to say that John suggested that we should add that feature after he saw your work here!

LOL - that explains it!

Really - any way we can get images into a project in a straightforward manner would be cool. It’s one of the key reasons I keep harping on sockets - I want/plan to grab them on first run, and stash them in persistent storage for followup runs. But - adding spritepacks, or cut and paste - whatever works. (I also want sockets for the holy grail of online games, but there you go).

Thinking about it - I don’t want to paste an image as base64, because that’s still a giant ugly wad of alphabet soup that doesn’t really belong in source code. Let us make new Spritepacks, as part of the project, and cut and paste into those. Or even just designate a tab as “image” and paste it in as an image (ie. don’t show the data - just a thumbnail). Or import a zipfile full of images, or such. encoded images in a tab as text is so 90’s. I know it wasn’t really designed to let us do Spritepacks - fix that!

BY THE WAY - I didn’t see it commented on here, but I did see the tweet - the new sprite pack looks AWESOME. What is good in life? Spaceships. Just sayin. There are umpteen games that will benefit from some nice top view spaceships. Very cool!

Here’s the teensy perl script I used to convert the jpeg - https://gist.github.com/1444149

Note having netpbm is a prerequisite.

I took the converted file and removed the first 3 lines (ie. I just wanted the raw data). If your image has > 256 colors, you’ll need to modify things.

Let me be the first to say - this is a stunt. In the long run, graphics are big, and need to be handled compressed - decompressing them, then doubling their size (at least) to fit them into source code is… “non optimal”. Indeed. This image is 3610 bytes - but the image plus a bit of source to use it is like 30k.

You don’t have to paste in Base64 as source code and leave it there. You could paste it in once and save it as project data yes? It looks like a string.

Once global data exists these string could be copied up to global and then copied down to project data.

As far as the size, if other formats were supported, old images could be loaded and saved in the newer better format.

That all being said … is writing from an image to a Base64 string doable? (I probally should have started with that question).

You can certainly image:get each pixel, base 64 encode the whole thing, and save it to project persistent data. But - yuck. And you’d still have to decode the whole thing every time you started the program. So - bigger, and way slower.

I’m looking forward to more support in later releases but for now, there is a way.