1 7 package com.sun.java.swing.plaf.nimbus; 8 9 import java.awt.AlphaComposite ; 10 import java.awt.Graphics2D ; 11 import java.awt.Transparency ; 12 import java.awt.GraphicsConfiguration ; 13 import java.awt.GraphicsEnvironment ; 14 import java.awt.image.BufferedImage ; 15 import java.awt.image.Raster ; 16 import java.awt.image.WritableRaster ; 17 import java.awt.image.ColorModel ; 18 19 25 class EffectUtils { 26 27 32 static void clearImage(BufferedImage img) { 33 Graphics2D g2 = img.createGraphics(); 34 g2.setComposite(AlphaComposite.Clear); 35 g2.fillRect(0, 0, img.getWidth(), img.getHeight()); 36 g2.dispose(); 37 } 38 39 42 50 static BufferedImage gaussianBlur(BufferedImage src, BufferedImage dst, int radius) { 51 int width = src.getWidth(); 52 int height = src.getHeight(); 53 if (dst == null || dst.getWidth() != width || dst.getHeight() != height || src.getType() != dst.getType()) { 54 dst = createColorModelCompatibleImage(src); 55 } 56 float[] kernel = createGaussianKernel(radius); 57 if (src.getType() == BufferedImage.TYPE_INT_ARGB) { 58 int[] srcPixels = new int[width * height]; 59 int[] dstPixels = new int[width * height]; 60 getPixels(src, 0, 0, width, height, srcPixels); 61 blur(srcPixels, dstPixels, width, height, kernel, radius); 63 blur(dstPixels, srcPixels, height, width, kernel, radius); 66 setPixels(dst, 0, 0, width, height, srcPixels); 68 } else if (src.getType() == BufferedImage.TYPE_BYTE_GRAY) { 69 byte[] srcPixels = new byte[width * height]; 70 byte[] dstPixels = new byte[width * height]; 71 getPixels(src, 0, 0, width, height, srcPixels); 72 blur(srcPixels, dstPixels, width, height, kernel, radius); 74 blur(dstPixels, srcPixels, height, width, kernel, radius); 77 setPixels(dst, 0, 0, width, height, srcPixels); 79 } else { 80 throw new IllegalArgumentException ("EffectUtils.gaussianBlur() src image is not a supported type, type=[" + 81 src.getType() + "]"); 82 } 83 return dst; 84 } 85 86 99 private static void blur(int[] srcPixels, int[] dstPixels, 100 int width, int height, 101 float[] kernel, int radius) { 102 float a; 103 float r; 104 float g; 105 float b; 106 107 int ca; 108 int cr; 109 int cg; 110 int cb; 111 112 for (int y = 0; y < height; y++) { 113 int index = y; 114 int offset = y * width; 115 116 for (int x = 0; x < width; x++) { 117 a = r = g = b = 0.0f; 118 119 for (int i = -radius; i <= radius; i++) { 120 int subOffset = x + i; 121 if (subOffset < 0 || subOffset >= width) { 122 subOffset = (x + width) % width; 123 } 124 125 int pixel = srcPixels[offset + subOffset]; 126 float blurFactor = kernel[radius + i]; 127 128 a += blurFactor * ((pixel >> 24) & 0xFF); 129 r += blurFactor * ((pixel >> 16) & 0xFF); 130 g += blurFactor * ((pixel >> 8) & 0xFF); 131 b += blurFactor * ((pixel) & 0xFF); 132 } 133 134 ca = (int) (a + 0.5f); 135 cr = (int) (r + 0.5f); 136 cg = (int) (g + 0.5f); 137 cb = (int) (b + 0.5f); 138 139 dstPixels[index] = ((ca > 255 ? 255 : ca) << 24) | 140 ((cr > 255 ? 255 : cr) << 16) | 141 ((cg > 255 ? 255 : cg) << 8) | 142 (cb > 255 ? 255 : cb); 143 index += height; 144 } 145 } 146 } 147 148 161 static void blur(byte[] srcPixels, byte[] dstPixels, 162 int width, int height, 163 float[] kernel, int radius) { 164 float p; 165 int cp; 166 for (int y = 0; y < height; y++) { 167 int index = y; 168 int offset = y * width; 169 for (int x = 0; x < width; x++) { 170 p = 0.0f; 171 for (int i = -radius; i <= radius; i++) { 172 int subOffset = x + i; 173 if (subOffset < 0 || subOffset >= width) { 176 subOffset = (x + width) % width; 177 } 178 int pixel = srcPixels[offset + subOffset] & 0xFF; 179 float blurFactor = kernel[radius + i]; 180 p += blurFactor * pixel; 181 } 182 cp = (int) (p + 0.5f); 183 dstPixels[index] = (byte) (cp > 255 ? 255 : cp); 184 index += height; 185 } 186 } 187 } 188 189 static float[] createGaussianKernel(int radius) { 190 if (radius < 1) { 191 throw new IllegalArgumentException ("Radius must be >= 1"); 192 } 193 194 float[] data = new float[radius * 2 + 1]; 195 196 float sigma = radius / 3.0f; 197 float twoSigmaSquare = 2.0f * sigma * sigma; 198 float sigmaRoot = (float) Math.sqrt(twoSigmaSquare * Math.PI); 199 float total = 0.0f; 200 201 for (int i = -radius; i <= radius; i++) { 202 float distance = i * i; 203 int index = i + radius; 204 data[index] = (float) Math.exp(-distance / twoSigmaSquare) / sigmaRoot; 205 total += data[index]; 206 } 207 208 for (int i = 0; i < data.length; i++) { 209 data[i] /= total; 210 } 211 212 return data; 213 } 214 215 218 233 static byte[] getPixels(BufferedImage img, 234 int x, int y, int w, int h, byte[] pixels) { 235 if (w == 0 || h == 0) { 236 return new byte[0]; 237 } 238 239 if (pixels == null) { 240 pixels = new byte[w * h]; 241 } else if (pixels.length < w * h) { 242 throw new IllegalArgumentException ("pixels array must have a length >= w*h"); 243 } 244 245 int imageType = img.getType(); 246 if (imageType == BufferedImage.TYPE_BYTE_GRAY) { 247 Raster raster = img.getRaster(); 248 return (byte[]) raster.getDataElements(x, y, w, h, pixels); 249 } else { 250 throw new IllegalArgumentException ("Only type BYTE_GRAY is supported"); 251 } 252 } 253 254 267 static void setPixels(BufferedImage img, 268 int x, int y, int w, int h, byte[] pixels) { 269 if (pixels == null || w == 0 || h == 0) { 270 return; 271 } else if (pixels.length < w * h) { 272 throw new IllegalArgumentException ("pixels array must have a length >= w*h"); 273 } 274 int imageType = img.getType(); 275 if (imageType == BufferedImage.TYPE_BYTE_GRAY) { 276 WritableRaster raster = img.getRaster(); 277 raster.setDataElements(x, y, w, h, pixels); 278 } else { 279 throw new IllegalArgumentException ("Only type BYTE_GRAY is supported"); 280 } 281 } 282 283 301 public static int[] getPixels(BufferedImage img, 302 int x, int y, int w, int h, int[] pixels) { 303 if (w == 0 || h == 0) { 304 return new int[0]; 305 } 306 307 if (pixels == null) { 308 pixels = new int[w * h]; 309 } else if (pixels.length < w * h) { 310 throw new IllegalArgumentException ("pixels array must have a length" + 311 " >= w*h"); 312 } 313 314 int imageType = img.getType(); 315 if (imageType == BufferedImage.TYPE_INT_ARGB || 316 imageType == BufferedImage.TYPE_INT_RGB) { 317 Raster raster = img.getRaster(); 318 return (int[]) raster.getDataElements(x, y, w, h, pixels); 319 } 320 321 return img.getRGB(x, y, w, h, pixels, 0, w); 323 } 324 325 340 public static void setPixels(BufferedImage img, 341 int x, int y, int w, int h, int[] pixels) { 342 if (pixels == null || w == 0 || h == 0) { 343 return; 344 } else if (pixels.length < w * h) { 345 throw new IllegalArgumentException ("pixels array must have a length" + 346 " >= w*h"); 347 } 348 349 int imageType = img.getType(); 350 if (imageType == BufferedImage.TYPE_INT_ARGB || 351 imageType == BufferedImage.TYPE_INT_RGB) { 352 WritableRaster raster = img.getRaster(); 353 raster.setDataElements(x, y, w, h, pixels); 354 } else { 355 img.setRGB(x, y, w, h, pixels, 0, w); 357 } 358 } 359 360 371 public static BufferedImage createColorModelCompatibleImage(BufferedImage image) { 372 ColorModel cm = image.getColorModel(); 373 return new BufferedImage (cm, 374 cm.createCompatibleWritableRaster(image.getWidth(), 375 image.getHeight()), 376 cm.isAlphaPremultiplied(), null); 377 } 378 379 391 public static BufferedImage createCompatibleTranslucentImage(int width, 392 int height) { 393 return isHeadless() ? 394 new BufferedImage (width, height, BufferedImage.TYPE_INT_ARGB) : 395 getGraphicsConfiguration().createCompatibleImage(width, height, 396 Transparency.TRANSLUCENT); 397 } 398 399 private static boolean isHeadless() { 400 return GraphicsEnvironment.isHeadless(); 401 } 402 403 private static GraphicsConfiguration getGraphicsConfiguration() { 405 return GraphicsEnvironment.getLocalGraphicsEnvironment(). 406 getDefaultScreenDevice().getDefaultConfiguration(); 407 } 408 409 } 410 | Popular Tags |