[分享] 無聊寫的BMP檔顏色相反處理

看板C_and_CPP作者 (127.0.0.1)時間10年前 (2014/07/03 20:27), 10年前編輯推噓5(5011)
留言16則, 11人參與, 最新討論串1/1
前情提要 : 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
耶..其實你漏了蠻多考量的,不過作最基礎教材是還...ok 吧
07/03 21:17, 1F

07/03 21:20, , 2F
像是 each row alignment to 4's multiple 就沒考量.
07/03 21:20, 2F

07/03 21:29, , 3F
cout 中文也挺酷的
07/03 21:29, 3F

07/03 22:10, , 4F
BMP是BGR吧
07/03 22:10, 4F

07/03 22:30, , 5F
Bitwise NOT 比較好用
07/03 22:30, 5F

07/03 23:06, , 6F
沒錯bitwise not卡實在
07/03 23:06, 6F

07/04 09:25, , 7F
一個byte一個byte處理為什麼要考慮alignment?
07/04 09:25, 7F

07/04 11:32, , 8F
@KevinR 是吶,這樣連 offset 都處理掉了..
07/04 11:32, 8F

07/04 19:56, , 9F
for(int i = 0l i < 54; i++)
07/04 19:56, 9F

07/04 19:56, , 10F
另外 何不考慮openCV
07/04 19:56, 10F

07/04 20:04, , 11F
現在影像處理作業可以用openCV囉?
07/04 20:04, 11F

07/04 22:04, , 12F
建議不要習慣數字開頭加 0,上次有人就這麼做,結果…… 
07/04 22:04, 12F

07/04 22:34, , 13F
變八進位
07/04 22:34, 13F

07/04 22:41, , 14F
00000000000000O0000000O000000000000O000000O000OOOOOOOO
07/04 22:41, 14F

07/04 23:56, , 15F
他應該是把分號打成小寫L而已 ...XD
07/04 23:56, 15F

07/05 19:21, , 16F
單單寫這個來講OpenCV不會比較快 :D
07/05 19:21, 16F
文章代碼(AID): #1JjKmQF9 (C_and_CPP)