In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to use C++ to achieve bmp format image reading and writing", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "how to use C++ to achieve bmp format image reading and writing" bar!
One feature of bmp format images is that this kind of data is divided into four parts:
1. Bitmap header (Bitmap File Header), size: 14 bytes
Mainly includes bitmap file size and bitmap file type information
two。 Bitmap information header (Bitmap Info Header), size: 40 bytes
Mainly include: bitmap width and height, pixel units, the number of bits per pixel (1 black and white image), (4-16 color map), (8-256 color map), (24-true color map), the new BMP format can support 32-bit color. There are other horizontal and vertical resolutions (in pixels per meter), etc.
3. Color table (Color Map), size: 4 bytes
Three categories: blue component, green component and red component
4. Bitmap data (Data Body)
For a 2-color bitmap, the pixel can be represented by 1 bit, then 1 byte can store the color value of 8 pixels.
For a 16-color bitmap, a pixel color is represented by 4 bytes, then a byte can store 2 pixel color values.
For 256-color bitmaps, 1 byte stores exactly 1 pixel of color value
For a true color bitmap, it takes 3 bytes to represent the color value of a pixel
1. Read bmp Images
Bool readBmp (char * bmpName) {/ / binary read to open the specified image file FILE * fp=fopen (bmpName, "rb"); if (fp==0) return 0; / skip the bitmap header structure BITMAPFILEHEADER fseek (fp, sizeof (BITMAPFILEHEADER), 0); / / define the bitmap header structure variable, read the bitmap information header into memory, and store it in the variable head BITMAPINFOHEADER head; fread (& head, sizeof (BITMAPINFOHEADER), 1Query FP) / / get information such as image width, height, number of bits per pixel, bmpWidth = head.biWidth; bmpHeight = head.biHeight; biBitCount = head.biBitCount; / / define variables, and calculate the number of bytes per row of pixels (must be a multiple of 4) int lineByte= (bmpWidth * biBitCount/8+3) / 4x4 / / the grayscale image has a color table, and the color table entry is 256 if (biBitCount==8) {/ / the space needed to apply for the color table. Read the color table into memory pColorTable=new RGBQUAD [256]; fread (pColorTable,sizeof (RGBQUAD), 256 RGBQUAD);} / / the space needed to apply for bitmap data, and read bitmap data into memory pBmpBuf=new unsigned char [lineByte * bmpHeight]; fread (pBmpBuf,1,lineByte * bmpHeight,fp); / / close the file fclose (fp). Return 1;}
two。 Write bmp image
Bool saveBmp (char * bmpName, unsigned char * imgBuf, 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, grayscale image color table is 1024 bytes, color image color table size is 0 int colorTablesize=0; if (biBitCount==8) colorTablesize=1024 / / A multiple of 4 bytes per line of image data to be stored int lineByte= (width * biBitCount/8+3) / 4x4; / / Open the file FILE * fp=fopen (bmpName, "wb") in binary mode; if (fp==0) return 0; / / apply for bitmap header structure variable, enter the 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 by the first three parts of the image file fileHead.bfOffBits=54+colorTablesize; / / write the header file fwrite (& fileHead, sizeof (BITMAPFILEHEADER), 1, fp); / / apply for bitmap header structure variables and fill in the 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 bitmap information into memory fwrite (& head, sizeof (BITMAPINFOHEADER), 1, fp); / / if a grayscale image has a color table, write to the file if (biBitCount==8) fwrite (pColorTable, sizeof (RGBQUAD), 256, fp) / / write bitmap data into the file fwrite (imgBuf, height*lineByte, 1, fp); / / close the file fclose (fp); return 1;} Thank you for reading, these are the contents of "how to use C++ to read and write images in bmp format". After the study of this article, I believe you have a deeper understanding of how to use C++ to achieve bmp format image reading and writing, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.