1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
/* Copyright 2013-2014 Pierre Ossman <ossman@cendio.se> for Cendio AB
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <rfb/PixelFormat.h>
static const rdr::U8 pixelRed = 0xf1;
static const rdr::U8 pixelGreen = 0xc3;
static const rdr::U8 pixelBlue = 0x97;
static const int fbWidth = 40;
static const int fbHeight = 30;
static const int fbArea = fbWidth * fbHeight;
// Maximum bpp, plus some room for unaligned fudging
static const int fbMalloc = (fbArea * 4) + 4;
typedef bool (*testfn) (const rfb::PixelFormat&, const rfb::PixelFormat&);
struct TestEntry {
const char *label;
testfn fn;
};
#define min(a,b) (((a) < (b)) ? (a) : (b))
namespace rfb {
void makePixel(const rfb::PixelFormat &pf,
rdr::U8 *buffer)
{
rfb::Pixel p;
p = 0;
p |= (pixelRed >> (8 - pf.redBits)) << pf.redShift;
p |= (pixelGreen >> (8 - pf.greenBits)) << pf.greenShift;
p |= (pixelBlue >> (8 - pf.blueBits)) << pf.blueShift;
// FIXME: Should we reimplement this as well?
pf.bufferFromPixel(buffer, p);
}
bool verifyPixel(const rfb::PixelFormat &dstpf,
const rfb::PixelFormat &srcpf,
const rdr::U8 *buffer)
{
rfb::Pixel p;
int r, g, b;
int re, ge, be;
// FIXME: Should we reimplement this as well?
p = dstpf.pixelFromBuffer(buffer);
r = (p >> dstpf.redShift) & dstpf.redMax;
g = (p >> dstpf.greenShift) & dstpf.greenMax;
b = (p >> dstpf.blueShift) & dstpf.blueMax;
r = (r * 255 + dstpf.redMax/2) / dstpf.redMax;
g = (g * 255 + dstpf.greenMax/2) / dstpf.greenMax;
b = (b * 255 + dstpf.blueMax/2) / dstpf.blueMax;
// The allowed error depends on:
//
// a) The number of bits the format can hold
// b) The number of bits the source format could hold
re = (1 << (8 - min(dstpf.redBits, srcpf.redBits))) - 1;
ge = (1 << (8 - min(dstpf.greenBits, srcpf.greenBits))) - 1;
be = (1 << (8 - min(dstpf.blueBits, srcpf.blueBits))) - 1;
if (abs(r - pixelRed) > re)
return false;
if (abs(g - pixelGreen) > ge)
return false;
if (abs(b - pixelBlue) > be)
return false;
return true;
}
}
using rfb::makePixel;
using rfb::verifyPixel;
static bool testPixel(const rfb::PixelFormat &dstpf,
const rfb::PixelFormat &srcpf)
{
rfb::Pixel p;
rdr::U8 buffer[4];
makePixel(srcpf, buffer);
p = srcpf.pixelFromBuffer(buffer);
p = dstpf.pixelFromPixel(srcpf, p);
memset(buffer, 0, sizeof(buffer));
dstpf.bufferFromPixel(buffer, p);
if (!verifyPixel(dstpf, srcpf, buffer))
return false;
return true;
}
static bool testBuffer(const rfb::PixelFormat &dstpf,
const rfb::PixelFormat &srcpf)
{
int i, x, y, unaligned;
rdr::U8 bufIn[fbMalloc], bufOut[fbMalloc];
// Once aligned, and once unaligned
for (unaligned = 0;unaligned < 2;unaligned++) {
for (i = 0;i < fbArea;i++)
makePixel(srcpf, bufIn + unaligned + i*srcpf.bpp/8);
memset(bufOut, 0, sizeof(bufOut));
dstpf.bufferFromBuffer(bufOut + unaligned, srcpf,
bufIn + unaligned, fbArea);
for (i = 0;i < fbArea;i++) {
if (!verifyPixel(dstpf, srcpf, bufOut + unaligned + i*dstpf.bpp/8))
return false;
}
memset(bufIn, 0, sizeof(bufIn));
for (y = 0;y < fbHeight;y++) {
for (x = 0;x < fbWidth/2;x++)
makePixel(srcpf, bufIn + unaligned + (x + y*fbWidth)*srcpf.bpp/8);
}
memset(bufOut, 0, sizeof(bufOut));
dstpf.bufferFromBuffer(bufOut + unaligned, srcpf, bufIn + unaligned,
fbWidth/2, fbHeight, fbWidth, fbWidth);
for (y = 0;y < fbHeight;y++) {
for (x = 0;x < fbWidth;x++) {
if (x < fbWidth/2) {
if (!verifyPixel(dstpf, srcpf,
bufOut + unaligned + (x + y*fbWidth)*dstpf.bpp/8))
return false;
} else {
const rdr::U8 zero[4] = { 0, 0, 0, 0 };
if (memcmp(bufOut + unaligned + (x + y*fbWidth)*dstpf.bpp/8, zero,
dstpf.bpp/8) != 0)
return false;
}
}
}
}
return true;
}
static bool testRGB(const rfb::PixelFormat &dstpf,
const rfb::PixelFormat &srcpf)
{
int i, x, y, unaligned;
rdr::U8 bufIn[fbMalloc], bufRGB[fbMalloc], bufOut[fbMalloc];
// Once aligned, and once unaligned
for (unaligned = 0;unaligned < 2;unaligned++) {
for (i = 0;i < fbArea;i++)
makePixel(srcpf, bufIn + unaligned + i*srcpf.bpp/8);
memset(bufRGB, 0, sizeof(bufRGB));
srcpf.rgbFromBuffer(bufRGB + unaligned, bufIn + unaligned, fbArea);
memset(bufOut, 0, sizeof(bufOut));
dstpf.bufferFromRGB(bufOut + unaligned, bufRGB + unaligned, fbArea);
for (i = 0;i < fbArea;i++) {
if (!verifyPixel(dstpf, srcpf, bufOut + unaligned + i*dstpf.bpp/8))
return false;
}
memset(bufIn, 0, sizeof(bufIn));
for (y = 0;y < fbHeight;y++) {
for (x = 0;x < fbWidth/2;x++)
makePixel(srcpf, bufIn + unaligned + (x + y*fbWidth)*srcpf.bpp/8);
}
memset(bufRGB, 0, sizeof(bufRGB));
srcpf.rgbFromBuffer(bufRGB + unaligned, bufIn + unaligned,
fbWidth/2, fbWidth, fbHeight);
memset(bufOut, 0, sizeof(bufOut));
dstpf.bufferFromRGB(bufOut + unaligned, bufRGB + unaligned,
fbWidth/2, fbWidth, fbHeight);
for (y = 0;y < fbHeight;y++) {
for (x = 0;x < fbWidth;x++) {
if (x < fbWidth/2) {
if (!verifyPixel(dstpf, srcpf,
bufOut + unaligned + (x + y*fbWidth)*dstpf.bpp/8))
return false;
} else {
const rdr::U8 zero[4] = { 0, 0, 0, 0 };
if (memcmp(bufOut + unaligned + (x + y*fbWidth)*dstpf.bpp/8, zero,
dstpf.bpp/8) != 0)
return false;
}
}
}
}
return true;
}
static bool testPixelRGB(const rfb::PixelFormat &dstpf,
const rfb::PixelFormat &srcpf)
{
rfb::Pixel p;
rdr::U16 r16, g16, b16;
rdr::U8 r8, g8, b8;
rdr::U8 buffer[4];
makePixel(srcpf, buffer);
p = srcpf.pixelFromBuffer(buffer);
srcpf.rgbFromPixel(p, &r16, &g16, &b16);
p = dstpf.pixelFromRGB(r16, g16, b16);
memset(buffer, 0, sizeof(buffer));
dstpf.bufferFromPixel(buffer, p);
if (!verifyPixel(dstpf, srcpf, buffer))
return false;
makePixel(srcpf, buffer);
p = srcpf.pixelFromBuffer(buffer);
srcpf.rgbFromPixel(p, &r8, &g8, &b8);
p = dstpf.pixelFromRGB(r8, g8, b8);
memset(buffer, 0, sizeof(buffer));
dstpf.bufferFromPixel(buffer, p);
if (!verifyPixel(dstpf, srcpf, buffer))
return false;
return true;
}
struct TestEntry tests[] = {
{"Pixel from pixel", testPixel},
{"Buffer from buffer", testBuffer},
{"Buffer to/from RGB", testRGB},
{"Pixel to/from RGB", testPixelRGB},
};
static void doTests(const rfb::PixelFormat &dstpf,
const rfb::PixelFormat &srcpf)
{
size_t i;
char dstb[256], srcb[256];
dstpf.print(dstb, sizeof(dstb));
srcpf.print(srcb, sizeof(srcb));
printf("\n");
printf("%s to %s\n", srcb, dstb);
printf("\n");
for (i = 0;i < sizeof(tests)/sizeof(tests[0]);i++) {
printf(" %s: ", tests[i].label);
fflush(stdout);
if (tests[i].fn(dstpf, srcpf))
printf("OK");
else
printf("FAILED");
printf("\n");
}
}
int main(int /*argc*/, char** /*argv*/)
{
rfb::PixelFormat dstpf, srcpf;
printf("Pixel Conversion Correctness Test\n");
/* rgb888 targets */
dstpf.parse("rgb888");
srcpf.parse("rgb888");
doTests(dstpf, srcpf);
srcpf.parse("bgr888");
doTests(dstpf, srcpf);
srcpf.parse("rgb565");
doTests(dstpf, srcpf);
srcpf.parse("rgb232");
doTests(dstpf, srcpf);
/* rgb565 targets */
dstpf.parse("rgb565");
srcpf.parse("rgb888");
doTests(dstpf, srcpf);
srcpf.parse("bgr565");
doTests(dstpf, srcpf);
srcpf.parse("rgb232");
doTests(dstpf, srcpf);
/* rgb232 targets */
dstpf.parse("rgb232");
srcpf.parse("rgb888");
doTests(dstpf, srcpf);
srcpf.parse("rgb565");
doTests(dstpf, srcpf);
srcpf.parse("bgr232");
doTests(dstpf, srcpf);
/* endian conversion (both ways) */
dstpf = rfb::PixelFormat(32, 24, false, true, 255, 255, 255, 0, 8, 16);
srcpf = rfb::PixelFormat(32, 24, true, true, 255, 255, 255, 0, 8, 16);
doTests(dstpf, srcpf);
doTests(srcpf, dstpf);
dstpf = rfb::PixelFormat(16, 16, false, true, 31, 63, 31, 0, 5, 11);
srcpf = rfb::PixelFormat(16, 16, true, true, 31, 63, 31, 0, 5, 11);
doTests(dstpf, srcpf);
doTests(srcpf, dstpf);
// Pesky case that is very asymetrical
dstpf = rfb::PixelFormat(32, 24, false, true, 255, 255, 255, 0, 8, 16);
srcpf = rfb::PixelFormat(32, 24, true, true, 255, 255, 255, 0, 24, 8);
doTests(dstpf, srcpf);
doTests(srcpf, dstpf);
}
|