|
2 | 2 |
|
3 | 3 | import java.awt.Color; |
4 | 4 | import java.awt.Graphics; |
| 5 | +import java.awt.Graphics2D; |
5 | 6 | import java.awt.GraphicsEnvironment; |
6 | 7 | import java.awt.Image; |
7 | 8 | import java.awt.Toolkit; |
@@ -39,8 +40,8 @@ public class Test_Image extends Test_ { |
39 | 40 | public static void main(String[] args) { |
40 | 41 |
|
41 | 42 | // testSource(); |
42 | | -// testPacked(); |
43 | | - testGray(); |
| 43 | + testPacked(); |
| 44 | +// testGray(); |
44 | 45 | // testRead(); |
45 | 46 | // testWrite(); |
46 | 47 |
|
@@ -264,15 +265,40 @@ private static void testPacked() { |
264 | 265 | DataBuffer databuffer = new DataBufferByte(packedData, len); |
265 | 266 | WritableRaster raster = Raster.createPackedRaster(databuffer, nx, ny, 1, null); |
266 | 267 | // default colors are red and blue |
267 | | - ColorModel colorModel = new IndexColorModel(1, 2, new byte[] {(byte) 255, (byte) 0}, new byte[] {(byte) 0, (byte) 0}, new byte[] {(byte) 0, (byte) 255}); |
| 268 | + ColorModel colorModel = new IndexColorModel(1, 2, |
| 269 | + new byte[] {(byte) 255, (byte) 0}, |
| 270 | + new byte[] {(byte) 0, (byte) 0}, |
| 271 | + new byte[] {(byte) 0, (byte) 255}); |
268 | 272 | BufferedImage image = new BufferedImage(colorModel, raster, false, null); |
269 | | - |
| 273 | + |
| 274 | + dumpImage(image, nx, ny); |
| 275 | + DataBuffer buf = image.getRaster().getDataBuffer(); |
| 276 | + byte[] data = ((DataBufferByte) buf).getData(); |
| 277 | + System.out.println(Arrays.toString(data)); |
| 278 | + Graphics2D g = image.createGraphics(); |
| 279 | + g.setColor(new Color(0,0,255)); |
| 280 | + g.fillRect(0, 0, 100, 100); |
| 281 | + g.dispose(); |
| 282 | + image.flush(); |
| 283 | + System.out.println(Arrays.toString(data)); |
| 284 | + dumpImage(image, nx, ny); |
| 285 | + } |
| 286 | + |
| 287 | + private static void dumpImage(BufferedImage image, int nx, int ny) { |
| 288 | + System.out.println("----------------"); |
270 | 289 | int n = nx * ny; |
271 | 290 | int[] pixels = new int[n * 4]; |
272 | 291 | for (int i = 0, pt = 0; i < n; i++, pt+=4) { |
| 292 | + |
273 | 293 | image.getColorModel().getComponents(i, pixels, pt); |
274 | | - System.out.println(pixels[pt] + " " + pixels[pt+1] + " " + pixels[pt+2] + " " + pixels[pt+3]); |
| 294 | + System.out.println(i + " " + nx + " " + ny + ": " + pixels[pt] + " " + pixels[pt+1] + " " + pixels[pt+2] + " " + pixels[pt+3]); |
275 | 295 | } |
| 296 | + System.out.println("==========="); |
| 297 | + for (int i = 0; i < nx; i++) { |
| 298 | + for (int j = 0; j < ny; j++) { |
| 299 | + System.out.println(Integer.toHexString(image.getRGB(i, j))); |
| 300 | + } |
| 301 | + } |
276 | 302 | } |
277 | 303 |
|
278 | 304 | } |
0 commit comments