Problem reading from bitmap

This is a discussion on Problem reading from bitmap within the C++ Programming forums, part of the General Programming Boards category; I'm making a prog that converts a bitmap to a kind of binary stencil. The data that gets written is ...

  1. #1
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218

    Problem reading from bitmap

    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".
    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;
    }/**/
    Cheers.
    Last edited by mike_g; 02-03-2008 at 01:22 PM.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Hi mike, testing "bytes" will always be true since bytes is an array and evaluates to its address.
    Maybe something like this. It should do for testing for zero.
    Code:
    unsigned pix = 0; // ensure all 4 (or more!) bytes start as zero
    in.read( (char*)&pix, 3 );
    if (pix)
     ...

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Yep, that looks like it fixed it. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 32 bit color depth bitmap problem with XP
    By kalabala in forum C++ Programming
    Replies: 0
    Last Post: 12-22-2008, 05:56 AM
  2. Problem reading input
    By gp364481 in forum C Programming
    Replies: 3
    Last Post: 09-24-2008, 05:10 PM
  3. Problem Reading Process Memory
    By carrotcake1029 in forum C Programming
    Replies: 12
    Last Post: 04-14-2008, 10:33 AM
  4. Problem in reading Character
    By Bargi in forum C Programming
    Replies: 5
    Last Post: 04-10-2008, 11:40 PM
  5. Problem With My Bitmap
    By Sebastiani in forum Windows Programming
    Replies: 9
    Last Post: 10-29-2001, 08:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21