/* -*- c-basic-offset: 8 -*- rdesktop: A Remote Desktop Protocol client. Bitmap decompression routines Copyright (C) Matthew Chapman 1999-2008 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 3 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, see . */ /* indent is confused by this file */ /* *INDENT-OFF* */ #include "rdesktop.h" #define CVAL(p) (*(p++)) #ifdef NEED_ALIGN #ifdef L_ENDIAN #define CVAL2(p, v) { v = (*(p++)); v |= (*(p++)) << 8; } #else #define CVAL2(p, v) { v = (*(p++)) << 8; v |= (*(p++)); } #endif /* L_ENDIAN */ #else #define CVAL2(p, v) { v = (*((uint16*)p)); p += 2; } #endif /* NEED_ALIGN */ #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; \ } \ } #define OPCODE_FILL 0 #define OPCODE_MIX 1 #define OPCODE_FILL_OR_MIX 2 #define OPCODE_COLOUR 3 #define OPCODE_COPY 4 #define OPCODE_BICOLOUR 8 #define OPCODE_WHITE 0xd #define OPCODE_BLACK 0xe #define WHITE_PIXEL 0xff #define BLACK_PIXEL 0 static RD_BOOL bitmap_decompress_n(uint8 *output, int width, int height, uint8 *input, int size, int bytes_per_pixel) { uint8 *end = input + size; uint8 *prevline = NULL, *line = NULL; int opcode, count, offset, isfillormix, x = width; int lastopcode = -1, insertmix = False, bicolour = False; uint8 code; uint8 colour1[3] = {0, 0, 0}, colour2[3] = {0, 0, 0}; uint8 mixmask, mask = 0; uint8 mix[3] = {0xff, 0xff, 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 == OPCODE_FILL_OR_MIX) || (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 OPCODE_FILL: if ((lastopcode == opcode) && !((x == width) && (prevline == NULL))) { insertmix = True; } break; case OPCODE_BICOLOUR: for (int i = 0; i < bytes_per_pixel; i++) { colour1[i] = CVAL(input); colour2[i] = CVAL(input); } break; case OPCODE_COLOUR: for (int i = 0; i < bytes_per_pixel; i++) { colour2[i] = CVAL(input); } break; case 6: /* SetMix/Mix */ case 7: /* SetMix/FillOrMix */ for (int i = 0; i < bytes_per_pixel; i++) { mix[i] = CVAL(input); } opcode -= 5; break; case 9: /* FillOrMix_1 */ mask = 0x03; opcode = OPCODE_FILL_OR_MIX; fom_mask = 3; break; case 0x0a: /* FillOrMix_2 */ mask = 0x05; opcode = OPCODE_FILL_OR_MIX; 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 * bytes_per_pixel); } switch (opcode) { case OPCODE_FILL: if (insertmix) { if (prevline == NULL) { for (int i = 0; i < bytes_per_pixel; i++) { line[x * bytes_per_pixel + i] = mix[i]; } } else { for (int i = 0; i < bytes_per_pixel; i++) { line[x * bytes_per_pixel + i] = prevline[x * bytes_per_pixel + i] ^ mix[i]; } } insertmix = False; count--; x++; } if (prevline == NULL) { REPEAT( for (int i = 0; i < bytes_per_pixel; i++) { line[x * bytes_per_pixel + i] = 0; } ) } else { REPEAT( for (int i = 0; i < bytes_per_pixel; i++) { line[x * bytes_per_pixel + i] = prevline[x * bytes_per_pixel + i]; } ) } break; case OPCODE_MIX: if (prevline == NULL) { REPEAT( for (int i = 0; i < bytes_per_pixel; i++) { line[x * bytes_per_pixel + i] = mix[i]; } ) } else { REPEAT( for (int i = 0; i < bytes_per_pixel; i++) { line[x * bytes_per_pixel + i] = prevline[x * bytes_per_pixel + i] ^ mix[i]; } ) } break; case OPCODE_FILL_OR_MIX: if (prevline == NULL) { REPEAT( MASK_UPDATE(); if (mask & mixmask) { for (int i = 0; i < bytes_per_pixel; i++) { line[x * bytes_per_pixel + i] = mix[i]; } } else { for (int i = 0; i < bytes_per_pixel; i++) { line[x * bytes_per_pixel + i] = 0; } } ) } else { REPEAT( MASK_UPDATE(); if (mask & mixmask) { for (int i = 0; i < bytes_per_pixel; i++) { line[x * bytes_per_pixel + i] = prevline[x * bytes_per_pixel + i] ^ mix[i]; } } else { for (int i = 0; i < bytes_per_pixel; i++) { line[x * bytes_per_pixel + i] = prevline[x * bytes_per_pixel + i]; } } ) } break; case OPCODE_COLOUR: REPEAT( for (int i = 0; i < bytes_per_pixel; i++) { line[x * bytes_per_pixel + i] = colour2[i]; } ) break; case OPCODE_COPY: REPEAT( for (int i = 0; i < bytes_per_pixel; i++) { line[x * bytes_per_pixel + i] = CVAL(input); } ) break; case OPCODE_BICOLOUR: REPEAT( if (bicolour) { for (int i = 0; i < bytes_per_pixel; i++) { line[x * bytes_per_pixel + i] = colour2[i]; } bicolour = False; } else { for (int i = 0; i < bytes_per_pixel; i++) { line[x * bytes_per_pixel + i] = colour1[i]; } bicolour = True; count++; } ) break; case OPCODE_WHITE: REPEAT( for (int i = 0; i < bytes_per_pixel; i++) { line[x * bytes_per_pixel + i] = WHITE_PIXEL; } ) break; case OPCODE_BLACK: REPEAT( for (int i = 0; i < bytes_per_pixel; i++) { line[x * bytes_per_pixel + i] = BLACK_PIXEL; } ) break; default: logger(Core, Warning, "bitmap_decompress_n(), unhandled bitmap opcode 0x%x", opcode); return False; } } } return True; } /* decompress a colour plane */ static int process_plane(uint8 *in, int width, int height, uint8 *out, int size) { UNUSED(size); int indexw; int indexh; int code; int collen; int replen; int color; int x; int revcode; uint8 *last_line; uint8 *this_line; uint8 *org_in; uint8 *org_out; org_in = in; org_out = out; last_line = 0; indexh = 0; while (indexh < height) { out = (org_out + width * height * 4) - ((indexh + 1) * width * 4); color = 0; this_line = out; indexw = 0; if (last_line == 0) { while (indexw < width) { code = CVAL(in); replen = code & 0xf; collen = (code >> 4) & 0xf; revcode = (replen << 4) | collen; if ((revcode <= 47) && (revcode >= 16)) { replen = revcode; collen = 0; } while (indexw < width && collen > 0) { color = CVAL(in); *out = color; out += 4; indexw++; collen--; } while (indexw < width && replen > 0) { *out = color; out += 4; indexw++; replen--; } } } else { while (indexw < width) { code = CVAL(in); replen = code & 0xf; collen = (code >> 4) & 0xf; revcode = (replen << 4) | collen; if ((revcode <= 47) && (revcode >= 16)) { replen = revcode; collen = 0; } while (indexw < width && collen > 0) { x = CVAL(in); if (x & 1) { x = x >> 1; x = x + 1; color = -x; } else { x = x >> 1; color = x; } x = last_line[indexw * 4] + color; *out = x; out += 4; indexw++; collen--; } while (indexw < width && replen > 0) { x = last_line[indexw * 4] + color; *out = x; out += 4; indexw++; replen--; } } } indexh++; last_line = this_line; } return (int)(in - org_in); } /* 4 byte bitmap decompress */ static RD_BOOL bitmap_decompress4(uint8 *output, int width, int height, uint8 *input, int size) { int code; int bytes_pro; int total_pro; code = CVAL(input); if (code != 0x10) { return False; } total_pro = 1; bytes_pro = process_plane(input, width, height, output + 3, size - total_pro); total_pro += bytes_pro; input += bytes_pro; bytes_pro = process_plane(input, width, height, output + 2, size - total_pro); total_pro += bytes_pro; input += bytes_pro; bytes_pro = process_plane(input, width, height, output + 1, size - total_pro); total_pro += bytes_pro; input += bytes_pro; bytes_pro = process_plane(input, width, height, output + 0, size - total_pro); total_pro += bytes_pro; return size == total_pro; } /* main decompress function */ RD_BOOL bitmap_decompress(uint8 *output, int width, int height, uint8 *input, int size, int Bpp) { RD_BOOL rv = False; switch (Bpp) { case 1: case 2: case 3: rv = bitmap_decompress_n(output, width, height, input, size, Bpp); break; case 4: rv = bitmap_decompress4(output, width, height, input, size); break; default: logger(Core, Debug, "bitmap_decompress(), unhandled BPP %d", Bpp); break; } return rv; } /* *INDENT-ON* */