📄 fad.bitmap.c
字号:
/** * libFAD - Flash Animation Decode library * Copyright (C) 2005-2006 VGSystem Technologies, Inc. * * libFAD is the legal property of its developers, whose names are too numerous * to list here. Please refer to the COPYRIGHT file distributed with this * source distribution. * * This program 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 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 Library 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 * * $Id: fad.bitmap.c,v 1.24 2006/03/07 10:17:57 wrxzzj Exp $ */#include "fad.bitmap.h"#include "fad.bits.h"#include "fad.tags.h"/**include zlib header*/#include "zlib/zlib.h"typedef struct { fad_object_t base; u16_t width, height; u8_t* image;} bitmap_bits_t;static void _bitmap_bits_v2_decode(bitmap_bits_t* bb, u8_t* img_buf, u32_t img_size, u8_t format) { s32_t stride, size; s32_t i, j; u8_t npad = 0, a; u16_t offset = 0; u8_t *dest = NULL, *src = NULL; size = (bb->width*bb->height)<<2; bb->image = calloc(1, size); dest = bb->image+size-4; if(uncompress(bb->image, (unsigned long *)&size, img_buf, img_size) != Z_OK) { FAD_ERROR("uncompress bitmap lossless2 data failure.\n"); } if(format == 0x03) { npad = bb->width&0x03; if(npad) npad = 4-npad; src = bb->image+size-1; for(i=bb->height-1; i>=0; i--, src-=npad) for(j=bb->width-1; j>=0; j--, src--, dest-=4) { offset = (*src)<<2; *(dest+0) = *(bb->image+offset+2); *(dest+1) = *(bb->image+offset+1); *(dest+2) = *(bb->image+offset+0); *(dest+3) = *(bb->image+offset+3); } } else { for(i=0, dest = bb->image; i<bb->height; i++) { for(j=0; j<bb->width; j++, dest += 4) { a = *(dest+0); *(dest+0) = *(dest+3); *(dest+3) = a; a = *(dest+1); *(dest+1) = *(dest+2); *(dest+2) = a; } } }}static void _bitmap_bits_v1_decode(bitmap_bits_t* bb, u8_t* img_buf, u32_t img_size, u8_t format) { s32_t size; s32_t i, j, stride; u8_t npad = 0, noff; u8_t *dest = NULL, *src = NULL; stride = bb->width<<2; size = stride*bb->height; bb->image = calloc(1, size); dest = bb->image+size-4; uncompress(bb->image, (unsigned long *)&size, img_buf, img_size); FAD_ERROR("[pre-uncompr] compressed image size = %d, raw bitmap image size = %d\n", img_size, size); switch(format) { case 0x03: npad = bb->width&0x03; if(npad) npad = 4-npad; /**FIXME:this will make several line ahead of bitmap damaged(color_table_size*4 point)*/ src = bb->image+size-1; noff = 1; break; case 0x04: npad = (bb->width<<1)&0x03; if(npad) npad = 4-npad; src = bb->image+size-2; noff = 2; break; case 0x05: src = bb->image+size-4; noff = 4; break; default: break; } FAD_ERROR("[post-uncompr] size = %d, format = %x, bb->width = %d, npad = %d\n", size, format, bb->width, npad); for(i=bb->height; i>0; i--, src-=npad) for(j=bb->width; j>0; j--, dest-=4, src -= noff) { if(format == 0x03) { u16_t offset = (*src)*3; *(dest+0) = *(bb->image+offset+2); *(dest+1) = *(bb->image+offset+1); *(dest+2) = *(bb->image+offset+0); } else if(format == 0x04) { u16_t rgb = (*src<<8)|*(src+1); *(dest+0) = (rgb&0x7c00)>>10; *(dest+1) = (rgb&0x03e0)>>5; *(dest+2) = (rgb&0x001f); } else { /** dest address == src address*/ *(dest+0) = *(src+3); /**R value*/ *(dest+3) = *(src+1); /**B saved*/ *(dest+1) = *(src+2); /**G value*/ *(dest+2) = *(src+3); /**B value*/ } *(dest+3) = 0xff; }}static void* _bitmap_bits_get_image(fad_object_t* fo, u32_t* w, u32_t* h) { bitmap_bits_t* bb = (bitmap_bits_t* )fo; if(bb->image) { *w = bb->width; *h = bb->height; } return bb->image;}static u8_t _bitmap_bits_do_render(fad_object_t* fo, fad_render_t* render, dl_node_t *node) { bitmap_bits_t* bb = (bitmap_bits_t* )fo; FAD_ERROR("bitmap render unimplement.\n"); return FAD_TRUE;}static void _bitmap_bits_do_free(fad_object_t* fo) { bitmap_bits_t* bb = (bitmap_bits_t* )fo; if(bb->image) free(bb->image);}s32_t bitmap_bits_decode(fad_frame_t* frame, fad_stream_t* s) { bitmap_bits_t* bb = NULL; u32_t size = s->tag_len; u16_t id; u8_t format, cts = 0; bb = calloc(1, sizeof(bitmap_bits_t)); if(bb == NULL) { s->err = FAD_ERROR_MEM; return -1; } bb->base.do_free = _bitmap_bits_do_free; bb->base.do_render = _bitmap_bits_do_render; bb->base.get_image = _bitmap_bits_get_image; bb->base.type = FO_TYPE_BITMAP; //bits_dump(&s->bits, 4, stderr); id = bits_get_u16(&s->bits); format = bits_get_u8(&s->bits); bb->width = bits_get_u16(&s->bits); bb->height = bits_get_u16(&s->bits); if(format == 0x03) { bits_get_u8(&s->bits); size--; } size -= 7; if(s->tag_id == TAG_DEFINEBITSLOSSLESS) _bitmap_bits_v1_decode(bb, bits_tell(&s->bits), size, format); else _bitmap_bits_v2_decode(bb, bits_tell(&s->bits), size, format); s->dict->put(s->dict, bb, id); FAD_ERROR("append bitmap to dictionary, id = %d, width = %d, height = %d, format = %x\n", id, bb->width, bb->height, format); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -