Byte alignment in C ++

Source: Internet
Author: User

# Pragma pack (n) C compiler alignment according to n Bytes;
# Use The Pragma pack () method to cancel custom settings;
# Pragma pack (push, 1)Sets the original alignment mode to the pressure stack, and sets the new method to a byte alignment.
# Restore The Pragma pack (POP) to its status

In this case, when reading BMP images in C ++ (windows is not required. h) when you write your own image headers without adding # pragme pack (push, 1) & # pragme pack (POP), you cannot read the image correctly,

 

 

# Pragma pack (push, 1) typedef struct tagbitmapfileheader {unsigned short bftype; // The bitmap file type must be 0x0000d, that is, the string "BM", that is, all "*. the first two bytes of the BMP file are "BM ". Unsigned long bfsize; // the size of the bitmap file, including the 14 bytes. Unsigned short bfreserved1; // reserved words for Windows. Unsigned short bfreserved2; // reserved words for Windows. Unsigned long bfoffbits; // The number of offset bytes from the file header to the actual bitmap data, and the length of the first three parts in Figure 1-7 .} Bitmapfileheader; typedef struct tabbitmapinfoheader {unsigned long bisize; // the length of this structure, which is 40 bytes. Long biwidth; // The width of the bitmap, in pixels. Long biheight; // The height of the bitmap, in pixels. Unsigned short biplanes; // The level of the target device, which must be 1. Unsigned short bibitcount; // The number of BITs occupied by each pixel. The value must be 1 (black and white image), 4 (16 color image), and 8 (256 color) and 24 (true color chart). The new BMP format supports 32-bit colors. Unsigned long bicompression; // bitmap compression type. Valid values include bi_rgb (uncompressed), bi_rle8, bi_rle4, and bi_bitfileds (Windows-defined constants ). Here we only discuss uncompress, that is, bicompression = bi_rgb. Unsigned long bisizeimage; // The number of bytes occupied by the actual bitmap data. The size of this value is described in Part 1 of the bitmap data. Long bixpelspermeter; // specify the horizontal resolution of the target device, in pixels/meters. Long biypelspermeter; // specifies the vertical resolution of the target device, in pixels/meters. Unsigned long biclrused; // number of colors actually used by the bitmap. If the value is zero, the number of colors used is the bibitcount power of 2. Unsigned long biclrimportant; // number of important colors in the bitmap display process. If the value is zero, all colors are considered important .} Bitmapinfoheader; typedef struct tagrgbquad {unsigned char rgbblue; // The Blue weight of the color; unsigned char rgbgreen; // The green weight of the color; unsigned char rgbred; // The Red weight of the color; unsigned char rgbreserved; // reserved byte .} Rgbquad; # pragma pack (POP)

Related Article

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.