Thread: TGA file loader...error reading

  1. #1
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972

    TGA file loader...error reading

    I'm working on a .tga file loader class, and I keep running into problems when I try to read the actual image data.

    Everything seems to be fine before that point; I get correct values for image width and height (in this case 256 x 256) and I get 24 bit pixel depth.

    So when I try to read the image data I use two loops, which should loop 256*256 times (altogether), or is there something wrong with that logic?

    I read 3 bytes (24 bits/8 bits/byte) each time, so I should read (256*256)*3 bytes total, correct? That comes out to 196,608 bytes, and the file is 196,652 bytes long.

    As far as I can tell, I am only reading about 18 bytes before I get to the image data, but my loop always fails to read at about 9,600 or so reads (28k bytes). This is how I am allocating and reading the image data:

    Code:
    int BytesToRead = (header.ImgSpec.PixDepth)/8;
    
    MapData.pixels = new int *[header.ImgSpec.ImgWidth];
    for (int i=0;i<header.ImgSpec.ImgWidth+1;i++)
     MapData.pixels[i] = new int[header.ImgSpec.ImgHeight];
    
    for (int i = 0;i<header.ImgSpec.ImgWidth;i++)
        {
         for (int x=0;x<header.ImgSpec.ImgHeight;x++)
          {
           if ( _read( fl, &TempMapData,BytesToRead)<1)//read successful?
             throw TgaErr(2); //read was not successful!  
              MapData.pixels[i][x] = TempMapData;
          TempMapData = 0;
          }
        }
    Now as I look at it I realize I might not be checking correctly to see if the read fails, but I'm fairly sure that will work...It ends up throwing an exception every time, which the function which called this one catches, and returns an error...

    I've worked through this in console mode using cout statements at each read to see what they turn up, and theres about 10 before this one, but none of them go above 2 bytes as far as i know.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Check out www.wotsit.org for all your file format needs.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Thats where I got my information from in the first place...I wouldn't even know where to start reading in the file to get the image data if I hadn't looked at wotsit.org first...I was hoping someone would have an idea where I went wrong if all the information I'm reading before the image data (i.e. pixel depth, image height and width) is correct, and image data should come directly after that information in this case, since there is no color map to read beforehand...So does anyone see problems with my logic, or should I post more of the code?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    well, for one

    for (int i=0;i<header.ImgSpec.ImgWidth+1;i++)

    why the +1, thats will cause you to overwrite the arrays bounds, this could be part of the problem, and since c indexing starts at 0 you dont need that.

    second, your reading the pixels as 'int's is this by design?

    third, are you testing this code as a standalone? or as part of a bigger project?

    also your reading it sorta sideways a tga is usually written in horizontal lines, your trying to read it vertically, this won't cause any errors till you try to display it, but it might be a bit confusing...

    nothing else is immediatly obvious.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Yeah I realized the loops might not be running the correct amount of times, but it shouldn't cause the problem that I have...I didn't really think about reading it sideways or whatever, but that also shouldn't cause a problem.

    second, your reading the pixels as 'int's is this by design?
    Should that matter? I mean maybe it uses too much space or something but at this point I'm not worried about that, and the most information a pixel can contain is 4 bytes (if you are in 32 bit mode) which is the size of an int, unless I'm mistaken?

    I'm really confused about this...as far as I can tell, even if I'm reading 4 times as much as I need to (which I'm fairly sure I'm not) I still wouldn't reach the end of the file, which is what _read() tells me (it returns 0)

    And yes this is a standalone project
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    >Yeah I realized the loops might not be running the correct amount of times, but it shouldn't cause the problem that I have...

    well actually it might, since you might over write some existing data, trust me this can cause serious errors later in the program... and they are very hard to find.

    >Should that matter?

    not at all i was just curious, as to be sure this was your intention.

    >which is what _read() tells me (it returns 0)
    are you reading in text mode?? this would be very likely to cause a false EOF.

    >And yes this is a standalone project.

    what i mean is, is this the only code (the tga reader and the driver) running?
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  7. #7
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    are you reading in text mode?? this would be very likely to cause a false EOF.
    Hm that seems like it could be the problem, this is how I open the file:
    Code:
    int fl = _open( file, _O_RDONLY );
    I'll have to look that up (I can't believe I never even gave that a second thought...).
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  8. #8
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    OR in a _O_BINARY and see if that fixes it, MS's _open defaults to text mode.

    anyway, i only though of it because i made the mistake twice when i wrote a BMP loader and also when i wrote a TGA loader...
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  9. #9
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Yes that fixed the error Now I have to figure out how to display it correctly lol...Right now I get a bunch of vertical lines with different colored pixels, and every other line has only blue pixels...Well one thing at a time, right?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  10. #10
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Now I'm really getting somewhere...What I want to display now is a black image with a green box in the upper left...after changing the origin to top-left instead of bottom-left, it seems to be fine, except the colors are wrong...is it possible the colors are in BGR instead of RGB format, or do you think its another problem?

    If so how should I go about changing it? Well heres screenshots of what i get/what i want:

    What I want
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  11. #11
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    What I get:
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  12. #12
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    well, i doubt its BGR since the spec says it should be other wise, a quick way to check is to open the files up in an image editor, and check and see if the r and b values are reversed...

    what are you using to display it, or are you just dumping it to a new tga file?
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  13. #13
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Heh I posted that way too hastily...I had just copied code I used earlier, and tried to change it to display what I wanted...After working for much too long on it, I realized I was in 16 bit mode, but still that wasn't the problem, since for some reason I can't even run 24 bit mode (maybe my card doesn't support it). So I'm rewriting my test file, but I don't think that will solve the problem.

    That sort of greenish color (although 16 bit color) comes from:

    Red : 9
    Green : 11
    Blue : 10

    Which, unless I'm mistaken should be 0 for all color modes...I haven't a clue why the values come out like that...I'll post back later when I finish rewriting my test file.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  14. #14
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Ok I got the colors working now, but I think the way I'm addressing the pixels is wrong...I noticed that in my test program if i use a loop like this:
    Code:
    for (int y=0;y<SCREEN_HEIGHT;y++)
     for (int x=0;x<SCREEN_WIDTH;x++)
          //plot pixel
    It only draws half the screen (the upper half, to be exact). Oh I'm using DirectDraw, and I set the display mode like this:

    Code:
    if (FAILED(lpdd->SetDisplayMode(SCREEN_WIDTH,SCREEN_HEIGHT,32,0,0)))
    {
    MessageBox(NULL,"SetDisplayMode() Failed!","ERROR",MB_OK|MB_ICONEXCLAMATION);
    return 0;
    }
    But I don't get an error, so it seems that its setting the display mode correctly.

    Ok, boiling it down this is my pixel-plotting function:
    Code:
    void Plot_Pixel32(int x,int y,int r,int g,int b,USHORT* video_buffer,int lpitch32) 
    {
    UINT pixel = __RGB32BIT(0,r,g,b); //ignoring alpha bits, since image is actually 24 bit
    video_buffer[x+y*lpitch32]=pixel; //where lpitch23 is lpitch>>2
    }
    Anyone have any idea whats going on?

    EDIT:

    I changed it to video_buffer[((x+(y*lpitch32)*2)]=pixel
    and that seems to work, but now I noticed that the colors aren't correct, just very close...black is black, but white is a bluish color...if i have R=255 G=255 B=255 it comes out bluish, so its not just my image...
    Code:
    #define  __RGB32BIT(a,r,g,b) ((b) + ((g) << 8) + ((r) << 16) + ((a) << 24))
    Thats the macro I'm using to put all the color data together, and I'm actually using macros to separate R G and B out of the pixel data I read from the image, so its easy to switch back and forth from hard-coded colors (i.e. 255,255,255) and my image data. Like I said, it seems to make no difference whether i use my image data or equivalent numbers. Should it matter that I have 0 as my alpha bits?

    EDIT2:

    No matter what I do I can't get any red in my program! At least I know its not my image loader, but I can't figure out why it wont display red!!
    Last edited by JaWiB; 10-13-2003 at 08:59 PM.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  15. #15
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    #define __RGB32BIT(a,r,g,b) ((b) + ((g) << 8) + ((r) << 16) + ((a) << 24))

    I think your color component ordering is off your getting ARGB i think you want RGBA
    this may explain getting no red since the alpha is always 0 which is probably where you want the red to be.

    Code:
    // this is a more commonly used form of such a macro
    // other than the component ordering it works the same
    #define RGB32BIT(r,g,b,a)  ( (r)<<24 | (g)<<16 | (b)<<8 | (a) )
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. multiple file loading. so fruturated! help!
    By psychopath in forum Game Programming
    Replies: 5
    Last Post: 05-09-2005, 05:13 PM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  4. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  5. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM