From 33107b160f09bbb301791759b83d772c820c4813 Mon Sep 17 00:00:00 2001 From: James Bursa Date: Tue, 12 Aug 2008 03:49:34 +0000 Subject: Merged revisions 4345-4346,4350-4351,4389,4391,4395,4401-4403,4423,4485-4486 via svnmerge from svn://semichrome.net/branches/dynis/netsurf ........ r4345 | dynis | 2008-06-15 18:37:23 -0500 (Sun, 15 Jun 2008) | 1 line Move NetSurf's gifread.h to libnsgif ........ r4346 | dynis | 2008-06-15 18:38:38 -0500 (Sun, 15 Jun 2008) | 1 line Remove NetSurf's gifread.c (replaced by libnsgif) ........ r4350 | dynis | 2008-06-15 18:57:17 -0500 (Sun, 15 Jun 2008) | 1 line Added references to libnsgif where necessary; corrected function calls where callbacks were implemented ........ r4351 | dynis | 2008-06-15 19:00:33 -0500 (Sun, 15 Jun 2008) | 1 line Updated Makefile to compile with libnsgif ........ r4389 | dynis | 2008-06-18 13:58:51 -0500 (Wed, 18 Jun 2008) | 1 line Altered bitmap callback table name for gif images to avoid ambiguity when bmp image library is created ........ r4391 | dynis | 2008-06-18 14:08:39 -0500 (Wed, 18 Jun 2008) | 1 line Updated netsurf branch to use new bitmap callback table structure name that was altered in libnsgif ........ r4395 | dynis | 2008-06-18 14:54:51 -0500 (Wed, 18 Jun 2008) | 1 line Corrected param comments for bitmap_set_suspendable() ........ r4401 | dynis | 2008-06-18 18:39:50 -0500 (Wed, 18 Jun 2008) | 1 line Added references to libnsbmp where necessary; corrected function calls where callbacks were implemented ........ r4402 | dynis | 2008-06-18 18:40:47 -0500 (Wed, 18 Jun 2008) | 1 line Updated Makefile to compile with libnsbmp ........ r4403 | dynis | 2008-06-18 18:41:53 -0500 (Wed, 18 Jun 2008) | 1 line Remove NetSurf's bmpread.c and bmpread.h (replaced by libnsbmp) ........ r4423 | dynis | 2008-06-22 14:21:30 -0500 (Sun, 22 Jun 2008) | 1 line Correct a silly mistake in nsbmp_bitmap_create ........ r4485 | dynis | 2008-07-01 04:13:48 -0500 (Tue, 01 Jul 2008) | 1 line Integrated the latest versions of libnsgif and libnsbmp into NetSurf ........ r4486 | dynis | 2008-07-01 05:27:10 -0500 (Tue, 01 Jul 2008) | 1 line Altered bitmap functions to receive void pointers for proper utilisation of libnsgif and libnsbmp ........ svn path=/trunk/netsurf/; revision=5071 --- image/bitmap.h | 27 +- image/bmp.c | 54 ++- image/bmp.h | 8 +- image/bmpread.c | 816 -------------------------------------------- image/bmpread.h | 84 ----- image/gif.c | 82 +++-- image/gif.h | 4 +- image/gifread.c | 1004 ------------------------------------------------------- image/gifread.h | 88 ----- image/ico.c | 23 +- image/ico.h | 2 +- 11 files changed, 141 insertions(+), 2051 deletions(-) delete mode 100644 image/bmpread.c delete mode 100644 image/bmpread.h delete mode 100644 image/gifread.c delete mode 100644 image/gifread.h (limited to 'image') diff --git a/image/bitmap.h b/image/bitmap.h index c9bd4cdde..4a99de17e 100644 --- a/image/bitmap.h +++ b/image/bitmap.h @@ -46,19 +46,20 @@ struct content; /** An opaque image. */ struct bitmap; -struct bitmap *bitmap_create(int width, int height, unsigned int state); -void bitmap_set_opaque(struct bitmap *bitmap, bool opaque); -bool bitmap_test_opaque(struct bitmap *bitmap); -bool bitmap_get_opaque(struct bitmap *bitmap); -char *bitmap_get_buffer(struct bitmap *bitmap); -size_t bitmap_get_rowstride(struct bitmap *bitmap); -void bitmap_destroy(struct bitmap *bitmap); -bool bitmap_save(struct bitmap *bitmap, const char *path, unsigned flags); -void bitmap_modified(struct bitmap *bitmap); -void bitmap_set_suspendable(struct bitmap *bitmap, void *private_word, - void (*invalidate)(struct bitmap *bitmap, void *private_word)); +void *bitmap_create(int width, int height, unsigned int state); +void bitmap_set_opaque(void *bitmap, bool opaque); +bool bitmap_test_opaque(void *bitmap); +bool bitmap_get_opaque(void *bitmap); +unsigned char *bitmap_get_buffer(void *bitmap); +size_t bitmap_get_rowstride(void *bitmap); +size_t bitmap_get_bpp(void *bitmap); +void bitmap_destroy(void *bitmap); +bool bitmap_save(void *bitmap, const char *path, unsigned flags); +void bitmap_modified(void *bitmap); +void bitmap_set_suspendable(void *bitmap, void *private_word, + void (*invalidate)(void *bitmap, void *private_word)); -int bitmap_get_width(struct bitmap *bitmap); -int bitmap_get_height(struct bitmap *bitmap); +int bitmap_get_width(void *bitmap); +int bitmap_get_height(void *bitmap); #endif diff --git a/image/bmp.c b/image/bmp.c index 25f65bc25..440e3e282 100644 --- a/image/bmp.c +++ b/image/bmp.c @@ -1,5 +1,6 @@ /* * Copyright 2006 Richard Wilson + * Copyright 2008 Sean Fox * * This file is part of NetSurf, http://www.netsurf-browser.org/ * @@ -23,16 +24,27 @@ #include #include #include +#include #include "utils/config.h" #include "content/content.h" #include "desktop/plotters.h" #include "image/bitmap.h" #include "image/bmp.h" -#include "image/bmpread.h" #include "utils/log.h" #include "utils/messages.h" #include "utils/utils.h" +/* The Bitmap callbacks function table; + necessary for interaction with nsbmplib. +*/ +bmp_bitmap_callback_vt bmp_bitmap_callbacks = { + .bitmap_create = nsbmp_bitmap_create, + .bitmap_destroy = bitmap_destroy, + .bitmap_set_suspendable = bitmap_set_suspendable, + .bitmap_get_buffer = bitmap_get_buffer, + .bitmap_get_bpp = bitmap_get_bpp +}; + bool nsbmp_create(struct content *c, const char *params[]) { union content_msg_data msg_data; @@ -42,22 +54,22 @@ bool nsbmp_create(struct content *c, const char *params[]) { content_broadcast(c, CONTENT_MSG_ERROR, msg_data); return false; } + bmp_create(c->data.bmp.bmp, &bmp_bitmap_callbacks); return true; } bool nsbmp_convert(struct content *c, int iwidth, int iheight) { bmp_result res; - struct bmp_image *bmp; + bmp_image *bmp; union content_msg_data msg_data; + uint32_t swidth; - /* set our source data */ + /* set the bmp data */ bmp = c->data.bmp.bmp; - bmp->bmp_data = (unsigned char *) c->source_data; - bmp->buffer_size = c->source_size; /* analyse the BMP */ - res = bmp_analyse(bmp); + res = bmp_analyse(bmp, c->source_size, (unsigned char *)c->source_data); switch (res) { case BMP_OK: break; @@ -76,11 +88,13 @@ bool nsbmp_convert(struct content *c, int iwidth, int iheight) { */ c->width = bmp->width; c->height = bmp->height; + LOG(("BMP width %u height %u\n\n", c->width, c->height)); c->title = malloc(100); if (c->title) snprintf(c->title, 100, messages_get("BMPTitle"), c->width, c->height, c->source_size); - c->size += (bmp->width * bmp->height * 4) + 16 + 44 + 100; + swidth = bmp->bitmap_callbacks.bitmap_get_bpp(bmp->bitmap) * bmp->width; + c->size += (swidth * bmp->height) + 16 + 44 + 100; /* exit as a success */ c->bitmap = bmp->bitmap; @@ -97,7 +111,8 @@ bool nsbmp_redraw(struct content *c, int x, int y, float scale, unsigned long background_colour) { if (!c->data.bmp.bmp->decoded) - bmp_decode(c->data.bmp.bmp); + if (bmp_decode(c->data.bmp.bmp) != BMP_OK) + return false; c->bitmap = c->data.bmp.bmp->bitmap; return plot.bitmap(x, y, width, height, c->bitmap, background_colour, c); } @@ -110,7 +125,8 @@ bool nsbmp_redraw_tiled(struct content *c, int x, int y, bool repeat_x, bool repeat_y) { if (!c->data.bmp.bmp->decoded) - bmp_decode(c->data.bmp.bmp); + if (bmp_decode(c->data.bmp.bmp) != BMP_OK) + return false; c->bitmap = c->data.bmp.bmp->bitmap; return plot.bitmap_tile(x, y, width, height, c->bitmap, background_colour, repeat_x, repeat_y, c); @@ -124,4 +140,24 @@ void nsbmp_destroy(struct content *c) free(c->title); } + +/** + * Callback for libnsbmp; forwards the call to bitmap_create() + * + * \param width width of image in pixels + * \param height width of image in pixels + * \param state a flag word indicating the initial state + * \return an opaque struct bitmap, or NULL on memory exhaustion + */ +void *nsbmp_bitmap_create(int width, int height, unsigned int bmp_state) { + unsigned int bitmap_state = BITMAP_NEW; + + /* set bitmap state based on bmp state */ + bitmap_state |= (bmp_state & BMP_OPAQUE) ? BITMAP_OPAQUE : 0; + bitmap_state |= (bmp_state & BMP_CLEAR_MEMORY) ? BITMAP_CLEAR_MEMORY : 0; + + /* return the created bitmap */ + return bitmap_create(width, height, bitmap_state); +} + #endif diff --git a/image/bmp.h b/image/bmp.h index bd761eb95..1c0e9a8c7 100644 --- a/image/bmp.h +++ b/image/bmp.h @@ -1,5 +1,6 @@ /* * Copyright 2006 Richard Wilson + * Copyright 2008 Sean Fox * * This file is part of NetSurf, http://www.netsurf-browser.org/ * @@ -23,12 +24,14 @@ #ifdef WITH_BMP #include -#include "image/bmpread.h" +#include +#include "image/bitmap.h" struct content; +struct bitmap; struct content_bmp_data { - struct bmp_image *bmp; /** BMP image data */ + bmp_image *bmp; /** BMP image data */ }; bool nsbmp_create(struct content *c, const char *params[]); @@ -43,6 +46,7 @@ bool nsbmp_redraw_tiled(struct content *c, int x, int y, int clip_x0, int clip_y0, int clip_x1, int clip_y1, float scale, unsigned long background_colour, bool repeat_x, bool repeat_y); +void *nsbmp_bitmap_create(int width, int height, unsigned int bmp_state); #endif /* WITH_BMP */ diff --git a/image/bmpread.c b/image/bmpread.c deleted file mode 100644 index bff31167b..000000000 --- a/image/bmpread.c +++ /dev/null @@ -1,816 +0,0 @@ -/* - * Copyright 2006 Richard Wilson - * - * This file is part of NetSurf, http://www.netsurf-browser.org/ - * - * NetSurf is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; version 2 of the License. - * - * NetSurf is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include -#include -#include -#include -#include -#include -#include "image/bmpread.h" -#include "image/bitmap.h" -#include "utils/log.h" -#include "utils/config.h" - -#define READ_SHORT(a, o) (a[o]|(a[o+1]<<8)) -#define READ_INT(a, o) (a[o]|(a[o+1]<<8)|(a[o+2]<<16)|(a[o+3]<<24)) - -bmp_result bmp_analyse_header(struct bmp_image *bmp, char *data); -bmp_result bmp_decode_rgb24(struct bmp_image *bmp, char **start, int bytes); -bmp_result bmp_decode_rgb16(struct bmp_image *bmp, char **start, int bytes); -bmp_result bmp_decode_rgb(struct bmp_image *bmp, char **start, int bytes); -bmp_result bmp_decode_mask(struct bmp_image *bmp, char *data, int bytes); -bmp_result bmp_decode_rle(struct bmp_image *bmp, char *data, int bytes, int size); -void bmp_invalidate(struct bitmap *bitmap, void *private_word); - - -/** - * 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. - * - * \param bmp the BMP image to analyse - * \return BMP_OK on success - */ -bmp_result bmp_analyse(struct bmp_image *bmp) { - char *data = (char *) bmp->bmp_data; - - /* ensure we aren't already initialised */ - if (bmp->bitmap) - return BMP_OK; - - /* standard 14-byte BMP file header is: - * - * +0 SHORT 'BM' - * +2 INT size of file (in bytes) - * +6 SHORT reserved field (1) - * +8 SHORT reserved field (2) - * +10 INT starting position of image data (in bytes) - */ - if (bmp->buffer_size < 14) - return BMP_INSUFFICIENT_DATA; - if ((data[0] != 'B') || (data[1] != 'M')) - return BMP_DATA_ERROR; - bmp->bitmap_offset = READ_INT(data, 10); - - /* decode the BMP header */ - return bmp_analyse_header(bmp, data + 14); -} - - -/** - * 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(). - * - * \param ico the ICO image to analyse - * \return BMP_OK on success - */ -bmp_result ico_analyse(struct ico_collection *ico) { - char *data = (char *) ico->ico_data; - unsigned int count, i; - bmp_result result; - struct ico_image *image; - int area, max_area = 0; - - /* ensure we aren't already initialised */ - if (ico->first) - return BMP_OK; - - /* standard 6-byte ICO file header is: - * - * +0 INT 0x00010000 - * +4 SHORT number of BMPs to follow - */ - if (ico->buffer_size < 6) - return BMP_INSUFFICIENT_DATA; - if (READ_INT(data, 0) != 0x00010000) - return BMP_DATA_ERROR; - count = READ_SHORT(data, 4); - if (count == 0) - return BMP_DATA_ERROR; - data += 6; - - /* decode the BMP files */ - if (ico->buffer_size < 6 + (16 * count)) - return BMP_INSUFFICIENT_DATA; - for (i = 0; i < count; i++) { - image = calloc(1, sizeof(struct ico_image)); - if (!image) - return BMP_INSUFFICIENT_MEMORY; - image->next = ico->first; - ico->first = image; - image->bmp.width = data[0]; - image->bmp.height = data[1]; - image->bmp.buffer_size = READ_INT(data, 8) + 40; - image->bmp.bmp_data = ico->ico_data + READ_INT(data, 12); - image->bmp.ico = true; - data += 16; - result = bmp_analyse_header(&image->bmp, - (char *) image->bmp.bmp_data); - if (result != BMP_OK) - return result; - area = image->bmp.width * image->bmp.height; - if (area > max_area) { - ico->width = image->bmp.width; - ico->height = image->bmp.height; - max_area = area; - } - } - return BMP_OK; -} - - -bmp_result bmp_analyse_header(struct bmp_image *bmp, char *data) { - unsigned int header_size; - unsigned int i; - int width, height, j; - int palette_size; - unsigned int flags; - - /* a variety of different bitmap headers can follow, depending - * on the BMP variant. A full description of the various headers - * can be found at http://www.fileformat.info/format/bmp/ - */ - header_size = READ_INT(data, 0); - if (bmp->buffer_size < (14 + header_size)) - return BMP_INSUFFICIENT_DATA; - if (header_size == 12) { - /* the following header is for os/2 and windows 2.x and consists of: - * - * +0 INT size of this header (in bytes) - * +4 SHORT image width (in pixels) - * +6 SHORT image height (in pixels) - * +8 SHORT number of color planes (always 1) - * +10 SHORT number of bits per pixel - */ - width = READ_SHORT(data, 4); - height = READ_SHORT(data, 6); - if (width < 0) - return BMP_DATA_ERROR; - if (height < 0) { - bmp->reversed = true; - height = -height; - } - bmp->width = width; - bmp->height = height; - if (READ_SHORT(data, 8) != 1) - return BMP_DATA_ERROR; - bmp->bpp = READ_SHORT(data, 10); - bmp->colours = (1 << bmp->bpp); - palette_size = 3; - } else if (header_size < 40) { - return BMP_DATA_ERROR; - } else { - /* the following header is for windows 3.x and onwards. it is a - * minimum of 40 bytes and (as of Windows 95) a maximum of 108 bytes. - * - * +0 INT size of this header (in bytes) - * +4 INT image width (in pixels) - * +8 INT image height (in pixels) - * +12 SHORT number of color planes (always 1) - * +14 SHORT number of bits per pixel - * +16 INT compression methods used - * +20 INT size of bitmap (in bytes) - * +24 INT horizontal resolution (in pixels per meter) - * +28 INT vertical resolution (in pixels per meter) - * +32 INT number of colors in the image - * +36 INT number of important colors - * +40 INT mask identifying bits of red component - * +44 INT mask identifying bits of green component - * +48 INT mask identifying bits of blue component - * +52 INT mask identifying bits of alpha component - * +56 INT color space type - * +60 INT x coordinate of red endpoint - * +64 INT y coordinate of red endpoint - * +68 INT z coordinate of red endpoint - * +72 INT x coordinate of green endpoint - * +76 INT y coordinate of green endpoint - * +80 INT z coordinate of green endpoint - * +84 INT x coordinate of blue endpoint - * +88 INT y coordinate of blue endpoint - * +92 INT z coordinate of blue endpoint - * +96 INT gamma red coordinate scale value - * +100 INT gamma green coordinate scale value - * +104 INT gamma blue coordinate scale value - */ - if (!bmp->ico) { - width = READ_INT(data, 4); - height = READ_INT(data, 8); - if (width < 0) - return BMP_DATA_ERROR; - if (height < 0) { - bmp->reversed = true; - height = -height; - } - bmp->width = width; - bmp->height = height; - } - if (READ_SHORT(data, 12) != 1) - return BMP_DATA_ERROR; - bmp->bpp = READ_SHORT(data, 14); - if (bmp->bpp == 0) - bmp->bpp = 8; - bmp->encoding = READ_INT(data, 16); - if (bmp->encoding > BMP_ENCODING_BITFIELDS) - return BMP_DATA_ERROR; - if (bmp->encoding == BMP_ENCODING_BITFIELDS) { - if ((bmp->bpp != 16) && (bmp->bpp != 32)) - return BMP_DATA_ERROR; - if (header_size == 40) { - header_size += 12; - if (bmp->buffer_size < (14 + header_size)) - return BMP_INSUFFICIENT_DATA; - for (i = 0; i < 3; i++) - bmp->mask[i] = READ_INT(data, 40 + (i << 2)); - } else { - for (i = 0; i < 4; i++) - bmp->mask[i] = READ_INT(data, 40 + (i << 2)); - } - for (i = 0; i < 4; i++) { - if (bmp->mask[i] == 0) - break; - for (j = 31; j > 0; j--) - if (bmp->mask[i] & (1 << j)) { - if ((j - 7) > 0) - bmp->mask[i] &= 0xff << (j - 7); - else - bmp->mask[i] &= 0xff >> (-(j - 7)); - bmp->shift[i] = (i << 3) - (j - 7); - break; - } - } - } - bmp->colours = READ_INT(data, 32); - if (bmp->colours == 0) - bmp->colours = (1 << bmp->bpp); - palette_size = 4; - } - data += header_size; - - /* we only have a palette for <16bpp */ - if (bmp->bpp < 16) { - /* we now have a series of palette entries of the format: - * - * +0 BYTE blue - * +1 BYTE green - * +2 BYTE red - * - * if the palette is from an OS/2 or Win2.x file then the entries - * are padded with an extra byte. - */ - if (bmp->buffer_size < (14 + header_size + (4 * bmp->colours))) - return BMP_INSUFFICIENT_DATA; - bmp->colour_table = (unsigned int *) - malloc(bmp->colours * sizeof(int)); - if (!bmp->colour_table) - return BMP_INSUFFICIENT_MEMORY; - for (i = 0; i < bmp->colours; i++) { - bmp->colour_table[i] = data[2] | (data[1] << 8) | - (data[0] << 16); - data += palette_size; - } - } - - /* create our bitmap */ - flags = BITMAP_NEW | BITMAP_CLEAR_MEMORY; - if ((!bmp->ico) && (bmp->mask[3] == 0)) - flags |= BITMAP_OPAQUE; - bmp->bitmap = bitmap_create(bmp->width, bmp->height, flags); - if (!bmp->bitmap) { - if (bmp->colour_table) - free(bmp->colour_table); - bmp->colour_table = NULL; - return BMP_INSUFFICIENT_MEMORY; - } - bmp->bitmap_offset = (intptr_t)data - (intptr_t)bmp->bmp_data; - bitmap_set_suspendable(bmp->bitmap, bmp, bmp_invalidate); - return BMP_OK; -} - - -/* - * 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 - * \param height the preferred height - */ -struct bmp_image *ico_find(struct ico_collection *ico, int width, int height) { - struct bmp_image *bmp = NULL; - struct ico_image *image; - int x, y, cur, distance = (1 << 24); - - for (image = ico->first; image; image = image->next) { - if (((int)image->bmp.width == width) && ((int)image->bmp.height == height)) - return &image->bmp; - x = image->bmp.width - width; - y = image->bmp.height - height; - cur = (x * x) + (y * y); - if (cur < distance) { - distance = cur; - bmp = &image->bmp; - } - } - return bmp; -} - - -/** - * Invalidates a BMP - * - * This function sets the BMP into a state such that the bitmap image data - * can be released from memory. - * - * \param bmp the BMP image to invalidate - */ -void bmp_invalidate(struct bitmap *bitmap, void *private_word) { - struct bmp_image *bmp = (struct bmp_image *)private_word; - - bmp->decoded = false; -} - - -/** - * 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(struct bmp_image *bmp) { - char *data; - int bytes; - bmp_result result = BMP_OK; - - assert(bmp->bitmap); - - data = (char *) bmp->bmp_data + bmp->bitmap_offset; - bytes = bmp->buffer_size - bmp->bitmap_offset; - - switch (bmp->encoding) { - case BMP_ENCODING_RGB: - if (bmp->bpp >= 24) - result = bmp_decode_rgb24(bmp, &data, bytes); - else if (bmp->bpp > 8) - result = bmp_decode_rgb16(bmp, &data, bytes); - else - result = bmp_decode_rgb(bmp, &data, bytes); - break; - case BMP_ENCODING_RLE8: - result = bmp_decode_rle(bmp, data, bytes, 8); - break; - case BMP_ENCODING_RLE4: - result = bmp_decode_rle(bmp, data, bytes, 4); - break; - case BMP_ENCODING_BITFIELDS: - if (bmp->bpp == 32) - result = bmp_decode_rgb24(bmp, &data, bytes); - else if (bmp->bpp == 16) - result = bmp_decode_rgb16(bmp, &data, bytes); - else - return BMP_DATA_ERROR; - } - - if ((!bmp->ico) || (result != BMP_OK)) - return result; - - bytes = (intptr_t)bmp->bmp_data + bmp->buffer_size - (intptr_t)data; - return bmp_decode_mask(bmp, data, bytes); -} - - -/** - * Decode BMP data stored in 24bpp colour. - * - * \param bmp the BMP image to decode - * \param start the data to decode, updated to last byte read on success - * \param bytes the number of bytes of data available - * \return BMP_OK on success - */ -bmp_result bmp_decode_rgb24(struct bmp_image *bmp, char **start, int bytes) { - char *top, *bottom, *end, *data; - unsigned int *scanline; - unsigned int x, y, swidth, skip; - intptr_t addr; - unsigned int i, word; - - data = *start; - swidth = bitmap_get_rowstride(bmp->bitmap); - top = bitmap_get_buffer(bmp->bitmap); - if (!top) - return BMP_INSUFFICIENT_MEMORY; - bottom = top + swidth * (bmp->height - 1); - end = data + bytes; - addr = ((intptr_t)data) & 3; - skip = bmp->bpp >> 3; - bmp->decoded = true; - - for (y = 0; y < bmp->height; y++) { - while (addr != (((intptr_t)data) & 3)) - data++; - if ((data + (skip * bmp->width)) > end) - return BMP_INSUFFICIENT_DATA; - if (bmp->reversed) - scanline = (unsigned int *)(top + (y * swidth)); - else - scanline = (unsigned int *)(bottom - (y * swidth)); - if (bmp->encoding == BMP_ENCODING_BITFIELDS) { - for (x = 0; x < bmp->width; x++) { - word = data[0] | (data[1] << 8) | (data[2] << 16) | - (data[3] << 24); - scanline[x] = (0xff << 24); - for (i = 0; i < 4; i++) - if (bmp->shift[i] > 0) - scanline[x] ^= ((word & bmp->mask[i]) << - bmp->shift[i]); - else - scanline[x] ^= ((word & bmp->mask[i]) >> - (-bmp->shift[i])); - data += 4; - } - } else { - for (x = 0; x < bmp->width; x++) { - scanline[x] = data[2] | (data[1] << 8) | (data[0] << 16) | - (data[3] << 24); - data += skip; - } - } - } - *start = data; - return BMP_OK; -} - - -/** - * Decode BMP data stored in 16bpp colour. - * - * \param bmp the BMP image to decode - * \param start the data to decode, updated to last byte read on success - * \param bytes the number of bytes of data available - * \return BMP_OK on success - */ -bmp_result bmp_decode_rgb16(struct bmp_image *bmp, char **start, int bytes) { - char *top, *bottom, *end, *data; - unsigned int *scanline; - unsigned int x, y, swidth; - intptr_t addr; - unsigned int word, i; - - data = *start; - swidth = bitmap_get_rowstride(bmp->bitmap); - top = bitmap_get_buffer(bmp->bitmap); - if (!top) - return BMP_INSUFFICIENT_MEMORY; - bottom = top + swidth * (bmp->height - 1); - end = data + bytes; - addr = ((intptr_t)data) & 3; - bmp->decoded = true; - - for (y = 0; y < bmp->height; y++) { - if (addr != (((intptr_t)data) & 3)) - data += 2; - if ((data + (2 * bmp->width)) > end) - return BMP_INSUFFICIENT_DATA; - if (bmp->reversed) - scanline = (unsigned int *)(top + (y * swidth)); - else - scanline = (unsigned int *)(bottom - (y * swidth)); - if (bmp->encoding == BMP_ENCODING_BITFIELDS) { - for (x = 0; x < bmp->width; x++) { - word = data[0] | (data[1] << 8); - scanline[x] = (0xff << 24); - for (i = 0; i < 4; i++) - if (bmp->shift[i] > 0) - scanline[x] ^= ((word & bmp->mask[i]) << - bmp->shift[i]); - else - scanline[x] ^= ((word & bmp->mask[i]) >> - (-bmp->shift[i])); - data += 2; - } - } else { - for (x = 0; x < bmp->width; x++) { - word = data[0] | (data[1] << 8); - scanline[x] = ((word & (31 << 0)) << 19) | - ((word & (31 << 5)) << 6) | - ((word & (31 << 10)) >> 7); - data += 2; - } - } - } - *start = data; - return BMP_OK; -} - - -/** - * Decode BMP data stored with a palette and in 8bpp colour or less. - * - * \param bmp the BMP image to decode - * \param start the data to decode, updated to last byte read on success - * \param bytes the number of bytes of data available - * \return BMP_OK on success - */ -bmp_result bmp_decode_rgb(struct bmp_image *bmp, char **start, int bytes) { - char *top, *bottom, *end, *data; - unsigned int *scanline; - intptr_t addr; - unsigned int x, y, swidth; - int i; - int bit_shifts[8]; - int ppb = 8 / bmp->bpp; - int bit_mask = (1 << bmp->bpp) - 1; - int cur_byte = 0, bit; - - for (i = 0; i < ppb; i++) - bit_shifts[i] = 8 - ((i + 1) * bmp->bpp); - - data = *start; - swidth = bitmap_get_rowstride(bmp->bitmap); - top = bitmap_get_buffer(bmp->bitmap); - if (!top) - return BMP_INSUFFICIENT_MEMORY; - bottom = top + swidth * (bmp->height - 1); - end = data + bytes; - addr = ((intptr_t)data) & 3; - bmp->decoded = true; - - for (y = 0; y < bmp->height; y++) { - while (addr != (((intptr_t)data) & 3)) - data++; - bit = 32; - if ((data + (bmp->width / ppb)) > end) - return BMP_INSUFFICIENT_DATA; - if (bmp->reversed) - scanline = (unsigned int *)(top + (y * swidth)); - else - scanline = (unsigned int *)(bottom - (y * swidth)); - for (x = 0; x < bmp->width; x++) { - if (bit >= ppb) { - bit = 0; - cur_byte = *data++; - } - scanline[x] = bmp->colour_table[(cur_byte >> - bit_shifts[bit++]) & bit_mask]; - } - } - *start = data; - return BMP_OK; -} - - -/** - * Decode a 1bpp mask for an ICO - * - * \param bmp the BMP image to decode - * \param data the data to decode - * \param bytes the number of bytes of data available - * \return BMP_OK on success - */ -bmp_result bmp_decode_mask(struct bmp_image *bmp, char *data, int bytes) { - char *top, *bottom, *end; - unsigned int *scanline; - intptr_t addr; - unsigned int x, y, swidth; - int cur_byte = 0; - - swidth = bitmap_get_rowstride(bmp->bitmap); - top = bitmap_get_buffer(bmp->bitmap); - if (!top) - return BMP_INSUFFICIENT_MEMORY; - bottom = top + swidth * (bmp->height - 1); - end = data + bytes; - addr = ((intptr_t)data) & 3; - - for (y = 0; y < bmp->height; y++) { - while (addr != (((intptr_t)data) & 3)) - data++; - if ((data + (bmp->width >> 3)) > end) - return BMP_INSUFFICIENT_DATA; - scanline = (unsigned int *)(bottom - (y * swidth)); - for (x = 0; x < bmp->width; x++) { - if ((x & 7) == 0) - cur_byte = *data++; - if ((cur_byte & 128) == 0) - scanline[x] |= (0xff << 24); - cur_byte = cur_byte << 1; - } - } - return BMP_OK; -} - - -/** - * Decode BMP data stored encoded in either RLE4 or RLE8. - * - * \param bmp the BMP image to decode - * \param data the data to decode - * \param bytes the number of bytes of data available - * \param size the size of the RLE tokens (4 or 8) - * \return BMP_OK on success - */ -bmp_result bmp_decode_rle(struct bmp_image *bmp, char *data, int bytes, int size) { - char *top, *bottom, *end; - unsigned int *scanline; - unsigned int swidth; - int i, length, pixels_left; - unsigned int x = 0, y = 0, last_y = 0; - unsigned int pixel = 0, pixel2; - - if (bmp->ico) - return BMP_DATA_ERROR; - - swidth = bitmap_get_rowstride(bmp->bitmap); - top = bitmap_get_buffer(bmp->bitmap); - if (!top) - return BMP_INSUFFICIENT_MEMORY; - bottom = top + swidth * (bmp->height - 1); - end = data + bytes; - bmp->decoded = true; - - do { - if (data + 2 > end) - return BMP_INSUFFICIENT_DATA; - length = *data++; - if (length == 0) { - length = *data++; - if (length == 0) { - /* 00 - 00 means end of scanline */ - x = 0; - if (last_y == y) { - if (++y > bmp->height) - return BMP_DATA_ERROR; - } - last_y = y; - } else if (length == 1) { - /* 00 - 01 means end of RLE data */ - return BMP_OK; - } else if (length == 2) { - /* 00 - 02 - XX - YY means move cursor */ - if (data + 2 > end) - return BMP_INSUFFICIENT_DATA; - x += *data++; - if (x >= bmp->width) - return BMP_DATA_ERROR; - y += *data++; - if (y >= bmp->height) - return BMP_DATA_ERROR; - } else { - /* 00 - NN means escape NN pixels */ - if (bmp->reversed) { - pixels_left = (y + 1) * bmp->width - x; - scanline = (unsigned int *)(top + (y * swidth)); - } else { - pixels_left = (bmp->height - y + 1) * bmp->width - x; - scanline = (unsigned int *)(bottom - (y * swidth)); - } - if (length > pixels_left) - length = pixels_left; - if (data + length > end) - return BMP_INSUFFICIENT_DATA; - - /* the following code could be easily optimised by simply - * checking the bounds on entry and using some simply copying - * routines if so */ - if (size == 8) { - for (i = 0; i < length; i++) { - if (x >= bmp->width) { - x = 0; - if (++y > bmp->height) - return BMP_DATA_ERROR; - scanline -= bmp->width; - } - scanline[x++] = bmp->colour_table[(int)*data++]; - } - } else { - for (i = 0; i < length; i++) { - if (x >= bmp->width) { - x = 0; - if (++y > bmp->height) - return BMP_DATA_ERROR; - scanline -= bmp->width; - } - if ((i & 1) == 0) { - pixel = *data++; - scanline[x++] = bmp->colour_table - [pixel >> 4]; - } else { - scanline[x++] = bmp->colour_table - [pixel & 0xf]; - } - } - length = (length + 1) >> 1; - } - if ((length & 1) && (*data++ != 0x00)) - return BMP_DATA_ERROR; - - } - } else { - /* NN means perform RLE for NN pixels */ - if (bmp->reversed) { - pixels_left = (y + 1) * bmp->width - x; - scanline = (unsigned int *)(top + (y * swidth)); - } else { - pixels_left = (bmp->height - y + 1) * bmp->width - x; - scanline = (unsigned int *)(bottom - (y * swidth)); - } - if (length > pixels_left) - length = pixels_left; - - /* the following code could be easily optimised by simply - * checking the bounds on entry and using some simply copying - * routines if so */ - if (size == 8) { - pixel = bmp->colour_table[(int)*data++]; - for (i = 0; i < length; i++) { - if (x >= bmp->width) { - x = 0; - if (++y > bmp->height) - return BMP_DATA_ERROR; - scanline -= bmp->width; - } - scanline[x++] = pixel; - } - } else { - pixel2 = *data++; - pixel = bmp->colour_table[pixel2 >> 4]; - pixel2 = bmp->colour_table[pixel2 & 0xf]; - for (i = 0; i < length; i++) { - if (x >= bmp->width) { - x = 0; - if (++y > bmp->height) - return BMP_DATA_ERROR; - scanline -= bmp->width; - } - if ((i & 1) == 0) - scanline[x++] = pixel; - else - scanline[x++] = pixel2; - } - } - } - } while (data < end); - return BMP_OK; -} - - -/** - * Finalise a BMP prior to destruction. - * - * \param bmp the BMP image to finalise - */ -void bmp_finalise(struct bmp_image *bmp) { - if (bmp->bitmap) - bitmap_destroy(bmp->bitmap); - bmp->bitmap = NULL; - if (bmp->colour_table) - free(bmp->colour_table); - bmp->colour_table = NULL; -} - - -/** - * Finalise an ICO prior to destruction. - * - * \param ico the ICO image to finalise - */ -void ico_finalise(struct ico_collection *ico) { - struct ico_image *image; - - for (image = ico->first; image; image = image->next) - bmp_finalise(&image->bmp); - while (ico->first) { - image = ico->first; - ico->first = image->next; - free(image); - } -} diff --git a/image/bmpread.h b/image/bmpread.h deleted file mode 100644 index e1ed6c3d9..000000000 --- a/image/bmpread.h +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Copyright 2006 Richard Wilson - * - * This file is part of NetSurf, http://www.netsurf-browser.org/ - * - * NetSurf is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; version 2 of the License. - * - * NetSurf is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -/** \file - * BMP file decoding (interface). - */ - -#ifndef _NETSURF_IMAGE_BMPREAD_H_ -#define _NETSURF_IMAGE_BMPREAD_H_ - -#include -#include "image/bitmap.h" - -/* 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; - -struct bmp_image { - unsigned char *bmp_data; /** pointer to BMP data */ - unsigned int buffer_size; /** total number of bytes of BMP data available */ - unsigned int width; /** width of BMP (valid after _analyse) */ - unsigned int height; /** heigth of BMP (valid after _analyse) */ - bmp_encoding encoding; /** pixel encoding type */ - unsigned int bitmap_offset; /** offset of bitmap data */ - unsigned int bpp; /** bits per pixel */ - unsigned int colours; /** number of colours */ - unsigned int *colour_table; /** colour table */ - bool reversed; /** scanlines are top to bottom */ - bool decoded; /** whether the image has been decoded */ - bool ico; /** image is part of an ICO, mask follows */ - unsigned int mask[4]; /** four bitwise mask */ - int shift[4]; /** four bitwise shifts */ - struct bitmap *bitmap; /** decoded image */ -}; - -struct ico_image { - struct bmp_image bmp; - struct ico_image *next; -}; - -struct ico_collection { - unsigned char *ico_data; /** pointer to ICO data */ - unsigned int buffer_size; /** total number of bytes of ICO data available */ - unsigned int width; /** width of largest BMP */ - unsigned int height; /** heigth of largest BMP */ - struct ico_image *first; -}; - -bmp_result bmp_analyse(struct bmp_image *bmp); -bmp_result bmp_decode(struct bmp_image *bmp); -void bmp_finalise(struct bmp_image *bmp); - -bmp_result ico_analyse(struct ico_collection *ico); -struct bmp_image *ico_find(struct ico_collection *ico, int width, int height); -void ico_finalise(struct ico_collection *ico); - -#endif diff --git a/image/gif.c b/image/gif.c index eff95db16..37705c66c 100644 --- a/image/gif.c +++ b/image/gif.c @@ -1,6 +1,7 @@ /* * Copyright 2003 John M Bell * Copyright 2004 Richard Wilson + * Copyright 2008 Sean Fox * * This file is part of NetSurf, http://www.netsurf-browser.org/ * @@ -24,13 +25,14 @@ #include #include #include +#include +#include "utils/config.h" #include "content/content.h" #include "desktop/browser.h" #include "desktop/options.h" #include "desktop/plotters.h" #include "image/bitmap.h" #include "image/gif.h" -#include "image/gifread.h" #include "utils/log.h" #include "utils/messages.h" #include "utils/utils.h" @@ -46,9 +48,21 @@ [rjw] - Sun 4th April 2004 */ -static void nsgif_invalidate(struct bitmap *bitmap, void *private_word); +static void nsgif_invalidate(void *bitmap, void *private_word); static void nsgif_animate(void *p); -static void nsgif_get_frame(struct content *c); +static gif_result nsgif_get_frame(struct content *c); + +/* The Bitmap callbacks function table; + necessary for interaction with nsgiflib. +*/ +gif_bitmap_callback_vt gif_bitmap_callbacks = { + .bitmap_create = nsgif_bitmap_create, + .bitmap_destroy = bitmap_destroy, + .bitmap_get_buffer = bitmap_get_buffer, + .bitmap_set_opaque = bitmap_set_opaque, + .bitmap_test_opaque = bitmap_test_opaque, + .bitmap_modified = bitmap_modified +}; bool nsgif_create(struct content *c, const char *params[]) { @@ -61,6 +75,7 @@ bool nsgif_create(struct content *c, const char *params[]) { content_broadcast(c, CONTENT_MSG_ERROR, msg_data); return false; } + gif_create(c->data.gif.gif, &gif_bitmap_callbacks); return true; } @@ -70,27 +85,31 @@ bool nsgif_convert(struct content *c, int iwidth, int iheight) { struct gif_animation *gif; union content_msg_data msg_data; - /* Create our animation + /* Get the animation */ gif = c->data.gif.gif; - gif->gif_data = (unsigned char *) c->source_data; - gif->buffer_size = c->source_size; - gif->buffer_position = 0; /* Initialise the GIF */ - res = gif_initialise(gif); - switch (res) { - case GIF_INSUFFICIENT_MEMORY: - msg_data.error = messages_get("NoMemory"); - content_broadcast(c, CONTENT_MSG_ERROR, msg_data); - return false; - case GIF_INSUFFICIENT_DATA: - case GIF_DATA_ERROR: - msg_data.error = messages_get("BadGIF"); + do { + res = gif_initialise(gif, c->source_size, (unsigned char *)c->source_data); + if (res != GIF_OK && res != GIF_WORKING) { + switch (res) + { + case GIF_INSUFFICIENT_FRAME_DATA: + case GIF_FRAME_DATA_ERROR: + case GIF_INSUFFICIENT_DATA: + case GIF_DATA_ERROR: + msg_data.error = messages_get("BadGIF"); + break; + case GIF_INSUFFICIENT_MEMORY: + msg_data.error = messages_get("NoMemory"); + break; + } content_broadcast(c, CONTENT_MSG_ERROR, msg_data); return false; - } + } + } while (res != GIF_OK); /* Abort on bad GIFs */ @@ -127,7 +146,7 @@ bool nsgif_convert(struct content *c, int iwidth, int iheight) { return true; } -void nsgif_invalidate(struct bitmap *bitmap, void *private_word) { +void nsgif_invalidate(void *bitmap, void *private_word) { struct gif_animation *gif = (struct gif_animation *)private_word; gif->decoded_frame = -1; @@ -139,7 +158,8 @@ bool nsgif_redraw(struct content *c, int x, int y, float scale, unsigned long background_colour) { if (c->data.gif.current_frame != c->data.gif.gif->decoded_frame) - nsgif_get_frame(c); + if (nsgif_get_frame(c) != GIF_OK) + return false; c->bitmap = c->data.gif.gif->frame_image; return plot.bitmap(x, y, width, height, c->bitmap, background_colour, c); } @@ -151,8 +171,11 @@ bool nsgif_redraw_tiled(struct content *c, int x, int y, float scale, unsigned long background_colour, bool repeat_x, bool repeat_y) { + gif_result res; + if (c->data.gif.current_frame != c->data.gif.gif->decoded_frame) - nsgif_get_frame(c); + if (nsgif_get_frame(c) != GIF_OK) + return false; c->bitmap = c->data.gif.gif->frame_image; return plot.bitmap_tile(x, y, width, height, c->bitmap, background_colour, repeat_x, repeat_y, c); @@ -161,6 +184,7 @@ bool nsgif_redraw_tiled(struct content *c, int x, int y, void nsgif_destroy(struct content *c) { + /* Free all the associated memory buffers */ schedule_remove(nsgif_animate, c); @@ -175,7 +199,7 @@ void nsgif_destroy(struct content *c) * * \param c the content to update */ -void nsgif_get_frame(struct content *c) { +gif_result nsgif_get_frame(struct content *c) { int previous_frame, current_frame, frame; current_frame = c->data.gif.current_frame; @@ -186,7 +210,7 @@ void nsgif_get_frame(struct content *c) { else previous_frame = c->data.gif.gif->decoded_frame + 1; for (frame = previous_frame; frame <= current_frame; frame++) - gif_decode_frame(c->data.gif.gif, frame); + return gif_decode_frame(c->data.gif.gif, frame); } @@ -229,7 +253,7 @@ void nsgif_animate(void *p) schedule(delay, nsgif_animate, c); } - if (!option_animate_images) + if ((!option_animate_images) || (!gif->frames[c->data.gif.current_frame].display)) return; /* area within gif to redraw */ @@ -286,4 +310,16 @@ void nsgif_animate(void *p) content_broadcast(c, CONTENT_MSG_REDRAW, data); } + +/** + * Callback for libnsgif; forwards the call to bitmap_create() + * + * \param width width of image in pixels + * \param height width of image in pixels + * \return an opaque struct bitmap, or NULL on memory exhaustion + */ +void *nsgif_bitmap_create(int width, int height) { + return bitmap_create(width, height, BITMAP_NEW); +} + #endif diff --git a/image/gif.h b/image/gif.h index 4a1a5c4c5..612aa1ad5 100644 --- a/image/gif.h +++ b/image/gif.h @@ -1,5 +1,6 @@ /* * Copyright 2004 Richard Wilson + * Copyright 2008 Sean Fox * * This file is part of NetSurf, http://www.netsurf-browser.org/ * @@ -23,7 +24,7 @@ #ifdef WITH_GIF #include -#include "image/gifread.h" +#include struct content; @@ -44,6 +45,7 @@ bool nsgif_redraw_tiled(struct content *c, int x, int y, int clip_x0, int clip_y0, int clip_x1, int clip_y1, float scale, unsigned long background_colour, bool repeat_x, bool repeat_y); +void *nsgif_bitmap_create(int width, int height); #endif /* WITH_GIF */ diff --git a/image/gifread.c b/image/gifread.c deleted file mode 100644 index 30fd19e0f..000000000 --- a/image/gifread.c +++ /dev/null @@ -1,1004 +0,0 @@ -/* - * Copyright 2004 Richard Wilson - * - * This file is part of NetSurf, http://www.netsurf-browser.org/ - * - * NetSurf is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; version 2 of the License. - * - * NetSurf is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include -#include -#include -#include -#include "gifread.h" -#include "image/bitmap.h" -#include "utils/log.h" - -/* READING GIF FILES - ================= - - The functions provided by this file 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 - 'background_action' 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. - - [rjw] - Fri 2nd April 2004 -*/ - - - -/* Internal GIF routines -*/ -static int gif_initialise_sprite(struct gif_animation *gif, unsigned int width, unsigned int height); -static int gif_initialise_frame(struct gif_animation *gif); -static unsigned int gif_interlaced_line(int height, int y); - - - -/* Internal LZW routines -*/ -static void gif_init_LZW(struct gif_animation *gif); -static bool gif_next_LZW(struct gif_animation *gif); -static int gif_next_code(struct gif_animation *gif, int code_size); - -/* General LZW values. They are shared for all GIFs being decoded, and - thus we can't handle progressive decoding efficiently without having - the data for each image which would use an extra 10Kb or so per GIF. -*/ -static unsigned char buf[4]; -static unsigned char *direct; -static int maskTbl[16] = {0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, - 0x00ff, 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff}; -static int table[2][(1 << GIF_MAX_LZW)]; -static unsigned char stack[(1 << GIF_MAX_LZW) * 2]; -static unsigned char *stack_pointer; -static int code_size, set_code_size; -static int max_code, max_code_size; -static int clear_code, end_code; -static int curbit, lastbit, last_byte; -static int firstcode, oldcode; -static bool zero_data_block = false; -static bool get_done; - -/* Whether to clear the decoded image rather than plot -*/ -static bool clear_image = false; - - -/* 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 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 - 0 for successful decoding - 1 for successful decoding (all frames completely read) -*/ -int gif_initialise(struct gif_animation *gif) { - unsigned char *gif_data; - unsigned int index; - int return_value; - - /* Check for sufficient data to be a GIF - */ - if (gif->buffer_size < 13) 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 = -1; - - /* Check we are a GIF - */ - if (strncmp((const char *) gif_data, "GIF", 3) != 0) - return GIF_DATA_ERROR; - gif_data += 3; - - /* Check we are a GIF type 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; - - /* Get our GIF data. - */ - gif->width = gif_data[0] | (gif_data[1] << 8); - gif->height = gif_data[2] | (gif_data[3] << 8); - gif->global_colours = (gif_data[4] & 0x80); - gif->colour_table_size = (2 << (gif_data[4] & 0x07)); - gif->background_colour = gif_data[5]; - gif->aspect_ratio = gif_data[6]; - gif->dirty_frame = -1; - 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 = (unsigned int *)calloc(GIF_MAX_COLOURS, sizeof(int)); - gif->local_colour_table = (unsigned int *)calloc(GIF_MAX_COLOURS, sizeof(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] = 0xaa000000; - - /* Initialise enough workspace for 4 frame initially - */ - if ((gif->frames = (gif_frame *)malloc(sizeof(gif_frame))) == NULL) { - gif_finalise(gif); - return GIF_INSUFFICIENT_MEMORY; - } - gif->frame_holders = 1; - - /* Initialise the sprite header - */ - if ((gif->frame_image = bitmap_create(gif->width, gif->height, BITMAP_NEW)) == 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] == 0xaa000000) { - /* Check for a global colour map signified by bit 7 - */ - if (gif->global_colours) { - if (gif->buffer_size < (gif->colour_table_size * 3 + 12)) { - return GIF_INSUFFICIENT_DATA; - } - for (index = 0; index < gif->colour_table_size; index++) { - char colour[] = {gif_data[0], gif_data[1], gif_data[2], 0xff}; - gif->global_colour_table[index] = *((int *) colour); - 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 - */ - gif->global_colour_table[0] = 0xff000000; - gif->global_colour_table[1] = 0xffffffff; - } - } - - /* Repeatedly try to decode frames - */ - while ((return_value = gif_initialise_frame(gif)) == 0); - - /* 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; -} - - - -/** Updates the sprite memory size - - @return -3 for a memory error - 0 for success -*/ -static int gif_initialise_sprite(struct 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 0; - - /* Get our maximum values - */ - max_width = (width > gif->width) ? width : gif->width; - max_height = (height > gif->height) ? height : gif->height; - - /* Allocate some more memory - */ - if ((buffer = bitmap_create(max_width, max_height, BITMAP_NEW)) == NULL) - return GIF_INSUFFICIENT_MEMORY; - 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 = -1; - return 0; -} - - -/* Attempts to initialise the next frame - - @return -4 for insufficient data to process the entire frame - -3 for a memory error - -2 for a data error - -1 for insufficient data to process anything - 0 for success - 1 for success (GIF terminator found) -*/ -int gif_initialise_frame(struct gif_animation *gif) { - int frame; - gif_frame *temp_buf; - - unsigned char *gif_data, *gif_end; - int gif_bytes; - unsigned int flags = 0; - unsigned int background_action; - unsigned int width, height, offset_x, offset_y; - unsigned int extension_size, colour_table_size; - unsigned int block_size; - bool more_images = true; - bool first_image = true; - - /* 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 we have enough data for at least the header, or if we've finished - */ - if ((gif_bytes > 0) && (gif_data[0] == 0x3b)) return 1; - if (gif_bytes < 11) return -1; - - /* 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_DATA_ERROR; - - /* Get some memory to store our pointers in etc. - */ - if ((int)gif->frame_holders <= frame) { - /* Allocate more memory - */ - if ((temp_buf = (gif_frame *)realloc(gif->frames, - (frame + 1) * sizeof(gif_frame))) == 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].virgin = true; - 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 = -1; - - /* 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. - */ - while (more_images) { - - /* Ensure we have some data - */ - if ((gif_end - gif_data) < 10) - return GIF_INSUFFICIENT_FRAME_DATA; - - /* Decode the extensions - */ - background_action = 0; - while (gif_data[0] == 0x21) { - /* Get the extension size - */ - extension_size = gif_data[2]; - - /* Check we've enough data for the extension then header - */ - if ((gif_end - gif_data) < (int)(extension_size + 13)) - return GIF_INSUFFICIENT_FRAME_DATA; - - /* Graphic control extension - store the frame delay. - */ - if (gif_data[1] == 0xf9) { - gif->frames[frame].frame_delay = gif_data[4] | (gif_data[5] << 8); - background_action = ((gif_data[3] & 0x1c) >> 2); - more_images = false; - - /* Application extension - handle NETSCAPE2.0 looping - */ - } else if ((gif_data[1] == 0xff) && - (gif_data[2] == 0x0b) && - (strncmp((const char *) gif_data + 3, - "NETSCAPE2.0", 11) == 0) && - (gif_data[14] == 0x03) && - (gif_data[15] == 0x01)) { - gif->loop_count = gif_data[16] | (gif_data[17] << 8); - } - - /* Move to the first sub-block - */ - gif_data += 2; - - /* Skip all the sub-blocks - */ - while (gif_data[0] != 0x00) { - gif_data += gif_data[0] + 1; - if ((gif_end - gif_data) < 10) return GIF_INSUFFICIENT_FRAME_DATA; - } - gif_data++; - } - - /* We must have at least one image descriptor - */ - if (gif_data[0] != 0x2c) return GIF_FRAME_DATA_ERROR; - - /* Do some simple boundary checking - */ - 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 = ((background_action == 2) || (background_action == 3)); - - /* 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 & 0x07); - - /* 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 & 0x80) { - 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_data[0] > GIF_MAX_LZW) - return GIF_DATA_ERROR; - - /* Move our data onwards - */ - gif_data++; - if (--gif_bytes < 0) - return GIF_INSUFFICIENT_FRAME_DATA; - - /* Repeatedly skip blocks until we get a zero block or run out of data - */ - block_size = 0; - while (block_size != 1) { - /* Skip the block data - */ - block_size = gif_data[0] + 1; - if ((gif_bytes -= block_size) < 0) - return GIF_INSUFFICIENT_FRAME_DATA; - gif_data += block_size; - } - - /* Check for end of data - */ - more_images &= !((gif_bytes < 1) || (gif_data[0] == 0x3b)); - } - - /* Check if we've finished - */ - if (gif_bytes < 1) - return GIF_INSUFFICIENT_FRAME_DATA; - else { - gif->buffer_position = gif_data - gif->gif_data; - gif->frame_count = frame + 1; - if (gif_data[0] == 0x3b) return 1; - } - return 0; -} - - -/** Decodes a GIF frame. - - @return 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 - 0 for successful decoding -*/ -int gif_decode_frame(struct gif_animation *gif, unsigned int frame) { - unsigned int index = 0; - unsigned char *gif_data; - unsigned char *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 extension_size; - unsigned int background_action; - int transparency_index = -1; - unsigned int save_buffer_position; - unsigned int return_value = 0; - unsigned int x, y, decode_y, burst_bytes; - unsigned int block_size; - register int colour; - bool more_images = true; - - /* Ensure we have a frame to decode - */ - if (frame > gif->frame_count_partial) - return GIF_INSUFFICIENT_DATA; - if ((!clear_image) && ((int)frame == gif->decoded_frame)) - return 0; - - /* If the previous frame was dirty, remove it - */ - if (!clear_image) { - if (frame == 0) - gif->dirty_frame = -1; - if (gif->decoded_frame == gif->dirty_frame) { - clear_image = true; - if (frame != 0) - gif_decode_frame(gif, gif->dirty_frame); - clear_image = false; - } - gif->dirty_frame = -1; - } - - /* 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); - - /* Check we have enough data for the header - */ - if (gif_bytes < 9) - return GIF_INSUFFICIENT_DATA; - - /* Clear the previous frame totally. We can't just pretend we've got a smaller - sprite and clear what we need as some frames have multiple images which would - produce errors. - */ - frame_data = (unsigned int *)bitmap_get_buffer(gif->frame_image); - if (!frame_data) - return GIF_INSUFFICIENT_MEMORY; - if (!clear_image) { - if ((frame == 0) || (gif->decoded_frame == -1)) - memset((char*)frame_data, 0x00, gif->width * gif->height * sizeof(int)); - gif->decoded_frame = frame; - } - - /* Save the buffer position - */ - save_buffer_position = gif->buffer_position; - gif->buffer_position = gif_data - gif->gif_data; - - /* We've got to do this more than one time if we've got multiple images - */ - while (more_images) { - background_action = 0; - - /* Ensure we have some data - */ - gif_data = gif->gif_data + gif->buffer_position; - if ((gif_end - gif_data) < 10) { - return_value = GIF_INSUFFICIENT_FRAME_DATA; - break; - } - - /* Decode the extensions - */ - while (gif_data[0] == 0x21) { - - /* Get the extension size - */ - extension_size = gif_data[2]; - - /* Check we've enough data for the extension then header - */ - if ((gif_end - gif_data) < (int)(extension_size + 13)) { - return_value = GIF_INSUFFICIENT_FRAME_DATA; - break; - } - - /* Graphic control extension - store the frame delay. - */ - if (gif_data[1] == 0xf9) { - flags = gif_data[3]; - if (flags & 0x01) transparency_index = gif_data[6]; - background_action = ((flags & 0x1c) >> 2); - more_images = false; - } - /* Move to the first sub-block - */ - gif_data += 2; - - /* Skip all the sub-blocks - */ - while (gif_data[0] != 0x00) { - gif_data += gif_data[0] + 1; - if ((gif_end - gif_data) < 10) { - return_value = GIF_INSUFFICIENT_FRAME_DATA; - break; - } - } - gif_data++; - } - - /* Decode the header - */ - if (gif_data[0] != 0x2c) { - return_value = GIF_DATA_ERROR; - break; - } - 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; - break; - } - - /* Decode the flags - */ - flags = gif_data[9]; - colour_table_size = 2 << (flags & 0x07); - interlace = flags & 0x40; - - /* Move through our data - */ - gif_data += 10; - gif_bytes = (int)(gif_end - gif_data); - - /* Set up the colour table - */ - if (flags & 0x80) { - if (gif_bytes < (int)(3 * colour_table_size)) { - return_value = GIF_INSUFFICIENT_FRAME_DATA; - break; - } - colour_table = gif->local_colour_table; - if (!clear_image) { - for (index = 0; index < colour_table_size; index++) { - char colour[] = {gif_data[0], gif_data[1], - gif_data[2], 0xff}; - colour_table[index] = *((int *) colour); - gif_data += 3; - } - } else { - gif_data += 3 * colour_table_size; - } - gif_bytes = (int)(gif_end - gif_data); - } else { - colour_table = gif->global_colour_table; - } - - /* If we are clearing the image we just clear, if not decode - */ - if (!clear_image) { - /* Set our dirty status - */ - if ((background_action == 2) || (background_action == 3)) - gif->dirty_frame = frame; - - /* Initialise the LZW decoding - */ - set_code_size = gif_data[0]; - gif->buffer_position = (gif_data - gif->gif_data) + 1; - - /* Set our code variables - */ - code_size = set_code_size + 1; - clear_code = (1 << set_code_size); - end_code = clear_code + 1; - max_code_size = clear_code << 1; - max_code = clear_code + 2; - curbit = lastbit = 0; - last_byte = 2; - get_done = false; - direct = buf; - gif_init_LZW(gif); - - /* 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_pointer - stack); - if (burst_bytes > 0) { - if (burst_bytes > x) - burst_bytes = x; - x -= burst_bytes; - while (burst_bytes-- > 0) { - if ((colour = *--stack_pointer) != transparency_index) - *frame_scanline = colour_table[colour]; - frame_scanline++; - } - } else { - if (!gif_next_LZW(gif)) { - return_value = gif->current_error; - goto gif_decode_frame_exit; - } - } - } - } - } else { - /* Clear our frame - */ - if ((background_action == 2) || (background_action == 3)) { - for (y = 0; y < height; y++) { - frame_scanline = frame_data + offset_x + ((offset_y + y) * gif->width); - memset(frame_scanline, 0x00, width * 4); - } - } - - /* Repeatedly skip blocks until we get a zero block or run out of data - */ - gif_bytes = gif->buffer_size - gif->buffer_position; - gif_data = gif->gif_data + gif->buffer_size; - block_size = 0; - while (block_size != 1) { - /* Skip the block data - */ - block_size = gif_data[0] + 1; - if ((gif_bytes -= block_size) < 0) { - return_value = GIF_INSUFFICIENT_FRAME_DATA; - goto gif_decode_frame_exit; - } - gif_data += block_size; - } - } -gif_decode_frame_exit: - - /* Check for end of data - */ - gif_bytes = gif->buffer_size - gif->buffer_position; - more_images &= !((gif_bytes < 1) || (gif_data[0] == 0x3b)); - gif->buffer_position++; - } - - /* Check if we should test for optimisation - */ - if (gif->frames[frame].virgin) { - gif->frames[frame].opaque = bitmap_test_opaque(gif->frame_image); - gif->frames[frame].virgin = false; - } - bitmap_set_opaque(gif->frame_image, gif->frames[frame].opaque); - bitmap_modified(gif->frame_image); - - /* Restore the buffer position - */ - gif->buffer_position = save_buffer_position; - - /* Success! - */ - return return_value; - -} - -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; -} - -/* Releases any workspace held by the animation -*/ -void gif_finalise(struct gif_animation *gif) { - /* Release all our memory blocks - */ - if (gif->frame_image) - 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; -} - -/** - * Initialise LZW decoding - */ -void gif_init_LZW(struct gif_animation *gif) { - int i; - - gif->current_error = 0; - if (clear_code >= (1 << GIF_MAX_LZW)) { - stack_pointer = stack; - gif->current_error = GIF_FRAME_DATA_ERROR; - return; - } - - /* initialise our table */ - memset(table, 0x00, (1 << GIF_MAX_LZW) * 8); - for (i = 0; i < clear_code; ++i) - table[1][i] = i; - - /* update our LZW parameters */ - code_size = set_code_size + 1; - max_code_size = clear_code << 1; - max_code = clear_code + 2; - stack_pointer = stack; - do { - firstcode = oldcode = gif_next_code(gif, code_size); - } while (firstcode == clear_code); - *stack_pointer++ =firstcode; -} - - -static bool gif_next_LZW(struct gif_animation *gif) { - int code, incode; - int block_size; - int new_code; - - code = gif_next_code(gif, code_size); - if (code < 0) { - gif->current_error = code; - return false; - } else if (code == clear_code) { - gif_init_LZW(gif); - return true; - } else if (code == end_code) { - /* skip to the end of our data so multi-image GIFs work */ - if (zero_data_block) { - gif->current_error = GIF_FRAME_DATA_ERROR; - return false; - } - block_size = 0; - while (block_size != 1) { - block_size = gif->gif_data[gif->buffer_position] + 1; - gif->buffer_position += block_size; - } - gif->current_error = GIF_FRAME_DATA_ERROR; - return false; - } - - incode = code; - if (code >= max_code) { - *stack_pointer++ = firstcode; - code = oldcode; - } - - /* The following loop is the most important in the GIF decoding cycle as every - * single pixel passes through it. - * - * Note: our stack is always big enough to hold a complete decompressed chunk. */ - while (code >= clear_code) { - *stack_pointer++ = table[1][code]; - new_code = table[0][code]; - if (new_code < clear_code) { - code = new_code; - break; - } - *stack_pointer++ = table[1][new_code]; - code = table[0][new_code]; - if (code == new_code) { - gif->current_error = GIF_FRAME_DATA_ERROR; - return false; - } - } - - *stack_pointer++ = firstcode = table[1][code]; - - if ((code = max_code) < (1 << GIF_MAX_LZW)) { - table[0][code] = oldcode; - table[1][code] = firstcode; - ++max_code; - if ((max_code >= max_code_size) && (max_code_size < (1 << GIF_MAX_LZW))) { - max_code_size = max_code_size << 1; - ++code_size; - } - } - oldcode = incode; - return true; -} - -static int gif_next_code(struct gif_animation *gif, int code_size) { - int i, j, end, count, ret; - unsigned char *b; - - end = curbit + code_size; - if (end >= lastbit) { - if (get_done) - return GIF_INSUFFICIENT_FRAME_DATA; - buf[0] = direct[last_byte - 2]; - buf[1] = direct[last_byte - 1]; - - /* get the next block */ - direct = gif->gif_data + gif->buffer_position; - zero_data_block = ((count = direct[0]) == 0); - if ((gif->buffer_position + count) >= gif->buffer_size) - return GIF_INSUFFICIENT_FRAME_DATA; - if (count == 0) - get_done = true; - else { - direct -= 1; - buf[2] = direct[2]; - buf[3] = direct[3]; - } - gif->buffer_position += count + 1; - - /* update our variables */ - last_byte = 2 + count; - curbit = (curbit - lastbit) + 16; - lastbit = (2 + count) << 3; - end = curbit + code_size; - } - - i = curbit >> 3; - if (i < 2) - b = buf; - else - b = direct; - - ret = b[i]; - j = (end >> 3) - 1; - if (i <= j) { - ret |= (b[i + 1] << 8); - if (i < j) - ret |= (b[i + 2] << 16); - } - ret = (ret >> (curbit % 8)) & maskTbl[code_size]; - curbit += code_size; - return ret; -} diff --git a/image/gifread.h b/image/gifread.h deleted file mode 100644 index 292c5a86d..000000000 --- a/image/gifread.h +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright 2004 Richard Wilson - * - * This file is part of NetSurf, http://www.netsurf-browser.org/ - * - * NetSurf is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; version 2 of the License. - * - * NetSurf is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -/** \file - * Progressive animated GIF file decoding (interface). - */ - -#ifndef _NETSURF_IMAGE_GIFREAD_H_ -#define _NETSURF_IMAGE_GIFREAD_H_ - -#include -#include "image/bitmap.h" - -/* Error return values -*/ -#define GIF_INSUFFICIENT_FRAME_DATA -1 -#define GIF_FRAME_DATA_ERROR -2 -#define GIF_INSUFFICIENT_DATA -3 -#define GIF_DATA_ERROR -4 -#define GIF_INSUFFICIENT_MEMORY -5 - -/* Maximum colour table size -*/ -#define GIF_MAX_COLOURS 256 - -/* Maximum LZW bits available -*/ -#define GIF_MAX_LZW 12 - -/* The GIF frame data -*/ -typedef struct gif_frame { - unsigned int frame_pointer; /**< offset (in bytes) to the GIF frame data */ - unsigned int frame_delay; /**< delay (in cs) before animating the frame */ - bool virgin; /**< whether the frame has previously been used */ - bool opaque; /**< whether the frame is totally opaque */ - bool redraw_required; /**< whether a forcable screen redraw is required */ - unsigned int redraw_x; /**< x co-ordinate of redraw rectangle */ - unsigned int redraw_y; /**< y co-ordinate of redraw rectangle */ - unsigned int redraw_width; /**< width of redraw rectangle */ - unsigned int redraw_height; /**< height of redraw rectangle */ -} gif_frame; - -/* The GIF animation data -*/ -typedef struct gif_animation { - unsigned char *gif_data; /**< pointer to GIF data */ - unsigned int buffer_position; /**< current index into GIF data */ - unsigned int buffer_size; /**< total number of bytes of GIF data available */ - unsigned int frame_holders; /**< current number of frame holders */ - int decoded_frame; /**< current frame decoded to bitmap */ - int loop_count; /**< number of times to loop animation */ - gif_frame *frames; /**< decoded frames */ - unsigned int width; /**< width of GIF (may increase during decoding) */ - unsigned int height; /**< heigth of GIF (may increase during decoding) */ - unsigned int frame_count; /**< number of frames decoded */ - unsigned int frame_count_partial; /**< number of frames partially decoded */ - unsigned int background_colour; /**< image background colour */ - unsigned int aspect_ratio; /**< image aspect ratio (ignored) */ - unsigned int colour_table_size; /**< size of colour table (in entries) */ - bool global_colours; /**< whether the GIF has a global colour table */ - unsigned int *global_colour_table; /**< global colour table */ - unsigned int *local_colour_table; /**< local colour table */ - int dirty_frame; /**< the current dirty frame, or -1 for none */ - struct bitmap *frame_image; /**< currently decoded image */ - int current_error; /**< current error type, or 0 for none*/ -} gif_animation; - -int gif_initialise(struct gif_animation *gif); -int gif_decode_frame(struct gif_animation *gif, unsigned int frame); -void gif_finalise(struct gif_animation *gif); - -#endif diff --git a/image/ico.c b/image/ico.c index b7e8d923c..d5977a829 100644 --- a/image/ico.c +++ b/image/ico.c @@ -23,11 +23,11 @@ #include #include #include +#include #include "utils/config.h" #include "content/content.h" #include "desktop/plotters.h" #include "image/bitmap.h" -#include "image/bmpread.h" #include "image/ico.h" #include "utils/log.h" #include "utils/messages.h" @@ -35,13 +35,16 @@ bool nsico_create(struct content *c, const char *params[]) { union content_msg_data msg_data; + extern bmp_bitmap_callback_vt bmp_bitmap_callbacks; /**< external structure containing + * bitmap callback functions */ - c->data.ico.ico = calloc(sizeof(struct ico_collection), 1); + c->data.ico.ico = calloc(sizeof(ico_collection), 1); if (!c->data.ico.ico) { msg_data.error = messages_get("NoMemory"); content_broadcast(c, CONTENT_MSG_ERROR, msg_data); return false; } + ico_collection_create(c->data.ico.ico, &bmp_bitmap_callbacks); return true; } @@ -49,16 +52,14 @@ bool nsico_create(struct content *c, const char *params[]) { bool nsico_convert(struct content *c, int iwidth, int iheight) { struct bmp_image *bmp; bmp_result res; - struct ico_collection *ico; + ico_collection *ico; union content_msg_data msg_data; - /* set our source data */ + /* set the ico data */ ico = c->data.ico.ico; - ico->ico_data = (unsigned char *) c->source_data; - ico->buffer_size = c->source_size; - /* analyse the BMP */ - res = ico_analyse(ico); + /* analyse the ico */ + res = ico_analyse(ico, c->source_size, (unsigned char *) c->source_data); switch (res) { case BMP_OK: break; @@ -99,7 +100,8 @@ bool nsico_redraw(struct content *c, int x, int y, float scale, unsigned long background_colour) { struct bmp_image *bmp = ico_find(c->data.ico.ico, width, height); if (!bmp->decoded) - bmp_decode(bmp); + if (bmp_decode(bmp) != BMP_OK) + return false; c->bitmap = bmp->bitmap; return plot.bitmap(x, y, width, height, c->bitmap, background_colour, c); @@ -113,7 +115,8 @@ bool nsico_redraw_tiled(struct content *c, int x, int y, bool repeat_x, bool repeat_y) { struct bmp_image *bmp = ico_find(c->data.ico.ico, width, height); if (!bmp->decoded) - bmp_decode(bmp); + if (bmp_decode(bmp) != BMP_OK) + return false; c->bitmap = bmp->bitmap; return plot.bitmap_tile(x, y, width, height, c->bitmap, background_colour, repeat_x, repeat_y, c); diff --git a/image/ico.h b/image/ico.h index d11120015..0927700d9 100644 --- a/image/ico.h +++ b/image/ico.h @@ -23,7 +23,7 @@ #ifdef WITH_BMP #include -#include "image/bmpread.h" +#include struct content; -- cgit v1.2.3