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 10:08] – john | blog:x68_launcher_3 [2020/09/07 10: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 <stdint.h> |
| - | bmp.h - C prototypes for the simple Windows BMP file loader | + | |
| - | based on the example at: | + | |
| - | | + | |
| - | | + | #define BMP_FILE_SIG_OFFSET 0x0000 // Should always be ' |
| - | + | #define BMP_FILE_SIZE_OFFSET 0x0002 // Size of file, including headers | |
| - | */ | + | #define DATA_OFFSET_OFFSET |
| - | + | # | |
| - | #define BMP_FILE_SIG_OFFSET 0x0000 // Should always be ' | + | #define HEIGHT_OFFSET |
| - | #define BMP_FILE_SIZE_OFFSET 0x0002 // Size of file, including headers | + | |
| - | #define DATA_OFFSET_OFFSET | + | |
| - | # | + | |
| - | #define DIB_HEADER_SIZE 4 // DIB header size field is 4 bytes | + | |
| - | #define WIDTH_OFFSET 0x0012 // Where we can find the x-axis pixel size | + | |
| - | #define HEIGHT_OFFSET | + | |
| #define BITS_PER_PIXEL_OFFSET 0x001C // Where we can find the bits-per-pixel number | #define BITS_PER_PIXEL_OFFSET 0x001C // Where we can find the bits-per-pixel number | ||
| #define COMPRESS_OFFSET 0x001E // Where we can find the compression flag | #define COMPRESS_OFFSET 0x001E // Where we can find the compression flag | ||
| - | #define COLOUR_NUM_OFFSET 0x002E // Where we can find the numbers of colours used in the image | + | #define COLOUR_NUM_OFFSET 0x002E // Where we can find the numbers of colours used in the image |
| - | #define COLOUR_PRI_OFFSET 0x0032 // Where we can find the number of the ' | + | #define COLOUR_PRI_OFFSET 0x0032 // Where we can find the number of the ' |
| - | #define PALETTE_OFFSET 0x0036 // Where the colour palette starts, for <=8bpp images. | + | #define PALETTE_OFFSET 0x0036 // Where the colour palette starts, for <=8bpp images. |
| - | #define HEADER_SIZE | + | #define HEADER_SIZE |
| - | #define INFO_HEADER_SIZE | + | #define INFO_HEADER_SIZE |
| - | #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 | + | #define BMP_UNCOMPRESSED 0 |
| - | #define BMP_UNCOMPRESSED 0 | + | #define BMP_VERBOSE 0 // Enable BMP specific debug/ |
| - | #define BMP_VERBOSE 0 // Enable BMP specific debug/ | + | #define BMP_OK 0 // BMP loaded and decode okay |
| - | #define BMP_OK 0 // BMP loaded and decode okay | + | #define BMP_ERR_NOFILE -1 // Cannot find file |
| - | #define BMP_ERR_NOFILE -1 // Cannot find file | + | #define BMP_ERR_SIZE -2 // Height/ |
| - | #define BMP_ERR_SIZE -2 // Height/ | + | #define BMP_ERR_MEM -3 // Unable to allocate memory |
| - | #define BMP_ERR_MEM -3 // Unable to allocate memory | + | #define BMP_ERR_BPP -4 // Unsupported colour depth/BPP |
| - | #define BMP_ERR_BPP -4 // Unsupported colour depth/BPP | + | #define BMP_ERR_READ -5 // Error reading or seeking within file |
| - | #define BMP_ERR_READ -5 // Error reading or seeking within file | + | |
| #define BMP_ERR_COMPRESSED -6 // We dont support comrpessed BMP files | #define BMP_ERR_COMPRESSED -6 // We dont support comrpessed BMP files | ||
| - | #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 77: | 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; | + | 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; // Bits per pixel | + | |
| - | unsigned short bytespp; // Bytes per pixel | + | |
| - | unsigned int offset; // Offset from header to data section, in bytes | + | |
| - | unsigned int row_padded; | + | |
| unsigned int row_unpadded; | unsigned int row_unpadded; | ||
| - | unsigned int size; // Size of the pixel data, in bytes | + | unsigned int size; // Size of the pixel data, in bytes |
| - | unsigned int n_pixels; | + | unsigned int n_pixels; |
| - | struct pal_entry palette[256]; | + | uint8_t |
| - | unsigned char *pixels; // Pointer to raw pixels - in font mode each byte is a single | + | } __attribute__((__packed__)) __attribute__((aligned (2))) bmpdata_t; |
| - | } bmpdata_t; | + | |
| // ============================ | // ============================ | ||
| Line 103: | 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 117: | 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; // Total number of 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: | ||
| + | |||
| + | |||