gpt4 book ai didi

c# - 在 C# 中,如何确定图像(jpg、gif 或 png)是隔行扫描(非逐行扫描)还是非隔行扫描(逐行扫描)?

转载 作者:太空狗 更新时间:2023-10-30 00:59:02 29 4
gpt4 key购买 nike

我如何在 C# 中执行此操作?我翻遍了整个网络,但找不到答案。

最佳答案

根据GIF spec , gif 图像的字节流以 6 字节开始为标题,7 字节为“逻辑屏幕描述符”。

逻辑屏幕描述符的第 5 个字节是“压缩字段”字节。如果图像包含全局颜色表,则设置“打包字段”的第一位。最后三位是一个数字 X,您可以使用它来计算全局颜色表的大小,如 3 x 2^(X+1)

然后遵循全局颜色表(如果存在)。要跳过这个,您需要知道它的大小,如上所示计算得出。

然后是一个 10 字节的“图像描述符”。这些的最后一个字节是另一个“打包字段”。 如果图像是隔行扫描的,则设置该字节的第二位。

   public bool IsInterlacedGif(Stream stream)
{
byte[] buffer = new byte[10];
int read;

// read header
// TODO: check that it starts with GIF, known version, 6 bytes read
read = stream.Read(buffer, 0, 6);

// read logical screen descriptor
// TODO: check that 7 bytes were read
read = stream.Read(buffer, 0, 7);
byte packed1 = buffer[4];
bool hasGlobalColorTable = ((packed1 & 0x80) != 0); // extract 1st bit

// skip over global color table
if (hasGlobalColorTable)
{
byte x = (byte)(packed1 & 0x07); // extract 3 last bits
int globalColorTableSize = 3 * 1 << (x + 1);
stream.Seek(globalColorTableSize, SeekOrigin.Current);
}

// read image descriptor
// TODO: check that 10 bytes were read
read = stream.Read(buffer, 0, 10);
byte packed2 = buffer[9];
return ((packed2 & 0x40) != 0); // extract second bit
}

毫无疑问,如果您阅读了这些规范,可以对 JPG 和 PNG 进行类似的字节流检查。

关于c# - 在 C# 中,如何确定图像(jpg、gif 或 png)是隔行扫描(非逐行扫描)还是非隔行扫描(逐行扫描)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1534966/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com