From 2f0e0d74d130c0b86ed05bfeca89c10c87ad345e Mon Sep 17 00:00:00 2001 From: SunnyGor Date: Fri, 13 Jan 2023 19:08:13 +0800 Subject: [PATCH] v4 --- bmp_copy_demo.c | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/bmp_copy_demo.c b/bmp_copy_demo.c index e98aefb..badab67 100644 --- a/bmp_copy_demo.c +++ b/bmp_copy_demo.c @@ -3,7 +3,7 @@ #include typedef unsigned char uch; -// 檔案結構 +// 妾旀绲愭 #pragma pack(2) struct BmpFileHeader { uint16_t bfTybe; @@ -35,7 +35,7 @@ void bmpWrite(const char* name, const uch* raw_img, perror("Error bmpWrite."); return; } - // 檔案資訊 + // 妾旀璩囪▕ struct BmpFileHeader file_h = { .bfTybe=0x4d42, .bfReserved1=0, @@ -44,7 +44,7 @@ void bmpWrite(const char* name, const uch* raw_img, }; file_h.bfSize = file_h.bfOffBits + width*height * bits/8; if(bits==8) {file_h.bfSize+= 1024, file_h.bfOffBits+= 1024;} - // 圖片資訊 + // 鍦栫墖璩囪▕ struct BmpInfoHeader info_h = { .biSize=40, .biPlanes=1, @@ -59,7 +59,7 @@ void bmpWrite(const char* name, const uch* raw_img, info_h.biBitCount = bits; info_h.biSizeImage = width*height * bits/8; if(bits == 8) {info_h.biClrUsed=256;} - // 寫入檔頭 + // 瀵叆妾旈牠 FILE *pFile = NULL; // pFile = fopen(name,"wb+"); fopen_s(&pFile, name,"wb+"); @@ -69,7 +69,7 @@ void bmpWrite(const char* name, const uch* raw_img, } fwrite((char*)&file_h, sizeof(char), sizeof(file_h), pFile); fwrite((char*)&info_h, sizeof(char), sizeof(info_h), pFile); - // 寫調色盤 + // 瀵鑹茬洡 if(bits == 8) { for(unsigned i = 0; i < 256; ++i) { uch c = i; @@ -79,20 +79,20 @@ void bmpWrite(const char* name, const uch* raw_img, fwrite("", sizeof(char), sizeof(uch), pFile); } } - // 寫入圖片資訊 + // 瀵叆鍦栫墖璩囪▕ size_t alig = ((width*bits/8)*3) % 4; for(int j = height-1; j >= 0; --j) { for(unsigned i = 0; i < width; ++i) { uint32_t idx = j*width +i; - if(bits == 24) { // RGB圖片 + if(bits == 24) { // RGB鍦栫墖 fwrite((char*)&raw_img[idx*3 +2], sizeof(char), sizeof(uch), pFile); fwrite((char*)&raw_img[idx*3 +1], sizeof(char), sizeof(uch), pFile); fwrite((char*)&raw_img[idx*3 +0], sizeof(char), sizeof(uch), pFile); - } else if(bits == 8) { // 灰階圖 + } else if(bits == 8) { // 鐏伴殠鍦 fwrite((char*)&raw_img[idx], sizeof(char), sizeof(uch), pFile); } } - // 對齊4byte + // 灏嶉綂4byte for(size_t i = 0; i < alig; ++i) { fwrite("", sizeof(char), sizeof(uch), pFile); } @@ -106,11 +106,11 @@ void bmpRead(const char* name, uch** raw_img, perror("Error bmpRead."); return; } - // 檔案資訊 + // 妾旀璩囪▕ struct BmpFileHeader file_h; - // 圖片資訊 + // 鍦栫墖璩囪▕ struct BmpInfoHeader info_h; - // 讀取檔頭 + // 璁鍙栨獢闋 FILE *pFile = NULL; // pFile = fopen(name, "rb+"); fopen_s(&pFile, name, "rb+"); @@ -120,23 +120,23 @@ void bmpRead(const char* name, uch** raw_img, } fread((char*)&file_h, sizeof(char), sizeof(file_h), pFile); fread((char*)&info_h, sizeof(char), sizeof(info_h), pFile); - // 讀取長寬 + // 璁鍙栭暦瀵 *width = info_h.biWidth; *height = info_h.biHeight; *bits = info_h.biBitCount; size_t ImgSize = ((size_t)*width) * ((size_t)*height) * 3; *raw_img = (uch*)calloc(ImgSize, sizeof(uch)); - // 讀取讀片資訊轉RAW檔資訊 + // 璁鍙栬畝鐗囪硣瑷婅綁RAW妾旇硣瑷 fseek(pFile, file_h.bfOffBits, SEEK_SET); size_t alig = ((info_h.biWidth*info_h.biBitCount/8)*3) % 4; for(int j = *height-1; j >= 0; --j) { for(unsigned i = 0; i < *width; ++i) { uint32_t idx = j*(*width)+i; - if(*bits == 24) { // RGB圖片 + if(*bits == 24) { // RGB鍦栫墖 fread((char*)&(*raw_img)[idx*3 +2], sizeof(char), sizeof(uch), pFile); fread((char*)&(*raw_img)[idx*3 +1], sizeof(char), sizeof(uch), pFile); fread((char*)&(*raw_img)[idx*3 +0], sizeof(char), sizeof(uch), pFile); - } else if(*bits == 8) { // 灰階圖 + } else if(*bits == 8) { // 鐏伴殠鍦 fread((char*)&(*raw_img)[idx], sizeof(char), sizeof(uch), pFile); } } @@ -145,7 +145,7 @@ void bmpRead(const char* name, uch** raw_img, fclose(pFile); } -// 圖像結構 +// 鍦栧儚绲愭 typedef struct Imgraw { uint32_t width, height; uint16_t bits; @@ -161,11 +161,11 @@ void Imgraw_Writ(const Imgraw* _this, const char* name) { } /*==============================================================*/ int main(int argc, char const *argv[]) { - // 建構 + // 寤烘 Imgraw img = {0, 0, 0, NULL}; - // 讀圖 + // 璁鍦 Imgraw_Read(&img, "./input/bw2x1.bmp"); - // 寫圖 + // 瀵湒 Imgraw_Writ(&img, "./output/bw2x1(output).bmp"); return 0; }