1 /* 2 * Copyright (c) 2009-2010 jMonkeyEngine 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * * Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 package com.jme3.scene.plugins.blender.textures; 33 34 import com.jme3.scene.plugins.blender.file.BlenderInputStream; 35 import com.jme3.texture.Image; 36 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 java.util.logging.Logger; 41 42 /** 43 * An image loader class. It uses three loaders (AWTLoader, TGALoader and DDSLoader) in an attempt to load the image from the given 44 * input stream. 45 * 46 * @author Marcin Roguski (Kaelthas) 47 */ 48 /*package*/ class ImageLoader extends AWTLoader { 49 private static final Logger LOGGER = Logger.getLogger(ImageLoader.class.getName()); 50 51 protected DDSLoader ddsLoader = new DDSLoader(); // DirectX image loader 52 53 /** 54 * This method loads the image from the blender file itself. It tries each loader to load the image. 55 * 56 * @param inputStream 57 * blender input stream 58 * @param startPosition 59 * position in the stream where the image data starts 60 * @param flipY 61 * if the image should be flipped (does not work with DirectX image) 62 * @return loaded image or null if it could not be loaded 63 */ loadImage(BlenderInputStream inputStream, int startPosition, boolean flipY)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); 77 } 78 79 if (result == null) { 80 LOGGER.warning("Image could not be loaded by none of available loaders!"); 81 } 82 83 return result; 84 } 85 86 /** 87 * This method loads an image of a specified type from the given input stream. 88 * 89 * @param inputStream 90 * the input stream we read the image from 91 * @param imageType 92 * the type of the image {@link ImageType} 93 * @param flipY 94 * if the image should be flipped (does not work with DirectX image) 95 * @return loaded image or null if it could not be loaded 96 */ loadImage(InputStream inputStream, ImageType imageType, boolean flipY)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.info("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.info("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.info("Unable to load image using TGA loader!"); 119 } 120 break; 121 default: 122 throw new IllegalStateException("Unknown image type: " + imageType); 123 } 124 return result; 125 } 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