i am doing gui stuff for my os these days and i got puzzled in bitmap file loading. I have read a lot articles about bmp file format. many people say that the format of rgb stored in bmp file is bgr(24 bpp), and when you load bmp file into memory, you should convert bgr to rgb order if you want to display it on screen.
but in the real world, everything goes weird...
take a look at my converting function:
only take care of 24bpp.
buf conatins the actual bytes of pixel of a bmp file, not include bmp file header and info header.
Code: Select all
void bmp_to_rgb_buffer(char *buf, bmp_t *bmp)
{
int padding;
int bpl;
int psw;
u32 bufpos;
u32 newpos;
int x, y;
u8 *newbuf = bmp->data;
padding = 0;
bpl = bmp->width * 3;
while ((bpl + padding) % 4 != 0)
padding++;
psw = bpl + padding;
for (y=0; y<bmp->height; y++) {
for (x=0; x<bpl; x+=3) {
newpos = (y*bmp->width *3) + x;
bufpos = (bmp->height-y-1)*psw + x;
//convert bgr to rgb order.
newbuf[newpos] = buf[bufpos+2];
newbuf[newpos+1] = buf[bufpos+1];
newbuf[newpos+2] = buf[bufpos];
}
}
}
Code: Select all
void bmp_to_rgb_buffer(char *buf, bmp_t *bmp)
{
int padding;
int bpl;
int psw;
u32 bufpos;
u32 newpos;
int x, y;
u8 *newbuf = bmp->data;
padding = 0;
bpl = bmp->width * 3;
while ((bpl + padding) % 4 != 0)
padding++;
psw = bpl + padding;
for (y=0; y<bmp->height; y++) {
for (x=0; x<bpl; x+=3) {
newpos = (y*bmp->width *3) + x;
bufpos = (bmp->height-y-1)*psw + x;
//Note, do not convert rgb order in bmp file, just copy the original order
newbuf[newpos] = buf[bufpos];
newbuf[newpos+1] = buf[bufpos+1];
newbuf[newpos+2] = buf[bufpos+2];
}
}
}