Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ nifty-style-black = { module = "com.github.nifty-gui:nifty-style-black",
spotbugs-gradle-plugin = "com.github.spotbugs.snom:spotbugs-gradle-plugin:6.4.8"
vecmath = "javax.vecmath:vecmath:1.5.2"

stb-image = "org.ngengine:stb-image:2.30.4"

[bundles]

[plugins]
Expand Down
2 changes: 0 additions & 2 deletions jme3-android/src/main/resources/com/jme3/asset/Android.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@ INCLUDE com/jme3/asset/General.cfg
# Android specific locators
LOCATOR / com.jme3.asset.plugins.AndroidLocator

# Android specific loaders
LOADER com.jme3.texture.plugins.AndroidNativeImageLoader : jpg, bmp, gif, png, jpeg
LOADER com.jme3.texture.plugins.AndroidBufferImageLoader : webp, heic, heif
22 changes: 15 additions & 7 deletions jme3-core/src/main/java/com/jme3/texture/image/ImageCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,21 @@ public ImageCodec(int bpp, int flags, int maxAlpha, int maxRed, int maxGreen, in

params.put(Format.RGB8, new ByteOffsetImageCodec(3, 0, -1, 0, 1, 2));

params.put(Format.RGB32F, new ByteAlignedImageCodec(12, FLAG_F32,
0, 4, 4, 4,
0, 0, 4, 8));

ByteAlignedImageCodec rgb16f = new ByteAlignedImageCodec(6, FLAG_F16,
0, 2, 2, 2,
0, 0, 2, 4);
params.put(Format.RGB32F, new ByteAlignedImageCodec(12, FLAG_F32,
0, 4, 4, 4,
0, 0, 4, 8));

params.put(Format.R16F, new ByteAlignedImageCodec(2, FLAG_F16,
0, 2, 0, 0,
0, 0, 0, 0));

params.put(Format.RG16F, new ByteAlignedImageCodec(4, FLAG_F16,
0, 2, 2, 0,
0, 0, 2, 0));

ByteAlignedImageCodec rgb16f = new ByteAlignedImageCodec(6, FLAG_F16,
0, 2, 2, 2,
0, 0, 2, 4);
params.put(Format.RGB16F, rgb16f);
params.put(Format.RGB16F_to_RGB111110F, rgb16f);
params.put(Format.RGB16F_to_RGB9E5, rgb16f);
Expand Down
1 change: 0 additions & 1 deletion jme3-core/src/main/resources/com/jme3/asset/Desktop.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
INCLUDE com/jme3/asset/General.cfg

# Desktop-specific loaders
LOADER com.jme3.texture.plugins.AWTLoader : jpg, bmp, gif, png, jpeg
LOADER com.jme3.cursors.plugins.CursorLoader : ani, cur, ico
3 changes: 1 addition & 2 deletions jme3-core/src/main/resources/com/jme3/asset/General.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ LOADER com.jme3.material.plugins.ShaderNodeDefinitionLoader : j3sn
LOADER com.jme3.font.plugins.BitmapFontLoader : fnt
LOADER com.jme3.texture.plugins.DDSLoader : dds
LOADER com.jme3.texture.plugins.PFMLoader : pfm
LOADER com.jme3.texture.plugins.HDRLoader : hdr
LOADER com.jme3.texture.plugins.TGALoader : tga
LOADER com.jme3.export.binary.BinaryLoader : j3o
LOADER com.jme3.export.binary.BinaryLoader : j3f
LOADER com.jme3.scene.plugins.OBJLoader : obj
Expand All @@ -23,4 +21,5 @@ LOADER com.jme3.shader.plugins.GLSLLoader : vert, frag, geom, tsctrl, tseval, gl
LOADER com.jme3.scene.plugins.gltf.GltfLoader : gltf
LOADER com.jme3.scene.plugins.gltf.BinLoader : bin
LOADER com.jme3.scene.plugins.gltf.GlbLoader : glb
LOADER com.jme3.texture.plugins.StbImageLoader : jpg, bmp, gif, png, jpeg, tga, psd, hdr
LOADER com.jme3.audio.plugins.OGGLoader : ogg
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* @deprecated use {@link StbImageLoader} instead, which supports HDR images and more formats. This loader is
* kept for backward compatibility but may be removed in future versions.
*/
@Deprecated
public class HDRLoader implements AssetLoader {

private static final Logger logger = Logger.getLogger(HDRLoader.class.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,16 @@
import java.nio.ByteBuffer;

/**
* <code>TextureManager</code> provides static methods for building a
* <code>Texture</code> object. Typically, the information supplied is the
* filename and the texture properties.
* <code>TextureManager</code> provides static methods for building a <code>Texture</code> object. Typically,
* the information supplied is the filename and the texture properties.
*
* @author Mark Powell
* @author Joshua Slack - cleaned, commented, added ability to read 16bit true color and color-mapped TGAs.
* @author Kirill Vainer - ported to jME3
* @version $Id: TGALoader.java 4131 2009-03-19 20:15:28Z blaine.dev $
* @deprecated use {@link StbImageLoader} instead
*/
@Deprecated
public final class TGALoader implements AssetLoader {

// 0 - no image data in file
Expand Down
1 change: 1 addition & 0 deletions jme3-desktop/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dependencies {
api project(':jme3-core')
api project(':jme3-plugins')
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@
import java.nio.ByteBuffer;
import javax.imageio.ImageIO;

/**
* @deprecated use {@link StbImageLoader} instead, which supports more formats and is more efficient and
* platform agnostic. This loader is kept for backward compatibility but may be removed in future
* versions.
*/
@Deprecated
public class AWTLoader implements AssetLoader {

public static final ColorModel AWT_RGBA4444 = new DirectColorModel(16,
Expand Down
2 changes: 2 additions & 0 deletions jme3-plugins/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ dependencies {
implementation "org.jmonkeyengine:drako:1.4.5-jme"
implementation project(':jme3-plugins-json')
implementation project(':jme3-plugins-json-gson')
implementation libs.stb.image

testRuntimeOnly project(':jme3-desktop')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package com.jme3.texture.plugins;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

import org.ngengine.stbimage.StbDecoder;
import org.ngengine.stbimage.StbImage;
import org.ngengine.stbimage.StbImageInfo;
import org.ngengine.stbimage.StbImageResult;

import com.jme3.asset.AssetInfo;
import com.jme3.asset.AssetKey;
import com.jme3.asset.AssetLoader;
import com.jme3.asset.TextureKey;
import com.jme3.export.binary.ByteUtils;
import com.jme3.math.FastMath;
import com.jme3.texture.Image;
import com.jme3.texture.image.ColorSpace;
import com.jme3.util.BufferUtils;

public class StbImageLoader implements AssetLoader {
private final StbImage stbImage = new StbImage(BufferUtils::createByteBuffer);

@Override
public Object load(AssetInfo assetInfo) throws IOException {
Comment thread
riccardobl marked this conversation as resolved.
AssetKey<?> key = assetInfo.getKey();
TextureKey textureKey = null;
if(key instanceof TextureKey){
textureKey = (TextureKey) key;
}

boolean flip = textureKey != null && textureKey.isFlipY();

try(InputStream is = assetInfo.openStream()) {
byte[] data = ByteUtils.getByteContent(is);
ByteBuffer buffer = BufferUtils.createByteBuffer(data);
stbImage.setConvertIphonePngToRgb(true);
stbImage.setUnpremultiplyOnLoad(true);

StbDecoder decoder = stbImage.getDecoder(buffer, flip);
StbImageInfo info = decoder.info();
int channels = info.getChannels();

int width = info.getWidth();
int height = info.getHeight();
int desiredChannels = channels;

boolean is16bit = info.is16Bit();
boolean isFloat = info.isFloat();
boolean sRGB = false;


Image.Format jmeFormat;
if (is16bit || isFloat) {
switch (channels) {
case 1:
jmeFormat = Image.Format.R16F;
desiredChannels = 1;
break;
case 2:
jmeFormat = Image.Format.RG16F;
desiredChannels = 2;
break;
case 3:
jmeFormat = Image.Format.RGB16F;
desiredChannels = 3;
break;
case 4:
jmeFormat = Image.Format.RGBA16F;
desiredChannels = 4;
break;
default:
throw new IOException("Unsupported number of channels: " + channels);

}
} else {
switch (channels) {
case 1:
jmeFormat = Image.Format.Luminance8;
desiredChannels = 1;
break;
case 2:
jmeFormat = Image.Format.Luminance8Alpha8;
desiredChannels = 2;
break;
case 3:
jmeFormat = Image.Format.RGB8;
desiredChannels = 3;
sRGB = true;
break;
case 4:
jmeFormat = Image.Format.RGBA8;
desiredChannels = 4;
sRGB = true;
break;
default:
throw new IOException("Unsupported number of channels: " + channels);
}
}
Comment thread
riccardobl marked this conversation as resolved.

StbImageResult imgData;
if(isFloat){
imgData = decoder.loadf(desiredChannels);
} else if(is16bit){
imgData = decoder.load16(desiredChannels);
} else {
imgData = decoder.load(desiredChannels);
}

ByteBuffer jmeImageBuffer = convertImageData(imgData, jmeFormat);

Image jmeImage = new Image(jmeFormat, width, height, jmeImageBuffer, sRGB ? ColorSpace.sRGB : ColorSpace.Linear);
return jmeImage;
}
}

private ByteBuffer convertImageData(StbImageResult imgData, Image.Format jmeFormat) {
int outputSize = jmeFormat.getBitsPerPixel() / 8 * imgData.getWidth() * imgData.getHeight();
ByteBuffer jmeImageBuffer = BufferUtils.createByteBuffer(outputSize);
ByteBuffer source = imgData.getData().duplicate();
source.order(imgData.getData().order());
source.position(0).limit(imgData.getDataSize());

if (!imgData.is16Bit() && !imgData.isFloat()) {
jmeImageBuffer.put(source);
jmeImageBuffer.flip();
return jmeImageBuffer;
}

int sampleCount = imgData.getWidth() * imgData.getHeight() * imgData.getRequestedChannels();
if (imgData.is16Bit()) {
for (int i = 0; i < sampleCount; i++) {
float value = (source.getShort() & 0xFFFF) / 65535f;
jmeImageBuffer.putShort(FastMath.convertFloatToHalf(value));
}
} else {
for (int i = 0; i < sampleCount; i++) {
jmeImageBuffer.putShort(FastMath.convertFloatToHalf(source.getFloat()));
}
}

jmeImageBuffer.flip();
return jmeImageBuffer;
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.