为拍摄的RGB数据加上BMP文件头

[cpp]  view plain  copy
  1. #ifndef RGB2BMP_H  
  2. #define RGB2BMP_H  
  3. ///为拍摄的RGB数据加上BMP文件头  
  4. ///作者:  
  5. /// 2014.6.25  
  6. ///  
  7. typedef unsigned char  BYTE;  
  8. typedef unsigned short WORD;  
  9. // BMP图像各部分说明如下  
  10. /*********** 
  11.     第一部分    位图文件头 
  12. 该结构的长度是固定的,为14个字节,各个域的依次如下: 
  13.     2byte   :文件类型,必须是0x4d42,即字符串"BM"。 
  14.     4byte   :整个文件大小 
  15.     4byte   :保留字,为0 
  16.     4byte   :从文件头到实际的位图图像数据的偏移字节数。 
  17. *************/  
  18. typedef struct  
  19. {    long imageSize;  
  20.     long blank;  
  21.     long startPosition;  
  22. }BmpHead;  
  23. /********************* 
  24. /********************* 
  25. 第二部分    位图信息头 
  26. 该结构的长度也是固定的,为40个字节,各个域的依次说明如下: 
  27.     4byte   :本结构的长度,值为40 
  28.     4byte   :图像的宽度是多少象素。 
  29.     4byte   :图像的高度是多少象素。 
  30.     2Byte   :必须是1。 
  31.     2Byte   :表示颜色时用到的位数,常用的值为1(黑白二色图)、4(16色图)、8(256色图)、24(真彩色图)。 
  32.     4byte   :指定位图是否压缩,有效值为BI_RGB,BI_RLE8,BI_RLE4,BI_BITFIELDS。Windows位图可采用RLE4和RLE8的压缩格式,BI_RGB表示不压缩。 
  33.     4byte   :指定实际的位图图像数据占用的字节数,可用以下的公式计算出来: 
  34.      图像数据 = Width' * Height * 表示每个象素颜色占用的byte数(即颜色位数/8,24bit图为3,256色为1) 
  35.      要注意的是:上述公式中的biWidth'必须是4的整数倍(不是biWidth,而是大于或等于biWidth的最小4的整数倍)。 
  36.      如果biCompression为BI_RGB,则该项可能为0。 
  37.     4byte   :目标设备的水平分辨率。 
  38.     4byte   :目标设备的垂直分辨率。 
  39.     4byte   :本图像实际用到的颜色数,如果该值为0,则用到的颜色数为2的(颜色位数)次幂,如颜色位数为8,2^8=256,即256色的位图 
  40.     4byte   :指定本图像中重要的颜色数,如果该值为0,则认为所有的颜色都是重要的。 
  41. ***********************************/  
  42. typedef struct  
  43.   
  44. {  
  45.     long    Length;  
  46.     long    width;  
  47.     long    height;  
  48.     WORD    colorPlane;  
  49.     WORD    bitColor;  
  50.     long    zipFormat;  
  51.     long    realSize;  
  52.     long    xPels;  
  53.     long    yPels;  
  54.     long    colorUse;  
  55.     long    colorImportant;  
  56.   
  57. }InfoHead;  
  58. /*************************** 
  59. /*************************** 
  60.     第三部分    调色盘结构  颜色表 
  61.     对于256色BMP位图,颜色位数为8,需要2^8 = 256个调色盘; 
  62.     对于24bitBMP位图,各象素RGB值直接保存在图像数据区,不需要调色盘,不存在调色盘区 
  63.     rgbBlue:   该颜色的蓝色分量。 
  64.     rgbGreen:  该颜色的绿色分量。 
  65.     rgbRed:    该颜色的红色分量。 
  66.     rgbReserved:保留值。 
  67. *****************************/  
  68. typedef struct  
  69. {         BYTE   rgbBlue;  
  70.          BYTE   rgbGreen;  
  71.          BYTE   rgbRed;  
  72.          BYTE   rgbReserved;  
  73.   
  74. }RGBMixPlate;  
  75. /********************* 
  76. /********************* 
  77. //输入 
  78. // rgb_buffer: RGB24缓冲区指针 
  79. // nWidth    : 图片宽度 
  80. // nHeight   : 图片高度 
  81. // fp2       : 指向加bmp头后的数据 
  82. **************************/  
  83. int RGB2BMP1(char *rgb_buffer,int nWidth,int nHeight,char*fp2)  
  84. {  
  85.      BmpHead m_BMPHeader;  
  86.      char bfType[2]={'B','M'};  
  87.      m_BMPHeader.imageSize=3*nWidth*nHeight+54;  
  88.      m_BMPHeader.blank=0;  
  89.      m_BMPHeader.startPosition=54;  
  90.      char* temp=fp2;  
  91.      memcpy(fp2,bfType,2);  
  92.      fp2+=sizeof(bfType);  
  93.   
  94.      memcpy(fp2,&m_BMPHeader,sizeof(m_BMPHeader.imageSize));  
  95.      fp2+=sizeof(m_BMPHeader.imageSize);  
  96.   
  97.   
  98.      memcpy(fp2,&m_BMPHeader.blank,sizeof(m_BMPHeader.blank));  
  99.      fp2+=sizeof(m_BMPHeader.blank);  
  100.   
  101.   
  102.      memcpy(fp2,&m_BMPHeader.startPosition,sizeof(m_BMPHeader.startPosition));  
  103.      fp2+=sizeof(m_BMPHeader.startPosition);  
  104.   
  105.   
  106.      InfoHead  m_BMPInfoHeader;  
  107.      m_BMPInfoHeader.Length=40;  
  108.      m_BMPInfoHeader.width=nWidth;  
  109.      m_BMPInfoHeader.height=-nHeight;  
  110.      m_BMPInfoHeader.colorPlane=1;  
  111.      m_BMPInfoHeader.bitColor=24;  
  112.      m_BMPInfoHeader.zipFormat=0;  
  113.      m_BMPInfoHeader.realSize=3*nWidth*nHeight;  
  114.      m_BMPInfoHeader.xPels=0;  
  115.      m_BMPInfoHeader.yPels=0;  
  116.      m_BMPInfoHeader.colorUse=0;  
  117.      m_BMPInfoHeader.colorImportant=0;  
  118.   
  119.      memcpy(fp2,&m_BMPInfoHeader.Length,sizeof(m_BMPInfoHeader.Length));  
  120.       fp2+=sizeof(m_BMPInfoHeader.Length);  
  121.   
  122.      memcpy(fp2,&m_BMPInfoHeader.width,sizeof(m_BMPInfoHeader.width));  
  123.       fp2+=sizeof(m_BMPInfoHeader.width);  
  124.   
  125.       memcpy(fp2,&m_BMPInfoHeader.height,sizeof(m_BMPInfoHeader.height));  
  126.       fp2+=sizeof(m_BMPInfoHeader.height);  
  127.   
  128.      memcpy(fp2,&m_BMPInfoHeader.colorPlane,sizeof(m_BMPInfoHeader.colorPlane));  
  129.      fp2+=sizeof(m_BMPInfoHeader.colorPlane);  
  130.   
  131.      memcpy(fp2,&m_BMPInfoHeader.bitColor,sizeof(m_BMPInfoHeader.bitColor));  
  132.      fp2+=sizeof(m_BMPInfoHeader.bitColor);  
  133.   
  134.   
  135.      memcpy(fp2,&m_BMPInfoHeader.zipFormat,sizeof(m_BMPInfoHeader.zipFormat));  
  136.      fp2+=sizeof(m_BMPInfoHeader.zipFormat);  
  137.   
  138.      memcpy(fp2,&m_BMPInfoHeader.realSize,sizeof(m_BMPInfoHeader.realSize));  
  139.      fp2+=sizeof(m_BMPInfoHeader.realSize);  
  140.   
  141.      memcpy(fp2,&m_BMPInfoHeader.xPels,sizeof(m_BMPInfoHeader.xPels));  
  142.      fp2+=sizeof(m_BMPInfoHeader.xPels);  
  143.   
  144.      memcpy(fp2,&m_BMPInfoHeader.yPels,sizeof(m_BMPInfoHeader.yPels));  
  145.      fp2+=sizeof(m_BMPInfoHeader.yPels);  
  146.   
  147.      memcpy(fp2,&m_BMPInfoHeader.colorUse,sizeof(m_BMPInfoHeader.colorUse));  
  148.      fp2+=sizeof(m_BMPInfoHeader.colorUse);  
  149.   
  150.      memcpy(fp2,&m_BMPInfoHeader.colorImportant,sizeof(m_BMPInfoHeader.colorImportant));  
  151.      fp2+=sizeof(m_BMPInfoHeader.colorImportant);  
  152.   
  153.      memcpy(fp2,rgb_buffer,3*nWidth*nHeight);  
  154.      fp2=temp;  
  155.      return 1;  
  156. }  
  157.   
  158. #endif // RGB2BMP_H  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值