comparison tools/libgfx.c @ 1309:5ad7d780a09b

Move bitstream reading functions to libgfx, as they were only used there.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 19 Aug 2017 15:27:43 +0300
parents 8f71ca1900ea
children 65f8fd1bf76e
comparison
equal deleted inserted replaced
1308:8f71ca1900ea 1309:5ad7d780a09b
5 * 5 *
6 * Please read file 'COPYING' for information on license and distribution. 6 * Please read file 'COPYING' for information on license and distribution.
7 */ 7 */
8 #include "libgfx.h" 8 #include "libgfx.h"
9 #include "dmfile.h" 9 #include "dmfile.h"
10 #include "dmbstr.h"
11 10
12 #ifdef DM_USE_LIBPNG 11 #ifdef DM_USE_LIBPNG
13 #include <png.h> 12 #include <png.h>
14 #endif 13 #endif
15 14
16 15
17 int dmGFXErrorMode = DM_ERRMODE_FAIL; 16 int dmGFXErrorMode = DM_ERRMODE_FAIL;
18 17
19 18
19 void dmInitBitStreamContext(DMBitStreamContext *ctx)
20 {
21 ctx->outBuf = 0;
22 ctx->outByteCount = 0;
23 ctx->outBitCount = 8;
24 }
25
26
27 BOOL dmPutBits(DMBitStreamContext *ctx, const int val, const int n)
28 {
29 int i;
30 unsigned int mask = 1 << (n - 1);
31
32 for (i = 0; i < n; i++)
33 {
34 ctx->outBuf <<= 1;
35
36 if (val & mask)
37 ctx->outBuf |= 1;
38
39 mask >>= 1;
40 ctx->outBitCount--;
41
42 if (ctx->outBitCount == 0)
43 {
44 if (!ctx->putByte(ctx, ctx->outBuf & 0xff))
45 return FALSE;
46
47 ctx->outBitCount = 8;
48 ctx->outByteCount++;
49 }
50 }
51
52 return TRUE;
53 }
54
55
56 int dmFlushBitStream(DMBitStreamContext *ctx)
57 {
58 if (ctx == NULL)
59 return DMERR_NULLPTR;
60
61 if (ctx->outBitCount != 8)
62 dmPutBits(ctx, 0, ctx->outBitCount);
63
64 return 0;
65 }
66
67
68 static BOOL dmPutByteFILE(DMBitStreamContext *ctx, const Uint8 val)
69 {
70 return fputc(val, (FILE *) ctx->handle) == val;
71 }
72
73
74 int dmInitBitStreamFILE(DMBitStreamContext *ctx, FILE *fp)
75 {
76 if (ctx == NULL || fp == NULL)
77 return DMERR_NULLPTR;
78
79 ctx->putByte = dmPutByteFILE;
80 ctx->handle = (void *) fp;
81
82 dmInitBitStreamContext(ctx);
83
84 return DMERR_OK;
85 }
20 86
21 BOOL dmCompareColor(const DMColor *c1, const DMColor *c2, BOOL alpha) 87 BOOL dmCompareColor(const DMColor *c1, const DMColor *c2, BOOL alpha)
22 { 88 {
23 if (c1->r == c2->r && 89 if (c1->r == c2->r &&
24 c1->g == c2->g && 90 c1->g == c2->g &&