Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame^] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
| 2 | * |
| 3 | * This is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This software is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this software; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 16 | * USA. |
| 17 | */ |
| 18 | |
| 19 | // |
| 20 | // ZRLE decoding function. |
| 21 | // |
| 22 | // This file is #included after having set the following macros: |
| 23 | // BPP - 8, 16 or 32 |
| 24 | // EXTRA_ARGS - optional extra arguments |
| 25 | // FILL_RECT - fill a rectangle with a single colour |
| 26 | // IMAGE_RECT - draw a rectangle of pixel data from a buffer |
| 27 | |
| 28 | #include <rdr/InStream.h> |
| 29 | #include <rdr/ZlibInStream.h> |
| 30 | #include <assert.h> |
| 31 | |
| 32 | namespace rfb { |
| 33 | |
| 34 | // CONCAT2E concatenates its arguments, expanding them if they are macros |
| 35 | |
| 36 | #ifndef CONCAT2E |
| 37 | #define CONCAT2(a,b) a##b |
| 38 | #define CONCAT2E(a,b) CONCAT2(a,b) |
| 39 | #endif |
| 40 | |
| 41 | #ifdef CPIXEL |
| 42 | #define PIXEL_T rdr::CONCAT2E(U,BPP) |
| 43 | #define READ_PIXEL CONCAT2E(readOpaque,CPIXEL) |
| 44 | #define ZRLE_DECODE CONCAT2E(zrleDecode,CPIXEL) |
| 45 | #else |
| 46 | #define PIXEL_T rdr::CONCAT2E(U,BPP) |
| 47 | #define READ_PIXEL CONCAT2E(readOpaque,BPP) |
| 48 | #define ZRLE_DECODE CONCAT2E(zrleDecode,BPP) |
| 49 | #endif |
| 50 | |
| 51 | void ZRLE_DECODE (const Rect& r, rdr::InStream* is, |
| 52 | rdr::ZlibInStream* zis, PIXEL_T* buf |
| 53 | #ifdef EXTRA_ARGS |
| 54 | , EXTRA_ARGS |
| 55 | #endif |
| 56 | ) |
| 57 | { |
| 58 | int length = is->readU32(); |
| 59 | zis->setUnderlying(is, length); |
| 60 | Rect t; |
| 61 | |
| 62 | for (t.tl.y = r.tl.y; t.tl.y < r.br.y; t.tl.y += 64) { |
| 63 | |
| 64 | t.br.y = __rfbmin(r.br.y, t.tl.y + 64); |
| 65 | |
| 66 | for (t.tl.x = r.tl.x; t.tl.x < r.br.x; t.tl.x += 64) { |
| 67 | |
| 68 | t.br.x = __rfbmin(r.br.x, t.tl.x + 64); |
| 69 | |
| 70 | int mode = zis->readU8(); |
| 71 | bool rle = mode & 128; |
| 72 | int palSize = mode & 127; |
| 73 | PIXEL_T palette[128]; |
| 74 | |
| 75 | for (int i = 0; i < palSize; i++) { |
| 76 | palette[i] = zis->READ_PIXEL(); |
| 77 | } |
| 78 | |
| 79 | if (palSize == 1) { |
| 80 | PIXEL_T pix = palette[0]; |
| 81 | FILL_RECT(t,pix); |
| 82 | continue; |
| 83 | } |
| 84 | |
| 85 | if (!rle) { |
| 86 | if (palSize == 0) { |
| 87 | |
| 88 | // raw |
| 89 | |
| 90 | #ifdef CPIXEL |
| 91 | for (PIXEL_T* ptr = buf; ptr < buf+t.area(); ptr++) { |
| 92 | *ptr = zis->READ_PIXEL(); |
| 93 | } |
| 94 | #else |
| 95 | zis->readBytes(buf, t.area() * (BPP / 8)); |
| 96 | #endif |
| 97 | |
| 98 | } else { |
| 99 | |
| 100 | // packed pixels |
| 101 | int bppp = ((palSize > 16) ? 8 : |
| 102 | ((palSize > 4) ? 4 : ((palSize > 2) ? 2 : 1))); |
| 103 | |
| 104 | PIXEL_T* ptr = buf; |
| 105 | |
| 106 | for (int i = 0; i < t.height(); i++) { |
| 107 | PIXEL_T* eol = ptr + t.width(); |
| 108 | rdr::U8 byte = 0; |
| 109 | rdr::U8 nbits = 0; |
| 110 | |
| 111 | while (ptr < eol) { |
| 112 | if (nbits == 0) { |
| 113 | byte = zis->readU8(); |
| 114 | nbits = 8; |
| 115 | } |
| 116 | nbits -= bppp; |
| 117 | rdr::U8 index = (byte >> nbits) & ((1 << bppp) - 1) & 127; |
| 118 | *ptr++ = palette[index]; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | #ifdef FAVOUR_FILL_RECT |
| 124 | //fprintf(stderr,"copying data to screen %dx%d at %d,%d\n", |
| 125 | //t.width(),t.height(),t.tl.x,t.tl.y); |
| 126 | IMAGE_RECT(t,buf); |
| 127 | #endif |
| 128 | |
| 129 | } else { |
| 130 | |
| 131 | if (palSize == 0) { |
| 132 | |
| 133 | // plain RLE |
| 134 | |
| 135 | PIXEL_T* ptr = buf; |
| 136 | PIXEL_T* end = ptr + t.area(); |
| 137 | while (ptr < end) { |
| 138 | PIXEL_T pix = zis->READ_PIXEL(); |
| 139 | int len = 1; |
| 140 | int b; |
| 141 | do { |
| 142 | b = zis->readU8(); |
| 143 | len += b; |
| 144 | } while (b == 255); |
| 145 | |
| 146 | assert(len <= end - ptr); |
| 147 | |
| 148 | #ifdef FAVOUR_FILL_RECT |
| 149 | int i = ptr - buf; |
| 150 | ptr += len; |
| 151 | |
| 152 | int runX = i % t.width(); |
| 153 | int runY = i / t.width(); |
| 154 | |
| 155 | if (runX + len > t.width()) { |
| 156 | if (runX != 0) { |
| 157 | FILL_RECT(Rect(t.tl.x+runX, t.tl.y+runY, t.width()-runX, 1), |
| 158 | pix); |
| 159 | len -= t.width()-runX; |
| 160 | runX = 0; |
| 161 | runY++; |
| 162 | } |
| 163 | |
| 164 | if (len > t.width()) { |
| 165 | FILL_RECT(Rect(t.tl.x, t.tl.y+runY, t.width(), len/t.width()), |
| 166 | pix); |
| 167 | runY += len / t.width(); |
| 168 | len = len % t.width(); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | if (len != 0) { |
| 173 | FILL_RECT(Rect(t.tl.x+runX, t.tl.y+runY, len, 1), pix); |
| 174 | } |
| 175 | #else |
| 176 | while (len-- > 0) *ptr++ = pix; |
| 177 | #endif |
| 178 | |
| 179 | } |
| 180 | } else { |
| 181 | |
| 182 | // palette RLE |
| 183 | |
| 184 | PIXEL_T* ptr = buf; |
| 185 | PIXEL_T* end = ptr + t.area(); |
| 186 | while (ptr < end) { |
| 187 | int index = zis->readU8(); |
| 188 | int len = 1; |
| 189 | if (index & 128) { |
| 190 | int b; |
| 191 | do { |
| 192 | b = zis->readU8(); |
| 193 | len += b; |
| 194 | } while (b == 255); |
| 195 | |
| 196 | assert(len <= end - ptr); |
| 197 | } |
| 198 | |
| 199 | index &= 127; |
| 200 | |
| 201 | PIXEL_T pix = palette[index]; |
| 202 | |
| 203 | #ifdef FAVOUR_FILL_RECT |
| 204 | int i = ptr - buf; |
| 205 | ptr += len; |
| 206 | |
| 207 | int runX = i % t.width(); |
| 208 | int runY = i / t.width(); |
| 209 | |
| 210 | if (runX + len > t.width()) { |
| 211 | if (runX != 0) { |
| 212 | FILL_RECT(Rect(t.tl.x+runX, t.tl.y+runY, t.width()-runX, 1), |
| 213 | pix); |
| 214 | len -= t.width()-runX; |
| 215 | runX = 0; |
| 216 | runY++; |
| 217 | } |
| 218 | |
| 219 | if (len > t.width()) { |
| 220 | FILL_RECT(Rect(t.tl.x, t.tl.y+runY, t.width(), len/t.width()), |
| 221 | pix); |
| 222 | runY += len / t.width(); |
| 223 | len = len % t.width(); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | if (len != 0) { |
| 228 | FILL_RECT(Rect(t.tl.x+runX, t.tl.y+runY, len, 1), pix); |
| 229 | } |
| 230 | #else |
| 231 | while (len-- > 0) *ptr++ = pix; |
| 232 | #endif |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | #ifndef FAVOUR_FILL_RECT |
| 238 | //fprintf(stderr,"copying data to screen %dx%d at %d,%d\n", |
| 239 | //t.width(),t.height(),t.tl.x,t.tl.y); |
| 240 | IMAGE_RECT(t,buf); |
| 241 | #endif |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | zis->reset(); |
| 246 | } |
| 247 | |
| 248 | #undef ZRLE_DECODE |
| 249 | #undef READ_PIXEL |
| 250 | #undef PIXEL_T |
| 251 | } |