I'm writing my own bitmap generator in c and I've run into some behavior that I can't figure out.
I'm storing pixel data in a 2D array of a pixel struct I wrote. I write my structs directly to the file after the headers.
typedef struct {
uint8_t blue;
uint8_t green;
uint8_t red;
} pixel_24;
Solid color bitmaps were working correctly but I ran into a problem when trying to produce gradients. The images were coming out corrupt. After some experiments I found that any image containing a pixel with a r, g, or b value of 10 would display corrupt. I altered my code to avoid all 10's in my color channels like this:
void load_pixels(pixel_24 pixels[VSIZE][HSIZE])
{
unsigned int y, x;
for (y = 0; y < VSIZE; y++)
{
for (x = 0; x < HSIZE; x++)
{
uint8_t b = (x+y)/4;
uint8_t g = 255 - (x+y)/4;
uint8_t r = 0;
pixels[y][x] = (pixel_24) {b, g, r};
if (b==10)
{
pixels[y][x].blue = 9;
}
if (g==10)
{
pixels[y][x].green = 9;
}
if (r==10)
{
pixels[y][x].red = 9;
}
}
}
}
This produces a correct gradient:
When I remove the if statements I get:
What do I need to know about bitmaps to avoid problems like this?
RogerRowland got it. I was opening my file in text mode when I needed binary.