changeset 800:c63e24f9aa9a

Modularize bitstream reader.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 02 Nov 2013 07:34:42 +0200
parents 5ec451795ab2
children 606a7ee88198
files dmbstr.c dmbstr.h libgfx.c
diffstat 3 files changed, 34 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/dmbstr.c	Wed Oct 30 02:25:40 2013 +0200
+++ b/dmbstr.c	Sat Nov 02 07:34:42 2013 +0200
@@ -7,21 +7,35 @@
 #include "dmbstr.h"
 
 
-int dmInitBitStream(DMBitStream *ctx, FILE *fp)
+static int dmPutByteFILE(DMBitStreamContext *ctx, Uint8 val)
+{
+    return fputc(val, (FILE *) ctx->fp);
+}
+
+
+void dmInitBitStreamContext(DMBitStreamContext *ctx)
+{
+    ctx->buf     = 0;
+    ctx->bytecnt = 0;
+    ctx->bitcnt  = 8;
+}
+
+
+int dmInitBitStreamFILE(DMBitStreamContext *ctx, FILE *fp)
 {
     if (ctx == NULL || fp == NULL)
         return DMERR_NULLPTR;
 
-    ctx->fp      = fp;
-    ctx->buf     = 0;
-    ctx->bytecnt = 0;
-    ctx->bitcnt  = 8;
+    ctx->putByte = dmPutByteFILE;
+    ctx->fp      = (void *) fp;
+
+    dmInitBitStreamContext(ctx);
 
     return DMERR_OK;
 }
 
 
-BOOL dmPutBits(DMBitStream *ctx, const int val, const int n)
+BOOL dmPutBits(DMBitStreamContext *ctx, const int val, const int n)
 {
     int i;
     unsigned int mask = 1 << (n - 1);
@@ -38,7 +52,7 @@
 
         if (ctx->bitcnt == 0)
         {
-            if (fputc((ctx->buf & 0xff), ctx->fp) == EOF)
+            if (ctx->putByte(ctx, (ctx->buf & 0xff)) == EOF)
                 return FALSE;
 
             ctx->bitcnt = 8;
@@ -50,7 +64,7 @@
 }
 
 
-int dmFlushBitStream(DMBitStream *ctx)
+int dmFlushBitStream(DMBitStreamContext *ctx)
 {
   if (ctx == NULL)
       return DMERR_NULLPTR;
--- a/dmbstr.h	Wed Oct 30 02:25:40 2013 +0200
+++ b/dmbstr.h	Sat Nov 02 07:34:42 2013 +0200
@@ -9,16 +9,20 @@
 
 #include "dmlib.h"
 
-typedef struct
+typedef struct _DMBitStreamContext
 {
-  FILE *fp;
+  void *fp;
+  int (*putByte)(struct _DMBitStreamContext *ctx, Uint8 val);
+
   int buf, bitcnt, bytecnt;
-} DMBitStream;
+} DMBitStreamContext;
 
 
-int   dmInitBitStream(DMBitStream *ctx, FILE *fp);
-BOOL  dmPutBits(DMBitStream *ctx, const int val, const int n);
-int   dmFlushBitStream(DMBitStream *ctx);
+void  dmInitBitStreamContext(DMBitStreamContext *ctx);
+BOOL  dmPutBits(DMBitStreamContext *ctx, const int val, const int n);
+int   dmFlushBitStream(DMBitStreamContext *ctx);
+
+int   dmInitBitStreamFILE(DMBitStreamContext *ctx, FILE *fp);
 
 
 #ifdef __cplusplus
--- a/libgfx.c	Wed Oct 30 02:25:40 2013 +0200
+++ b/libgfx.c	Sat Nov 02 07:34:42 2013 +0200
@@ -251,9 +251,9 @@
 int dmWriteRAWImageFILE(FILE *fp, DMImage *img, DMImageSpec *spec)
 {
     int xc, yc, plane, res;
-    DMBitStream bs;
+    DMBitStreamContext bs;
     
-    if ((res = dmInitBitStream(&bs, fp)) != DMERR_OK)
+    if ((res = dmInitBitStreamFILE(&bs, fp)) != DMERR_OK)
         return res;
 
     if (spec->interleave)