1 | |
package org.jtheque.utils.ui; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
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 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
public final class ImageUtils { |
45 | |
private static final float ALPHA_REFLECTION = 0.75f; |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
private ImageUtils() { |
51 | 0 | super(); |
52 | 0 | } |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
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 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
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 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
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 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
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 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
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 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
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 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
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 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
public static boolean isHeadless() { |
212 | 0 | return GraphicsEnvironment.isHeadless() || GraphicsEnvironment.getLocalGraphicsEnvironment().isHeadlessInstance(); |
213 | |
} |
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
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 | |
|
230 | 0 | g2.drawImage(image, 0, 0, null); |
231 | |
|
232 | |
|
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 | |
|
238 | 0 | g2.translate(0, height); |
239 | |
|
240 | |
|
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 | |
|
247 | 0 | g2.setComposite(AlphaComposite.DstIn); |
248 | |
|
249 | |
|
250 | 0 | g2.fillRect(0, 0, image.getWidth(), height); |
251 | |
|
252 | 0 | g2.dispose(); |
253 | |
|
254 | 0 | return result; |
255 | |
} |
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
public static BufferedImage openCompatibleImageFromClassPath(String path) { |
264 | 0 | return openCompatibleImage(ImageUtils.class.getResourceAsStream(path)); |
265 | |
} |
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
|
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 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
public static BufferedImage openCompatibleImage(InputStream stream) { |
294 | 0 | return createCompatibleImage(read(stream)); |
295 | |
} |
296 | |
} |