forked from jMonkeyEngine/jmonkeyengine
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathStbImageLoader.java
More file actions
146 lines (126 loc) · 5.25 KB
/
StbImageLoader.java
File metadata and controls
146 lines (126 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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 {
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 = ByteBuffer.wrap(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.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);
}
}
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;
}
}