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" |
|
#include <fcntl.h> |
|
22 |
|
|
23 |
#define BITMAP_DEBUG 1 |
#define CVAL(p) (*(p++)) |
24 |
|
#define SVAL(p) ((*((p++) + 1) << 8) | CVAL(p)) |
25 |
|
|
26 |
#if BITMAP_DEBUG |
#define REPEAT(statement) { while ((count > 0) && (x < width)) { statement; count--; x++; } } |
27 |
void hexdump(char *filename, unsigned char *data, int length); |
#define MASK_UPDATE() { mixmask <<= 1; if (mixmask == 0) { mask = CVAL(input); mixmask = 1; } } |
|
#endif |
|
|
|
|
|
#define RCVAL() (*(input++)) |
|
|
#define RSVAL() ((*((input++) + 1) << 8) | RCVAL()) |
|
|
#define SCVAL(v) {*(output++) = (v);} |
|
|
|
|
|
#define FILL() {while (n-- > 0) { if (output - start < width) { SCVAL(0) } else { SCVAL(*(output-width)); }}} |
|
|
#define MIX() {while (n-- > 0) { if (output - start < width) { SCVAL(mix) } else { SCVAL(*(output-width) ^ mix); }}} |
|
|
#define COPY() {while (n-- > 0) { SCVAL(RCVAL()); }} |
|
|
#define COLOR() {int color = RCVAL(); \ |
|
|
while (n-- > 0) { SCVAL(color); }} |
|
|
#define BICOLOR() {int color1 = RCVAL(); int color2 = RCVAL(); \ |
|
|
while (n-- > 0) { SCVAL(color1); SCVAL(color2); }} |
|
|
#define SETMIX_MIX() {mix = RCVAL(); MIX();} |
|
|
#define COPY_PACKED() {n++; n/=2; while (n-- > 0) \ |
|
|
{unsigned char c = RCVAL(); SCVAL((c & 0xF0) >> 4); \ |
|
|
SCVAL(c & 0x0F); }} |
|
28 |
|
|
29 |
BOOL bitmap_decompress(unsigned char *input, int size, |
BOOL bitmap_decompress(unsigned char *output, int width, int height, |
30 |
unsigned char *output, int width) |
unsigned char *input, int size) |
31 |
{ |
{ |
|
unsigned char *savedinput = input; |
|
|
unsigned char *start = output; |
|
32 |
unsigned char *end = input + size; |
unsigned char *end = input + size; |
33 |
unsigned char code; |
unsigned char *prevline = NULL, *line = NULL; |
34 |
unsigned char mix = 0xFF; |
int opcode, count, offset, isfillormix, x = width; |
35 |
int n, savedn; |
int lastopcode = -1, insertmix = False, bicolour = False; |
36 |
|
uint8 code, colour1 = 0, colour2 = 0; |
37 |
|
uint8 mixmask, mask = 0, mix = 0xff; |
38 |
|
|
39 |
while (input < end) |
while (input < end) |
40 |
{ |
{ |
41 |
code = RCVAL(); |
code = CVAL(input); |
42 |
switch (code) |
opcode = code >> 4; |
43 |
|
|
44 |
|
/* Handle different opcode forms */ |
45 |
|
switch (opcode) |
46 |
|
{ |
47 |
|
case 0xc: |
48 |
|
case 0xd: |
49 |
|
case 0xe: |
50 |
|
opcode -= 6; |
51 |
|
count = code & 0xf; |
52 |
|
offset = 16; |
53 |
|
break; |
54 |
|
|
55 |
|
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 |
case 0x00: // Fill |
isfillormix = ((opcode == 2) || (opcode == 7)); |
|
n = RCVAL() + 32; |
|
|
FILL(); |
|
|
break; |
|
|
case 0xF0: // Fill |
|
|
n = RSVAL(); |
|
|
FILL(); |
|
|
break; |
|
|
case 0x20: // Mix |
|
|
n = RCVAL() + 32; |
|
|
MIX(); |
|
|
break; |
|
|
case 0xF1: // Mix |
|
|
n = RSVAL(); |
|
|
MIX(); |
|
|
break; |
|
|
case 0x40: // FillOrMix |
|
|
fprintf(stderr, "FillOrMix unsupported\n"); |
|
|
savedn = n = RCVAL() + 1; |
|
|
MIX(); |
|
|
input += (savedn+7)/8; |
|
|
break; |
|
|
case 0xF2: |
|
|
fprintf(stderr, "FillOrMix unsupported\n"); |
|
|
savedn = n = RSVAL(); |
|
|
MIX(); |
|
|
input += (savedn+7)/8; |
|
|
break; |
|
|
case 0x60: // Color |
|
|
n = RCVAL() + 32; |
|
|
COLOR(); |
|
|
break; |
|
|
case 0xF3: |
|
|
n = RSVAL(); |
|
|
fprintf(stderr, "Color %d\n", n); |
|
|
COLOR(); |
|
|
break; |
|
|
case 0x80: // Copy |
|
|
n = RCVAL() + 32; |
|
|
COPY(); |
|
|
break; |
|
|
case 0xF4: |
|
|
n = RSVAL(); |
|
|
COPY(); |
|
|
break; |
|
|
case 0xA0: // Copy Packed |
|
|
fprintf(stderr, "CopyPacked 1\n"); |
|
|
n = RCVAL() + 32; |
|
|
COPY_PACKED(); |
|
|
break; |
|
|
case 0xF5: |
|
|
fprintf(stderr, "CopyPacked 2\n"); |
|
|
n = RSVAL(); |
|
|
COPY_PACKED(); |
|
|
break; |
|
|
case 0xC0: // SetMix_Mix |
|
|
fprintf(stderr, "SetMix_Mix 1\n"); |
|
|
n = RCVAL() + 16; |
|
|
SETMIX_MIX(); |
|
|
break; |
|
|
case 0xF6: |
|
|
fprintf(stderr, "SetMix_Mix 2\n"); |
|
|
n = RSVAL(); |
|
|
SETMIX_MIX(); |
|
|
break; |
|
|
case 0xD0: // SetMix_FillOrMix |
|
|
fprintf(stderr, "SetMix_FillOrMix unsupported\n"); |
|
|
savedn = n = RCVAL() + 1; |
|
|
SETMIX_MIX(); |
|
|
input += (savedn+7)/8; |
|
|
break; |
|
|
case 0xF7: |
|
|
fprintf(stderr, "SetMix_FillOrMix unsupported\n"); |
|
|
savedn = n = RSVAL(); |
|
|
SETMIX_MIX(); |
|
|
input += (savedn+7)/8; |
|
|
break; |
|
|
case 0xE0: // Bicolor |
|
|
fprintf(stderr, "Bicolor 1\n"); |
|
|
n = RCVAL() + 16; |
|
|
BICOLOR(); |
|
|
break; |
|
|
case 0xF8: |
|
|
fprintf(stderr, "Bicolor 2\n"); |
|
|
n = RSVAL(); |
|
|
BICOLOR(); |
|
|
break; |
|
|
case 0xF9: // FillOrMix_1 |
|
|
fprintf(stderr, "FillOrMix_1 unsupported\n"); |
|
|
return False; |
|
|
case 0xFA: // FillOrMix_2 |
|
|
fprintf(stderr, "FillOrMix_2 unsupported\n"); |
|
|
return False; |
|
|
case 0xFD: // White |
|
|
SCVAL(0xFF); |
|
|
break; |
|
|
case 0xFE: // Black |
|
|
SCVAL(0); |
|
|
break; |
|
|
default: |
|
|
n = code & 31; |
|
72 |
|
|
73 |
if (n == 0) |
if (count == 0) |
74 |
{ |
{ |
75 |
fprintf(stderr, "Undefined escape 0x%X\n", code); |
if (isfillormix) |
76 |
return False; |
count = CVAL(input) + 1; |
77 |
|
else |
78 |
|
count = CVAL(input) + offset; |
79 |
} |
} |
80 |
|
else if (isfillormix) |
|
switch ((code >> 5) & 7) |
|
81 |
{ |
{ |
82 |
case 0: // Fill |
count <<= 3; |
83 |
FILL(); |
} |
84 |
break; |
} |
85 |
case 1: // Mix |
|
86 |
MIX(); |
/* Read preliminary data */ |
87 |
|
mixmask = 0; |
88 |
|
switch (opcode) |
89 |
|
{ |
90 |
|
case 0: /* Fill */ |
91 |
|
if ((lastopcode == opcode) |
92 |
|
&& !((x == width) && (prevline == NULL))) |
93 |
|
insertmix = True; |
94 |
|
break; |
95 |
|
case 8: /* Bicolour */ |
96 |
|
colour1 = CVAL(input); |
97 |
|
case 3: /* Colour */ |
98 |
|
colour2 = CVAL(input); |
99 |
|
break; |
100 |
|
case 6: /* SetMix/Mix */ |
101 |
|
case 7: /* SetMix/FillOrMix */ |
102 |
|
mix = CVAL(input); |
103 |
|
opcode -= 5; |
104 |
break; |
break; |
105 |
case 2: // FillOrMix |
} |
106 |
fprintf(stderr, "FillOrMix unsupported\n"); |
lastopcode = opcode; |
107 |
n *= 8; |
|
108 |
savedn = n; |
/* Output body */ |
109 |
MIX(); |
while (count > 0) |
110 |
input += (savedn+7)/8; |
{ |
111 |
break; |
if (x >= width) |
112 |
case 3: // Color |
{ |
113 |
COLOR(); |
if (height <= 0) |
|
break; |
|
|
case 4: // Copy |
|
|
COPY(); |
|
|
break; |
|
|
case 5: // Copy Packed |
|
|
fprintf(stderr, "CopyPacked 3\n"); |
|
|
COPY_PACKED(); |
|
|
break; |
|
|
case 6: |
|
|
n = code & 15; |
|
|
|
|
|
switch ((code >> 4) & 15) |
|
|
{ |
|
|
case 0xC: |
|
|
fprintf(stderr, "SetMix_Mix 3\n"); |
|
|
SETMIX_MIX(); |
|
|
break; |
|
|
case 0xD: |
|
|
fprintf(stderr, "SetMix_FillOrMix unsupported\n"); |
|
|
n *= 8; |
|
|
savedn = n; |
|
|
SETMIX_MIX(); |
|
|
input += (savedn+7)/8; |
|
|
break; |
|
|
case 0xE: |
|
|
fprintf(stderr, "Bicolor 3\n"); |
|
|
BICOLOR(); |
|
|
break; |
|
|
default: |
|
|
fprintf(stderr, "Undefined escape 0x%X\n", code); |
|
114 |
return False; |
return False; |
115 |
} |
|
116 |
|
x = 0; |
117 |
|
height--; |
118 |
|
|
119 |
|
prevline = line; |
120 |
|
line = output + height * width; |
121 |
} |
} |
|
} |
|
|
} |
|
122 |
|
|
123 |
printf("Uncompressed size: %d\n", output - start); |
switch (opcode) |
124 |
#if BITMAP_DEBUG |
{ |
125 |
{ |
case 0: /* Fill */ |
126 |
static int bmpno = 1; |
if (insertmix) |
127 |
char filename[64]; |
{ |
128 |
|
if (prevline == NULL) |
129 |
|
line[x] = mix; |
130 |
|
else |
131 |
|
line[x] = prevline[x] ^ mix; |
132 |
|
|
133 |
|
insertmix = False; |
134 |
|
count--; |
135 |
|
x++; |
136 |
|
} |
137 |
|
|
138 |
|
if (prevline == NULL) |
139 |
|
REPEAT(line[x] = 0) |
140 |
|
else |
141 |
|
REPEAT(line[x] = prevline[x]) |
142 |
|
break; |
143 |
|
|
144 |
snprintf(filename, sizeof(filename)-1, "in%d.raw", bmpno); |
case 1: /* Mix */ |
145 |
hexdump(filename, savedinput, size); |
if (prevline == NULL) |
146 |
|
REPEAT(line[x] = mix) |
147 |
|
else |
148 |
|
REPEAT(line[x] = prevline[x] ^ mix) |
149 |
|
break; |
150 |
|
|
151 |
snprintf(filename, sizeof(filename)-1, "out%d.raw", bmpno); |
case 2: /* Fill or Mix */ |
152 |
hexdump(filename, start, output-start); |
if (prevline == NULL) |
153 |
|
REPEAT( |
154 |
|
MASK_UPDATE(); |
155 |
|
|
156 |
|
if (mask & mixmask) |
157 |
|
line[x] = mix; |
158 |
|
else |
159 |
|
line[x] = 0; |
160 |
|
) |
161 |
|
else |
162 |
|
REPEAT( |
163 |
|
MASK_UPDATE(); |
164 |
|
|
165 |
|
if (mask & mixmask) |
166 |
|
line[x] = prevline[x] ^ mix; |
167 |
|
else |
168 |
|
line[x] = prevline[x]; |
169 |
|
) |
170 |
|
break; |
171 |
|
|
172 |
bmpno++; |
case 3: /* Colour */ |
173 |
} |
REPEAT(line[x] = colour2) |
174 |
#endif |
break; |
175 |
|
|
176 |
return True; |
case 4: /* Copy */ |
177 |
} |
REPEAT(line[x] = CVAL(input)) |
178 |
|
break; |
179 |
|
|
180 |
|
case 8: /* Bicolour */ |
181 |
|
REPEAT( |
182 |
|
if (bicolour) |
183 |
|
{ |
184 |
|
line[x] = colour2; |
185 |
|
bicolour = False; |
186 |
|
} |
187 |
|
else |
188 |
|
{ |
189 |
|
line[x] = colour1; |
190 |
|
bicolour = True; |
191 |
|
count++; |
192 |
|
} |
193 |
|
) |
194 |
|
break; |
195 |
|
|
196 |
#if BITMAP_DEBUG |
case 13: /* White */ |
197 |
void hexdump(char *filename, unsigned char *data, int length) |
REPEAT(line[x] = 0xff) |
198 |
{ |
break; |
|
/* |
|
|
int i; |
|
199 |
|
|
200 |
for (i = 0; i < length; i++) |
case 14: /* Black */ |
201 |
{ |
REPEAT(line[x] = 0x00) |
202 |
printf("%02X ", data[i]); |
break; |
203 |
|
|
204 |
if (i % 16 == 15) |
default: |
205 |
printf("\n"); |
NOTIMP("bitmap opcode 0x%x\n", opcode); |
206 |
|
return False; |
207 |
|
} |
208 |
|
} |
209 |
} |
} |
|
*/ |
|
210 |
|
|
211 |
int fd; |
return True; |
|
|
|
|
fd = open(filename, O_WRONLY|O_CREAT, 0600); |
|
|
write(fd, data, length); |
|
|
close(fd); |
|
212 |
} |
} |
|
#endif |
|