view tests/encbr1test.c @ 2039:210a0041081b

Fix MinGW build of encbr1test.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 30 Nov 2018 06:48:36 +0200
parents f41bc7203a16
children c67d863384a5
line wrap: on
line source

#include "dmtool.h"
#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;
}