testautomation_surface.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326
  1. /**
  2. * Original code: automated SDL surface test written by Edgar Simo "bobbens"
  3. * Adapted/rewritten for test lib by Andreas Schiffler
  4. */
  5. /* Suppress C4996 VS compiler warnings for unlink() */
  6. #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_DEPRECATE)
  7. #define _CRT_SECURE_NO_DEPRECATE
  8. #endif
  9. #if defined(_MSC_VER) && !defined(_CRT_NONSTDC_NO_DEPRECATE)
  10. #define _CRT_NONSTDC_NO_DEPRECATE
  11. #endif
  12. #include <stdio.h>
  13. #ifndef _MSC_VER
  14. #include <unistd.h>
  15. #endif
  16. #include <sys/stat.h>
  17. #include <SDL3/SDL.h>
  18. #include <SDL3/SDL_test.h>
  19. #include "testautomation_suites.h"
  20. #include "testautomation_images.h"
  21. #define CHECK_FUNC(FUNC, PARAMS) \
  22. { \
  23. int result = FUNC PARAMS; \
  24. if (result != 0) { \
  25. SDLTest_AssertCheck(result == 0, "Validate result from %s, expected: 0, got: %i, %s", #FUNC, result, SDL_GetError()); \
  26. } \
  27. }
  28. /* ================= Test Case Implementation ================== */
  29. /* Shared test surface */
  30. static SDL_Surface *referenceSurface = NULL;
  31. static SDL_Surface *testSurface = NULL;
  32. /* Fixture */
  33. /* Create a 32-bit writable surface for blitting tests */
  34. static void surfaceSetUp(void *arg)
  35. {
  36. int result;
  37. SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
  38. SDL_BlendMode currentBlendMode;
  39. referenceSurface = SDLTest_ImageBlit(); /* For size info */
  40. testSurface = SDL_CreateSurface(referenceSurface->w, referenceSurface->h, SDL_PIXELFORMAT_RGBA32);
  41. SDLTest_AssertCheck(testSurface != NULL, "Check that testSurface is not NULL");
  42. if (testSurface != NULL) {
  43. /* Disable blend mode for target surface */
  44. result = SDL_SetSurfaceBlendMode(testSurface, blendMode);
  45. SDLTest_AssertCheck(result == 0, "Validate result from SDL_SetSurfaceBlendMode, expected: 0, got: %i", result);
  46. result = SDL_GetSurfaceBlendMode(testSurface, &currentBlendMode);
  47. SDLTest_AssertCheck(result == 0, "Validate result from SDL_GetSurfaceBlendMode, expected: 0, got: %i", result);
  48. SDLTest_AssertCheck(currentBlendMode == blendMode, "Validate blendMode, expected: %" SDL_PRIu32 ", got: %" SDL_PRIu32, blendMode, currentBlendMode);
  49. /* Clear the target surface */
  50. result = SDL_FillSurfaceRect(testSurface, NULL, SDL_MapSurfaceRGBA(testSurface, 0, 0, 0, 255));
  51. SDLTest_AssertCheck(result == 0, "Validate result from SDL_FillSurfaceRect, expected: 0, got: %i", result);
  52. }
  53. }
  54. static void surfaceTearDown(void *arg)
  55. {
  56. SDL_DestroySurface(referenceSurface);
  57. referenceSurface = NULL;
  58. SDL_DestroySurface(testSurface);
  59. testSurface = NULL;
  60. }
  61. static void DitherPalette(SDL_Palette *palette)
  62. {
  63. int i;
  64. for (i = 0; i < palette->ncolors; i++) {
  65. int r, g, b;
  66. /* map each bit field to the full [0, 255] interval,
  67. so 0 is mapped to (0, 0, 0) and 255 to (255, 255, 255) */
  68. r = i & 0xe0;
  69. r |= r >> 3 | r >> 6;
  70. palette->colors[i].r = (Uint8)r;
  71. g = (i << 3) & 0xe0;
  72. g |= g >> 3 | g >> 6;
  73. palette->colors[i].g = (Uint8)g;
  74. b = i & 0x3;
  75. b |= b << 2;
  76. b |= b << 4;
  77. palette->colors[i].b = (Uint8)b;
  78. palette->colors[i].a = SDL_ALPHA_OPAQUE;
  79. }
  80. }
  81. /**
  82. * Helper that blits in a specific blend mode, -1 for color mod, -2 for alpha mod
  83. */
  84. static void testBlitBlendModeWithFormats(int mode, SDL_PixelFormat src_format, SDL_PixelFormat dst_format)
  85. {
  86. /* Allow up to 1 delta from theoretical value to account for rounding error */
  87. const int MAXIMUM_ERROR = 1;
  88. int ret;
  89. SDL_Surface *src;
  90. SDL_Surface *dst;
  91. Uint32 color;
  92. Uint8 srcR = 10, srcG = 128, srcB = 240, srcA = 100;
  93. Uint8 dstR = 128, dstG = 128, dstB = 128, dstA = 128;
  94. Uint8 expectedR, expectedG, expectedB, expectedA;
  95. Uint8 actualR, actualG, actualB, actualA;
  96. int deltaR, deltaG, deltaB, deltaA;
  97. /* Create dst surface */
  98. dst = SDL_CreateSurface(1, 1, dst_format);
  99. SDLTest_AssertCheck(dst != NULL, "Verify dst surface is not NULL");
  100. if (dst == NULL) {
  101. return;
  102. }
  103. /* Clear surface. */
  104. if (SDL_ISPIXELFORMAT_INDEXED(dst_format)) {
  105. SDL_Palette *palette = SDL_CreateSurfacePalette(dst);
  106. DitherPalette(palette);
  107. palette->colors[0].r = dstR;
  108. palette->colors[0].g = dstG;
  109. palette->colors[0].b = dstB;
  110. palette->colors[0].a = dstA;
  111. color = 0;
  112. } else {
  113. color = SDL_MapSurfaceRGBA(dst, dstR, dstG, dstB, dstA);
  114. SDLTest_AssertPass("Call to SDL_MapSurfaceRGBA()");
  115. }
  116. ret = SDL_FillSurfaceRect(dst, NULL, color);
  117. SDLTest_AssertPass("Call to SDL_FillSurfaceRect()");
  118. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_FillSurfaceRect, expected: 0, got: %i", ret);
  119. SDL_GetRGBA(color, SDL_GetPixelFormatDetails(dst->format), SDL_GetSurfacePalette(dst), &dstR, &dstG, &dstB, &dstA);
  120. /* Create src surface */
  121. src = SDL_CreateSurface(1, 1, src_format);
  122. SDLTest_AssertCheck(src != NULL, "Verify src surface is not NULL");
  123. if (src == NULL) {
  124. return;
  125. }
  126. if (SDL_ISPIXELFORMAT_INDEXED(src_format)) {
  127. SDL_Palette *palette = SDL_CreateSurfacePalette(src);
  128. palette->colors[0].r = srcR;
  129. palette->colors[0].g = srcG;
  130. palette->colors[0].b = srcB;
  131. palette->colors[0].a = srcA;
  132. }
  133. /* Reset alpha modulation */
  134. ret = SDL_SetSurfaceAlphaMod(src, 255);
  135. SDLTest_AssertPass("Call to SDL_SetSurfaceAlphaMod()");
  136. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SetSurfaceAlphaMod(), expected: 0, got: %i", ret);
  137. /* Reset color modulation */
  138. ret = SDL_SetSurfaceColorMod(src, 255, 255, 255);
  139. SDLTest_AssertPass("Call to SDL_SetSurfaceColorMod()");
  140. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SetSurfaceColorMod(), expected: 0, got: %i", ret);
  141. /* Reset color key */
  142. ret = SDL_SetSurfaceColorKey(src, SDL_FALSE, 0);
  143. SDLTest_AssertPass("Call to SDL_SetSurfaceColorKey()");
  144. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SetSurfaceColorKey(), expected: 0, got: %i", ret);
  145. /* Clear surface. */
  146. color = SDL_MapSurfaceRGBA(src, srcR, srcG, srcB, srcA);
  147. SDLTest_AssertPass("Call to SDL_MapSurfaceRGBA()");
  148. ret = SDL_FillSurfaceRect(src, NULL, color);
  149. SDLTest_AssertPass("Call to SDL_FillSurfaceRect()");
  150. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_FillSurfaceRect, expected: 0, got: %i", ret);
  151. SDL_GetRGBA(color, SDL_GetPixelFormatDetails(src->format), SDL_GetSurfacePalette(src), &srcR, &srcG, &srcB, &srcA);
  152. /* Set blend mode. */
  153. if (mode >= 0) {
  154. ret = SDL_SetSurfaceBlendMode(src, (SDL_BlendMode)mode);
  155. SDLTest_AssertPass("Call to SDL_SetSurfaceBlendMode()");
  156. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SetSurfaceBlendMode(..., %i), expected: 0, got: %i", mode, ret);
  157. } else {
  158. ret = SDL_SetSurfaceBlendMode(src, SDL_BLENDMODE_BLEND);
  159. SDLTest_AssertPass("Call to SDL_SetSurfaceBlendMode()");
  160. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SetSurfaceBlendMode(..., %i), expected: 0, got: %i", mode, ret);
  161. }
  162. /* Test blend mode. */
  163. #define FLOAT(X) ((float)X / 255.0f)
  164. switch (mode) {
  165. case -1:
  166. /* Set color mod. */
  167. ret = SDL_SetSurfaceColorMod(src, srcR, srcG, srcB);
  168. SDLTest_AssertCheck(ret == 0, "Validate results from calls to SDL_SetSurfaceColorMod, expected: 0, got: %i", ret);
  169. expectedR = (Uint8)SDL_roundf(SDL_clamp((FLOAT(srcR) * FLOAT(srcR)) * FLOAT(srcA) + FLOAT(dstR) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  170. expectedG = (Uint8)SDL_roundf(SDL_clamp((FLOAT(srcG) * FLOAT(srcG)) * FLOAT(srcA) + FLOAT(dstG) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  171. expectedB = (Uint8)SDL_roundf(SDL_clamp((FLOAT(srcB) * FLOAT(srcB)) * FLOAT(srcA) + FLOAT(dstB) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  172. expectedA = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcA) + FLOAT(dstA) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  173. break;
  174. case -2:
  175. /* Set alpha mod. */
  176. ret = SDL_SetSurfaceAlphaMod(src, srcA);
  177. SDLTest_AssertCheck(ret == 0, "Validate results from calls to SDL_SetSurfaceAlphaMod, expected: 0, got: %i", ret);
  178. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * (FLOAT(srcA) * FLOAT(srcA)) + FLOAT(dstR) * (1.0f - (FLOAT(srcA) * FLOAT(srcA))), 0.0f, 1.0f) * 255.0f);
  179. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * (FLOAT(srcA) * FLOAT(srcA)) + FLOAT(dstG) * (1.0f - (FLOAT(srcA) * FLOAT(srcA))), 0.0f, 1.0f) * 255.0f);
  180. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * (FLOAT(srcA) * FLOAT(srcA)) + FLOAT(dstB) * (1.0f - (FLOAT(srcA) * FLOAT(srcA))), 0.0f, 1.0f) * 255.0f);
  181. expectedA = (Uint8)SDL_roundf(SDL_clamp((FLOAT(srcA) * FLOAT(srcA)) + FLOAT(dstA) * (1.0f - (FLOAT(srcA) * FLOAT(srcA))), 0.0f, 1.0f) * 255.0f);
  182. break;
  183. case SDL_BLENDMODE_NONE:
  184. expectedR = srcR;
  185. expectedG = srcG;
  186. expectedB = srcB;
  187. expectedA = SDL_ISPIXELFORMAT_ALPHA(dst_format) ? srcA : 255;
  188. break;
  189. case SDL_BLENDMODE_BLEND:
  190. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * FLOAT(srcA) + FLOAT(dstR) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  191. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * FLOAT(srcA) + FLOAT(dstG) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  192. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * FLOAT(srcA) + FLOAT(dstB) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  193. expectedA = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcA) + FLOAT(dstA) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  194. break;
  195. case SDL_BLENDMODE_BLEND_PREMULTIPLIED:
  196. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) + FLOAT(dstR) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  197. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) + FLOAT(dstG) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  198. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) + FLOAT(dstB) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  199. expectedA = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcA) + FLOAT(dstA) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  200. break;
  201. case SDL_BLENDMODE_ADD:
  202. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * FLOAT(srcA) + FLOAT(dstR), 0.0f, 1.0f) * 255.0f);
  203. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * FLOAT(srcA) + FLOAT(dstG), 0.0f, 1.0f) * 255.0f);
  204. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * FLOAT(srcA) + FLOAT(dstB), 0.0f, 1.0f) * 255.0f);
  205. expectedA = dstA;
  206. break;
  207. case SDL_BLENDMODE_ADD_PREMULTIPLIED:
  208. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) + FLOAT(dstR), 0.0f, 1.0f) * 255.0f);
  209. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) + FLOAT(dstG), 0.0f, 1.0f) * 255.0f);
  210. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) + FLOAT(dstB), 0.0f, 1.0f) * 255.0f);
  211. expectedA = dstA;
  212. break;
  213. case SDL_BLENDMODE_MOD:
  214. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * FLOAT(dstR), 0.0f, 1.0f) * 255.0f);
  215. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * FLOAT(dstG), 0.0f, 1.0f) * 255.0f);
  216. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * FLOAT(dstB), 0.0f, 1.0f) * 255.0f);
  217. expectedA = dstA;
  218. break;
  219. case SDL_BLENDMODE_MUL:
  220. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * FLOAT(dstR) + FLOAT(dstR) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  221. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * FLOAT(dstG) + FLOAT(dstG) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  222. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * FLOAT(dstB) + FLOAT(dstB) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  223. expectedA = dstA;
  224. break;
  225. default:
  226. SDLTest_LogError("Invalid blending mode: %d", mode);
  227. return;
  228. }
  229. if (SDL_ISPIXELFORMAT_INDEXED(dst_format)) {
  230. SDL_Palette *palette = SDL_GetSurfacePalette(dst);
  231. palette->colors[1].r = expectedR;
  232. palette->colors[1].g = expectedG;
  233. palette->colors[1].b = expectedB;
  234. palette->colors[1].a = expectedA;
  235. }
  236. /* Blitting. */
  237. ret = SDL_BlitSurface(src, NULL, dst, NULL);
  238. SDLTest_AssertCheck(ret == 0, "Validate results from calls to SDL_BlitSurface, expected: 0, got: %i: %s", ret, (ret < 0) ? SDL_GetError() : "success");
  239. if (ret == 0) {
  240. SDL_ReadSurfacePixel(dst, 0, 0, &actualR, &actualG, &actualB, &actualA);
  241. deltaR = SDL_abs((int)actualR - expectedR);
  242. deltaG = SDL_abs((int)actualG - expectedG);
  243. deltaB = SDL_abs((int)actualB - expectedB);
  244. deltaA = SDL_abs((int)actualA - expectedA);
  245. SDLTest_AssertCheck(
  246. deltaR <= MAXIMUM_ERROR &&
  247. deltaG <= MAXIMUM_ERROR &&
  248. deltaB <= MAXIMUM_ERROR &&
  249. deltaA <= MAXIMUM_ERROR,
  250. "Checking %s -> %s blit results, expected %d,%d,%d,%d, got %d,%d,%d,%d",
  251. SDL_GetPixelFormatName(src_format),
  252. SDL_GetPixelFormatName(dst_format),
  253. expectedR, expectedG, expectedB, expectedA, actualR, actualG, actualB, actualA);
  254. }
  255. /* Clean up */
  256. SDL_DestroySurface(src);
  257. SDL_DestroySurface(dst);
  258. }
  259. static void testBlitBlendMode(int mode)
  260. {
  261. const SDL_PixelFormat src_formats[] = {
  262. SDL_PIXELFORMAT_INDEX8, SDL_PIXELFORMAT_XRGB8888, SDL_PIXELFORMAT_ARGB8888
  263. };
  264. const SDL_PixelFormat dst_formats[] = {
  265. SDL_PIXELFORMAT_XRGB8888, SDL_PIXELFORMAT_ARGB8888
  266. };
  267. int i, j;
  268. for (i = 0; i < SDL_arraysize(src_formats); ++i) {
  269. for (j = 0; j < SDL_arraysize(dst_formats); ++j) {
  270. testBlitBlendModeWithFormats(mode, src_formats[i], dst_formats[j]);
  271. }
  272. }
  273. }
  274. /* Helper to check that a file exists */
  275. static void AssertFileExist(const char *filename)
  276. {
  277. struct stat st;
  278. int ret = stat(filename, &st);
  279. SDLTest_AssertCheck(ret == 0, "Verify file '%s' exists", filename);
  280. }
  281. /* Test case functions */
  282. /**
  283. * Tests sprite saving and loading
  284. */
  285. static int surface_testSaveLoadBitmap(void *arg)
  286. {
  287. int ret;
  288. const char *sampleFilename = "testSaveLoadBitmap.bmp";
  289. SDL_Surface *face;
  290. SDL_Surface *rface;
  291. /* Create sample surface */
  292. face = SDLTest_ImageFace();
  293. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  294. if (face == NULL) {
  295. return TEST_ABORTED;
  296. }
  297. /* Delete test file; ignore errors */
  298. unlink(sampleFilename);
  299. /* Save a surface */
  300. ret = SDL_SaveBMP(face, sampleFilename);
  301. SDLTest_AssertPass("Call to SDL_SaveBMP()");
  302. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SaveBMP, expected: 0, got: %i", ret);
  303. AssertFileExist(sampleFilename);
  304. /* Load a surface */
  305. rface = SDL_LoadBMP(sampleFilename);
  306. SDLTest_AssertPass("Call to SDL_LoadBMP()");
  307. SDLTest_AssertCheck(rface != NULL, "Verify result from SDL_LoadBMP is not NULL");
  308. if (rface != NULL) {
  309. SDLTest_AssertCheck(face->w == rface->w, "Verify width of loaded surface, expected: %i, got: %i", face->w, rface->w);
  310. SDLTest_AssertCheck(face->h == rface->h, "Verify height of loaded surface, expected: %i, got: %i", face->h, rface->h);
  311. }
  312. /* Delete test file; ignore errors */
  313. unlink(sampleFilename);
  314. /* Clean up */
  315. SDL_DestroySurface(face);
  316. face = NULL;
  317. SDL_DestroySurface(rface);
  318. rface = NULL;
  319. return TEST_COMPLETED;
  320. }
  321. /**
  322. * Tests tiled blitting.
  323. */
  324. static int surface_testBlitTiled(void *arg)
  325. {
  326. SDL_Surface *face = NULL;
  327. SDL_Surface *testSurface2x = NULL;
  328. SDL_Surface *referenceSurface2x = NULL;
  329. int ret = 0;
  330. /* Create sample surface */
  331. face = SDLTest_ImageFace();
  332. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  333. if (face == NULL) {
  334. return TEST_ABORTED;
  335. }
  336. /* Tiled blit - 1.0 scale */
  337. {
  338. ret = SDL_BlitSurfaceTiled(face, NULL, testSurface, NULL);
  339. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_BlitSurfaceTiled expected: 0, got: %i", ret);
  340. /* See if it's the same */
  341. SDL_DestroySurface(referenceSurface);
  342. referenceSurface = SDLTest_ImageBlitTiled();
  343. ret = SDLTest_CompareSurfaces(testSurface, referenceSurface, 0);
  344. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  345. }
  346. /* Tiled blit - 2.0 scale */
  347. {
  348. testSurface2x = SDL_CreateSurface(testSurface->w * 2, testSurface->h * 2, testSurface->format);
  349. SDLTest_AssertCheck(testSurface != NULL, "Check that testSurface2x is not NULL");
  350. ret = SDL_FillSurfaceRect(testSurface2x, NULL, SDL_MapSurfaceRGBA(testSurface2x, 0, 0, 0, 255));
  351. SDLTest_AssertCheck(ret == 0, "Validate result from SDL_FillSurfaceRect, expected: 0, got: %i", ret);
  352. ret = SDL_BlitSurfaceTiledWithScale(face, NULL, 2.0f, SDL_SCALEMODE_NEAREST, testSurface2x, NULL);
  353. SDLTest_AssertCheck(ret == 0, "Validate results from call to SDL_RenderTextureTiled, expected: 0, got: %i", ret);
  354. /* See if it's the same */
  355. referenceSurface2x = SDL_CreateSurface(referenceSurface->w * 2, referenceSurface->h * 2, referenceSurface->format);
  356. SDL_BlitSurfaceScaled(referenceSurface, NULL, referenceSurface2x, NULL, SDL_SCALEMODE_NEAREST);
  357. SDLTest_AssertCheck(ret == 0, "Validate results from call to SDL_BlitSurfaceScaled, expected: 0, got: %i", ret);
  358. ret = SDLTest_CompareSurfaces(testSurface2x, referenceSurface2x, 0);
  359. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  360. }
  361. /* Clean up. */
  362. SDL_DestroySurface(face);
  363. SDL_DestroySurface(testSurface2x);
  364. SDL_DestroySurface(referenceSurface2x);
  365. return TEST_COMPLETED;
  366. }
  367. static const Uint8 COLOR_SEPARATION = 85;
  368. static void Fill9GridReferenceSurface(SDL_Surface *surface, int corner_size)
  369. {
  370. SDL_Rect rect;
  371. // Upper left
  372. rect.x = 0;
  373. rect.y = 0;
  374. rect.w = corner_size;
  375. rect.h = corner_size;
  376. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 1 * COLOR_SEPARATION, 1 * COLOR_SEPARATION, 0));
  377. // Top
  378. rect.x = corner_size;
  379. rect.y = 0;
  380. rect.w = surface->w - 2 * corner_size;
  381. rect.h = corner_size;
  382. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 2 * COLOR_SEPARATION, 1 * COLOR_SEPARATION, 0));
  383. // Upper right
  384. rect.x = surface->w - corner_size;
  385. rect.y = 0;
  386. rect.w = corner_size;
  387. rect.h = corner_size;
  388. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 3 * COLOR_SEPARATION, 1 * COLOR_SEPARATION, 0));
  389. // Left
  390. rect.x = 0;
  391. rect.y = corner_size;
  392. rect.w = corner_size;
  393. rect.h = surface->h - 2 * corner_size;
  394. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 1 * COLOR_SEPARATION, 2 * COLOR_SEPARATION, 0));
  395. // Center
  396. rect.x = corner_size;
  397. rect.y = corner_size;
  398. rect.w = surface->w - 2 * corner_size;
  399. rect.h = surface->h - 2 * corner_size;
  400. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 2 * COLOR_SEPARATION, 2 * COLOR_SEPARATION, 0));
  401. // Right
  402. rect.x = surface->w - corner_size;
  403. rect.y = corner_size;
  404. rect.w = corner_size;
  405. rect.h = surface->h - 2 * corner_size;
  406. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 3 * COLOR_SEPARATION, 2 * COLOR_SEPARATION, 0));
  407. // Lower left
  408. rect.x = 0;
  409. rect.y = surface->h - corner_size;
  410. rect.w = corner_size;
  411. rect.h = corner_size;
  412. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 1 * COLOR_SEPARATION, 3 * COLOR_SEPARATION, 0));
  413. // Bottom
  414. rect.x = corner_size;
  415. rect.y = surface->h - corner_size;
  416. rect.w = surface->w - 2 * corner_size;
  417. rect.h = corner_size;
  418. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 2 * COLOR_SEPARATION, 3 * COLOR_SEPARATION, 0));
  419. // Lower right
  420. rect.x = surface->w - corner_size;
  421. rect.y = surface->h - corner_size;
  422. rect.w = corner_size;
  423. rect.h = corner_size;
  424. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 3 * COLOR_SEPARATION, 3 * COLOR_SEPARATION, 0));
  425. }
  426. /**
  427. * Tests 9-grid blitting.
  428. */
  429. static int surface_testBlit9Grid(void *arg)
  430. {
  431. SDL_Surface *source = NULL;
  432. int x, y;
  433. int ret = 0;
  434. /* Create source surface */
  435. source = SDL_CreateSurface(3, 3, SDL_PIXELFORMAT_RGBA32);
  436. SDLTest_AssertCheck(source != NULL, "Verify source surface is not NULL");
  437. for (y = 0; y < 3; ++y) {
  438. for (x = 0; x < 3; ++x) {
  439. SDL_WriteSurfacePixel(source, x, y, (Uint8)((1 + x) * COLOR_SEPARATION), (Uint8)((1 + y) * COLOR_SEPARATION), 0, 255);
  440. }
  441. }
  442. /* 9-grid blit - 1.0 scale */
  443. {
  444. /* Create reference surface */
  445. SDL_DestroySurface(referenceSurface);
  446. referenceSurface = SDL_CreateSurface(testSurface->w, testSurface->h, testSurface->format);
  447. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  448. Fill9GridReferenceSurface(referenceSurface, 1);
  449. ret = SDL_BlitSurface9Grid(source, NULL, 1, 0.0f, SDL_SCALEMODE_NEAREST, testSurface, NULL);
  450. SDLTest_AssertCheck(ret == 0, "Validate result from SDL_BlitSurface9Grid, expected: 0, got: %i", ret);
  451. ret = SDLTest_CompareSurfaces(testSurface, referenceSurface, 0);
  452. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  453. }
  454. /* 9-grid blit - 2.0 scale */
  455. {
  456. /* Create reference surface */
  457. SDL_DestroySurface(referenceSurface);
  458. referenceSurface = SDL_CreateSurface(testSurface->w, testSurface->h, testSurface->format);
  459. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  460. Fill9GridReferenceSurface(referenceSurface, 2);
  461. ret = SDL_BlitSurface9Grid(source, NULL, 1, 2.0f, SDL_SCALEMODE_NEAREST, testSurface, NULL);
  462. SDLTest_AssertCheck(ret == 0, "Validate result from SDL_BlitSurface9Grid, expected: 0, got: %i", ret);
  463. ret = SDLTest_CompareSurfaces(testSurface, referenceSurface, 0);
  464. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  465. }
  466. /* Clean up. */
  467. SDL_DestroySurface(source);
  468. return TEST_COMPLETED;
  469. }
  470. /**
  471. * Tests surface conversion.
  472. */
  473. static int surface_testSurfaceConversion(void *arg)
  474. {
  475. SDL_Surface *rface = NULL, *face = NULL;
  476. int ret = 0;
  477. /* Create sample surface */
  478. face = SDLTest_ImageFace();
  479. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  480. if (face == NULL) {
  481. return TEST_ABORTED;
  482. }
  483. /* Set transparent pixel as the pixel at (0,0) */
  484. if (SDL_GetSurfacePalette(face)) {
  485. ret = SDL_SetSurfaceColorKey(face, SDL_TRUE, *(Uint8 *)face->pixels);
  486. SDLTest_AssertPass("Call to SDL_SetSurfaceColorKey()");
  487. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SetSurfaceColorKey, expected: 0, got: %i", ret);
  488. }
  489. /* Convert to 32 bit to compare. */
  490. rface = SDL_ConvertSurface(face, testSurface->format);
  491. SDLTest_AssertPass("Call to SDL_ConvertSurface()");
  492. SDLTest_AssertCheck(rface != NULL, "Verify result from SDL_ConvertSurface is not NULL");
  493. /* Compare surface. */
  494. ret = SDLTest_CompareSurfaces(rface, face, 0);
  495. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  496. /* Clean up. */
  497. SDL_DestroySurface(face);
  498. face = NULL;
  499. SDL_DestroySurface(rface);
  500. rface = NULL;
  501. return TEST_COMPLETED;
  502. }
  503. /**
  504. * Tests surface conversion across all pixel formats.
  505. */
  506. static int surface_testCompleteSurfaceConversion(void *arg)
  507. {
  508. Uint32 pixel_formats[] = {
  509. SDL_PIXELFORMAT_INDEX8,
  510. SDL_PIXELFORMAT_RGB332,
  511. SDL_PIXELFORMAT_XRGB4444,
  512. SDL_PIXELFORMAT_XBGR4444,
  513. SDL_PIXELFORMAT_XRGB1555,
  514. SDL_PIXELFORMAT_XBGR1555,
  515. SDL_PIXELFORMAT_ARGB4444,
  516. SDL_PIXELFORMAT_RGBA4444,
  517. SDL_PIXELFORMAT_ABGR4444,
  518. SDL_PIXELFORMAT_BGRA4444,
  519. SDL_PIXELFORMAT_ARGB1555,
  520. SDL_PIXELFORMAT_RGBA5551,
  521. SDL_PIXELFORMAT_ABGR1555,
  522. SDL_PIXELFORMAT_BGRA5551,
  523. SDL_PIXELFORMAT_RGB565,
  524. SDL_PIXELFORMAT_BGR565,
  525. SDL_PIXELFORMAT_RGB24,
  526. SDL_PIXELFORMAT_BGR24,
  527. SDL_PIXELFORMAT_XRGB8888,
  528. SDL_PIXELFORMAT_RGBX8888,
  529. SDL_PIXELFORMAT_XBGR8888,
  530. SDL_PIXELFORMAT_BGRX8888,
  531. SDL_PIXELFORMAT_ARGB8888,
  532. SDL_PIXELFORMAT_RGBA8888,
  533. SDL_PIXELFORMAT_ABGR8888,
  534. SDL_PIXELFORMAT_BGRA8888,
  535. #if 0 /* We aren't testing HDR10 colorspace conversion */
  536. SDL_PIXELFORMAT_XRGB2101010,
  537. SDL_PIXELFORMAT_XBGR2101010,
  538. SDL_PIXELFORMAT_ARGB2101010,
  539. SDL_PIXELFORMAT_ABGR2101010,
  540. #endif
  541. };
  542. SDL_Surface *face = NULL, *cvt1, *cvt2, *final;
  543. const SDL_PixelFormatDetails *fmt1, *fmt2;
  544. int i, j, ret = 0;
  545. /* Create sample surface */
  546. face = SDLTest_ImageFace();
  547. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  548. if (face == NULL) {
  549. return TEST_ABORTED;
  550. }
  551. /* Set transparent pixel as the pixel at (0,0) */
  552. if (SDL_GetSurfacePalette(face)) {
  553. ret = SDL_SetSurfaceColorKey(face, SDL_TRUE, *(Uint8 *)face->pixels);
  554. SDLTest_AssertPass("Call to SDL_SetSurfaceColorKey()");
  555. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SetSurfaceColorKey, expected: 0, got: %i", ret);
  556. }
  557. for (i = 0; i < SDL_arraysize(pixel_formats); ++i) {
  558. for (j = 0; j < SDL_arraysize(pixel_formats); ++j) {
  559. fmt1 = SDL_GetPixelFormatDetails(pixel_formats[i]);
  560. SDLTest_AssertCheck(fmt1 != NULL, "SDL_GetPixelFormatDetails(%s[0x%08" SDL_PRIx32 "]) should return a non-null pixel format",
  561. SDL_GetPixelFormatName(pixel_formats[i]), pixel_formats[i]);
  562. cvt1 = SDL_ConvertSurface(face, fmt1->format);
  563. SDLTest_AssertCheck(cvt1 != NULL, "SDL_ConvertSurface(..., %s[0x%08" SDL_PRIx32 "]) should return a non-null surface",
  564. SDL_GetPixelFormatName(pixel_formats[i]), pixel_formats[i]);
  565. fmt2 = SDL_GetPixelFormatDetails(pixel_formats[j]);
  566. SDLTest_AssertCheck(fmt2 != NULL, "SDL_GetPixelFormatDetails(%s[0x%08" SDL_PRIx32 "]) should return a non-null pixel format",
  567. SDL_GetPixelFormatName(pixel_formats[i]), pixel_formats[i]);
  568. cvt2 = SDL_ConvertSurface(cvt1, fmt2->format);
  569. SDLTest_AssertCheck(cvt2 != NULL, "SDL_ConvertSurface(..., %s[0x%08" SDL_PRIx32 "]) should return a non-null surface",
  570. SDL_GetPixelFormatName(pixel_formats[i]), pixel_formats[i]);
  571. if (fmt1 && fmt2 &&
  572. fmt1->bytes_per_pixel == SDL_BYTESPERPIXEL(face->format) &&
  573. fmt2->bytes_per_pixel == SDL_BYTESPERPIXEL(face->format) &&
  574. SDL_ISPIXELFORMAT_ALPHA(fmt1->format) == SDL_ISPIXELFORMAT_ALPHA(face->format) &&
  575. SDL_ISPIXELFORMAT_ALPHA(fmt2->format) == SDL_ISPIXELFORMAT_ALPHA(face->format)) {
  576. final = SDL_ConvertSurface(cvt2, face->format);
  577. SDL_assert(final != NULL);
  578. /* Compare surface. */
  579. ret = SDLTest_CompareSurfaces(face, final, 0);
  580. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  581. SDL_DestroySurface(final);
  582. }
  583. SDL_DestroySurface(cvt1);
  584. SDL_DestroySurface(cvt2);
  585. }
  586. }
  587. /* Clean up. */
  588. SDL_DestroySurface(face);
  589. return TEST_COMPLETED;
  590. }
  591. /**
  592. * Tests sprite loading. A failure case.
  593. */
  594. static int surface_testLoadFailure(void *arg)
  595. {
  596. SDL_Surface *face = SDL_LoadBMP("nonexistant.bmp");
  597. SDLTest_AssertCheck(face == NULL, "SDL_CreateLoadBmp");
  598. return TEST_COMPLETED;
  599. }
  600. /**
  601. * Tests some blitting routines.
  602. */
  603. static int surface_testBlit(void *arg)
  604. {
  605. /* Basic blitting */
  606. testBlitBlendMode(SDL_BLENDMODE_NONE);
  607. return TEST_COMPLETED;
  608. }
  609. /**
  610. * Tests some blitting routines with color mod
  611. */
  612. static int surface_testBlitColorMod(void *arg)
  613. {
  614. /* Basic blitting with color mod */
  615. testBlitBlendMode(-1);
  616. return TEST_COMPLETED;
  617. }
  618. /**
  619. * Tests some blitting routines with alpha mod
  620. */
  621. static int surface_testBlitAlphaMod(void *arg)
  622. {
  623. /* Basic blitting with alpha mod */
  624. testBlitBlendMode(-2);
  625. return TEST_COMPLETED;
  626. }
  627. /**
  628. * Tests some more blitting routines.
  629. */
  630. static int surface_testBlitBlendBlend(void *arg)
  631. {
  632. /* Blend blitting */
  633. testBlitBlendMode(SDL_BLENDMODE_BLEND);
  634. return TEST_COMPLETED;
  635. }
  636. /**
  637. * @brief Tests some more blitting routines.
  638. */
  639. static int surface_testBlitBlendPremultiplied(void *arg)
  640. {
  641. /* Blend premultiplied blitting */
  642. testBlitBlendMode(SDL_BLENDMODE_BLEND_PREMULTIPLIED);
  643. return TEST_COMPLETED;
  644. }
  645. /**
  646. * Tests some more blitting routines.
  647. */
  648. static int surface_testBlitBlendAdd(void *arg)
  649. {
  650. /* Add blitting */
  651. testBlitBlendMode(SDL_BLENDMODE_ADD);
  652. return TEST_COMPLETED;
  653. }
  654. /**
  655. * Tests some more blitting routines.
  656. */
  657. static int surface_testBlitBlendAddPremultiplied(void *arg)
  658. {
  659. /* Add premultiplied blitting */
  660. testBlitBlendMode(SDL_BLENDMODE_ADD_PREMULTIPLIED);
  661. return TEST_COMPLETED;
  662. }
  663. /**
  664. * Tests some more blitting routines.
  665. */
  666. static int surface_testBlitBlendMod(void *arg)
  667. {
  668. /* Mod blitting */
  669. testBlitBlendMode(SDL_BLENDMODE_MOD);
  670. return TEST_COMPLETED;
  671. }
  672. /**
  673. * Tests some more blitting routines.
  674. */
  675. static int surface_testBlitBlendMul(void *arg)
  676. {
  677. /* Mod blitting */
  678. testBlitBlendMode(SDL_BLENDMODE_MUL);
  679. return TEST_COMPLETED;
  680. }
  681. static int surface_testOverflow(void *arg)
  682. {
  683. char buf[1024];
  684. const char *expectedError;
  685. SDL_Surface *surface;
  686. SDL_memset(buf, '\0', sizeof(buf));
  687. expectedError = "Parameter 'width' is invalid";
  688. surface = SDL_CreateSurface(-3, 100, SDL_PIXELFORMAT_INDEX8);
  689. SDLTest_AssertCheck(surface == NULL, "Should detect negative width");
  690. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  691. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  692. surface = SDL_CreateSurfaceFrom(-1, 1, SDL_PIXELFORMAT_INDEX8, buf, 4);
  693. SDLTest_AssertCheck(surface == NULL, "Should detect negative width");
  694. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  695. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  696. surface = SDL_CreateSurfaceFrom(-1, 1, SDL_PIXELFORMAT_RGBA8888, buf, 4);
  697. SDLTest_AssertCheck(surface == NULL, "Should detect negative width");
  698. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  699. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  700. expectedError = "Parameter 'height' is invalid";
  701. surface = SDL_CreateSurface(100, -3, SDL_PIXELFORMAT_INDEX8);
  702. SDLTest_AssertCheck(surface == NULL, "Should detect negative height");
  703. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  704. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  705. surface = SDL_CreateSurfaceFrom(1, -1, SDL_PIXELFORMAT_INDEX8, buf, 4);
  706. SDLTest_AssertCheck(surface == NULL, "Should detect negative height");
  707. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  708. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  709. surface = SDL_CreateSurfaceFrom(1, -1, SDL_PIXELFORMAT_RGBA8888, buf, 4);
  710. SDLTest_AssertCheck(surface == NULL, "Should detect negative height");
  711. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  712. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  713. expectedError = "Parameter 'pitch' is invalid";
  714. surface = SDL_CreateSurfaceFrom(4, 1, SDL_PIXELFORMAT_INDEX8, buf, -1);
  715. SDLTest_AssertCheck(surface == NULL, "Should detect negative pitch");
  716. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  717. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  718. surface = SDL_CreateSurfaceFrom(1, 1, SDL_PIXELFORMAT_RGBA8888, buf, -1);
  719. SDLTest_AssertCheck(surface == NULL, "Should detect negative pitch");
  720. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  721. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  722. surface = SDL_CreateSurfaceFrom(1, 1, SDL_PIXELFORMAT_RGBA8888, buf, 0);
  723. SDLTest_AssertCheck(surface == NULL, "Should detect zero pitch");
  724. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  725. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  726. surface = SDL_CreateSurfaceFrom(1, 1, SDL_PIXELFORMAT_RGBA8888, NULL, 0);
  727. SDLTest_AssertCheck(surface != NULL, "Allow zero pitch for partially set up surfaces: %s",
  728. surface != NULL ? "(success)" : SDL_GetError());
  729. SDL_DestroySurface(surface);
  730. /* Less than 1 byte per pixel: the pitch can legitimately be less than
  731. * the width, but it must be enough to hold the appropriate number of
  732. * bits per pixel. SDL_PIXELFORMAT_INDEX4* needs 1 byte per 2 pixels. */
  733. surface = SDL_CreateSurfaceFrom(6, 1, SDL_PIXELFORMAT_INDEX4LSB, buf, 3);
  734. SDLTest_AssertCheck(surface != NULL, "6px * 4 bits per px fits in 3 bytes: %s",
  735. surface != NULL ? "(success)" : SDL_GetError());
  736. SDL_DestroySurface(surface);
  737. surface = SDL_CreateSurfaceFrom(6, 1, SDL_PIXELFORMAT_INDEX4MSB, buf, 3);
  738. SDLTest_AssertCheck(surface != NULL, "6px * 4 bits per px fits in 3 bytes: %s",
  739. surface != NULL ? "(success)" : SDL_GetError());
  740. SDL_DestroySurface(surface);
  741. surface = SDL_CreateSurfaceFrom(7, 1, SDL_PIXELFORMAT_INDEX4LSB, buf, 3);
  742. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  743. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  744. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  745. surface = SDL_CreateSurfaceFrom(7, 1, SDL_PIXELFORMAT_INDEX4MSB, buf, 3);
  746. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  747. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  748. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  749. surface = SDL_CreateSurfaceFrom(7, 1, SDL_PIXELFORMAT_INDEX4LSB, buf, 4);
  750. SDLTest_AssertCheck(surface != NULL, "7px * 4 bits per px fits in 4 bytes: %s",
  751. surface != NULL ? "(success)" : SDL_GetError());
  752. SDL_DestroySurface(surface);
  753. surface = SDL_CreateSurfaceFrom(7, 1, SDL_PIXELFORMAT_INDEX4MSB, buf, 4);
  754. SDLTest_AssertCheck(surface != NULL, "7px * 4 bits per px fits in 4 bytes: %s",
  755. surface != NULL ? "(success)" : SDL_GetError());
  756. SDL_DestroySurface(surface);
  757. /* SDL_PIXELFORMAT_INDEX2* needs 1 byte per 4 pixels. */
  758. surface = SDL_CreateSurfaceFrom(12, 1, SDL_PIXELFORMAT_INDEX2LSB, buf, 3);
  759. SDLTest_AssertCheck(surface != NULL, "12px * 2 bits per px fits in 3 bytes: %s",
  760. surface != NULL ? "(success)" : SDL_GetError());
  761. SDL_DestroySurface(surface);
  762. surface = SDL_CreateSurfaceFrom(12, 1, SDL_PIXELFORMAT_INDEX2MSB, buf, 3);
  763. SDLTest_AssertCheck(surface != NULL, "12px * 2 bits per px fits in 3 bytes: %s",
  764. surface != NULL ? "(success)" : SDL_GetError());
  765. SDL_DestroySurface(surface);
  766. surface = SDL_CreateSurfaceFrom(13, 1, SDL_PIXELFORMAT_INDEX2LSB, buf, 3);
  767. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp (%d)", surface ? surface->pitch : 0);
  768. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  769. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  770. surface = SDL_CreateSurfaceFrom(13, 1, SDL_PIXELFORMAT_INDEX2MSB, buf, 3);
  771. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  772. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  773. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  774. surface = SDL_CreateSurfaceFrom(13, 1, SDL_PIXELFORMAT_INDEX2LSB, buf, 4);
  775. SDLTest_AssertCheck(surface != NULL, "13px * 2 bits per px fits in 4 bytes: %s",
  776. surface != NULL ? "(success)" : SDL_GetError());
  777. SDL_DestroySurface(surface);
  778. surface = SDL_CreateSurfaceFrom(13, 1, SDL_PIXELFORMAT_INDEX2MSB, buf, 4);
  779. SDLTest_AssertCheck(surface != NULL, "13px * 2 bits per px fits in 4 bytes: %s",
  780. surface != NULL ? "(success)" : SDL_GetError());
  781. SDL_DestroySurface(surface);
  782. /* SDL_PIXELFORMAT_INDEX1* needs 1 byte per 8 pixels. */
  783. surface = SDL_CreateSurfaceFrom(16, 1, SDL_PIXELFORMAT_INDEX1LSB, buf, 2);
  784. SDLTest_AssertCheck(surface != NULL, "16px * 1 bit per px fits in 2 bytes: %s",
  785. surface != NULL ? "(success)" : SDL_GetError());
  786. SDL_DestroySurface(surface);
  787. surface = SDL_CreateSurfaceFrom(16, 1, SDL_PIXELFORMAT_INDEX1MSB, buf, 2);
  788. SDLTest_AssertCheck(surface != NULL, "16px * 1 bit per px fits in 2 bytes: %s",
  789. surface != NULL ? "(success)" : SDL_GetError());
  790. SDL_DestroySurface(surface);
  791. surface = SDL_CreateSurfaceFrom(17, 1, SDL_PIXELFORMAT_INDEX1LSB, buf, 2);
  792. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  793. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  794. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  795. surface = SDL_CreateSurfaceFrom(17, 1, SDL_PIXELFORMAT_INDEX1MSB, buf, 2);
  796. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  797. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  798. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  799. surface = SDL_CreateSurfaceFrom(17, 1, SDL_PIXELFORMAT_INDEX1LSB, buf, 3);
  800. SDLTest_AssertCheck(surface != NULL, "17px * 1 bit per px fits in 3 bytes: %s",
  801. surface != NULL ? "(success)" : SDL_GetError());
  802. SDL_DestroySurface(surface);
  803. surface = SDL_CreateSurfaceFrom(17, 1, SDL_PIXELFORMAT_INDEX1MSB, buf, 3);
  804. SDLTest_AssertCheck(surface != NULL, "17px * 1 bit per px fits in 3 bytes: %s",
  805. surface != NULL ? "(success)" : SDL_GetError());
  806. SDL_DestroySurface(surface);
  807. /* SDL_PIXELFORMAT_INDEX8 and SDL_PIXELFORMAT_RGB332 require 1 byte per pixel. */
  808. surface = SDL_CreateSurfaceFrom(5, 1, SDL_PIXELFORMAT_RGB332, buf, 5);
  809. SDLTest_AssertCheck(surface != NULL, "5px * 8 bits per px fits in 5 bytes: %s",
  810. surface != NULL ? "(success)" : SDL_GetError());
  811. SDL_DestroySurface(surface);
  812. surface = SDL_CreateSurfaceFrom(5, 1, SDL_PIXELFORMAT_INDEX8, buf, 5);
  813. SDLTest_AssertCheck(surface != NULL, "5px * 8 bits per px fits in 5 bytes: %s",
  814. surface != NULL ? "(success)" : SDL_GetError());
  815. SDL_DestroySurface(surface);
  816. surface = SDL_CreateSurfaceFrom(6, 1, SDL_PIXELFORMAT_RGB332, buf, 5);
  817. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  818. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  819. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  820. surface = SDL_CreateSurfaceFrom(6, 1, SDL_PIXELFORMAT_INDEX8, buf, 5);
  821. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  822. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  823. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  824. /* Everything else requires more than 1 byte per pixel, and rounds up
  825. * each pixel to an integer number of bytes (e.g. RGB555 is really
  826. * XRGB1555, with 1 bit per pixel wasted). */
  827. surface = SDL_CreateSurfaceFrom(3, 1, SDL_PIXELFORMAT_XRGB1555, buf, 6);
  828. SDLTest_AssertCheck(surface != NULL, "3px * 15 (really 16) bits per px fits in 6 bytes: %s",
  829. surface != NULL ? "(success)" : SDL_GetError());
  830. SDL_DestroySurface(surface);
  831. surface = SDL_CreateSurfaceFrom(3, 1, SDL_PIXELFORMAT_XRGB1555, buf, 6);
  832. SDLTest_AssertCheck(surface != NULL, "5px * 15 (really 16) bits per px fits in 6 bytes: %s",
  833. surface != NULL ? "(success)" : SDL_GetError());
  834. SDL_DestroySurface(surface);
  835. surface = SDL_CreateSurfaceFrom(4, 1, SDL_PIXELFORMAT_XRGB1555, buf, 6);
  836. SDLTest_AssertCheck(surface == NULL, "4px * 15 (really 16) bits per px doesn't fit in 6 bytes");
  837. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  838. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  839. surface = SDL_CreateSurfaceFrom(4, 1, SDL_PIXELFORMAT_XRGB1555, buf, 6);
  840. SDLTest_AssertCheck(surface == NULL, "4px * 15 (really 16) bits per px doesn't fit in 6 bytes");
  841. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  842. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  843. if (sizeof(size_t) == 4 && sizeof(int) >= 4) {
  844. SDL_ClearError();
  845. expectedError = "aligning pitch would overflow";
  846. /* 0x5555'5555 * 3bpp = 0xffff'ffff which fits in size_t, but adding
  847. * alignment padding makes it overflow */
  848. surface = SDL_CreateSurface(0x55555555, 1, SDL_PIXELFORMAT_RGB24);
  849. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in pitch + alignment");
  850. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  851. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  852. SDL_ClearError();
  853. expectedError = "width * bpp would overflow";
  854. /* 0x4000'0000 * 4bpp = 0x1'0000'0000 which (just) overflows */
  855. surface = SDL_CreateSurface(0x40000000, 1, SDL_PIXELFORMAT_ARGB8888);
  856. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in width * bytes per pixel");
  857. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  858. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  859. SDL_ClearError();
  860. expectedError = "height * pitch would overflow";
  861. surface = SDL_CreateSurface((1 << 29) - 1, (1 << 29) - 1, SDL_PIXELFORMAT_INDEX8);
  862. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in width * height");
  863. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  864. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  865. SDL_ClearError();
  866. expectedError = "height * pitch would overflow";
  867. surface = SDL_CreateSurface((1 << 15) + 1, (1 << 15) + 1, SDL_PIXELFORMAT_ARGB8888);
  868. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in width * height * bytes per pixel");
  869. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  870. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  871. } else {
  872. SDLTest_Log("Can't easily overflow size_t on this platform");
  873. }
  874. return TEST_COMPLETED;
  875. }
  876. static int surface_testFlip(void *arg)
  877. {
  878. SDL_Surface *surface;
  879. Uint8 *pixels;
  880. int offset;
  881. const char *expectedError;
  882. surface = SDL_CreateSurface(3, 3, SDL_PIXELFORMAT_RGB24);
  883. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  884. SDL_ClearError();
  885. expectedError = "Parameter 'surface' is invalid";
  886. SDL_FlipSurface(NULL, SDL_FLIP_HORIZONTAL);
  887. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  888. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  889. SDL_ClearError();
  890. expectedError = "Parameter 'flip' is invalid";
  891. SDL_FlipSurface(surface, SDL_FLIP_NONE);
  892. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  893. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  894. pixels = (Uint8 *)surface->pixels;
  895. *pixels = 0xFF;
  896. offset = 0;
  897. SDLTest_AssertPass("Call to SDL_FlipSurface(surface, SDL_FLIP_VERTICAL)");
  898. CHECK_FUNC(SDL_FlipSurface, (surface, SDL_FLIP_VERTICAL));
  899. SDLTest_AssertCheck(pixels[offset] == 0x00,
  900. "Expected pixels[%d] == 0x00 got 0x%.2X", offset, pixels[offset]);
  901. offset = 2 * surface->pitch;
  902. SDLTest_AssertCheck(pixels[offset] == 0xFF,
  903. "Expected pixels[%d] == 0xFF got 0x%.2X", offset, pixels[offset]);
  904. SDLTest_AssertPass("Call to SDL_FlipSurface(surface, SDL_FLIP_HORIZONTAL)");
  905. CHECK_FUNC(SDL_FlipSurface, (surface, SDL_FLIP_HORIZONTAL));
  906. SDLTest_AssertCheck(pixels[offset] == 0x00,
  907. "Expected pixels[%d] == 0x00 got 0x%.2X", offset, pixels[offset]);
  908. offset += (surface->w - 1) * SDL_BYTESPERPIXEL(surface->format);
  909. SDLTest_AssertCheck(pixels[offset] == 0xFF,
  910. "Expected pixels[%d] == 0xFF got 0x%.2X", offset, pixels[offset]);
  911. SDL_DestroySurface(surface);
  912. return TEST_COMPLETED;
  913. }
  914. static int surface_testPalette(void *arg)
  915. {
  916. SDL_Surface *source, *surface, *output;
  917. SDL_Palette *palette;
  918. Uint8 *pixels;
  919. palette = SDL_CreatePalette(2);
  920. SDLTest_AssertCheck(palette != NULL, "SDL_CreatePalette()");
  921. source = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_INDEX8);
  922. SDLTest_AssertCheck(source != NULL, "SDL_CreateSurface()");
  923. SDLTest_AssertCheck(SDL_GetSurfacePalette(source) == NULL, "SDL_GetSurfacePalette(source)");
  924. surface = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_INDEX8);
  925. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  926. SDLTest_AssertCheck(SDL_GetSurfacePalette(surface) == NULL, "SDL_GetSurfacePalette(surface)");
  927. pixels = (Uint8 *)surface->pixels;
  928. SDLTest_AssertCheck(*pixels == 0, "Expected *pixels == 0 got %u", *pixels);
  929. /* Identity copy between indexed surfaces without a palette */
  930. *(Uint8 *)source->pixels = 1;
  931. SDL_BlitSurface(source, NULL, surface, NULL);
  932. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  933. /* Identity copy between indexed surfaces where the destination has a palette */
  934. palette->colors[0].r = 0;
  935. palette->colors[0].g = 0;
  936. palette->colors[0].b = 0;
  937. palette->colors[1].r = 0xFF;
  938. palette->colors[1].g = 0;
  939. palette->colors[1].b = 0;
  940. SDL_SetSurfacePalette(surface, palette);
  941. *pixels = 0;
  942. SDL_BlitSurface(source, NULL, surface, NULL);
  943. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  944. output = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA32);
  945. SDLTest_AssertCheck(output != NULL, "SDL_CreateSurface()");
  946. pixels = (Uint8 *)output->pixels;
  947. SDL_BlitSurface(surface, NULL, output, NULL);
  948. SDLTest_AssertCheck(*pixels == 0xFF, "Expected *pixels == 0xFF got 0x%.2X", *pixels);
  949. /* Set the palette color and blit again */
  950. palette->colors[1].r = 0xAA;
  951. SDL_SetSurfacePalette(surface, palette);
  952. SDL_BlitSurface(surface, NULL, output, NULL);
  953. SDLTest_AssertCheck(*pixels == 0xAA, "Expected *pixels == 0xAA got 0x%.2X", *pixels);
  954. SDL_DestroyPalette(palette);
  955. SDL_DestroySurface(source);
  956. SDL_DestroySurface(surface);
  957. SDL_DestroySurface(output);
  958. return TEST_COMPLETED;
  959. }
  960. static int surface_testClearSurface(void *arg)
  961. {
  962. SDL_PixelFormat formats[] = {
  963. SDL_PIXELFORMAT_ARGB8888, SDL_PIXELFORMAT_RGBA8888,
  964. SDL_PIXELFORMAT_ARGB2101010, SDL_PIXELFORMAT_ABGR2101010,
  965. SDL_PIXELFORMAT_ARGB64, SDL_PIXELFORMAT_RGBA64,
  966. SDL_PIXELFORMAT_ARGB128_FLOAT, SDL_PIXELFORMAT_RGBA128_FLOAT,
  967. SDL_PIXELFORMAT_YV12, SDL_PIXELFORMAT_UYVY, SDL_PIXELFORMAT_NV12
  968. };
  969. SDL_Surface *surface;
  970. SDL_PixelFormat format;
  971. const float MAXIMUM_ERROR_RGB = 0.0001f;
  972. const float MAXIMUM_ERROR_YUV = 0.01f;
  973. float srcR = 10 / 255.0f, srcG = 128 / 255.0f, srcB = 240 / 255.0f, srcA = 1.0f;
  974. float actualR, actualG, actualB, actualA;
  975. float deltaR, deltaG, deltaB, deltaA;
  976. int i, ret;
  977. for (i = 0; i < SDL_arraysize(formats); ++i) {
  978. const float MAXIMUM_ERROR = SDL_ISPIXELFORMAT_FOURCC(formats[i]) ? MAXIMUM_ERROR_YUV : MAXIMUM_ERROR_RGB;
  979. format = formats[i];
  980. surface = SDL_CreateSurface(1, 1, format);
  981. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  982. ret = SDL_ClearSurface(surface, srcR, srcG, srcB, srcA);
  983. SDLTest_AssertCheck(ret == 0, "SDL_ClearSurface()");
  984. ret = SDL_ReadSurfacePixelFloat(surface, 0, 0, &actualR, &actualG, &actualB, &actualA);
  985. SDLTest_AssertCheck(ret == 0, "SDL_ReadSurfacePixelFloat()");
  986. deltaR = SDL_fabsf(actualR - srcR);
  987. deltaG = SDL_fabsf(actualG - srcG);
  988. deltaB = SDL_fabsf(actualB - srcB);
  989. deltaA = SDL_fabsf(actualA - srcA);
  990. SDLTest_AssertCheck(
  991. deltaR <= MAXIMUM_ERROR &&
  992. deltaG <= MAXIMUM_ERROR &&
  993. deltaB <= MAXIMUM_ERROR &&
  994. deltaA <= MAXIMUM_ERROR,
  995. "Checking %s surface clear results, expected %.4f,%.4f,%.4f,%.4f, got %.4f,%.4f,%.4f,%.4f",
  996. SDL_GetPixelFormatName(format),
  997. srcR, srcG, srcB, srcA, actualR, actualG, actualB, actualA);
  998. SDL_DestroySurface(surface);
  999. }
  1000. return TEST_COMPLETED;
  1001. }
  1002. static int surface_testPremultiplyAlpha(void *arg)
  1003. {
  1004. SDL_PixelFormat formats[] = {
  1005. SDL_PIXELFORMAT_ARGB8888, SDL_PIXELFORMAT_RGBA8888,
  1006. SDL_PIXELFORMAT_ARGB2101010, SDL_PIXELFORMAT_ABGR2101010,
  1007. SDL_PIXELFORMAT_ARGB64, SDL_PIXELFORMAT_RGBA64,
  1008. SDL_PIXELFORMAT_ARGB128_FLOAT, SDL_PIXELFORMAT_RGBA128_FLOAT,
  1009. };
  1010. SDL_Surface *surface;
  1011. SDL_PixelFormat format;
  1012. const float MAXIMUM_ERROR_LOW_PRECISION = 1 / 255.0f;
  1013. const float MAXIMUM_ERROR_HIGH_PRECISION = 0.0001f;
  1014. float srcR = 10 / 255.0f, srcG = 128 / 255.0f, srcB = 240 / 255.0f, srcA = 170 / 255.0f;
  1015. float expectedR = srcR * srcA;
  1016. float expectedG = srcG * srcA;
  1017. float expectedB = srcB * srcA;
  1018. float actualR, actualG, actualB;
  1019. float deltaR, deltaG, deltaB;
  1020. int i, ret;
  1021. for (i = 0; i < SDL_arraysize(formats); ++i) {
  1022. const float MAXIMUM_ERROR = (SDL_BITSPERPIXEL(formats[i]) > 32) ? MAXIMUM_ERROR_HIGH_PRECISION : MAXIMUM_ERROR_LOW_PRECISION;
  1023. format = formats[i];
  1024. surface = SDL_CreateSurface(1, 1, format);
  1025. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  1026. ret = SDL_SetSurfaceColorspace(surface, SDL_COLORSPACE_SRGB);
  1027. SDLTest_AssertCheck(ret == 0, "SDL_SetSurfaceColorspace()");
  1028. ret = SDL_ClearSurface(surface, srcR, srcG, srcB, srcA);
  1029. SDLTest_AssertCheck(ret == 0, "SDL_ClearSurface()");
  1030. ret = SDL_PremultiplySurfaceAlpha(surface, SDL_FALSE);
  1031. SDLTest_AssertCheck(ret == 0, "SDL_PremultiplySurfaceAlpha()");
  1032. ret = SDL_ReadSurfacePixelFloat(surface, 0, 0, &actualR, &actualG, &actualB, NULL);
  1033. SDLTest_AssertCheck(ret == 0, "SDL_ReadSurfacePixelFloat()");
  1034. deltaR = SDL_fabsf(actualR - expectedR);
  1035. deltaG = SDL_fabsf(actualG - expectedG);
  1036. deltaB = SDL_fabsf(actualB - expectedB);
  1037. SDLTest_AssertCheck(
  1038. deltaR <= MAXIMUM_ERROR &&
  1039. deltaG <= MAXIMUM_ERROR &&
  1040. deltaB <= MAXIMUM_ERROR,
  1041. "Checking %s alpha premultiply results, expected %.4f,%.4f,%.4f, got %.4f,%.4f,%.4f",
  1042. SDL_GetPixelFormatName(format),
  1043. expectedR, expectedG, expectedB, actualR, actualG, actualB);
  1044. SDL_DestroySurface(surface);
  1045. }
  1046. return TEST_COMPLETED;
  1047. }
  1048. /* ================= Test References ================== */
  1049. /* Surface test cases */
  1050. static const SDLTest_TestCaseReference surfaceTestSaveLoadBitmap = {
  1051. (SDLTest_TestCaseFp)surface_testSaveLoadBitmap, "surface_testSaveLoadBitmap", "Tests sprite saving and loading.", TEST_ENABLED
  1052. };
  1053. static const SDLTest_TestCaseReference surfaceTestBlit = {
  1054. (SDLTest_TestCaseFp)surface_testBlit, "surface_testBlit", "Tests basic blitting.", TEST_ENABLED
  1055. };
  1056. static const SDLTest_TestCaseReference surfaceTestBlitTiled = {
  1057. (SDLTest_TestCaseFp)surface_testBlitTiled, "surface_testBlitTiled", "Tests tiled blitting.", TEST_ENABLED
  1058. };
  1059. static const SDLTest_TestCaseReference surfaceTestBlit9Grid = {
  1060. (SDLTest_TestCaseFp)surface_testBlit9Grid, "surface_testBlit9Grid", "Tests 9-grid blitting.", TEST_ENABLED
  1061. };
  1062. static const SDLTest_TestCaseReference surfaceTestLoadFailure = {
  1063. (SDLTest_TestCaseFp)surface_testLoadFailure, "surface_testLoadFailure", "Tests sprite loading. A failure case.", TEST_ENABLED
  1064. };
  1065. static const SDLTest_TestCaseReference surfaceTestSurfaceConversion = {
  1066. (SDLTest_TestCaseFp)surface_testSurfaceConversion, "surface_testSurfaceConversion", "Tests surface conversion.", TEST_ENABLED
  1067. };
  1068. static const SDLTest_TestCaseReference surfaceTestCompleteSurfaceConversion = {
  1069. (SDLTest_TestCaseFp)surface_testCompleteSurfaceConversion, "surface_testCompleteSurfaceConversion", "Tests surface conversion across all pixel formats", TEST_ENABLED
  1070. };
  1071. static const SDLTest_TestCaseReference surfaceTestBlitColorMod = {
  1072. (SDLTest_TestCaseFp)surface_testBlitColorMod, "surface_testBlitColorMod", "Tests some blitting routines with color mod.", TEST_ENABLED
  1073. };
  1074. static const SDLTest_TestCaseReference surfaceTestBlitAlphaMod = {
  1075. (SDLTest_TestCaseFp)surface_testBlitAlphaMod, "surface_testBlitAlphaMod", "Tests some blitting routines with alpha mod.", TEST_ENABLED
  1076. };
  1077. static const SDLTest_TestCaseReference surfaceTestBlitBlendBlend = {
  1078. (SDLTest_TestCaseFp)surface_testBlitBlendBlend, "surface_testBlitBlendBlend", "Tests blitting routines with blend blending mode.", TEST_ENABLED
  1079. };
  1080. static const SDLTest_TestCaseReference surfaceTestBlitBlendPremultiplied = {
  1081. (SDLTest_TestCaseFp)surface_testBlitBlendPremultiplied, "surface_testBlitBlendPremultiplied", "Tests blitting routines with premultiplied blending mode.", TEST_ENABLED
  1082. };
  1083. static const SDLTest_TestCaseReference surfaceTestBlitBlendAdd = {
  1084. (SDLTest_TestCaseFp)surface_testBlitBlendAdd, "surface_testBlitBlendAdd", "Tests blitting routines with add blending mode.", TEST_ENABLED
  1085. };
  1086. static const SDLTest_TestCaseReference surfaceTestBlitBlendAddPremultiplied = {
  1087. (SDLTest_TestCaseFp)surface_testBlitBlendAddPremultiplied, "surface_testBlitBlendAddPremultiplied", "Tests blitting routines with premultiplied add blending mode.", TEST_ENABLED
  1088. };
  1089. static const SDLTest_TestCaseReference surfaceTestBlitBlendMod = {
  1090. (SDLTest_TestCaseFp)surface_testBlitBlendMod, "surface_testBlitBlendMod", "Tests blitting routines with mod blending mode.", TEST_ENABLED
  1091. };
  1092. static const SDLTest_TestCaseReference surfaceTestBlitBlendMul = {
  1093. (SDLTest_TestCaseFp)surface_testBlitBlendMul, "surface_testBlitBlendMul", "Tests blitting routines with mul blending mode.", TEST_ENABLED
  1094. };
  1095. static const SDLTest_TestCaseReference surfaceTestOverflow = {
  1096. surface_testOverflow, "surface_testOverflow", "Test overflow detection.", TEST_ENABLED
  1097. };
  1098. static const SDLTest_TestCaseReference surfaceTestFlip = {
  1099. surface_testFlip, "surface_testFlip", "Test surface flipping.", TEST_ENABLED
  1100. };
  1101. static const SDLTest_TestCaseReference surfaceTestPalette = {
  1102. surface_testPalette, "surface_testPalette", "Test surface palette operations.", TEST_ENABLED
  1103. };
  1104. static const SDLTest_TestCaseReference surfaceTestClearSurface = {
  1105. surface_testClearSurface, "surface_testClearSurface", "Test clear surface operations.", TEST_ENABLED
  1106. };
  1107. static const SDLTest_TestCaseReference surfaceTestPremultiplyAlpha = {
  1108. surface_testPremultiplyAlpha, "surface_testPremultiplyAlpha", "Test alpha premultiply operations.", TEST_ENABLED
  1109. };
  1110. /* Sequence of Surface test cases */
  1111. static const SDLTest_TestCaseReference *surfaceTests[] = {
  1112. &surfaceTestSaveLoadBitmap,
  1113. &surfaceTestBlit,
  1114. &surfaceTestBlitTiled,
  1115. &surfaceTestBlit9Grid,
  1116. &surfaceTestLoadFailure,
  1117. &surfaceTestSurfaceConversion,
  1118. &surfaceTestCompleteSurfaceConversion,
  1119. &surfaceTestBlitColorMod,
  1120. &surfaceTestBlitAlphaMod,
  1121. &surfaceTestBlitBlendBlend,
  1122. &surfaceTestBlitBlendPremultiplied,
  1123. &surfaceTestBlitBlendAdd,
  1124. &surfaceTestBlitBlendAddPremultiplied,
  1125. &surfaceTestBlitBlendMod,
  1126. &surfaceTestBlitBlendMul,
  1127. &surfaceTestOverflow,
  1128. &surfaceTestFlip,
  1129. &surfaceTestPalette,
  1130. &surfaceTestClearSurface,
  1131. &surfaceTestPremultiplyAlpha,
  1132. NULL
  1133. };
  1134. /* Surface test suite (global) */
  1135. SDLTest_TestSuiteReference surfaceTestSuite = {
  1136. "Surface",
  1137. surfaceSetUp,
  1138. surfaceTests,
  1139. surfaceTearDown
  1140. };