changeset 2024:f41bc7203a16

Add a test program for BR1 compression.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 25 Oct 2018 20:19:53 +0300
parents 362fb8295f0c
children 307a52e0dc04
files Makefile.gen tests/encbr1test.c
diffstat 2 files changed, 168 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile.gen	Thu Oct 25 19:21:33 2018 +0300
+++ b/Makefile.gen	Thu Oct 25 20:19:53 2018 +0300
@@ -259,7 +259,7 @@
 
 ### What tests to build?
 ifeq ($(DM_BUILD_TESTS),yes)
-TESTS_BINARIES += vecmattest fptest dzlibtest
+TESTS_BINARIES += vecmattest fptest dzlibtest encbr1test
 endif
 
 FONTCONV_BIN=fontconv
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/encbr1test.c	Thu Oct 25 20:19:53 2018 +0300
@@ -0,0 +1,167 @@
+#include "dmlib.h"
+#include "dmres.h"
+
+
+#define BR_DEBUG
+//#define BR_WRITE
+
+enum
+{
+    DMODE_LIT,
+    DMODE_RLE,
+};
+
+
+BOOL dmIFFEncodeByteRun1Flush(
+    DMResource *fp, const int mode, const BOOL flush,
+    size_t *l_offs, const size_t offs, const Uint8 *buf,
+    const Uint8 data, unsigned int *r_count)
+{
+#ifdef BR_DEBUG
+    if (flush)
+        printf("FLUSH:\n");
+#endif
+
+    if (mode == DMODE_LIT)
+    {
+        size_t l_count = offs - *l_offs;
+        if (l_count > *r_count || flush)
+        {
+            size_t count = l_count - *r_count;
+            Uint8 tmp = count - 1;
+
+#ifdef BR_DEBUG
+            printf("L: ");
+            for (size_t n = 0; n < count; n++)
+                printf("%02x ", buf[*l_offs + n]);
+            printf(" [%" DM_PRIu_SIZE_T " -> %d]\n", count, tmp);
+#endif
+
+#ifdef BR_WRITE
+            if (!dmf_write_byte(fp, tmp) ||
+                !dmf_write_str(fp, buf + *l_offs, count))
+                return FALSE;
+#endif
+        }
+        (*r_count)++;
+    }
+    else
+    {
+        if (*r_count > 0)
+        {
+            unsigned int count = *r_count;
+            Uint8 tmp = ((Uint8) count - 2) ^ 0xff;
+
+#ifdef BR_DEBUG
+            printf("R: %02x x %x [%02x]\n", data, count, tmp);
+#endif
+#ifdef BR_WRITE
+            if (!dmf_write_byte(fp, tmp) ||
+                !dmf_write_byte(fp, data))
+                return FALSE;
+#endif
+            *r_count = 0;
+        }
+        *l_offs = offs;
+    }
+
+    return TRUE;
+}
+
+
+BOOL dmIFFEncodeByteRun1Row(DMResource *fp, const Uint8 *buf, const size_t bufLen)
+{
+    unsigned int r_count = 0;
+    int prev = -1, mode = DMODE_LIT;
+    size_t offs, l_offs = 0;
+
+#ifdef BR_DEBUG
+    printf("\n\nByteRUN1ROW: %" DM_PRIu_SIZE_T " = $%" DM_PRIx_SIZE_T "\n", bufLen, bufLen);
+#endif
+    for (offs = 0; offs < bufLen; offs++)
+    {
+        Uint8 data = buf[offs];
+        int next_mode;
+        BOOL flush;
+
+        if (data == prev)
+        {
+            r_count++;
+            next_mode = DMODE_RLE;
+        }
+        else
+        {
+            next_mode = DMODE_LIT;
+        }
+
+//#ifdef BR_DEBUG
+#if 0
+        printf("%08" DM_PRIx_SIZE_T ": %02x | r_count=%x, l_count=%" DM_PRIx_SIZE_T ", mode=%s [%08" DM_PRIx_SIZE_T "]\n",
+            offs, data, r_count, offs - l_offs + 1, mode == DMODE_RLE ? "RLE" : "LIT", l_offs);
+#endif
+
+        flush = offs - l_offs >= 128 || r_count >= 128;
+        if ((next_mode != mode || flush) &&
+            !dmIFFEncodeByteRun1Flush(fp, mode, flush, &l_offs, offs, buf, prev, &r_count))
+            return FALSE;
+
+        mode = next_mode;
+        prev = data;
+    }
+
+#ifdef BR_DEBUG
+    printf("END\n");
+#endif
+
+    if (!dmIFFEncodeByteRun1Flush(fp, mode, TRUE, &l_offs, offs, buf, prev, &r_count))
+        return FALSE;
+
+    return TRUE;
+}
+
+
+
+int main(int argc, char *argv[])
+{
+    (void) argc;
+    (void) argv;
+
+    Uint8 test[] =
+    {
+        1,
+        0, 0, 0,
+        2,
+        0, 0, 0, 0,
+        3, 3, 3,
+        2, 2,
+        0,
+        1,
+        0, 0, 0, 0, 0, 0, 0, 0,
+
+        1, 2, 3, 4, 5, 6, 7, 8,
+
+        0, 0, 0, 0, 0,
+
+        32,
+
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+
+//        15,
+
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+
+        1
+        //0, 0, 0, 0,
+        //1
+
+    };
+
+    dmIFFEncodeByteRun1Row(NULL, test, sizeof(test));
+    return 0;
+}