]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/ImageUtils.java
Version 14, October 2012
[GpsPrune.git] / tim / prune / gui / ImageUtils.java
1 package tim.prune.gui;
2
3 import java.awt.Dimension;
4 import java.awt.Graphics;
5 import java.awt.Graphics2D;
6 import java.awt.Image;
7 import java.awt.image.BufferedImage;
8 import java.awt.image.ConvolveOp;
9 import java.awt.image.Kernel;
10
11 import javax.swing.ImageIcon;
12
13 /**
14  * Class for providing generic image processing functions
15  */
16 public abstract class ImageUtils
17 {
18         private static final float SMOOTH_FACTOR = 0.008f;
19         private static ConvolveOp CONVOLVER = null;
20
21         /** Static block for initialization */
22         static
23         {
24                 float[] smoothMatrix = {
25                         0, SMOOTH_FACTOR, 0,
26                         SMOOTH_FACTOR, 1-(SMOOTH_FACTOR*4), SMOOTH_FACTOR,
27                         0, SMOOTH_FACTOR, 0
28                 };
29                 CONVOLVER = new ConvolveOp(new Kernel(3, 3, smoothMatrix));
30         }
31
32
33         /**
34          * Create a scaled and smoothed image according to the specified size
35          * @param inImage image to scale
36          * @param inWidth width to scale to
37          * @param inHeight height to scale to
38          * @return BufferedImage containing scaled result
39          */
40         public static BufferedImage createScaledImage(Image inImage, int inWidth, int inHeight)
41         {
42                 // create smaller image and force its loading
43                 Image smallerImage = inImage.getScaledInstance(inWidth, inHeight, Image.SCALE_SMOOTH);
44                 Image tempImage = new ImageIcon(smallerImage).getImage();
45                 tempImage.getWidth(null);
46
47                 // create buffered image to do transform
48                 BufferedImage buffer = new BufferedImage(inWidth, inHeight, BufferedImage.TYPE_INT_RGB);
49                 // copy scaled picture into buffer
50                 Graphics buffG = buffer.getGraphics();
51                 buffG.drawImage(smallerImage, 0, 0, inWidth, inHeight, null);
52                 buffG.dispose();
53
54                 // clear variables
55                 smallerImage = null; tempImage = null;
56                 // smooth scaled image using a normalized 3x3 matrix - taking next neighbour
57                 buffer = CONVOLVER.filter(buffer, null);
58
59                 return buffer;
60         }
61
62
63         /**
64          * Work out the max size of a thumbnail
65          * @param inOrigWidth width of original picture
66          * @param inOrigHeight height of original picture
67          * @param inMaxWidth max width of thumbnail
68          * @param inMaxHeight max height of thumbnail
69          * @return size of thumbnail as Dimension
70          */
71         public static Dimension getThumbnailSize(int inOrigWidth, int inOrigHeight, int inMaxWidth, int inMaxHeight)
72         {
73                 if (inMaxWidth <= 0 || inMaxHeight <= 0) {return new Dimension(1, 1);}
74                 // work out maximum zoom ratio available so that thumbnail isn't too big
75                 double xZoom = inMaxWidth * 1.0 / inOrigWidth;
76                 double yZoom = inMaxHeight * 1.0 / inOrigHeight;
77                 double zoom = (xZoom > yZoom?yZoom:xZoom);
78                 // Don't make thumbnail bigger than picture
79                 if (zoom > 1.0) {return new Dimension(inOrigWidth, inOrigHeight);}
80
81                 // calculate new width and height
82                 final int xSize = (int) (zoom * inOrigWidth);
83                 final int ySize = (int) (zoom * inOrigHeight);
84                 if (xSize <= 0 || ySize <= 0) {return new Dimension(1, 1);}
85                 return new Dimension (xSize, ySize);
86         }
87
88
89         /**
90          * Create a new image by rotating and scaling the given one
91          * @param inImage input image
92          * @param inMaxWidth maximum width of output image
93          * @param inMaxHeight maximum height of output image
94          * @param inRotationDegrees number of degrees to rotate clockwise (0, 90, 180 or 270)
95          * @return rotated, scaled image
96          */
97         public static BufferedImage rotateImage(Image inImage, int inMaxWidth, int inMaxHeight, int inRotationDegrees)
98         {
99                 // Create scaled image of suitable size
100                 boolean isRotated = (inRotationDegrees % 180 != 0);
101                 int origWidth = inImage.getWidth(null);
102                 int origHeight = inImage.getHeight(null);
103                 int thumbWidth = isRotated?origHeight:origWidth;
104                 int thumbHeight = isRotated?origWidth:origHeight;
105                 Dimension scaledSize = getThumbnailSize(thumbWidth, thumbHeight, inMaxWidth, inMaxHeight);
106                 BufferedImage result = new BufferedImage(scaledSize.width, scaledSize.height, BufferedImage.TYPE_INT_RGB);
107                 // Do different things according to rotation angle (a bit messy, sorry!)
108                 if (inRotationDegrees == 0)
109                 {
110                         // Not rotated, so just copy image directly
111                         result.getGraphics().drawImage(inImage, 0, 0, scaledSize.width, scaledSize.height, null);
112                 }
113                 else
114                 {
115                         // Need to use Graphics2D for rotation, not Graphics
116                         Graphics2D g2d = result.createGraphics();
117                         switch (inRotationDegrees)
118                         {
119                                 case 90:
120                                         g2d.rotate(Math.PI / 2, 0.0, 0.0);
121                                         g2d.drawImage(inImage, 0, -scaledSize.width, scaledSize.height, scaledSize.width, null);
122                                         break;
123                                 case 180:
124                                         g2d.rotate(Math.PI, scaledSize.width/2.0, scaledSize.height/2.0);
125                                         g2d.drawImage(inImage, 0, 0, scaledSize.width, scaledSize.height, null);
126                                         break;
127                                 case 270:
128                                         g2d.rotate(Math.PI * 3/2, 0.0, 0.0);
129                                         g2d.drawImage(inImage, -scaledSize.height, 0, scaledSize.height, scaledSize.width, null);
130                         }
131                         // Clear up memory
132                         g2d.dispose();
133                 }
134                 return result;
135         }
136 }