BufferedImageOps: creating compatible RGBA outputs?
Slightly longer code snippet for this one:
public LookupOp maketable(Color target) {
short[] red = new short[256];
short[] green = new short[256];
short[] blue = new short[256];
short[] alpha = new short[256];
for (int i = 0; i < 256; i++) {
alpha[i] = (short) i;
red[i] = (short) ((double) target.getRed() * i / 256);
green[i] = (short) ((double) target.getGreen() * i / 256);
blue[i] = (short) ((double) target.getBlue() * i / 256);
}
short[][] data = { red, green, blue };
LookupTable lookupTable = new ShortLookupTable(0, data);
return new LookupOp(lookupTable, null);
}
public class GameArea extends JPanel {
public Color colour = new Color(0,0,0);
BufferedImage image = null;
BufferedImage outimage = null;
AffineTransform trans = new AffineTransform();
ColorConvertOp conv = null;
public GameArea() {
setPreferredSize(new Dimension(600, 600));
setBackground(new Color(0, 0, 5));
try {
image = ImageIO.read(new File("transTest.png"));
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void paintComponent(Graphics g) {
try {
BufferedImageOp filter = maketable(colour);
outimage = filter.createCompatibleDestImage(image, ColorModel.getRGBdefault());
filter.filter(image, outimage);
trans.setToScale((double) getWidth() / (double) image.getWidth(),
(double) getHeight() / (double) image.getHeight());
((Graphics2D) g).drawRenderedImage(outimage, trans);
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Refresher extends Thread {
//refreshes thread and changes the colour in the gamearea to make a fade between filters
}
I'm doing some tests in preparation for a larger topic and familiarising myself with images, colours and filters.
The above example takes a simple image, and fades it through various colourisations, scaling it to fit into whatever size the window is. It has worked for some files after I took out the alpha channel on the LookupOp (when using jpgs), but for others I get the following exception:
java.lang.IllegalArgumentException: Number of channels in the src (4) does not match number of channels in the destination (2) at java.awt.image.LookupOp.filter(Unknown Source) at java.awt.image.LookupOp.filter(Unknown Source) at objects.MainWindow$GameArea.paintComponent(MainWindow.java:72) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JComponent.paintToOffscreen(Unknown Source)
I suspect the problem is the number of channels in the source image is not compatible with my lookup table (which i want to be a standard RGBA 32 bit system) I've managed to make it work for a flat JPG by making the dest colorModel match the source colorModel, but this doesnt work for the PNG I'm testing with (transparency is important for my project)
What is the usual way to deal with different image colorModels? Is there a sensible way to convert an image into a compatible RGBA model?
edit: I can resolve this for .png by adding the alpha channel back into the make table (so it's at 4 channels. However, this breaks it again for jpgs. How do you detect the number (an开发者_JS百科d type) of channels in a colorModel?
K.Barad
精彩评论