Example usage for com.badlogic.gdx.graphics Texture getTextureData

List of usage examples for com.badlogic.gdx.graphics Texture getTextureData

Introduction

In this page you can find the example usage for com.badlogic.gdx.graphics Texture getTextureData.

Prototype

public TextureData getTextureData() 

Source Link

Usage

From source file:com.kotcrab.vis.editor.ui.dialog.PolygonAutoTraceDialog.java

License:Apache License

private void tracePolygon() {
    TextureRegion region = textureCacheModule.getRegion(assetDescriptor);
    int width = region.getRegionWidth();
    int height = region.getRegionHeight();
    int[] pixelArray = new int[width * height];

    Texture texture = region.getTexture();
    if (texture.getTextureData().isPrepared() == false) {
        texture.getTextureData().prepare();
    }/*w  w w  .j  ava2  s.  c o  m*/

    Pixmap pixmap = texture.getTextureData().consumePixmap();

    for (int x = 0; x < region.getRegionWidth(); x++) {
        for (int y = 0; y < region.getRegionHeight(); y++) {
            pixelArray[x + y * width] = pixmap.getPixel(region.getRegionX() + x, region.getRegionY() + y);
        }
    }

    Vector2[][] vertices = Tracer.trace(pixelArray, width, height, hullTolerance.getValue(),
            (int) alphaTolerance.getValue());

    if (vertices == null || vertices.length == 0) {
        Dialogs.showErrorDialog(stage, "Auto tracer could not create polygon, please create points manually.");
        Log.warn(PolygonTool.TAG, "Failed to auto trace polygon for asset descriptor: " + assetDescriptor);
        fadeOut();
        return;
    }

    if (vertices.length > 1) {
        Log.warn(PolygonTool.TAG,
                "Auto tracer found multiple parts, will be discarded. Asset descriptor: " + assetDescriptor);
    }

    resultListener.finished(vertices[0]);
}

From source file:com.o2d.pkayjava.editor.utils.poly.TextureUtils.java

License:Apache License

public static Pixmap getPOTPixmap(Texture texture) {
    if (texture == null)
        return null;
    texture.getTextureData().prepare();
    Pixmap pixmap = texture.getTextureData().consumePixmap();
    int origW = pixmap.getWidth();
    int origH = pixmap.getHeight();
    int w = getNearestPOT(origW);
    int h = getNearestPOT(origH);
    int len = Math.max(w, h);

    Pixmap potPixmap = new Pixmap(len, len, pixmap.getFormat());
    potPixmap.drawPixmap(pixmap, 0, 0, 0, 0, origW, origH);
    pixmap.dispose();/*from w w w . j  ava  2  s  .c  om*/

    return potPixmap;
}

From source file:com.o2d.pkayjava.editor.utils.poly.TextureUtils.java

License:Apache License

public static TextureRegion getPOTTexture(Texture texture) {
    if (texture == null)
        return null;

    texture.getTextureData().prepare();
    Pixmap pixmap = texture.getTextureData().consumePixmap();
    int origW = pixmap.getWidth();
    int origH = pixmap.getHeight();
    int w = getNearestPOT(origW);
    int h = getNearestPOT(origH);
    int len = Math.max(w, h);

    Pixmap potPixmap = new Pixmap(len, len, pixmap.getFormat());
    potPixmap.drawPixmap(pixmap, 0, 0, 0, 0, origW, origH);
    pixmap.dispose();/*ww  w. java2  s . co m*/

    Texture otherTexture = new Texture(potPixmap);
    otherTexture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);

    return new TextureRegion(otherTexture, 0, 0, origW, origH);
}

From source file:com.ray3k.skincomposer.data.AtlasData.java

License:Open Source License

public void readAtlas(FileHandle fileHandle) throws Exception {
    if (fileHandle.exists()) {
        FileHandle saveFile = main.getProjectData().getSaveFile();
        FileHandle targetDirectory;//ww  w  .  j  a  va  2s. com
        if (saveFile != null) {
            targetDirectory = saveFile.sibling(saveFile.nameWithoutExtension() + "_data/");
        } else {
            targetDirectory = Gdx.files.local("temp/" + main.getProjectData().getId() + "_data/");
        }

        targetDirectory.mkdirs();

        TextureAtlas atlas = new TextureAtlas(fileHandle);
        Array<AtlasRegion> regions = atlas.getRegions();

        for (AtlasRegion region : regions) {
            Texture texture = region.getTexture();
            if (!texture.getTextureData().isPrepared()) {
                texture.getTextureData().prepare();
            }
            Pixmap.setBlending(Pixmap.Blending.None);
            Pixmap pixmap = texture.getTextureData().consumePixmap();
            Pixmap savePixmap;
            String name;

            if (region.splits == null && region.pads == null) {
                name = region.name + ".png";
                savePixmap = new Pixmap(region.getRegionWidth(), region.getRegionHeight(),
                        Pixmap.Format.RGBA8888);
                for (int x = 0; x < region.getRegionWidth(); x++) {
                    for (int y = 0; y < region.getRegionHeight(); y++) {
                        int colorInt = pixmap.getPixel(region.getRegionX() + x, region.getRegionY() + y);
                        savePixmap.drawPixel(x, y, colorInt);
                    }
                }
            } else {
                name = region.name + ".9.png";
                savePixmap = new Pixmap(region.getRegionWidth() + 2, region.getRegionHeight() + 2,
                        pixmap.getFormat());
                int x;
                int y;

                //draw 9 patch lines
                savePixmap.setColor(Color.BLACK);

                if (region.splits != null) {
                    x = 0;
                    for (y = region.splits[2] + 1; y < savePixmap.getHeight() - region.splits[3] - 1; y++) {
                        savePixmap.drawPixel(x, y);
                    }

                    y = 0;
                    for (x = region.splits[0] + 1; x < savePixmap.getWidth() - region.splits[1] - 1; x++) {
                        savePixmap.drawPixel(x, y);
                    }
                }

                if (region.pads != null) {
                    x = savePixmap.getWidth() - 1;
                    for (y = region.pads[2] + 1; y < savePixmap.getHeight() - region.pads[3] - 1; y++) {
                        savePixmap.drawPixel(x, y);
                    }

                    y = savePixmap.getHeight() - 1;
                    for (x = region.pads[0] + 1; x < savePixmap.getWidth() - region.pads[1] - 1; x++) {
                        savePixmap.drawPixel(x, y);
                    }
                }

                for (x = 0; x < region.getRegionWidth(); x++) {
                    for (y = 0; y < region.getRegionHeight(); y++) {
                        int colorInt = pixmap.getPixel(region.getRegionX() + x, region.getRegionY() + y);
                        savePixmap.drawPixel(x + 1, y + 1, colorInt);
                    }
                }
            }
            FileHandle outputFile = targetDirectory.child(name);
            PixmapIO.writePNG(outputFile, savePixmap);
            DrawableData drawable = new DrawableData(outputFile);

            //delete drawables with the same name
            for (DrawableData originalData : new Array<>(main.getProjectData().getAtlasData().getDrawables())) {
                if (originalData.name.equals(drawable.name)) {
                    main.getProjectData().getAtlasData().getDrawables().removeValue(originalData, true);
                }
            }

            drawables.add(drawable);
        }

    } else {
        throw new FileNotFoundException();
    }
}

From source file:ru.electronikas.dogexpert.trainer.ImageNeuralNetworkProcessor.java

License:Apache License

private Pixmap getPixmapFromImageFile(String path) {
    Texture texture = new Texture(new FileHandle(path));
    if (!texture.getTextureData().isPrepared()) {
        texture.getTextureData().prepare();
    }//from w  w  w.j ava  2  s  .c o  m
    Pixmap pixmp = texture.getTextureData().consumePixmap();
    texture.dispose();
    texture = null;
    return pixmp;
}