diff options
author | Michael Drake <tlsa@netsurf-browser.org> | 2021-11-18 18:46:08 +0000 |
---|---|---|
committer | Michael Drake <tlsa@netsurf-browser.org> | 2021-11-19 12:37:05 +0000 |
commit | e2458d5e968323735a2a836da5f5838e62caf6b6 (patch) | |
tree | b1f7e42804ad36ce722a4e0d5772eae2f8cbb07e | |
parent | 470c70dac58c146ac280f6c7920764e59a885a42 (diff) | |
download | libnsgif-e2458d5e968323735a2a836da5f5838e62caf6b6.tar.gz libnsgif-e2458d5e968323735a2a836da5f5838e62caf6b6.tar.bz2 |
GIF: Split out colour table extraction.
This will allow the local and global colour table extraction to
share code.
-rw-r--r-- | src/libnsgif.c | 69 |
1 files changed, 48 insertions, 21 deletions
diff --git a/src/libnsgif.c b/src/libnsgif.c index 6de26a0..b7b007b 100644 --- a/src/libnsgif.c +++ b/src/libnsgif.c @@ -810,40 +810,32 @@ static gif_result gif__parse_image_descriptor( } /** - * Get a frame's colour table. - * - * Sets up gif->colour_table for the frame. + * Extract a GIF colour table into a LibNSGIF colour table buffer. * - * \param[in] gif The gif object we're decoding. - * \param[in] frame The frame to get the colour table for. + * \param[in] gif The gif object we're decoding. + * \param[in] colour_table The colour table to populate. + * \param[in] colour_table_entries The number of colour table entries. + * \param[in] pos Current position in data, updated on exit. + * \param[in] decode Whether to decode the colour table. * \return GIF_OK on success, appropriate error otherwise. */ -static gif_result gif__parse_colour_table( +static gif_result gif__colour_table_extract( struct gif_animation *gif, - struct gif_frame *frame, + uint32_t *colour_table, + size_t colour_table_entries, const uint8_t **pos, bool decode) { - unsigned colour_table_size; const uint8_t *data = *pos; size_t len = gif->gif_data + gif->buffer_size - data; - assert(gif != NULL); - assert(frame != NULL); - - if ((frame->flags & GIF_COLOUR_TABLE_MASK) == 0) { - gif->colour_table = gif->global_colour_table; - return GIF_OK; - } - - colour_table_size = 2 << (frame->flags & GIF_COLOUR_TABLE_SIZE_MASK); - if (len < colour_table_size * 3) { + if (len < colour_table_entries * 3) { return GIF_INSUFFICIENT_FRAME_DATA; } if (decode) { - int count = colour_table_size; - uint8_t *entry = (uint8_t *)gif->local_colour_table; + int count = colour_table_entries; + uint8_t *entry = (uint8_t *)colour_table; while (count--) { /* Gif colour map contents are r,g,b. @@ -861,8 +853,43 @@ static gif_result gif__parse_colour_table( } } + *pos += colour_table_entries * 3; + return GIF_OK; +} + +/** + * Get a frame's colour table. + * + * Sets up gif->colour_table for the frame. + * + * \param[in] gif The gif object we're decoding. + * \param[in] frame The frame to get the colour table for. + * \return GIF_OK on success, appropriate error otherwise. + */ +static gif_result gif__parse_colour_table( + struct gif_animation *gif, + struct gif_frame *frame, + const uint8_t **pos, + bool decode) +{ + gif_result ret; + + assert(gif != NULL); + assert(frame != NULL); + + if ((frame->flags & GIF_COLOUR_TABLE_MASK) == 0) { + gif->colour_table = gif->global_colour_table; + return GIF_OK; + } + + ret = gif__colour_table_extract(gif, gif->local_colour_table, + 2 << (frame->flags & GIF_COLOUR_TABLE_SIZE_MASK), + pos, decode); + if (ret != GIF_OK) { + return ret; + } + gif->colour_table = gif->local_colour_table; - *pos += colour_table_size * 3; return GIF_OK; } |