Solid Fluid System Solutions  
Home Software About Hardware Firmware
Document Icon LibGIF
Document Icon LibGif Header
Document Icon LibGif Source
Document Icon ToGIF / FromGIF Common
Document Icon ToGIF
Current Document Icon FromGIF

The FromGIF function

void gif_FromGIF(void*& pBuf,unsigned long& BufSz,ImgGIFInfo& Info)
{
	GifBuffer aGB;
	aGB.pBuf = (unsigned char*) pBuf;
	aGB.Size = BufSz;
	aGB.Ptr = 0;

	BufSz = 0;
	pBuf = NULL;

	Gif* pGif = read_gif_file(&aGB);

	img_free(aGB.pBuf);
	aGB.Size = 0;
	aGB.Ptr = 0;

	if(pGif != NULL)
	{
		Info.XDim = pGif->screen->width;
		Info.YDim = pGif->screen->height;

		GifPicture* pGP = NULL;
		GifPalette* pGL = NULL;
		if(SelectPicture(pGif,pGP,pGL,Info.Trns,Info.TrnsIndex))
		{
			bool DoRead = false;
			bool TwoBit = false;
			char TableIndex;

			if(((unsigned long)pGP->width) < Info.XDim)
			 Info.XDim = pGP->width;

			if(((unsigned long)pGP->height) < Info.YDim)
			 Info.YDim = pGP->height;

			if(pGL->length == 256)
			{
				TableIndex = TABLE_8BIT;
				DoRead = true;
			}
			else
			if(pGL->length == 16)
			{
				TableIndex = TABLE_4BIT;
				DoRead = true;
			}
			else
			if(pGL->length == 4)
			{
				TwoBit = true;
				TableIndex = TABLE_4BIT;
				DoRead = true;
			}
			else
			if(pGL->length == 2)
			{
				TableIndex = TABLE_1BIT;
				DoRead = true;
			}

			Info.BitCount = BitCount[TableIndex];

			if(DoRead)
			{
				Info.LineSize = (Info.XDim / PixelDivisor[TableIndex]);
				if((Info.XDim % PixelDivisor[TableIndex]) != 0)
				 Info.LineSize++;

				char Rem = (char)(Info.LineSize % 4);
				if(Rem != 0)
				 Info.LineSize += (4 - Rem);

				BufSz = ((PaletteSize[TableIndex] * 4) + (Info.YDim * Info.LineSize));
				pBuf = img_malloc(BufSz);
				img_memset(pBuf,0,BufSz);

				void* pPalette = pBuf;
				void* pPixel = &(((char*)pBuf)[(PaletteSize[TableIndex] * 4)]);

				unsigned long Index;
				for(Index=0;Index<PaletteSize[TableIndex];Index++)
				{
					if(Index < ((unsigned long)pGL->length))
					{
						((unsigned char*)pPalette)[(Index * 4) + 0] = pGL->colours[Index].blue;
						((unsigned char*)pPalette)[(Index * 4) + 1] = pGL->colours[Index].green;
						((unsigned char*)pPalette)[(Index * 4) + 2] = pGL->colours[Index].red;
						((unsigned char*)pPalette)[(Index * 4) + 3] = 0xFF;
					}
					else
					{
						((unsigned char*)pPalette)[(Index * 4) + 0] = 0x00;
						((unsigned char*)pPalette)[(Index * 4) + 1] = 0x00;
						((unsigned char*)pPalette)[(Index * 4) + 2] = 0x00;
						((unsigned char*)pPalette)[(Index * 4) + 3] = 0xFF;
					}
				}

				unsigned long x,y;
				for(y=0;y<Info.YDim;y++)
				{
					unsigned char* pSrcRow = pGP->data[((Info.YDim - 1) - y)];
					unsigned char* pDestRow = &(((unsigned char*)pPixel)[(y * Info.LineSize)]);
					for(x=0;x<Info.XDim;x++)
					{
						unsigned long TableOffs = (x & AddressMaskTable[TableIndex]);
						unsigned char Data = pSrcRow[x];

						Data <<= ShiftTable[TableIndex][TableOffs];
						Data &= MaskTable[TableIndex][TableOffs];

						if(TwoBit)
						 Data &= (0xC0 << ShiftTable[TableIndex][TableOffs]);

						pDestRow[(x >> AddressShiftTable[TableIndex])] |= Data;
					}
				}
			}
		}

		del_gif(pGif);
	}
}
Copyright © Solid Fluid 2007-2022
Last modified: SolFlu  Sun, 14 Jun 2009 10:01:51 GMT