C語言實現BMP圖片生成


##

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


typedef unsigned char byte;
typedef unsigned short dbyte;
typedef unsigned long int dword;
typedef unsigned short word;


/*******************************************
*定義bmp文件的頭部數據結構
********************************************/
#pragma pack(push,2)  //保持2字節對齊


struct tagBITMAPFILEHEADER {
    //bmp file header
    dbyte bfType;        //文件類型
    dword bfSize;            //文件大小,字節為單位
    word bfReserved1;   //保留,必須為0
    word bfReserved2;   //保留,必須為0
    dword bfOffBits;         //從文件頭開始的偏移量


    //bmp info head
    dword  biSize;            //該結構的大小
    dword  biWidth;           //圖像的寬度,以像素為單位
    dword  biHeight;          //圖像的高度,以像素為單位
    word biPlanes;          //為目標設備說明位面數,其值總是設為1
    word biBitCount;        //說明比特數/像素
    dword biCompression;     //圖像數據壓縮類型
    dword biSizeImage;       //圖像大小,以字節為單位
    dword biXPelsPerMeter;   //水平分辨率,像素/米
    dword biYPelsPerMeter;   //垂直分辨率,同上
    dword biClrUsed;         //位圖實際使用的彩色表中的顏色索引數
    dword biClrImportant;    //對圖像顯示有重要影響的顏色索引的數目
      
    //bmp rgb quad
     //對於16位,24位,32位的位圖不需要色彩表
    //unsigned char rgbBlue;    //指定藍色強度
    //unsigned char rgbGreen;   //指定綠色強度
    //unsigned char rgbRed;     //指定紅色強度
    //unsigned char rgbReserved; //保留,設置為0 
}BMPFILEHEADER;
#pragma (pop)


struct tagBITMAPFILEHEADER *bmp_p;  //定義bmp文件頭結構體指針
FILE *fd;    //定義一個文件類型的指針


/*************************************************************
*初始化bmp文件頭部,設置bmp圖片
**************************************************************/


void Init_bmp_head(void)
{
    bmp_p = &BMPFILEHEADER;
    bmp_p-> bfType = 0x4D42;    //文件類型
    bmp_p-> bfSize = 0x25836;   //文件大小,字節為單位
    bmp_p-> bfReserved1 = 0x0;   //保留,必須為0
    bmp_p-> bfReserved2 = 0x0;   //保留,必須為0
    bmp_p-> bfOffBits = 0x36;         //從文件頭開始的偏移量
 
    //bmp info head
    bmp_p-> biSize = 0x28;            //該結構的大小
    bmp_p-> biWidth = 320;           //圖像的寬度,以像素為單位
    bmp_p-> biHeight = 240;          //圖像的高度,以像素為單位
    bmp_p-> biPlanes = 0x01;          //為目標設備說明位面數,其值總是設為1
    bmp_p-> biBitCount = 16;        //說明比特數/像素
    bmp_p-> biCompression = 0;     //圖像數據壓縮類型
    bmp_p-> biSizeImage = 0x25800;//0x09f8;       //圖像大小,以字節為單位
    bmp_p-> biXPelsPerMeter = 0x60;//0x60;   //水平分辨率,像素/米
    bmp_p-> biYPelsPerMeter = 0x60;   //垂直分辨率,同上
    bmp_p-> biClrUsed = 0;         //位圖實際使用的彩色表中的顏色索引數
    bmp_p-> biClrImportant = 0;    //對圖像顯示有重要影響的顏色索引的數目
     
}


int main(void)
{
    static char *file_name =NULL;    //保存文件名的指針
    static long file_length = 0x25836;  //文件的大小(整個文件)
    unsigned char *file_p = NULL;       //寫入數據指針
    unsigned char *file_p_tmp = NULL;    //寫入數據臨時指針
    unsigned char *byte_copy_p = NULL;   //文件頭部傳遞指針
    unsigned char byte_copy = 0;        //文件頭部數據拷貝變量
    int i = 0;         
    file_name = "test1.bmp";
    Init_bmp_head(); 
    file_p = (unsigned char *)malloc(sizeof(char)*153654);   //申請一段內存
    file_p_tmp = file_p;
    for(i = 0;i < 153654;i++ )
    {
        if(i%2 ==0)
        {
            *file_p_tmp = 0x00;    //圖像前8位值 
        }
        else  
        { 
            *file_p_tmp = 0xf0;    //圖像后8位值 
        }
       file_p_tmp++;
    } 
    byte_copy_p = (unsigned char *)bmp_p;
    file_p_tmp = file_p;
    for(i = 0;i < 54;i++)
    {
        *file_p_tmp = *byte_copy_p; 
        file_p_tmp++;
        byte_copy_p++;
    }
   fd = fopen(file_name, "w");
   fwrite(file_p, file_length, 1,fd);
   free(file_p);                        //釋放申請的內存(重要)
   fclose(fd); 
   printf("Done success!!!\n");
   getchar();
   return (0);
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM