use of com.jme3.texture.Image 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;
}
use of com.jme3.texture.Image in project jmonkeyengine by jMonkeyEngine.
the class MipMapGenerator method generateMipMaps.
public static void generateMipMaps(Image image) {
int width = image.getWidth();
int height = image.getHeight();
Image current = image;
ArrayList<ByteBuffer> output = new ArrayList<ByteBuffer>();
int totalSize = 0;
while (height >= 1 || width >= 1) {
output.add(current.getData(0));
totalSize += current.getData(0).capacity();
if (height == 1 || width == 1) {
break;
}
height /= 2;
width /= 2;
current = scaleImage(current, width, height);
}
ByteBuffer combinedData = BufferUtils.createByteBuffer(totalSize);
int[] mipSizes = new int[output.size()];
for (int i = 0; i < output.size(); i++) {
ByteBuffer data = output.get(i);
data.clear();
combinedData.put(data);
mipSizes[i] = data.capacity();
}
combinedData.flip();
// insert mip data into image
image.setData(0, combinedData);
image.setMipMapSizes(mipSizes);
}
use of com.jme3.texture.Image in project jmonkeyengine by jMonkeyEngine.
the class PlaceholderAssets method getPlaceholderImage.
@Deprecated
public static Image getPlaceholderImage() {
ByteBuffer tempData = BufferUtils.createByteBuffer(3 * 4 * 4);
tempData.put(imageData).flip();
return new Image(Format.RGB8, 4, 4, tempData, null, ColorSpace.Linear);
}
use of com.jme3.texture.Image in project jmonkeyengine by jMonkeyEngine.
the class SkyFactory method checkImagesForCubeMap.
private static void checkImagesForCubeMap(Image... images) {
if (images.length == 1) {
return;
}
Format fmt = images[0].getFormat();
int width = images[0].getWidth();
int height = images[0].getHeight();
ByteBuffer data = images[0].getData(0);
int size = data != null ? data.capacity() : 0;
checkImage(images[0]);
for (int i = 1; i < images.length; i++) {
Image image = images[i];
checkImage(images[i]);
if (image.getFormat() != fmt) {
throw new IllegalArgumentException("Images must have same format");
}
if (image.getWidth() != width || image.getHeight() != height) {
throw new IllegalArgumentException("Images must have same resolution");
}
ByteBuffer data2 = image.getData(0);
if (data2 != null) {
if (data2.capacity() != size) {
throw new IllegalArgumentException("Images must have same size");
}
}
}
}
use of com.jme3.texture.Image in project jmonkeyengine by jMonkeyEngine.
the class DefaultImageRaster method getPixel.
@Override
public ColorRGBA getPixel(int x, int y, ColorRGBA store) {
rangeCheck(x, y);
codec.readComponents(getBuffer(), x, y, width, offset, components, temp);
if (store == null) {
store = new ColorRGBA();
}
switch(codec.type) {
case ImageCodec.FLAG_F16:
store.set(FastMath.convertHalfToFloat((short) components[1]), FastMath.convertHalfToFloat((short) components[2]), FastMath.convertHalfToFloat((short) components[3]), FastMath.convertHalfToFloat((short) components[0]));
break;
case ImageCodec.FLAG_F32:
store.set(Float.intBitsToFloat((int) components[1]), Float.intBitsToFloat((int) components[2]), Float.intBitsToFloat((int) components[3]), Float.intBitsToFloat((int) components[0]));
break;
case 0:
// Convert to float and divide by bitsize to get into range 0.0 - 1.0.
store.set((float) components[1] / codec.maxRed, (float) components[2] / codec.maxGreen, (float) components[3] / codec.maxBlue, (float) components[0] / codec.maxAlpha);
break;
}
if (codec.isGray) {
store.g = store.b = store.r;
} else {
if (codec.maxRed == 0) {
store.r = 1;
}
if (codec.maxGreen == 0) {
store.g = 1;
}
if (codec.maxBlue == 0) {
store.b = 1;
}
if (codec.maxAlpha == 0) {
store.a = 1;
}
}
if (convertToLinear) {
// Input image is sRGB, need to convert to linear.
store.setAsSrgb(store.r, store.g, store.b, store.a);
}
return store;
}
Aggregations