zoukankan      html  css  js  c++  java
  • C++中的字节对齐

    #pragma pack(n) C编译器按照n个字节对齐;
    #pragma pack()  取消自定义对其方式;
    #pragma pack(push,1) 把原来对齐方式设置压栈,并设新的对其方式设置为一个字节对齐
    #pragma pack(pop) 恢复对其状态

    其中的案例是在c++读取bmp图片时(不用Windows.h),自己编写图片头的时候不加入#pragme pack(push,1) & #pragme pack(pop) 时,是不能正确读取图片的,

    #pragma pack(push, 1)
    typedef struct tagBITMAPFILEHEADER
    {
    unsigned short bfType;//位图文件类型 必须是0x424D,即字符串“BM”,也就是说,所有的“*.bmp”文件的头两个字节都是“BM”。
    unsigned long bfSize;//位图文件大小,包括这14个字节。
    unsigned short bfReserved1;//Windows保留字,暂不用。
    unsigned short bfReserved2;//Windows保留字,暂不用。
    unsigned long bfOffbits;//从文件头到实际的位图数据的偏移字节数,图1-7中前3个部分的长度之和。
    }BITMAPFILEHEADER;
    
    
    typedef struct tabBITMAPINFOHEADER
    {
    unsigned long biSize;//本结构的长度,为40个字节。
    long biWidth;//位图的宽度,以像素为单位。
    long biHeight;//位图的高度,以像素为单位。
    unsigned short biPlanes;//目标设备的级别,必须是1。
    unsigned short biBitCount;//每个像素所占的位数(bit),其值必须为1(黑白图像)、4(16色图)、8(256色)、24(真彩色图),新的BMP格式支持32位色。
    unsigned long biCompression;//位图压缩类型,有效的值为BI_RGB(未经压缩)、BI_RLE8、BI_RLE4、BI_BITFILEDS(均为Windows定义常量)。这里只讨论未经压缩的情况,即biCompression=BI_RGB。
    unsigned long biSizeImage;//实际的位图数据占用的字节数,该值的大小在第4部分位图数据中有具体解释。
    long biXPelsPerMeter;//指定目标设备的水平分辨率,单位是像素/米。
    long biYPelsPerMeter;//指定目标设备的垂直分辨率,单位是像素/米。
    unsigned long biClrUsed;//位图实际用到的颜色数,如果该值为零,则用到的颜色数为2的biBitCount次幂。
    unsigned long biClrImportant;//位图显示过程中重要的颜色数,如果该值为零,则认为所有的颜色都是重要的。
    }BITMAPINFOHEADER;
    
    typedef struct tagRGBQUAD
    {
    unsigned char rgbBlue;//该颜色的蓝色分量;
    unsigned char rgbGreen;//该颜色的绿色分量;
    unsigned char rgbRed;//该颜色的红色分量;
    unsigned char rgbReserved;//保留字节,暂不用。
    }RGBQUAD;
    #pragma pack(pop)
    

      

  • 相关阅读:
    2. Add Two Numbers
    1. Two Sum
    22. Generate Parentheses (backTracking)
    21. Merge Two Sorted Lists
    20. Valid Parentheses (Stack)
    19. Remove Nth Node From End of List
    18. 4Sum (通用算法 nSum)
    17. Letter Combinations of a Phone Number (backtracking)
    LeetCode SQL: Combine Two Tables
    LeetCode SQL:Employees Earning More Than Their Managers
  • 原文地址:https://www.cnblogs.com/tongchuanxing/p/C_bmp.html
Copyright © 2011-2022 走看看