从调色板创建图像的最佳方法Color array + indice byte array? [英] Best way to create Image from palette Color array + indice byte array?
问题描述
我正在开发一个Java组件来显示一些视频和视频的每一帧,我的解码器给了我一个Color [256]调色板+一个宽*高的字节像素索引数组。以下是我现在如何创建 BufferedImage
:
I'm developing a Java component for displaying some videos and for each frame of the video, my decoder gives me a Color[256] palette + a width*height bytes pixel indices array. Here's how I create my BufferedImage
right now:
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
的最快方法是什么呢?
This works but it looks ugly and I'm sure there is a better way. So what would the fastest way to create the BufferedImage
be then?
/编辑:我尝试过使用setRGB方法直接在我的BufferedImage上,但它导致了比上面更差的性能。
/ I've tried using the setRGB method directly on my BufferedImage but it resulted in worse performance than the above.
谢谢
推荐答案
我会这样做:
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;
可能更快,不要测试它。
Maybe it is faster don't test it yet.
这篇关于从调色板创建图像的最佳方法Color array + indice byte array?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!