r4472 joty - in /trunk/libparserutils: src/charset/encodings/ test/ test/data/cscodec-utf16/ test/data/cscodec-utf8/ test/data/cscodec/
by netsurf@semichrome.net
Author: joty
Date: Sat Jun 28 15:57:47 2008
New Revision: 4472
URL: http://source.netsurf-browser.org?rev=4472&view=rev
Log:
- parserutils_charset_utf16_to_ucs4(): fixed surrogate handling.
- cscodec-utf8.c(run_test): Added more asserts.
- Added UTF-16 tester (based on the UTF-8 one).
Added:
trunk/libparserutils/test/cscodec-utf16.c
- copied, changed from r4467, trunk/libparserutils/test/cscodec.c
trunk/libparserutils/test/cscodec-utf8.c
- copied, changed from r4467, trunk/libparserutils/test/cscodec.c
trunk/libparserutils/test/data/cscodec-utf16/
trunk/libparserutils/test/data/cscodec-utf16/INDEX
trunk/libparserutils/test/data/cscodec-utf16/simple.dat (with props)
trunk/libparserutils/test/data/cscodec-utf8/
- copied from r4467, trunk/libparserutils/test/data/cscodec/
Removed:
trunk/libparserutils/test/cscodec.c
trunk/libparserutils/test/data/cscodec/
Modified:
trunk/libparserutils/src/charset/encodings/utf16.c
trunk/libparserutils/test/INDEX
trunk/libparserutils/test/Makefile
Modified: trunk/libparserutils/src/charset/encodings/utf16.c
URL: http://source.netsurf-browser.org/trunk/libparserutils/src/charset/encodi...
==============================================================================
--- trunk/libparserutils/src/charset/encodings/utf16.c (original)
+++ trunk/libparserutils/src/charset/encodings/utf16.c Sat Jun 28 15:57:47 2008
@@ -19,7 +19,7 @@
* Convert a UTF-16 sequence into a single UCS-4 character
*
* \param s The sequence to process
- * \param len Length of sequence
+ * \param len Length of sequence in bytes
* \param ucs4 Pointer to location to receive UCS-4 character (host endian)
* \param clen Pointer to location to receive byte length of UTF-16 sequence
* \return PARSERUTILS_OK on success, appropriate error otherwise
@@ -38,17 +38,22 @@
if (*ss < 0xD800 || *ss > 0xDFFF) {
*ucs4 = *ss;
*clen = 2;
- } else if (0xD800 <= *ss && *ss <= 0xBFFF) {
+ } else if (0xD800 <= *ss && *ss <= 0xDBFF) {
+ /* High-surrogate code unit. */
if (len < 4)
return PARSERUTILS_NEEDDATA;
- if (0xDC00 <= ss[1] && ss[1] <= 0xE000) {
- *ucs4 = (((s[0] >> 6) & 0x1f) + 1) |
- ((s[0] & 0x3f) | (s[1] & 0x3ff));
+ if (0xDC00 <= ss[1] && ss[1] <= 0xDFFF) {
+ /* We have a valid surrogate pair. */
+ *ucs4 = (((ss[0] & 0x3FF) << 10) | (ss[1] & 0x3FF))
+ + (1<<16);
*clen = 4;
} else {
return PARSERUTILS_INVALID;
}
+ } else {
+ /* Low-surrogate code unit. */
+ return PARSERUTILS_INVALID;
}
return PARSERUTILS_OK;
Modified: trunk/libparserutils/test/INDEX
URL: http://source.netsurf-browser.org/trunk/libparserutils/test/INDEX?rev=447...
==============================================================================
--- trunk/libparserutils/test/INDEX (original)
+++ trunk/libparserutils/test/INDEX Sat Jun 28 15:57:47 2008
@@ -5,7 +5,8 @@
charset Charset initialisation/finalisation
parserutils Library initialisation/finalisation
aliases Encoding alias handling
-cscodec Charset codec implementation cscodec
+cscodec-utf8 UTF-8 charset codec implementation cscodec-utf8
+cscodec-utf16 UTF-16 charset codec implementation cscodec-utf16
dict Dictionary handling
rbtree Red-black tree implementation
filter Input stream filtering
Modified: trunk/libparserutils/test/Makefile
URL: http://source.netsurf-browser.org/trunk/libparserutils/test/Makefile?rev=...
==============================================================================
--- trunk/libparserutils/test/Makefile (original)
+++ trunk/libparserutils/test/Makefile Sat Jun 28 15:57:47 2008
@@ -35,8 +35,8 @@
override CFLAGS := $(CFLAGS) -I$(TOP)/src/ -I$(d)
# Tests
-TESTS_$(d) := aliases cscodec charset dict filter inputstream parserutils \
- rbtree
+TESTS_$(d) := aliases cscodec-utf8 cscodec-utf16 charset dict filter \
+ inputstream parserutils rbtree
TESTS_$(d) := $(TESTS_$(d)) regression/cscodec-segv regression/filter-segv \
regression/stream-nomem
Copied: trunk/libparserutils/test/cscodec-utf16.c (from r4467, trunk/libparserutils/test/cscodec.c)
URL: http://source.netsurf-browser.org/trunk/libparserutils/test/cscodec-utf16...
==============================================================================
--- trunk/libparserutils/test/cscodec.c (original)
+++ trunk/libparserutils/test/cscodec-utf16.c Sat Jun 28 15:57:47 2008
@@ -1,5 +1,10 @@
+#include <ctype.h>
#include <stdio.h>
#include <string.h>
+
+/* These two are for htonl / ntohl */
+#include <arpa/inet.h>
+#include <netinet/in.h>
#include "charset/charset.h"
#include <parserutils/charset/codec.h>
@@ -51,7 +56,7 @@
assert(parserutils_charset_codec_create("NATS-SEFI-ADD",
myrealloc, NULL) == NULL);
- ctx.codec = parserutils_charset_codec_create("UTF-8", myrealloc, NULL);
+ ctx.codec = parserutils_charset_codec_create("UTF-16", myrealloc, NULL);
assert(ctx.codec != NULL);
ctx.buflen = parse_filesize(argv[2]);
@@ -97,6 +102,17 @@
printf("PASS\n");
return 0;
+}
+
+/**
+ * Converts hex character ('0' ... '9' or 'a' ... 'f' or 'A' ... 'F') to
+ * digit value.
+ * \param hex Valid hex character
+ * \return Corresponding digit value.
+ */
+static inline int hex2digit(char hex)
+{
+ return (hex <= '9') ? hex - '0' : (hex | 0x20) - 'a' + 10;
}
bool handle_line(const char *data, size_t datalen, void *pw)
@@ -170,12 +186,61 @@
}
} else {
if (ctx->indata) {
- memcpy(ctx->buf + ctx->bufused, data, datalen);
- ctx->bufused += datalen;
+ /* Process "&#xNNNN" as 16-bit code units. */
+ while (datalen) {
+ if (data[0] == '\n') {
+ ctx->buf[ctx->bufused++] = *data++;
+ --datalen;
+ continue;
+ }
+ assert(datalen >= sizeof ("&#xNNNN")-1 \
+ && data[0] == '&' && data[1] == '#' \
+ && data[2] == 'x' && isxdigit(data[3]) \
+ && isxdigit(data[4]) && isxdigit(data[5]) \
+ && isxdigit(data[6]));
+ /* UTF-16 code is always host endian (different
+ than UCS-32 !). */
+ ctx->buf[ctx->bufused++]
+ = (hex2digit(data[5]) << 4) | hex2digit(data[6]);
+ ctx->buf[ctx->bufused++]
+ = (hex2digit(data[3]) << 4) | hex2digit(data[4]);
+ data += sizeof ("&#xNNNN")-1;
+ datalen -= sizeof ("&#xNNNN")-1;
+ }
}
if (ctx->inexp) {
- memcpy(ctx->exp + ctx->expused, data, datalen);
- ctx->expused += datalen;
+ /* Process "&#xXXXXYYYY as 32-bit code units. */
+ while (datalen) {
+ if (data[0] == '\n') {
+ ctx->exp[ctx->expused++] = *data++;
+ --datalen;
+ continue;
+ }
+ assert(datalen >= sizeof ("&#xXXXXYYYY")-1 \
+ && data[0] == '&' && data[1] == '#' \
+ && data[2] == 'x' && isxdigit(data[3]) \
+ && isxdigit(data[4]) && isxdigit(data[5]) \
+ && isxdigit(data[6]) && isxdigit(data[7]) \
+ && isxdigit(data[8]) && isxdigit(data[9]) \
+ && isxdigit(data[10]));
+ /* UCS-4 code is always big endian, so convert
+ host endian to big endian. */
+ const uint32_t nCodePoint =
+ htonl((hex2digit(data[3]) << 28)
+ | (hex2digit(data[4]) << 24)
+ | (hex2digit(data[5]) << 20)
+ | (hex2digit(data[6]) << 16)
+ | (hex2digit(data[7]) << 12)
+ | (hex2digit(data[8]) << 8)
+ | (hex2digit(data[9]) << 4)
+ | hex2digit(data[10]));
+ ctx->exp[ctx->expused++] = (nCodePoint >> 0) & 0xFF;
+ ctx->exp[ctx->expused++] = (nCodePoint >> 8) & 0xFF;
+ ctx->exp[ctx->expused++] = (nCodePoint >> 16) & 0xFF;
+ ctx->exp[ctx->expused++] = (nCodePoint >> 24) & 0xFF;
+ data += sizeof ("&#xXXXXYYYY")-1;
+ datalen -= sizeof ("&#xXXXXYYYY")-1;
+ }
}
}
@@ -204,15 +269,33 @@
size_t templen = ctx->bufused * 4;
uint8_t temp[templen];
uint8_t *ptemp = temp;
+ const uint8_t *ptemp2;
+ size_t templen2;
assert(parserutils_charset_codec_decode(ctx->codec,
&psrc, &srclen,
&ptemp, &templen) == ctx->exp_ret);
- ptemp = temp;
- templen = ctx->bufused * 4 - templen;
+ /* \todo currently there is no way to specify the number of
+ consumed & produced data in case of a deliberate bad input
+ data set. */
+ if (ctx->exp_ret == PARSERUTILS_OK) {
+ assert(temp + (ctx->bufused * 4 - templen) == ptemp);
+ }
+
+ ptemp2 = temp;
+ templen2 = ctx->bufused * 4 - templen;
assert(parserutils_charset_codec_encode(ctx->codec,
- (const uint8_t **) &ptemp, &templen,
+ &ptemp2, &templen2,
&pdest, &destlen) == ctx->exp_ret);
+ if (ctx->exp_ret == PARSERUTILS_OK) {
+ assert(templen2 == 0);
+ assert(temp + (ctx->bufused * 4 - templen) == ptemp2);
+ }
+ }
+ if (ctx->exp_ret == PARSERUTILS_OK) {
+ assert(srclen == 0);
+ assert(ctx->buf + ctx->bufused == psrc);
+ assert(dest + (ctx->bufused * 4 - destlen) == pdest);
}
printf("%d: Read '", ++testnum);
@@ -227,6 +310,7 @@
}
printf("'\n");
+ assert(pdest == dest + ctx->expused);
assert(memcmp(dest, ctx->exp, ctx->expused) == 0);
}
Copied: trunk/libparserutils/test/cscodec-utf8.c (from r4467, trunk/libparserutils/test/cscodec.c)
URL: http://source.netsurf-browser.org/trunk/libparserutils/test/cscodec-utf8....
==============================================================================
--- trunk/libparserutils/test/cscodec.c (original)
+++ trunk/libparserutils/test/cscodec-utf8.c Sat Jun 28 15:57:47 2008
@@ -204,15 +204,33 @@
size_t templen = ctx->bufused * 4;
uint8_t temp[templen];
uint8_t *ptemp = temp;
+ const uint8_t *ptemp2;
+ size_t templen2;
assert(parserutils_charset_codec_decode(ctx->codec,
&psrc, &srclen,
&ptemp, &templen) == ctx->exp_ret);
- ptemp = temp;
- templen = ctx->bufused * 4 - templen;
+ /* \todo currently there is no way to specify the number of
+ consumed & produced data in case of a deliberate bad input
+ data set. */
+ if (ctx->exp_ret == PARSERUTILS_OK) {
+ assert(temp + (ctx->bufused * 4 - templen) == ptemp);
+ }
+
+ ptemp2 = temp;
+ templen2 = ctx->bufused * 4 - templen;
assert(parserutils_charset_codec_encode(ctx->codec,
- (const uint8_t **) &ptemp, &templen,
+ &ptemp2, &templen2,
&pdest, &destlen) == ctx->exp_ret);
+ if (ctx->exp_ret == PARSERUTILS_OK) {
+ assert(templen2 == 0);
+ assert(temp + (ctx->bufused * 4 - templen) == ptemp2);
+ }
+ }
+ if (ctx->exp_ret == PARSERUTILS_OK) {
+ assert(srclen == 0);
+ assert(ctx->buf + ctx->bufused == psrc);
+ assert(dest + (ctx->bufused * 4 - destlen) == pdest);
}
printf("%d: Read '", ++testnum);
@@ -227,6 +245,7 @@
}
printf("'\n");
+ assert(pdest == dest + ctx->expused);
assert(memcmp(dest, ctx->exp, ctx->expused) == 0);
}
Removed: trunk/libparserutils/test/cscodec.c
URL: http://source.netsurf-browser.org/trunk/libparserutils/test/cscodec.c?rev...
==============================================================================
--- trunk/libparserutils/test/cscodec.c (original)
+++ trunk/libparserutils/test/cscodec.c (removed)
@@ -1,232 +1,0 @@
-#include <stdio.h>
-#include <string.h>
-
-#include "charset/charset.h"
-#include <parserutils/charset/codec.h>
-
-#include "utils/utils.h"
-
-#include "testutils.h"
-
-typedef struct line_ctx {
- parserutils_charset_codec *codec;
-
- size_t buflen;
- size_t bufused;
- uint8_t *buf;
- size_t explen;
- size_t expused;
- uint8_t *exp;
-
- bool indata;
- bool inexp;
-
- parserutils_error exp_ret;
-
- enum { ENCODE, DECODE, BOTH } dir;
-} line_ctx;
-
-static bool handle_line(const char *data, size_t datalen, void *pw);
-static void run_test(line_ctx *ctx);
-
-static void *myrealloc(void *ptr, size_t len, void *pw)
-{
- UNUSED(pw);
-
- return realloc(ptr, len);
-}
-
-int main(int argc, char **argv)
-{
- line_ctx ctx;
-
- if (argc != 3) {
- printf("Usage: %s <aliases_file> <filename>\n", argv[0]);
- return 1;
- }
-
- assert(parserutils_charset_initialise(argv[1], myrealloc, NULL) ==
- PARSERUTILS_OK);
-
- assert(parserutils_charset_codec_create("NATS-SEFI-ADD",
- myrealloc, NULL) == NULL);
-
- ctx.codec = parserutils_charset_codec_create("UTF-8", myrealloc, NULL);
- assert(ctx.codec != NULL);
-
- ctx.buflen = parse_filesize(argv[2]);
- if (ctx.buflen == 0)
- return 1;
-
- ctx.buf = malloc(2 * ctx.buflen);
- if (ctx.buf == NULL) {
- printf("Failed allocating %u bytes\n",
- (unsigned int) ctx.buflen);
- return 1;
- }
-
- ctx.exp = ctx.buf + ctx.buflen;
- ctx.explen = ctx.buflen;
-
- ctx.buf[0] = '\0';
- ctx.exp[0] = '\0';
- ctx.bufused = 0;
- ctx.expused = 0;
- ctx.indata = false;
- ctx.inexp = false;
- ctx.exp_ret = PARSERUTILS_OK;
-
- assert(parse_testfile(argv[2], handle_line, &ctx) == true);
-
- /* and run final test */
- if (ctx.bufused > 0 && ctx.buf[ctx.bufused - 1] == '\n')
- ctx.bufused -= 1;
-
- if (ctx.expused > 0 && ctx.exp[ctx.expused - 1] == '\n')
- ctx.expused -= 1;
-
- run_test(&ctx);
-
- free(ctx.buf);
-
- parserutils_charset_codec_destroy(ctx.codec);
-
- assert(parserutils_charset_finalise(myrealloc, NULL) ==
- PARSERUTILS_OK);
-
- printf("PASS\n");
-
- return 0;
-}
-
-bool handle_line(const char *data, size_t datalen, void *pw)
-{
- line_ctx *ctx = (line_ctx *) pw;
-
- if (data[0] == '#') {
- if (ctx->inexp) {
- /* This marks end of testcase, so run it */
-
- if (ctx->buf[ctx->bufused - 1] == '\n')
- ctx->bufused -= 1;
-
- if (ctx->exp[ctx->expused - 1] == '\n')
- ctx->expused -= 1;
-
- run_test(ctx);
-
- ctx->buf[0] = '\0';
- ctx->exp[0] = '\0';
- ctx->bufused = 0;
- ctx->expused = 0;
- ctx->exp_ret = PARSERUTILS_OK;
- }
-
- if (strncasecmp(data+1, "data", 4) == 0) {
- parserutils_charset_codec_optparams params;
- const char *ptr = data + 6;
-
- ctx->indata = true;
- ctx->inexp = false;
-
- if (strncasecmp(ptr, "decode", 6) == 0)
- ctx->dir = DECODE;
- else if (strncasecmp(ptr, "encode", 6) == 0)
- ctx->dir = ENCODE;
- else
- ctx->dir = BOTH;
-
- ptr += 7;
-
- if (strncasecmp(ptr, "LOOSE", 5) == 0) {
- params.error_mode.mode =
- PARSERUTILS_CHARSET_CODEC_ERROR_LOOSE;
- ptr += 6;
- } else if (strncasecmp(ptr, "STRICT", 6) == 0) {
- params.error_mode.mode =
- PARSERUTILS_CHARSET_CODEC_ERROR_STRICT;
- ptr += 7;
- } else {
- params.error_mode.mode =
- PARSERUTILS_CHARSET_CODEC_ERROR_TRANSLIT;
- ptr += 9;
- }
-
- assert(parserutils_charset_codec_setopt(ctx->codec,
- PARSERUTILS_CHARSET_CODEC_ERROR_MODE,
- (parserutils_charset_codec_optparams *) ¶ms)
- == PARSERUTILS_OK);
- } else if (strncasecmp(data+1, "expected", 8) == 0) {
- ctx->indata = false;
- ctx->inexp = true;
-
- ctx->exp_ret = parserutils_error_from_string(data + 10,
- datalen - 10 - 1 /* \n */);
- } else if (strncasecmp(data+1, "reset", 5) == 0) {
- ctx->indata = false;
- ctx->inexp = false;
-
- parserutils_charset_codec_reset(ctx->codec);
- }
- } else {
- if (ctx->indata) {
- memcpy(ctx->buf + ctx->bufused, data, datalen);
- ctx->bufused += datalen;
- }
- if (ctx->inexp) {
- memcpy(ctx->exp + ctx->expused, data, datalen);
- ctx->expused += datalen;
- }
- }
-
- return true;
-}
-
-void run_test(line_ctx *ctx)
-{
- static int testnum;
- size_t destlen = ctx->bufused * 4;
- uint8_t dest[destlen];
- uint8_t *pdest = dest;
- const uint8_t *psrc = ctx->buf;
- size_t srclen = ctx->bufused;
- size_t i;
-
- if (ctx->dir == DECODE) {
- assert(parserutils_charset_codec_decode(ctx->codec,
- &psrc, &srclen,
- &pdest, &destlen) == ctx->exp_ret);
- } else if (ctx->dir == ENCODE) {
- assert(parserutils_charset_codec_encode(ctx->codec,
- &psrc, &srclen,
- &pdest, &destlen) == ctx->exp_ret);
- } else {
- size_t templen = ctx->bufused * 4;
- uint8_t temp[templen];
- uint8_t *ptemp = temp;
-
- assert(parserutils_charset_codec_decode(ctx->codec,
- &psrc, &srclen,
- &ptemp, &templen) == ctx->exp_ret);
- ptemp = temp;
- templen = ctx->bufused * 4 - templen;
- assert(parserutils_charset_codec_encode(ctx->codec,
- (const uint8_t **) &ptemp, &templen,
- &pdest, &destlen) == ctx->exp_ret);
- }
-
- printf("%d: Read '", ++testnum);
- for (i = 0; i < ctx->expused; i++) {
- printf("%c%c ", "0123456789abcdef"[(dest[i] >> 4) & 0xf],
- "0123456789abcdef"[dest[i] & 0xf]);
- }
- printf("' Expected '");
- for (i = 0; i < ctx->expused; i++) {
- printf("%c%c ", "0123456789abcdef"[(ctx->exp[i] >> 4) & 0xf],
- "0123456789abcdef"[ctx->exp[i] & 0xf]);
- }
- printf("'\n");
-
- assert(memcmp(dest, ctx->exp, ctx->expused) == 0);
-}
-
Added: trunk/libparserutils/test/data/cscodec-utf16/INDEX
URL: http://source.netsurf-browser.org/trunk/libparserutils/test/data/cscodec-...
==============================================================================
--- trunk/libparserutils/test/data/cscodec-utf16/INDEX (added)
+++ trunk/libparserutils/test/data/cscodec-utf16/INDEX Sat Jun 28 15:57:47 2008
@@ -1,0 +1,6 @@
+# Index file for UTF-16 charset codec tests
+#
+# Test Description
+
+simple.dat Simple tests, designed to validate testdriver
+
Added: trunk/libparserutils/test/data/cscodec-utf16/simple.dat
URL: http://source.netsurf-browser.org/trunk/libparserutils/test/data/cscodec-...
==============================================================================
Binary file - no diff available.
Propchange: trunk/libparserutils/test/data/cscodec-utf16/simple.dat
------------------------------------------------------------------------------
svn:mime-type = application/octet-stream
14 years, 11 months
r4471 jmb - /trunk/libparserutils/src/input/inputstream.c
by netsurf@semichrome.net
Author: jmb
Date: Sat Jun 28 15:55:43 2008
New Revision: 4471
URL: http://source.netsurf-browser.org?rev=4471&view=rev
Log:
Update filter's input encoding when it's been auto-detected.
Modified:
trunk/libparserutils/src/input/inputstream.c
Modified: trunk/libparserutils/src/input/inputstream.c
URL: http://source.netsurf-browser.org/trunk/libparserutils/src/input/inputstr...
==============================================================================
--- trunk/libparserutils/src/input/inputstream.c (original)
+++ trunk/libparserutils/src/input/inputstream.c Sat Jun 28 15:55:43 2008
@@ -306,6 +306,8 @@
/* If this is the first chunk of data, we must detect the charset and
* strip the BOM, if one exists */
if (!stream->done_first_chunk) {
+ parserutils_filter_optparams params;
+
if (stream->csdetect != NULL) {
error = stream->csdetect(stream->raw->data,
stream->raw->length,
@@ -322,6 +324,16 @@
if (stream->mibenum == 0)
abort();
+
+ /* Ensure filter is using the correct encoding */
+ params.encoding.name =
+ parserutils_charset_mibenum_to_name(stream->mibenum);
+
+ error = parserutils_filter_setopt(stream->input,
+ PARSERUTILS_FILTER_SET_ENCODING,
+ ¶ms);
+ if (error != PARSERUTILS_OK)
+ return error;
error = parserutils_inputstream_strip_bom(stream->mibenum,
stream->raw);
14 years, 11 months
r4470 joty - in /trunk/libparserutils/src/charset/codecs: codec_iconv.c codec_utf16.c codec_utf8.c
by netsurf@semichrome.net
Author: joty
Date: Sat Jun 28 14:06:08 2008
New Revision: 4470
URL: http://source.netsurf-browser.org?rev=4470&view=rev
Log:
Remove unnecessary casting.
Modified:
trunk/libparserutils/src/charset/codecs/codec_iconv.c
trunk/libparserutils/src/charset/codecs/codec_utf16.c
trunk/libparserutils/src/charset/codecs/codec_utf8.c
Modified: trunk/libparserutils/src/charset/codecs/codec_iconv.c
URL: http://source.netsurf-browser.org/trunk/libparserutils/src/charset/codecs...
==============================================================================
--- trunk/libparserutils/src/charset/codecs/codec_iconv.c (original)
+++ trunk/libparserutils/src/charset/codecs/codec_iconv.c Sat Jun 28 14:06:08 2008
@@ -517,7 +517,7 @@
if (*sourcelen > INVAL_BUFSIZE)
abort();
- memmove(c->inval_buf, (const char *) *source, *sourcelen);
+ memmove(c->inval_buf, *source, *sourcelen);
c->inval_buf[*sourcelen] = '\0';
c->inval_len = *sourcelen;
Modified: trunk/libparserutils/src/charset/codecs/codec_utf16.c
URL: http://source.netsurf-browser.org/trunk/libparserutils/src/charset/codecs...
==============================================================================
--- trunk/libparserutils/src/charset/codecs/codec_utf16.c (original)
+++ trunk/libparserutils/src/charset/codecs/codec_utf16.c Sat Jun 28 14:06:08 2008
@@ -447,7 +447,7 @@
if (*sourcelen > INVAL_BUFSIZE)
abort();
- memmove(c->inval_buf, (char *) *source, *sourcelen);
+ memmove(c->inval_buf, *source, *sourcelen);
c->inval_buf[*sourcelen] = '\0';
c->inval_len = *sourcelen;
@@ -480,8 +480,7 @@
if (*sourcelen > INVAL_BUFSIZE)
abort();
- memmove(c->inval_buf, (char *) *source,
- *sourcelen);
+ memmove(c->inval_buf, *source, *sourcelen);
c->inval_buf[*sourcelen] = '\0';
c->inval_len = *sourcelen;
Modified: trunk/libparserutils/src/charset/codecs/codec_utf8.c
URL: http://source.netsurf-browser.org/trunk/libparserutils/src/charset/codecs...
==============================================================================
--- trunk/libparserutils/src/charset/codecs/codec_utf8.c (original)
+++ trunk/libparserutils/src/charset/codecs/codec_utf8.c Sat Jun 28 14:06:08 2008
@@ -438,7 +438,7 @@
if (*sourcelen > INVAL_BUFSIZE)
abort();
- memmove(c->inval_buf, (char *) *source, *sourcelen);
+ memmove(c->inval_buf, *source, *sourcelen);
c->inval_buf[*sourcelen] = '\0';
c->inval_len = *sourcelen;
@@ -477,8 +477,7 @@
if (*sourcelen > INVAL_BUFSIZE)
abort();
- memmove(c->inval_buf, (char *) *source,
- *sourcelen);
+ memmove(c->inval_buf, *source, *sourcelen);
c->inval_buf[*sourcelen] = '\0';
c->inval_len = *sourcelen;
14 years, 11 months
r4469 joty - in /trunk/libparserutils: include/parserutils/charset/ include/parserutils/input/ src/charset/ src/charset/codecs/ src/charset/encodings/
by netsurf@semichrome.net
Author: joty
Date: Sat Jun 28 09:20:50 2008
New Revision: 4469
URL: http://source.netsurf-browser.org?rev=4469&view=rev
Log:
Pedantic spelling changes of encoding names UTF-8, UTF-16 and UCS-4
Modified:
trunk/libparserutils/include/parserutils/charset/codec.h
trunk/libparserutils/include/parserutils/input/inputstream.h
trunk/libparserutils/src/charset/codec.c
trunk/libparserutils/src/charset/codecs/codec_iconv.c
trunk/libparserutils/src/charset/codecs/codec_utf16.c
trunk/libparserutils/src/charset/codecs/codec_utf8.c
trunk/libparserutils/src/charset/encodings/utf16.c
trunk/libparserutils/src/charset/encodings/utf8.c
trunk/libparserutils/src/charset/encodings/utf8impl.h
Modified: trunk/libparserutils/include/parserutils/charset/codec.h
URL: http://source.netsurf-browser.org/trunk/libparserutils/include/parserutil...
==============================================================================
--- trunk/libparserutils/include/parserutils/charset/codec.h (original)
+++ trunk/libparserutils/include/parserutils/charset/codec.h Sat Jun 28 09:20:50 2008
@@ -23,7 +23,7 @@
* A codec's error mode determines its behaviour in the face of:
*
* + characters which are unrepresentable in the destination charset (if
- * encoding data) or which cannot be converted to UCS4 (if decoding data).
+ * encoding data) or which cannot be converted to UCS-4 (if decoding data).
* + invalid byte sequences (both encoding and decoding)
*
* The options provide a choice between the following approaches:
@@ -95,13 +95,13 @@
parserutils_charset_codec_opttype type,
parserutils_charset_codec_optparams *params);
-/* Encode a chunk of UCS4 data into a codec's charset */
+/* Encode a chunk of UCS-4 data into a codec's charset */
parserutils_error parserutils_charset_codec_encode(
parserutils_charset_codec *codec,
const uint8_t **source, size_t *sourcelen,
uint8_t **dest, size_t *destlen);
-/* Decode a chunk of data in a codec's charset into UCS4 */
+/* Decode a chunk of data in a codec's charset into UCS-4 */
parserutils_error parserutils_charset_codec_decode(
parserutils_charset_codec *codec,
const uint8_t **source, size_t *sourcelen,
Modified: trunk/libparserutils/include/parserutils/input/inputstream.h
URL: http://source.netsurf-browser.org/trunk/libparserutils/include/parserutil...
==============================================================================
--- trunk/libparserutils/include/parserutils/input/inputstream.h (original)
+++ trunk/libparserutils/include/parserutils/input/inputstream.h Sat Jun 28 09:20:50 2008
@@ -30,7 +30,7 @@
*/
typedef struct parserutils_inputstream
{
- parserutils_buffer *utf8; /**< Buffer containing utf8 data */
+ parserutils_buffer *utf8; /**< Buffer containing UTF-8 data */
uint32_t cursor; /**< Byte offset of current position */
Modified: trunk/libparserutils/src/charset/codec.c
URL: http://source.netsurf-browser.org/trunk/libparserutils/src/charset/codec....
==============================================================================
--- trunk/libparserutils/src/charset/codec.c (original)
+++ trunk/libparserutils/src/charset/codec.c Sat Jun 28 09:20:50 2008
@@ -117,7 +117,7 @@
}
/**
- * Encode a chunk of UCS4 data into a codec's charset
+ * Encode a chunk of UCS-4 data into a codec's charset
*
* \param codec The codec to use
* \param source Pointer to pointer to source data
@@ -142,7 +142,7 @@
}
/**
- * Decode a chunk of data in a codec's charset into UCS4
+ * Decode a chunk of data in a codec's charset into UCS-4
*
* \param codec The codec to use
* \param source Pointer to pointer to source data
Modified: trunk/libparserutils/src/charset/codecs/codec_iconv.c
URL: http://source.netsurf-browser.org/trunk/libparserutils/src/charset/codecs...
==============================================================================
--- trunk/libparserutils/src/charset/codecs/codec_iconv.c (original)
+++ trunk/libparserutils/src/charset/codecs/codec_iconv.c Sat Jun 28 09:20:50 2008
@@ -161,7 +161,7 @@
}
/**
- * Encode a chunk of UCS4 data into an iconv-based codec's charset
+ * Encode a chunk of UCS-4 data into an iconv-based codec's charset
*
* \param codec The codec to use
* \param source Pointer to pointer to source data
@@ -271,7 +271,7 @@
}
/**
- * Decode a chunk of data in an iconv-based codec's charset into UCS4
+ * Decode a chunk of data in an iconv-based codec's charset into UCS-4
*
* \param codec The codec to use
* \param source Pointer to pointer to source data
@@ -419,10 +419,10 @@
}
/**
- * Output a UCS4 character
+ * Output a UCS-4 character
*
* \param c Codec to use
- * \param ucs4 UCS4 character (big endian)
+ * \param ucs4 UCS-4 character (big endian)
* \param dest Pointer to pointer to output buffer
* \param destlen Pointer to output buffer length
* \return PARSERUTILS_OK on success,
@@ -448,7 +448,7 @@
}
/**
- * Read a character from the codec's native charset to UCS4 (big endian)
+ * Read a character from the codec's native charset to UCS-4 (big endian)
*
* \param c The codec
* \param source Pointer to pointer to source buffer (updated on exit)
@@ -600,10 +600,10 @@
}
/**
- * Write a UCS4 character in a codec's native charset
+ * Write a UCS-4 character in a codec's native charset
*
* \param c The codec
- * \param ucs4 The UCS4 character to write (big endian)
+ * \param ucs4 The UCS-4 character to write (big endian)
* \param dest Pointer to pointer to output buffer (updated on exit)
* \param destlen Pointer to length of output buffer (updated on exit)
* \return PARSERUTILS_OK on success,
Modified: trunk/libparserutils/src/charset/codecs/codec_utf16.c
URL: http://source.netsurf-browser.org/trunk/libparserutils/src/charset/codecs...
==============================================================================
--- trunk/libparserutils/src/charset/codecs/codec_utf16.c (original)
+++ trunk/libparserutils/src/charset/codecs/codec_utf16.c Sat Jun 28 09:20:50 2008
@@ -80,7 +80,7 @@
}
/**
- * Create a utf16 codec
+ * Create a UTF-16 codec
*
* \param charset The charset to read from / write to
* \param alloc Memory (de)allocation function
@@ -117,7 +117,7 @@
}
/**
- * Destroy a utf16 codec
+ * Destroy a UTF-16 codec
*
* \param codec The codec to destroy
*/
@@ -127,7 +127,7 @@
}
/**
- * Encode a chunk of UCS4 data into utf16
+ * Encode a chunk of UCS-4 data into UTF-16
*
* \param codec The codec to use
* \param source Pointer to pointer to source data
@@ -246,7 +246,7 @@
}
/**
- * Decode a chunk of utf16 data into UCS4
+ * Decode a chunk of UTF-16 data into UCS-4
*
* \param codec The codec to use
* \param source Pointer to pointer to source data
@@ -366,7 +366,7 @@
}
/**
- * Clear a utf16 codec's encoding state
+ * Clear a UTF-16 codec's encoding state
*
* \param codec The codec to reset
* \return PARSERUTILS_OK on success, appropriate error otherwise
@@ -389,7 +389,7 @@
/**
- * Read a character from the UTF-16 to UCS4 (big endian)
+ * Read a character from the UTF-16 to UCS-4 (big endian)
*
* \param c The codec
* \param source Pointer to pointer to source buffer (updated on exit)
@@ -510,10 +510,10 @@
}
/**
- * Output a UCS4 character
+ * Output a UCS-4 character
*
* \param c Codec to use
- * \param ucs4 UCS4 character (host endian)
+ * \param ucs4 UCS-4 character (host endian)
* \param dest Pointer to pointer to output buffer
* \param destlen Pointer to output buffer length
* \return PARSERUTILS_OK on success,
Modified: trunk/libparserutils/src/charset/codecs/codec_utf8.c
URL: http://source.netsurf-browser.org/trunk/libparserutils/src/charset/codecs...
==============================================================================
--- trunk/libparserutils/src/charset/codecs/codec_utf8.c (original)
+++ trunk/libparserutils/src/charset/codecs/codec_utf8.c Sat Jun 28 09:20:50 2008
@@ -81,7 +81,7 @@
}
/**
- * Create a utf8 codec
+ * Create a UTF-8 codec
*
* \param charset The charset to read from / write to
* \param alloc Memory (de)allocation function
@@ -118,7 +118,7 @@
}
/**
- * Destroy a utf8 codec
+ * Destroy a UTF-8 codec
*
* \param codec The codec to destroy
*/
@@ -128,7 +128,7 @@
}
/**
- * Encode a chunk of UCS4 data into utf8
+ * Encode a chunk of UCS-4 data into UTF-8
*
* \param codec The codec to use
* \param source Pointer to pointer to source data
@@ -232,7 +232,7 @@
}
/**
- * Decode a chunk of utf8 data into UCS4
+ * Decode a chunk of UTF-8 data into UCS-4
*
* \param codec The codec to use
* \param source Pointer to pointer to source data
@@ -352,7 +352,7 @@
}
/**
- * Clear a utf8 codec's encoding state
+ * Clear a UTF-8 codec's encoding state
*
* \param codec The codec to reset
* \return PARSERUTILS_OK on success, appropriate error otherwise
@@ -375,7 +375,7 @@
/**
- * Read a character from the UTF-8 to UCS4 (big endian)
+ * Read a character from the UTF-8 to UCS-4 (big endian)
*
* \param c The codec
* \param source Pointer to pointer to source buffer (updated on exit)
@@ -511,10 +511,10 @@
}
/**
- * Output a UCS4 character
+ * Output a UCS-4 character
*
* \param c Codec to use
- * \param ucs4 UCS4 character (host endian)
+ * \param ucs4 UCS-4 character (host endian)
* \param dest Pointer to pointer to output buffer
* \param destlen Pointer to output buffer length
* \return PARSERUTILS_OK on success,
Modified: trunk/libparserutils/src/charset/encodings/utf16.c
URL: http://source.netsurf-browser.org/trunk/libparserutils/src/charset/encodi...
==============================================================================
--- trunk/libparserutils/src/charset/encodings/utf16.c (original)
+++ trunk/libparserutils/src/charset/encodings/utf16.c Sat Jun 28 09:20:50 2008
@@ -16,11 +16,11 @@
#include <parserutils/charset/utf16.h>
/**
- * Convert a UTF-16 sequence into a single UCS4 character
+ * Convert a UTF-16 sequence into a single UCS-4 character
*
* \param s The sequence to process
* \param len Length of sequence
- * \param ucs4 Pointer to location to receive UCS4 character (host endian)
+ * \param ucs4 Pointer to location to receive UCS-4 character (host endian)
* \param clen Pointer to location to receive byte length of UTF-16 sequence
* \return PARSERUTILS_OK on success, appropriate error otherwise
*/
@@ -55,7 +55,7 @@
}
/**
- * Convert a single UCS4 character into a UTF-16 sequence
+ * Convert a single UCS-4 character into a UTF-16 sequence
*
* \param ucs4 The character to process (0 <= c <= 0x7FFFFFFF) (host endian)
* \param s Pointer to 4 byte long output buffer
Modified: trunk/libparserutils/src/charset/encodings/utf8.c
URL: http://source.netsurf-browser.org/trunk/libparserutils/src/charset/encodi...
==============================================================================
--- trunk/libparserutils/src/charset/encodings/utf8.c (original)
+++ trunk/libparserutils/src/charset/encodings/utf8.c Sat Jun 28 09:20:50 2008
@@ -37,14 +37,14 @@
};
/**
- * Convert a UTF-8 multibyte sequence into a single UCS4 character
+ * Convert a UTF-8 multibyte sequence into a single UCS-4 character
*
* Encoding of UCS values outside the UTF-16 plane has been removed from
* RFC3629. This function conforms to RFC2279, however.
*
* \param s The sequence to process
* \param len Length of sequence
- * \param ucs4 Pointer to location to receive UCS4 character (host endian)
+ * \param ucs4 Pointer to location to receive UCS-4 character (host endian)
* \param clen Pointer to location to receive byte length of UTF-8 sequence
* \return PARSERUTILS_OK on success, appropriate error otherwise
*/
@@ -59,7 +59,7 @@
}
/**
- * Convert a single UCS4 character into a UTF-8 multibyte sequence
+ * Convert a single UCS-4 character into a UTF-8 multibyte sequence
*
* Encoding of UCS values outside the UTF-16 plane has been removed from
* RFC3629. This function conforms to RFC2279, however.
Modified: trunk/libparserutils/src/charset/encodings/utf8impl.h
URL: http://source.netsurf-browser.org/trunk/libparserutils/src/charset/encodi...
==============================================================================
--- trunk/libparserutils/src/charset/encodings/utf8impl.h (original)
+++ trunk/libparserutils/src/charset/encodings/utf8impl.h Sat Jun 28 09:20:50 2008
@@ -20,14 +20,14 @@
extern const uint8_t numContinuations[256];
/**
- * Convert a UTF-8 multibyte sequence into a single UCS4 character
+ * Convert a UTF-8 multibyte sequence into a single UCS-4 character
*
* Encoding of UCS values outside the UTF-16 plane has been removed from
* RFC3629. This macro conforms to RFC2279, however.
*
* \param s The sequence to process
* \param len Length of sequence
- * \param ucs4 Pointer to location to receive UCS4 character (host endian)
+ * \param ucs4 Pointer to location to receive UCS-4 character (host endian)
* \param clen Pointer to location to receive byte length of UTF-8 sequence
* \param error Location to receive error code
*/
@@ -109,7 +109,7 @@
} while(0)
/**
- * Convert a single UCS4 character into a UTF-8 multibyte sequence
+ * Convert a single UCS-4 character into a UTF-8 multibyte sequence
*
* Encoding of UCS values outside the UTF-16 plane has been removed from
* RFC3629. This macro conforms to RFC2279, however.
14 years, 11 months
r4468 joty - in /trunk/libparserutils: src/utils/dict.c test/rbtree.c
by netsurf@semichrome.net
Author: joty
Date: Sat Jun 28 08:23:19 2008
New Revision: 4468
URL: http://source.netsurf-browser.org?rev=4468&view=rev
Log:
Fix compiler errors when ptrs are 64bit.
Modified:
trunk/libparserutils/src/utils/dict.c
trunk/libparserutils/test/rbtree.c
Modified: trunk/libparserutils/src/utils/dict.c
URL: http://source.netsurf-browser.org/trunk/libparserutils/src/utils/dict.c?r...
==============================================================================
--- trunk/libparserutils/src/utils/dict.c (original)
+++ trunk/libparserutils/src/utils/dict.c Sat Jun 28 08:23:19 2008
@@ -253,7 +253,7 @@
while (depth-- > 0)
putchar(' ');
- printf("'%.*s'\n", e->len, e->data);
+ printf("'%.*s'\n", (int)e->len, e->data);
}
/**
Modified: trunk/libparserutils/test/rbtree.c
URL: http://source.netsurf-browser.org/trunk/libparserutils/test/rbtree.c?rev=...
==============================================================================
--- trunk/libparserutils/test/rbtree.c (original)
+++ trunk/libparserutils/test/rbtree.c Sat Jun 28 08:23:19 2008
@@ -15,7 +15,7 @@
static int mycmp(const void *a, const void *b)
{
- return ((const uint32_t) a) - ((const uint32_t) b);
+ return ((intptr_t) a) - ((intptr_t) b);
}
int main(int argc, char **argv)
@@ -37,7 +37,8 @@
for (int i = G, count = 1; i != 0; i = (i + G) % N, count++) {
void *old;
- assert(parserutils_rbtree_insert(tree, (void *) i, (void *) i,
+ assert(parserutils_rbtree_insert(tree,
+ (char *) NULL + i, (char *) NULL + i,
&old) == PARSERUTILS_OK);
if ((count % 10000) == 0)
@@ -48,8 +49,8 @@
for (int i = 1, count = 1; i < N; i += 2, count++) {
void *key, *value;
- assert(parserutils_rbtree_delete(tree, (void *) i, &key,
- &value) == PARSERUTILS_OK);
+ assert(parserutils_rbtree_delete(tree, (char *) NULL + i,
+ &key, &value) == PARSERUTILS_OK);
if ((count % 10000) == 0)
printf("%d\n", count);
}
@@ -58,9 +59,9 @@
for (int i = 2, count = 1; i < N; i += 2, count++) {
void *value = NULL;
- assert(parserutils_rbtree_find(tree, (void *) i, &value) ==
- PARSERUTILS_OK);
- assert(value != NULL && ((int) value) == i);
+ assert(parserutils_rbtree_find(tree, (char *) NULL + i,
+ &value) == PARSERUTILS_OK);
+ assert(value != NULL && value == (char *) NULL + i);
if ((count % 10000) == 0)
printf("%d\n", count);
}
@@ -69,10 +70,10 @@
for (int i = 1, count = 1; i < N; i += 2, count++) {
void *key, *value = NULL;
- assert(parserutils_rbtree_find(tree, (void *) i, &value) ==
- PARSERUTILS_OK);
+ assert(parserutils_rbtree_find(tree, (char *) NULL + i,
+ &value) == PARSERUTILS_OK);
assert(value == NULL);
- assert(parserutils_rbtree_delete(tree, (void *) i,
+ assert(parserutils_rbtree_delete(tree, (char *) NULL + i,
&key, &value) == PARSERUTILS_OK);
if ((count % 10000) == 0)
printf("%d\n", count);
14 years, 11 months
r4466 dynis - in /branches/dynis/libnsbmp: Makefile examples/decode_ico.c examples/monitor.ico
by netsurf@semichrome.net
Author: dynis
Date: Fri Jun 27 04:54:55 2008
New Revision: 4466
URL: http://source.netsurf-browser.org?rev=4466&view=rev
Log:
Added ico decoding example
Added:
branches/dynis/libnsbmp/examples/decode_ico.c
branches/dynis/libnsbmp/examples/monitor.ico (with props)
Modified:
branches/dynis/libnsbmp/Makefile
Modified: branches/dynis/libnsbmp/Makefile
URL: http://source.netsurf-browser.org/branches/dynis/libnsbmp/Makefile?rev=44...
==============================================================================
--- branches/dynis/libnsbmp/Makefile (original)
+++ branches/dynis/libnsbmp/Makefile Fri Jun 27 04:54:55 2008
@@ -23,7 +23,7 @@
.PHONY: all clean docs install uninstall
-all: libnsbmp.a bin/decode_bmp
+all: libnsbmp.a bin/decode_bmp bin/decode_ico
libnsbmp.a: libnsbmp.o libnsbmp.pc
${AR} ${ARFLAGS} libnsbmp.a libnsbmp.o
@@ -35,6 +35,9 @@
${CC} -c ${CFLAGS} -o $@ $<
bin/decode_bmp: examples/decode_bmp.c libnsbmp.a
+ ${CC} ${CFLAGS} -o $@ $< libnsbmp.a
+
+bin/decode_ico: examples/decode_ico.c libnsbmp.a
${CC} ${CFLAGS} -o $@ $< libnsbmp.a
docs:
Added: branches/dynis/libnsbmp/examples/decode_ico.c
URL: http://source.netsurf-browser.org/branches/dynis/libnsbmp/examples/decode...
==============================================================================
--- branches/dynis/libnsbmp/examples/decode_ico.c (added)
+++ branches/dynis/libnsbmp/examples/decode_ico.c Fri Jun 27 04:54:55 2008
@@ -1,0 +1,212 @@
+/*
+ * Copyright 2008 Sean Fox <dyntryx(a)gmail.com>
+ * Copyright 2008 James Bursa <james(a)netsurf-browser.org>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <assert.h>
+#include <errno.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <libnsbmp.h>
+
+#define BITMAP_BYTES_PER_PIXEL 4
+
+unsigned char *load_file(const char *path, size_t *data_size);
+void warning(const char *context, bmp_result code);
+void *bitmap_create(int width, int height, unsigned int state);
+void bitmap_set_suspendable(void *bitmap, void *private_word,
+ void (*invalidate)(void *bitmap, void *private_word));
+void invalidate(void *bitmap, void *private_word);
+unsigned char *bitmap_get_buffer(void *bitmap);
+size_t bitmap_get_bpp(void *bitmap);
+void bitmap_destroy(void *bitmap);
+
+
+int main(int argc, char *argv[])
+{
+ bmp_bitmap_callback_vt bitmap_callbacks = {
+ bitmap_create,
+ bitmap_destroy,
+ bitmap_set_suspendable,
+ bitmap_get_buffer,
+ bitmap_get_bpp
+ };
+ bmp_result code;
+ ico_collection ico;
+ struct bmp_image *bmp;
+ size_t size;
+
+ if (argc != 2) {
+ fprintf(stderr, "Usage: %s collection.ico\n", argv[0]);
+ return 1;
+ }
+
+ /* create our bmp image */
+ ico_collection_create(&ico, &bitmap_callbacks);
+
+ /* load file into memory */
+ unsigned char *data = load_file(argv[1], &size);
+
+ /* analyse the BMP */
+ code = ico_analyse(&ico, size, data);
+ if (code != BMP_OK) {
+ warning("ico_analyse", code);
+ exit(1);
+ }
+
+ printf("P3\n");
+ printf("# %s\n", argv[1]);
+ printf("# width %u \n", ico.width);
+ printf("# height %u \n", ico.height);
+ printf("%u %u 256\n", ico.width, ico.height);
+
+ /* decode the image */
+ bmp = ico_find(&ico, 255, 255);
+ assert(bmp);
+ bmp_decode(bmp);
+ {
+ unsigned int row, col;
+ unsigned char *image;
+ image = (unsigned char *) bmp->bitmap;
+ for (row = 0; row != bmp->height; row++) {
+ for (col = 0; col != bmp->width; col++) {
+ size_t z = (row * bmp->width + col) * BITMAP_BYTES_PER_PIXEL;
+ printf("%u %u %u ",
+ (unsigned char) image[z],
+ (unsigned char) image[z + 1],
+ (unsigned char) image[z + 2]);
+ }
+ printf("\n");
+ }
+ }
+
+ /* clean up */
+ bmp_finalise(bmp);
+ ico_finalise(&ico);
+ free(data);
+
+ return 0;
+}
+
+
+unsigned char *load_file(const char *path, size_t *data_size)
+{
+ FILE *fd;
+ struct stat sb;
+ unsigned char *buffer;
+ size_t size;
+ size_t n;
+
+ fd = fopen(path, "rb");
+ if (!fd) {
+ perror(path);
+ exit(EXIT_FAILURE);
+ }
+
+ if (stat(path, &sb)) {
+ perror(path);
+ exit(EXIT_FAILURE);
+ }
+ size = sb.st_size;
+
+ buffer = malloc(size);
+ if (!buffer) {
+ fprintf(stderr, "Unable to allocate %lld bytes\n",
+ (long long) size);
+ exit(EXIT_FAILURE);
+ }
+
+ n = fread(buffer, 1, size, fd);
+ if (n != size) {
+ perror(path);
+ exit(EXIT_FAILURE);
+ }
+
+ fclose(fd);
+
+ *data_size = size;
+ return buffer;
+}
+
+
+void warning(const char *context, bmp_result code)
+{
+ fprintf(stderr, "%s failed: ", context);
+ switch (code) {
+ case BMP_INSUFFICIENT_MEMORY:
+ fprintf(stderr, "BMP_INSUFFICIENT_MEMORY");
+ break;
+ case BMP_INSUFFICIENT_DATA:
+ fprintf(stderr, "BMP_INSUFFICIENT_DATA");
+ break;
+ case BMP_DATA_ERROR:
+ fprintf(stderr, "BMP_DATA_ERROR");
+ break;
+ default:
+ fprintf(stderr, "unknown code %i", code);
+ break;
+ }
+ fprintf(stderr, "\n");
+}
+
+
+void *bitmap_create(int width, int height, unsigned int state)
+{
+ (void) state; /* unused */
+ return calloc(width * height, BITMAP_BYTES_PER_PIXEL);
+}
+
+
+void bitmap_set_suspendable(void *bitmap, void *private_word,
+ void (*invalidate)(void *bitmap, void *private_word))
+{
+ (void) bitmap; /* unused */
+ (void) private_word; /* unused */
+ (void) invalidate; /* unused */
+}
+
+
+void invalidate(void *bitmap, void *private_word)
+{
+ (void) bitmap; /* unused */
+ (void) private_word; /* unused */
+}
+
+
+unsigned char *bitmap_get_buffer(void *bitmap)
+{
+ assert(bitmap);
+ return bitmap;
+}
+
+
+size_t bitmap_get_bpp(void *bitmap)
+{
+ (void) bitmap; /* unused */
+ return BITMAP_BYTES_PER_PIXEL;
+}
+
+
+void bitmap_destroy(void *bitmap)
+{
+ assert(bitmap);
+ free(bitmap);
+}
+
Added: branches/dynis/libnsbmp/examples/monitor.ico
URL: http://source.netsurf-browser.org/branches/dynis/libnsbmp/examples/monito...
==============================================================================
Binary file - no diff available.
Propchange: branches/dynis/libnsbmp/examples/monitor.ico
------------------------------------------------------------------------------
svn:mime-type = application/octet-stream
14 years, 11 months
r4465 dynis - in /branches/dynis/libnsbmp: examples/decode_bmp.c libnsbmp.c libnsbmp.h
by netsurf@semichrome.net
Author: dynis
Date: Fri Jun 27 03:36:30 2008
New Revision: 4465
URL: http://source.netsurf-browser.org?rev=4465&view=rev
Log:
Correct ico support and added documentation; corrected handling of rgb16 encoding; bmp_data/buffer_size are now passed to bmp_analyse; several bmp functions are now defined as static
Modified:
branches/dynis/libnsbmp/examples/decode_bmp.c
branches/dynis/libnsbmp/libnsbmp.c
branches/dynis/libnsbmp/libnsbmp.h
Modified: branches/dynis/libnsbmp/examples/decode_bmp.c
URL: http://source.netsurf-browser.org/branches/dynis/libnsbmp/examples/decode...
==============================================================================
--- branches/dynis/libnsbmp/examples/decode_bmp.c (original)
+++ branches/dynis/libnsbmp/examples/decode_bmp.c Fri Jun 27 03:36:30 2008
@@ -63,12 +63,8 @@
/* load file into memory */
unsigned char *data = load_file(argv[1], &size);
- /* set our source data */
- bmp.bmp_data = data;
- bmp.buffer_size = size;
-
/* analyse the BMP */
- code = bmp_analyse(&bmp);
+ code = bmp_analyse(&bmp, size, data);
if (code != BMP_OK) {
warning("bmp_analyse", code);
exit(1);
Modified: branches/dynis/libnsbmp/libnsbmp.c
URL: http://source.netsurf-browser.org/branches/dynis/libnsbmp/libnsbmp.c?rev=...
==============================================================================
--- branches/dynis/libnsbmp/libnsbmp.c (original)
+++ branches/dynis/libnsbmp/libnsbmp.c Fri Jun 27 03:36:30 2008
@@ -37,13 +37,14 @@
return ((unsigned char)data[o] | ((unsigned char)data[o+1] << 8) | ((unsigned char)data[o+2] << 16) | ((unsigned char)data[o+3] << 24));
}
-bmp_result bmp_analyse_header(struct bmp_image *bmp, unsigned char *data);
-bmp_result bmp_decode_rgb24(struct bmp_image *bmp, unsigned char **start, int bytes);
-bmp_result bmp_decode_rgb16(struct bmp_image *bmp, unsigned char **start, int bytes);
-bmp_result bmp_decode_rgb(struct bmp_image *bmp, unsigned char **start, int bytes);
-bmp_result bmp_decode_mask(struct bmp_image *bmp, unsigned char *data, int bytes);
-bmp_result bmp_decode_rle(struct bmp_image *bmp, unsigned char *data, int bytes, int size);
-void bmp_invalidate(void *bitmap, void *private_word);
+static bmp_result next_ico_image(ico_collection *ico, ico_image *image);
+static bmp_result bmp_analyse_header(bmp_image *bmp, unsigned char *data);
+static bmp_result bmp_decode_rgb24(bmp_image *bmp, unsigned char **start, int bytes);
+static bmp_result bmp_decode_rgb16(bmp_image *bmp, unsigned char **start, int bytes);
+static bmp_result bmp_decode_rgb(bmp_image *bmp, unsigned char **start, int bytes);
+static bmp_result bmp_decode_mask(bmp_image *bmp, unsigned char *data, int bytes);
+static bmp_result bmp_decode_rle(bmp_image *bmp, unsigned char *data, int bytes, int size);
+static void bmp_invalidate(void *bitmap, void *private_word);
@@ -55,6 +56,14 @@
}
+/** Initialises necessary ico_collection members.
+*/
+void ico_collection_create(ico_collection *ico, bmp_bitmap_callback_vt *bitmap_callbacks) {
+ memset(ico, 0, sizeof(ico_collection));
+ ico->bitmap_callbacks = *bitmap_callbacks;
+}
+
+
/**
* Analyse a BMP prior to decoding.
*
@@ -67,12 +76,15 @@
* \param bmp the BMP image to analyse
* \return BMP_OK on success
*/
-bmp_result bmp_analyse(struct bmp_image *bmp) {
- unsigned char *data = bmp->bmp_data;
-
+bmp_result bmp_analyse(bmp_image *bmp, size_t size, unsigned char *data) {
/* ensure we aren't already initialised */
if (bmp->bitmap)
return BMP_OK;
+
+ /* Initialize values
+ */
+ bmp->buffer_size = size;
+ bmp->bmp_data = data;
/* standard 14-byte BMP file header is:
*
@@ -104,21 +116,25 @@
* \param ico the ICO image to analyse
* \return BMP_OK on success
*/
-bmp_result ico_analyse(struct ico_collection *ico) {
- unsigned char *data = ico->ico_data;
+bmp_result ico_analyse(ico_collection *ico, size_t size, unsigned char *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:
+ /* Initialize values
+ */
+ ico->buffer_size = size;
+ ico->ico_data = data;
+
+ /* 6-byte ICO file header is:
*
- * +0 INT 0x00010000
- * +4 SHORT number of BMPs to follow
+ * +0 SHORT Reserved (should be 0)
+ * +2 SHORT Type (1 for ICO, 2 for CUR)
+ * +4 SHORT Number of BMPs to follow
*/
if (ico->buffer_size < 6)
return BMP_INSUFFICIENT_DATA;
@@ -129,17 +145,39 @@
return BMP_DATA_ERROR;
data += 6;
- /* decode the BMP files */
+ /* Check if we have enough data for the directory
+ * 6-byte header + 16-byte directory entry for each BMP
+ */
if (ico->buffer_size < 6 + (16 * count))
return BMP_INSUFFICIENT_DATA;
+
+ /* Decode the BMP files.
+ *
+ * 16-byte ICO directory entry is:
+ *
+ * +0 CHAR Width (0 for 256 pixels)
+ * +1 CHAR Height (0 for 256 pixels)
+ * +2 CHAR Colour count (0 if more than 256 colours)
+ * +3 CHAR Reserved (should be 0, but may not be)
+ * +4 SHORT Colour Planes (should be 0 or 1)
+ * +6 SHORT Bits Per Pixel
+ * +8 INT Size of the bitmap data in bytes
+ * +12 INT Offset (bitmap data address in file)
+ */
for (i = 0; i < count; i++) {
- image = calloc(1, sizeof(struct ico_image));
+ ico_image *image;
+ image = calloc(1, sizeof(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];
+ result = next_ico_image(ico, image);
+ if (result != BMP_OK)
+ return result;
+ image->bmp.width = (unsigned int)data[0];
+ if (image->bmp.width == 0)
+ image->bmp.width = 256;
+ image->bmp.height = (unsigned int)data[1];
+ if (image->bmp.height == 0)
+ image->bmp.height = 256;
image->bmp.buffer_size = read_int(data, 8) + 40;
image->bmp.bmp_data = ico->ico_data + read_int(data, 12);
image->bmp.ico = true;
@@ -158,7 +196,23 @@
}
-bmp_result bmp_analyse_header(struct bmp_image *bmp, unsigned char *data) {
+/**
+ * Allocates memory for the next BMP in an ICO collection
+ *
+ * Sets proper structure values
+ *
+ * \param ico the ICO collection to add the image to
+ * \param image a pointer to the ICO image to be initialised
+ */
+static bmp_result next_ico_image(ico_collection *ico, ico_image *image) {
+ bmp_create(&image->bmp, &ico->bitmap_callbacks);
+ image->next = ico->first;
+ ico->first = image;
+ return BMP_OK;
+}
+
+
+static bmp_result bmp_analyse_header(bmp_image *bmp, unsigned char *data) {
unsigned int header_size;
unsigned int i;
int width, height, j;
@@ -181,16 +235,18 @@
* +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 (!bmp->ico) {
+ 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);
@@ -334,9 +390,9 @@
* \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;
+bmp_image *ico_find(ico_collection *ico, int width, int height) {
+ bmp_image *bmp = NULL;
+ ico_image *image;
int x, y, cur, distance = (1 << 24);
for (image = ico->first; image; image = image->next) {
@@ -362,9 +418,9 @@
*
* \param bmp the BMP image to invalidate
*/
-void bmp_invalidate(void *bitmap, void *private_word) {
+static void bmp_invalidate(void *bitmap, void *private_word) {
UNUSED(bitmap);
- struct bmp_image *bmp = (struct bmp_image *)private_word;
+ bmp_image *bmp = (bmp_image *)private_word;
bmp->decoded = false;
}
@@ -380,7 +436,7 @@
* \param bmp the BMP image to decode
* \return BMP_OK on success
*/
-bmp_result bmp_decode(struct bmp_image *bmp) {
+bmp_result bmp_decode(bmp_image *bmp) {
unsigned char *data;
int bytes;
bmp_result result = BMP_OK;
@@ -430,7 +486,7 @@
* \param bytes the number of bytes of data available
* \return BMP_OK on success
*/
-bmp_result bmp_decode_rgb24(struct bmp_image *bmp, unsigned char **start, int bytes) {
+static bmp_result bmp_decode_rgb24(bmp_image *bmp, unsigned char **start, int bytes) {
unsigned char *top, *bottom, *end, *data;
unsigned int *scanline;
unsigned int x, y, swidth, skip;
@@ -491,7 +547,7 @@
* \param bytes the number of bytes of data available
* \return BMP_OK on success
*/
-bmp_result bmp_decode_rgb16(struct bmp_image *bmp, unsigned char **start, int bytes) {
+static bmp_result bmp_decode_rgb16(bmp_image *bmp, unsigned char **start, int bytes) {
unsigned char *top, *bottom, *end, *data;
unsigned int *scanline;
unsigned int x, y, swidth;
@@ -532,10 +588,11 @@
}
} else {
for (x = 0; x < bmp->width; x++) {
- word = data[0] | (data[1] << 8);
+ word = data[0] | (data[1] << 8) | (0xff << 16) | (0xff << 24);
scanline[x] = ((word & (31 << 0)) << 19) |
((word & (31 << 5)) << 6) |
- ((word & (31 << 10)) >> 7);
+ ((word & (31 << 10)) >> 7) |
+ (word & (0xff << 24));
data += 2;
}
}
@@ -553,7 +610,7 @@
* \param bytes the number of bytes of data available
* \return BMP_OK on success
*/
-bmp_result bmp_decode_rgb(struct bmp_image *bmp, unsigned char **start, int bytes) {
+static bmp_result bmp_decode_rgb(bmp_image *bmp, unsigned char **start, int bytes) {
unsigned char *top, *bottom, *end, *data;
unsigned int *scanline;
intptr_t addr;
@@ -609,7 +666,7 @@
* \param bytes the number of bytes of data available
* \return BMP_OK on success
*/
-bmp_result bmp_decode_mask(struct bmp_image *bmp, unsigned char *data, int bytes) {
+static bmp_result bmp_decode_mask(bmp_image *bmp, unsigned char *data, int bytes) {
unsigned char *top, *bottom, *end;
unsigned int *scanline;
intptr_t addr;
@@ -651,7 +708,7 @@
* \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, unsigned char *data, int bytes, int size) {
+static bmp_result bmp_decode_rle(bmp_image *bmp, unsigned char *data, int bytes, int size) {
unsigned char *top, *bottom, *end;
unsigned int *scanline;
unsigned int swidth;
@@ -801,7 +858,7 @@
*
* \param bmp the BMP image to finalise
*/
-void bmp_finalise(struct bmp_image *bmp) {
+void bmp_finalise(bmp_image *bmp) {
if (bmp->bitmap)
bmp->bitmap_callbacks.bitmap_destroy(bmp->bitmap);
bmp->bitmap = NULL;
@@ -816,8 +873,8 @@
*
* \param ico the ICO image to finalise
*/
-void ico_finalise(struct ico_collection *ico) {
- struct ico_image *image;
+void ico_finalise(ico_collection *ico) {
+ ico_image *image;
for (image = ico->first; image; image = image->next)
bmp_finalise(&image->bmp);
Modified: branches/dynis/libnsbmp/libnsbmp.h
URL: http://source.netsurf-browser.org/branches/dynis/libnsbmp/libnsbmp.h?rev=...
==============================================================================
--- branches/dynis/libnsbmp/libnsbmp.h (original)
+++ branches/dynis/libnsbmp/libnsbmp.h Fri Jun 27 03:36:30 2008
@@ -87,28 +87,30 @@
int shift[4]; /** four bitwise shifts */
} bmp_image;
-struct ico_image {
- struct bmp_image bmp;
+typedef struct ico_image {
+ bmp_image bmp;
struct ico_image *next;
-};
+} ico_image;
-struct ico_collection {
- unsigned int width; /** width of largest BMP */
- unsigned int height; /** heigth of largest BMP */
+typedef struct ico_collection {
+ bmp_bitmap_callback_vt bitmap_callbacks; /**< callbacks for bitmap functions */
+ unsigned int width; /** width of largest BMP */
+ unsigned int height; /** heigth of largest BMP */
/** Internal members are listed below
*/
- unsigned char *ico_data; /** pointer to ICO data */
- unsigned int buffer_size; /** total number of bytes of ICO data available */
- struct ico_image *first;
-};
+ unsigned char *ico_data; /** pointer to ICO data */
+ unsigned int buffer_size; /** total number of bytes of ICO data available */
+ ico_image *first;
+} ico_collection;
void bmp_create(bmp_image *gif, bmp_bitmap_callback_vt *bitmap_callbacks);
-bmp_result bmp_analyse(struct bmp_image *bmp);
-bmp_result bmp_decode(struct bmp_image *bmp);
-void bmp_finalise(struct bmp_image *bmp);
+void ico_collection_create(ico_collection *ico, bmp_bitmap_callback_vt *bitmap_callbacks);
+bmp_result bmp_analyse(bmp_image *bmp, size_t size, unsigned char *data);
+bmp_result bmp_decode(bmp_image *bmp);
+void bmp_finalise(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);
+bmp_result ico_analyse(ico_collection *ico, size_t size, unsigned char *data);
+bmp_image *ico_find(ico_collection *ico, int width, int height);
+void ico_finalise(ico_collection *ico);
#endif
14 years, 11 months
r4463 dynis - in /branches/dynis/libnsbmp: bin/decode_bmp examples/decode_bmp.c libnsbmp.c libnsbmp.h
by netsurf@semichrome.net
Author: dynis
Date: Thu Jun 26 23:27:02 2008
New Revision: 4463
URL: http://source.netsurf-browser.org?rev=4463&view=rev
Log:
Arranged bmp_image members, moved bitmap callbacks into bmp_image
Modified:
branches/dynis/libnsbmp/bin/decode_bmp
branches/dynis/libnsbmp/examples/decode_bmp.c
branches/dynis/libnsbmp/libnsbmp.c
branches/dynis/libnsbmp/libnsbmp.h
Modified: branches/dynis/libnsbmp/bin/decode_bmp
URL: http://source.netsurf-browser.org/branches/dynis/libnsbmp/bin/decode_bmp?...
==============================================================================
Binary files - no diff available.
Modified: branches/dynis/libnsbmp/examples/decode_bmp.c
URL: http://source.netsurf-browser.org/branches/dynis/libnsbmp/examples/decode...
==============================================================================
--- branches/dynis/libnsbmp/examples/decode_bmp.c (original)
+++ branches/dynis/libnsbmp/examples/decode_bmp.c Thu Jun 26 23:27:02 2008
@@ -38,20 +38,16 @@
size_t bitmap_get_bpp(void *bitmap);
void bitmap_destroy(void *bitmap);
-/* The Bitmap callbacks function table;
- necessary for interaction with nsgiflib.
-*/
-bmp_bitmap_callback_vt bitmap_callbacks = {
- bitmap_create,
- bitmap_destroy,
- bitmap_set_suspendable,
- bitmap_get_buffer,
- bitmap_get_bpp
-};
-
int main(int argc, char *argv[])
{
+ bmp_bitmap_callback_vt bitmap_callbacks = {
+ bitmap_create,
+ bitmap_destroy,
+ bitmap_set_suspendable,
+ bitmap_get_buffer,
+ bitmap_get_bpp
+ };
bmp_result code;
bmp_image bmp;
size_t size;
@@ -62,7 +58,7 @@
}
/* create our bmp image */
- bmp_create(&bmp);
+ bmp_create(&bmp, &bitmap_callbacks);
/* load file into memory */
unsigned char *data = load_file(argv[1], &size);
@@ -72,7 +68,7 @@
bmp.buffer_size = size;
/* analyse the BMP */
- code = bmp_analyse(&bmp, &bitmap_callbacks);
+ code = bmp_analyse(&bmp);
if (code != BMP_OK) {
warning("bmp_analyse", code);
exit(1);
@@ -85,7 +81,7 @@
printf("%u %u 256\n", bmp.width, bmp.height);
/* decode the image */
- code = bmp_decode(&bmp, &bitmap_callbacks);
+ code = bmp_decode(&bmp);
if (code != BMP_OK) {
warning("bmp_decode", code);
exit(1);
@@ -107,7 +103,7 @@
}
/* clean up */
- bmp_finalise(&bmp, &bitmap_callbacks);
+ bmp_finalise(&bmp);
free(data);
return 0;
Modified: branches/dynis/libnsbmp/libnsbmp.c
URL: http://source.netsurf-browser.org/branches/dynis/libnsbmp/libnsbmp.c?rev=...
==============================================================================
--- branches/dynis/libnsbmp/libnsbmp.c (original)
+++ branches/dynis/libnsbmp/libnsbmp.c Thu Jun 26 23:27:02 2008
@@ -37,20 +37,21 @@
return ((unsigned char)data[o] | ((unsigned char)data[o+1] << 8) | ((unsigned char)data[o+2] << 16) | ((unsigned char)data[o+3] << 24));
}
-bmp_result bmp_analyse_header(struct bmp_image *bmp, unsigned char *data, bmp_bitmap_callback_vt *bitmap_callbacks);
-bmp_result bmp_decode_rgb24(struct bmp_image *bmp, unsigned char **start, int bytes, bmp_bitmap_callback_vt *bitmap_callbacks);
-bmp_result bmp_decode_rgb16(struct bmp_image *bmp, unsigned char **start, int bytes, bmp_bitmap_callback_vt *bitmap_callbacks);
-bmp_result bmp_decode_rgb(struct bmp_image *bmp, unsigned char **start, int bytes, bmp_bitmap_callback_vt *bitmap_callbacks);
-bmp_result bmp_decode_mask(struct bmp_image *bmp, unsigned char *data, int bytes, bmp_bitmap_callback_vt *bitmap_callbacks);
-bmp_result bmp_decode_rle(struct bmp_image *bmp, unsigned char *data, int bytes, int size, bmp_bitmap_callback_vt *bitmap_callbacks);
+bmp_result bmp_analyse_header(struct bmp_image *bmp, unsigned char *data);
+bmp_result bmp_decode_rgb24(struct bmp_image *bmp, unsigned char **start, int bytes);
+bmp_result bmp_decode_rgb16(struct bmp_image *bmp, unsigned char **start, int bytes);
+bmp_result bmp_decode_rgb(struct bmp_image *bmp, unsigned char **start, int bytes);
+bmp_result bmp_decode_mask(struct bmp_image *bmp, unsigned char *data, int bytes);
+bmp_result bmp_decode_rle(struct bmp_image *bmp, unsigned char *data, int bytes, int size);
void bmp_invalidate(void *bitmap, void *private_word);
/** Initialises necessary bmp_image members.
*/
-void bmp_create(bmp_image *bmp) {
+void bmp_create(bmp_image *bmp, bmp_bitmap_callback_vt *bitmap_callbacks) {
memset(bmp, 0, sizeof(bmp_image));
+ bmp->bitmap_callbacks = *bitmap_callbacks;
}
@@ -66,7 +67,7 @@
* \param bmp the BMP image to analyse
* \return BMP_OK on success
*/
-bmp_result bmp_analyse(struct bmp_image *bmp, bmp_bitmap_callback_vt *bitmap_callbacks) {
+bmp_result bmp_analyse(struct bmp_image *bmp) {
unsigned char *data = bmp->bmp_data;
/* ensure we aren't already initialised */
@@ -88,7 +89,7 @@
bmp->bitmap_offset = read_int(data, 10);
/* decode the BMP header */
- return bmp_analyse_header(bmp, data + 14, bitmap_callbacks);
+ return bmp_analyse_header(bmp, data + 14);
}
@@ -103,7 +104,7 @@
* \param ico the ICO image to analyse
* \return BMP_OK on success
*/
-bmp_result ico_analyse(struct ico_collection *ico, bmp_bitmap_callback_vt *bitmap_callbacks) {
+bmp_result ico_analyse(struct ico_collection *ico) {
unsigned char *data = ico->ico_data;
unsigned int count, i;
bmp_result result;
@@ -143,8 +144,7 @@
image->bmp.bmp_data = ico->ico_data + read_int(data, 12);
image->bmp.ico = true;
data += 16;
- result = bmp_analyse_header(&image->bmp,
- image->bmp.bmp_data, bitmap_callbacks);
+ result = bmp_analyse_header(&image->bmp, image->bmp.bmp_data);
if (result != BMP_OK)
return result;
area = image->bmp.width * image->bmp.height;
@@ -158,7 +158,7 @@
}
-bmp_result bmp_analyse_header(struct bmp_image *bmp, unsigned char *data, bmp_bitmap_callback_vt *bitmap_callbacks) {
+bmp_result bmp_analyse_header(struct bmp_image *bmp, unsigned char *data) {
unsigned int header_size;
unsigned int i;
int width, height, j;
@@ -311,7 +311,7 @@
flags = BMP_NEW | BMP_CLEAR_MEMORY;
if ((!bmp->ico) && (bmp->mask[3] == 0))
flags |= BMP_OPAQUE;
- bmp->bitmap = bitmap_callbacks->bitmap_create(bmp->width, bmp->height, flags);
+ bmp->bitmap = bmp->bitmap_callbacks.bitmap_create(bmp->width, bmp->height, flags);
if (!bmp->bitmap) {
if (bmp->colour_table)
free(bmp->colour_table);
@@ -319,7 +319,7 @@
return BMP_INSUFFICIENT_MEMORY;
}
bmp->bitmap_offset = (intptr_t)data - (intptr_t)bmp->bmp_data;
- bitmap_callbacks->bitmap_set_suspendable(bmp->bitmap, bmp, bmp_invalidate);
+ bmp->bitmap_callbacks.bitmap_set_suspendable(bmp->bitmap, bmp, bmp_invalidate);
return BMP_OK;
}
@@ -380,7 +380,7 @@
* \param bmp the BMP image to decode
* \return BMP_OK on success
*/
-bmp_result bmp_decode(struct bmp_image *bmp, bmp_bitmap_callback_vt *bitmap_callbacks) {
+bmp_result bmp_decode(struct bmp_image *bmp) {
unsigned char *data;
int bytes;
bmp_result result = BMP_OK;
@@ -393,23 +393,23 @@
switch (bmp->encoding) {
case BMP_ENCODING_RGB:
if (bmp->bpp >= 24)
- result = bmp_decode_rgb24(bmp, &data, bytes, bitmap_callbacks);
+ result = bmp_decode_rgb24(bmp, &data, bytes);
else if (bmp->bpp > 8)
- result = bmp_decode_rgb16(bmp, &data, bytes, bitmap_callbacks);
+ result = bmp_decode_rgb16(bmp, &data, bytes);
else
- result = bmp_decode_rgb(bmp, &data, bytes, bitmap_callbacks);
+ result = bmp_decode_rgb(bmp, &data, bytes);
break;
case BMP_ENCODING_RLE8:
- result = bmp_decode_rle(bmp, data, bytes, 8, bitmap_callbacks);
+ result = bmp_decode_rle(bmp, data, bytes, 8);
break;
case BMP_ENCODING_RLE4:
- result = bmp_decode_rle(bmp, data, bytes, 4, bitmap_callbacks);
+ result = bmp_decode_rle(bmp, data, bytes, 4);
break;
case BMP_ENCODING_BITFIELDS:
if (bmp->bpp == 32)
- result = bmp_decode_rgb24(bmp, &data, bytes, bitmap_callbacks);
+ result = bmp_decode_rgb24(bmp, &data, bytes);
else if (bmp->bpp == 16)
- result = bmp_decode_rgb16(bmp, &data, bytes, bitmap_callbacks);
+ result = bmp_decode_rgb16(bmp, &data, bytes);
else
return BMP_DATA_ERROR;
}
@@ -418,7 +418,7 @@
return result;
bytes = (intptr_t)bmp->bmp_data + bmp->buffer_size - (intptr_t)data;
- return bmp_decode_mask(bmp, data, bytes, bitmap_callbacks);
+ return bmp_decode_mask(bmp, data, bytes);
}
@@ -430,7 +430,7 @@
* \param bytes the number of bytes of data available
* \return BMP_OK on success
*/
-bmp_result bmp_decode_rgb24(struct bmp_image *bmp, unsigned char **start, int bytes, bmp_bitmap_callback_vt *bitmap_callbacks) {
+bmp_result bmp_decode_rgb24(struct bmp_image *bmp, unsigned char **start, int bytes) {
unsigned char *top, *bottom, *end, *data;
unsigned int *scanline;
unsigned int x, y, swidth, skip;
@@ -438,8 +438,8 @@
unsigned int i, word;
data = *start;
- swidth = bitmap_callbacks->bitmap_get_bpp(bmp->bitmap) * bmp->width;
- top = bitmap_callbacks->bitmap_get_buffer(bmp->bitmap);
+ swidth = bmp->bitmap_callbacks.bitmap_get_bpp(bmp->bitmap) * bmp->width;
+ top = bmp->bitmap_callbacks.bitmap_get_buffer(bmp->bitmap);
if (!top)
return BMP_INSUFFICIENT_MEMORY;
bottom = top + swidth * (bmp->height - 1);
@@ -491,7 +491,7 @@
* \param bytes the number of bytes of data available
* \return BMP_OK on success
*/
-bmp_result bmp_decode_rgb16(struct bmp_image *bmp, unsigned char **start, int bytes, bmp_bitmap_callback_vt *bitmap_callbacks) {
+bmp_result bmp_decode_rgb16(struct bmp_image *bmp, unsigned char **start, int bytes) {
unsigned char *top, *bottom, *end, *data;
unsigned int *scanline;
unsigned int x, y, swidth;
@@ -499,8 +499,8 @@
unsigned int word, i;
data = *start;
- swidth = bitmap_callbacks->bitmap_get_bpp(bmp->bitmap) * bmp->width;
- top = bitmap_callbacks->bitmap_get_buffer(bmp->bitmap);
+ swidth = bmp->bitmap_callbacks.bitmap_get_bpp(bmp->bitmap) * bmp->width;
+ top = bmp->bitmap_callbacks.bitmap_get_buffer(bmp->bitmap);
if (!top)
return BMP_INSUFFICIENT_MEMORY;
bottom = top + swidth * (bmp->height - 1);
@@ -553,7 +553,7 @@
* \param bytes the number of bytes of data available
* \return BMP_OK on success
*/
-bmp_result bmp_decode_rgb(struct bmp_image *bmp, unsigned char **start, int bytes, bmp_bitmap_callback_vt *bitmap_callbacks) {
+bmp_result bmp_decode_rgb(struct bmp_image *bmp, unsigned char **start, int bytes) {
unsigned char *top, *bottom, *end, *data;
unsigned int *scanline;
intptr_t addr;
@@ -568,8 +568,8 @@
bit_shifts[i] = 8 - ((i + 1) * bmp->bpp);
data = *start;
- swidth = bitmap_callbacks->bitmap_get_bpp(bmp->bitmap) * bmp->width;
- top = bitmap_callbacks->bitmap_get_buffer(bmp->bitmap);
+ swidth = bmp->bitmap_callbacks.bitmap_get_bpp(bmp->bitmap) * bmp->width;
+ top = bmp->bitmap_callbacks.bitmap_get_buffer(bmp->bitmap);
if (!top)
return BMP_INSUFFICIENT_MEMORY;
bottom = top + swidth * (bmp->height - 1);
@@ -609,15 +609,15 @@
* \param bytes the number of bytes of data available
* \return BMP_OK on success
*/
-bmp_result bmp_decode_mask(struct bmp_image *bmp, unsigned char *data, int bytes, bmp_bitmap_callback_vt *bitmap_callbacks) {
+bmp_result bmp_decode_mask(struct bmp_image *bmp, unsigned char *data, int bytes) {
unsigned char *top, *bottom, *end;
unsigned int *scanline;
intptr_t addr;
unsigned int x, y, swidth;
int cur_byte = 0;
- swidth = bitmap_callbacks->bitmap_get_bpp(bmp->bitmap) * bmp->width;
- top = bitmap_callbacks->bitmap_get_buffer(bmp->bitmap);
+ swidth = bmp->bitmap_callbacks.bitmap_get_bpp(bmp->bitmap) * bmp->width;
+ top = bmp->bitmap_callbacks.bitmap_get_buffer(bmp->bitmap);
if (!top)
return BMP_INSUFFICIENT_MEMORY;
bottom = top + swidth * (bmp->height - 1);
@@ -651,7 +651,7 @@
* \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, unsigned char *data, int bytes, int size, bmp_bitmap_callback_vt *bitmap_callbacks) {
+bmp_result bmp_decode_rle(struct bmp_image *bmp, unsigned char *data, int bytes, int size) {
unsigned char *top, *bottom, *end;
unsigned int *scanline;
unsigned int swidth;
@@ -662,8 +662,8 @@
if (bmp->ico)
return BMP_DATA_ERROR;
- swidth = bitmap_callbacks->bitmap_get_bpp(bmp->bitmap) * bmp->width;
- top = bitmap_callbacks->bitmap_get_buffer(bmp->bitmap);
+ swidth = bmp->bitmap_callbacks.bitmap_get_bpp(bmp->bitmap) * bmp->width;
+ top = bmp->bitmap_callbacks.bitmap_get_buffer(bmp->bitmap);
if (!top)
return BMP_INSUFFICIENT_MEMORY;
bottom = top + swidth * (bmp->height - 1);
@@ -801,9 +801,9 @@
*
* \param bmp the BMP image to finalise
*/
-void bmp_finalise(struct bmp_image *bmp, bmp_bitmap_callback_vt *bitmap_callbacks) {
+void bmp_finalise(struct bmp_image *bmp) {
if (bmp->bitmap)
- bitmap_callbacks->bitmap_destroy(bmp->bitmap);
+ bmp->bitmap_callbacks.bitmap_destroy(bmp->bitmap);
bmp->bitmap = NULL;
if (bmp->colour_table)
free(bmp->colour_table);
@@ -816,11 +816,11 @@
*
* \param ico the ICO image to finalise
*/
-void ico_finalise(struct ico_collection *ico, bmp_bitmap_callback_vt *bitmap_callbacks) {
+void ico_finalise(struct ico_collection *ico) {
struct ico_image *image;
for (image = ico->first; image; image = image->next)
- bmp_finalise(&image->bmp, bitmap_callbacks);
+ bmp_finalise(&image->bmp);
while (ico->first) {
image = ico->first;
ico->first = image->next;
Modified: branches/dynis/libnsbmp/libnsbmp.h
URL: http://source.netsurf-browser.org/branches/dynis/libnsbmp/libnsbmp.h?rev=...
==============================================================================
--- branches/dynis/libnsbmp/libnsbmp.h (original)
+++ branches/dynis/libnsbmp/libnsbmp.h Thu Jun 26 23:27:02 2008
@@ -63,25 +63,28 @@
bmp_bitmap_cb_destroy bitmap_destroy; /**< Free a bitmap. */
bmp_bitmap_cb_set_suspendable bitmap_set_suspendable; /**< The bitmap image can be suspended. */
bmp_bitmap_cb_get_buffer bitmap_get_buffer; /**< Return a pointer to the pixel data in a bitmap. */
- bmp_bitmap_cb_get_bpp bitmap_get_bpp; /**< Find the width of a pixel row in bytes. */
+ bmp_bitmap_cb_get_bpp bitmap_get_bpp; /**< Find the width of a pixel row in bytes. */
} bmp_bitmap_callback_vt;
typedef 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 */
- void *bitmap; /** decoded image */
+ bmp_bitmap_callback_vt bitmap_callbacks; /**< callbacks for bitmap functions */
+ unsigned int width; /** width of BMP (valid after _analyse) */
+ unsigned int height; /** heigth of BMP (valid after _analyse) */
+ bool decoded; /** whether the image has been decoded */
+ void *bitmap; /** decoded image */
+ /** Internal members are listed below
+ */
+ unsigned char *bmp_data; /** pointer to BMP data */
+ unsigned int buffer_size; /** total number of bytes of BMP data available */
+ 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 ico; /** image is part of an ICO, mask follows */
+ unsigned int mask[4]; /** four bitwise mask */
+ int shift[4]; /** four bitwise shifts */
} bmp_image;
struct ico_image {
@@ -90,20 +93,22 @@
};
struct ico_collection {
+ unsigned int width; /** width of largest BMP */
+ unsigned int height; /** heigth of largest BMP */
+ /** Internal members are listed below
+ */
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;
};
-void bmp_create(bmp_image *gif);
-bmp_result bmp_analyse(struct bmp_image *bmp, bmp_bitmap_callback_vt *bitmap_callbacks);
-bmp_result bmp_decode(struct bmp_image *bmp, bmp_bitmap_callback_vt *bitmap_callbacks);
-void bmp_finalise(struct bmp_image *bmp, bmp_bitmap_callback_vt *bitmap_callbacks);
+void bmp_create(bmp_image *gif, bmp_bitmap_callback_vt *bitmap_callbacks);
+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, bmp_bitmap_callback_vt *bitmap_callbacks);
+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, bmp_bitmap_callback_vt *bitmap_callbacks);
+void ico_finalise(struct ico_collection *ico);
#endif
14 years, 11 months