繁体   English   中英

从调色板颜色数组+索引字节数组创建图像的最佳方法?

[英]Best way to create Image from palette Color array + indice byte array?

我正在开发一个Java组件来显示一些视频,对于视频的每一帧,我的解码器为我提供了Color [256]调色板+宽*高字节像素索引数组。 这是我现在创建BufferedImage

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);

这可行,但是看起来很丑,我相信有更好的方法。 那么,创建BufferedImage的最快方法是什么?

/编辑:我尝试直接在我的BufferedImage上使用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;

也许速度更快,请不要进行测试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM