We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexSuggestions & BugsSoftware Bugs › Can't load TGA (follow up)
Page Index Toggle Pages: 1
Can't load TGA (follow up) (Read 612 times)
Can't load TGA (follow up)
Sep 29th, 2005, 3:13pm
 
Sorry to start a new thread, but it's the only way to respond as the other one has been locked...

The TGA loader would actually work with 24bit images too, however the TGA you pointed to doesn't seem to stick to the proper format convention. It does mention 24bits/pixel, but doesn't have a value for the number of bits per colour channel. This is what my code didn't like (as I've been checking for 8bits/colour). Below is a patched version which still will only read uncompressed 24 or 32bit images, though it's a little bit more unprejudiced/unassuming... Wink

Code:
PImage loadTGA(String filename) {
byte[] buffer = loadBytes(filename);

// [toxi20050929] changed format validation
// only number of bits/pixel are checked now...
if (buffer[2] == 2 && (buffer[16] == 24 || buffer[16]==32 )) {
// get image dimensions
int w = ((buffer[13] & 0xff) << 8) + (buffer[12] & 0xff);
int h = ((buffer[15] & 0xff) << 8) + (buffer[14] & 0xff);
// check if image has alpha
boolean hasAlpha=(buffer[16] == 32);

// setup new image object
PImage img = new PImage(w,h);
img.format = (hasAlpha ? ARGB : RGB);

// targa's are written upside down,
// so we need to parse it in reverse
int index = (h-1) * w;
// actual bitmap data starts at byte 18
int offset = 18;

// read out line by line
for (int y = h-1; y >= 0; y--) {
for (int x = 0; x < w; x++) {
img.pixels[index + x] =
(buffer[offset++] & 0xff) |
((buffer[offset++] & 0xff) << 8) |
((buffer[offset++] & 0xff) << 16) |
(hasAlpha ? ((buffer[offset++] & 0xff) << 24) : 0xff000000);
}
index -= w;
}
return img;
}
die("loadImage(): bad targa image format");
return null;
}


Ben, if you want to apply the patch to the method in PApplet, only the marked line with the 1st if() statement has changed...
Re: Can't load TGA (follow up)
Reply #1 - Sep 29th, 2005, 7:51pm
 
God, toxi or better Toxi, god!

Thanks so much, thats really incredible and saves me quite some hassle.
Funnily enough I just yesterday thought about you (scary, when complete strangers think of one, huh) when I saw your Yacht located here in Amsterdam, just infront of my house!

...

Gracias!
Re: Can't load TGA (follow up)
Reply #2 - Sep 30th, 2005, 8:20pm
 
gosh, that's a bit over reacting, isn't it? Wink it was just an if() statement i've changed...

wicked picture, though!
i knew it. sooner or later my secret hideout would be discovered... Wink have a good weekend!
Re: Can't load TGA (follow up)
Reply #3 - Oct 15th, 2005, 7:46pm
 
as always, thanks toxi, i've applied this patch to rev 94.

and uh, nice boat. Wink
Page Index Toggle Pages: 1