將rgb映像資料儲存為BMP格式圖片的方法

來源:互聯網
上載者:User
extern "C"{    #include <stdio.h>    #include <stdlib.h>    #include <jpeglib.h>}typedef long LONG;typedef unsigned long DWORD;typedef unsigned short WORD;typedef struct {        WORD    bfType;        DWORD   bfSize;        WORD    bfReserved1;        WORD    bfReserved2;        DWORD   bfOffBits;} BMPFILEHEADER_T;typedef struct{        DWORD      biSize;        LONG       biWidth;        LONG       biHeight;        WORD       biPlanes;        WORD       biBitCount;        DWORD      biCompression;        DWORD      biSizeImage;        LONG       biXPelsPerMeter;        LONG       biYPelsPerMeter;        DWORD      biClrUsed;        DWORD      biClrImportant;} BMPINFOHEADER_T;void savebmp(uchar * pdata, char * bmp_file, int width, int height ){      //分別為rgb資料,要儲存的bmp檔案名稱,圖片長寬       int size = width*height*3*sizeof(char); // 每個像素點3個位元組       // 位元影像第一部分,檔案資訊       BMPFILEHEADER_T bfh;       bfh.bfType = (WORD)0x4d42;  //bm       bfh.bfSize = size  // data size              + sizeof( BMPFILEHEADER_T ) // first section size              + sizeof( BMPINFOHEADER_T ) // second section size              ;       bfh.bfReserved1 = 0; // reserved       bfh.bfReserved2 = 0; // reserved       bfh.bfOffBits = sizeof( BMPFILEHEADER_T )+ sizeof( BMPINFOHEADER_T );//真正的資料的位置       // 位元影像第二部分,資料資訊       BMPINFOHEADER_T bih;       bih.biSize = sizeof(BMPINFOHEADER_T);       bih.biWidth = width;       bih.biHeight = -height;//BMP圖片從最後一個點開始掃描,顯示時圖片是倒著的,所以用-height,這樣圖片就正了       bih.biPlanes = 1;//為1,不用改       bih.biBitCount = 24;       bih.biCompression = 0;//不壓縮       bih.biSizeImage = size;       bih.biXPelsPerMeter = 2835 ;//像素每米       bih.biYPelsPerMeter = 2835 ;       bih.biClrUsed = 0;//已用過的顏色,24位的為0       bih.biClrImportant = 0;//每個像素都重要       FILE * fp = fopen( bmp_file,"wb" );       if( !fp ) return;       fwrite( &bfh, 8, 1,  fp );//由於linux上4位元組對齊,而資訊頭大小為54位元組,第一部分14位元組,第二部分40位元組,所以會將第一部分補齊為16自己,直接用sizeof,開啟圖片時就會遇到premature end-of-file encountered錯誤       fwrite(&bfh.bfReserved2, sizeof(bfh.bfReserved2), 1, fp);       fwrite(&bfh.bfOffBits, sizeof(bfh.bfOffBits), 1, fp);       fwrite( &bih, sizeof(BMPINFOHEADER_T),1,fp );       fwrite(pdata,size,1,fp);       fclose( fp );}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.