- pngdump tool overlay multiple images (combine spritesheets)
[gba_blender] / tools / pngdump / image.c
index 0ae86f9..f276e63 100644 (file)
@@ -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; i<dst->height; i++) {
+               for(j=0; j<dst->width; j++) {
+                       pix = get_pixel(src, j, i);
+                       if(pix != key) {
+                               put_pixel(dst, j, i, pix);
+                       }
+               }
+       }
+}