ColorModels和WritableRasters如何在Java BufferedImages中工作? [英] How do ColorModels and WritableRasters work in java BufferedImages?
问题描述
在Java中使用 BufferedImage
类时,我通常使用构造函数,其参数为 int width,int height,int type
。但是,对于某些应用程序,我想要一幅图像,该图像将以ARGB顺序使用字节来存储颜色数据,这不能通过这种方式完成(它只有 TYPE_4BYTE_ABGR
)。
When working with the BufferedImage
class in Java, I usually use the constructor with parameters int width, int height, int type
. For a certain application, though, I wanted an image which would store the color data using bytes in the order ARGB, which can't be done in that way (it has only TYPE_4BYTE_ABGR
).
我找到了以下解决方案,效果很好:
I found the following solution, which worked fine:
WritableRaster raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height, 4, null);
ColorModel colorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[]{8,8,8,8}, true, false, ColorModel.TRANSLUCENT, DataBuffer.TYPE_BYTE);
img = new BufferedImage(colorModel, raster, false, new Hashtable<>());
我不明白为什么这样做?
I don't understand why this works?
尽管-我知道 WritableRaster
是保存图片像素数据的数据结构,但是过去我迷路了。这两个对象(栅格或ColorModel)中的哪一个确定像素数据的顺序为RGBA?以及如何使用(ColorModel,WritableRaster,boolean,HashTable)在BufferedImage的
(int,int,int)
构造函数中模拟任何类型/ code>构造函数?
Though - I understand that the WritableRaster
is the data structure that holds the pixel data of the picture, but past that I am lost. Which of these two objects - the Raster, or the ColorModel - determines that the pixel data is in the order RGBA? And how could I simulate any of the types in BufferedImage's (int, int, int)
constructor using the (ColorModel, WritableRaster, boolean, HashTable)
constructor?
推荐答案
这是方法
Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height, 4, null);
...指定字节顺序。它隐含地这样做,通过假设4个波段,您希望波段偏移为 0、1、2、3
(对应于RGBA;有关详细信息,请参见源) 。对于RGB颜色空间,波段0 =红色,1 =绿色,2 =蓝色和3 = Alpha。
...that specifies the byte order. It does so implicitly, by assuming for 4 bands, you want the band offsets to be 0, 1, 2, 3
(which corresponds to RGBA; see the source for details). For RGB color space, band 0 = Red, 1 = Green, 2 = Blue and 3 = Alpha.
如果需要不同的顺序,则可以使用其他颜色工厂方法,例如以ARGB顺序创建栅格:
If you wanted a different order, you could have used a different factory method, for instance to create a raster with ARGB order:
Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height,
width * 4, 4, new int[] {3, 0, 1, 2}, null);
这两种方法都将创建 PixelInterleavedSampleModel $ c $的实例c>对您来说,正是这个
SampleModel
真正控制了样品顺序。
Both of these methods will create an instance of PixelInterleavedSampleModel
for you, and it's this SampleModel
that really controls the sample order.
对于 BufferedImage(int,int,int)
构造函数可以工作,以及如何做类似的事情,我认为最好的办法就是自己看一下源代码。这基本上是一个很大的 switch
语句,其中对于每个常量 TYPE _ *
,它都会创建一个 WritableRaster
和 ColorModel
与上面的操作类似。
For how the BufferedImage(int, int, int)
constructor works, and how you could do similar things, I think the best would be to just look at the source code for yourself. It's basically one big switch
statement, where for each constant TYPE_*
it creates a WritableRaster
and a ColorModel
similar to how you do it above.
例如:
ColorModel colorModel = ColorModel.getRGBdefault();
WritableRaster raster = colorModel.createCompatibleWritableRaster(width, height);
new BufferedImage(colorModel, raster, colorModel.isAlpahPremultiplied(), null);
...将创建类型为 TYPE_INT_ARGB $ c $的图像c>(这种反向查找的实际工作方式有些讨厌,但它的工作方式是... :-))。如果
BufferedImage
中没有相应的类型,则该类型将为 TYPE_CUSTOM
( 0
)。
...will create an image with type TYPE_INT_ARGB
(the way this reverse lookup actually works is somewhat nasty, but it works... :-)). If no corresponding type exists in BufferedImage
the type will be TYPE_CUSTOM
(0
).
这篇关于ColorModels和WritableRasters如何在Java BufferedImages中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!