d - 我可以从FreeImage导出的最高位深度灰度图像是多少?



作为上下文,我正在构建一个需要相对极端细节的地形程序。我不希望文件很小,也不需要在显示器上正式查看它们,它们只需要具有非常高的分辨率。

我知道大多数图像格式限制为 8 bpp,因为显示器(价格合理)和人类感知都有标准限制。然而,2⁸ 只是 256 个可能的值,这在重建的位移中会导致平台伪影。2¹⁶ 可能足够接近 65,536 个可能的值,我已经实现了。

我正在使用FreeImage和DLang来构建数据,目前在Linux Mint机器上。

然而,当我继续使用2³²时,软件支持似乎对我逐渐消失。我尝试了这种形式的TIFF,但似乎没有什么能够解释它,要么显示完全(或大部分)透明的图像(记住我没想到任何显示器真正支持2³²阴影的通道)或抱怨无法解码RGB数据。我想这是因为它被认为是RGB或RGBA图像。

FreeImage 在大多数情况下都有相当好的文档记录,但我现在想知道,我可以导出的最高精度单通道格式是什么,我将如何做到这一点?谁能举个例子?在任何典型的和非家庭卷的图像格式中,我真的被限制为 16 位吗?我知道这对于医学成像来说已经足够高了,但我敢肯定,我不是第一个试图瞄准更高目标的人,我们科学类型可能对我们的精度水平非常雄心勃勃......

我在代码中是否犯了一个明显的错误?对于这种精度,我还有什么应该尝试

的吗?这是我的代码。

有效的 16 位 TIFF

void writeGrayscaleMonochromeBitmap(const double width, const double height) {
FIBITMAP *bitmap = FreeImage_AllocateT(FIT_UINT16, cast(int)width, cast(int)height);
for(int y = 0; y < height; y++) {
ubyte *scanline = FreeImage_GetScanLine(bitmap, y);
for(int x = 0; x < width; x++) {
ushort v = cast(ushort)((x * 0xFFFF)/width);
ubyte[2] bytes = nativeToLittleEndian(cast(ushort)(x/width * 0xFFFF));
scanline[x * ushort.sizeof + 0] = bytes[0];
scanline[x * ushort.sizeof + 1] = bytes[1];
}
}
FreeImage_Save(FIF_TIFF, bitmap, "test.tif", TIFF_DEFAULT);
FreeImage_Unload(bitmap);
}

没有真正起作用的 32 位 TIFF

void writeGrayscaleMonochromeBitmap32(const double width, const double height) {
FIBITMAP *bitmap = FreeImage_AllocateT(FIT_UINT32, cast(int)width, cast(int)height);
writeln(width, ", ", height);
writeln("Width: ", FreeImage_GetWidth(bitmap));
for(int y = 0; y < height; y++) {
ubyte *scanline = FreeImage_GetScanLine(bitmap, y);
writeln(y, ": ", scanline);
for(int x = 0; x < width; x++) {
//writeln(x, " < ", width);
uint v = cast(uint)((x/width) * 0xFFFFFFFF);
writeln("V: ", v);
ubyte[4] bytes = nativeToLittleEndian(v);
scanline[x * uint.sizeof + 0] = bytes[0];
scanline[x * uint.sizeof + 1] = bytes[1];
scanline[x * uint.sizeof + 2] = bytes[2];
scanline[x * uint.sizeof + 3] = bytes[3];
}
}
FreeImage_Save(FIF_TIFF, bitmap, "test32.tif", TIFF_NONE);
FreeImage_Unload(bitmap);
}

感谢您的任何指示。

对于单个通道,FreeImage 提供的最高值为 32 位,如FIT_UINT32。但是,文件格式必须能够做到这一点,截至目前,似乎只有TIFF可以胜任这项任务(参见斯坦福文档的第104页)。此外,大多数监视器无法表示每个样本超过 8 位,极端情况下为 12 位,因此很难读出数据并使其正确呈现。

涉及在封送处理之前比较字节到位图,然后从同一位图采样的单元测试表明,数据实际上正在编码。

要将数据打印为 16 位灰度(目前支持 J2K、JP2、PGM、PGMRAW、PNG 和 TIF),您可以执行以下操作:

void toFreeImageUINT16PNG(string fileName, const double width, const double height, double[] data) {
FIBITMAP *bitmap = FreeImage_AllocateT(FIT_UINT16, cast(int)width, cast(int)height);
for(int y = 0; y < height; y++) {
ubyte *scanline = FreeImage_GetScanLine(bitmap, y);
for(int x = 0; x < width; x++) {
//This magic has to happen with the y-coordinate in order to keep FreeImage from following its default behavior, and generating
//the image upside down.
ushort v = cast(ushort)(data[cast(ulong)(((height - 1) - y) * width + x)] * 0xFFFF); //((x * 0xFFFF)/width);
ubyte[2] bytes = nativeToLittleEndian(v);
scanline[x * ushort.sizeof + 0] = bytes[0];
scanline[x * ushort.sizeof + 1] = bytes[1];
}
}
FreeImage_Save(FIF_PNG, bitmap, fileName.toStringz);
FreeImage_Unload(bitmap);
}

当然,您需要针对目标文件类型进行调整。要导出为 48 位 RGB16,您需要这样做。

void toFreeImageColorPNG(string fileName, const double width, const double height, double[] data) {
FIBITMAP *bitmap = FreeImage_AllocateT(FIT_RGB16, cast(int)width, cast(int)height);
uint pitch = FreeImage_GetPitch(bitmap);
uint bpp = FreeImage_GetBPP(bitmap);
for(int y = 0; y < height; y++) {
ubyte *scanline = FreeImage_GetScanLine(bitmap, y);
for(int x = 0; x < width; x++) {
ulong offset = cast(ulong)((((height - 1) - y) * width + x) * 3);
ushort r = cast(ushort)(data[(offset + 0)] * 0xFFFF);
ushort g = cast(ushort)(data[(offset + 1)] * 0xFFFF);
ushort b = cast(ushort)(data[(offset + 2)] * 0xFFFF);
ubyte[6] bytes = nativeToLittleEndian(r) ~ nativeToLittleEndian(g) ~ nativeToLittleEndian(b);
scanline[(x * 3 * ushort.sizeof) + 0] = bytes[0];
scanline[(x * 3 * ushort.sizeof) + 1] = bytes[1];
scanline[(x * 3 * ushort.sizeof) + 2] = bytes[2];
scanline[(x * 3 * ushort.sizeof) + 3] = bytes[3];
scanline[(x * 3 * ushort.sizeof) + 4] = bytes[4];
scanline[(x * 3 * ushort.sizeof) + 5] = bytes[5];
}
}
FreeImage_Save(FIF_PNG, bitmap, fileName.toStringz);
FreeImage_Unload(bitmap);
}

最后,要对 UINT32 灰度图像进行编码(目前仅限于 TIFF),您需要这样做。

void toFreeImageTIF32(string fileName, const double width, const double height, double[] data) {
FIBITMAP *bitmap = FreeImage_AllocateT(FIT_UINT32, cast(int)width, cast(int)height);
//DEBUG
int xtest = cast(int)(width/2);
int ytest = cast(int)(height/2);
uint comp1a = cast(uint)(data[cast(ulong)(((height - 1) - ytest) * width + xtest)] * 0xFFFFFFFF);
writeln("initial: ", nativeToLittleEndian(comp1a));
for(int y = 0; y < height; y++) {
ubyte *scanline = FreeImage_GetScanLine(bitmap, y);
for(int x = 0; x < width; x++) {
//This magic has to happen with the y-coordinate in order to keep FreeImage from following its default behavior, and generating
//the image upside down.
ulong i = cast(ulong)(((height - 1) - y) * width + x);
uint v = cast(uint)(data[i] * 0xFFFFFFFF);
ubyte[4] bytes = nativeToLittleEndian(v);
scanline[x * uint.sizeof + 0] = bytes[0];
scanline[x * uint.sizeof + 1] = bytes[1];
scanline[x * uint.sizeof + 2] = bytes[2];
scanline[x * uint.sizeof + 3] = bytes[3];
}
}
//DEBUG
ulong index = cast(ulong)(xtest * uint.sizeof);
writeln("Final: ", FreeImage_GetScanLine(bitmap, ytest)
[index .. index + uint.sizeof]);
FreeImage_Save(FIF_TIFF, bitmap, fileName.toStringz);
FreeImage_Unload(bitmap);
}

我还没有找到一个由其他人构建的程序,它可以很容易地在显示器的可用调色板上渲染 32 位灰度图像。但是,我留下了我的检查代码,其中将在顶部 DEBUG 和底部始终如一地写出相同的数组,这对我来说足够一致。

希望这将在未来帮助其他人。

最新更新