1 |
| package photospace.graphics; |
2 |
| |
3 |
| import java.awt.*; |
4 |
| import java.awt.image.*; |
5 |
| import java.io.*; |
6 |
| import org.apache.commons.logging.*; |
7 |
| import com.mullassery.imaging.*; |
8 |
| import com.mullassery.imaging.util.*; |
9 |
| |
10 |
| public class Sampler |
11 |
| { |
12 |
| private static final Log log = LogFactory.getLog(Sampler.class); |
13 |
| |
14 |
| private static final String JPEG = "JPEG"; |
15 |
| private static final float quality = 1.0f; |
16 |
| |
17 |
| private static Imaging imaging; |
18 |
| |
19 |
| static |
20 |
| { |
21 |
1
| imaging = ImagingFactory.createImagingInstance();
|
22 |
1
| imaging.setImagingQuality(Imaging.HIGH);
|
23 |
| } |
24 |
| |
25 |
6
| public static BufferedImage read(File file)
|
26 |
| { |
27 |
6
| return imaging.read(file);
|
28 |
| } |
29 |
| |
30 |
3
| public static void write(BufferedImage image, File file) throws IOException
|
31 |
| { |
32 |
3
| imaging.write(image, new FileOutputStream(file), JPEG, 1.0f);
|
33 |
| } |
34 |
| |
35 |
3
| public static BufferedImage scaleToFit(BufferedImage image, Dimension dim)
|
36 |
| { |
37 |
3
| if (dim.getWidth() >= image.getWidth()
|
38 |
2
| && dim.getHeight() >= image.getHeight()) return image;
|
39 |
| |
40 |
1
| int width = -1;
|
41 |
1
| int height = -1;
|
42 |
1
| if (dim.getWidth() == -1)
|
43 |
| { |
44 |
0
| height = (int) dim.getHeight();
|
45 |
| } |
46 |
1
| else if (dim.getHeight() == -1)
|
47 |
| { |
48 |
0
| width = (int) dim.getWidth();
|
49 |
| } |
50 |
1
| else if (dim.getWidth()/image.getWidth() > dim.getHeight()/image.getHeight())
|
51 |
1
| height = (int) dim.getHeight();
|
52 |
| else |
53 |
0
| width = (int) dim.getWidth();
|
54 |
| |
55 |
1
| Image scaled = image.getScaledInstance(width,
|
56 |
| height, |
57 |
| Image.SCALE_SMOOTH); |
58 |
1
| return Util.createBufferedImage(scaled, JPEG);
|
59 |
| } |
60 |
| |
61 |
8
| public static BufferedImage scale(BufferedImage image, Transform transform)
|
62 |
| { |
63 |
5
| if (Transform.FILL.equals(transform.getScale())) return Sampler.scaleToFill(image, transform.getDimension());
|
64 |
3
| else return Sampler.scaleToFit(image, transform.getDimension());
|
65 |
| } |
66 |
| |
67 |
5
| public static BufferedImage scaleToFill(BufferedImage image, Dimension dim)
|
68 |
| { |
69 |
| |
70 |
5
| int width = -1;
|
71 |
5
| int height = -1;
|
72 |
5
| if (dim.getWidth()/image.getWidth() > dim.getHeight()/image.getHeight())
|
73 |
1
| width = (int) dim.getWidth();
|
74 |
| else |
75 |
4
| height = (int) dim.getHeight();
|
76 |
| |
77 |
5
| BufferedImage scaled = Util.createBufferedImage(image.getScaledInstance(width,
|
78 |
| height, |
79 |
| Image.SCALE_SMOOTH), JPEG); |
80 |
| |
81 |
5
| int x = (int) (scaled.getWidth() - dim.getWidth())/2;
|
82 |
5
| int y = (int) (scaled.getHeight() - dim.getHeight())/2;
|
83 |
5
| return scaled.getSubimage(x, y, (int) dim.getWidth(), (int) dim.getHeight());
|
84 |
| } |
85 |
| |
86 |
| |
87 |
| |
88 |
| |
89 |
| |
90 |
| |
91 |
| |
92 |
| |
93 |
| |
94 |
| |
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
| |
100 |
| |
101 |
| |
102 |
| |
103 |
| |
104 |
| |
105 |
| |
106 |
| |
107 |
| |
108 |
| |
109 |
| |
110 |
| |
111 |
| |
112 |
| |
113 |
| |
114 |
| |
115 |
| |
116 |
| |
117 |
1
| public static BufferedImage rotate(BufferedImage image, float degrees)
|
118 |
| { |
119 |
1
| return imaging.rotate(image, Util.degrees2Radians(degrees));
|
120 |
| } |
121 |
| |
122 |
1
| public static byte [] getBytes(BufferedImage image)
|
123 |
| { |
124 |
1
| ByteArrayOutputStream output = new ByteArrayOutputStream();
|
125 |
1
| imaging.write(image, output, JPEG, quality);
|
126 |
1
| return output.toByteArray();
|
127 |
| } |
128 |
| } |
129 |
| |
130 |
| |
131 |
| |
132 |
| |