blob: 3e5aa3e74190ed4be711430f03d9d32c20349ffe [file] [log] [blame]
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +00001/* Copyright (C) 2004-2005 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// PollingManager.cxx
20//
21
Constantin Kaplinskyce676c62006-02-08 13:36:58 +000022// FIXME: Don't compare pixels already marked as changed.
23// FIXME: Use Image::copyPixels() instead of Image::updateRect()?
24// In that case, note the fact that arguments are not checked.
25
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +000026#include <stdio.h>
27#include <string.h>
28#include <sys/time.h>
29#include <X11/Xlib.h>
30#include <rfb/VNCServer.h>
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +000031#include <rfb/Configuration.h>
Constantin Kaplinskyce676c62006-02-08 13:36:58 +000032#include <rfb/ServerCore.h>
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +000033
34#include <x0vncserver/Image.h>
35#include <x0vncserver/PollingManager.h>
36
Constantin Kaplinskyce676c62006-02-08 13:36:58 +000037BoolParameter PollingManager::pollPointer
38("PollPointer",
39 "Poll area under the pointer with higher priority",
40 true);
41
42IntParameter PollingManager::pollingType
43("PollingType",
44 "Polling algorithm to use (0..3)",
45 3);
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +000046
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +000047const int PollingManager::m_pollingOrder[32] = {
48 0, 16, 8, 24, 4, 20, 12, 28,
49 10, 26, 18, 2, 22, 6, 30, 14,
50 1, 17, 9, 25, 7, 23, 15, 31,
51 19, 3, 27, 11, 29, 13, 5, 21
52};
53
54//
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +000055// Constructor.
56//
57// Note that dpy and image should remain valid during the object
58// lifetime, while factory is used only in the constructor itself.
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +000059//
60
61PollingManager::PollingManager(Display *dpy, Image *image,
62 ImageFactory *factory)
Constantin Kaplinskyce676c62006-02-08 13:36:58 +000063 : m_dpy(dpy), m_server(0), m_image(image), m_pointerPosKnown(false),
64 m_pollingStep(0)
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +000065{
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +000066 // Save width and height of the screen (and the image).
67 m_width = m_image->xim->width;
68 m_height = m_image->xim->height;
69
70 // Compute width and height in 32x32 tiles.
71 m_widthTiles = (m_width + 31) / 32;
72 m_heightTiles = (m_height + 31) / 32;
73
Constantin Kaplinskyce676c62006-02-08 13:36:58 +000074 // Create additional images used in the polling algorithm.
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +000075 // FIXME: verify that these images use the same pixel format as in m_image.
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +000076 m_rowImage = factory->newImage(m_dpy, m_width, 1);
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +000077 m_tileImage = factory->newImage(m_dpy, 32, 32);
Constantin Kaplinskyce676c62006-02-08 13:36:58 +000078 m_areaImage = factory->newImage(m_dpy, 128, 128);
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +000079
Constantin Kaplinskydc54fc12005-10-19 13:57:16 +000080 // FIXME: Extend the comment.
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +000081 // Create a matrix with one byte per each 32x32 tile. It will be
82 // used to limit the rate of updates on continuously-changed screen
83 // areas (like video).
84 int numTiles = m_widthTiles * m_heightTiles;
85 m_statusMatrix = new char[numTiles];
86 memset(m_statusMatrix, 0, numTiles);
Constantin Kaplinskydc54fc12005-10-19 13:57:16 +000087
88 // FIXME: Extend the comment.
89 // Create a matrix with one byte per each 32x32 tile. It will be
90 // used to limit the rate of updates on continuously-changed screen
91 // areas (like video).
92 m_rateMatrix = new char[numTiles];
93 m_videoFlags = new char[numTiles];
94 m_changedFlags = new char[numTiles];
95 memset(m_rateMatrix, 0, numTiles);
96 memset(m_videoFlags, 0, numTiles);
97 memset(m_changedFlags, 0, numTiles);
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +000098}
99
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000100PollingManager::~PollingManager()
101{
Constantin Kaplinskydc54fc12005-10-19 13:57:16 +0000102 delete[] m_changedFlags;
103 delete[] m_videoFlags;
104 delete[] m_rateMatrix;
105
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000106 delete[] m_statusMatrix;
Constantin Kaplinskyce676c62006-02-08 13:36:58 +0000107
108 delete m_areaImage;
109 delete m_tileImage;
110 delete m_rowImage;
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000111}
112
113//
114// Register VNCServer object.
115//
116
117void PollingManager::setVNCServer(VNCServer *s)
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000118{
119 m_server = s;
120}
121
122//
Constantin Kaplinskyce676c62006-02-08 13:36:58 +0000123// Update current pointer position which may be used as a hint for
124// polling algorithms.
125//
126
127void PollingManager::setPointerPos(const Point &pos)
128{
129 m_pointerPos = pos;
130 m_pointerPosKnown = true;
131}
132
133//
134// Indicate that current pointer position is unknown.
135// FIXME: Perhaps this should be done automatically after a number of
136// polling cycles if the cursor position have not been changed?
137//
138
139void PollingManager::unsetPointerPos()
140{
141 m_pointerPosKnown = false;
142}
143
144//
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000145// DEBUG: a version of poll() measuring time spent in the function.
146//
147
148void PollingManager::pollDebug()
149{
150 struct timeval timeSaved, timeNow;
151 struct timezone tz;
152 timeSaved.tv_sec = 0;
153 timeSaved.tv_usec = 0;
154 gettimeofday(&timeSaved, &tz);
155
156 poll();
157
158 gettimeofday(&timeNow, &tz);
159 int diff = (int)((timeNow.tv_usec - timeSaved.tv_usec + 500) / 1000 +
160 (timeNow.tv_sec - timeSaved.tv_sec) * 1000);
161 if (diff != 0)
162 fprintf(stderr, "DEBUG: poll(): %4d ms\n", diff);
163}
164
165//
166// Search for changed rectangles on the screen.
167//
168
169void PollingManager::poll()
170{
Constantin Kaplinsky98bfcd32006-02-10 06:39:35 +0000171 // First step: full-screen polling.
172
173 bool changes1 = false;
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000174
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000175 switch((int)pollingType) {
176 case 0:
Constantin Kaplinsky98bfcd32006-02-10 06:39:35 +0000177 changes1 = poll_Dumb();
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000178 break;
179 case 1:
Constantin Kaplinsky98bfcd32006-02-10 06:39:35 +0000180 changes1 = poll_Traditional();
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000181 break;
Constantin Kaplinskydc54fc12005-10-19 13:57:16 +0000182 case 2:
Constantin Kaplinsky98bfcd32006-02-10 06:39:35 +0000183 changes1 = poll_SkipCycles();
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000184 break;
Constantin Kaplinskydc54fc12005-10-19 13:57:16 +0000185//case 3:
186 default:
Constantin Kaplinsky98bfcd32006-02-10 06:39:35 +0000187 changes1 = poll_DetectVideo();
Constantin Kaplinskydc54fc12005-10-19 13:57:16 +0000188 break;
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000189 }
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000190
Constantin Kaplinsky98bfcd32006-02-10 06:39:35 +0000191 // Second step: optional thorough polling of the area around the pointer.
192
193 bool changes2 = pollPointer && m_pointerPosKnown && pollPointerArea();
194
195 // Update if needed.
196
197 if (changes1 || changes2)
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000198 m_server->tryUpdate();
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000199}
200
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000201bool PollingManager::poll_DetectVideo()
Constantin Kaplinskydc54fc12005-10-19 13:57:16 +0000202{
203 if (!m_server)
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000204 return false;
Constantin Kaplinskydc54fc12005-10-19 13:57:16 +0000205
206 const int GRAND_STEP_DIVISOR = 8;
207 const int VIDEO_THRESHOLD_0 = 3;
208 const int VIDEO_THRESHOLD_1 = 5;
209
210 bool grandStep = (m_pollingStep % GRAND_STEP_DIVISOR == 0);
211
Constantin Kaplinskyce676c62006-02-08 13:36:58 +0000212 // FIXME: Save shortcuts in member variables?
Constantin Kaplinskydc54fc12005-10-19 13:57:16 +0000213 int scanLine = m_pollingOrder[m_pollingStep++ % 32];
214 int bytesPerPixel = m_image->xim->bits_per_pixel / 8;
215 int bytesPerLine = m_image->xim->bytes_per_line;
216
217 Rect rect;
218 int nTilesChanged = 0;
219 int idx = 0;
220
221 for (int y = 0; y * 32 < m_height; y++) {
222 int tile_h = (m_height - y * 32 >= 32) ? 32 : m_height - y * 32;
223 if (scanLine >= tile_h)
224 break;
225 int scan_y = y * 32 + scanLine;
226 m_rowImage->get(DefaultRootWindow(m_dpy), 0, scan_y);
227 char *ptr_old = m_image->xim->data + scan_y * bytesPerLine;
228 char *ptr_new = m_rowImage->xim->data;
229 for (int x = 0; x * 32 < m_width; x++) {
230 int tile_w = (m_width - x * 32 >= 32) ? 32 : m_width - x * 32;
231 int nBytes = tile_w * bytesPerPixel;
232
233 char wasChanged = (memcmp(ptr_old, ptr_new, nBytes) != 0);
234 m_rateMatrix[idx] += wasChanged;
235
236 if (grandStep) {
237 if (m_rateMatrix[idx] <= VIDEO_THRESHOLD_0) {
238 m_videoFlags[idx] = 0;
239 } else if (m_rateMatrix[idx] >= VIDEO_THRESHOLD_1) {
240 m_videoFlags[idx] = 1;
241 }
242 m_rateMatrix[idx] = 0;
243 }
244
245 m_changedFlags[idx] |= wasChanged;
246 if ( m_changedFlags[idx] && (!m_videoFlags[idx] || grandStep) ) {
247 if (tile_w == 32 && tile_h == 32) {
248 m_tileImage->get(DefaultRootWindow(m_dpy), x * 32, y * 32);
249 } else {
250 m_tileImage->get(DefaultRootWindow(m_dpy), x * 32, y * 32,
251 tile_w, tile_h);
252 }
253 m_image->updateRect(m_tileImage, x * 32, y * 32);
254 rect.setXYWH(x * 32, y * 32, tile_w, tile_h);
255 m_server->add_changed(rect);
256 nTilesChanged++;
257 m_changedFlags[idx] = 0;
258 }
259
Constantin Kaplinskydc54fc12005-10-19 13:57:16 +0000260 ptr_old += nBytes;
261 ptr_new += nBytes;
262 idx++;
263 }
264 }
265
Constantin Kaplinsky6df69902006-02-03 11:42:00 +0000266 if (grandStep)
267 adjustVideoArea();
268
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000269 return (nTilesChanged != 0);
Constantin Kaplinskydc54fc12005-10-19 13:57:16 +0000270}
271
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000272bool PollingManager::poll_SkipCycles()
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000273{
274 if (!m_server)
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000275 return false;
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000276
277 enum {
278 NOT_CHANGED, CHANGED_ONCE, CHANGED_AGAIN
279 };
280
281 bool grandStep = (m_pollingStep % 8 == 0);
282
283 int nTilesChanged = 0;
284 int scanLine = m_pollingOrder[m_pollingStep++ % 32];
285 int bytesPerPixel = m_image->xim->bits_per_pixel / 8;
286 int bytesPerLine = m_image->xim->bytes_per_line;
287 char *pstatus = m_statusMatrix;
288 bool wasChanged;
289 Rect rect;
290
291 for (int y = 0; y * 32 < m_height; y++) {
292 int tile_h = (m_height - y * 32 >= 32) ? 32 : m_height - y * 32;
293 if (scanLine >= tile_h)
294 scanLine %= tile_h;
295 int scan_y = y * 32 + scanLine;
296 m_rowImage->get(DefaultRootWindow(m_dpy), 0, scan_y);
297 char *ptr_old = m_image->xim->data + scan_y * bytesPerLine;
298 char *ptr_new = m_rowImage->xim->data;
299 for (int x = 0; x * 32 < m_width; x++) {
300 int tile_w = (m_width - x * 32 >= 32) ? 32 : m_width - x * 32;
301 int nBytes = tile_w * bytesPerPixel;
302
303 if (grandStep || *pstatus != CHANGED_AGAIN) {
304 wasChanged = (*pstatus == CHANGED_AGAIN) ?
305 true : (memcmp(ptr_old, ptr_new, nBytes) != 0);
306 if (wasChanged) {
307 if (grandStep || *pstatus == NOT_CHANGED) {
308 if (tile_w == 32 && tile_h == 32) {
309 m_tileImage->get(DefaultRootWindow(m_dpy), x * 32, y * 32);
310 } else {
311 m_tileImage->get(DefaultRootWindow(m_dpy), x * 32, y * 32,
312 tile_w, tile_h);
313 }
314 m_image->updateRect(m_tileImage, x * 32, y * 32);
315 rect.setXYWH(x * 32, y * 32, tile_w, tile_h);
316 m_server->add_changed(rect);
317 nTilesChanged++;
318 *pstatus = CHANGED_ONCE;
319 } else {
320 *pstatus = CHANGED_AGAIN;
321 }
322 } else if (grandStep) {
323 *pstatus = NOT_CHANGED;
324 }
325 }
326
327 ptr_old += nBytes;
328 ptr_new += nBytes;
329 pstatus++;
330 }
331 }
332
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000333 return (nTilesChanged != 0);
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000334}
335
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000336bool PollingManager::poll_Traditional()
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000337{
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000338 if (!m_server)
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000339 return false;
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000340
341 int nTilesChanged = 0;
342 int scanLine = m_pollingOrder[m_pollingStep++ % 32];
343 int bytesPerPixel = m_image->xim->bits_per_pixel / 8;
344 int bytesPerLine = m_image->xim->bytes_per_line;
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000345 Rect rect;
346
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000347 for (int y = 0; y * 32 < m_height; y++) {
348 int tile_h = (m_height - y * 32 >= 32) ? 32 : m_height - y * 32;
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000349 if (scanLine >= tile_h)
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000350 break;
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000351 int scan_y = y * 32 + scanLine;
352 m_rowImage->get(DefaultRootWindow(m_dpy), 0, scan_y);
353 char *ptr_old = m_image->xim->data + scan_y * bytesPerLine;
354 char *ptr_new = m_rowImage->xim->data;
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000355 for (int x = 0; x * 32 < m_width; x++) {
356 int tile_w = (m_width - x * 32 >= 32) ? 32 : m_width - x * 32;
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000357 int nBytes = tile_w * bytesPerPixel;
358 if (memcmp(ptr_old, ptr_new, nBytes)) {
359 if (tile_w == 32 && tile_h == 32) {
360 m_tileImage->get(DefaultRootWindow(m_dpy), x * 32, y * 32);
361 } else {
362 m_tileImage->get(DefaultRootWindow(m_dpy), x * 32, y * 32,
363 tile_w, tile_h);
364 }
365 m_image->updateRect(m_tileImage, x * 32, y * 32);
366 rect.setXYWH(x * 32, y * 32, tile_w, tile_h);
367 m_server->add_changed(rect);
368 nTilesChanged++;
369 }
370 ptr_old += nBytes;
371 ptr_new += nBytes;
372 }
373 }
374
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000375 return (nTilesChanged != 0);
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000376}
377
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000378//
379// Simplest polling method, from the original x0vncserver of VNC4.
380//
381
382bool PollingManager::poll_Dumb()
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000383{
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000384 if (!m_server)
385 return false;
386
387 m_image->get(DefaultRootWindow(m_dpy));
388 Rect rect(0, 0, m_width, m_height);
389 m_server->add_changed(rect);
390
Constantin Kaplinskyce676c62006-02-08 13:36:58 +0000391 // Report that some changes have been detected.
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000392 return true;
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000393}
Constantin Kaplinsky6df69902006-02-03 11:42:00 +0000394
395//
Constantin Kaplinskyce676c62006-02-08 13:36:58 +0000396// Compute coordinates of the rectangle around the pointer.
Constantin Kaplinsky6df69902006-02-03 11:42:00 +0000397//
Constantin Kaplinsky98bfcd32006-02-10 06:39:35 +0000398// ASSUMES: (m_pointerPosKnown != false)
399//
Constantin Kaplinsky6df69902006-02-03 11:42:00 +0000400
Constantin Kaplinskyce676c62006-02-08 13:36:58 +0000401void PollingManager::computePointerArea(Rect *r)
402{
403 int x = m_pointerPos.x - 64;
404 int y = m_pointerPos.y - 64;
405 int w = 128;
406 int h = 128;
407 if (x < 0) {
408 w += x; x = 0;
409 }
410 if (x + w > m_width) {
411 w = m_width - x;
412 }
413 if (y < 0) {
414 h += y; y = 0;
415 }
416 if (y + h > m_height) {
417 h = m_height - y;
418 }
419
420 r->setXYWH(x, y, w, h);
421}
422
423//
424// Poll the area under current pointer position. Each pixel of the
425// area should be compared. Using such polling option gives higher
426// priority to screen area under the pointer.
427//
428// ASSUMES: (m_server != NULL && m_pointerPosKnown != false)
429//
430
431bool PollingManager::pollPointerArea()
432{
433 Rect r;
434 computePointerArea(&r);
435
436 // Shortcuts for coordinates.
437 int x = r.tl.x, y = r.tl.y;
438 int w = r.width(), h = r.height();
439
440 // Get new pixels.
441 if (w == 128 && h == 128) {
442 m_areaImage->get(DefaultRootWindow(m_dpy), x, y);
443 } else {
444 m_areaImage->get(DefaultRootWindow(m_dpy), x, y, w, h);
445 }
446
447 // Now, try to minimize the rectangle by cutting out unchanged
448 // borders (at top and bottom).
449 //
450 // FIXME: Perhaps we should work on 32x32 tiles (properly aligned)
451 // to produce a region instead of a rectangle. If there would
452 // be just one universal polling algorithm, it could be
453 // better to integrate pointer area polling into that
454 // algorithm, instead of a separate pollPointerArea()
455 // function.
456
457 // Shortcuts.
458 int bytesPerPixel = m_image->xim->bits_per_pixel / 8;
459 int oldBytesPerLine = m_image->xim->bytes_per_line;
460 int newBytesPerLine = m_areaImage->xim->bytes_per_line;
461 char *oldPtr = m_image->xim->data + y * oldBytesPerLine + x * bytesPerPixel;
462 char *newPtr = m_areaImage->xim->data;
463
464 // Check and cut out unchanged rows at the top.
465 int ty;
466 for (ty = 0; ty < h; ty++) {
467 if (memcmp(oldPtr, newPtr, w * bytesPerPixel) != 0)
468 break;
469 oldPtr += oldBytesPerLine;
470 newPtr += newBytesPerLine;
471 }
472 if (ty == h) {
473 return false; // no changes at all
474 }
475 y += ty; h -= ty;
476
477 // Check and cut out unchanged rows at the bottom.
478 oldPtr = m_image->xim->data + (y+h-1) * oldBytesPerLine + x * bytesPerPixel;
479 newPtr = m_areaImage->xim->data + (ty+h-1) * newBytesPerLine;
480 int by;
481 for (by = 0; by < h - 1; by++) {
482 if (memcmp(oldPtr, newPtr, w * bytesPerPixel) != 0)
483 break;
484 oldPtr -= oldBytesPerLine;
485 newPtr -= newBytesPerLine;
486 }
487 h -= by;
488
489 // Copy pixels.
490 m_image->updateRect(m_areaImage, x, y, 0, ty, w, h);
491
492 // Report updates to the server.
493 Rect rect(x, y, x+w, y+h);
494 m_server->add_changed(rect);
495 return true;
496}
497
498//
499// Make video area pattern more regular.
500//
501// FIXME: Replace the above with a normal comment.
502// FIXME: Is the function efficient enough?
503//
504
Constantin Kaplinsky6df69902006-02-03 11:42:00 +0000505void PollingManager::adjustVideoArea()
506{
507 char newFlags[m_widthTiles * m_heightTiles];
508 char *ptr = newFlags;
509 int x, y;
510
511 // DEBUG:
512 // int nVideoTiles = 0;
513
514 for (y = 0; y < m_heightTiles; y++) {
515 for (x = 0; x < m_widthTiles; x++) {
516
517 // DEBUG:
518 // nVideoTiles += m_videoFlags[y * m_widthTiles + x];
519
520 int weightedSum = 0, n;
521 if (y > 0 && x > 0) {
522 n = (m_videoFlags[ y * m_widthTiles + (x-1)] +
523 m_videoFlags[(y-1) * m_widthTiles + (x-1)] +
524 m_videoFlags[(y-1) * m_widthTiles + x ]);
525 if (n == 3) {
526 *ptr++ = 1;
527 continue;
528 }
529 weightedSum += n;
530 }
531 if (y > 0 && x < m_widthTiles - 1) {
532 n = (m_videoFlags[ y * m_widthTiles + (x+1)] +
533 m_videoFlags[(y-1) * m_widthTiles + (x+1)] +
534 m_videoFlags[(y-1) * m_widthTiles + x ]);
535 if (n == 3) {
536 *ptr++ = 1;
537 continue;
538 }
539 weightedSum += n;
540 }
541 if (y < m_heightTiles - 1 && x > 0) {
542 n = (m_videoFlags[ y * m_widthTiles + (x-1)] +
543 m_videoFlags[(y+1) * m_widthTiles + (x-1)] +
544 m_videoFlags[(y+1) * m_widthTiles + x ]);
545 if (n == 3) {
546 *ptr++ = 1;
547 continue;
548 }
549 weightedSum += n;
550 }
551 if (y < m_heightTiles - 1 && x < m_widthTiles - 1) {
552 n = (m_videoFlags[ y * m_widthTiles + (x+1)] +
553 m_videoFlags[(y+1) * m_widthTiles + (x+1)] +
554 m_videoFlags[(y+1) * m_widthTiles + x ]);
555 if (n == 3) {
556 *ptr++ = 1;
557 continue;
558 }
559 weightedSum += n;
560 }
561 *ptr++ = (weightedSum <= 3) ? 0 : m_videoFlags[y * m_widthTiles + x];
562 }
563 }
564
565 /*
566 /// DEBUG: ------------------------------------------------------
567 if (nVideoTiles) {
568 for (y = 0; y < m_heightTiles; y++) {
569 for (x = 0; x < m_widthTiles; x++) {
570 printf("%c", m_videoFlags[y * m_widthTiles + x] ? '@' : ':');
571 }
572 printf(" ");
573 for (x = 0; x < m_widthTiles; x++) {
574 printf("%c", newFlags[y * m_widthTiles + x] ? '@' : ':');
575 }
576 printf("\n");
577 }
578 printf("\n");
579 }
580 /// -------------------------------------------------------------
581 */
582
583 memcpy(m_videoFlags, newFlags, m_widthTiles * m_heightTiles);
584}