Coverage Report - org.jtheque.utils.ui.ImageUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ImageUtils
0%
0/69
0%
0/26
2.692
 
 1  
 package org.jtheque.utils.ui;
 2  
 
 3  
 /*
 4  
  * This file is part of JTheque.
 5  
  *
 6  
  * JTheque is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License.
 9  
  *
 10  
  * JTheque is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
  * GNU General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 17  
  */
 18  
 
 19  
 import org.jtheque.utils.io.FileUtils;
 20  
 import org.slf4j.LoggerFactory;
 21  
 
 22  
 import javax.imageio.ImageIO;
 23  
 import java.awt.AlphaComposite;
 24  
 import java.awt.Color;
 25  
 import java.awt.GradientPaint;
 26  
 import java.awt.Graphics;
 27  
 import java.awt.Graphics2D;
 28  
 import java.awt.GraphicsConfiguration;
 29  
 import java.awt.GraphicsEnvironment;
 30  
 import java.awt.Image;
 31  
 import java.awt.Paint;
 32  
 import java.awt.RenderingHints;
 33  
 import java.awt.image.BufferedImage;
 34  
 import java.io.File;
 35  
 import java.io.FileNotFoundException;
 36  
 import java.io.IOException;
 37  
 import java.io.InputStream;
 38  
 
 39  
 /**
 40  
  * Utility class for image processing.
 41  
  *
 42  
  * @author Baptiste Wicht
 43  
  */
 44  
 public final class ImageUtils {
 45  
     private static final float ALPHA_REFLECTION = 0.75f;
 46  
 
 47  
     /**
 48  
      * Construct a new ImageUtils. This constructor is private, all the methods are static.
 49  
      */
 50  
     private ImageUtils() {
 51  0
         super();
 52  0
     }
 53  
 
 54  
     /**
 55  
      * Create a thumbnail of an image.
 56  
      *
 57  
      * @param image              The source image.
 58  
      * @param requestedThumbSize The requested size.
 59  
      * 
 60  
      * @return The thumbnail
 61  
      */
 62  
     public static BufferedImage createThumbnail(BufferedImage image, int requestedThumbSize) {
 63  0
         float ratio = (float) image.getWidth() / (float) image.getHeight();
 64  0
         int width = image.getWidth();
 65  0
         boolean divide = requestedThumbSize < width;
 66  
 
 67  0
         BufferedImage thumb = image;
 68  
 
 69  
         do {
 70  0
             if (divide) {
 71  0
                 width /= 2;
 72  
 
 73  0
                 if (width < requestedThumbSize) {
 74  0
                     width = requestedThumbSize;
 75  
                 }
 76  
             } else {
 77  0
                 width *= 2;
 78  
 
 79  0
                 if (width > requestedThumbSize) {
 80  0
                     width = requestedThumbSize;
 81  
                 }
 82  
             }
 83  
 
 84  0
             BufferedImage temp = createCompatibleImage(width, (int) (width / ratio), image.getTransparency());
 85  
 
 86  0
             Graphics2D g2 = temp.createGraphics();
 87  0
             g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
 88  0
             g2.drawImage(thumb, 0, 0, temp.getWidth(), temp.getHeight(), null);
 89  0
             g2.dispose();
 90  
 
 91  0
             thumb = temp;
 92  0
         } while (width != requestedThumbSize);
 93  
 
 94  0
         return thumb;
 95  
     }
 96  
 
 97  
     /**
 98  
      * Read an image from a file.
 99  
      *
 100  
      * @param file The file to read from.
 101  
      *
 102  
      * @return The buffered image from the file.
 103  
      */
 104  
     public static BufferedImage read(File file){
 105  
         try {
 106  0
             return ImageIO.read(file);
 107  0
         } catch (IOException e) {
 108  0
             LoggerFactory.getLogger(ImageUtils.class).error("Unable to read image", e);
 109  
         }
 110  
 
 111  0
         return null;
 112  
     }
 113  
 
 114  
     /**
 115  
      * Open an image from an input stream.
 116  
      *
 117  
      * @param stream The stream to read from.
 118  
      *
 119  
      * @return The buffered image from the stream.
 120  
      */
 121  
     public static BufferedImage read(InputStream stream) {
 122  
         try {
 123  0
             return ImageIO.read(stream);
 124  0
         } catch (IOException e) {
 125  0
             LoggerFactory.getLogger(ImageUtils.class).error("Unable to read image", e);
 126  
         }
 127  
 
 128  0
         return null;
 129  
     }
 130  
 
 131  
     /**
 132  
      * Create a compatible image.In headless mode, this method will return a simple BufferedImage of type ARGB.
 133  
      *
 134  
      * @param width  The width.
 135  
      * @param height The height.
 136  
      * 
 137  
      * @return A compatible image.
 138  
      *
 139  
      * @see ImageUtils#isHeadless()
 140  
      */
 141  
     public static Image createCompatibleImage(int width, int height) {
 142  0
         if(isHeadless()){
 143  0
             return new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
 144  
         }
 145  
 
 146  0
         return getGraphicsConfiguration().createCompatibleImage(width, height);
 147  
     }
 148  
 
 149  
     /**
 150  
      * Create a compatible image. In headless mode, this method will return a simple BufferedImage. 
 151  
      *
 152  
      * @param width  The width.
 153  
      * @param height The height.
 154  
      * @param type   The type of the image.
 155  
      *
 156  
      * @return A compatible image.
 157  
      *
 158  
      * @see ImageUtils#isHeadless()
 159  
      */
 160  
     public static BufferedImage createCompatibleImage(int width, int height, int type) {
 161  0
         if(isHeadless()){
 162  0
             return new BufferedImage(width, height, type);
 163  
         }
 164  
 
 165  0
         return getGraphicsConfiguration().createCompatibleImage(width, height, type);
 166  
     }
 167  
 
 168  
     /**
 169  
      * Return the graphics configuration of the system.
 170  
      *
 171  
      * @return The graphics configuration of the environment or null if we are in headless mode.
 172  
      *
 173  
      * @see ImageUtils#isHeadless()
 174  
      */
 175  
     private static GraphicsConfiguration getGraphicsConfiguration() {
 176  0
         if(isHeadless()){
 177  0
             return null;
 178  
         }
 179  
 
 180  0
         return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
 181  
     }
 182  
 
 183  
     /**
 184  
      * Create a compatible image from an existing image. In headless mode, this method will not affect the image. 
 185  
      *
 186  
      * @param image The image to make compatible.
 187  
      *
 188  
      * @return A compatible image filled with the base image.
 189  
      */
 190  
     public static BufferedImage createCompatibleImage(BufferedImage image) {
 191  0
         GraphicsConfiguration gc = getGraphicsConfiguration();
 192  
 
 193  0
         if (image.getColorModel().equals(gc.getColorModel()) || isHeadless()) {
 194  0
             return image;
 195  
         } else {
 196  0
             BufferedImage compatibleImage = createCompatibleImage(image.getWidth(), image.getHeight(), image.getTransparency());
 197  
 
 198  0
             Graphics g = compatibleImage.getGraphics();
 199  0
             g.drawImage(image, 0, 0, null);
 200  0
             g.dispose();
 201  
 
 202  0
             return compatibleImage;
 203  
         }
 204  
     }
 205  
 
 206  
     /**
 207  
      * Indicate if we are in a headless mode or normal.
 208  
      *
 209  
      * @return true if we are in headless mode else false. 
 210  
      */
 211  
     public static boolean isHeadless() {
 212  0
         return GraphicsEnvironment.isHeadless() || GraphicsEnvironment.getLocalGraphicsEnvironment().isHeadlessInstance();
 213  
     }
 214  
 
 215  
     /**
 216  
      * Create a reflection of the image.
 217  
      *
 218  
      * @param image The image to reflect.
 219  
      * 
 220  
      * @return The reflection image.
 221  
      */
 222  
     public static BufferedImage createReflection(BufferedImage image) {
 223  0
         int height = image.getHeight();
 224  
 
 225  0
         BufferedImage result = new BufferedImage(image.getWidth(), height * 2, BufferedImage.TYPE_INT_ARGB);
 226  
 
 227  0
         Graphics2D g2 = result.createGraphics();
 228  
 
 229  
         // Paints original image
 230  0
         g2.drawImage(image, 0, 0, null);
 231  
 
 232  
         // Paints mirrored image
 233  0
         g2.scale(1.0, -1.0);
 234  0
         g2.drawImage(image, 0, -height - height, null);
 235  0
         g2.scale(1.0, -1.0);
 236  
 
 237  
         // Move to the origin of the clone
 238  0
         g2.translate(0, height);
 239  
 
 240  
         // Creates the alpha mask
 241  0
         Paint mask = new GradientPaint(0, 0, new Color(1.0f, 1.0f, 1.0f, ALPHA_REFLECTION),
 242  
                 0, height / 2, new Color(1.0f, 1.0f, 1.0f, 0.0f));
 243  
 
 244  0
         g2.setPaint(mask);
 245  
 
 246  
         // Sets the alpha composite
 247  0
         g2.setComposite(AlphaComposite.DstIn);
 248  
 
 249  
         // Paints the mask
 250  0
         g2.fillRect(0, 0, image.getWidth(), height);
 251  
 
 252  0
         g2.dispose();
 253  
 
 254  0
         return result;
 255  
     }
 256  
 
 257  
     /**
 258  
      * Open a image from the classpath at the specified path.
 259  
      *
 260  
      * @param path The path to the image.
 261  
      * @return The image.
 262  
      */
 263  
     public static BufferedImage openCompatibleImageFromClassPath(String path) {
 264  0
         return openCompatibleImage(ImageUtils.class.getResourceAsStream(path));
 265  
     }
 266  
 
 267  
     /**
 268  
      * Open an image from the file system at the specified path.
 269  
      *
 270  
      * @param path The path to the image.
 271  
      * @return The image or null if the file doesn't exist.
 272  
      */
 273  
     public static BufferedImage openCompatibleImageFromFileSystem(String path) {
 274  0
         if(isHeadless()){
 275  0
             return read(new File(path));
 276  
         }
 277  
 
 278  
         try {
 279  0
             return openCompatibleImage(FileUtils.asInputStream(path));
 280  0
         } catch (FileNotFoundException e) {
 281  0
             LoggerFactory.getLogger(ImageUtils.class).error("Unable to open file", e);
 282  
 
 283  0
             return null;
 284  
         }
 285  
     }
 286  
 
 287  
     /**
 288  
      * Open a compatible image form an input stream.
 289  
      *
 290  
      * @param stream The stream to use to open the image.
 291  
      * @return The image.
 292  
      */
 293  
     public static BufferedImage openCompatibleImage(InputStream stream) {
 294  0
         return createCompatibleImage(read(stream));
 295  
     }
 296  
 }