Mercurial > hg > dmlib
diff tools/libgfx.c @ 1309:5ad7d780a09b
Move bitstream reading functions to libgfx, as they were only used there.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sat, 19 Aug 2017 15:27:43 +0300 |
parents | 8f71ca1900ea |
children | 65f8fd1bf76e |
line wrap: on
line diff
--- a/tools/libgfx.c Sat Aug 19 15:21:41 2017 +0300 +++ b/tools/libgfx.c Sat Aug 19 15:27:43 2017 +0300 @@ -7,7 +7,6 @@ */ #include "libgfx.h" #include "dmfile.h" -#include "dmbstr.h" #ifdef DM_USE_LIBPNG #include <png.h> @@ -17,6 +16,73 @@ int dmGFXErrorMode = DM_ERRMODE_FAIL; +void dmInitBitStreamContext(DMBitStreamContext *ctx) +{ + ctx->outBuf = 0; + ctx->outByteCount = 0; + ctx->outBitCount = 8; +} + + +BOOL dmPutBits(DMBitStreamContext *ctx, const int val, const int n) +{ + int i; + unsigned int mask = 1 << (n - 1); + + for (i = 0; i < n; i++) + { + ctx->outBuf <<= 1; + + if (val & mask) + ctx->outBuf |= 1; + + mask >>= 1; + ctx->outBitCount--; + + if (ctx->outBitCount == 0) + { + if (!ctx->putByte(ctx, ctx->outBuf & 0xff)) + return FALSE; + + ctx->outBitCount = 8; + ctx->outByteCount++; + } + } + + return TRUE; +} + + +int dmFlushBitStream(DMBitStreamContext *ctx) +{ + if (ctx == NULL) + return DMERR_NULLPTR; + + if (ctx->outBitCount != 8) + dmPutBits(ctx, 0, ctx->outBitCount); + + return 0; +} + + +static BOOL dmPutByteFILE(DMBitStreamContext *ctx, const Uint8 val) +{ + return fputc(val, (FILE *) ctx->handle) == val; +} + + +int dmInitBitStreamFILE(DMBitStreamContext *ctx, FILE *fp) +{ + if (ctx == NULL || fp == NULL) + return DMERR_NULLPTR; + + ctx->putByte = dmPutByteFILE; + ctx->handle = (void *) fp; + + dmInitBitStreamContext(ctx); + + return DMERR_OK; +} BOOL dmCompareColor(const DMColor *c1, const DMColor *c2, BOOL alpha) {