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