blob: 2c5f7e40ea6355c04a236f3ce8f6d8736aa1e3d5 [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) {
DRCa5004a32011-11-04 04:51:17 +0000111 rdr::U8 tightPalette[256 * 3];
112 is->readBytes(tightPalette, palSize * 3);
113 serverpf.bufferFromRGB((rdr::U8*)palette, tightPalette, palSize, NULL);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000114 } else {
DRCa5004a32011-11-04 04:51:17 +0000115 is->readBytes(palette, palSize * sizeof(PIXEL_T));
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000116 }
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;
DRCa5004a32011-11-04 04:51:17 +0000141 bool useZlib = false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000142 if (dataSize < TIGHT_MIN_TO_COMPRESS) {
143 input = is;
144 } else {
DRCa5004a32011-11-04 04:51:17 +0000145 useZlib = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000146 int length = is->readCompactLength();
147 streamId = comp_ctl & 0x03;
148 zis[streamId].setUnderlying(is, length);
149 input = &zis[streamId];
150 }
151
DRCa5004a32011-11-04 04:51:17 +0000152 // Allocate netbuf and read in data
153 rdr::U8 *netbuf = new rdr::U8[dataSize];
154 if (!netbuf) {
155 throw Exception("rfb::TightDecoder::tightDecode unable to allocate buffer");
156 }
157 input->readBytes(netbuf, dataSize);
158
DRC33c15e32011-11-03 18:49:21 +0000159 PIXEL_T *buf;
160 int stride = r.width();
161 if (directDecode) buf = (PIXEL_T *)handler->getRawPixelsRW(r, &stride);
162 else buf = (PIXEL_T *)reader->getImageBuf(r.area());
163
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000164 if (palSize == 0) {
165 // Truecolor data
166 if (useGradient) {
167#if BPP == 32
168 if (cutZeros) {
DRCa5004a32011-11-04 04:51:17 +0000169 FilterGradient24(netbuf, buf, stride, r);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000170 } else
171#endif
DRC33c15e32011-11-03 18:49:21 +0000172 {
DRCa5004a32011-11-04 04:51:17 +0000173 FILTER_GRADIENT(netbuf, buf, stride, r);
DRC33c15e32011-11-03 18:49:21 +0000174 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000175 } else {
DRC33c15e32011-11-03 18:49:21 +0000176 // Copy
177 int h = r.height();
178 PIXEL_T *ptr = buf;
DRCa5004a32011-11-04 04:51:17 +0000179 rdr::U8 *srcPtr = netbuf;
180 int w = r.width();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000181 if (cutZeros) {
DRC33c15e32011-11-03 18:49:21 +0000182 while (h > 0) {
DRCa5004a32011-11-04 04:51:17 +0000183 serverpf.bufferFromRGB((rdr::U8*)ptr, srcPtr, w, NULL);
184 ptr += stride;
185 srcPtr += w * sizeof(PIXEL_T);
DRC33c15e32011-11-03 18:49:21 +0000186 h--;
Pierre Ossman7c4efd72010-09-30 11:30:20 +0000187 }
188 } else {
DRC33c15e32011-11-03 18:49:21 +0000189 while (h > 0) {
DRCa5004a32011-11-04 04:51:17 +0000190 memcpy(ptr, srcPtr, rowSize * sizeof(PIXEL_T));
DRC33c15e32011-11-03 18:49:21 +0000191 ptr += stride;
DRCa5004a32011-11-04 04:51:17 +0000192 srcPtr += w * sizeof(PIXEL_T);
DRC33c15e32011-11-03 18:49:21 +0000193 h--;
194 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000195 }
196 }
197 } else {
DRC33c15e32011-11-03 18:49:21 +0000198 // Indexed color
199 int x, h = r.height(), w = r.width(), b, pad = stride - w;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000200 PIXEL_T *ptr = buf;
DRCa5004a32011-11-04 04:51:17 +0000201 rdr::U8 bits, *srcPtr = netbuf;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000202 if (palSize <= 2) {
203 // 2-color palette
DRC33c15e32011-11-03 18:49:21 +0000204 while (h > 0) {
205 for (x = 0; x < w / 8; x++) {
DRCa5004a32011-11-04 04:51:17 +0000206 bits = *srcPtr++;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000207 for (b = 7; b >= 0; b--) {
208 *ptr++ = palette[bits >> b & 1];
DRC33c15e32011-11-03 18:49:21 +0000209 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000210 }
DRC33c15e32011-11-03 18:49:21 +0000211 if (w % 8 != 0) {
DRCa5004a32011-11-04 04:51:17 +0000212 bits = *srcPtr++;
DRC33c15e32011-11-03 18:49:21 +0000213 for (b = 7; b >= 8 - w % 8; b--) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000214 *ptr++ = palette[bits >> b & 1];
215 }
216 }
DRC33c15e32011-11-03 18:49:21 +0000217 ptr += pad;
218 h--;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000219 }
220 } else {
221 // 256-color palette
DRC33c15e32011-11-03 18:49:21 +0000222 while (h > 0) {
223 PIXEL_T *endOfRow = ptr + w;
224 while (ptr < endOfRow) {
DRCa5004a32011-11-04 04:51:17 +0000225 *ptr++ = palette[*srcPtr++];
DRC33c15e32011-11-03 18:49:21 +0000226 }
227 ptr += pad;
228 h--;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000229 }
230 }
231 }
232
DRC33c15e32011-11-03 18:49:21 +0000233 if (directDecode) handler->releaseRawPixels(r);
234 else IMAGE_RECT(r, buf);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000235
DRCa5004a32011-11-04 04:51:17 +0000236 delete [] netbuf;
237
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000238 if (streamId != -1) {
239 zis[streamId].reset();
240 }
241}
242
DRC33c15e32011-11-03 18:49:21 +0000243void
244DECOMPRESS_JPEG_RECT(const Rect& r)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000245{
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000246 // Read length
247 int compressedLen = is->readCompactLength();
248 if (compressedLen <= 0) {
249 throw Exception("Incorrect data received from the server.\n");
250 }
251
252 // Allocate netbuf and read in data
253 rdr::U8* netbuf = new rdr::U8[compressedLen];
254 if (!netbuf) {
DRCa5004a32011-11-04 04:51:17 +0000255 throw Exception("rfb::TightDecoder::DecompressJpegRect unable to allocate buffer");
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000256 }
257 is->readBytes(netbuf, compressedLen);
258
DRC33c15e32011-11-03 18:49:21 +0000259 // We always use direct decoding with JPEG images
260 int stride;
261 rdr::U8 *buf = handler->getRawPixelsRW(r, &stride);
262 jd.decompress(netbuf, compressedLen, buf, stride * clientpf.bpp / 8, r,
263 clientpf);
264 handler->releaseRawPixels(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 }
DRC33c15e32011-11-03 18:49:21 +0000293 serverpf.bufferFromRGB((rdr::U8*)&buf[y*stride], pix, 1, NULL);
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 }
DRC33c15e32011-11-03 18:49:21 +0000307 serverpf.bufferFromRGB((rdr::U8*)&buf[y*stride+x], pix, 1, NULL);
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 */
DRC33c15e32011-11-03 18:49:21 +0000333 serverpf.rgbFromBuffer(pix, (rdr::U8*)&netbuf[y*rectWidth], 1, NULL);
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
DRC33c15e32011-11-03 18:49:21 +0000339 serverpf.bufferFromRGB((rdr::U8*)&buf[y*stride], pix, 1, NULL);
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
DRC33c15e32011-11-03 18:49:21 +0000352 serverpf.rgbFromBuffer(pix, (rdr::U8*)&netbuf[y*rectWidth+x], 1, NULL);
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
DRC33c15e32011-11-03 18:49:21 +0000358 serverpf.bufferFromRGB((rdr::U8*)&buf[y*stride+x], pix, 1, NULL);
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}