首页 > 解决方案 > 我可以从 FreeImage 导出的最高位深度灰度图像是多少?

问题描述

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

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

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

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

FreeImage 对于大多数用途都有很好的记录,但我现在想知道,我可以导出的最高精度单通道格式是什么,我该怎么做?谁能提供一个例子?在任何典型的和非家庭滚动的图像格式中,我真的被限制为 16 位吗?我知道这对于医学成像来说已经足够高了,但我敢肯定我不是第一个尝试瞄准更高目标的人,我们科学类型的人对我们的精度水平可能非常雄心勃勃……</p>

我在代码中犯了明显的错误吗?为了这种精度,我还应该尝试其他什么方法吗?

这是我的代码。

有效的 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);
}

感谢您的任何指示。

标签: dtiffgrayscalefreeimagebpp

解决方案


对于单个通道,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 和底部写出相同的数组,这对我来说已经足够一致了。

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


推荐阅读