Skip to content
Merged
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: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ 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"
stb-image = "org.ngengine:stb-image:2.30.5"
imagewebp = "org.ngengine:image-webp-decoder:1.3.0"
angle = { module = "org.ngengine:angle-natives", version.ref = "angle" }
saferalloc = { module = "org.ngengine:saferalloc", version.ref = "saferalloc" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,56 +58,8 @@ public Image load(byte[] data, boolean flipY) throws IOException {

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


Image.Format jmeFormat;
if (is16bit || isFloat) {
switch (channels) {
case 1:
jmeFormat = Image.Format.Luminance16F;
desiredChannels = 1;
break;
case 2:
jmeFormat = Image.Format.Luminance16FAlpha16F;
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);
}
}
Image.Format jmeFormat = selectFormat(channels, is16bit, isFloat);

StbImageResult imgData;
if (isFloat){
Expand All @@ -118,6 +70,7 @@ public Image load(byte[] data, boolean flipY) throws IOException {
imgData = decoder.load(desiredChannels);
}

boolean sRGB = (jmeFormat == Image.Format.RGB8 || jmeFormat == Image.Format.RGBA8);
ByteBuffer jmeImageBuffer = convertImageData(imgData, jmeFormat);

return new Image(jmeFormat, width, height, jmeImageBuffer, sRGB ? ColorSpace.sRGB : ColorSpace.Linear);
Expand All @@ -138,6 +91,36 @@ public Object load(AssetInfo assetInfo) throws IOException {
}
}

/**
* Maps a channel count and bit-depth to the corresponding {@link Image.Format}.
*
* @param channels number of channels (1–4)
* @param is16bit {@code true} for 16-bit-per-channel source data
* @param isFloat {@code true} for floating-point source data
* @return the matching {@link Image.Format}
* @throws IOException if {@code channels} is outside the supported range
*/
private static Image.Format selectFormat(int channels, boolean is16bit, boolean isFloat)
throws IOException {
if (is16bit || isFloat) {
switch (channels) {
case 1: return Image.Format.Luminance16F;
case 2: return Image.Format.Luminance16FAlpha16F;
case 3: return Image.Format.RGB16F;
case 4: return Image.Format.RGBA16F;
default: throw new IOException("Unsupported number of channels: " + channels);
}
} else {
switch (channels) {
case 1: return Image.Format.Luminance8;
case 2: return Image.Format.Luminance8Alpha8;
case 3: return Image.Format.RGB8;
case 4: return Image.Format.RGBA8;
default: throw new IOException("Unsupported number of channels: " + channels);
}
}
}

private ByteBuffer convertImageData(StbImageResult imgData, Image.Format jmeFormat) {
int outputSize = jmeFormat.getBitsPerPixel() / 8 * imgData.getWidth() * imgData.getHeight();
ByteBuffer jmeImageBuffer = BufferUtils.createByteBuffer(outputSize);
Expand All @@ -151,7 +134,7 @@ private ByteBuffer convertImageData(StbImageResult imgData, Image.Format jmeForm
return jmeImageBuffer;
}

int sampleCount = imgData.getWidth() * imgData.getHeight() * imgData.getRequestedChannels();
int sampleCount = imgData.getWidth() * imgData.getHeight() * imgData.getChannels();
if (imgData.is16Bit()) {
for (int i = 0; i < sampleCount; i++) {
float value = (source.getShort() & 0xFFFF) / 65535f;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
* Copyright (c) 2024 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jme3.texture.plugins;

import com.jme3.texture.Image;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.IOException;

/**
* Tests for {@link StbImageLoader}, covering formats whose channel count is
* not reported by the decoder's {@code info()} method (e.g. TGA).
*/
public class StbImageLoaderTest {

/**
* Creates a minimal, valid TGA image as a byte array.
*
* @param width image width in pixels
* @param height image height in pixels
* @param bpp bits per pixel: 24 (RGB) or 32 (RGBA)
* @return raw TGA bytes
*/
private static byte[] makeTga(int width, int height, int bpp) {
int bytesPerPixel = bpp / 8;
int pixelDataSize = width * height * bytesPerPixel;
// TGA header is 18 bytes, followed by pixel data
byte[] tga = new byte[18 + pixelDataSize];

tga[0] = 0; // ID length
tga[1] = 0; // Color map type: none
tga[2] = 2; // Image type: uncompressed true-color
// Color map spec (5 bytes): all zero
// Image spec:
tga[8] = 0; tga[9] = 0; // X origin
tga[10] = 0; tga[11] = 0; // Y origin
tga[12] = (byte) (width & 0xFF); tga[13] = (byte) ((width >> 8) & 0xFF);
tga[14] = (byte) (height & 0xFF); tga[15] = (byte) ((height >> 8) & 0xFF);
tga[16] = (byte) bpp; // Bits per pixel
tga[17] = 0x20; // Image descriptor: top-left origin

// Fill pixel data with a simple pattern (stored as BGR or BGRA in TGA)
for (int i = 0; i < width * height; i++) {
int base = 18 + i * bytesPerPixel;
tga[base] = (byte) 0xFF; // B
tga[base + 1] = (byte) 0x80; // G
tga[base + 2] = (byte) 0x40; // R
if (bpp == 32) {
tga[base + 3] = (byte) 0xFF; // A
}
}
return tga;
}

/**
* Verifies that a 24-bit (RGB) TGA image loads without throwing an exception
* and is decoded as {@link Image.Format#RGB8}.
*
* <p>This is a regression test for the bug where TGA images caused an
* {@code IOException("Unsupported number of channels: 0")} because the
* stb-image {@code TgaDecoder.info()} did not set the channel count.
*/
@Test
public void testLoad24BitTga() throws IOException {
byte[] tgaData = makeTga(2, 2, 24);
StbImageLoader loader = new StbImageLoader();
Image image = loader.load(tgaData, false);

Assertions.assertNotNull(image, "Image must not be null");
Assertions.assertEquals(Image.Format.RGB8, image.getFormat(),
"24-bit TGA should be decoded as RGB8");
Assertions.assertEquals(2, image.getWidth());
Assertions.assertEquals(2, image.getHeight());
}

/**
* Verifies that a grayscale (8-bit, 1-channel) TGA image loads correctly
* as {@link Image.Format#Luminance8}.
*/
@Test
public void testLoadGrayscaleTga() throws IOException {
// Build a 1-channel (grayscale) TGA: image type 3 = uncompressed grayscale
int width = 2, height = 2;
byte[] tga = new byte[18 + width * height];
tga[0] = 0; // ID length
tga[1] = 0; // Color map type: none
tga[2] = 3; // Image type: uncompressed grayscale
tga[12] = (byte) width;
tga[14] = (byte) height;
tga[16] = 8; // 8 bits per pixel
tga[17] = 0x20; // top-left origin
// Fill pixel data
for (int i = 0; i < width * height; i++) {
tga[18 + i] = (byte) (i * 64);
}

StbImageLoader loader = new StbImageLoader();
Image image = loader.load(tga, false);

Assertions.assertNotNull(image, "Image must not be null");
Assertions.assertEquals(Image.Format.Luminance8, image.getFormat(),
"Grayscale TGA should be decoded as Luminance8");
Assertions.assertEquals(width, image.getWidth());
Assertions.assertEquals(height, image.getHeight());
}
}
Loading