我试图弄清楚如何在通过c ++生成的硬编码位图文件中绘制直线垂直线:
#include <fstream>
using std::ifstream;
using std::ofstream;
#include <iostream>
using std::cout;
using std::endl;
int main ()
{
ifstream infile ("white8x8.bmp");
ofstream outfile ("output.bmp");
char c;
cout << "Start of original read/write: " << endl;
for (int i = 0; i <= 53; i++)
{
infile.read (&c, 1);
cout << (int) c << ' ' << c;
outfile.write (&c, 1);
}
char z = 0;
char x = 0;
int j_prev = 0;
for (int i = 0; i <= 250; i++){
for (int j = 0; j <= 250; j++)
{
if(j == 10){
c = 0;
z = 0;
x = 0;
outfile.write (&c, 1);
outfile.write (&x, 1);
outfile.write (&z, 1);
j_prev = j;
}
/*if(j %250 == 0){
c = 0;
z = 0;
x = 0;
outfile.write (&c, 1);
outfile.write (&x, 1);
outfile.write (&z, 1);
}*/
else{
c = 255;
x = 255;
z = 255;
outfile.write (&c, 1);
outfile.write (&x, 1);
outfile.write (&z, 1);
}
}
}
cout << endl << "Start of read new file: " << endl;
infile.close();
outfile.close();
ifstream out2 ("output.bmp");
out2.seekg(53);
int count = 0;
for(int i = 53; i < 15000; i++){
out2.read(&c, 1);
cout << count << ":" << (int) c << ' ' << c;
count++;
}
out2.close();
return 0;
}
我认为您可以将像素阵列视为二维数组,并且获得水平线只需要在每次j点击特定数字时绘制像素。这似乎并非如此,因为这样做会给我一个如下所示的偏斜线。
为了澄清我只是从已经创建的位图复制位图标题信息,然后简单地创建一个伴随的像素数组并从那里修改它。
答案 0 :(得分:3)
每行需要将BMP文件填充为4个字节的倍数。你的行是250 * 3 = 750,不是是4的倍数;在每行的开头吞下2个字节进行填充,使其偏移。只需在j
循环结束时再写2个字节的零。