/* * build with: * gcc -o bitmap bitmap.c * or maybe you'd prefer: * g++ -o bitmap bitmap.c * I really don't care, it'll work in anything: * clang -o bitmap bitmap.c */ #include #include #include #include typedef struct sprite { uint16_t width; uint16_t height; uint32_t * bitmap; uint32_t * masks; uint32_t blank; uint8_t alpha; } sprite_t; #define _RED(color) ((color & 0x00FF0000) / 0x10000) #define _GRE(color) ((color & 0x0000FF00) / 0x100) #define _BLU(color) ((color & 0x000000FF) / 0x1) #define _ALP(color) ((color & 0xFF000000) / 0x1000000) void load_sprite(sprite_t * sprite, char * filename) { /* Open the requested binary */ FILE * image = fopen(filename, "r"); size_t image_size= 0; fseek(image, 0, SEEK_END); image_size = ftell(image); fseek(image, 0, SEEK_SET); /* Alright, we have the length */ char * bufferb = (char *)malloc(image_size); fread(bufferb, image_size, 1, image); uint16_t x = 0; /* -> 212 */ uint16_t y = 0; /* -> 68 */ /* Get the width / height of the image */ signed int *bufferi = (signed int *)((uintptr_t)bufferb + 2); uint32_t width = bufferi[4]; uint32_t height = bufferi[5]; uint16_t bpp = bufferi[6] / 0x10000; uint32_t row_width = (bpp * width + 31) / 32 * 4; /* Skip right to the important part */ size_t i = bufferi[2]; sprite->width = width; sprite->height = height; sprite->bitmap = (uint32_t *)malloc(sizeof(uint32_t) * width * height); for (y = 0; y < height; ++y) { for (x = 0; x < width; ++x) { if (i > image_size) return; /* Extract the color */ uint32_t color; if (bpp == 24) { color = (bufferb[i + 3 * x] & 0xFF) + (bufferb[i+1 + 3 * x] & 0xFF) * 0x100 + (bufferb[i+2 + 3 * x] & 0xFF) * 0x10000; } else if (bpp == 32) { color = (bufferb[i + 4 * x] & 0xFF) * 0x1000000 + (bufferb[i+1 + 4 * x] & 0xFF) * 0x100 + (bufferb[i+2 + 4 * x] & 0xFF) * 0x10000 + (bufferb[i+3 + 4 * x] & 0xFF) * 0x1; } /* Set our point */ sprite->bitmap[(height - y - 1) * width + x] = color; } i += row_width; } free(bufferb); } int main(int argc, char * argv[]) { int x = 0, y = 0; sprite_t sprite; load_sprite(&sprite, argv[1]); uint32_t color = sprite.bitmap[y * sprite.width + x]; char red = _RED(color); char blue = _BLU(color); char green = _GRE(color); free(sprite.bitmap); printf("(%d,%d) = #%02X%02X%02X\n", x, y, red, blue, green); return 0; }