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.
Page Index Toggle Pages: 1
PPM Images (Read 2428 times)
PPM Images
Apr 27th, 2007, 8:42pm
 
Here's a function I wrote to parse PPMs to PImages.  If you wanted to, you could include it in loadImage in a future version with very little adaptation.  If not, I hope someone who needs it in the future finds it here.

Code:

PImage loadPPM(String ppmfile) {
PImage out;
byte[] bytefile = loadBytes(ppmfile);
String stringfile = new String(bytefile);
StringTokenizer st = new StringTokenizer(stringfile);
st.nextToken();
int iwidth = Integer.parseInt(st.nextToken());
int iheight = Integer.parseInt(st.nextToken());
int maxval = Integer.parseInt(st.nextToken());
out = new PImage(iwidth,iheight);
out.parent = this;
int c=0;
int[] px = out.pixels;
int endoffset = (stringfile.endsWith("\n"))?1:0;
for (int i=bytefile.length-iwidth*iheight*3-endoffset;i<bytefile.length-endoffset;i+=3) {
px[c] = 0xff << 24;
if (bytefile[i]<0) px[c] = px[c] | ((int)round(255.0*(int)(256+bytefile[i])/maxval)) << 16;
else px[c] = px[c] | ((int)round(255.0*(int)bytefile[i]/maxval)) << 16;
if (bytefile[i+1]<0) px[c] = px[c] | ((int)round(255.0*(int)(256+bytefile[i+1])/maxval)) << 8;
else px[c] = px[c] | (int)round(255.0*(int)bytefile[i+1]/maxval) << 8;
if (bytefile[i+2]<0) px[c] = px[c] | ((int)round(255.0*(int)(256+bytefile[i+2])/maxval));
else px[c] = px[c] | ((int)round(255.0*(int)bytefile[i+2]/maxval));
c++;
}
return out;
}
Re: PPM Images
Reply #1 - Jun 23rd, 2008, 2:38pm
 
Thanks a lot.
It's exactly what I was looking for.
Page Index Toggle Pages: 1