blob: 2efd74eea9f91aa3cd90e3671681ba74ed4a7ad7 [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//
Pierre Ossmanbcc295e2014-02-12 13:16:43 +010022// This file is #included after having set the following macro:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000023// BPP - 8, 16 or 32
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000024
25#include <rdr/OutStream.h>
26#include <rfb/hextileConstants.h>
27
28namespace 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
43int TEST_TILE_TYPE (PIXEL_T* data, int w, int h, PIXEL_T* bg, PIXEL_T* fg);
44int HEXTILE_ENCODE_TILE (PIXEL_T* data, int w, int h, int tileType,
45 rdr::U8* encoded, PIXEL_T bg);
46
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +020047void HEXTILE_ENCODE(const Rect& r, rdr::OutStream* os,
48 const PixelFormat& pf, PixelBuffer* pb)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000049{
50 Rect t;
51 PIXEL_T buf[256];
52 PIXEL_T oldBg = 0, oldFg = 0;
53 bool oldBgValid = false;
54 bool oldFgValid = false;
55 rdr::U8 encoded[256*(BPP/8)];
56
57 for (t.tl.y = r.tl.y; t.tl.y < r.br.y; t.tl.y += 16) {
58
59 t.br.y = __rfbmin(r.br.y, t.tl.y + 16);
60
61 for (t.tl.x = r.tl.x; t.tl.x < r.br.x; t.tl.x += 16) {
62
63 t.br.x = __rfbmin(r.br.x, t.tl.x + 16);
64
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +020065 pb->getImage(pf, buf, t);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000066
DRCbf380e72011-11-03 17:51:01 +000067 PIXEL_T bg = 0, fg = 0;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000068 int tileType = TEST_TILE_TYPE(buf, t.width(), t.height(), &bg, &fg);
69
70 if (!oldBgValid || oldBg != bg) {
71 tileType |= hextileBgSpecified;
72 oldBg = bg;
73 oldBgValid = true;
74 }
75
76 int encodedLen = 0;
77
78 if (tileType & hextileAnySubrects) {
79
80 if (tileType & hextileSubrectsColoured) {
81 oldFgValid = false;
82 } else {
83 if (!oldFgValid || oldFg != fg) {
84 tileType |= hextileFgSpecified;
85 oldFg = fg;
86 oldFgValid = true;
87 }
88 }
89
90 encodedLen = HEXTILE_ENCODE_TILE(buf, t.width(), t.height(), tileType,
91 encoded, bg);
92
93 if (encodedLen < 0) {
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +020094 pb->getImage(pf, buf, t);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000095 os->writeU8(hextileRaw);
96 os->writeBytes(buf, t.width() * t.height() * (BPP/8));
97 oldBgValid = oldFgValid = false;
98 continue;
99 }
100 }
101
102 os->writeU8(tileType);
103 if (tileType & hextileBgSpecified) os->WRITE_PIXEL(bg);
104 if (tileType & hextileFgSpecified) os->WRITE_PIXEL(fg);
105 if (tileType & hextileAnySubrects) os->writeBytes(encoded, encodedLen);
106 }
107 }
108}
109
110
111int HEXTILE_ENCODE_TILE (PIXEL_T* data, int w, int h, int tileType,
112 rdr::U8* encoded, PIXEL_T bg)
113{
114 rdr::U8* nSubrectsPtr = encoded;
115 *nSubrectsPtr = 0;
116 encoded++;
117
118 for (int y = 0; y < h; y++)
119 {
120 int x = 0;
121 while (x < w) {
122 if (*data == bg) {
123 x++;
124 data++;
125 continue;
126 }
127
128 // Find horizontal subrect first
129 PIXEL_T* ptr = data+1;
130 PIXEL_T* eol = data+w-x;
131 while (ptr < eol && *ptr == *data) ptr++;
132 int sw = ptr - data;
133
134 ptr = data + w;
135 int sh = 1;
136 while (sh < h-y) {
137 eol = ptr + sw;
138 while (ptr < eol)
139 if (*ptr++ != *data) goto endOfSubrect;
140 ptr += w - sw;
141 sh++;
142 }
143 endOfSubrect:
144
145 (*nSubrectsPtr)++;
146
147 if (tileType & hextileSubrectsColoured) {
148 if (encoded - nSubrectsPtr + (BPP/8) > w*h*(BPP/8)) return -1;
149#if (BPP == 8)
150 *encoded++ = *data;
151#elif (BPP == 16)
152 *encoded++ = ((rdr::U8*)data)[0];
153 *encoded++ = ((rdr::U8*)data)[1];
154#elif (BPP == 32)
155 *encoded++ = ((rdr::U8*)data)[0];
156 *encoded++ = ((rdr::U8*)data)[1];
157 *encoded++ = ((rdr::U8*)data)[2];
158 *encoded++ = ((rdr::U8*)data)[3];
159#endif
160 }
161
162 if (encoded - nSubrectsPtr + 2 > w*h*(BPP/8)) return -1;
163 *encoded++ = (x << 4) | y;
164 *encoded++ = ((sw-1) << 4) | (sh-1);
165
166 ptr = data+w;
167 PIXEL_T* eor = data+w*sh;
168 while (ptr < eor) {
169 eol = ptr + sw;
170 while (ptr < eol) *ptr++ = bg;
171 ptr += w - sw;
172 }
173 x += sw;
174 data += sw;
175 }
176 }
177 return encoded - nSubrectsPtr;
178}
179
180
181int TEST_TILE_TYPE (PIXEL_T* data, int w, int h, PIXEL_T* bg, PIXEL_T* fg)
182{
183 PIXEL_T pix1 = *data;
184 PIXEL_T* end = data + w * h;
185
186 PIXEL_T* ptr = data + 1;
187 while (ptr < end && *ptr == pix1)
188 ptr++;
189
190 if (ptr == end) {
191 *bg = pix1;
192 return 0; // solid-color tile
193 }
194
195 int count1 = ptr - data;
196 int count2 = 1;
197 PIXEL_T pix2 = *ptr++;
198 int tileType = hextileAnySubrects;
199
200 for (; ptr < end; ptr++) {
201 if (*ptr == pix1) {
202 count1++;
203 } else if (*ptr == pix2) {
204 count2++;
205 } else {
206 tileType |= hextileSubrectsColoured;
207 break;
208 }
209 }
210
211 if (count1 >= count2) {
212 *bg = pix1; *fg = pix2;
213 } else {
214 *bg = pix2; *fg = pix1;
215 }
216 return tileType;
217}
218
219#undef PIXEL_T
220#undef WRITE_PIXEL
221#undef HEXTILE_ENCODE
222#undef HEXTILE_ENCODE_TILE
223#undef TEST_TILE_TYPE
224}