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