]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/ImageUtils.java
Version 3, August 2007
[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.Image;
6 import java.awt.image.BufferedImage;
7 import java.awt.image.ConvolveOp;
8 import java.awt.image.Kernel;
9
10 import javax.swing.ImageIcon;
11
12 /**
13  * Class for providing generic image processing functions
14  */
15 public abstract class ImageUtils
16 {
17         private static final float SMOOTH_FACTOR = 0.008f;
18         private static ConvolveOp CONVOLVER = null;
19
20         /** Static block for initialization */
21         static
22         {
23                 float[] smoothMatrix = {
24                         0, SMOOTH_FACTOR, 0,
25                         SMOOTH_FACTOR, 1-(SMOOTH_FACTOR*4), SMOOTH_FACTOR,
26                         0, SMOOTH_FACTOR, 0
27                 };
28                 CONVOLVER = new ConvolveOp(new Kernel(3, 3, smoothMatrix));
29         }
30
31
32         /**
33          * Create a scaled and smoothed image according to the specified size
34          * @param inImage image to scale
35          * @param inWidth width to scale to
36          * @param inHeight height to scale to
37          * @return BufferedImage containing scaled result
38          */
39         public static BufferedImage createScaledImage(Image inImage, int inWidth, int inHeight)
40         {
41                 // create smaller image and force its loading
42                 Image smallerImage = inImage.getScaledInstance(inWidth, inHeight, Image.SCALE_SMOOTH);
43                 Image tempImage = new ImageIcon(smallerImage).getImage();
44                 tempImage.getWidth(null);
45
46                 // create buffered image to do transform
47                 BufferedImage buffer = new BufferedImage(inWidth, inHeight, BufferedImage.TYPE_INT_RGB);
48                 // copy scaled picture into buffer
49                 Graphics buffG = buffer.getGraphics();
50                 buffG.drawImage(smallerImage, 0, 0, inWidth, inHeight, null);
51                 buffG.dispose();
52
53                 // clear variables
54                 smallerImage = null; tempImage = null;
55                 // smooth scaled image using a normalized 3x3 matrix - taking next neighbour
56                 buffer = CONVOLVER.filter(buffer, null);
57
58                 return buffer;
59         }
60
61
62         /**
63          * Work out the max size of a thumbnail
64          * @param inOrigWidth width of original picture
65          * @param inOrigHeight height of original picture
66          * @param inMaxWidth max width of thumbnail
67          * @param inMaxHeight max height of thumbnail
68          * @return size of thumbnail as Dimension
69          */
70         public static Dimension getThumbnailSize(int inOrigWidth, int inOrigHeight, int inMaxWidth, int inMaxHeight)
71         {
72                 if (inMaxWidth <= 0 || inMaxHeight <= 0)
73                 {
74                         //System.out.println("Can't do it - maxwidth=" + inMaxWidth + ", maxheight=" + inMaxHeight);
75                         return new Dimension(0,0);
76                 }
77                 // work out maximum zoom ratio available so that thumbnail isn't too big
78                 double xZoom = inMaxWidth * 1.0 / inOrigWidth;
79                 double yZoom = inMaxHeight * 1.0 / inOrigHeight;
80                 double zoom = (xZoom > yZoom?yZoom:xZoom);
81                 // Don't make thumbnail bigger than picture
82                 if (zoom > 1.0) {return new Dimension(inOrigWidth, inOrigHeight);}
83                 // calculate new width and height
84                 return new Dimension ((int) (zoom * inOrigWidth), (int) (zoom * inOrigHeight));
85         }
86 }