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 |
matty |
10 |
#include "rdesktop.h" |
22 |
matty |
3 |
|
23 |
matty |
7 |
#define CVAL(p) (*(p++)) |
24 |
|
|
#define SVAL(p) ((*((p++) + 1) << 8) | CVAL(p)) |
25 |
matty |
3 |
|
26 |
matty |
7 |
#define REPEAT(statement) { while ((count > 0) && (x < width)) { statement; count--; x++; } } |
27 |
matty |
9 |
#define MASK_UPDATE() { mixmask <<= 1; if (mixmask == 0) { mask = CVAL(input); mixmask = 1; } } |
28 |
matty |
3 |
|
29 |
matty |
7 |
BOOL bitmap_decompress(unsigned char *output, int width, int height, |
30 |
|
|
unsigned char *input, int size) |
31 |
matty |
3 |
{ |
32 |
|
|
unsigned char *end = input + size; |
33 |
matty |
10 |
unsigned char *prevline = NULL, *line = NULL; |
34 |
matty |
7 |
int opcode, count, offset, isfillormix, x = width; |
35 |
matty |
19 |
int lastopcode = -1, insertmix = False, bicolour = False; |
36 |
matty |
10 |
uint8 code, colour1 = 0, colour2 = 0; |
37 |
|
|
uint8 mixmask, mask = 0, mix = 0xff; |
38 |
matty |
3 |
|
39 |
|
|
while (input < end) |
40 |
|
|
{ |
41 |
matty |
7 |
code = CVAL(input); |
42 |
|
|
opcode = code >> 4; |
43 |
|
|
|
44 |
|
|
/* Handle different opcode forms */ |
45 |
|
|
switch (opcode) |
46 |
matty |
3 |
{ |
47 |
matty |
7 |
case 0xc: |
48 |
|
|
case 0xd: |
49 |
|
|
case 0xe: |
50 |
|
|
opcode -= 6; |
51 |
|
|
count = code & 0xf; |
52 |
|
|
offset = 16; |
53 |
|
|
break; |
54 |
matty |
3 |
|
55 |
matty |
7 |
case 0xf: |
56 |
|
|
opcode = code & 0xf; |
57 |
|
|
count = (opcode < 13) ? SVAL(input) : 1; |
58 |
|
|
offset = 0; |
59 |
|
|
break; |
60 |
|
|
|
61 |
|
|
default: |
62 |
|
|
opcode >>= 1; |
63 |
|
|
count = code & 0x1f; |
64 |
|
|
offset = 32; |
65 |
|
|
break; |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
/* Handle strange cases for counts */ |
69 |
|
|
if (offset != 0) |
70 |
|
|
{ |
71 |
|
|
isfillormix = ((opcode == 2) || (opcode == 7)); |
72 |
|
|
|
73 |
|
|
if (count == 0) |
74 |
matty |
3 |
{ |
75 |
matty |
7 |
if (isfillormix) |
76 |
|
|
count = CVAL(input) + 1; |
77 |
|
|
else |
78 |
|
|
count = CVAL(input) + offset; |
79 |
matty |
3 |
} |
80 |
matty |
7 |
else if (isfillormix) |
81 |
|
|
{ |
82 |
|
|
count <<= 3; |
83 |
|
|
} |
84 |
|
|
} |
85 |
matty |
3 |
|
86 |
matty |
7 |
/* Read preliminary data */ |
87 |
matty |
9 |
mixmask = 0; |
88 |
matty |
7 |
switch (opcode) |
89 |
|
|
{ |
90 |
matty |
24 |
case 0: /* Fill */ |
91 |
matty |
18 |
if ((lastopcode == opcode) |
92 |
|
|
&& !((x == width) && (prevline == NULL))) |
93 |
matty |
9 |
insertmix = True; |
94 |
matty |
3 |
break; |
95 |
matty |
24 |
case 8: /* Bicolour */ |
96 |
matty |
9 |
colour1 = CVAL(input); |
97 |
matty |
24 |
case 3: /* Colour */ |
98 |
matty |
9 |
colour2 = CVAL(input); |
99 |
|
|
break; |
100 |
matty |
24 |
case 6: /* SetMix/Mix */ |
101 |
|
|
case 7: /* SetMix/FillOrMix */ |
102 |
matty |
7 |
mix = CVAL(input); |
103 |
|
|
opcode -= 5; |
104 |
matty |
3 |
break; |
105 |
matty |
7 |
} |
106 |
matty |
9 |
lastopcode = opcode; |
107 |
matty |
3 |
|
108 |
matty |
7 |
/* Output body */ |
109 |
|
|
while (count > 0) |
110 |
|
|
{ |
111 |
|
|
if (x >= width) |
112 |
|
|
{ |
113 |
|
|
if (height <= 0) |
114 |
matty |
9 |
return False; |
115 |
matty |
7 |
|
116 |
|
|
x = 0; |
117 |
|
|
height--; |
118 |
|
|
|
119 |
|
|
prevline = line; |
120 |
|
|
line = output + height * width; |
121 |
|
|
} |
122 |
|
|
|
123 |
|
|
switch (opcode) |
124 |
|
|
{ |
125 |
matty |
24 |
case 0: /* Fill */ |
126 |
matty |
9 |
if (insertmix) |
127 |
|
|
{ |
128 |
|
|
if (prevline == NULL) |
129 |
|
|
line[x] = mix; |
130 |
|
|
else |
131 |
matty |
24 |
line[x] = |
132 |
|
|
prevline[x] ^ |
133 |
|
|
mix; |
134 |
matty |
9 |
|
135 |
|
|
insertmix = False; |
136 |
|
|
count--; |
137 |
|
|
x++; |
138 |
|
|
} |
139 |
|
|
|
140 |
matty |
7 |
if (prevline == NULL) |
141 |
matty |
24 |
{ |
142 |
|
|
REPEAT(line[x] = 0); |
143 |
|
|
} |
144 |
matty |
7 |
else |
145 |
matty |
24 |
{ |
146 |
|
|
REPEAT(line[x] = prevline[x]); |
147 |
|
|
} |
148 |
matty |
3 |
break; |
149 |
matty |
7 |
|
150 |
matty |
24 |
case 1: /* Mix */ |
151 |
matty |
7 |
if (prevline == NULL) |
152 |
matty |
24 |
{ |
153 |
|
|
REPEAT(line[x] = mix); |
154 |
|
|
} |
155 |
matty |
7 |
else |
156 |
matty |
24 |
{ |
157 |
|
|
REPEAT(line[x] = |
158 |
|
|
prevline[x] ^ mix); |
159 |
|
|
} |
160 |
matty |
3 |
break; |
161 |
matty |
7 |
|
162 |
matty |
24 |
case 2: /* Fill or Mix */ |
163 |
matty |
7 |
if (prevline == NULL) |
164 |
matty |
24 |
{ |
165 |
|
|
REPEAT(MASK_UPDATE(); |
166 |
|
|
if (mask & mixmask) |
167 |
|
|
line[x] = mix; |
168 |
|
|
else |
169 |
|
|
line[x] = 0;); |
170 |
|
|
} |
171 |
matty |
7 |
else |
172 |
matty |
24 |
{ |
173 |
|
|
REPEAT(MASK_UPDATE(); |
174 |
|
|
if (mask & mixmask) |
175 |
|
|
line[x] = |
176 |
|
|
prevline[x] ^ mix; |
177 |
|
|
else |
178 |
|
|
line[x] = |
179 |
|
|
prevline[x];); |
180 |
|
|
} |
181 |
matty |
7 |
break; |
182 |
matty |
3 |
|
183 |
matty |
24 |
case 3: /* Colour */ |
184 |
|
|
REPEAT(line[x] = colour2); |
185 |
matty |
7 |
break; |
186 |
matty |
3 |
|
187 |
matty |
24 |
case 4: /* Copy */ |
188 |
|
|
REPEAT(line[x] = CVAL(input)); |
189 |
matty |
7 |
break; |
190 |
matty |
3 |
|
191 |
matty |
24 |
case 8: /* Bicolour */ |
192 |
|
|
REPEAT(if (bicolour) |
193 |
|
|
{ |
194 |
|
|
line[x] = colour2; |
195 |
|
|
bicolour = False;} |
196 |
|
|
else |
197 |
|
|
{ |
198 |
|
|
line[x] = colour1; |
199 |
|
|
bicolour = True; count++;} |
200 |
|
|
); |
201 |
matty |
7 |
break; |
202 |
matty |
3 |
|
203 |
matty |
24 |
case 13: /* White */ |
204 |
|
|
REPEAT(line[x] = 0xff); |
205 |
matty |
7 |
break; |
206 |
matty |
3 |
|
207 |
matty |
24 |
case 14: /* Black */ |
208 |
|
|
REPEAT(line[x] = 0x00); |
209 |
matty |
7 |
break; |
210 |
matty |
3 |
|
211 |
matty |
7 |
default: |
212 |
matty |
24 |
NOTIMP("bitmap opcode 0x%x\n", |
213 |
|
|
opcode); |
214 |
matty |
7 |
return False; |
215 |
|
|
} |
216 |
|
|
} |
217 |
matty |
3 |
} |
218 |
|
|
|
219 |
matty |
7 |
return True; |
220 |
matty |
3 |
} |