Blender ImageLoader: Support Loading of HDR Files and many other

empirephoenix-patch-1
MeFisto94 7 years ago committed by Rémy Bouquet
parent fc8135412f
commit 3cd96b2053
  1. 133
      jme3-blender/src/main/java/com/jme3/scene/plugins/blender/textures/ImageLoader.java

@ -31,12 +31,14 @@
*/ */
package com.jme3.scene.plugins.blender.textures; package com.jme3.scene.plugins.blender.textures;
import com.jme3.asset.AssetManager;
import com.jme3.asset.TextureKey;
import com.jme3.scene.plugins.blender.file.BlenderInputStream; import com.jme3.scene.plugins.blender.file.BlenderInputStream;
import com.jme3.texture.Image; import com.jme3.texture.Image;
import com.jme3.texture.Texture;
import com.jme3.texture.plugins.AWTLoader; import com.jme3.texture.plugins.AWTLoader;
import com.jme3.texture.plugins.DDSLoader; import com.jme3.texture.plugins.HDRLoader;
import com.jme3.texture.plugins.TGALoader; import java.util.logging.Level;
import java.io.InputStream;
import java.util.logging.Logger; import java.util.logging.Logger;
/** /**
@ -47,11 +49,29 @@ import java.util.logging.Logger;
*/ */
/* package */class ImageLoader extends AWTLoader { /* package */class ImageLoader extends AWTLoader {
private static final Logger LOGGER = Logger.getLogger(ImageLoader.class.getName()); private static final Logger LOGGER = Logger.getLogger(ImageLoader.class.getName());
private static final Logger hdrLogger = Logger.getLogger(HDRLoader.class.getName()); // Used to silence HDR Errors
protected DDSLoader ddsLoader = new DDSLoader(); // DirectX image loader
/**
* List of Blender-Supported Texture Extensions (we have to guess them, so
* the AssetLoader can find them. Not good, but better than nothing.
* Source: https://docs.blender.org/manual/en/dev/data_system/files/media/image_formats.html
*/
private static final String[] extensions = new String[]
{ /* Windows Bitmap */".bmp",
/* Iris */ ".sgi", ".rgb", ".bw",
/* PNG */ ".png",
/* JPEG */ ".jpg", ".jpeg",
/* JPEG 2000 */ ".jp2", ".j2c",
/* Targa */".tga",
/* Cineon & DPX */".cin", ".dpx",
/* OpenEXR */ ".exr",
/* Radiance HDR */ ".hdr",
/* TIFF */ ".tif", ".tiff",
/* DDS (Direct X) */ ".dds" };
/** /**
* This method loads the image from the blender file itself. It tries each loader to load the image. * This method loads a image which is packed into the blender file.
* It makes use of all the registered AssetLoaders
* *
* @param inputStream * @param inputStream
* blender input stream * blender input stream
@ -60,76 +80,57 @@ import java.util.logging.Logger;
* @param flipY * @param flipY
* if the image should be flipped (does not work with DirectX image) * if the image should be flipped (does not work with DirectX image)
* @return loaded image or null if it could not be loaded * @return loaded image or null if it could not be loaded
* @deprecated This method has only been left in for API compability.
* Use loadTexture instead
*/ */
public Image loadImage(BlenderInputStream inputStream, int startPosition, boolean flipY) { public Image loadImage(AssetManager assetManager, BlenderInputStream inputStream, int startPosition, boolean flipY) {
// loading using AWT loader Texture tex = loadTexture(assetManager, inputStream, startPosition, flipY);
inputStream.setPosition(startPosition);
Image result = this.loadImage(inputStream, ImageType.AWT, flipY); if (tex == null) {
// loading using TGA loader return null;
if (result == null) { } else {
inputStream.setPosition(startPosition); return tex.getImage();
result = this.loadImage(inputStream, ImageType.TGA, flipY);
}
// loading using DDS loader
if (result == null) {
inputStream.setPosition(startPosition);
result = this.loadImage(inputStream, ImageType.DDS, flipY);
} }
if (result == null) {
LOGGER.warning("Image could not be loaded by none of available loaders!");
}
return result;
} }
/** /**
* This method loads an image of a specified type from the given input stream. * This method loads a texture which is packed into the blender file.
* It makes use of all the registered AssetLoaders
* *
* @param inputStream * @param inputStream
* the input stream we read the image from * blender input stream
* @param imageType * @param startPosition
* the type of the image {@link ImageType} * position in the stream where the image data starts
* @param flipY * @param flipY
* if the image should be flipped (does not work with DirectX image) * if the image should be flipped (does not work with DirectX image)
* @return loaded image or null if it could not be loaded * @return loaded texture or null if it could not be loaded
*/ */
public Image loadImage(InputStream inputStream, ImageType imageType, boolean flipY) { public Texture loadTexture(AssetManager assetManager, BlenderInputStream inputStream, int startPosition, boolean flipY) {
Image result = null; inputStream.setPosition(startPosition);
switch (imageType) { TextureKey tKey;
case AWT: Texture result = null;
try {
result = this.load(inputStream, flipY); hdrLogger.setLevel(Level.SEVERE); // When we bruteforce try HDR on a non hdr file, it prints unreadable chars
} catch (Exception e) {
LOGGER.warning("Unable to load image using AWT loader!"); for (String ext: extensions) {
} tKey = new TextureKey("dummy" + ext, flipY);
break; try {
case DDS: result = assetManager.loadAssetFromStream(tKey, inputStream);
try { } catch (Exception e) {
result = ddsLoader.load(inputStream); continue;
} catch (Exception e) { }
LOGGER.warning("Unable to load image using DDS loader!");
} if (result != null) {
break; break; // Could locate a possible asset
case TGA: }
try { }
result = TGALoader.load(inputStream, flipY);
} catch (Exception e) { if (result == null) {
LOGGER.warning("Unable to load image using TGA loader!"); LOGGER.warning("Texture could not be loaded by any of the available loaders!\n"
} + "Since the file has been packed into the blender file, there is no"
break; + "way for us to tell you which texture it was.");
default:
throw new IllegalStateException("Unknown image type: " + imageType);
} }
return result; return result;
} }
/**
* Image types that can be loaded. AWT: png, jpg, jped or bmp TGA: tga DDS: DirectX image files
*
* @author Marcin Roguski (Kaelthas)
*/
private static enum ImageType {
AWT, TGA, DDS;
}
} }

Loading…
Cancel
Save