2013-09-25 18 views
1

我正在學習在java中使用BufferedImages,並試圖創建一個動畫,其中動畫的每一幀都是數學上擺弄像素數據的結果。我只是在玩耍而已。最初我使用了一個索引的ColorModel,但我已經將它改變(以利用更多的顏色)到一個直接的ColorModel。但現在一個錯誤影響了說 -使用BufferedImages創建WritableRaster時,如何確保它與特定的ColorModel兼容?

光柵[email protected]是ColorModel的DirectColorModel不兼容:rmask = FF0000的GMask = FF00 bmask = FF AMASK = FF000000

我使用的代碼創建的BufferedImage和WriteableRaster是在這裏:

public void initialize(){ 
    int width = getSize().width; 
    int height = getSize().height; 
    data = new int [width * height]; 

    DataBuffer db = new DataBufferInt(data,height * width); 
    WritableRaster wr = Raster.createPackedRaster(db,width,height,1,null); 
    image = new BufferedImage(ColorModel.getRGBdefault(),wr,false,null); 
    image.setRGB(0, 0, width, height, data, 0, width); 
} 

回答

1

,最簡單的方式,以確保你有一個WritableRaster是兼容的ColorModel是先選擇顏色模型,然後從它創建柵格像這樣:

ColorModel colorModel = ColorModel.getRGBdefault(); // Or any other color model 
WritableRaster raster = colorModel.createCompatibleWritableRaster(width, height); 

然而,這可能是不實際的,比如像你來自哪裏,現有陣列創建DataBuffer你的情況。在這種情況下,我真的建議望着java.awt.image.BufferedImage建設者和不同ColorModel實現的createCompatibleWritableRaster方法的源代碼(這是我自學如何做到這一點:-)方式。它顯示了可以很好地協同工作的最常見的光柵和顏色模型組合。

你行:

Raster.createPackedRaster(db,width,height,1,null); 

...似乎創建柵格每像素MultiPixelPackedSampleModel和1位......這兩者都是可能與RGB顏色模式不兼容。你想可能需要定義:這是

int[] masks = new int[]{0xff0000, 0xff00, 0xff}; // Add 0xff000000 if you want alpha 
Raster.createPackedRaster(db, width, height, width, masks, null); 

PS:你不應該需要做image.setRGB您的代碼的最後一行,因爲圖像是使用你的data數組作爲後盾緩衝了。

相關問題