I'm making a prog that converts a bitmap to a kind of binary stencil.
The data that gets written is the image width, followed by a bool signaling if the first pixel is on or not. The data after that is written as a sequence of short values each saying how many pixels to draw or skip.
My problem is that only 3 bytes are being written to the file and the on-off states never seem to flip. I added some debug output and it displays the first pixel on my bmp to have a value of 2293488 when it is black and should be 0.
The problem should be in the highlighted area. Also to test it there needs to be a 24bit bitmap image in the same directory called "test.bmp".
Cheers.Code:#include <iostream> #include <fstream> using namespace::std; bool Convert(const char*, const char*); int main() { if(! Convert("test.bmp", "result.dat")) cout << "Error"; else cout << "Success"; cin.ignore(); } // Converts 24bit bmp data to compressed format bool Convert(const char* bmpFile, const char* outFile) { short w, h; char bytes[3]; bool state; int count =0; //Set up streams ifstream in; ofstream out; in.open(bmpFile, ios::binary); if(! in.is_open()) return false; out.open(outFile, ios::out); //Write the image width in.seekg(18, ios::beg); in.read((char *)&w, sizeof w); in.seekg(22, ios::beg); out.write(bytes, 2); in.read((char *)&h, sizeof h); cout << "Width: " << w << endl; cout << "Height: " << h << endl; //Write if first pixel is on or not in.seekg(54,ios::beg); in.read(bytes, 3); if(bytes) state = true; else state = false; out.write((char*)&state, 1); cout << "Pix 1: " << ((int)bytes & 0x00FFFFFF) << endl; cout << "State: " << state << endl; in.seekg(54, ios::beg); for(int y=h; y>0; y--) { for(int x=0; x<w; x++) { in.read((char*)&bytes, 3); if(state) { if(bytes) { count++; } else { out.write((char*)&count, 2); cout << "On: " << count << endl; count = 1; state = false; } } else { if(!bytes) { count++; } else { out.write((char*)&count, 2); cout << "Off: " << count << endl; count = 1; state = true; } } } } cout << "Counter at: " << count << endl; in.close(); out.close(); return true; }/**/



LinkBack URL
About LinkBacks


