Mercurial > hg > dmlib
comparison tests/encbr1test.c @ 2024:f41bc7203a16
Add a test program for BR1 compression.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 25 Oct 2018 20:19:53 +0300 |
parents | |
children | 210a0041081b |
comparison
equal
deleted
inserted
replaced
2023:362fb8295f0c | 2024:f41bc7203a16 |
---|---|
1 #include "dmlib.h" | |
2 #include "dmres.h" | |
3 | |
4 | |
5 #define BR_DEBUG | |
6 //#define BR_WRITE | |
7 | |
8 enum | |
9 { | |
10 DMODE_LIT, | |
11 DMODE_RLE, | |
12 }; | |
13 | |
14 | |
15 BOOL dmIFFEncodeByteRun1Flush( | |
16 DMResource *fp, const int mode, const BOOL flush, | |
17 size_t *l_offs, const size_t offs, const Uint8 *buf, | |
18 const Uint8 data, unsigned int *r_count) | |
19 { | |
20 #ifdef BR_DEBUG | |
21 if (flush) | |
22 printf("FLUSH:\n"); | |
23 #endif | |
24 | |
25 if (mode == DMODE_LIT) | |
26 { | |
27 size_t l_count = offs - *l_offs; | |
28 if (l_count > *r_count || flush) | |
29 { | |
30 size_t count = l_count - *r_count; | |
31 Uint8 tmp = count - 1; | |
32 | |
33 #ifdef BR_DEBUG | |
34 printf("L: "); | |
35 for (size_t n = 0; n < count; n++) | |
36 printf("%02x ", buf[*l_offs + n]); | |
37 printf(" [%" DM_PRIu_SIZE_T " -> %d]\n", count, tmp); | |
38 #endif | |
39 | |
40 #ifdef BR_WRITE | |
41 if (!dmf_write_byte(fp, tmp) || | |
42 !dmf_write_str(fp, buf + *l_offs, count)) | |
43 return FALSE; | |
44 #endif | |
45 } | |
46 (*r_count)++; | |
47 } | |
48 else | |
49 { | |
50 if (*r_count > 0) | |
51 { | |
52 unsigned int count = *r_count; | |
53 Uint8 tmp = ((Uint8) count - 2) ^ 0xff; | |
54 | |
55 #ifdef BR_DEBUG | |
56 printf("R: %02x x %x [%02x]\n", data, count, tmp); | |
57 #endif | |
58 #ifdef BR_WRITE | |
59 if (!dmf_write_byte(fp, tmp) || | |
60 !dmf_write_byte(fp, data)) | |
61 return FALSE; | |
62 #endif | |
63 *r_count = 0; | |
64 } | |
65 *l_offs = offs; | |
66 } | |
67 | |
68 return TRUE; | |
69 } | |
70 | |
71 | |
72 BOOL dmIFFEncodeByteRun1Row(DMResource *fp, const Uint8 *buf, const size_t bufLen) | |
73 { | |
74 unsigned int r_count = 0; | |
75 int prev = -1, mode = DMODE_LIT; | |
76 size_t offs, l_offs = 0; | |
77 | |
78 #ifdef BR_DEBUG | |
79 printf("\n\nByteRUN1ROW: %" DM_PRIu_SIZE_T " = $%" DM_PRIx_SIZE_T "\n", bufLen, bufLen); | |
80 #endif | |
81 for (offs = 0; offs < bufLen; offs++) | |
82 { | |
83 Uint8 data = buf[offs]; | |
84 int next_mode; | |
85 BOOL flush; | |
86 | |
87 if (data == prev) | |
88 { | |
89 r_count++; | |
90 next_mode = DMODE_RLE; | |
91 } | |
92 else | |
93 { | |
94 next_mode = DMODE_LIT; | |
95 } | |
96 | |
97 //#ifdef BR_DEBUG | |
98 #if 0 | |
99 printf("%08" DM_PRIx_SIZE_T ": %02x | r_count=%x, l_count=%" DM_PRIx_SIZE_T ", mode=%s [%08" DM_PRIx_SIZE_T "]\n", | |
100 offs, data, r_count, offs - l_offs + 1, mode == DMODE_RLE ? "RLE" : "LIT", l_offs); | |
101 #endif | |
102 | |
103 flush = offs - l_offs >= 128 || r_count >= 128; | |
104 if ((next_mode != mode || flush) && | |
105 !dmIFFEncodeByteRun1Flush(fp, mode, flush, &l_offs, offs, buf, prev, &r_count)) | |
106 return FALSE; | |
107 | |
108 mode = next_mode; | |
109 prev = data; | |
110 } | |
111 | |
112 #ifdef BR_DEBUG | |
113 printf("END\n"); | |
114 #endif | |
115 | |
116 if (!dmIFFEncodeByteRun1Flush(fp, mode, TRUE, &l_offs, offs, buf, prev, &r_count)) | |
117 return FALSE; | |
118 | |
119 return TRUE; | |
120 } | |
121 | |
122 | |
123 | |
124 int main(int argc, char *argv[]) | |
125 { | |
126 (void) argc; | |
127 (void) argv; | |
128 | |
129 Uint8 test[] = | |
130 { | |
131 1, | |
132 0, 0, 0, | |
133 2, | |
134 0, 0, 0, 0, | |
135 3, 3, 3, | |
136 2, 2, | |
137 0, | |
138 1, | |
139 0, 0, 0, 0, 0, 0, 0, 0, | |
140 | |
141 1, 2, 3, 4, 5, 6, 7, 8, | |
142 | |
143 0, 0, 0, 0, 0, | |
144 | |
145 32, | |
146 | |
147 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
148 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
149 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
150 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
151 | |
152 // 15, | |
153 | |
154 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
155 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
156 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
157 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
158 | |
159 1 | |
160 //0, 0, 0, 0, | |
161 //1 | |
162 | |
163 }; | |
164 | |
165 dmIFFEncodeByteRun1Row(NULL, test, sizeof(test)); | |
166 return 0; | |
167 } |