乙個tif轉bmp的示例

2021-10-22 07:27:00 字數 2379 閱讀 8228

本文受了 的啟發。

bmp的格式是rgbrgb.....排列下來的,每個畫素的三個分量靠在一起,描述完乙個畫素,接著描述下乙個畫素。

tif的格式更多樣。通常的格式也是bgrbgr.....(次序與bmp相反)排列下來的,但偶爾有一些tif檔案採用rrrrrr.......ggggg.........bbbbb.....的安排。還有的tif檔案內容是壓縮過的。。。。這裡我們只考慮三個顏色分量靠在一起的情況。

下面的例子是乙個tif檔案,寬896,高608,三個分量。這裡先把其內容取出,並交換rb的位置,然後儲存為.rgb檔案。再給這個rgb檔案加上頭,形成bmp。

#include#include#includeconst int cnst_iwidth = 896;//圖寬

const int cnst_iheight = 608;

const int cnst_ichan = 3;//rgb三通道

#pragma pack(2)

struct bmpheader

};//檔案頭

struct bmpinfo

};//資訊頭

int rgb2bmp(bmpheader*, bmpinfo*);

using namespace std;

const char*pathin = "earthrgbrgb.tif";

const char*pathout = "earth.rgb";

unsigned char*buffer = new unsigned char[12];

int main()

int offset = 0;//檔案目錄的偏移量

offset = (buffer[7] << 24) + (buffer[6] << 16) + (buffer[5] << 8) + buffer[4];

in.seekg(offset);

in.read((char*)buffer, 2);

unsigned int package_num = buffer[0] + (buffer[1] << 8);

class package

;//定義了目錄項

package*a= new package[package_num];

head.type = 0x4d42;

head.size = cnst_iwidth * cnst_iheight * cnst_ichan + 54;

head.offbit = 54;

info.size = 40;

for (int i = 0; i < package_num; i++)

for (int j = 0; j < 2; j++)

for (int j = 0; j < 4; j++)

if (a[i].tag_id[0]== 17&&a[i].tag_id[1]==01)

if (a[i].tag_id[0] ==23&&a[i].tag_id[1]==01)

unsigned int length = (a[i].data[3] << 24) + (a[i].data[2] << 16) + (a[i].data[1] << 8) + a[i].data[0];

unsigned char*data = new unsigned char[length];

in.seekg(off_img);

in.read((char*)data, length);

for (int i = 0; i < length; i = i + 3)

out.write((char*)data, length);

out.close();

in.seekg((i+1)*12+2+offset, ios::beg);

} }in.close();

rgb2bmp(&head, &info);

system("pause");

return 0;

}const char*pathbmp = "earthrgbrgb.bmp";

unsigned char*bufferrgb = new unsigned char[cnst_iwidth * cnst_iheight * cnst_ichan ];

int rgb2bmp(bmpheader*a,bmpinfo*b)

原tif檔案

新的bmp檔案:

兩圖上下翻轉,這是因為bmp把最底部一行的畫素放在檔案的前面,而最頂部的一行放在檔案後面。

乙個yuv轉bmp的程式

在黑客中國上找到的,執行除錯後,可以使用,如下 include stdafx.h include include include include include define byte unsigned char define bool bool define dword unsigned long...

xml的乙個示例

function.h cstring openfile cstring cstring getcontext cstring,int,int void str2array cstring struct xmlnode function.cpp include include include incl...

Flask Vue的乙個示例

這裡記錄下乙個flask vue的乙個示例 1.系統環境 python3.7 sqlite3資料庫,資料庫名稱 books.db 2.專案結構 title 測試 api books defbooks conn sqlite3.connect books.db conn.row factory sql...