在 c++ 中读取 .bmp 文件
我正在尝试加载 bmp 文件以便在 opengl 中重用它。 我通过谷歌找到了一些关于如何加载 bmp 文件的代码。 我获取了这段代码并在我的项目中放入了一个 Bitmap 类。 该类还远未完成,但文件头的读取已经出错。读取 INFOHEADER 和 FILEHEADER 的字节后,我的结构中没有正确的值。 一些想法?
//
// Bitmap.h
//
#ifndef LaserMaze_Bitmap_h
#define LaserMaze_Bitmap_h
typedef struct /**** BMP file header structure ****/
{
unsigned short bfType; /* Magic number for file */
unsigned int bfSize; /* Size of file */
unsigned short bfReserved1; /* Reserved */
unsigned short bfReserved2; /* ... */
unsigned int bfOffBits; /* Offset to bitmap data */
} BITMAPFILEHEADER;
# define BF_TYPE 0x4D42 /* "MB" */
typedef struct /**** BMP file info structure ****/
{
unsigned int biSize; /* Size of info header */
int biWidth; /* Width of image */
int biHeight; /* Height of image */
unsigned short biPlanes; /* Number of color planes */
unsigned short biBitCount; /* Number of bits per pixel */
unsigned int biCompression; /* Type of compression to use */
unsigned int biSizeImage; /* Size of image data */
int biXPelsPerMeter; /* X pixels per meter */
int biYPelsPerMeter; /* Y pixels per meter */
unsigned int biClrUsed; /* Number of colors used */
unsigned int biClrImportant; /* Number of important colors */
} BITMAPINFOHEADER;
/*
* Constants for the biCompression field...
*/
# define BI_RGB 0 /* No compression - straight BGR data */
# define BI_RLE8 1 /* 8-bit run-length compression */
# define BI_RLE4 2 /* 4-bit run-length compression */
# define BI_BITFIELDS 3 /* RGB bitmap with RGB masks */
typedef struct /**** Colormap entry structure ****/
{
unsigned char rgbBlue; /* Blue value */
unsigned char rgbGreen; /* Green value */
unsigned char rgbRed; /* Red value */
unsigned char rgbReserved; /* Reserved */
} RGBQUAD;
class Bitmap {
public:
Bitmap(const char* filename);
~Bitmap();
RGBQUAD* pixels;
BITMAPFILEHEADER fh;
BITMAPINFOHEADER ih;
private:
};
#endif
菲律宾共产党
// Bitmap.cpp
//
#include <iostream>
#include <stdio.h>
#include "Bitmap.h"
Bitmap::Bitmap(const char* filename) {
FILE* file;
file = fopen(filename, "rb");
std::cout << sizeof(BITMAPFILEHEADER) << std::endl;
if(file != NULL) { // file opened
BITMAPFILEHEADER h;
size_t x = fread(&h, sizeof(BITMAPFILEHEADER), 1, file); //reading the FILEHEADER
std::cout << x;
fread(&this->ih, sizeof(BITMAPINFOHEADER), 1, file);
fclose(file);
}
}
I'm trying to load a bmp file for reusing it in opengl.
I've found some code via google on how to load a bmp file.
I took this code and put in a class Bitmap in my project.
The class is far away from being finished but already the reading of the file headers goes wrong. After reading the bytes for INFOHEADER and FILEHEADER there aren't the right values in my structs.
Some ideas?
//
// Bitmap.h
//
#ifndef LaserMaze_Bitmap_h
#define LaserMaze_Bitmap_h
typedef struct /**** BMP file header structure ****/
{
unsigned short bfType; /* Magic number for file */
unsigned int bfSize; /* Size of file */
unsigned short bfReserved1; /* Reserved */
unsigned short bfReserved2; /* ... */
unsigned int bfOffBits; /* Offset to bitmap data */
} BITMAPFILEHEADER;
# define BF_TYPE 0x4D42 /* "MB" */
typedef struct /**** BMP file info structure ****/
{
unsigned int biSize; /* Size of info header */
int biWidth; /* Width of image */
int biHeight; /* Height of image */
unsigned short biPlanes; /* Number of color planes */
unsigned short biBitCount; /* Number of bits per pixel */
unsigned int biCompression; /* Type of compression to use */
unsigned int biSizeImage; /* Size of image data */
int biXPelsPerMeter; /* X pixels per meter */
int biYPelsPerMeter; /* Y pixels per meter */
unsigned int biClrUsed; /* Number of colors used */
unsigned int biClrImportant; /* Number of important colors */
} BITMAPINFOHEADER;
/*
* Constants for the biCompression field...
*/
# define BI_RGB 0 /* No compression - straight BGR data */
# define BI_RLE8 1 /* 8-bit run-length compression */
# define BI_RLE4 2 /* 4-bit run-length compression */
# define BI_BITFIELDS 3 /* RGB bitmap with RGB masks */
typedef struct /**** Colormap entry structure ****/
{
unsigned char rgbBlue; /* Blue value */
unsigned char rgbGreen; /* Green value */
unsigned char rgbRed; /* Red value */
unsigned char rgbReserved; /* Reserved */
} RGBQUAD;
class Bitmap {
public:
Bitmap(const char* filename);
~Bitmap();
RGBQUAD* pixels;
BITMAPFILEHEADER fh;
BITMAPINFOHEADER ih;
private:
};
#endif
the cpp
// Bitmap.cpp
//
#include <iostream>
#include <stdio.h>
#include "Bitmap.h"
Bitmap::Bitmap(const char* filename) {
FILE* file;
file = fopen(filename, "rb");
std::cout << sizeof(BITMAPFILEHEADER) << std::endl;
if(file != NULL) { // file opened
BITMAPFILEHEADER h;
size_t x = fread(&h, sizeof(BITMAPFILEHEADER), 1, file); //reading the FILEHEADER
std::cout << x;
fread(&this->ih, sizeof(BITMAPINFOHEADER), 1, file);
fclose(file);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
标头需要 2 字节对齐。
The header needs to be 2 byte aligned.
让您的 Windows 操作系统使用
LoadImage
为您加载它怎么样?使用
GetObject()
查找更多信息,例如大小等,如果您想获取各个位,则使用GetDIBits()
。How about letting your Windows OS load it for you with
LoadImage
.Use
GetObject()
to find further info like size, etc. andGetDIBits()
if you want to get at the individual bits.BITMAPINFOHEADER ::
您需要首先读取biSize
才能知道信息标头有多大,不能依赖sizeof()
。查看这篇关于文件格式的 wiki 文章
BITMAPINFOHEADER ::
You need to read first thebiSize
in order to know how large the info header is, you cannot rely onsizeof()
.Check out this wiki article about the file format