blob: 88fda73b81657dcc4c8a39fba2a5372ce0c21123 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2000-2003 Constantin Kaplinsky. All Rights Reserved.
Peter Åstrandd69bcc42011-09-28 12:52:53 +00002 * Copyright 2004-2005 Cendio AB.
DRC33c15e32011-11-03 18:49:21 +00003 * Copyright (C) 2011 D. R. Commander. All Rights Reserved.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00004 *
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 Ossmanbcc295e2014-02-12 13:16:43 +010024// This file is #included after having set the following macro:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000025// BPP - 8, 16 or 32
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000026
27#include <rdr/InStream.h>
28#include <rdr/ZlibInStream.h>
29#include <rfb/Exception.h>
Pierre Ossmanc0397262014-03-14 15:59:46 +010030#include <rfb/TightConstants.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000031
32namespace 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)
DRC33c15e32011-11-03 18:49:21 +000043#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 Kaplinskya2adc8d2006-05-25 05:01:55 +000046
47#define TIGHT_MIN_TO_COMPRESS 12
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000048
49// Main function implementing Tight decoder
50
DRC33c15e32011-11-03 18:49:21 +000051void TIGHT_DECODE (const Rect& r)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000052{
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000053 bool cutZeros = false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000054#if BPP == 32
DRC33c15e32011-11-03 18:49:21 +000055 if (serverpf.is888()) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000056 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 Ossmanc0397262014-03-14 15:59:46 +010071 if (comp_ctl == tightFill) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000072 PIXEL_T pix;
73 if (cutZeros) {
DRC33c15e32011-11-03 18:49:21 +000074 rdr::U8 bytebuf[3];
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000075 is->readBytes(bytebuf, 3);
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +010076 serverpf.bufferFromRGB((rdr::U8*)&pix, bytebuf, 1);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000077 } else {
78 pix = is->READ_PIXEL();
79 }
Pierre Ossman56f99d62015-06-05 12:57:02 +020080 pb->fillRect(serverpf, r, &pix);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000081 return;
82 }
83
84 // "JPEG" compression type.
Pierre Ossmanc0397262014-03-14 15:59:46 +010085 if (comp_ctl == tightJpeg) {
DRC33c15e32011-11-03 18:49:21 +000086 DECOMPRESS_JPEG_RECT(r);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000087 return;
88 }
89
90 // Quit on unsupported compression type.
Pierre Ossmanc0397262014-03-14 15:59:46 +010091 if (comp_ctl > tightMaxSubencoding) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000092 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 Ossmanc0397262014-03-14 15:59:46 +0100101 if ((comp_ctl & tightExplicitFilter) != 0) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000102 rdr::U8 filterId = is->readU8();
103
104 switch (filterId) {
Pierre Ossmanc0397262014-03-14 15:59:46 +0100105 case tightFilterPalette:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000106 palSize = is->readU8() + 1;
107 if (cutZeros) {
DRCa5004a32011-11-04 04:51:17 +0000108 rdr::U8 tightPalette[256 * 3];
109 is->readBytes(tightPalette, palSize * 3);
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +0100110 serverpf.bufferFromRGB((rdr::U8*)palette, tightPalette, palSize);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000111 } else {
DRCa5004a32011-11-04 04:51:17 +0000112 is->readBytes(palette, palSize * sizeof(PIXEL_T));
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000113 }
114 break;
Pierre Ossmanc0397262014-03-14 15:59:46 +0100115 case tightFilterGradient:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000116 useGradient = true;
117 break;
Pierre Ossmanc0397262014-03-14 15:59:46 +0100118 case tightFilterCopy:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000119 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 Ossman7b5c0692014-03-17 14:35:51 +0100141 int length = readCompact(is);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000142 streamId = comp_ctl & 0x03;
143 zis[streamId].setUnderlying(is, length);
144 input = &zis[streamId];
145 }
146
DRCa5004a32011-11-04 04:51:17 +0000147 // 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
DRC33c15e32011-11-03 18:49:21 +0000154 PIXEL_T *buf;
155 int stride = r.width();
Pierre Ossman86350622015-11-10 13:02:12 +0100156 if (directDecode)
157 buf = (PIXEL_T *)pb->getBufferRW(r, &stride);
158 else
159 buf = new PIXEL_T[r.area()];
DRC33c15e32011-11-03 18:49:21 +0000160
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000161 if (palSize == 0) {
162 // Truecolor data
163 if (useGradient) {
164#if BPP == 32
165 if (cutZeros) {
DRCa5004a32011-11-04 04:51:17 +0000166 FilterGradient24(netbuf, buf, stride, r);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000167 } else
168#endif
DRC33c15e32011-11-03 18:49:21 +0000169 {
DRCa5004a32011-11-04 04:51:17 +0000170 FILTER_GRADIENT(netbuf, buf, stride, r);
DRC33c15e32011-11-03 18:49:21 +0000171 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000172 } else {
DRC33c15e32011-11-03 18:49:21 +0000173 // Copy
174 int h = r.height();
175 PIXEL_T *ptr = buf;
DRCa5004a32011-11-04 04:51:17 +0000176 rdr::U8 *srcPtr = netbuf;
177 int w = r.width();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000178 if (cutZeros) {
DRC33c15e32011-11-03 18:49:21 +0000179 while (h > 0) {
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +0100180 serverpf.bufferFromRGB((rdr::U8*)ptr, srcPtr, w);
DRCa5004a32011-11-04 04:51:17 +0000181 ptr += stride;
Pierre Ossmane3cb4a22011-11-08 13:52:33 +0000182 srcPtr += w * 3;
DRC33c15e32011-11-03 18:49:21 +0000183 h--;
Pierre Ossman7c4efd72010-09-30 11:30:20 +0000184 }
185 } else {
DRC33c15e32011-11-03 18:49:21 +0000186 while (h > 0) {
DRC77b50282011-11-09 18:18:11 +0000187 memcpy(ptr, srcPtr, w * sizeof(PIXEL_T));
DRC33c15e32011-11-03 18:49:21 +0000188 ptr += stride;
DRCa5004a32011-11-04 04:51:17 +0000189 srcPtr += w * sizeof(PIXEL_T);
DRC33c15e32011-11-03 18:49:21 +0000190 h--;
191 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000192 }
193 }
194 } else {
DRC33c15e32011-11-03 18:49:21 +0000195 // Indexed color
196 int x, h = r.height(), w = r.width(), b, pad = stride - w;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000197 PIXEL_T *ptr = buf;
DRCa5004a32011-11-04 04:51:17 +0000198 rdr::U8 bits, *srcPtr = netbuf;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000199 if (palSize <= 2) {
200 // 2-color palette
DRC33c15e32011-11-03 18:49:21 +0000201 while (h > 0) {
202 for (x = 0; x < w / 8; x++) {
DRCa5004a32011-11-04 04:51:17 +0000203 bits = *srcPtr++;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000204 for (b = 7; b >= 0; b--) {
205 *ptr++ = palette[bits >> b & 1];
DRC33c15e32011-11-03 18:49:21 +0000206 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000207 }
DRC33c15e32011-11-03 18:49:21 +0000208 if (w % 8 != 0) {
DRCa5004a32011-11-04 04:51:17 +0000209 bits = *srcPtr++;
DRC33c15e32011-11-03 18:49:21 +0000210 for (b = 7; b >= 8 - w % 8; b--) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000211 *ptr++ = palette[bits >> b & 1];
212 }
213 }
DRC33c15e32011-11-03 18:49:21 +0000214 ptr += pad;
215 h--;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000216 }
217 } else {
218 // 256-color palette
DRC33c15e32011-11-03 18:49:21 +0000219 while (h > 0) {
220 PIXEL_T *endOfRow = ptr + w;
221 while (ptr < endOfRow) {
DRCa5004a32011-11-04 04:51:17 +0000222 *ptr++ = palette[*srcPtr++];
DRC33c15e32011-11-03 18:49:21 +0000223 }
224 ptr += pad;
225 h--;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000226 }
227 }
228 }
229
Pierre Ossman86350622015-11-10 13:02:12 +0100230 if (directDecode)
231 pb->commitBufferRW(r);
232 else {
233 pb->imageRect(serverpf, r, buf);
234 delete [] buf;
235 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000236
DRCa5004a32011-11-04 04:51:17 +0000237 delete [] netbuf;
238
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000239 if (streamId != -1) {
240 zis[streamId].reset();
241 }
242}
243
DRC33c15e32011-11-03 18:49:21 +0000244void
245DECOMPRESS_JPEG_RECT(const Rect& r)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000246{
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000247 // Read length
Pierre Ossman7b5c0692014-03-17 14:35:51 +0100248 int compressedLen = readCompact(is);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000249 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) {
DRCa5004a32011-11-04 04:51:17 +0000256 throw Exception("rfb::TightDecoder::DecompressJpegRect unable to allocate buffer");
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000257 }
258 is->readBytes(netbuf, compressedLen);
259
DRC33c15e32011-11-03 18:49:21 +0000260 // We always use direct decoding with JPEG images
261 int stride;
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +0200262 rdr::U8 *buf = pb->getBufferRW(r, &stride);
Pierre Ossmana10d8fe2014-01-22 11:28:05 +0100263 jd.decompress(netbuf, compressedLen, buf, stride, r, clientpf);
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +0200264 pb->commitBufferRW(r);
DRCe420dcb2009-04-06 07:20:34 +0000265
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000266 delete [] netbuf;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000267}
268
269#if BPP == 32
270
DRC33c15e32011-11-03 18:49:21 +0000271void
DRCa5004a32011-11-04 04:51:17 +0000272TightDecoder::FilterGradient24(rdr::U8 *netbuf, PIXEL_T* buf, int stride,
273 const Rect& r)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000274{
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 Kaplinskya2adc8d2006-05-25 05:01:55 +0000283 // Set up shortcut variables
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000284 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 Ossmanb6b4dc62014-01-20 15:05:21 +0100293 serverpf.bufferFromRGB((rdr::U8*)&buf[y*stride], pix, 1);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000294
295 /* Remaining pixels of a row */
296 for (x = 1; x < rectWidth; x++) {
297 for (c = 0; c < 3; c++) {
DRC33c15e32011-11-03 18:49:21 +0000298 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 Kaplinskya2adc8d2006-05-25 05:01:55 +0000306 }
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +0100307 serverpf.bufferFromRGB((rdr::U8*)&buf[y*stride+x], pix, 1);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000308 }
309
310 memcpy(prevRow, thisRow, sizeof(prevRow));
311 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000312}
313
314#endif
315
DRC33c15e32011-11-03 18:49:21 +0000316void
DRCa5004a32011-11-04 04:51:17 +0000317FILTER_GRADIENT(rdr::U8 *netbuf, PIXEL_T* buf, int stride, const Rect& r)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000318{
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 Ossman67b2b2f2009-03-06 10:12:55 +0000322 rdr::U8 pix[3];
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000323 int est[3];
324
325 memset(prevRow, 0, sizeof(prevRow));
326
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000327 // Set up shortcut variables
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000328 int rectHeight = r.height();
329 int rectWidth = r.width();
330
331 for (y = 0; y < rectHeight; y++) {
332 /* First pixel in a row */
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +0100333 serverpf.rgbFromBuffer(pix, (rdr::U8*)&netbuf[y*rectWidth], 1);
Pierre Ossman67b2b2f2009-03-06 10:12:55 +0000334 for (c = 0; c < 3; c++)
335 pix[c] += prevRow[c];
336
337 memcpy(thisRow, pix, sizeof(pix));
338
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +0100339 serverpf.bufferFromRGB((rdr::U8*)&buf[y*stride], pix, 1);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000340
341 /* Remaining pixels of a row */
342 for (x = 1; x < rectWidth; x++) {
343 for (c = 0; c < 3; c++) {
Pierre Ossman67b2b2f2009-03-06 10:12:55 +0000344 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 Kaplinskya2adc8d2006-05-25 05:01:55 +0000350 }
Pierre Ossman67b2b2f2009-03-06 10:12:55 +0000351
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +0100352 serverpf.rgbFromBuffer(pix, (rdr::U8*)&netbuf[y*rectWidth+x], 1);
Pierre Ossman67b2b2f2009-03-06 10:12:55 +0000353 for (c = 0; c < 3; c++)
354 pix[c] += est[c];
355
356 memcpy(&thisRow[x*3], pix, sizeof(pix));
357
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +0100358 serverpf.bufferFromRGB((rdr::U8*)&buf[y*stride+x], pix, 1);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000359 }
360
361 memcpy(prevRow, thisRow, sizeof(prevRow));
362 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000363}
364
365#undef TIGHT_MIN_TO_COMPRESS
DRC33c15e32011-11-03 18:49:21 +0000366#undef FILTER_GRADIENT
367#undef DECOMPRESS_JPEG_RECT
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000368#undef TIGHT_DECODE
369#undef READ_PIXEL
370#undef PIXEL_T
371}