|
The ToGIF Function
void gif_ToGIF(void*& pBuf,unsigned long& BufSz,ImgGIFInfo& Info)
{
bool DoWrite = false;
char TableIndex;
if(Info.BitCount == 8)
{
TableIndex = TABLE_8BIT;
DoWrite = true;
}
else
if(Info.BitCount == 4)
{
TableIndex = TABLE_4BIT;
DoWrite = true;
}
else
if(Info.BitCount == 1)
{
TableIndex = TABLE_1BIT;
DoWrite = true;
}
if(DoWrite)
{
void* pPalette = pBuf;
void* pPixel = &(((char*)pBuf)[(PaletteSize[TableIndex] * 4)]);
Gif* pGif = new_gif(Info.Trns);
GifScreen* pGS = pGif->screen;
pGS->width = Info.XDim;
pGS->height = Info.YDim;
pGS->has_cmap = 1;
pGS->color_res = BitCount[TableIndex];
pGS->sorted = 0;
pGS->cmap_depth = BitCount[TableIndex];
pGS->bgcolour = 0;
pGS->aspect = 0;
GifPalette* pGL = pGS->cmap;
pGL->length = PaletteSize[TableIndex];
pGL->colours = (Colour*) img_malloc(pGL->length * sizeof(Colour));
unsigned long Index;
for(Index=0;Index<PaletteSize[TableIndex];Index++)
{
pGL->colours[Index].blue = ((unsigned char*)pPalette)[(Index * 4) + 0];
pGL->colours[Index].green = ((unsigned char*)pPalette)[(Index * 4) + 1];
pGL->colours[Index].red = ((unsigned char*)pPalette)[(Index * 4) + 2];
}
GifBlock* pGBTrns = NULL;
GifBlock* pGBImg = NULL;
if(Info.Trns)
{
pGBTrns = new_gif_block();
pGBImg = new_gif_block();
pGif->blocks = (GifBlock**) img_malloc(sizeof(GifBlock*) * 2);
pGif->blocks[0] = pGBTrns;
pGif->blocks[1] = pGBImg;
pGif->block_count = 2;
}
else
{
pGBImg = new_gif_block();
pGif->blocks = (GifBlock**) img_malloc(sizeof(GifBlock*));
pGif->blocks[0] = pGBImg;
pGif->block_count = 1;
}
if(pGBTrns != NULL)
{
GifExtension* pGE = new_gif_extension();
pGBTrns->intro = BLOCK_INTRO_EXTENSION;
pGBTrns->ext = pGE;
pGE->marker = 0xF9;
pGE->data = (GifData**) img_malloc(sizeof(GifData*));
GifData* pGD = new_gif_data(4);
pGE->data_count = 1;
pGE->data[0] = pGD;
pGD->bytes[0] |= 0x01;
pGD->bytes[3] = Info.TrnsIndex;
}
pGBImg->intro = BLOCK_INTRO_PICTURE;
GifPicture* pGP = new_gif_picture();
pGBImg->pic = pGP;
pGP->width = Info.XDim;
pGP->height = Info.YDim;
pGP->cmap_depth = BitCount[TableIndex];
unsigned long x,y;
pGP->data = (unsigned char**) img_malloc(Info.YDim * sizeof(unsigned char*));
for(y=0;y<Info.YDim;y++)
{
pGP->data[y] = (unsigned char*) img_malloc(Info.XDim * sizeof(unsigned char));
img_memset(pGP->data[y],0,(Info.XDim * sizeof(unsigned char)));
}
for(y=0;y<Info.YDim;y++)
{
unsigned char* pDestRow = pGP->data[((Info.YDim - 1) - y)];
unsigned char* pSrcRow = &(((unsigned char*)pPixel)[(y * Info.LineSize)]);
for(x=0;x<Info.XDim;x++)
{
unsigned long TableOffs = (x & AddressMaskTable[TableIndex]);
unsigned char Data = pSrcRow[(x >> AddressShiftTable[TableIndex])];
Data &= MaskTable[TableIndex][TableOffs];
Data >>= ShiftTable[TableIndex][TableOffs];
pDestRow[x] |= Data;
}
}
GifBuffer aGB;
aGB.pBuf = NULL;
aGB.Size = 0;
aGB.Ptr = 0;
write_gif_file(&aGB,pGif);
del_gif(pGif);
img_free(pBuf);
pBuf = aGB.pBuf;
BufSz = aGB.Size;
}
else
{
img_free(pBuf);
pBuf = NULL;
BufSz = 0;
}
}
|