summaryrefslogtreecommitdiff
path: root/include/libnsbmp.h
blob: ad683f0053291442c0acdb27ea6ce8ebb8d50cf4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
 * Copyright 2006 Richard Wilson <richard.wilson@netsurf-browser.org>
 * Copyright 2008 Sean Fox <dyntryx@gmail.com>
 *
 * This file is part of NetSurf's libnsbmp, http://www.netsurf-browser.org/
 * Licenced under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 */

/**
 * \file
 * Bitmap file decoding interface.
 */

#ifndef libnsbmp_h_
#define libnsbmp_h_

#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>

/* bmp flags */
#define BMP_NEW			0
/** image is opaque (as opposed to having an alpha mask) */
#define BMP_OPAQUE		(1 << 0)
/** memory should be wiped */
#define BMP_CLEAR_MEMORY	(1 << 1)

/**
 * error return values
 */
typedef enum {
        BMP_OK = 0,
        BMP_INSUFFICIENT_MEMORY = 1,
        BMP_INSUFFICIENT_DATA = 2,
        BMP_DATA_ERROR = 3
} bmp_result;

/**
 * encoding types
 */
typedef enum {
        BMP_ENCODING_RGB = 0,
        BMP_ENCODING_RLE8 = 1,
        BMP_ENCODING_RLE4 = 2,
        BMP_ENCODING_BITFIELDS = 3
} bmp_encoding;

/* API for Bitmap callbacks */
typedef void* (*bmp_bitmap_cb_create)(int width, int height, unsigned int state);
typedef void (*bmp_bitmap_cb_destroy)(void *bitmap);
typedef unsigned char* (*bmp_bitmap_cb_get_buffer)(void *bitmap);
typedef size_t (*bmp_bitmap_cb_get_bpp)(void *bitmap);

/**
 * The Bitmap callbacks function table
 */
typedef struct bmp_bitmap_callback_vt_s {
        /** Callback to allocate bitmap storage. */
        bmp_bitmap_cb_create bitmap_create;
        /** Called to free bitmap storage. */
        bmp_bitmap_cb_destroy bitmap_destroy;
        /** Return a pointer to the pixel data in a bitmap. */
        bmp_bitmap_cb_get_buffer bitmap_get_buffer;
} bmp_bitmap_callback_vt;

/**
 * bitmap image
 */
typedef struct bmp_image {
        /** callbacks for bitmap functions */
        bmp_bitmap_callback_vt bitmap_callbacks;
        /** pointer to BMP data */
        uint8_t *bmp_data;
        /** width of BMP (valid after _analyse) */
        uint32_t width;
        /** heigth of BMP (valid after _analyse) */
        uint32_t height;
        /** whether the image has been decoded */
        bool decoded;
        /** decoded image */
        void *bitmap;

        /* Internal members are listed below */
        /** total number of bytes of BMP data available */
        uint32_t buffer_size;
        /** pixel encoding type */
        bmp_encoding encoding;
        /** offset of bitmap data */
        uint32_t bitmap_offset;
        /** bits per pixel */
        uint16_t bpp;
        /** number of colours */
        uint32_t colours;
        /** colour table */
        uint32_t *colour_table;
        /** whether to use bmp's limited transparency */
        bool limited_trans;
        /** colour to display for "transparent" pixels when using limited
         * transparency
         */
        uint32_t trans_colour;
        /** scanlines are top to bottom */
        bool reversed;
        /** image is part of an ICO, mask follows */
        bool ico;
        /** true if the bitmap does not contain an alpha channel */
        bool opaque;
        /** four bitwise mask */
        uint32_t mask[4];
        /** four bitwise shifts */
        int32_t shift[4];
        /** colour representing "transparency" in the bitmap */
        uint32_t transparent_index;
} bmp_image;

typedef struct ico_image {
        bmp_image bmp;
        struct ico_image *next;
} ico_image;

/**
 * icon image collection
 */
typedef struct ico_collection {
        /** callbacks for bitmap functions */
        bmp_bitmap_callback_vt bitmap_callbacks;
        /** width of largest BMP */
        uint16_t width;
        /** heigth of largest BMP */
        uint16_t height;

        /* Internal members are listed below */
        /** pointer to ICO data */
        uint8_t *ico_data;
        /** total number of bytes of ICO data available */
        uint32_t buffer_size;
        /** root of linked list of images */
        ico_image *first;
} ico_collection;

/**
 * Initialises bitmap ready for analysing the bitmap.
 *
 * \param bmp The Bitmap to initialise
 * \param callbacks The callbacks the library will call on operations.
 * \return BMP_OK on success or appropriate error code.
 */
bmp_result bmp_create(bmp_image *bmp, bmp_bitmap_callback_vt *callbacks);

/**
 * Initialises icon ready for analysing the icon
 *
 * \param bmp The Bitmap to initialise
 * \param callbacks The callbacks the library will call on operations.
 * \return BMP_OK on success or appropriate error code.
 */
bmp_result ico_collection_create(ico_collection *ico,
                                 bmp_bitmap_callback_vt *callbacks);

/**
 * Analyse a BMP prior to decoding.
 *
 * This will scan the data provided and perform checks to ensure the data is a
 * valid BMP and prepare the bitmap image structure ready for decode.
 *
 * This function must be called and resturn BMP_OK before bmp_decode() as it
 * prepares the bmp internal state for the decode process.
 *
 * \param bmp the BMP image to analyse.
 * \param size The size of data in cdata.
 * \param data The bitmap source data.
 * \return BMP_OK on success or error code on faliure.
 */
bmp_result bmp_analyse(bmp_image *bmp, size_t size, uint8_t *data);

/**
 * Analyse an ICO prior to decoding.
 *
 * This function will scan the data provided and perform checks to ensure the
 * data is a valid ICO.
 *
 * This function must be called before ico_find().
 *
 * \param ico the ICO image to analyse
 * \param size The size of data in cdata.
 * \param data The bitmap source data.
 * \return BMP_OK on success
 */
bmp_result ico_analyse(ico_collection *ico, size_t size, uint8_t *data);

/**
 * 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.
 *
 * \param bmp the BMP image to decode
 * \return BMP_OK on success
 */
bmp_result bmp_decode(bmp_image *bmp);

/**
 * 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.
 *
 * \param bmp the BMP image to decode
 * \param colour the colour to use as "transparent"
 * \return BMP_OK on success
 */
bmp_result bmp_decode_trans(bmp_image *bmp, uint32_t transparent_colour);

/**
 * 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.
 *
 * \param ico the ICO collection to examine
 * \param width the preferred width (0 to use ICO header width)
 * \param height the preferred height (0 to use ICO header height)
 */
bmp_image *ico_find(ico_collection *ico, uint16_t width, uint16_t height);

/**
 * Finalise a BMP prior to destruction.
 *
 * \param bmp the BMP image to finalise.
 */
void bmp_finalise(bmp_image *bmp);

/**
 * Finalise an ICO prior to destruction.
 *
 * \param ico the ICO image to finalise,
 */
void ico_finalise(ico_collection *ico);

#endif