#include "BitmapLoader.h" #include "BinaryLoader.h" #include "PNG.h" #include static void *PixelFunc(void *destination,uint8_t r,uint8_t g,uint8_t b,uint8_t a,int x,int y); UnpackedBitmap *AllocAndLoadBitmap(const char *filename) { size_t length; void *bytes=AllocAndLoadFile(filename,&length); if(!bytes) return NULL; PNGLoader loader; InitializePNGLoader(&loader,bytes,length); if(!LoadPNGHeader(&loader)) { fprintf(stderr,"Not a valid PNG file.\n"); free(bytes); return NULL; } UnpackedBitmap *self=malloc(sizeof(UnpackedBitmap)+ loader.width*loader.height*sizeof(UnpackedPixel)); if(!self) return NULL; self->width=loader.width; self->height=loader.height; if(!LoadPNGImageData(&loader,self->pixels,sizeof(UnpackedPixel)*loader.width,PixelFunc)) { fprintf(stderr,"Failed while loading PNG data.\n"); free(bytes); free(self); return NULL; } free(bytes); return self; } static void *PixelFunc(void *destination,uint8_t r,uint8_t g,uint8_t b,uint8_t a,int x,int y) { UnpackedPixel *pixel=destination; pixel->r=r; pixel->g=g; pixel->b=b; pixel->a=a; return pixel+1; } UnpackedBitmap *AllocSubBitmap(const UnpackedBitmap *bitmap,int x,int y,int w,int h) { UnpackedBitmap *self=malloc(sizeof(UnpackedBitmap)+w*h*sizeof(UnpackedPixel)); if(!self) return NULL; self->width=w; self->height=h; UnpackedPixel *ptr=self->pixels; for(int dy=0;dywidth,self->height); } bool IsPartialBitmapTransparent(const UnpackedBitmap *self,int x,int y,int w,int h) { for(int dy=0;dywidth,self->height); } bool IsPartialBitmapInvisible(const UnpackedBitmap *self,int x,int y,int w,int h) { for(int dy=0;dy