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