EU1KY AA
libnsbmp.c File Reference
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <libnsbmp.h>

Macros

#define UNUSED(x)   ((x)=(x))
 
#define BMP_FILE_HEADER_SIZE   14
 
#define ICO_FILE_HEADER_SIZE   6
 
#define ICO_DIR_ENTRY_SIZE   16
 

Functions

static int8_t read_int8 (const uint8_t *data, unsigned int o)
 
static uint8_t read_uint8 (const uint8_t *data, unsigned int o)
 
static int16_t read_int16 (const uint8_t *data, unsigned int o)
 
static uint16_t read_uint16 (const uint8_t *data, unsigned int o)
 
static int32_t read_int32 (const uint8_t *data, unsigned int o)
 
static uint32_t read_uint32 (const uint8_t *data, unsigned int o)
 
static bmp_result next_ico_image (ico_collection *ico, ico_image *image)
 
static bmp_result bmp_analyse_header (bmp_image *bmp, unsigned char *data)
 
static bmp_result bmp_decode_rgb24 (bmp_image *bmp, uint8_t **start, int bytes)
 
static bmp_result bmp_decode_rgb16 (bmp_image *bmp, uint8_t **start, int bytes)
 
static bmp_result bmp_decode_rgb (bmp_image *bmp, uint8_t **start, int bytes)
 
static bmp_result bmp_decode_mask (bmp_image *bmp, uint8_t *data, int bytes)
 
static bmp_result bmp_decode_rle (bmp_image *bmp, uint8_t *data, int bytes, int size)
 
void bmp_create (bmp_image *bmp, bmp_bitmap_callback_vt *bitmap_callbacks)
 
void ico_collection_create (ico_collection *ico, bmp_bitmap_callback_vt *bitmap_callbacks)
 
bmp_result bmp_analyse (bmp_image *bmp, size_t size, const uint8_t *cdata)
 
bmp_result ico_analyse (ico_collection *ico, size_t size, const uint8_t *data)
 
static bmp_result bmp_analyse_header (bmp_image *bmp, uint8_t *data)
 
bmp_imageico_find (ico_collection *ico, uint16_t width, uint16_t height)
 
bmp_result bmp_decode (bmp_image *bmp)
 
bmp_result bmp_decode_trans (bmp_image *bmp, uint32_t colour)
 
void bmp_finalise (bmp_image *bmp)
 
void ico_finalise (ico_collection *ico)
 

Macro Definition Documentation

#define BMP_FILE_HEADER_SIZE   14
#define ICO_DIR_ENTRY_SIZE   16
#define ICO_FILE_HEADER_SIZE   6
#define UNUSED (   x)    ((x)=(x))

Function Documentation

bmp_result bmp_analyse ( bmp_image bmp,
size_t  size,
const uint8_t *  cdata 
)

Analyse a BMP prior to decoding.

This function will scan the data provided and perform simple checks to ensure the data is a valid BMP.

This function must be called before bmp_decode() and sets up all the relevant values in the bmp structure.

Parameters
bmpthe BMP image to analyse
Returns
BMP_OK on success
static bmp_result bmp_analyse_header ( bmp_image bmp,
unsigned char *  data 
)
static
static bmp_result bmp_analyse_header ( bmp_image bmp,
uint8_t *  data 
)
static

The bpp value should be in the range 1-32, but the only values considered legal are: RGB ENCODING: 1, 4, 8, 16, 24 and 32

The bpp value should be in the range 1-32, but the only values considered legal are: RGB ENCODING: 1, 4, 8, 16, 24 and 32 RLE4 ENCODING: 4 RLE8 ENCODING: 8 BITFIELD ENCODING: 16 and 32

void bmp_create ( bmp_image bmp,
bmp_bitmap_callback_vt bitmap_callbacks 
)

Initialises necessary bmp_image members.

bmp_result bmp_decode ( bmp_image bmp)

Decode a BMP

This function decodes the BMP data such that bmp->bitmap is a valid image. The state of bmp->decoded is set to TRUE on exit such that it can easily be identified which BMPs are in a fully decoded state.

Parameters
bmpthe BMP image to decode
Returns
BMP_OK on success
static bmp_result bmp_decode_mask ( bmp_image bmp,
uint8_t *  data,
int  bytes 
)
static

Decode a 1bpp mask for an ICO

Parameters
bmpthe BMP image to decode
datathe data to decode
bytesthe number of bytes of data available
Returns
BMP_OK on success
static bmp_result bmp_decode_rgb ( bmp_image bmp,
uint8_t **  start,
int  bytes 
)
static

Decode BMP data stored with a palette and in 8bpp colour or less.

Parameters
bmpthe BMP image to decode
startthe data to decode, updated to last byte read on success
bytesthe number of bytes of data available
Returns
BMP_OK on success BMP_INSUFFICIENT_DATA if the bitmap data ends unexpectedly; in this case, the image may be partially viewable
static bmp_result bmp_decode_rgb16 ( bmp_image bmp,
uint8_t **  start,
int  bytes 
)
static

Decode BMP data stored in 16bpp colour.

Parameters
bmpthe BMP image to decode
startthe data to decode, updated to last byte read on success
bytesthe number of bytes of data available
Returns
BMP_OK on success BMP_INSUFFICIENT_DATA if the bitmap data ends unexpectedly; in this case, the image may be partially viewable
static bmp_result bmp_decode_rgb24 ( bmp_image bmp,
uint8_t **  start,
int  bytes 
)
static

Decode BMP data stored in 24bpp colour.

Parameters
bmpthe BMP image to decode
startthe data to decode, updated to last byte read on success
bytesthe number of bytes of data available
Returns
BMP_OK on success BMP_INSUFFICIENT_DATA if the bitmap data ends unexpectedly; in this case, the image may be partially viewable
static bmp_result bmp_decode_rle ( bmp_image bmp,
uint8_t *  data,
int  bytes,
int  size 
)
static

Decode BMP data stored encoded in either RLE4 or RLE8.

Parameters
bmpthe BMP image to decode
datathe data to decode
bytesthe number of bytes of data available
sizethe size of the RLE tokens (4 or 8)
Returns
BMP_OK on success BMP_INSUFFICIENT_DATA if the bitmap data ends unexpectedly; in this case, the image may be partially viewable
bmp_result bmp_decode_trans ( bmp_image bmp,
uint32_t  colour 
)

Decode a BMP using "limited transparency"

Bitmaps do not have native transparency support. However, there is a "trick" that is used in some instances in which the first pixel of the bitmap becomes the "transparency index". The decoding application can replace this index with whatever background colour it chooses to create the illusion of transparency.

When to use transparency is at the discretion of the decoding application.

Parameters
bmpthe BMP image to decode
colourthe colour to use as "transparent"
Returns
BMP_OK on success
void bmp_finalise ( bmp_image bmp)

Finalise a BMP prior to destruction.

Parameters
bmpthe BMP image to finalise
bmp_result ico_analyse ( ico_collection ico,
size_t  size,
const uint8_t *  data 
)

Analyse an ICO prior to decoding.

This function will scan the data provided and perform simple checks to ensure the data is a valid ICO.

This function must be called before ico_find().

Parameters
icothe ICO image to analyse
Returns
BMP_OK on success
void ico_collection_create ( ico_collection ico,
bmp_bitmap_callback_vt bitmap_callbacks 
)

Initialises necessary ico_collection members.

void ico_finalise ( ico_collection ico)

Finalise an ICO prior to destruction.

Parameters
icothe ICO image to finalise
bmp_image* ico_find ( ico_collection ico,
uint16_t  width,
uint16_t  height 
)

Finds the closest BMP within an ICO collection

This function finds the BMP with dimensions as close to a specified set as possible from the images in the collection.

Parameters
icothe ICO collection to examine
widththe preferred width (0 to use ICO header width)
heightthe preferred height (0 to use ICO header height)
static bmp_result next_ico_image ( ico_collection ico,
ico_image image 
)
static

Allocates memory for the next BMP in an ICO collection

Sets proper structure values

Parameters
icothe ICO collection to add the image to
imagea pointer to the ICO image to be initialised
static int16_t read_int16 ( const uint8_t *  data,
unsigned int  o 
)
inlinestatic
static int32_t read_int32 ( const uint8_t *  data,
unsigned int  o 
)
inlinestatic
static int8_t read_int8 ( const uint8_t *  data,
unsigned int  o 
)
inlinestatic
static uint16_t read_uint16 ( const uint8_t *  data,
unsigned int  o 
)
inlinestatic
static uint32_t read_uint32 ( const uint8_t *  data,
unsigned int  o 
)
inlinestatic
static uint8_t read_uint8 ( const uint8_t *  data,
unsigned int  o 
)
inlinestatic