#include #include #include #include #include #include "bmp.h" typedef struct { uint16_t file_type; // File type always BM which is 0x4D42 uint32_t file_size; // Size of the file (in bytes) uint16_t reserved1; // Reserved, always 0 uint16_t reserved2; // Reserved, always 0 uint32_t offset_data; // Start position of pixel data (bytes from the beginning of the file) } BMPFileHeader_ts; typedef struct { uint32_t size; // Size of this header (in bytes) int32_t width; // width of bitmap in pixels int32_t height; // width of bitmap in pixels // (if positive, bottom-up, with origin in lower left corner) // (if negative, top-down, with origin in upper left corner) uint16_t planes; // No. of planes for the target device, this is always 1 uint16_t bit_count; // No. of bits per pixel uint32_t compression; // 0 or 3 - uncompressed. THIS PROGRAM CONSIDERS ONLY UNCOMPRESSED BMP images uint32_t size_image; // 0 - for uncompressed images int32_t x_pixels_per_meter; int32_t y_pixels_per_meter; uint32_t colors_used; // No. color indexes in the color table. Use 0 for the max number of colors allowed by bit_count uint32_t colors_important; // No. of colors used for displaying the bitmap. If 0 all colors are required } BMPInfoHeader_ts; // not used yet //typedef struct //{ // uint32_t red_mask; // Bit mask for the red channel // uint32_t green_mask; // Bit mask for the green channel // uint32_t blue_mask; // Bit mask for the blue channel // uint32_t alpha_mask; // Bit mask for the alpha channel // uint32_t color_space_type; // Default "sRGB" (0x73524742) // uint32_t unused; // Unused data for sRGB color space //} BMPColorHeader_ts; typedef struct { BMPFileHeader_ts fileHeader_s; BMPInfoHeader_ts infoHeader_s; // BMPColorHeader_ts colorHeader_s; uint32_t data_au32[4096]; } BMPData_ts; BMPData_ts bmpData_s; static uint8_t reds[64][64] = {0}; static uint8_t greens[64][64] = {0}; static uint8_t blues[64][64] = {0}; #define FILE_HEADER_OFFSET 0x00 // The beginning of the File #define FILE_HEADER_SIZE 0x0E #define INFO_HEADER_OFFSET 0x0E // The fileHeader is fixed size: 40 bytes #define INFO_HEADER_SIZE 0x28 #define COLOR_HEADER_OFFSET 0x36 // Offest to colorHeader_s #define DATA_HEADER_OFFSET (FILE_HEADER_SIZE + INFO_HEADER_SIZE) void read_file(const char *path) { printf("Read file DREIECK.BMP\n"); FILE *imageFile = fopen(path, "rb"); if (imageFile == NULL) { printf("Failed to open file for reading\n"); return; } fread(&bmpData_s.fileHeader_s, sizeof(bmpData_s.fileHeader_s), 1, imageFile); printf("FileHeader.file_type (%c%c). \n", (bmpData_s.fileHeader_s.file_type & 0xFF), ((bmpData_s.fileHeader_s.file_type >> 8) & 0xFF)); printf("FileHeader.file_size (%lu). \n", bmpData_s.fileHeader_s.file_size); printf("FileHeader.offset_data (%lu). \n", bmpData_s.fileHeader_s.offset_data); fseek(imageFile, INFO_HEADER_OFFSET, SEEK_SET); fread(&bmpData_s.infoHeader_s, sizeof(bmpData_s.infoHeader_s), 1, imageFile); printf("FileHeader.size (%lu). \n", bmpData_s.infoHeader_s.size); printf("FileHeader.width (%ld). \n", bmpData_s.infoHeader_s.width); printf("FileHeader.height (%ld). \n", bmpData_s.infoHeader_s.height); printf("FileHeader.planes (%u). \n", bmpData_s.infoHeader_s.planes); printf("FileHeader.bit_count (%u). \n", bmpData_s.infoHeader_s.bit_count); printf("FileHeader.compression (%lu). \n", bmpData_s.infoHeader_s.compression); printf("FileHeader.size_image (%lu). \n", bmpData_s.infoHeader_s.size_image); printf("FileHeader.x_pixels_per_meter(%ld). \n", bmpData_s.infoHeader_s.x_pixels_per_meter); printf("FileHeader.y_pixels_per_meter (%ld). \n", bmpData_s.infoHeader_s.y_pixels_per_meter); printf("FileHeader.colors_used (%lu). \n", bmpData_s.infoHeader_s.colors_used); printf("FileHeader.colors_important (%lu). \n", bmpData_s.infoHeader_s.colors_important); printf("DATA_OFFSET %d\n", DATA_HEADER_OFFSET); printf("DATA_OFFSET read %lu\n", bmpData_s.fileHeader_s.offset_data); //fseek(imageFile, 2621440, SEEK_SET); fseek(imageFile, bmpData_s.fileHeader_s.offset_data, SEEK_SET); fread(&bmpData_s.data_au32[0], sizeof(bmpData_s.data_au32[0]) * 4096, 1, imageFile); uint8_t row = 0; uint8_t col = 0; uint32_t count = 0; for(row=0; row<64-1; row++) { for(col=0; col<64-1; col++) { for(uint8_t i=0; i<3; i++) { switch(i) { case 0: reds[row][col] = bmpData_s.data_au32[count++]; break; case 2: greens[row][col] = bmpData_s.data_au32[count++]; break; case 3: blues[row][col] = bmpData_s.data_au32[count++]; break; } } } } printf("data parsed\n"); for (uint8_t x=0; x<64; x++) { for (uint8_t y=0; y<64; y++) { if(reds[x][y] != 0) { printf("reds[%u][%u]: %u\n", x, y, reds[x][y]); } } } }