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

📄 gif2png.c

📁 神龙卡开发原代码
💻 C
📖 第 1 页 / 共 2 页
字号:
	return 0;    }    if ((curbit + code_size) >= lastbit) {	if (done) {	    if (curbit >= lastbit)		EPRINTF("LoadGIF: bad decode\n");	    return -1;	}	buf[0] = buf[last_byte - 2];	buf[1] = buf[last_byte - 1];	if ((count = GetDataBlock(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 intLWZReadByte(buffer_t *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;	GetCode(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 = GetCode(src, code_size, FALSE);	} while (firstcode == clear_code);	return firstcode;    }    if (sp > stack)	return *--sp;    while ((code = GetCode(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 = GetCode(src, code_size, FALSE);	    return firstcode;	} else if (code == end_code) {	    int count;	    unsigned char buf[260];	    if (ZeroDataBlock)		return -2;	    while ((count = GetDataBlock(src, buf)) > 0);	    if (count != 0) {		/*		 * EPRINTF("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])		EPRINTF("LoadGIF: 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 intReadImage(buffer_t* src, const char *png_filename, 	  int len, int height, int cmapSize,	  unsigned char cmap[3][MAXCOLORMAPSIZE],	  int gray, int interlace, int ignore){    unsigned char c;    int i, v;    int xpos, ypos, pass;    FILE *fp;    png_structp png_ptr;    png_infop info_ptr;    int png_num_palette;    png_colorp png_palette;    png_bytepp png_row_pointers;    png_uint_32 png_width;    png_uint_32 png_height;    int png_bit_depth;    int png_color_type;    int png_interlace_method;    int png_compression_method;    int png_filter_method;        /*     *	Initialize the compression routines     */    if (!ReadOK(src, &c, 1)) {	EPRINTF("LoadGIF: EOF on image data\n");	return 0;    }    if (LWZReadByte(src, TRUE, c) < 0) {	EPRINTF("LoadGIF: error reading image\n");	return 0;    }    /*     *	If this is an "uninteresting picture" ignore it.     */    if (ignore) {	EPRINTF("Ignoring...\n");	while (LWZReadByte(src, FALSE, c) >= 0);	return 0;    }    /* GIF seems OK, lets go write a PNG */    EPRINTF("Lets start to write the png...%s\n",png_filename);        fp = fopen(png_filename,"wb");    if(!fp) {	EPRINTF("Failed to open %s\n",png_filename);	return 0;    }    EPRINTF("File opened\n");    /* Init PNG lib */    png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL,				      (png_error_ptr)NULL, (png_error_ptr)NULL);    if (png_ptr == NULL) {	EPRINTF("Failed to create write struct\n");	fclose(fp);	return 0;    }        EPRINTF("Write struct created...\n");        info_ptr = png_create_info_struct(png_ptr);    if (info_ptr == NULL) {	    fclose(fp);	    EPRINTF("Failed to create info struct\n");	    png_destroy_write_struct(&png_ptr, (png_infopp)NULL);	    return 0;    }    EPRINTF("Info struct created...\n");    if (setjmp(png_jmpbuf(png_ptr))) {	/* If we get here, we had a problem writing the file */        EPRINTF("Error, back to the setjmp\n");	fclose(fp);	png_destroy_write_struct(&png_ptr, &info_ptr);	return 0;    }        EPRINTF("jmpbuf set...\n");    png_init_io(png_ptr, fp);    EPRINTF("init IO done...\n");       /* Set the PNG headers */        png_width=len;    png_height=height;    png_bit_depth=8;    png_color_type=PNG_COLOR_TYPE_PALETTE;    png_interlace_method=PNG_INTERLACE_NONE;    png_compression_method=PNG_COMPRESSION_TYPE_DEFAULT;    png_filter_method=PNG_FILTER_TYPE_DEFAULT;        png_set_IHDR(png_ptr, info_ptr, png_width, png_height, png_bit_depth,		 png_color_type,png_interlace_method, png_compression_method,		 png_filter_method);    EPRINTF("PNG header setup...W=%ld, H=%ld\n",png_width, png_height);          /* Set the PNG palette */        png_num_palette = cmapSize;    png_palette=(png_color *)malloc(png_num_palette*sizeof(png_color));    for(i=0;i<png_num_palette; i++) {	png_palette[i].red=cmap[CM_RED][i];	png_palette[i].green=cmap[CM_GREEN][i];	png_palette[i].blue=cmap[CM_BLUE][i];   }    //    png_set_PLTE(png_ptr, info_ptr, png_palette, png_num_palette);//    EPRINTF("PNG palette setup...\n");            /* allocate data rows */        png_row_pointers=(png_bytepp) malloc(png_height*sizeof(png_bytep));    if(png_row_pointers==NULL) {	EPRINTF("Cannot allocate png_row_pointers\n");	return 0;    }    for(i=0; i<png_height; i++) {	png_row_pointers[i]=(png_bytep)malloc(png_width*sizeof(png_byte));	if(png_row_pointers[i]==NULL) {	    EPRINTF("Cannot allocate png_row_pointers[%d]\n",i);	    return 0;	}    }        xpos = 0;    ypos = 0;    pass = 0;        /* GIF decompress - fill data rows */    while ((v = LWZReadByte(src, FALSE, c)) >= 0) {	png_row_pointers[ypos][xpos] = v;		++xpos;	if (xpos == len) {	    xpos = 0;	    if (interlace) {		switch (pass) {		case 0:		case 1:		    ypos += 8;		    break;		case 2:		    ypos += 4;		    break;		case 3:		    ypos += 2;		    break;		}		if (ypos >= height) {		    ++pass;		    switch (pass) {		    case 1:			ypos = 4;			break;		    case 2:			ypos = 2;			break;		    case 3:			ypos = 1;			break;		    default:			goto fini;		    }		}	    } else {		++ypos;	    }	}	if (ypos >= height)	    break;    }    /* Set PNG data rows */    png_set_rows(png_ptr, info_ptr, png_row_pointers);    EPRINTF("PNG Data rows set...\n");        /* Write PNG file */    png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);    EPRINTF("PNG Write DONE\n");fini:    return 1;}int main(int argc, char *argv[]){    int fd;    struct stat s;    void *buffer = 0;    buffer_t src;    char *png_filename;    if(argc<=1) {	printf("Usage: %s <filename.gif>\n",argv[0]);	return 1;    }            fd = open(argv[1], O_RDONLY);    if (fd < 0 || fstat(fd, &s) < 0) {	fprintf(stderr,"Can't open image: %s\n", argv[1]);	return(0);    }    png_filename=(char *)malloc(strlen(argv[1])+5);    strcpy(png_filename, argv[1]);    strcat(png_filename,".png");				        buffer = mmap(0, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);        if (!buffer) {	fprintf(stderr,"Couldn't map image %s\n", argv[1]);	close(fd);	return(0);    }        printf("binit: size=%ld\n",s.st_size);    binit(buffer, s.st_size, &src);        printf("Converting to %s\n",png_filename);    LoadGIF(&src,png_filename);    munmap(buffer, s.st_size);       return 0;}

⌨️ 快捷键说明

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