blob: 32bb4f1537700039fe650bd959487d2563882fe3 [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//
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
35namespace 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)
DRC33c15e32011-11-03 18:49:21 +000046#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 Kaplinskya2adc8d2006-05-25 05:01:55 +000049
50#define TIGHT_MIN_TO_COMPRESS 12
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000051
52// Main function implementing Tight decoder
53
DRC33c15e32011-11-03 18:49:21 +000054void TIGHT_DECODE (const Rect& r)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000055{
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000056 bool cutZeros = false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000057#if BPP == 32
DRC33c15e32011-11-03 18:49:21 +000058 if (serverpf.is888()) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000059 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) {
DRC33c15e32011-11-03 18:49:21 +000077 rdr::U8 bytebuf[3];
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000078 is->readBytes(bytebuf, 3);
DRC33c15e32011-11-03 18:49:21 +000079 serverpf.bufferFromRGB((rdr::U8*)&pix, bytebuf, 1, NULL);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000080 } else {
81 pix = is->READ_PIXEL();
82 }
DRC4f24c1a2011-11-03 23:56:10 +000083 FILL_RECT(r, pix);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000084 return;
85 }
86
87 // "JPEG" compression type.
88 if (comp_ctl == rfbTightJpeg) {
DRC33c15e32011-11-03 18:49:21 +000089 DECOMPRESS_JPEG_RECT(r);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000090 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) {
Pierre Ossman7c4efd72010-09-30 11:30:20 +0000111 rdr::U8 elem[3];
112 for (int i = 0;i < palSize;i++) {
113 is->readBytes(elem, 3);
DRC33c15e32011-11-03 18:49:21 +0000114 serverpf.bufferFromRGB((rdr::U8*)&palette[i], elem, 1, NULL);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000115 }
116 } else {
DRC33c15e32011-11-03 18:49:21 +0000117 for (int i = 0; i < palSize; i++)
118 palette[i] = is->READ_PIXEL();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000119 }
120 break;
121 case rfbTightFilterGradient:
122 useGradient = true;
123 break;
124 case rfbTightFilterCopy:
125 break;
126 default:
127 throw Exception("TightDecoder: unknown filter code received");
128 return;
129 }
130 }
131
132 int bppp = BPP;
133 if (palSize != 0) {
134 bppp = (palSize <= 2) ? 1 : 8;
135 } else if (cutZeros) {
136 bppp = 24;
137 }
138
139 // Determine if the data should be decompressed or just copied.
140 int rowSize = (r.width() * bppp + 7) / 8;
141 int dataSize = r.height() * rowSize;
142 int streamId = -1;
143 rdr::InStream *input;
144 if (dataSize < TIGHT_MIN_TO_COMPRESS) {
145 input = is;
146 } else {
147 int length = is->readCompactLength();
148 streamId = comp_ctl & 0x03;
149 zis[streamId].setUnderlying(is, length);
150 input = &zis[streamId];
151 }
152
DRC33c15e32011-11-03 18:49:21 +0000153 PIXEL_T *buf;
154 int stride = r.width();
155 if (directDecode) buf = (PIXEL_T *)handler->getRawPixelsRW(r, &stride);
156 else buf = (PIXEL_T *)reader->getImageBuf(r.area());
157
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000158 if (palSize == 0) {
159 // Truecolor data
160 if (useGradient) {
161#if BPP == 32
162 if (cutZeros) {
DRC33c15e32011-11-03 18:49:21 +0000163 FilterGradient24(input, buf, stride, r, dataSize);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000164 } else
165#endif
DRC33c15e32011-11-03 18:49:21 +0000166 {
167 FILTER_GRADIENT(input, buf, stride, r, dataSize);
168 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000169 } else {
DRC33c15e32011-11-03 18:49:21 +0000170 // Copy
171 int h = r.height();
172 PIXEL_T *ptr = buf;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000173 if (cutZeros) {
DRC33c15e32011-11-03 18:49:21 +0000174 int w = r.width(), pad = stride - w;
Pierre Ossman7c4efd72010-09-30 11:30:20 +0000175 rdr::U8 elem[3];
DRC33c15e32011-11-03 18:49:21 +0000176 while (h > 0) {
177 PIXEL_T *endOfRow = ptr + w;
178 while (ptr < endOfRow) {
179 input->readBytes(elem, 3);
180 serverpf.bufferFromRGB((rdr::U8*)ptr++, elem, 1, NULL);
181 }
182 ptr += pad;
183 h--;
Pierre Ossman7c4efd72010-09-30 11:30:20 +0000184 }
185 } else {
DRC33c15e32011-11-03 18:49:21 +0000186 while (h > 0) {
187 input->readBytes(ptr, rowSize);
188 ptr += stride;
189 h--;
190 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000191 }
192 }
193 } else {
DRC33c15e32011-11-03 18:49:21 +0000194 // Indexed color
195 int x, h = r.height(), w = r.width(), b, pad = stride - w;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000196 PIXEL_T *ptr = buf;
197 rdr::U8 bits;
198 if (palSize <= 2) {
199 // 2-color palette
DRC33c15e32011-11-03 18:49:21 +0000200 while (h > 0) {
201 for (x = 0; x < w / 8; x++) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000202 bits = input->readU8();
203 for (b = 7; b >= 0; b--) {
204 *ptr++ = palette[bits >> b & 1];
DRC33c15e32011-11-03 18:49:21 +0000205 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000206 }
DRC33c15e32011-11-03 18:49:21 +0000207 if (w % 8 != 0) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000208 bits = input->readU8();
DRC33c15e32011-11-03 18:49:21 +0000209 for (b = 7; b >= 8 - w % 8; b--) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000210 *ptr++ = palette[bits >> b & 1];
211 }
212 }
DRC33c15e32011-11-03 18:49:21 +0000213 ptr += pad;
214 h--;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000215 }
216 } else {
217 // 256-color palette
DRC33c15e32011-11-03 18:49:21 +0000218 while (h > 0) {
219 PIXEL_T *endOfRow = ptr + w;
220 while (ptr < endOfRow) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000221 *ptr++ = palette[input->readU8()];
DRC33c15e32011-11-03 18:49:21 +0000222 }
223 ptr += pad;
224 h--;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000225 }
226 }
227 }
228
DRC33c15e32011-11-03 18:49:21 +0000229 if (directDecode) handler->releaseRawPixels(r);
230 else IMAGE_RECT(r, buf);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000231
232 if (streamId != -1) {
233 zis[streamId].reset();
234 }
235}
236
DRC33c15e32011-11-03 18:49:21 +0000237void
238DECOMPRESS_JPEG_RECT(const Rect& r)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000239{
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000240 // Read length
241 int compressedLen = is->readCompactLength();
242 if (compressedLen <= 0) {
243 throw Exception("Incorrect data received from the server.\n");
244 }
245
246 // Allocate netbuf and read in data
247 rdr::U8* netbuf = new rdr::U8[compressedLen];
248 if (!netbuf) {
249 throw Exception("rfb::tightDecode unable to allocate buffer");
250 }
251 is->readBytes(netbuf, compressedLen);
252
DRC33c15e32011-11-03 18:49:21 +0000253 // We always use direct decoding with JPEG images
254 int stride;
255 rdr::U8 *buf = handler->getRawPixelsRW(r, &stride);
256 jd.decompress(netbuf, compressedLen, buf, stride * clientpf.bpp / 8, r,
257 clientpf);
258 handler->releaseRawPixels(r);
DRCe420dcb2009-04-06 07:20:34 +0000259
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000260 delete [] netbuf;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000261}
262
263#if BPP == 32
264
DRC33c15e32011-11-03 18:49:21 +0000265void
266TightDecoder::FilterGradient24(rdr::InStream* is, PIXEL_T* buf, int stride,
267 const Rect& r, int dataSize)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000268{
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
277 // Allocate netbuf and read in data
278 rdr::U8 *netbuf = new rdr::U8[dataSize];
279 if (!netbuf) {
280 throw Exception("rfb::tightDecode unable to allocate buffer");
281 }
282 is->readBytes(netbuf, dataSize);
283
284 // Set up shortcut variables
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000285 int rectHeight = r.height();
286 int rectWidth = r.width();
287
288 for (y = 0; y < rectHeight; y++) {
289 /* First pixel in a row */
290 for (c = 0; c < 3; c++) {
291 pix[c] = netbuf[y*rectWidth*3+c] + prevRow[c];
292 thisRow[c] = pix[c];
293 }
DRC33c15e32011-11-03 18:49:21 +0000294 serverpf.bufferFromRGB((rdr::U8*)&buf[y*stride], pix, 1, NULL);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000295
296 /* Remaining pixels of a row */
297 for (x = 1; x < rectWidth; x++) {
298 for (c = 0; c < 3; c++) {
DRC33c15e32011-11-03 18:49:21 +0000299 est[c] = prevRow[x*3+c] + pix[c] - prevRow[(x-1)*3+c];
300 if (est[c] > 0xff) {
301 est[c] = 0xff;
302 } else if (est[c] < 0) {
303 est[c] = 0;
304 }
305 pix[c] = netbuf[(y*rectWidth+x)*3+c] + est[c];
306 thisRow[x*3+c] = pix[c];
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000307 }
DRC33c15e32011-11-03 18:49:21 +0000308 serverpf.bufferFromRGB((rdr::U8*)&buf[y*stride+x], pix, 1, NULL);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000309 }
310
311 memcpy(prevRow, thisRow, sizeof(prevRow));
312 }
313
314 delete [] netbuf;
315}
316
317#endif
318
DRC33c15e32011-11-03 18:49:21 +0000319void
320FILTER_GRADIENT(rdr::InStream* is, PIXEL_T* buf, int stride, const Rect& r,
321 int dataSize)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000322{
323 int x, y, c;
324 static rdr::U8 prevRow[TIGHT_MAX_WIDTH*sizeof(PIXEL_T)];
325 static rdr::U8 thisRow[TIGHT_MAX_WIDTH*sizeof(PIXEL_T)];
Pierre Ossman67b2b2f2009-03-06 10:12:55 +0000326 rdr::U8 pix[3];
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000327 int est[3];
328
329 memset(prevRow, 0, sizeof(prevRow));
330
331 // Allocate netbuf and read in data
332 PIXEL_T *netbuf = (PIXEL_T*)new rdr::U8[dataSize];
333 if (!netbuf) {
334 throw Exception("rfb::tightDecode unable to allocate buffer");
335 }
336 is->readBytes(netbuf, dataSize);
337
338 // Set up shortcut variables
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000339 int rectHeight = r.height();
340 int rectWidth = r.width();
341
342 for (y = 0; y < rectHeight; y++) {
343 /* First pixel in a row */
DRC33c15e32011-11-03 18:49:21 +0000344 serverpf.rgbFromBuffer(pix, (rdr::U8*)&netbuf[y*rectWidth], 1, NULL);
Pierre Ossman67b2b2f2009-03-06 10:12:55 +0000345 for (c = 0; c < 3; c++)
346 pix[c] += prevRow[c];
347
348 memcpy(thisRow, pix, sizeof(pix));
349
DRC33c15e32011-11-03 18:49:21 +0000350 serverpf.bufferFromRGB((rdr::U8*)&buf[y*stride], pix, 1, NULL);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000351
352 /* Remaining pixels of a row */
353 for (x = 1; x < rectWidth; x++) {
354 for (c = 0; c < 3; c++) {
Pierre Ossman67b2b2f2009-03-06 10:12:55 +0000355 est[c] = prevRow[x*3+c] + pix[c] - prevRow[(x-1)*3+c];
356 if (est[c] > 255) {
357 est[c] = 255;
358 } else if (est[c] < 0) {
359 est[c] = 0;
360 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000361 }
Pierre Ossman67b2b2f2009-03-06 10:12:55 +0000362
DRC33c15e32011-11-03 18:49:21 +0000363 serverpf.rgbFromBuffer(pix, (rdr::U8*)&netbuf[y*rectWidth+x], 1, NULL);
Pierre Ossman67b2b2f2009-03-06 10:12:55 +0000364 for (c = 0; c < 3; c++)
365 pix[c] += est[c];
366
367 memcpy(&thisRow[x*3], pix, sizeof(pix));
368
DRC33c15e32011-11-03 18:49:21 +0000369 serverpf.bufferFromRGB((rdr::U8*)&buf[y*stride+x], pix, 1, NULL);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000370 }
371
372 memcpy(prevRow, thisRow, sizeof(prevRow));
373 }
374
375 delete [] netbuf;
376}
377
378#undef TIGHT_MIN_TO_COMPRESS
DRC33c15e32011-11-03 18:49:21 +0000379#undef FILTER_GRADIENT
380#undef DECOMPRESS_JPEG_RECT
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000381#undef TIGHT_DECODE
382#undef READ_PIXEL
383#undef PIXEL_T
384}