|
2 | 2 |
|
3 | 3 | import java.awt.Color; |
4 | 4 | import java.awt.Graphics; |
| 5 | +import java.awt.GraphicsEnvironment; |
5 | 6 | import java.awt.Image; |
6 | 7 | import java.awt.Toolkit; |
7 | 8 | import java.awt.color.ColorSpace; |
@@ -178,78 +179,79 @@ private static void testWrite() { |
178 | 179 | } |
179 | 180 |
|
180 | 181 | private static void testGray() { |
181 | | - BufferedImage image = new BufferedImage(100,100, BufferedImage.TYPE_USHORT_GRAY); |
182 | | - ColorModel cm = image.getColorModel(); |
183 | | - |
184 | | - // Tests stolen buffer, though it is not used. |
185 | | - |
186 | | - short[] bwData = ((DataBufferUShort) image.getRaster().getDataBuffer()).getData(); |
187 | | - ColorSpace x = image.getColorModel().getColorSpace(); |
188 | | - System.out.println(cm instanceof PackedColorModel); |
189 | | - System.out.println(cm instanceof ColorModel); |
190 | | - System.out.println(cm instanceof ComponentColorModel); |
191 | | - |
192 | | - PixelConverter pc = PixelConverter.UshortGray.instance; |
193 | | - int rgb, p; |
194 | | - |
195 | | - rgb = 0xFFFFFF00; |
196 | | - image.setRGB(0, 0, rgb); |
197 | | - p = image.getRGB(0, 0); |
198 | | - System.out.println(p + " " + Integer.toHexString(rgb)); |
199 | | - rgb = pc.pixelToRgb(p, null); |
200 | | - System.out.println(p + " " + Integer.toHexString(rgb)); |
201 | | - |
202 | | - rgb = 0xFFFF0000; |
203 | | - p = pc.rgbToPixel(rgb, null); |
204 | | - System.out.println(p + " " + Integer.toHexString(rgb)); |
205 | | - rgb = pc.pixelToRgb(p, null); |
206 | | - System.out.println(p + " " + Integer.toHexString(rgb)); |
207 | | - |
208 | | - rgb = 0xFF00FF00; |
209 | | - p = pc.rgbToPixel(rgb, null); |
210 | | - System.out.println(p + " " + Integer.toHexString(rgb)); |
211 | | - rgb = pc.pixelToRgb(p, null); |
212 | | - System.out.println(p + " " + Integer.toHexString(rgb)); |
213 | | - |
214 | | - rgb = 0xFF0000FF; |
215 | | - p = pc.rgbToPixel(rgb, null); |
216 | | - System.out.println(p + " " + Integer.toHexString(rgb)); |
217 | | - rgb = pc.pixelToRgb(p, null); |
218 | | - System.out.println(p + " " + Integer.toHexString(rgb)); |
219 | | - |
220 | | - rgb = 0xFFFF0000; |
221 | | - p = pc.rgbToPixel(rgb, null); |
222 | | - System.out.println(p + " " + Integer.toHexString(rgb)); |
223 | | - rgb = pc.pixelToRgb(p, null); |
224 | | - System.out.println(p + " " + Integer.toHexString(rgb)); |
225 | | - |
226 | | - |
227 | | - rgb = 0xFF4c4c4c; |
228 | | - p = pc.rgbToPixel(rgb, null); |
229 | | - System.out.println(p + " " + Integer.toHexString(rgb)); |
230 | | - rgb = pc.pixelToRgb(p, null); |
231 | | - System.out.println(p + " " + Integer.toHexString(rgb)); |
232 | | - |
| 182 | + BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_USHORT_GRAY); |
| 183 | + ColorModel cm = image.getColorModel(); |
| 184 | + |
| 185 | + // Tests stolen buffer, though it is not used. |
| 186 | + |
| 187 | + short[] bwData = ((DataBufferUShort) image.getRaster().getDataBuffer()).getData(); |
| 188 | + ColorSpace x = image.getColorModel().getColorSpace(); |
| 189 | + System.out.println(cm instanceof PackedColorModel); |
| 190 | + System.out.println(cm instanceof ColorModel); |
| 191 | + System.out.println(cm instanceof ComponentColorModel); |
| 192 | + |
| 193 | + PixelConverter pc = PixelConverter.UshortGray.instance; |
| 194 | + int rgb, p; |
| 195 | + |
| 196 | + rgb = 0xFFFFFF00; |
| 197 | + image.setRGB(0, 0, rgb); |
| 198 | + p = image.getRGB(0, 0); |
| 199 | + System.out.println(p + " " + Integer.toHexString(rgb)); |
| 200 | + rgb = pc.pixelToRgb(p, null); |
| 201 | + System.out.println(p + " " + Integer.toHexString(rgb)); |
| 202 | + |
| 203 | + rgb = 0xFFFF0000; |
| 204 | + p = pc.rgbToPixel(rgb, null); |
| 205 | + System.out.println(p + " " + Integer.toHexString(rgb)); |
| 206 | + rgb = pc.pixelToRgb(p, null); |
| 207 | + System.out.println(p + " " + Integer.toHexString(rgb)); |
| 208 | + |
| 209 | + rgb = 0xFF00FF00; |
| 210 | + p = pc.rgbToPixel(rgb, null); |
| 211 | + System.out.println(p + " " + Integer.toHexString(rgb)); |
| 212 | + rgb = pc.pixelToRgb(p, null); |
| 213 | + System.out.println(p + " " + Integer.toHexString(rgb)); |
| 214 | + |
| 215 | + rgb = 0xFF0000FF; |
| 216 | + p = pc.rgbToPixel(rgb, null); |
| 217 | + System.out.println(p + " " + Integer.toHexString(rgb)); |
| 218 | + rgb = pc.pixelToRgb(p, null); |
| 219 | + System.out.println(p + " " + Integer.toHexString(rgb)); |
| 220 | + |
| 221 | + rgb = 0xFFFF0000; |
| 222 | + p = pc.rgbToPixel(rgb, null); |
| 223 | + System.out.println(p + " " + Integer.toHexString(rgb)); |
| 224 | + rgb = pc.pixelToRgb(p, null); |
| 225 | + System.out.println(p + " " + Integer.toHexString(rgb)); |
| 226 | + |
| 227 | + rgb = 0xFF4c4c4c; |
| 228 | + p = pc.rgbToPixel(rgb, null); |
| 229 | + System.out.println(p + " " + Integer.toHexString(rgb)); |
| 230 | + rgb = pc.pixelToRgb(p, null); |
| 231 | + System.out.println(p + " " + Integer.toHexString(rgb)); |
233 | 232 |
|
234 | 233 | int[] pixels = new int[10000]; |
235 | | - for (int i = 0; i < 10000; i++) { |
236 | | - int pixel = (int) ((i % 100)* 2.55); |
237 | | - pixels[i] = ((((((0xff << 8) | pixel) << 8) | pixel) << 8) | pixel); // ((i % 100)*0xFFFFFF/100) | 0xFF000000;//Math.random() * 0xFFFFFF); |
| 234 | + for (int i = 0; i < 10000; i++) { |
| 235 | + int pixel = (int) ((i % 100) * 2.55); |
| 236 | + pixels[i] = ((((((0xff << 8) | pixel) << 8) | pixel) << 8) | pixel); // ((i % 100)*0xFFFFFF/100) | |
| 237 | + // 0xFF000000;//Math.random() * |
| 238 | + // 0xFFFFFF); |
238 | 239 | } |
239 | | - image.setRGB(0, 0, 100, 100, pixels, 0, 100); |
| 240 | + image.setRGB(0, 0, 100, 100, pixels, 0, 100); |
240 | 241 |
|
241 | 242 | // test to send color to a gray-scale image |
242 | 243 | Graphics g = image.getGraphics(); |
243 | 244 | g.setColor(Color.green); |
244 | 245 | System.out.println(Integer.toHexString(g.getColor().getRGB())); |
245 | 246 | g.fillRect(25, 25, 50, 50); |
246 | 247 | showImage(image); |
247 | | - |
248 | | - JFrame f = new JFrame(); |
249 | | - f.add(new JLabel(new ImageIcon(image))); |
250 | | - f.pack(); |
251 | | - f.show(); |
252 | | -} |
| 248 | + if (!GraphicsEnvironment.isHeadless()) { |
| 249 | + JFrame f = new JFrame(); |
| 250 | + f.add(new JLabel(new ImageIcon(image))); |
| 251 | + f.pack(); |
| 252 | + f.show(); |
| 253 | + } |
| 254 | + } |
253 | 255 |
|
254 | 256 | private static void testPacked() { |
255 | 257 |
|
|
0 commit comments