Search in sources :

Example 1 with AWTLoader

use of com.jme3.texture.plugins.AWTLoader in project jmonkeyengine by jMonkeyEngine.

the class MipMapGenerator method resizeToPowerOf2.

public static void resizeToPowerOf2(Image image) {
    BufferedImage original = ImageToAwt.convert(image, false, true, 0);
    int potWidth = FastMath.nearestPowerOfTwo(image.getWidth());
    int potHeight = FastMath.nearestPowerOfTwo(image.getHeight());
    int potSize = Math.max(potWidth, potHeight);
    BufferedImage scaled = scaleDown(original, potSize, potSize);
    AWTLoader loader = new AWTLoader();
    Image output = loader.load(scaled, false);
    image.setWidth(potSize);
    image.setHeight(potSize);
    image.setDepth(0);
    image.setData(output.getData(0));
    image.setFormat(output.getFormat());
    image.setMipMapSizes(null);
}
Also used : AWTLoader(com.jme3.texture.plugins.AWTLoader) BufferedImage(java.awt.image.BufferedImage) Image(com.jme3.texture.Image) BufferedImage(java.awt.image.BufferedImage)

Example 2 with AWTLoader

use of com.jme3.texture.plugins.AWTLoader in project jmonkeyengine by jMonkeyEngine.

the class MipMapGenerator method generateMipMaps.

public static void generateMipMaps(Image image) {
    BufferedImage original = ImageToAwt.convert(image, false, true, 0);
    int width = original.getWidth();
    int height = original.getHeight();
    int level = 0;
    BufferedImage current = original;
    AWTLoader loader = new AWTLoader();
    ArrayList<ByteBuffer> output = new ArrayList<ByteBuffer>();
    int totalSize = 0;
    Format format = null;
    while (height >= 1 || width >= 1) {
        Image converted = loader.load(current, false);
        format = converted.getFormat();
        output.add(converted.getData(0));
        totalSize += converted.getData(0).capacity();
        if (height == 1 || width == 1) {
            break;
        }
        level++;
        height /= 2;
        width /= 2;
        current = scaleDown(current, width, height);
    }
    ByteBuffer combinedData = BufferUtils.createByteBuffer(totalSize);
    int[] mipSizes = new int[output.size()];
    for (int i = 0; i < output.size(); i++) {
        ByteBuffer data = output.get(i);
        data.clear();
        combinedData.put(data);
        mipSizes[i] = data.capacity();
    }
    combinedData.flip();
    // insert mip data into image
    image.setData(0, combinedData);
    image.setMipMapSizes(mipSizes);
    image.setFormat(format);
}
Also used : Format(com.jme3.texture.Image.Format) ArrayList(java.util.ArrayList) AWTLoader(com.jme3.texture.plugins.AWTLoader) BufferedImage(java.awt.image.BufferedImage) Image(com.jme3.texture.Image) ByteBuffer(java.nio.ByteBuffer) BufferedImage(java.awt.image.BufferedImage)

Aggregations

Image (com.jme3.texture.Image)2 AWTLoader (com.jme3.texture.plugins.AWTLoader)2 BufferedImage (java.awt.image.BufferedImage)2 Format (com.jme3.texture.Image.Format)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1