Skip to content

Commit b00ba5b

Browse files
Copilotriccardobl
andauthored
Refactor: extract selectFormat() helper, remove code duplication in StbImageLoader
Agent-Logs-Url: https://github.com/jMonkeyEngine/jmonkeyengine/sessions/06d3e2b9-a0ea-4a94-b058-73910f64df28 Co-authored-by: riccardobl <4943530+riccardobl@users.noreply.github.com>
1 parent c6a6408 commit b00ba5b

1 file changed

Lines changed: 34 additions & 85 deletions

File tree

jme3-plugins/src/main/java/com/jme3/texture/plugins/StbImageLoader.java

Lines changed: 34 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -54,62 +54,17 @@ public Image load(byte[] data, boolean flipY) throws IOException {
5454

5555
int width = info.getWidth();
5656
int height = info.getHeight();
57+
// When channels == 0 the decoder will choose its natural output count.
5758
int desiredChannels = channels;
5859

5960
boolean is16bit = info.is16Bit();
6061
boolean isFloat = info.isFloat();
61-
boolean sRGB = false;
62-
6362

6463
// Some formats (e.g., TGA) don't report channel count via info().
6564
// When channels == 0, load first and determine the format afterwards.
6665
Image.Format jmeFormat = null;
6766
if (channels != 0) {
68-
if (is16bit || isFloat) {
69-
switch (channels) {
70-
case 1:
71-
jmeFormat = Image.Format.Luminance16F;
72-
desiredChannels = 1;
73-
break;
74-
case 2:
75-
jmeFormat = Image.Format.Luminance16FAlpha16F;
76-
desiredChannels = 2;
77-
break;
78-
case 3:
79-
jmeFormat = Image.Format.RGB16F;
80-
desiredChannels = 3;
81-
break;
82-
case 4:
83-
jmeFormat = Image.Format.RGBA16F;
84-
desiredChannels = 4;
85-
break;
86-
default:
87-
throw new IOException("Unsupported number of channels: " + channels);
88-
}
89-
} else {
90-
switch (channels) {
91-
case 1:
92-
jmeFormat = Image.Format.Luminance8;
93-
desiredChannels = 1;
94-
break;
95-
case 2:
96-
jmeFormat = Image.Format.Luminance8Alpha8;
97-
desiredChannels = 2;
98-
break;
99-
case 3:
100-
jmeFormat = Image.Format.RGB8;
101-
desiredChannels = 3;
102-
sRGB = true;
103-
break;
104-
case 4:
105-
jmeFormat = Image.Format.RGBA8;
106-
desiredChannels = 4;
107-
sRGB = true;
108-
break;
109-
default:
110-
throw new IOException("Unsupported number of channels: " + channels);
111-
}
112-
}
67+
jmeFormat = selectFormat(channels, is16bit, isFloat);
11368
}
11469

11570
StbImageResult imgData;
@@ -124,46 +79,10 @@ public Image load(byte[] data, boolean flipY) throws IOException {
12479
if (channels == 0) {
12580
// Channel count was not available from info(); derive the format from
12681
// the number of channels actually produced by the decoder.
127-
int actualChannels = imgData.getChannels();
128-
if (is16bit || isFloat) {
129-
switch (actualChannels) {
130-
case 1:
131-
jmeFormat = Image.Format.Luminance16F;
132-
break;
133-
case 2:
134-
jmeFormat = Image.Format.Luminance16FAlpha16F;
135-
break;
136-
case 3:
137-
jmeFormat = Image.Format.RGB16F;
138-
break;
139-
case 4:
140-
jmeFormat = Image.Format.RGBA16F;
141-
break;
142-
default:
143-
throw new IOException("Unsupported number of channels: " + actualChannels);
144-
}
145-
} else {
146-
switch (actualChannels) {
147-
case 1:
148-
jmeFormat = Image.Format.Luminance8;
149-
break;
150-
case 2:
151-
jmeFormat = Image.Format.Luminance8Alpha8;
152-
break;
153-
case 3:
154-
jmeFormat = Image.Format.RGB8;
155-
sRGB = true;
156-
break;
157-
case 4:
158-
jmeFormat = Image.Format.RGBA8;
159-
sRGB = true;
160-
break;
161-
default:
162-
throw new IOException("Unsupported number of channels: " + actualChannels);
163-
}
164-
}
82+
jmeFormat = selectFormat(imgData.getChannels(), is16bit, isFloat);
16583
}
16684

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

16988
return new Image(jmeFormat, width, height, jmeImageBuffer, sRGB ? ColorSpace.sRGB : ColorSpace.Linear);
@@ -184,6 +103,36 @@ public Object load(AssetInfo assetInfo) throws IOException {
184103
}
185104
}
186105

106+
/**
107+
* Maps a channel count and bit-depth to the corresponding {@link Image.Format}.
108+
*
109+
* @param channels number of channels (1–4)
110+
* @param is16bit {@code true} for 16-bit-per-channel source data
111+
* @param isFloat {@code true} for floating-point source data
112+
* @return the matching {@link Image.Format}
113+
* @throws IOException if {@code channels} is outside the supported range
114+
*/
115+
private static Image.Format selectFormat(int channels, boolean is16bit, boolean isFloat)
116+
throws IOException {
117+
if (is16bit || isFloat) {
118+
switch (channels) {
119+
case 1: return Image.Format.Luminance16F;
120+
case 2: return Image.Format.Luminance16FAlpha16F;
121+
case 3: return Image.Format.RGB16F;
122+
case 4: return Image.Format.RGBA16F;
123+
default: throw new IOException("Unsupported number of channels: " + channels);
124+
}
125+
} else {
126+
switch (channels) {
127+
case 1: return Image.Format.Luminance8;
128+
case 2: return Image.Format.Luminance8Alpha8;
129+
case 3: return Image.Format.RGB8;
130+
case 4: return Image.Format.RGBA8;
131+
default: throw new IOException("Unsupported number of channels: " + channels);
132+
}
133+
}
134+
}
135+
187136
private ByteBuffer convertImageData(StbImageResult imgData, Image.Format jmeFormat) {
188137
int outputSize = jmeFormat.getBitsPerPixel() / 8 * imgData.getWidth() * imgData.getHeight();
189138
ByteBuffer jmeImageBuffer = BufferUtils.createByteBuffer(outputSize);

0 commit comments

Comments
 (0)