1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
|
/* Copyright (C) 2000-2003 Constantin Kaplinsky. All Rights Reserved.
* Copyright (C) 2004-2005 Cendio AB. All rights reserved.
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
//
// Tight decoding functions.
//
// This file is #included after having set the following macros:
// BPP - 8, 16 or 32
// EXTRA_ARGS - optional extra arguments
// FILL_RECT - fill a rectangle with a single color
// IMAGE_RECT - draw a rectangle of pixel data from a buffer
#include <rdr/InStream.h>
#include <rdr/ZlibInStream.h>
#include <rfb/Exception.h>
#include <assert.h>
namespace rfb {
// CONCAT2E concatenates its arguments, expanding them if they are macros
#ifndef CONCAT2E
#define CONCAT2(a,b) a##b
#define CONCAT2E(a,b) CONCAT2(a,b)
#endif
#define PIXEL_T rdr::CONCAT2E(U,BPP)
#define READ_PIXEL CONCAT2E(readOpaque,BPP)
#define TIGHT_DECODE CONCAT2E(tightDecode,BPP)
#define TIGHT_MIN_TO_COMPRESS 12
static bool DecompressJpegRect(const Rect& r, rdr::InStream* is,
PIXEL_T* buf, CMsgHandler* handler);
static void FilterGradient(const Rect& r, rdr::InStream* is, int dataSize,
PIXEL_T* buf, CMsgHandler* handler);
#if BPP == 32
static void FilterGradient24(const Rect& r, rdr::InStream* is, int dataSize,
PIXEL_T* buf, CMsgHandler* handler);
#endif
// Main function implementing Tight decoder
void TIGHT_DECODE (const Rect& r, rdr::InStream* is,
rdr::ZlibInStream zis[], PIXEL_T* buf
#ifdef EXTRA_ARGS
, EXTRA_ARGS
#endif
)
{
rdr::U8 *bytebuf = (rdr::U8*) buf;
bool cutZeros = false;
const rfb::PixelFormat& myFormat = handler->cp.pf();
#if BPP == 32
if (myFormat.is888()) {
cutZeros = true;
}
#endif
rdr::U8 comp_ctl = is->readU8();
// Flush zlib streams if we are told by the server to do so.
for (int i = 0; i < 4; i++) {
if (comp_ctl & 1) {
zis[i].reset();
}
comp_ctl >>= 1;
}
// "Fill" compression type.
if (comp_ctl == rfbTightFill) {
PIXEL_T pix;
if (cutZeros) {
is->readBytes(bytebuf, 3);
pix = myFormat.pixelFromRGB(bytebuf[0], bytebuf[1], bytebuf[2]);
} else {
pix = is->READ_PIXEL();
}
FILL_RECT(r, pix);
return;
}
// "JPEG" compression type.
if (comp_ctl == rfbTightJpeg) {
DecompressJpegRect(r, is, buf, handler);
return;
}
// Quit on unsupported compression type.
if (comp_ctl > rfbTightMaxSubencoding) {
throw Exception("TightDecoder: bad subencoding value received");
return;
}
// "Basic" compression type.
int palSize = 0;
static PIXEL_T palette[256];
bool useGradient = false;
if ((comp_ctl & rfbTightExplicitFilter) != 0) {
rdr::U8 filterId = is->readU8();
switch (filterId) {
case rfbTightFilterPalette:
palSize = is->readU8() + 1;
if (cutZeros) {
rdr::U8 *tightPalette = (rdr::U8*) palette;
is->readBytes(tightPalette, palSize*3);
for (int i = palSize - 1; i >= 0; i--) {
palette[i] = myFormat.pixelFromRGB(tightPalette[i*3],
tightPalette[i*3+1],
tightPalette[i*3+2]);
}
} else {
for (int i = 0; i < palSize; i++)
palette[i] = is->READ_PIXEL();
}
break;
case rfbTightFilterGradient:
useGradient = true;
break;
case rfbTightFilterCopy:
break;
default:
throw Exception("TightDecoder: unknown filter code received");
return;
}
}
int bppp = BPP;
if (palSize != 0) {
bppp = (palSize <= 2) ? 1 : 8;
} else if (cutZeros) {
bppp = 24;
}
// Determine if the data should be decompressed or just copied.
int rowSize = (r.width() * bppp + 7) / 8;
int dataSize = r.height() * rowSize;
int streamId = -1;
rdr::InStream *input;
if (dataSize < TIGHT_MIN_TO_COMPRESS) {
input = is;
} else {
int length = is->readCompactLength();
streamId = comp_ctl & 0x03;
zis[streamId].setUnderlying(is, length);
input = &zis[streamId];
}
if (palSize == 0) {
// Truecolor data
if (useGradient) {
#if BPP == 32
if (cutZeros) {
FilterGradient24(r, input, dataSize, buf, handler);
} else
#endif
{
FilterGradient(r, input, dataSize, buf, handler);
}
} else {
input->readBytes(buf, dataSize);
if (cutZeros) {
for (int p = r.height() * r.width() - 1; p >= 0; p--) {
buf[p] = myFormat.pixelFromRGB(bytebuf[p*3],
bytebuf[p*3+1],
bytebuf[p*3+2]);
}
}
}
} else {
int x, y, b, w;
PIXEL_T *ptr = buf;
rdr::U8 bits;
if (palSize <= 2) {
// 2-color palette
w = (r.width() + 7) / 8;
for (y = 0; y < r.height(); y++) {
for (x = 0; x < r.width() / 8; x++) {
bits = input->readU8();
for (b = 7; b >= 0; b--) {
*ptr++ = palette[bits >> b & 1];
}
}
if (r.width() % 8 != 0) {
bits = input->readU8();
for (b = 7; b >= 8 - r.width() % 8; b--) {
*ptr++ = palette[bits >> b & 1];
}
}
}
} else {
// 256-color palette
for (y = 0; y < r.height(); y++) {
for (x = 0; x < r.width(); x++) {
*ptr++ = palette[input->readU8()];
}
}
}
}
IMAGE_RECT(r, buf);
if (streamId != -1) {
zis[streamId].reset();
}
}
static bool
DecompressJpegRect(const Rect& r, rdr::InStream* is,
PIXEL_T* buf, CMsgHandler* handler)
{
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
int w = r.width();
int h = r.height();
int pixelsize = 3;
rdr::U8 *dstBuf = NULL;
bool dstBufIsTemp = false;
const rfb::PixelFormat& pf = handler->cp.pf();
// Read length
int compressedLen = is->readCompactLength();
if (compressedLen <= 0) {
throw Exception("Incorrect data received from the server.\n");
}
// Allocate netbuf and read in data
rdr::U8* netbuf = new rdr::U8[compressedLen];
if (!netbuf) {
throw Exception("rfb::tightDecode unable to allocate buffer");
}
is->readBytes(netbuf, compressedLen);
// Set up JPEG decompression
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
JpegSetSrcManager(&cinfo, (char*)netbuf, compressedLen);
jpeg_read_header(&cinfo, TRUE);
#ifdef JCS_EXTENSIONS
pixelsize = pf.bpp / 8;
if(pf.redMax == 255 && pf.greenMax == 255 && pf.blueMax == 255) {
int redShift, greenShift, blueShift;
if(pf.bigEndian) {
redShift = 24 - pf.redShift;
greenShift = 24 - pf.greenShift;
blueShift = 24 - pf.blueShift;
}
else {
redShift = pf.redShift;
greenShift = pf.greenShift;
blueShift = pf.blueShift;
}
if(redShift == 0 && greenShift == 8 && blueShift == 16 && pixelsize == 3)
cinfo.out_color_space = JCS_EXT_RGB;
if(redShift == 0 && greenShift == 8 && blueShift == 16 && pixelsize == 4)
cinfo.out_color_space = JCS_EXT_RGBX;
if(redShift == 16 && greenShift == 8 && blueShift == 0 && pixelsize == 3)
cinfo.out_color_space = JCS_EXT_BGR;
if(redShift == 16 && greenShift == 8 && blueShift == 0 && pixelsize == 4)
cinfo.out_color_space = JCS_EXT_BGRX;
if(redShift == 24 && greenShift == 16 && blueShift == 8 && pixelsize == 4)
cinfo.out_color_space = JCS_EXT_XBGR;
if(redShift == 8 && greenShift == 16 && blueShift == 24 && pixelsize == 4)
cinfo.out_color_space = JCS_EXT_XRGB;
if(cinfo.out_color_space != JCS_RGB)
dstBuf = (rdr::U8 *)buf;
}
else
#endif
{
dstBuf = new rdr::U8[w * h * pixelsize];
dstBufIsTemp = true;
cinfo.out_color_space = JCS_RGB;
}
JSAMPROW *rowPointer = new JSAMPROW[h];
for (int dy = 0; dy < h; dy++)
rowPointer[dy] = (JSAMPROW)(&dstBuf[dy * w * pixelsize]);
jpeg_start_decompress(&cinfo);
if (cinfo.output_width != (unsigned)r.width() || cinfo.output_height != (unsigned)r.height() ||
cinfo.output_components != pixelsize) {
jpeg_destroy_decompress(&cinfo);
throw Exception("Tight Encoding: Wrong JPEG data received.\n");
}
// Decompress
const rfb::PixelFormat& myFormat = handler->cp.pf();
while (cinfo.output_scanline < cinfo.output_height) {
jpeg_read_scanlines(&cinfo, &rowPointer[cinfo.output_scanline],
cinfo.output_height - cinfo.output_scanline);
if (jpegError) {
break;
}
}
if (cinfo.out_color_space == JCS_RGB)
myFormat.bufferFromRGB((rdr::U8*)buf, dstBuf, w * h);
IMAGE_RECT(r, buf);
if (!jpegError) {
jpeg_finish_decompress(&cinfo);
}
jpeg_destroy_decompress(&cinfo);
if (dstBufIsTemp) delete [] dstBuf;
delete [] netbuf;
return !jpegError;
}
#if BPP == 32
static void
FilterGradient24(const Rect& r, rdr::InStream* is, int dataSize,
PIXEL_T* buf, CMsgHandler* handler)
{
int x, y, c;
static rdr::U8 prevRow[TIGHT_MAX_WIDTH*3];
static rdr::U8 thisRow[TIGHT_MAX_WIDTH*3];
rdr::U8 pix[3];
int est[3];
memset(prevRow, 0, sizeof(prevRow));
// Allocate netbuf and read in data
rdr::U8 *netbuf = new rdr::U8[dataSize];
if (!netbuf) {
throw Exception("rfb::tightDecode unable to allocate buffer");
}
is->readBytes(netbuf, dataSize);
// Set up shortcut variables
const rfb::PixelFormat& myFormat = handler->cp.pf();
int rectHeight = r.height();
int rectWidth = r.width();
for (y = 0; y < rectHeight; y++) {
/* First pixel in a row */
for (c = 0; c < 3; c++) {
pix[c] = netbuf[y*rectWidth*3+c] + prevRow[c];
thisRow[c] = pix[c];
}
buf[y*rectWidth] = myFormat.pixelFromRGB(pix[0], pix[1], pix[2]);
/* Remaining pixels of a row */
for (x = 1; x < rectWidth; x++) {
for (c = 0; c < 3; c++) {
est[c] = prevRow[x*3+c] + pix[c] - prevRow[(x-1)*3+c];
if (est[c] > 0xff) {
est[c] = 0xff;
} else if (est[c] < 0) {
est[c] = 0;
}
pix[c] = netbuf[(y*rectWidth+x)*3+c] + est[c];
thisRow[x*3+c] = pix[c];
}
buf[y*rectWidth+x] = myFormat.pixelFromRGB(pix[0], pix[1], pix[2]);
}
memcpy(prevRow, thisRow, sizeof(prevRow));
}
delete [] netbuf;
}
#endif
static void
FilterGradient(const Rect& r, rdr::InStream* is, int dataSize,
PIXEL_T* buf, CMsgHandler* handler)
{
int x, y, c;
static rdr::U8 prevRow[TIGHT_MAX_WIDTH*sizeof(PIXEL_T)];
static rdr::U8 thisRow[TIGHT_MAX_WIDTH*sizeof(PIXEL_T)];
rdr::U8 pix[3];
int est[3];
memset(prevRow, 0, sizeof(prevRow));
// Allocate netbuf and read in data
PIXEL_T *netbuf = (PIXEL_T*)new rdr::U8[dataSize];
if (!netbuf) {
throw Exception("rfb::tightDecode unable to allocate buffer");
}
is->readBytes(netbuf, dataSize);
// Set up shortcut variables
const rfb::PixelFormat& myFormat = handler->cp.pf();
int rectHeight = r.height();
int rectWidth = r.width();
for (y = 0; y < rectHeight; y++) {
/* First pixel in a row */
myFormat.rgbFromPixel(netbuf[y*rectWidth], NULL,
&pix[0], &pix[1], &pix[2]);
for (c = 0; c < 3; c++)
pix[c] += prevRow[c];
memcpy(thisRow, pix, sizeof(pix));
buf[y*rectWidth] = myFormat.pixelFromRGB(pix[0], pix[1], pix[2]);
/* Remaining pixels of a row */
for (x = 1; x < rectWidth; x++) {
for (c = 0; c < 3; c++) {
est[c] = prevRow[x*3+c] + pix[c] - prevRow[(x-1)*3+c];
if (est[c] > 255) {
est[c] = 255;
} else if (est[c] < 0) {
est[c] = 0;
}
}
myFormat.rgbFromPixel(netbuf[y*rectWidth+x], NULL,
&pix[0], &pix[1], &pix[2]);
for (c = 0; c < 3; c++)
pix[c] += est[c];
memcpy(&thisRow[x*3], pix, sizeof(pix));
buf[y*rectWidth+x] = myFormat.pixelFromRGB(pix[0], pix[1], pix[2]);
}
memcpy(prevRow, thisRow, sizeof(prevRow));
}
delete [] netbuf;
}
#undef TIGHT_MIN_TO_COMPRESS
#undef TIGHT_DECODE
#undef READ_PIXEL
#undef PIXEL_T
}
|