Fast Contrast & Brightness

Hi there

I'm been trying to create an image contrast and brightness class

The thing is I would like to change the contrast Brightness with two seperated scrollbars

But I can not make it work fast enough

It hacks and you have to wait for a long time

whats wrong

should I use several threads or what

Here is my code that the scrollbar is calling

Contrast

publicvoid contrast2()

{

pixel =newint[imw*imh];

try

{

PixelGrabber pg =new PixelGrabber(im,0,0,imw,imh,pixel,0,imw);

pg.grabPixels();

}

catch(InterruptedException e)

{

e.printStackTrace();

}

int r;

int g;

int b;

int offset;

int i =0;

for(int x = 0; x < imw;x++)

{

for(int y = 0; y < imh; y++)

{

i = x+y*imw;

r= (pixel[i]>>16)&0xff;

g= (pixel[i]>>8)&0xff;

b= (pixel[i])&0xff;

r=(int)(r*contrast);

g=(int)(g*contrast);

b=(int)(b*contrast);

if(r > 255) r = 255;

if(g > 255) g = 255;

if(b > 255) b = 255;

pixel[i] = (pixel[i] & 0xff000000)|(r<<16) | (g<<8) | b;

}

}

ImageProducer ip =new MemoryImageSource(imw,imh,pixel,0,imw);

temp = toolkit.createImage(ip);

repaint();

}

Brightness

publicvoid brightness2()

{

pixel =newint[imw*imh];

try

{

PixelGrabber pg =new PixelGrabber(im,0,0,imw,imh,pixel,0,imw);

pg.grabPixels();

}

catch(InterruptedException e)

{

e.printStackTrace();

}

int r;

int g;

int b;

int offset;

int i =0;

for(int x = 0; x < imw;x++)

{

for(int y = 0; y < imh; y++)

{

i = x+y*imw;

r= (pixel[i]>>16)&0xff;

g= (pixel[i]>>8)&0xff;

b= (pixel[i])&0xff;

r=r+brightness;

g=g+brightness;

b=b+brightness;

if(r > 255) r = 255;

if(r < 0) r = 0;

if(g > 255) g = 255;

if(g < 0) g = 0;

if(b > 255) b = 255;

if(b < 0) b = 0;

pixel[i] = (pixel[i] & 0xff000000)|(r<<16) | (g<<8) | b;

}

}

ImageProducer ip =new MemoryImageSource(imw,imh,pixel,0,imw);

temp = toolkit.createImage(ip);

repaint();

}

Hm sun has to change the format renderer I see that the arrays look a litle suspicous but I think you understand any whay

Markus

[4649 byte] By [markusbengtsson2] at [2007-9-26 6:32:52]
# 1

Your spped problem can be solved with several simple optimizations. Your main problem is not your algorithm, but you are spewing memory everywhere without need.

When you load an image create two int[][] arrays and a memory image source of correct dimensions. Then load one int[][] array with the pixels from the image. Do this ONCE per image, not once per pass. Save the variables in your frame object.

For each pass all you need to do is load the second int[][] array with the modified pixels and use the setPixels of memory imge source and display the memoryimagesource.

Each of your functions are spewing expensive PixelGrabbers and large memory allocations both of which slow your app down.

Once you have that down the next optimization is to roll you two operations together to help cache coherancy. The other optimitation is to create your MemoryImageSource with a color model DirectColorModel without an alpha channel.

If you are still having trouble you could also try posting to the javagmaing bboard as many people there are deep into graphics and java. http://www.javagaming.org/

ericew at 2007-7-1 15:43:52 > top of Java-index,Security,Cryptography...
# 2
Hi thereWell I done as you say but the performance is not fast enough (it's better but not good)I have posted the issue on the java gaming site so we will see if they can come up with something.Thanks for your ideasMarkus
markusbengtsson2 at 2007-7-1 15:43:52 > top of Java-index,Security,Cryptography...