img_gif.c

来自「微型浏览器」· C语言 代码 · 共 947 行 · 第 1/2 页

C
947 行
字号
/******************************************************************************* * * img_gif.h  *                    * Original code from dillo http://dillo.sourceforge.net * * Hacked by Garett Spencley for Cheetah Web Browser * *  Adapted by Raph Levien from giftopnm.c as found in the *  netpbm-1mar1994 release. The copyright notice for giftopnm.c *  follows: *//* +-------------------------------------------------------------------+ *//* | Copyright 1990, 1991, 1993, David Koblas.  (koblas@netcom.com)    | *//* |   Permission to use, copy, modify, and distribute this software   | *//* |   and its documentation for any purpose and without fee is hereby | *//* |   granted, provided that the above copyright notice appear in all | *//* |   copies and that both that copyright notice and this permission  | *//* |   notice appear in supporting documentation.  This software is    | *//* |   provided "as is" without express or implied warranty.           | *//* +-------------------------------------------------------------------+ *//* *  * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  * *******************************************************************************/#include <stdio.h>#include <string.h>#include <gtk/gtk.h>#include "debug.h"#include "img_gif.h"#include "image.h"static size_t Gif_process_bytes(GifImage * gif, const guchar * buf, size_t bufsize, void *Buf);/* * MIME handler for "image/gif" type * (Sets Gif_callback as cache-client) */DwWidget *a_Gif_image(const char *Type, ImageData * Image, void **Data){	if (!Image)		Image = Image_new(0, 0, NULL, DW_PAINT_DEFAULT_BGND);	*Data = Image;	return DW_WIDGET(Image->dw);}/* * Create a new gif structure for decoding a gif into a RGB buffer */GifImage *Gif_new(ImageData * Image){	GifImage *gif = g_malloc(sizeof(GifImage));	gif->Image = Image;	gif->Flags = 0;	gif->state = 0;	gif->Start_Ofs = 0;	gif->linebuf = NULL;	gif->Background = -1;	gif->transparent = -1;	gif->num_spill_lines_max = 0;	gif->spill_lines = NULL;	gif->window = 0;	gif->packet_size = 0;	return gif;}/* * Receive and process new chunks of GIF image data */void Gif_write(GifImage *gif, void *Buf, gint BufSize){	guchar *buf;	size_t bufsize, bytes_consumed;	/* Sanity checks */	if (!Buf || !gif->Image || BufSize <= 0)		return;	buf = ((guchar *) Buf) + gif->Start_Ofs;	bufsize = BufSize - gif->Start_Ofs;	debug_print("Gif_write: %d bytes", BufSize);	/* Process the bytes in the input buffer. */	bytes_consumed = Gif_process_bytes(gif, buf, bufsize, Buf);	if (bytes_consumed < 1)		return;	gif->Start_Ofs += bytes_consumed;	debug_print("exit Gif_write, bufsize=%d", bufsize);}/* * Finish the decoding process (and free the memory) */void Gif_close(GifImage * gif){	gint i;	debug_print("destroy gif %x\n", gif);	if (gif->linebuf != NULL)		g_free(gif->linebuf);	if (gif->spill_lines != NULL) {		for (i = 0; i < gif->num_spill_lines_max; i++)			g_free(gif->spill_lines[i]);		g_free(gif->spill_lines);	}	g_free(gif);}/* --- GIF Extensions --------------------------------------------------------- *//* * This reads a sequence of GIF data blocks.. and ignores them! * Buf points to the first data block. * * Return Value * 0 = There wasn't enough bytes read yet to read the whole datablock * otherwise the size of the data blocks */static inline size_t Gif_data_blocks(const guchar * Buf, size_t BSize){	size_t Size = 0;	if (BSize < 1)		return 0;	while (Buf[0]) {		if (BSize <= (size_t) (Buf[0] + 1))			return 0;		Size += Buf[0] + 1;		BSize -= Buf[0] + 1;		Buf += Buf[0] + 1;	}	return Size + 1;}/* * This is a GIF extension.  We ignore it with this routine. * Buffer points to just after the extension label. * * Return Value * 0 -- block not processed * otherwise the size of the extension label. */static inline size_t Gif_do_generic_ext(const guchar * Buf, size_t BSize){	size_t Size = Buf[0] + 1, DSize;	/*The Block size (the first byte) is supposed to be a specific size for each	 * extension... we don't check.	 */	if (Buf[0] > BSize)		return 0;	DSize = Gif_data_blocks(Buf + Size, BSize - Size);	if (!DSize)		return 0;	Size += DSize;	return Size <= BSize ? Size : 0;}/* * ? */static __inline size_t Gif_do_gc_ext(GifImage * gif, const guchar * Buf, size_t BSize){	/* Graphic Control Extension */	size_t Size = Buf[0] + 2;	guint Flags;	if (Size > BSize)		return 0;	Buf++;	Flags = Buf[0];	/* The packed fields */#if 0	gif->disposal = (Buf[0] >> 2) & 0x7;	gif->inputFlag = (Buf[0] >> 1) & 0x1;	/* Delay time */	gif->delayTime = LM_to_uint(Buf[1], Buf[2]);#endif	/* Transparent color index, may not be valid  (unless flag is set) */	if ((Flags & 0x1)) {		gif->transparent = Buf[3];	}	return Size;}#define App_Ext  (0xff)#define Cmt_Ext  (0xfe)#define GC_Ext   (0xf9)#define Txt_Ext  (0x01)/* * ? * Return value: *    TRUE when the extension is over */static size_t Gif_do_extension(GifImage * gif, guchar Label, const guchar * buf, size_t BSize){	switch (Label) {	case GC_Ext:		/* Graphics extension */		return Gif_do_gc_ext(gif, buf, BSize);	case Cmt_Ext:		/* Comment extension */		return Gif_data_blocks(buf, BSize);	case Txt_Ext:		/* Plain text Extension */		/* This extension allows (rcm thinks) the image to be rendered as text.		 */	case App_Ext:		/* Application Extension */	default:		return Gif_do_generic_ext(buf, BSize);	/*Ignore Extension */	}}/* --- General Image Decoder ------------------------------------------------------ *//* Here begins the new push-oriented decoder. It should be quite a bit * faster than the old code, which was adapted to be asynchronous from * David Koblas's original giftopnm code. * *//* * ? */static void Gif_lwz_init(GifImage * gif){	gif->num_spill_lines_max = 1;	gif->spill_lines = g_malloc(sizeof(guchar *) * gif->num_spill_lines_max);	gif->spill_lines[0] = g_malloc(gif->Width);	gif->bits_in_window = 0;	/* First code in table = clear_code +1	 * Last code in table = first code in table	 * clear_code = (1<< input code size)	 */	gif->last_code = (1 << gif->input_code_size) + 1;	memset(gif->code_and_byte, 0, (1 + gif->last_code) * sizeof(gif->code_and_byte[0]));	gif->code_size = gif->input_code_size + 1;	gif->line_index = 0;}/* * Send the image line to the dicache, also handling the interlacing. */static void Gif_emit_line(GifImage * gif, const guchar * linebuf){	Image_write(gif->Image, linebuf, gif->y, TRUE);	if (gif->Flags & INTERLACE) {		switch (gif->pass) {		case 0:		case 1:			gif->y += 8;			break;		case 2:			gif->y += 4;			break;		case 3:			gif->y += 2;			break;		}		if (gif->y >= gif->Height) {			gif->pass++;			switch (gif->pass) {			case 1:				gif->y = 4;				break;			case 2:				gif->y = 2;				break;			case 3:				gif->y = 1;				break;			default:				/* arriving here is an error in the input image. */				gif->y = 0;				break;			}		}	} else {		if (gif->y < gif->Height)			gif->y++;	}}/* * I apologize for the large size of this routine and the goto error * construct - I almost _never_ do that. I offer the excuse of * optimizing for speed. * * RCM -- busted these down into smaller subroutines... still very hard to * read. *//* * Decode the packetized lwz bytes */static void Gif_literal(GifImage * gif, guint code){	gif->linebuf[gif->line_index++] = code;	if (gif->line_index >= gif->Width) {		Gif_emit_line(gif, gif->linebuf);		gif->line_index = 0;	}	gif->length[gif->last_code + 1] = 2;	gif->code_and_byte[gif->last_code + 1] = (code << 8);	gif->code_and_byte[gif->last_code] |= code;}/* * ? *//* Profiling reveals over half the GIF time is spent here: */static void Gif_sequence(GifImage * gif, guint code){	guint o_index, o_size, orig_code;	guchar *obuf = NULL;	guint sequence_length = gif->length[code];	guint line_index = gif->line_index;	gint num_spill_lines;	gint spill_line_index = gif->spill_line_index;	guchar *last_byte_ptr;	gif->length[gif->last_code + 1] = sequence_length + 1;	gif->code_and_byte[gif->last_code + 1] = (code << 8);	/* We're going to traverse the sequence backwards. Thus,	 * we need to allocate spill lines if the sequence won't	 * fit entirely within the present scan line. */	if (line_index + sequence_length <= gif->Width) {		num_spill_lines = 0;		obuf = gif->linebuf;		o_index = line_index + sequence_length;		o_size = sequence_length - 1;	} else {		num_spill_lines = (line_index + sequence_length - 1) / gif->Width;		o_index = (line_index + sequence_length - 1) % gif->Width + 1;		if (num_spill_lines > gif->num_spill_lines_max) {			/* Allocate more spill lines. */			spill_line_index = gif->num_spill_lines_max;			gif->num_spill_lines_max = num_spill_lines << 1;			gif->spill_lines = g_realloc(gif->spill_lines, gif->num_spill_lines_max * sizeof(guchar *));			for (; spill_line_index < gif->num_spill_lines_max; spill_line_index++)				gif->spill_lines[spill_line_index] = g_malloc(gif->Width);		}		spill_line_index = num_spill_lines - 1;		obuf = gif->spill_lines[spill_line_index];		o_size = o_index;	}	gif->line_index = o_index;	/* for afterwards */	/* for fixing up later if last_code == code */	orig_code = code;	last_byte_ptr = obuf + o_index - 1;	/* spill lines are allocated, and we are clear to	 * write. This loop does not write the first byte of	 * the sequence, however (last byte traversed). */	while (sequence_length > 1) {		sequence_length -= o_size;		/* Write o_size bytes to		 * obuf[o_index - o_size..o_index). */		for (; o_size > 0 && o_index > 0; o_size--) {			guint code_and_byte = gif->code_and_byte[code];			obuf[--o_index] = code_and_byte & 255;			code = code_and_byte >> 8;		}		/* Prepare for writing to next line. */		if (o_index == 0) {			if (spill_line_index > 0) {				spill_line_index--;				obuf = gif->spill_lines[spill_line_index];				o_size = gif->Width;			} else {				obuf = gif->linebuf;				o_size = sequence_length - 1;			}			o_index = gif->Width;		}	}	/* Ok, now we write the first byte of the sequence. */	/* We are sure that the code is literal. */	obuf[--o_index] = code;	gif->code_and_byte[gif->last_code] |= code;	/* Fix up the output if the original code was last_code. */	if (orig_code == gif->last_code)		*last_byte_ptr = code;	/* Output any full lines. */	o_index = gif->line_index;	if (gif->line_index >= gif->Width) {		Gif_emit_line(gif, gif->linebuf);		gif->line_index = 0;	}	if (num_spill_lines) {		if (gif->line_index)			Gif_emit_line(gif, gif->linebuf);		for (spill_line_index = 0; spill_line_index < num_spill_lines - (o_index ? 1 : 0); spill_line_index++)			Gif_emit_line(gif, gif->spill_lines[spill_line_index]);	}

⌨️ 快捷键说明

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