2

This question was asked in the comments of another question, and I think it's a good question, but I wanted to make sure this was addressed in the proper place:

How do I know RGB triple is 3 bytes? Is it a fixed value or variable according to the bmp's type?

2 Answers 2

5

If you take a look in bmp.h, which is included as a header file to copy.c, you'll see a definition for structures named RGBTRIPLE:

typedef struct
{
    BYTE  rgbtBlue;
    BYTE  rgbtGreen;
    BYTE  rgbtRed;
} __attribute__((__packed__))
RGBTRIPLE;

So a triple has 3 variables in it of type BYTE. BYTE is defined in the header of the same bmp.h:

#include <stdint.h>

/**
 * Common Data Types 
 *
 * The data types in this section are essentially aliases for C/C++ 
 * primitive data types.
 *
 * Adapted from http://msdn.microsoft.com/en-us/library/cc230309.aspx.
 * See http://en.wikipedia.org/wiki/Stdint.h for more on stdint.h.
 */
typedef uint8_t  BYTE;

SO: BYTE was explicitly defined as 8 bits of data via stdint.h, which is our standard idea of what a byte is anyway.

The structure called RGBTRIPLE therefore has 3 variables, each using 1 byte of space, for a total of 3 bytes.

We could change the size of an RGBTRIPLE by changing its definition in the bmp.h file, although I suspect we would also need to make some adjustments to our file/info headers.

2

The missing part of Dr.Queso's response is this. A pixel is made of 3 parts - one red, one green and one blue. Each of these is represented by a number that determines it's intensity. The range for each color part of a pixel runs from 0 to 255, with 0 being the absence of that color and 255 being the maximum. Of course, that's in base 10. In hexadecimal, that would be from 0x00 to 0xFF. And since a byte can hold two hexadecimal digits (4 bits each ), each color is represented by one byte and a pixel is represented by 3 bytes. Thus the definition of an RGBTRIPLE as a 3 byte unsigned hexadecimal number - one byte for each of the primary colors.

And yes, it's also an industry standard.

2
  • With reference to the line, "since a byte can hold two hexadecimal digits (4 bytes each)", do you mean 4 *bits each instead of bytes? Jun 25, 2017 at 2:11
  • Good catch. I've corrected the answer. Force of habit is a strong thing! I discuss bytes so often and bits almost never, so it's a bad habit of saying byte when I mean bit. :-/
    – Cliff B
    Jun 25, 2017 at 2:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .