1173N/A/*
2362N/A * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
1173N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1173N/A *
1173N/A * This code is free software; you can redistribute it and/or modify it
1173N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
1173N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
1173N/A *
1173N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1173N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1173N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1173N/A * version 2 for more details (a copy is included in the LICENSE file that
1173N/A * accompanied this code).
1173N/A *
1173N/A * You should have received a copy of the GNU General Public License version
1173N/A * 2 along with this work; if not, write to the Free Software Foundation,
1173N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1173N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1173N/A */
1173N/Apackage javax.swing.plaf.nimbus;
1173N/A
1173N/Aimport java.awt.AlphaComposite;
1173N/Aimport java.awt.Graphics2D;
1173N/Aimport java.awt.Transparency;
1173N/Aimport java.awt.GraphicsConfiguration;
1173N/Aimport java.awt.GraphicsEnvironment;
1173N/Aimport java.awt.image.BufferedImage;
1173N/Aimport java.awt.image.Raster;
1173N/Aimport java.awt.image.WritableRaster;
1173N/Aimport java.awt.image.ColorModel;
1173N/A
1173N/A/**
1173N/A * EffectUtils
1173N/A *
1173N/A * @author Created by Jasper Potts (Jun 18, 2007)
1173N/A */
1173N/Aclass EffectUtils {
1173N/A
1173N/A /**
1173N/A * Clear a transparent image to 100% transparent
1173N/A *
1173N/A * @param img The image to clear
1173N/A */
1173N/A static void clearImage(BufferedImage img) {
1173N/A Graphics2D g2 = img.createGraphics();
1173N/A g2.setComposite(AlphaComposite.Clear);
1173N/A g2.fillRect(0, 0, img.getWidth(), img.getHeight());
1173N/A g2.dispose();
1173N/A }
1173N/A
1173N/A // =================================================================================================================
1173N/A // Blur
1173N/A
1173N/A /**
1173N/A * Apply Gaussian Blur to Image
1173N/A *
1173N/A * @param src The image tp
1173N/A * @param dst The destination image to draw blured src image into, null if you want a new one created
1173N/A * @param radius The blur kernel radius
1173N/A * @return The blured image
1173N/A */
1173N/A static BufferedImage gaussianBlur(BufferedImage src, BufferedImage dst, int radius) {
1173N/A int width = src.getWidth();
1173N/A int height = src.getHeight();
1173N/A if (dst == null || dst.getWidth() != width || dst.getHeight() != height || src.getType() != dst.getType()) {
1173N/A dst = createColorModelCompatibleImage(src);
1173N/A }
1173N/A float[] kernel = createGaussianKernel(radius);
1173N/A if (src.getType() == BufferedImage.TYPE_INT_ARGB) {
1173N/A int[] srcPixels = new int[width * height];
1173N/A int[] dstPixels = new int[width * height];
1173N/A getPixels(src, 0, 0, width, height, srcPixels);
1173N/A // horizontal pass
1173N/A blur(srcPixels, dstPixels, width, height, kernel, radius);
1173N/A // vertical pass
1173N/A //noinspection SuspiciousNameCombination
1173N/A blur(dstPixels, srcPixels, height, width, kernel, radius);
1173N/A // the result is now stored in srcPixels due to the 2nd pass
1173N/A setPixels(dst, 0, 0, width, height, srcPixels);
1173N/A } else if (src.getType() == BufferedImage.TYPE_BYTE_GRAY) {
1173N/A byte[] srcPixels = new byte[width * height];
1173N/A byte[] dstPixels = new byte[width * height];
1173N/A getPixels(src, 0, 0, width, height, srcPixels);
1173N/A // horizontal pass
1173N/A blur(srcPixels, dstPixels, width, height, kernel, radius);
1173N/A // vertical pass
1173N/A //noinspection SuspiciousNameCombination
1173N/A blur(dstPixels, srcPixels, height, width, kernel, radius);
1173N/A // the result is now stored in srcPixels due to the 2nd pass
1173N/A setPixels(dst, 0, 0, width, height, srcPixels);
1173N/A } else {
1173N/A throw new IllegalArgumentException("EffectUtils.gaussianBlur() src image is not a supported type, type=[" +
1173N/A src.getType() + "]");
1173N/A }
1173N/A return dst;
1173N/A }
1173N/A
1173N/A /**
1173N/A * <p>Blurs the source pixels into the destination pixels. The force of the blur is specified by the radius which
1173N/A * must be greater than 0.</p> <p>The source and destination pixels arrays are expected to be in the INT_ARGB
1173N/A * format.</p> <p>After this method is executed, dstPixels contains a transposed and filtered copy of
1173N/A * srcPixels.</p>
1173N/A *
1173N/A * @param srcPixels the source pixels
1173N/A * @param dstPixels the destination pixels
1173N/A * @param width the width of the source picture
1173N/A * @param height the height of the source picture
1173N/A * @param kernel the kernel of the blur effect
1173N/A * @param radius the radius of the blur effect
1173N/A */
1173N/A private static void blur(int[] srcPixels, int[] dstPixels,
1173N/A int width, int height,
1173N/A float[] kernel, int radius) {
1173N/A float a;
1173N/A float r;
1173N/A float g;
1173N/A float b;
1173N/A
1173N/A int ca;
1173N/A int cr;
1173N/A int cg;
1173N/A int cb;
1173N/A
1173N/A for (int y = 0; y < height; y++) {
1173N/A int index = y;
1173N/A int offset = y * width;
1173N/A
1173N/A for (int x = 0; x < width; x++) {
1173N/A a = r = g = b = 0.0f;
1173N/A
1173N/A for (int i = -radius; i <= radius; i++) {
1173N/A int subOffset = x + i;
1173N/A if (subOffset < 0 || subOffset >= width) {
1173N/A subOffset = (x + width) % width;
1173N/A }
1173N/A
1173N/A int pixel = srcPixels[offset + subOffset];
1173N/A float blurFactor = kernel[radius + i];
1173N/A
1173N/A a += blurFactor * ((pixel >> 24) & 0xFF);
1173N/A r += blurFactor * ((pixel >> 16) & 0xFF);
1173N/A g += blurFactor * ((pixel >> 8) & 0xFF);
1173N/A b += blurFactor * ((pixel) & 0xFF);
1173N/A }
1173N/A
1173N/A ca = (int) (a + 0.5f);
1173N/A cr = (int) (r + 0.5f);
1173N/A cg = (int) (g + 0.5f);
1173N/A cb = (int) (b + 0.5f);
1173N/A
1173N/A dstPixels[index] = ((ca > 255 ? 255 : ca) << 24) |
1173N/A ((cr > 255 ? 255 : cr) << 16) |
1173N/A ((cg > 255 ? 255 : cg) << 8) |
1173N/A (cb > 255 ? 255 : cb);
1173N/A index += height;
1173N/A }
1173N/A }
1173N/A }
1173N/A
1173N/A /**
1173N/A * <p>Blurs the source pixels into the destination pixels. The force of the blur is specified by the radius which
1173N/A * must be greater than 0.</p> <p>The source and destination pixels arrays are expected to be in the BYTE_GREY
1173N/A * format.</p> <p>After this method is executed, dstPixels contains a transposed and filtered copy of
1173N/A * srcPixels.</p>
1173N/A *
1173N/A * @param srcPixels the source pixels
1173N/A * @param dstPixels the destination pixels
1173N/A * @param width the width of the source picture
1173N/A * @param height the height of the source picture
1173N/A * @param kernel the kernel of the blur effect
1173N/A * @param radius the radius of the blur effect
1173N/A */
1173N/A static void blur(byte[] srcPixels, byte[] dstPixels,
1173N/A int width, int height,
1173N/A float[] kernel, int radius) {
1173N/A float p;
1173N/A int cp;
1173N/A for (int y = 0; y < height; y++) {
1173N/A int index = y;
1173N/A int offset = y * width;
1173N/A for (int x = 0; x < width; x++) {
1173N/A p = 0.0f;
1173N/A for (int i = -radius; i <= radius; i++) {
1173N/A int subOffset = x + i;
1173N/A// if (subOffset < 0) subOffset = 0;
1173N/A// if (subOffset >= width) subOffset = width-1;
1173N/A if (subOffset < 0 || subOffset >= width) {
1173N/A subOffset = (x + width) % width;
1173N/A }
1173N/A int pixel = srcPixels[offset + subOffset] & 0xFF;
1173N/A float blurFactor = kernel[radius + i];
1173N/A p += blurFactor * pixel;
1173N/A }
1173N/A cp = (int) (p + 0.5f);
1173N/A dstPixels[index] = (byte) (cp > 255 ? 255 : cp);
1173N/A index += height;
1173N/A }
1173N/A }
1173N/A }
1173N/A
1173N/A static float[] createGaussianKernel(int radius) {
1173N/A if (radius < 1) {
1173N/A throw new IllegalArgumentException("Radius must be >= 1");
1173N/A }
1173N/A
1173N/A float[] data = new float[radius * 2 + 1];
1173N/A
1173N/A float sigma = radius / 3.0f;
1173N/A float twoSigmaSquare = 2.0f * sigma * sigma;
1173N/A float sigmaRoot = (float) Math.sqrt(twoSigmaSquare * Math.PI);
1173N/A float total = 0.0f;
1173N/A
1173N/A for (int i = -radius; i <= radius; i++) {
1173N/A float distance = i * i;
1173N/A int index = i + radius;
1173N/A data[index] = (float) Math.exp(-distance / twoSigmaSquare) / sigmaRoot;
1173N/A total += data[index];
1173N/A }
1173N/A
1173N/A for (int i = 0; i < data.length; i++) {
1173N/A data[i] /= total;
1173N/A }
1173N/A
1173N/A return data;
1173N/A }
1173N/A
1173N/A // =================================================================================================================
1173N/A // Get/Set Pixels helper methods
1173N/A
1173N/A /**
1173N/A * <p>Returns an array of pixels, stored as integers, from a <code>BufferedImage</code>. The pixels are grabbed from
1173N/A * a rectangular area defined by a location and two dimensions. Calling this method on an image of type different
1173N/A * from <code>BufferedImage.TYPE_INT_ARGB</code> and <code>BufferedImage.TYPE_INT_RGB</code> will unmanage the
1173N/A * image.</p>
1173N/A *
1173N/A * @param img the source image
1173N/A * @param x the x location at which to start grabbing pixels
1173N/A * @param y the y location at which to start grabbing pixels
1173N/A * @param w the width of the rectangle of pixels to grab
1173N/A * @param h the height of the rectangle of pixels to grab
1173N/A * @param pixels a pre-allocated array of pixels of size w*h; can be null
1173N/A * @return <code>pixels</code> if non-null, a new array of integers otherwise
1173N/A * @throws IllegalArgumentException is <code>pixels</code> is non-null and of length &lt; w*h
1173N/A */
1173N/A static byte[] getPixels(BufferedImage img,
1173N/A int x, int y, int w, int h, byte[] pixels) {
1173N/A if (w == 0 || h == 0) {
1173N/A return new byte[0];
1173N/A }
1173N/A
1173N/A if (pixels == null) {
1173N/A pixels = new byte[w * h];
1173N/A } else if (pixels.length < w * h) {
1173N/A throw new IllegalArgumentException("pixels array must have a length >= w*h");
1173N/A }
1173N/A
1173N/A int imageType = img.getType();
1173N/A if (imageType == BufferedImage.TYPE_BYTE_GRAY) {
1173N/A Raster raster = img.getRaster();
1173N/A return (byte[]) raster.getDataElements(x, y, w, h, pixels);
1173N/A } else {
1173N/A throw new IllegalArgumentException("Only type BYTE_GRAY is supported");
1173N/A }
1173N/A }
1173N/A
1173N/A /**
1173N/A * <p>Writes a rectangular area of pixels in the destination <code>BufferedImage</code>. Calling this method on an
1173N/A * image of type different from <code>BufferedImage.TYPE_INT_ARGB</code> and <code>BufferedImage.TYPE_INT_RGB</code>
1173N/A * will unmanage the image.</p>
1173N/A *
1173N/A * @param img the destination image
1173N/A * @param x the x location at which to start storing pixels
1173N/A * @param y the y location at which to start storing pixels
1173N/A * @param w the width of the rectangle of pixels to store
1173N/A * @param h the height of the rectangle of pixels to store
1173N/A * @param pixels an array of pixels, stored as integers
1173N/A * @throws IllegalArgumentException is <code>pixels</code> is non-null and of length &lt; w*h
1173N/A */
1173N/A static void setPixels(BufferedImage img,
1173N/A int x, int y, int w, int h, byte[] pixels) {
1173N/A if (pixels == null || w == 0 || h == 0) {
1173N/A return;
1173N/A } else if (pixels.length < w * h) {
1173N/A throw new IllegalArgumentException("pixels array must have a length >= w*h");
1173N/A }
1173N/A int imageType = img.getType();
1173N/A if (imageType == BufferedImage.TYPE_BYTE_GRAY) {
1173N/A WritableRaster raster = img.getRaster();
1173N/A raster.setDataElements(x, y, w, h, pixels);
1173N/A } else {
1173N/A throw new IllegalArgumentException("Only type BYTE_GRAY is supported");
1173N/A }
1173N/A }
1173N/A
1173N/A /**
1173N/A * <p>Returns an array of pixels, stored as integers, from a
1173N/A * <code>BufferedImage</code>. The pixels are grabbed from a rectangular
1173N/A * area defined by a location and two dimensions. Calling this method on
1173N/A * an image of type different from <code>BufferedImage.TYPE_INT_ARGB</code>
1173N/A * and <code>BufferedImage.TYPE_INT_RGB</code> will unmanage the image.</p>
1173N/A *
1173N/A * @param img the source image
1173N/A * @param x the x location at which to start grabbing pixels
1173N/A * @param y the y location at which to start grabbing pixels
1173N/A * @param w the width of the rectangle of pixels to grab
1173N/A * @param h the height of the rectangle of pixels to grab
1173N/A * @param pixels a pre-allocated array of pixels of size w*h; can be null
1173N/A * @return <code>pixels</code> if non-null, a new array of integers
1173N/A * otherwise
1173N/A * @throws IllegalArgumentException is <code>pixels</code> is non-null and
1173N/A * of length &lt; w*h
1173N/A */
1173N/A public static int[] getPixels(BufferedImage img,
1173N/A int x, int y, int w, int h, int[] pixels) {
1173N/A if (w == 0 || h == 0) {
1173N/A return new int[0];
1173N/A }
1173N/A
1173N/A if (pixels == null) {
1173N/A pixels = new int[w * h];
1173N/A } else if (pixels.length < w * h) {
1173N/A throw new IllegalArgumentException("pixels array must have a length" +
1173N/A " >= w*h");
1173N/A }
1173N/A
1173N/A int imageType = img.getType();
1173N/A if (imageType == BufferedImage.TYPE_INT_ARGB ||
1173N/A imageType == BufferedImage.TYPE_INT_RGB) {
1173N/A Raster raster = img.getRaster();
1173N/A return (int[]) raster.getDataElements(x, y, w, h, pixels);
1173N/A }
1173N/A
1173N/A // Unmanages the image
1173N/A return img.getRGB(x, y, w, h, pixels, 0, w);
1173N/A }
1173N/A
1173N/A /**
1173N/A * <p>Writes a rectangular area of pixels in the destination
1173N/A * <code>BufferedImage</code>. Calling this method on
1173N/A * an image of type different from <code>BufferedImage.TYPE_INT_ARGB</code>
1173N/A * and <code>BufferedImage.TYPE_INT_RGB</code> will unmanage the image.</p>
1173N/A *
1173N/A * @param img the destination image
1173N/A * @param x the x location at which to start storing pixels
1173N/A * @param y the y location at which to start storing pixels
1173N/A * @param w the width of the rectangle of pixels to store
1173N/A * @param h the height of the rectangle of pixels to store
1173N/A * @param pixels an array of pixels, stored as integers
1173N/A * @throws IllegalArgumentException is <code>pixels</code> is non-null and
1173N/A * of length &lt; w*h
1173N/A */
1173N/A public static void setPixels(BufferedImage img,
1173N/A int x, int y, int w, int h, int[] pixels) {
1173N/A if (pixels == null || w == 0 || h == 0) {
1173N/A return;
1173N/A } else if (pixels.length < w * h) {
1173N/A throw new IllegalArgumentException("pixels array must have a length" +
1173N/A " >= w*h");
1173N/A }
1173N/A
1173N/A int imageType = img.getType();
1173N/A if (imageType == BufferedImage.TYPE_INT_ARGB ||
1173N/A imageType == BufferedImage.TYPE_INT_RGB) {
1173N/A WritableRaster raster = img.getRaster();
1173N/A raster.setDataElements(x, y, w, h, pixels);
1173N/A } else {
1173N/A // Unmanages the image
1173N/A img.setRGB(x, y, w, h, pixels, 0, w);
1173N/A }
1173N/A }
1173N/A
1173N/A /**
1173N/A * <p>Returns a new <code>BufferedImage</code> using the same color model
1173N/A * as the image passed as a parameter. The returned image is only compatible
1173N/A * with the image passed as a parameter. This does not mean the returned
1173N/A * image is compatible with the hardware.</p>
1173N/A *
1173N/A * @param image the reference image from which the color model of the new
1173N/A * image is obtained
1173N/A * @return a new <code>BufferedImage</code>, compatible with the color model
1173N/A * of <code>image</code>
1173N/A */
1173N/A public static BufferedImage createColorModelCompatibleImage(BufferedImage image) {
1173N/A ColorModel cm = image.getColorModel();
1173N/A return new BufferedImage(cm,
1173N/A cm.createCompatibleWritableRaster(image.getWidth(),
1173N/A image.getHeight()),
1173N/A cm.isAlphaPremultiplied(), null);
1173N/A }
1173N/A
1173N/A /**
1173N/A * <p>Returns a new translucent compatible image of the specified width and
1173N/A * height. That is, the returned <code>BufferedImage</code> is compatible with
1173N/A * the graphics hardware. If the method is called in a headless
1173N/A * environment, then the returned BufferedImage will be compatible with
1173N/A * the source image.</p>
1173N/A *
1173N/A * @param width the width of the new image
1173N/A * @param height the height of the new image
1173N/A * @return a new translucent compatible <code>BufferedImage</code> of the
1173N/A * specified width and height
1173N/A */
1173N/A public static BufferedImage createCompatibleTranslucentImage(int width,
1173N/A int height) {
1173N/A return isHeadless() ?
1173N/A new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB) :
1173N/A getGraphicsConfiguration().createCompatibleImage(width, height,
1173N/A Transparency.TRANSLUCENT);
1173N/A }
1173N/A
1173N/A private static boolean isHeadless() {
1173N/A return GraphicsEnvironment.isHeadless();
1173N/A }
1173N/A
1173N/A // Returns the graphics configuration for the primary screen
1173N/A private static GraphicsConfiguration getGraphicsConfiguration() {
1173N/A return GraphicsEnvironment.getLocalGraphicsEnvironment().
1173N/A getDefaultScreenDevice().getDefaultConfiguration();
1173N/A }
1173N/A
1173N/A}