--- sourceforge.net/trunk/rdesktop/bitmap.c 2000/07/07 09:40:03 7 +++ sourceforge.net/trunk/rdesktop/bitmap.c 2003/02/10 12:58:51 318 @@ -1,44 +1,89 @@ /* rdesktop: A Remote Desktop Protocol client. Bitmap decompression routines - Copyright (C) Matthew Chapman 1999-2000 - + Copyright (C) Matthew Chapman 1999-2002 + This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. - + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - + You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -#include "includes.h" +#include "rdesktop.h" #define CVAL(p) (*(p++)) -#define SVAL(p) ((*((p++) + 1) << 8) | CVAL(p)) -#define REPEAT(statement) { while ((count > 0) && (x < width)) { statement; count--; x++; } } -#define MASK_UPDATE() { maskpix <<= 1; if (maskpix == 0) { mask = CVAL(input); maskpix = 1; } } +uint32 +cvalx(unsigned char **input, int Bpp) +{ + uint32 rv = 0; + memcpy(&rv, *input, Bpp); + *input += Bpp; + return rv; +} + +void +setli(unsigned char *input, int offset, uint32 value, int Bpp) +{ + input += offset * Bpp; + memcpy(input, &value, Bpp); +} -BOOL bitmap_decompress(unsigned char *output, int width, int height, - unsigned char *input, int size) +uint32 +getli(unsigned char *input, int offset, int Bpp) +{ + uint32 rv = 0; + input += offset * Bpp; + memcpy(&rv, input, Bpp); + return rv; +} + +#define UNROLL8(exp) { exp exp exp exp exp exp exp exp } + +#define REPEAT(statement) \ +{ \ + while((count & ~0x7) && ((x+8) < width)) \ + UNROLL8( statement; count--; x++; ); \ + \ + while((count > 0) && (x < width)) { statement; count--; x++; } \ +} + +#define MASK_UPDATE() \ +{ \ + mixmask <<= 1; \ + if (mixmask == 0) \ + { \ + mask = fom_mask ? fom_mask : CVAL(input); \ + mixmask = 1; \ + } \ +} + +BOOL +bitmap_decompress(unsigned char *output, int width, int height, unsigned char *input, int size, + int Bpp) { unsigned char *end = input + size; - unsigned char *prevline, *line = NULL; + unsigned char *prevline = NULL, *line = NULL; int opcode, count, offset, isfillormix, x = width; - uint8 code, mask, maskpix, color1, color2; - uint8 mix = 0xff; + int lastopcode = -1, insertmix = False, bicolour = False; + uint8 code; + uint32 colour1 = 0, colour2 = 0; + uint8 mixmask, mask = 0; + uint32 mix = 0xffffffff; + int fom_mask = 0; - dump_data(input, end-input); while (input < end) { - fprintf(stderr, "Offset %d from end\n", end-input); + fom_mask = 0; code = CVAL(input); opcode = code >> 4; @@ -55,7 +100,15 @@ case 0xf: opcode = code & 0xf; - count = (opcode < 13) ? SVAL(input) : 1; + if (opcode < 9) + { + count = CVAL(input); + count |= CVAL(input) << 8; + } + else + { + count = (opcode < 0xb) ? 8 : 1; + } offset = 0; break; @@ -85,105 +138,141 @@ } /* Read preliminary data */ - maskpix = 0; switch (opcode) { - case 3: /* Color */ - color1 = CVAL(input); - case 8: /* Bicolor */ - color2 = CVAL(input); - break; - case 6: /* SetMix/Mix */ - case 7: /* SetMix/FillOrMix */ - mix = CVAL(input); + case 0: /* Fill */ + if ((lastopcode == opcode) && !((x == width) && (prevline == NULL))) + insertmix = True; + break; + case 8: /* Bicolour */ + colour1 = cvalx(&input, Bpp); + case 3: /* Colour */ + colour2 = cvalx(&input, Bpp); + break; + case 6: /* SetMix/Mix */ + case 7: /* SetMix/FillOrMix */ + mix = cvalx(&input, Bpp); opcode -= 5; break; + case 9: /* FillOrMix_1 */ + mask = 0x03; + opcode = 0x02; + fom_mask = 3; + break; + case 0x0a: /* FillOrMix_2 */ + mask = 0x05; + opcode = 0x02; + fom_mask = 5; + break; + } + lastopcode = opcode; + mixmask = 0; + /* Output body */ while (count > 0) { if (x >= width) { if (height <= 0) - return True; + return False; x = 0; height--; prevline = line; - line = output + height * width; + line = output + height * width * Bpp; } switch (opcode) { - case 0: /* Fill */ - fprintf(stderr, "Fill %d\n", count); + case 0: /* Fill */ + if (insertmix) + { + if (prevline == NULL) + setli(line, x, mix, Bpp); + else + setli(line, x, + getli(prevline, x, Bpp) ^ mix, Bpp); + + insertmix = False; + count--; + x++; + } + if (prevline == NULL) - REPEAT(line[x] = 0) + { + REPEAT(setli(line, x, 0, Bpp))} else - REPEAT(line[x] = prevline[x]) + { + REPEAT(setli + (line, x, getli(prevline, x, Bpp), Bpp)); + } break; - case 1: /* Mix */ - fprintf(stderr, "Mix %d\n", count); + case 1: /* Mix */ if (prevline == NULL) - REPEAT(line[x] = mix) + { + REPEAT(setli(line, x, mix, Bpp)); + } else - REPEAT(line[x] = prevline[x] ^ mix) + { + REPEAT(setli + (line, x, getli(prevline, x, Bpp) ^ mix, + Bpp)); + } break; -#if 0 - case 2: /* Fill or Mix */ - REPEAT(line[x] = 0); - break; + case 2: /* Fill or Mix */ if (prevline == NULL) - REPEAT( - MASK_UPDATE(); - - if (mask & maskpix) - line[x] = mix; - else - line[x] = 0; - ) + { + REPEAT(MASK_UPDATE(); + if (mask & mixmask) setli(line, x, mix, Bpp); + else + setli(line, x, 0, Bpp);); + } else - REPEAT( - MASK_UPDATE(); - - if (mask & maskpix) - line[x] = prevline[x] ^ mix; - else - line[x] = prevline[x]; - ) + { + REPEAT(MASK_UPDATE(); + if (mask & mixmask) + setli(line, x, getli(prevline, x, Bpp) ^ mix, + Bpp); + else + setli(line, x, getli(prevline, x, Bpp), + Bpp);); + } break; -#endif - case 3: /* Colour */ - fprintf(stderr, "Colour %d\n", count); - REPEAT(line[x] = color2) + case 3: /* Colour */ + REPEAT(setli(line, x, colour2, Bpp)); break; - case 4: /* Copy */ - fprintf(stderr, "Copy %d\n", count); - REPEAT(line[x] = CVAL(input)) + case 4: /* Copy */ + REPEAT(setli(line, x, cvalx(&input, Bpp), Bpp)); break; -#if 0 - case 8: /* Bicolor */ - REPEAT(line[x] = color1; line[++x] = color2) + case 8: /* Bicolour */ + REPEAT(if (bicolour) + { + setli(line, x, colour2, Bpp); bicolour = False;} + else + { + setli(line, x, colour1, Bpp); bicolour = True; + count++;} + ); break; - case 13: /* White */ - REPEAT(line[x] = 0xff) + case 0xd: /* White */ + REPEAT(setli(line, x, 0xffffffff, Bpp)); break; - case 14: /* Black */ - REPEAT(line[x] = 0x00) + case 0xe: /* Black */ + REPEAT(setli(line, x, 0, Bpp)); break; -#endif default: - fprintf(stderr, "Unknown bitmap opcode 0x%x\n", opcode); + unimpl("bitmap opcode 0x%x\n", opcode); return False; } }