This commit is contained in:
parent
df85a8810b
commit
2f0e0d74d1
|
@ -3,7 +3,7 @@
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
typedef unsigned char uch;
|
typedef unsigned char uch;
|
||||||
|
|
||||||
// ™n°¸½Y˜‹
|
// 檔案結構
|
||||||
#pragma pack(2)
|
#pragma pack(2)
|
||||||
struct BmpFileHeader {
|
struct BmpFileHeader {
|
||||||
uint16_t bfTybe;
|
uint16_t bfTybe;
|
||||||
|
@ -35,7 +35,7 @@ void bmpWrite(const char* name, const uch* raw_img,
|
||||||
perror("Error bmpWrite.");
|
perror("Error bmpWrite.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// ™n°¸ÙYÓ<EFBFBD>
|
// 檔案資訊
|
||||||
struct BmpFileHeader file_h = {
|
struct BmpFileHeader file_h = {
|
||||||
.bfTybe=0x4d42,
|
.bfTybe=0x4d42,
|
||||||
.bfReserved1=0,
|
.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;
|
file_h.bfSize = file_h.bfOffBits + width*height * bits/8;
|
||||||
if(bits==8) {file_h.bfSize+= 1024, file_h.bfOffBits+= 1024;}
|
if(bits==8) {file_h.bfSize+= 1024, file_h.bfOffBits+= 1024;}
|
||||||
// ˆDƬÙYÓ<EFBFBD>
|
// 圖片資訊
|
||||||
struct BmpInfoHeader info_h = {
|
struct BmpInfoHeader info_h = {
|
||||||
.biSize=40,
|
.biSize=40,
|
||||||
.biPlanes=1,
|
.biPlanes=1,
|
||||||
|
@ -59,7 +59,7 @@ void bmpWrite(const char* name, const uch* raw_img,
|
||||||
info_h.biBitCount = bits;
|
info_h.biBitCount = bits;
|
||||||
info_h.biSizeImage = width*height * bits/8;
|
info_h.biSizeImage = width*height * bits/8;
|
||||||
if(bits == 8) {info_h.biClrUsed=256;}
|
if(bits == 8) {info_h.biClrUsed=256;}
|
||||||
// Œ‘Èë™nî^
|
// 寫入檔頭
|
||||||
FILE *pFile = NULL;
|
FILE *pFile = NULL;
|
||||||
// pFile = fopen(name,"wb+");
|
// pFile = fopen(name,"wb+");
|
||||||
fopen_s(&pFile, 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*)&file_h, sizeof(char), sizeof(file_h), pFile);
|
||||||
fwrite((char*)&info_h, sizeof(char), sizeof(info_h), pFile);
|
fwrite((char*)&info_h, sizeof(char), sizeof(info_h), pFile);
|
||||||
// Œ‘Õ{É«±P
|
// 寫調色盤
|
||||||
if(bits == 8) {
|
if(bits == 8) {
|
||||||
for(unsigned i = 0; i < 256; ++i) {
|
for(unsigned i = 0; i < 256; ++i) {
|
||||||
uch c = i;
|
uch c = i;
|
||||||
|
@ -79,20 +79,20 @@ void bmpWrite(const char* name, const uch* raw_img,
|
||||||
fwrite("", sizeof(char), sizeof(uch), pFile);
|
fwrite("", sizeof(char), sizeof(uch), pFile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Œ‘ÈëˆDƬÙYÓ<EFBFBD>
|
// 寫入圖片資訊
|
||||||
size_t alig = ((width*bits/8)*3) % 4;
|
size_t alig = ((width*bits/8)*3) % 4;
|
||||||
for(int j = height-1; j >= 0; --j) {
|
for(int j = height-1; j >= 0; --j) {
|
||||||
for(unsigned i = 0; i < width; ++i) {
|
for(unsigned i = 0; i < width; ++i) {
|
||||||
uint32_t idx = j*width +i;
|
uint32_t idx = j*width +i;
|
||||||
if(bits == 24) { // RGBˆDƬ
|
if(bits == 24) { // RGB圖片
|
||||||
fwrite((char*)&raw_img[idx*3 +2], sizeof(char), sizeof(uch), pFile);
|
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 +1], sizeof(char), sizeof(uch), pFile);
|
||||||
fwrite((char*)&raw_img[idx*3 +0], sizeof(char), sizeof(uch), pFile);
|
fwrite((char*)&raw_img[idx*3 +0], sizeof(char), sizeof(uch), pFile);
|
||||||
} else if(bits == 8) { // »ÒëAˆD
|
} else if(bits == 8) { // 灰階圖
|
||||||
fwrite((char*)&raw_img[idx], sizeof(char), sizeof(uch), pFile);
|
fwrite((char*)&raw_img[idx], sizeof(char), sizeof(uch), pFile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Œ¦ýR4byte
|
// 對齊4byte
|
||||||
for(size_t i = 0; i < alig; ++i) {
|
for(size_t i = 0; i < alig; ++i) {
|
||||||
fwrite("", sizeof(char), sizeof(uch), pFile);
|
fwrite("", sizeof(char), sizeof(uch), pFile);
|
||||||
}
|
}
|
||||||
|
@ -106,11 +106,11 @@ void bmpRead(const char* name, uch** raw_img,
|
||||||
perror("Error bmpRead.");
|
perror("Error bmpRead.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// ™n°¸ÙYÓ<EFBFBD>
|
// 檔案資訊
|
||||||
struct BmpFileHeader file_h;
|
struct BmpFileHeader file_h;
|
||||||
// ˆDƬÙYÓ<EFBFBD>
|
// 圖片資訊
|
||||||
struct BmpInfoHeader info_h;
|
struct BmpInfoHeader info_h;
|
||||||
// ×xÈ¡™nî^
|
// 讀取檔頭
|
||||||
FILE *pFile = NULL;
|
FILE *pFile = NULL;
|
||||||
// pFile = fopen(name, "rb+");
|
// pFile = fopen(name, "rb+");
|
||||||
fopen_s(&pFile, 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*)&file_h, sizeof(char), sizeof(file_h), pFile);
|
||||||
fread((char*)&info_h, sizeof(char), sizeof(info_h), pFile);
|
fread((char*)&info_h, sizeof(char), sizeof(info_h), pFile);
|
||||||
// ×xÈ¡éLŒ’
|
// 讀取長寬
|
||||||
*width = info_h.biWidth;
|
*width = info_h.biWidth;
|
||||||
*height = info_h.biHeight;
|
*height = info_h.biHeight;
|
||||||
*bits = info_h.biBitCount;
|
*bits = info_h.biBitCount;
|
||||||
size_t ImgSize = ((size_t)*width) * ((size_t)*height) * 3;
|
size_t ImgSize = ((size_t)*width) * ((size_t)*height) * 3;
|
||||||
*raw_img = (uch*)calloc(ImgSize, sizeof(uch));
|
*raw_img = (uch*)calloc(ImgSize, sizeof(uch));
|
||||||
// ×xÈ¡×xƬÙYÓ<EFBFBD>ÞDRAW™nÙYÓ<EFBFBD>
|
// 讀取讀片資訊轉RAW檔資訊
|
||||||
fseek(pFile, file_h.bfOffBits, SEEK_SET);
|
fseek(pFile, file_h.bfOffBits, SEEK_SET);
|
||||||
size_t alig = ((info_h.biWidth*info_h.biBitCount/8)*3) % 4;
|
size_t alig = ((info_h.biWidth*info_h.biBitCount/8)*3) % 4;
|
||||||
for(int j = *height-1; j >= 0; --j) {
|
for(int j = *height-1; j >= 0; --j) {
|
||||||
for(unsigned i = 0; i < *width; ++i) {
|
for(unsigned i = 0; i < *width; ++i) {
|
||||||
uint32_t idx = j*(*width)+i;
|
uint32_t idx = j*(*width)+i;
|
||||||
if(*bits == 24) { // RGBˆDƬ
|
if(*bits == 24) { // RGB圖片
|
||||||
fread((char*)&(*raw_img)[idx*3 +2], sizeof(char), sizeof(uch), pFile);
|
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 +1], sizeof(char), sizeof(uch), pFile);
|
||||||
fread((char*)&(*raw_img)[idx*3 +0], sizeof(char), sizeof(uch), pFile);
|
fread((char*)&(*raw_img)[idx*3 +0], sizeof(char), sizeof(uch), pFile);
|
||||||
} else if(*bits == 8) { // »ÒëAˆD
|
} else if(*bits == 8) { // 灰階圖
|
||||||
fread((char*)&(*raw_img)[idx], sizeof(char), sizeof(uch), pFile);
|
fread((char*)&(*raw_img)[idx], sizeof(char), sizeof(uch), pFile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -145,7 +145,7 @@ void bmpRead(const char* name, uch** raw_img,
|
||||||
fclose(pFile);
|
fclose(pFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ˆDÏñ½Y˜‹
|
// 圖像結構
|
||||||
typedef struct Imgraw {
|
typedef struct Imgraw {
|
||||||
uint32_t width, height;
|
uint32_t width, height;
|
||||||
uint16_t bits;
|
uint16_t bits;
|
||||||
|
@ -161,11 +161,11 @@ void Imgraw_Writ(const Imgraw* _this, const char* name) {
|
||||||
}
|
}
|
||||||
/*==============================================================*/
|
/*==============================================================*/
|
||||||
int main(int argc, char const *argv[]) {
|
int main(int argc, char const *argv[]) {
|
||||||
// ½¨˜‹
|
// 建構
|
||||||
Imgraw img = {0, 0, 0, NULL};
|
Imgraw img = {0, 0, 0, NULL};
|
||||||
// ×xˆD
|
// 讀圖
|
||||||
Imgraw_Read(&img, "./input/bw2x1.bmp");
|
Imgraw_Read(&img, "./input/bw2x1.bmp");
|
||||||
// Œ‘ˆD
|
// 寫圖
|
||||||
Imgraw_Writ(&img, "./output/bw2x1(output).bmp");
|
Imgraw_Writ(&img, "./output/bw2x1(output).bmp");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue