Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

conv.cxx 9.2KB

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