Mercurial > hg > dmlib
changeset 412:24548dba1eb6
Add simple bitstream writing functions.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sat, 03 Nov 2012 03:49:30 +0200 |
parents | 4e52354e5c25 |
children | a7ab6bf5b012 |
files | Makefile.gen dmbstr.c dmbstr.h |
diffstat | 3 files changed, 92 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/Makefile.gen Sat Nov 03 02:48:03 2012 +0200 +++ b/Makefile.gen Sat Nov 03 03:49:30 2012 +0200 @@ -229,7 +229,7 @@ DMLIB_A=$(OBJPATH)dmlib.a -DMLIB_OBJS += dmfile.o dmlib.o dmlerp.o dmstring.o \ +DMLIB_OBJS += dmfile.o dmbstr.o dmlib.o dmlerp.o dmstring.o \ dmargs.o dmvecmat.o dmperlin.o dmimage.o \ dmwav.o dmengine.o dmtimeline.o dmtimelinew.o dmq3d.o
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dmbstr.c Sat Nov 03 03:49:30 2012 +0200 @@ -0,0 +1,64 @@ +/* + * DMLib + * -- Simple bitstream reader functions + * Programmed and designed by Matti 'ccr' Hamalainen + * (C) Copyright 2012 Tecnic Software productions (TNSP) + */ +#include "dmbstr.h" + + +int dmInitBitStream(DMBitStream *ctx, FILE *fp) +{ + if (ctx == NULL || fp == NULL) + return DMERR_NULLPTR; + + ctx->fp = fp; + ctx->buf = 0; + ctx->bytecnt = 0; + ctx->bitcnt = 8; + + return DMERR_OK; +} + + +BOOL dmPutBits(DMBitStream *ctx, const int val, const int n) +{ + int i; + unsigned int mask = 1 << (n - 1); + + for (i = 0; i < n; i++) + { + ctx->buf <<= 1; + + if (val & mask) + ctx->buf |= 1; + + mask >>= 1; + ctx->bitcnt--; + + if (ctx->bitcnt == 0) + { + if (fputc((ctx->buf & 0xff), ctx->fp) == EOF) + return FALSE; + + ctx->bitcnt = 8; + ctx->bytecnt++; + } + } + + return TRUE; +} + + +int dmFlushBitStream(DMBitStream *ctx) +{ + if (ctx == NULL) + return DMERR_NULLPTR; + + if (ctx->bitcnt != 8) + dmPutBits(ctx, 0, ctx->bitcnt); + + return 0; +} + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dmbstr.h Sat Nov 03 03:49:30 2012 +0200 @@ -0,0 +1,27 @@ +/* + * DMLib + * -- Simple bitstream reader functions + * Programmed and designed by Matti 'ccr' Hamalainen + * (C) Copyright 2012 Tecnic Software productions (TNSP) + */ +#ifndef DMBSTR_H +#define DMBSTR_H + +#include "dmlib.h" + +typedef struct +{ + FILE *fp; + int buf, bitcnt, bytecnt; +} DMBitStream; + + +int dmInitBitStream(DMBitStream *ctx, FILE *fp); +BOOL dmPutBits(DMBitStream *ctx, const int val, const int n); +int dmFlushBitStream(DMBitStream *ctx); + + +#ifdef __cplusplus +} +#endif +#endif // DMBSTR_H