CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2010
    Posts
    7

    can't get 10 simple lines to work right

    //simplified example to get RGBs out of a BMP file.

    unsigned int padding = 4 - ((numCols * 3) % 4);//bmps can have a few bytes of padding at end of each row
    if (padding == 4) padding = 0;

    ifstream fin;
    fin.open(filename.c_str());
    fin.ignore(54); //skip header

    char* imageptr;
    imageptr = new char[numRows * (numCols*3 + padding)]; //file size - 54

    fin.read(imageptr, (numRows * (numCols*3+padding)));

    for (int i = 0; i < numRows*(numCols*3+padding); ++i) cout << (int) imageptr[i] << " ";



    This is a simplified version of my code to demonstrate the problem (I'm using unsigned chars of course and skipping over the padding at the end of the rows). so basically the cout at the end should be spamming me with all different sorts of values between -127 and 127, but instead I'm getting only -51, with a few groupings of 0's here and there. How could this be?

    So basically what happens when I read in information like this from an image, and use it write another bmp file, I get a picture with the exact same dimensions as the original, only its all grey with a few black specks here and there. (I think -51 is 205 or something unsigned). I don't understand how I can read a file and get the same character values over and over when they should be changing dramatically.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: can't get 10 simple lines to work right

    Why are your files opened in text mode? If you're doing all of this file manipulation down to the byte level, the file(s) need to be opened as binary files.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    May 2010
    Posts
    7

    Re: can't get 10 simple lines to work right

    tried it with

    fin.open(filename.c_str(), fstream::binary);

    earlier... same result

  4. #4
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: can't get 10 simple lines to work right

    Did you try reading your file in hex mode to see just what you should be seing?

    AFAIK, a lot of bitmap files per-pixel pad to 4 bytes (rather than per row). You may want to double check your file using an external hex editor.

    You could also try something simpler: Forget everything about bitmaps, and imagine your file is just plain raw data. Try to read that and print the values. Your success (or failure) of achieving this should give you a good idea of where you went wrong.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  5. #5
    Join Date
    May 2010
    Posts
    7

    Re: can't get 10 simple lines to work right

    Ummm... dont get mad but...

    I found the error... the bitmap I was using to test somehow got corrupted and didn't think to check that. I probably overwrote it a long time ago while testing and wasted a whole day of programming lol

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured