(原創) 如何使用ISO C++讀寫BMP圖檔? (C/C++) (Image Processing)
阿新 • • 發佈:2018-12-11
{
47 constint headerSize =54;
48
49 char header[headerSize] ={
50 0x42, 0x4d, 0, 0, 0, 0, 0, 0, 0, 0,
51 54, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 24, 0,
52 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
53 0, 0, 0, 0 54 };
55
56 int ysize = imageVec.size();
57 int xsize = imageVec[0].size();
58
59 long file_size = (long)ysize * xsize *3+54;
60 header[2] = (unsigned char)(file_size &0x000000ff);
61 header[3] = (file_size >>8) &0x000000ff;
62 header[4] = (file_size >>16) &0x000000ff;
63 header[5] = (file_size >>24) &0x000000ff;
64
65 long width = xsize;
66 header[18] = width &0x000000ff;
67 header[19] = (width >>8) &0x000000ff;
68 header[20] = (width >>16) &0x000000ff;
69 header[21] = (width >>24) &0x000000ff;
70
71 long height = ysize;
72 header[22] = height &0x000000ff;
73 header[23] = (height >>8) &0x000000ff;
74 header[24] = (height >>16 ) &0x000000ff;
75 header[25] = (height >>24) &0x000000ff;
76
77 ofstream file(fileName,ios::out| ios::binary);
78 if (!file)
79 returnfalse;
80
81 // write header 82 file.write(header, headerSize);
83 // write body 84for(size_t y =0; y != imageVec.size(); ++y) {
85 for(size_t x =0; x != imageVec[0].size(); ++x) {
86 char chB = imageVec[y][x].B;
87 char chG = imageVec[y][x].G;
88 char chR = imageVec[y][x].R;
89
90 file.put(chB).put(chG).put(chR);
91 } 92 } 93
94 file.close();
95
96 returntrue;
97}
47 constint headerSize =54;
48
49 char header[headerSize] ={
50 0x42, 0x4d, 0, 0, 0, 0, 0, 0, 0, 0,
51 54, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 24, 0,
52 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
53 0, 0, 0, 0 54 };
55
56 int ysize = imageVec.size();
57 int xsize = imageVec[0].size();
59 long file_size = (long)ysize * xsize *3+54;
60 header[2] = (unsigned char)(file_size &0x000000ff);
61 header[3] = (file_size >>8) &0x000000ff;
62 header[4] = (file_size >>16) &0x000000ff;
63 header[5] = (file_size >>24) &0x000000ff;
64
65 long width = xsize;
66 header[18]
67 header[19] = (width >>8) &0x000000ff;
68 header[20] = (width >>16) &0x000000ff;
69 header[21] = (width >>24) &0x000000ff;
70
71 long height = ysize;
72 header[22] = height &0x000000ff;
73 header[23] = (height >>8) &0x000000ff;
74 header[24] = (height >>16
75 header[25] = (height >>24) &0x000000ff;
76
77 ofstream file(fileName,ios::out| ios::binary);
78 if (!file)
79 returnfalse;
80
81 // write header 82 file.write(header, headerSize);
83 // write body 84for(size_t y =0; y != imageVec.size(); ++y) {
85 for(size_t x =0; x != imageVec[0].size(); ++x) {
86 char chB = imageVec[y][x].B;
87 char chG = imageVec[y][x].G;
88 char chR = imageVec[y][x].R;
89
90 file.put(chB).put(chG).put(chR);
91 } 92 } 93
94 file.close();
95
96 returntrue;
97}