Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2000-2003 Constantin Kaplinsky. All Rights Reserved. |
Peter Åstrand | d69bcc4 | 2011-09-28 12:52:53 +0000 | [diff] [blame] | 2 | * Copyright 2004-2005 Cendio AB. |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 3 | * Copyright (C) 2011 D. R. Commander. All Rights Reserved. |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 4 | * |
| 5 | * This is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This software is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this software; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 18 | * USA. |
| 19 | */ |
| 20 | |
| 21 | // |
| 22 | // Tight decoding functions. |
| 23 | // |
Pierre Ossman | bcc295e | 2014-02-12 13:16:43 +0100 | [diff] [blame] | 24 | // This file is #included after having set the following macro: |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 25 | // BPP - 8, 16 or 32 |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 26 | |
| 27 | #include <rdr/InStream.h> |
| 28 | #include <rdr/ZlibInStream.h> |
| 29 | #include <rfb/Exception.h> |
Pierre Ossman | c039726 | 2014-03-14 15:59:46 +0100 | [diff] [blame] | 30 | #include <rfb/TightConstants.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 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 | #define PIXEL_T rdr::CONCAT2E(U,BPP) |
| 42 | #define READ_PIXEL CONCAT2E(readOpaque,BPP) |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 43 | #define TIGHT_DECODE TightDecoder::CONCAT2E(tightDecode,BPP) |
| 44 | #define DECOMPRESS_JPEG_RECT TightDecoder::CONCAT2E(DecompressJpegRect,BPP) |
| 45 | #define FILTER_GRADIENT TightDecoder::CONCAT2E(FilterGradient,BPP) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 46 | |
| 47 | #define TIGHT_MIN_TO_COMPRESS 12 |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 48 | |
| 49 | // Main function implementing Tight decoder |
| 50 | |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 51 | void TIGHT_DECODE (const Rect& r) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 52 | { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 53 | bool cutZeros = false; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 54 | #if BPP == 32 |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 55 | if (serverpf.is888()) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 56 | cutZeros = true; |
| 57 | } |
| 58 | #endif |
| 59 | |
| 60 | rdr::U8 comp_ctl = is->readU8(); |
| 61 | |
| 62 | // Flush zlib streams if we are told by the server to do so. |
| 63 | for (int i = 0; i < 4; i++) { |
| 64 | if (comp_ctl & 1) { |
| 65 | zis[i].reset(); |
| 66 | } |
| 67 | comp_ctl >>= 1; |
| 68 | } |
| 69 | |
| 70 | // "Fill" compression type. |
Pierre Ossman | c039726 | 2014-03-14 15:59:46 +0100 | [diff] [blame] | 71 | if (comp_ctl == tightFill) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 72 | PIXEL_T pix; |
| 73 | if (cutZeros) { |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 74 | rdr::U8 bytebuf[3]; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 75 | is->readBytes(bytebuf, 3); |
Pierre Ossman | b6b4dc6 | 2014-01-20 15:05:21 +0100 | [diff] [blame] | 76 | serverpf.bufferFromRGB((rdr::U8*)&pix, bytebuf, 1); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 77 | } else { |
| 78 | pix = is->READ_PIXEL(); |
| 79 | } |
Pierre Ossman | 56f99d6 | 2015-06-05 12:57:02 +0200 | [diff] [blame] | 80 | pb->fillRect(serverpf, r, &pix); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 81 | return; |
| 82 | } |
| 83 | |
| 84 | // "JPEG" compression type. |
Pierre Ossman | c039726 | 2014-03-14 15:59:46 +0100 | [diff] [blame] | 85 | if (comp_ctl == tightJpeg) { |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 86 | DECOMPRESS_JPEG_RECT(r); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 87 | return; |
| 88 | } |
| 89 | |
| 90 | // Quit on unsupported compression type. |
Pierre Ossman | c039726 | 2014-03-14 15:59:46 +0100 | [diff] [blame] | 91 | if (comp_ctl > tightMaxSubencoding) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 92 | throw Exception("TightDecoder: bad subencoding value received"); |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | // "Basic" compression type. |
| 97 | int palSize = 0; |
| 98 | static PIXEL_T palette[256]; |
| 99 | bool useGradient = false; |
| 100 | |
Pierre Ossman | c039726 | 2014-03-14 15:59:46 +0100 | [diff] [blame] | 101 | if ((comp_ctl & tightExplicitFilter) != 0) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 102 | rdr::U8 filterId = is->readU8(); |
| 103 | |
| 104 | switch (filterId) { |
Pierre Ossman | c039726 | 2014-03-14 15:59:46 +0100 | [diff] [blame] | 105 | case tightFilterPalette: |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 106 | palSize = is->readU8() + 1; |
| 107 | if (cutZeros) { |
DRC | a5004a3 | 2011-11-04 04:51:17 +0000 | [diff] [blame] | 108 | rdr::U8 tightPalette[256 * 3]; |
| 109 | is->readBytes(tightPalette, palSize * 3); |
Pierre Ossman | b6b4dc6 | 2014-01-20 15:05:21 +0100 | [diff] [blame] | 110 | serverpf.bufferFromRGB((rdr::U8*)palette, tightPalette, palSize); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 111 | } else { |
DRC | a5004a3 | 2011-11-04 04:51:17 +0000 | [diff] [blame] | 112 | is->readBytes(palette, palSize * sizeof(PIXEL_T)); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 113 | } |
| 114 | break; |
Pierre Ossman | c039726 | 2014-03-14 15:59:46 +0100 | [diff] [blame] | 115 | case tightFilterGradient: |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 116 | useGradient = true; |
| 117 | break; |
Pierre Ossman | c039726 | 2014-03-14 15:59:46 +0100 | [diff] [blame] | 118 | case tightFilterCopy: |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 119 | break; |
| 120 | default: |
| 121 | throw Exception("TightDecoder: unknown filter code received"); |
| 122 | return; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | int bppp = BPP; |
| 127 | if (palSize != 0) { |
| 128 | bppp = (palSize <= 2) ? 1 : 8; |
| 129 | } else if (cutZeros) { |
| 130 | bppp = 24; |
| 131 | } |
| 132 | |
| 133 | // Determine if the data should be decompressed or just copied. |
| 134 | int rowSize = (r.width() * bppp + 7) / 8; |
| 135 | int dataSize = r.height() * rowSize; |
| 136 | int streamId = -1; |
| 137 | rdr::InStream *input; |
| 138 | if (dataSize < TIGHT_MIN_TO_COMPRESS) { |
| 139 | input = is; |
| 140 | } else { |
Pierre Ossman | 7b5c069 | 2014-03-17 14:35:51 +0100 | [diff] [blame] | 141 | int length = readCompact(is); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 142 | streamId = comp_ctl & 0x03; |
| 143 | zis[streamId].setUnderlying(is, length); |
| 144 | input = &zis[streamId]; |
| 145 | } |
| 146 | |
DRC | a5004a3 | 2011-11-04 04:51:17 +0000 | [diff] [blame] | 147 | // Allocate netbuf and read in data |
| 148 | rdr::U8 *netbuf = new rdr::U8[dataSize]; |
| 149 | if (!netbuf) { |
| 150 | throw Exception("rfb::TightDecoder::tightDecode unable to allocate buffer"); |
| 151 | } |
| 152 | input->readBytes(netbuf, dataSize); |
| 153 | |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 154 | PIXEL_T *buf; |
| 155 | int stride = r.width(); |
Pierre Ossman | 8635062 | 2015-11-10 13:02:12 +0100 | [diff] [blame] | 156 | if (directDecode) |
| 157 | buf = (PIXEL_T *)pb->getBufferRW(r, &stride); |
| 158 | else |
| 159 | buf = new PIXEL_T[r.area()]; |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 160 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 161 | if (palSize == 0) { |
| 162 | // Truecolor data |
| 163 | if (useGradient) { |
| 164 | #if BPP == 32 |
| 165 | if (cutZeros) { |
DRC | a5004a3 | 2011-11-04 04:51:17 +0000 | [diff] [blame] | 166 | FilterGradient24(netbuf, buf, stride, r); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 167 | } else |
| 168 | #endif |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 169 | { |
DRC | a5004a3 | 2011-11-04 04:51:17 +0000 | [diff] [blame] | 170 | FILTER_GRADIENT(netbuf, buf, stride, r); |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 171 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 172 | } else { |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 173 | // Copy |
| 174 | int h = r.height(); |
| 175 | PIXEL_T *ptr = buf; |
DRC | a5004a3 | 2011-11-04 04:51:17 +0000 | [diff] [blame] | 176 | rdr::U8 *srcPtr = netbuf; |
| 177 | int w = r.width(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 178 | if (cutZeros) { |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 179 | while (h > 0) { |
Pierre Ossman | b6b4dc6 | 2014-01-20 15:05:21 +0100 | [diff] [blame] | 180 | serverpf.bufferFromRGB((rdr::U8*)ptr, srcPtr, w); |
DRC | a5004a3 | 2011-11-04 04:51:17 +0000 | [diff] [blame] | 181 | ptr += stride; |
Pierre Ossman | e3cb4a2 | 2011-11-08 13:52:33 +0000 | [diff] [blame] | 182 | srcPtr += w * 3; |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 183 | h--; |
Pierre Ossman | 7c4efd7 | 2010-09-30 11:30:20 +0000 | [diff] [blame] | 184 | } |
| 185 | } else { |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 186 | while (h > 0) { |
DRC | 77b5028 | 2011-11-09 18:18:11 +0000 | [diff] [blame] | 187 | memcpy(ptr, srcPtr, w * sizeof(PIXEL_T)); |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 188 | ptr += stride; |
DRC | a5004a3 | 2011-11-04 04:51:17 +0000 | [diff] [blame] | 189 | srcPtr += w * sizeof(PIXEL_T); |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 190 | h--; |
| 191 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | } else { |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 195 | // Indexed color |
| 196 | int x, h = r.height(), w = r.width(), b, pad = stride - w; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 197 | PIXEL_T *ptr = buf; |
DRC | a5004a3 | 2011-11-04 04:51:17 +0000 | [diff] [blame] | 198 | rdr::U8 bits, *srcPtr = netbuf; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 199 | if (palSize <= 2) { |
| 200 | // 2-color palette |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 201 | while (h > 0) { |
| 202 | for (x = 0; x < w / 8; x++) { |
DRC | a5004a3 | 2011-11-04 04:51:17 +0000 | [diff] [blame] | 203 | bits = *srcPtr++; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 204 | for (b = 7; b >= 0; b--) { |
| 205 | *ptr++ = palette[bits >> b & 1]; |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 206 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 207 | } |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 208 | if (w % 8 != 0) { |
DRC | a5004a3 | 2011-11-04 04:51:17 +0000 | [diff] [blame] | 209 | bits = *srcPtr++; |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 210 | for (b = 7; b >= 8 - w % 8; b--) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 211 | *ptr++ = palette[bits >> b & 1]; |
| 212 | } |
| 213 | } |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 214 | ptr += pad; |
| 215 | h--; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 216 | } |
| 217 | } else { |
| 218 | // 256-color palette |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 219 | while (h > 0) { |
| 220 | PIXEL_T *endOfRow = ptr + w; |
| 221 | while (ptr < endOfRow) { |
DRC | a5004a3 | 2011-11-04 04:51:17 +0000 | [diff] [blame] | 222 | *ptr++ = palette[*srcPtr++]; |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 223 | } |
| 224 | ptr += pad; |
| 225 | h--; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
Pierre Ossman | 8635062 | 2015-11-10 13:02:12 +0100 | [diff] [blame] | 230 | if (directDecode) |
| 231 | pb->commitBufferRW(r); |
| 232 | else { |
| 233 | pb->imageRect(serverpf, r, buf); |
| 234 | delete [] buf; |
| 235 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 236 | |
DRC | a5004a3 | 2011-11-04 04:51:17 +0000 | [diff] [blame] | 237 | delete [] netbuf; |
| 238 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 239 | if (streamId != -1) { |
| 240 | zis[streamId].reset(); |
| 241 | } |
| 242 | } |
| 243 | |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 244 | void |
| 245 | DECOMPRESS_JPEG_RECT(const Rect& r) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 246 | { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 247 | // Read length |
Pierre Ossman | 7b5c069 | 2014-03-17 14:35:51 +0100 | [diff] [blame] | 248 | int compressedLen = readCompact(is); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 249 | if (compressedLen <= 0) { |
| 250 | throw Exception("Incorrect data received from the server.\n"); |
| 251 | } |
| 252 | |
| 253 | // Allocate netbuf and read in data |
| 254 | rdr::U8* netbuf = new rdr::U8[compressedLen]; |
| 255 | if (!netbuf) { |
DRC | a5004a3 | 2011-11-04 04:51:17 +0000 | [diff] [blame] | 256 | throw Exception("rfb::TightDecoder::DecompressJpegRect unable to allocate buffer"); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 257 | } |
| 258 | is->readBytes(netbuf, compressedLen); |
| 259 | |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 260 | // We always use direct decoding with JPEG images |
| 261 | int stride; |
Pierre Ossman | 0c9bd4b | 2014-07-09 16:44:11 +0200 | [diff] [blame] | 262 | rdr::U8 *buf = pb->getBufferRW(r, &stride); |
Pierre Ossman | a10d8fe | 2014-01-22 11:28:05 +0100 | [diff] [blame] | 263 | jd.decompress(netbuf, compressedLen, buf, stride, r, clientpf); |
Pierre Ossman | 0c9bd4b | 2014-07-09 16:44:11 +0200 | [diff] [blame] | 264 | pb->commitBufferRW(r); |
DRC | e420dcb | 2009-04-06 07:20:34 +0000 | [diff] [blame] | 265 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 266 | delete [] netbuf; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | #if BPP == 32 |
| 270 | |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 271 | void |
DRC | a5004a3 | 2011-11-04 04:51:17 +0000 | [diff] [blame] | 272 | TightDecoder::FilterGradient24(rdr::U8 *netbuf, PIXEL_T* buf, int stride, |
| 273 | const Rect& r) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 274 | { |
| 275 | int x, y, c; |
| 276 | static rdr::U8 prevRow[TIGHT_MAX_WIDTH*3]; |
| 277 | static rdr::U8 thisRow[TIGHT_MAX_WIDTH*3]; |
| 278 | rdr::U8 pix[3]; |
| 279 | int est[3]; |
| 280 | |
| 281 | memset(prevRow, 0, sizeof(prevRow)); |
| 282 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 283 | // Set up shortcut variables |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 284 | int rectHeight = r.height(); |
| 285 | int rectWidth = r.width(); |
| 286 | |
| 287 | for (y = 0; y < rectHeight; y++) { |
| 288 | /* First pixel in a row */ |
| 289 | for (c = 0; c < 3; c++) { |
| 290 | pix[c] = netbuf[y*rectWidth*3+c] + prevRow[c]; |
| 291 | thisRow[c] = pix[c]; |
| 292 | } |
Pierre Ossman | b6b4dc6 | 2014-01-20 15:05:21 +0100 | [diff] [blame] | 293 | serverpf.bufferFromRGB((rdr::U8*)&buf[y*stride], pix, 1); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 294 | |
| 295 | /* Remaining pixels of a row */ |
| 296 | for (x = 1; x < rectWidth; x++) { |
| 297 | for (c = 0; c < 3; c++) { |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 298 | est[c] = prevRow[x*3+c] + pix[c] - prevRow[(x-1)*3+c]; |
| 299 | if (est[c] > 0xff) { |
| 300 | est[c] = 0xff; |
| 301 | } else if (est[c] < 0) { |
| 302 | est[c] = 0; |
| 303 | } |
| 304 | pix[c] = netbuf[(y*rectWidth+x)*3+c] + est[c]; |
| 305 | thisRow[x*3+c] = pix[c]; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 306 | } |
Pierre Ossman | b6b4dc6 | 2014-01-20 15:05:21 +0100 | [diff] [blame] | 307 | serverpf.bufferFromRGB((rdr::U8*)&buf[y*stride+x], pix, 1); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | memcpy(prevRow, thisRow, sizeof(prevRow)); |
| 311 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | #endif |
| 315 | |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 316 | void |
DRC | a5004a3 | 2011-11-04 04:51:17 +0000 | [diff] [blame] | 317 | FILTER_GRADIENT(rdr::U8 *netbuf, PIXEL_T* buf, int stride, const Rect& r) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 318 | { |
| 319 | int x, y, c; |
| 320 | static rdr::U8 prevRow[TIGHT_MAX_WIDTH*sizeof(PIXEL_T)]; |
| 321 | static rdr::U8 thisRow[TIGHT_MAX_WIDTH*sizeof(PIXEL_T)]; |
Pierre Ossman | 67b2b2f | 2009-03-06 10:12:55 +0000 | [diff] [blame] | 322 | rdr::U8 pix[3]; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 323 | int est[3]; |
| 324 | |
| 325 | memset(prevRow, 0, sizeof(prevRow)); |
| 326 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 327 | // Set up shortcut variables |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 328 | int rectHeight = r.height(); |
| 329 | int rectWidth = r.width(); |
| 330 | |
| 331 | for (y = 0; y < rectHeight; y++) { |
| 332 | /* First pixel in a row */ |
Pierre Ossman | b6b4dc6 | 2014-01-20 15:05:21 +0100 | [diff] [blame] | 333 | serverpf.rgbFromBuffer(pix, (rdr::U8*)&netbuf[y*rectWidth], 1); |
Pierre Ossman | 67b2b2f | 2009-03-06 10:12:55 +0000 | [diff] [blame] | 334 | for (c = 0; c < 3; c++) |
| 335 | pix[c] += prevRow[c]; |
| 336 | |
| 337 | memcpy(thisRow, pix, sizeof(pix)); |
| 338 | |
Pierre Ossman | b6b4dc6 | 2014-01-20 15:05:21 +0100 | [diff] [blame] | 339 | serverpf.bufferFromRGB((rdr::U8*)&buf[y*stride], pix, 1); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 340 | |
| 341 | /* Remaining pixels of a row */ |
| 342 | for (x = 1; x < rectWidth; x++) { |
| 343 | for (c = 0; c < 3; c++) { |
Pierre Ossman | 67b2b2f | 2009-03-06 10:12:55 +0000 | [diff] [blame] | 344 | est[c] = prevRow[x*3+c] + pix[c] - prevRow[(x-1)*3+c]; |
| 345 | if (est[c] > 255) { |
| 346 | est[c] = 255; |
| 347 | } else if (est[c] < 0) { |
| 348 | est[c] = 0; |
| 349 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 350 | } |
Pierre Ossman | 67b2b2f | 2009-03-06 10:12:55 +0000 | [diff] [blame] | 351 | |
Pierre Ossman | b6b4dc6 | 2014-01-20 15:05:21 +0100 | [diff] [blame] | 352 | serverpf.rgbFromBuffer(pix, (rdr::U8*)&netbuf[y*rectWidth+x], 1); |
Pierre Ossman | 67b2b2f | 2009-03-06 10:12:55 +0000 | [diff] [blame] | 353 | for (c = 0; c < 3; c++) |
| 354 | pix[c] += est[c]; |
| 355 | |
| 356 | memcpy(&thisRow[x*3], pix, sizeof(pix)); |
| 357 | |
Pierre Ossman | b6b4dc6 | 2014-01-20 15:05:21 +0100 | [diff] [blame] | 358 | serverpf.bufferFromRGB((rdr::U8*)&buf[y*stride+x], pix, 1); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | memcpy(prevRow, thisRow, sizeof(prevRow)); |
| 362 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | #undef TIGHT_MIN_TO_COMPRESS |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 366 | #undef FILTER_GRADIENT |
| 367 | #undef DECOMPRESS_JPEG_RECT |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 368 | #undef TIGHT_DECODE |
| 369 | #undef READ_PIXEL |
| 370 | #undef PIXEL_T |
| 371 | } |