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