1 |
matty |
3 |
/* |
2 |
|
|
rdesktop: A Remote Desktop Protocol client. |
3 |
|
|
Bitmap decompression routines |
4 |
|
|
Copyright (C) Matthew Chapman 1999-2000 |
5 |
|
|
|
6 |
|
|
This program is free software; you can redistribute it and/or modify |
7 |
|
|
it under the terms of the GNU General Public License as published by |
8 |
|
|
the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
(at your option) any later version. |
10 |
|
|
|
11 |
|
|
This program is distributed in the hope that it will be useful, |
12 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
GNU General Public License for more details. |
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License |
17 |
|
|
along with this program; if not, write to the Free Software |
18 |
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 |
|
|
*/ |
20 |
|
|
|
21 |
|
|
#include "includes.h" |
22 |
|
|
#include <fcntl.h> |
23 |
|
|
|
24 |
|
|
#define BITMAP_DEBUG 1 |
25 |
|
|
|
26 |
|
|
#if BITMAP_DEBUG |
27 |
|
|
void hexdump(char *filename, unsigned char *data, int length); |
28 |
|
|
#endif |
29 |
|
|
|
30 |
|
|
#define RCVAL() (*(input++)) |
31 |
|
|
#define RSVAL() ((*((input++) + 1) << 8) | RCVAL()) |
32 |
|
|
#define SCVAL(v) {*(output++) = (v);} |
33 |
|
|
|
34 |
|
|
#define FILL() {while (n-- > 0) { if (output - start < width) { SCVAL(0) } else { SCVAL(*(output-width)); }}} |
35 |
|
|
#define MIX() {while (n-- > 0) { if (output - start < width) { SCVAL(mix) } else { SCVAL(*(output-width) ^ mix); }}} |
36 |
|
|
#define COPY() {while (n-- > 0) { SCVAL(RCVAL()); }} |
37 |
|
|
#define COLOR() {int color = RCVAL(); \ |
38 |
|
|
while (n-- > 0) { SCVAL(color); }} |
39 |
|
|
#define BICOLOR() {int color1 = RCVAL(); int color2 = RCVAL(); \ |
40 |
|
|
while (n-- > 0) { SCVAL(color1); SCVAL(color2); }} |
41 |
|
|
#define SETMIX_MIX() {mix = RCVAL(); MIX();} |
42 |
|
|
#define COPY_PACKED() {n++; n/=2; while (n-- > 0) \ |
43 |
|
|
{unsigned char c = RCVAL(); SCVAL((c & 0xF0) >> 4); \ |
44 |
|
|
SCVAL(c & 0x0F); }} |
45 |
|
|
|
46 |
|
|
BOOL bitmap_decompress(unsigned char *input, int size, |
47 |
|
|
unsigned char *output, int width) |
48 |
|
|
{ |
49 |
|
|
unsigned char *savedinput = input; |
50 |
|
|
unsigned char *start = output; |
51 |
|
|
unsigned char *end = input + size; |
52 |
|
|
unsigned char code; |
53 |
|
|
unsigned char mix = 0xFF; |
54 |
|
|
int n, savedn; |
55 |
|
|
|
56 |
|
|
while (input < end) |
57 |
|
|
{ |
58 |
|
|
code = RCVAL(); |
59 |
|
|
switch (code) |
60 |
|
|
{ |
61 |
|
|
case 0x00: // Fill |
62 |
|
|
n = RCVAL() + 32; |
63 |
|
|
FILL(); |
64 |
|
|
break; |
65 |
|
|
case 0xF0: // Fill |
66 |
|
|
n = RSVAL(); |
67 |
|
|
FILL(); |
68 |
|
|
break; |
69 |
|
|
case 0x20: // Mix |
70 |
|
|
n = RCVAL() + 32; |
71 |
|
|
MIX(); |
72 |
|
|
break; |
73 |
|
|
case 0xF1: // Mix |
74 |
|
|
n = RSVAL(); |
75 |
|
|
MIX(); |
76 |
|
|
break; |
77 |
|
|
case 0x40: // FillOrMix |
78 |
|
|
fprintf(stderr, "FillOrMix unsupported\n"); |
79 |
|
|
savedn = n = RCVAL() + 1; |
80 |
|
|
MIX(); |
81 |
|
|
input += (savedn+7)/8; |
82 |
|
|
break; |
83 |
|
|
case 0xF2: |
84 |
|
|
fprintf(stderr, "FillOrMix unsupported\n"); |
85 |
|
|
savedn = n = RSVAL(); |
86 |
|
|
MIX(); |
87 |
|
|
input += (savedn+7)/8; |
88 |
|
|
break; |
89 |
|
|
case 0x60: // Color |
90 |
|
|
n = RCVAL() + 32; |
91 |
|
|
COLOR(); |
92 |
|
|
break; |
93 |
|
|
case 0xF3: |
94 |
|
|
n = RSVAL(); |
95 |
|
|
fprintf(stderr, "Color %d\n", n); |
96 |
|
|
COLOR(); |
97 |
|
|
break; |
98 |
|
|
case 0x80: // Copy |
99 |
|
|
n = RCVAL() + 32; |
100 |
|
|
COPY(); |
101 |
|
|
break; |
102 |
|
|
case 0xF4: |
103 |
|
|
n = RSVAL(); |
104 |
|
|
COPY(); |
105 |
|
|
break; |
106 |
|
|
case 0xA0: // Copy Packed |
107 |
|
|
fprintf(stderr, "CopyPacked 1\n"); |
108 |
|
|
n = RCVAL() + 32; |
109 |
|
|
COPY_PACKED(); |
110 |
|
|
break; |
111 |
|
|
case 0xF5: |
112 |
|
|
fprintf(stderr, "CopyPacked 2\n"); |
113 |
|
|
n = RSVAL(); |
114 |
|
|
COPY_PACKED(); |
115 |
|
|
break; |
116 |
|
|
case 0xC0: // SetMix_Mix |
117 |
|
|
fprintf(stderr, "SetMix_Mix 1\n"); |
118 |
|
|
n = RCVAL() + 16; |
119 |
|
|
SETMIX_MIX(); |
120 |
|
|
break; |
121 |
|
|
case 0xF6: |
122 |
|
|
fprintf(stderr, "SetMix_Mix 2\n"); |
123 |
|
|
n = RSVAL(); |
124 |
|
|
SETMIX_MIX(); |
125 |
|
|
break; |
126 |
|
|
case 0xD0: // SetMix_FillOrMix |
127 |
|
|
fprintf(stderr, "SetMix_FillOrMix unsupported\n"); |
128 |
|
|
savedn = n = RCVAL() + 1; |
129 |
|
|
SETMIX_MIX(); |
130 |
|
|
input += (savedn+7)/8; |
131 |
|
|
break; |
132 |
|
|
case 0xF7: |
133 |
|
|
fprintf(stderr, "SetMix_FillOrMix unsupported\n"); |
134 |
|
|
savedn = n = RSVAL(); |
135 |
|
|
SETMIX_MIX(); |
136 |
|
|
input += (savedn+7)/8; |
137 |
|
|
break; |
138 |
|
|
case 0xE0: // Bicolor |
139 |
|
|
fprintf(stderr, "Bicolor 1\n"); |
140 |
|
|
n = RCVAL() + 16; |
141 |
|
|
BICOLOR(); |
142 |
|
|
break; |
143 |
|
|
case 0xF8: |
144 |
|
|
fprintf(stderr, "Bicolor 2\n"); |
145 |
|
|
n = RSVAL(); |
146 |
|
|
BICOLOR(); |
147 |
|
|
break; |
148 |
|
|
case 0xF9: // FillOrMix_1 |
149 |
|
|
fprintf(stderr, "FillOrMix_1 unsupported\n"); |
150 |
|
|
return False; |
151 |
|
|
case 0xFA: // FillOrMix_2 |
152 |
|
|
fprintf(stderr, "FillOrMix_2 unsupported\n"); |
153 |
|
|
return False; |
154 |
|
|
case 0xFD: // White |
155 |
|
|
SCVAL(0xFF); |
156 |
|
|
break; |
157 |
|
|
case 0xFE: // Black |
158 |
|
|
SCVAL(0); |
159 |
|
|
break; |
160 |
|
|
default: |
161 |
|
|
n = code & 31; |
162 |
|
|
|
163 |
|
|
if (n == 0) |
164 |
|
|
{ |
165 |
|
|
fprintf(stderr, "Undefined escape 0x%X\n", code); |
166 |
|
|
return False; |
167 |
|
|
} |
168 |
|
|
|
169 |
|
|
switch ((code >> 5) & 7) |
170 |
|
|
{ |
171 |
|
|
case 0: // Fill |
172 |
|
|
FILL(); |
173 |
|
|
break; |
174 |
|
|
case 1: // Mix |
175 |
|
|
MIX(); |
176 |
|
|
break; |
177 |
|
|
case 2: // FillOrMix |
178 |
|
|
fprintf(stderr, "FillOrMix unsupported\n"); |
179 |
|
|
n *= 8; |
180 |
|
|
savedn = n; |
181 |
|
|
MIX(); |
182 |
|
|
input += (savedn+7)/8; |
183 |
|
|
break; |
184 |
|
|
case 3: // Color |
185 |
|
|
COLOR(); |
186 |
|
|
break; |
187 |
|
|
case 4: // Copy |
188 |
|
|
COPY(); |
189 |
|
|
break; |
190 |
|
|
case 5: // Copy Packed |
191 |
|
|
fprintf(stderr, "CopyPacked 3\n"); |
192 |
|
|
COPY_PACKED(); |
193 |
|
|
break; |
194 |
|
|
case 6: |
195 |
|
|
n = code & 15; |
196 |
|
|
|
197 |
|
|
switch ((code >> 4) & 15) |
198 |
|
|
{ |
199 |
|
|
case 0xC: |
200 |
|
|
fprintf(stderr, "SetMix_Mix 3\n"); |
201 |
|
|
SETMIX_MIX(); |
202 |
|
|
break; |
203 |
|
|
case 0xD: |
204 |
|
|
fprintf(stderr, "SetMix_FillOrMix unsupported\n"); |
205 |
|
|
n *= 8; |
206 |
|
|
savedn = n; |
207 |
|
|
SETMIX_MIX(); |
208 |
|
|
input += (savedn+7)/8; |
209 |
|
|
break; |
210 |
|
|
case 0xE: |
211 |
|
|
fprintf(stderr, "Bicolor 3\n"); |
212 |
|
|
BICOLOR(); |
213 |
|
|
break; |
214 |
|
|
default: |
215 |
|
|
fprintf(stderr, "Undefined escape 0x%X\n", code); |
216 |
|
|
return False; |
217 |
|
|
} |
218 |
|
|
} |
219 |
|
|
} |
220 |
|
|
} |
221 |
|
|
|
222 |
|
|
printf("Uncompressed size: %d\n", output - start); |
223 |
|
|
#if BITMAP_DEBUG |
224 |
|
|
{ |
225 |
|
|
static int bmpno = 1; |
226 |
|
|
char filename[64]; |
227 |
|
|
|
228 |
|
|
snprintf(filename, sizeof(filename)-1, "in%d.raw", bmpno); |
229 |
|
|
hexdump(filename, savedinput, size); |
230 |
|
|
|
231 |
|
|
snprintf(filename, sizeof(filename)-1, "out%d.raw", bmpno); |
232 |
|
|
hexdump(filename, start, output-start); |
233 |
|
|
|
234 |
|
|
bmpno++; |
235 |
|
|
} |
236 |
|
|
#endif |
237 |
|
|
|
238 |
|
|
return True; |
239 |
|
|
} |
240 |
|
|
|
241 |
|
|
|
242 |
|
|
#if BITMAP_DEBUG |
243 |
|
|
void hexdump(char *filename, unsigned char *data, int length) |
244 |
|
|
{ |
245 |
|
|
/* |
246 |
|
|
int i; |
247 |
|
|
|
248 |
|
|
for (i = 0; i < length; i++) |
249 |
|
|
{ |
250 |
|
|
printf("%02X ", data[i]); |
251 |
|
|
|
252 |
|
|
if (i % 16 == 15) |
253 |
|
|
printf("\n"); |
254 |
|
|
} |
255 |
|
|
*/ |
256 |
|
|
|
257 |
|
|
int fd; |
258 |
|
|
|
259 |
|
|
fd = open(filename, O_WRONLY|O_CREAT, 0600); |
260 |
|
|
write(fd, data, length); |
261 |
|
|
close(fd); |
262 |
|
|
} |
263 |
|
|
#endif |