lunes, 30 de julio de 2012

Acceder a una imagen pixel por pixel.


Para cargar la imagen y comprobar el color de cada pixel he usado un pixmap. Hay que tener cuidado 
con el formato de la imagen ya que si es RGB8888 tenemos tres bytes por pixel y si es RGBA8888 hay cuatro.


Este es el codigo:

public static void loadTexture (String levelfile)
{
try
{
Pixmap pixmap = new Pixmap(Gdx.files.internal(levelfile));

altura = pixmap.getHeight();
anchura = pixmap.getWidth();
tilefactory = new TileFactory();
tiles = new Tile[anchura][altura];

java.nio.ByteBuffer bb =  ByteBuffer.allocateDirect(altura * anchura * 3);
Pixmap.Format format = pixmap.getFormat();
  bb = pixmap.getPixels();

for (int i = 0; i < altura * anchura; i++)
{
int  r = (bb.get() & 0xff) / 255;
int  g = (bb.get() & 0xff) / 255;
int  b = (bb.get() & 0xff) / 255;
//bb.get();
if (format == Pixmap.Format.RGBA8888)
{
int  a = (bb.get() & 0xff) / 255;
}
if ((r != 0 && g != 0 && b != 0)); // es negro puro
procesaSegunColor(r,  g,  b, i, anchura, altura);
}
pixmap.dispose();
}
catch(Exception ex)
{
Gdx.app.log("error", ex.toString());
}
}

Ejemplo de nivel:


No hay comentarios:

Publicar un comentario