1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 package org.carrot2.util;
38
39 import java.awt.AlphaComposite;
40 import java.awt.Color;
41 import java.awt.CompositeContext;
42 import java.awt.Transparency;
43 import java.awt.image.BufferedImage;
44 import java.awt.image.Raster;
45 import java.util.HashSet;
46 import java.util.Set;
47
48
49
50
51 public class BufferedImageUtils {
52
53
54
55
56
57
58
59
60
61 public static boolean hasPartialTransparency(BufferedImage image) {
62 final Raster alphaRaster = image.getAlphaRaster();
63 if (image.getTransparency() != Transparency.TRANSLUCENT || alphaRaster == null) {
64 return false;
65 }
66
67 int[] pixels = alphaRaster.getPixels(0, 0, alphaRaster.getWidth(), alphaRaster.getHeight(), (int[]) null);
68 for (int i : pixels) {
69 if (i != 0 && i != 255) {
70 return true;
71 }
72 }
73
74 return false;
75 }
76
77
78
79
80
81
82
83
84
85 public static boolean hasTransparency(BufferedImage image) {
86 final Raster alphaRaster = image.getAlphaRaster();
87 if (image.getTransparency() != Transparency.TRANSLUCENT || alphaRaster == null) {
88 return false;
89 }
90
91 int[] pixels = alphaRaster.getPixels(0, 0, alphaRaster.getWidth(), alphaRaster.getHeight(), (int[]) null);
92 for (int i : pixels) {
93 if (i != 255) {
94 return true;
95 }
96 }
97
98 return false;
99 }
100
101
102
103
104
105
106
107
108
109 public static int countDistinctColors(BufferedImage image) {
110 return getDistinctColors(image).length;
111 }
112
113
114
115
116
117
118
119
120
121 public static int[] getDistinctColors(BufferedImage image) {
122 return getDistinctColors(image, 0);
123 }
124
125
126
127
128
129
130
131
132
133
134
135
136 public static int[] getDistinctColors(BufferedImage image, int padding) {
137 final int width = image.getWidth();
138 final int height = image.getHeight();
139
140 final Set<Integer> colors = new HashSet<>();
141
142 for (int x = 0; x < width; x++) {
143 for (int y = 0; y < height; y++) {
144 final int pixel = image.getRGB(x, y);
145
146
147 if ((pixel & 0xff000000) != 0x00000000) {
148 colors.add(Integer.valueOf(pixel & 0x00ffffff));
149 }
150 }
151 }
152
153 final int[] colorMap = new int[colors.size() + padding];
154 int index = padding;
155 for (Integer color : colors) {
156 colorMap[index] = color;
157 index++;
158 }
159
160 return colorMap;
161 }
162
163
164
165
166
167
168
169
170
171 public static int[][] getRgb(BufferedImage image) {
172 final int width = image.getWidth();
173 final int height = image.getHeight();
174
175 final int[][] rgb = new int[width][height];
176
177 for (int x = 0; x < width; x++) {
178 for (int y = 0; y < height; y++) {
179 rgb[x][y] = image.getRGB(x, y);
180 }
181 }
182
183 return rgb;
184 }
185
186
187
188
189
190
191
192
193
194
195
196
197
198 public static BufferedImage matte(BufferedImage source, Color matteColor) {
199 final int width = source.getWidth();
200 final int height = source.getHeight();
201
202
203
204 final BufferedImage sourceConverted = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
205 sourceConverted.getGraphics().drawImage(source, 0, 0, null);
206
207 final BufferedImage matted = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
208
209 final BufferedImage matte = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
210 final int matteRgb = matteColor.getRGB();
211 for (int x = 0; x < width; x++) {
212 for (int y = 0; y < height; y++) {
213 matte.setRGB(x, y, matteRgb);
214 }
215 }
216
217 CompositeContext context = AlphaComposite.DstOver.createContext(matte.getColorModel(),
218 sourceConverted.getColorModel(), null);
219 context.compose(matte.getRaster(), sourceConverted.getRaster(), matted.getRaster());
220
221 return matted;
222 }
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237 public static void drawImage(BufferedImage image, BufferedImage canvas, int x, int y) {
238 final int[] imgRGB = image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth());
239 canvas.setRGB(x, y, image.getWidth(), image.getHeight(), imgRGB, 0, image.getWidth());
240 }
241
242
243
244
245 private BufferedImageUtils() {
246 }
247 }