将 Tif 索引的 8 位颜色转换为 32 位颜色

发布于 2024-12-28 22:32:01 字数 543 浏览 3 评论 0原文

我有一个索引 Tiff 图像,我正在使用 LibTiff.Net 读取该图像以生成大图像一部分的位图图像。我相信 Tiff 将有 256 个颜色条目,我想将其转换为 256 个 32 位像素值以在输出位图中使用。

int bitsPerSample = tif.GetField(TiffTag.BITSPERSAMPLE)[0].ToInt();
FieldValue[] colourIndex = tif.GetField(TiffTag.COLORMAP);
int[] palette = new int[256];
for( int i = 0; i < 256; i++ )
{
    short red = colourIndex[0].ToShortArray()[i];
    short green = colourIndex[1].ToShortArray()[i];
    short blue = colourIndex[2].ToShortArray()[i];

    palette[i] = ?
}

如何将 RGB Shorts 转换为 32 位像素值?

I have an indexed Tiff image which I'm reading using LibTiff.Net to produce a bitmap image of a section of the large image. I believe the Tiff will have 256 colour entries which I want to convert to 256 32-bit pixel values to be used in the output bitmap.

int bitsPerSample = tif.GetField(TiffTag.BITSPERSAMPLE)[0].ToInt();
FieldValue[] colourIndex = tif.GetField(TiffTag.COLORMAP);
int[] palette = new int[256];
for( int i = 0; i < 256; i++ )
{
    short red = colourIndex[0].ToShortArray()[i];
    short green = colourIndex[1].ToShortArray()[i];
    short blue = colourIndex[2].ToShortArray()[i];

    palette[i] = ?
}

How do I convert the RGB shorts into a 32-bit pixel value?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扭转时空 2025-01-04 22:32:01

尝试 System.Drawing.Color.FromArgb(red, green, blue).ToArgb()。

但是,只有当 LibTiff 和 Win32 都对其 ARGB 值使用相同的字节顺序时,这才会给出正确的结果。它还假设您的红色、绿色和蓝色值在 0 到 255 范围内;如果没有,你可以缩放它们。

Try System.Drawing.Color.FromArgb(red, green, blue).ToArgb().

However, this will only give the right results if LibTiff and Win32 both use the same byte ordering for their ARGB values. It also assumes your red, green, and blue values are in the range 0 to 255; you can scale them if not.

单挑你×的.吻 2025-01-04 22:32:01

在 TIFF ColorMap 中,每种颜色的值数为 2**BitsPerSample。因此,8 位调色板颜色图像的 ColorMap 字段将具有 3 * 256 个值。

每个值的宽度为 16 位。 0代表最小强度,65535代表最大强度。黑色用 0,0,0 表示,白色用 65535, 65535, 65535 表示。

因此,您可能应该使用以下代码:

ushort red = colourIndex[0].ToShortArray()[i];
ushort green = colourIndex[1].ToShortArray()[i];
ushort blue = colourIndex[2].ToShortArray()[i];

palette[i] = System.Drawing.Color.FromArgb(red / 255, green / 255, blue / 255).ToArgb();

In a TIFF ColorMap, the number of values for each color is 2**BitsPerSample. Therefore, the ColorMap field for an 8-bit palette-color image would have 3 * 256 values.

The width of each value is 16 bits. 0 represents the minimum intensity, and 65535 represents the maximum intensity. Black is represented by 0,0,0, and white by 65535, 65535, 65535.

So, you probably should use following code:

ushort red = colourIndex[0].ToShortArray()[i];
ushort green = colourIndex[1].ToShortArray()[i];
ushort blue = colourIndex[2].ToShortArray()[i];

palette[i] = System.Drawing.Color.FromArgb(red / 255, green / 255, blue / 255).ToArgb();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文