ここで説明するように、クラスBufferedImage
を使用して各ピクセルの青い部分を反転するために使用されるアルゴリズムを実装しようとしています。私の試みの結果、このメソッドが作成されました。BufferedImageOp
private BufferedImage getInvertedVersion(BufferedImage source) {
short[] invert = new short[256];
short[] straight = new short[256];
for (int i = 0; i < 256; i++) {
invert[i] = (short)(255 - i);
straight[i] = (short)i;
}
short[][] blueInvert = new short[][] { straight, straight, invert }; //Red stays the same, Green stays the same, Blue is inverted
BufferedImageOp blueInvertOp = new LookupOp(new ShortLookupTable(0, blueInvert), null);
//This produces error #1 when uncommented
/*blueInvertOp.filter(source, source);
return source;*/
//This produces error #2 instead when uncommented
/*return blueInvertOp.filter(source, null);*/
}
ただし、クラスの.filterメソッドを呼び出すと、チャネル数またはバイト数に関連するエラーが発生しますBufferedImageOp
。上記のコードのコメント付きセクションでは、それぞれのエラーが発生します。
エラー#1:
Exception in thread "AWT-EventQueue-0" 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(LookupOp.java:273)
at java.awt.image.LookupOp.filter(LookupOp.java:221)
エラー#2:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Number of color/alpha components should be 4 but length of bits array is 2
at java.awt.image.ColorModel.<init>(ColorModel.java:336)
at java.awt.image.ComponentColorModel.<init>(ComponentColorModel.java:273)
at java.awt.image.LookupOp.createCompatibleDestImage(LookupOp.java:413)
リンクのコードは非常に古いので(1998年に書かれました!)、それ以降に何かが変更されたと思います。そのため、コードは機能しなくなりました。しかし、私の主な関心事である概念をほぼ同様に説明する別の情報源を見つけることができませんでした。
これらのエラーの意味と修正方法を誰かが説明できますか?または、さらに良いことに、画像を操作する方法についての、より最新でありながら徹底的なチュートリアルを教えてください。