1. 程式人生 > >RGB資料儲存為BMP圖片

RGB資料儲存為BMP圖片

一、BMP檔案由檔案頭、點陣圖資訊頭、顏色資訊和圖形資料四部分組成。

1、BMP檔案頭(14位元組)

  1. typedefstruct/**** BMP file header structure ****/
  2. {  
  3.     unsigned int   bfSize;           /* Size of file */
  4.     unsigned short bfReserved1;      /* Reserved */
  5.     unsigned short bfReserved2;      /* ... */
  6.     unsigned int   bfOffBits;        /* Offset to bitmap data */
  7. } MyBITMAPFILEHEADER;  

2、點陣圖資訊頭(40位元組)

  1. typedefstruct/**** BMP file info structure ****/
  2. {  
  3.     unsigned int   biSize;           /* Size of info header */
  4.     int            biWidth;          /* Width of image */
  5.     int            biHeight;         /* Height of image */
  6.     unsigned short biPlanes;         
    /* Number of color planes */
  7.     unsigned short biBitCount;       /* Number of bits per pixel */
  8.     unsigned int   biCompression;    /* Type of compression to use */
  9.     unsigned int   biSizeImage;      /* Size of image data */
  10.     int            biXPelsPerMeter;  /* X pixels per meter */
  11.     int            biYPelsPerMeter;  
    /* Y pixels per meter */
  12.     unsigned int   biClrUsed;        /* Number of colors used */
  13.     unsigned int   biClrImportant;   /* Number of important colors */
  14. } MyBITMAPINFOHEADER;  

3、顏色表

顏色表用於說明點陣圖中的顏色,它有若干個表項,每一個表項是一個RGBQUAD型別的結構,定義一種顏色。RGBQUAD結構的定義如下:

  1. typedefstructtagRGBQUAD{  
  2.     BYTErgbBlue;//藍色的亮度(值範圍為0-255)
  3.     BYTErgbGreen;//綠色的亮度(值範圍為0-255)
  4.     BYTErgbRed;//紅色的亮度(值範圍為0-255)
  5.     BYTErgbReserved;//保留,必須為0
  6. }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圖片的方法

  1. void CDecVideoFilter::MySaveBmp(constchar *filename,unsigned char *rgbbuf,int width,int height)  
  2. {  
  3.     MyBITMAPFILEHEADER bfh;  
  4.     MyBITMAPINFOHEADER bih;  
  5.     /* Magic number for file. It does not fit in the header structure due to alignment requirements, so put it outside */
  6.     unsigned short bfType=0x4d42;             
  7.     bfh.bfReserved1 = 0;  
  8.     bfh.bfReserved2 = 0;  
  9.     bfh.bfSize = 2+sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)+width*height*3;  
  10.     bfh.bfOffBits = 0x36;  
  11.     bih.biSize = sizeof(BITMAPINFOHEADER);  
  12.     bih.biWidth = width;  
  13.     bih.biHeight = height;  
  14.     bih.biPlanes = 1;  
  15.     bih.biBitCount = 24;  
  16.     bih.biCompression = 0;  
  17.     bih.biSizeImage = 0;  
  18.     bih.biXPelsPerMeter = 5000;  
  19.     bih.biYPelsPerMeter = 5000;  
  20.     bih.biClrUsed = 0;  
  21.     bih.biClrImportant = 0;  
  22.     FILE *file = fopen(filename, "wb");  
  23.     if (!file)  
  24.     {  
  25.         printf("Could not write file\n");  
  26.         return;  
  27.     }  
  28.     /*Write headers*/
  29.     fwrite(&bfType,sizeof(bfType),1,file);  
  30.     fwrite(&bfh,sizeof(bfh),1, file);  
  31.     fwrite(&bih,sizeof(bih),1, file);  
  32.     fwrite(rgbbuf,width*height*3,1,file);  
  33.     fclose(file);  
  34. }  

三、測試

相關推薦

RGB資料儲存BMP圖片

一、BMP檔案由檔案頭、點陣圖資訊頭、顏色資訊和圖形資料四部分組成。 1、BMP檔案頭(14位元組) typedefstruct/**** BMP file header structure ****/ {       unsigned int   bfSize;

24位RGB資料儲存BMP圖片

在做Qt與ffmpeg結合的視訊播放器時,由於解碼後是RGB24資料格式,不知道解碼的資料是否正確,於是在網上找了很久才找到一個RGB24轉bmp檔案的方法,於是嘗試了一下,發現生成的bmp檔案是電影中的片段截圖,才知道解碼的RGB24資料是正確的,最終才成功用

純C++程式碼實現將畫素矩陣儲存bmp圖片

       用C++程式碼將畫素矩陣儲存為圖片,這裡以讀取yuv序列視訊幀為例進行分析,假設4:2:0yuv序列有300幀,則首先需要將每一視訊幀儲存在一個畫素矩陣中,然後將每一個矩陣儲存為圖片,最終會有300個bmp圖片。       純C++程式碼如下:       s

C++將HBITMAP儲存bmp圖片

BOOL CBMP2ArrayMultiplyDlg::SaveBitmapToFile(HBITMAP hBitmap, CString szfilename) { HDC hDC; //當前解析度下每象素所佔位元組數

使用ffmpeg將BMP圖片編碼x264視訊檔案,將H264視訊儲存BMP圖片,yuv視訊檔案儲存圖片的程式碼

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <windows.h> #ifdef __cplusplus extern "C" { #endif #include

libqrencode學習筆記(二): 用libqrencode靜態庫庫生成二維碼並儲存BMP圖片

#include <stdio.h> #include <Windows.h> #include <qrencode.h> #pragma comment(lib,"libqrencode.lib") int main() { const char * QRTEXT

raw資料新增影象頭,並儲存bmp影象(改進)

改進:不再從參考影象讀調色盤,程式內部建立調色盤 #include "stdio.h" #include "Windows.h" unsigned char *pBmpBuf;//讀入影象資料的指標 int bmpWidth;//影象的寬 int bmpHeight;//

YUV420儲存BMP和JPG圖片

       網上大多數關於YUV420的資料都是關於YUV420P的,很少有YUV420SP的,因為YUV420SP的UV是交錯存放的,處理起來相對麻煩點,但是YUV420SP也是一種常見格式,因此,在這裡,我將關於YUV420SP格式資料的處理總結下,方便有需要的同志。

獲取視訊的每一幀,並儲存.jpg圖片

#include<opencv2\opencv.hpp> #include <iostream> #include <stdio.h> #include<fstream> using namespace std; using names

簡單知識點例項之一:如何將各個單一獲取的資料儲存陣列物件並將其取出

一、將獲取的值存為陣列或陣列物件 (1)存為陣列(例如所有怪物的id值可以存為陣列) 是以逗號隔開的,建議用到存單個特定值時用(如光存id值時) <!DOCTYPE html> <html lang="en"> <head> <

爬蟲資料儲存csv檔案時,表格中間隔有空行問題

問題描述:將爬取的資料儲存的csv檔案,遇到幾個問題,原始碼如下: with open('F:\\Pythontest1\\douban.csv','w') as f: writer = csv.writer(f,dialect='excel') writer.writero

nrrd格式用Python讀取並儲存png圖片

安裝pynrrd pip install pynrrd pip install git+https://github.com/mhe/pynrrd.git cd pynrrd pip install . 如果報錯了,則輸入 python setup.py install 注意:

如何將10進位制資料儲存2進位制資料(IMG2LCD的使用,pic2bin)

背景    fpga處理大資料時從txt讀取資料形式為2進位制或者16進位制,&readmemb/&readmemh,所以需要將資料轉換為二進位制或十六進位制存在txt中。 針對影象而言可以參考exe>IMG2LCD 下

Python Pillow (PIL) Image.save 儲存jpg圖片壓縮問題

在使用Pillow中的Image.save()方法,使用預設引數儲存jpg圖片的過程中發現圖片被壓縮的很嚴重,導致原來很大的大小變成幾十K。這是因為在儲存為jpg的過程中,內部使用壓縮演算法對圖片進行的壓縮處理。 但是有些時候往往需要圖片的大小不能變化太大或不能太小。所以在

iOS -FDMB 資料儲存blob後的增刪改查

  2018.10.31 FMDB是iOS平臺下的SQLite資料庫,以OC方式封裝後,更加方便快捷。 沒學資料庫之前,一直糾結用什麼方式能夠持久儲存且追加不覆蓋,直到看到了離線快取SQLite。 FMDB應用在當前的專案中,儲存好友資料,以往都是id主鍵加各種型別作為

獲取當前螢幕影象並儲存bmp檔案

此函式建立了1080p高清圖片,即解析度為:1920 x  1080 如果想建立不同的解析度圖片,可以修改函式中的 nWidth 和 nHeight. 此文僅供參考,如有不妥之處,請多多指教。 void Create1080p() {     int nWidth = 19

VC++擷取螢幕(截圖)並存bmp圖片

  主要程式碼是:   ShowWindow(SW_HIDE);   HDC myDC=::GetDC(0);   int x=GetSystemMetrics(SM_CXSCREEN);   int y=GetSystemMetrics(SM_CYSCREEN);   

VC螢幕截圖,儲存Bmp檔案

新建一個MFC基於對話方塊的應用程式,在介面上放一個Button,為其實現點選事件,程式碼如下: void CScreenShotDlg::OnBtnScreenshot() { RECT rect = {0, 0, 1900, 1000}; HBITMAP hbmp

利用win api 實現截圖 儲存BMP並轉為位元組流

c++ 利用winAPI 實現截圖幕; 程式碼: #include<windows.h> void ScreenSnap(HBITMAP hBitmap,char *bmpPath,HDC dc); int main() { HWND DeskWnd=::

如何將點陣圖物件儲存BMP檔案

  GDI中點陣圖物件是很常見的GDI物件,但是無論是SDK,還是MFC都沒有提供現在的函式或是方法來將一個位圖物件儲存為一個BMP檔案,這裡介紹一下儲存方法。 點陣圖檔案格式: DIB檔案有四個主要部分: 檔案表頭(BITMAPFILEHEADER) 資訊表頭  (BITM