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 | 0c9bd4b | 2014-07-09 16:44:11 +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 | 0c9bd4b | 2014-07-09 16:44:11 +0200 | [diff] [blame] | 156 | if (directDecode) buf = (PIXEL_T *)pb->getBufferRW(r, &stride); |
Pierre Ossman | 668468b | 2014-01-31 12:37:32 +0100 | [diff] [blame] | 157 | else buf = (PIXEL_T *)conn->reader()->getImageBuf(r.area()); |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 158 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 159 | if (palSize == 0) { |
| 160 | // Truecolor data |
| 161 | if (useGradient) { |
| 162 | #if BPP == 32 |
| 163 | if (cutZeros) { |
DRC | a5004a3 | 2011-11-04 04:51:17 +0000 | [diff] [blame] | 164 | FilterGradient24(netbuf, buf, stride, r); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 165 | } else |
| 166 | #endif |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 167 | { |
DRC | a5004a3 | 2011-11-04 04:51:17 +0000 | [diff] [blame] | 168 | FILTER_GRADIENT(netbuf, buf, stride, r); |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 169 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 170 | } else { |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 171 | // Copy |
| 172 | int h = r.height(); |
| 173 | PIXEL_T *ptr = buf; |
DRC | a5004a3 | 2011-11-04 04:51:17 +0000 | [diff] [blame] | 174 | rdr::U8 *srcPtr = netbuf; |
| 175 | int w = r.width(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 176 | if (cutZeros) { |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 177 | while (h > 0) { |
Pierre Ossman | b6b4dc6 | 2014-01-20 15:05:21 +0100 | [diff] [blame] | 178 | serverpf.bufferFromRGB((rdr::U8*)ptr, srcPtr, w); |
DRC | a5004a3 | 2011-11-04 04:51:17 +0000 | [diff] [blame] | 179 | ptr += stride; |
Pierre Ossman | e3cb4a2 | 2011-11-08 13:52:33 +0000 | [diff] [blame] | 180 | srcPtr += w * 3; |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 181 | h--; |
Pierre Ossman | 7c4efd7 | 2010-09-30 11:30:20 +0000 | [diff] [blame] | 182 | } |
| 183 | } else { |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 184 | while (h > 0) { |
DRC | 77b5028 | 2011-11-09 18:18:11 +0000 | [diff] [blame] | 185 | memcpy(ptr, srcPtr, w * sizeof(PIXEL_T)); |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 186 | ptr += stride; |
DRC | a5004a3 | 2011-11-04 04:51:17 +0000 | [diff] [blame] | 187 | srcPtr += w * sizeof(PIXEL_T); |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 188 | h--; |
| 189 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | } else { |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 193 | // Indexed color |
| 194 | int x, h = r.height(), w = r.width(), b, pad = stride - w; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 195 | PIXEL_T *ptr = buf; |
DRC | a5004a3 | 2011-11-04 04:51:17 +0000 | [diff] [blame] | 196 | rdr::U8 bits, *srcPtr = netbuf; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 197 | if (palSize <= 2) { |
| 198 | // 2-color palette |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 199 | while (h > 0) { |
| 200 | for (x = 0; x < w / 8; x++) { |
DRC | a5004a3 | 2011-11-04 04:51:17 +0000 | [diff] [blame] | 201 | bits = *srcPtr++; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 202 | for (b = 7; b >= 0; b--) { |
| 203 | *ptr++ = palette[bits >> b & 1]; |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 204 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 205 | } |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 206 | if (w % 8 != 0) { |
DRC | a5004a3 | 2011-11-04 04:51:17 +0000 | [diff] [blame] | 207 | bits = *srcPtr++; |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 208 | for (b = 7; b >= 8 - w % 8; b--) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 209 | *ptr++ = palette[bits >> b & 1]; |
| 210 | } |
| 211 | } |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 212 | ptr += pad; |
| 213 | h--; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 214 | } |
| 215 | } else { |
| 216 | // 256-color palette |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 217 | while (h > 0) { |
| 218 | PIXEL_T *endOfRow = ptr + w; |
| 219 | while (ptr < endOfRow) { |
DRC | a5004a3 | 2011-11-04 04:51:17 +0000 | [diff] [blame] | 220 | *ptr++ = palette[*srcPtr++]; |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 221 | } |
| 222 | ptr += pad; |
| 223 | h--; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
Pierre Ossman | 0c9bd4b | 2014-07-09 16:44:11 +0200 | [diff] [blame] | 228 | if (directDecode) pb->commitBufferRW(r); |
| 229 | else pb->imageRect(serverpf, r, buf); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 230 | |
DRC | a5004a3 | 2011-11-04 04:51:17 +0000 | [diff] [blame] | 231 | delete [] netbuf; |
| 232 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 233 | if (streamId != -1) { |
| 234 | zis[streamId].reset(); |
| 235 | } |
| 236 | } |
| 237 | |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 238 | void |
| 239 | DECOMPRESS_JPEG_RECT(const Rect& r) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 240 | { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 241 | // Read length |
Pierre Ossman | 7b5c069 | 2014-03-17 14:35:51 +0100 | [diff] [blame] | 242 | int compressedLen = readCompact(is); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 243 | if (compressedLen <= 0) { |
| 244 | throw Exception("Incorrect data received from the server.\n"); |
| 245 | } |
| 246 | |
| 247 | // Allocate netbuf and read in data |
| 248 | rdr::U8* netbuf = new rdr::U8[compressedLen]; |
| 249 | if (!netbuf) { |
DRC | a5004a3 | 2011-11-04 04:51:17 +0000 | [diff] [blame] | 250 | throw Exception("rfb::TightDecoder::DecompressJpegRect unable to allocate buffer"); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 251 | } |
| 252 | is->readBytes(netbuf, compressedLen); |
| 253 | |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 254 | // We always use direct decoding with JPEG images |
| 255 | int stride; |
Pierre Ossman | 0c9bd4b | 2014-07-09 16:44:11 +0200 | [diff] [blame] | 256 | rdr::U8 *buf = pb->getBufferRW(r, &stride); |
Pierre Ossman | a10d8fe | 2014-01-22 11:28:05 +0100 | [diff] [blame] | 257 | jd.decompress(netbuf, compressedLen, buf, stride, r, clientpf); |
Pierre Ossman | 0c9bd4b | 2014-07-09 16:44:11 +0200 | [diff] [blame] | 258 | pb->commitBufferRW(r); |
DRC | e420dcb | 2009-04-06 07:20:34 +0000 | [diff] [blame] | 259 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 260 | delete [] netbuf; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | #if BPP == 32 |
| 264 | |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 265 | void |
DRC | a5004a3 | 2011-11-04 04:51:17 +0000 | [diff] [blame] | 266 | TightDecoder::FilterGradient24(rdr::U8 *netbuf, PIXEL_T* buf, int stride, |
| 267 | const Rect& r) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 268 | { |
| 269 | int x, y, c; |
| 270 | static rdr::U8 prevRow[TIGHT_MAX_WIDTH*3]; |
| 271 | static rdr::U8 thisRow[TIGHT_MAX_WIDTH*3]; |
| 272 | rdr::U8 pix[3]; |
| 273 | int est[3]; |
| 274 | |
| 275 | memset(prevRow, 0, sizeof(prevRow)); |
| 276 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 277 | // Set up shortcut variables |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 278 | int rectHeight = r.height(); |
| 279 | int rectWidth = r.width(); |
| 280 | |
| 281 | for (y = 0; y < rectHeight; y++) { |
| 282 | /* First pixel in a row */ |
| 283 | for (c = 0; c < 3; c++) { |
| 284 | pix[c] = netbuf[y*rectWidth*3+c] + prevRow[c]; |
| 285 | thisRow[c] = pix[c]; |
| 286 | } |
Pierre Ossman | b6b4dc6 | 2014-01-20 15:05:21 +0100 | [diff] [blame] | 287 | serverpf.bufferFromRGB((rdr::U8*)&buf[y*stride], pix, 1); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 288 | |
| 289 | /* Remaining pixels of a row */ |
| 290 | for (x = 1; x < rectWidth; x++) { |
| 291 | for (c = 0; c < 3; c++) { |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 292 | est[c] = prevRow[x*3+c] + pix[c] - prevRow[(x-1)*3+c]; |
| 293 | if (est[c] > 0xff) { |
| 294 | est[c] = 0xff; |
| 295 | } else if (est[c] < 0) { |
| 296 | est[c] = 0; |
| 297 | } |
| 298 | pix[c] = netbuf[(y*rectWidth+x)*3+c] + est[c]; |
| 299 | thisRow[x*3+c] = pix[c]; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 300 | } |
Pierre Ossman | b6b4dc6 | 2014-01-20 15:05:21 +0100 | [diff] [blame] | 301 | serverpf.bufferFromRGB((rdr::U8*)&buf[y*stride+x], pix, 1); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | memcpy(prevRow, thisRow, sizeof(prevRow)); |
| 305 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | #endif |
| 309 | |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 310 | void |
DRC | a5004a3 | 2011-11-04 04:51:17 +0000 | [diff] [blame] | 311 | 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] | 312 | { |
| 313 | int x, y, c; |
| 314 | static rdr::U8 prevRow[TIGHT_MAX_WIDTH*sizeof(PIXEL_T)]; |
| 315 | static rdr::U8 thisRow[TIGHT_MAX_WIDTH*sizeof(PIXEL_T)]; |
Pierre Ossman | 67b2b2f | 2009-03-06 10:12:55 +0000 | [diff] [blame] | 316 | rdr::U8 pix[3]; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 317 | int est[3]; |
| 318 | |
| 319 | memset(prevRow, 0, sizeof(prevRow)); |
| 320 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 321 | // Set up shortcut variables |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 322 | int rectHeight = r.height(); |
| 323 | int rectWidth = r.width(); |
| 324 | |
| 325 | for (y = 0; y < rectHeight; y++) { |
| 326 | /* First pixel in a row */ |
Pierre Ossman | b6b4dc6 | 2014-01-20 15:05:21 +0100 | [diff] [blame] | 327 | serverpf.rgbFromBuffer(pix, (rdr::U8*)&netbuf[y*rectWidth], 1); |
Pierre Ossman | 67b2b2f | 2009-03-06 10:12:55 +0000 | [diff] [blame] | 328 | for (c = 0; c < 3; c++) |
| 329 | pix[c] += prevRow[c]; |
| 330 | |
| 331 | memcpy(thisRow, pix, sizeof(pix)); |
| 332 | |
Pierre Ossman | b6b4dc6 | 2014-01-20 15:05:21 +0100 | [diff] [blame] | 333 | serverpf.bufferFromRGB((rdr::U8*)&buf[y*stride], pix, 1); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 334 | |
| 335 | /* Remaining pixels of a row */ |
| 336 | for (x = 1; x < rectWidth; x++) { |
| 337 | for (c = 0; c < 3; c++) { |
Pierre Ossman | 67b2b2f | 2009-03-06 10:12:55 +0000 | [diff] [blame] | 338 | est[c] = prevRow[x*3+c] + pix[c] - prevRow[(x-1)*3+c]; |
| 339 | if (est[c] > 255) { |
| 340 | est[c] = 255; |
| 341 | } else if (est[c] < 0) { |
| 342 | est[c] = 0; |
| 343 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 344 | } |
Pierre Ossman | 67b2b2f | 2009-03-06 10:12:55 +0000 | [diff] [blame] | 345 | |
Pierre Ossman | b6b4dc6 | 2014-01-20 15:05:21 +0100 | [diff] [blame] | 346 | serverpf.rgbFromBuffer(pix, (rdr::U8*)&netbuf[y*rectWidth+x], 1); |
Pierre Ossman | 67b2b2f | 2009-03-06 10:12:55 +0000 | [diff] [blame] | 347 | for (c = 0; c < 3; c++) |
| 348 | pix[c] += est[c]; |
| 349 | |
| 350 | memcpy(&thisRow[x*3], pix, sizeof(pix)); |
| 351 | |
Pierre Ossman | b6b4dc6 | 2014-01-20 15:05:21 +0100 | [diff] [blame] | 352 | serverpf.bufferFromRGB((rdr::U8*)&buf[y*stride+x], pix, 1); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | memcpy(prevRow, thisRow, sizeof(prevRow)); |
| 356 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | #undef TIGHT_MIN_TO_COMPRESS |
DRC | 33c15e3 | 2011-11-03 18:49:21 +0000 | [diff] [blame] | 360 | #undef FILTER_GRADIENT |
| 361 | #undef DECOMPRESS_JPEG_RECT |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 362 | #undef TIGHT_DECODE |
| 363 | #undef READ_PIXEL |
| 364 | #undef PIXEL_T |
| 365 | } |