You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pixelformat.cxx 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* Copyright 2019 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. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21. #include <stdio.h>
  22. #include <rfb/PixelFormat.h>
  23. #include <rfb/Exception.h>
  24. static void doTest(bool should_fail, int b, int d, bool e, bool t,
  25. int rm, int gm, int bm, int rs, int gs, int bs)
  26. {
  27. rfb::PixelFormat* pf;
  28. printf("PixelFormat(%d, %d, %s, %s, %d, %d, %d, %d, %d, %d): ",
  29. b, d, e ? "true" : "false", t ? "true": "false",
  30. rm, gm, bm, rs, gs, bs);
  31. try {
  32. pf = new rfb::PixelFormat(b, d, e, t, rm, gm, bm, rs, gs, bs);
  33. } catch(rfb::Exception &e) {
  34. if (should_fail)
  35. printf("OK");
  36. else
  37. printf("FAILED");
  38. printf("\n");
  39. fflush(stdout);
  40. return;
  41. }
  42. delete pf;
  43. if (should_fail)
  44. printf("FAILED");
  45. else
  46. printf("OK");
  47. printf("\n");
  48. fflush(stdout);
  49. }
  50. static void do888Test(bool expected, int b, int d, bool e, bool t,
  51. int rm, int gm, int bm, int rs, int gs, int bs)
  52. {
  53. rfb::PixelFormat* pf;
  54. printf("PixelFormat(%d, %d, %s, %s, %d, %d, %d, %d, %d, %d): ",
  55. b, d, e ? "true" : "false", t ? "true": "false",
  56. rm, gm, bm, rs, gs, bs);
  57. pf = new rfb::PixelFormat(b, d, e, t, rm, gm, bm, rs, gs, bs);
  58. if (pf->is888() == expected)
  59. printf("OK");
  60. else
  61. printf("FAILED");
  62. printf("\n");
  63. fflush(stdout);
  64. delete pf;
  65. }
  66. static void sanityTests()
  67. {
  68. printf("Sanity checks:\n\n");
  69. /* Normal true color formats */
  70. doTest(false, 32, 24, false, true, 255, 255, 255, 0, 8, 16);
  71. doTest(false, 32, 24, false, true, 255, 255, 255, 24, 16, 8);
  72. doTest(false, 16, 16, false, true, 15, 31, 15, 0, 5, 11);
  73. doTest(false, 8, 8, false, true, 3, 7, 3, 0, 2, 5);
  74. /* Excessive bpp */
  75. doTest(false, 32, 16, false, true, 15, 31, 15, 0, 5, 11);
  76. doTest(false, 16, 16, false, true, 15, 31, 15, 0, 5, 11);
  77. doTest(false, 32, 8, false, true, 3, 7, 3, 0, 2, 5);
  78. doTest(false, 16, 8, false, true, 3, 7, 3, 0, 2, 5);
  79. /* Colour map */
  80. doTest(false, 8, 8, false, false, 0, 0, 0, 0, 0, 0);
  81. /* Invalid bpp */
  82. doTest(true, 64, 24, false, true, 255, 255, 255, 0, 8, 16);
  83. doTest(true, 18, 16, false, true, 15, 31, 15, 0, 5, 11);
  84. doTest(true, 3, 3, false, true, 1, 1, 1, 0, 1, 2);
  85. /* Invalid depth */
  86. doTest(true, 16, 24, false, true, 15, 31, 15, 0, 5, 11);
  87. doTest(true, 8, 24, false, true, 3, 7, 3, 0, 2, 5);
  88. doTest(true, 8, 16, false, true, 3, 7, 3, 0, 2, 5);
  89. doTest(true, 32, 24, false, false, 0, 0, 0, 0, 0, 0);
  90. /* Invalid max values */
  91. doTest(true, 32, 24, false, true, 254, 255, 255, 0, 8, 16);
  92. doTest(true, 32, 24, false, true, 255, 253, 255, 0, 8, 16);
  93. doTest(true, 32, 24, false, true, 255, 255, 252, 0, 8, 16);
  94. doTest(true, 32, 24, false, true, 511, 127, 127, 0, 16, 20);
  95. doTest(true, 32, 24, false, true, 127, 511, 127, 0, 4, 20);
  96. doTest(true, 32, 24, false, true, 127, 127, 511, 0, 4, 8);
  97. /* Insufficient depth */
  98. doTest(true, 32, 16, false, true, 255, 255, 255, 0, 8, 16);
  99. /* Invalid shift values */
  100. doTest(true, 32, 24, false, true, 255, 255, 255, 25, 8, 16);
  101. doTest(true, 32, 24, false, true, 255, 255, 255, 0, 25, 16);
  102. doTest(true, 32, 24, false, true, 255, 255, 255, 0, 8, 25);
  103. /* Overlapping channels */
  104. doTest(true, 32, 24, false, true, 255, 255, 255, 0, 7, 16);
  105. doTest(true, 32, 24, false, true, 255, 255, 255, 0, 8, 15);
  106. doTest(true, 32, 24, false, true, 255, 255, 255, 0, 16, 7);
  107. printf("\n");
  108. }
  109. void is888Tests()
  110. {
  111. printf("Simple format detection:\n\n");
  112. /* Positive cases */
  113. do888Test(true, 32, 24, false, true, 255, 255, 255, 0, 8, 16);
  114. do888Test(true, 32, 24, false, true, 255, 255, 255, 24, 16, 8);
  115. do888Test(true, 32, 24, false, true, 255, 255, 255, 24, 8, 0);
  116. /* Low depth */
  117. do888Test(false, 32, 16, false, true, 15, 31, 15, 0, 8, 16);
  118. do888Test(false, 32, 8, false, true, 3, 7, 3, 0, 8, 16);
  119. /* Low bpp and depth */
  120. do888Test(false, 16, 16, false, true, 15, 31, 15, 0, 5, 11);
  121. do888Test(false, 8, 8, false, true, 3, 7, 3, 0, 2, 5);
  122. /* Colour map */
  123. do888Test(false, 8, 8, false, false, 0, 0, 0, 0, 0, 0);
  124. /* Odd shifts */
  125. do888Test(false, 32, 24, false, true, 255, 255, 255, 0, 8, 18);
  126. do888Test(false, 32, 24, false, true, 255, 255, 255, 0, 11, 24);
  127. do888Test(false, 32, 24, false, true, 255, 255, 255, 4, 16, 24);
  128. printf("\n");
  129. }
  130. int main(int /*argc*/, char** /*argv*/)
  131. {
  132. sanityTests();
  133. is888Tests();
  134. return 0;
  135. }