testautomation_surface.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675
  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. bool result = FUNC PARAMS; \
  24. if (!result) { \
  25. SDLTest_AssertCheck(result, "Validate result from %s, expected: true, got: false, %s", #FUNC, 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 SDLCALL 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 == true, "Validate result from SDL_SetSurfaceBlendMode, expected: true, got: %i", result);
  46. result = SDL_GetSurfaceBlendMode(testSurface, &currentBlendMode);
  47. SDLTest_AssertCheck(result == true, "Validate result from SDL_GetSurfaceBlendMode, expected: true, 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 == true, "Validate result from SDL_FillSurfaceRect, expected: true, got: %i", result);
  52. }
  53. }
  54. static void SDLCALL 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 == true, "Verify result from SDL_FillSurfaceRect, expected: true, 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 == true, "Verify result from SDL_SetSurfaceAlphaMod(), expected: true, 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 == true, "Verify result from SDL_SetSurfaceColorMod(), expected: true, got: %i", ret);
  141. /* Reset color key */
  142. ret = SDL_SetSurfaceColorKey(src, false, 0);
  143. SDLTest_AssertPass("Call to SDL_SetSurfaceColorKey()");
  144. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetSurfaceColorKey(), expected: true, 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 == true, "Verify result from SDL_FillSurfaceRect, expected: true, 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 == true, "Verify result from SDL_SetSurfaceBlendMode(..., %i), expected: true, 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 == true, "Verify result from SDL_SetSurfaceBlendMode(..., %i), expected: true, 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 == true, "Validate results from calls to SDL_SetSurfaceColorMod, expected: true, 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 == true, "Validate results from calls to SDL_SetSurfaceAlphaMod, expected: true, 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 == true, "Validate results from calls to SDL_BlitSurface, expected: true, got: %i: %s", ret, !ret ? SDL_GetError() : "success");
  239. if (ret) {
  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 SDLCALL 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 == true, "Verify result from SDL_SaveBMP, expected: true, 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 SDLCALL 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 == true, "Verify result from SDL_BlitSurfaceTiled expected: true, 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 == true, "Validate result from SDL_FillSurfaceRect, expected: true, got: %i", ret);
  352. ret = SDL_BlitSurfaceTiledWithScale(face, NULL, 2.0f, SDL_SCALEMODE_NEAREST, testSurface2x, NULL);
  353. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_BlitSurfaceTiledWithScale, expected: true, 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 == true, "Validate results from call to SDL_BlitSurfaceScaled, expected: true, 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 left_width, int right_width, int top_height, int bottom_height)
  369. {
  370. SDL_Rect rect;
  371. // Upper left
  372. rect.x = 0;
  373. rect.y = 0;
  374. rect.w = left_width;
  375. rect.h = top_height;
  376. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 1 * COLOR_SEPARATION, 1 * COLOR_SEPARATION, 0));
  377. // Top
  378. rect.x = left_width;
  379. rect.y = 0;
  380. rect.w = surface->w - left_width - right_width;
  381. rect.h = top_height;
  382. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 2 * COLOR_SEPARATION, 1 * COLOR_SEPARATION, 0));
  383. // Upper right
  384. rect.x = surface->w - right_width;
  385. rect.y = 0;
  386. rect.w = right_width;
  387. rect.h = top_height;
  388. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 3 * COLOR_SEPARATION, 1 * COLOR_SEPARATION, 0));
  389. // Left
  390. rect.x = 0;
  391. rect.y = top_height;
  392. rect.w = left_width;
  393. rect.h = surface->h - top_height - bottom_height;
  394. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 1 * COLOR_SEPARATION, 2 * COLOR_SEPARATION, 0));
  395. // Center
  396. rect.x = left_width;
  397. rect.y = top_height;
  398. rect.w = surface->w - right_width - left_width;
  399. rect.h = surface->h - top_height - bottom_height;
  400. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 2 * COLOR_SEPARATION, 2 * COLOR_SEPARATION, 0));
  401. // Right
  402. rect.x = surface->w - right_width;
  403. rect.y = top_height;
  404. rect.w = right_width;
  405. rect.h = surface->h - top_height - bottom_height;
  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 - bottom_height;
  410. rect.w = left_width;
  411. rect.h = bottom_height;
  412. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 1 * COLOR_SEPARATION, 3 * COLOR_SEPARATION, 0));
  413. // Bottom
  414. rect.x = left_width;
  415. rect.y = surface->h - bottom_height;
  416. rect.w = surface->w - left_width - right_width;
  417. rect.h = bottom_height;
  418. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 2 * COLOR_SEPARATION, 3 * COLOR_SEPARATION, 0));
  419. // Lower right
  420. rect.x = surface->w - right_width;
  421. rect.y = surface->h - bottom_height;
  422. rect.w = right_width;
  423. rect.h = bottom_height;
  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 SDLCALL 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, 1, 1, 1);
  449. ret = SDL_BlitSurface9Grid(source, NULL, 1, 1, 1, 1, 0.0f, SDL_SCALEMODE_NEAREST, testSurface, NULL);
  450. SDLTest_AssertCheck(ret == true, "Validate result from SDL_BlitSurface9Grid, expected: true, 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, 2, 2, 2);
  461. ret = SDL_BlitSurface9Grid(source, NULL, 1, 1, 1, 1, 2.0f, SDL_SCALEMODE_NEAREST, testSurface, NULL);
  462. SDLTest_AssertCheck(ret == true, "Validate result from SDL_BlitSurface9Grid, expected: true, 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. /* Create complex source surface */
  469. source = SDL_CreateSurface(5, 5, SDL_PIXELFORMAT_RGBA32);
  470. SDLTest_AssertCheck(source != NULL, "Verify source surface is not NULL");
  471. SDL_WriteSurfacePixel(source, 0, 0, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  472. SDL_WriteSurfacePixel(source, 1, 0, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  473. SDL_WriteSurfacePixel(source, 2, 0, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  474. SDL_WriteSurfacePixel(source, 3, 0, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  475. SDL_WriteSurfacePixel(source, 4, 0, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  476. SDL_WriteSurfacePixel(source, 0, 1, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  477. SDL_WriteSurfacePixel(source, 1, 1, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  478. SDL_WriteSurfacePixel(source, 2, 1, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  479. SDL_WriteSurfacePixel(source, 3, 1, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  480. SDL_WriteSurfacePixel(source, 4, 1, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  481. SDL_WriteSurfacePixel(source, 0, 2, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  482. SDL_WriteSurfacePixel(source, 1, 2, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  483. SDL_WriteSurfacePixel(source, 2, 2, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  484. SDL_WriteSurfacePixel(source, 3, 2, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  485. SDL_WriteSurfacePixel(source, 4, 2, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  486. SDL_WriteSurfacePixel(source, 0, 3, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  487. SDL_WriteSurfacePixel(source, 1, 3, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  488. SDL_WriteSurfacePixel(source, 2, 3, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  489. SDL_WriteSurfacePixel(source, 3, 3, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  490. SDL_WriteSurfacePixel(source, 4, 3, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  491. SDL_WriteSurfacePixel(source, 0, 4, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  492. SDL_WriteSurfacePixel(source, 1, 4, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  493. SDL_WriteSurfacePixel(source, 2, 4, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  494. SDL_WriteSurfacePixel(source, 3, 4, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  495. SDL_WriteSurfacePixel(source, 4, 4, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  496. /* complex 9-grid blit - 1.0 scale */
  497. {
  498. SDLTest_Log("complex 9-grid blit - 1.0 scale");
  499. /* Create reference surface */
  500. SDL_DestroySurface(referenceSurface);
  501. referenceSurface = SDL_CreateSurface(testSurface->w, testSurface->h, testSurface->format);
  502. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  503. Fill9GridReferenceSurface(referenceSurface, 1, 2, 1, 2);
  504. ret = SDL_BlitSurface9Grid(source, NULL, 1, 2, 1, 2, 0.0f, SDL_SCALEMODE_NEAREST, testSurface, NULL);
  505. SDLTest_AssertCheck(ret == true, "Validate result from SDL_BlitSurface9Grid, expected: true, got: %i", ret);
  506. ret = SDLTest_CompareSurfaces(testSurface, referenceSurface, 0);
  507. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  508. }
  509. /* complex 9-grid blit - 2.0 scale */
  510. {
  511. SDLTest_Log("complex 9-grid blit - 2.0 scale");
  512. /* Create reference surface */
  513. SDL_DestroySurface(referenceSurface);
  514. referenceSurface = SDL_CreateSurface(testSurface->w, testSurface->h, testSurface->format);
  515. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  516. Fill9GridReferenceSurface(referenceSurface, 2, 4, 2, 4);
  517. ret = SDL_BlitSurface9Grid(source, NULL, 1, 2, 1, 2, 2.0f, SDL_SCALEMODE_NEAREST, testSurface, NULL);
  518. SDLTest_AssertCheck(ret == true, "Validate result from SDL_BlitSurface9Grid, expected: true, got: %i", ret);
  519. ret = SDLTest_CompareSurfaces(testSurface, referenceSurface, 0);
  520. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  521. }
  522. /* Clean up. */
  523. SDL_DestroySurface(source);
  524. return TEST_COMPLETED;
  525. }
  526. /**
  527. * Tests blitting between multiple surfaces of the same format
  528. */
  529. static int SDLCALL surface_testBlitMultiple(void *arg)
  530. {
  531. SDL_Surface *source, *surface;
  532. SDL_Palette *palette;
  533. Uint8 *pixels;
  534. palette = SDL_CreatePalette(2);
  535. SDLTest_AssertCheck(palette != NULL, "SDL_CreatePalette()");
  536. palette->colors[0].r = 0;
  537. palette->colors[0].g = 0;
  538. palette->colors[0].b = 0;
  539. palette->colors[1].r = 0xFF;
  540. palette->colors[1].g = 0;
  541. palette->colors[1].b = 0;
  542. source = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_INDEX8);
  543. SDLTest_AssertCheck(source != NULL, "SDL_CreateSurface()");
  544. SDL_SetSurfacePalette(source, palette);
  545. *(Uint8 *)source->pixels = 1;
  546. /* Set up a blit to a surface using the palette */
  547. surface = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_INDEX8);
  548. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  549. SDL_SetSurfacePalette(surface, palette);
  550. pixels = (Uint8 *)surface->pixels;
  551. *pixels = 0;
  552. SDL_BlitSurface(source, NULL, surface, NULL);
  553. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  554. /* Set up a blit to another surface using the same palette */
  555. SDL_DestroySurface(surface);
  556. surface = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_INDEX8);
  557. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  558. SDL_SetSurfacePalette(surface, palette);
  559. pixels = (Uint8 *)surface->pixels;
  560. *pixels = 0;
  561. SDL_BlitSurface(source, NULL, surface, NULL);
  562. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  563. /* Set up a blit to new surface with a different format */
  564. SDL_DestroySurface(surface);
  565. surface = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA32);
  566. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  567. pixels = (Uint8 *)surface->pixels;
  568. SDL_BlitSurface(source, NULL, surface, NULL);
  569. SDLTest_AssertCheck(*pixels == 0xFF, "Expected *pixels == 0xFF got 0x%.2X", *pixels);
  570. /* Set up a blit to another surface with the same format */
  571. SDL_DestroySurface(surface);
  572. surface = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA32);
  573. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  574. pixels = (Uint8 *)surface->pixels;
  575. SDL_BlitSurface(source, NULL, surface, NULL);
  576. SDLTest_AssertCheck(*pixels == 0xFF, "Expected *pixels == 0xFF got 0x%.2X", *pixels);
  577. SDL_DestroyPalette(palette);
  578. SDL_DestroySurface(source);
  579. SDL_DestroySurface(surface);
  580. return TEST_COMPLETED;
  581. }
  582. /**
  583. * Tests surface conversion.
  584. */
  585. static int SDLCALL surface_testSurfaceConversion(void *arg)
  586. {
  587. SDL_Surface *rface = NULL, *face = NULL;
  588. int ret = 0;
  589. /* Create sample surface */
  590. face = SDLTest_ImageFace();
  591. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  592. if (face == NULL) {
  593. return TEST_ABORTED;
  594. }
  595. /* Set transparent pixel as the pixel at (0,0) */
  596. if (SDL_GetSurfacePalette(face)) {
  597. ret = SDL_SetSurfaceColorKey(face, true, *(Uint8 *)face->pixels);
  598. SDLTest_AssertPass("Call to SDL_SetSurfaceColorKey()");
  599. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetSurfaceColorKey, expected: true, got: %i", ret);
  600. }
  601. /* Convert to 32 bit to compare. */
  602. rface = SDL_ConvertSurface(face, testSurface->format);
  603. SDLTest_AssertPass("Call to SDL_ConvertSurface()");
  604. SDLTest_AssertCheck(rface != NULL, "Verify result from SDL_ConvertSurface is not NULL");
  605. /* Compare surface. */
  606. ret = SDLTest_CompareSurfaces(rface, face, 0);
  607. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  608. /* Clean up. */
  609. SDL_DestroySurface(face);
  610. face = NULL;
  611. SDL_DestroySurface(rface);
  612. rface = NULL;
  613. return TEST_COMPLETED;
  614. }
  615. /**
  616. * Tests surface conversion across all pixel formats.
  617. */
  618. static int SDLCALL surface_testCompleteSurfaceConversion(void *arg)
  619. {
  620. Uint32 pixel_formats[] = {
  621. SDL_PIXELFORMAT_INDEX8,
  622. SDL_PIXELFORMAT_RGB332,
  623. SDL_PIXELFORMAT_XRGB4444,
  624. SDL_PIXELFORMAT_XBGR4444,
  625. SDL_PIXELFORMAT_XRGB1555,
  626. SDL_PIXELFORMAT_XBGR1555,
  627. SDL_PIXELFORMAT_ARGB4444,
  628. SDL_PIXELFORMAT_RGBA4444,
  629. SDL_PIXELFORMAT_ABGR4444,
  630. SDL_PIXELFORMAT_BGRA4444,
  631. SDL_PIXELFORMAT_ARGB1555,
  632. SDL_PIXELFORMAT_RGBA5551,
  633. SDL_PIXELFORMAT_ABGR1555,
  634. SDL_PIXELFORMAT_BGRA5551,
  635. SDL_PIXELFORMAT_RGB565,
  636. SDL_PIXELFORMAT_BGR565,
  637. SDL_PIXELFORMAT_RGB24,
  638. SDL_PIXELFORMAT_BGR24,
  639. SDL_PIXELFORMAT_XRGB8888,
  640. SDL_PIXELFORMAT_RGBX8888,
  641. SDL_PIXELFORMAT_XBGR8888,
  642. SDL_PIXELFORMAT_BGRX8888,
  643. SDL_PIXELFORMAT_ARGB8888,
  644. SDL_PIXELFORMAT_RGBA8888,
  645. SDL_PIXELFORMAT_ABGR8888,
  646. SDL_PIXELFORMAT_BGRA8888,
  647. #if 0 /* We aren't testing HDR10 colorspace conversion */
  648. SDL_PIXELFORMAT_XRGB2101010,
  649. SDL_PIXELFORMAT_XBGR2101010,
  650. SDL_PIXELFORMAT_ARGB2101010,
  651. SDL_PIXELFORMAT_ABGR2101010,
  652. #endif
  653. };
  654. SDL_Surface *face = NULL, *cvt1, *cvt2, *final;
  655. const SDL_PixelFormatDetails *fmt1, *fmt2;
  656. int i, j, ret = 0;
  657. /* Create sample surface */
  658. face = SDLTest_ImageFace();
  659. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  660. if (face == NULL) {
  661. return TEST_ABORTED;
  662. }
  663. /* Set transparent pixel as the pixel at (0,0) */
  664. if (SDL_GetSurfacePalette(face)) {
  665. ret = SDL_SetSurfaceColorKey(face, true, *(Uint8 *)face->pixels);
  666. SDLTest_AssertPass("Call to SDL_SetSurfaceColorKey()");
  667. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetSurfaceColorKey, expected: true, got: %i", ret);
  668. }
  669. for (i = 0; i < SDL_arraysize(pixel_formats); ++i) {
  670. for (j = 0; j < SDL_arraysize(pixel_formats); ++j) {
  671. fmt1 = SDL_GetPixelFormatDetails(pixel_formats[i]);
  672. SDLTest_AssertCheck(fmt1 != NULL, "SDL_GetPixelFormatDetails(%s[0x%08" SDL_PRIx32 "]) should return a non-null pixel format",
  673. SDL_GetPixelFormatName(pixel_formats[i]), pixel_formats[i]);
  674. cvt1 = SDL_ConvertSurface(face, fmt1->format);
  675. SDLTest_AssertCheck(cvt1 != NULL, "SDL_ConvertSurface(..., %s[0x%08" SDL_PRIx32 "]) should return a non-null surface",
  676. SDL_GetPixelFormatName(pixel_formats[i]), pixel_formats[i]);
  677. fmt2 = SDL_GetPixelFormatDetails(pixel_formats[j]);
  678. SDLTest_AssertCheck(fmt2 != NULL, "SDL_GetPixelFormatDetails(%s[0x%08" SDL_PRIx32 "]) should return a non-null pixel format",
  679. SDL_GetPixelFormatName(pixel_formats[i]), pixel_formats[i]);
  680. cvt2 = SDL_ConvertSurface(cvt1, fmt2->format);
  681. SDLTest_AssertCheck(cvt2 != NULL, "SDL_ConvertSurface(..., %s[0x%08" SDL_PRIx32 "]) should return a non-null surface",
  682. SDL_GetPixelFormatName(pixel_formats[i]), pixel_formats[i]);
  683. if (fmt1 && fmt2 &&
  684. fmt1->bytes_per_pixel == SDL_BYTESPERPIXEL(face->format) &&
  685. fmt2->bytes_per_pixel == SDL_BYTESPERPIXEL(face->format) &&
  686. SDL_ISPIXELFORMAT_ALPHA(fmt1->format) == SDL_ISPIXELFORMAT_ALPHA(face->format) &&
  687. SDL_ISPIXELFORMAT_ALPHA(fmt2->format) == SDL_ISPIXELFORMAT_ALPHA(face->format)) {
  688. final = SDL_ConvertSurface(cvt2, face->format);
  689. SDL_assert(final != NULL);
  690. /* Compare surface. */
  691. ret = SDLTest_CompareSurfaces(face, final, 0);
  692. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  693. SDL_DestroySurface(final);
  694. }
  695. SDL_DestroySurface(cvt1);
  696. SDL_DestroySurface(cvt2);
  697. }
  698. }
  699. /* Clean up. */
  700. SDL_DestroySurface(face);
  701. return TEST_COMPLETED;
  702. }
  703. /**
  704. * Tests sprite loading. A failure case.
  705. */
  706. static int SDLCALL surface_testLoadFailure(void *arg)
  707. {
  708. SDL_Surface *face = SDL_LoadBMP("nonexistant.bmp");
  709. SDLTest_AssertCheck(face == NULL, "SDL_CreateLoadBmp");
  710. return TEST_COMPLETED;
  711. }
  712. /**
  713. * Tests blitting from a zero sized source rectangle
  714. */
  715. static int SDLCALL surface_testBlitZeroSource(void *arg)
  716. {
  717. SDL_Surface *src = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA8888);
  718. SDL_Surface *dst = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA8888);
  719. SDL_Rect srcrect = { 0, 0, 0, 0 };
  720. int ret;
  721. SDLTest_AssertPass("Call to SDL_BlitSurfaceScaled() with zero sized source rectangle");
  722. SDL_FillSurfaceRect(src, NULL, SDL_MapSurfaceRGB(src, 255, 255, 255));
  723. SDL_BlitSurfaceScaled(src, &srcrect, dst, NULL, SDL_SCALEMODE_NEAREST);
  724. ret = SDLTest_CompareSurfaces(dst, src, 0);
  725. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  726. SDL_DestroySurface(src);
  727. SDL_DestroySurface(dst);
  728. return TEST_COMPLETED;
  729. }
  730. /**
  731. * Tests some blitting routines.
  732. */
  733. static int SDLCALL surface_testBlit(void *arg)
  734. {
  735. /* Basic blitting */
  736. testBlitBlendMode(SDL_BLENDMODE_NONE);
  737. return TEST_COMPLETED;
  738. }
  739. /**
  740. * Tests some blitting routines with color mod
  741. */
  742. static int SDLCALL surface_testBlitColorMod(void *arg)
  743. {
  744. /* Basic blitting with color mod */
  745. testBlitBlendMode(-1);
  746. return TEST_COMPLETED;
  747. }
  748. /**
  749. * Tests some blitting routines with alpha mod
  750. */
  751. static int SDLCALL surface_testBlitAlphaMod(void *arg)
  752. {
  753. /* Basic blitting with alpha mod */
  754. testBlitBlendMode(-2);
  755. return TEST_COMPLETED;
  756. }
  757. /**
  758. * Tests some more blitting routines.
  759. */
  760. static int SDLCALL surface_testBlitBlendBlend(void *arg)
  761. {
  762. /* Blend blitting */
  763. testBlitBlendMode(SDL_BLENDMODE_BLEND);
  764. return TEST_COMPLETED;
  765. }
  766. /**
  767. * @brief Tests some more blitting routines.
  768. */
  769. static int SDLCALL surface_testBlitBlendPremultiplied(void *arg)
  770. {
  771. /* Blend premultiplied blitting */
  772. testBlitBlendMode(SDL_BLENDMODE_BLEND_PREMULTIPLIED);
  773. return TEST_COMPLETED;
  774. }
  775. /**
  776. * Tests some more blitting routines.
  777. */
  778. static int SDLCALL surface_testBlitBlendAdd(void *arg)
  779. {
  780. /* Add blitting */
  781. testBlitBlendMode(SDL_BLENDMODE_ADD);
  782. return TEST_COMPLETED;
  783. }
  784. /**
  785. * Tests some more blitting routines.
  786. */
  787. static int SDLCALL surface_testBlitBlendAddPremultiplied(void *arg)
  788. {
  789. /* Add premultiplied blitting */
  790. testBlitBlendMode(SDL_BLENDMODE_ADD_PREMULTIPLIED);
  791. return TEST_COMPLETED;
  792. }
  793. /**
  794. * Tests some more blitting routines.
  795. */
  796. static int SDLCALL surface_testBlitBlendMod(void *arg)
  797. {
  798. /* Mod blitting */
  799. testBlitBlendMode(SDL_BLENDMODE_MOD);
  800. return TEST_COMPLETED;
  801. }
  802. /**
  803. * Tests some more blitting routines.
  804. */
  805. static int SDLCALL surface_testBlitBlendMul(void *arg)
  806. {
  807. /* Mod blitting */
  808. testBlitBlendMode(SDL_BLENDMODE_MUL);
  809. return TEST_COMPLETED;
  810. }
  811. static int SDLCALL surface_testOverflow(void *arg)
  812. {
  813. char buf[1024];
  814. const char *expectedError;
  815. SDL_Surface *surface;
  816. SDL_memset(buf, '\0', sizeof(buf));
  817. expectedError = "Parameter 'width' is invalid";
  818. surface = SDL_CreateSurface(-3, 100, SDL_PIXELFORMAT_INDEX8);
  819. SDLTest_AssertCheck(surface == NULL, "Should detect negative width");
  820. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  821. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  822. surface = SDL_CreateSurfaceFrom(-1, 1, SDL_PIXELFORMAT_INDEX8, buf, 4);
  823. SDLTest_AssertCheck(surface == NULL, "Should detect negative width");
  824. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  825. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  826. surface = SDL_CreateSurfaceFrom(-1, 1, SDL_PIXELFORMAT_RGBA8888, buf, 4);
  827. SDLTest_AssertCheck(surface == NULL, "Should detect negative width");
  828. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  829. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  830. expectedError = "Parameter 'height' is invalid";
  831. surface = SDL_CreateSurface(100, -3, SDL_PIXELFORMAT_INDEX8);
  832. SDLTest_AssertCheck(surface == NULL, "Should detect negative height");
  833. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  834. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  835. surface = SDL_CreateSurfaceFrom(1, -1, SDL_PIXELFORMAT_INDEX8, buf, 4);
  836. SDLTest_AssertCheck(surface == NULL, "Should detect negative height");
  837. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  838. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  839. surface = SDL_CreateSurfaceFrom(1, -1, SDL_PIXELFORMAT_RGBA8888, buf, 4);
  840. SDLTest_AssertCheck(surface == NULL, "Should detect negative height");
  841. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  842. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  843. expectedError = "Parameter 'pitch' is invalid";
  844. surface = SDL_CreateSurfaceFrom(4, 1, SDL_PIXELFORMAT_INDEX8, buf, -1);
  845. SDLTest_AssertCheck(surface == NULL, "Should detect negative pitch");
  846. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  847. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  848. surface = SDL_CreateSurfaceFrom(1, 1, SDL_PIXELFORMAT_RGBA8888, buf, -1);
  849. SDLTest_AssertCheck(surface == NULL, "Should detect negative pitch");
  850. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  851. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  852. surface = SDL_CreateSurfaceFrom(1, 1, SDL_PIXELFORMAT_RGBA8888, buf, 0);
  853. SDLTest_AssertCheck(surface == NULL, "Should detect zero pitch");
  854. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  855. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  856. surface = SDL_CreateSurfaceFrom(1, 1, SDL_PIXELFORMAT_RGBA8888, NULL, 0);
  857. SDLTest_AssertCheck(surface != NULL, "Allow zero pitch for partially set up surfaces: %s",
  858. surface != NULL ? "(success)" : SDL_GetError());
  859. SDL_DestroySurface(surface);
  860. /* Less than 1 byte per pixel: the pitch can legitimately be less than
  861. * the width, but it must be enough to hold the appropriate number of
  862. * bits per pixel. SDL_PIXELFORMAT_INDEX4* needs 1 byte per 2 pixels. */
  863. surface = SDL_CreateSurfaceFrom(6, 1, SDL_PIXELFORMAT_INDEX4LSB, buf, 3);
  864. SDLTest_AssertCheck(surface != NULL, "6px * 4 bits per px fits in 3 bytes: %s",
  865. surface != NULL ? "(success)" : SDL_GetError());
  866. SDL_DestroySurface(surface);
  867. surface = SDL_CreateSurfaceFrom(6, 1, SDL_PIXELFORMAT_INDEX4MSB, buf, 3);
  868. SDLTest_AssertCheck(surface != NULL, "6px * 4 bits per px fits in 3 bytes: %s",
  869. surface != NULL ? "(success)" : SDL_GetError());
  870. SDL_DestroySurface(surface);
  871. surface = SDL_CreateSurfaceFrom(7, 1, SDL_PIXELFORMAT_INDEX4LSB, buf, 3);
  872. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  873. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  874. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  875. surface = SDL_CreateSurfaceFrom(7, 1, SDL_PIXELFORMAT_INDEX4MSB, buf, 3);
  876. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  877. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  878. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  879. surface = SDL_CreateSurfaceFrom(7, 1, SDL_PIXELFORMAT_INDEX4LSB, buf, 4);
  880. SDLTest_AssertCheck(surface != NULL, "7px * 4 bits per px fits in 4 bytes: %s",
  881. surface != NULL ? "(success)" : SDL_GetError());
  882. SDL_DestroySurface(surface);
  883. surface = SDL_CreateSurfaceFrom(7, 1, SDL_PIXELFORMAT_INDEX4MSB, buf, 4);
  884. SDLTest_AssertCheck(surface != NULL, "7px * 4 bits per px fits in 4 bytes: %s",
  885. surface != NULL ? "(success)" : SDL_GetError());
  886. SDL_DestroySurface(surface);
  887. /* SDL_PIXELFORMAT_INDEX2* needs 1 byte per 4 pixels. */
  888. surface = SDL_CreateSurfaceFrom(12, 1, SDL_PIXELFORMAT_INDEX2LSB, buf, 3);
  889. SDLTest_AssertCheck(surface != NULL, "12px * 2 bits per px fits in 3 bytes: %s",
  890. surface != NULL ? "(success)" : SDL_GetError());
  891. SDL_DestroySurface(surface);
  892. surface = SDL_CreateSurfaceFrom(12, 1, SDL_PIXELFORMAT_INDEX2MSB, buf, 3);
  893. SDLTest_AssertCheck(surface != NULL, "12px * 2 bits per px fits in 3 bytes: %s",
  894. surface != NULL ? "(success)" : SDL_GetError());
  895. SDL_DestroySurface(surface);
  896. surface = SDL_CreateSurfaceFrom(13, 1, SDL_PIXELFORMAT_INDEX2LSB, buf, 3);
  897. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp (%d)", surface ? surface->pitch : 0);
  898. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  899. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  900. surface = SDL_CreateSurfaceFrom(13, 1, SDL_PIXELFORMAT_INDEX2MSB, buf, 3);
  901. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  902. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  903. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  904. surface = SDL_CreateSurfaceFrom(13, 1, SDL_PIXELFORMAT_INDEX2LSB, buf, 4);
  905. SDLTest_AssertCheck(surface != NULL, "13px * 2 bits per px fits in 4 bytes: %s",
  906. surface != NULL ? "(success)" : SDL_GetError());
  907. SDL_DestroySurface(surface);
  908. surface = SDL_CreateSurfaceFrom(13, 1, SDL_PIXELFORMAT_INDEX2MSB, buf, 4);
  909. SDLTest_AssertCheck(surface != NULL, "13px * 2 bits per px fits in 4 bytes: %s",
  910. surface != NULL ? "(success)" : SDL_GetError());
  911. SDL_DestroySurface(surface);
  912. /* SDL_PIXELFORMAT_INDEX1* needs 1 byte per 8 pixels. */
  913. surface = SDL_CreateSurfaceFrom(16, 1, SDL_PIXELFORMAT_INDEX1LSB, buf, 2);
  914. SDLTest_AssertCheck(surface != NULL, "16px * 1 bit per px fits in 2 bytes: %s",
  915. surface != NULL ? "(success)" : SDL_GetError());
  916. SDL_DestroySurface(surface);
  917. surface = SDL_CreateSurfaceFrom(16, 1, SDL_PIXELFORMAT_INDEX1MSB, buf, 2);
  918. SDLTest_AssertCheck(surface != NULL, "16px * 1 bit per px fits in 2 bytes: %s",
  919. surface != NULL ? "(success)" : SDL_GetError());
  920. SDL_DestroySurface(surface);
  921. surface = SDL_CreateSurfaceFrom(17, 1, SDL_PIXELFORMAT_INDEX1LSB, buf, 2);
  922. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  923. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  924. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  925. surface = SDL_CreateSurfaceFrom(17, 1, SDL_PIXELFORMAT_INDEX1MSB, buf, 2);
  926. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  927. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  928. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  929. surface = SDL_CreateSurfaceFrom(17, 1, SDL_PIXELFORMAT_INDEX1LSB, buf, 3);
  930. SDLTest_AssertCheck(surface != NULL, "17px * 1 bit per px fits in 3 bytes: %s",
  931. surface != NULL ? "(success)" : SDL_GetError());
  932. SDL_DestroySurface(surface);
  933. surface = SDL_CreateSurfaceFrom(17, 1, SDL_PIXELFORMAT_INDEX1MSB, buf, 3);
  934. SDLTest_AssertCheck(surface != NULL, "17px * 1 bit per px fits in 3 bytes: %s",
  935. surface != NULL ? "(success)" : SDL_GetError());
  936. SDL_DestroySurface(surface);
  937. /* SDL_PIXELFORMAT_INDEX8 and SDL_PIXELFORMAT_RGB332 require 1 byte per pixel. */
  938. surface = SDL_CreateSurfaceFrom(5, 1, SDL_PIXELFORMAT_RGB332, buf, 5);
  939. SDLTest_AssertCheck(surface != NULL, "5px * 8 bits per px fits in 5 bytes: %s",
  940. surface != NULL ? "(success)" : SDL_GetError());
  941. SDL_DestroySurface(surface);
  942. surface = SDL_CreateSurfaceFrom(5, 1, SDL_PIXELFORMAT_INDEX8, buf, 5);
  943. SDLTest_AssertCheck(surface != NULL, "5px * 8 bits per px fits in 5 bytes: %s",
  944. surface != NULL ? "(success)" : SDL_GetError());
  945. SDL_DestroySurface(surface);
  946. surface = SDL_CreateSurfaceFrom(6, 1, SDL_PIXELFORMAT_RGB332, buf, 5);
  947. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  948. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  949. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  950. surface = SDL_CreateSurfaceFrom(6, 1, SDL_PIXELFORMAT_INDEX8, buf, 5);
  951. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  952. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  953. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  954. /* Everything else requires more than 1 byte per pixel, and rounds up
  955. * each pixel to an integer number of bytes (e.g. RGB555 is really
  956. * XRGB1555, with 1 bit per pixel wasted). */
  957. surface = SDL_CreateSurfaceFrom(3, 1, SDL_PIXELFORMAT_XRGB1555, buf, 6);
  958. SDLTest_AssertCheck(surface != NULL, "3px * 15 (really 16) bits per px fits in 6 bytes: %s",
  959. surface != NULL ? "(success)" : SDL_GetError());
  960. SDL_DestroySurface(surface);
  961. surface = SDL_CreateSurfaceFrom(3, 1, SDL_PIXELFORMAT_XRGB1555, buf, 6);
  962. SDLTest_AssertCheck(surface != NULL, "5px * 15 (really 16) bits per px fits in 6 bytes: %s",
  963. surface != NULL ? "(success)" : SDL_GetError());
  964. SDL_DestroySurface(surface);
  965. surface = SDL_CreateSurfaceFrom(4, 1, SDL_PIXELFORMAT_XRGB1555, buf, 6);
  966. SDLTest_AssertCheck(surface == NULL, "4px * 15 (really 16) bits per px doesn't fit in 6 bytes");
  967. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  968. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  969. surface = SDL_CreateSurfaceFrom(4, 1, SDL_PIXELFORMAT_XRGB1555, buf, 6);
  970. SDLTest_AssertCheck(surface == NULL, "4px * 15 (really 16) bits per px doesn't fit in 6 bytes");
  971. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  972. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  973. if (sizeof(size_t) == 4 && sizeof(int) >= 4) {
  974. SDL_ClearError();
  975. expectedError = "aligning pitch would overflow";
  976. /* 0x5555'5555 * 3bpp = 0xffff'ffff which fits in size_t, but adding
  977. * alignment padding makes it overflow */
  978. surface = SDL_CreateSurface(0x55555555, 1, SDL_PIXELFORMAT_RGB24);
  979. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in pitch + alignment");
  980. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  981. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  982. SDL_ClearError();
  983. expectedError = "width * bpp would overflow";
  984. /* 0x4000'0000 * 4bpp = 0x1'0000'0000 which (just) overflows */
  985. surface = SDL_CreateSurface(0x40000000, 1, SDL_PIXELFORMAT_ARGB8888);
  986. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in width * bytes per pixel");
  987. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  988. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  989. SDL_ClearError();
  990. expectedError = "height * pitch would overflow";
  991. surface = SDL_CreateSurface((1 << 29) - 1, (1 << 29) - 1, SDL_PIXELFORMAT_INDEX8);
  992. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in width * height");
  993. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  994. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  995. SDL_ClearError();
  996. expectedError = "height * pitch would overflow";
  997. surface = SDL_CreateSurface((1 << 15) + 1, (1 << 15) + 1, SDL_PIXELFORMAT_ARGB8888);
  998. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in width * height * bytes per pixel");
  999. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1000. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1001. } else {
  1002. SDLTest_Log("Can't easily overflow size_t on this platform");
  1003. }
  1004. return TEST_COMPLETED;
  1005. }
  1006. static int SDLCALL surface_testFlip(void *arg)
  1007. {
  1008. SDL_Surface *surface;
  1009. Uint8 *pixels;
  1010. int offset;
  1011. const char *expectedError;
  1012. surface = SDL_CreateSurface(3, 3, SDL_PIXELFORMAT_RGB24);
  1013. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  1014. SDL_ClearError();
  1015. expectedError = "Parameter 'surface' is invalid";
  1016. SDL_FlipSurface(NULL, SDL_FLIP_HORIZONTAL);
  1017. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1018. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1019. SDL_ClearError();
  1020. expectedError = "Parameter 'flip' is invalid";
  1021. SDL_FlipSurface(surface, SDL_FLIP_NONE);
  1022. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1023. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1024. pixels = (Uint8 *)surface->pixels;
  1025. *pixels = 0xFF;
  1026. offset = 0;
  1027. SDLTest_AssertPass("Call to SDL_FlipSurface(surface, SDL_FLIP_VERTICAL)");
  1028. CHECK_FUNC(SDL_FlipSurface, (surface, SDL_FLIP_VERTICAL));
  1029. SDLTest_AssertCheck(pixels[offset] == 0x00,
  1030. "Expected pixels[%d] == 0x00 got 0x%.2X", offset, pixels[offset]);
  1031. offset = 2 * surface->pitch;
  1032. SDLTest_AssertCheck(pixels[offset] == 0xFF,
  1033. "Expected pixels[%d] == 0xFF got 0x%.2X", offset, pixels[offset]);
  1034. SDLTest_AssertPass("Call to SDL_FlipSurface(surface, SDL_FLIP_HORIZONTAL)");
  1035. CHECK_FUNC(SDL_FlipSurface, (surface, SDL_FLIP_HORIZONTAL));
  1036. SDLTest_AssertCheck(pixels[offset] == 0x00,
  1037. "Expected pixels[%d] == 0x00 got 0x%.2X", offset, pixels[offset]);
  1038. offset += (surface->w - 1) * SDL_BYTESPERPIXEL(surface->format);
  1039. SDLTest_AssertCheck(pixels[offset] == 0xFF,
  1040. "Expected pixels[%d] == 0xFF got 0x%.2X", offset, pixels[offset]);
  1041. SDL_DestroySurface(surface);
  1042. return TEST_COMPLETED;
  1043. }
  1044. static int SDLCALL surface_testPalette(void *arg)
  1045. {
  1046. SDL_Surface *source, *surface, *output;
  1047. SDL_Palette *palette;
  1048. Uint8 *pixels;
  1049. palette = SDL_CreatePalette(2);
  1050. SDLTest_AssertCheck(palette != NULL, "SDL_CreatePalette()");
  1051. source = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_INDEX8);
  1052. SDLTest_AssertCheck(source != NULL, "SDL_CreateSurface()");
  1053. SDLTest_AssertCheck(SDL_GetSurfacePalette(source) == NULL, "SDL_GetSurfacePalette(source)");
  1054. surface = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_INDEX8);
  1055. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  1056. SDLTest_AssertCheck(SDL_GetSurfacePalette(surface) == NULL, "SDL_GetSurfacePalette(surface)");
  1057. pixels = (Uint8 *)surface->pixels;
  1058. SDLTest_AssertCheck(*pixels == 0, "Expected *pixels == 0 got %u", *pixels);
  1059. /* Identity copy between indexed surfaces without a palette */
  1060. *(Uint8 *)source->pixels = 1;
  1061. SDL_BlitSurface(source, NULL, surface, NULL);
  1062. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  1063. /* Identity copy between indexed surfaces where the source has a palette */
  1064. palette->colors[0].r = 0;
  1065. palette->colors[0].g = 0;
  1066. palette->colors[0].b = 0;
  1067. palette->colors[1].r = 0xFF;
  1068. palette->colors[1].g = 0;
  1069. palette->colors[1].b = 0;
  1070. SDL_SetSurfacePalette(source, palette);
  1071. *pixels = 0;
  1072. SDL_BlitSurface(source, NULL, surface, NULL);
  1073. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  1074. /* Identity copy between indexed surfaces where the destination has a palette */
  1075. palette->colors[0].r = 0;
  1076. palette->colors[0].g = 0;
  1077. palette->colors[0].b = 0;
  1078. palette->colors[1].r = 0xFF;
  1079. palette->colors[1].g = 0;
  1080. palette->colors[1].b = 0;
  1081. SDL_SetSurfacePalette(source, NULL);
  1082. SDL_SetSurfacePalette(surface, palette);
  1083. *pixels = 0;
  1084. SDL_BlitSurface(source, NULL, surface, NULL);
  1085. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  1086. /* Identity copy between indexed surfaces where the source and destination share a palette */
  1087. palette->colors[0].r = 0;
  1088. palette->colors[0].g = 0;
  1089. palette->colors[0].b = 0;
  1090. palette->colors[1].r = 0xFF;
  1091. palette->colors[1].g = 0;
  1092. palette->colors[1].b = 0;
  1093. SDL_SetSurfacePalette(source, palette);
  1094. SDL_SetSurfacePalette(surface, palette);
  1095. *pixels = 0;
  1096. SDL_BlitSurface(source, NULL, surface, NULL);
  1097. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  1098. output = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA32);
  1099. SDLTest_AssertCheck(output != NULL, "SDL_CreateSurface()");
  1100. pixels = (Uint8 *)output->pixels;
  1101. SDL_BlitSurface(surface, NULL, output, NULL);
  1102. SDLTest_AssertCheck(*pixels == 0xFF, "Expected *pixels == 0xFF got 0x%.2X", *pixels);
  1103. /* Set the palette color and blit again */
  1104. palette->colors[1].r = 0xAA;
  1105. SDL_SetSurfacePalette(surface, palette);
  1106. SDL_BlitSurface(surface, NULL, output, NULL);
  1107. SDLTest_AssertCheck(*pixels == 0xAA, "Expected *pixels == 0xAA got 0x%.2X", *pixels);
  1108. SDL_DestroyPalette(palette);
  1109. SDL_DestroySurface(source);
  1110. SDL_DestroySurface(surface);
  1111. SDL_DestroySurface(output);
  1112. return TEST_COMPLETED;
  1113. }
  1114. static int SDLCALL surface_testPalettization(void *arg)
  1115. {
  1116. const SDL_Color palette_colors[] = {
  1117. { 0x80, 0x00, 0x00, 0xff },
  1118. { 0x00, 0x80, 0x00, 0xff },
  1119. { 0x00, 0x00, 0x80, 0xff },
  1120. { 0x40, 0x00, 0x00, 0xff },
  1121. { 0x00, 0x40, 0x00, 0xff },
  1122. { 0x00, 0x00, 0x40, 0xff },
  1123. { 0x00, 0x00, 0x00, 0xff },
  1124. { 0xff, 0x00, 0x00, 0xff },
  1125. { 0x00, 0xff, 0x00, 0xff },
  1126. { 0x00, 0x00, 0xff, 0xff },
  1127. { 0xff, 0xff, 0x00, 0xff },
  1128. { 0x00, 0xff, 0xff, 0xff },
  1129. { 0xff, 0x00, 0xff, 0xff },
  1130. };
  1131. const struct {
  1132. SDL_Color c;
  1133. Uint8 e;
  1134. } colors[] = {
  1135. { { 0xff, 0x00, 0x00, 0xff }, 7 },
  1136. { { 0xfe, 0x00, 0x00, 0xff }, 7 },
  1137. { { 0xfd, 0x00, 0x00, 0xff }, 7 },
  1138. { { 0xf0, 0x00, 0x00, 0xff }, 7 },
  1139. { { 0xd0, 0x00, 0x00, 0xff }, 7 },
  1140. { { 0xb0, 0x00, 0x00, 0xff }, 0 },
  1141. { { 0xa0, 0x00, 0x00, 0xff }, 0 },
  1142. { { 0xff, 0x00, 0x00, 0x00 }, 7 },
  1143. { { 0x00, 0x10, 0x21, 0xff }, 5 },
  1144. { { 0x00, 0x10, 0x19, 0xff }, 6 },
  1145. { { 0x81, 0x00, 0x41, 0xff }, 0 },
  1146. { { 0x80, 0xf0, 0xf0, 0x7f }, 11 },
  1147. { { 0x00, 0x00, 0x00, 0xff }, 6 },
  1148. { { 0x00, 0x00, 0x00, 0x01 }, 6 },
  1149. };
  1150. int i;
  1151. int result;
  1152. SDL_Surface *source, *output;
  1153. SDL_Palette *palette;
  1154. Uint8 *pixels;
  1155. palette = SDL_CreatePalette(SDL_arraysize(palette_colors));
  1156. SDLTest_AssertCheck(palette != NULL, "SDL_CreatePalette()");
  1157. result = SDL_SetPaletteColors(palette, palette_colors, 0, SDL_arraysize(palette_colors));
  1158. SDLTest_AssertCheck(result, "SDL_SetPaletteColors()");
  1159. source = SDL_CreateSurface(SDL_arraysize(palette_colors) + SDL_arraysize(colors), 1, SDL_PIXELFORMAT_RGBA8888);
  1160. SDLTest_AssertCheck(source != NULL, "SDL_CreateSurface()");
  1161. SDLTest_AssertCheck(source->w == SDL_arraysize(palette_colors) + SDL_arraysize(colors), "Expected source->w == %d, got %d", (int)(SDL_arraysize(palette_colors) + SDL_arraysize(colors)), source->w);
  1162. SDLTest_AssertCheck(source->h == 1, "Expected source->h == %d, got %d", 1, source->h);
  1163. SDLTest_AssertCheck(source->format == SDL_PIXELFORMAT_RGBA8888, "Expected source->format == SDL_PIXELFORMAT_RGBA8888, got 0x%x (%s)", source->format, SDL_GetPixelFormatName(source->format));
  1164. for (i = 0; i < SDL_arraysize(colors); i++) {
  1165. result = SDL_WriteSurfacePixel(source, i, 0, colors[i].c.r, colors[i].c.g, colors[i].c.b, colors[i].c.a);
  1166. SDLTest_AssertCheck(result == true, "SDL_WriteSurfacePixel");
  1167. }
  1168. for (i = 0; i < SDL_arraysize(palette_colors); i++) {
  1169. result = SDL_WriteSurfacePixel(source, SDL_arraysize(colors) + i, 0, palette_colors[i].r, palette_colors[i].g, palette_colors[i].b, palette_colors[i].a);
  1170. SDLTest_AssertCheck(result == true, "SDL_WriteSurfacePixel");
  1171. }
  1172. output = SDL_ConvertSurfaceAndColorspace(source, SDL_PIXELFORMAT_INDEX8, palette, SDL_COLORSPACE_UNKNOWN, 0);
  1173. SDLTest_AssertCheck(output != NULL, "SDL_ConvertSurfaceAndColorspace()");
  1174. SDLTest_AssertCheck(output->w == source->w, "Expected output->w == %d, got %d", source->w, output->w);
  1175. SDLTest_AssertCheck(output->h == source->h, "Expected output->h == %d, got %d", source->h, output->h);
  1176. SDLTest_AssertCheck(output->format == SDL_PIXELFORMAT_INDEX8, "Expected output->format == SDL_PIXELFORMAT_INDEX8, got 0x%x (%s)", output->format, SDL_GetPixelFormatName(output->format));
  1177. pixels = output->pixels;
  1178. for (i = 0; i < SDL_arraysize(colors); i++) {
  1179. int idx = i;
  1180. Uint8 actual = pixels[idx];
  1181. Uint8 expected = colors[i].e;
  1182. SDLTest_AssertCheck(actual < SDL_arraysize(palette_colors), "output->pixels[%d] < %d", idx, (int)SDL_arraysize(palette_colors));
  1183. SDLTest_AssertCheck(actual == expected, "Expected output->pixels[%d] == %u, got %u", idx, expected, actual);
  1184. }
  1185. SDLTest_AssertPass("Check palette 1:1 mapping");
  1186. for (i = 0; i < SDL_arraysize(palette_colors); i++) {
  1187. int idx = SDL_arraysize(colors) + i;
  1188. Uint8 actual = pixels[idx];
  1189. Uint8 expected = i;
  1190. SDLTest_AssertCheck(actual < SDL_arraysize(palette_colors), "output->pixels[%d] < %d", idx, (int)SDL_arraysize(palette_colors));
  1191. SDLTest_AssertCheck(actual == expected, "Expected output->pixels[%d] == %u, got %u", idx, expected, actual);
  1192. }
  1193. SDL_DestroyPalette(palette);
  1194. SDL_DestroySurface(source);
  1195. SDL_DestroySurface(output);
  1196. return TEST_COMPLETED;
  1197. }
  1198. static int SDLCALL surface_testClearSurface(void *arg)
  1199. {
  1200. SDL_PixelFormat formats[] = {
  1201. SDL_PIXELFORMAT_ARGB8888, SDL_PIXELFORMAT_RGBA8888,
  1202. SDL_PIXELFORMAT_ARGB2101010, SDL_PIXELFORMAT_ABGR2101010,
  1203. SDL_PIXELFORMAT_ARGB64, SDL_PIXELFORMAT_RGBA64,
  1204. SDL_PIXELFORMAT_ARGB128_FLOAT, SDL_PIXELFORMAT_RGBA128_FLOAT,
  1205. SDL_PIXELFORMAT_YV12, SDL_PIXELFORMAT_UYVY, SDL_PIXELFORMAT_NV12
  1206. };
  1207. SDL_Surface *surface;
  1208. SDL_PixelFormat format;
  1209. const float MAXIMUM_ERROR_RGB = 0.0001f;
  1210. const float MAXIMUM_ERROR_YUV = 0.01f;
  1211. float srcR = 10 / 255.0f, srcG = 128 / 255.0f, srcB = 240 / 255.0f, srcA = 1.0f;
  1212. float actualR, actualG, actualB, actualA;
  1213. float deltaR, deltaG, deltaB, deltaA;
  1214. int i, ret;
  1215. for (i = 0; i < SDL_arraysize(formats); ++i) {
  1216. const float MAXIMUM_ERROR = SDL_ISPIXELFORMAT_FOURCC(formats[i]) ? MAXIMUM_ERROR_YUV : MAXIMUM_ERROR_RGB;
  1217. format = formats[i];
  1218. surface = SDL_CreateSurface(1, 1, format);
  1219. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  1220. ret = SDL_ClearSurface(surface, srcR, srcG, srcB, srcA);
  1221. SDLTest_AssertCheck(ret == true, "SDL_ClearSurface()");
  1222. ret = SDL_ReadSurfacePixelFloat(surface, 0, 0, &actualR, &actualG, &actualB, &actualA);
  1223. SDLTest_AssertCheck(ret == true, "SDL_ReadSurfacePixelFloat()");
  1224. deltaR = SDL_fabsf(actualR - srcR);
  1225. deltaG = SDL_fabsf(actualG - srcG);
  1226. deltaB = SDL_fabsf(actualB - srcB);
  1227. deltaA = SDL_fabsf(actualA - srcA);
  1228. SDLTest_AssertCheck(
  1229. deltaR <= MAXIMUM_ERROR &&
  1230. deltaG <= MAXIMUM_ERROR &&
  1231. deltaB <= MAXIMUM_ERROR &&
  1232. deltaA <= MAXIMUM_ERROR,
  1233. "Checking %s surface clear results, expected %.4f,%.4f,%.4f,%.4f, got %.4f,%.4f,%.4f,%.4f",
  1234. SDL_GetPixelFormatName(format),
  1235. srcR, srcG, srcB, srcA, actualR, actualG, actualB, actualA);
  1236. SDL_DestroySurface(surface);
  1237. }
  1238. return TEST_COMPLETED;
  1239. }
  1240. static int SDLCALL surface_testPremultiplyAlpha(void *arg)
  1241. {
  1242. SDL_PixelFormat formats[] = {
  1243. SDL_PIXELFORMAT_ARGB8888, SDL_PIXELFORMAT_RGBA8888,
  1244. SDL_PIXELFORMAT_ARGB2101010, SDL_PIXELFORMAT_ABGR2101010,
  1245. SDL_PIXELFORMAT_ARGB64, SDL_PIXELFORMAT_RGBA64,
  1246. SDL_PIXELFORMAT_ARGB128_FLOAT, SDL_PIXELFORMAT_RGBA128_FLOAT,
  1247. };
  1248. SDL_Surface *surface;
  1249. SDL_PixelFormat format;
  1250. const float MAXIMUM_ERROR_LOW_PRECISION = 1 / 255.0f;
  1251. const float MAXIMUM_ERROR_HIGH_PRECISION = 0.0001f;
  1252. float srcR = 10 / 255.0f, srcG = 128 / 255.0f, srcB = 240 / 255.0f, srcA = 170 / 255.0f;
  1253. float expectedR = srcR * srcA;
  1254. float expectedG = srcG * srcA;
  1255. float expectedB = srcB * srcA;
  1256. float actualR, actualG, actualB;
  1257. float deltaR, deltaG, deltaB;
  1258. int i, ret;
  1259. for (i = 0; i < SDL_arraysize(formats); ++i) {
  1260. const float MAXIMUM_ERROR = (SDL_BITSPERPIXEL(formats[i]) > 32) ? MAXIMUM_ERROR_HIGH_PRECISION : MAXIMUM_ERROR_LOW_PRECISION;
  1261. format = formats[i];
  1262. surface = SDL_CreateSurface(1, 1, format);
  1263. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  1264. ret = SDL_SetSurfaceColorspace(surface, SDL_COLORSPACE_SRGB);
  1265. SDLTest_AssertCheck(ret == true, "SDL_SetSurfaceColorspace()");
  1266. ret = SDL_ClearSurface(surface, srcR, srcG, srcB, srcA);
  1267. SDLTest_AssertCheck(ret == true, "SDL_ClearSurface()");
  1268. ret = SDL_PremultiplySurfaceAlpha(surface, false);
  1269. SDLTest_AssertCheck(ret == true, "SDL_PremultiplySurfaceAlpha()");
  1270. ret = SDL_ReadSurfacePixelFloat(surface, 0, 0, &actualR, &actualG, &actualB, NULL);
  1271. SDLTest_AssertCheck(ret == true, "SDL_ReadSurfacePixelFloat()");
  1272. deltaR = SDL_fabsf(actualR - expectedR);
  1273. deltaG = SDL_fabsf(actualG - expectedG);
  1274. deltaB = SDL_fabsf(actualB - expectedB);
  1275. SDLTest_AssertCheck(
  1276. deltaR <= MAXIMUM_ERROR &&
  1277. deltaG <= MAXIMUM_ERROR &&
  1278. deltaB <= MAXIMUM_ERROR,
  1279. "Checking %s alpha premultiply results, expected %.4f,%.4f,%.4f, got %.4f,%.4f,%.4f",
  1280. SDL_GetPixelFormatName(format),
  1281. expectedR, expectedG, expectedB, actualR, actualG, actualB);
  1282. SDL_DestroySurface(surface);
  1283. }
  1284. return TEST_COMPLETED;
  1285. }
  1286. static int SDLCALL surface_testScale(void *arg)
  1287. {
  1288. SDL_PixelFormat formats[] = {
  1289. SDL_PIXELFORMAT_ARGB8888, SDL_PIXELFORMAT_RGBA8888,
  1290. SDL_PIXELFORMAT_ARGB2101010, SDL_PIXELFORMAT_ABGR2101010,
  1291. SDL_PIXELFORMAT_ARGB64, SDL_PIXELFORMAT_RGBA64,
  1292. SDL_PIXELFORMAT_ARGB128_FLOAT, SDL_PIXELFORMAT_RGBA128_FLOAT,
  1293. };
  1294. SDL_ScaleMode modes[] = {
  1295. SDL_SCALEMODE_NEAREST, SDL_SCALEMODE_LINEAR
  1296. };
  1297. SDL_Surface *surface, *result;
  1298. SDL_PixelFormat format;
  1299. SDL_ScaleMode mode;
  1300. const float MAXIMUM_ERROR = 0.0001f;
  1301. float srcR = 10 / 255.0f, srcG = 128 / 255.0f, srcB = 240 / 255.0f, srcA = 170 / 255.0f;
  1302. float actualR, actualG, actualB, actualA;
  1303. float deltaR, deltaG, deltaB, deltaA;
  1304. int i, j, ret;
  1305. for (i = 0; i < SDL_arraysize(formats); ++i) {
  1306. for (j = 0; j < SDL_arraysize(modes); ++j) {
  1307. format = formats[i];
  1308. mode = modes[j];
  1309. surface = SDL_CreateSurface(1, 1, format);
  1310. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  1311. ret = SDL_SetSurfaceColorspace(surface, SDL_COLORSPACE_SRGB);
  1312. SDLTest_AssertCheck(ret == true, "SDL_SetSurfaceColorspace()");
  1313. ret = SDL_ClearSurface(surface, srcR, srcG, srcB, srcA);
  1314. SDLTest_AssertCheck(ret == true, "SDL_ClearSurface()");
  1315. result = SDL_ScaleSurface(surface, 2, 2, mode);
  1316. SDLTest_AssertCheck(ret == true, "SDL_PremultiplySurfaceAlpha()");
  1317. ret = SDL_ReadSurfacePixelFloat(result, 1, 1, &actualR, &actualG, &actualB, &actualA);
  1318. SDLTest_AssertCheck(ret == true, "SDL_ReadSurfacePixelFloat()");
  1319. deltaR = SDL_fabsf(actualR - srcR);
  1320. deltaG = SDL_fabsf(actualG - srcG);
  1321. deltaB = SDL_fabsf(actualB - srcB);
  1322. deltaA = SDL_fabsf(actualA - srcA);
  1323. SDLTest_AssertCheck(
  1324. deltaR <= MAXIMUM_ERROR &&
  1325. deltaG <= MAXIMUM_ERROR &&
  1326. deltaB <= MAXIMUM_ERROR &&
  1327. deltaA <= MAXIMUM_ERROR,
  1328. "Checking %s %s scaling results, expected %.4f,%.4f,%.4f,%.4f got %.4f,%.4f,%.4f,%.4f",
  1329. SDL_GetPixelFormatName(format),
  1330. mode == SDL_SCALEMODE_NEAREST ? "nearest" : "linear",
  1331. srcR, srcG, srcB, srcA, actualR, actualG, actualB, actualA);
  1332. SDL_DestroySurface(surface);
  1333. SDL_DestroySurface(result);
  1334. }
  1335. }
  1336. return TEST_COMPLETED;
  1337. }
  1338. /* ================= Test References ================== */
  1339. /* Surface test cases */
  1340. static const SDLTest_TestCaseReference surfaceTestSaveLoadBitmap = {
  1341. surface_testSaveLoadBitmap, "surface_testSaveLoadBitmap", "Tests sprite saving and loading.", TEST_ENABLED
  1342. };
  1343. static const SDLTest_TestCaseReference surfaceTestBlitZeroSource = {
  1344. surface_testBlitZeroSource, "surface_testBlitZeroSource", "Tests blitting from a zero sized source rectangle", TEST_ENABLED
  1345. };
  1346. static const SDLTest_TestCaseReference surfaceTestBlit = {
  1347. surface_testBlit, "surface_testBlit", "Tests basic blitting.", TEST_ENABLED
  1348. };
  1349. static const SDLTest_TestCaseReference surfaceTestBlitTiled = {
  1350. surface_testBlitTiled, "surface_testBlitTiled", "Tests tiled blitting.", TEST_ENABLED
  1351. };
  1352. static const SDLTest_TestCaseReference surfaceTestBlit9Grid = {
  1353. surface_testBlit9Grid, "surface_testBlit9Grid", "Tests 9-grid blitting.", TEST_ENABLED
  1354. };
  1355. static const SDLTest_TestCaseReference surfaceTestBlitMultiple = {
  1356. surface_testBlitMultiple, "surface_testBlitMultiple", "Tests blitting between multiple surfaces of the same format.", TEST_ENABLED
  1357. };
  1358. static const SDLTest_TestCaseReference surfaceTestLoadFailure = {
  1359. surface_testLoadFailure, "surface_testLoadFailure", "Tests sprite loading. A failure case.", TEST_ENABLED
  1360. };
  1361. static const SDLTest_TestCaseReference surfaceTestSurfaceConversion = {
  1362. surface_testSurfaceConversion, "surface_testSurfaceConversion", "Tests surface conversion.", TEST_ENABLED
  1363. };
  1364. static const SDLTest_TestCaseReference surfaceTestCompleteSurfaceConversion = {
  1365. surface_testCompleteSurfaceConversion, "surface_testCompleteSurfaceConversion", "Tests surface conversion across all pixel formats", TEST_ENABLED
  1366. };
  1367. static const SDLTest_TestCaseReference surfaceTestBlitColorMod = {
  1368. surface_testBlitColorMod, "surface_testBlitColorMod", "Tests some blitting routines with color mod.", TEST_ENABLED
  1369. };
  1370. static const SDLTest_TestCaseReference surfaceTestBlitAlphaMod = {
  1371. surface_testBlitAlphaMod, "surface_testBlitAlphaMod", "Tests some blitting routines with alpha mod.", TEST_ENABLED
  1372. };
  1373. static const SDLTest_TestCaseReference surfaceTestBlitBlendBlend = {
  1374. surface_testBlitBlendBlend, "surface_testBlitBlendBlend", "Tests blitting routines with blend blending mode.", TEST_ENABLED
  1375. };
  1376. static const SDLTest_TestCaseReference surfaceTestBlitBlendPremultiplied = {
  1377. surface_testBlitBlendPremultiplied, "surface_testBlitBlendPremultiplied", "Tests blitting routines with premultiplied blending mode.", TEST_ENABLED
  1378. };
  1379. static const SDLTest_TestCaseReference surfaceTestBlitBlendAdd = {
  1380. surface_testBlitBlendAdd, "surface_testBlitBlendAdd", "Tests blitting routines with add blending mode.", TEST_ENABLED
  1381. };
  1382. static const SDLTest_TestCaseReference surfaceTestBlitBlendAddPremultiplied = {
  1383. surface_testBlitBlendAddPremultiplied, "surface_testBlitBlendAddPremultiplied", "Tests blitting routines with premultiplied add blending mode.", TEST_ENABLED
  1384. };
  1385. static const SDLTest_TestCaseReference surfaceTestBlitBlendMod = {
  1386. surface_testBlitBlendMod, "surface_testBlitBlendMod", "Tests blitting routines with mod blending mode.", TEST_ENABLED
  1387. };
  1388. static const SDLTest_TestCaseReference surfaceTestBlitBlendMul = {
  1389. surface_testBlitBlendMul, "surface_testBlitBlendMul", "Tests blitting routines with mul blending mode.", TEST_ENABLED
  1390. };
  1391. static const SDLTest_TestCaseReference surfaceTestOverflow = {
  1392. surface_testOverflow, "surface_testOverflow", "Test overflow detection.", TEST_ENABLED
  1393. };
  1394. static const SDLTest_TestCaseReference surfaceTestFlip = {
  1395. surface_testFlip, "surface_testFlip", "Test surface flipping.", TEST_ENABLED
  1396. };
  1397. static const SDLTest_TestCaseReference surfaceTestPalette = {
  1398. surface_testPalette, "surface_testPalette", "Test surface palette operations.", TEST_ENABLED
  1399. };
  1400. static const SDLTest_TestCaseReference surfaceTestPalettization = {
  1401. surface_testPalettization, "surface_testPalettization", "Test surface palettization.", TEST_ENABLED
  1402. };
  1403. static const SDLTest_TestCaseReference surfaceTestClearSurface = {
  1404. surface_testClearSurface, "surface_testClearSurface", "Test clear surface operations.", TEST_ENABLED
  1405. };
  1406. static const SDLTest_TestCaseReference surfaceTestPremultiplyAlpha = {
  1407. surface_testPremultiplyAlpha, "surface_testPremultiplyAlpha", "Test alpha premultiply operations.", TEST_ENABLED
  1408. };
  1409. static const SDLTest_TestCaseReference surfaceTestScale = {
  1410. surface_testScale, "surface_testScale", "Test scaling operations.", TEST_ENABLED
  1411. };
  1412. /* Sequence of Surface test cases */
  1413. static const SDLTest_TestCaseReference *surfaceTests[] = {
  1414. &surfaceTestSaveLoadBitmap,
  1415. &surfaceTestBlitZeroSource,
  1416. &surfaceTestBlit,
  1417. &surfaceTestBlitTiled,
  1418. &surfaceTestBlit9Grid,
  1419. &surfaceTestBlitMultiple,
  1420. &surfaceTestLoadFailure,
  1421. &surfaceTestSurfaceConversion,
  1422. &surfaceTestCompleteSurfaceConversion,
  1423. &surfaceTestBlitColorMod,
  1424. &surfaceTestBlitAlphaMod,
  1425. &surfaceTestBlitBlendBlend,
  1426. &surfaceTestBlitBlendPremultiplied,
  1427. &surfaceTestBlitBlendAdd,
  1428. &surfaceTestBlitBlendAddPremultiplied,
  1429. &surfaceTestBlitBlendMod,
  1430. &surfaceTestBlitBlendMul,
  1431. &surfaceTestOverflow,
  1432. &surfaceTestFlip,
  1433. &surfaceTestPalette,
  1434. &surfaceTestPalettization,
  1435. &surfaceTestClearSurface,
  1436. &surfaceTestPremultiplyAlpha,
  1437. &surfaceTestScale,
  1438. NULL
  1439. };
  1440. /* Surface test suite (global) */
  1441. SDLTest_TestSuiteReference surfaceTestSuite = {
  1442. "Surface",
  1443. surfaceSetUp,
  1444. surfaceTests,
  1445. surfaceTearDown
  1446. };