What you could do is something like this. It just makes a straight copy of the input file to the output with no changes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream fin("abc.bmp", ios::binary);
ofstream fout("imgInvert.bmp", ios::binary);
char byte;
while ( fin.get(byte) )
{
fout.put(byte);
}
}
|
If we make lots of assumptions about what sort of format the bmp file has, then we can build on that.
1. use a loop to copy the first 54 bytes from input to output - as above, but use a count to limit it to just 54 iterations.
2. use another loop, almost identical to the loop in my example. The only change needed is to invert the value of byte before the
fout.put()
What does invert mean? I assume it means subtract byte from 255 to get the new value.
Using this approach, there is no need for any of the complex processing your code was doing.
I did write another version (not shown here) where I used seekg() to move to offset 10 in the input, read a 4-byte unsigned integer, then reset the position to the start using seekg. That meant instead of assuming the pixel data was at offset 54, I used the actual value read from the file.
To understand the bmp format, I referred to this simplified page:
http://www.dragonwins.com/domains/getteched/bmp/bmpfileformat.htm