Sign in to follow this  
Little Coding Fox

Bitmap loading in opengl in c++ WITHOUT any libraries

Recommended Posts

My own source that works with 24-bit bitmaps:
http://www.nife.1go.dk/c++.php?id=1
And one for targa if you need it some day (24 and 32-bit):
http://www.nife.1go.dk/c++.php?id=2

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
if you're going to use a bitmap then you may as well step up and use a .tga because they support an alpha channel.

Share this post


Link to post
Share on other sites
Well, here's some code if it helps.. by the way, just replace the stream.get(), stream.read(), and so on with standard c library calls... for example.. stream.get() because fgetc(), read becomes fread(), etc.

Btw, here's some of the simple types
S8 - signed char (1 byte)
U8 - unsigned char (1 byte)
S16 - signed short (2 bytes)
U16 - unsigned short (2 bytes)
S32 - signed long (4 bytes)
U32 - unsigned long (4 bytes)


bool Bitmap::loadBmp( IStream &stream )
{
if (!stream.initialized())
return false;

S16 id;
U32 offset;
U32 headerSize;
U32 width, height;
U16 bpp;
U32 compression;
U32 dataSize;

stream.read(&id, sizeof(S16));

if (id != 'MB')
return false;

stream.seek(8, STREAM_CUR);
stream.read(&offset, sizeof(U32));
stream.read(&headerSize, sizeof(U32));

if (headerSize != 40)
return false;

stream.read(&width, sizeof(U32));
stream.read(&height, sizeof(U32));
stream.seek(2, STREAM_CUR);

stream.read(&bpp, sizeof(U16));

if (bpp != 24) // currently only support 24-bit bitmaps
return false;

stream.read(&compression, sizeof(U32));

if (compression != 0) // currently only support non-compressed bmp's
return false;

stream.read(&dataSize, sizeof(U32));

if (dataSize == 0)
dataSize = (width * bpp + 7) / 8 * abs(height);

stream.seek(-(S32)dataSize, STREAM_END);

mWidth = width;
mHeight = abs(height);

mData = new U8[mWidth * mHeight * 3];
if (!mData)
return false;

// read in data, flipping it as we read vertically, plus converting BGR -> RGB
U8 *p = mData + (mHeight - 1) * mWidth * 3;
U8 byte1, byte2, byte3;

// if mWidth is not a multiple of 4, then there is extra crap at the end of every line
S32 paddedBytes = (mWidth % 4) ? (4 - ((mWidth * 3) % 4)) : 0;

for(S32 y = 0; y < mHeight; y++)
{
for(S32 x = 0; x < mWidth; x++)
{
if (stream.eos())
{
destroy();
return false;
}

// test for last pixel to read
if( y == mHeight - 1 && x == mWidth - 1)
{
int z = z * 65;


}

byte1 = (U8)stream.get();
byte2 = (U8)stream.get();
byte3 = (U8)stream.get();

*p++ = byte3; // swap BGR->RGB
*p++ = byte2;
*p++ = byte1;

}

// read the extra crap (align on 32-bit (4 byte) boundary)
for (S32 padCount=0; padCount<paddedBytes; padCount++)
stream.get();

// work bottom to top
p -= (mWidth * 3) * 2;
}

mFormat = BITMAP_FMT_24;
return true;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this