summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--Makefile8
-rw-r--r--src/librosprite.c14
-rw-r--r--test/Makefile3
-rw-r--r--test/decode_rosprite.c135
-rwxr-xr-xtest/runtest.sh58
-rw-r--r--test/sprite/32bpp-alpha-test.spr (renamed from test-data/32bpp-alpha-test)bin30356 -> 30356 bytes
-rw-r--r--test/sprite/primary-color-16bpp.spr (renamed from test-data/primary-color-16bpp)bin180144 -> 180144 bytes
-rw-r--r--test/sprite/small.spr (renamed from test-data/small)bin224 -> 224 bytes
9 files changed, 213 insertions, 6 deletions
diff --git a/.gitignore b/.gitignore
index dd07bff..015f2f7 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
build-*
+*~
diff --git a/Makefile b/Makefile
index 32ef847..a2bb66a 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
# Component settings
COMPONENT := rosprite
-COMPONENT_VERSION := 0.1.1
+COMPONENT_VERSION := 0.1.3
# Default to a static library
COMPONENT_TYPE ?= lib-static
@@ -13,6 +13,9 @@ PREFIX ?= /opt/netsurf
NSSHARED ?= $(PREFIX)/share/netsurf-buildsystem
include $(NSSHARED)/makefiles/Makefile.tools
+# Reevaluate when used, as BUILDDIR won't be defined yet
+TESTRUNNER = test/runtest.sh $(BUILDDIR) $(EXEEXT)
+
# Toolchain flags
WARNFLAGS := -Wall -Wextra -Wundef -Wpointer-arith -Wcast-align \
-Wwrite-strings -Wstrict-prototypes \
@@ -36,6 +39,9 @@ else
CFLAGS := $(CFLAGS) -Dinline="__inline__"
endif
+TESTCFLAGS := -g -O2
+TESTLDFLAGS := -lm -l$(COMPONENT) $(TESTLDFLAGS)
+
include $(NSBUILD)/Makefile.top
# Extra installation rules
diff --git a/src/librosprite.c b/src/librosprite.c
index b382ce0..7cfcaaa 100644
--- a/src/librosprite.c
+++ b/src/librosprite.c
@@ -21,7 +21,11 @@
/**
* Reads four bytes, 00, 11, 22 and 33, of a byte array b to give 0x33221100.
*/
-#define BTUINT(b) (b[0] | (b[1] << 8) | (b[2] << 16) | (b[3] << 24))
+#define BTUINT(b) \
+ ((unsigned)b[0] | \
+ ((unsigned)b[1] << 8) | \
+ ((unsigned)b[2] << 16) | \
+ ((unsigned)b[3] << 24))
/**
* Reverse the byte order of a word such that 0xAABBCCDD becomes 0xDDCCBBAA.
@@ -559,7 +563,7 @@ static rosprite_error rosprite_load_high_color(uint8_t* image_in, uint8_t* mask,
uint32_t currentByteIndex = 0;
uint32_t j, x, y, x_pixels, pixel;
bool has_alpha_pixel_data = false;
- uint8_t b;
+ uint32_t b;
bool old_has_alpha;
if (sprite->has_mask) {
@@ -809,9 +813,9 @@ static uint32_t rosprite_upscale_color(uint32_t pixel, struct rosprite_mode* mod
assert(green < 32);
assert(blue < 32);
- pixel = (sprite_16bpp_translate[red] << 24)
- | (sprite_16bpp_translate[green] << 16)
- | (sprite_16bpp_translate[blue] << 8);
+ pixel = ((uint32_t)sprite_16bpp_translate[red] << 24)|
+ ((uint32_t)sprite_16bpp_translate[green] << 16)|
+ ((uint32_t)sprite_16bpp_translate[blue] << 8);
}
break;
case 8:
diff --git a/test/Makefile b/test/Makefile
new file mode 100644
index 0000000..1402632
--- /dev/null
+++ b/test/Makefile
@@ -0,0 +1,3 @@
+DIR_TEST_ITEMS := decode_rosprite:decode_rosprite.c
+
+include $(NSBUILD)/Makefile.subdir
diff --git a/test/decode_rosprite.c b/test/decode_rosprite.c
new file mode 100644
index 0000000..c038b1f
--- /dev/null
+++ b/test/decode_rosprite.c
@@ -0,0 +1,135 @@
+/*
+ * Copyright 2018 Vincent Sanders <vince@netsurf-browser.org>
+ *
+ * This file is part of NetSurf's librosprite, http://www.netsurf-browser.org/
+ * Licenced under the MIT License,
+ * http://www.opensource.org/licenses/mit-license.php
+ */
+
+#include <assert.h>
+#include <errno.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/stat.h>
+#include "../include/librosprite.h"
+
+
+static void write_ppm(FILE* fh, const char *srcname, struct rosprite* sprite)
+{
+ fprintf(fh, "P3\n");
+ fprintf(fh, "# %s\n", srcname);
+
+ fprintf(fh, "# name %s\n", sprite->name);
+ fprintf(fh, "# color_model %s\n",
+ sprite->mode.color_model == ROSPRITE_RGB ? "RGB" : "CMYK");
+ fprintf(fh, "# colorbpp %u\n", sprite->mode.colorbpp);
+ fprintf(fh, "# xdpi %u\n", sprite->mode.xdpi);
+ fprintf(fh, "# ydpi %u\n", sprite->mode.ydpi);
+ fprintf(fh, "# width %u px\n", sprite->width);
+ fprintf(fh, "# height %u px\n", sprite->height);
+
+ fprintf(fh, "# hasPalette %s\n", sprite->has_palette ? "YES" : "NO");
+ if (sprite->has_palette) {
+ fprintf(fh, "# paletteSize %u\n", sprite->palettesize);
+ }
+ fprintf(fh, "# hasMask %s\n", sprite->has_mask ? "YES" : "NO");
+ if (sprite->has_mask) {
+ fprintf(fh, "# mask_width %u\n", sprite->mode.mask_width);
+ }
+ if (sprite->has_mask) {
+ fprintf(fh, "# maskbpp %u\n", sprite->mode.maskbpp);
+ }
+
+ fprintf(fh, "%u %u 256\n", sprite->width, sprite->height);
+
+
+ for (uint32_t y = 0; y < sprite->height; y++) {
+ for (uint32_t x = 0; x < sprite->width; x++) {
+ uint32_t color; /* color is 0xrrggbbaa */
+
+ color = sprite->image[y*sprite->width + x];
+ fprintf(fh, "%u %u %u ",
+ (color & 0xff000000) >> 24,
+ (color & 0x00ff0000) >> 16,
+ (color & 0x0000ff00) >> 8);
+
+ }
+ fprintf(fh, "\n");
+ }
+
+}
+
+
+int main(int argc, char *argv[])
+{
+
+ int res = 0;
+ FILE *inf;
+ FILE *outf = stdout;
+ struct rosprite_file_context *ctx;
+ struct rosprite_area *sprite_area;
+ unsigned int sprite_number = 0;/* number of sprite in sprite area to convert */
+
+ if (argc < 2) {
+ fprintf(stderr, "Usage: %s image.spr [out]\n", argv[0]);
+ return 1;
+ }
+
+ inf = fopen(argv[1], "rb");
+ if (inf == NULL) {
+ fprintf(stderr, "Unable to open %s for reading\n", argv[1]);
+ return 3;
+ }
+
+ if (argc > 2) {
+ outf = fopen(argv[2], "w+");
+ if (outf == NULL) {
+ fprintf(stderr,
+ "Unable to open %s for writing\n", argv[2]);
+ fclose(inf);
+ return 2;
+ }
+ }
+
+ if (rosprite_create_file_context(inf, &ctx) != ROSPRITE_OK) {
+ fprintf(stderr, "Unable to create file context\n");
+ res = 4;
+ goto cleanup;
+ }
+
+ /* load sprites into sprite area */
+ if (rosprite_load(rosprite_file_reader,
+ ctx,
+ &sprite_area) != ROSPRITE_OK) {
+ fprintf(stderr, "Error loading spritefile\n");
+ res = 5;
+ goto cleanup;
+ };
+
+ if (sprite_number >= sprite_area->sprite_count) {
+ fprintf(stderr,
+ "Sprite %d of %d is not present in sprite pool\n",
+ sprite_number, sprite_area->sprite_count);
+ res = 6;
+ goto cleanup;
+ }
+
+ /* write out sprite */
+ write_ppm(outf, argv[1], sprite_area->sprites[sprite_number]);
+
+ rosprite_destroy_file_context(ctx);
+ rosprite_destroy_sprite_area(sprite_area);
+
+cleanup:
+
+ fclose(inf);
+
+ if (argc > 2) {
+ fclose(outf);
+ }
+
+ return res;
+}
diff --git a/test/runtest.sh b/test/runtest.sh
new file mode 100755
index 0000000..c5535d9
--- /dev/null
+++ b/test/runtest.sh
@@ -0,0 +1,58 @@
+#!/bin/sh
+
+# run test images through librosprite and count results
+
+TEST_PATH=$1
+TEST_OUT=${TEST_PATH}/ppm
+TEST_LOG=${TEST_PATH}/test.log
+
+mkdir -p ${TEST_OUT}
+
+echo "RO Sprite tests" > ${TEST_LOG}
+
+# netsurf test sprites
+SPRTESTS="test/sprite/*.spr"
+
+
+rospritedecode()
+{
+ OUTF=$(basename ${1} .spr)
+ CMPF=$(dirname ${1})/${OUTF}.ppm
+ echo "Icon:${1}" >> ${TEST_LOG}
+ ${TEST_PATH}/test_decode_rosprite ${1} ${TEST_OUT}/${OUTF}.ppm 2>> ${TEST_LOG}
+ if [ -f "${CMPF}" ]; then
+ cmp ${CMPF} ${TEST_OUT}/${OUTF}.ppm >> ${TEST_LOG} 2>> ${TEST_LOG}
+ if [ "$?" -ne 0 ]; then
+ return 128
+ fi
+ fi
+}
+
+# sprite tests
+
+SPRTESTTOTC=0
+SPRTESTPASSC=0
+SPRTESTERRC=0
+
+# netsurf test sprites
+for SPR in $(ls ${SPRTESTS});do
+ SPRTESTTOTC=$((SPRTESTTOTC+1))
+ rospritedecode ${SPR}
+ ECODE=$?
+ if [ \( "${ECODE}" -gt 127 \) -o \( "${ECODE}" -eq 1 \) ];then
+ SPRTESTERRC=$((SPRTESTERRC+1))
+ else
+ SPRTESTPASSC=$((SPRTESTPASSC+1))
+ fi
+done
+
+echo "Test sprite decode"
+echo "Tests:${SPRTESTTOTC} Pass:${SPRTESTPASSC} Error:${SPRTESTERRC}"
+
+
+# exit code
+if [ "${SPRTESTERRC}" -gt 0 ]; then
+ exit 1
+fi
+
+exit 0
diff --git a/test-data/32bpp-alpha-test b/test/sprite/32bpp-alpha-test.spr
index 92ebd63..92ebd63 100644
--- a/test-data/32bpp-alpha-test
+++ b/test/sprite/32bpp-alpha-test.spr
Binary files differ
diff --git a/test-data/primary-color-16bpp b/test/sprite/primary-color-16bpp.spr
index fba7a08..fba7a08 100644
--- a/test-data/primary-color-16bpp
+++ b/test/sprite/primary-color-16bpp.spr
Binary files differ
diff --git a/test-data/small b/test/sprite/small.spr
index 5552782..5552782 100644
--- a/test-data/small
+++ b/test/sprite/small.spr
Binary files differ