Simple BMP image Reading

Source: Internet
Author: User
Tags bmp image

BMP full Name Bitmap (bitmap), is the standard image file format in Windows, can be read into a BMP image as a texture.

here is the bitmap load Class I am currently using. First is the header file:

#define Swapcolor (A, a) {         (a) ^= (b);             (b) ^= (a);             (a) ^= (b);     Class Bmploader {private:        unsigned char* header;//file header        unsigned int datapos;//read position        unsigned int width, height;//picture width height        unsigned int imagesize;//picture content size public:        unsigned char * data;//picture content RGB        bmploader ();        ~bmploader ();        void Bfree ();        int getwidth ();        int getheight ();        BOOL LoadBitmap (const char* fileName);};

Swapcolor is used to exchange color values, because the BMP storage color order is BGR, to turn it into RGB
This is followed by a specific implementation:

Bmploader::bmploader () {        header=new unsigned char[54];} Bmploader::~bmploader () {        bfree ();        delete[] data;} void Bmploader::bfree () {        delete[] header;}

constructors and destructors with a file header size of 54 bytes


followed by the main method of reading the image, filename is the image name (with path)

BOOL Bmploader::loadbitmap (const char* fileName) {file * file = fopen (FileName, "RB");                if (!file) {printf ("Image could not being opened");        return false;            } if (header, 1, fread, file)!=54) {//Header not 54 bytes read failed printf ("Not a correct BMP file");        return false;            } if (header[0]!= ' B ' | | header[1]!= ' M ') {//header at the beginning of the file is not BM Read failed printf ("Not a correct BMP file");        return false; } datapos = * (int*) & (header[0x0a]);//Read position location at file header 0x0A imageSize = * (int*) & (header[0x22]);//picture content is large Small data location at file header 0x22 width = * (int*) & (header[0x12]);//Picture width data position at file header 0x12 height = * (int*) & (Heade R[0X16]);//Picture height data position at file header 0x16 if (imagesize==0) imagesize=width*height*3;//picture content data = Total pixel number x3 (simplicity uses a width height of 2 N-size picture, regardless of 4-byte alignment) if (datapos==0) datapos=54;//file header is read at 54 bytes at data = new unsigned char [imag Esize];//data put Pixel information frEAD (data,1,imagesize,file);//Read pixel fclose (file);  for (int i= 0;i< (int) imagesize;i+=3) Swapcolor (data[i],data[i+2]);//BGR becomes RGB return true;}

get picture width and height:

int Bmploader::getwidth () {        return width;} int Bmploader::getheight () {        return height;}

call LoadBitmap to load the image data, data is the image of the pixels.
Note that this class can only be used to read 24-bit BMP bitmaps, and the width and height of the graph is 2 of the n-th square.

reference:  http://www.opengl-tutorial.org/beginners-tutorials/tutorial-5-a-textured-cube/


Simple BMP image Reading

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.