blob: 004f641fc893ed5bbb827e4749158613f431158b [file] [log] [blame]
Peter Åstrand462753d2004-11-16 15:23:25 +00001/* Copyright (C) 2000-2003 Constantin Kaplinsky. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19//
20// Tight decoding functions.
21//
22// This file is #included after having set the following macros:
23// BPP - 8, 16 or 32
24// EXTRA_ARGS - optional extra arguments
25// FILL_RECT - fill a rectangle with a single colour
26// IMAGE_RECT - draw a rectangle of pixel data from a buffer
27
28#include <rdr/InStream.h>
29#include <rdr/ZlibInStream.h>
30#include <rfb/Exception.h>
31#include <assert.h>
32
33namespace rfb {
34
35// CONCAT2E concatenates its arguments, expanding them if they are macros
36
37#ifndef CONCAT2E
38#define CONCAT2(a,b) a##b
39#define CONCAT2E(a,b) CONCAT2(a,b)
40#endif
41
42#define PIXEL_T rdr::CONCAT2E(U,BPP)
43#define READ_PIXEL CONCAT2E(readOpaque,BPP)
44#define TIGHT_DECODE CONCAT2E(tightDecode,BPP)
45
46#define TIGHT_MIN_TO_COMPRESS 12
47
48// Main function implementing Tight decoder
49
50void TIGHT_DECODE (const Rect& r, rdr::InStream* is,
51 rdr::ZlibInStream zis[], PIXEL_T* buf
52#ifdef EXTRA_ARGS
53 , EXTRA_ARGS
54#endif
55 )
56{
57 rdr::U8 comp_ctl = is->readU8();
58
59 // Flush zlib streams if we are told by the server to do so.
60 for (int i = 0; i < 4; i++) {
Peter Åstrandef5dd312004-11-17 08:50:05 +000061 if (comp_ctl & 1) {
62 zis[i].reset();
Peter Åstrand462753d2004-11-16 15:23:25 +000063 }
Peter Åstrand462753d2004-11-16 15:23:25 +000064 comp_ctl >>= 1;
65 }
66
67 // "Fill" compression type.
68 if (comp_ctl == 0x08) {
69 PIXEL_T pix = is->READ_PIXEL();
70 FILL_RECT(r, pix);
71 return;
72 }
73
74 // "JPEG" compression type.
75 if (comp_ctl == 0x09) {
76 throw Exception("TightDecoder: FIXME: JPEG compression is not supported yet");
77 return;
78 }
79
80 // Quit on unsupported compression type.
81 if (comp_ctl > 0x09) {
82 throw Exception("TightDecoder: bad subencoding value received");
83 return;
84 }
85
86 // "Basic" compression type.
87 int palSize = 0;
88 PIXEL_T palette[256];
89 bool useGradient = false;
90
91 if ((comp_ctl & 0x04) != 0) {
92 rdr::U8 filterId = is->readU8();
93
94 switch (filterId) {
95 case 0x01: // "palette" filter
96 palSize = is->readU8() + 1;
97 {
98 for (int i = 0; i < palSize; i++)
99 palette[i] = is->READ_PIXEL();
100 }
101 break;
102 case 0x02: // "gradient" filter
103 useGradient = true;
104 break;
105 case 0x00: // no filter
106 break;
107 default:
108 throw Exception("TightDecoder: unknown filter code received");
109 return;
110 }
111 }
112
113 int bppp = BPP;
114 if (palSize != 0) {
115 bppp = (palSize <= 2) ? 1 : 8;
116 }
117
118 // Determine if the data should be decompressed or just copied.
119 int rowSize = (r.width() * bppp + 7) / 8;
120 int dataSize = r.height() * rowSize;
121 rdr::InStream *input;
122 if (dataSize < TIGHT_MIN_TO_COMPRESS) {
123 input = is;
124 } else {
125 int length = is->readCompactLength();
126 int streamId = comp_ctl & 0x03;
127 zis[streamId].setUnderlying(is, length);
128 input = &zis[streamId];
129 }
130
131 if (palSize == 0) {
132 // Truecolor data
133 input->readBytes(buf, dataSize);
134 if (useGradient) {
135 // FIXME: Implement the "gradient" filter.
136 }
137 } else {
138 int x, y, b, w;
139 PIXEL_T *ptr = buf;
140 rdr::U8 bits;
141 if (palSize <= 2) {
142 // 2-color palette
143 w = (r.width() + 7) / 8;
144 for (y = 0; y < r.height(); y++) {
145 for (x = 0; x < r.width() / 8; x++) {
146 bits = input->readU8();
147 for (b = 7; b >= 0; b--)
148 *ptr++ = palette[bits >> b & 1];
149 }
150 if (r.width() % 8 != 0) {
151 bits = input->readU8();
152 for (b = 7; b >= 8 - r.width() % 8; b--) {
153 *ptr++ = palette[bits >> b & 1];
154 }
155 }
156 }
157 } else {
158 // 256-color palette
159 for (y = 0; y < r.height(); y++)
160 for (x = 0; x < r.width(); x++)
161 *ptr++ = palette[input->readU8()];
162 }
163 }
164
165 IMAGE_RECT(r, buf);
Peter Åstrand462753d2004-11-16 15:23:25 +0000166}
167
168#undef TIGHT_DECODE
169#undef READ_PIXEL
170#undef PIXEL_T
171}