1 /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 * Copyright (C) 2005 Constantin Kaplinsky. 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 // Hextile encoding function.
21 //
22 // This file is #included after having set the following macro:
23 // BPP - 8, 16 or 32
24
25 #include <rdr/OutStream.h>
26 #include <rfb/hextileConstants.h>
27
28 namespace rfb {
29
30 // CONCAT2E concatenates its arguments, expanding them if they are macros
31
32 #ifndef CONCAT2E
33 #define CONCAT2(a,b) a##b
34 #define CONCAT2E(a,b) CONCAT2(a,b)
35 #endif
36
37 #define PIXEL_T rdr::CONCAT2E(U,BPP)
38 #define WRITE_PIXEL CONCAT2E(writeOpaque,BPP)
39 #define HEXTILE_ENCODE CONCAT2E(hextileEncode,BPP)
40 #define HEXTILE_ENCODE_TILE CONCAT2E(hextileEncodeTile,BPP)
41 #define TEST_TILE_TYPE CONCAT2E(hextileTestTileType,BPP)
42
43 int TEST_TILE_TYPE (PIXEL_T* data, int w, int h, PIXEL_T* bg, PIXEL_T* fg);
44 int HEXTILE_ENCODE_TILE (PIXEL_T* data, int w, int h, int tileType,
45 rdr::U8* encoded, PIXEL_T bg);
46
HEXTILE_ENCODE(rdr::OutStream * os,const PixelBuffer * pb)47 void HEXTILE_ENCODE(rdr::OutStream* os, const PixelBuffer* pb)
48 {
49 Rect t;
50 PIXEL_T buf[256];
51 PIXEL_T oldBg = 0, oldFg = 0;
52 bool oldBgValid = false;
53 bool oldFgValid = false;
54 rdr::U8 encoded[256*(BPP/8)];
55
56 for (t.tl.y = 0; t.tl.y < pb->height(); t.tl.y += 16) {
57
58 t.br.y = __rfbmin(pb->height(), t.tl.y + 16);
59
60 for (t.tl.x = 0; t.tl.x < pb->width(); t.tl.x += 16) {
61
62 t.br.x = __rfbmin(pb->width(), t.tl.x + 16);
63
64 pb->getImage(buf, t);
65
66 PIXEL_T bg = 0, fg = 0;
67 int tileType = TEST_TILE_TYPE(buf, t.width(), t.height(), &bg, &fg);
68
69 if (!oldBgValid || oldBg != bg) {
70 tileType |= hextileBgSpecified;
71 oldBg = bg;
72 oldBgValid = true;
73 }
74
75 int encodedLen = 0;
76
77 if (tileType & hextileAnySubrects) {
78
79 if (tileType & hextileSubrectsColoured) {
80 oldFgValid = false;
81 } else {
82 if (!oldFgValid || oldFg != fg) {
83 tileType |= hextileFgSpecified;
84 oldFg = fg;
85 oldFgValid = true;
86 }
87 }
88
89 encodedLen = HEXTILE_ENCODE_TILE(buf, t.width(), t.height(), tileType,
90 encoded, bg);
91
92 if (encodedLen < 0) {
93 pb->getImage(buf, t);
94 os->writeU8(hextileRaw);
95 os->writeBytes(buf, t.width() * t.height() * (BPP/8));
96 oldBgValid = oldFgValid = false;
97 continue;
98 }
99 }
100
101 os->writeU8(tileType);
102 if (tileType & hextileBgSpecified) os->WRITE_PIXEL(bg);
103 if (tileType & hextileFgSpecified) os->WRITE_PIXEL(fg);
104 if (tileType & hextileAnySubrects) os->writeBytes(encoded, encodedLen);
105 }
106 }
107 }
108
109
HEXTILE_ENCODE_TILE(PIXEL_T * data,int w,int h,int tileType,rdr::U8 * encoded,PIXEL_T bg)110 int HEXTILE_ENCODE_TILE (PIXEL_T* data, int w, int h, int tileType,
111 rdr::U8* encoded, PIXEL_T bg)
112 {
113 rdr::U8* nSubrectsPtr = encoded;
114 *nSubrectsPtr = 0;
115 encoded++;
116
117 for (int y = 0; y < h; y++)
118 {
119 int x = 0;
120 while (x < w) {
121 if (*data == bg) {
122 x++;
123 data++;
124 continue;
125 }
126
127 // Find horizontal subrect first
128 PIXEL_T* ptr = data+1;
129 PIXEL_T* eol = data+w-x;
130 while (ptr < eol && *ptr == *data) ptr++;
131 int sw = ptr - data;
132
133 ptr = data + w;
134 int sh = 1;
135 while (sh < h-y) {
136 eol = ptr + sw;
137 while (ptr < eol)
138 if (*ptr++ != *data) goto endOfSubrect;
139 ptr += w - sw;
140 sh++;
141 }
142 endOfSubrect:
143
144 (*nSubrectsPtr)++;
145
146 if (tileType & hextileSubrectsColoured) {
147 if (encoded - nSubrectsPtr + (BPP/8) > w*h*(BPP/8)) return -1;
148 #if (BPP == 8)
149 *encoded++ = *data;
150 #elif (BPP == 16)
151 *encoded++ = ((rdr::U8*)data)[0];
152 *encoded++ = ((rdr::U8*)data)[1];
153 #elif (BPP == 32)
154 *encoded++ = ((rdr::U8*)data)[0];
155 *encoded++ = ((rdr::U8*)data)[1];
156 *encoded++ = ((rdr::U8*)data)[2];
157 *encoded++ = ((rdr::U8*)data)[3];
158 #endif
159 }
160
161 if (encoded - nSubrectsPtr + 2 > w*h*(BPP/8)) return -1;
162 *encoded++ = (x << 4) | y;
163 *encoded++ = ((sw-1) << 4) | (sh-1);
164
165 ptr = data+w;
166 PIXEL_T* eor = data+w*sh;
167 while (ptr < eor) {
168 eol = ptr + sw;
169 while (ptr < eol) *ptr++ = bg;
170 ptr += w - sw;
171 }
172 x += sw;
173 data += sw;
174 }
175 }
176 return encoded - nSubrectsPtr;
177 }
178
179
TEST_TILE_TYPE(PIXEL_T * data,int w,int h,PIXEL_T * bg,PIXEL_T * fg)180 int TEST_TILE_TYPE (PIXEL_T* data, int w, int h, PIXEL_T* bg, PIXEL_T* fg)
181 {
182 PIXEL_T pix1 = *data;
183 PIXEL_T* end = data + w * h;
184
185 PIXEL_T* ptr = data + 1;
186 while (ptr < end && *ptr == pix1)
187 ptr++;
188
189 if (ptr == end) {
190 *bg = pix1;
191 return 0; // solid-color tile
192 }
193
194 int count1 = ptr - data;
195 int count2 = 1;
196 PIXEL_T pix2 = *ptr++;
197 int tileType = hextileAnySubrects;
198
199 for (; ptr < end; ptr++) {
200 if (*ptr == pix1) {
201 count1++;
202 } else if (*ptr == pix2) {
203 count2++;
204 } else {
205 tileType |= hextileSubrectsColoured;
206 break;
207 }
208 }
209
210 if (count1 >= count2) {
211 *bg = pix1; *fg = pix2;
212 } else {
213 *bg = pix2; *fg = pix1;
214 }
215 return tileType;
216 }
217
218 #undef PIXEL_T
219 #undef WRITE_PIXEL
220 #undef HEXTILE_ENCODE
221 #undef HEXTILE_ENCODE_TILE
222 #undef TEST_TILE_TYPE
223 }
224