1 | | - | /* -*- c-basic-offset: 8 -*- |
2 | | - | rdesktop: A Remote Desktop Protocol client. |
3 | | - | Bitmap decompression routines |
4 | | - | Copyright (C) Matthew Chapman <matthewc.unsw.edu.au> 1999-2008 |
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 3 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, see <http://www.gnu.org/licenses/>. |
18 | | - | */ |
19 | | - | |
20 | | - | /* |
21 | | - | Added few modifications: |
22 | | - | The code now ALWAYS returns RGB32 format regardless of input bpp. Makes life easier on upper levels. |
23 | | - | The code also takes non-compressed input (and a flag that indicates wether it's compressed or not.) |
24 | | - | Non-compressed data will be only converted to RGB32. |
25 | | - | There are some out-of-bounds write possibilities in the RRE decoder. |
26 | | - | |
27 | | - | Mod author: Tamas Jos @skelsec |
28 | | - | */ |
29 | | - | |
30 | | - | /* three seperate function for speed when decompressing the bitmaps |
31 | | - | when modifing one function make the change in the others |
32 | | - | [email protected] */ |
33 | | - | |
34 | | - | /* indent is confused by this file */ |
35 | | - | /* *INDENT-OFF* */ |
36 | | - | |
37 | | - | #include <stdlib.h> |
38 | | - | #include <Python.h> |
39 | | - | |
40 | | - | /* Specific rename for RDPY integration */ |
41 | | - | #define uint8 unsigned char |
42 | | - | #define uint16 unsigned short |
43 | | - | #define uint unsigned int |
44 | | - | #define unimpl(str, code) |
45 | | - | |
46 | | - | #define RD_BOOL int |
47 | | - | #define False 0 |
48 | | - | #define True 1 |
49 | | - | /* end specific rename */ |
50 | | - | |
51 | | - | #define CVAL(p) (*(p++)) |
52 | | - | #ifdef NEED_ALIGN |
53 | | - | #ifdef L_ENDIAN |
54 | | - | #define CVAL2(p, v) { v = (*(p++)); v |= (*(p++)) << 8; } |
55 | | - | #else |
56 | | - | #define CVAL2(p, v) { v = (*(p++)) << 8; v |= (*(p++)); } |
57 | | - | #endif /* L_ENDIAN */ |
58 | | - | #else |
59 | | - | #define CVAL2(p, v) { v = (*((uint16*)p)); p += 2; } |
60 | | - | #endif /* NEED_ALIGN */ |
61 | | - | |
62 | | - | #define UNROLL8(exp) { exp exp exp exp exp exp exp exp } |
63 | | - | |
64 | | - | #define REPEAT(statement) \ |
65 | | - | { \ |
66 | | - | while((count & ~0x7) && ((x+8) < width)) \ |
67 | | - | UNROLL8( statement; count--; x++; ); \ |
68 | | - | \ |
69 | | - | while((count > 0) && (x < width)) \ |
70 | | - | { \ |
71 | | - | statement; \ |
72 | | - | count--; \ |
73 | | - | x++; \ |
74 | | - | } \ |
75 | | - | } |
76 | | - | |
77 | | - | #define MASK_UPDATE() \ |
78 | | - | { \ |
79 | | - | mixmask <<= 1; \ |
80 | | - | if (mixmask == 0) \ |
81 | | - | { \ |
82 | | - | mask = fom_mask ? fom_mask : CVAL(input); \ |
83 | | - | mixmask = 1; \ |
84 | | - | } \ |
85 | | - | } |
86 | | - | |
87 | | - | /* 1 byte bitmap decompress */ |
88 | | - | static RD_BOOL |
89 | | - | bitmap_decompress1(uint8 * output, int width, int height, uint8 * input, int size) |
90 | | - | { |
91 | | - | uint8 *end = input + size; |
92 | | - | uint8 *prevline = NULL, *line = NULL; |
93 | | - | int opcode, count, offset, isfillormix, x = width; |
94 | | - | int lastopcode = -1, insertmix = False, bicolour = False; |
95 | | - | uint8 code; |
96 | | - | uint8 colour1 = 0, colour2 = 0; |
97 | | - | uint8 mixmask, mask = 0; |
98 | | - | uint8 mix = 0xff; |
99 | | - | int fom_mask = 0; |
100 | | - | |
101 | | - | while (input < end) |
102 | | - | { |
103 | | - | fom_mask = 0; |
104 | | - | code = CVAL(input); |
105 | | - | opcode = code >> 4; |
106 | | - | /* Handle different opcode forms */ |
107 | | - | switch (opcode) |
108 | | - | { |
109 | | - | case 0xc: |
110 | | - | case 0xd: |
111 | | - | case 0xe: |
112 | | - | opcode -= 6; |
113 | | - | count = code & 0xf; |
114 | | - | offset = 16; |
115 | | - | break; |
116 | | - | case 0xf: |
117 | | - | opcode = code & 0xf; |
118 | | - | if (opcode < 9) |
119 | | - | { |
120 | | - | count = CVAL(input); |
121 | | - | count |= CVAL(input) << 8; |
122 | | - | } |
123 | | - | else |
124 | | - | { |
125 | | - | count = (opcode < 0xb) ? 8 : 1; |
126 | | - | } |
127 | | - | offset = 0; |
128 | | - | break; |
129 | | - | default: |
130 | | - | opcode >>= 1; |
131 | | - | count = code & 0x1f; |
132 | | - | offset = 32; |
133 | | - | break; |
134 | | - | } |
135 | | - | /* Handle strange cases for counts */ |
136 | | - | if (offset != 0) |
137 | | - | { |
138 | | - | isfillormix = ((opcode == 2) || (opcode == 7)); |
139 | | - | if (count == 0) |
140 | | - | { |
141 | | - | if (isfillormix) |
142 | | - | count = CVAL(input) + 1; |
143 | | - | else |
144 | | - | count = CVAL(input) + offset; |
145 | | - | } |
146 | | - | else if (isfillormix) |
147 | | - | { |
148 | | - | count <<= 3; |
149 | | - | } |
150 | | - | } |
151 | | - | /* Read preliminary data */ |
152 | | - | switch (opcode) |
153 | | - | { |
154 | | - | case 0: /* Fill */ |
155 | | - | if ((lastopcode == opcode) && !((x == width) && (prevline == NULL))) |
156 | | - | insertmix = True; |
157 | | - | break; |
158 | | - | case 8: /* Bicolour */ |
159 | | - | colour1 = CVAL(input); |
160 | | - | case 3: /* Colour */ |
161 | | - | colour2 = CVAL(input); |
162 | | - | break; |
163 | | - | case 6: /* SetMix/Mix */ |
164 | | - | case 7: /* SetMix/FillOrMix */ |
165 | | - | mix = CVAL(input); |
166 | | - | opcode -= 5; |
167 | | - | break; |
168 | | - | case 9: /* FillOrMix_1 */ |
169 | | - | mask = 0x03; |
170 | | - | opcode = 0x02; |
171 | | - | fom_mask = 3; |
172 | | - | break; |
173 | | - | case 0x0a: /* FillOrMix_2 */ |
174 | | - | mask = 0x05; |
175 | | - | opcode = 0x02; |
176 | | - | fom_mask = 5; |
177 | | - | break; |
178 | | - | } |
179 | | - | lastopcode = opcode; |
180 | | - | mixmask = 0; |
181 | | - | /* Output body */ |
182 | | - | while (count > 0) |
183 | | - | { |
184 | | - | if (x >= width) |
185 | | - | { |
186 | | - | if (height <= 0) |
187 | | - | return False; |
188 | | - | x = 0; |
189 | | - | height--; |
190 | | - | prevline = line; |
191 | | - | line = output + height * width; |
192 | | - | } |
193 | | - | switch (opcode) |
194 | | - | { |
195 | | - | case 0: /* Fill */ |
196 | | - | if (insertmix) |
197 | | - | { |
198 | | - | if (prevline == NULL) |
199 | | - | line[x] = mix; |
200 | | - | else |
201 | | - | line[x] = prevline[x] ^ mix; |
202 | | - | insertmix = False; |
203 | | - | count--; |
204 | | - | x++; |
205 | | - | } |
206 | | - | if (prevline == NULL) |
207 | | - | { |
208 | | - | REPEAT(line[x] = 0) |
209 | | - | } |
210 | | - | else |
211 | | - | { |
212 | | - | REPEAT(line[x] = prevline[x]) |
213 | | - | } |
214 | | - | break; |
215 | | - | case 1: /* Mix */ |
216 | | - | if (prevline == NULL) |
217 | | - | { |
218 | | - | REPEAT(line[x] = mix) |
219 | | - | } |
220 | | - | else |
221 | | - | { |
222 | | - | REPEAT(line[x] = prevline[x] ^ mix) |
223 | | - | } |
224 | | - | break; |
225 | | - | case 2: /* Fill or Mix */ |
226 | | - | if (prevline == NULL) |
227 | | - | { |
228 | | - | REPEAT |
229 | | - | ( |
230 | | - | MASK_UPDATE(); |
231 | | - | if (mask & mixmask) |
232 | | - | line[x] = mix; |
233 | | - | else |
234 | | - | line[x] = 0; |
235 | | - | ) |
236 | | - | } |
237 | | - | else |
238 | | - | { |
239 | | - | REPEAT |
240 | | - | ( |
241 | | - | MASK_UPDATE(); |
242 | | - | if (mask & mixmask) |
243 | | - | line[x] = prevline[x] ^ mix; |
244 | | - | else |
245 | | - | line[x] = prevline[x]; |
246 | | - | ) |
247 | | - | } |
248 | | - | break; |
249 | | - | case 3: /* Colour */ |
250 | | - | REPEAT(line[x] = colour2) |
251 | | - | break; |
252 | | - | case 4: /* Copy */ |
253 | | - | REPEAT(line[x] = CVAL(input)) |
254 | | - | break; |
255 | | - | case 8: /* Bicolour */ |
256 | | - | REPEAT |
257 | | - | ( |
258 | | - | if (bicolour) |
259 | | - | { |
260 | | - | line[x] = colour2; |
261 | | - | bicolour = False; |
262 | | - | } |
263 | | - | else |
264 | | - | { |
265 | | - | line[x] = colour1; |
266 | | - | bicolour = True; count++; |
267 | | - | } |
268 | | - | ) |
269 | | - | break; |
270 | | - | case 0xd: /* White */ |
271 | | - | REPEAT(line[x] = 0xff) |
272 | | - | break; |
273 | | - | case 0xe: /* Black */ |
274 | | - | REPEAT(line[x] = 0) |
275 | | - | break; |
276 | | - | default: |
277 | | - | unimpl("bitmap opcode 0x%x\n", opcode); |
278 | | - | return False; |
279 | | - | } |
280 | | - | } |
281 | | - | } |
282 | | - | return True; |
283 | | - | } |
284 | | - | |
285 | | - | /* 2 byte bitmap decompress */ |
286 | | - | static RD_BOOL |
287 | | - | bitmap_decompress2(uint8 * output, int width, int height, uint8 * input, int size) |
288 | | - | { |
289 | | - | uint8 *end = input + size; |
290 | | - | uint16 *prevline = NULL, *line = NULL; |
291 | | - | int opcode, count, offset, isfillormix, x = width; |
292 | | - | int lastopcode = -1, insertmix = False, bicolour = False; |
293 | | - | uint8 code; |
294 | | - | uint16 colour1 = 0, colour2 = 0; |
295 | | - | uint8 mixmask, mask = 0; |
296 | | - | uint16 mix = 0xffff; |
297 | | - | int fom_mask = 0; |
298 | | - | |
299 | | - | while (input < end) |
300 | | - | { |
301 | | - | fom_mask = 0; |
302 | | - | code = CVAL(input); |
303 | | - | opcode = code >> 4; |
304 | | - | /* Handle different opcode forms */ |
305 | | - | switch (opcode) |
306 | | - | { |
307 | | - | case 0xc: |
308 | | - | case 0xd: |
309 | | - | case 0xe: |
310 | | - | opcode -= 6; |
311 | | - | count = code & 0xf; |
312 | | - | offset = 16; |
313 | | - | break; |
314 | | - | case 0xf: |
315 | | - | opcode = code & 0xf; |
316 | | - | if (opcode < 9) |
317 | | - | { |
318 | | - | count = CVAL(input); |
319 | | - | count |= CVAL(input) << 8; |
320 | | - | } |
321 | | - | else |
322 | | - | { |
323 | | - | count = (opcode < 0xb) ? 8 : 1; |
324 | | - | } |
325 | | - | offset = 0; |
326 | | - | break; |
327 | | - | default: |
328 | | - | opcode >>= 1; |
329 | | - | count = code & 0x1f; |
330 | | - | offset = 32; |
331 | | - | break; |
332 | | - | } |
333 | | - | /* Handle strange cases for counts */ |
334 | | - | if (offset != 0) |
335 | | - | { |
336 | | - | isfillormix = ((opcode == 2) || (opcode == 7)); |
337 | | - | if (count == 0) |
338 | | - | { |
339 | | - | if (isfillormix) |
340 | | - | count = CVAL(input) + 1; |
341 | | - | else |
342 | | - | count = CVAL(input) + offset; |
343 | | - | } |
344 | | - | else if (isfillormix) |
345 | | - | { |
346 | | - | count <<= 3; |
347 | | - | } |
348 | | - | } |
349 | | - | /* Read preliminary data */ |
350 | | - | switch (opcode) |
351 | | - | { |
352 | | - | case 0: /* Fill */ |
353 | | - | if ((lastopcode == opcode) && !((x == width) && (prevline == NULL))) |
354 | | - | insertmix = True; |
355 | | - | break; |
356 | | - | case 8: /* Bicolour */ |
357 | | - | CVAL2(input, colour1); |
358 | | - | case 3: /* Colour */ |
359 | | - | CVAL2(input, colour2); |
360 | | - | break; |
361 | | - | case 6: /* SetMix/Mix */ |
362 | | - | case 7: /* SetMix/FillOrMix */ |
363 | | - | CVAL2(input, mix); |
364 | | - | opcode -= 5; |
365 | | - | break; |
366 | | - | case 9: /* FillOrMix_1 */ |
367 | | - | mask = 0x03; |
368 | | - | opcode = 0x02; |
369 | | - | fom_mask = 3; |
370 | | - | break; |
371 | | - | case 0x0a: /* FillOrMix_2 */ |
372 | | - | mask = 0x05; |
373 | | - | opcode = 0x02; |
374 | | - | fom_mask = 5; |
375 | | - | break; |
376 | | - | } |
377 | | - | lastopcode = opcode; |
378 | | - | mixmask = 0; |
379 | | - | /* Output body */ |
380 | | - | while (count > 0) |
381 | | - | { |
382 | | - | if (x >= width) |
383 | | - | { |
384 | | - | if (height <= 0) |
385 | | - | return False; |
386 | | - | x = 0; |
387 | | - | height--; |
388 | | - | prevline = line; |
389 | | - | line = ((uint16 *) output) + height * width; |
390 | | - | } |
391 | | - | switch (opcode) |
392 | | - | { |
393 | | - | case 0: /* Fill */ |
394 | | - | if (insertmix) |
395 | | - | { |
396 | | - | if (prevline == NULL) |
397 | | - | line[x] = mix; |
398 | | - | else |
399 | | - | line[x] = prevline[x] ^ mix; |
400 | | - | insertmix = False; |
401 | | - | count--; |
402 | | - | x++; |
403 | | - | } |
404 | | - | if (prevline == NULL) |
405 | | - | { |
406 | | - | REPEAT(line[x] = 0) |
407 | | - | } |
408 | | - | else |
409 | | - | { |
410 | | - | REPEAT(line[x] = prevline[x]) |
411 | | - | } |
412 | | - | break; |
413 | | - | case 1: /* Mix */ |
414 | | - | if (prevline == NULL) |
415 | | - | { |
416 | | - | REPEAT(line[x] = mix) |
417 | | - | } |
418 | | - | else |
419 | | - | { |
420 | | - | REPEAT(line[x] = prevline[x] ^ mix) |
421 | | - | } |
422 | | - | break; |
423 | | - | case 2: /* Fill or Mix */ |
424 | | - | if (prevline == NULL) |
425 | | - | { |
426 | | - | REPEAT |
427 | | - | ( |
428 | | - | MASK_UPDATE(); |
429 | | - | if (mask & mixmask) |
430 | | - | line[x] = mix; |
431 | | - | else |
432 | | - | line[x] = 0; |
433 | | - | ) |
434 | | - | } |
435 | | - | else |
436 | | - | { |
437 | | - | REPEAT |
438 | | - | ( |
439 | | - | MASK_UPDATE(); |
440 | | - | if (mask & mixmask) |
441 | | - | line[x] = prevline[x] ^ mix; |
442 | | - | else |
443 | | - | line[x] = prevline[x]; |
444 | | - | ) |
445 | | - | } |
446 | | - | break; |
447 | | - | case 3: /* Colour */ |
448 | | - | REPEAT(line[x] = colour2) |
449 | | - | break; |
450 | | - | case 4: /* Copy */ |
451 | | - | REPEAT(CVAL2(input, line[x])) |
452 | | - | break; |
453 | | - | case 8: /* Bicolour */ |
454 | | - | REPEAT |
455 | | - | ( |
456 | | - | if (bicolour) |
457 | | - | { |
458 | | - | line[x] = colour2; |
459 | | - | bicolour = False; |
460 | | - | } |
461 | | - | else |
462 | | - | { |
463 | | - | line[x] = colour1; |
464 | | - | bicolour = True; |
465 | | - | count++; |
466 | | - | } |
467 | | - | ) |
468 | | - | break; |
469 | | - | case 0xd: /* White */ |
470 | | - | REPEAT(line[x] = 0xffff) |
471 | | - | break; |
472 | | - | case 0xe: /* Black */ |
473 | | - | REPEAT(line[x] = 0) |
474 | | - | break; |
475 | | - | default: |
476 | | - | unimpl("bitmap opcode 0x%x\n", opcode); |
477 | | - | return False; |
478 | | - | } |
479 | | - | } |
480 | | - | } |
481 | | - | return True; |
482 | | - | } |
483 | | - | |
484 | | - | /* 3 byte bitmap decompress */ |
485 | | - | static RD_BOOL |
486 | | - | bitmap_decompress3(uint8 * output, int width, int height, uint8 * input, int size) |
487 | | - | { |
488 | | - | uint8 *end = input + size; |
489 | | - | uint8 *prevline = NULL, *line = NULL; |
490 | | - | int opcode, count, offset, isfillormix, x = width; |
491 | | - | int lastopcode = -1, insertmix = False, bicolour = False; |
492 | | - | uint8 code; |
493 | | - | uint8 colour1[3] = {0, 0, 0}, colour2[3] = {0, 0, 0}; |
494 | | - | uint8 mixmask, mask = 0; |
495 | | - | uint8 mix[3] = {0xff, 0xff, 0xff}; |
496 | | - | int fom_mask = 0; |
497 | | - | |
498 | | - | while (input < end) |
499 | | - | { |
500 | | - | fom_mask = 0; |
501 | | - | code = CVAL(input); |
502 | | - | opcode = code >> 4; |
503 | | - | /* Handle different opcode forms */ |
504 | | - | switch (opcode) |
505 | | - | { |
506 | | - | case 0xc: |
507 | | - | case 0xd: |
508 | | - | case 0xe: |
509 | | - | opcode -= 6; |
510 | | - | count = code & 0xf; |
511 | | - | offset = 16; |
512 | | - | break; |
513 | | - | case 0xf: |
514 | | - | opcode = code & 0xf; |
515 | | - | if (opcode < 9) |
516 | | - | { |
517 | | - | count = CVAL(input); |
518 | | - | count |= CVAL(input) << 8; |
519 | | - | } |
520 | | - | else |
521 | | - | { |
522 | | - | count = (opcode < |
523 | | - | 0xb) ? 8 : 1; |
524 | | - | } |
525 | | - | offset = 0; |
526 | | - | break; |
527 | | - | default: |
528 | | - | opcode >>= 1; |
529 | | - | count = code & 0x1f; |
530 | | - | offset = 32; |
531 | | - | break; |
532 | | - | } |
533 | | - | /* Handle strange cases for counts */ |
534 | | - | if (offset != 0) |
535 | | - | { |
536 | | - | isfillormix = ((opcode == 2) || (opcode == 7)); |
537 | | - | if (count == 0) |
538 | | - | { |
539 | | - | if (isfillormix) |
540 | | - | count = CVAL(input) + 1; |
541 | | - | else |
542 | | - | count = CVAL(input) + offset; |
543 | | - | } |
544 | | - | else if (isfillormix) |
545 | | - | { |
546 | | - | count <<= 3; |
547 | | - | } |
548 | | - | } |
549 | | - | /* Read preliminary data */ |
550 | | - | switch (opcode) |
551 | | - | { |
552 | | - | case 0: /* Fill */ |
553 | | - | if ((lastopcode == opcode) && !((x == width) && (prevline == NULL))) |
554 | | - | insertmix = True; |
555 | | - | break; |
556 | | - | case 8: /* Bicolour */ |
557 | | - | colour1[0] = CVAL(input); |
558 | | - | colour1[1] = CVAL(input); |
559 | | - | colour1[2] = CVAL(input); |
560 | | - | case 3: /* Colour */ |
561 | | - | colour2[0] = CVAL(input); |
562 | | - | colour2[1] = CVAL(input); |
563 | | - | colour2[2] = CVAL(input); |
564 | | - | break; |
565 | | - | case 6: /* SetMix/Mix */ |
566 | | - | case 7: /* SetMix/FillOrMix */ |
567 | | - | mix[0] = CVAL(input); |
568 | | - | mix[1] = CVAL(input); |
569 | | - | mix[2] = CVAL(input); |
570 | | - | opcode -= 5; |
571 | | - | break; |
572 | | - | case 9: /* FillOrMix_1 */ |
573 | | - | mask = 0x03; |
574 | | - | opcode = 0x02; |
575 | | - | fom_mask = 3; |
576 | | - | break; |
577 | | - | case 0x0a: /* FillOrMix_2 */ |
578 | | - | mask = 0x05; |
579 | | - | opcode = 0x02; |
580 | | - | fom_mask = 5; |
581 | | - | break; |
582 | | - | } |
583 | | - | lastopcode = opcode; |
584 | | - | mixmask = 0; |
585 | | - | /* Output body */ |
586 | | - | while (count > 0) |
587 | | - | { |
588 | | - | if (x >= width) |
589 | | - | { |
590 | | - | if (height <= 0) |
591 | | - | return False; |
592 | | - | x = 0; |
593 | | - | height--; |
594 | | - | prevline = line; |
595 | | - | line = output + height * (width * 3); |
596 | | - | } |
597 | | - | switch (opcode) |
598 | | - | { |
599 | | - | case 0: /* Fill */ |
600 | | - | if (insertmix) |
601 | | - | { |
602 | | - | if (prevline == NULL) |
603 | | - | { |
604 | | - | line[x * 3] = mix[0]; |
605 | | - | line[x * 3 + 1] = mix[1]; |
606 | | - | line[x * 3 + 2] = mix[2]; |
607 | | - | } |
608 | | - | else |
609 | | - | { |
610 | | - | line[x * 3] = |
611 | | - | prevline[x * 3] ^ mix[0]; |
612 | | - | line[x * 3 + 1] = |
613 | | - | prevline[x * 3 + 1] ^ mix[1]; |
614 | | - | line[x * 3 + 2] = |
615 | | - | prevline[x * 3 + 2] ^ mix[2]; |
616 | | - | } |
617 | | - | insertmix = False; |
618 | | - | count--; |
619 | | - | x++; |
620 | | - | } |
621 | | - | if (prevline == NULL) |
622 | | - | { |
623 | | - | REPEAT |
624 | | - | ( |
625 | | - | line[x * 3] = 0; |
626 | | - | line[x * 3 + 1] = 0; |
627 | | - | line[x * 3 + 2] = 0; |
628 | | - | ) |
629 | | - | } |
630 | | - | else |
631 | | - | { |
632 | | - | REPEAT |
633 | | - | ( |
634 | | - | line[x * 3] = prevline[x * 3]; |
635 | | - | line[x * 3 + 1] = prevline[x * 3 + 1]; |
636 | | - | line[x * 3 + 2] = prevline[x * 3 + 2]; |
637 | | - | ) |
638 | | - | } |
639 | | - | break; |
640 | | - | case 1: /* Mix */ |
641 | | - | if (prevline == NULL) |
642 | | - | { |
643 | | - | REPEAT |
644 | | - | ( |
645 | | - | line[x * 3] = mix[0]; |
646 | | - | line[x * 3 + 1] = mix[1]; |
647 | | - | line[x * 3 + 2] = mix[2]; |
648 | | - | ) |
649 | | - | } |
650 | | - | else |
651 | | - | { |
652 | | - | REPEAT |
653 | | - | ( |
654 | | - | line[x * 3] = |
655 | | - | prevline[x * 3] ^ mix[0]; |
656 | | - | line[x * 3 + 1] = |
657 | | - | prevline[x * 3 + 1] ^ mix[1]; |
658 | | - | line[x * 3 + 2] = |
659 | | - | prevline[x * 3 + 2] ^ mix[2]; |
660 | | - | ) |
661 | | - | } |
662 | | - | break; |
663 | | - | case 2: /* Fill or Mix */ |
664 | | - | if (prevline == NULL) |
665 | | - | { |
666 | | - | REPEAT |
667 | | - | ( |
668 | | - | MASK_UPDATE(); |
669 | | - | if (mask & mixmask) |
670 | | - | { |
671 | | - | line[x * 3] = mix[0]; |
672 | | - | line[x * 3 + 1] = mix[1]; |
673 | | - | line[x * 3 + 2] = mix[2]; |
674 | | - | } |
675 | | - | else |
676 | | - | { |
677 | | - | line[x * 3] = 0; |
678 | | - | line[x * 3 + 1] = 0; |
679 | | - | line[x * 3 + 2] = 0; |
680 | | - | } |
681 | | - | ) |
682 | | - | } |
683 | | - | else |
684 | | - | { |
685 | | - | REPEAT |
686 | | - | ( |
687 | | - | MASK_UPDATE(); |
688 | | - | if (mask & mixmask) |
689 | | - | { |
690 | | - | line[x * 3] = |
691 | | - | prevline[x * 3] ^ mix [0]; |
692 | | - | line[x * 3 + 1] = |
693 | | - | prevline[x * 3 + 1] ^ mix [1]; |
694 | | - | line[x * 3 + 2] = |
695 | | - | prevline[x * 3 + 2] ^ mix [2]; |
696 | | - | } |
697 | | - | else |
698 | | - | { |
699 | | - | line[x * 3] = |
700 | | - | prevline[x * 3]; |
701 | | - | line[x * 3 + 1] = |
702 | | - | prevline[x * 3 + 1]; |
703 | | - | line[x * 3 + 2] = |
704 | | - | prevline[x * 3 + 2]; |
705 | | - | } |
706 | | - | ) |
707 | | - | } |
708 | | - | break; |
709 | | - | case 3: /* Colour */ |
710 | | - | REPEAT |
711 | | - | ( |
712 | | - | line[x * 3] = colour2 [0]; |
713 | | - | line[x * 3 + 1] = colour2 [1]; |
714 | | - | line[x * 3 + 2] = colour2 [2]; |
715 | | - | ) |
716 | | - | break; |
717 | | - | case 4: /* Copy */ |
718 | | - | REPEAT |
719 | | - | ( |
720 | | - | line[x * 3] = CVAL(input); |
721 | | - | line[x * 3 + 1] = CVAL(input); |
722 | | - | line[x * 3 + 2] = CVAL(input); |
723 | | - | ) |
724 | | - | break; |
725 | | - | case 8: /* Bicolour */ |
726 | | - | REPEAT |
727 | | - | ( |
728 | | - | if (bicolour) |
729 | | - | { |
730 | | - | line[x * 3] = colour2[0]; |
731 | | - | line[x * 3 + 1] = colour2[1]; |
732 | | - | line[x * 3 + 2] = colour2[2]; |
733 | | - | bicolour = False; |
734 | | - | } |
735 | | - | else |
736 | | - | { |
737 | | - | line[x * 3] = colour1[0]; |
738 | | - | line[x * 3 + 1] = colour1[1]; |
739 | | - | line[x * 3 + 2] = colour1[2]; |
740 | | - | bicolour = True; |
741 | | - | count++; |
742 | | - | } |
743 | | - | ) |
744 | | - | break; |
745 | | - | case 0xd: /* White */ |
746 | | - | REPEAT |
747 | | - | ( |
748 | | - | line[x * 3] = 0xff; |
749 | | - | line[x * 3 + 1] = 0xff; |
750 | | - | line[x * 3 + 2] = 0xff; |
751 | | - | ) |
752 | | - | break; |
753 | | - | case 0xe: /* Black */ |
754 | | - | REPEAT |
755 | | - | ( |
756 | | - | line[x * 3] = 0; |
757 | | - | line[x * 3 + 1] = 0; |
758 | | - | line[x * 3 + 2] = 0; |
759 | | - | ) |
760 | | - | break; |
761 | | - | default: |
762 | | - | unimpl("bitmap opcode 0x%x\n", opcode); |
763 | | - | return False; |
764 | | - | } |
765 | | - | } |
766 | | - | } |
767 | | - | return True; |
768 | | - | } |
769 | | - | |
770 | | - | /* decompress a colour plane */ |
771 | | - | static int |
772 | | - | process_plane(uint8 * in, int width, int height, uint8 * out, int size) |
773 | | - | { |
774 | | - | int indexw; |
775 | | - | int indexh; |
776 | | - | int code; |
777 | | - | int collen; |
778 | | - | int replen; |
779 | | - | int color; |
780 | | - | int x; |
781 | | - | int revcode; |
782 | | - | uint8 * last_line; |
783 | | - | uint8 * this_line; |
784 | | - | uint8 * org_in; |
785 | | - | uint8 * org_out; |
786 | | - | |
787 | | - | org_in = in; |
788 | | - | org_out = out; |
789 | | - | last_line = 0; |
790 | | - | indexh = 0; |
791 | | - | while (indexh < height) |
792 | | - | { |
793 | | - | out = (org_out + width * height * 4) - ((indexh + 1) * width * 4); |
794 | | - | color = 0; |
795 | | - | this_line = out; |
796 | | - | indexw = 0; |
797 | | - | if (last_line == 0) |
798 | | - | { |
799 | | - | while (indexw < width) |
800 | | - | { |
801 | | - | code = CVAL(in); |
802 | | - | replen = code & 0xf; |
803 | | - | collen = (code >> 4) & 0xf; |
804 | | - | revcode = (replen << 4) | collen; |
805 | | - | if ((revcode <= 47) && (revcode >= 16)) |
806 | | - | { |
807 | | - | replen = revcode; |
808 | | - | collen = 0; |
809 | | - | } |
810 | | - | while (collen > 0) |
811 | | - | { |
812 | | - | color = CVAL(in); |
813 | | - | *out = color; |
814 | | - | out += 4; |
815 | | - | indexw++; |
816 | | - | collen--; |
817 | | - | } |
818 | | - | while (replen > 0) |
819 | | - | { |
820 | | - | *out = color; |
821 | | - | out += 4; |
822 | | - | indexw++; |
823 | | - | replen--; |
824 | | - | } |
825 | | - | } |
826 | | - | } |
827 | | - | else |
828 | | - | { |
829 | | - | while (indexw < width) |
830 | | - | { |
831 | | - | code = CVAL(in); |
832 | | - | replen = code & 0xf; |
833 | | - | collen = (code >> 4) & 0xf; |
834 | | - | revcode = (replen << 4) | collen; |
835 | | - | if ((revcode <= 47) && (revcode >= 16)) |
836 | | - | { |
837 | | - | replen = revcode; |
838 | | - | collen = 0; |
839 | | - | } |
840 | | - | while (collen > 0) |
841 | | - | { |
842 | | - | x = CVAL(in); |
843 | | - | if (x & 1) |
844 | | - | { |
845 | | - | x = x >> 1; |
846 | | - | x = x + 1; |
847 | | - | color = -x; |
848 | | - | } |
849 | | - | else |
850 | | - | { |
851 | | - | x = x >> 1; |
852 | | - | color = x; |
853 | | - | } |
854 | | - | x = last_line[indexw * 4] + color; |
855 | | - | *out = x; |
856 | | - | out += 4; |
857 | | - | indexw++; |
858 | | - | collen--; |
859 | | - | } |
860 | | - | while (replen > 0) |
861 | | - | { |
862 | | - | x = last_line[indexw * 4] + color; |
863 | | - | *out = x; |
864 | | - | out += 4; |
865 | | - | indexw++; |
866 | | - | replen--; |
867 | | - | } |
868 | | - | } |
869 | | - | } |
870 | | - | indexh++; |
871 | | - | last_line = this_line; |
872 | | - | } |
873 | | - | return (int) (in - org_in); |
874 | | - | } |
875 | | - | |
876 | | - | /* 4 byte bitmap decompress */ |
877 | | - | static RD_BOOL |
878 | | - | bitmap_decompress4(uint8 * output, int width, int height, uint8 * input, int size) |
879 | | - | { |
880 | | - | int code; |
881 | | - | int bytes_pro; |
882 | | - | int total_pro; |
883 | | - | |
884 | | - | code = CVAL(input); |
885 | | - | if (code != 0x10) |
886 | | - | { |
887 | | - | return False; |
888 | | - | } |
889 | | - | total_pro = 1; |
890 | | - | bytes_pro = process_plane(input, width, height, output + 3, size - total_pro); |
891 | | - | total_pro += bytes_pro; |
892 | | - | input += bytes_pro; |
893 | | - | bytes_pro = process_plane(input, width, height, output + 2, size - total_pro); |
894 | | - | total_pro += bytes_pro; |
895 | | - | input += bytes_pro; |
896 | | - | bytes_pro = process_plane(input, width, height, output + 1, size - total_pro); |
897 | | - | total_pro += bytes_pro; |
898 | | - | input += bytes_pro; |
899 | | - | bytes_pro = process_plane(input, width, height, output + 0, size - total_pro); |
900 | | - | total_pro += bytes_pro; |
901 | | - | return size == total_pro; |
902 | | - | } |
903 | | - | |
904 | | - | /* main decompress function */ |
905 | | - | static int |
906 | | - | bitmap_decompress(uint8 * output, int width, int height, uint8* input, int size, int Bpp) |
907 | | - | { |
908 | | - | RD_BOOL rv = False; |
909 | | - | |
910 | | - | switch (Bpp) |
911 | | - | { |
912 | | - | case 1: |
913 | | - | rv = bitmap_decompress1(output, width, height, input, size); |
914 | | - | break; |
915 | | - | case 2: |
916 | | - | rv = bitmap_decompress2(output, width, height, input, size); |
917 | | - | break; |
918 | | - | case 3: |
919 | | - | rv = bitmap_decompress3(output, width, height, input, size); |
920 | | - | break; |
921 | | - | case 4: |
922 | | - | rv = bitmap_decompress4(output, width, height, input, size); |
923 | | - | break; |
924 | | - | default: |
925 | | - | unimpl("Bpp %d\n", Bpp); |
926 | | - | break; |
927 | | - | } |
928 | | - | return rv; |
929 | | - | } |
930 | | - | |
931 | | - | static void |
932 | | - | convert_rgb555_rgb32(uint8 *decomp_buff, int decomp_buff_size, uint8 *dst, int dst_size){ |
933 | | - | int j = 0; |
934 | | - | for(int i =0; i< decomp_buff_size; i=i+2){ |
935 | | - | int t = (decomp_buff[i+1] << 8) + decomp_buff[i]; |
936 | | - | dst[j] = t >> 8; |
937 | | - | dst[j+1] = ((t >> 5) & 0b11111) << 3; |
938 | | - | dst[j+2] = (t & 0x1F) << 3; |
939 | | - | dst[j+3] = 0xff; |
940 | | - | j = j+4; |
941 | | - | } |
942 | | - | } |
943 | | - | |
944 | | - | static void |
945 | | - | convert_rgb565_rgb32(uint8 *decomp_buff, int decomp_buff_size, uint8 *dst, int dst_size){ |
946 | | - | int j = 0; |
947 | | - | for(int i =0; i< decomp_buff_size; i=i+2){ |
948 | | - | int t = (decomp_buff[i+1] << 8) + decomp_buff[i]; |
949 | | - | dst[j] = t >> 8; |
950 | | - | dst[j+1] = ((t >> 5) & 0b111111) << 2; |
951 | | - | dst[j+2] = (t & 0x1F) << 3; |
952 | | - | dst[j+3] = 0xff; |
953 | | - | j = j+4; |
954 | | - | } |
955 | | - | } |
956 | | - | |
957 | | - | static void |
958 | | - | convert_rgb24_rgb32(uint8 *decomp_buff, int decomp_buff_size, uint8 *dst, int dst_size){ |
959 | | - | int j = 0; |
960 | | - | for(int i =0; i< decomp_buff_size; i=i+3){ |
961 | | - | dst[j] = decomp_buff[i]; |
962 | | - | dst[j+1] = decomp_buff[i+1]; |
963 | | - | dst[j+2] = decomp_buff[i+2]; |
964 | | - | dst[j+3] = 0xff; |
965 | | - | j = j+4; |
966 | | - | } |
967 | | - | } |
968 | | - | |
969 | | - | static void |
970 | | - | convert_rgbx_rgba(uint8 *decomp_buff, int decomp_buff_size, uint8 *dst, int dst_size){ |
971 | | - | for(int i =0; i< decomp_buff_size; i=i+4){ |
972 | | - | dst[i] = decomp_buff[i]; |
973 | | - | dst[i+1] = decomp_buff[i+1]; |
974 | | - | dst[i+2] = decomp_buff[i+2]; |
975 | | - | dst[i+3] = 0xff; |
976 | | - | } |
977 | | - | } |
978 | | - | |
979 | | - | static int |
980 | | - | decode_rre(uint8 *rre_buff, int rre_buff_size, uint8 *dst, int dst_size, uint bypp, uint width, uint height){ |
981 | | - | uint sub_rect_num = (rre_buff[0] << 24) + (rre_buff[1] << 16) + (rre_buff[2] << 8) + rre_buff[3]; |
982 | | - | uint sub_rect_num_bytes = sub_rect_num*12; |
983 | | - | uint rectangle_pixel_count = width*height; |
984 | | - | uint rectangle_size = rectangle_pixel_count*bypp; |
985 | | - | uint subrectangle_pixel_count, subrect_color_offset, subwidth_bytes = 0; |
986 | | - | uint16 subx, suby, subwidth, subheight, substart = 0; |
987 | | - | |
988 | | - | if(rectangle_size > dst_size){ |
989 | | - | return 1; |
990 | | - | } |
991 | | - | |
992 | | - | // filling rectangle with default pattern |
993 | | - | for(uint i = 0; i< rectangle_size; i += bypp){ |
994 | | - | dst[i] = rre_buff[4]; |
995 | | - | dst[i+1] = rre_buff[5]; |
996 | | - | dst[i+2] = rre_buff[6]; |
997 | | - | dst[i+3] = 0xff; // alpha channel |
998 | | - | } |
999 | | - | |
1000 | | - | if((((sub_rect_num*12)+8)) != (uint)rre_buff_size){ |
1001 | | - | printf("sub_rect_num %d\r\n", sub_rect_num); |
1002 | | - | printf("boundary %d\r\n", ((sub_rect_num*12)+8)); |
1003 | | - | printf("rre_buff_size %d\r\n", rre_buff_size); |
1004 | | - | return 1; |
1005 | | - | } |
1006 | | - | |
1007 | | - | for(uint i =0; i < sub_rect_num_bytes; i+=12){ |
1008 | | - | //memo: 12 = 4(RGBX) + subx, suby, subwidth, subheight |
1009 | | - | subx = (rre_buff[8+4+i] << 8) + rre_buff[8+5+i]; |
1010 | | - | suby = (rre_buff[8+6+i] << 8) + rre_buff[8+7+i]; |
1011 | | - | subwidth = (rre_buff[8+8+i] << 8) + rre_buff[8+9+i]; |
1012 | | - | subwidth_bytes = subwidth*4; |
1013 | | - | subheight = (rre_buff[8+10+i] << 8) + rre_buff[8+11+i]; |
1014 | | - | subrectangle_pixel_count = subwidth*subheight; |
1015 | | - | subrect_color_offset = 8+i; // 8 is the start of subrects |
1016 | | - | |
1017 | | - | for(uint j = 0; j< subrectangle_pixel_count; j += 1){ |
1018 | | - | for(uint y = 0; y < subheight; y += 1){ |
1019 | | - | substart = (subx + ((suby+y)*width))*bypp; |
1020 | | - | if(substart > (uint)dst_size || substart + subwidth_bytes > (uint)dst_size){ |
1021 | | - | return 1; |
1022 | | - | } |
1023 | | - | for(uint x = 0; x < subwidth_bytes; x += 4){ |
1024 | | - | dst[substart+x] = rre_buff[subrect_color_offset]; |
1025 | | - | dst[substart+x+1] = rre_buff[subrect_color_offset+1]; |
1026 | | - | dst[substart+x+2] = rre_buff[subrect_color_offset+2]; |
1027 | | - | dst[substart+x+3] = 0xff; // alpha channel |
1028 | | - | } |
1029 | | - | } |
1030 | | - | } |
1031 | | - | } |
1032 | | - | return 0; |
1033 | | - | |
1034 | | - | } |
1035 | | - | |
1036 | | - | /* *INDENT-ON* */ |
1037 | | - | |
1038 | | - | static PyObject* |
1039 | | - | bitmap_decompress_wrapper(PyObject* self, PyObject* args) |
1040 | | - | { |
1041 | | - | Py_buffer output, input; |
1042 | | - | int width = 0, height = 0, bpp = 0, bitsperpixel = 0, isCompressed = 0; |
1043 | | - | int decomp_size = 0; |
1044 | | - | uint8 *decomp_buffer = NULL; |
1045 | | - | |
1046 | | - | if (!PyArg_ParseTuple(args, "s*iis*iii", &output, &width, &height, &input, &bitsperpixel, &bpp, &isCompressed)){ |
1047 | | - | PyErr_SetString(PyExc_TypeError, "Input parameter error"); |
1048 | | - | return (PyObject *) NULL; |
1049 | | - | } |
1050 | | - | |
1051 | | - | if(isCompressed){ |
1052 | | - | decomp_size = width * height * bpp; |
1053 | | - | decomp_buffer = (uint8*)malloc(decomp_size); |
1054 | | - | |
1055 | | - | if(decomp_buffer == NULL){ |
1056 | | - | PyErr_SetString(PyExc_TypeError, "malloc failed!"); |
1057 | | - | return (PyObject *) NULL; |
1058 | | - | } |
1059 | | - | |
1060 | | - | if(bitmap_decompress(decomp_buffer, width, height, (uint8*)input.buf, input.len, bpp) == False){ |
1061 | | - | free(decomp_buffer); |
1062 | | - | PyErr_SetString(PyExc_TypeError, "Decompression error"); |
1063 | | - | return (PyObject *) NULL; |
1064 | | - | } |
1065 | | - | } |
1066 | | - | else{ |
1067 | | - | decomp_buffer = (uint8*)input.buf; |
1068 | | - | decomp_size = input.len; |
1069 | | - | } |
1070 | | - | |
1071 | | - | |
1072 | | - | switch(bitsperpixel){ |
1073 | | - | case 15: |
1074 | | - | convert_rgb555_rgb32(decomp_buffer, decomp_size, (uint8*)output.buf, output.len); |
1075 | | - | break; |
1076 | | - | case 16: |
1077 | | - | convert_rgb565_rgb32(decomp_buffer, decomp_size, (uint8*)output.buf, output.len); |
1078 | | - | break; |
1079 | | - | case 24: |
1080 | | - | convert_rgb24_rgb32(decomp_buffer, decomp_size, (uint8*)output.buf, output.len); |
1081 | | - | break; |
1082 | | - | case 32: |
1083 | | - | break; |
1084 | | - | } |
1085 | | - | if(isCompressed){ |
1086 | | - | free(decomp_buffer); |
1087 | | - | } |
1088 | | - | |
1089 | | - | Py_RETURN_NONE; |
1090 | | - | } |
1091 | | - | |
1092 | | - | static PyObject* |
1093 | | - | mask_rgbx_wrapper(PyObject* self, PyObject* args) |
1094 | | - | { |
1095 | | - | Py_buffer output, input; |
1096 | | - | |
1097 | | - | if (!PyArg_ParseTuple(args, "s*s*", &output, &input)){ |
1098 | | - | PyErr_SetString(PyExc_TypeError, "Input parameter error"); |
1099 | | - | return (PyObject *) NULL; |
1100 | | - | } |
1101 | | - | |
1102 | | - | convert_rgbx_rgba((uint8*)input.buf, input.len, (uint8*)output.buf, output.len); |
1103 | | - | |
1104 | | - | |
1105 | | - | Py_RETURN_NONE; |
1106 | | - | } |
1107 | | - | |
1108 | | - | static PyObject* |
1109 | | - | decode_rre_wrapper(PyObject* self, PyObject* args) |
1110 | | - | { |
1111 | | - | Py_buffer output, input; |
1112 | | - | int width = 0, height = 0, bypp = 0; |
1113 | | - | |
1114 | | - | if (!PyArg_ParseTuple(args, "s*s*iii", &output, &input, &width, &height, &bypp)){ |
1115 | | - | PyErr_SetString(PyExc_TypeError, "Input parameter error"); |
1116 | | - | return (PyObject *) NULL; |
1117 | | - | } |
1118 | | - | |
1119 | | - | if(decode_rre((uint8*)input.buf, input.len, (uint8*)output.buf, output.len, bypp, width, height)){ |
1120 | | - | PyErr_SetString(PyExc_TypeError, "Decode failed!"); |
1121 | | - | return (PyObject *) NULL; |
1122 | | - | } |
1123 | | - | |
1124 | | - | Py_RETURN_NONE; |
1125 | | - | } |
1126 | | - | |
1127 | | - | static PyMethodDef rle_methods[] = |
1128 | | - | { |
1129 | | - | {"bitmap_decompress", bitmap_decompress_wrapper, METH_VARARGS, "decompress bitmap from microsoft rle algorithm."}, |
1130 | | - | {"mask_rgbx", mask_rgbx_wrapper, METH_VARARGS, "Converts RGBX to RGBA"}, |
1131 | | - | {"decode_rre", decode_rre_wrapper, METH_VARARGS, "Converts RRE encoded data to RGBA recatngle"}, |
1132 | | - | {NULL, NULL, 0, NULL} |
1133 | | - | }; |
1134 | | - | |
1135 | | - | static struct PyModuleDef cModPyDem = |
1136 | | - | { |
1137 | | - | PyModuleDef_HEAD_INIT, |
1138 | | - | "rle", /* name of module */ |
1139 | | - | "", /* module documentation, may be NULL */ |
1140 | | - | -1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */ |
1141 | | - | rle_methods |
1142 | | - | }; |
1143 | | - | |
1144 | | - | static volatile int *_dummy_malloc; |
1145 | | - | |
1146 | | - | PyMODINIT_FUNC PyInit_rle(void) |
1147 | | - | { |
1148 | | - | // This is for pyodide for some reason it didn't work without it. |
1149 | | - | _dummy_malloc = (int *)malloc(sizeof(int)); |
1150 | | - | *_dummy_malloc = 1; |
1151 | | - | free((void *)_dummy_malloc); |
1152 | | - | // yup. |
1153 | | - | |
1154 | | - | PyObject *m = PyModule_Create(&cModPyDem); |
1155 | | - | return m; |
1156 | | - | }; |
1157 | | - | |
1158 | | - | |