1*803618f4SDamon Ding /* 2*803618f4SDamon Ding * Copyright 2006 Richard Wilson <richard.wilson@netsurf-browser.org> 3*803618f4SDamon Ding * Copyright 2008 Sean Fox <dyntryx@gmail.com> 4*803618f4SDamon Ding * 5*803618f4SDamon Ding * This file is part of NetSurf's libnsbmp, http://www.netsurf-browser.org/ 6*803618f4SDamon Ding * Licenced under the MIT License, 7*803618f4SDamon Ding * http://www.opensource.org/licenses/mit-license.php 8*803618f4SDamon Ding */ 9*803618f4SDamon Ding 10*803618f4SDamon Ding /** 11*803618f4SDamon Ding * \file 12*803618f4SDamon Ding * Bitmap file decoding interface. 13*803618f4SDamon Ding */ 14*803618f4SDamon Ding 15*803618f4SDamon Ding #ifndef _LIBNSBMP_H_ 16*803618f4SDamon Ding #define _LIBNSBMP_H_ 17*803618f4SDamon Ding 18*803618f4SDamon Ding #include <common.h> 19*803618f4SDamon Ding #include <malloc.h> 20*803618f4SDamon Ding #include <linux/types.h> 21*803618f4SDamon Ding 22*803618f4SDamon Ding #ifndef INT32_MAX 23*803618f4SDamon Ding #define INT32_MAX (2147483647) 24*803618f4SDamon Ding #endif 25*803618f4SDamon Ding 26*803618f4SDamon Ding /* bmp flags */ 27*803618f4SDamon Ding #define BMP_NEW 0 28*803618f4SDamon Ding /** image is opaque (as opposed to having an alpha mask) */ 29*803618f4SDamon Ding #define BMP_OPAQUE (1 << 0) 30*803618f4SDamon Ding /** memory should be wiped */ 31*803618f4SDamon Ding #define BMP_CLEAR_MEMORY (1 << 1) 32*803618f4SDamon Ding 33*803618f4SDamon Ding /** 34*803618f4SDamon Ding * error return values 35*803618f4SDamon Ding */ 36*803618f4SDamon Ding typedef enum { 37*803618f4SDamon Ding BMP_OK = 0, 38*803618f4SDamon Ding BMP_INSUFFICIENT_MEMORY = 1, 39*803618f4SDamon Ding BMP_INSUFFICIENT_DATA = 2, 40*803618f4SDamon Ding BMP_DATA_ERROR = 3 41*803618f4SDamon Ding } bmp_result; 42*803618f4SDamon Ding 43*803618f4SDamon Ding /** 44*803618f4SDamon Ding * encoding types 45*803618f4SDamon Ding */ 46*803618f4SDamon Ding typedef enum { 47*803618f4SDamon Ding BMP_ENCODING_RGB = 0, 48*803618f4SDamon Ding BMP_ENCODING_RLE8 = 1, 49*803618f4SDamon Ding BMP_ENCODING_RLE4 = 2, 50*803618f4SDamon Ding BMP_ENCODING_BITFIELDS = 3 51*803618f4SDamon Ding } bmp_encoding; 52*803618f4SDamon Ding 53*803618f4SDamon Ding /* API for Bitmap callbacks */ 54*803618f4SDamon Ding typedef void* (*bmp_bitmap_cb_create)(int width, int height, unsigned int state); 55*803618f4SDamon Ding typedef void (*bmp_bitmap_cb_destroy)(void *bitmap); 56*803618f4SDamon Ding typedef unsigned char* (*bmp_bitmap_cb_get_buffer)(void *bitmap); 57*803618f4SDamon Ding typedef size_t (*bmp_bitmap_cb_get_bpp)(void *bitmap); 58*803618f4SDamon Ding 59*803618f4SDamon Ding /** 60*803618f4SDamon Ding * The Bitmap callbacks function table 61*803618f4SDamon Ding */ 62*803618f4SDamon Ding typedef struct bmp_bitmap_callback_vt_s { 63*803618f4SDamon Ding /** Callback to allocate bitmap storage. */ 64*803618f4SDamon Ding bmp_bitmap_cb_create bitmap_create; 65*803618f4SDamon Ding /** Called to free bitmap storage. */ 66*803618f4SDamon Ding bmp_bitmap_cb_destroy bitmap_destroy; 67*803618f4SDamon Ding /** Return a pointer to the pixel data in a bitmap. */ 68*803618f4SDamon Ding bmp_bitmap_cb_get_buffer bitmap_get_buffer; 69*803618f4SDamon Ding } bmp_bitmap_callback_vt; 70*803618f4SDamon Ding 71*803618f4SDamon Ding /** 72*803618f4SDamon Ding * bitmap image 73*803618f4SDamon Ding */ 74*803618f4SDamon Ding typedef struct bmp_img { 75*803618f4SDamon Ding /** callbacks for bitmap functions */ 76*803618f4SDamon Ding bmp_bitmap_callback_vt bitmap_callbacks; 77*803618f4SDamon Ding /** pointer to BMP data */ 78*803618f4SDamon Ding uint8_t *bmp_data; 79*803618f4SDamon Ding /** width of BMP (valid after _analyse) */ 80*803618f4SDamon Ding uint32_t width; 81*803618f4SDamon Ding /** heigth of BMP (valid after _analyse) */ 82*803618f4SDamon Ding uint32_t height; 83*803618f4SDamon Ding /** whether the image has been decoded */ 84*803618f4SDamon Ding bool decoded; 85*803618f4SDamon Ding /** decoded image */ 86*803618f4SDamon Ding void *bitmap; 87*803618f4SDamon Ding 88*803618f4SDamon Ding /* Internal members are listed below */ 89*803618f4SDamon Ding /** total number of bytes of BMP data available */ 90*803618f4SDamon Ding uint32_t buffer_size; 91*803618f4SDamon Ding /** pixel encoding type */ 92*803618f4SDamon Ding bmp_encoding encoding; 93*803618f4SDamon Ding /** offset of bitmap data */ 94*803618f4SDamon Ding uint32_t bitmap_offset; 95*803618f4SDamon Ding /** bits per pixel */ 96*803618f4SDamon Ding uint16_t bpp; 97*803618f4SDamon Ding /** number of colours */ 98*803618f4SDamon Ding uint32_t colours; 99*803618f4SDamon Ding /** colour table */ 100*803618f4SDamon Ding uint32_t *colour_table; 101*803618f4SDamon Ding /** whether to use bmp's limited transparency */ 102*803618f4SDamon Ding bool limited_trans; 103*803618f4SDamon Ding /** colour to display for "transparent" pixels when using limited 104*803618f4SDamon Ding * transparency 105*803618f4SDamon Ding */ 106*803618f4SDamon Ding uint32_t trans_colour; 107*803618f4SDamon Ding /** scanlines are top to bottom */ 108*803618f4SDamon Ding bool reversed; 109*803618f4SDamon Ding /** image is part of an ICO, mask follows */ 110*803618f4SDamon Ding bool ico; 111*803618f4SDamon Ding /** true if the bitmap does not contain an alpha channel */ 112*803618f4SDamon Ding bool opaque; 113*803618f4SDamon Ding /** four bitwise mask */ 114*803618f4SDamon Ding uint32_t mask[4]; 115*803618f4SDamon Ding /** four bitwise shifts */ 116*803618f4SDamon Ding int32_t shift[4]; 117*803618f4SDamon Ding /** colour representing "transparency" in the bitmap */ 118*803618f4SDamon Ding uint32_t transparent_index; 119*803618f4SDamon Ding } bmp_image; 120*803618f4SDamon Ding 121*803618f4SDamon Ding typedef struct ico_image { 122*803618f4SDamon Ding bmp_image bmp; 123*803618f4SDamon Ding struct ico_image *next; 124*803618f4SDamon Ding } ico_image; 125*803618f4SDamon Ding 126*803618f4SDamon Ding /** 127*803618f4SDamon Ding * icon image collection 128*803618f4SDamon Ding */ 129*803618f4SDamon Ding typedef struct ico_collection { 130*803618f4SDamon Ding /** callbacks for bitmap functions */ 131*803618f4SDamon Ding bmp_bitmap_callback_vt bitmap_callbacks; 132*803618f4SDamon Ding /** width of largest BMP */ 133*803618f4SDamon Ding uint16_t width; 134*803618f4SDamon Ding /** heigth of largest BMP */ 135*803618f4SDamon Ding uint16_t height; 136*803618f4SDamon Ding 137*803618f4SDamon Ding /* Internal members are listed below */ 138*803618f4SDamon Ding /** pointer to ICO data */ 139*803618f4SDamon Ding uint8_t *ico_data; 140*803618f4SDamon Ding /** total number of bytes of ICO data available */ 141*803618f4SDamon Ding uint32_t buffer_size; 142*803618f4SDamon Ding /** root of linked list of images */ 143*803618f4SDamon Ding ico_image *first; 144*803618f4SDamon Ding } ico_collection; 145*803618f4SDamon Ding 146*803618f4SDamon Ding /** 147*803618f4SDamon Ding * Initialises bitmap ready for analysing the bitmap. 148*803618f4SDamon Ding * 149*803618f4SDamon Ding * \param bmp The Bitmap to initialise 150*803618f4SDamon Ding * \param callbacks The callbacks the library will call on operations. 151*803618f4SDamon Ding * \return BMP_OK on success or appropriate error code. 152*803618f4SDamon Ding */ 153*803618f4SDamon Ding bmp_result bmp_create(bmp_image *bmp, bmp_bitmap_callback_vt *callbacks); 154*803618f4SDamon Ding 155*803618f4SDamon Ding /** 156*803618f4SDamon Ding * Initialises icon ready for analysing the icon 157*803618f4SDamon Ding * 158*803618f4SDamon Ding * \param bmp The Bitmap to initialise 159*803618f4SDamon Ding * \param callbacks The callbacks the library will call on operations. 160*803618f4SDamon Ding * \return BMP_OK on success or appropriate error code. 161*803618f4SDamon Ding */ 162*803618f4SDamon Ding bmp_result ico_collection_create(ico_collection *ico, 163*803618f4SDamon Ding bmp_bitmap_callback_vt *callbacks); 164*803618f4SDamon Ding 165*803618f4SDamon Ding /** 166*803618f4SDamon Ding * Analyse a BMP prior to decoding. 167*803618f4SDamon Ding * 168*803618f4SDamon Ding * This will scan the data provided and perform checks to ensure the data is a 169*803618f4SDamon Ding * valid BMP and prepare the bitmap image structure ready for decode. 170*803618f4SDamon Ding * 171*803618f4SDamon Ding * This function must be called and resturn BMP_OK before bmp_decode() as it 172*803618f4SDamon Ding * prepares the bmp internal state for the decode process. 173*803618f4SDamon Ding * 174*803618f4SDamon Ding * \param bmp the BMP image to analyse. 175*803618f4SDamon Ding * \param size The size of data in cdata. 176*803618f4SDamon Ding * \param data The bitmap source data. 177*803618f4SDamon Ding * \return BMP_OK on success or error code on faliure. 178*803618f4SDamon Ding */ 179*803618f4SDamon Ding bmp_result bmp_analyse(bmp_image *bmp, size_t size, uint8_t *data); 180*803618f4SDamon Ding 181*803618f4SDamon Ding /** 182*803618f4SDamon Ding * Analyse an ICO prior to decoding. 183*803618f4SDamon Ding * 184*803618f4SDamon Ding * This function will scan the data provided and perform checks to ensure the 185*803618f4SDamon Ding * data is a valid ICO. 186*803618f4SDamon Ding * 187*803618f4SDamon Ding * This function must be called before ico_find(). 188*803618f4SDamon Ding * 189*803618f4SDamon Ding * \param ico the ICO image to analyse 190*803618f4SDamon Ding * \param size The size of data in cdata. 191*803618f4SDamon Ding * \param data The bitmap source data. 192*803618f4SDamon Ding * \return BMP_OK on success 193*803618f4SDamon Ding */ 194*803618f4SDamon Ding bmp_result ico_analyse(ico_collection *ico, size_t size, uint8_t *data); 195*803618f4SDamon Ding 196*803618f4SDamon Ding /** 197*803618f4SDamon Ding * Decode a BMP 198*803618f4SDamon Ding * 199*803618f4SDamon Ding * This function decodes the BMP data such that bmp->bitmap is a valid 200*803618f4SDamon Ding * image. The state of bmp->decoded is set to TRUE on exit such that it 201*803618f4SDamon Ding * can easily be identified which BMPs are in a fully decoded state. 202*803618f4SDamon Ding * 203*803618f4SDamon Ding * \param bmp the BMP image to decode 204*803618f4SDamon Ding * \return BMP_OK on success 205*803618f4SDamon Ding */ 206*803618f4SDamon Ding bmp_result bmp_decode(bmp_image *bmp); 207*803618f4SDamon Ding 208*803618f4SDamon Ding /** 209*803618f4SDamon Ding * Decode a BMP using "limited transparency" 210*803618f4SDamon Ding * 211*803618f4SDamon Ding * Bitmaps do not have native transparency support. However, there is a 212*803618f4SDamon Ding * "trick" that is used in some instances in which the first pixel of the 213*803618f4SDamon Ding * bitmap becomes the "transparency index". The decoding application can 214*803618f4SDamon Ding * replace this index with whatever background colour it chooses to 215*803618f4SDamon Ding * create the illusion of transparency. 216*803618f4SDamon Ding * 217*803618f4SDamon Ding * When to use transparency is at the discretion of the decoding 218*803618f4SDamon Ding * application. 219*803618f4SDamon Ding * 220*803618f4SDamon Ding * \param bmp the BMP image to decode 221*803618f4SDamon Ding * \param colour the colour to use as "transparent" 222*803618f4SDamon Ding * \return BMP_OK on success 223*803618f4SDamon Ding */ 224*803618f4SDamon Ding bmp_result bmp_decode_trans(bmp_image *bmp, uint32_t transparent_colour); 225*803618f4SDamon Ding 226*803618f4SDamon Ding /** 227*803618f4SDamon Ding * Finds the closest BMP within an ICO collection 228*803618f4SDamon Ding * 229*803618f4SDamon Ding * This function finds the BMP with dimensions as close to a specified set 230*803618f4SDamon Ding * as possible from the images in the collection. 231*803618f4SDamon Ding * 232*803618f4SDamon Ding * \param ico the ICO collection to examine 233*803618f4SDamon Ding * \param width the preferred width (0 to use ICO header width) 234*803618f4SDamon Ding * \param height the preferred height (0 to use ICO header height) 235*803618f4SDamon Ding */ 236*803618f4SDamon Ding bmp_image *ico_find(ico_collection *ico, uint16_t width, uint16_t height); 237*803618f4SDamon Ding 238*803618f4SDamon Ding /** 239*803618f4SDamon Ding * Finalise a BMP prior to destruction. 240*803618f4SDamon Ding * 241*803618f4SDamon Ding * \param bmp the BMP image to finalise. 242*803618f4SDamon Ding */ 243*803618f4SDamon Ding void bmp_finalise(bmp_image *bmp); 244*803618f4SDamon Ding 245*803618f4SDamon Ding /** 246*803618f4SDamon Ding * Finalise an ICO prior to destruction. 247*803618f4SDamon Ding * 248*803618f4SDamon Ding * \param ico the ICO image to finalise, 249*803618f4SDamon Ding */ 250*803618f4SDamon Ding void ico_finalise(ico_collection *ico); 251*803618f4SDamon Ding 252*803618f4SDamon Ding #endif 253