Sign in to follow this  

Error decompressing gif images using giflib

Recommended Posts

Hello all


I am using giflib to parse gif files and retreive the infos I need. In genereal this works pretty nice, though there are some gifs (usually ones showing people) that have an invalid behavior. See the following video:


After further investigation i have realized, that those invalid parts are actually using the transparent color index that is given in the extension block. Right now i get the color information from the gif like that:

		mImageNumber = (mImageNumber + 1) % (mGifFile->ImageCount);
		auto& img = mGifFile->SavedImages[mImageNumber];

		auto colorMap = img.ImageDesc.ColorMap ? img.ImageDesc.ColorMap : mGifFile->SColorMap;
		memset(&mGcb, 0, sizeof(mGcb));

		int ret = DGifSavedExtensionToGCB(mGifFile, mImageNumber, &mGcb);
		int32 transparent = -1;
		if (ret == GIF_OK) {
			mHasGcb = true;
			mLastFrame = sTime->getGameTime();
			transparent = mGcb.TransparentColor;
		} else {
			mHasGcb = false;

		for (uint32 i = 0; i < img.ImageDesc.Height; ++i) {
			for (uint32 j = 0; j < img.ImageDesc.Width; ++j) {
				auto clrIndex = img.RasterBits[i * img.ImageDesc.Width + j];
				auto& clrObj = colorMap->Colors[clrIndex];
				auto r = clrObj.Red;
				auto g = clrObj.Green;
				auto b = clrObj.Blue;
				auto alpha = clrIndex == transparent ? 0x00 : 0xFF;
				mColorData[(i + img.ImageDesc.Top) * mGifFile->SWidth + (j + img.ImageDesc.Left)] = (alpha << 24) | (b << 16) | (g << 8) | r;
		mTexture->fromMemory(mGifFile->SWidth, mGifFile->SHeight, mColorData);

All those wrong pixels are having the transparent color. I also tried ignoring alpha, but the color value of the transparent color is white, so it does not look any better.


I load the image using:

		mGifFile = DGifOpen(this, onRead, &mError);
		if (mError) {
			throw System::InvalidOperation(GifErrorString(mError));

		mError = DGifSlurp(mGifFile);
		if (mError == 0) {
			throw System::InvalidOperation(GifErrorString(mGifFile->Error));

Could this be an issue of giflib? The gif im using is:


Is there another library available besides giflib that is able to handle gif files? 


Thanks in advance and greetings


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