Java Code Examples for com.jme3.texture.Texture2D#getImage()

The following examples show how to use com.jme3.texture.Texture2D#getImage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CombinedTexture.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * This method merges two given textures. The result is stored in the
 * 'target' texture.
 * 
 * @param target
 *            the target texture
 * @param source
 *            the source texture
 */
private void merge(Texture2D target, Texture2D source) {
    if (target.getImage().getDepth() != source.getImage().getDepth()) {
        throw new IllegalArgumentException("Cannot merge images with different depths!");
    }
    Image sourceImage = source.getImage();
    Image targetImage = target.getImage();
    PixelInputOutput sourceIO = PixelIOFactory.getPixelIO(sourceImage.getFormat());
    PixelInputOutput targetIO = PixelIOFactory.getPixelIO(targetImage.getFormat());
    TexturePixel sourcePixel = new TexturePixel();
    TexturePixel targetPixel = new TexturePixel();
    int depth = target.getImage().getDepth() == 0 ? 1 : target.getImage().getDepth();

    for (int layerIndex = 0; layerIndex < depth; ++layerIndex) {
        for (int x = 0; x < sourceImage.getWidth(); ++x) {
            for (int y = 0; y < sourceImage.getHeight(); ++y) {
                sourceIO.read(sourceImage, layerIndex, sourcePixel, x, y);
                targetIO.read(targetImage, layerIndex, targetPixel, x, y);
                targetPixel.merge(sourcePixel);
                targetIO.write(targetImage, layerIndex, targetPixel, x, y);
            }
        }
    }
}
 
Example 2
Source File: CombinedTexture.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * This method scales the given texture to the given size.
 * 
 * @param texture
 *            the texture to be scaled
 * @param width
 *            new width of the texture
 * @param height
 *            new height of the texture
 */
private void scale(Texture2D texture, int width, int height) {
    // first determine if scaling is required
    boolean scaleRequired = texture.getImage().getWidth() != width || texture.getImage().getHeight() != height;

    if (scaleRequired) {
        Image image = texture.getImage();
        BufferedImage sourceImage = ImageToAwt.convert(image, false, true, 0);

        int sourceWidth = sourceImage.getWidth();
        int sourceHeight = sourceImage.getHeight();

        BufferedImage targetImage = new BufferedImage(width, height, sourceImage.getType());

        Graphics2D g = targetImage.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.drawImage(sourceImage, 0, 0, width, height, 0, 0, sourceWidth, sourceHeight, null);
        g.dispose();

        Image output = new ImageLoader().load(targetImage, false);
        image.setWidth(width);
        image.setHeight(height);
        image.setData(output.getData(0));
        image.setFormat(output.getFormat());
    }
}
 
Example 3
Source File: RenderImageJme.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public RenderImageJme(String filename, boolean linear, NiftyJmeDisplay display){
    TextureKey key = new TextureKey(filename, true);

    key.setAnisotropy(0);
    key.setGenerateMips(false);

    texture = (Texture2D) display.getAssetManager().loadTexture(key);
    texture.setMagFilter(linear ? MagFilter.Bilinear : MagFilter.Nearest);
    texture.setMinFilter(linear ? MinFilter.BilinearNoMipMaps : MinFilter.NearestNoMipMaps);
    image = texture.getImage();

    width = image.getWidth();
    height = image.getHeight();
}
 
Example 4
Source File: RenderImageJme.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public RenderImageJme(Texture2D texture){
    if (texture.getImage() == null) {
        throw new IllegalArgumentException("texture.getImage() cannot be null");
    }

    this.texture = texture;
    this.image = texture.getImage();
    width = image.getWidth();
    height = image.getHeight();
}
 
Example 5
Source File: JmeBatchRenderBackend.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Image loadImage(final String filename) {
    TextureKey key = new TextureKey(filename, false);
    key.setAnisotropy(0);
    key.setGenerateMips(false);

    Texture2D texture = (Texture2D) display.getAssetManager().loadTexture(key);
    // Fix GLES format incompatibility issue with glTexSubImage
    Renderer renderer = display.getRenderer();
    if (renderer == null || renderer.getCaps().contains(Caps.OpenGLES20)) {
        if (texture.getImage().getFormat() != Format.RGBA8) {
            com.jme3.texture.Image sourceImage = texture.getImage();
            int size = sourceImage.getWidth() * sourceImage.getHeight() * 4;
            ByteBuffer buffer = BufferUtils.createByteBuffer(size);
            com.jme3.texture.Image rgba8Image = new com.jme3.texture.Image(Format.RGBA8,
                    sourceImage.getWidth(),
                    sourceImage.getHeight(),
                    buffer,
                    sourceImage.getColorSpace());

            ImageRaster input = ImageRaster.create(sourceImage, 0, 0, false);
            ImageRaster output = ImageRaster.create(rgba8Image, 0, 0, false);
            ColorRGBA color = new ColorRGBA();

            for (int y = 0; y < sourceImage.getHeight(); y++) {
                for (int x = 0; x < sourceImage.getWidth(); x++) {
                    output.setPixel(x, y, input.getPixel(x, y, color));
                }
            }
            return new ImageImpl(rgba8Image);
        }
    }
    return new ImageImpl(texture.getImage());
}
 
Example 6
Source File: RenderImageJme.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public RenderImageJme(String filename, boolean linear, NiftyJmeDisplay display){
    TextureKey key = new TextureKey(filename, true);

    key.setAnisotropy(0);
    key.setAsCube(false);
    key.setGenerateMips(false);
    
    texture = (Texture2D) display.getAssetManager().loadTexture(key);
    texture.setMagFilter(linear ? MagFilter.Bilinear : MagFilter.Nearest);
    texture.setMinFilter(linear ? MinFilter.BilinearNoMipMaps : MinFilter.NearestNoMipMaps);
    image = texture.getImage();

    width = image.getWidth();
    height = image.getHeight();
}
 
Example 7
Source File: RenderImageJme.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public RenderImageJme(Texture2D texture){
    if (texture.getImage() == null)
        throw new IllegalArgumentException("texture.getImage() cannot be null");
    
    this.texture = texture;
    this.image = texture.getImage();
    width = image.getWidth();
    height = image.getHeight();
}