1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
package test;
import java.awt.*;
import java.awt.image.*;
import java.awt.datatransfer.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.imageio.*;
import java.awt.BorderLayout;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class TestTransparency extends JPanel {
private Image image;
public TestTransparency(String file) throws Exception {
this(ImageIO.read(new File(file)));
}
public TestTransparency(Image image) throws Exception {
this.image = image;
setPreferredSize(new Dimension(image.getWidth(null), image.getHeight(null)));
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (image != null) {
g.drawImage(image, 0, 0, null);
}
}
public static void main(String ...args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
try {
frame.add(new TestTransparency("fbavatar.gif"));
/*
BufferedImage image = ImageIO.read(new File("fbavatar.gif"));
short[] invert = new short[256];
for (int i = 0; i < 256; i++) {
invert[i] = (short) (255 - i);
}
BufferedImageOp invertOp = new LookupOp(new ShortLookupTable(0, invert), null);
image = invertOp.filter(image, null);
*/
// Get RGB.
BufferedImage image2 = ImageIO.read(new File("fbavatar.gif"));
int[] rgb = image2.getRGB(0, 0, image2.getWidth(), image2.getHeight(), null, 0, image2.getWidth());
for (int i = 0; i < rgb.length; i++) {
int c = rgb[i];
int a = (c >> 24) & 0xFF;
int r = (c >> 16) & 0xFF;
int g = (c >> 8) & 0xFF;
int b = (c >> 0) & 0xFF;
if ((g == 24) && (b == 181)) {
c = (a << 24) | (r << 16) | (b << 8) | (g << 0);
rgb[i] = c;
System.out.println("old G: "+g+", old B: "+b);
System.out.println("new G: "+((c >> 8) & 0xFF)+", new B: "+((c >> 0) & 0xFF));
}
}
image2.setRGB(0, 0, image2.getWidth(), image2.getHeight(), rgb, 0, image2.getWidth());
frame.add(new TestTransparency(image2));
// Index Color Model.
BufferedImage image3 = ImageIO.read(new File("fbavatar.gif"));
IndexColorModel cm = (IndexColorModel)image3.getColorModel();
int size = cm.getMapSize();
System.out.println(size);
int[] cmap = new int[size];
rgb = null;
for (int i = 0 ; i < size ; i++) {
rgb = cm.getComponents(i, rgb, 0);
if (i == 102) {
System.out.printf("%d, R: %d, G: %d, B: %d, A: %d\n", rgb.length, rgb[0], rgb[1], rgb[2], rgb[3]);
int green = rgb[1];
int blue = rgb[2];
rgb[1] = blue;
rgb[2] = green;
}
int c = (rgb[3] << 24) | (rgb[0] << 16) | (rgb[1] << 8) | (rgb[2] << 0);
cmap[i] = c;
}
int bits = cm.getPixelSize();
int start = 0;
boolean hasAlpha = cm.hasAlpha();
int trans = cm.getTransparentPixel();
int transferType = cm.getTransferType();
IndexColorModel cm2 = new IndexColorModel(bits, size, cmap, start, hasAlpha, trans, transferType);
image3 = new BufferedImage(cm2, (WritableRaster)image3.getData(), false, null);
frame.add(new TestTransparency(image3));
// RGB Filter.
BufferedImage image4 = ImageIO.read(new File("fbavatar.gif"));
RGBImageFilter filter = new RGBImageFilter() {
/** {@inheritDoc}
*/
@Override public int filterRGB(int x, int y, int rgb) {
int a = (rgb >> 24) & 0xFF;
int r = (rgb >> 16) & 0xFF;
int g = (rgb >> 8) & 0xFF;
int b = (rgb >> 0) & 0xFF;
if ((g == 24) && (b == 181)) {
rgb = (a << 24) | (r << 16) | (b << 8) | (g << 0);
}
return rgb;
}
};
FilteredImageSource filteredSrc = new FilteredImageSource(image4.getSource(), filter);
// Create the filtered image
Image image5 = Toolkit.getDefaultToolkit().createImage(filteredSrc);
frame.add(new TestTransparency(image5));
}
catch (Exception e) {
e.printStackTrace();
}
frame.setVisible(true);
frame.pack();
}
});
}
} |
Partager