Search in sources :

Example 21 with Format

use of com.jme3.texture.Image.Format in project jmonkeyengine by jMonkeyEngine.

the class ShaderNodeLoaderDelegate method parseMapping.

/**
     * reads a mapping statement. Sets the nameSpace, name and swizzling of the
     * left variable. Sets the name, nameSpace and swizzling of the right
     * variable types will be determined later.
     *
     * <code>
     * Format : <nameSpace>.<varName>[.<swizzling>] =
     * <nameSpace>.<varName>[.<swizzling>][:Condition]
     * </code>
     * 
     * @param statement the statement to read
     * @return the read mapping
     */
protected VariableMapping parseMapping(Statement statement, boolean[] hasNameSpace) throws IOException {
    VariableMapping mapping = new VariableMapping();
    String[] cond = statement.getLine().split(":");
    String[] vars = cond[0].split("=");
    checkMappingFormat(vars, statement);
    ShaderNodeVariable[] variables = new ShaderNodeVariable[2];
    String[] swizzle = new String[2];
    for (int i = 0; i < vars.length; i++) {
        String[] expression = vars[i].trim().split("\\.");
        if (hasNameSpace[i]) {
            if (expression.length <= 3) {
                variables[i] = new ShaderNodeVariable("", expression[0].trim(), expression[1].trim());
            }
            if (expression.length == 3) {
                swizzle[i] = expression[2].trim();
            }
        } else {
            if (expression.length <= 2) {
                variables[i] = new ShaderNodeVariable("", expression[0].trim());
            }
            if (expression.length == 2) {
                swizzle[i] = expression[1].trim();
            }
        }
    }
    mapping.setLeftVariable(variables[0]);
    mapping.setLeftSwizzling(swizzle[0] != null ? swizzle[0] : "");
    mapping.setRightVariable(variables[1]);
    mapping.setRightSwizzling(swizzle[1] != null ? swizzle[1] : "");
    if (cond.length > 1) {
        extractCondition(cond[1], statement);
        mapping.setCondition(conditionParser.getFormattedExpression());
    }
    return mapping;
}
Also used : VariableMapping(com.jme3.shader.VariableMapping) ShaderNodeVariable(com.jme3.shader.ShaderNodeVariable)

Example 22 with Format

use of com.jme3.texture.Image.Format in project jmonkeyengine by jMonkeyEngine.

the class EnvMapUtils method createPrefilteredEnvMap.

/**
     * initialize the pem map
     * @param size the size of the map
     * @param imageFormat the format of the image
     * @return the initialized prefiltered env map
     */
public static TextureCubeMap createPrefilteredEnvMap(int size, Image.Format imageFormat) {
    TextureCubeMap pem = new TextureCubeMap(size, size, imageFormat);
    pem.setMagFilter(Texture.MagFilter.Bilinear);
    pem.setMinFilter(Texture.MinFilter.Trilinear);
    pem.getImage().setColorSpace(ColorSpace.Linear);
    int nbMipMap = (int) (Math.log(size) / Math.log(2) - 1);
    CubeMapWrapper targetWrapper = new CubeMapWrapper(pem);
    targetWrapper.initMipMaps(nbMipMap);
    return pem;
}
Also used : TextureCubeMap(com.jme3.texture.TextureCubeMap)

Example 23 with Format

use of com.jme3.texture.Image.Format in project jmonkeyengine by jMonkeyEngine.

the class EnvMapUtils method generateIrradianceMap.

/**
     * Generates the Irradiance map (used for image based difuse lighting) from
     * Spherical Harmonics coefficients previously computed with
     * {@link EnvMapUtils#getSphericalHarmonicsCoefficents(com.jme3.texture.TextureCubeMap)}
     * Note that the output cube map is in RGBA8 format.
     *
     * @param shCoeffs the SH coeffs
     * @param targetMapSize the size of the irradiance map to generate
     * @param fixSeamsMethod the method to fix seams
     * @param store
     * @return The irradiance cube map for the given coefficients
     */
public static TextureCubeMap generateIrradianceMap(Vector3f[] shCoeffs, int targetMapSize, FixSeamsMethod fixSeamsMethod, TextureCubeMap store) {
    TextureCubeMap irrCubeMap = store;
    if (irrCubeMap == null) {
        irrCubeMap = new TextureCubeMap(targetMapSize, targetMapSize, Image.Format.RGB16F);
        irrCubeMap.setMagFilter(Texture.MagFilter.Bilinear);
        irrCubeMap.setMinFilter(Texture.MinFilter.BilinearNoMipMaps);
        irrCubeMap.getImage().setColorSpace(ColorSpace.Linear);
    }
    for (int i = 0; i < 6; i++) {
        ByteBuffer buf = BufferUtils.createByteBuffer(targetMapSize * targetMapSize * irrCubeMap.getImage().getFormat().getBitsPerPixel() / 8);
        irrCubeMap.getImage().setData(i, buf);
    }
    Vector3f texelVect = new Vector3f();
    ColorRGBA color = new ColorRGBA(ColorRGBA.Black);
    float[] shDir = new float[9];
    CubeMapWrapper envMapWriter = new CubeMapWrapper(irrCubeMap);
    for (int face = 0; face < 6; face++) {
        for (int y = 0; y < targetMapSize; y++) {
            for (int x = 0; x < targetMapSize; x++) {
                getVectorFromCubemapFaceTexCoord(x, y, targetMapSize, face, texelVect, fixSeamsMethod);
                evalShBasis(texelVect, shDir);
                color.set(0, 0, 0, 0);
                for (int i = 0; i < NUM_SH_COEFFICIENT; i++) {
                    color.set(color.r + shCoeffs[i].x * shDir[i] * shBandFactor[i], color.g + shCoeffs[i].y * shDir[i] * shBandFactor[i], color.b + shCoeffs[i].z * shDir[i] * shBandFactor[i], 1.0f);
                }
                //clamping the color because very low value close to zero produce artifacts
                color.r = Math.max(0.0001f, color.r);
                color.g = Math.max(0.0001f, color.g);
                color.b = Math.max(0.0001f, color.b);
                envMapWriter.setPixel(x, y, face, color);
            }
        }
    }
    return irrCubeMap;
}
Also used : ColorRGBA(com.jme3.math.ColorRGBA) TextureCubeMap(com.jme3.texture.TextureCubeMap) Vector3f(com.jme3.math.Vector3f) ByteBuffer(java.nio.ByteBuffer)

Example 24 with Format

use of com.jme3.texture.Image.Format in project jmonkeyengine by jMonkeyEngine.

the class EnvMapUtils method createIrradianceMap.

/**
     * initialize the Irradiancemap
     * @param size the size of the map
     * @param imageFormat the format of the image
     * @return the initialized Irradiance map
     */
public static TextureCubeMap createIrradianceMap(int size, Image.Format imageFormat) {
    TextureCubeMap irrMap = new TextureCubeMap(size, size, imageFormat);
    irrMap.setMagFilter(Texture.MagFilter.Bilinear);
    irrMap.setMinFilter(Texture.MinFilter.BilinearNoMipMaps);
    irrMap.getImage().setColorSpace(ColorSpace.Linear);
    return irrMap;
}
Also used : TextureCubeMap(com.jme3.texture.TextureCubeMap)

Example 25 with Format

use of com.jme3.texture.Image.Format in project jmonkeyengine by jMonkeyEngine.

the class ImageUtils method decompress.

/**
     * This method decompresses the given image. If the given image is already
     * decompressed nothing happens and it is simply returned.
     * 
     * @param image
     *            the image to decompress
     * @return the decompressed image
     */
public static Image decompress(Image image) {
    Format format = image.getFormat();
    int depth = image.getDepth();
    if (depth == 0) {
        depth = 1;
    }
    ArrayList<ByteBuffer> dataArray = new ArrayList<ByteBuffer>(depth);
    int[] sizes = image.getMipMapSizes() != null ? image.getMipMapSizes() : new int[1];
    int[] newMipmapSizes = image.getMipMapSizes() != null ? new int[image.getMipMapSizes().length] : null;
    for (int dataLayerIndex = 0; dataLayerIndex < depth; ++dataLayerIndex) {
        ByteBuffer data = image.getData(dataLayerIndex);
        data.rewind();
        if (sizes.length == 1) {
            sizes[0] = data.remaining();
        }
        // this should always be constant for each mipmap
        float widthToHeightRatio = image.getWidth() / image.getHeight();
        List<DDSTexelData> texelDataList = new ArrayList<DDSTexelData>(sizes.length);
        int maxPosition = 0, resultSize = 0;
        for (int sizeIndex = 0; sizeIndex < sizes.length; ++sizeIndex) {
            maxPosition += sizes[sizeIndex];
            DDSTexelData texelData = new DDSTexelData(sizes[sizeIndex], widthToHeightRatio, format);
            texelDataList.add(texelData);
            switch(format) {
                // BC1
                case DXT1:
                case DXT1A:
                    while (data.position() < maxPosition) {
                        TexturePixel[] colors = new TexturePixel[] { new TexturePixel(), new TexturePixel(), new TexturePixel(), new TexturePixel() };
                        short c0 = data.getShort();
                        short c1 = data.getShort();
                        int col0 = RGB565.RGB565_to_ARGB8(c0);
                        int col1 = RGB565.RGB565_to_ARGB8(c1);
                        colors[0].fromARGB8(col0);
                        colors[1].fromARGB8(col1);
                        if (col0 > col1) {
                            // creating color2 = 2/3color0 + 1/3color1
                            colors[2].fromPixel(colors[0]);
                            colors[2].mult(2);
                            colors[2].add(colors[1]);
                            colors[2].divide(3);
                            // creating color3 = 1/3color0 + 2/3color1;
                            colors[3].fromPixel(colors[1]);
                            colors[3].mult(2);
                            colors[3].add(colors[0]);
                            colors[3].divide(3);
                        } else {
                            // creating color2 = 1/2color0 + 1/2color1
                            colors[2].fromPixel(colors[0]);
                            colors[2].add(colors[1]);
                            colors[2].mult(0.5f);
                            colors[3].fromARGB8(0);
                        }
                        // 4-byte table with color indexes in decompressed table
                        int indexes = data.getInt();
                        texelData.add(colors, indexes);
                    }
                    break;
                case // BC2
                DXT3:
                    while (data.position() < maxPosition) {
                        TexturePixel[] colors = new TexturePixel[] { new TexturePixel(), new TexturePixel(), new TexturePixel(), new TexturePixel() };
                        long alpha = data.getLong();
                        float[] alphas = new float[16];
                        long alphasIndex = 0;
                        for (int i = 0; i < 16; ++i) {
                            alphasIndex |= i << i * 4;
                            byte a = (byte) ((alpha >> i * 4 & 0x0F) << 4);
                            alphas[i] = a >= 0 ? a / 255.0f : 1.0f - ~a / 255.0f;
                        }
                        short c0 = data.getShort();
                        short c1 = data.getShort();
                        int col0 = RGB565.RGB565_to_ARGB8(c0);
                        int col1 = RGB565.RGB565_to_ARGB8(c1);
                        colors[0].fromARGB8(col0);
                        colors[1].fromARGB8(col1);
                        // creating color2 = 2/3color0 + 1/3color1
                        colors[2].fromPixel(colors[0]);
                        colors[2].mult(2);
                        colors[2].add(colors[1]);
                        colors[2].divide(3);
                        // creating color3 = 1/3color0 + 2/3color1;
                        colors[3].fromPixel(colors[1]);
                        colors[3].mult(2);
                        colors[3].add(colors[0]);
                        colors[3].divide(3);
                        // 4-byte table with color indexes in decompressed table
                        int indexes = data.getInt();
                        texelData.add(colors, indexes, alphas, alphasIndex);
                    }
                    break;
                case // BC3
                DXT5:
                    float[] alphas = new float[8];
                    while (data.position() < maxPosition) {
                        TexturePixel[] colors = new TexturePixel[] { new TexturePixel(), new TexturePixel(), new TexturePixel(), new TexturePixel() };
                        alphas[0] = data.get() * 255.0f;
                        alphas[1] = data.get() * 255.0f;
                        //the casts to long must be done here because otherwise 32-bit integers would be shifetd by 32 and 40 bits which would result in improper values
                        long alphaIndices = data.get() | (long) data.get() << 8 | (long) data.get() << 16 | (long) data.get() << 24 | (long) data.get() << 32 | (long) data.get() << 40;
                        if (alphas[0] > alphas[1]) {
                            // 6 interpolated alpha values.
                            alphas[2] = (6 * alphas[0] + alphas[1]) / 7;
                            alphas[3] = (5 * alphas[0] + 2 * alphas[1]) / 7;
                            alphas[4] = (4 * alphas[0] + 3 * alphas[1]) / 7;
                            alphas[5] = (3 * alphas[0] + 4 * alphas[1]) / 7;
                            alphas[6] = (2 * alphas[0] + 5 * alphas[1]) / 7;
                            alphas[7] = (alphas[0] + 6 * alphas[1]) / 7;
                        } else {
                            alphas[2] = (4 * alphas[0] + alphas[1]) * 0.2f;
                            alphas[3] = (3 * alphas[0] + 2 * alphas[1]) * 0.2f;
                            alphas[4] = (2 * alphas[0] + 3 * alphas[1]) * 0.2f;
                            alphas[5] = (alphas[0] + 4 * alphas[1]) * 0.2f;
                            alphas[6] = 0;
                            alphas[7] = 1;
                        }
                        short c0 = data.getShort();
                        short c1 = data.getShort();
                        int col0 = RGB565.RGB565_to_ARGB8(c0);
                        int col1 = RGB565.RGB565_to_ARGB8(c1);
                        colors[0].fromARGB8(col0);
                        colors[1].fromARGB8(col1);
                        // creating color2 = 2/3color0 + 1/3color1
                        colors[2].fromPixel(colors[0]);
                        colors[2].mult(2);
                        colors[2].add(colors[1]);
                        colors[2].divide(3);
                        // creating color3 = 1/3color0 + 2/3color1;
                        colors[3].fromPixel(colors[1]);
                        colors[3].mult(2);
                        colors[3].add(colors[0]);
                        colors[3].divide(3);
                        // 4-byte table with color indexes in decompressed table
                        int indexes = data.getInt();
                        texelData.add(colors, indexes, alphas, alphaIndices);
                    }
                    break;
                default:
                    throw new IllegalStateException("Unknown compressed format: " + format);
            }
            newMipmapSizes[sizeIndex] = texelData.getSizeInBytes();
            resultSize += texelData.getSizeInBytes();
        }
        byte[] bytes = new byte[resultSize];
        int offset = 0;
        byte[] pixelBytes = new byte[4];
        for (DDSTexelData texelData : texelDataList) {
            for (int i = 0; i < texelData.getPixelWidth(); ++i) {
                for (int j = 0; j < texelData.getPixelHeight(); ++j) {
                    if (texelData.getRGBA8(i, j, pixelBytes)) {
                        bytes[offset + (j * texelData.getPixelWidth() + i) * 4] = pixelBytes[0];
                        bytes[offset + (j * texelData.getPixelWidth() + i) * 4 + 1] = pixelBytes[1];
                        bytes[offset + (j * texelData.getPixelWidth() + i) * 4 + 2] = pixelBytes[2];
                        bytes[offset + (j * texelData.getPixelWidth() + i) * 4 + 3] = pixelBytes[3];
                    } else {
                        break;
                    }
                }
            }
            offset += texelData.getSizeInBytes();
        }
        dataArray.add(BufferUtils.createByteBuffer(bytes));
    }
    Image result = depth > 1 ? new Image(Format.RGBA8, image.getWidth(), image.getHeight(), depth, dataArray, com.jme3.texture.image.ColorSpace.Linear) : new Image(Format.RGBA8, image.getWidth(), image.getHeight(), dataArray.get(0), com.jme3.texture.image.ColorSpace.Linear);
    if (newMipmapSizes != null) {
        result.setMipMapSizes(newMipmapSizes);
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) BufferedImage(java.awt.image.BufferedImage) Image(com.jme3.texture.Image) ByteBuffer(java.nio.ByteBuffer) Format(com.jme3.texture.Image.Format)

Aggregations

Image (com.jme3.texture.Image)20 ByteBuffer (java.nio.ByteBuffer)19 Format (com.jme3.texture.Image.Format)13 IOException (java.io.IOException)8 BufferedImage (java.awt.image.BufferedImage)7 ArrayList (java.util.ArrayList)7 TextureCubeMap (com.jme3.texture.TextureCubeMap)6 ColorRGBA (com.jme3.math.ColorRGBA)5 Texture2D (com.jme3.texture.Texture2D)5 TexturePixel (com.jme3.scene.plugins.blender.textures.TexturePixel)4 PixelInputOutput (com.jme3.scene.plugins.blender.textures.io.PixelInputOutput)4 DataInputStream (java.io.DataInputStream)4 AssetNotFoundException (com.jme3.asset.AssetNotFoundException)3 Vector3f (com.jme3.math.Vector3f)3 ShaderNodeVariable (com.jme3.shader.ShaderNodeVariable)3 VariableMapping (com.jme3.shader.VariableMapping)3 FrameBuffer (com.jme3.texture.FrameBuffer)3 LittleEndien (com.jme3.util.LittleEndien)3 InputStream (java.io.InputStream)3 TextureKey (com.jme3.asset.TextureKey)2