I am trying to convert a 24 bit bitmap to grayscale.
#include<iostream> #include<fstream> #include<conio.h> #include<stdio.h> using namespace std; class pixel{ public: unsigned char b; unsigned char g; unsigned char r; void display() { cout<<r<<" "<<g<<" "<<b<<" "; } }p1; using namespace std; int main(){ unsigned char avg; fstream file("image.bmp",ios::binary|ios::in|ios::out); int start; file.seekg(10); file.read((char*)&start,4); file.seekg(start); int i=0; while(!file.eof()){ cout<<file.tellg();//Remove this and the program doesn't work! file.read((char*)&p1,3); avg=(p1.b+p1.g+p1.r)/3; p1.b=avg; p1.g=avg; p1.r=avg; file.seekg(-3,ios::cur); file.write((char*)&p1,3); } file.close(); getch(); return 0; }
When I delete the cout tellg statement, the loop only works twice!
I don't understand what is the difference when deleting a cout statement?
Result: only one pixel changes to grayscale.
I found a simpler version of my problem here
Reading and writing to files at the same time?
But did not find a solution ...
source share