You have to take into account that bitmaps have rows aligned on
32
bits (i.e four bytes) boundaries (they are padded). The original bitmap, having width
800
doesn't need padding (hence is size is exactly
1646454
bytes) while the rotated image needs it (
686 % 4
is non-zero), that is you need to add
2
padding bytes per each row (hence the file size is
1648054
, that is
800x2
longer than the original one).
The following code rotates the original image, writing the resulting one to the '
rflower.bmp
' file.
#include<iostream>
#include<fstream>
#include <stdint.h>
using namespace std;
int main()
{
const int BFHS = 14; const int BIHS = 40; const int W = 800;
const int H = 686;
ifstream bmp("flower.bmp", ios::binary);
ofstream rbm("rflower.bmp", ios::binary);
char head[BFHS+BIHS];
char rgb[3*W*H];
bmp.read(head, BFHS+BIHS);
uint32_t temp = *(uint32_t*)&head[BFHS+4];
*(uint32_t*)&head[BFHS+4] = *(uint32_t*)&head[BFHS+8];
*(uint32_t*)&head[BFHS+8]= temp;
rbm.write(head, BFHS+BIHS);
bmp.read(rgb, 3*W*H);
char zero[3] = {0};
for (int y=0; y<W; ++y)
{
for (int x=0; x<H; ++x)
{
rbm.write(&rgb[(H - x - 1) * W * 3 + y * 3], 3);
}
rbm.write(zero,2); }
}