2012-10-12 2 views
3

here에 설명 된대로 BufferedImage 클래스의 각 픽셀의 파란색 부분을 반전하는 알고리즘을 BufferedImageOp 클래스를 사용하여 구현하려고합니다. 내 시도는이 방법의 생성 결과 :BufferedImageOp.filter() Throwing Channel Errors

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);*/ 
} 

을하지만, 나는 내 BufferedImageOp 클래스의 .filter 메서드를 호출 할 때 채널 또는 바이트 수와 관련된 오류를 받고 있어요.

오류 # 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 년에!) 그래서 나는 그때부터 코드가 더 이상 작동하지 않는 이유가 무엇인가가 바뀌 었다고 가정합니다. 그러나, 나는 개념을 거의 설명하는 또 다른 출처를 찾을 수 없었다. 이것은 나의 가장 중요한 관심사이다.

누구든지이 오류의 의미와 해결 방법을 설명 할 수 있습니까? 또는 더 나은 방법은 이미지를 조작하는 방법에 대한 최신 정보를 제공하지만 여전히 철저한 자습서입니다.

답변

6

나는 똑같은 것을 만났습니다 ... 그리고 대답은 찾고있는 컬러 모델로 대상 이미지를 만드는 것입니다. 또한 짧은 [] [] 데이터에는 알파 채널의 크기가 포함되어야합니다. 이것은 나를 위해 일한

short[] red = new short[256]; 
short[] green = new short[256]; 
short[] blue = new short[256]; 
short[] alpha = new short[256]; 

for (short i = 0; i < 256; i++) { 
    green[i] = blue[i] = 0; 
    alpha[i] = red[i] = i; 
} 
short[][] data = new short[][] { 
    red, green, blue, alpha 
}; 

LookupTable lookupTable = new ShortLookupTable(0, data); 
LookupOp op = new LookupOp(lookupTable, null); 
BufferedImage destinationImage = new BufferedImage(24, 24, BufferedImage.TYPE_INT_ARGB); 
destinationImage = op.filter(sourceImage, destinationImage); 

: 여기 재판을 통해 우연히 코드 및 오류 작업의 내 예입니다.