2
* Copyright (C) 2000, 2001 Const Kaplinsky. All Rights Reserved.
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.
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.
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,
21
* tight.c - handle ``tight'' encoding.
23
* This file shouldn't be compiled directly. It is included multiple
24
* times by rfbproto.c, each time with a different definition of the
25
* macro BPP. For each value of BPP, this file defines a function
26
* which handles a tight-encoded rectangle with BPP bits per pixel.
30
#define TIGHT_MIN_TO_COMPRESS 12
32
#define CARDBPP CONCAT2E(CARD,BPP)
33
#define filterPtrBPP CONCAT2E(filterPtr,BPP)
35
#define HandleTightBPP CONCAT2E(HandleTight,BPP)
36
#define InitFilterCopyBPP CONCAT2E(InitFilterCopy,BPP)
37
#define InitFilterPaletteBPP CONCAT2E(InitFilterPalette,BPP)
38
#define InitFilterGradientBPP CONCAT2E(InitFilterGradient,BPP)
39
#define FilterCopyBPP CONCAT2E(FilterCopy,BPP)
40
#define FilterPaletteBPP CONCAT2E(FilterPalette,BPP)
41
#define FilterGradientBPP CONCAT2E(FilterGradient,BPP)
44
#define DecompressJpegRectBPP CONCAT2E(DecompressJpegRect,BPP)
49
#define RGB_TO_PIXEL(bpp,r,g,b) \
50
(((CARD##bpp)(r) & myFormat.redMax) << myFormat.redShift | \
51
((CARD##bpp)(g) & myFormat.greenMax) << myFormat.greenShift | \
52
((CARD##bpp)(b) & myFormat.blueMax) << myFormat.blueShift)
54
#define RGB24_TO_PIXEL(bpp,r,g,b) \
55
((((CARD##bpp)(r) & 0xFF) * myFormat.redMax + 127) / 255 \
56
<< myFormat.redShift | \
57
(((CARD##bpp)(g) & 0xFF) * myFormat.greenMax + 127) / 255 \
58
<< myFormat.greenShift | \
59
(((CARD##bpp)(b) & 0xFF) * myFormat.blueMax + 127) / 255 \
60
<< myFormat.blueShift)
62
#define RGB24_TO_PIXEL32(r,g,b) \
63
(((CARD32)(r) & 0xFF) << myFormat.redShift | \
64
((CARD32)(g) & 0xFF) << myFormat.greenShift | \
65
((CARD32)(b) & 0xFF) << myFormat.blueShift)
69
/* Type declarations */
71
typedef void (*filterPtrBPP)(int, CARDBPP *);
75
static int InitFilterCopyBPP (int rw, int rh);
76
static int InitFilterPaletteBPP (int rw, int rh);
77
static int InitFilterGradientBPP (int rw, int rh);
78
static void FilterCopyBPP (int numRows, CARDBPP *destBuffer);
79
static void FilterPaletteBPP (int numRows, CARDBPP *destBuffer);
80
static void FilterGradientBPP (int numRows, CARDBPP *destBuffer);
82
static Bool DecompressJpegRectBPP(int x, int y, int w, int h);
87
HandleTightBPP (int rx, int ry, int rw, int rh)
93
filterPtrBPP filterFn;
96
int err, stream_id, compressedLen, bitsPixel;
97
int bufferSize, rowSize, numRows, portionLen, rowsProcessed, extraBytes;
100
if (!ReadFromRFBServer((char *)&comp_ctl, 1))
103
/* Flush zlib streams if we are told by the server to do so. */
104
for (stream_id = 0; stream_id < 4; stream_id++) {
105
if ((comp_ctl & 1) && zlibStreamActive[stream_id]) {
106
if (inflateEnd (&zlibStream[stream_id]) != Z_OK &&
107
zlibStream[stream_id].msg != NULL)
108
fprintf(stderr, "inflateEnd: %s\n", zlibStream[stream_id].msg);
109
zlibStreamActive[stream_id] = False;
114
/* Handle solid rectangles. */
115
if (comp_ctl == rfbTightFill) {
117
if (myFormat.depth == 24 && myFormat.redMax == 0xFF &&
118
myFormat.greenMax == 0xFF && myFormat.blueMax == 0xFF) {
119
if (!ReadFromRFBServer(buffer, 3))
121
fill_colour = RGB24_TO_PIXEL32(buffer[0], buffer[1], buffer[2]);
123
if (!ReadFromRFBServer((char*)&fill_colour, sizeof(fill_colour)))
127
if (!ReadFromRFBServer((char*)&fill_colour, sizeof(fill_colour)))
132
gcv.foreground = (appData.useBGR233) ?
133
BGR233ToPixel[fill_colour] : fill_colour;
135
gcv.foreground = fill_colour;
138
XChangeGC(dpy, gc, GCForeground, &gcv);
139
XFillRectangle(dpy, desktopWin, gc, rx, ry, rw, rh);
144
if (comp_ctl == rfbTightJpeg) {
145
fprintf(stderr, "Tight encoding: JPEG is not supported in 8 bpp mode.\n");
149
if (comp_ctl == rfbTightJpeg) {
150
return DecompressJpegRectBPP(rx, ry, rw, rh);
154
/* Quit on unsupported subencoding value. */
155
if (comp_ctl > rfbTightMaxSubencoding) {
156
fprintf(stderr, "Tight encoding: bad subencoding value received.\n");
161
* Here primary compression mode handling begins.
162
* Data was processed with optional filter + zlib compression.
165
/* First, we should identify a filter to use. */
166
if ((comp_ctl & rfbTightExplicitFilter) != 0) {
167
if (!ReadFromRFBServer((char*)&filter_id, 1))
171
case rfbTightFilterCopy:
172
filterFn = FilterCopyBPP;
173
bitsPixel = InitFilterCopyBPP(rw, rh);
175
case rfbTightFilterPalette:
176
filterFn = FilterPaletteBPP;
177
bitsPixel = InitFilterPaletteBPP(rw, rh);
179
case rfbTightFilterGradient:
180
filterFn = FilterGradientBPP;
181
bitsPixel = InitFilterGradientBPP(rw, rh);
184
fprintf(stderr, "Tight encoding: unknown filter code received.\n");
188
filterFn = FilterCopyBPP;
189
bitsPixel = InitFilterCopyBPP(rw, rh);
191
if (bitsPixel == 0) {
192
fprintf(stderr, "Tight encoding: error receiving palette.\n");
196
/* Determine if the data should be decompressed or just copied. */
197
rowSize = (rw * bitsPixel + 7) / 8;
198
if (rh * rowSize < TIGHT_MIN_TO_COMPRESS) {
199
if (!ReadFromRFBServer((char*)buffer, rh * rowSize))
202
buffer2 = &buffer[TIGHT_MIN_TO_COMPRESS * 4];
203
filterFn(rh, (CARDBPP *)buffer2);
204
CopyDataToScreen(buffer2, rx, ry, rw, rh);
209
/* Read the length (1..3 bytes) of compressed data following. */
210
compressedLen = (int)ReadCompactLen();
211
if (compressedLen <= 0) {
212
fprintf(stderr, "Incorrect data received from the server.\n");
216
/* Now let's initialize compression stream if needed. */
217
stream_id = comp_ctl & 0x03;
218
zs = &zlibStream[stream_id];
219
if (!zlibStreamActive[stream_id]) {
223
err = inflateInit(zs);
226
fprintf(stderr, "InflateInit error: %s.\n", zs->msg);
229
zlibStreamActive[stream_id] = True;
232
/* Read, decode and draw actual pixel data in a loop. */
234
bufferSize = BUFFER_SIZE * bitsPixel / (bitsPixel + BPP) & 0xFFFFFFFC;
235
buffer2 = &buffer[bufferSize];
236
if (rowSize > bufferSize) {
237
/* Should be impossible when BUFFER_SIZE >= 16384 */
238
fprintf(stderr, "Internal error: incorrect buffer size.\n");
245
while (compressedLen > 0) {
246
if (compressedLen > ZLIB_BUFFER_SIZE)
247
portionLen = ZLIB_BUFFER_SIZE;
249
portionLen = compressedLen;
251
if (!ReadFromRFBServer((char*)zlib_buffer, portionLen))
254
compressedLen -= portionLen;
256
zs->next_in = (Bytef *)zlib_buffer;
257
zs->avail_in = portionLen;
260
zs->next_out = (Bytef *)&buffer[extraBytes];
261
zs->avail_out = bufferSize - extraBytes;
263
err = inflate(zs, Z_SYNC_FLUSH);
264
if (err == Z_BUF_ERROR) /* Input exhausted -- no problem. */
266
if (err != Z_OK && err != Z_STREAM_END) {
267
if (zs->msg != NULL) {
268
fprintf(stderr, "Inflate error: %s.\n", zs->msg);
270
fprintf(stderr, "Inflate error: %d.\n", err);
275
numRows = (bufferSize - zs->avail_out) / rowSize;
277
filterFn(numRows, (CARDBPP *)buffer2);
279
extraBytes = bufferSize - zs->avail_out - numRows * rowSize;
281
memcpy(buffer, &buffer[numRows * rowSize], extraBytes);
283
CopyDataToScreen(buffer2, rx, ry + rowsProcessed, rw, numRows);
284
rowsProcessed += numRows;
286
while (zs->avail_out == 0);
289
if (rowsProcessed != rh) {
290
fprintf(stderr, "Incorrect number of scan lines after decompression.\n");
297
/*----------------------------------------------------------------------------
304
The following variables are defined in rfbproto.c:
305
static Bool cutZeros;
306
static int rectWidth, rectColors;
307
static CARD8 tightPalette[256*4];
308
static CARD8 tightPrevRow[2048*3*sizeof(CARD16)];
312
InitFilterCopyBPP (int rw, int rh)
317
if (myFormat.depth == 24 && myFormat.redMax == 0xFF &&
318
myFormat.greenMax == 0xFF && myFormat.blueMax == 0xFF) {
330
FilterCopyBPP (int numRows, CARDBPP *dst)
337
for (y = 0; y < numRows; y++) {
338
for (x = 0; x < rectWidth; x++) {
340
RGB24_TO_PIXEL32(buffer[(y*rectWidth+x)*3],
341
buffer[(y*rectWidth+x)*3+1],
342
buffer[(y*rectWidth+x)*3+2]);
349
memcpy (dst, buffer, numRows * rectWidth * (BPP / 8));
353
InitFilterGradientBPP (int rw, int rh)
357
bits = InitFilterCopyBPP(rw, rh);
359
memset(tightPrevRow, 0, rw * 3);
361
memset(tightPrevRow, 0, rw * 3 * sizeof(CARD16));
369
FilterGradient24 (int numRows, CARD32 *dst)
372
CARD8 thisRow[2048*3];
376
for (y = 0; y < numRows; y++) {
378
/* First pixel in a row */
379
for (c = 0; c < 3; c++) {
380
pix[c] = tightPrevRow[c] + buffer[y*rectWidth*3+c];
383
dst[y*rectWidth] = RGB24_TO_PIXEL32(pix[0], pix[1], pix[2]);
385
/* Remaining pixels of a row */
386
for (x = 1; x < rectWidth; x++) {
387
for (c = 0; c < 3; c++) {
388
est[c] = (int)tightPrevRow[x*3+c] + (int)pix[c] -
389
(int)tightPrevRow[(x-1)*3+c];
392
} else if (est[c] < 0x00) {
395
pix[c] = (CARD8)est[c] + buffer[(y*rectWidth+x)*3+c];
396
thisRow[x*3+c] = pix[c];
398
dst[y*rectWidth+x] = RGB24_TO_PIXEL32(pix[0], pix[1], pix[2]);
401
memcpy(tightPrevRow, thisRow, rectWidth * 3);
408
FilterGradientBPP (int numRows, CARDBPP *dst)
411
CARDBPP *src = (CARDBPP *)buffer;
412
CARD16 *thatRow = (CARD16 *)tightPrevRow;
413
CARD16 thisRow[2048*3];
421
FilterGradient24(numRows, dst);
426
max[0] = myFormat.redMax;
427
max[1] = myFormat.greenMax;
428
max[2] = myFormat.blueMax;
430
shift[0] = myFormat.redShift;
431
shift[1] = myFormat.greenShift;
432
shift[2] = myFormat.blueShift;
434
for (y = 0; y < numRows; y++) {
436
/* First pixel in a row */
437
for (c = 0; c < 3; c++) {
438
pix[c] = (CARD16)((src[y*rectWidth] >> shift[c]) + thatRow[c] & max[c]);
441
dst[y*rectWidth] = RGB_TO_PIXEL(BPP, pix[0], pix[1], pix[2]);
443
/* Remaining pixels of a row */
444
for (x = 1; x < rectWidth; x++) {
445
for (c = 0; c < 3; c++) {
446
est[c] = (int)thatRow[x*3+c] + (int)pix[c] - (int)thatRow[(x-1)*3+c];
447
if (est[c] > (int)max[c]) {
448
est[c] = (int)max[c];
449
} else if (est[c] < 0) {
452
pix[c] = (CARD16)((src[y*rectWidth+x] >> shift[c]) + est[c] & max[c]);
453
thisRow[x*3+c] = pix[c];
455
dst[y*rectWidth+x] = RGB_TO_PIXEL(BPP, pix[0], pix[1], pix[2]);
457
memcpy(thatRow, thisRow, rectWidth * 3 * sizeof(CARD16));
462
InitFilterPaletteBPP (int rw, int rh)
466
CARDBPP *palette = (CARDBPP *)tightPalette;
470
if (!ReadFromRFBServer((char*)&numColors, 1))
473
rectColors = (int)numColors;
474
if (++rectColors < 2)
478
if (myFormat.depth == 24 && myFormat.redMax == 0xFF &&
479
myFormat.greenMax == 0xFF && myFormat.blueMax == 0xFF) {
480
if (!ReadFromRFBServer((char*)&tightPalette, rectColors * 3))
482
for (i = rectColors - 1; i >= 0; i--) {
483
palette[i] = RGB24_TO_PIXEL32(tightPalette[i*3],
485
tightPalette[i*3+2]);
487
return (rectColors == 2) ? 1 : 8;
491
if (!ReadFromRFBServer((char*)&tightPalette, rectColors * (BPP / 8)))
494
return (rectColors == 2) ? 1 : 8;
498
FilterPaletteBPP (int numRows, CARDBPP *dst)
501
CARD8 *src = (CARD8 *)buffer;
502
CARDBPP *palette = (CARDBPP *)tightPalette;
504
if (rectColors == 2) {
505
w = (rectWidth + 7) / 8;
506
for (y = 0; y < numRows; y++) {
507
for (x = 0; x < rectWidth / 8; x++) {
508
for (b = 7; b >= 0; b--)
509
dst[y*rectWidth+x*8+7-b] = palette[src[y*w+x] >> b & 1];
511
for (b = 7; b >= 8 - rectWidth % 8; b--) {
512
dst[y*rectWidth+x*8+7-b] = palette[src[y*w+x] >> b & 1];
516
for (y = 0; y < numRows; y++)
517
for (x = 0; x < rectWidth; x++)
518
dst[y*rectWidth+x] = palette[(int)src[y*rectWidth+x]];
524
/*----------------------------------------------------------------------------
526
* JPEG decompression.
531
The following variables are defined in rfbproto.c:
532
static Bool jpegError;
533
static struct jpeg_source_mgr jpegSrcManager;
534
static JOCTET *jpegBufferPtr;
535
static size_t *jpegBufferLen;
539
DecompressJpegRectBPP(int x, int y, int w, int h)
541
struct jpeg_decompress_struct cinfo;
542
struct jpeg_error_mgr jerr;
544
CARD8 *compressedData;
546
JSAMPROW rowPointer[1];
549
compressedLen = (int)ReadCompactLen();
550
if (compressedLen <= 0) {
551
fprintf(stderr, "Incorrect data received from the server.\n");
555
compressedData = malloc(compressedLen);
556
if (compressedData == NULL) {
557
fprintf(stderr, "Memory allocation error.\n");
561
if (!ReadFromRFBServer((char*)compressedData, compressedLen)) {
562
free(compressedData);
566
cinfo.err = jpeg_std_error(&jerr);
567
jpeg_create_decompress(&cinfo);
569
JpegSetSrcManager(&cinfo, compressedData, compressedLen);
571
jpeg_read_header(&cinfo, TRUE);
572
cinfo.out_color_space = JCS_RGB;
574
jpeg_start_decompress(&cinfo);
575
if (cinfo.output_width != w || cinfo.output_height != h ||
576
cinfo.output_components != 3) {
577
fprintf(stderr, "Tight Encoding: Wrong JPEG data received.\n");
578
jpeg_destroy_decompress(&cinfo);
579
free(compressedData);
583
rowPointer[0] = (JSAMPROW)buffer;
585
while (cinfo.output_scanline < cinfo.output_height) {
586
jpeg_read_scanlines(&cinfo, rowPointer, 1);
590
pixelPtr = (CARDBPP *)&buffer[BUFFER_SIZE / 2];
591
for (dx = 0; dx < w; dx++) {
593
RGB24_TO_PIXEL(BPP, buffer[dx*3], buffer[dx*3+1], buffer[dx*3+2]);
595
CopyDataToScreen(&buffer[BUFFER_SIZE / 2], x, y + dy, w, 1);
600
jpeg_finish_decompress(&cinfo);
602
jpeg_destroy_decompress(&cinfo);
603
free(compressedData);