Something went wrong. Try again.
Reactos
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113/* -*- c-basic-offset: 8 -*- rdesktop: A Remote Desktop Protocol client. Bitmap decompression routines Copyright (C) Matthew Chapman 1999-2005
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.*/
/* three seperate function for speed when decompressing the bitmaps *//* when modifing one function make the change in the others *//* comment out #define BITMAP_SPEED_OVER_SIZE below for one slower function *//* j@american-data.com */
#define BITMAP_SPEED_OVER_SIZE
/* 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; \ } \}
#ifdef BITMAP_SPEED_OVER_SIZE
/* 1 byte bitmap decompress */static BOOLbitmap_decompress1(uint8 * output, int width, int height, uint8 * input, int size){ uint8 *end = input + size; uint8 *prevline = NULL; int opcode, count, offset, isfillormix; int lastopcode = -1, insertmix = False, bicolour = False; uint8 code; uint8 colour1 = 0, colour2 = 0; uint8 mixmask, mask = 0; uint8 mix = 0xff; int fom_mask = 0;#if 0 uint8 *line = NULL; int x = width;#else uint8 *line = output; int x = 0; int y = 0;#endif
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 0 if (height <= 0)#else if (y >= height)#endif return False; x = 0;
#if 0 height--;#else y ++;#endif
prevline = line;
#if 0 line = output + height * width;#else line = output + y * width;#endif } 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] = 0) break; default: unimpl("bitmap opcode 0x%x\n", opcode); return False; } } } return True;}
/* 2 byte bitmap decompress */static BOOLbitmap_decompress2(uint8 * output, int width, int height, uint8 * input, int size){ uint8 *end = input + size; uint16 *prevline = NULL; int opcode, count, offset, isfillormix; int lastopcode = -1, insertmix = False, bicolour = False; uint8 code; uint16 colour1 = 0, colour2 = 0; uint8 mixmask, mask = 0; uint16 mix = 0xffff; int fom_mask = 0;#if 0 uint8 *line = NULL; int x = width;#else uint8 *line = output; int x = 0; int y = 0;#endif
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 */ CVAL2(input, colour1); case 3: /* Colour */ CVAL2(input, colour2); break; case 6: /* SetMix/Mix */ case 7: /* SetMix/FillOrMix */ CVAL2(input, mix); 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 0 if (height <= 0)#else if (y >= height)#endif return False; x = 0;
#if 0 height--;#else y ++;#endif
prevline = line;
#if 0 line = ((uint16 *) output) + height * width;#else line = ((uint16 *) output) + y * width;#endif } 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(CVAL2(input, line[x])) 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] = 0xffff) break; case 0xe: /* Black */ REPEAT(line[x] = 0) break; default: unimpl("bitmap opcode 0x%x\n", opcode); return False; } } } return True;}
/* 3 byte bitmap decompress */static BOOLbitmap_decompress3(uint8 * output, int width, int height, uint8 * input, int size){ uint8 *end = input + size; uint8 *prevline = NULL; int opcode, count, offset, isfillormix; 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;#if 0 uint8 *line = NULL; int x = width;#else uint8 *line = output; int x = 0; int y = 0;#endif
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[0] = CVAL(input); colour1[1] = CVAL(input); colour1[2] = CVAL(input); case 3: /* Colour */ colour2[0] = CVAL(input); colour2[1] = CVAL(input); colour2[2] = CVAL(input); break; case 6: /* SetMix/Mix */ case 7: /* SetMix/FillOrMix */ mix[0] = CVAL(input); mix[1] = CVAL(input); mix[2] = 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 0 if (height <= 0)#else if (y >= height)#endif return False; x = 0;
#if 0 height--;#else y ++;#endif
prevline = line;
#if 0 line = output + height * (width * 3);#else line = output + y * (width * 3);#endif } switch (opcode) { case 0: /* Fill */ if (insertmix) { if (prevline == NULL) { line[x * 3] = mix[0]; line[x * 3 + 1] = mix[1]; line[x * 3 + 2] = mix[2]; } else { line[x * 3] = prevline[x * 3] ^ mix[0]; line[x * 3 + 1] = prevline[x * 3 + 1] ^ mix[1]; line[x * 3 + 2] = prevline[x * 3 + 2] ^ mix[2]; } insertmix = False; count--; x++; } if (prevline == NULL) { REPEAT ( line[x * 3] = 0; line[x * 3 + 1] = 0; line[x * 3 + 2] = 0; ) } else { REPEAT ( line[x * 3] = prevline[x * 3]; line[x * 3 + 1] = prevline[x * 3 + 1]; line[x * 3 + 2] = prevline[x * 3 + 2]; ) } break; case 1: /* Mix */ if (prevline == NULL) { REPEAT ( line[x * 3] = mix[0]; line[x * 3 + 1] = mix[1]; line[x * 3 + 2] = mix[2]; ) } else { REPEAT ( line[x * 3] = prevline[x * 3] ^ mix[0]; line[x * 3 + 1] = prevline[x * 3 + 1] ^ mix[1]; line[x * 3 + 2] = prevline[x * 3 + 2] ^ mix[2]; ) } break; case 2: /* Fill or Mix */ if (prevline == NULL) { REPEAT ( MASK_UPDATE(); if (mask & mixmask) { line[x * 3] = mix[0]; line[x * 3 + 1] = mix[1]; line[x * 3 + 2] = mix[2]; } else { line[x * 3] = 0; line[x * 3 + 1] = 0; line[x * 3 + 2] = 0; } ) } else { REPEAT ( MASK_UPDATE(); if (mask & mixmask) { line[x * 3] = prevline[x * 3] ^ mix [0]; line[x * 3 + 1] = prevline[x * 3 + 1] ^ mix [1]; line[x * 3 + 2] = prevline[x * 3 + 2] ^ mix [2]; } else { line[x * 3] = prevline[x * 3]; line[x * 3 + 1] = prevline[x * 3 + 1]; line[x * 3 + 2] = prevline[x * 3 + 2]; } ) } break; case 3: /* Colour */ REPEAT ( line[x * 3] = colour2 [0]; line[x * 3 + 1] = colour2 [1]; line[x * 3 + 2] = colour2 [2]; ) break; case 4: /* Copy */ REPEAT ( line[x * 3] = CVAL(input); line[x * 3 + 1] = CVAL(input); line[x * 3 + 2] = CVAL(input); ) break; case 8: /* Bicolour */ REPEAT ( if (bicolour) { line[x * 3] = colour2[0]; line[x * 3 + 1] = colour2[1]; line[x * 3 + 2] = colour2[2]; bicolour = False; } else { line[x * 3] = colour1[0]; line[x * 3 + 1] = colour1[1]; line[x * 3 + 2] = colour1[2]; bicolour = True; count++; } ) break; case 0xd: /* White */ REPEAT ( line[x * 3] = 0xff; line[x * 3 + 1] = 0xff; line[x * 3 + 2] = 0xff; ) break; case 0xe: /* Black */ REPEAT ( line[x * 3] = 0; line[x * 3 + 1] = 0; line[x * 3 + 2] = 0; ) break; default: unimpl("bitmap opcode 0x%x\n", opcode); return False; } } } return True;}
#else
static uint32cvalx(uint8 **input, int Bpp){ uint32 rv = 0; memcpy(&rv, *input, Bpp); *input += Bpp; return rv;}
static voidsetli(uint8 *input, int offset, uint32 value, int Bpp){ input += offset * Bpp; memcpy(input, &value, Bpp);}
static uint32getli(uint8 *input, int offset, int Bpp){ uint32 rv = 0; input += offset * Bpp; memcpy(&rv, input, Bpp); return rv;}
static BOOLbitmap_decompressx(uint8 *output, int width, int height, uint8 *input, int size, int Bpp){ uint8 *end = input + size; uint8 *prevline = NULL; int opcode, count, offset, isfillormix; 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;#if 0 uint8 *line = NULL; int x = width;#else uint8 *line = output; int x = 0; int y = 0;#endif
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 = 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 0 if (height <= 0)#else if (y >= height)#endif return False;
x = 0;
#if 0 height--;#else y ++;#endif
prevline = line;
#if 0 line = output + height * width * Bpp;#else line = output + y * width * Bpp;#endif }
switch (opcode) { 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(setli(line, x, 0, Bpp))} else { REPEAT(setli (line, x, getli(prevline, x, Bpp), Bpp)); } break;
case 1: /* Mix */ if (prevline == NULL) { REPEAT(setli(line, x, mix, Bpp)); } else { REPEAT(setli (line, x, getli(prevline, x, Bpp) ^ mix, Bpp)); } break;
case 2: /* Fill or Mix */ if (prevline == NULL) { REPEAT(MASK_UPDATE(); if (mask & mixmask) setli(line, x, mix, Bpp); else setli(line, x, 0, Bpp);); } else { 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;
case 3: /* Colour */ REPEAT(setli(line, x, colour2, Bpp)); break;
case 4: /* Copy */ REPEAT(setli(line, x, cvalx(&input, Bpp), Bpp)); break;
case 8: /* Bicolour */ REPEAT(if (bicolour) { setli(line, x, colour2, Bpp); bicolour = False;} else { setli(line, x, colour1, Bpp); bicolour = True; count++;} ); break;
case 0xd: /* White */ REPEAT(setli(line, x, 0xffffffff, Bpp)); break;
case 0xe: /* Black */ REPEAT(setli(line, x, 0, Bpp)); break;
default: unimpl("bitmap opcode 0x%x\n", opcode); return False; } } }
return True;}
#endif
/* main decompress function */BOOLbitmap_decompress(uint8 * output, int width, int height, uint8 * input, int size, int Bpp){#ifdef BITMAP_SPEED_OVER_SIZE BOOL rv = False; switch (Bpp) { case 1: rv = bitmap_decompress1(output, width, height, input, size); break; case 2: rv = bitmap_decompress2(output, width, height, input, size); break; case 3: rv = bitmap_decompress3(output, width, height, input, size); break; }#else BOOL rv; rv = bitmap_decompressx(output, width, height, input, size, Bpp);#endif return rv;}
/* *INDENT-ON* */