opengl-tutorials/ogl

size of bpp for bitmap format is 2 bytes, it ought to be cast into short* rather than int*

zyzyz opened this issue · 0 comments

zyzyz commented

Although the tutorial 5 doesn't mention about the need of checking the bmp file's bpp when loading, but in common/texture.cpp it does do. However, according to the wiki, it shows that the size of data representing the bpp of bitmap is actually 2 bytes:

Offset (hex) Offset (dec) Size (bytes) Windows BITMAPINFOHEADER[1]
1C 28 2 the number of bits per pixel, which is the color depth of the image. Typical values are 1, 4, 8, 16, 24 and 32.

I'm curious why this statement can work,

// the original one...
if ( *(int*)&(header[0x1C])!=24 ) ...

// ...or the re-write version by me 
unsigned int bpp = *reinterpret_cast<int*>(&header[0x1C]);
if (bpp != 24) ...

but I think casting it into short* should make more sense, since short is 2-byte-size in this case. And changing this doesn't affect the correctness.

unsigned int bpp = *reinterpret_cast<short*>(&header[0x1C]);