C + + read BMP file

Source: Internet
Author: User

#include <string>#include<vector>#include<fstream>using namespacestd;classcbmpfile{ Public: Cbmpfile () {}~Cbmpfile () {}BOOLLoadConst string&file) {        //Open FileIfstream fs (file, Ios_base::inch| Ios_base::beg |ios_base::binary); if(!Fs.is_open ()) {            return false; }        //Read File FlagsFs.read ((Char*) &m_tag,sizeof(WORD)); if(M_tag! =0x4d42){            return false; }        //Read file headerFs.read ((Char*) &m_fileheader,sizeof(M_fileheader)); if(Fs.gcount ()! =sizeof(M_fileheader)) {            return false; }        //Read Bitmap information headerFs.read ((Char*) &m_infoheader,sizeof(M_infoheader)); if(Fs.gcount ()! =sizeof(M_infoheader)) {            return false; }        //Palette DataM_palette.resize (M_fileheader.bfoffbits-sizeof(M_tag)-sizeof(M_fileheader)-sizeof(M_infoheader)); Fs.read (&m_palette[0], m_palette.size ()); //reading bitmap DataM_data.resize (M_fileheader.bfsize-m_fileheader.bfoffbits); Fs.read (&m_data[0], m_data.size ()); if(Fs.gcount ()! =m_data.size ()) {            return false;        } fs.close (); return true;    } WORD M_tag;    Bitmapfileheader M_fileheader;    Bitmapinfoheader M_infoheader; Vector<Char>M_palette; Vector<Char>m_data;Private: typedef unsignedCharBYTE; typedef unsigned ShortWORD; typedef unsignedLongDWORD; typedefLongLONG; //bitmap file header definition; typedefstruct{DWORD bfsize;//File SizeWORD bfReserved1;//Reserved WordsWORD BfReserved2;//Reserved WordsDWORD bfoffbits;//The number of offset bytes from the file header to the actual bitmap data}bitmapfileheader; typedefstruct{DWORD bisize;//Information Header sizeLONG Biwidth;//Image WidthLONG Biheight;//Image HeightWORD biplanes;//number of bit planes, must be 1WORD biBitCount;//number of bits per pixelDWORD bicompression;//Compression TypeDWORD biSizeImage;//compressed image size bytesLONG Bixpelspermeter;//Horizontal ResolutionLONG Biypelspermeter;//Vertical ResolutionDWORD biclrused;//the number of colors that the bitmap actually usesDWORD biclrimportant;//the number of important colors in a standard chart}bitmapinfoheader;//Bitmap information Header definition};

C + + read BMP file

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.