Read BMP images in C Language

Source: Internet
Author: User

# Include <windows. h> // contains the definition structure of the relevant BMP format <br/> # include <stdio. h> <br/> # include <stdlib. h> </P> <p> int BMP width; // BMP image width (pixels) <br/> int bmpheight; // BMP Image Height (pixels) <br/> rgbquad * pcolortable; // color palette array (not used in true color) <br/> int bibitcount; // bytes occupied by each pixel </P> <p> /************************* ********************************** <br/> * Name: read_bmp <br/> * function: Read the BMP image and return the read hexadecimal array <br/> * entry parameter: BMP filename: BMP image name <br/> * exit parameter: none <br/> *********************************** * *************************/<br/> unsigned char * read_bmp (const char * BMP filename) <br/>{< br/> unsigned char * pbmpbuf; // storage buffer <br/> file * fp = fopen (BMP filename, "rb "); // enable the binary system <br/> If (FP = NULL) <br/> {<br/> printf ("can not open this file "); <br/> return 0; <br/>}< br/> fseek (FP, sizeof (bitmapfileheader), 0); // skip the BMP header <br/> bitmapinfoheader infoheader; <br/> fread (& infoheader, sizeof (bitmapinfoheader), 1, FP); // read BMP Image Information <br/> BMP width = infoheader. biwidth; <br/> bmpheight = infoheader. biheight; <br/> bibitcount = infoheader. bibitcount; <br/> int line_byte = (BMP width * bibitcount/8 + 3)/4*4; // The number of bytes in each row <br/> If (bibitcount = 8) <br/>{< br/> pcolortable = new rgbquad [256]; <br/> fread (pcolortable, sizeof (rgbquad), 256, FP ); <br/>}< br/> pbmpbuf = new unsigned char [line_byte * bmpheight]; // open the storage area <br/> fread (pbmpbuf, 1, line_byte * bmpheight, FP); <br/> fclose (FP); // close the file <br/> return pbmpbuf; <br/>}</P> <p> /*************************** * ********************************* <br/> * Name: main <br/> * function: test function <br/> * entry parameter: None <br/> * exit parameter: none <br/> *********************************** * **************************/<br/> int main () <br/>{< br/> unsigned char * buffer = read_bmp ("1.bmp"); // read BMP images <br/> file * fp = fopen ("test.txt ", "WB"); <br/> fwrite (buffer, 1, (BMP width * bibitcount/8 + 3)/4*4 * bmpheight, FP ); // write the read image data to test.txt <br/> return 0; <br/>}

There is a problem here: The hexadecimal data stored in the text file is garbled and needs to be read using a hexadecimal editing tool. By default, the BMP image reading sequence starts from the lower left corner, in this way, you need to scan data from the lower left corner or convert the data to the order starting from the upper left corner.

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.