X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=tools%2Fpngdump%2Fimage.c;h=f276e63778c56fbdb106f014b923d04788472064;hb=HEAD;hp=0ae86f96592463064f3d9607df228ae72b73a859;hpb=ca3123df6cd875b1361533020c1f4eebfe9da295;p=gba_blender diff --git a/tools/pngdump/image.c b/tools/pngdump/image.c index 0ae86f9..f276e63 100644 --- a/tools/pngdump/image.c +++ b/tools/pngdump/image.c @@ -235,3 +235,78 @@ void blit(struct image *src, int sx, int sy, int w, int h, struct image *dst, in sptr += src->pitch; } } + +static unsigned int get_pixel(struct image *img, int x, int y) +{ + unsigned char *pptr; + unsigned short *pptr16; + + switch(img->bpp) { + case 4: + pptr = img->pixels + y * img->pitch + x / 2; + return x & 1 ? *pptr & 0xf : *pptr >> 4; + case 8: + pptr = img->pixels + y * img->pitch + x; + return *pptr; + case 16: + pptr16 = (unsigned short*)(img->pixels + y * img->pitch + x * 2); + return *pptr16; + default: + fprintf(stderr, "get_pixel not implemented for %d bpp\n", img->bpp); + } + + return 0; +} + +static void put_pixel(struct image *img, int x, int y, unsigned int pix) +{ + unsigned char *pptr; + unsigned short *pptr16; + + switch(img->bpp) { + case 4: + pptr = img->pixels + y * img->pitch + x / 2; + if(x & 1) { + *pptr = (*pptr & 0xf0) | pix; + } else { + *pptr = (*pptr & 0xf) | (pix << 4); + } + break; + + case 8: + pptr = img->pixels + y * img->pitch + x; + *pptr = pix; + break; + + case 16: + pptr16 = (unsigned short*)(img->pixels + y * img->pitch + x * 2); + *pptr16 = pix; + break; + + default: + fprintf(stderr, "put_pixel not implemented for %d bpp\n", img->bpp); + } +} + +void overlay_key(struct image *src, unsigned int key, struct image *dst) +{ + int i, j; + unsigned char *sptr, *dptr; + unsigned int pix; + + assert(src->bpp == dst->bpp); + assert(src->width == dst->width); + assert(src->height == dst->height); + + sptr = src->pixels; + dptr = dst->pixels; + + for(i=0; iheight; i++) { + for(j=0; jwidth; j++) { + pix = get_pixel(src, j, i); + if(pix != key) { + put_pixel(dst, j, i, pix); + } + } + } +}