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