BufferedImage Ops:创建兼容的RGB输出?

时间:2011-03-07 11:32:15

标签: java image colors

这个代码片段稍微长一些:

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    
}

我正在做一些测试,以准备更大的主题并熟悉图像,颜色和过滤器。

上面的示例采用了一个简单的图像,并通过各种颜色淡化它,缩放它以适应窗口的任何大小。在我取出LookupOp上的alpha通道(使用jpgs)后,它已经用于某些文件,但是对于其他人我得到以下异常:

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)

我怀疑问题是源图像中的通道数与我的查找表(我想成为标准的RGBA 32位系统)不兼容我已经设法通过制作使其适用于平面JPG dest colorModel匹配源colorModel,但这对我正在测试的PNG不起作用(透明度对我的项目很重要)

处理不同图像颜色模型的常用方法是什么?有没有合理的方法将图像转换为兼容的RGBA模型?

编辑:我可以通过将alpha通道添加回make表来解决此问题(因此它在4个通道中。但是,这会再次打破jpgs。如何检测通道的数量(和类型)在colorModel?

K.Barad

0 个答案:

没有答案