Jump to content
  • Advertisement
Sign in to follow this  

Error decompressing gif images using giflib

This topic is 2017 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!