Search in sources :

Example 6 with PixelInputOutput

use of com.jme3.scene.plugins.blender.textures.io.PixelInputOutput in project jmonkeyengine by jMonkeyEngine.

the class TextureBlenderDDS method blend.

@Override
public Image blend(Image image, Image baseImage, BlenderContext blenderContext) {
    this.prepareImagesForBlending(image, baseImage);
    Format format = image.getFormat();
    int width = image.getWidth();
    int height = image.getHeight();
    int depth = image.getDepth();
    if (depth == 0) {
        depth = 1;
    }
    ArrayList<ByteBuffer> dataArray = new ArrayList<ByteBuffer>(depth);
    PixelInputOutput basePixelIO = null;
    float[][] compressedMaterialColor = null;
    TexturePixel[] baseTextureColors = null;
    if (baseImage != null) {
        basePixelIO = PixelIOFactory.getPixelIO(baseImage.getFormat());
        compressedMaterialColor = new float[2][4];
        baseTextureColors = new TexturePixel[] { new TexturePixel(), new TexturePixel() };
    }
    float[] resultPixel = new float[4];
    float[] pixelColor = new float[4];
    TexturePixel[] colors = new TexturePixel[] { new TexturePixel(), new TexturePixel() };
    int baseXTexelIndex = 0, baseYTexelIndex = 0;
    float[] alphas = new float[] { 1, 1 };
    for (int dataLayerIndex = 0; dataLayerIndex < depth; ++dataLayerIndex) {
        ByteBuffer data = image.getData(dataLayerIndex);
        data.rewind();
        ByteBuffer newData = BufferUtils.createByteBuffer(data.remaining());
        while (data.hasRemaining()) {
            if (format == Format.DXT3) {
                long alpha = data.getLong();
                // get alpha for first and last pixel that is compressed in the texel
                byte alpha0 = (byte) (alpha << 4 & 0xFF);
                byte alpha1 = (byte) (alpha >> 60 & 0xFF);
                alphas[0] = alpha0 >= 0 ? alpha0 / 255.0f : 1.0f - ~alpha0 / 255.0f;
                alphas[1] = alpha1 >= 0 ? alpha1 / 255.0f : 1.0f - ~alpha1 / 255.0f;
                newData.putLong(alpha);
            } else if (format == Format.DXT5) {
                byte alpha0 = data.get();
                byte alpha1 = data.get();
                alphas[0] = alpha0 >= 0 ? alpha0 / 255.0f : 1.0f - ~alpha0 / 255.0f;
                alphas[1] = alpha1 >= 0 ? alpha0 / 255.0f : 1.0f - ~alpha0 / 255.0f;
                newData.put(alpha0);
                newData.put(alpha1);
                // only read the next 6 bytes (these are alpha indexes)
                newData.putInt(data.getInt());
                newData.putShort(data.getShort());
            }
            int col0 = RGB565.RGB565_to_ARGB8(data.getShort());
            int col1 = RGB565.RGB565_to_ARGB8(data.getShort());
            colors[0].fromARGB8(col0);
            colors[1].fromARGB8(col1);
            // compressing 16 pixels from the base texture as if they belonged to a texel
            if (baseImage != null) {
                // reading pixels (first and last of the 16 colors array)
                // first pixel
                basePixelIO.read(baseImage, dataLayerIndex, baseTextureColors[0], baseXTexelIndex << 2, baseYTexelIndex << 2);
                // last pixel
                basePixelIO.read(baseImage, dataLayerIndex, baseTextureColors[1], baseXTexelIndex << 2 + 4, baseYTexelIndex << 2 + 4);
                baseTextureColors[0].toRGBA(compressedMaterialColor[0]);
                baseTextureColors[1].toRGBA(compressedMaterialColor[1]);
            }
            // blending colors
            for (int i = 0; i < colors.length; ++i) {
                if (negateTexture) {
                    colors[i].negate();
                }
                colors[i].toRGBA(pixelColor);
                pixelColor[3] = alphas[i];
                this.blendPixel(resultPixel, compressedMaterialColor != null ? compressedMaterialColor[i] : materialColor, pixelColor, blenderContext);
                colors[i].fromARGB(1, resultPixel[0], resultPixel[1], resultPixel[2]);
                int argb8 = colors[i].toARGB8();
                short rgb565 = RGB565.ARGB8_to_RGB565(argb8);
                newData.putShort(rgb565);
            }
            // just copy the remaining 4 bytes of the current texel
            newData.putInt(data.getInt());
            ++baseXTexelIndex;
            if (baseXTexelIndex > image.getWidth() >> 2) {
                baseXTexelIndex = 0;
                ++baseYTexelIndex;
            }
        }
        dataArray.add(newData);
    }
    Image result = dataArray.size() > 1 ? new Image(format, width, height, depth, dataArray, ColorSpace.Linear) : new Image(format, width, height, dataArray.get(0), ColorSpace.Linear);
    if (image.getMipMapSizes() != null) {
        result.setMipMapSizes(image.getMipMapSizes().clone());
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) Image(com.jme3.texture.Image) ByteBuffer(java.nio.ByteBuffer) Format(com.jme3.texture.Image.Format) PixelInputOutput(com.jme3.scene.plugins.blender.textures.io.PixelInputOutput) TexturePixel(com.jme3.scene.plugins.blender.textures.TexturePixel)

Example 7 with PixelInputOutput

use of com.jme3.scene.plugins.blender.textures.io.PixelInputOutput in project jmonkeyengine by jMonkeyEngine.

the class TextureBlenderLuminance method blend.

public Image blend(Image image, Image baseImage, BlenderContext blenderContext) {
    this.prepareImagesForBlending(image, baseImage);
    Format format = image.getFormat();
    PixelInputOutput basePixelIO = null;
    TexturePixel basePixel = null;
    float[] materialColor = this.materialColor;
    if (baseImage != null) {
        basePixelIO = PixelIOFactory.getPixelIO(baseImage.getFormat());
        materialColor = new float[this.materialColor.length];
        basePixel = new TexturePixel();
    }
    int width = image.getWidth();
    int height = image.getHeight();
    int depth = image.getDepth();
    if (depth == 0) {
        depth = 1;
    }
    ArrayList<ByteBuffer> dataArray = new ArrayList<ByteBuffer>(depth);
    float[] resultPixel = new float[4];
    float[] tinAndAlpha = new float[2];
    for (int dataLayerIndex = 0; dataLayerIndex < depth; ++dataLayerIndex) {
        ByteBuffer data = image.getData(dataLayerIndex);
        data.rewind();
        ByteBuffer newData = BufferUtils.createByteBuffer(data.limit() * 4);
        int dataIndex = 0, x = 0, y = 0;
        while (data.hasRemaining()) {
            // getting the proper material color if the base texture is applied
            if (basePixelIO != null) {
                basePixelIO.read(baseImage, dataLayerIndex, basePixel, x, y);
                basePixel.toRGBA(materialColor);
                ++x;
                if (x >= width) {
                    x = 0;
                    ++y;
                }
            }
            this.getTinAndAlpha(data, format, negateTexture, tinAndAlpha);
            this.blendPixel(resultPixel, materialColor, color, tinAndAlpha[0], blendFactor, blendType, blenderContext);
            newData.put(dataIndex++, (byte) (resultPixel[0] * 255.0f));
            newData.put(dataIndex++, (byte) (resultPixel[1] * 255.0f));
            newData.put(dataIndex++, (byte) (resultPixel[2] * 255.0f));
            newData.put(dataIndex++, (byte) (tinAndAlpha[1] * 255.0f));
        }
        dataArray.add(newData);
    }
    Image result = depth > 1 ? new Image(Format.RGBA8, width, height, depth, dataArray, ColorSpace.Linear) : new Image(Format.RGBA8, width, height, dataArray.get(0), ColorSpace.Linear);
    if (image.getMipMapSizes() != null) {
        result.setMipMapSizes(image.getMipMapSizes().clone());
    }
    return result;
}
Also used : Format(com.jme3.texture.Image.Format) PixelInputOutput(com.jme3.scene.plugins.blender.textures.io.PixelInputOutput) ArrayList(java.util.ArrayList) Image(com.jme3.texture.Image) TexturePixel(com.jme3.scene.plugins.blender.textures.TexturePixel) ByteBuffer(java.nio.ByteBuffer)

Example 8 with PixelInputOutput

use of com.jme3.scene.plugins.blender.textures.io.PixelInputOutput in project jmonkeyengine by jMonkeyEngine.

the class LandscapeHelper method toSky.

/**
     * Loads scene's sky. Sky can be plain or textured.
     * If no sky type is selected in blender then no sky is loaded.
     * @param worldStructure
     *            the world's structure
     * @return the scene's sky
     * @throws BlenderFileException
     *             blender exception is thrown when problems with blender file occur
     */
public Spatial toSky(Structure worldStructure) throws BlenderFileException {
    int skytype = ((Number) worldStructure.getFieldValue("skytype")).intValue();
    if (skytype == 0) {
        return null;
    }
    LOGGER.fine("Loading sky.");
    ColorRGBA horizontalColor = this.toBackgroundColor(worldStructure);
    float zenr = ((Number) worldStructure.getFieldValue("zenr")).floatValue();
    float zeng = ((Number) worldStructure.getFieldValue("zeng")).floatValue();
    float zenb = ((Number) worldStructure.getFieldValue("zenb")).floatValue();
    ColorRGBA zenithColor = new ColorRGBA(zenr, zeng, zenb, 1);
    // jutr for this case load generated textures wheather user had set it or not because those might be needed to properly load the sky
    boolean loadGeneratedTextures = blenderContext.getBlenderKey().isLoadGeneratedTextures();
    blenderContext.getBlenderKey().setLoadGeneratedTextures(true);
    TextureHelper textureHelper = blenderContext.getHelper(TextureHelper.class);
    List<CombinedTexture> loadedTextures = null;
    try {
        loadedTextures = textureHelper.readTextureData(worldStructure, new float[] { horizontalColor.r, horizontalColor.g, horizontalColor.b, horizontalColor.a }, true);
    } finally {
        blenderContext.getBlenderKey().setLoadGeneratedTextures(loadGeneratedTextures);
    }
    TextureCubeMap texture = null;
    if (loadedTextures != null && loadedTextures.size() > 0) {
        if (loadedTextures.size() > 1) {
            throw new IllegalStateException("There should be only one combined texture for sky!");
        }
        CombinedTexture combinedTexture = loadedTextures.get(0);
        texture = combinedTexture.generateSkyTexture(horizontalColor, zenithColor, blenderContext);
    } else {
        LOGGER.fine("Preparing colors for colorband.");
        int colorbandType = ColorBand.IPO_CARDINAL;
        List<ColorRGBA> colorbandColors = new ArrayList<ColorRGBA>(3);
        colorbandColors.add(horizontalColor);
        if ((skytype & SKYTYPE_BLEND) != 0) {
            if ((skytype & SKYTYPE_PAPER) != 0) {
                colorbandType = ColorBand.IPO_LINEAR;
            }
            if ((skytype & SKYTYPE_REAL) != 0) {
                colorbandColors.add(0, zenithColor);
            }
            colorbandColors.add(zenithColor);
        }
        int size = blenderContext.getBlenderKey().getSkyGeneratedTextureSize();
        List<Integer> positions = new ArrayList<Integer>(colorbandColors.size());
        positions.add(0);
        if (colorbandColors.size() == 2) {
            positions.add(size - 1);
        } else if (colorbandColors.size() == 3) {
            positions.add(size / 2);
            positions.add(size - 1);
        }
        LOGGER.fine("Generating sky texture.");
        float[][] values = new ColorBand(colorbandType, colorbandColors, positions, size).computeValues();
        Image image = ImageUtils.createEmptyImage(Format.RGB8, size, size, 6);
        PixelInputOutput pixelIO = PixelIOFactory.getPixelIO(image.getFormat());
        TexturePixel pixel = new TexturePixel();
        LOGGER.fine("Creating side textures.");
        int[] sideImagesIndexes = new int[] { 0, 1, 4, 5 };
        for (int i : sideImagesIndexes) {
            for (int y = 0; y < size; ++y) {
                pixel.red = values[y][0];
                pixel.green = values[y][1];
                pixel.blue = values[y][2];
                for (int x = 0; x < size; ++x) {
                    pixelIO.write(image, i, pixel, x, y);
                }
            }
        }
        LOGGER.fine("Creating top texture.");
        pixelIO.read(image, 0, pixel, 0, image.getHeight() - 1);
        for (int y = 0; y < size; ++y) {
            for (int x = 0; x < size; ++x) {
                pixelIO.write(image, 3, pixel, x, y);
            }
        }
        LOGGER.fine("Creating bottom texture.");
        pixelIO.read(image, 0, pixel, 0, 0);
        for (int y = 0; y < size; ++y) {
            for (int x = 0; x < size; ++x) {
                pixelIO.write(image, 2, pixel, x, y);
            }
        }
        texture = new TextureCubeMap(image);
    }
    LOGGER.fine("Sky texture created. Creating sky.");
    return SkyFactory.createSky(blenderContext.getAssetManager(), texture, SkyFactory.EnvMapType.CubeMap);
}
Also used : ColorBand(com.jme3.scene.plugins.blender.textures.ColorBand) ArrayList(java.util.ArrayList) Image(com.jme3.texture.Image) PixelInputOutput(com.jme3.scene.plugins.blender.textures.io.PixelInputOutput) ColorRGBA(com.jme3.math.ColorRGBA) TextureCubeMap(com.jme3.texture.TextureCubeMap) TextureHelper(com.jme3.scene.plugins.blender.textures.TextureHelper) TexturePixel(com.jme3.scene.plugins.blender.textures.TexturePixel) CombinedTexture(com.jme3.scene.plugins.blender.textures.CombinedTexture)

Example 9 with PixelInputOutput

use of com.jme3.scene.plugins.blender.textures.io.PixelInputOutput in project jmonkeyengine by jMonkeyEngine.

the class ImageUtils method mix.

/**
     * This method merges two given images. The result is stored in the
     * 'target' image.
     * 
     * @param targetImage
     *            the target image
     * @param sourceImage
     *            the source image
     */
public static void mix(Image targetImage, Image sourceImage) {
    if (sourceImage.getDepth() != targetImage.getDepth()) {
        throw new IllegalArgumentException("The given images should have the same depth to merge them!");
    }
    if (sourceImage.getWidth() != targetImage.getWidth()) {
        throw new IllegalArgumentException("The given images should have the same width to merge them!");
    }
    if (sourceImage.getHeight() != targetImage.getHeight()) {
        throw new IllegalArgumentException("The given images should have the same height to merge them!");
    }
    PixelInputOutput sourceIO = PixelIOFactory.getPixelIO(sourceImage.getFormat());
    PixelInputOutput targetIO = PixelIOFactory.getPixelIO(targetImage.getFormat());
    TexturePixel sourcePixel = new TexturePixel();
    TexturePixel targetPixel = new TexturePixel();
    int depth = targetImage.getDepth() == 0 ? 1 : targetImage.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.mix(sourcePixel);
                targetIO.write(targetImage, layerIndex, targetPixel, x, y);
            }
        }
    }
}
Also used : PixelInputOutput(com.jme3.scene.plugins.blender.textures.io.PixelInputOutput)

Example 10 with PixelInputOutput

use of com.jme3.scene.plugins.blender.textures.io.PixelInputOutput in project jmonkeyengine by jMonkeyEngine.

the class TextureHelper method applyColorbandAndColorFactors.

/**
     * This method applies the colorband and color factors to image type
     * textures. If there is no colorband defined for the texture or the color
     * factors are all equal to 1.0f then no changes are made.
     * 
     * @param tex
     *            the texture structure
     * @param image
     *            the image that will be altered if necessary
     * @param blenderContext
     *            the blender context
     */
private void applyColorbandAndColorFactors(Structure tex, Image image, BlenderContext blenderContext) {
    float rfac = ((Number) tex.getFieldValue("rfac")).floatValue();
    float gfac = ((Number) tex.getFieldValue("gfac")).floatValue();
    float bfac = ((Number) tex.getFieldValue("bfac")).floatValue();
    float[][] colorBand = new ColorBand(tex, blenderContext).computeValues();
    int depth = image.getDepth() == 0 ? 1 : image.getDepth();
    if (colorBand != null) {
        TexturePixel pixel = new TexturePixel();
        PixelInputOutput imageIO = PixelIOFactory.getPixelIO(image.getFormat());
        for (int layerIndex = 0; layerIndex < depth; ++layerIndex) {
            for (int x = 0; x < image.getWidth(); ++x) {
                for (int y = 0; y < image.getHeight(); ++y) {
                    imageIO.read(image, layerIndex, pixel, x, y);
                    int colorbandIndex = (int) (pixel.alpha * 1000.0f);
                    pixel.red = colorBand[colorbandIndex][0] * rfac;
                    pixel.green = colorBand[colorbandIndex][1] * gfac;
                    pixel.blue = colorBand[colorbandIndex][2] * bfac;
                    pixel.alpha = colorBand[colorbandIndex][3];
                    imageIO.write(image, layerIndex, pixel, x, y);
                }
            }
        }
    } else if (rfac != 1.0f || gfac != 1.0f || bfac != 1.0f) {
        TexturePixel pixel = new TexturePixel();
        PixelInputOutput imageIO = PixelIOFactory.getPixelIO(image.getFormat());
        for (int layerIndex = 0; layerIndex < depth; ++layerIndex) {
            for (int x = 0; x < image.getWidth(); ++x) {
                for (int y = 0; y < image.getHeight(); ++y) {
                    imageIO.read(image, layerIndex, pixel, x, y);
                    pixel.red *= rfac;
                    pixel.green *= gfac;
                    pixel.blue *= bfac;
                    imageIO.write(image, layerIndex, pixel, x, y);
                }
            }
        }
    }
}
Also used : PixelInputOutput(com.jme3.scene.plugins.blender.textures.io.PixelInputOutput)

Aggregations

PixelInputOutput (com.jme3.scene.plugins.blender.textures.io.PixelInputOutput)12 Image (com.jme3.texture.Image)8 ByteBuffer (java.nio.ByteBuffer)5 ArrayList (java.util.ArrayList)5 TexturePixel (com.jme3.scene.plugins.blender.textures.TexturePixel)4 Format (com.jme3.texture.Image.Format)3 TextureCubeMap (com.jme3.texture.TextureCubeMap)3 BufferedImage (java.awt.image.BufferedImage)2 ColorRGBA (com.jme3.math.ColorRGBA)1 ColorBand (com.jme3.scene.plugins.blender.textures.ColorBand)1 CombinedTexture (com.jme3.scene.plugins.blender.textures.CombinedTexture)1 TextureHelper (com.jme3.scene.plugins.blender.textures.TextureHelper)1 Texture2D (com.jme3.texture.Texture2D)1 TempVars (com.jme3.util.TempVars)1