001: package snow.utils.gui;
002:
003: import java.util.Arrays;
004: import javax.swing.*;
005: import java.awt.*;
006: import java.awt.geom.*;
007: import java.awt.image.*;
008:
009: /** image utilities
010: */
011: public final class ImageUtils {
012: private ImageUtils() {
013: }
014:
015: /** create a gray image
016: */
017: public static BufferedImage gradientEngray(BufferedImage source,
018: float level) {
019: BufferedImage dest = new BufferedImage(source.getWidth(),
020: source.getHeight(), BufferedImage.TYPE_INT_ARGB);
021: AlphaComposite ac = AlphaComposite.getInstance(
022: AlphaComposite.SRC_OVER, level);
023: Graphics2D g = dest.createGraphics();
024: g.drawImage(source, null, 0, 0);
025: g.setComposite(ac);
026: GradientPaint gp = new GradientPaint(0f, 0f, Color.white,
027: source.getWidth(), source.getHeight(), Color.lightGray);
028: g.setPaint(gp);
029: g.fill(g.getClipBounds());
030: g.dispose();
031: return dest;
032: }
033:
034: /**BufferedImage.TYPE_BYTE_BINARY: black and white (png: 21k)
035: * BufferedImage.TYPE_USHORT_555_RGB (png: 260k)
036: * BufferedImage.TYPE_BYTE_INDEXED (260k)
037: * BufferedImage.TYPE_BYTE_GRAY
038: */
039:
040: public static BufferedImage reduceBits(BufferedImage im, int type) {
041: BufferedImage ret = new BufferedImage(im.getWidth(), im
042: .getHeight(), type);
043: //BufferedImage.TYPE_BYTE_GRAY);
044: ret.getGraphics().drawImage(im, 0, 0, null);
045: return ret;
046: }
047:
048: public static BufferedImage reduceBits(BufferedImage im) {
049: BufferedImage ret = new BufferedImage(im.getWidth(), im
050: .getHeight(), BufferedImage.TYPE_BYTE_INDEXED);
051: //BufferedImage.TYPE_BYTE_GRAY);
052: ret.getGraphics().drawImage(im, 0, 0, null);
053: return ret;
054: }
055:
056: public static boolean imageEquals(BufferedImage im1,
057: BufferedImage im2) {
058: return imageEquals_byte(reduceBits(im1), reduceBits(im2));
059: }
060:
061: /** May be parallelized easilly ...
062: */
063: private static boolean imageEquals_byte(BufferedImage im1,
064: BufferedImage im2) {
065: int blocW = 128;
066: int blocH = 128;
067:
068: final Raster r1 = im1.getData();
069: final Raster r2 = im2.getData();
070:
071: int n = 0;
072: int normalBlocSize = r1.getNumBands() * blocW * blocH;
073:
074: // not quicker...
075: byte[] p1 = new byte[normalBlocSize]; // reuse them
076: byte[] p2 = new byte[normalBlocSize];
077:
078: for (int i = 0; i < im1.getWidth(); i += blocW) {
079: for (int j = 0; j < im1.getHeight(); j += blocH) {
080: n++;
081:
082: int w = blocW;
083: if (i + w > im1.getWidth()) {
084: p1 = null; // IMPORTANT
085: p2 = null; // IMPORTANT
086: w = im1.getWidth() - i;
087: }
088:
089: int h = blocH;
090: if (j + h > im1.getHeight()) {
091: p1 = null; // IMPORTANT
092: p2 = null;
093: h = im1.getHeight() - j;
094: }
095:
096: if (w == 0 || h == 0)
097: continue;
098:
099: if (p1 != null) {
100: if (p1.length != normalBlocSize) {
101: p1 = null;
102: }
103: }
104:
105: if (p2 != null) {
106: if (p2.length != normalBlocSize) {
107: p2 = null;
108: }
109: }
110:
111: p1 = (byte[]) r1.getDataElements(i, j, w, h, p1); // getPixels
112: p2 = (byte[]) r2.getDataElements(i, j, w, h, p2);
113: if (!Arrays.equals(p1, p2)) {
114: return false;
115: }
116: }
117: }
118:
119: return true;
120: }
121:
122: }
|