⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ebgif.c

📁 基于minigui开发的一套图形控件系统
💻 C
📖 第 1 页 / 共 2 页
字号:
//
// $Id: ebgif.c,v 1.5 2005/10/17 07:55:53 tangjb Exp $
//
// gif.c: the gif module 
//
// Copyright (C) 2001, Zhang Yunfan
//
// Create date: 2001/2/02
//

/*
**  This library is free software; you can redistribute it and/or
**  modify it under the terms of the GNU Library General Public
**  License as published by the Free Software Foundation; either
**  version 2 of the License, or (at your option) any later version.
**
**  This library is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
**  Library General Public License for more details.
**
**  You should have received a copy of the GNU Library General Public
**  License along with this library; if not, write to the Free
**  Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
**  MA 02111-1307, USA
*/
//
//
// Modify records:
//
//  Who             When        Where       For What                Status
//-----------------------------------------------------------------------------
//
// TODO: the tranparent color support
// 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include "ebgif.h"

/*
 * GIF decoding routine
 */

#define MAX_LWZ_BITS            12
#define INTERLACE               0x40
#define LOCALCOLORMAP           0x80

#define CM_RED                  0
#define CM_GREEN                1
#define CM_BLUE                 2

#define BitSet(byte, bit)           (((byte) & (bit)) == (bit))
#define ReadOK(file,buffer,len)     fread(buffer, len, 1, file)
#define LM_to_uint(a,b)             (((b)<<8)|(a))
#define PIX2BYTES(n)    			(((n)+7)/8)

static int ZeroDataBlock = 0;

static int EbbmpComputePitch (int bpp, Uint32 width, Uint32* pitch, BOOL does_round);
static int EbLWZReadByte(FILE *src, int flag, int input_code_size);
static int EbGetCode(FILE *src, int code_size, int flag);
static int EbGetDataBlock(FILE *src, unsigned char *buf);
static int EbDoExtension(FILE *src, int label, GIFBITMAPELEMENT *pgifitem);
static int ReadColorMap (FILE *src, int number, RGB* ColorMap);
static int EbReadImageDesc (FILE *src, IMAGEDESC* ImageDesc, GIFSCREEN* GifScreen);

static int EbbmpComputePitch (int bpp, Uint32 width, Uint32* pitch, BOOL does_round)
{
    Uint32 linesize;
    int bytespp = 1;

    if(bpp == 1)
        linesize = PIX2BYTES (width);
    else if(bpp <= 4)
        linesize = PIX2BYTES (width << 2);
    else if (bpp <= 8)
        linesize = width;
    else if(bpp <= 16) {
        linesize = width * 2;
        bytespp = 2;
    } else if(bpp <= 24) {
        linesize = width * 3;
        bytespp = 3;
    } else {
        linesize = width * 4;
        bytespp = 4;
    }

    /* rows are DWORD right aligned*/
    if (does_round)
        *pitch = (linesize + 3) & -4;
    else
        *pitch = linesize;
    return bytespp;
}

static int EbLWZReadByte (FILE *src, int flag, int input_code_size)
{
    int code, incode;
    register int i;
    static int fresh = FALSE;
    static int code_size, set_code_size;
    static int max_code, max_code_size;
    static int firstcode, oldcode;
    static int clear_code, end_code;
    static int table[2][(1 << MAX_LWZ_BITS)];
    static int stack[(1 << (MAX_LWZ_BITS)) * 2], *sp;

    if (flag) 
	{
        set_code_size = input_code_size;
        code_size = set_code_size + 1;
        clear_code = 1 << set_code_size;
        end_code = clear_code + 1;
        max_code_size = 2 * clear_code;
        max_code = clear_code + 2;

        EbGetCode(src, 0, TRUE);

        fresh = TRUE;

        for (i = 0; i < clear_code; ++i) {
            table[0][i] = 0;
            table[1][i] = i;
        }
        for (; i < (1 << MAX_LWZ_BITS); ++i)
            table[0][i] = table[1][0] = 0;

        sp = stack;

        return 0;
    }
	else if (fresh) 
	{
        fresh = FALSE;
        do 
		{
            firstcode = oldcode = EbGetCode(src, code_size, FALSE);
        } while (firstcode == clear_code);
        return firstcode;
    }
    if (sp > stack)
        return *--sp;

    while ((code = EbGetCode(src, code_size, FALSE)) >= 0) 
	{
        if (code == clear_code) 
		{
            for (i = 0; i < clear_code; ++i) {
                table[0][i] = 0;
                table[1][i] = i;
            }
            for (; i < (1 << MAX_LWZ_BITS); ++i)
                table[0][i] = table[1][i] = 0;
            code_size = set_code_size + 1;
            max_code_size = 2 * clear_code;
            max_code = clear_code + 2;
            sp = stack;
            firstcode = oldcode = EbGetCode(src, code_size, FALSE);
            return firstcode;
        }
		else if (code == end_code) 
        {
            int count;
            unsigned char buf[260];

            if (ZeroDataBlock)
                return -2;

            while ((count = EbGetDataBlock(src, buf)) > 0);

            if (count != 0) {
                /*
                 * fprintf (stderr,"missing EOD in data stream (common occurence)");
                 */
            }
            return -2;
        }
        incode = code;

        if (code >= max_code) {
            *sp++ = firstcode;
            code = oldcode;
        }
        while (code >= clear_code) 
		{
            *sp++ = table[1][code];
            if (code == table[0][code])
                fprintf (stderr,"EbLWZReadByte load_gif: circular table entry\n");
            code = table[0][code];
        }

        *sp++ = firstcode = table[1][code];

        if ((code = max_code) < (1 << MAX_LWZ_BITS)) 
		{
            table[0][code] = oldcode;
            table[1][code] = firstcode;
            ++max_code;
            if ((max_code >= max_code_size) && (max_code_size < (1 << MAX_LWZ_BITS))) 
           	{
                max_code_size *= 2;
                ++code_size;
            }
        }
        oldcode = incode;

        if (sp > stack)
            return *--sp;
    }
    return code;
}


static int EbGetCode(FILE *src, int code_size, int flag)
{
    static unsigned char buf[280];
    static int curbit, lastbit, done, last_byte;
    int i, j, ret;
    unsigned char count;

    if (flag) 
	{
        curbit = 0;
        lastbit = 0;
        done = FALSE;
        return 0;
    }
    if ((curbit + code_size) >= lastbit) 
	{
        if (done) 
		{
            if (curbit >= lastbit)
#ifdef _DEBUG
                fprintf (stderr,"load_gif: bad decode\n");
#endif
            return -1;
        }
        buf[0] = buf[last_byte - 2];
        buf[1] = buf[last_byte - 1];

        if ((count = EbGetDataBlock(src, &buf[2])) == 0)
            done = TRUE;

        last_byte = 2 + count;
        curbit = (curbit - lastbit) + 16;
        lastbit = (2 + count) * 8;
    }
    ret = 0;
    for (i = curbit, j = 0; j < code_size; ++i, ++j)
        ret |= ((buf[i / 8] & (1 << (i % 8))) != 0) << j;

    curbit += code_size;

    return ret;
}


static int EbGetDataBlock(FILE *src, unsigned char *buf)
{
    unsigned char count;

    if (!ReadOK(src, &count, 1))
        return 0;
    ZeroDataBlock = count == 0;

    if ((count != 0) && (!ReadOK(src, buf, count)))
        return 0;
    return count;
}

static int EbDoExtension(FILE *src, int label, GIFBITMAPELEMENT * pgifitem)
{
    static unsigned char buf[4];
	unsigned char c;
	
    switch (label) 
	{
    case 0x01:                        /* Plain Text Extension 图形文本扩展*/
    case 0xff:                        /* Application Extension 应用程序扩展*/
    case 0xfe:                        /* Comment Extension 注释扩展*/
		while (!feof(src))
		{
			ReadOK(src, &c, 1);
			if ( c == 0)
				break;
			fseek(src, c, SEEK_CUR);
		}
    //	while (EbGetDataBlock(src, (unsigned char *) buf) != 0);
        return 0;
    case 0xf9:                      /* Graphic Control Extension 图形控制扩展标签*/
	{	
    //	EbGetDataBlock(src, (unsigned char *) buf);
    	while (!feof(src))
    	{
    		ReadOK(src, &c, 1);
			if (c == 0)
				break;
			else if (c == 4)
			{
				ReadOK(src, buf, 4);
				if (pgifitem != NULL)
				{
					pgifitem->ctrl.disposalMethod = (buf[0] >> 2) & 0x7;	//处置方法
					pgifitem->ctrl.userInputFlag = (buf[0] >> 1) & 0x1;		//用户输入标志
					pgifitem->ctrl.trsFlag = buf[0] & 0x1 ;					//透明色标志
					pgifitem->ctrl.delayTime = LM_to_uint(buf[1], buf[2]);	//延迟时间
					
				//	fprintf(stderr, "___dispoal method=%d \n", pgifitem->ctrl.disposalMethod);
					 
					if ( pgifitem->ctrl.trsFlag)
			            pgifitem->ctrl.trsColorIndex =  buf[3];				//透明色索引值
			       
				}
			}
			else
				fseek(src, c, SEEK_CUR);
		}

    //	while (EbGetDataBlock(src, (unsigned char *) buf) != 0);
		
        return 0;
    }
    default:
		return -1;//error
    }


    return 0;
}


static int EbReadColorMap (FILE *src, int number, RGB* ColorMap)
{
    int i;
    unsigned char rgb[3];

    for (i = 0; i < number; ++i) 
	{
        if (!ReadOK (src, rgb, sizeof(rgb))) 
		{
            return -1;
	    }

        ColorMap [i].r = rgb[0];
        ColorMap [i].g = rgb[1];
        ColorMap [i].b = rgb[2];
    }

    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -