[gegl] libs: improvements to animated gif handling
- From: Øyvind "pippin" Kolås <ok src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gegl] libs: improvements to animated gif handling
- Date: Wed, 19 Sep 2018 12:09:37 +0000 (UTC)
commit 55999d4a06617791634140820dee58f886cfc9ac
Author: Øyvind Kolås <pippin gimp org>
Date: Wed Sep 19 12:41:01 2018 +0200
libs: improvements to animated gif handling
Deal with frame delays and automatically loop in the gegl commandline
image viewer. A copy of libnsgif now lives in the libs folder, with
minimal modifications from upstream.
bin/ui.c | 50 +-
libs/libnsgif/COPYING | 20 +
libs/libnsgif/README | 36 ++
libs/libnsgif/libnsgif.c | 1169 +++++++++++++++++++++++++++++++++++
libs/libnsgif/libnsgif.h | 183 ++++++
libs/libnsgif/libnsgif.pc.in | 10 +
libs/libnsgif/log.h | 21 +
libs/libnsgif/lzw.c | 377 ++++++++++++
libs/libnsgif/lzw.h | 105 ++++
operations/workshop/gif-load.c | 7 +-
operations/workshop/libnsgif.c | 1308 ----------------------------------------
operations/workshop/libnsgif.h | 108 ----
12 files changed, 1968 insertions(+), 1426 deletions(-)
---
diff --git a/bin/ui.c b/bin/ui.c
index fd3e3073c..3132f3c28 100644
--- a/bin/ui.c
+++ b/bin/ui.c
@@ -94,6 +94,7 @@ struct _State {
int is_video;
int frame_no;
int prev_frame_played;
+ double prev_ms;
};
typedef struct ActionData {
@@ -990,13 +991,6 @@ static void gegl_ui (Mrg *mrg, void *data)
{
State *o = data;
- if (o->is_video)
- {
- o->frame_no++;
- fprintf (stderr, "\r%i", o->frame_no); /* */
- gegl_node_set (o->load, "frame", o->frame_no, NULL);
- mrg_queue_draw (o->mrg, NULL);
- }
mrg_gegl_blit (mrg,
0, 0,
@@ -1006,6 +1000,35 @@ static void gegl_ui (Mrg *mrg, void *data)
o->scale,
o->render_quality);
+ if (g_str_has_suffix (o->path, ".gif") ||
+ g_str_has_suffix (o->path, ".GIF"))
+ {
+ int frames = 0;
+ int frame_delay = 0;
+ gegl_node_get (o->load, "frames", &frames, "frame-delay", &frame_delay, NULL);
+ if (o->prev_ms + frame_delay < mrg_ms (mrg))
+ {
+ o->frame_no++;
+ fprintf (stderr, "\r%i/%i", o->frame_no, frames); /* */
+ if (o->frame_no >= frames)
+ o->frame_no = 0;
+ gegl_node_set (o->load, "frame", o->frame_no, NULL);
+ o->prev_ms = mrg_ms (mrg);
+ }
+ mrg_queue_draw (o->mrg, NULL);
+ }
+ else if (o->is_video)
+ {
+ int frames = 0;
+ o->frame_no++;
+ gegl_node_get (o->load, "frames", &frames, NULL);
+ fprintf (stderr, "\r%i/%i", o->frame_no, frames); /* */
+ if (o->frame_no >= frames)
+ o->frame_no = 0;
+ gegl_node_set (o->load, "frame", o->frame_no, NULL);
+ mrg_queue_draw (o->mrg, NULL);
+ }
+
if (o->is_video)
{
GeglAudioFragment *audio = NULL;
@@ -1203,7 +1226,18 @@ static void load_path (State *o)
o->frame_no = 0;
o->prev_frame_played = 0;
- if (gegl_str_has_video_suffix (path))
+ if (g_str_has_suffix (path, ".gif"))
+ {
+ o->gegl = gegl_node_new ();
+ o->sink = gegl_node_new_child (o->gegl,
+ "operation", "gegl:nop", NULL);
+ o->source = gegl_node_new_child (o->gegl,
+ "operation", "gegl:nop", NULL);
+ o->load = gegl_node_new_child (o->gegl,
+ "operation", "gegl:gif-load", "path", path, "frame", o->frame_no, NULL);
+ gegl_node_link_many (o->load, o->source, o->sink, NULL);
+ }
+ else if (gegl_str_has_video_suffix (path))
{
o->is_video = 1;
o->gegl = gegl_node_new ();
diff --git a/libs/libnsgif/COPYING b/libs/libnsgif/COPYING
new file mode 100644
index 000000000..c6e1688d7
--- /dev/null
+++ b/libs/libnsgif/COPYING
@@ -0,0 +1,20 @@
+Copyright (C) 2004 Richard Wilson
+Copyright (C) 2008 Sean Fox
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+ * The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
diff --git a/libs/libnsgif/README b/libs/libnsgif/README
new file mode 100644
index 000000000..498ee4663
--- /dev/null
+++ b/libs/libnsgif/README
@@ -0,0 +1,36 @@
+libnsgif - Decoding GIF files
+=============================
+
+The functions provided by this library allow for efficient progressive
+GIF decoding. Whilst the initialisation does not ensure that there is
+sufficient image data to complete the entire frame, it does ensure
+that the information provided is valid. Any subsequent attempts to
+decode an initialised GIF are guaranteed to succeed, and any bytes of
+the image not present are assumed to be totally transparent.
+
+To begin decoding a GIF, the 'gif' structure must be initialised with
+the 'gif_data' and 'buffer_size' set to their initial values. The
+'buffer_position' should initially be 0, and will be internally
+updated as the decoding commences. The caller should then repeatedly
+call gif_initialise() with the structure until the function returns 1,
+or no more data is avaliable.
+
+Once the initialisation has begun, the decoder completes the variables
+'frame_count' and 'frame_count_partial'. The former being the total
+number of frames that have been successfully initialised, and the
+latter being the number of frames that a partial amount of data is
+available for. This assists the caller in managing the animation
+whilst decoding is continuing.
+
+To decode a frame, the caller must use gif_decode_frame() which
+updates the current 'frame_image' to reflect the desired frame. The
+required 'disposal_method' is also updated to reflect how the frame
+should be plotted. The caller must not assume that the current
+'frame_image' will be valid between calls if initialisation is still
+occuring, and should either always request that the frame is decoded
+(no processing will occur if the 'decoded_frame' has not been
+invalidated by initialisation) or perform the check itself.
+
+It should be noted that gif_finalise() should always be called, even
+if no frames were initialised. Additionally, it is the responsibility
+of the caller to free 'gif_data'.
diff --git a/libs/libnsgif/libnsgif.c b/libs/libnsgif/libnsgif.c
new file mode 100644
index 000000000..7865c1471
--- /dev/null
+++ b/libs/libnsgif/libnsgif.c
@@ -0,0 +1,1169 @@
+/*
+ * Copyright 2004 Richard Wilson <richard wilson netsurf-browser org>
+ * Copyright 2008 Sean Fox <dyntryx gmail com>
+ *
+ * This file is part of NetSurf's libnsgif, http://www.netsurf-browser.org/
+ * Licenced under the MIT License,
+ * http://www.opensource.org/licenses/mit-license.php
+ */
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <assert.h>
+#include "libnsgif.h"
+#include "log.h"
+
+#include "lzw.h"
+
+/**
+ *
+ * \file
+ * \brief GIF image decoder
+ *
+ * The GIF format is thoroughly documented; a full description can be found at
+ * http://www.w3.org/Graphics/GIF/spec-gif89a.txt
+ *
+ * \todo Plain text and comment extensions should be implemented.
+ */
+
+
+/** Maximum colour table size */
+#define GIF_MAX_COLOURS 256
+
+/** Internal flag that the colour table needs to be processed */
+#define GIF_PROCESS_COLOURS 0xaa000000
+
+/** Internal flag that a frame is invalid/unprocessed */
+#define GIF_INVALID_FRAME -1
+
+/** Transparent colour */
+#define GIF_TRANSPARENT_COLOUR 0x00
+
+/* GIF Flags */
+#define GIF_FRAME_COMBINE 1
+#define GIF_FRAME_CLEAR 2
+#define GIF_FRAME_RESTORE 3
+#define GIF_FRAME_QUIRKS_RESTORE 4
+
+#define GIF_IMAGE_SEPARATOR 0x2c
+#define GIF_INTERLACE_MASK 0x40
+#define GIF_COLOUR_TABLE_MASK 0x80
+#define GIF_COLOUR_TABLE_SIZE_MASK 0x07
+#define GIF_EXTENSION_INTRODUCER 0x21
+#define GIF_EXTENSION_GRAPHIC_CONTROL 0xf9
+#define GIF_DISPOSAL_MASK 0x1c
+#define GIF_TRANSPARENCY_MASK 0x01
+#define GIF_EXTENSION_COMMENT 0xfe
+#define GIF_EXTENSION_PLAIN_TEXT 0x01
+#define GIF_EXTENSION_APPLICATION 0xff
+#define GIF_BLOCK_TERMINATOR 0x00
+#define GIF_TRAILER 0x3b
+
+/** standard GIF header size */
+#define GIF_STANDARD_HEADER_SIZE 13
+
+
+/**
+ * Updates the sprite memory size
+ *
+ * \param gif The animation context
+ * \param width The width of the sprite
+ * \param height The height of the sprite
+ * \return GIF_INSUFFICIENT_MEMORY for a memory error GIF_OK for success
+ */
+static gif_result
+gif_initialise_sprite(gif_animation *gif,
+ unsigned int width,
+ unsigned int height)
+{
+ unsigned int max_width;
+ unsigned int max_height;
+ struct bitmap *buffer;
+
+ /* Check if we've changed */
+ if ((width <= gif->width) && (height <= gif->height)) {
+ return GIF_OK;
+ }
+
+ /* Get our maximum values */
+ max_width = (width > gif->width) ? width : gif->width;
+ max_height = (height > gif->height) ? height : gif->height;
+
+ /* Allocate some more memory */
+ assert(gif->bitmap_callbacks.bitmap_create);
+ buffer = gif->bitmap_callbacks.bitmap_create(max_width, max_height);
+ if (buffer == NULL) {
+ return GIF_INSUFFICIENT_MEMORY;
+ }
+
+ assert(gif->bitmap_callbacks.bitmap_destroy);
+ gif->bitmap_callbacks.bitmap_destroy(gif->frame_image);
+ gif->frame_image = buffer;
+ gif->width = max_width;
+ gif->height = max_height;
+
+ /* Invalidate our currently decoded image */
+ gif->decoded_frame = GIF_INVALID_FRAME;
+ return GIF_OK;
+}
+
+
+/**
+ * Attempts to initialise the frame's extensions
+ *
+ * \param gif The animation context
+ * \param frame The frame number
+ * @return GIF_INSUFFICIENT_FRAME_DATA for insufficient data to complete the
+ * frame GIF_OK for successful initialisation.
+ */
+static gif_result
+gif_initialise_frame_extensions(gif_animation *gif, const int frame)
+{
+ unsigned char *gif_data, *gif_end;
+ int gif_bytes;
+ unsigned int block_size;
+
+ /* Get our buffer position etc. */
+ gif_data = (unsigned char *)(gif->gif_data + gif->buffer_position);
+ gif_end = (unsigned char *)(gif->gif_data + gif->buffer_size);
+
+ /* Initialise the extensions */
+ while (gif_data < gif_end && gif_data[0] == GIF_EXTENSION_INTRODUCER) {
+ ++gif_data;
+ if ((gif_bytes = (gif_end - gif_data)) < 1) {
+ return GIF_INSUFFICIENT_FRAME_DATA;
+ }
+
+ /* Switch on extension label */
+ switch (gif_data[0]) {
+ case GIF_EXTENSION_GRAPHIC_CONTROL:
+ /* 6-byte Graphic Control Extension is:
+ *
+ * +0 CHAR Graphic Control Label
+ * +1 CHAR Block Size
+ * +2 CHAR __Packed Fields__
+ * 3BITS Reserved
+ * 3BITS Disposal Method
+ * 1BIT User Input Flag
+ * 1BIT Transparent Color Flag
+ * +3 SHORT Delay Time
+ * +5 CHAR Transparent Color Index
+ */
+ if (gif_bytes < 6) {
+ return GIF_INSUFFICIENT_FRAME_DATA;
+ }
+
+ gif->frames[frame].frame_delay = gif_data[3] | (gif_data[4] << 8);
+ if (gif_data[2] & GIF_TRANSPARENCY_MASK) {
+ gif->frames[frame].transparency = true;
+ gif->frames[frame].transparency_index = gif_data[5];
+ }
+ gif->frames[frame].disposal_method = ((gif_data[2] & GIF_DISPOSAL_MASK) >> 2);
+ /* I have encountered documentation and GIFs in the
+ * wild that use 0x04 to restore the previous frame,
+ * rather than the officially documented 0x03. I
+ * believe some (older?) software may even actually
+ * export this way. We handle this as a type of
+ * "quirks" mode.
+ */
+ if (gif->frames[frame].disposal_method == GIF_FRAME_QUIRKS_RESTORE) {
+ gif->frames[frame].disposal_method = GIF_FRAME_RESTORE;
+ }
+ gif_data += (2 + gif_data[1]);
+ break;
+
+ case GIF_EXTENSION_APPLICATION:
+ /* 14-byte+ Application Extension is:
+ *
+ * +0 CHAR Application Extension Label
+ * +1 CHAR Block Size
+ * +2 8CHARS Application Identifier
+ * +10 3CHARS Appl. Authentication Code
+ * +13 1-256 Application Data (Data sub-blocks)
+ */
+ if (gif_bytes < 17) {
+ return GIF_INSUFFICIENT_FRAME_DATA;
+ }
+ if ((gif_data[1] == 0x0b) &&
+ (strncmp((const char *) gif_data + 2,
+ "NETSCAPE2.0", 11) == 0) &&
+ (gif_data[13] == 0x03) &&
+ (gif_data[14] == 0x01)) {
+ gif->loop_count = gif_data[15] | (gif_data[16] << 8);
+ }
+ gif_data += (2 + gif_data[1]);
+ break;
+
+ case GIF_EXTENSION_COMMENT:
+ /* Move the pointer to the first data sub-block Skip 1
+ * byte for the extension label
+ */
+ ++gif_data;
+ break;
+
+ default:
+ /* Move the pointer to the first data sub-block Skip 2
+ * bytes for the extension label and size fields Skip
+ * the extension size itself
+ */
+ if (gif_bytes < 2) {
+ return GIF_INSUFFICIENT_FRAME_DATA;
+ }
+ gif_data += (2 + gif_data[1]);
+ }
+
+ /* Repeatedly skip blocks until we get a zero block or run out
+ * of data This data is ignored by this gif decoder
+ */
+ gif_bytes = (gif_end - gif_data);
+ block_size = 0;
+ while (gif_data < gif_end && gif_data[0] != GIF_BLOCK_TERMINATOR) {
+ block_size = gif_data[0] + 1;
+ if ((gif_bytes -= block_size) < 0) {
+ return GIF_INSUFFICIENT_FRAME_DATA;
+ }
+ gif_data += block_size;
+ }
+ ++gif_data;
+ }
+
+ /* Set buffer position and return */
+ gif->buffer_position = (gif_data - gif->gif_data);
+ return GIF_OK;
+}
+
+
+/**
+ * Attempts to initialise the next frame
+ *
+ * \param gif The animation context
+ * \return error code
+ * - GIF_INSUFFICIENT_DATA for insufficient data to do anything
+ * - GIF_FRAME_DATA_ERROR for GIF frame data error
+ * - GIF_INSUFFICIENT_MEMORY for insufficient memory to process
+ * - GIF_INSUFFICIENT_FRAME_DATA for insufficient data to complete the frame
+ * - GIF_DATA_ERROR for GIF error (invalid frame header)
+ * - GIF_OK for successful decoding
+ * - GIF_WORKING for successful decoding if more frames are expected
+*/
+static gif_result gif_initialise_frame(gif_animation *gif)
+{
+ int frame;
+ gif_frame *temp_buf;
+
+ unsigned char *gif_data, *gif_end;
+ int gif_bytes;
+ unsigned int flags = 0;
+ unsigned int width, height, offset_x, offset_y;
+ unsigned int block_size, colour_table_size;
+ bool first_image = true;
+ gif_result return_value;
+
+ /* Get the frame to decode and our data position */
+ frame = gif->frame_count;
+
+ /* Get our buffer position etc. */
+ gif_data = (unsigned char *)(gif->gif_data + gif->buffer_position);
+ gif_end = (unsigned char *)(gif->gif_data + gif->buffer_size);
+ gif_bytes = (gif_end - gif_data);
+
+ /* Check if we've finished */
+ if ((gif_bytes > 0) && (gif_data[0] == GIF_TRAILER)) {
+ return GIF_OK;
+ }
+
+ /* Check if there is enough data remaining. The shortest block of data
+ * is a 4-byte comment extension + 1-byte block terminator + 1-byte gif
+ * trailer
+ */
+ if (gif_bytes < 6) {
+ return GIF_INSUFFICIENT_DATA;
+ }
+
+ /* We could theoretically get some junk data that gives us millions of
+ * frames, so we ensure that we don't have a silly number
+ */
+ if (frame > 4096) {
+ return GIF_FRAME_DATA_ERROR;
+ }
+
+ /* Get some memory to store our pointers in etc. */
+ if ((int)gif->frame_holders <= frame) {
+ /* Allocate more memory */
+ temp_buf = (gif_frame *)realloc(gif->frames, (frame + 1) * sizeof(gif_frame));
+ if (temp_buf == NULL) {
+ return GIF_INSUFFICIENT_MEMORY;
+ }
+ gif->frames = temp_buf;
+ gif->frame_holders = frame + 1;
+ }
+
+ /* Store our frame pointer. We would do it when allocating except we
+ * start off with one frame allocated so we can always use realloc.
+ */
+ gif->frames[frame].frame_pointer = gif->buffer_position;
+ gif->frames[frame].display = false;
+ gif->frames[frame].virgin = true;
+ gif->frames[frame].disposal_method = 0;
+ gif->frames[frame].transparency = false;
+ gif->frames[frame].frame_delay = 100;
+ gif->frames[frame].redraw_required = false;
+
+ /* Invalidate any previous decoding we have of this frame */
+ if (gif->decoded_frame == frame) {
+ gif->decoded_frame = GIF_INVALID_FRAME;
+ }
+
+ /* We pretend to initialise the frames, but really we just skip over
+ * all the data contained within. This is all basically a cut down
+ * version of gif_decode_frame that doesn't have any of the LZW bits in
+ * it.
+ */
+
+ /* Initialise any extensions */
+ gif->buffer_position = gif_data - gif->gif_data;
+ return_value = gif_initialise_frame_extensions(gif, frame);
+ if (return_value != GIF_OK) {
+ return return_value;
+ }
+ gif_data = (gif->gif_data + gif->buffer_position);
+ gif_bytes = (gif_end - gif_data);
+
+ /* Check if we've finished */
+ if ((gif_bytes = (gif_end - gif_data)) < 1) {
+ return GIF_INSUFFICIENT_FRAME_DATA;
+ }
+
+ if (gif_data[0] == GIF_TRAILER) {
+ gif->buffer_position = (gif_data - gif->gif_data);
+ gif->frame_count = frame + 1;
+ return GIF_OK;
+ }
+
+ /* If we're not done, there should be an image descriptor */
+ if (gif_data[0] != GIF_IMAGE_SEPARATOR) {
+ return GIF_FRAME_DATA_ERROR;
+ }
+
+ /* Do some simple boundary checking */
+ if (gif_bytes < 10) {
+ return GIF_INSUFFICIENT_FRAME_DATA;
+ }
+ offset_x = gif_data[1] | (gif_data[2] << 8);
+ offset_y = gif_data[3] | (gif_data[4] << 8);
+ width = gif_data[5] | (gif_data[6] << 8);
+ height = gif_data[7] | (gif_data[8] << 8);
+
+ /* Set up the redraw characteristics. We have to check for extending
+ * the area due to multi-image frames.
+ */
+ if (!first_image) {
+ if (gif->frames[frame].redraw_x > offset_x) {
+ gif->frames[frame].redraw_width += (gif->frames[frame].redraw_x - offset_x);
+ gif->frames[frame].redraw_x = offset_x;
+ }
+
+ if (gif->frames[frame].redraw_y > offset_y) {
+ gif->frames[frame].redraw_height += (gif->frames[frame].redraw_y - offset_y);
+ gif->frames[frame].redraw_y = offset_y;
+ }
+
+ if ((offset_x + width) > (gif->frames[frame].redraw_x + gif->frames[frame].redraw_width)) {
+ gif->frames[frame].redraw_width = (offset_x + width) - gif->frames[frame].redraw_x;
+ }
+
+ if ((offset_y + height) > (gif->frames[frame].redraw_y + gif->frames[frame].redraw_height)) {
+ gif->frames[frame].redraw_height = (offset_y + height) - gif->frames[frame].redraw_y;
+ }
+ } else {
+ first_image = false;
+ gif->frames[frame].redraw_x = offset_x;
+ gif->frames[frame].redraw_y = offset_y;
+ gif->frames[frame].redraw_width = width;
+ gif->frames[frame].redraw_height = height;
+ }
+
+ /* if we are clearing the background then we need to redraw enough to
+ * cover the previous frame too
+ */
+ gif->frames[frame].redraw_required = ((gif->frames[frame].disposal_method == GIF_FRAME_CLEAR) ||
+ (gif->frames[frame].disposal_method == GIF_FRAME_RESTORE));
+
+ /* Boundary checking - shouldn't ever happen except with junk data */
+ if (gif_initialise_sprite(gif, (offset_x + width), (offset_y + height))) {
+ return GIF_INSUFFICIENT_MEMORY;
+ }
+
+ /* Decode the flags */
+ flags = gif_data[9];
+ colour_table_size = 2 << (flags & GIF_COLOUR_TABLE_SIZE_MASK);
+
+ /* Move our data onwards and remember we've got a bit of this frame */
+ gif_data += 10;
+ gif_bytes = (gif_end - gif_data);
+ gif->frame_count_partial = frame + 1;
+
+ /* Skip the local colour table */
+ if (flags & GIF_COLOUR_TABLE_MASK) {
+ gif_data += 3 * colour_table_size;
+ if ((gif_bytes = (gif_end - gif_data)) < 0) {
+ return GIF_INSUFFICIENT_FRAME_DATA;
+ }
+ }
+
+ /* Ensure we have a correct code size */
+ if (gif_bytes < 1) {
+ return GIF_INSUFFICIENT_FRAME_DATA;
+ }
+ if (gif_data[0] > LZW_CODE_MAX) {
+ return GIF_DATA_ERROR;
+ }
+
+ /* Move our pointer to the actual image data */
+ gif_data++;
+ --gif_bytes;
+
+ /* Repeatedly skip blocks until we get a zero block or run out of data
+ * These blocks of image data are processed later by gif_decode_frame()
+ */
+ block_size = 0;
+ while (block_size != 1) {
+ if (gif_bytes < 1) return GIF_INSUFFICIENT_FRAME_DATA;
+ block_size = gif_data[0] + 1;
+ /* Check if the frame data runs off the end of the file */
+ if ((int)(gif_bytes - block_size) < 0) {
+ /* Try to recover by signaling the end of the gif.
+ * Once we get garbage data, there is no logical way to
+ * determine where the next frame is. It's probably
+ * better to partially load the gif than not at all.
+ */
+ if (gif_bytes >= 2) {
+ gif_data[0] = 0;
+ gif_data[1] = GIF_TRAILER;
+ gif_bytes = 1;
+ ++gif_data;
+ break;
+ } else {
+ return GIF_INSUFFICIENT_FRAME_DATA;
+ }
+ } else {
+ gif_bytes -= block_size;
+ gif_data += block_size;
+ }
+ }
+
+ /* Add the frame and set the display flag */
+ gif->buffer_position = gif_data - gif->gif_data;
+ gif->frame_count = frame + 1;
+ gif->frames[frame].display = true;
+
+ /* Check if we've finished */
+ if (gif_bytes < 1) {
+ return GIF_INSUFFICIENT_FRAME_DATA;
+ } else {
+ if (gif_data[0] == GIF_TRAILER) {
+ return GIF_OK;
+ }
+ }
+ return GIF_WORKING;
+}
+
+
+
+
+/**
+ * Skips the frame's extensions (which have been previously initialised)
+ *
+ * \param gif The animation context
+ * \return GIF_INSUFFICIENT_FRAME_DATA for insufficient data to complete the
+ * frame GIF_OK for successful decoding
+ */
+static gif_result gif_skip_frame_extensions(gif_animation *gif)
+{
+ unsigned char *gif_data, *gif_end;
+ int gif_bytes;
+ unsigned int block_size;
+
+ /* Get our buffer position etc. */
+ gif_data = (unsigned char *)(gif->gif_data + gif->buffer_position);
+ gif_end = (unsigned char *)(gif->gif_data + gif->buffer_size);
+ gif_bytes = (gif_end - gif_data);
+
+ /* Skip the extensions */
+ while (gif_data < gif_end && gif_data[0] == GIF_EXTENSION_INTRODUCER) {
+ ++gif_data;
+ if (gif_data >= gif_end) {
+ return GIF_INSUFFICIENT_FRAME_DATA;
+ }
+
+ /* Switch on extension label */
+ switch(gif_data[0]) {
+ case GIF_EXTENSION_COMMENT:
+ /* Move the pointer to the first data sub-block
+ * 1 byte for the extension label
+ */
+ ++gif_data;
+ break;
+
+ default:
+ /* Move the pointer to the first data sub-block 2 bytes
+ * for the extension label and size fields Skip the
+ * extension size itself
+ */
+ if (gif_data + 1 >= gif_end) {
+ return GIF_INSUFFICIENT_FRAME_DATA;
+ }
+ gif_data += (2 + gif_data[1]);
+ }
+
+ /* Repeatedly skip blocks until we get a zero block or run out
+ * of data This data is ignored by this gif decoder
+ */
+ gif_bytes = (gif_end - gif_data);
+ block_size = 0;
+ while (gif_data < gif_end && gif_data[0] != GIF_BLOCK_TERMINATOR) {
+ block_size = gif_data[0] + 1;
+ if ((gif_bytes -= block_size) < 0) {
+ return GIF_INSUFFICIENT_FRAME_DATA;
+ }
+ gif_data += block_size;
+ }
+ ++gif_data;
+ }
+
+ /* Set buffer position and return */
+ gif->buffer_position = (gif_data - gif->gif_data);
+ return GIF_OK;
+}
+
+static unsigned int gif_interlaced_line(int height, int y) {
+ if ((y << 3) < height) {
+ return (y << 3);
+ }
+ y -= ((height + 7) >> 3);
+ if ((y << 3) < (height - 4)) {
+ return (y << 3) + 4;
+ }
+ y -= ((height + 3) >> 3);
+ if ((y << 2) < (height - 2)) {
+ return (y << 2) + 2;
+ }
+ y -= ((height + 1) >> 2);
+ return (y << 1) + 1;
+}
+
+
+static gif_result gif_error_from_lzw(lzw_result l_res)
+{
+ static const gif_result g_res[] = {
+ [LZW_OK] = GIF_OK,
+ [LZW_OK_EOD] = GIF_END_OF_FRAME,
+ [LZW_NO_MEM] = GIF_INSUFFICIENT_MEMORY,
+ [LZW_NO_DATA] = GIF_INSUFFICIENT_FRAME_DATA,
+ [LZW_EOI_CODE] = GIF_FRAME_DATA_ERROR,
+ [LZW_BAD_ICODE] = GIF_FRAME_DATA_ERROR,
+ [LZW_BAD_CODE] = GIF_FRAME_DATA_ERROR,
+ };
+ return g_res[l_res];
+}
+
+
+/**
+ * decode a gif frame
+ *
+ * \param gif gif animation context.
+ * \param frame The frame number to decode.
+ * \param clear_image flag for image data being cleared instead of plotted.
+ */
+static gif_result
+gif_internal_decode_frame(gif_animation *gif,
+ unsigned int frame,
+ bool clear_image)
+{
+ unsigned int index = 0;
+ unsigned char *gif_data, *gif_end;
+ int gif_bytes;
+ unsigned int width, height, offset_x, offset_y;
+ unsigned int flags, colour_table_size, interlace;
+ unsigned int *colour_table;
+ unsigned int *frame_data = 0; // Set to 0 for no warnings
+ unsigned int *frame_scanline;
+ unsigned int save_buffer_position;
+ unsigned int return_value = 0;
+ unsigned int x, y, decode_y, burst_bytes;
+ register unsigned char colour;
+
+ /* Ensure this frame is supposed to be decoded */
+ if (gif->frames[frame].display == false) {
+ return GIF_OK;
+ }
+
+ /* Ensure the frame is in range to decode */
+ if (frame > gif->frame_count_partial) {
+ return GIF_INSUFFICIENT_DATA;
+ }
+
+ /* done if frame is already decoded */
+ if ((!clear_image) &&
+ ((int)frame == gif->decoded_frame)) {
+ return GIF_OK;
+ }
+
+ /* Get the start of our frame data and the end of the GIF data */
+ gif_data = gif->gif_data + gif->frames[frame].frame_pointer;
+ gif_end = gif->gif_data + gif->buffer_size;
+ gif_bytes = (gif_end - gif_data);
+
+ /*
+ * Ensure there is a minimal amount of data to proceed. The shortest
+ * block of data is a 10-byte image descriptor + 1-byte gif trailer
+ */
+ if (gif_bytes < 12) {
+ return GIF_INSUFFICIENT_FRAME_DATA;
+ }
+
+ /* Save the buffer position */
+ save_buffer_position = gif->buffer_position;
+ gif->buffer_position = gif_data - gif->gif_data;
+
+ /* Skip any extensions because they have allready been processed */
+ if ((return_value = gif_skip_frame_extensions(gif)) != GIF_OK) {
+ goto gif_decode_frame_exit;
+ }
+ gif_data = (gif->gif_data + gif->buffer_position);
+ gif_bytes = (gif_end - gif_data);
+
+ /* Ensure we have enough data for the 10-byte image descriptor + 1-byte
+ * gif trailer
+ */
+ if (gif_bytes < 12) {
+ return_value = GIF_INSUFFICIENT_FRAME_DATA;
+ goto gif_decode_frame_exit;
+ }
+
+ /* 10-byte Image Descriptor is:
+ *
+ * +0 CHAR Image Separator (0x2c)
+ * +1 SHORT Image Left Position
+ * +3 SHORT Image Top Position
+ * +5 SHORT Width
+ * +7 SHORT Height
+ * +9 CHAR __Packed Fields__
+ * 1BIT Local Colour Table Flag
+ * 1BIT Interlace Flag
+ * 1BIT Sort Flag
+ * 2BITS Reserved
+ * 3BITS Size of Local Colour Table
+ */
+ if (gif_data[0] != GIF_IMAGE_SEPARATOR) {
+ return_value = GIF_DATA_ERROR;
+ goto gif_decode_frame_exit;
+ }
+ offset_x = gif_data[1] | (gif_data[2] << 8);
+ offset_y = gif_data[3] | (gif_data[4] << 8);
+ width = gif_data[5] | (gif_data[6] << 8);
+ height = gif_data[7] | (gif_data[8] << 8);
+
+ /* Boundary checking - shouldn't ever happen except unless the data has
+ * been modified since initialisation.
+ */
+ if ((offset_x + width > gif->width) ||
+ (offset_y + height > gif->height)) {
+ return_value = GIF_DATA_ERROR;
+ goto gif_decode_frame_exit;
+ }
+
+ /* Decode the flags */
+ flags = gif_data[9];
+ colour_table_size = 2 << (flags & GIF_COLOUR_TABLE_SIZE_MASK);
+ interlace = flags & GIF_INTERLACE_MASK;
+
+ /* Advance data pointer to next block either colour table or image
+ * data.
+ */
+ gif_data += 10;
+ gif_bytes = (gif_end - gif_data);
+
+ /* Set up the colour table */
+ if (flags & GIF_COLOUR_TABLE_MASK) {
+ if (gif_bytes < (int)(3 * colour_table_size)) {
+ return_value = GIF_INSUFFICIENT_FRAME_DATA;
+ goto gif_decode_frame_exit;
+ }
+ colour_table = gif->local_colour_table;
+ if (!clear_image) {
+ for (index = 0; index < colour_table_size; index++) {
+ /* Gif colour map contents are r,g,b.
+ *
+ * We want to pack them bytewise into the
+ * colour table, such that the red component
+ * is in byte 0 and the alpha component is in
+ * byte 3.
+ */
+ unsigned char *entry =
+ (unsigned char *) &colour_table[index];
+
+ entry[0] = gif_data[0]; /* r */
+ entry[1] = gif_data[1]; /* g */
+ entry[2] = gif_data[2]; /* b */
+ entry[3] = 0xff; /* a */
+
+ gif_data += 3;
+ }
+ } else {
+ gif_data += 3 * colour_table_size;
+ }
+ gif_bytes = (gif_end - gif_data);
+ } else {
+ colour_table = gif->global_colour_table;
+ }
+
+ /* Ensure sufficient data remains */
+ if (gif_bytes < 1) {
+ return_value = GIF_INSUFFICIENT_FRAME_DATA;
+ goto gif_decode_frame_exit;
+ }
+
+ /* check for an end marker */
+ if (gif_data[0] == GIF_TRAILER) {
+ return_value = GIF_OK;
+ goto gif_decode_frame_exit;
+ }
+
+ /* Get the frame data */
+ assert(gif->bitmap_callbacks.bitmap_get_buffer);
+ frame_data = (void *)gif->bitmap_callbacks.bitmap_get_buffer(gif->frame_image);
+ if (!frame_data) {
+ return GIF_INSUFFICIENT_MEMORY;
+ }
+
+ /* If we are clearing the image we just clear, if not decode */
+ if (!clear_image) {
+ lzw_result res;
+ const uint8_t *stack_base;
+ const uint8_t *stack_pos;
+
+ /* Ensure we have enough data for a 1-byte LZW code size +
+ * 1-byte gif trailer
+ */
+ if (gif_bytes < 2) {
+ return_value = GIF_INSUFFICIENT_FRAME_DATA;
+ goto gif_decode_frame_exit;
+ }
+
+ /* If we only have a 1-byte LZW code size + 1-byte gif trailer,
+ * we're finished
+ */
+ if ((gif_bytes == 2) && (gif_data[1] == GIF_TRAILER)) {
+ return_value = GIF_OK;
+ goto gif_decode_frame_exit;
+ }
+
+ /* If the previous frame's disposal method requires we restore
+ * the background colour or this is the first frame, clear
+ * the frame data
+ */
+ if ((frame == 0) || (gif->decoded_frame == GIF_INVALID_FRAME)) {
+ memset((char*)frame_data,
+ GIF_TRANSPARENT_COLOUR,
+ gif->width * gif->height * sizeof(int));
+ gif->decoded_frame = frame;
+ /* The line below would fill the image with its
+ * background color, but because GIFs support
+ * transparency we likely wouldn't want to do that. */
+ /* memset((char*)frame_data, colour_table[gif->background_index], gif->width *
gif->height * sizeof(int)); */
+ } else if ((frame != 0) &&
+ (gif->frames[frame - 1].disposal_method == GIF_FRAME_CLEAR)) {
+ return_value = gif_internal_decode_frame(gif,
+ (frame - 1),
+ true);
+ if (return_value != GIF_OK) {
+ goto gif_decode_frame_exit;
+ }
+
+ } else if ((frame != 0) &&
+ (gif->frames[frame - 1].disposal_method == GIF_FRAME_RESTORE)) {
+ /*
+ * If the previous frame's disposal method requires we
+ * restore the previous image, find the last image set
+ * to "do not dispose" and get that frame data
+ */
+ int last_undisposed_frame = frame - 2;
+ while ((last_undisposed_frame >= 0) &&
+ (gif->frames[last_undisposed_frame].disposal_method == GIF_FRAME_RESTORE)) {
+ last_undisposed_frame--;
+ }
+
+ /* If we don't find one, clear the frame data */
+ if (last_undisposed_frame == -1) {
+ /* see notes above on transparency
+ * vs. background color
+ */
+ memset((char*)frame_data,
+ GIF_TRANSPARENT_COLOUR,
+ gif->width * gif->height * sizeof(int));
+ } else {
+ return_value = gif_internal_decode_frame(gif, last_undisposed_frame, false);
+ if (return_value != GIF_OK) {
+ goto gif_decode_frame_exit;
+ }
+ /* Get this frame's data */
+ assert(gif->bitmap_callbacks.bitmap_get_buffer);
+ frame_data = (void
*)gif->bitmap_callbacks.bitmap_get_buffer(gif->frame_image);
+ if (!frame_data) {
+ return GIF_INSUFFICIENT_MEMORY;
+ }
+ }
+ }
+ gif->decoded_frame = frame;
+ gif->buffer_position = (gif_data - gif->gif_data) + 1;
+
+ /* Initialise the LZW decoding */
+ res = lzw_decode_init(gif->lzw_ctx, gif->gif_data,
+ gif->buffer_size, gif->buffer_position,
+ gif_data[0], &stack_base, &stack_pos);
+ if (res != LZW_OK) {
+ return gif_error_from_lzw(res);
+ }
+
+ /* Decompress the data */
+ for (y = 0; y < height; y++) {
+ if (interlace) {
+ decode_y = gif_interlaced_line(height, y) + offset_y;
+ } else {
+ decode_y = y + offset_y;
+ }
+ frame_scanline = frame_data + offset_x + (decode_y * gif->width);
+
+ /* Rather than decoding pixel by pixel, we try to burst
+ * out streams of data to remove the need for end-of
+ * data checks every pixel.
+ */
+ x = width;
+ while (x > 0) {
+ burst_bytes = (stack_pos - stack_base);
+ if (burst_bytes > 0) {
+ if (burst_bytes > x) {
+ burst_bytes = x;
+ }
+ x -= burst_bytes;
+ while (burst_bytes-- > 0) {
+ colour = *--stack_pos;
+ if (((gif->frames[frame].transparency) &&
+ (colour != gif->frames[frame].transparency_index)) ||
+ (!gif->frames[frame].transparency)) {
+ *frame_scanline = colour_table[colour];
+ }
+ frame_scanline++;
+ }
+ } else {
+ res = lzw_decode(gif->lzw_ctx, &stack_pos);
+ if (res != LZW_OK) {
+ /* Unexpected end of frame, try to recover */
+ if (res == LZW_OK_EOD) {
+ return_value = GIF_OK;
+ } else {
+ return_value = gif_error_from_lzw(res);
+ }
+ goto gif_decode_frame_exit;
+ }
+ }
+ }
+ }
+ } else {
+ /* Clear our frame */
+ if (gif->frames[frame].disposal_method == GIF_FRAME_CLEAR) {
+ for (y = 0; y < height; y++) {
+ frame_scanline = frame_data + offset_x + ((offset_y + y) * gif->width);
+ if (gif->frames[frame].transparency) {
+ memset(frame_scanline,
+ GIF_TRANSPARENT_COLOUR,
+ width * 4);
+ } else {
+ memset(frame_scanline,
+ colour_table[gif->background_index],
+ width * 4);
+ }
+ }
+ }
+ }
+gif_decode_frame_exit:
+
+ /* Check if we should test for optimisation */
+ if (gif->frames[frame].virgin) {
+ if (gif->bitmap_callbacks.bitmap_test_opaque) {
+ gif->frames[frame].opaque =
gif->bitmap_callbacks.bitmap_test_opaque(gif->frame_image);
+ } else {
+ gif->frames[frame].opaque = false;
+ }
+ gif->frames[frame].virgin = false;
+ }
+
+ if (gif->bitmap_callbacks.bitmap_set_opaque) {
+ gif->bitmap_callbacks.bitmap_set_opaque(gif->frame_image, gif->frames[frame].opaque);
+ }
+
+ if (gif->bitmap_callbacks.bitmap_modified) {
+ gif->bitmap_callbacks.bitmap_modified(gif->frame_image);
+ }
+
+ /* Restore the buffer position */
+ gif->buffer_position = save_buffer_position;
+
+ return return_value;
+}
+
+
+/* exported function documented in libnsgif.h */
+void gif_create(gif_animation *gif, gif_bitmap_callback_vt *bitmap_callbacks)
+{
+ memset(gif, 0, sizeof(gif_animation));
+ gif->bitmap_callbacks = *bitmap_callbacks;
+ gif->decoded_frame = GIF_INVALID_FRAME;
+}
+
+
+/* exported function documented in libnsgif.h */
+gif_result gif_initialise(gif_animation *gif, size_t size, unsigned char *data)
+{
+ unsigned char *gif_data;
+ unsigned int index;
+ gif_result return_value;
+
+ /* Initialize values */
+ gif->buffer_size = size;
+ gif->gif_data = data;
+
+ if (gif->lzw_ctx == NULL) {
+ lzw_result res = lzw_context_create(
+ (struct lzw_ctx **)&gif->lzw_ctx);
+ if (res != LZW_OK) {
+ return gif_error_from_lzw(res);
+ }
+ }
+
+ /* Check for sufficient data to be a GIF (6-byte header + 7-byte
+ * logical screen descriptor)
+ */
+ if (gif->buffer_size < GIF_STANDARD_HEADER_SIZE) {
+ return GIF_INSUFFICIENT_DATA;
+ }
+
+ /* Get our current processing position */
+ gif_data = gif->gif_data + gif->buffer_position;
+
+ /* See if we should initialise the GIF */
+ if (gif->buffer_position == 0) {
+ /* We want everything to be NULL before we start so we've no
+ * chance of freeing bad pointers (paranoia)
+ */
+ gif->frame_image = NULL;
+ gif->frames = NULL;
+ gif->local_colour_table = NULL;
+ gif->global_colour_table = NULL;
+
+ /* The caller may have been lazy and not reset any values */
+ gif->frame_count = 0;
+ gif->frame_count_partial = 0;
+ gif->decoded_frame = GIF_INVALID_FRAME;
+
+ /* 6-byte GIF file header is:
+ *
+ * +0 3CHARS Signature ('GIF')
+ * +3 3CHARS Version ('87a' or '89a')
+ */
+ if (strncmp((const char *) gif_data, "GIF", 3) != 0) {
+ return GIF_DATA_ERROR;
+ }
+ gif_data += 3;
+
+ /* Ensure GIF reports version 87a or 89a */
+ /*
+ if ((strncmp(gif_data, "87a", 3) != 0) &&
+ (strncmp(gif_data, "89a", 3) != 0))
+ LOG(("Unknown GIF format - proceeding anyway"));
+ */
+ gif_data += 3;
+
+ /* 7-byte Logical Screen Descriptor is:
+ *
+ * +0 SHORT Logical Screen Width
+ * +2 SHORT Logical Screen Height
+ * +4 CHAR __Packed Fields__
+ * 1BIT Global Colour Table Flag
+ * 3BITS Colour Resolution
+ * 1BIT Sort Flag
+ * 3BITS Size of Global Colour Table
+ * +5 CHAR Background Colour Index
+ * +6 CHAR Pixel Aspect Ratio
+ */
+ gif->width = gif_data[0] | (gif_data[1] << 8);
+ gif->height = gif_data[2] | (gif_data[3] << 8);
+ gif->global_colours = (gif_data[4] & GIF_COLOUR_TABLE_MASK);
+ gif->colour_table_size = (2 << (gif_data[4] & GIF_COLOUR_TABLE_SIZE_MASK));
+ gif->background_index = gif_data[5];
+ gif->aspect_ratio = gif_data[6];
+ gif->loop_count = 1;
+ gif_data += 7;
+
+ /* Some broken GIFs report the size as the screen size they
+ * were created in. As such, we detect for the common cases and
+ * set the sizes as 0 if they are found which results in the
+ * GIF being the maximum size of the frames.
+ */
+ if (((gif->width == 640) && (gif->height == 480)) ||
+ ((gif->width == 640) && (gif->height == 512)) ||
+ ((gif->width == 800) && (gif->height == 600)) ||
+ ((gif->width == 1024) && (gif->height == 768)) ||
+ ((gif->width == 1280) && (gif->height == 1024)) ||
+ ((gif->width == 1600) && (gif->height == 1200)) ||
+ ((gif->width == 0) || (gif->height == 0)) ||
+ ((gif->width > 2048) || (gif->height > 2048))) {
+ gif->width = 1;
+ gif->height = 1;
+ }
+
+ /* Allocate some data irrespective of whether we've got any
+ * colour tables. We always get the maximum size in case a GIF
+ * is lying to us. It's far better to give the wrong colours
+ * than to trample over some memory somewhere.
+ */
+ gif->global_colour_table = calloc(GIF_MAX_COLOURS, sizeof(unsigned int));
+ gif->local_colour_table = calloc(GIF_MAX_COLOURS, sizeof(unsigned int));
+ if ((gif->global_colour_table == NULL) ||
+ (gif->local_colour_table == NULL)) {
+ gif_finalise(gif);
+ return GIF_INSUFFICIENT_MEMORY;
+ }
+
+ /* Set the first colour to a value that will never occur in
+ * reality so we know if we've processed it
+ */
+ gif->global_colour_table[0] = GIF_PROCESS_COLOURS;
+
+ /* Check if the GIF has no frame data (13-byte header + 1-byte
+ * termination block) Although generally useless, the GIF
+ * specification does not expressly prohibit this
+ */
+ if (gif->buffer_size == (GIF_STANDARD_HEADER_SIZE + 1)) {
+ if (gif_data[0] == GIF_TRAILER) {
+ return GIF_OK;
+ } else {
+ return GIF_INSUFFICIENT_DATA;
+ }
+ }
+
+ /* Initialise enough workspace for a frame */
+ if ((gif->frames = (gif_frame *)malloc(sizeof(gif_frame))) == NULL) {
+ gif_finalise(gif);
+ return GIF_INSUFFICIENT_MEMORY;
+ }
+ gif->frame_holders = 1;
+
+ /* Initialise the bitmap header */
+ assert(gif->bitmap_callbacks.bitmap_create);
+ gif->frame_image = gif->bitmap_callbacks.bitmap_create(gif->width, gif->height);
+ if (gif->frame_image == NULL) {
+ gif_finalise(gif);
+ return GIF_INSUFFICIENT_MEMORY;
+ }
+
+ /* Remember we've done this now */
+ gif->buffer_position = gif_data - gif->gif_data;
+ }
+
+ /* Do the colour map if we haven't already. As the top byte is always
+ * 0xff or 0x00 depending on the transparency we know if it's been
+ * filled in.
+ */
+ if (gif->global_colour_table[0] == GIF_PROCESS_COLOURS) {
+ /* Check for a global colour map signified by bit 7 */
+ if (gif->global_colours) {
+ if (gif->buffer_size < (gif->colour_table_size * 3 + GIF_STANDARD_HEADER_SIZE)) {
+ return GIF_INSUFFICIENT_DATA;
+ }
+ for (index = 0; index < gif->colour_table_size; index++) {
+ /* Gif colour map contents are r,g,b.
+ *
+ * We want to pack them bytewise into the
+ * colour table, such that the red component
+ * is in byte 0 and the alpha component is in
+ * byte 3.
+ */
+ unsigned char *entry = (unsigned char *) &gif->
+ global_colour_table[index];
+
+ entry[0] = gif_data[0]; /* r */
+ entry[1] = gif_data[1]; /* g */
+ entry[2] = gif_data[2]; /* b */
+ entry[3] = 0xff; /* a */
+
+ gif_data += 3;
+ }
+ gif->buffer_position = (gif_data - gif->gif_data);
+ } else {
+ /* Create a default colour table with the first two
+ * colours as black and white
+ */
+ unsigned int *entry = gif->global_colour_table;
+
+ entry[0] = 0x00000000;
+ /* Force Alpha channel to opaque */
+ ((unsigned char *) entry)[3] = 0xff;
+
+ entry[1] = 0xffffffff;
+ }
+ }
+
+ /* Repeatedly try to initialise frames */
+ while ((return_value = gif_initialise_frame(gif)) == GIF_WORKING);
+
+ /* If there was a memory error tell the caller */
+ if ((return_value == GIF_INSUFFICIENT_MEMORY) ||
+ (return_value == GIF_DATA_ERROR)) {
+ return return_value;
+ }
+
+ /* If we didn't have some frames then a GIF_INSUFFICIENT_DATA becomes a
+ * GIF_INSUFFICIENT_FRAME_DATA
+ */
+ if ((return_value == GIF_INSUFFICIENT_DATA) &&
+ (gif->frame_count_partial > 0)) {
+ return GIF_INSUFFICIENT_FRAME_DATA;
+ }
+
+ /* Return how many we got */
+ return return_value;
+}
+
+
+/* exported function documented in libnsgif.h */
+gif_result gif_decode_frame(gif_animation *gif, unsigned int frame)
+{
+ return gif_internal_decode_frame(gif, frame, false);
+}
+
+
+/* exported function documented in libnsgif.h */
+void gif_finalise(gif_animation *gif)
+{
+ /* Release all our memory blocks */
+ if (gif->frame_image) {
+ assert(gif->bitmap_callbacks.bitmap_destroy);
+ gif->bitmap_callbacks.bitmap_destroy(gif->frame_image);
+ }
+
+ gif->frame_image = NULL;
+ free(gif->frames);
+ gif->frames = NULL;
+ free(gif->local_colour_table);
+ gif->local_colour_table = NULL;
+ free(gif->global_colour_table);
+ gif->global_colour_table = NULL;
+
+ lzw_context_destroy(gif->lzw_ctx);
+ gif->lzw_ctx = NULL;
+}
diff --git a/libs/libnsgif/libnsgif.h b/libs/libnsgif/libnsgif.h
new file mode 100644
index 000000000..a819fec0b
--- /dev/null
+++ b/libs/libnsgif/libnsgif.h
@@ -0,0 +1,183 @@
+/*
+ * Copyright 2004 Richard Wilson <richard wilson netsurf-browser org>
+ * Copyright 2008 Sean Fox <dyntryx gmail com>
+ *
+ * This file is part of NetSurf's libnsgif, http://www.netsurf-browser.org/
+ * Licenced under the MIT License,
+ * http://www.opensource.org/licenses/mit-license.php
+ */
+
+/**
+ * \file
+ * Interface to progressive animated GIF file decoding.
+ */
+
+#ifndef _LIBNSGIF_H_
+#define _LIBNSGIF_H_
+
+#include <stdbool.h>
+#include <inttypes.h>
+
+/* Error return values */
+typedef enum {
+ GIF_WORKING = 1,
+ GIF_OK = 0,
+ GIF_INSUFFICIENT_FRAME_DATA = -1,
+ GIF_FRAME_DATA_ERROR = -2,
+ GIF_INSUFFICIENT_DATA = -3,
+ GIF_DATA_ERROR = -4,
+ GIF_INSUFFICIENT_MEMORY = -5,
+ GIF_FRAME_NO_DISPLAY = -6,
+ GIF_END_OF_FRAME = -7
+} gif_result;
+
+/** GIF frame data */
+typedef struct gif_frame {
+ /** whether the frame should be displayed/animated */
+ bool display;
+ /** delay (in cs) before animating the frame */
+ unsigned int frame_delay;
+
+ /* Internal members are listed below */
+
+ /** offset (in bytes) to the GIF frame data */
+ unsigned int frame_pointer;
+ /** whether the frame has previously been used */
+ bool virgin;
+ /** whether the frame is totally opaque */
+ bool opaque;
+ /** whether a forcable screen redraw is required */
+ bool redraw_required;
+ /** how the previous frame should be disposed; affects plotting */
+ unsigned char disposal_method;
+ /** whether we acknoledge transparency */
+ bool transparency;
+ /** the index designating a transparent pixel */
+ unsigned char transparency_index;
+ /** x co-ordinate of redraw rectangle */
+ unsigned int redraw_x;
+ /** y co-ordinate of redraw rectangle */
+ unsigned int redraw_y;
+ /** width of redraw rectangle */
+ unsigned int redraw_width;
+ /** height of redraw rectangle */
+ unsigned int redraw_height;
+} gif_frame;
+
+/* API for Bitmap callbacks */
+typedef void* (*gif_bitmap_cb_create)(int width, int height);
+typedef void (*gif_bitmap_cb_destroy)(void *bitmap);
+typedef unsigned char* (*gif_bitmap_cb_get_buffer)(void *bitmap);
+typedef void (*gif_bitmap_cb_set_opaque)(void *bitmap, bool opaque);
+typedef bool (*gif_bitmap_cb_test_opaque)(void *bitmap);
+typedef void (*gif_bitmap_cb_modified)(void *bitmap);
+
+/** Bitmap callbacks function table */
+typedef struct gif_bitmap_callback_vt {
+ /** Create a bitmap. */
+ gif_bitmap_cb_create bitmap_create;
+ /** Free a bitmap. */
+ gif_bitmap_cb_destroy bitmap_destroy;
+ /** Return a pointer to the pixel data in a bitmap. */
+ gif_bitmap_cb_get_buffer bitmap_get_buffer;
+
+ /* Members below are optional */
+
+ /** Sets whether a bitmap should be plotted opaque. */
+ gif_bitmap_cb_set_opaque bitmap_set_opaque;
+ /** Tests whether a bitmap has an opaque alpha channel. */
+ gif_bitmap_cb_test_opaque bitmap_test_opaque;
+ /** The bitmap image has changed, so flush any persistant cache. */
+ gif_bitmap_cb_modified bitmap_modified;
+} gif_bitmap_callback_vt;
+
+/** GIF animation data */
+typedef struct gif_animation {
+ /** LZW decode context */
+ void *lzw_ctx;
+ /** callbacks for bitmap functions */
+ gif_bitmap_callback_vt bitmap_callbacks;
+ /** pointer to GIF data */
+ unsigned char *gif_data;
+ /** width of GIF (may increase during decoding) */
+ unsigned int width;
+ /** heigth of GIF (may increase during decoding) */
+ unsigned int height;
+ /** number of frames decoded */
+ unsigned int frame_count;
+ /** number of frames partially decoded */
+ unsigned int frame_count_partial;
+ /** decoded frames */
+ gif_frame *frames;
+ /** current frame decoded to bitmap */
+ int decoded_frame;
+ /** currently decoded image; stored as bitmap from bitmap_create callback */
+ void *frame_image;
+ /** number of times to loop animation */
+ int loop_count;
+
+ /* Internal members are listed below */
+
+ /** current index into GIF data */
+ unsigned int buffer_position;
+ /** total number of bytes of GIF data available */
+ unsigned int buffer_size;
+ /** current number of frame holders */
+ unsigned int frame_holders;
+ /** index in the colour table for the background colour */
+ unsigned int background_index;
+ /** image aspect ratio (ignored) */
+ unsigned int aspect_ratio;
+ /** size of colour table (in entries) */
+ unsigned int colour_table_size;
+ /** whether the GIF has a global colour table */
+ bool global_colours;
+ /** global colour table */
+ unsigned int *global_colour_table;
+ /** local colour table */
+ unsigned int *local_colour_table;
+} gif_animation;
+
+/**
+ * Initialises necessary gif_animation members.
+ */
+void gif_create(gif_animation *gif, gif_bitmap_callback_vt *bitmap_callbacks);
+
+/**
+ * Initialises any workspace held by the animation and attempts to decode
+ * any information that hasn't already been decoded.
+ * If an error occurs, all previously decoded frames are retained.
+ *
+ * @return Error return value.
+ * - GIF_FRAME_DATA_ERROR for GIF frame data error
+ * - GIF_INSUFFICIENT_FRAME_DATA for insufficient data to process
+ * any more frames
+ * - GIF_INSUFFICIENT_MEMORY for memory error
+ * - GIF_DATA_ERROR for GIF error
+ * - GIF_INSUFFICIENT_DATA for insufficient data to do anything
+ * - GIF_OK for successful decoding
+ * - GIF_WORKING for successful decoding if more frames are expected
+ */
+gif_result gif_initialise(gif_animation *gif, size_t size, unsigned char *data);
+
+/**
+ * Decodes a GIF frame.
+ *
+ * @return Error return value. If a frame does not contain any image data,
+ * GIF_OK is returned and gif->current_error is set to
+ * GIF_FRAME_NO_DISPLAY
+ * - GIF_FRAME_DATA_ERROR for GIF frame data error
+ * - GIF_INSUFFICIENT_FRAME_DATA for insufficient data to complete the frame
+ * - GIF_DATA_ERROR for GIF error (invalid frame header)
+ * - GIF_INSUFFICIENT_DATA for insufficient data to do anything
+ * - GIF_INSUFFICIENT_MEMORY for insufficient memory to process
+ * - GIF_OK for successful decoding
+ */
+gif_result gif_decode_frame(gif_animation *gif, unsigned int frame);
+
+/**
+ * Releases any workspace held by a gif
+ */
+void gif_finalise(gif_animation *gif);
+
+#endif
diff --git a/libs/libnsgif/libnsgif.pc.in b/libs/libnsgif/libnsgif.pc.in
new file mode 100644
index 000000000..0255bf9df
--- /dev/null
+++ b/libs/libnsgif/libnsgif.pc.in
@@ -0,0 +1,10 @@
+prefix=PREFIX
+exec_prefix=${prefix}
+libdir=${exec_prefix}/LIBDIR
+includedir=${prefix}/INCLUDEDIR
+
+Name: libnsgif
+Description: Provides gif loading and conversion
+Version: VERSION
+Libs: -L${libdir} -lnsgif
+Cflags: -I${includedir}
diff --git a/libs/libnsgif/log.h b/libs/libnsgif/log.h
new file mode 100644
index 000000000..1413374cb
--- /dev/null
+++ b/libs/libnsgif/log.h
@@ -0,0 +1,21 @@
+/*
+ * Copyright 2003 James Bursa <bursa users sourceforge net>
+ * Copyright 2004 John Tytgat <John Tytgat aaug net>
+ *
+ * This file is part of NetSurf, http://www.netsurf-browser.org/
+ * Licenced under the MIT License,
+ * http://www.opensource.org/licenses/mit-license.php
+ */
+
+#include <stdio.h>
+
+#ifndef _LIBNSGIF_LOG_H_
+#define _LIBNSGIF_LOG_H_
+
+#ifdef NDEBUG
+# define LOG(x) ((void) 0)
+#else
+# define LOG(x) do { fprintf(stderr, x), fputc('\n', stderr); } while (0)
+#endif /* NDEBUG */
+
+#endif /* _LIBNSGIF_LOG_H_ */
diff --git a/libs/libnsgif/lzw.c b/libs/libnsgif/lzw.c
new file mode 100644
index 000000000..31cf7d4e9
--- /dev/null
+++ b/libs/libnsgif/lzw.c
@@ -0,0 +1,377 @@
+/*
+ * This file is part of NetSurf's LibNSGIF, http://www.netsurf-browser.org/
+ * Licensed under the MIT License,
+ * http://www.opensource.org/licenses/mit-license.php
+ *
+ * Copyright 2017 Michael Drake <michael drake codethink co uk>
+ */
+
+#include <assert.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <stdbool.h>
+
+#include "lzw.h"
+
+/**
+ * \file
+ * \brief LZW decompression (implementation)
+ *
+ * Decoder for GIF LZW data.
+ */
+
+
+/**
+ * Context for reading LZW data.
+ *
+ * LZW data is split over multiple sub-blocks. Each sub-block has a
+ * byte at the start, which says the sub-block size, and then the data.
+ * Zero-size sub-blocks have no data, and the biggest sub-block size is
+ * 255, which means there are 255 bytes of data following the sub-block
+ * size entry.
+ *
+ * Note that an individual LZW code can be split over up to three sub-blocks.
+ */
+struct lzw_read_ctx {
+ const uint8_t *data; /**< Pointer to start of input data */
+ uint32_t data_len; /**< Input data length */
+ uint32_t data_sb_next; /**< Offset to sub-block size */
+
+ const uint8_t *sb_data; /**< Pointer to current sub-block in data */
+ uint32_t sb_bit; /**< Current bit offset in sub-block */
+ uint32_t sb_bit_count; /**< Bit count in sub-block */
+};
+
+/**
+ * LZW dictionary entry.
+ *
+ * Records in the dictionary are composed of 1 or more entries.
+ * Entries point to previous entries which can be followed to compose
+ * the complete record. To compose the record in reverse order, take
+ * the `last_value` from each entry, and move to the previous entry.
+ * If the previous_entry's index is < the current clear_code, then it
+ * is the last entry in the record.
+ */
+struct lzw_dictionary_entry {
+ uint8_t last_value; /**< Last value for record ending at entry. */
+ uint8_t first_value; /**< First value for entry's record. */
+ uint16_t previous_entry; /**< Offset in dictionary to previous entry. */
+};
+
+/**
+ * LZW decompression context.
+ */
+struct lzw_ctx {
+ /** Input reading context */
+ struct lzw_read_ctx input;
+
+ uint32_t previous_code; /**< Code read from input previously. */
+ uint32_t previous_code_first; /**< First value of previous code. */
+
+ uint32_t initial_code_size; /**< Starting LZW code size. */
+ uint32_t current_code_size; /**< Current LZW code size. */
+ uint32_t current_code_size_max; /**< Max code value for current size. */
+
+ uint32_t clear_code; /**< Special Clear code value */
+ uint32_t eoi_code; /**< Special End of Information code value */
+
+ uint32_t current_entry; /**< Next position in table to fill. */
+
+ /** Output value stack. */
+ uint8_t stack_base[1 << LZW_CODE_MAX];
+
+ /** LZW decode dictionary. Generated during decode. */
+ struct lzw_dictionary_entry table[1 << LZW_CODE_MAX];
+};
+
+
+/* Exported function, documented in lzw.h */
+lzw_result lzw_context_create(struct lzw_ctx **ctx)
+{
+ struct lzw_ctx *c = malloc(sizeof(*c));
+ if (c == NULL) {
+ return LZW_NO_MEM;
+ }
+
+ *ctx = c;
+ return LZW_OK;
+}
+
+
+/* Exported function, documented in lzw.h */
+void lzw_context_destroy(struct lzw_ctx *ctx)
+{
+ free(ctx);
+}
+
+
+/**
+ * Advance the context to the next sub-block in the input data.
+ *
+ * \param[in] ctx LZW reading context, updated on success.
+ * \return LZW_OK or LZW_OK_EOD on success, appropriate error otherwise.
+ */
+static lzw_result lzw__block_advance(struct lzw_read_ctx *ctx)
+{
+ uint32_t block_size;
+ uint32_t next_block_pos = ctx->data_sb_next;
+ const uint8_t *data_next = ctx->data + next_block_pos;
+
+ if (next_block_pos >= ctx->data_len) {
+ return LZW_NO_DATA;
+ }
+
+ block_size = *data_next;
+
+ if ((next_block_pos + block_size) >= ctx->data_len) {
+ return LZW_NO_DATA;
+ }
+
+ ctx->sb_bit = 0;
+ ctx->sb_bit_count = block_size * 8;
+
+ if (block_size == 0) {
+ ctx->data_sb_next += 1;
+ return LZW_OK_EOD;
+ }
+
+ ctx->sb_data = data_next + 1;
+ ctx->data_sb_next += block_size + 1;
+
+ return LZW_OK;
+}
+
+
+/**
+ * Get the next LZW code of given size from the raw input data.
+ *
+ * Reads codes from the input data stream coping with GIF data sub-blocks.
+ *
+ * \param[in] ctx LZW reading context, updated.
+ * \param[in] code_size Size of LZW code to get from data.
+ * \param[out] code_out Returns an LZW code on success.
+ * \return LZW_OK or LZW_OK_EOD on success, appropriate error otherwise.
+ */
+static inline lzw_result lzw__next_code(
+ struct lzw_read_ctx *ctx,
+ uint8_t code_size,
+ uint32_t *code_out)
+{
+ uint32_t code = 0;
+ uint8_t current_bit = ctx->sb_bit & 0x7;
+ uint8_t byte_advance = (current_bit + code_size) >> 3;
+
+ assert(byte_advance <= 2);
+
+ if (ctx->sb_bit + code_size <= ctx->sb_bit_count) {
+ /* Fast path: code fully inside this sub-block */
+ const uint8_t *data = ctx->sb_data + (ctx->sb_bit >> 3);
+ switch (byte_advance) {
+ case 2: code |= data[2] << 16; /* Fall through */
+ case 1: code |= data[1] << 8; /* Fall through */
+ case 0: code |= data[0] << 0;
+ }
+ ctx->sb_bit += code_size;
+ } else {
+ /* Slow path: code spans sub-blocks */
+ uint8_t byte = 0;
+ uint8_t bits_remaining_0 = (code_size < (8 - current_bit)) ?
+ code_size : (8 - current_bit);
+ uint8_t bits_remaining_1 = code_size - bits_remaining_0;
+ uint8_t bits_used[3] = {
+ [0] = bits_remaining_0,
+ [1] = bits_remaining_1 < 8 ? bits_remaining_1 : 8,
+ [2] = bits_remaining_1 - 8,
+ };
+
+ while (true) {
+ const uint8_t *data = ctx->sb_data;
+ lzw_result res;
+
+ /* Get any data from end of this sub-block */
+ while (byte <= byte_advance &&
+ ctx->sb_bit < ctx->sb_bit_count) {
+ code |= data[ctx->sb_bit >> 3] << (byte << 3);
+ ctx->sb_bit += bits_used[byte];
+ byte++;
+ }
+
+ /* Check if we have all we need */
+ if (byte > byte_advance) {
+ break;
+ }
+
+ /* Move to next sub-block */
+ res = lzw__block_advance(ctx);
+ if (res != LZW_OK) {
+ return res;
+ }
+ }
+ }
+
+ *code_out = (code >> current_bit) & ((1 << code_size) - 1);
+ return LZW_OK;
+}
+
+
+/**
+ * Clear LZW code dictionary.
+ *
+ * \param[in] ctx LZW reading context, updated.
+ * \param[out] stack_pos_out Returns current stack position.
+ * \return LZW_OK or error code.
+ */
+static lzw_result lzw__clear_codes(
+ struct lzw_ctx *ctx,
+ const uint8_t ** const stack_pos_out)
+{
+ uint32_t code;
+ uint8_t *stack_pos;
+
+ /* Reset dictionary building context */
+ ctx->current_code_size = ctx->initial_code_size + 1;
+ ctx->current_code_size_max = (1 << ctx->current_code_size) - 1;;
+ ctx->current_entry = (1 << ctx->initial_code_size) + 2;
+
+ /* There might be a sequence of clear codes, so process them all */
+ do {
+ lzw_result res = lzw__next_code(&ctx->input,
+ ctx->current_code_size, &code);
+ if (res != LZW_OK) {
+ return res;
+ }
+ } while (code == ctx->clear_code);
+
+ /* The initial code must be from the initial dictionary. */
+ if (code > ctx->clear_code) {
+ return LZW_BAD_ICODE;
+ }
+
+ /* Record this initial code as "previous" code, needed during decode. */
+ ctx->previous_code = code;
+ ctx->previous_code_first = code;
+
+ /* Reset the stack, and add first non-clear code added as first item. */
+ stack_pos = ctx->stack_base;
+ *stack_pos++ = code;
+
+ *stack_pos_out = stack_pos;
+ return LZW_OK;
+}
+
+
+/* Exported function, documented in lzw.h */
+lzw_result lzw_decode_init(
+ struct lzw_ctx *ctx,
+ const uint8_t *compressed_data,
+ uint32_t compressed_data_len,
+ uint32_t compressed_data_pos,
+ uint8_t code_size,
+ const uint8_t ** const stack_base_out,
+ const uint8_t ** const stack_pos_out)
+{
+ struct lzw_dictionary_entry *table = ctx->table;
+
+ /* Initialise the input reading context */
+ ctx->input.data = compressed_data;
+ ctx->input.data_len = compressed_data_len;
+ ctx->input.data_sb_next = compressed_data_pos;
+
+ ctx->input.sb_bit = 0;
+ ctx->input.sb_bit_count = 0;
+
+ /* Initialise the dictionary building context */
+ ctx->initial_code_size = code_size;
+
+ ctx->clear_code = (1 << code_size) + 0;
+ ctx->eoi_code = (1 << code_size) + 1;
+
+ /* Initialise the standard dictionary entries */
+ for (uint32_t i = 0; i < ctx->clear_code; ++i) {
+ table[i].first_value = i;
+ table[i].last_value = i;
+ }
+
+ *stack_base_out = ctx->stack_base;
+ return lzw__clear_codes(ctx, stack_pos_out);
+}
+
+
+/* Exported function, documented in lzw.h */
+lzw_result lzw_decode(struct lzw_ctx *ctx,
+ const uint8_t ** const stack_pos_out)
+{
+ lzw_result res;
+ uint32_t code_new;
+ uint32_t code_out;
+ uint8_t last_value;
+ uint8_t *stack_pos = ctx->stack_base;
+ uint32_t clear_code = ctx->clear_code;
+ uint32_t current_entry = ctx->current_entry;
+ struct lzw_dictionary_entry * const table = ctx->table;
+
+ /* Get a new code from the input */
+ res = lzw__next_code(&ctx->input, ctx->current_code_size, &code_new);
+ if (res != LZW_OK) {
+ return res;
+ }
+
+ /* Handle the new code */
+ if (code_new == clear_code) {
+ /* Got Clear code */
+ return lzw__clear_codes(ctx, stack_pos_out);
+
+ } else if (code_new == ctx->eoi_code) {
+ /* Got End of Information code */
+ return LZW_EOI_CODE;
+
+ } else if (code_new > current_entry) {
+ /* Code is invalid */
+ return LZW_BAD_CODE;
+
+ } else if (code_new < current_entry) {
+ /* Code is in table */
+ code_out = code_new;
+ last_value = table[code_new].first_value;
+ } else {
+ /* Code not in table */
+ *stack_pos++ = ctx->previous_code_first;
+ code_out = ctx->previous_code;
+ last_value = ctx->previous_code_first;
+ }
+
+ /* Add to the dictionary, only if there's space */
+ if (current_entry < (1 << LZW_CODE_MAX)) {
+ struct lzw_dictionary_entry *entry = table + current_entry;
+ entry->last_value = last_value;
+ entry->first_value = ctx->previous_code_first;
+ entry->previous_entry = ctx->previous_code;
+ ctx->current_entry++;
+ }
+
+ /* Ensure code size is increased, if needed. */
+ if (current_entry == ctx->current_code_size_max) {
+ if (ctx->current_code_size < LZW_CODE_MAX) {
+ ctx->current_code_size++;
+ ctx->current_code_size_max =
+ (1 << ctx->current_code_size) - 1;
+ }
+ }
+
+ /* Store details of this code as "previous code" to the context. */
+ ctx->previous_code_first = table[code_new].first_value;
+ ctx->previous_code = code_new;
+
+ /* Put rest of data for this code on output stack.
+ * Note, in the case of "code not in table", the last entry of the
+ * current code has already been placed on the stack above. */
+ while (code_out > clear_code) {
+ struct lzw_dictionary_entry *entry = table + code_out;
+ *stack_pos++ = entry->last_value;
+ code_out = entry->previous_entry;
+ }
+ *stack_pos++ = table[code_out].last_value;
+
+ *stack_pos_out = stack_pos;
+ return LZW_OK;
+}
diff --git a/libs/libnsgif/lzw.h b/libs/libnsgif/lzw.h
new file mode 100644
index 000000000..385b42557
--- /dev/null
+++ b/libs/libnsgif/lzw.h
@@ -0,0 +1,105 @@
+/*
+ * This file is part of NetSurf's LibNSGIF, http://www.netsurf-browser.org/
+ * Licensed under the MIT License,
+ * http://www.opensource.org/licenses/mit-license.php
+ *
+ * Copyright 2017 Michael Drake <michael drake codethink co uk>
+ */
+
+#ifndef LZW_H_
+#define LZW_H_
+
+/**
+ * \file
+ * \brief LZW decompression (interface)
+ *
+ * Decoder for GIF LZW data.
+ */
+
+
+/** Maximum LZW code size in bits */
+#define LZW_CODE_MAX 12
+
+
+/* Declare lzw internal context structure */
+struct lzw_ctx;
+
+
+/** LZW decoding response codes */
+typedef enum lzw_result {
+ LZW_OK, /**< Success */
+ LZW_OK_EOD, /**< Success; reached zero-length sub-block */
+ LZW_NO_MEM, /**< Error: Out of memory */
+ LZW_NO_DATA, /**< Error: Out of data */
+ LZW_EOI_CODE, /**< Error: End of Information code */
+ LZW_BAD_ICODE, /**< Error: Bad initial LZW code */
+ LZW_BAD_CODE, /**< Error: Bad LZW code */
+} lzw_result;
+
+
+/**
+ * Create an LZW decompression context.
+ *
+ * \param[out] ctx Returns an LZW decompression context. Caller owned,
+ * free with lzw_context_destroy().
+ * \return LZW_OK on success, or appropriate error code otherwise.
+ */
+lzw_result lzw_context_create(
+ struct lzw_ctx **ctx);
+
+/**
+ * Destroy an LZW decompression context.
+ *
+ * \param[in] ctx The LZW decompression context to destroy.
+ */
+void lzw_context_destroy(
+ struct lzw_ctx *ctx);
+
+/**
+ * Initialise an LZW decompression context for decoding.
+ *
+ * Caller owns neither `stack_base_out` or `stack_pos_out`.
+ *
+ * \param[in] ctx The LZW decompression context to initialise.
+ * \param[in] compressed_data The compressed data.
+ * \param[in] compressed_data_len Byte length of compressed data.
+ * \param[in] compressed_data_pos Start position in data. Must be position
+ * of a size byte at sub-block start.
+ * \param[in] code_size The initial LZW code size to use.
+ * \param[out] stack_base_out Returns base of decompressed data stack.
+ * \param[out] stack_pos_out Returns current stack position.
+ * There are `stack_pos_out - stack_base_out`
+ * current stack entries.
+ * \return LZW_OK on success, or appropriate error code otherwise.
+ */
+lzw_result lzw_decode_init(
+ struct lzw_ctx *ctx,
+ const uint8_t *compressed_data,
+ uint32_t compressed_data_len,
+ uint32_t compressed_data_pos,
+ uint8_t code_size,
+ const uint8_t ** const stack_base_out,
+ const uint8_t ** const stack_pos_out);
+
+/**
+ * Fill the LZW stack with decompressed data
+ *
+ * Ensure anything on the stack is used before calling this, as anything
+ * on the stack before this call will be trampled.
+ *
+ * Caller does not own `stack_pos_out`.
+ *
+ * \param[in] ctx LZW reading context, updated.
+ * \param[out] stack_pos_out Returns current stack position.
+ * Use with `stack_base_out` value from previous
+ * lzw_decode_init() call.
+ * There are `stack_pos_out - stack_base_out`
+ * current stack entries.
+ * \return LZW_OK on success, or appropriate error code otherwise.
+ */
+lzw_result lzw_decode(
+ struct lzw_ctx *ctx,
+ const uint8_t ** const stack_pos_out);
+
+
+#endif
diff --git a/operations/workshop/gif-load.c b/operations/workshop/gif-load.c
index fe982b0d2..9f314eb95 100644
--- a/operations/workshop/gif-load.c
+++ b/operations/workshop/gif-load.c
@@ -48,8 +48,11 @@ property_int (frame_delay, _("frame-delay"), 100)
#include <gegl-op.h>
#include <gegl-gio-private.h>
-#include "libnsgif.h"
-#include "libnsgif.c"
+
+/* since libnsgif is nice and simple we directly embed it in the .so */
+#include "libs/libnsgif/libnsgif.h"
+#include "libs/libnsgif/libnsgif.c"
+#include "libs/libnsgif/lzw.c"
#define IO_BUFFER_SIZE 4096
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]