Skip to content

Commit 3cd96b2

Browse files
MeFisto94Nehon
authored andcommitted
Blender ImageLoader: Support Loading of HDR Files and many other
1 parent fc81354 commit 3cd96b2

File tree

1 file changed

+67
-66
lines changed
  • jme3-blender/src/main/java/com/jme3/scene/plugins/blender/textures

1 file changed

+67
-66
lines changed

jme3-blender/src/main/java/com/jme3/scene/plugins/blender/textures/ImageLoader.java

+67-66
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@
3131
*/
3232
package com.jme3.scene.plugins.blender.textures;
3333

34+
import com.jme3.asset.AssetManager;
35+
import com.jme3.asset.TextureKey;
3436
import com.jme3.scene.plugins.blender.file.BlenderInputStream;
3537
import com.jme3.texture.Image;
38+
import com.jme3.texture.Texture;
3639
import com.jme3.texture.plugins.AWTLoader;
37-
import com.jme3.texture.plugins.DDSLoader;
38-
import com.jme3.texture.plugins.TGALoader;
39-
import java.io.InputStream;
40+
import com.jme3.texture.plugins.HDRLoader;
41+
import java.util.logging.Level;
4042
import java.util.logging.Logger;
4143

4244
/**
@@ -47,11 +49,29 @@
4749
*/
4850
/* package */class ImageLoader extends AWTLoader {
4951
private static final Logger LOGGER = Logger.getLogger(ImageLoader.class.getName());
50-
51-
protected DDSLoader ddsLoader = new DDSLoader(); // DirectX image loader
52+
private static final Logger hdrLogger = Logger.getLogger(HDRLoader.class.getName()); // Used to silence HDR Errors
53+
54+
/**
55+
* List of Blender-Supported Texture Extensions (we have to guess them, so
56+
* the AssetLoader can find them. Not good, but better than nothing.
57+
* Source: https://docs.blender.org/manual/en/dev/data_system/files/media/image_formats.html
58+
*/
59+
private static final String[] extensions = new String[]
60+
{ /* Windows Bitmap */".bmp",
61+
/* Iris */ ".sgi", ".rgb", ".bw",
62+
/* PNG */ ".png",
63+
/* JPEG */ ".jpg", ".jpeg",
64+
/* JPEG 2000 */ ".jp2", ".j2c",
65+
/* Targa */".tga",
66+
/* Cineon & DPX */".cin", ".dpx",
67+
/* OpenEXR */ ".exr",
68+
/* Radiance HDR */ ".hdr",
69+
/* TIFF */ ".tif", ".tiff",
70+
/* DDS (Direct X) */ ".dds" };
5271

5372
/**
54-
* This method loads the image from the blender file itself. It tries each loader to load the image.
73+
* This method loads a image which is packed into the blender file.
74+
* It makes use of all the registered AssetLoaders
5575
*
5676
* @param inputStream
5777
* blender input stream
@@ -60,76 +80,57 @@
6080
* @param flipY
6181
* if the image should be flipped (does not work with DirectX image)
6282
* @return loaded image or null if it could not be loaded
83+
* @deprecated This method has only been left in for API compability.
84+
* Use loadTexture instead
6385
*/
64-
public Image loadImage(BlenderInputStream inputStream, int startPosition, boolean flipY) {
65-
// loading using AWT loader
66-
inputStream.setPosition(startPosition);
67-
Image result = this.loadImage(inputStream, ImageType.AWT, flipY);
68-
// loading using TGA loader
69-
if (result == null) {
70-
inputStream.setPosition(startPosition);
71-
result = this.loadImage(inputStream, ImageType.TGA, flipY);
72-
}
73-
// loading using DDS loader
74-
if (result == null) {
75-
inputStream.setPosition(startPosition);
76-
result = this.loadImage(inputStream, ImageType.DDS, flipY);
86+
public Image loadImage(AssetManager assetManager, BlenderInputStream inputStream, int startPosition, boolean flipY) {
87+
Texture tex = loadTexture(assetManager, inputStream, startPosition, flipY);
88+
89+
if (tex == null) {
90+
return null;
91+
} else {
92+
return tex.getImage();
7793
}
78-
79-
if (result == null) {
80-
LOGGER.warning("Image could not be loaded by none of available loaders!");
81-
}
82-
83-
return result;
8494
}
85-
95+
8696
/**
87-
* This method loads an image of a specified type from the given input stream.
97+
* This method loads a texture which is packed into the blender file.
98+
* It makes use of all the registered AssetLoaders
8899
*
89100
* @param inputStream
90-
* the input stream we read the image from
91-
* @param imageType
92-
* the type of the image {@link ImageType}
101+
* blender input stream
102+
* @param startPosition
103+
* position in the stream where the image data starts
93104
* @param flipY
94105
* if the image should be flipped (does not work with DirectX image)
95-
* @return loaded image or null if it could not be loaded
106+
* @return loaded texture or null if it could not be loaded
96107
*/
97-
public Image loadImage(InputStream inputStream, ImageType imageType, boolean flipY) {
98-
Image result = null;
99-
switch (imageType) {
100-
case AWT:
101-
try {
102-
result = this.load(inputStream, flipY);
103-
} catch (Exception e) {
104-
LOGGER.warning("Unable to load image using AWT loader!");
105-
}
106-
break;
107-
case DDS:
108-
try {
109-
result = ddsLoader.load(inputStream);
110-
} catch (Exception e) {
111-
LOGGER.warning("Unable to load image using DDS loader!");
112-
}
113-
break;
114-
case TGA:
115-
try {
116-
result = TGALoader.load(inputStream, flipY);
117-
} catch (Exception e) {
118-
LOGGER.warning("Unable to load image using TGA loader!");
119-
}
120-
break;
121-
default:
122-
throw new IllegalStateException("Unknown image type: " + imageType);
108+
public Texture loadTexture(AssetManager assetManager, BlenderInputStream inputStream, int startPosition, boolean flipY) {
109+
inputStream.setPosition(startPosition);
110+
TextureKey tKey;
111+
Texture result = null;
112+
113+
hdrLogger.setLevel(Level.SEVERE); // When we bruteforce try HDR on a non hdr file, it prints unreadable chars
114+
115+
for (String ext: extensions) {
116+
tKey = new TextureKey("dummy" + ext, flipY);
117+
try {
118+
result = assetManager.loadAssetFromStream(tKey, inputStream);
119+
} catch (Exception e) {
120+
continue;
121+
}
122+
123+
if (result != null) {
124+
break; // Could locate a possible asset
125+
}
126+
}
127+
128+
if (result == null) {
129+
LOGGER.warning("Texture could not be loaded by any of the available loaders!\n"
130+
+ "Since the file has been packed into the blender file, there is no"
131+
+ "way for us to tell you which texture it was.");
123132
}
133+
124134
return result;
125135
}
126-
127-
/**
128-
* Image types that can be loaded. AWT: png, jpg, jped or bmp TGA: tga DDS: DirectX image files
129-
*
130-
* @author Marcin Roguski (Kaelthas)
131-
*/
132-
private static enum ImageType {
133-
AWT, TGA, DDS;
134-
}
135136
}

0 commit comments

Comments
 (0)