Search code examples
javaimagelibgdxtextures

Libgdx get Texture from Image or Drawable


I would like to get the Texture data out of an image, so it may be rendered directly, but I see no straightforward way of making it, google searches show the other way around only.

I am able to get the Drawable, but the interface doesn't specify the exact texture data, so there's no way that I see to convert an Image actor into a Texture.

What I am trying to achieve is to have brushes, where the data of the brushes are stored in the image of an ImageButton. So upon clicking on an ImageButton The user would be able to draw on the screen based on the image stored in the buttons.

How might I be able to do that?


Solution

  • After digging deeper, I found a solution on the forums:

    Your best bet is probably to draw your textures to a FrameBuffer with a 1:1 scale, then you can use

    ScreenUtils.getFrameBufferPixmap

    To copy a pixmap off the FrameBuffer and resize as you wish.

    Something like this:

        fb.begin();
    
       
        sb.begin();
        sb.draw(texture, 0, 0);
        sb.end();
    
        Pixmap pm = ScreenUtils.getFrameBufferPixmap(0, 0, width, height);
    
        fb.end();
    

    It's not exactly a texture, but it is exactly what I would have needed: the pixel data extracted from a drawable.

    After much thought I decided that it is easier to just store the texture data redundantly next to to the Imagebuttons, and query directly the stored data, instead of using the ImageButton as a kind of container, which it shouldn't be.