我的任务是将bmp文件放大
k
次。
如果我先写
BITMAPHEADER
然后写像素(24
位)就可以了。
但反过来不行。
写完我使用的所有像素后:
out.seekp(0, ios_base::beg);
BITMAPHEADER
写得正确,但我看到添加了额外的像素。
为什么会这样?
此代码有效:
//this works correctly
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
#pragma pack(push, 1)
struct BMP {
int8_t id[2];
int32_t filesize;
int32_t reserved;
int32_t headersize;
int32_t infoSize;
int32_t width;
int32_t height;
int16_t biPlanes;
int16_t bits;
int32_t biCompression;
int32_t biSizeImage;
int32_t biXPelsPerMeter;
int32_t biYPelsPerMeter;
int32_t biClrUsed;
int32_t biClrImportant;
};
#pragma pack(pop)
#pragma pack(push, 1)
struct PIXEL {
char B;
char G;
char R;
};
#pragma pack(pop)
int main(){
int k = 100;
BMP bitMap;
BMP cpy;
PIXEL px;
ifstream in; in.open("bmp.bmp", ios_base::binary);
ofstream out; out.open("output.bmp", ios_base::binary );
in.read((char*)&bitMap, sizeof(BMP));
in.seekg( bitMap.headersize, ios_base::beg );
int paddingRead = (4 - bitMap.width * sizeof(PIXEL) % 4) % 4;
int paddingWrite = (4 - bitMap.width * sizeof(PIXEL) * k % 4) % 4;
cpy = bitMap;
cpy.width *= k;
cpy.height *= k;
cpy.filesize = cpy.headersize + (cpy.height * cpy.width * sizeof(PIXEL)) + (cpy.height * paddingWrite);
out.write( (char*)&cpy, sizeof(BMP) );
for (int row = 1; row <= bitMap.height; row++) {
vector<PIXEL> Line;
char str[3] = {};
for (int j = 0; j < bitMap.width; j++) {
in.read( (char*)&px, sizeof(PIXEL) );
Line.push_back(px);
}
in.read( str, paddingRead );
for (int cnt = 1; cnt <= k; cnt++) {//k lines add in this way
for (PIXEL pixel : Line) {
for (int x = 1; x <= k; x++)
out.write((char*)&pixel, sizeof(PIXEL));
}
out.write(str, paddingWrite);
}
}
in.close();
out.close();
return 0;
}
但是这个没有:
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
#pragma pack(push, 1)
struct BMP {
int8_t id[2];
int32_t filesize;
int32_t reserved;
int32_t headersize;
int32_t infoSize;
int32_t width;
int32_t height;
int16_t biPlanes;
int16_t bits;
int32_t biCompression;
int32_t biSizeImage;
int32_t biXPelsPerMeter;
int32_t biYPelsPerMeter;
int32_t biClrUsed;
int32_t biClrImportant;
};
#pragma pack(pop)
#pragma pack(push, 1)
struct PIXEL {
char B;
char G;
char R;
};
#pragma pack(pop)
int main(){
int k = 100;
BMP bitMap;
BMP cpy;
PIXEL px;
ifstream in; in.open("bmp.bmp", ios_base::binary);
ofstream out; out.open("output.bmp", ios_base::binary );
in.read((char*)&bitMap, sizeof(BMP));
in.seekg( bitMap.headersize, ios_base::beg );
int paddingRead = (4 - bitMap.width * sizeof(PIXEL) % 4) % 4;
int paddingWrite = (4 - bitMap.width * sizeof(PIXEL) * k % 4) % 4;
cpy = bitMap;
cpy.width *= k;
cpy.height *= k;
cpy.filesize = cpy.headersize + (cpy.height * cpy.width * sizeof(PIXEL)) + (cpy.height * paddingWrite);
//out.write( (char*)&cpy, sizeof(BMP) );//comment this line
for (int row = 1; row <= bitMap.height; row++) {
vector<PIXEL> Line;
char str[3] = {};
for (int j = 0; j < bitMap.width; j++) {
in.read( (char*)&px, sizeof(PIXEL) );
Line.push_back(px);
}
in.read( str, paddingRead );
for (int cnt = 1; cnt <= k; cnt++) {//k lines add in this way
for (PIXEL pixel : Line) {
for (int x = 1; x <= k; x++)
out.write((char*)&pixel, sizeof(PIXEL));
}
out.write(str, paddingWrite);
}
}
out.seekp(0, ios::beg);//added
out.write( (char*)&cpy, sizeof(BMP) );//added
in.close();
out.close();
return 0;
}