blob: c8bbee7af3e9b1f511694eaf332c1b5c8cb11d2e [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 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// RRE encoding function.
20//
Pierre Ossmanbcc295e2014-02-12 13:16:43 +010021// This file is #included after having set the following macro:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000022// BPP - 8, 16 or 32
23//
24// The data argument to RRE_ENCODE contains the pixel data, and it writes the
25// encoded version to the given OutStream. If the encoded version exceeds w*h
26// it aborts and returns -1, otherwise it returns the number of subrectangles.
27//
28
29#include <rdr/OutStream.h>
30
31namespace rfb {
32
33// CONCAT2E concatenates its arguments, expanding them if they are macros
34
35#ifndef CONCAT2E
36#define CONCAT2(a,b) a##b
37#define CONCAT2E(a,b) CONCAT2(a,b)
38#endif
39
40#define PIXEL_T rdr::CONCAT2E(U,BPP)
41#define WRITE_PIXEL CONCAT2E(writeOpaque,BPP)
42#define RRE_ENCODE CONCAT2E(rreEncode,BPP)
43
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000044int RRE_ENCODE (PIXEL_T* data, int w, int h, rdr::OutStream* os, PIXEL_T bg)
45{
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000046 os->WRITE_PIXEL(bg);
47
48 int nSubrects = 0;
49
50 for (int y = 0; y < h; y++)
51 {
52 int x = 0;
53 while (x < w) {
54 if (*data == bg) {
55 x++;
56 data++;
57 continue;
58 }
59
60 // Find horizontal subrect first
61 PIXEL_T* ptr = data+1;
62 PIXEL_T* eol = data+w-x;
63 while (ptr < eol && *ptr == *data) ptr++;
64 int sw = ptr - data;
65
66 ptr = data + w;
67 int sh = 1;
68 while (sh < h-y) {
69 eol = ptr + sw;
70 while (ptr < eol)
71 if (*ptr++ != *data) goto endOfHorizSubrect;
72 ptr += w - sw;
73 sh++;
74 }
75 endOfHorizSubrect:
76
77 // Find vertical subrect
78 int vh;
79 for (vh = sh; vh < h-y; vh++)
80 if (data[vh*w] != *data) break;
81
82 if (vh != sh) {
83 ptr = data+1;
84 int vw;
85 for (vw = 1; vw < sw; vw++) {
86 for (int i = 0; i < vh; i++)
87 if (ptr[i*w] != *data) goto endOfVertSubrect;
88 ptr++;
89 }
90 endOfVertSubrect:
91
92 // If vertical subrect bigger than horizontal then use that.
93 if (sw*sh < vw*vh) {
94 sw = vw;
95 sh = vh;
96 }
97 }
98
99 nSubrects++;
100 os->WRITE_PIXEL(*data);
101 os->writeU16(x);
102 os->writeU16(y);
103 os->writeU16(sw);
104 os->writeU16(sh);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000105
106 ptr = data+w;
107 PIXEL_T* eor = data+w*sh;
108 while (ptr < eor) {
109 eol = ptr + sw;
110 while (ptr < eol) *ptr++ = bg;
111 ptr += w - sw;
112 }
113 x += sw;
114 data += sw;
115 }
116 }
117
118 return nSubrects;
119}
120
121#undef PIXEL_T
122#undef WRITE_PIXEL
123#undef RRE_ENCODE
124}