Read Pixel from a texture

hello @ all

i got a new question and hunting for the solution :slight_smile:

i load textures via jogl

texture[lo] = TextureIO.newTexture(new File(“gfx/d00”+lo1+lo2+".JPG"), true);

and one of this textures is a heightmap another is my map…
my goal is to read the pixels from this texture for something like this:


for (int ii=0;ii<50;ii++)
{
for (int ii=0;ii<50;ii++)
{
int red= readpixel(i,ii)...
int blue...
int green....
...
...calc heightmap...

}
}

how to access the pixels from texture
and read the color values of red,blue and green?

thx for all answers :slight_smile:

If you use ImageIO for the heightmap instead of TextureIO, you can use the following:


            BufferedImage image = ImageIO.read( resource );
            int xDim = image.getWidth( null );
            int zDim = image.getHeight( null );
            PixelGrabber grabber = new PixelGrabber( image, 0, 0, xDim, zDim, true );
            try
            {
                grabber.grabPixels( 0 );
            }
            catch( InterruptedException e )
            {
                e.printStackTrace();
            }
            int[] source = (int[]) grabber.getPixels(); // source now contains all the pixels. one int per pixel

much thanks

bu…what did i need to include(import) to use the ImageIO commands in your example?


import javax.imageio.ImageIO;
import java.net.URL;
import java.io.IOException;
import java.io.InputStream;
import java.awt.image.BufferedImage;
import java.awt.image.PixelGrabber;

Do you use an IDE? Pretty much every decent IDE supports automatic resolving of imports.

thank you both :slight_smile: it works perfect

[quote]Do you use an IDE? Pretty much every decent IDE supports automatic resolving of imports.
[/quote]
i dont know what an IDE is,
i use eclipse and all stuff i need to basically run jogl

Eclipse is and IDE (I think it stands for Integrated Development Environment). In eclipse, if you’re missing an import, their will be a red underline under the variable (or nearby can’t remember), clicking on the red box on the side of the text screen (same line as the error) should bring up possible error solutions (which should include the option to import everything).

I think you don’t have to use the PixelGrabber. Just call int[] getRGB(…) on the BufferedImage created by ImageIO.

Greetz
Klemens

the pixelgrabber works perfect for my project and
its fast (i dont need more)…