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