#ifndef __RLE_LOADER_H__ #define __RLE_LOADER_H__ #include "UnpackedBitmap.h" #include "RLELoader.h" #include "BitmapLoader.h" #include #include #include #include typedef struct UnpackedRLECode { bool islength:1; bool isend:1; union { UnpackedPixel pixel; struct { uint16_t empty,filled; } lengths; } value; } UnpackedRLECode; typedef struct UnpackedRLEBitmap { int width,height,numcodes; UnpackedRLECode codes[0]; } UnpackedRLEBitmap; static UnpackedRLEBitmap *AllocRLEBitmapFromBitmapOrDie(const UnpackedBitmap *bitmap); static UnpackedRLEBitmap *AllocRLEBitmapFromPartialBitmapOrDie(const UnpackedBitmap *bitmap,int x0,int y0,int width,int height); static size_t SizeOfRLEBitmapFromPartialBitmap(const UnpackedBitmap *bitmap,int x0,int y0,int width,int height); static void InitializeRLEBitmapFromPartialBitmap(UnpackedRLEBitmap *self,const UnpackedBitmap *bitmap,int x0,int y0,int width,int height); static UnpackedRLEBitmap *AllocRLEBitmapFromBitmapOrDie(const UnpackedBitmap *bitmap) { return AllocRLEBitmapFromPartialBitmapOrDie(bitmap,0,0,bitmap->width,bitmap->height); } static UnpackedRLEBitmap *AllocRLEBitmapFromPartialBitmapOrDie(const UnpackedBitmap *bitmap,int x0,int y0,int width,int height) { size_t size=SizeOfRLEBitmapFromPartialBitmap(bitmap,0,0,bitmap->width,bitmap->height); UnpackedRLEBitmap *self=malloc(size); if(!self) { fprintf(stderr,"Out of memory when trying to allocate RLE bitmap of size %dx%d.\n",width,height); exit(1); } InitializeRLEBitmapFromPartialBitmap(self,bitmap,x0,y0,width,height); return self; } static size_t SizeOfRLEBitmapFromPartialBitmap(const UnpackedBitmap *bitmap,int x0,int y0,int width,int height) { int numcodes=0; for(int y=0;ywidth=width; self->height=height; self->numcodes=0; UnpackedRLECode *ptr=self->codes; for(int y=0;yislength=true; ptr->isend=x>=width && filled==0; ptr->value.lengths.empty=empty; ptr->value.lengths.filled=filled; ptr++; for(int i=0;iislength=false; ptr->value.pixel=p; ptr++; } self->numcodes+=1+filled; } } } #endif