1 |
/* |
/* |
2 |
rdesktop: A Remote Desktop Protocol client. |
rdesktop: A Remote Desktop Protocol client. |
3 |
Bitmap decompression routines |
Bitmap decompression routines |
4 |
Copyright (C) Matthew Chapman 1999-2000 |
Copyright (C) Matthew Chapman 1999-2001 |
5 |
|
|
6 |
This program is free software; you can redistribute it and/or modify |
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 |
it under the terms of the GNU General Public License as published by |
18 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 |
*/ |
*/ |
20 |
|
|
21 |
#include "includes.h" |
#include "rdesktop.h" |
22 |
|
|
23 |
#define CVAL(p) (*(p++)) |
#define CVAL(p) (*(p++)) |
|
#define SVAL(p) ((*((p++) + 1) << 8) | CVAL(p)) |
|
24 |
|
|
25 |
#define REPEAT(statement) { while ((count > 0) && (x < width)) { statement; count--; x++; } } |
#define UNROLL8(exp) { exp exp exp exp exp exp exp exp } |
|
#define MASK_UPDATE() { maskpix <<= 1; if (maskpix == 0) { mask = CVAL(input); maskpix = 1; } } |
|
26 |
|
|
27 |
BOOL bitmap_decompress(unsigned char *output, int width, int height, |
#define REPEAT(statement) \ |
28 |
unsigned char *input, int size) |
{ \ |
29 |
|
while((count & ~0x7) && ((x+8) < width)) \ |
30 |
|
UNROLL8( statement; count--; x++; ); \ |
31 |
|
\ |
32 |
|
while((count > 0) && (x < width)) { statement; count--; x++; } \ |
33 |
|
} |
34 |
|
|
35 |
|
#define MASK_UPDATE() \ |
36 |
|
{ \ |
37 |
|
mixmask <<= 1; \ |
38 |
|
if (mixmask == 0) \ |
39 |
|
{ \ |
40 |
|
mask = fom_mask ? fom_mask : CVAL(input); \ |
41 |
|
mixmask = 1; \ |
42 |
|
} \ |
43 |
|
} |
44 |
|
|
45 |
|
BOOL |
46 |
|
bitmap_decompress(unsigned char *output, int width, int height, |
47 |
|
unsigned char *input, int size) |
48 |
{ |
{ |
49 |
unsigned char *end = input + size; |
unsigned char *end = input + size; |
50 |
unsigned char *prevline, *line = NULL; |
unsigned char *prevline = NULL, *line = NULL; |
51 |
int opcode, count, offset, isfillormix, x = width; |
int opcode, count, offset, isfillormix, x = width; |
52 |
uint8 code, mask, maskpix, color1, color2; |
int lastopcode = -1, insertmix = False, bicolour = False; |
53 |
uint8 mix = 0xff; |
uint8 code, colour1 = 0, colour2 = 0; |
54 |
|
uint8 mixmask, mask = 0, mix = 0xff; |
55 |
|
int fom_mask = 0; |
56 |
|
|
|
dump_data(input, end-input); |
|
57 |
while (input < end) |
while (input < end) |
58 |
{ |
{ |
59 |
fprintf(stderr, "Offset %d from end\n", end-input); |
fom_mask = 0; |
60 |
code = CVAL(input); |
code = CVAL(input); |
61 |
opcode = code >> 4; |
opcode = code >> 4; |
62 |
|
|
73 |
|
|
74 |
case 0xf: |
case 0xf: |
75 |
opcode = code & 0xf; |
opcode = code & 0xf; |
76 |
count = (opcode < 13) ? SVAL(input) : 1; |
if (opcode < 9) |
77 |
|
{ |
78 |
|
count = CVAL(input); |
79 |
|
count |= CVAL(input) << 8; |
80 |
|
} |
81 |
|
else |
82 |
|
{ |
83 |
|
count = (opcode < 0xb) ? 8 : 1; |
84 |
|
} |
85 |
offset = 0; |
offset = 0; |
86 |
break; |
break; |
87 |
|
|
111 |
} |
} |
112 |
|
|
113 |
/* Read preliminary data */ |
/* Read preliminary data */ |
|
maskpix = 0; |
|
114 |
switch (opcode) |
switch (opcode) |
115 |
{ |
{ |
116 |
case 3: /* Color */ |
case 0: /* Fill */ |
117 |
color1 = CVAL(input); |
if ((lastopcode == opcode) |
118 |
case 8: /* Bicolor */ |
&& !((x == width) && (prevline == NULL))) |
119 |
color2 = CVAL(input); |
insertmix = True; |
120 |
|
break; |
121 |
|
case 8: /* Bicolour */ |
122 |
|
colour1 = CVAL(input); |
123 |
|
case 3: /* Colour */ |
124 |
|
colour2 = CVAL(input); |
125 |
break; |
break; |
126 |
case 6: /* SetMix/Mix */ |
case 6: /* SetMix/Mix */ |
127 |
case 7: /* SetMix/FillOrMix */ |
case 7: /* SetMix/FillOrMix */ |
128 |
mix = CVAL(input); |
mix = CVAL(input); |
129 |
opcode -= 5; |
opcode -= 5; |
130 |
break; |
break; |
131 |
|
case 9: /* FillOrMix_1 */ |
132 |
|
mask = 0x03; |
133 |
|
opcode = 0x02; |
134 |
|
fom_mask = 3; |
135 |
|
break; |
136 |
|
case 0x0a: /* FillOrMix_2 */ |
137 |
|
mask = 0x05; |
138 |
|
opcode = 0x02; |
139 |
|
fom_mask = 5; |
140 |
|
break; |
141 |
|
|
142 |
} |
} |
143 |
|
|
144 |
|
lastopcode = opcode; |
145 |
|
mixmask = 0; |
146 |
|
|
147 |
/* Output body */ |
/* Output body */ |
148 |
while (count > 0) |
while (count > 0) |
149 |
{ |
{ |
150 |
if (x >= width) |
if (x >= width) |
151 |
{ |
{ |
152 |
if (height <= 0) |
if (height <= 0) |
153 |
return True; |
return False; |
154 |
|
|
155 |
x = 0; |
x = 0; |
156 |
height--; |
height--; |
161 |
|
|
162 |
switch (opcode) |
switch (opcode) |
163 |
{ |
{ |
164 |
case 0: /* Fill */ |
case 0: /* Fill */ |
165 |
fprintf(stderr, "Fill %d\n", count); |
if (insertmix) |
166 |
|
{ |
167 |
|
if (prevline == NULL) |
168 |
|
line[x] = mix; |
169 |
|
else |
170 |
|
line[x] = |
171 |
|
prevline[x] ^ |
172 |
|
mix; |
173 |
|
|
174 |
|
insertmix = False; |
175 |
|
count--; |
176 |
|
x++; |
177 |
|
} |
178 |
|
|
179 |
if (prevline == NULL) |
if (prevline == NULL) |
180 |
REPEAT(line[x] = 0) |
{ |
181 |
|
REPEAT(line[x] = 0); |
182 |
|
} |
183 |
else |
else |
184 |
REPEAT(line[x] = prevline[x]) |
{ |
185 |
|
REPEAT(line[x] = prevline[x]); |
186 |
|
} |
187 |
break; |
break; |
188 |
|
|
189 |
case 1: /* Mix */ |
case 1: /* Mix */ |
|
fprintf(stderr, "Mix %d\n", count); |
|
190 |
if (prevline == NULL) |
if (prevline == NULL) |
191 |
REPEAT(line[x] = mix) |
{ |
192 |
|
REPEAT(line[x] = mix); |
193 |
|
} |
194 |
else |
else |
195 |
REPEAT(line[x] = prevline[x] ^ mix) |
{ |
196 |
|
REPEAT(line[x] = |
197 |
|
prevline[x] ^ mix); |
198 |
|
} |
199 |
break; |
break; |
200 |
|
|
201 |
#if 0 |
case 2: /* Fill or Mix */ |
|
case 2: /* Fill or Mix */ |
|
|
REPEAT(line[x] = 0); |
|
|
break; |
|
202 |
if (prevline == NULL) |
if (prevline == NULL) |
203 |
REPEAT( |
{ |
204 |
MASK_UPDATE(); |
REPEAT(MASK_UPDATE(); |
205 |
|
if (mask & mixmask) |
206 |
if (mask & maskpix) |
line[x] = mix; |
207 |
line[x] = mix; |
else |
208 |
else |
line[x] = 0;); |
209 |
line[x] = 0; |
} |
|
) |
|
210 |
else |
else |
211 |
REPEAT( |
{ |
212 |
MASK_UPDATE(); |
REPEAT(MASK_UPDATE(); |
213 |
|
if (mask & mixmask) |
214 |
if (mask & maskpix) |
line[x] = |
215 |
line[x] = prevline[x] ^ mix; |
prevline[x] ^ mix; |
216 |
else |
else |
217 |
line[x] = prevline[x]; |
line[x] = |
218 |
) |
prevline[x];); |
219 |
|
} |
220 |
break; |
break; |
|
#endif |
|
221 |
|
|
222 |
case 3: /* Colour */ |
case 3: /* Colour */ |
223 |
fprintf(stderr, "Colour %d\n", count); |
REPEAT(line[x] = colour2); |
|
REPEAT(line[x] = color2) |
|
224 |
break; |
break; |
225 |
|
|
226 |
case 4: /* Copy */ |
case 4: /* Copy */ |
227 |
fprintf(stderr, "Copy %d\n", count); |
REPEAT(line[x] = CVAL(input)); |
|
REPEAT(line[x] = CVAL(input)) |
|
228 |
break; |
break; |
229 |
|
|
230 |
#if 0 |
case 8: /* Bicolour */ |
231 |
case 8: /* Bicolor */ |
REPEAT(if (bicolour) |
232 |
REPEAT(line[x] = color1; line[++x] = color2) |
{ |
233 |
|
line[x] = colour2; |
234 |
|
bicolour = False;} |
235 |
|
else |
236 |
|
{ |
237 |
|
line[x] = colour1; |
238 |
|
bicolour = True; count++;} |
239 |
|
); |
240 |
break; |
break; |
241 |
|
|
242 |
case 13: /* White */ |
case 0xd: /* White */ |
243 |
REPEAT(line[x] = 0xff) |
REPEAT(line[x] = 0xff); |
244 |
break; |
break; |
245 |
|
|
246 |
case 14: /* Black */ |
case 0xe: /* Black */ |
247 |
REPEAT(line[x] = 0x00) |
REPEAT(line[x] = 0x00); |
248 |
break; |
break; |
|
#endif |
|
249 |
|
|
250 |
default: |
default: |
251 |
fprintf(stderr, "Unknown bitmap opcode 0x%x\n", opcode); |
unimpl("bitmap opcode 0x%x\n", |
252 |
|
opcode); |
253 |
return False; |
return False; |
254 |
} |
} |
255 |
} |
} |