How to read an 8 bit Raw Image

This is a discussion on How to read an 8 bit Raw Image within the C++ Programming forums, part of the General Programming Boards category; What I would like to do is read a raw Image in decimal (8 bits, so value 0 - 255) ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    37

    How to read an 8 bit Raw Image

    What I would like to do is read
    a raw Image in decimal (8 bits, so value 0 - 255)

    The code below is something I have started. When I tested it prints out numbers, letters, and ! ? etc.
    I understand I am using char, but I should be getting just numbers.
    If I do int it prints -858993460 sixteen times.

    Am I missing something or am I using the wrong file opener or is there something else I should be using?

    Code:
    char data[1024];
    
    ifstream inStream;
    
        inStream.open("test.raw");
        if(inStream.fail())
        {
            cout << "Input file opening failed.\n";
            exit(1);
        }
    
    
        while(!inStream.eof())
        {
            inStream >> data[i];
            cout << data[i] << endl;
            i++;
            while(i == 16);
        }

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Where is "i" even declared, let alone initialized? When you have a variable with no initialization, it's undefined what its starting value will be, depending on what was in that block of memory before. Also, unless it's a volatile variable, line 18 is either useless (if i != 16) or an infinite loop (if i == 16).

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    37
    This does not show all the code I have in main.

    Assume i has been initialize
    that while loop i == 16 is for testing.
    I want to see what is my first 16 values so the terminal does not exist.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,023
    Use unsigned char data[1024];

    As for the rest, try to create a complete example for us that compiles. In trying to do this you will likely find your error.
    Last edited by King Mir; 07-13-2012 at 09:08 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    37
    King Mir,

    I did your suggestions to do unsigned char data and I still got the same results

    It outputs something like this:
    ‘70X‰Z=!FL[’™kJ’¶

    Here is the somewhat the code. To be honest all I was missing is the int main, return, close file, and int i =0
    Code:
    int main(){
    
    int i = 0;
    unsigned char data[1024];
    
    
    ifstream inStream;
    
    
        inStream.open("test.raw");
        if(inStream.fail())
        {
            cout << "Input file opening failed.\n";
            exit(1);
        }
    
    
    
    
        while(!inStream.eof())
        {
            inStream >> data[i];
            cout << data[i] << endl;
            i++;
            while(i == 16);
        }
    
    inStream.close();
    return 0;
    }

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,023
    static_cast data[i] to int or unsigned int when passing it to cout <<. This is to stop cout from thinking you want to print the glyph, and instead print the integer.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    37
    King Mir,

    Do you mean first use unsigned char data[1024];
    then int a = data[1024]?

    I ask this because it gives me the range 0-255 (so far I haven't seen a negative number or higher than 255), but when I print this the values are:
    145, 55, 48, 88, 137.....

    The problem with is that the value should be similar because it is an image. May be like 145 144 145 145 etc etc using the Huffman Code.

    Code:
    int main(){ 
    int i = 0;
    unsigned char data[1024];
     
     
    ifstream inStream;
     
     
        inStream.open("test.raw");
        if(inStream.fail())
        {
            cout << "Input file opening failed.\n";
            exit(1);
        }
     
        while(!inStream.eof())
        {
            inStream >> data[i];
            int a = data[i];
            cout << a << endl;
            i++;
            while(i == 16);
        }
     
    inStream.close();
    return 0;
    }

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,023
    I actually meant using the static_cast operator, but that works just as well.

    If you're still getting the wrong values, I think your remaining error is elsewhere. Or maybe the values aren't actually wrong.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read image to 2d matrix???
    By sukuku in forum C++ Programming
    Replies: 3
    Last Post: 10-10-2011, 06:06 AM
  2. Replies: 13
    Last Post: 11-20-2009, 03:43 PM
  3. How to read out the RGB values of an image?
    By pjeremy in forum C Programming
    Replies: 5
    Last Post: 05-28-2006, 08:49 AM
  4. Read Image to a Matrix
    By scrapedbr in forum C Programming
    Replies: 3
    Last Post: 05-02-2003, 03:53 PM
  5. How to read a image in C++?
    By DramaKing in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2001, 12:34 AM

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