Mercurial > hg > dmlib
annotate src/dmbstr.c @ 1091:988a3397839e
Move functions around for saner order.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 02 Mar 2015 20:30:25 +0200 |
parents | 27d041472c8f |
children | e06abfde6c39 |
rev | line source |
---|---|
412
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1 /* |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
2 * DMLib |
413
a7ab6bf5b012
Correct the file header comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
412
diff
changeset
|
3 * -- Simple bitstream I/O functions |
412
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
4 * Programmed and designed by Matti 'ccr' Hamalainen |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
5 * (C) Copyright 2012 Tecnic Software productions (TNSP) |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
6 */ |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
7 #include "dmbstr.h" |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
8 |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
9 |
800
c63e24f9aa9a
Modularize bitstream reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
748
diff
changeset
|
10 void dmInitBitStreamContext(DMBitStreamContext *ctx) |
c63e24f9aa9a
Modularize bitstream reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
748
diff
changeset
|
11 { |
1089
27d041472c8f
Cleanup, rename some variables, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
12 ctx->outBuf = 0; |
27d041472c8f
Cleanup, rename some variables, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
13 ctx->outByteCount = 0; |
27d041472c8f
Cleanup, rename some variables, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
14 ctx->outBitCount = 8; |
800
c63e24f9aa9a
Modularize bitstream reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
748
diff
changeset
|
15 } |
c63e24f9aa9a
Modularize bitstream reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
748
diff
changeset
|
16 |
c63e24f9aa9a
Modularize bitstream reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
748
diff
changeset
|
17 |
c63e24f9aa9a
Modularize bitstream reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
748
diff
changeset
|
18 BOOL dmPutBits(DMBitStreamContext *ctx, const int val, const int n) |
412
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
19 { |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
20 int i; |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
21 unsigned int mask = 1 << (n - 1); |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
22 |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
23 for (i = 0; i < n; i++) |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
24 { |
1089
27d041472c8f
Cleanup, rename some variables, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
25 ctx->outBuf <<= 1; |
412
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
26 |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
27 if (val & mask) |
1089
27d041472c8f
Cleanup, rename some variables, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
28 ctx->outBuf |= 1; |
412
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
29 |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
30 mask >>= 1; |
1089
27d041472c8f
Cleanup, rename some variables, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
31 ctx->outBitCount--; |
412
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
32 |
1089
27d041472c8f
Cleanup, rename some variables, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
33 if (ctx->outBitCount == 0) |
412
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
34 { |
1089
27d041472c8f
Cleanup, rename some variables, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
35 if (!ctx->putByte(ctx, ctx->outBuf & 0xff)) |
412
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
36 return FALSE; |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
37 |
1089
27d041472c8f
Cleanup, rename some variables, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
38 ctx->outBitCount = 8; |
27d041472c8f
Cleanup, rename some variables, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
39 ctx->outByteCount++; |
412
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
40 } |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
41 } |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
42 |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
43 return TRUE; |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
44 } |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
45 |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
46 |
800
c63e24f9aa9a
Modularize bitstream reader.
Matti Hamalainen <ccr@tnsp.org>
parents:
748
diff
changeset
|
47 int dmFlushBitStream(DMBitStreamContext *ctx) |
412
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
48 { |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
49 if (ctx == NULL) |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
50 return DMERR_NULLPTR; |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
51 |
1089
27d041472c8f
Cleanup, rename some variables, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
52 if (ctx->outBitCount != 8) |
27d041472c8f
Cleanup, rename some variables, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
53 dmPutBits(ctx, 0, ctx->outBitCount); |
412
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
54 |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
55 return 0; |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
56 } |
1091
988a3397839e
Move functions around for saner order.
Matti Hamalainen <ccr@tnsp.org>
parents:
1089
diff
changeset
|
57 |
988a3397839e
Move functions around for saner order.
Matti Hamalainen <ccr@tnsp.org>
parents:
1089
diff
changeset
|
58 |
988a3397839e
Move functions around for saner order.
Matti Hamalainen <ccr@tnsp.org>
parents:
1089
diff
changeset
|
59 static BOOL dmPutByteFILE(DMBitStreamContext *ctx, const Uint8 val) |
988a3397839e
Move functions around for saner order.
Matti Hamalainen <ccr@tnsp.org>
parents:
1089
diff
changeset
|
60 { |
988a3397839e
Move functions around for saner order.
Matti Hamalainen <ccr@tnsp.org>
parents:
1089
diff
changeset
|
61 return fputc(val, (FILE *) ctx->fp) == val; |
988a3397839e
Move functions around for saner order.
Matti Hamalainen <ccr@tnsp.org>
parents:
1089
diff
changeset
|
62 } |
988a3397839e
Move functions around for saner order.
Matti Hamalainen <ccr@tnsp.org>
parents:
1089
diff
changeset
|
63 |
988a3397839e
Move functions around for saner order.
Matti Hamalainen <ccr@tnsp.org>
parents:
1089
diff
changeset
|
64 |
988a3397839e
Move functions around for saner order.
Matti Hamalainen <ccr@tnsp.org>
parents:
1089
diff
changeset
|
65 int dmInitBitStreamFILE(DMBitStreamContext *ctx, FILE *fp) |
988a3397839e
Move functions around for saner order.
Matti Hamalainen <ccr@tnsp.org>
parents:
1089
diff
changeset
|
66 { |
988a3397839e
Move functions around for saner order.
Matti Hamalainen <ccr@tnsp.org>
parents:
1089
diff
changeset
|
67 if (ctx == NULL || fp == NULL) |
988a3397839e
Move functions around for saner order.
Matti Hamalainen <ccr@tnsp.org>
parents:
1089
diff
changeset
|
68 return DMERR_NULLPTR; |
988a3397839e
Move functions around for saner order.
Matti Hamalainen <ccr@tnsp.org>
parents:
1089
diff
changeset
|
69 |
988a3397839e
Move functions around for saner order.
Matti Hamalainen <ccr@tnsp.org>
parents:
1089
diff
changeset
|
70 ctx->putByte = dmPutByteFILE; |
988a3397839e
Move functions around for saner order.
Matti Hamalainen <ccr@tnsp.org>
parents:
1089
diff
changeset
|
71 ctx->fp = (void *) fp; |
988a3397839e
Move functions around for saner order.
Matti Hamalainen <ccr@tnsp.org>
parents:
1089
diff
changeset
|
72 |
988a3397839e
Move functions around for saner order.
Matti Hamalainen <ccr@tnsp.org>
parents:
1089
diff
changeset
|
73 dmInitBitStreamContext(ctx); |
988a3397839e
Move functions around for saner order.
Matti Hamalainen <ccr@tnsp.org>
parents:
1089
diff
changeset
|
74 |
988a3397839e
Move functions around for saner order.
Matti Hamalainen <ccr@tnsp.org>
parents:
1089
diff
changeset
|
75 return DMERR_OK; |
988a3397839e
Move functions around for saner order.
Matti Hamalainen <ccr@tnsp.org>
parents:
1089
diff
changeset
|
76 } |