Search in sources :

Example 11 with TexturePixel

use of com.jme3.scene.plugins.blender.textures.TexturePixel 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)

Example 12 with TexturePixel

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

the class TriangulatedTexture method draw.

/**
     * This method draws the source image on the target image starting with the
     * specified positions.
     * 
     * @param target
     *            the target image
     * @param source
     *            the source image
     * @param targetXPos
     *            start X position on the target image
     * @param targetYPos
     *            start Y position on the target image
     */
private void draw(Image target, Image source, int targetXPos, int targetYPos) {
    PixelInputOutput sourceIO = PixelIOFactory.getPixelIO(source.getFormat());
    PixelInputOutput targetIO = PixelIOFactory.getPixelIO(target.getFormat());
    TexturePixel pixel = new TexturePixel();
    for (int x = 0; x < source.getWidth(); ++x) {
        for (int y = 0; y < source.getHeight(); ++y) {
            sourceIO.read(source, 0, pixel, x, y);
            targetIO.write(target, 0, pixel, targetXPos + x, targetYPos + y);
        }
    }
}
Also used : PixelInputOutput(com.jme3.scene.plugins.blender.textures.io.PixelInputOutput)

Example 13 with TexturePixel

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

the class TextureBlenderAWT method blend.

@Override
public Image blend(Image image, Image baseImage, BlenderContext blenderContext) {
    this.prepareImagesForBlending(image, baseImage);
    float[] pixelColor = new float[] { color[0], color[1], color[2], 1.0f };
    Format format = image.getFormat();
    PixelInputOutput basePixelIO = null, pixelReader = PixelIOFactory.getPixelIO(format);
    TexturePixel basePixel = null, pixel = new TexturePixel();
    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;
    }
    int bytesPerPixel = image.getFormat().getBitsPerPixel() >> 3;
    ArrayList<ByteBuffer> dataArray = new ArrayList<ByteBuffer>(depth);
    float[] resultPixel = new float[4];
    for (int dataLayerIndex = 0; dataLayerIndex < depth; ++dataLayerIndex) {
        ByteBuffer data = image.getData(dataLayerIndex);
        data.rewind();
        int imagePixelCount = data.limit() / bytesPerPixel;
        ByteBuffer newData = BufferUtils.createByteBuffer(imagePixelCount * 4);
        int dataIndex = 0, x = 0, y = 0, index = 0;
        while (index < data.limit()) {
            // 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;
                }
            }
            // reading the current texture's pixel
            pixelReader.read(image, dataLayerIndex, pixel, index);
            index += bytesPerPixel;
            pixel.toRGBA(pixelColor);
            if (negateTexture) {
                pixel.negate();
            }
            this.blendPixel(resultPixel, materialColor, pixelColor, 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) (pixelColor[3] * 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 14 with TexturePixel

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

the class DDSPixelInputOutput method read.

public void read(Image image, int layer, TexturePixel pixel, int x, int y) {
    int xTexetlIndex = x % image.getWidth() >> 2;
    int yTexelIndex = y % image.getHeight() >> 2;
    int xTexelCount = image.getWidth() >> 2;
    int texelIndex = yTexelIndex * xTexelCount + xTexetlIndex;
    TexturePixel[] colors = new TexturePixel[] { new TexturePixel(), new TexturePixel(), new TexturePixel(), new TexturePixel() };
    int indexes = 0;
    long alphaIndexes = 0;
    float[] alphas = null;
    ByteBuffer data = image.getData().get(layer);
    switch(image.getFormat()) {
        // BC1
        case DXT1:
        case DXT1A:
            {
                data.position(texelIndex * 8);
                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
                indexes = data.getInt();
                break;
            }
        case DXT3:
            {
                // BC2
                data.position(texelIndex * 16);
                long alpha = data.getLong();
                alphas = new float[16];
                for (int i = 0; i < 16; ++i) {
                    alphaIndexes |= 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
                indexes = data.getInt();
                break;
            }
        case DXT5:
            {
                // BC3
                data.position(texelIndex * 16);
                alphas = new float[8];
                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
                alphaIndexes = (long) 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
                indexes = data.getInt();
                break;
            }
        default:
            throw new IllegalStateException("Unsupported decompression format.");
    }
    // coordinates of the pixel in the selected texel
    // pixels are arranged from left to right
    x = x - 4 * xTexetlIndex;
    // pixels are arranged from bottom to top (that is why '3 - ...' is at the start)
    y = 3 - y - 4 * yTexelIndex;
    int pixelIndexInTexel = (y * 4 + x) * (int) FastMath.log(colors.length, 2);
    int alphaIndexInTexel = alphas != null ? (y * 4 + x) * (int) FastMath.log(alphas.length, 2) : 0;
    // getting the pixel
    int indexMask = colors.length - 1;
    int colorIndex = indexes >> pixelIndexInTexel & indexMask;
    float alpha = alphas != null ? alphas[(int) (alphaIndexes >> alphaIndexInTexel & 0x07)] : colors[colorIndex].alpha;
    pixel.fromPixel(colors[colorIndex]);
    pixel.alpha = alpha;
}
Also used : TexturePixel(com.jme3.scene.plugins.blender.textures.TexturePixel) ByteBuffer(java.nio.ByteBuffer)

Example 15 with TexturePixel

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

the class SubdivisionSurfaceModifier method interpolateVertexColors.

/**
     * The method computes the average value for the given vertex colors.
     * @param colors
     *            the vertex colors
     * @return vertex colors' average value
     */
private byte[] interpolateVertexColors(byte[]... colors) {
    TexturePixel pixel = new TexturePixel();
    TexturePixel temp = new TexturePixel();
    for (int i = 0; i < colors.length; ++i) {
        temp.fromARGB8(colors[i][3], colors[i][0], colors[i][1], colors[i][2]);
        pixel.add(temp);
    }
    pixel.divide(colors.length);
    byte[] result = new byte[4];
    pixel.toRGBA8(result);
    return result;
}
Also used : TexturePixel(com.jme3.scene.plugins.blender.textures.TexturePixel)

Aggregations

PixelInputOutput (com.jme3.scene.plugins.blender.textures.io.PixelInputOutput)12 Image (com.jme3.texture.Image)9 ByteBuffer (java.nio.ByteBuffer)7 TexturePixel (com.jme3.scene.plugins.blender.textures.TexturePixel)6 ArrayList (java.util.ArrayList)6 Format (com.jme3.texture.Image.Format)4 TextureCubeMap (com.jme3.texture.TextureCubeMap)3 BufferedImage (java.awt.image.BufferedImage)3 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