Search in sources :

Example 1 with ImageRaster

use of com.jme3.texture.image.ImageRaster in project jmonkeyengine by jMonkeyEngine.

the class ImageBasedHeightMap method load.

public boolean load(boolean flipX, boolean flipY) {
    int imageWidth = colorImage.getWidth();
    int imageHeight = colorImage.getHeight();
    if (imageWidth != imageHeight)
        throw new RuntimeException("imageWidth: " + imageWidth + " != imageHeight: " + imageHeight);
    size = imageWidth;
    ImageRaster raster = getImageRaster();
    heightData = new float[(imageWidth * imageHeight)];
    ColorRGBA colorStore = new ColorRGBA();
    int index = 0;
    if (flipY) {
        for (int h = 0; h < imageHeight; ++h) {
            if (flipX) {
                for (int w = imageWidth - 1; w >= 0; --w) {
                    //int baseIndex = (h * imageWidth)+ w;
                    //heightData[index++] = getHeightAtPostion(raster, baseIndex, colorStore)*heightScale;
                    heightData[index++] = calculateHeight(raster.getPixel(w, h, colorStore)) * heightScale * backwardsCompScale;
                }
            } else {
                for (int w = 0; w < imageWidth; ++w) {
                    //int baseIndex = (h * imageWidth)+ w;
                    //heightData[index++] = getHeightAtPostion(raster, baseIndex, colorStore)*heightScale;
                    heightData[index++] = calculateHeight(raster.getPixel(w, h, colorStore)) * heightScale * backwardsCompScale;
                }
            }
        }
    } else {
        for (int h = imageHeight - 1; h >= 0; --h) {
            if (flipX) {
                for (int w = imageWidth - 1; w >= 0; --w) {
                    //int baseIndex = (h * imageWidth)+ w;
                    //heightData[index++] = getHeightAtPostion(raster, baseIndex, colorStore)*heightScale;
                    heightData[index++] = calculateHeight(raster.getPixel(w, h, colorStore)) * heightScale * backwardsCompScale;
                }
            } else {
                for (int w = 0; w < imageWidth; ++w) {
                    //int baseIndex = (h * imageWidth)+ w;
                    //heightData[index++] = getHeightAtPostion(raster, baseIndex, colorStore)*heightScale;
                    heightData[index++] = calculateHeight(raster.getPixel(w, h, colorStore)) * heightScale * backwardsCompScale;
                }
            }
        }
    }
    return true;
}
Also used : ColorRGBA(com.jme3.math.ColorRGBA) ImageRaster(com.jme3.texture.image.ImageRaster)

Example 2 with ImageRaster

use of com.jme3.texture.image.ImageRaster in project jmonkeyengine by jMonkeyEngine.

the class MipMapGenerator method scaleImage.

public static Image scaleImage(Image inputImage, int outputWidth, int outputHeight) {
    int size = outputWidth * outputHeight * inputImage.getFormat().getBitsPerPixel() / 8;
    ByteBuffer buffer = BufferUtils.createByteBuffer(size);
    Image outputImage = new Image(inputImage.getFormat(), outputWidth, outputHeight, buffer, inputImage.getColorSpace());
    ImageRaster input = ImageRaster.create(inputImage, 0, 0, false);
    ImageRaster output = ImageRaster.create(outputImage, 0, 0, false);
    float xRatio = ((float) (input.getWidth() - 1)) / output.getWidth();
    float yRatio = ((float) (input.getHeight() - 1)) / output.getHeight();
    ColorRGBA outputColor = new ColorRGBA();
    ColorRGBA bottomLeft = new ColorRGBA();
    ColorRGBA bottomRight = new ColorRGBA();
    ColorRGBA topLeft = new ColorRGBA();
    ColorRGBA topRight = new ColorRGBA();
    for (int y = 0; y < outputHeight; y++) {
        for (int x = 0; x < outputWidth; x++) {
            float x2f = x * xRatio;
            float y2f = y * yRatio;
            int x2 = (int) x2f;
            int y2 = (int) y2f;
            float xDiff = x2f - x2;
            float yDiff = y2f - y2;
            input.getPixel(x2, y2, bottomLeft);
            input.getPixel(x2 + 1, y2, bottomRight);
            input.getPixel(x2, y2 + 1, topLeft);
            input.getPixel(x2 + 1, y2 + 1, topRight);
            bottomLeft.multLocal((1f - xDiff) * (1f - yDiff));
            bottomRight.multLocal((xDiff) * (1f - yDiff));
            topLeft.multLocal((1f - xDiff) * (yDiff));
            topRight.multLocal((xDiff) * (yDiff));
            outputColor.set(bottomLeft).addLocal(bottomRight).addLocal(topLeft).addLocal(topRight);
            output.setPixel(x, y, outputColor);
        }
    }
    return outputImage;
}
Also used : ColorRGBA(com.jme3.math.ColorRGBA) ImageRaster(com.jme3.texture.image.ImageRaster) Image(com.jme3.texture.Image) ByteBuffer(java.nio.ByteBuffer)

Example 3 with ImageRaster

use of com.jme3.texture.image.ImageRaster in project jmonkeyengine by jMonkeyEngine.

the class TestImageRaster method createTestImage.

private Image createTestImage() {
    Image testImage = new Image(Format.BGR8, 4, 3, BufferUtils.createByteBuffer(4 * 4 * 3), null, ColorSpace.Linear);
    ImageRaster io = ImageRaster.create(testImage);
    io.setPixel(0, 0, ColorRGBA.Black);
    io.setPixel(1, 0, ColorRGBA.Gray);
    io.setPixel(2, 0, ColorRGBA.White);
    // HDR color
    io.setPixel(3, 0, ColorRGBA.White.mult(4));
    io.setPixel(0, 1, ColorRGBA.Red);
    io.setPixel(1, 1, ColorRGBA.Green);
    io.setPixel(2, 1, ColorRGBA.Blue);
    io.setPixel(3, 1, new ColorRGBA(0, 0, 0, 0));
    io.setPixel(0, 2, ColorRGBA.Yellow);
    io.setPixel(1, 2, ColorRGBA.Magenta);
    io.setPixel(2, 2, ColorRGBA.Cyan);
    io.setPixel(3, 2, new ColorRGBA(1, 1, 1, 0));
    return testImage;
}
Also used : ColorRGBA(com.jme3.math.ColorRGBA) ImageRaster(com.jme3.texture.image.ImageRaster) Image(com.jme3.texture.Image)

Example 4 with ImageRaster

use of com.jme3.texture.image.ImageRaster in project jmonkeyengine by jMonkeyEngine.

the class TestAnisotropicFilter method createCheckerBoardTexture.

private static Texture2D createCheckerBoardTexture() {
    Image image = new Image(Format.RGBA8, 1024, 1024, BufferUtils.createByteBuffer(1024 * 1024 * 4), ColorSpace.sRGB);
    ImageRaster raster = ImageRaster.create(image);
    for (int y = 0; y < 1024; y++) {
        for (int x = 0; x < 1024; x++) {
            if (y < 512) {
                if (x < 512) {
                    raster.setPixel(x, y, ColorRGBA.Black);
                } else {
                    raster.setPixel(x, y, ColorRGBA.White);
                }
            } else {
                if (x < 512) {
                    raster.setPixel(x, y, ColorRGBA.White);
                } else {
                    raster.setPixel(x, y, ColorRGBA.Black);
                }
            }
        }
    }
    return new Texture2D(image);
}
Also used : Texture2D(com.jme3.texture.Texture2D) ImageRaster(com.jme3.texture.image.ImageRaster) Image(com.jme3.texture.Image)

Example 5 with ImageRaster

use of com.jme3.texture.image.ImageRaster in project jmonkeyengine by jMonkeyEngine.

the class TestImageRaster method convertImage.

private Image convertImage(Image image, Format newFormat) {
    int width = image.getWidth();
    int height = image.getHeight();
    ByteBuffer data = BufferUtils.createByteBuffer((int) Math.ceil(newFormat.getBitsPerPixel() / 8.0) * width * height);
    Image convertedImage = new Image(newFormat, width, height, data, null, image.getColorSpace());
    ImageRaster sourceReader = ImageRaster.create(image);
    ImageRaster targetWriter = ImageRaster.create(convertedImage);
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            ColorRGBA color = sourceReader.getPixel(x, y);
            targetWriter.setPixel(x, y, color);
        }
    }
    return convertedImage;
}
Also used : ColorRGBA(com.jme3.math.ColorRGBA) ImageRaster(com.jme3.texture.image.ImageRaster) Image(com.jme3.texture.Image) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ImageRaster (com.jme3.texture.image.ImageRaster)5 ColorRGBA (com.jme3.math.ColorRGBA)4 Image (com.jme3.texture.Image)4 ByteBuffer (java.nio.ByteBuffer)2 Texture2D (com.jme3.texture.Texture2D)1