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.
IndexProgramming Questions & HelpSyntax Questions › loadBytes() vs. loadImage()
Page Index Toggle Pages: 1
loadBytes() vs. loadImage() (Read 632 times)
loadBytes() vs. loadImage()
Apr 9th, 2006, 10:54pm
 
Mighty List,

I am trying to load an image to my applet with loadBytes but it simply doesn't load (gives me out of bounds error). The image format is jpeg. Contrary when I try to load a raw image file it loads without any problems. Why?

Code:

byte input[];
byte output[];

int imgWidth;
int imgHeight;
int imgNumPixels;

void setup() {
imgWidth = 200;
imgHeight = 150;
imgNumPixels = imgWidth * imgHeight;

input = loadBytes("deneme.jpg"); // deneme.raw loads
output = new byte[imgNumPixels];

size(200,150);
background(0);
}

void draw () {
loadPixels();

for(int i=0; i<imgWidth; i++) {
for(int j=0; j<imgHeight; j++) {
pixels[i*imgWidth + j] = color(input[i*imgWidth + j] & 0xFF);
}
}
updatePixels();

}

Re: loadBytes() vs. loadImage()
Reply #1 - Apr 9th, 2006, 11:10pm
 
loadBytes will take the compressed .jpg data in, which of course will throw an array index out of bounds error when you try to access, as it is not as large as indicated by imgNumPixels.

Insert this code to see the difference:


Code:

input = loadBytes("deneme.jpg"); // deneme.raw loads
println(input.length);
println(imgNumPixels);


LoadImage is decompressing the file to its correct size.

Re: loadBytes() vs. loadImage()
Reply #2 - Apr 9th, 2006, 11:57pm
 
that makes sense! yes you are right, they are outputting different numbers, so my question is: what should i do in order to load an jpeg through loadBytes() ?

ty
Re: loadBytes() vs. loadImage()
Reply #3 - Apr 10th, 2006, 12:03am
 
Well aside from using loadImage, you'll need a .jpeg decoder

like this:

http://java.sun.com/j2se/1.5.0/docs/guide/2d/api-jpeg/com/sun/image/codec/jpeg/JPEGImageDecoder.html

Any reason for not using loadImage?
Re: loadBytes() vs. loadImage()
Reply #4 - Apr 10th, 2006, 12:08am
 
thanks for the headsup Mark!

nope not really, I just want to understand in and out that's all. I think I am almost there. I was trying to get the RGB sum of an image and I succeeded it. Here is my code:

Code:

PImage b;
void setup() {
b = loadImage("deneme.jpg");
int rsum = 0;
int gsum = 0;
int bsum = 0;

for (int i = 0; i <b.pixels.length; i++) {

byte redk = (byte) ((b.pixels[i] & 0xFF0000) >> 16);
// println("redk: " + redk);
byte greenk = (byte) ((b.pixels[i] & 0x00FF00) >> 8);
// println("greenk: " + greenk);
byte bluek = (byte) ( b.pixels[i] & 0x0000FF);
// println("bluek: " + bluek);

rsum += redk;
gsum += greenk;
bsum += bluek;
}
println(rsum/b.pixels.length);
println(gsum/b.pixels.length);
println(bsum/b.pixels.length);
size(200,150);
image(b,0,0);
//background(0);
}




now I am trying to convert this into HSB values, can you slap me in the right direction?


best
ilteris
Re: loadBytes() vs. loadImage()
Reply #5 - Apr 10th, 2006, 11:55am
 
Try this, just hacked from various sources, so forgive any nastiness!

You'll need an image to work from, or you can generate a spectrum as you please.

This demonstrates:
the rgb (range 256)
our hsb (range 360.0, 100.0, 100.0) and java.awt.color.RGBtoHSB (range 1.0)

You can see the minor imperfections in the maths.

Code:


PImage rgb;

void setup() {
size(320,200);

rgb = loadImage("spectrum.jpg");
image(rgb,0,0,320,200);
}

void draw () {


}

void mousePressed () {

colorMode(RGB, 255);
loadPixels();
int col = pixels[mouseY*width+mouseX];
int r = (col & 0xff0000) >> 16;
int g = (col & 0x00ff00) >> 8;
int b = (col & 0x0000ff);

fill (r,g,b);
rect (20, height/2, 20, 20);
println("RGB- " + "r: " + r + " g: " + g + " b: " + b);

colorMode(HSB, 360, 100, 100);
float[] hsb = RGBtoHSB (r,g,b);
fill (hsb[0], hsb[1], hsb[2]);
rect (60, height/2, 20, 20);
println ("myHSB- " + "h: " + hsb[0] + " s: " + hsb[1] + " b: " + hsb[2]);

colorMode(HSB,1.0);
float[] jav = javaRGBtoHSB(r, g, b);
fill (jav[0], jav[1], jav[2]);
rect (100, height/2, 20, 20);
println ("JavaHSB- " + "h: " + jav[0] + " s: " + jav[1] + " b: " + jav[2]);
}

public float[] javaRGBtoHSB(int R, int G, int B) {
float[] hsb = Color.RGBtoHSB(R, G, B, null);
return hsb;
}

float[] RGBtoHSB (int r, int g, int b) {

float[] hsb = new float[3];

hsb[2] = max(max(r,g),b);
float mymin = min(min(r,g),b);
hsb[1] = (hsb[2] <= 0) ? 0 : round(100*(hsb[2] - mymin)/hsb[2]);
hsb[2] = round((hsb[2] /255)*100);
hsb[0] = 0;

if((r == g) && (g == b)){
hsb[0] = 0;
}else if(r >= g && g >= b){
hsb[0] = 60*(g-b)/(r-b);
}else if(g >= r && r >= b){
hsb[0] = 60 + 60*(g-r)/(g-b);
}else if(g >= b && b >= r){
hsb[0] = 120 + 60*(b-r)/(g-r);
}else if(b >= g && g >= r){
hsb[0] = 180 + 60*(b-g)/(b-r);
}else if(b >= r && r >= g){
hsb[0] = 240 + 60*(r-g)/(b-g);
}else if(r >= b && b >= g){
hsb[0] = 300 + 60*(r-b)/(r-g);
}else{
hsb[0] = 0;
}
hsb[0] = Math.round(hsb[0]);
return hsb;
}


Page Index Toggle Pages: 1