Java 从调色板颜色数组创建图像的最佳方法+;指示字节数组?
我正在开发一个Java组件来显示一些视频,对于视频的每一帧,我的解码器给我一个颜色[256]调色板+一个宽度*高度字节像素索引数组。下面是我现在如何创建我的Java 从调色板颜色数组创建图像的最佳方法+;指示字节数组?,java,image,indices,palette,Java,Image,Indices,Palette,我正在开发一个Java组件来显示一些视频,对于视频的每一帧,我的解码器给我一个颜色[256]调色板+一个宽度*高度字节像素索引数组。下面是我现在如何创建我的buffereImage: byte[] iArray = new byte[width * height * 3]; int j = 0; for (byte i : this.lastFrameData) { iArray[j] = (byte) this.currentPalette[i & 0xFF].getRed()
buffereImage
:
byte[] iArray = new byte[width * height * 3];
int j = 0;
for (byte i : this.lastFrameData) {
iArray[j] = (byte) this.currentPalette[i & 0xFF].getRed();
iArray[j + 1] = (byte) this.currentPalette[i & 0xFF].getGreen();
iArray[j + 2] = (byte) this.currentPalette[i & 0xFF].getBlue();
j += 3;
}
DataBufferByte dbb = new DataBufferByte(iArray, iArray.length);
ColorModel cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[] { 8, 8, 8 }, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
return new BufferedImage(cm, Raster.createInterleavedRaster(dbb, width, height, width * 3, 3, new int[] { 0, 1, 2 }, null), false, null);
这是可行的,但看起来很难看,我相信有更好的办法。那么,创建buffereImage
的最快方法是什么呢
/编辑:我曾尝试直接在我的BuffereImage上使用setRGB方法,但它导致的性能比上述方法更差
谢谢我会这样做:
int[] imagePixels = new int[width * height]
int j = 0;
for (byte i : this.lastFrameData) {
byte r = (byte) this.currentPalette[i & 0xFF].getRed();
byte g = (byte) this.currentPalette[i & 0xFF].getGreen();
byte b = (byte) this.currentPalette[i & 0xFF].getBlue();
imagePixels[j] = 0xFF000000 | (r<<16) | (g<<8) | b;
j++;
}
BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
result.setRGB(0, 0, width, height, imagePixels , 0, width);
return result;
int[]imagePixels=新int[宽度*高度]
int j=0;
for(字节i:this.lastFrameData){
字节r=(字节)this.currentPalette[i&0xFF].getRed();
字节g=(字节)this.currentPalette[i&0xFF].getGreen();
字节b=(字节)this.currentPalette[i&0xFF].getBlue();
图像像素[j]=0xFF000000|(这似乎是一个很好的方法。你有什么特别的担心吗?我主要关心性能,但你似乎是正确的,因为这是最快的方法。我比较了我的解决方案和你的解决方案,结果如下:16秒解码2039帧22秒解码2039帧似乎setRBG比使用如上所述的颜色模型。thx:)