What you could do is something like this. It just makes a straight copy of the input file to the output with no changes.
using namespace std;
ifstream fin("abc.bmp", ios::binary);
ofstream fout("imgInvert.bmp", ios::binary);
while ( fin.get(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
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: