blob: 0b9b8dca77c6c545cca8f2ea4c3c90ef282d3a08 [file] [log] [blame]
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +00001/* Copyright (C) 2002-2003 RealVNC Ltd. 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// Hextile encoding function.
20//
21// This file is #included after having set the following macros:
22// BPP - 8, 16 or 32
23// EXTRA_ARGS - optional extra arguments
24// GET_IMAGE_INTO_BUF - gets a rectangle of pixel data into a buffer
25
26#include <rdr/OutStream.h>
27#include <rfb/hextileConstants.h>
28
29namespace rfb {
30
31// CONCAT2E concatenates its arguments, expanding them if they are macros
32
33#ifndef CONCAT2E
34#define CONCAT2(a,b) a##b
35#define CONCAT2E(a,b) CONCAT2(a,b)
36#endif
37
38#define PIXEL_T rdr::CONCAT2E(U,BPP)
39#define WRITE_PIXEL CONCAT2E(writeOpaque,BPP)
40#define HEXTILE_ENCODE CONCAT2E(hextileEncode,BPP)
41#define HEXTILE_ENCODE_TILE CONCAT2E(hextileEncodeTile,BPP)
42#define TEST_TILE_TYPE CONCAT2E(hextileTestTileType,BPP)
43
44int TEST_TILE_TYPE (PIXEL_T* data, int w, int h, PIXEL_T* bg, PIXEL_T* fg);
45int HEXTILE_ENCODE_TILE (PIXEL_T* data, int w, int h, int tileType,
46 rdr::U8* encoded, PIXEL_T bg);
47
48void HEXTILE_ENCODE(const Rect& r, rdr::OutStream* os
49#ifdef EXTRA_ARGS
50 , EXTRA_ARGS
51#endif
52 )
53{
54 Rect t;
55 PIXEL_T buf[256];
56 PIXEL_T oldBg = 0, oldFg = 0;
57 bool oldBgValid = false;
58 bool oldFgValid = false;
59 rdr::U8 encoded[256*(BPP/8)];
60
61 for (t.tl.y = r.tl.y; t.tl.y < r.br.y; t.tl.y += 16) {
62
Peter Åstrand0f49e222005-01-13 22:11:30 +000063 t.br.y = vncmin(r.br.y, t.tl.y + 16);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000064
65 for (t.tl.x = r.tl.x; t.tl.x < r.br.x; t.tl.x += 16) {
66
Peter Åstrand0f49e222005-01-13 22:11:30 +000067 t.br.x = vncmin(r.br.x, t.tl.x + 16);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000068
69 GET_IMAGE_INTO_BUF(t,buf);
70
71 PIXEL_T bg, fg;
72 int tileType = TEST_TILE_TYPE(buf, t.width(), t.height(), &bg, &fg);
73
74 if (!oldBgValid || oldBg != bg) {
75 tileType |= hextileBgSpecified;
76 oldBg = bg;
77 oldBgValid = true;
78 }
79
80 int encodedLen = 0;
81
82 if (tileType & hextileAnySubrects) {
83
84 if (tileType & hextileSubrectsColoured) {
85 oldFgValid = false;
86 } else {
87 if (!oldFgValid || oldFg != fg) {
88 tileType |= hextileFgSpecified;
89 oldFg = fg;
90 oldFgValid = true;
91 }
92 }
93
94 encodedLen = HEXTILE_ENCODE_TILE(buf, t.width(), t.height(), tileType,
95 encoded, bg);
96
97 if (encodedLen < 0) {
98 GET_IMAGE_INTO_BUF(t,buf);
99 os->writeU8(hextileRaw);
100 os->writeBytes(buf, t.width() * t.height() * (BPP/8));
101 oldBgValid = oldFgValid = false;
102 continue;
103 }
104 }
105
106 os->writeU8(tileType);
107 if (tileType & hextileBgSpecified) os->WRITE_PIXEL(bg);
108 if (tileType & hextileFgSpecified) os->WRITE_PIXEL(fg);
109 if (tileType & hextileAnySubrects) os->writeBytes(encoded, encodedLen);
110 }
111 }
112}
113
114
115int HEXTILE_ENCODE_TILE (PIXEL_T* data, int w, int h, int tileType,
116 rdr::U8* encoded, PIXEL_T bg)
117{
118 rdr::U8* nSubrectsPtr = encoded;
119 *nSubrectsPtr = 0;
120 encoded++;
121
122 for (int y = 0; y < h; y++)
123 {
124 int x = 0;
125 while (x < w) {
126 if (*data == bg) {
127 x++;
128 data++;
129 continue;
130 }
131
132 // Find horizontal subrect first
133 PIXEL_T* ptr = data+1;
134 PIXEL_T* eol = data+w-x;
135 while (ptr < eol && *ptr == *data) ptr++;
136 int sw = ptr - data;
137
138 ptr = data + w;
139 int sh = 1;
140 while (sh < h-y) {
141 eol = ptr + sw;
142 while (ptr < eol)
143 if (*ptr++ != *data) goto endOfHorizSubrect;
144 ptr += w - sw;
145 sh++;
146 }
147 endOfHorizSubrect:
148
149 // Find vertical subrect
150 int vh;
151 for (vh = sh; vh < h-y; vh++)
152 if (data[vh*w] != *data) break;
153
154 if (vh != sh) {
155 ptr = data+1;
156 int vw;
157 for (vw = 1; vw < sw; vw++) {
158 for (int i = 0; i < vh; i++)
159 if (ptr[i*w] != *data) goto endOfVertSubrect;
160 ptr++;
161 }
162 endOfVertSubrect:
163
164 // If vertical subrect bigger than horizontal then use that.
165 if (sw*sh < vw*vh) {
166 sw = vw;
167 sh = vh;
168 }
169 }
170
171 (*nSubrectsPtr)++;
172
173 if (tileType & hextileSubrectsColoured) {
174 if (encoded - nSubrectsPtr + (BPP/8) > w*h*(BPP/8)) return -1;
175#if (BPP == 8)
176 *encoded++ = *data;
177#elif (BPP == 16)
178 *encoded++ = ((rdr::U8*)data)[0];
179 *encoded++ = ((rdr::U8*)data)[1];
180#elif (BPP == 32)
181 *encoded++ = ((rdr::U8*)data)[0];
182 *encoded++ = ((rdr::U8*)data)[1];
183 *encoded++ = ((rdr::U8*)data)[2];
184 *encoded++ = ((rdr::U8*)data)[3];
185#endif
186 }
187
188 if (encoded - nSubrectsPtr + 2 > w*h*(BPP/8)) return -1;
189 *encoded++ = (x << 4) | y;
190 *encoded++ = ((sw-1) << 4) | (sh-1);
191
192 ptr = data+w;
193 PIXEL_T* eor = data+w*sh;
194 while (ptr < eor) {
195 eol = ptr + sw;
196 while (ptr < eol) *ptr++ = bg;
197 ptr += w - sw;
198 }
199 x += sw;
200 data += sw;
201 }
202 }
203 return encoded - nSubrectsPtr;
204}
205
206
207int TEST_TILE_TYPE (PIXEL_T* data, int w, int h, PIXEL_T* bg, PIXEL_T* fg)
208{
209 int tileType = 0;
210 PIXEL_T pix1 = *data, pix2 = 0;
211 int count1 = 0, count2 = 0;
212 PIXEL_T* end = data + w*h;
213
214 for (PIXEL_T* ptr = data; ptr < end; ptr++) {
215 if (*ptr == pix1) {
216 count1++;
217 continue;
218 }
219
220 if (count2 == 0) {
221 tileType |= hextileAnySubrects;
222 pix2 = *ptr;
223 }
224
Constantin Kaplinsky7a33dc22005-08-29 10:44:57 +0000225 if (*ptr == pix2) {
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000226 count2++;
227 continue;
228 }
229
230 tileType |= hextileSubrectsColoured;
231 break;
232 }
233
234 if (count1 >= count2) {
235 *bg = pix1; *fg = pix2;
236 } else {
237 *bg = pix2; *fg = pix1;
238 }
239 return tileType;
240}
241
242#undef PIXEL_T
243#undef WRITE_PIXEL
244#undef HEXTILE_ENCODE
245#undef HEXTILE_ENCODE_TILE
246#undef TEST_TILE_TYPE
247}