🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
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.

496 lines
18 KiB

  1. /**
  2. * Pixels test suite
  3. */
  4. #include <stdio.h>
  5. #include "SDL.h"
  6. #include "SDL_test.h"
  7. /* Test case functions */
  8. /* Definition of all RGB formats used to test pixel conversions */
  9. const int _numRGBPixelFormats = 31;
  10. Uint32 _RGBPixelFormats[] = {
  11. SDL_PIXELFORMAT_INDEX1LSB,
  12. SDL_PIXELFORMAT_INDEX1MSB,
  13. SDL_PIXELFORMAT_INDEX4LSB,
  14. SDL_PIXELFORMAT_INDEX4MSB,
  15. SDL_PIXELFORMAT_INDEX8,
  16. SDL_PIXELFORMAT_RGB332,
  17. SDL_PIXELFORMAT_RGB444,
  18. SDL_PIXELFORMAT_BGR444,
  19. SDL_PIXELFORMAT_RGB555,
  20. SDL_PIXELFORMAT_BGR555,
  21. SDL_PIXELFORMAT_ARGB4444,
  22. SDL_PIXELFORMAT_RGBA4444,
  23. SDL_PIXELFORMAT_ABGR4444,
  24. SDL_PIXELFORMAT_BGRA4444,
  25. SDL_PIXELFORMAT_ARGB1555,
  26. SDL_PIXELFORMAT_RGBA5551,
  27. SDL_PIXELFORMAT_ABGR1555,
  28. SDL_PIXELFORMAT_BGRA5551,
  29. SDL_PIXELFORMAT_RGB565,
  30. SDL_PIXELFORMAT_BGR565,
  31. SDL_PIXELFORMAT_RGB24,
  32. SDL_PIXELFORMAT_BGR24,
  33. SDL_PIXELFORMAT_RGB888,
  34. SDL_PIXELFORMAT_RGBX8888,
  35. SDL_PIXELFORMAT_BGR888,
  36. SDL_PIXELFORMAT_BGRX8888,
  37. SDL_PIXELFORMAT_ARGB8888,
  38. SDL_PIXELFORMAT_RGBA8888,
  39. SDL_PIXELFORMAT_ABGR8888,
  40. SDL_PIXELFORMAT_BGRA8888,
  41. SDL_PIXELFORMAT_ARGB2101010
  42. };
  43. const char *_RGBPixelFormatsVerbose[] = {
  44. "SDL_PIXELFORMAT_INDEX1LSB",
  45. "SDL_PIXELFORMAT_INDEX1MSB",
  46. "SDL_PIXELFORMAT_INDEX4LSB",
  47. "SDL_PIXELFORMAT_INDEX4MSB",
  48. "SDL_PIXELFORMAT_INDEX8",
  49. "SDL_PIXELFORMAT_RGB332",
  50. "SDL_PIXELFORMAT_RGB444",
  51. "SDL_PIXELFORMAT_BGR444",
  52. "SDL_PIXELFORMAT_RGB555",
  53. "SDL_PIXELFORMAT_BGR555",
  54. "SDL_PIXELFORMAT_ARGB4444",
  55. "SDL_PIXELFORMAT_RGBA4444",
  56. "SDL_PIXELFORMAT_ABGR4444",
  57. "SDL_PIXELFORMAT_BGRA4444",
  58. "SDL_PIXELFORMAT_ARGB1555",
  59. "SDL_PIXELFORMAT_RGBA5551",
  60. "SDL_PIXELFORMAT_ABGR1555",
  61. "SDL_PIXELFORMAT_BGRA5551",
  62. "SDL_PIXELFORMAT_RGB565",
  63. "SDL_PIXELFORMAT_BGR565",
  64. "SDL_PIXELFORMAT_RGB24",
  65. "SDL_PIXELFORMAT_BGR24",
  66. "SDL_PIXELFORMAT_RGB888",
  67. "SDL_PIXELFORMAT_RGBX8888",
  68. "SDL_PIXELFORMAT_BGR888",
  69. "SDL_PIXELFORMAT_BGRX8888",
  70. "SDL_PIXELFORMAT_ARGB8888",
  71. "SDL_PIXELFORMAT_RGBA8888",
  72. "SDL_PIXELFORMAT_ABGR8888",
  73. "SDL_PIXELFORMAT_BGRA8888",
  74. "SDL_PIXELFORMAT_ARGB2101010"
  75. };
  76. /* Definition of all Non-RGB formats used to test pixel conversions */
  77. const int _numNonRGBPixelFormats = 7;
  78. Uint32 _nonRGBPixelFormats[] = {
  79. SDL_PIXELFORMAT_YV12,
  80. SDL_PIXELFORMAT_IYUV,
  81. SDL_PIXELFORMAT_YUY2,
  82. SDL_PIXELFORMAT_UYVY,
  83. SDL_PIXELFORMAT_YVYU,
  84. SDL_PIXELFORMAT_NV12,
  85. SDL_PIXELFORMAT_NV21
  86. };
  87. const char *_nonRGBPixelFormatsVerbose[] = {
  88. "SDL_PIXELFORMAT_YV12",
  89. "SDL_PIXELFORMAT_IYUV",
  90. "SDL_PIXELFORMAT_YUY2",
  91. "SDL_PIXELFORMAT_UYVY",
  92. "SDL_PIXELFORMAT_YVYU",
  93. "SDL_PIXELFORMAT_NV12",
  94. "SDL_PIXELFORMAT_NV21"
  95. };
  96. /* Definition of some invalid formats for negative tests */
  97. const int _numInvalidPixelFormats = 2;
  98. Uint32 _invalidPixelFormats[] = {
  99. SDL_DEFINE_PIXELFORMAT(SDL_PIXELTYPE_PACKED32, SDL_PACKEDORDER_ABGR, SDL_PACKEDLAYOUT_1010102 + 1, 32, 4),
  100. SDL_DEFINE_PIXELFORMAT(SDL_PIXELTYPE_PACKED32, SDL_PACKEDORDER_ABGR, SDL_PACKEDLAYOUT_1010102 + 2, 32, 4)
  101. };
  102. const char *_invalidPixelFormatsVerbose[] = {
  103. "SDL_PIXELFORMAT_UNKNOWN",
  104. "SDL_PIXELFORMAT_UNKNOWN"
  105. };
  106. /* Test case functions */
  107. /**
  108. * @brief Call to SDL_AllocFormat and SDL_FreeFormat
  109. *
  110. * @sa http://wiki.libsdl.org/SDL_AllocFormat
  111. * @sa http://wiki.libsdl.org/SDL_FreeFormat
  112. */
  113. int pixels_allocFreeFormat(void *arg)
  114. {
  115. const char *unknownFormat = "SDL_PIXELFORMAT_UNKNOWN";
  116. const char *error;
  117. int i;
  118. Uint32 format;
  119. Uint32 masks;
  120. SDL_PixelFormat *result;
  121. /* Blank/unknown format */
  122. format = 0;
  123. SDLTest_Log("RGB Format: %s (%" SDL_PRIu32 ")", unknownFormat, format);
  124. /* Allocate format */
  125. result = SDL_AllocFormat(format);
  126. SDLTest_AssertPass("Call to SDL_AllocFormat()");
  127. SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
  128. if (result != NULL) {
  129. SDLTest_AssertCheck(result->format == format, "Verify value of result.format; expected: %" SDL_PRIu32 ", got %" SDL_PRIu32, format, result->format);
  130. SDLTest_AssertCheck(result->BitsPerPixel == 0, "Verify value of result.BitsPerPixel; expected: 0, got %u", result->BitsPerPixel);
  131. SDLTest_AssertCheck(result->BytesPerPixel == 0, "Verify value of result.BytesPerPixel; expected: 0, got %u", result->BytesPerPixel);
  132. masks = result->Rmask | result->Gmask | result->Bmask | result->Amask;
  133. SDLTest_AssertCheck(masks == 0, "Verify value of result.[RGBA]mask combined; expected: 0, got %" SDL_PRIu32, masks);
  134. /* Deallocate again */
  135. SDL_FreeFormat(result);
  136. SDLTest_AssertPass("Call to SDL_FreeFormat()");
  137. }
  138. /* RGB formats */
  139. for (i = 0; i < _numRGBPixelFormats; i++) {
  140. format = _RGBPixelFormats[i];
  141. SDLTest_Log("RGB Format: %s (%" SDL_PRIu32 ")", _RGBPixelFormatsVerbose[i], format);
  142. /* Allocate format */
  143. result = SDL_AllocFormat(format);
  144. SDLTest_AssertPass("Call to SDL_AllocFormat()");
  145. SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
  146. if (result != NULL) {
  147. SDLTest_AssertCheck(result->format == format, "Verify value of result.format; expected: %" SDL_PRIu32 ", got %" SDL_PRIu32, format, result->format);
  148. SDLTest_AssertCheck(result->BitsPerPixel > 0, "Verify value of result.BitsPerPixel; expected: >0, got %u", result->BitsPerPixel);
  149. SDLTest_AssertCheck(result->BytesPerPixel > 0, "Verify value of result.BytesPerPixel; expected: >0, got %u", result->BytesPerPixel);
  150. if (result->palette != NULL) {
  151. masks = result->Rmask | result->Gmask | result->Bmask | result->Amask;
  152. SDLTest_AssertCheck(masks > 0, "Verify value of result.[RGBA]mask combined; expected: >0, got %" SDL_PRIu32, masks);
  153. }
  154. /* Deallocate again */
  155. SDL_FreeFormat(result);
  156. SDLTest_AssertPass("Call to SDL_FreeFormat()");
  157. }
  158. }
  159. #if 0 /* This succeeds for SDL3, but we don't expect SDL2 applications to call SDL_AllocFormat() for FOURCC formats directly */
  160. /* Non-RGB formats */
  161. for (i = 0; i < _numNonRGBPixelFormats; i++) {
  162. format = _nonRGBPixelFormats[i];
  163. SDLTest_Log("non-RGB Format: %s (%" SDL_PRIu32 ")", _nonRGBPixelFormatsVerbose[i], format);
  164. /* Try to allocate format */
  165. result = SDL_AllocFormat(format);
  166. SDLTest_AssertPass("Call to SDL_AllocFormat()");
  167. SDLTest_AssertCheck(result == NULL, "Verify result is NULL");
  168. }
  169. #endif
  170. /* Negative cases */
  171. /* Invalid Formats */
  172. for (i = 0; i < _numInvalidPixelFormats; i++) {
  173. SDL_ClearError();
  174. SDLTest_AssertPass("Call to SDL_ClearError()");
  175. format = _invalidPixelFormats[i];
  176. result = SDL_AllocFormat(format);
  177. SDLTest_AssertPass("Call to SDL_AllocFormat(%" SDL_PRIu32 ")", format);
  178. SDLTest_AssertCheck(result == NULL, "Verify result is NULL");
  179. error = SDL_GetError();
  180. SDLTest_AssertPass("Call to SDL_GetError()");
  181. SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
  182. }
  183. return TEST_COMPLETED;
  184. }
  185. /**
  186. * @brief Call to SDL_GetPixelFormatName
  187. *
  188. * @sa http://wiki.libsdl.org/SDL_GetPixelFormatName
  189. */
  190. int pixels_getPixelFormatName(void *arg)
  191. {
  192. const char *unknownFormat = "SDL_PIXELFORMAT_UNKNOWN";
  193. const char *error;
  194. int i;
  195. Uint32 format;
  196. const char *result;
  197. /* Blank/undefined format */
  198. format = 0;
  199. SDLTest_Log("RGB Format: %s (%" SDL_PRIu32 ")", unknownFormat, format);
  200. /* Get name of format */
  201. result = SDL_GetPixelFormatName(format);
  202. SDLTest_AssertPass("Call to SDL_GetPixelFormatName()");
  203. SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
  204. if (result != NULL) {
  205. SDLTest_AssertCheck(result[0] != '\0', "Verify result is non-empty");
  206. SDLTest_AssertCheck(SDL_strcmp(result, unknownFormat) == 0,
  207. "Verify result text; expected: %s, got %s", unknownFormat, result);
  208. }
  209. /* RGB formats */
  210. for (i = 0; i < _numRGBPixelFormats; i++) {
  211. format = _RGBPixelFormats[i];
  212. SDLTest_Log("RGB Format: %s (%" SDL_PRIu32 ")", _RGBPixelFormatsVerbose[i], format);
  213. /* Get name of format */
  214. result = SDL_GetPixelFormatName(format);
  215. SDLTest_AssertPass("Call to SDL_GetPixelFormatName()");
  216. SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
  217. if (result != NULL) {
  218. SDLTest_AssertCheck(result[0] != '\0', "Verify result is non-empty");
  219. SDLTest_AssertCheck(SDL_strcmp(result, _RGBPixelFormatsVerbose[i]) == 0,
  220. "Verify result text; expected: %s, got %s", _RGBPixelFormatsVerbose[i], result);
  221. }
  222. }
  223. /* Non-RGB formats */
  224. for (i = 0; i < _numNonRGBPixelFormats; i++) {
  225. format = _nonRGBPixelFormats[i];
  226. SDLTest_Log("non-RGB Format: %s (%" SDL_PRIu32 ")", _nonRGBPixelFormatsVerbose[i], format);
  227. /* Get name of format */
  228. result = SDL_GetPixelFormatName(format);
  229. SDLTest_AssertPass("Call to SDL_GetPixelFormatName()");
  230. SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
  231. if (result != NULL) {
  232. SDLTest_AssertCheck(result[0] != '\0', "Verify result is non-empty");
  233. SDLTest_AssertCheck(SDL_strcmp(result, _nonRGBPixelFormatsVerbose[i]) == 0,
  234. "Verify result text; expected: %s, got %s", _nonRGBPixelFormatsVerbose[i], result);
  235. }
  236. }
  237. /* Negative cases */
  238. /* Invalid Formats */
  239. SDL_ClearError();
  240. SDLTest_AssertPass("Call to SDL_ClearError()");
  241. for (i = 0; i < _numInvalidPixelFormats; i++) {
  242. format = _invalidPixelFormats[i];
  243. result = SDL_GetPixelFormatName(format);
  244. SDLTest_AssertPass("Call to SDL_GetPixelFormatName(%" SDL_PRIu32 ")", format);
  245. SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
  246. if (result != NULL) {
  247. SDLTest_AssertCheck(result[0] != '\0',
  248. "Verify result is non-empty; got: %s", result);
  249. SDLTest_AssertCheck(SDL_strcmp(result, _invalidPixelFormatsVerbose[i]) == 0,
  250. "Validate name is UNKNOWN, expected: '%s', got: '%s'", _invalidPixelFormatsVerbose[i], result);
  251. }
  252. error = SDL_GetError();
  253. SDLTest_AssertPass("Call to SDL_GetError()");
  254. SDLTest_AssertCheck(error == NULL || error[0] == '\0', "Validate that error message is empty");
  255. }
  256. return TEST_COMPLETED;
  257. }
  258. /**
  259. * @brief Call to SDL_AllocPalette and SDL_FreePalette
  260. *
  261. * @sa http://wiki.libsdl.org/SDL_AllocPalette
  262. * @sa http://wiki.libsdl.org/SDL_FreePalette
  263. */
  264. int pixels_allocFreePalette(void *arg)
  265. {
  266. const char *expectedError1 = "Parameter 'ncolors' is invalid";
  267. const char *error;
  268. int variation;
  269. int i;
  270. int ncolors;
  271. SDL_Palette *result;
  272. /* Allocate palette */
  273. for (variation = 1; variation <= 3; variation++) {
  274. switch (variation) {
  275. /* Just one color */
  276. default:
  277. case 1:
  278. ncolors = 1;
  279. break;
  280. /* Two colors */
  281. case 2:
  282. ncolors = 2;
  283. break;
  284. /* More than two colors */
  285. case 3:
  286. ncolors = SDLTest_RandomIntegerInRange(8, 16);
  287. break;
  288. }
  289. result = SDL_AllocPalette(ncolors);
  290. SDLTest_AssertPass("Call to SDL_AllocPalette(%d)", ncolors);
  291. SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
  292. if (result != NULL) {
  293. SDLTest_AssertCheck(result->ncolors == ncolors, "Verify value of result.ncolors; expected: %u, got %u", ncolors, result->ncolors);
  294. if (result->ncolors > 0) {
  295. SDLTest_AssertCheck(result->colors != NULL, "Verify value of result.colors is not NULL");
  296. if (result->colors != NULL) {
  297. for (i = 0; i < result->ncolors; i++) {
  298. SDLTest_AssertCheck(result->colors[i].r == 255, "Verify value of result.colors[%d].r; expected: 255, got %u", i, result->colors[i].r);
  299. SDLTest_AssertCheck(result->colors[i].g == 255, "Verify value of result.colors[%d].g; expected: 255, got %u", i, result->colors[i].g);
  300. SDLTest_AssertCheck(result->colors[i].b == 255, "Verify value of result.colors[%d].b; expected: 255, got %u", i, result->colors[i].b);
  301. }
  302. }
  303. }
  304. /* Deallocate again */
  305. SDL_FreePalette(result);
  306. SDLTest_AssertPass("Call to SDL_FreePalette()");
  307. }
  308. }
  309. /* Negative cases */
  310. /* Invalid number of colors */
  311. for (ncolors = 0; ncolors > -3; ncolors--) {
  312. SDL_ClearError();
  313. SDLTest_AssertPass("Call to SDL_ClearError()");
  314. result = SDL_AllocPalette(ncolors);
  315. SDLTest_AssertPass("Call to SDL_AllocPalette(%d)", ncolors);
  316. SDLTest_AssertCheck(result == NULL, "Verify result is NULL");
  317. error = SDL_GetError();
  318. SDLTest_AssertPass("Call to SDL_GetError()");
  319. SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
  320. if (error != NULL) {
  321. SDLTest_AssertCheck(SDL_strcmp(error, expectedError1) == 0,
  322. "Validate error message, expected: '%s', got: '%s'", expectedError1, error);
  323. }
  324. }
  325. return TEST_COMPLETED;
  326. }
  327. /**
  328. * @brief Call to SDL_CalculateGammaRamp
  329. *
  330. * @sa http://wiki.libsdl.org/SDL_CalculateGammaRamp
  331. */
  332. int
  333. pixels_calcGammaRamp(void *arg)
  334. {
  335. const char *expectedError1 = "Parameter 'gamma' is invalid";
  336. const char *expectedError2 = "Parameter 'ramp' is invalid";
  337. const char *error;
  338. float gamma;
  339. Uint16 *ramp;
  340. int variation;
  341. int i;
  342. int changed;
  343. Uint16 magic = 0xbeef;
  344. /* Allocate temp ramp array and fill with some value */
  345. ramp = (Uint16 *)SDL_malloc(256 * sizeof(Uint16));
  346. SDLTest_AssertCheck(ramp != NULL, "Validate temp ramp array could be allocated");
  347. if (ramp == NULL) return TEST_ABORTED;
  348. /* Make call with different gamma values */
  349. for (variation = 0; variation < 4; variation++) {
  350. switch (variation) {
  351. /* gamma = 0 all black */
  352. default:
  353. case 0:
  354. gamma = 0.0f;
  355. break;
  356. /* gamma = 1 identity */
  357. case 1:
  358. gamma = 1.0f;
  359. break;
  360. /* gamma = [0.2,0.8] normal range */
  361. case 2:
  362. gamma = 0.2f + 0.8f * SDLTest_RandomUnitFloat();
  363. break;
  364. /* gamma = >1.1 non-standard range */
  365. case 3:
  366. gamma = 1.1f + SDLTest_RandomUnitFloat();
  367. break;
  368. }
  369. /* Make call and check that values were updated */
  370. for (i = 0; i < 256; i++) ramp[i] = magic;
  371. SDL_CalculateGammaRamp(gamma, ramp);
  372. SDLTest_AssertPass("Call to SDL_CalculateGammaRamp(%f)", gamma);
  373. changed = 0;
  374. for (i = 0; i < 256; i++) if (ramp[i] != magic) changed++;
  375. SDLTest_AssertCheck(changed > 250, "Validate that ramp was calculated; expected: >250 values changed, got: %d values changed", changed);
  376. /* Additional value checks for some cases */
  377. i = SDLTest_RandomIntegerInRange(64,192);
  378. switch (variation) {
  379. case 0:
  380. SDLTest_AssertCheck(ramp[i] == 0, "Validate value at position %d; expected: 0, got: %d", i, ramp[i]);
  381. break;
  382. case 1:
  383. SDLTest_AssertCheck(ramp[i] == ((i << 8) | i), "Validate value at position %d; expected: %d, got: %d", i, (i << 8) | i, ramp[i]);
  384. break;
  385. case 2:
  386. case 3:
  387. SDLTest_AssertCheck(ramp[i] > 0, "Validate value at position %d; expected: >0, got: %d", i, ramp[i]);
  388. break;
  389. }
  390. }
  391. /* Negative cases */
  392. SDL_ClearError();
  393. SDLTest_AssertPass("Call to SDL_ClearError()");
  394. gamma = -1;
  395. for (i=0; i<256; i++) ramp[i] = magic;
  396. SDL_CalculateGammaRamp(gamma, ramp);
  397. SDLTest_AssertPass("Call to SDL_CalculateGammaRamp(%f)", gamma);
  398. error = SDL_GetError();
  399. SDLTest_AssertPass("Call to SDL_GetError()");
  400. SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
  401. if (error != NULL) {
  402. SDLTest_AssertCheck(SDL_strcmp(error, expectedError1) == 0,
  403. "Validate error message, expected: '%s', got: '%s'", expectedError1, error);
  404. }
  405. changed = 0;
  406. for (i = 0; i < 256; i++) if (ramp[i] != magic) changed++;
  407. SDLTest_AssertCheck(changed ==0, "Validate that ramp unchanged; expected: 0 values changed got: %d values changed", changed);
  408. SDL_CalculateGammaRamp(0.5f, NULL);
  409. SDLTest_AssertPass("Call to SDL_CalculateGammaRamp(0.5,NULL)");
  410. error = SDL_GetError();
  411. SDLTest_AssertPass("Call to SDL_GetError()");
  412. SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
  413. if (error != NULL) {
  414. SDLTest_AssertCheck(SDL_strcmp(error, expectedError2) == 0,
  415. "Validate error message, expected: '%s', got: '%s'", expectedError2, error);
  416. }
  417. /* Cleanup */
  418. SDL_free(ramp);
  419. return TEST_COMPLETED;
  420. }
  421. /* ================= Test References ================== */
  422. /* Pixels test cases */
  423. static const SDLTest_TestCaseReference pixelsTest1 = {
  424. (SDLTest_TestCaseFp)pixels_allocFreeFormat, "pixels_allocFreeFormat", "Call to SDL_AllocFormat and SDL_FreeFormat", TEST_ENABLED
  425. };
  426. static const SDLTest_TestCaseReference pixelsTest2 = {
  427. (SDLTest_TestCaseFp)pixels_allocFreePalette, "pixels_allocFreePalette", "Call to SDL_AllocPalette and SDL_FreePalette", TEST_ENABLED
  428. };
  429. static const SDLTest_TestCaseReference pixelsTest3 =
  430. { (SDLTest_TestCaseFp)pixels_calcGammaRamp, "pixels_calcGammaRamp", "Call to SDL_CalculateGammaRamp", TEST_ENABLED };
  431. static const SDLTest_TestCaseReference pixelsTest4 =
  432. { (SDLTest_TestCaseFp)pixels_getPixelFormatName, "pixels_getPixelFormatName", "Call to SDL_GetPixelFormatName", TEST_ENABLED };
  433. /* Sequence of Pixels test cases */
  434. static const SDLTest_TestCaseReference *pixelsTests[] = {
  435. &pixelsTest1, &pixelsTest2, &pixelsTest3, &pixelsTest4, NULL
  436. };
  437. /* Pixels test suite (global) */
  438. SDLTest_TestSuiteReference pixelsTestSuite = {
  439. "Pixels",
  440. NULL,
  441. pixelsTests,
  442. NULL
  443. };