# HG changeset patch # User Matti Hamalainen # Date 1503145663 -10800 # Node ID 5ad7d780a09b758084ace054ee4d16aceeef9328 # Parent 8f71ca1900ea7d7db12c0ccba7fb88fa25a12e8b Move bitstream reading functions to libgfx, as they were only used there. diff -r 8f71ca1900ea -r 5ad7d780a09b Makefile.gen --- a/Makefile.gen Sat Aug 19 15:21:41 2017 +0300 +++ b/Makefile.gen Sat Aug 19 15:27:43 2017 +0300 @@ -305,7 +305,7 @@ DMLIB_A=$(OBJPATH)dmlib.a DMLIB_OBJS += \ - dmfile.o dmbstr.o dmlib.o dmcurves.o dmstring.o \ + dmfile.o dmlib.o dmcurves.o dmstring.o \ dmargs.o dmvecmat.o dmperlin.o dmimage.o \ dmwav.o dmengine.o dmfft.o dmzlib.o diff -r 8f71ca1900ea -r 5ad7d780a09b src/dmbstr.c --- a/src/dmbstr.c Sat Aug 19 15:21:41 2017 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,76 +0,0 @@ -/* - * DMLib - * -- Simple bitstream I/O functions - * Programmed and designed by Matti 'ccr' Hamalainen - * (C) Copyright 2012 Tecnic Software productions (TNSP) - */ -#include "dmbstr.h" - - -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->fp) == val; -} - - -int dmInitBitStreamFILE(DMBitStreamContext *ctx, FILE *fp) -{ - if (ctx == NULL || fp == NULL) - return DMERR_NULLPTR; - - ctx->putByte = dmPutByteFILE; - ctx->fp = (void *) fp; - - dmInitBitStreamContext(ctx); - - return DMERR_OK; -} diff -r 8f71ca1900ea -r 5ad7d780a09b src/dmbstr.h --- a/src/dmbstr.h Sat Aug 19 15:21:41 2017 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,40 +0,0 @@ -/* - * DMLib - * -- Simple bitstream I/O functions - * Programmed and designed by Matti 'ccr' Hamalainen - * (C) Copyright 2012 Tecnic Software productions (TNSP) - */ -#ifndef DMBSTR_H -#define DMBSTR_H - -#include "dmlib.h" - - -#ifdef __cplusplus -extern "C" { -#endif - - -typedef struct _DMBitStreamContext -{ - void *fp; - - BOOL (*putByte)(struct _DMBitStreamContext *ctx, Uint8 val); - - int outBuf, outBitCount, outByteCount; -} DMBitStreamContext; - - -void dmInitBitStreamContext(DMBitStreamContext *ctx); -int dmFlushBitStream(DMBitStreamContext *ctx); - -BOOL dmPutBits(DMBitStreamContext *ctx, const int val, const int n); - - -int dmInitBitStreamFILE(DMBitStreamContext *ctx, FILE *fp); - - -#ifdef __cplusplus -} -#endif -#endif // DMBSTR_H diff -r 8f71ca1900ea -r 5ad7d780a09b tools/libgfx.c --- 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 @@ -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) { diff -r 8f71ca1900ea -r 5ad7d780a09b tools/libgfx.h --- a/tools/libgfx.h Sat Aug 19 15:21:41 2017 +0300 +++ b/tools/libgfx.h Sat Aug 19 15:27:43 2017 +0300 @@ -124,6 +124,23 @@ int dmReadPCXImage(const char *filename, DMImage **pimg); +typedef struct _DMBitStreamContext +{ + void *handle; + + BOOL (*putByte)(struct _DMBitStreamContext *ctx, Uint8 val); + + int outBuf, outBitCount, outByteCount; +} DMBitStreamContext; + + +void dmInitBitStreamContext(DMBitStreamContext *ctx); +int dmFlushBitStream(DMBitStreamContext *ctx); +BOOL dmPutBits(DMBitStreamContext *ctx, const int val, const int n); +int dmInitBitStreamFILE(DMBitStreamContext *ctx, FILE *fp); + + + #ifdef __cplusplus } #endif