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 | // |
Pierre Ossman | bcc295e | 2014-02-12 13:16:43 +0100 | [diff] [blame] | 22 | // This file is #included after having set the following macro: |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 23 | // BPP - 8, 16 or 32 |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 24 | |
Pierre Ossman | c1244c0 | 2014-03-19 12:16:48 +0000 | [diff] [blame] | 25 | #include <stdio.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 26 | #include <rdr/InStream.h> |
| 27 | #include <rdr/ZlibInStream.h> |
Pierre Ossman | c1244c0 | 2014-03-19 12:16:48 +0000 | [diff] [blame] | 28 | #include <rfb/Exception.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 29 | |
| 30 | namespace rfb { |
| 31 | |
| 32 | // CONCAT2E concatenates its arguments, expanding them if they are macros |
| 33 | |
| 34 | #ifndef CONCAT2E |
| 35 | #define CONCAT2(a,b) a##b |
| 36 | #define CONCAT2E(a,b) CONCAT2(a,b) |
| 37 | #endif |
| 38 | |
| 39 | #ifdef CPIXEL |
| 40 | #define PIXEL_T rdr::CONCAT2E(U,BPP) |
Pierre Ossman | 7b5c069 | 2014-03-17 14:35:51 +0100 | [diff] [blame] | 41 | #define READ_PIXEL(is) CONCAT2E(readOpaque,CPIXEL)(is) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 42 | #define ZRLE_DECODE CONCAT2E(zrleDecode,CPIXEL) |
| 43 | #else |
| 44 | #define PIXEL_T rdr::CONCAT2E(U,BPP) |
Pierre Ossman | 7b5c069 | 2014-03-17 14:35:51 +0100 | [diff] [blame] | 45 | #define READ_PIXEL(is) is->CONCAT2E(readOpaque,BPP)() |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 46 | #define ZRLE_DECODE CONCAT2E(zrleDecode,BPP) |
| 47 | #endif |
| 48 | |
| 49 | void ZRLE_DECODE (const Rect& r, rdr::InStream* is, |
Pierre Ossman | 1349e42 | 2016-10-05 11:00:37 +0200 | [diff] [blame] | 50 | rdr::ZlibInStream* zis, |
Pierre Ossman | 0c9bd4b | 2014-07-09 16:44:11 +0200 | [diff] [blame] | 51 | const PixelFormat& pf, ModifiablePixelBuffer* pb) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 52 | { |
| 53 | int length = is->readU32(); |
| 54 | zis->setUnderlying(is, length); |
| 55 | Rect t; |
Pierre Ossman | 1349e42 | 2016-10-05 11:00:37 +0200 | [diff] [blame] | 56 | PIXEL_T buf[64 * 64]; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 57 | |
| 58 | for (t.tl.y = r.tl.y; t.tl.y < r.br.y; t.tl.y += 64) { |
| 59 | |
| 60 | t.br.y = __rfbmin(r.br.y, t.tl.y + 64); |
| 61 | |
| 62 | for (t.tl.x = r.tl.x; t.tl.x < r.br.x; t.tl.x += 64) { |
| 63 | |
| 64 | t.br.x = __rfbmin(r.br.x, t.tl.x + 64); |
| 65 | |
| 66 | int mode = zis->readU8(); |
| 67 | bool rle = mode & 128; |
| 68 | int palSize = mode & 127; |
| 69 | PIXEL_T palette[128]; |
| 70 | |
| 71 | for (int i = 0; i < palSize; i++) { |
Pierre Ossman | 7b5c069 | 2014-03-17 14:35:51 +0100 | [diff] [blame] | 72 | palette[i] = READ_PIXEL(zis); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | if (palSize == 1) { |
| 76 | PIXEL_T pix = palette[0]; |
Pierre Ossman | 56f99d6 | 2015-06-05 12:57:02 +0200 | [diff] [blame] | 77 | pb->fillRect(pf, t, &pix); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 78 | continue; |
| 79 | } |
| 80 | |
| 81 | if (!rle) { |
| 82 | if (palSize == 0) { |
| 83 | |
| 84 | // raw |
| 85 | |
| 86 | #ifdef CPIXEL |
| 87 | for (PIXEL_T* ptr = buf; ptr < buf+t.area(); ptr++) { |
Pierre Ossman | 7b5c069 | 2014-03-17 14:35:51 +0100 | [diff] [blame] | 88 | *ptr = READ_PIXEL(zis); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 89 | } |
| 90 | #else |
| 91 | zis->readBytes(buf, t.area() * (BPP / 8)); |
| 92 | #endif |
| 93 | |
| 94 | } else { |
| 95 | |
| 96 | // packed pixels |
| 97 | int bppp = ((palSize > 16) ? 8 : |
| 98 | ((palSize > 4) ? 4 : ((palSize > 2) ? 2 : 1))); |
| 99 | |
| 100 | PIXEL_T* ptr = buf; |
| 101 | |
| 102 | for (int i = 0; i < t.height(); i++) { |
| 103 | PIXEL_T* eol = ptr + t.width(); |
| 104 | rdr::U8 byte = 0; |
| 105 | rdr::U8 nbits = 0; |
| 106 | |
| 107 | while (ptr < eol) { |
| 108 | if (nbits == 0) { |
| 109 | byte = zis->readU8(); |
| 110 | nbits = 8; |
| 111 | } |
| 112 | nbits -= bppp; |
| 113 | rdr::U8 index = (byte >> nbits) & ((1 << bppp) - 1) & 127; |
| 114 | *ptr++ = palette[index]; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 119 | } else { |
| 120 | |
| 121 | if (palSize == 0) { |
| 122 | |
| 123 | // plain RLE |
| 124 | |
| 125 | PIXEL_T* ptr = buf; |
| 126 | PIXEL_T* end = ptr + t.area(); |
| 127 | while (ptr < end) { |
Pierre Ossman | 7b5c069 | 2014-03-17 14:35:51 +0100 | [diff] [blame] | 128 | PIXEL_T pix = READ_PIXEL(zis); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 129 | int len = 1; |
| 130 | int b; |
| 131 | do { |
| 132 | b = zis->readU8(); |
| 133 | len += b; |
| 134 | } while (b == 255); |
| 135 | |
Pierre Ossman | c1244c0 | 2014-03-19 12:16:48 +0000 | [diff] [blame] | 136 | if (end - ptr < len) { |
Pierre Ossman | c1244c0 | 2014-03-19 12:16:48 +0000 | [diff] [blame] | 137 | throw Exception ("ZRLE decode error"); |
| 138 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 139 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 140 | while (len-- > 0) *ptr++ = pix; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 141 | |
| 142 | } |
| 143 | } else { |
| 144 | |
| 145 | // palette RLE |
| 146 | |
| 147 | PIXEL_T* ptr = buf; |
| 148 | PIXEL_T* end = ptr + t.area(); |
| 149 | while (ptr < end) { |
| 150 | int index = zis->readU8(); |
| 151 | int len = 1; |
| 152 | if (index & 128) { |
| 153 | int b; |
| 154 | do { |
| 155 | b = zis->readU8(); |
| 156 | len += b; |
| 157 | } while (b == 255); |
| 158 | |
Pierre Ossman | c1244c0 | 2014-03-19 12:16:48 +0000 | [diff] [blame] | 159 | if (end - ptr < len) { |
Pierre Ossman | c1244c0 | 2014-03-19 12:16:48 +0000 | [diff] [blame] | 160 | throw Exception ("ZRLE decode error"); |
| 161 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | index &= 127; |
| 165 | |
| 166 | PIXEL_T pix = palette[index]; |
| 167 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 168 | while (len-- > 0) *ptr++ = pix; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
Pierre Ossman | 0c9bd4b | 2014-07-09 16:44:11 +0200 | [diff] [blame] | 173 | pb->imageRect(pf, t, buf); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
Pierre Ossman | 6f318e4 | 2015-11-11 13:11:09 +0100 | [diff] [blame] | 177 | zis->removeUnderlying(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | #undef ZRLE_DECODE |
| 181 | #undef READ_PIXEL |
| 182 | #undef PIXEL_T |
| 183 | } |