Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2000-2003 Constantin Kaplinsky. All Rights Reserved. |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 2 | * Copyright (C) 2011 D. R. Commander. All Rights Reserved. |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 3 | * |
| 4 | * This is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This software is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this software; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 17 | * USA. |
| 18 | */ |
| 19 | #include <rdr/OutStream.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 20 | #include <rfb/encodings.h> |
| 21 | #include <rfb/ConnParams.h> |
| 22 | #include <rfb/SMsgWriter.h> |
| 23 | #include <rfb/TightEncoder.h> |
| 24 | |
| 25 | using namespace rfb; |
| 26 | |
| 27 | // Minimum amount of data to be compressed. This value should not be |
| 28 | // changed, doing so will break compatibility with existing clients. |
| 29 | #define TIGHT_MIN_TO_COMPRESS 12 |
| 30 | |
| 31 | // Adjustable parameters. |
| 32 | // FIXME: Get rid of #defines |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 33 | #define TIGHT_MAX_SPLIT_TILE_SIZE 16 |
| 34 | #define TIGHT_MIN_SPLIT_RECT_SIZE 4096 |
| 35 | #define TIGHT_MIN_SOLID_SUBRECT_SIZE 2048 |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 36 | |
| 37 | // |
| 38 | // Compression level stuff. The following array contains various |
| 39 | // encoder parameters for each of 10 compression levels (0..9). |
| 40 | // Last three parameters correspond to JPEG quality levels (0..9). |
| 41 | // |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 42 | // NOTE: The parameters used in this encoder are the result of painstaking |
| 43 | // research by The VirtualGL Project using RFB session captures from a variety |
| 44 | // of both 2D and 3D applications. See http://www.VirtualGL.org for the full |
| 45 | // reports. |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 46 | |
DRC | 773cf3c | 2009-03-12 19:26:44 +0000 | [diff] [blame] | 47 | // NOTE: The JPEG quality and subsampling levels below were obtained |
| 48 | // experimentally by the VirtualGL Project. They represent the approximate |
| 49 | // average compression ratios listed below, as measured across the set of |
| 50 | // every 10th frame in the SPECviewperf 9 benchmark suite. |
| 51 | // |
| 52 | // 9 = JPEG quality 100, no subsampling (ratio ~= 10:1) |
| 53 | // [this should be lossless, except for round-off error] |
| 54 | // 8 = JPEG quality 92, no subsampling (ratio ~= 20:1) |
| 55 | // [this should be perceptually lossless, based on current research] |
| 56 | // 7 = JPEG quality 86, no subsampling (ratio ~= 25:1) |
| 57 | // 6 = JPEG quality 79, no subsampling (ratio ~= 30:1) |
| 58 | // 5 = JPEG quality 77, 4:2:2 subsampling (ratio ~= 40:1) |
| 59 | // 4 = JPEG quality 62, 4:2:2 subsampling (ratio ~= 50:1) |
| 60 | // 3 = JPEG quality 42, 4:2:2 subsampling (ratio ~= 60:1) |
| 61 | // 2 = JPEG quality 41, 4:2:0 subsampling (ratio ~= 70:1) |
| 62 | // 1 = JPEG quality 29, 4:2:0 subsampling (ratio ~= 80:1) |
| 63 | // 0 = JPEG quality 15, 4:2:0 subsampling (ratio ~= 100:1) |
| 64 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 65 | const TIGHT_CONF TightEncoder::conf[10] = { |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 66 | { 65536, 2048, 6, 0, 0, 0, 4, 24, 15, SUBSAMP_420 }, // 0 |
| 67 | { 65536, 2048, 6, 1, 1, 1, 8, 24, 29, SUBSAMP_420 }, // 1 |
| 68 | { 65536, 2048, 8, 3, 3, 2, 24, 96, 41, SUBSAMP_420 }, // 2 |
| 69 | { 65536, 2048, 12, 5, 5, 2, 32, 96, 42, SUBSAMP_422 }, // 3 |
| 70 | { 65536, 2048, 12, 6, 7, 3, 32, 96, 62, SUBSAMP_422 }, // 4 |
| 71 | { 65536, 2048, 12, 7, 8, 4, 32, 96, 77, SUBSAMP_422 }, // 5 |
| 72 | { 65536, 2048, 16, 7, 8, 5, 32, 96, 79, SUBSAMP_NONE }, // 6 |
| 73 | { 65536, 2048, 16, 8, 9, 6, 64, 96, 86, SUBSAMP_NONE }, // 7 |
| 74 | { 65536, 2048, 24, 9, 9, 7, 64, 96, 92, SUBSAMP_NONE }, // 8 |
| 75 | { 65536, 2048, 32, 9, 9, 9, 96, 96,100, SUBSAMP_NONE } // 9 |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 76 | }; |
Pierre Ossman | 701ad68 | 2011-11-20 15:39:17 +0000 | [diff] [blame] | 77 | |
| 78 | const int TightEncoder::defaultCompressLevel = 2; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 79 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 80 | // |
| 81 | // Including BPP-dependent implementation of the encoder. |
| 82 | // |
| 83 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 84 | #define BPP 8 |
| 85 | #include <rfb/tightEncode.h> |
| 86 | #undef BPP |
| 87 | #define BPP 16 |
| 88 | #include <rfb/tightEncode.h> |
| 89 | #undef BPP |
| 90 | #define BPP 32 |
| 91 | #include <rfb/tightEncode.h> |
| 92 | #undef BPP |
| 93 | |
| 94 | Encoder* TightEncoder::create(SMsgWriter* writer) |
| 95 | { |
| 96 | return new TightEncoder(writer); |
| 97 | } |
| 98 | |
| 99 | TightEncoder::TightEncoder(SMsgWriter* writer_) : writer(writer_) |
| 100 | { |
| 101 | setCompressLevel(defaultCompressLevel); |
| 102 | setQualityLevel(-1); |
| 103 | } |
| 104 | |
| 105 | TightEncoder::~TightEncoder() |
| 106 | { |
| 107 | } |
| 108 | |
| 109 | void TightEncoder::setCompressLevel(int level) |
| 110 | { |
| 111 | if (level >= 0 && level <= 9) { |
| 112 | pconf = &conf[level]; |
| 113 | } else { |
| 114 | pconf = &conf[defaultCompressLevel]; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | void TightEncoder::setQualityLevel(int level) |
| 119 | { |
| 120 | if (level >= 0 && level <= 9) { |
DRC | b4a8323 | 2011-08-19 04:57:18 +0000 | [diff] [blame] | 121 | jpegQuality = conf[level].jpegQuality; |
| 122 | jpegSubsampling = conf[level].jpegSubsampling; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 123 | } else { |
DRC | b4a8323 | 2011-08-19 04:57:18 +0000 | [diff] [blame] | 124 | jpegQuality = -1; |
| 125 | jpegSubsampling = SUBSAMP_UNDEFINED; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | void TightEncoder::setFineQualityLevel(int quality, JPEG_SUBSAMP subsampling) |
| 130 | { |
| 131 | if (quality >= 1 && quality <= 100) { |
| 132 | jpegQuality = quality; |
| 133 | } |
| 134 | if (subsampling >= SUBSAMP_NONE && subsampling <= SUBSAMP_GRAY) { |
| 135 | jpegSubsampling = subsampling; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 139 | bool TightEncoder::checkSolidTile(Rect& r, rdr::U32* colorPtr, |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 140 | bool needSameColor) |
| 141 | { |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 142 | switch (serverpf.bpp) { |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 143 | case 32: |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 144 | return checkSolidTile32(r, colorPtr, needSameColor); |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 145 | case 16: |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 146 | return checkSolidTile16(r, colorPtr, needSameColor); |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 147 | default: |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 148 | return checkSolidTile8(r, colorPtr, needSameColor); |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 152 | void TightEncoder::findBestSolidArea(Rect& r, rdr::U32 colorValue, Rect& bestr) |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 153 | { |
| 154 | int dx, dy, dw, dh; |
| 155 | int w_prev; |
| 156 | Rect sr; |
| 157 | int w_best = 0, h_best = 0; |
| 158 | |
| 159 | bestr.tl.x = bestr.br.x = r.tl.x; |
| 160 | bestr.tl.y = bestr.br.y = r.tl.y; |
| 161 | |
| 162 | w_prev = r.width(); |
| 163 | |
| 164 | for (dy = r.tl.y; dy < r.br.y; dy += TIGHT_MAX_SPLIT_TILE_SIZE) { |
| 165 | |
| 166 | dh = (dy + TIGHT_MAX_SPLIT_TILE_SIZE <= r.br.y) ? |
| 167 | TIGHT_MAX_SPLIT_TILE_SIZE : (r.br.y - dy); |
| 168 | dw = (w_prev > TIGHT_MAX_SPLIT_TILE_SIZE) ? |
| 169 | TIGHT_MAX_SPLIT_TILE_SIZE : w_prev; |
| 170 | |
| 171 | sr.setXYWH(r.tl.x, dy, dw, dh); |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 172 | if (!checkSolidTile(sr, &colorValue, true)) |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 173 | break; |
| 174 | |
| 175 | for (dx = r.tl.x + dw; dx < r.tl.x + w_prev;) { |
| 176 | dw = (dx + TIGHT_MAX_SPLIT_TILE_SIZE <= r.tl.x + w_prev) ? |
| 177 | TIGHT_MAX_SPLIT_TILE_SIZE : (r.tl.x + w_prev - dx); |
| 178 | sr.setXYWH(dx, dy, dw, dh); |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 179 | if (!checkSolidTile(sr, &colorValue, true)) |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 180 | break; |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 181 | dx += dw; |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | w_prev = dx - r.tl.x; |
| 185 | if (w_prev * (dy + dh - r.tl.y) > w_best * h_best) { |
| 186 | w_best = w_prev; |
| 187 | h_best = dy + dh - r.tl.y; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | bestr.br.x = bestr.tl.x + w_best; |
| 192 | bestr.br.y = bestr.tl.y + h_best; |
| 193 | } |
| 194 | |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 195 | void TightEncoder::extendSolidArea(const Rect& r, rdr::U32 colorValue, |
| 196 | Rect& er) |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 197 | { |
| 198 | int cx, cy; |
| 199 | Rect sr; |
| 200 | |
| 201 | // Try to extend the area upwards. |
| 202 | for (cy = er.tl.y - 1; ; cy--) { |
| 203 | sr.setXYWH(er.tl.x, cy, er.width(), 1); |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 204 | if (cy < r.tl.y || !checkSolidTile(sr, &colorValue, true)) |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 205 | break; |
| 206 | } |
| 207 | er.tl.y = cy + 1; |
| 208 | |
| 209 | // ... downwards. |
| 210 | for (cy = er.br.y; ; cy++) { |
| 211 | sr.setXYWH(er.tl.x, cy, er.width(), 1); |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 212 | if (cy >= r.br.y || !checkSolidTile(sr, &colorValue, true)) |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 213 | break; |
| 214 | } |
| 215 | er.br.y = cy; |
| 216 | |
| 217 | // ... to the left. |
| 218 | for (cx = er.tl.x - 1; ; cx--) { |
| 219 | sr.setXYWH(cx, er.tl.y, 1, er.height()); |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 220 | if (cx < r.tl.x || !checkSolidTile(sr, &colorValue, true)) |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 221 | break; |
| 222 | } |
| 223 | er.tl.x = cx + 1; |
| 224 | |
| 225 | // ... to the right. |
| 226 | for (cx = er.br.x; ; cx++) { |
| 227 | sr.setXYWH(cx, er.tl.y, 1, er.height()); |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 228 | if (cx >= r.br.x || !checkSolidTile(sr, &colorValue, true)) |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 229 | break; |
| 230 | } |
| 231 | er.br.x = cx; |
| 232 | } |
| 233 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 234 | int TightEncoder::getNumRects(const Rect &r) |
| 235 | { |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 236 | ConnParams* cp = writer->getConnParams(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 237 | const unsigned int w = r.width(); |
| 238 | const unsigned int h = r.height(); |
| 239 | |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 240 | // If last rect. encoding is enabled, we can use the higher-performance |
| 241 | // code that pre-computes solid rectangles. In that case, we don't care |
| 242 | // about the rectangle count. |
| 243 | if (cp->supportsLastRect && w * h >= TIGHT_MIN_SPLIT_RECT_SIZE) |
| 244 | return 0; |
| 245 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 246 | // Will this rectangle split into subrects? |
| 247 | bool rectTooBig = w > pconf->maxRectWidth || w * h > pconf->maxRectSize; |
| 248 | if (!rectTooBig) |
| 249 | return 1; |
| 250 | |
| 251 | // Compute max sub-rectangle size. |
| 252 | const unsigned int subrectMaxWidth = |
| 253 | (w > pconf->maxRectWidth) ? pconf->maxRectWidth : w; |
| 254 | const unsigned int subrectMaxHeight = |
| 255 | pconf->maxRectSize / subrectMaxWidth; |
| 256 | |
| 257 | // Return the number of subrects. |
| 258 | return (((w - 1) / pconf->maxRectWidth + 1) * |
| 259 | ((h - 1) / subrectMaxHeight + 1)); |
| 260 | } |
| 261 | |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 262 | void TightEncoder::sendRectSimple(const Rect& r) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 263 | { |
| 264 | // Shortcuts to rectangle coordinates and dimensions. |
| 265 | const int x = r.tl.x; |
| 266 | const int y = r.tl.y; |
| 267 | const unsigned int w = r.width(); |
| 268 | const unsigned int h = r.height(); |
| 269 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 270 | // Encode small rects as is. |
| 271 | bool rectTooBig = w > pconf->maxRectWidth || w * h > pconf->maxRectSize; |
| 272 | if (!rectTooBig) { |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 273 | writeSubrect(r); |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 274 | return; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | // Compute max sub-rectangle size. |
| 278 | const unsigned int subrectMaxWidth = |
| 279 | (w > pconf->maxRectWidth) ? pconf->maxRectWidth : w; |
| 280 | const unsigned int subrectMaxHeight = |
| 281 | pconf->maxRectSize / subrectMaxWidth; |
| 282 | |
| 283 | // Split big rects into separately encoded subrects. |
| 284 | Rect sr; |
| 285 | unsigned int dx, dy, sw, sh; |
| 286 | for (dy = 0; dy < h; dy += subrectMaxHeight) { |
| 287 | for (dx = 0; dx < w; dx += pconf->maxRectWidth) { |
| 288 | sw = (dx + pconf->maxRectWidth < w) ? pconf->maxRectWidth : w - dx; |
| 289 | sh = (dy + subrectMaxHeight < h) ? subrectMaxHeight : h - dy; |
| 290 | sr.setXYWH(x + dx, y + dy, sw, sh); |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 291 | writeSubrect(sr); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 292 | } |
| 293 | } |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 294 | } |
| 295 | |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 296 | bool TightEncoder::writeRect(const Rect& _r, TransImageGetter* _ig, |
| 297 | Rect* actual) |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 298 | { |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 299 | ig = _ig; |
| 300 | serverpf = ig->getPixelBuffer()->getPF(); |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 301 | ConnParams* cp = writer->getConnParams(); |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 302 | clientpf = cp->pf(); |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 303 | |
| 304 | // Shortcuts to rectangle coordinates and dimensions. |
| 305 | Rect r = _r; |
| 306 | int x = r.tl.x; |
| 307 | int y = r.tl.y; |
DRC | e3ffcf7 | 2011-08-19 03:11:32 +0000 | [diff] [blame] | 308 | int w = r.width(); |
| 309 | int h = r.height(); |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 310 | |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 311 | // Encode small rects as is. |
| 312 | if (!cp->supportsLastRect || w * h < TIGHT_MIN_SPLIT_RECT_SIZE) { |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 313 | sendRectSimple(r); |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 314 | return true; |
| 315 | } |
| 316 | |
| 317 | // Split big rects into separately encoded subrects. |
| 318 | Rect sr, bestr; |
DRC | e3ffcf7 | 2011-08-19 03:11:32 +0000 | [diff] [blame] | 319 | int dx, dy, dw, dh; |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 320 | rdr::U32 colorValue; |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 321 | int maxRectWidth = pconf->maxRectWidth; |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 322 | int nMaxWidth = (w > maxRectWidth) ? maxRectWidth : w; |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 323 | int nMaxRows = pconf->maxRectSize / nMaxWidth; |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 324 | |
| 325 | // Try to find large solid-color areas and send them separately. |
| 326 | for (dy = y; dy < y + h; dy += TIGHT_MAX_SPLIT_TILE_SIZE) { |
| 327 | |
| 328 | // If a rectangle becomes too large, send its upper part now. |
| 329 | if (dy - y >= nMaxRows) { |
| 330 | sr.setXYWH(x, y, w, nMaxRows); |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 331 | sendRectSimple(sr); |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 332 | r.tl.y += nMaxRows; |
| 333 | y = r.tl.y; |
| 334 | h = r.height(); |
| 335 | } |
| 336 | |
| 337 | dh = (dy + TIGHT_MAX_SPLIT_TILE_SIZE <= y + h) ? |
| 338 | TIGHT_MAX_SPLIT_TILE_SIZE : (y + h - dy); |
| 339 | |
| 340 | for (dx = x; dx < x + w; dx += TIGHT_MAX_SPLIT_TILE_SIZE) { |
| 341 | |
| 342 | dw = (dx + TIGHT_MAX_SPLIT_TILE_SIZE <= x + w) ? |
| 343 | TIGHT_MAX_SPLIT_TILE_SIZE : (x + w - dx); |
| 344 | |
| 345 | sr.setXYWH(dx, dy, dw, dh); |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 346 | if (checkSolidTile(sr, &colorValue, false)) { |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 347 | |
DRC | b4a8323 | 2011-08-19 04:57:18 +0000 | [diff] [blame] | 348 | if (jpegSubsampling == SUBSAMP_GRAY && jpegQuality != -1) { |
| 349 | Colour rgb; |
| 350 | serverpf.rgbFromPixel(colorValue, NULL, &rgb); |
| 351 | rdr::U32 lum = ((257 * rgb.r) + (504 * rgb.g) + (98 * rgb.b) |
| 352 | + 16500) / 1000; |
| 353 | colorValue = lum + (lum << 8) + (lum << 16); |
| 354 | } |
| 355 | |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 356 | // Get dimensions of solid-color area. |
| 357 | sr.setXYWH(dx, dy, r.br.x - dx, r.br.y - dy); |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 358 | findBestSolidArea(sr, colorValue, bestr); |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 359 | |
| 360 | // Make sure a solid rectangle is large enough |
| 361 | // (or the whole rectangle is of the same color). |
| 362 | if (bestr.area() != r.area() |
| 363 | && bestr.area() < TIGHT_MIN_SOLID_SUBRECT_SIZE) |
| 364 | continue; |
| 365 | |
| 366 | // Try to extend solid rectangle to maximum size. |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 367 | extendSolidArea(r, colorValue, bestr); |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 368 | |
| 369 | // Send rectangles at top and left to solid-color area. |
| 370 | if (bestr.tl.y != y) { |
| 371 | sr.setXYWH(x, y, w, bestr.tl.y - y); |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 372 | sendRectSimple(sr); |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 373 | } |
| 374 | if (bestr.tl.x != x) { |
| 375 | sr.setXYWH(x, bestr.tl.y, bestr.tl.x - x, bestr.height()); |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 376 | writeRect(sr, _ig, NULL); |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | // Send solid-color rectangle. |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 380 | writeSubrect(bestr, true); |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 381 | |
| 382 | // Send remaining rectangles (at right and bottom). |
| 383 | if (bestr.br.x != r.br.x) { |
| 384 | sr.setXYWH(bestr.br.x, bestr.tl.y, r.br.x - bestr.br.x, |
| 385 | bestr.height()); |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 386 | writeRect(sr, _ig, NULL); |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 387 | } |
| 388 | if (bestr.br.y != r.br.y) { |
| 389 | sr.setXYWH(x, bestr.br.y, w, r.br.y - bestr.br.y); |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 390 | writeRect(sr, _ig, NULL); |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | return true; |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | // No suitable solid-color rectangles found. |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 399 | sendRectSimple(r); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 400 | return true; |
| 401 | } |
| 402 | |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 403 | void TightEncoder::writeSubrect(const Rect& r, bool forceSolid) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 404 | { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 405 | mos.clear(); |
| 406 | |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 407 | switch (clientpf.bpp) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 408 | case 8: |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 409 | tightEncode8(r, &mos, forceSolid); break; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 410 | case 16: |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 411 | tightEncode16(r, &mos, forceSolid); break; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 412 | case 32: |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 413 | tightEncode32(r, &mos, forceSolid); break; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | writer->startRect(r, encodingTight); |
| 417 | rdr::OutStream* os = writer->getOutStream(); |
| 418 | os->writeBytes(mos.data(), mos.length()); |
| 419 | writer->endRect(); |
| 420 | } |