Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
blog:x68_launcher_3 [2020/09/07 11:10] – [bmp.c / bmp.h] john | blog:x68_launcher_3 [2020/09/07 11:51] (current) – john | ||
---|---|---|---|
Line 2: | Line 2: | ||
==== bmp.c / bmp.h ==== | ==== bmp.c / bmp.h ==== | ||
+ | |||
+ | * https:// | ||
+ | * https:// | ||
The header includes these common files: | The header includes these common files: | ||
Line 17: | Line 20: | ||
<code " | <code " | ||
+ | #include < | ||
+ | |||
#define BMP_FILE_SIG_OFFSET 0x0000 // Should always be ' | #define BMP_FILE_SIG_OFFSET 0x0000 // Should always be ' | ||
#define BMP_FILE_SIZE_OFFSET 0x0002 // Size of file, including headers | #define BMP_FILE_SIZE_OFFSET 0x0002 // Size of file, including headers | ||
#define DATA_OFFSET_OFFSET 0x000A // How many bytes from 0x0000 the data section starts | #define DATA_OFFSET_OFFSET 0x000A // How many bytes from 0x0000 the data section starts | ||
- | #define DIB_HEADER_OFFSET 0x000E // Where the DIB header can be found | ||
- | #define DIB_HEADER_SIZE 4 | ||
#define WIDTH_OFFSET 0x0012 // Where we can find the x-axis pixel size | #define WIDTH_OFFSET 0x0012 // Where we can find the x-axis pixel size | ||
#define HEIGHT_OFFSET 0x0016 // Where we can find the y-axis pixel size | #define HEIGHT_OFFSET 0x0016 // Where we can find the y-axis pixel size | ||
Line 32: | Line 35: | ||
#define INFO_HEADER_SIZE 40 | #define INFO_HEADER_SIZE 40 | ||
#define BMP_1BPP 1 | #define BMP_1BPP 1 | ||
- | #define BMP_4BPP 4 | ||
#define BMP_8BPP 8 | #define BMP_8BPP 8 | ||
#define BMP_16BPP 16 | #define BMP_16BPP 16 | ||
Line 46: | Line 48: | ||
#define BMP_ERR_FONT_WIDTH -7 // We dont support fonts of this width | #define BMP_ERR_FONT_WIDTH -7 // We dont support fonts of this width | ||
#define BMP_ERR_FONT_HEIGHT -8 // We dont support fonts of this height | #define BMP_ERR_FONT_HEIGHT -8 // We dont support fonts of this height | ||
+ | |||
#define BMP_FONT_MAX_WIDTH 8 | #define BMP_FONT_MAX_WIDTH 8 | ||
#define BMP_FONT_MAX_HEIGHT 16 | #define BMP_FONT_MAX_HEIGHT 16 | ||
#define BMP_FONT_PLANES 4 // Number of colour planes per pixel | #define BMP_FONT_PLANES 4 // Number of colour planes per pixel | ||
- | // ============================ | ||
- | // | ||
- | // A single palette entry of 8bit r, g and b values | ||
- | // | ||
- | // ============================ | ||
- | typedef struct pal_entry { | ||
- | unsigned char r; | ||
- | unsigned char g; | ||
- | unsigned char b; | ||
- | unsigned char new_palette_entry; | ||
- | } pal_entry_t; | ||
// ============================ | // ============================ | ||
Line 68: | Line 60: | ||
// ============================ | // ============================ | ||
typedef struct bmpdata { | typedef struct bmpdata { | ||
- | unsigned int width; // X resolution in pixels | + | unsigned int width; // X resolution in pixels |
- | unsigned int height; // Y resolution in pixels | + | unsigned int height; // Y resolution in pixels |
- | char compressed; | + | char compressed; |
- | unsigned int dib_size; | + | uint16_t |
- | unsigned char is_indexed; // If the image uses a palette table | + | uint16_t bytespp; // Bytes per pixel |
- | unsigned short colours_offset; | + | uint32_t offset; // Offset from header to data section, in bytes |
- | unsigned int colours; | + | unsigned int row_padded; |
- | unsigned short bpp; | + | unsigned int row_unpadded; |
- | unsigned short bytespp; // Bytes per pixel | + | unsigned int size; // Size of the pixel data, in bytes |
- | unsigned int offset; // Offset from header to data section, in bytes | + | unsigned int n_pixels; |
- | unsigned int row_padded; | + | uint8_t |
- | unsigned int row_unpadded; | + | } __attribute__((__packed__)) __attribute__((aligned (2))) bmpdata_t; |
- | unsigned int size; // Size of the pixel data, in bytes | + | |
- | unsigned int n_pixels; | + | |
- | struct pal_entry palette[256]; | + | |
- | unsigned char *pixels; // Pointer to raw pixels - in font mode each byte is a single | + | |
- | } bmpdata_t; | + | |
// ============================ | // ============================ | ||
Line 94: | Line 81: | ||
unsigned int width_bytes; | unsigned int width_bytes; | ||
unsigned int rows_remaining; | unsigned int rows_remaining; | ||
- | unsigned char *pixels; // Needs to be malloc' | + | uint8_t *pixels; // Needs to be malloc' |
- | } bmpstate_t; | + | } __attribute__((__packed__)) __attribute__((aligned (2))) bmpstate_t; |
// ============================ | // ============================ | ||
Line 108: | Line 95: | ||
// | // | ||
typedef struct fontdata { | typedef struct fontdata { | ||
- | unsigned char width; // Width of each character, in pixels | + | uint8_t width; // Width of each character, in pixels |
- | unsigned char height; // Height of each character, in pixels | + | uint8_t height; // Height of each character, in pixels |
- | unsigned char ascii_start; | + | uint8_t ascii_start; |
- | unsigned char n_symbols; | + | uint8_t n_symbols; |
- | unsigned char unknown_symbol; | + | uint8_t unknown_symbol; |
- | unsigned char symbol[96][16][16]; // Only up to 16px high, 16 px wide fonts | + | uint8_t |
- | } fontdata_t; | + | } __attribute__((__packed__)) __attribute__((aligned (2))) fontdata_t; |
void bmp_Destroy(bmpdata_t *bmpdata); | void bmp_Destroy(bmpdata_t *bmpdata); | ||
void bmp_DestroyFont(fontdata_t *fontdata); | void bmp_DestroyFont(fontdata_t *fontdata); | ||
- | int bmp_ReadFont(FILE *bmp_image, bmpdata_t *bmpdata, fontdata_t *fontdata, | + | int bmp_ReadFont(FILE *bmp_image, bmpdata_t *bmpdata, fontdata_t *fontdata, |
- | int bmp_ReadImage(FILE *bmp_image, bmpdata_t *bmpdata, | + | int bmp_ReadImage(FILE *bmp_image, bmpdata_t *bmpdata, |
int bmp_ReadImageHeader(FILE *bmp_image, bmpdata_t *bmpdata); | int bmp_ReadImageHeader(FILE *bmp_image, bmpdata_t *bmpdata); | ||
- | int bmp_ReadImagePalette(FILE *bmp_image, bmpdata_t *bmpdata); | ||
int bmp_ReadImageData(FILE *bmp_image, bmpdata_t *bmpdata); | int bmp_ReadImageData(FILE *bmp_image, bmpdata_t *bmpdata); | ||
+ | </ | ||
+ | The bitmap library has a single function, __bmp_ReadImage()__, | ||
+ | |||
+ | The reason the function is called in two passes is to ensure that when the header data is retrieved the image does not exceeded any specific size: | ||
+ | |||
+ | * Open file | ||
+ | * Read BMP header | ||
+ | * Check BMP is correct pixel depth and size | ||
+ | * Read pixel data | ||
+ | |||
+ | |||
+ | <code " | ||
+ | FILE *f; | ||
+ | int status; | ||
+ | |||
+ | f = fopen(" | ||
+ | if (f == NULL){ | ||
+ | | ||
+ | } | ||
+ | |||
+ | bmp = (bmpdata_t *) malloc(sizeof(bmpdata_t)); | ||
+ | bmp-> | ||
+ | status = bmp_ReadImageHeader(f, | ||
+ | if (status != 0){ | ||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | if (bmp-> | ||
+ | printf(" | ||
+ | } else ( | ||
+ | printf(" | ||
+ | status = bmp_ReadImageData(f, | ||
+ | ) | ||
+ | |||
+ | fclose(f); | ||
</ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | [[blog: | ||
+ | |||
+ |