我正在做一个android应用程序来解压缩,解码和显示HDR图片。
这些HDR图片每个分量(A,R,G,B)使用2个字节,因此一个像素由一个8字节值表示,该值只能适合long类型。
我使用安卓的位图来显示图片,因为他们有一个构造器,允许使用Bitmap.Config.RGBA_F16进行HDR:
int width = 1;
int height = 1;
Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.RGBA_F16);不幸的是,我找不到任何方法来填充位图的像素。我使用推荐的公式,但它不能在位图的setPixel(x,y,color)方法中使用,因为颜色必须是整数:
long color = (A & 0xffff) << 48 | (B & 0xffff) << 32 | (G & 0xffff) << 16 | (R & 0xffff);
image.setPixel(0,0,color); //Argument type error。
谢谢你的帮助!
发布于 2020-12-22 12:28:22
如果您需要打开一个HDR文件,例如16位png,然后显示它,您可以使用ImageDecoder.setTargetColorSpace创建格式为Bitmap.Config.RGBA_F16的位图,如下所示:
File file = new File(...);
ImageDecoder.Source source = ImageDecoder.createSource(file);
Drawable drawable = ImageDecoder.decodeDrawable(source, (decoder, info, src) -> {
decoder.setTargetColorSpace(ColorSpace.Named.EXTENDED_SRGB);
});如果您需要显示存储在内存中的HDR图像,您可以使用Bitmap.copyPixelsFromBuffer,因为此方法允许像Bitmap.setPixel那样设置位图的像素,而无需转换颜色空间。在这种情况下,您需要为每个像素将由Half值表示的4个通道打包为long,然后将这些长值写入Buffer,最后将像素从缓冲区复制到位图。
LongBuffer buffer = LongBuffer.allocate(width * height);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
// fill pixels values as needed
float r = (float)y / height;
float g = (float)y / height;
float b = (float)y / height;
float a = 1f;
long rBits = Half.halfToShortBits(Half.toHalf(r));
long gBits = Half.halfToShortBits(Half.toHalf(g));
long bBits = Half.halfToShortBits(Half.toHalf(b));
long aBits = Half.halfToShortBits(Half.toHalf(a));
long color = aBits << 48 | bBits << 32 | gBits << 16 | rBits;
buffer.put(color);
}
}
buffer.rewind();
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGBA_F16);
bitmap.copyPixelsFromBuffer(buffer);https://stackoverflow.com/questions/55458589
复制相似问题