blob: 22c86c4eefc667675b8bfede2af5ef2be35e5094 [file] [log] [blame]
Pierre Ossman41deb882014-09-25 09:58:55 +02001/* Copyright 2013-2014 Pierre Ossman <ossman@cendio.se> for Cendio AB
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#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22
23#include <rfb/PixelFormat.h>
24
25static const rdr::U8 pixelRed = 0xf1;
26static const rdr::U8 pixelGreen = 0xc3;
27static const rdr::U8 pixelBlue = 0x97;
28
29typedef bool (*testfn) (const rfb::PixelFormat&, const rfb::PixelFormat&);
30
31struct TestEntry {
32 const char *label;
33 testfn fn;
34};
35
36#define min(a,b) (((a) < (b)) ? (a) : (b))
37
38namespace rfb {
39
40void makePixel(const rfb::PixelFormat &pf,
41 rdr::U8 *buffer)
42{
43 rfb::Pixel p;
44
45 p = 0;
46 p |= (pixelRed >> (8 - pf.redBits)) << pf.redShift;
47 p |= (pixelGreen >> (8 - pf.greenBits)) << pf.greenShift;
48 p |= (pixelBlue >> (8 - pf.blueBits)) << pf.blueShift;
49
50 // FIXME: Should we reimplement this as well?
51 pf.bufferFromPixel(buffer, p);
52}
53
54bool verifyPixel(const rfb::PixelFormat &dstpf,
55 const rfb::PixelFormat &srcpf,
56 const rdr::U8 *buffer)
57{
58 rfb::Pixel p;
59 int r, g, b;
60 int re, ge, be;
61
62 // FIXME: Should we reimplement this as well?
63 p = dstpf.pixelFromBuffer(buffer);
64
65 r = (p >> dstpf.redShift) & dstpf.redMax;
66 g = (p >> dstpf.greenShift) & dstpf.greenMax;
67 b = (p >> dstpf.blueShift) & dstpf.blueMax;
68
69 r <<= 8 - dstpf.redBits;
70 g <<= 8 - dstpf.greenBits;
71 b <<= 8 - dstpf.blueBits;
72
73 // The allowed error depends on:
74 //
75 // a) The number of bits the format can hold
76 // b) The number of bits the source format could hold
77
78 re = (1 << (8 - min(dstpf.redBits, srcpf.redBits))) - 1;
79 ge = (1 << (8 - min(dstpf.greenBits, srcpf.greenBits))) - 1;
80 be = (1 << (8 - min(dstpf.blueBits, srcpf.blueBits))) - 1;
81
82 if (abs(r - pixelRed) > re)
83 return false;
84 if (abs(g - pixelGreen) > ge)
85 return false;
86 if (abs(b - pixelBlue) > be)
87 return false;
88
89 return true;
90}
91
92}
93
94using rfb::makePixel;
95using rfb::verifyPixel;
96
97static bool testPixel(const rfb::PixelFormat &dstpf,
98 const rfb::PixelFormat &srcpf)
99{
100 rfb::Pixel p;
101 rdr::U8 buffer[4];
102
103 makePixel(srcpf, buffer);
104
105 p = srcpf.pixelFromBuffer(buffer);
106 p = dstpf.pixelFromPixel(srcpf, p);
107 memset(buffer, 0, sizeof(buffer));
108 dstpf.bufferFromPixel(buffer, p);
109
110 if (!verifyPixel(dstpf, srcpf, buffer))
111 return false;
112
113 return true;
114}
115
116static bool testBuffer(const rfb::PixelFormat &dstpf,
117 const rfb::PixelFormat &srcpf)
118{
119 int i, unaligned;
120 rdr::U8 bufIn[1024 + 1], bufOut[1024 + 1];
121
122 // Once aligned, and once unaligned
123 for (unaligned = 0;unaligned < 2;unaligned++) {
124 for (i = 0;i < 1024/4;i++)
125 makePixel(srcpf, bufIn + unaligned + i*srcpf.bpp/8);
126
127 memset(bufOut, 0, sizeof(bufOut));
128 dstpf.bufferFromBuffer(bufOut + unaligned, srcpf,
129 bufIn + unaligned, 1024/4);
130
131 for (i = 0;i < 1024/4;i++) {
132 if (!verifyPixel(dstpf, srcpf, bufOut + unaligned + i*dstpf.bpp/8))
133 return false;
134 }
135
136 memset(bufOut, 0, sizeof(bufOut));
137 dstpf.bufferFromBuffer(bufOut + unaligned, srcpf, bufIn + unaligned,
138 1024/4/10, 10, 1024/4/10, 1024/4/10);
139
140 for (i = 0;i < 1024/4/10*10;i++) {
141 if (!verifyPixel(dstpf, srcpf, bufOut + unaligned + i*dstpf.bpp/8))
142 return false;
143 }
144 }
145
146 return true;
147}
148
149static bool testRGB(const rfb::PixelFormat &dstpf,
150 const rfb::PixelFormat &srcpf)
151{
152 int i, unaligned;
153 rdr::U8 bufIn[1024 + 1], bufRGB[1024 + 1], bufOut[1024 + 1];
154
155 // Once aligned, and once unaligned
156 for (unaligned = 0;unaligned < 2;unaligned++) {
157 for (i = 0;i < 1024/4;i++)
158 makePixel(srcpf, bufIn + unaligned + i*srcpf.bpp/8);
159
160 memset(bufRGB, 0, sizeof(bufRGB));
161 srcpf.rgbFromBuffer(bufRGB + unaligned, bufIn + unaligned, 1024/4);
162
163 memset(bufOut, 0, sizeof(bufOut));
164 dstpf.bufferFromRGB(bufOut + unaligned, bufRGB + unaligned, 1024/4);
165
166 for (i = 0;i < 1024/4;i++) {
167 if (!verifyPixel(dstpf, srcpf, bufOut + unaligned + i*dstpf.bpp/8))
168 return false;
169 }
170
171 memset(bufRGB, 0, sizeof(bufRGB));
172 srcpf.rgbFromBuffer(bufRGB + unaligned, bufIn + unaligned,
173 1024/4/10, 1024/4/10, 10);
174
175 memset(bufOut, 0, sizeof(bufOut));
176 dstpf.bufferFromRGB(bufOut + unaligned, bufRGB + unaligned,
177 1024/4/10, 1024/4/10, 10);
178
179 for (i = 0;i < 1024/4/10*10;i++) {
180 if (!verifyPixel(dstpf, srcpf, bufOut + unaligned + i*dstpf.bpp/8))
181 return false;
182 }
183 }
184
185 return true;
186}
187
188static bool testPixelRGB(const rfb::PixelFormat &dstpf,
189 const rfb::PixelFormat &srcpf)
190{
191 rfb::Pixel p;
192 rdr::U16 r16, g16, b16;
193 rdr::U8 r8, g8, b8;
194 rdr::U8 buffer[4];
195
196 makePixel(srcpf, buffer);
197
198 p = srcpf.pixelFromBuffer(buffer);
199 srcpf.rgbFromPixel(p, &r16, &g16, &b16);
200 p = dstpf.pixelFromRGB(r16, g16, b16);
201 memset(buffer, 0, sizeof(buffer));
202 dstpf.bufferFromPixel(buffer, p);
203
204 if (!verifyPixel(dstpf, srcpf, buffer))
205 return false;
206
207 makePixel(srcpf, buffer);
208
209 p = srcpf.pixelFromBuffer(buffer);
210 srcpf.rgbFromPixel(p, &r8, &g8, &b8);
211 p = dstpf.pixelFromRGB(r8, g8, b8);
212 memset(buffer, 0, sizeof(buffer));
213 dstpf.bufferFromPixel(buffer, p);
214
215 if (!verifyPixel(dstpf, srcpf, buffer))
216 return false;
217
218 return true;
219}
220
221struct TestEntry tests[] = {
222 {"Pixel from pixel", testPixel},
223 {"Buffer from buffer", testBuffer},
224 {"Buffer to/from RGB", testRGB},
225 {"Pixel to/from RGB", testPixelRGB},
226};
227
228static void doTests(const rfb::PixelFormat &dstpf,
229 const rfb::PixelFormat &srcpf)
230{
231 int i;
232 char dstb[256], srcb[256];
233
234 dstpf.print(dstb, sizeof(dstb));
235 srcpf.print(srcb, sizeof(srcb));
236
237 printf("\n");
238 printf("%s to %s\n", srcb, dstb);
239 printf("\n");
240
241 for (i = 0;i < sizeof(tests)/sizeof(tests[0]);i++) {
242 printf(" %s: ", tests[i].label);
243 fflush(stdout);
244 if (tests[i].fn(dstpf, srcpf))
245 printf("OK");
246 else
247 printf("FAILED");
248 printf("\n");
249 }
250}
251
252int main(int argc, char **argv)
253{
254 rfb::PixelFormat dstpf, srcpf;
255
256 printf("Pixel Conversion Correctness Test\n");
257
258 /* rgb888 targets */
259
260 dstpf.parse("rgb888");
261
262 srcpf.parse("rgb888");
263 doTests(dstpf, srcpf);
264
265 srcpf.parse("bgr888");
266 doTests(dstpf, srcpf);
267
268 srcpf.parse("rgb565");
269 doTests(dstpf, srcpf);
270
271 srcpf.parse("rgb232");
272 doTests(dstpf, srcpf);
273
274 /* rgb565 targets */
275
276 dstpf.parse("rgb565");
277
278 srcpf.parse("rgb888");
279 doTests(dstpf, srcpf);
280
281 srcpf.parse("bgr565");
282 doTests(dstpf, srcpf);
283
284 srcpf.parse("rgb232");
285 doTests(dstpf, srcpf);
286
287 /* rgb232 targets */
288
289 dstpf.parse("rgb232");
290
291 srcpf.parse("rgb888");
292 doTests(dstpf, srcpf);
293
294 srcpf.parse("rgb565");
295 doTests(dstpf, srcpf);
296
297 srcpf.parse("bgr232");
298 doTests(dstpf, srcpf);
299
300 /* endian conversion (both ways) */
301
302 dstpf = rfb::PixelFormat(32, 24, false, true, 255, 255, 255, 0, 8, 16);
303 srcpf = rfb::PixelFormat(32, 24, true, true, 255, 255, 255, 0, 8, 16);
304
305 doTests(dstpf, srcpf);
306
307 doTests(srcpf, dstpf);
308
309 dstpf = rfb::PixelFormat(16, 16, false, true, 31, 63, 31, 0, 5, 11);
310 srcpf = rfb::PixelFormat(16, 16, true, true, 31, 63, 31, 0, 5, 11);
311
312 doTests(dstpf, srcpf);
313
314 doTests(srcpf, dstpf);
315
316 // Pesky case that is very asymetrical
317 dstpf = rfb::PixelFormat(32, 24, false, true, 255, 255, 255, 0, 8, 16);
318 srcpf = rfb::PixelFormat(32, 24, true, true, 255, 255, 255, 0, 24, 8);
319
320 doTests(dstpf, srcpf);
321
322 doTests(srcpf, dstpf);
323}