简体   繁体   English

用c ++读取.bmp文件

[英]reading a .bmp file in c++

I'm trying to load a bmp file for reusing it in opengl. 我正在尝试加载一个bmp文件,以便在opengl中重用它。 I've found some code via google on how to load a bmp file. 我已经通过谷歌找到了一些关于如何加载bmp文件的代码。 I took this code and put in a class Bitmap in my project. 我接受了这段代码并在我的项目中添加了一个类Bitmap。 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. 在读取INFOHEADER和FILEHEADER的字节后,我的结构中没有正确的值。 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 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);
    }
}

The header needs to be 2 byte aligned. 标头需要2字节对齐。

#pragma pack(2) // Add this

typedef struct
{
    unsigned short bfType;
    unsigned int   bfSize;
    unsigned short bfReserved1;
    unsigned short bfReserved2;
    unsigned int   bfOffBits;
} BITMAPFILEHEADER;

#pragma pack() // and this

How about letting your Windows OS load it for you with LoadImage . 如何让你的Windows操作系统使用LoadImage为你加载它。

HBITMAP hbm = LoadImage( NULL, path, 0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);

Use GetObject() to find further info like size, etc. and GetDIBits() if you want to get at the individual bits. 如果要获取各个位,请使用GetObject()查找大小等更多信息和GetDIBits()

BITMAPINFOHEADER :: You need to read first the biSize in order to know how large the info header is, you cannot rely on sizeof() . BITMAPINFOHEADER ::您需要首先阅读biSize以了解信息标头的大小,您不能依赖sizeof()

Check out this wiki article about the file format 查看这篇关于文件格式的wiki文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM