zoukankan      html  css  js  c++  java
  • [转]RGB数据保存为BMP图片

            转自:http://blog.csdn.net/yixianfeng41/article/details/52591585

    一、BMP文件由文件头、位图信息头、颜色信息和图形数据四部分组成。

    1、BMP文件头(14字节)
    typedef struct                       /**** BMP file header structure ****/  
    {  
        unsigned int   bfSize;           /* Size of file */  
        unsigned short bfReserved1;      /* Reserved */  
        unsigned short bfReserved2;      /* ... */  
        unsigned int   bfOffBits;        /* Offset to bitmap data */  
    } MyBITMAPFILEHEADER;
    2、位图信息头(40字节)
    typedef struct                       /**** BMP file info structure ****/  
    {  
        unsigned int   biSize;           /* Size of info header */  
        int            biWidth;          /* Width of image */  
        int            biHeight;         /* Height of image */  
        unsigned short biPlanes;         /* Number of color planes */  
        unsigned short biBitCount;       /* Number of bits per pixel */  
        unsigned int   biCompression;    /* Type of compression to use */  
        unsigned int   biSizeImage;      /* Size of image data */  
        int            biXPelsPerMeter;  /* X pixels per meter */  
        int            biYPelsPerMeter;  /* Y pixels per meter */  
        unsigned int   biClrUsed;        /* Number of colors used */  
        unsigned int   biClrImportant;   /* Number of important colors */  
    } MyBITMAPINFOHEADER;
    3、颜色表

    颜色表用于说明位图中的颜色,它有若干个表项,每一个表项是一个RGBQUAD类型的结构,定义一种颜色。RGBQUAD结构的定义如下:

    typedef struct tagRGBQUAD{  
        BYTE rgbBlue;//蓝色的亮度(值范围为0-255)  
        BYTE rgbGreen;//绿色的亮度(值范围为0-255)  
        BYTE rgbRed;//红色的亮度(值范围为0-255)  
        BYTE rgbReserved;//保留,必须为0  
    }RGBQUAD;

    颜色表中的RGBQUAD结构数据的个数由biBitCount来确定:当biBitCount=1,4,8时,分别为2,16,256个表项;当biBitCount=24时,没有颜色表项。

    4、位图数据

    位图数据记录了位图的每一个像素值,记录顺序是在扫描行内是从左到右,扫描行之间是从下到上。位图的一个像素值所占的字节数:

    当biBitCount=1时,8个像素占1个字节;

    当biBitCount=4时,2个像素占1个字节;

    当biBitCount=8时,1个像素占1个字节;

    当biBitCount=24时,1个像素占3个字节,按顺序分别为B,G,R;

    二、将rgb数据保存为bmp图片的方法

    void CDecVideoFilter::MySaveBmp(const char *filename,unsigned char *rgbbuf,int width,int height)  
    {  
        MyBITMAPFILEHEADER bfh;  
        MyBITMAPINFOHEADER bih;  
        /* Magic number for file. It does not fit in the header structure due to alignment requirements, so put it outside */  
        unsigned short bfType=0x4d42;             
        bfh.bfReserved1 = 0;  
        bfh.bfReserved2 = 0;  
        bfh.bfSize = 2+sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)+width*height*3;  
        bfh.bfOffBits = 0x36;  
      
        bih.biSize = sizeof(BITMAPINFOHEADER);  
        bih.biWidth = width;  
        bih.biHeight = height;  
        bih.biPlanes = 1;  
        bih.biBitCount = 24;  
        bih.biCompression = 0;  
        bih.biSizeImage = 0;  
        bih.biXPelsPerMeter = 5000;  
        bih.biYPelsPerMeter = 5000;  
        bih.biClrUsed = 0;  
        bih.biClrImportant = 0;  
      
        FILE *file = fopen(filename, "wb");  
        if (!file)  
        {  
            printf("Could not write file
    ");  
            return;  
        }  
      
        /*Write headers*/  
        fwrite(&bfType,sizeof(bfType),1,file);  
        fwrite(&bfh,sizeof(bfh),1, file);  
        fwrite(&bih,sizeof(bih),1, file);  
      
        fwrite(rgbbuf,width*height*3,1,file);  
        fclose(file);  
    }
  • 相关阅读:
    学习使用&运算符
    企业发放的奖金根据利润提成。
    取一个整数a从右端开始的47位。
    jQuery Select操作大集合
    js 获取某年某月的最后一天
    sql 语句区分大小写查询
    js 冒泡排序
    一个初学者的程序自学计划
    JWNL体验
    (转)GIS相关的SCI、EI期刊
  • 原文地址:https://www.cnblogs.com/betterwgo/p/6909787.html
Copyright © 2011-2022 走看看