testautomation_surface.c 73 KB

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