--- sourceforge.net/trunk/rdesktop/bitmap.c 2003/02/06 14:31:24 313 +++ sourceforge.net/trunk/rdesktop/bitmap.c 2003/02/07 23:43:37 314 @@ -21,7 +21,28 @@ #include "rdesktop.h" #define CVAL(p) (*(p++)) -#define CVAL16(p) (*(((uint16*)p)++)) + +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); +} + +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 } @@ -44,221 +65,16 @@ } BOOL -bitmap_decompress8(unsigned char *output, int width, int height, unsigned char *input, int size) +bitmap_decompress(unsigned char *output, int width, int height, unsigned char *input, int size, int Bpp) { unsigned char *end = input + size; unsigned char *prevline = NULL, *line = NULL; int opcode, count, offset, isfillormix, x = width; int lastopcode = -1, insertmix = False, bicolour = False; - uint8 code, colour1 = 0, colour2 = 0; - uint8 mixmask, mask = 0, mix = 0xff; - int fom_mask = 0; - - while (input < end) - { - fom_mask = 0; - code = CVAL(input); - opcode = code >> 4; - - /* Handle different opcode forms */ - switch (opcode) - { - case 0xc: - case 0xd: - case 0xe: - opcode -= 6; - count = code & 0xf; - offset = 16; - break; - - case 0xf: - opcode = code & 0xf; - if (opcode < 9) - { - count = CVAL(input); - count |= CVAL(input) << 8; - } - else - { - count = (opcode < 0xb) ? 8 : 1; - } - offset = 0; - break; - - default: - opcode >>= 1; - count = code & 0x1f; - offset = 32; - break; - } - - /* Handle strange cases for counts */ - if (offset != 0) - { - isfillormix = ((opcode == 2) || (opcode == 7)); - - if (count == 0) - { - if (isfillormix) - count = CVAL(input) + 1; - else - count = CVAL(input) + offset; - } - else if (isfillormix) - { - count <<= 3; - } - } - - /* Read preliminary data */ - switch (opcode) - { - case 0: /* Fill */ - if ((lastopcode == opcode) && !((x == width) && (prevline == NULL))) - insertmix = True; - break; - case 8: /* Bicolour */ - colour1 = CVAL(input); - case 3: /* Colour */ - colour2 = CVAL(input); - break; - case 6: /* SetMix/Mix */ - case 7: /* SetMix/FillOrMix */ - mix = CVAL(input); - 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 False; - - x = 0; - height--; - - prevline = line; - line = output + height * width; - } - - switch (opcode) - { - case 0: /* Fill */ - if (insertmix) - { - if (prevline == NULL) - line[x] = mix; - else - line[x] = prevline[x] ^ mix; - - insertmix = False; - count--; - x++; - } - - if (prevline == NULL) - { - REPEAT(line[x] = 0); - } - else - { - REPEAT(line[x] = prevline[x]); - } - break; - - case 1: /* Mix */ - if (prevline == NULL) - { - REPEAT(line[x] = mix); - } - else - { - REPEAT(line[x] = prevline[x] ^ mix); - } - break; - - case 2: /* Fill or Mix */ - if (prevline == NULL) - { - REPEAT(MASK_UPDATE(); - if (mask & mixmask) line[x] = mix; - else - line[x] = 0;); - } - else - { - REPEAT(MASK_UPDATE(); - if (mask & mixmask) - line[x] = prevline[x] ^ mix; - else - line[x] = prevline[x];); - } - break; - - case 3: /* Colour */ - REPEAT(line[x] = colour2); - break; - - case 4: /* Copy */ - REPEAT(line[x] = CVAL(input)); - break; - - case 8: /* Bicolour */ - REPEAT(if (bicolour) - { - line[x] = colour2; bicolour = False;} - else - { - line[x] = colour1; bicolour = True; count++;} - ); - break; - - case 0xd: /* White */ - REPEAT(line[x] = 0xff); - break; - - case 0xe: /* Black */ - REPEAT(line[x] = 0x00); - break; - - default: - unimpl("bitmap opcode 0x%x\n", opcode); - return False; - } - } - } - - return True; -} - -BOOL -bitmap_decompress16(unsigned char *output, int width, int height, unsigned char *input, int size) -{ - unsigned char *end = input + size; - uint16 *prevline = NULL, *line = NULL; - int opcode, count, offset, isfillormix, x = width; - int lastopcode = -1, insertmix = False, bicolour = False; uint8 code; - uint16 colour1 = 0, colour2 = 0; + uint32 colour1 = 0, colour2 = 0; uint8 mixmask, mask = 0; - uint16 mix = 0xffff; + uint32 mix = 0xffffffff; int fom_mask = 0; while (input < end) @@ -287,7 +103,7 @@ } else { - count = (opcode < 0xd) ? 8 : 1; // was 0xb in 8 bit + count = (opcode < 0xb) ? 8 : 1; } offset = 0; break; @@ -325,13 +141,13 @@ insertmix = True; break; case 8: /* Bicolour */ - colour1 = CVAL16(input); + colour1 = cvalx(&input, Bpp); case 3: /* Colour */ - colour2 = CVAL16(input); + colour2 = cvalx(&input, Bpp); break; case 6: /* SetMix/Mix */ case 7: /* SetMix/FillOrMix */ - mix = CVAL16(input); + mix = cvalx(&input, Bpp); opcode -= 5; break; case 9: /* FillOrMix_1 */ @@ -362,7 +178,7 @@ height--; prevline = line; - line = (uint16*)output + height * width; + line = output + height * width * Bpp; } switch (opcode) @@ -371,9 +187,9 @@ if (insertmix) { if (prevline == NULL) - line[x] = mix; + setli(line, x, mix, Bpp); else - line[x] = prevline[x] ^ mix; + setli(line, x, getli(prevline, x, Bpp) ^ mix, Bpp); insertmix = False; count--; @@ -382,22 +198,22 @@ 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 */ 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; @@ -405,44 +221,44 @@ if (prevline == NULL) { REPEAT(MASK_UPDATE(); - if (mask & mixmask) line[x] = mix; + if (mask & mixmask) setli(line, x, mix, Bpp); else - line[x] = 0;); + setli(line, x, 0, Bpp);); } else { REPEAT(MASK_UPDATE(); if (mask & mixmask) - line[x] = prevline[x] ^ mix; + setli(line, x, getli(prevline, x, Bpp) ^ mix, Bpp); else - line[x] = prevline[x];); + setli(line, x, getli(prevline, x, Bpp), Bpp);); } break; case 3: /* Colour */ - REPEAT(line[x] = colour2); + REPEAT(setli(line, x, colour2, Bpp)); break; case 4: /* Copy */ - REPEAT(line[x] = CVAL16(input)); + REPEAT(setli(line, x, cvalx(&input, Bpp), Bpp)); break; case 8: /* Bicolour */ REPEAT(if (bicolour) { - line[x] = colour2; bicolour = False;} + setli(line, x, colour2, Bpp); bicolour = False;} else { - line[x] = colour1; bicolour = True; count++;} + setli(line, x, colour1, Bpp); bicolour = True; count++;} ); break; case 0xd: /* White */ - REPEAT(line[x] = 0xffff); + REPEAT(setli(line, x, 0xffffffff, Bpp)); break; case 0xe: /* Black */ - REPEAT(line[x] = 0x00); + REPEAT(setli(line, x, 0, Bpp)); break; default: @@ -455,13 +271,3 @@ return True; } -BOOL -bitmap_decompress(unsigned char *output, int width, int height, unsigned char *input, int size, int bpp) -{ - if (bpp == 8) - return bitmap_decompress8(output, width, height, input, size); - else if (bpp == 16) - return bitmap_decompress16(output, width, height, input, size); - else - return False; -}