【发布时间】:2017-01-14 18:11:54
【问题描述】:
我刚开始创建位图图像文件加载器,只是从 wiki 中得出结论,从文件开头到图像数据的偏移量是 50 字节。因此,我将数据设置为 50。这是原始图像:
现在当我使用以下代码加载图像时:
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
有了这个假设:这就是图像产生的结果-
现在,经过一些研究,我了解到
The offset, i.e. starting address, of the byte where the bitmap image data (pixel array) can be found. 在文件中的偏移量为 10。于是,我决定改变
GLuint offset = 50;
到下面
GLuint offset = *(GLuint *)&data[10];
但是,当我这样做时,颜色会切换到错误的顺序。这是一张图片:
这是对问题的解释:原始图像从上到下是蓝-绿-红-白-灰。我渲染的第一张图片遵循了这一点。第二个(从代码中找到偏移量的那个)没有。谁能解释为什么会这样?
【问题讨论】: