Thread: Bmp read out only red value

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    44

    Bmp read out only red value

    People,

    I have a little problem and I don't seem to get. I've seen examples that worked this way but mine won't.

    Code:
    typedef struct
    {
        BYTE  rgbtBlue;
        BYTE  rgbtGreen;
        BYTE  rgbtRed;
    } __attribute__((__packed__))
    RGBTRIPLE;
    
        // iterate over infile's scanlines
        for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
        {
            // iterate over pixels in scanline
            for (int j = 0; j < bi.biWidth; j++)
            {
                // temporary storage
                RGBTRIPLE triple;
    
                // read RGB triple from infile
                fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
                
                printf("%x\n",triple.rgbtRed);
    
                // write RGB triple to outfile
                fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }
    With this it seem to compile fine and seems to get only the red value.

    But if I want to change printf("%x\n",triple.rgbtRed); to something like this :
    Code:
                if (triple.rgbtRed != 0x00)
                    triple.rgbtred = 0x00;
    I'll get a compile error :

    error: ‘RGBTRIPLE’ has no member named ‘rgbtred’

    If searched on google for the error message but the results I got were way to complicated.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > error: ‘RGBTRIPLE’ has no member named ‘rgbtred’
    Well the Red in your struct has a capital-R
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    44
    I'm a shamed, because I had lots of errors and didnt know when to use -> or when to use . So I just was using some trial and error. But It seem it had nothing to do with that

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    BMP file format - Wikipedia, the free encyclopedia
    Unless your width is always a multiple of 4, there is padding at the end of each scan line, which your code fails to take into account.

    Plus you don't show how you open either file, or how you determine the width/height, or how you "seeked" to the start of the pixel data.

    Nor do you check fread/fwrite for errors.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    44
    You're right, I didn't post that code because I'm sure that there wasnt a problem. All the things you mentioned are all there.
    Thx for the quick reply, that wiki page I have also used a lot and is sure an excellent piece of info.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read a number with scanf() and read char[] with fgets()
    By nutzu2010 in forum C Programming
    Replies: 5
    Last Post: 03-11-2011, 05:05 AM
  2. blocked on read/recv / how to read/send proper buffers
    By fudgecode in forum Networking/Device Communication
    Replies: 1
    Last Post: 11-02-2010, 11:42 PM
  3. scanf to read int but read in char?
    By maybabier in forum C Programming
    Replies: 3
    Last Post: 04-11-2009, 12:58 AM
  4. How can I know the actual bytes read in a file read
    By pliang in forum C++ Programming
    Replies: 1
    Last Post: 06-08-2005, 04:23 PM