What is the best way to get the pixel brightness

I have a gray-scale .png image. The pixel brightness is used to communicate the information that i need in my application. Note that i do not show this image in my application, the png file is only used to communicate data. Since this data is being collected as an image at the source we are using a png file to pass around this data. The code is given below

JarEntry entry = m_jarFile.getJarEntry("imageFile.png");

InputStream stream = m_jarFile.getInputStream(entry);

BufferedImage image = ImageIO.read(overlayImageFileStream);

int cols = overlayImage.getWidth();

int rows = overlayImage.getHeight();

short[][] m_pixelBrightnessValues =newshort[cols][rows];

for (int i=0; i<cols; i++){

for (int j=0; j><rows; j++){

/** BEGIN: Takes too much time */

int rgb = overlayImage.getRGB(i, j);

// convert to HSB values.

int red = (rgb>>16)&0xFF;

int green = (rgb>>8)&0xFF;

int blue = rgb&0xFF;

float[] inputHSB =newfloat[3];

inputHSB = Color.RGBtoHSB(red, green, blue, inputHSB);

/** END: Takes too much time */

float brightness = inputHSB[2];

m_pixelBrightnessValues[i][j]= (short)Math.round(brightness * MAX_BRIGHTNESS_VALUE);

}

}

Some portion of the code above takes too much time. I have been tinkering with the following approaches:

1). I can parse the png file myself and extract the brightness information thus bypassing the creation of the BufferedImage altogether.

2). I can continue to create BufferedImage but find out a way to grab brightness directly from the buffered image.

I prefer option 2, since option 1 will require that I figure out png format and then write custom png parsing logic. I have explored all the methods of BufferedImage and it seems there is no way of getting the HSB directly, the only thing readily availabe is RGB values.

I would really appreciate any pointers (e.g. is there something in the Raster that can let me access the brightness data directly ?)

-kashif

[2906 byte] By [manzoor2a] at [2007-10-2 21:33:48]
# 1

> I have a gray-scale .png image. The pixel brightness

> is used to communicate the information that i need in

> my application. Note that i do not show this image in

> my application, the png file is only used to

> communicate data. Since this data is being collected

> as an image at the source we are using a png file to

> pass around this data. The code is given below

<snip>

>

> Some portion of the code above takes too much time. I

> have been tinkering with the following approaches:

>

> 1). I can parse the png file myself and extract the

> brightness information thus bypassing the creation of

> the BufferedImage altogether.

>

> 2). I can continue to create BufferedImage but find

> out a way to grab brightness directly from the

> buffered image.

>

> I prefer option 2, since option 1 will require that I

> figure out png format and then write custom png

> parsing logic. I have explored all the methods of

> BufferedImage and it seems there is no way of getting

> the HSB directly, the only thing readily availabe is

> RGB values.

>

> I would really appreciate any pointers (e.g. is there

> something in the Raster that can let me access the

> brightness data directly ?)

For a greyscale image, the R G abd B values are all the same, and the brightess is simply that value divided by the maximum value. You could create a brightness map in advance which is simply a 256 element array that maps an RGB value to a brightness:

short[] brightnessMap = new short[256];

for (int i = 0; i < 256; i++) {

brightnessMap[i] = (short)(i/255.0f * MAX_BRIGHTNESS_VALUE);

}

Various experiments I performed using PixelGrabber, Raster and such didn't seem to speed things up more than 25% on a 1280 x 1024 image. Times went from around 400 milliseconds to 300.

Niceguy1a at 2007-7-14 0:47:33 > top of Java-index,Security,Cryptography...