gpt4 book ai didi

c++ - 位图文件图像数据偏移产生不同图像

转载 作者:行者123 更新时间:2023-11-28 05:20:22 25 4
gpt4 key购买 nike

我刚刚开始创建一个位图图像文件加载器,并且只是从 wiki 中得出结论,从文件开头到图像数据的偏移量为 50 字节。因此,我将数据设置为 50。这是原始图像:

enter image description here

现在,当我使用以下代码加载图像时:

   std::fstream file;
file.open(fileName, std::fstream::binary | std::fstream::in);

if (file.fail()) std::cout << "Couldn't open: `" << fileName << "`\n";
GLchar * data;

file.seekg(0, file.end);
int length = file.tellg();
file.seekg(0, file.beg);

data = new GLchar[length];


file.read(data, length);


if(file)
std::cout << "all characters read successfully.\n";
else
std::cout << "error: only " << file.gcount() << " could be read";

GLchar sec = data[1];

std::cout << data[0] << data[1] << "= ";

switch (sec) {
case 'M':std::cout << "Windows 3.1x"; break;
case 'A':std::cout << "OS/2 struct bitmap array"; break;
case 'I':std::cout << "OS/2 struct color icon"; break;
case 'P':std::cout << "OS/2 const color pointer"; break;
case 'C':std::cout << "OS/2 struct icon"; break;
}

int headerOffset = 50;
std::cout << "\n\n~~ "<< *(GLuint *)&data[10];
width = *(GLuint *)&data[18];
height = *(GLuint *)&data[22];

int bpp = *(int *)&data[28];

int compressionMethod = *(int *)&data[30];

std::cout << "\nDimensions: " << width << "x" << height << "\n";
std::cout << "Bits per pixel: " << bpp;
std::cout << "\nCompression Method: " << compressionMethod << "\n";
//start of pixel array - 50
unsigned char *pixels = new unsigned char[width*height * 3];
file.seekg(headerOffset + 40);
file.read((char *)pixels, width*height*3);

unsigned char tmpRGB = 0; // Swap buffer
for (unsigned long i = 0; i < width * height * 3; i += 3)
{
tmpRGB = pixels[i];
pixels[i] = pixels[i + 2];
pixels[i + 2] = tmpRGB;
}

glGenTextures(1, &texture); // Generate a texture
glBindTexture(GL_TEXTURE_2D, texture); // Bind that texture temporarily

GLint mode = GL_RGB; // Set the mode

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

glTexImage2D(GL_TEXTURE_2D, 0, mode, width, height, 0, mode, GL_UNSIGNED_BYTE, pixels);


delete[] pixels;
delete[] data;
file.close();
std::cout << "\n\n\n";

此代码假设图像数组的偏移量为 50

有了这个假设:这是图像产生的结果-

enter image description here

现在,经过一些研究,我了解到可以找到位图图像数据(像素数组)的字节的偏移量,即起始地址。 在文件中的偏移量为 10。所以我决定改变

       GLuint offset = 50;

以下内容

       GLuint offset = *(GLuint *)&data[10];

但是,当我这样做时,颜色会切换到错误的顺序。这是一张图片:

enter image description here

这是对问题的解释:原始图像从上到下依次为蓝-绿-红-白-灰。我渲染的第一张图片遵循了这一点。第二个(从代码中找到偏移量的那个)没有。谁能解释为什么会这样?

最佳答案

没看太多代码,好像你没有考虑到图片是用Little Endian存储的:

对于 24 位像素,内存中的像素如下所示:0xBBGGRR,即错误可能是您编写的代码假设一个像素以 Big endian 编写,如下所示:0xRRGGBB

这意味着,即使我们将一个像素视为 3 个字节的 RGB 值(1B 红色、1B 绿色和 1B 蓝色),字节也会在内存中交换。

编辑:这就解释了为什么只有蓝色换成了红色,而绿色和其他颜色保持不变;因为未交换的颜色在 Big endian 和 Little endian 表示法中都具有类似的十六进制值。

关于c++ - 位图文件图像数据偏移产生不同图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41653296/

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