[分享] 無聊寫的BMP檔顏色相反處理
前情提要 : BMP是個圖檔, 而BMP前面有54byte是記錄著裡面的一些屬性而
後面的東西皆為圖片的資料(24位元全彩圖是這樣的格式)
檔頭+屬性介紹共54byte由於我寫的程式是採用24bit全彩圖(未壓縮)
所以後面沒有調色盤的資料, 直接就是圖片內容, 而內容則
RGBRGBRGB....這樣一直排列下去一個R或G或B使用1byte
而互補色(顏色相反公式為 (255-r, 255-g, 255-b)), 所以輸出的檔案為前54byte照抄
後面所有東西到底之前都是 把他轉成數字 在用255去減去他 在輸出即可~
==========以下為CODE==========
//use Dev C++
#include <iostream>
#include <fstream> //存取檔案
#include <string>
using namespace std;
int main(int argc, char** argv) {
cout << "《BMP圖片顏色相反程式》" << endl;
cout << "此程式讀取之檔案限「*.BMP」格式且屬性為";
cout <<「未壓縮」、「24bit全彩」圖檔" << endl;
cout << "程式設計:ymzk" << endl;
while(true) {
string input_filename, output_filename;
cout << "請輸入要開啟的檔案:";
getline(cin, input_filename);
cout << "請輸入要儲存的檔案:";
getline(cin, output_filename);
fstream input, output;
input.open(input_filename.c_str(), ios::in | ios::binary);
output.open(output_filename.c_str(), ios::out | ios::binary);
cout << "處理中..." << endl;
unsigned char temp;
for(int i = 0l i < 54; i++) {
if(input.good()) {
input.read((char*)&temp, 1);
output.write((char*)&temp, 1);
}
else break;
}
while(input.good()) {
input.read((char*)&temp), 1);
temp = (char)(255 - (unsigned int)temp);
output.write((char*)&temp, 1);
}
input.close();
output.close();
cout << "已完成。" << endl;
}
return 0;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 106.1.226.184
※ 文章網址: http://www.ptt.cc/bbs/C_and_CPP/M.1404390426.A.3C9.html
※ 編輯: ymzk (106.1.226.184), 07/03/2014 20:38:36
推
07/03 21:17, , 1F
07/03 21:17, 1F
推
07/03 21:20, , 2F
07/03 21:20, 2F
推
07/03 21:29, , 3F
07/03 21:29, 3F
推
07/03 22:10, , 4F
07/03 22:10, 4F
→
07/03 22:30, , 5F
07/03 22:30, 5F
→
07/03 23:06, , 6F
07/03 23:06, 6F
→
07/04 09:25, , 7F
07/04 09:25, 7F
→
07/04 11:32, , 8F
07/04 11:32, 8F
推
07/04 19:56, , 9F
07/04 19:56, 9F
→
07/04 19:56, , 10F
07/04 19:56, 10F
→
07/04 20:04, , 11F
07/04 20:04, 11F
→
07/04 22:04, , 12F
07/04 22:04, 12F
→
07/04 22:34, , 13F
07/04 22:34, 13F
→
07/04 22:41, , 14F
07/04 22:41, 14F
→
07/04 23:56, , 15F
07/04 23:56, 15F
→
07/05 19:21, , 16F
07/05 19:21, 16F