Mercurial > hg > dmlib
annotate dmbstr.c @ 570:a26636faa6b7
Update copyright.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sat, 05 Jan 2013 19:58:23 +0200 |
parents | a7ab6bf5b012 |
children | 136d4ddc7438 |
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 |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
10 int dmInitBitStream(DMBitStream *ctx, FILE *fp) |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
11 { |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
12 if (ctx == NULL || fp == NULL) |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
13 return DMERR_NULLPTR; |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
14 |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
15 ctx->fp = fp; |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
16 ctx->buf = 0; |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
17 ctx->bytecnt = 0; |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
18 ctx->bitcnt = 8; |
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 return DMERR_OK; |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
21 } |
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 |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
24 BOOL dmPutBits(DMBitStream *ctx, const int val, const int n) |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
25 { |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
26 int i; |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
27 unsigned int mask = 1 << (n - 1); |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
28 |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
29 for (i = 0; i < n; i++) |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
30 { |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
31 ctx->buf <<= 1; |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
32 |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
33 if (val & mask) |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
34 ctx->buf |= 1; |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
35 |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
36 mask >>= 1; |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
37 ctx->bitcnt--; |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
38 |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
39 if (ctx->bitcnt == 0) |
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 if (fputc((ctx->buf & 0xff), ctx->fp) == EOF) |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
42 return FALSE; |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
43 |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
44 ctx->bitcnt = 8; |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
45 ctx->bytecnt++; |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
46 } |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
47 } |
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 return TRUE; |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
50 } |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
51 |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
52 |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
53 int dmFlushBitStream(DMBitStream *ctx) |
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 if (ctx == NULL) |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
56 return DMERR_NULLPTR; |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
57 |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
58 if (ctx->bitcnt != 8) |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
59 dmPutBits(ctx, 0, ctx->bitcnt); |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
60 |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
61 return 0; |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
62 } |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
63 |
24548dba1eb6
Add simple bitstream writing functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
64 |