Advertisement
Guest User

Java Image Utils

a guest
May 10th, 2014
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.83 KB | None | 0 0
  1. package com.deadendgine.utils;
  2.  
  3. import java.awt.AlphaComposite;
  4. import java.awt.Color;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.GraphicsConfiguration;
  8. import java.awt.GraphicsDevice;
  9. import java.awt.GraphicsEnvironment;
  10. import java.awt.RenderingHints;
  11. import java.awt.Transparency;
  12. import java.awt.geom.AffineTransform;
  13. import java.awt.image.BufferedImage;
  14. import java.awt.image.VolatileImage;
  15. import java.io.File;
  16.  
  17. import javax.imageio.ImageIO;
  18.  
  19. /**
  20.  *
  21.  * @author Troy
  22.  * @version 1.0
  23.  *
  24.  */
  25. public class ImageUtils {
  26.     private static final ImageUtils instance = new ImageUtils();
  27.     public static GraphicsEnvironment graphicsEnviroment = GraphicsEnvironment.getLocalGraphicsEnvironment();
  28.     public static GraphicsDevice graphicsDevice = graphicsEnviroment.getDefaultScreenDevice();
  29.     public static GraphicsConfiguration graphicsConfiguration = graphicsDevice.getDefaultConfiguration();
  30.     private static AffineTransform affineTransform = new AffineTransform();
  31.    
  32.     private static BufferedImage bimg;
  33.     private static BufferedImage cimg;
  34.     private static BufferedImage imgs[];
  35.     private static BufferedImage imgss[][];
  36.     private static VolatileImage rt;
  37.     private static Graphics g;
  38.     private static Graphics2D g2;
  39.     private static int w;  
  40.     private static int h;
  41.     private static int num;
  42.     private static int imageType;
  43.    
  44.     /**
  45.      * Load a image.
  46.      *
  47.      * Note: Use loadCompatibleImage where possible.
  48.      *
  49.      * @param ref
  50.      * @return BufferedImage
  51.      */
  52.     public static BufferedImage loadBufferedImage(String ref){
  53.         bimg = null;
  54.        
  55.         try{
  56.             if(ref.startsWith("res"))
  57.                 bimg = ImageIO.read(instance.getClass().getClassLoader().getResource(ref));
  58.             else
  59.                 bimg = ImageIO.read(new File(ref));
  60.         }catch(Exception e){e.printStackTrace();}
  61.        
  62.         return bimg;
  63.     }
  64.    
  65.     /**
  66.      * Load a image from resources and save it to a compatible
  67.      * image for best performance from the graphics card.
  68.      *
  69.      * @param ref
  70.      * @return BufferedImage
  71.      */
  72.     public static BufferedImage loadCompatibleBufferedImage(String ref){
  73.         bimg = null;
  74.         cimg = null;
  75.         try{
  76.             if(ref.startsWith("res"))
  77.                 bimg = ImageIO.read(instance.getClass().getClassLoader().getResource(ref));
  78.             else
  79.                 bimg = ImageIO.read(new File(ref));
  80.            
  81.             cimg = getCompatibleBufferedImage(bimg.getWidth(), bimg.getHeight());
  82.            
  83.             g = cimg.getGraphics();
  84.             g.drawImage(bimg, 0, 0, null);
  85.             g.dispose();
  86.         }catch(Exception e){e.printStackTrace();}
  87.        
  88.         return cimg;
  89.     }
  90.    
  91.     /**
  92.      * Load a image from resources and save it to a compatible
  93.      * image for best performance from the graphics card.
  94.      *
  95.      * @param ref
  96.      * @return VolatileImage
  97.      */
  98.     public static VolatileImage loadCompatibleVolatileImage(String ref){
  99.         bimg = null;
  100.         rt = null;
  101.        
  102.         try{
  103.             bimg = ImageIO.read(instance.getClass().getClassLoader().getResource(ref));
  104.             rt = getCompatibleVolatileImage(bimg.getWidth(), bimg.getHeight());
  105.            
  106.             g = rt.getGraphics();
  107.             g.drawImage(bimg, 0, 0, null);
  108.             g.dispose();
  109.         }catch(Exception e){e.printStackTrace();}
  110.        
  111.         return rt;
  112.     }
  113.    
  114.     /**
  115.      * This will create a compatible image to be used
  116.      * by the graphics card.
  117.      *
  118.      * @param width
  119.      * @param height
  120.      * @return BufferedImage
  121.      */
  122.     public static BufferedImage getCompatibleBufferedImage(int width, int height){
  123.         return graphicsConfiguration.createCompatibleImage(width, height, Transparency.TRANSLUCENT);
  124.     }
  125.    
  126.     /**
  127.      * This will create a compatible Volatile image to be used
  128.      * by the graphics card.
  129.      *
  130.      * @param width
  131.      * @param height
  132.      * @return BufferedImage
  133.      */
  134.     public static VolatileImage getCompatibleVolatileImage(int width, int height){
  135.         return graphicsConfiguration.createCompatibleVolatileImage(width, height, Transparency.TRANSLUCENT);
  136.     }
  137.    
  138.     public static BufferedImage getSubImage(BufferedImage img, int x, int y){
  139.         img.getSubimage(x * 32 + x, y * 32 + y, 32, 32);
  140.         return null;
  141.     }
  142.    
  143.     /**
  144.      * Load a image array.
  145.      *
  146.      * @param img
  147.      * @param cols
  148.      * @param rows
  149.      * @return BufferedImage[]
  150.      */
  151.     public static BufferedImage[] splitImage(BufferedImage img, int cols, int rows) {  
  152.         w = img.getWidth()/cols;
  153.         h = img.getHeight()/rows;
  154.         num = 0;
  155.         imageType = img.getType();
  156.         if(imageType == 0) imageType = 5;
  157.         imgs = new BufferedImage[w*h];
  158.        
  159.         for(int y = 0; y < rows; y++) {
  160.             for(int x = 0; x < cols; x++) {
  161.                 imgs[num] = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
  162.                 g2 = imgs[num].createGraphics();
  163.                 g2.setComposite(AlphaComposite.Src);  
  164.                 g2.drawImage(img, 0, 0, w, h, w*x, h*y, w*x+w, h*y+h, null);
  165.                 g2.dispose();
  166.                 num++;
  167.             }
  168.         }
  169.        
  170.         return imgs;
  171.     }
  172.    
  173.     /**
  174.      * Load a image array.
  175.      *
  176.      * @param img
  177.      * @param cols
  178.      * @param rows
  179.      * @return BufferedImage[]
  180.      */
  181.     public static BufferedImage[][] splitImage2D(BufferedImage img, int cols, int rows) {  
  182.         w = img.getWidth()/cols;
  183.         h = img.getHeight()/rows;
  184.         imageType = img.getType();
  185.         if(imageType == 0) imageType = 5;
  186.         imgss = new BufferedImage[w][h];
  187.        
  188.         for(int y = 0; y < rows; y++) {
  189.             for(int x = 0; x < cols; x++) {
  190.                 imgss[x][y] = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
  191.                 g2 = imgss[x][y].createGraphics();
  192.                 g2.setComposite(AlphaComposite.Src);  
  193.                 g2.drawImage(img, 0, 0, w, h, w*x, h*y, w*x+w, h*y+h, null);
  194.                 g2.dispose();
  195.             }
  196.         }
  197.        
  198.         return imgss;
  199.     }
  200.    
  201.     /**
  202.      * Make's a certain colour transparent.
  203.      * @param source
  204.      * @param color
  205.      * @return BufferedImage
  206.      */
  207.     public static BufferedImage makeColorTransparent(String ref, Color color) {
  208.         bimg = loadBufferedImage(ref);
  209.         cimg = new BufferedImage(bimg.getWidth(), bimg.getHeight(), BufferedImage.TYPE_INT_ARGB);  
  210.         g2 = cimg.createGraphics();  
  211.         g2.setComposite(AlphaComposite.Src);  
  212.         g2.drawImage(bimg, null, 0, 0);  
  213.         g2.dispose();  
  214.        
  215.         for(int i = 0; i < cimg.getHeight(); i++) {  
  216.             for(int j = 0; j < cimg.getWidth(); j++) {  
  217.                 if(cimg.getRGB(j, i) == color.getRGB()) {  
  218.                     cimg.setRGB(j, i, 0x8F1C1C);  
  219.                 }  
  220.             }  
  221.         }  
  222.        
  223.         return cimg;  
  224.     }
  225.    
  226.     /**
  227.      * Load a image with a certain transparency.
  228.      *
  229.      * @param url
  230.      * @param transperancy
  231.      * @return BufferedImage
  232.      */
  233.     public static BufferedImage loadTranslucentImage(String url, float transperancy) {  
  234.         bimg = loadBufferedImage(url);  
  235.         cimg = new BufferedImage(bimg.getWidth(), bimg.getHeight(), BufferedImage.TRANSLUCENT);  
  236.         g2 = cimg.createGraphics();  
  237.         g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, transperancy));  
  238.         g2.drawImage(bimg, null, 0, 0);  
  239.         g2.dispose();    
  240.        
  241.         return cimg;  
  242.     }
  243.    
  244.     /**
  245.      * Change a image with a certain transparency.
  246.      *
  247.      * @param url
  248.      * @param transperancy
  249.      * @return BufferedImage
  250.      */
  251.     public static BufferedImage changeTranslucentImage(BufferedImage img, float transperancy) {    
  252.         bimg = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TRANSLUCENT);  
  253.         g2 = bimg.createGraphics();  
  254.         g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, transperancy));  
  255.         g2.drawImage(img, null, 0, 0);  
  256.         g2.dispose();  
  257.        
  258.         return bimg;  
  259.     }
  260.    
  261.     /**
  262.      * Flips a given image horizontally.
  263.      * @param source The source image.
  264.      * @return BufferedImage
  265.      */
  266.     public static BufferedImage flipHorizontally(BufferedImage source) {  
  267.         w = source.getWidth();  
  268.         h = source.getHeight();  
  269.         bimg = new BufferedImage(w, h, source.getType());  
  270.         g2 = bimg.createGraphics();  
  271.         g2.drawImage(source, 0, 0, w, h, w, 0, 0, h, null);  
  272.         g2.dispose();  
  273.        
  274.         return bimg;  
  275.     }  
  276.  
  277.     /**
  278.      * Flips a given image vertically.
  279.      * @param source The source image.
  280.      * @return BufferedImage
  281.      */
  282.     public static BufferedImage flipVertically(BufferedImage source) {  
  283.         w = source.getWidth();  
  284.         h = source.getHeight();  
  285.         bimg = new BufferedImage(w, h, source.getType());  
  286.         g2 = bimg.createGraphics();  
  287.         g2.drawImage(source, 0, 0, w, h, 0, h, w, 0, null);  
  288.         g2.dispose();  
  289.        
  290.         return bimg;  
  291.     }  
  292.  
  293.     /**
  294.      * Rotates a given image to a given angle.
  295.      * @param source The source image.
  296.      * @param angle The angle to rotate by (in degrees).
  297.      * @return BufferedImage
  298.      */
  299.     public static BufferedImage rotate(BufferedImage source, int angle) {  
  300.         w = source.getWidth();  
  301.         h = source.getHeight();  
  302.         bimg = new BufferedImage(w, h, source.getType());  
  303.         g2 = bimg.createGraphics();  
  304.         g2.rotate(Math.toRadians(angle), w / 2, h / 2);  
  305.         g2.drawImage(source, null, 0, 0);
  306.        
  307.         return bimg;  
  308.     }  
  309.  
  310.     /**
  311.      * Resizes a given image to a given width and height.
  312.      * @param source The source image.
  313.      * @param width The new width.
  314.      * @param height The new height.
  315.      * @return BufferedImage
  316.      */
  317.     public static BufferedImage resize(BufferedImage source, int width, int height) {  
  318.         w = source.getWidth();  
  319.         h = source.getHeight();  
  320.         bimg = new BufferedImage(width, height, source.getType());  
  321.         g2 = bimg.createGraphics();  
  322.         g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);  
  323.         g2.drawImage(source, 0, 0, width, height, 0, 0, w, h, null);  
  324.         g2.dispose();  
  325.        
  326.         return bimg;  
  327.     }
  328.  
  329.     public static AffineTransform getAffineTransform() {
  330.         return affineTransform;
  331.     }
  332.  
  333.     public static void setAffineTransform(AffineTransform affineTransform) {
  334.         ImageUtils.affineTransform = affineTransform;
  335.     }  
  336.    
  337. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement