|
31 | 31 | */
|
32 | 32 | package com.jme3.scene.plugins.blender.textures;
|
33 | 33 |
|
| 34 | +import com.jme3.asset.AssetManager; |
| 35 | +import com.jme3.asset.TextureKey; |
34 | 36 | import com.jme3.scene.plugins.blender.file.BlenderInputStream;
|
35 | 37 | import com.jme3.texture.Image;
|
| 38 | +import com.jme3.texture.Texture; |
36 | 39 | 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; |
40 | 42 | import java.util.logging.Logger;
|
41 | 43 |
|
42 | 44 | /**
|
|
47 | 49 | */
|
48 | 50 | /* package */class ImageLoader extends AWTLoader {
|
49 | 51 | 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" }; |
52 | 71 |
|
53 | 72 | /**
|
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 |
55 | 75 | *
|
56 | 76 | * @param inputStream
|
57 | 77 | * blender input stream
|
|
60 | 80 | * @param flipY
|
61 | 81 | * if the image should be flipped (does not work with DirectX image)
|
62 | 82 | * @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 |
63 | 85 | */
|
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(); |
77 | 93 | }
|
78 |
| - |
79 |
| - if (result == null) { |
80 |
| - LOGGER.warning("Image could not be loaded by none of available loaders!"); |
81 |
| - } |
82 |
| - |
83 |
| - return result; |
84 | 94 | }
|
85 |
| - |
| 95 | + |
86 | 96 | /**
|
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 |
88 | 99 | *
|
89 | 100 | * @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 |
93 | 104 | * @param flipY
|
94 | 105 | * 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 |
96 | 107 | */
|
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."); |
123 | 132 | }
|
| 133 | + |
124 | 134 | return result;
|
125 | 135 | }
|
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 |
| - } |
135 | 136 | }
|
0 commit comments