Imports Images/ Sprites

Hey Guys!

Sorry for my unprofessional English, but I am from Germany. I started with Codea three days ago or so and now I can not solve the following problem:

How can I import my own images/ sprites into Codea or a project?

Thanks a lot for your answer(s),


Hi Xaxa

This feature is coming soon in Codea version 1.4, which has been submitted to Apple and is waiting to be reviewed for the App Store. Version 1.4 will let you get images from your photo library, Dropbox, and your clipboard.

Yeah, this sounds great! Which file types will be possible?

PNG format. You can probably use other formats supported by iOS (jpg, gif, tif, and so on) but Codea will always save images to PNG.

1.4 is cool :slight_smile:

I can vouch for jpeg - it’s working for me in beta. And I think I tried gif as well.

If you are too impatient to wait for version 1.4 there is a way using e.g. Perl on a Mac or a PC with Linux or even Windows…
The script below will accept an image as parameter and create the Lua code that will populate an image that you can use for your sprite.
You need to install Image::Magick.
Once you get the Lua code you can transfer it to your iPad using e-mail, SkyDrive whatever and do a copy-paste to Codea.
Quite brute-force and not optimized (all pixels are R,G,B) but worked for me with images up to 100x100 - although Codea does not like much big buffers and would crash on me once in a while :slight_smile:

# Read an image and generate a file with the content 
# of each pixels as 1 LUA tables Red Green Blue

use Image::Magick;

 $image = Image::Magick->new;
 $input_file_name = $ARGV[0];
 $image->Read($input_file_name) ;
 $input_file_name =~ m/^(.*)\\./;
 $file_name_base = $1;
 $height = $image->Get('rows');
 $width  = $image->Get('columns');

 @pixels = $image->GetPixels(map=>'RGB', height=>$height, width=>$width, normalize=>true);

$pixels_nr = 0;
@txt = "-- Lua array \

push @txt, "$file_name_base","Rgb = { ";
foreach my $pixel_color (@pixels) {
	push @txt, int($pixel_color*256), ",";
	$pixels_nr += 1;
	if ( ($pixels_nr % 20) == 0 ) { push @txt, "\
"; }
push @txt, " } \
push @txt, "$file_name_base","PixelsNr = ", int($pixels_nr/3), "\
push @txt, "$file_name_base","Width     = ", $width, "\
push @txt, "$file_name_base","Height    = ", $height, "\

push @txt, "$file_name_base","Image = image($file_name_base","Width,$file_name_base","Height)\
push @txt, "for y=0,$file_name_base","Height-1 do\
push @txt, "    for x=0,$file_name_base","Width-1 do\
push @txt, "        r = $file_name_base","Rgb[(3*x)+(y*$file_name_base","Width*3)+1]\
push @txt, "        v = $file_name_base","Rgb[(3*x)+1+(y*$file_name_base","Width*3)+1]\
push @txt, "        b = $file_name_base","Rgb[(3*x)+2+(y*$file_name_base","Width*3)+1]\

push @txt, "        if r == 256 and v == 256 and b == 256 then \
push @txt, "          $file_name_base","Image:set(x,y,0,0,0,0)\
push @txt, "        else\
push @txt, "          $file_name_base","Image:set(x,y,r*0.98,v*0.98,b*0.98,255)\
push @txt, "        end\
push @txt, "    end\
push @txt, "end\

print @txt

??? :-S

I fixed the code formatting - use “~~~” on a single line to start/stop code sections.

I’ve done this, by the way, or similar - the big problem is that it’s very easy with even a small image to make enough source code that it spanks Codea hard.

Real image support - getting them from the web, cut and paste, and saving them into custom spritepacks - should show up in version 1.4. It’s good, I’ve seen it.

If you download the iExplore utility for mac or PC, you can navigate to where the sprite packs are and create your own - they are just a folder with an XML plist file.

As @Marsdaddy says, using iExplorer is probably the cleanest way to get custom images in right now.

Edit: Also if you name your custom sprite pack folder “Documents.spritepack” and then use it in your code, when you upgrade to 1.4 and bring your sprites across your existing code should work.