[VC application] Read and Write BMP files

Source: Internet
Author: User

BMP file read/write

Skyseraph Mar 10th 2011 hqu

Email: zgzhaobo@gmail.com QQ: 452728574

Latest modified Date: Mar 10th 2011 hqu

Step 1: Include the header file and define global variables

# Include "windows. H" // contains bitmapfileheader, bitmapinfoheader, rgbquad, and other structural Definitions

Unsigned char * pbmpbuf; // pointer for reading image data
Int BMP width; // The image width.
Int bmpheight; // Image Height
Rgbquad * pcolortable; // color table pointer
Int bibitcount; // image type, number of digits per pixel

Step 2: Write a read/write function

Read operations

 /************************************ * *********************************** Function Name: readbmp () * function parameter: char * BMP name-file name and path * return value: 0 indicates failure, 1 indicates success * Description: an image file name and path are given, read the bitmap data, width, height, color table, and * bits per pixel into the memory, in the corresponding global variables ********************************* * ***********************************/bool readbmp (char * BMP name) {// open the specified image file * fp = fopen (BMP name, "rb") in binary read mode; If (FP = 0) return 0; // skip the bitmap file header structure bitmapfileheaderfseek (FP, sizeof (bitmapfileheader), 0); // define the bitmap information header structure variable and read the bitmap information header into the memory, in the variable head, bitmapinfoheader head; fread (& head, sizeof (bitmapinfoheader), 1, FP ); // obtain information such as image width, height, and the number of digits occupied by each pixel. BMP width = head. biwidth; bmpheight = head. biheight; bibitcount = head. bibitcount; // defines the variable to calculate the number of bytes occupied by pixels in each line of the image (must be a multiple of 4) int linebyte = (BMP width * bibitcount/8 + 3)/4*4; // grayscale images have a color table, and the color table items are 256if (bibitcount = 8) // apply for the space required by the color table, read the color table into memory {pcolortable = new rgbquad [256]; fread (pcolortable, sizeof (rgbquad), 256, FP);} // space required to apply for bitmap data, read bitmap data into memory pbmpbuf = new unsigned char [linebyte * bmpheight]; fread (pbmpbuf, 1, linebyte * bmpheight, FP); // close the file fclose (FP ); return 1 ;}

Write operation

/*************************************** * ** Function Name: savebmp () * function parameters: * char * BMP name-file name and path * unsigned char * imgbuf-bitmap data to be stored on the disk * int width-the width of the bitmap to be stored in pixels * int height-pixel unit: disk to be saved. The bitmap height is * int ititcount-the number of digits occupied by each pixel * rgbquad * pcolortable-color table pointer *. Return Value: 0 is a failure, 1 is a success ** Note: given an image bitmap data, width, height, color table pointer, and the number of digits occupied by each pixel, etc, * write it to the specified file ********************************* * ***********************************/bool savebmp (char * BMP name, unsigned char * I Mgbuf, int width, int height, int bibitcount, rgbquad * pcolortable) {// If the bitmap data pointer is 0, no data is passed in, and the function returns if (! Imgbuf) return 0; // color table size, in bytes. The gray image color table size is 1024 bytes, and the color image color table size is 0int colortablesize = 0; If (bibitcount = 8) colortablesize = 1024; // The number of bytes in each row of the image data to be stored is a multiple of 4. Int linebyte = (width * bibitcount/8 + 3)/4*4; // open the file * fp = fopen (BMP name, "WB") in binary mode; If (FP = 0) return 0; // apply for bitmap file header Structure Variables, fill in the file header information bitmapfileheader filehead; filehead. bftype = 0x4d42; // BMP Type // bfsize is the sum of the four components of the image file filehead. bfsize = sizeof (bitmapfileheader) + sizeof (bitmapinfoheader) + colortablesize + linebyte * height; filehead. bfreserved1 = 0; filehead. bfreserved2 = 0; // bfoffbits is the sum of the space required for the first three parts of the image file filehead. bfoffbits = 54 + colortablesize; // write the file header into the file fwrite (& filehead, sizeof (bitmapfileheader), 1, FP); // apply for bitmap information header Structure Variables, fill in the Information header information bitmapinfoheader head; head. bibitcount = bibitcount; head. biclrimportant = 0; head. biclrused = 0; head. bicompression = 0; head. biheight = height; head. biplanes = 1; head. bisize = 40; head. bisizeimage = linebyte * height; head. biwidth = width; head. bixpelspermeter = 0; head. biypelspermeter = 0; // write the bitmap information header into the memory fwrite (& head, sizeof (bitmapinfoheader), 1, FP); // If the grayscale image has a color table, write File if (bibitcount = 8) fwrite (pcolortable, sizeof (rgbquad), 256, FP); // write bitmap data into file fwrite (imgbuf, height * linebyte, 1, FP); // close the file fclose (FP); return 1 ;}

Step 3: Compile the main function

Test 1

 
Int main () {// read the specified BMP file into the memory char readpath [] = "lena.bmp"; readbmp (readpath ); // output image information printf ("width = % d, Height = % d, bibitcount = % d \ n", BMP width, bmpheight, bibitcount ); // Save the image data disk char writepath [] = "lenacpy1.bmp"; savebmp (writepath, pbmpbuf, BMP width, bmpheight, bibitcount, pcolortable); // clear the buffer, pbmpbuf and pcolortable are global variables. When a file is read, the requested space is Delete [] pbmpbuf; If (bibitcount = 8) Delete [] pcolortable; return 0 ;}

Test 2

Int main () {// read the specified BMP file into the memory char readpath [] = "lena.bmp"; readbmp (readpath ); // output image information printf ("width = % d, Height = % d, bibitcount = % d \ n", BMP width, bmpheight, bibitcount); // loop variable, the coordinate int I, j; // number of bytes in each row int linebyte = (BMP width * bibitcount/8 + 3)/4*4; // cyclic variable, for color images, traverse each pixel's three components int K; // set the first part of the image in the lower left corner to black if (bibitcount = 8) // for Grayscale Images {for (I = 0; I <bmpheight/2; I ++) {for (j = 0; j <BMP width/2; j ++) {* (pbmpbuf + I * linebyte + J) = 0 ;}} else if (bibitcount = 24) // color image {for (I = 0; I <bmpheight/2; I ++) {for (j = 0; j <BMP width/2; j ++) {for (k = 0; k <3; k ++) // each pixel RGB three components are set to 0 to black * (pbmpbuf + I * linebyte + J * 3 + k) = 0 ;}}} // Save the image data disk char writepath [] = "lenacpy2.bmp"; savebmp (writepath, pbmpbuf, BMP width, bmpheight, bibitcount, pcolortable); // clear the buffer, pbmpbuf and pcolortable are global variables. When a file is read, the requested space is Delete [] pbmpbuf; If (bibitcount = 8) Delete [] pcolortable; return 0;

Test 3

Int main () {// read the specified BMP file into the memory char readpath [] = "lena.bmp"; readbmp (readpath ); // output image information printf ("width = % d, Height = % d, bibitcount = % d \ n", BMP width, bmpheight, bibitcount ); // change the value of the blue component of the grayscale image. Check the changes before and after the image. If (bibitcount = 8) {for (INT I = 0; I <256; I ++) {pcolortable [I]. rgbblue = 255-pcolortable [I]. rgbblue ;}}// Save the image data disk char writepath [] = "lenacpy3.bmp"; savebmp (writepath, pbmpbuf, BMP width, bmpheight, bibitcount, pcolortable); // clear the buffer, pbmpbuf and pcolortable are global variables. When a file is read, the requested space is Delete [] pbmpbuf; If (bibitcount = 8) Delete [] pcolortable; return 0 ;}

Author: skyseraph

Email/Gtalk: zgzhaobo@gmail.com QQ: 452728574

From: http://www.cnblogs.com/skyseraph/

the copyright of this article is shared by the author and the blog. You are welcome to reprint it. However, you must retain this statement without the consent of the author, the original Article connection is clearly displayed on the Article page. Please respect the labor achievements of the author.

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.