testautomation_surface.c 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139
  1. /**
  2. * Original code: automated SDL surface test written by Edgar Simo "bobbens"
  3. * Adapted/rewritten for test lib by Andreas Schiffler
  4. */
  5. #include <stdio.h>
  6. #ifndef _MSC_VER
  7. #include <unistd.h>
  8. #endif
  9. #include <sys/stat.h>
  10. #include <SDL3/SDL.h>
  11. #include <SDL3/SDL_test.h>
  12. #include "testautomation_suites.h"
  13. #include "testautomation_images.h"
  14. #define CHECK_FUNC(FUNC, PARAMS) \
  15. { \
  16. bool result = FUNC PARAMS; \
  17. if (!result) { \
  18. SDLTest_AssertCheck(result, "Validate result from %s, expected: true, got: false, %s", #FUNC, SDL_GetError()); \
  19. } \
  20. }
  21. /* ================= Test Case Implementation ================== */
  22. /* Shared test surface */
  23. static SDL_Surface *referenceSurface = NULL;
  24. static SDL_Surface *testSurface = NULL;
  25. /* Fixture */
  26. /* Create a 32-bit writable surface for blitting tests */
  27. static void SDLCALL surfaceSetUp(void **arg)
  28. {
  29. int result;
  30. SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
  31. SDL_BlendMode currentBlendMode;
  32. referenceSurface = SDLTest_ImageBlit(); /* For size info */
  33. testSurface = SDL_CreateSurface(referenceSurface->w, referenceSurface->h, SDL_PIXELFORMAT_RGBA32);
  34. SDLTest_AssertCheck(testSurface != NULL, "Check that testSurface is not NULL");
  35. if (testSurface != NULL) {
  36. /* Disable blend mode for target surface */
  37. result = SDL_SetSurfaceBlendMode(testSurface, blendMode);
  38. SDLTest_AssertCheck(result == true, "Validate result from SDL_SetSurfaceBlendMode, expected: true, got: %i", result);
  39. result = SDL_GetSurfaceBlendMode(testSurface, &currentBlendMode);
  40. SDLTest_AssertCheck(result == true, "Validate result from SDL_GetSurfaceBlendMode, expected: true, got: %i", result);
  41. SDLTest_AssertCheck(currentBlendMode == blendMode, "Validate blendMode, expected: %" SDL_PRIu32 ", got: %" SDL_PRIu32, blendMode, currentBlendMode);
  42. /* Clear the target surface */
  43. result = SDL_FillSurfaceRect(testSurface, NULL, SDL_MapSurfaceRGBA(testSurface, 0, 0, 0, 255));
  44. SDLTest_AssertCheck(result == true, "Validate result from SDL_FillSurfaceRect, expected: true, got: %i", result);
  45. }
  46. }
  47. static void SDLCALL surfaceTearDown(void *arg)
  48. {
  49. SDL_DestroySurface(referenceSurface);
  50. referenceSurface = NULL;
  51. SDL_DestroySurface(testSurface);
  52. testSurface = NULL;
  53. }
  54. static void DitherPalette(SDL_Palette *palette)
  55. {
  56. int i;
  57. for (i = 0; i < palette->ncolors; i++) {
  58. int r, g, b;
  59. /* map each bit field to the full [0, 255] interval,
  60. so 0 is mapped to (0, 0, 0) and 255 to (255, 255, 255) */
  61. r = i & 0xe0;
  62. r |= r >> 3 | r >> 6;
  63. palette->colors[i].r = (Uint8)r;
  64. g = (i << 3) & 0xe0;
  65. g |= g >> 3 | g >> 6;
  66. palette->colors[i].g = (Uint8)g;
  67. b = i & 0x3;
  68. b |= b << 2;
  69. b |= b << 4;
  70. palette->colors[i].b = (Uint8)b;
  71. palette->colors[i].a = SDL_ALPHA_OPAQUE;
  72. }
  73. }
  74. /**
  75. * Helper that blits in a specific blend mode, -1 for color mod, -2 for alpha mod
  76. */
  77. static void testBlitBlendModeWithFormats(int mode, SDL_PixelFormat src_format, SDL_PixelFormat dst_format)
  78. {
  79. /* Allow up to 1 delta from theoretical value to account for rounding error */
  80. const int MAXIMUM_ERROR = 1;
  81. int ret;
  82. SDL_Surface *src;
  83. SDL_Surface *dst;
  84. Uint32 color;
  85. Uint8 srcR = 10, srcG = 128, srcB = 240, srcA = 100;
  86. Uint8 dstR = 128, dstG = 128, dstB = 128, dstA = 128;
  87. Uint8 expectedR, expectedG, expectedB, expectedA;
  88. Uint8 actualR, actualG, actualB, actualA;
  89. int deltaR, deltaG, deltaB, deltaA;
  90. /* Create dst surface */
  91. dst = SDL_CreateSurface(9, 1, dst_format);
  92. SDLTest_AssertCheck(dst != NULL, "Verify dst surface is not NULL");
  93. if (dst == NULL) {
  94. return;
  95. }
  96. /* Clear surface. */
  97. if (SDL_ISPIXELFORMAT_INDEXED(dst_format)) {
  98. SDL_Palette *palette = SDL_CreateSurfacePalette(dst);
  99. DitherPalette(palette);
  100. palette->colors[0].r = dstR;
  101. palette->colors[0].g = dstG;
  102. palette->colors[0].b = dstB;
  103. palette->colors[0].a = dstA;
  104. color = 0;
  105. } else {
  106. color = SDL_MapSurfaceRGBA(dst, dstR, dstG, dstB, dstA);
  107. SDLTest_AssertPass("Call to SDL_MapSurfaceRGBA()");
  108. }
  109. ret = SDL_FillSurfaceRect(dst, NULL, color);
  110. SDLTest_AssertPass("Call to SDL_FillSurfaceRect()");
  111. SDLTest_AssertCheck(ret == true, "Verify result from SDL_FillSurfaceRect, expected: true, got: %i", ret);
  112. SDL_GetRGBA(color, SDL_GetPixelFormatDetails(dst->format), SDL_GetSurfacePalette(dst), &dstR, &dstG, &dstB, &dstA);
  113. /* Create src surface */
  114. src = SDL_CreateSurface(9, 1, src_format);
  115. SDLTest_AssertCheck(src != NULL, "Verify src surface is not NULL");
  116. if (src == NULL) {
  117. return;
  118. }
  119. if (SDL_ISPIXELFORMAT_INDEXED(src_format)) {
  120. SDL_Palette *palette = SDL_CreateSurfacePalette(src);
  121. palette->colors[0].r = srcR;
  122. palette->colors[0].g = srcG;
  123. palette->colors[0].b = srcB;
  124. palette->colors[0].a = srcA;
  125. }
  126. /* Reset alpha modulation */
  127. ret = SDL_SetSurfaceAlphaMod(src, 255);
  128. SDLTest_AssertPass("Call to SDL_SetSurfaceAlphaMod()");
  129. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetSurfaceAlphaMod(), expected: true, got: %i", ret);
  130. /* Reset color modulation */
  131. ret = SDL_SetSurfaceColorMod(src, 255, 255, 255);
  132. SDLTest_AssertPass("Call to SDL_SetSurfaceColorMod()");
  133. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetSurfaceColorMod(), expected: true, got: %i", ret);
  134. /* Reset color key */
  135. ret = SDL_SetSurfaceColorKey(src, false, 0);
  136. SDLTest_AssertPass("Call to SDL_SetSurfaceColorKey()");
  137. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetSurfaceColorKey(), expected: true, got: %i", ret);
  138. /* Clear surface. */
  139. color = SDL_MapSurfaceRGBA(src, srcR, srcG, srcB, srcA);
  140. SDLTest_AssertPass("Call to SDL_MapSurfaceRGBA()");
  141. ret = SDL_FillSurfaceRect(src, NULL, color);
  142. SDLTest_AssertPass("Call to SDL_FillSurfaceRect()");
  143. SDLTest_AssertCheck(ret == true, "Verify result from SDL_FillSurfaceRect, expected: true, got: %i", ret);
  144. SDL_GetRGBA(color, SDL_GetPixelFormatDetails(src->format), SDL_GetSurfacePalette(src), &srcR, &srcG, &srcB, &srcA);
  145. /* Set blend mode. */
  146. if (mode >= 0) {
  147. ret = SDL_SetSurfaceBlendMode(src, (SDL_BlendMode)mode);
  148. SDLTest_AssertPass("Call to SDL_SetSurfaceBlendMode()");
  149. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetSurfaceBlendMode(..., %i), expected: true, got: %i", mode, ret);
  150. } else {
  151. ret = SDL_SetSurfaceBlendMode(src, SDL_BLENDMODE_BLEND);
  152. SDLTest_AssertPass("Call to SDL_SetSurfaceBlendMode()");
  153. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetSurfaceBlendMode(..., %i), expected: true, got: %i", mode, ret);
  154. }
  155. /* Test blend mode. */
  156. #define FLOAT(X) ((float)X / 255.0f)
  157. switch (mode) {
  158. case -1:
  159. /* Set color mod. */
  160. ret = SDL_SetSurfaceColorMod(src, srcR, srcG, srcB);
  161. SDLTest_AssertCheck(ret == true, "Validate results from calls to SDL_SetSurfaceColorMod, expected: true, got: %i", ret);
  162. expectedR = (Uint8)SDL_roundf(SDL_clamp((FLOAT(srcR) * FLOAT(srcR)) * FLOAT(srcA) + FLOAT(dstR) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  163. expectedG = (Uint8)SDL_roundf(SDL_clamp((FLOAT(srcG) * FLOAT(srcG)) * FLOAT(srcA) + FLOAT(dstG) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  164. expectedB = (Uint8)SDL_roundf(SDL_clamp((FLOAT(srcB) * FLOAT(srcB)) * FLOAT(srcA) + FLOAT(dstB) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  165. expectedA = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcA) + FLOAT(dstA) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  166. break;
  167. case -2:
  168. /* Set alpha mod. */
  169. ret = SDL_SetSurfaceAlphaMod(src, srcA);
  170. SDLTest_AssertCheck(ret == true, "Validate results from calls to SDL_SetSurfaceAlphaMod, expected: true, got: %i", ret);
  171. 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);
  172. 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);
  173. 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);
  174. expectedA = (Uint8)SDL_roundf(SDL_clamp((FLOAT(srcA) * FLOAT(srcA)) + FLOAT(dstA) * (1.0f - (FLOAT(srcA) * FLOAT(srcA))), 0.0f, 1.0f) * 255.0f);
  175. break;
  176. case SDL_BLENDMODE_NONE:
  177. expectedR = srcR;
  178. expectedG = srcG;
  179. expectedB = srcB;
  180. expectedA = SDL_ISPIXELFORMAT_ALPHA(dst_format) ? srcA : 255;
  181. break;
  182. case SDL_BLENDMODE_BLEND:
  183. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * FLOAT(srcA) + FLOAT(dstR) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  184. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * FLOAT(srcA) + FLOAT(dstG) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  185. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * FLOAT(srcA) + FLOAT(dstB) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  186. expectedA = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcA) + FLOAT(dstA) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  187. break;
  188. case SDL_BLENDMODE_BLEND_PREMULTIPLIED:
  189. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) + FLOAT(dstR) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  190. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) + FLOAT(dstG) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  191. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) + FLOAT(dstB) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  192. expectedA = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcA) + FLOAT(dstA) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  193. break;
  194. case SDL_BLENDMODE_ADD:
  195. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * FLOAT(srcA) + FLOAT(dstR), 0.0f, 1.0f) * 255.0f);
  196. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * FLOAT(srcA) + FLOAT(dstG), 0.0f, 1.0f) * 255.0f);
  197. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * FLOAT(srcA) + FLOAT(dstB), 0.0f, 1.0f) * 255.0f);
  198. expectedA = dstA;
  199. break;
  200. case SDL_BLENDMODE_ADD_PREMULTIPLIED:
  201. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) + FLOAT(dstR), 0.0f, 1.0f) * 255.0f);
  202. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) + FLOAT(dstG), 0.0f, 1.0f) * 255.0f);
  203. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) + FLOAT(dstB), 0.0f, 1.0f) * 255.0f);
  204. expectedA = dstA;
  205. break;
  206. case SDL_BLENDMODE_MOD:
  207. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * FLOAT(dstR), 0.0f, 1.0f) * 255.0f);
  208. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * FLOAT(dstG), 0.0f, 1.0f) * 255.0f);
  209. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * FLOAT(dstB), 0.0f, 1.0f) * 255.0f);
  210. expectedA = dstA;
  211. break;
  212. case SDL_BLENDMODE_MUL:
  213. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * FLOAT(dstR) + FLOAT(dstR) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  214. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * FLOAT(dstG) + FLOAT(dstG) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  215. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * FLOAT(dstB) + FLOAT(dstB) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  216. expectedA = dstA;
  217. break;
  218. default:
  219. SDLTest_LogError("Invalid blending mode: %d", mode);
  220. return;
  221. }
  222. if (SDL_ISPIXELFORMAT_INDEXED(dst_format)) {
  223. SDL_Palette *palette = SDL_GetSurfacePalette(dst);
  224. palette->colors[1].r = expectedR;
  225. palette->colors[1].g = expectedG;
  226. palette->colors[1].b = expectedB;
  227. palette->colors[1].a = expectedA;
  228. }
  229. /* Blitting. */
  230. ret = SDL_BlitSurface(src, NULL, dst, NULL);
  231. SDLTest_AssertCheck(ret == true, "Validate results from calls to SDL_BlitSurface, expected: true, got: %i: %s", ret, !ret ? SDL_GetError() : "success");
  232. if (ret) {
  233. SDL_ReadSurfacePixel(dst, 0, 0, &actualR, &actualG, &actualB, &actualA);
  234. deltaR = SDL_abs((int)actualR - expectedR);
  235. deltaG = SDL_abs((int)actualG - expectedG);
  236. deltaB = SDL_abs((int)actualB - expectedB);
  237. deltaA = SDL_abs((int)actualA - expectedA);
  238. SDLTest_AssertCheck(
  239. deltaR <= MAXIMUM_ERROR &&
  240. deltaG <= MAXIMUM_ERROR &&
  241. deltaB <= MAXIMUM_ERROR &&
  242. deltaA <= MAXIMUM_ERROR,
  243. "Checking %s -> %s blit results, expected %d,%d,%d,%d, got %d,%d,%d,%d",
  244. SDL_GetPixelFormatName(src_format),
  245. SDL_GetPixelFormatName(dst_format),
  246. expectedR, expectedG, expectedB, expectedA, actualR, actualG, actualB, actualA);
  247. }
  248. /* Clean up */
  249. SDL_DestroySurface(src);
  250. SDL_DestroySurface(dst);
  251. }
  252. static void testBlitBlendMode(int mode)
  253. {
  254. const SDL_PixelFormat src_formats[] = {
  255. SDL_PIXELFORMAT_INDEX8, SDL_PIXELFORMAT_XRGB8888, SDL_PIXELFORMAT_ARGB8888
  256. };
  257. const SDL_PixelFormat dst_formats[] = {
  258. SDL_PIXELFORMAT_XRGB8888, SDL_PIXELFORMAT_ARGB8888
  259. };
  260. int i, j;
  261. for (i = 0; i < SDL_arraysize(src_formats); ++i) {
  262. for (j = 0; j < SDL_arraysize(dst_formats); ++j) {
  263. testBlitBlendModeWithFormats(mode, src_formats[i], dst_formats[j]);
  264. }
  265. }
  266. }
  267. /* Helper to check that a file exists */
  268. static void AssertFileExist(const char *filename)
  269. {
  270. SDLTest_AssertCheck(SDL_GetPathInfo(filename, NULL), "Verify file '%s' exists", filename);
  271. }
  272. /* Test case functions */
  273. /**
  274. * Tests creating surface with invalid format
  275. */
  276. static int SDLCALL surface_testInvalidFormat(void *arg)
  277. {
  278. SDL_Surface *surface;
  279. surface = SDL_CreateSurface(32, 32, SDL_PIXELFORMAT_UNKNOWN);
  280. SDLTest_AssertCheck(surface == NULL, "Verify SDL_CreateSurface(SDL_PIXELFORMAT_UNKNOWN) returned NULL");
  281. SDL_DestroySurface(surface);
  282. surface = SDL_CreateSurfaceFrom(32, 32, SDL_PIXELFORMAT_UNKNOWN, NULL, 0);
  283. SDLTest_AssertCheck(surface == NULL, "Verify SDL_CreateSurfaceFrom(SDL_PIXELFORMAT_UNKNOWN) returned NULL");
  284. SDL_DestroySurface(surface);
  285. return TEST_COMPLETED;
  286. }
  287. /**
  288. * Tests sprite saving and loading
  289. */
  290. static int SDLCALL surface_testSaveLoad(void *arg)
  291. {
  292. int ret;
  293. const char *sampleFilename = "testSaveLoad.tmp";
  294. SDL_Surface *face;
  295. SDL_Surface *rface;
  296. SDL_Palette *palette;
  297. SDL_Color colors[] = {
  298. { 255, 0, 0, SDL_ALPHA_OPAQUE }, /* Red */
  299. { 0, 255, 0, SDL_ALPHA_OPAQUE } /* Green */
  300. };
  301. SDL_IOStream *stream;
  302. Uint8 r, g, b, a;
  303. /* Create sample surface */
  304. face = SDLTest_ImageFace();
  305. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  306. if (face == NULL) {
  307. return TEST_ABORTED;
  308. }
  309. /* Delete test file; ignore errors */
  310. SDL_RemovePath(sampleFilename);
  311. /* Save a BMP surface */
  312. ret = SDL_SaveBMP(face, sampleFilename);
  313. SDLTest_AssertPass("Call to SDL_SaveBMP()");
  314. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SaveBMP, expected: true, got: %i", ret);
  315. AssertFileExist(sampleFilename);
  316. /* Load a BMP surface */
  317. rface = SDL_LoadBMP(sampleFilename);
  318. SDLTest_AssertPass("Call to SDL_LoadBMP()");
  319. SDLTest_AssertCheck(rface != NULL, "Verify result from SDL_LoadBMP is not NULL");
  320. if (rface != NULL) {
  321. SDLTest_AssertCheck(face->w == rface->w, "Verify width of loaded surface, expected: %i, got: %i", face->w, rface->w);
  322. SDLTest_AssertCheck(face->h == rface->h, "Verify height of loaded surface, expected: %i, got: %i", face->h, rface->h);
  323. SDL_DestroySurface(rface);
  324. rface = NULL;
  325. }
  326. /* Delete test file; ignore errors */
  327. SDL_RemovePath(sampleFilename);
  328. /* Save a PNG surface */
  329. ret = SDL_SavePNG(face, sampleFilename);
  330. SDLTest_AssertPass("Call to SDL_SavePNG()");
  331. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SavePNG, expected: true, got: %i", ret);
  332. AssertFileExist(sampleFilename);
  333. /* Load a PNG surface */
  334. rface = SDL_LoadPNG(sampleFilename);
  335. SDLTest_AssertPass("Call to SDL_LoadPNG()");
  336. SDLTest_AssertCheck(rface != NULL, "Verify result from SDL_LoadPNG is not NULL");
  337. if (rface != NULL) {
  338. SDLTest_AssertCheck(face->w == rface->w, "Verify width of loaded surface, expected: %i, got: %i", face->w, rface->w);
  339. SDLTest_AssertCheck(face->h == rface->h, "Verify height of loaded surface, expected: %i, got: %i", face->h, rface->h);
  340. SDL_DestroySurface(rface);
  341. rface = NULL;
  342. }
  343. /* Delete test file; ignore errors */
  344. SDL_RemovePath(sampleFilename);
  345. /* Clean up */
  346. SDL_DestroySurface(face);
  347. face = NULL;
  348. /* Create an 8-bit image */
  349. face = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_INDEX8);
  350. SDLTest_AssertCheck(face != NULL, "Verify 8-bit surface is not NULL");
  351. if (face == NULL) {
  352. return TEST_ABORTED;
  353. }
  354. palette = SDL_CreatePalette(2);
  355. SDLTest_AssertCheck(palette != NULL, "Verify palette is not NULL");
  356. if (palette == NULL) {
  357. return TEST_ABORTED;
  358. }
  359. SDL_SetPaletteColors(palette, colors, 0, SDL_arraysize(colors));
  360. SDL_SetSurfacePalette(face, palette);
  361. SDL_DestroyPalette(palette);
  362. /* Set a green pixel */
  363. *(Uint8 *)face->pixels = 1;
  364. /* Save and reload as a BMP */
  365. stream = SDL_IOFromDynamicMem();
  366. SDLTest_AssertCheck(stream != NULL, "Verify iostream is not NULL");
  367. if (stream == NULL) {
  368. return TEST_ABORTED;
  369. }
  370. ret = SDL_SaveBMP_IO(face, stream, false);
  371. SDLTest_AssertPass("Call to SDL_SaveBMP()");
  372. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SaveBMP, expected: true, got: %i", ret);
  373. SDL_SeekIO(stream, 0, SDL_IO_SEEK_SET);
  374. rface = SDL_LoadBMP_IO(stream, false);
  375. SDLTest_AssertPass("Call to SDL_LoadBMP()");
  376. SDLTest_AssertCheck(rface != NULL, "Verify result from SDL_LoadBMP is not NULL");
  377. if (rface != NULL) {
  378. SDLTest_AssertCheck(face->w == rface->w, "Verify width of loaded surface, expected: %i, got: %i", face->w, rface->w);
  379. SDLTest_AssertCheck(face->h == rface->h, "Verify height of loaded surface, expected: %i, got: %i", face->h, rface->h);
  380. SDLTest_AssertCheck(rface->format == SDL_PIXELFORMAT_INDEX8, "Verify format of loaded surface, expected: %s, got: %s", SDL_GetPixelFormatName(face->format), SDL_GetPixelFormatName(rface->format));
  381. SDL_ReadSurfacePixel(rface, 0, 0, &r, &g, &b, &a);
  382. SDLTest_AssertCheck(r == colors[1].r &&
  383. g == colors[1].g &&
  384. b == colors[1].b &&
  385. a == colors[1].a,
  386. "Verify color of loaded surface, expected: %d,%d,%d,%d, got: %d,%d,%d,%d",
  387. r, g, b, a,
  388. colors[1].r, colors[1].g, colors[1].b, colors[1].a);
  389. SDL_DestroySurface(rface);
  390. rface = NULL;
  391. }
  392. SDL_CloseIO(stream);
  393. stream = NULL;
  394. /* Save and reload as a PNG */
  395. stream = SDL_IOFromDynamicMem();
  396. SDLTest_AssertCheck(stream != NULL, "Verify iostream is not NULL");
  397. if (stream == NULL) {
  398. return TEST_ABORTED;
  399. }
  400. ret = SDL_SavePNG_IO(face, stream, false);
  401. SDLTest_AssertPass("Call to SDL_SavePNG()");
  402. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SavePNG, expected: true, got: %i", ret);
  403. SDL_SeekIO(stream, 0, SDL_IO_SEEK_SET);
  404. rface = SDL_LoadPNG_IO(stream, false);
  405. SDLTest_AssertPass("Call to SDL_LoadPNG()");
  406. SDLTest_AssertCheck(rface != NULL, "Verify result from SDL_LoadPNG is not NULL");
  407. if (rface != NULL) {
  408. SDLTest_AssertCheck(face->w == rface->w, "Verify width of loaded surface, expected: %i, got: %i", face->w, rface->w);
  409. SDLTest_AssertCheck(face->h == rface->h, "Verify height of loaded surface, expected: %i, got: %i", face->h, rface->h);
  410. SDLTest_AssertCheck(rface->format == SDL_PIXELFORMAT_INDEX8, "Verify format of loaded surface, expected: %s, got: %s", SDL_GetPixelFormatName(face->format), SDL_GetPixelFormatName(rface->format));
  411. SDL_ReadSurfacePixel(rface, 0, 0, &r, &g, &b, &a);
  412. SDLTest_AssertCheck(r == colors[1].r &&
  413. g == colors[1].g &&
  414. b == colors[1].b &&
  415. a == colors[1].a,
  416. "Verify color of loaded surface, expected: %d,%d,%d,%d, got: %d,%d,%d,%d",
  417. r, g, b, a,
  418. colors[1].r, colors[1].g, colors[1].b, colors[1].a);
  419. SDL_DestroySurface(rface);
  420. rface = NULL;
  421. }
  422. SDL_CloseIO(stream);
  423. stream = NULL;
  424. SDL_DestroySurface(face);
  425. return TEST_COMPLETED;
  426. }
  427. /**
  428. * Tests tiled blitting.
  429. */
  430. static int SDLCALL surface_testBlitTiled(void *arg)
  431. {
  432. SDL_Surface *face = NULL;
  433. SDL_Surface *testSurface2x = NULL;
  434. SDL_Surface *referenceSurface2x = NULL;
  435. int ret = 0;
  436. /* Create sample surface */
  437. face = SDLTest_ImageFace();
  438. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  439. if (face == NULL) {
  440. return TEST_ABORTED;
  441. }
  442. /* Tiled blit - 1.0 scale */
  443. {
  444. ret = SDL_BlitSurfaceTiled(face, NULL, testSurface, NULL);
  445. SDLTest_AssertCheck(ret == true, "Verify result from SDL_BlitSurfaceTiled expected: true, got: %i", ret);
  446. /* See if it's the same */
  447. SDL_DestroySurface(referenceSurface);
  448. referenceSurface = SDLTest_ImageBlitTiled();
  449. ret = SDLTest_CompareSurfaces(testSurface, referenceSurface, 0);
  450. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  451. }
  452. /* Tiled blit - 2.0 scale */
  453. {
  454. testSurface2x = SDL_CreateSurface(testSurface->w * 2, testSurface->h * 2, testSurface->format);
  455. SDLTest_AssertCheck(testSurface != NULL, "Check that testSurface2x is not NULL");
  456. ret = SDL_FillSurfaceRect(testSurface2x, NULL, SDL_MapSurfaceRGBA(testSurface2x, 0, 0, 0, 255));
  457. SDLTest_AssertCheck(ret == true, "Validate result from SDL_FillSurfaceRect, expected: true, got: %i", ret);
  458. ret = SDL_BlitSurfaceTiledWithScale(face, NULL, 2.0f, SDL_SCALEMODE_NEAREST, testSurface2x, NULL);
  459. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_BlitSurfaceTiledWithScale, expected: true, got: %i", ret);
  460. /* See if it's the same */
  461. referenceSurface2x = SDL_CreateSurface(referenceSurface->w * 2, referenceSurface->h * 2, referenceSurface->format);
  462. SDL_BlitSurfaceScaled(referenceSurface, NULL, referenceSurface2x, NULL, SDL_SCALEMODE_NEAREST);
  463. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_BlitSurfaceScaled, expected: true, got: %i", ret);
  464. ret = SDLTest_CompareSurfaces(testSurface2x, referenceSurface2x, 0);
  465. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  466. }
  467. /* Tiled blit - very small scale */
  468. {
  469. float tiny_scale = 0.01f;
  470. ret = SDL_BlitSurfaceTiledWithScale(face, NULL, tiny_scale, SDL_SCALEMODE_NEAREST, testSurface, NULL);
  471. SDLTest_AssertCheck(ret == true, "Expected SDL_BlitSurfaceTiledWithScale to succeed with very small scale: %f, got: %i", tiny_scale, ret);
  472. }
  473. /* Clean up. */
  474. SDL_DestroySurface(face);
  475. SDL_DestroySurface(testSurface2x);
  476. SDL_DestroySurface(referenceSurface2x);
  477. return TEST_COMPLETED;
  478. }
  479. static const Uint8 COLOR_SEPARATION = 85;
  480. static void Fill9GridReferenceSurface(SDL_Surface *surface, int left_width, int right_width, int top_height, int bottom_height)
  481. {
  482. SDL_Rect rect;
  483. // Upper left
  484. rect.x = 0;
  485. rect.y = 0;
  486. rect.w = left_width;
  487. rect.h = top_height;
  488. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 1 * COLOR_SEPARATION, 1 * COLOR_SEPARATION, 0));
  489. // Top
  490. rect.x = left_width;
  491. rect.y = 0;
  492. rect.w = surface->w - left_width - right_width;
  493. rect.h = top_height;
  494. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 2 * COLOR_SEPARATION, 1 * COLOR_SEPARATION, 0));
  495. // Upper right
  496. rect.x = surface->w - right_width;
  497. rect.y = 0;
  498. rect.w = right_width;
  499. rect.h = top_height;
  500. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 3 * COLOR_SEPARATION, 1 * COLOR_SEPARATION, 0));
  501. // Left
  502. rect.x = 0;
  503. rect.y = top_height;
  504. rect.w = left_width;
  505. rect.h = surface->h - top_height - bottom_height;
  506. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 1 * COLOR_SEPARATION, 2 * COLOR_SEPARATION, 0));
  507. // Center
  508. rect.x = left_width;
  509. rect.y = top_height;
  510. rect.w = surface->w - right_width - left_width;
  511. rect.h = surface->h - top_height - bottom_height;
  512. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 2 * COLOR_SEPARATION, 2 * COLOR_SEPARATION, 0));
  513. // Right
  514. rect.x = surface->w - right_width;
  515. rect.y = top_height;
  516. rect.w = right_width;
  517. rect.h = surface->h - top_height - bottom_height;
  518. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 3 * COLOR_SEPARATION, 2 * COLOR_SEPARATION, 0));
  519. // Lower left
  520. rect.x = 0;
  521. rect.y = surface->h - bottom_height;
  522. rect.w = left_width;
  523. rect.h = bottom_height;
  524. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 1 * COLOR_SEPARATION, 3 * COLOR_SEPARATION, 0));
  525. // Bottom
  526. rect.x = left_width;
  527. rect.y = surface->h - bottom_height;
  528. rect.w = surface->w - left_width - right_width;
  529. rect.h = bottom_height;
  530. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 2 * COLOR_SEPARATION, 3 * COLOR_SEPARATION, 0));
  531. // Lower right
  532. rect.x = surface->w - right_width;
  533. rect.y = surface->h - bottom_height;
  534. rect.w = right_width;
  535. rect.h = bottom_height;
  536. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 3 * COLOR_SEPARATION, 3 * COLOR_SEPARATION, 0));
  537. }
  538. /**
  539. * Tests 9-grid blitting.
  540. */
  541. static int SDLCALL surface_testBlit9Grid(void *arg)
  542. {
  543. SDL_Surface *source = NULL;
  544. int x, y;
  545. int ret = 0;
  546. /* Create source surface */
  547. source = SDL_CreateSurface(3, 3, SDL_PIXELFORMAT_RGBA32);
  548. SDLTest_AssertCheck(source != NULL, "Verify source surface is not NULL");
  549. for (y = 0; y < 3; ++y) {
  550. for (x = 0; x < 3; ++x) {
  551. SDL_WriteSurfacePixel(source, x, y, (Uint8)((1 + x) * COLOR_SEPARATION), (Uint8)((1 + y) * COLOR_SEPARATION), 0, 255);
  552. }
  553. }
  554. /* 9-grid blit - 1.0 scale */
  555. {
  556. /* Create reference surface */
  557. SDL_DestroySurface(referenceSurface);
  558. referenceSurface = SDL_CreateSurface(testSurface->w, testSurface->h, testSurface->format);
  559. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  560. Fill9GridReferenceSurface(referenceSurface, 1, 1, 1, 1);
  561. ret = SDL_BlitSurface9Grid(source, NULL, 1, 1, 1, 1, 0.0f, SDL_SCALEMODE_NEAREST, testSurface, NULL);
  562. SDLTest_AssertCheck(ret == true, "Validate result from SDL_BlitSurface9Grid, expected: true, got: %i", ret);
  563. ret = SDLTest_CompareSurfaces(testSurface, referenceSurface, 0);
  564. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  565. }
  566. /* 9-grid blit - 2.0 scale */
  567. {
  568. /* Create reference surface */
  569. SDL_DestroySurface(referenceSurface);
  570. referenceSurface = SDL_CreateSurface(testSurface->w, testSurface->h, testSurface->format);
  571. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  572. Fill9GridReferenceSurface(referenceSurface, 2, 2, 2, 2);
  573. ret = SDL_BlitSurface9Grid(source, NULL, 1, 1, 1, 1, 2.0f, SDL_SCALEMODE_NEAREST, testSurface, NULL);
  574. SDLTest_AssertCheck(ret == true, "Validate result from SDL_BlitSurface9Grid, expected: true, got: %i", ret);
  575. ret = SDLTest_CompareSurfaces(testSurface, referenceSurface, 0);
  576. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  577. }
  578. /* Clean up. */
  579. SDL_DestroySurface(source);
  580. /* Create complex source surface */
  581. source = SDL_CreateSurface(5, 5, SDL_PIXELFORMAT_RGBA32);
  582. SDLTest_AssertCheck(source != NULL, "Verify source surface is not NULL");
  583. SDL_WriteSurfacePixel(source, 0, 0, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  584. SDL_WriteSurfacePixel(source, 1, 0, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  585. SDL_WriteSurfacePixel(source, 2, 0, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  586. SDL_WriteSurfacePixel(source, 3, 0, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  587. SDL_WriteSurfacePixel(source, 4, 0, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  588. SDL_WriteSurfacePixel(source, 0, 1, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  589. SDL_WriteSurfacePixel(source, 1, 1, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  590. SDL_WriteSurfacePixel(source, 2, 1, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  591. SDL_WriteSurfacePixel(source, 3, 1, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  592. SDL_WriteSurfacePixel(source, 4, 1, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  593. SDL_WriteSurfacePixel(source, 0, 2, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  594. SDL_WriteSurfacePixel(source, 1, 2, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  595. SDL_WriteSurfacePixel(source, 2, 2, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  596. SDL_WriteSurfacePixel(source, 3, 2, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  597. SDL_WriteSurfacePixel(source, 4, 2, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  598. SDL_WriteSurfacePixel(source, 0, 3, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  599. SDL_WriteSurfacePixel(source, 1, 3, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  600. SDL_WriteSurfacePixel(source, 2, 3, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  601. SDL_WriteSurfacePixel(source, 3, 3, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  602. SDL_WriteSurfacePixel(source, 4, 3, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  603. SDL_WriteSurfacePixel(source, 0, 4, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  604. SDL_WriteSurfacePixel(source, 1, 4, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  605. SDL_WriteSurfacePixel(source, 2, 4, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  606. SDL_WriteSurfacePixel(source, 3, 4, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  607. SDL_WriteSurfacePixel(source, 4, 4, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  608. /* complex 9-grid blit - 1.0 scale */
  609. {
  610. SDLTest_Log("complex 9-grid blit - 1.0 scale");
  611. /* Create reference surface */
  612. SDL_DestroySurface(referenceSurface);
  613. referenceSurface = SDL_CreateSurface(testSurface->w, testSurface->h, testSurface->format);
  614. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  615. Fill9GridReferenceSurface(referenceSurface, 1, 2, 1, 2);
  616. ret = SDL_BlitSurface9Grid(source, NULL, 1, 2, 1, 2, 0.0f, SDL_SCALEMODE_NEAREST, testSurface, NULL);
  617. SDLTest_AssertCheck(ret == true, "Validate result from SDL_BlitSurface9Grid, expected: true, got: %i", ret);
  618. ret = SDLTest_CompareSurfaces(testSurface, referenceSurface, 0);
  619. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  620. }
  621. /* complex 9-grid blit - 2.0 scale */
  622. {
  623. SDLTest_Log("complex 9-grid blit - 2.0 scale");
  624. /* Create reference surface */
  625. SDL_DestroySurface(referenceSurface);
  626. referenceSurface = SDL_CreateSurface(testSurface->w, testSurface->h, testSurface->format);
  627. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  628. Fill9GridReferenceSurface(referenceSurface, 2, 4, 2, 4);
  629. ret = SDL_BlitSurface9Grid(source, NULL, 1, 2, 1, 2, 2.0f, SDL_SCALEMODE_NEAREST, testSurface, NULL);
  630. SDLTest_AssertCheck(ret == true, "Validate result from SDL_BlitSurface9Grid, expected: true, got: %i", ret);
  631. ret = SDLTest_CompareSurfaces(testSurface, referenceSurface, 0);
  632. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  633. }
  634. /* Clean up. */
  635. SDL_DestroySurface(source);
  636. return TEST_COMPLETED;
  637. }
  638. /**
  639. * Tests blitting between multiple surfaces of the same format
  640. */
  641. static int SDLCALL surface_testBlitMultiple(void *arg)
  642. {
  643. SDL_Surface *source, *surface;
  644. SDL_Palette *palette;
  645. Uint8 *pixels;
  646. palette = SDL_CreatePalette(2);
  647. SDLTest_AssertCheck(palette != NULL, "SDL_CreatePalette()");
  648. palette->colors[0].r = 0;
  649. palette->colors[0].g = 0;
  650. palette->colors[0].b = 0;
  651. palette->colors[1].r = 0xFF;
  652. palette->colors[1].g = 0;
  653. palette->colors[1].b = 0;
  654. source = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_INDEX8);
  655. SDLTest_AssertCheck(source != NULL, "SDL_CreateSurface()");
  656. SDL_SetSurfacePalette(source, palette);
  657. *(Uint8 *)source->pixels = 1;
  658. /* Set up a blit to a surface using the palette */
  659. surface = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_INDEX8);
  660. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  661. SDL_SetSurfacePalette(surface, palette);
  662. pixels = (Uint8 *)surface->pixels;
  663. *pixels = 0;
  664. SDL_BlitSurface(source, NULL, surface, NULL);
  665. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  666. /* Set up a blit to another surface using the same palette */
  667. SDL_DestroySurface(surface);
  668. surface = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_INDEX8);
  669. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  670. SDL_SetSurfacePalette(surface, palette);
  671. pixels = (Uint8 *)surface->pixels;
  672. *pixels = 0;
  673. SDL_BlitSurface(source, NULL, surface, NULL);
  674. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  675. /* Set up a blit to new surface with a different format */
  676. SDL_DestroySurface(surface);
  677. surface = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA32);
  678. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  679. pixels = (Uint8 *)surface->pixels;
  680. SDL_BlitSurface(source, NULL, surface, NULL);
  681. SDLTest_AssertCheck(*pixels == 0xFF, "Expected *pixels == 0xFF got 0x%.2X", *pixels);
  682. /* Set up a blit to another surface with the same format */
  683. SDL_DestroySurface(surface);
  684. surface = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA32);
  685. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  686. pixels = (Uint8 *)surface->pixels;
  687. SDL_BlitSurface(source, NULL, surface, NULL);
  688. SDLTest_AssertCheck(*pixels == 0xFF, "Expected *pixels == 0xFF got 0x%.2X", *pixels);
  689. SDL_DestroyPalette(palette);
  690. SDL_DestroySurface(source);
  691. SDL_DestroySurface(surface);
  692. return TEST_COMPLETED;
  693. }
  694. /**
  695. * Tests operations on surfaces with NULL pixels
  696. */
  697. static int SDLCALL surface_testSurfaceNULLPixels(void *arg)
  698. {
  699. SDL_Surface *a, *b, *face;
  700. bool result;
  701. face = SDLTest_ImageFace();
  702. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  703. if (face == NULL) {
  704. return TEST_ABORTED;
  705. }
  706. /* Test blitting with NULL pixels */
  707. a = SDL_CreateSurfaceFrom(face->w, face->h, SDL_PIXELFORMAT_ARGB8888, NULL, 0);
  708. SDLTest_AssertCheck(a != NULL, "Verify result from SDL_CreateSurfaceFrom() with NULL pixels is not NULL");
  709. result = SDL_BlitSurface(a, NULL, face, NULL);
  710. SDLTest_AssertCheck(!result, "Verify result from SDL_BlitSurface() with src having NULL pixels is false");
  711. result = SDL_BlitSurface(face, NULL, a, NULL);
  712. SDLTest_AssertCheck(!result, "Verify result from SDL_BlitSurface() with dst having NULL pixels is false");
  713. b = SDL_CreateSurfaceFrom(face->w * 2, face->h * 2, SDL_PIXELFORMAT_ARGB8888, NULL, 0);
  714. SDLTest_AssertCheck(b != NULL, "Verify result from SDL_CreateSurfaceFrom() with NULL pixels is not NULL");
  715. result = SDL_BlitSurfaceScaled(b, NULL, face, NULL, SDL_SCALEMODE_NEAREST);
  716. SDLTest_AssertCheck(!result, "Verify result from SDL_BlitSurfaceScaled() with src having NULL pixels is false");
  717. result = SDL_BlitSurfaceScaled(face, NULL, b, NULL, SDL_SCALEMODE_NEAREST);
  718. SDLTest_AssertCheck(!result, "Verify result from SDL_BlitSurfaceScaled() with dst having NULL pixels is false");
  719. SDL_DestroySurface(b);
  720. b = NULL;
  721. /* Test conversion with NULL pixels */
  722. b = SDL_ConvertSurfaceAndColorspace(a, SDL_PIXELFORMAT_ABGR8888, NULL, SDL_COLORSPACE_UNKNOWN, 0);
  723. SDLTest_AssertCheck(b != NULL, "Verify result from SDL_ConvertSurfaceAndColorspace() with NULL pixels is not NULL");
  724. SDL_DestroySurface(b);
  725. b = NULL;
  726. /* Test duplication with NULL pixels */
  727. b = SDL_DuplicateSurface(a);
  728. SDLTest_AssertCheck(b != NULL, "Verify result from SDL_DuplicateSurface() with NULL pixels is not NULL");
  729. SDL_DestroySurface(b);
  730. b = NULL;
  731. /* Test scaling with NULL pixels */
  732. b = SDL_ScaleSurface(a, a->w * 2, a->h * 2, SDL_SCALEMODE_NEAREST);
  733. SDLTest_AssertCheck(b != NULL, "Verify result from SDL_ScaleSurface() with NULL pixels is not NULL");
  734. SDLTest_AssertCheck(b->pixels == NULL, "Verify pixels from SDL_ScaleSurface() is NULL");
  735. SDL_DestroySurface(b);
  736. b = NULL;
  737. /* Test filling surface with NULL pixels */
  738. result = SDL_FillSurfaceRect(a, NULL, 0);
  739. SDLTest_AssertCheck(result, "Verify result from SDL_FillSurfaceRect() with dst having NULL pixels is true");
  740. /* Clean up. */
  741. SDL_DestroySurface(face);
  742. SDL_DestroySurface(a);
  743. SDL_DestroySurface(b);
  744. return TEST_COMPLETED;
  745. }
  746. /**
  747. * Tests operations on surfaces with RLE pixels
  748. */
  749. static int SDLCALL surface_testSurfaceRLEPixels(void *arg)
  750. {
  751. SDL_Surface *face, *a, *b, *tmp;
  752. int ret;
  753. bool result;
  754. face = SDLTest_ImageFace();
  755. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  756. if (face == NULL) {
  757. return TEST_ABORTED;
  758. }
  759. /* RLE encoding only works for 32-bit surfaces with alpha in the high bits */
  760. if (face->format != SDL_PIXELFORMAT_ARGB8888) {
  761. tmp = SDL_ConvertSurface(face, SDL_PIXELFORMAT_ARGB8888);
  762. SDLTest_AssertCheck(tmp != NULL, "Verify tmp surface is not NULL");
  763. if (tmp == NULL) {
  764. return TEST_ABORTED;
  765. }
  766. SDL_DestroySurface(face);
  767. face = tmp;
  768. }
  769. /* Create a temporary surface to trigger RLE encoding during blit */
  770. tmp = SDL_DuplicateSurface(face);
  771. SDLTest_AssertCheck(tmp != NULL, "Verify result from SDL_DuplicateSurface() with RLE pixels is not NULL");
  772. result = SDL_SetSurfaceRLE(face, true);
  773. SDLTest_AssertCheck(result, "Verify result from SDL_SetSurfaceRLE() is true");
  774. /* Test duplication with RLE pixels */
  775. a = SDL_DuplicateSurface(face);
  776. SDLTest_AssertCheck(a != NULL, "Verify result from SDL_DuplicateSurface() with RLE pixels is not NULL");
  777. SDLTest_AssertCheck(SDL_SurfaceHasRLE(a), "Verify result from SDL_DuplicateSurface() with RLE pixels has RLE set");
  778. ret = SDLTest_CompareSurfaces(a, face, 0);
  779. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  780. /* Verify that blitting from an RLE surface does RLE encode it */
  781. SDLTest_AssertCheck(!SDL_MUSTLOCK(a), "Verify initial RLE surface does not need to be locked");
  782. SDLTest_AssertCheck(a->pixels != NULL, "Verify initial RLE surface has pixels available");
  783. result = SDL_BlitSurface(a, NULL, tmp, NULL);
  784. SDLTest_AssertCheck(result, "Verify result from SDL_BlitSurface() with RLE surface is true");
  785. SDLTest_AssertCheck(SDL_MUSTLOCK(a), "Verify RLE surface after blit needs to be locked");
  786. SDLTest_AssertCheck(a->pixels == NULL, "Verify RLE surface after blit does not have pixels available");
  787. ret = SDLTest_CompareSurfaces(tmp, face, 0);
  788. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  789. /* Test scaling with RLE pixels */
  790. b = SDL_ScaleSurface(a, a->w * 2, a->h * 2, SDL_SCALEMODE_NEAREST);
  791. SDLTest_AssertCheck(b != NULL, "Verify result from SDL_ScaleSurface() is not NULL");
  792. SDLTest_AssertCheck(SDL_SurfaceHasRLE(b), "Verify result from SDL_ScaleSurface() with RLE pixels has RLE set");
  793. /* Test scaling blitting with RLE pixels */
  794. result = SDL_BlitSurfaceScaled(a, NULL, b, NULL, SDL_SCALEMODE_NEAREST);
  795. SDLTest_AssertCheck(result, "Verify result from SDL_BlitSurfaceScaled() with src having RLE pixels is true");
  796. SDL_BlitSurface(a, NULL, tmp, NULL);
  797. SDL_DestroySurface(b);
  798. b = NULL;
  799. /* Test conversion with RLE pixels */
  800. b = SDL_ConvertSurfaceAndColorspace(a, SDL_PIXELFORMAT_ABGR8888, NULL, SDL_COLORSPACE_UNKNOWN, 0);
  801. SDLTest_AssertCheck(b != NULL, "Verify result from SDL_ConvertSurfaceAndColorspace() with RLE pixels is not NULL");
  802. SDLTest_AssertCheck(SDL_SurfaceHasRLE(b), "Verify result from SDL_ConvertSurfaceAndColorspace() with RLE pixels has RLE set");
  803. ret = SDLTest_CompareSurfacesIgnoreTransparentPixels(b, face, 0);
  804. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  805. SDL_BlitSurface(a, NULL, tmp, NULL);
  806. SDL_DestroySurface(b);
  807. b = NULL;
  808. #if 0 /* This will currently fail, you must lock the surface first */
  809. /* Test filling surface with RLE pixels */
  810. result = SDL_FillSurfaceRect(a, NULL, 0);
  811. SDLTest_AssertCheck(result, "Verify result from SDL_FillSurfaceRect() with dst having RLE pixels is true");
  812. #endif
  813. /* Make sure the RLE surface still needs to be locked after surface operations */
  814. SDLTest_AssertCheck(a->pixels == NULL, "Verify RLE surface after operations does not have pixels available");
  815. /* Clean up. */
  816. SDL_DestroySurface(face);
  817. SDL_DestroySurface(a);
  818. SDL_DestroySurface(b);
  819. SDL_DestroySurface(tmp);
  820. return TEST_COMPLETED;
  821. }
  822. /**
  823. * Tests surface conversion.
  824. */
  825. static int SDLCALL surface_testSurfaceConversion(void *arg)
  826. {
  827. SDL_Surface *rface = NULL, *face = NULL;
  828. int ret = 0;
  829. /* Create sample surface */
  830. face = SDLTest_ImageFace();
  831. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  832. if (face == NULL) {
  833. return TEST_ABORTED;
  834. }
  835. /* Set transparent pixel as the pixel at (0,0) */
  836. if (SDL_GetSurfacePalette(face)) {
  837. ret = SDL_SetSurfaceColorKey(face, true, *(Uint8 *)face->pixels);
  838. SDLTest_AssertPass("Call to SDL_SetSurfaceColorKey()");
  839. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetSurfaceColorKey, expected: true, got: %i", ret);
  840. }
  841. /* Convert to 32 bit to compare. */
  842. rface = SDL_ConvertSurface(face, testSurface->format);
  843. SDLTest_AssertPass("Call to SDL_ConvertSurface()");
  844. SDLTest_AssertCheck(rface != NULL, "Verify result from SDL_ConvertSurface is not NULL");
  845. /* Compare surface. */
  846. ret = SDLTest_CompareSurfaces(rface, face, 0);
  847. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  848. /* Clean up. */
  849. SDL_DestroySurface(face);
  850. SDL_DestroySurface(rface);
  851. return TEST_COMPLETED;
  852. }
  853. /**
  854. * Tests surface conversion across all pixel formats.
  855. */
  856. static int SDLCALL surface_testCompleteSurfaceConversion(void *arg)
  857. {
  858. Uint32 pixel_formats[] = {
  859. SDL_PIXELFORMAT_INDEX8,
  860. SDL_PIXELFORMAT_RGB332,
  861. SDL_PIXELFORMAT_XRGB4444,
  862. SDL_PIXELFORMAT_XBGR4444,
  863. SDL_PIXELFORMAT_XRGB1555,
  864. SDL_PIXELFORMAT_XBGR1555,
  865. SDL_PIXELFORMAT_ARGB4444,
  866. SDL_PIXELFORMAT_RGBA4444,
  867. SDL_PIXELFORMAT_ABGR4444,
  868. SDL_PIXELFORMAT_BGRA4444,
  869. SDL_PIXELFORMAT_ARGB1555,
  870. SDL_PIXELFORMAT_RGBA5551,
  871. SDL_PIXELFORMAT_ABGR1555,
  872. SDL_PIXELFORMAT_BGRA5551,
  873. SDL_PIXELFORMAT_RGB565,
  874. SDL_PIXELFORMAT_BGR565,
  875. SDL_PIXELFORMAT_RGB24,
  876. SDL_PIXELFORMAT_BGR24,
  877. SDL_PIXELFORMAT_XRGB8888,
  878. SDL_PIXELFORMAT_RGBX8888,
  879. SDL_PIXELFORMAT_XBGR8888,
  880. SDL_PIXELFORMAT_BGRX8888,
  881. SDL_PIXELFORMAT_ARGB8888,
  882. SDL_PIXELFORMAT_RGBA8888,
  883. SDL_PIXELFORMAT_ABGR8888,
  884. SDL_PIXELFORMAT_BGRA8888,
  885. #if 0 /* We aren't testing HDR10 colorspace conversion */
  886. SDL_PIXELFORMAT_XRGB2101010,
  887. SDL_PIXELFORMAT_XBGR2101010,
  888. SDL_PIXELFORMAT_ARGB2101010,
  889. SDL_PIXELFORMAT_ABGR2101010,
  890. #endif
  891. };
  892. SDL_Surface *face = NULL, *cvt1, *cvt2, *final;
  893. const SDL_PixelFormatDetails *fmt1, *fmt2;
  894. int i, j, ret = 0;
  895. /* Create sample surface */
  896. face = SDLTest_ImageFace();
  897. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  898. if (face == NULL) {
  899. return TEST_ABORTED;
  900. }
  901. /* Set transparent pixel as the pixel at (0,0) */
  902. if (SDL_GetSurfacePalette(face)) {
  903. ret = SDL_SetSurfaceColorKey(face, true, *(Uint8 *)face->pixels);
  904. SDLTest_AssertPass("Call to SDL_SetSurfaceColorKey()");
  905. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetSurfaceColorKey, expected: true, got: %i", ret);
  906. }
  907. for (i = 0; i < SDL_arraysize(pixel_formats); ++i) {
  908. for (j = 0; j < SDL_arraysize(pixel_formats); ++j) {
  909. fmt1 = SDL_GetPixelFormatDetails(pixel_formats[i]);
  910. SDLTest_AssertCheck(fmt1 != NULL, "SDL_GetPixelFormatDetails(%s[0x%08" SDL_PRIx32 "]) should return a non-null pixel format",
  911. SDL_GetPixelFormatName(pixel_formats[i]), pixel_formats[i]);
  912. cvt1 = SDL_ConvertSurface(face, fmt1->format);
  913. SDLTest_AssertCheck(cvt1 != NULL, "SDL_ConvertSurface(..., %s[0x%08" SDL_PRIx32 "]) should return a non-null surface",
  914. SDL_GetPixelFormatName(pixel_formats[i]), pixel_formats[i]);
  915. fmt2 = SDL_GetPixelFormatDetails(pixel_formats[j]);
  916. SDLTest_AssertCheck(fmt2 != NULL, "SDL_GetPixelFormatDetails(%s[0x%08" SDL_PRIx32 "]) should return a non-null pixel format",
  917. SDL_GetPixelFormatName(pixel_formats[i]), pixel_formats[i]);
  918. cvt2 = SDL_ConvertSurface(cvt1, fmt2->format);
  919. SDLTest_AssertCheck(cvt2 != NULL, "SDL_ConvertSurface(..., %s[0x%08" SDL_PRIx32 "]) should return a non-null surface",
  920. SDL_GetPixelFormatName(pixel_formats[i]), pixel_formats[i]);
  921. if (fmt1 && fmt2 &&
  922. fmt1->bytes_per_pixel == SDL_BYTESPERPIXEL(face->format) &&
  923. fmt2->bytes_per_pixel == SDL_BYTESPERPIXEL(face->format) &&
  924. SDL_ISPIXELFORMAT_ALPHA(fmt1->format) == SDL_ISPIXELFORMAT_ALPHA(face->format) &&
  925. SDL_ISPIXELFORMAT_ALPHA(fmt2->format) == SDL_ISPIXELFORMAT_ALPHA(face->format)) {
  926. final = SDL_ConvertSurface(cvt2, face->format);
  927. SDL_assert(final != NULL);
  928. /* Compare surface. */
  929. ret = SDLTest_CompareSurfaces(face, final, 0);
  930. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  931. SDL_DestroySurface(final);
  932. }
  933. SDL_DestroySurface(cvt1);
  934. SDL_DestroySurface(cvt2);
  935. }
  936. }
  937. /* Clean up. */
  938. SDL_DestroySurface(face);
  939. return TEST_COMPLETED;
  940. }
  941. /**
  942. * Tests sprite loading. A failure case.
  943. */
  944. static int SDLCALL surface_testLoadFailure(void *arg)
  945. {
  946. SDL_Surface *face = SDL_LoadBMP("nonexistant.bmp");
  947. SDLTest_AssertCheck(face == NULL, "SDL_CreateLoadBmp");
  948. return TEST_COMPLETED;
  949. }
  950. /**
  951. * Tests blitting from a zero sized source rectangle
  952. */
  953. static int SDLCALL surface_testBlitZeroSource(void *arg)
  954. {
  955. SDL_Surface *src = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA8888);
  956. SDL_Surface *dst = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA8888);
  957. SDL_Rect srcrect = { 0, 0, 0, 0 };
  958. int ret;
  959. SDLTest_AssertPass("Call to SDL_BlitSurfaceScaled() with zero sized source rectangle");
  960. SDL_FillSurfaceRect(src, NULL, SDL_MapSurfaceRGB(src, 255, 255, 255));
  961. SDL_BlitSurfaceScaled(src, &srcrect, dst, NULL, SDL_SCALEMODE_NEAREST);
  962. ret = SDLTest_CompareSurfaces(dst, src, 0);
  963. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  964. SDL_DestroySurface(src);
  965. SDL_DestroySurface(dst);
  966. return TEST_COMPLETED;
  967. }
  968. /**
  969. * Tests some blitting routines.
  970. */
  971. static int SDLCALL surface_testBlit(void *arg)
  972. {
  973. /* Basic blitting */
  974. testBlitBlendMode(SDL_BLENDMODE_NONE);
  975. return TEST_COMPLETED;
  976. }
  977. /**
  978. * Tests some blitting routines with color mod
  979. */
  980. static int SDLCALL surface_testBlitColorMod(void *arg)
  981. {
  982. /* Basic blitting with color mod */
  983. testBlitBlendMode(-1);
  984. return TEST_COMPLETED;
  985. }
  986. /**
  987. * Tests some blitting routines with alpha mod
  988. */
  989. static int SDLCALL surface_testBlitAlphaMod(void *arg)
  990. {
  991. /* Basic blitting with alpha mod */
  992. testBlitBlendMode(-2);
  993. return TEST_COMPLETED;
  994. }
  995. /**
  996. * Tests some more blitting routines.
  997. */
  998. static int SDLCALL surface_testBlitBlendBlend(void *arg)
  999. {
  1000. /* Blend blitting */
  1001. testBlitBlendMode(SDL_BLENDMODE_BLEND);
  1002. return TEST_COMPLETED;
  1003. }
  1004. /**
  1005. * @brief Tests some more blitting routines.
  1006. */
  1007. static int SDLCALL surface_testBlitBlendPremultiplied(void *arg)
  1008. {
  1009. /* Blend premultiplied blitting */
  1010. testBlitBlendMode(SDL_BLENDMODE_BLEND_PREMULTIPLIED);
  1011. return TEST_COMPLETED;
  1012. }
  1013. /**
  1014. * Tests some more blitting routines.
  1015. */
  1016. static int SDLCALL surface_testBlitBlendAdd(void *arg)
  1017. {
  1018. /* Add blitting */
  1019. testBlitBlendMode(SDL_BLENDMODE_ADD);
  1020. return TEST_COMPLETED;
  1021. }
  1022. /**
  1023. * Tests some more blitting routines.
  1024. */
  1025. static int SDLCALL surface_testBlitBlendAddPremultiplied(void *arg)
  1026. {
  1027. /* Add premultiplied blitting */
  1028. testBlitBlendMode(SDL_BLENDMODE_ADD_PREMULTIPLIED);
  1029. return TEST_COMPLETED;
  1030. }
  1031. /**
  1032. * Tests some more blitting routines.
  1033. */
  1034. static int SDLCALL surface_testBlitBlendMod(void *arg)
  1035. {
  1036. /* Mod blitting */
  1037. testBlitBlendMode(SDL_BLENDMODE_MOD);
  1038. return TEST_COMPLETED;
  1039. }
  1040. /**
  1041. * Tests some more blitting routines.
  1042. */
  1043. static int SDLCALL surface_testBlitBlendMul(void *arg)
  1044. {
  1045. /* Mod blitting */
  1046. testBlitBlendMode(SDL_BLENDMODE_MUL);
  1047. return TEST_COMPLETED;
  1048. }
  1049. /**
  1050. * Tests blitting invalid surfaces.
  1051. */
  1052. static int SDLCALL surface_testBlitInvalid(void *arg)
  1053. {
  1054. SDL_Surface *valid, *invalid;
  1055. bool result;
  1056. valid = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA8888);
  1057. SDLTest_AssertCheck(valid != NULL, "Check surface creation");
  1058. invalid = SDL_CreateSurface(0, 0, SDL_PIXELFORMAT_RGBA8888);
  1059. SDLTest_AssertCheck(invalid != NULL, "Check surface creation");
  1060. SDLTest_AssertCheck(invalid->pixels == NULL, "Check surface pixels are NULL");
  1061. result = SDL_BlitSurface(invalid, NULL, valid, NULL);
  1062. SDLTest_AssertCheck(result == false, "SDL_BlitSurface(invalid, NULL, valid, NULL), result = %s\n", result ? "true" : "false");
  1063. result = SDL_BlitSurface(valid, NULL, invalid, NULL);
  1064. SDLTest_AssertCheck(result == false, "SDL_BlitSurface(valid, NULL, invalid, NULL), result = %s\n", result ? "true" : "false");
  1065. result = SDL_BlitSurfaceScaled(invalid, NULL, valid, NULL, SDL_SCALEMODE_NEAREST);
  1066. SDLTest_AssertCheck(result == false, "SDL_BlitSurfaceScaled(invalid, NULL, valid, NULL, SDL_SCALEMODE_NEAREST), result = %s\n", result ? "true" : "false");
  1067. result = SDL_BlitSurfaceScaled(valid, NULL, invalid, NULL, SDL_SCALEMODE_NEAREST);
  1068. SDLTest_AssertCheck(result == false, "SDL_BlitSurfaceScaled(valid, NULL, invalid, NULL, SDL_SCALEMODE_NEAREST), result = %s\n", result ? "true" : "false");
  1069. SDL_DestroySurface(valid);
  1070. SDL_DestroySurface(invalid);
  1071. return TEST_COMPLETED;
  1072. }
  1073. static int SDLCALL surface_testBlitsWithBadCoordinates(void *arg)
  1074. {
  1075. const SDL_Rect rect[8] = {
  1076. { SDL_MAX_SINT32, 0, 2, 2 },
  1077. { 0, SDL_MAX_SINT32, 2, 2 },
  1078. { 0, 0, SDL_MAX_SINT32, 2 },
  1079. { 0, 0, 2, SDL_MAX_SINT32 },
  1080. { SDL_MIN_SINT32, 0, 2, 2 },
  1081. { 0, SDL_MIN_SINT32, 2, 2 },
  1082. { 0, 0, SDL_MIN_SINT32, 2 },
  1083. { 0, 0, 2, SDL_MIN_SINT32 }
  1084. };
  1085. SDL_Surface *s;
  1086. bool result;
  1087. int i;
  1088. s = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA8888);
  1089. SDLTest_AssertCheck(s != NULL, "Check surface creation");
  1090. for (i = 0; i < 8; i++) {
  1091. result = SDL_BlitSurface(s, NULL, s, &rect[i]);
  1092. SDLTest_AssertCheck(result == true, "SDL_BlitSurface(valid, NULL, valid, &rect), result = %s", result ? "true" : "false");
  1093. result = SDL_BlitSurface(s, &rect[i], s, NULL);
  1094. SDLTest_AssertCheck(result == true, "SDL_BlitSurface(valid, &rect, valid, NULL), result = %s", result ? "true" : "false");
  1095. result = SDL_BlitSurfaceScaled(s, NULL, s, &rect[i], SDL_SCALEMODE_NEAREST);
  1096. SDLTest_AssertCheck(result == true, "SDL_BlitSurfaceScaled(valid, NULL, valid, &rect, SDL_SCALEMODE_NEAREST), result = %s", result ? "true" : "false");
  1097. result = SDL_BlitSurfaceScaled(s, &rect[i], s, NULL, SDL_SCALEMODE_NEAREST);
  1098. SDLTest_AssertCheck(result == true, "SDL_BlitSurfaceScaled(valid, &rect, valid, NULL, SDL_SCALEMODE_NEAREST), result = %s", result ? "true" : "false");
  1099. }
  1100. SDL_DestroySurface(s);
  1101. return TEST_COMPLETED;
  1102. }
  1103. static int SDLCALL surface_testOverflow(void *arg)
  1104. {
  1105. char buf[1024];
  1106. const char *expectedError;
  1107. SDL_Surface *surface;
  1108. SDL_memset(buf, '\0', sizeof(buf));
  1109. expectedError = "Parameter 'width' is invalid";
  1110. surface = SDL_CreateSurface(-3, 100, SDL_PIXELFORMAT_INDEX8);
  1111. SDLTest_AssertCheck(surface == NULL, "Should detect negative width");
  1112. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1113. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1114. surface = SDL_CreateSurfaceFrom(-1, 1, SDL_PIXELFORMAT_INDEX8, buf, 4);
  1115. SDLTest_AssertCheck(surface == NULL, "Should detect negative width");
  1116. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1117. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1118. surface = SDL_CreateSurfaceFrom(-1, 1, SDL_PIXELFORMAT_RGBA8888, buf, 4);
  1119. SDLTest_AssertCheck(surface == NULL, "Should detect negative width");
  1120. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1121. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1122. expectedError = "Parameter 'height' is invalid";
  1123. surface = SDL_CreateSurface(100, -3, SDL_PIXELFORMAT_INDEX8);
  1124. SDLTest_AssertCheck(surface == NULL, "Should detect negative height");
  1125. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1126. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1127. surface = SDL_CreateSurfaceFrom(1, -1, SDL_PIXELFORMAT_INDEX8, buf, 4);
  1128. SDLTest_AssertCheck(surface == NULL, "Should detect negative height");
  1129. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1130. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1131. surface = SDL_CreateSurfaceFrom(1, -1, SDL_PIXELFORMAT_RGBA8888, buf, 4);
  1132. SDLTest_AssertCheck(surface == NULL, "Should detect negative height");
  1133. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1134. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1135. expectedError = "Parameter 'pitch' is invalid";
  1136. surface = SDL_CreateSurfaceFrom(4, 1, SDL_PIXELFORMAT_INDEX8, buf, -1);
  1137. SDLTest_AssertCheck(surface == NULL, "Should detect negative pitch");
  1138. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1139. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1140. surface = SDL_CreateSurfaceFrom(1, 1, SDL_PIXELFORMAT_RGBA8888, buf, -1);
  1141. SDLTest_AssertCheck(surface == NULL, "Should detect negative pitch");
  1142. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1143. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1144. surface = SDL_CreateSurfaceFrom(1, 1, SDL_PIXELFORMAT_RGBA8888, buf, 0);
  1145. SDLTest_AssertCheck(surface == NULL, "Should detect zero pitch");
  1146. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1147. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1148. surface = SDL_CreateSurfaceFrom(1, 1, SDL_PIXELFORMAT_RGBA8888, NULL, 0);
  1149. SDLTest_AssertCheck(surface != NULL, "Allow zero pitch for partially set up surfaces: %s",
  1150. surface != NULL ? "(success)" : SDL_GetError());
  1151. SDL_DestroySurface(surface);
  1152. /* Less than 1 byte per pixel: the pitch can legitimately be less than
  1153. * the width, but it must be enough to hold the appropriate number of
  1154. * bits per pixel. SDL_PIXELFORMAT_INDEX4* needs 1 byte per 2 pixels. */
  1155. surface = SDL_CreateSurfaceFrom(6, 1, SDL_PIXELFORMAT_INDEX4LSB, buf, 3);
  1156. SDLTest_AssertCheck(surface != NULL, "6px * 4 bits per px fits in 3 bytes: %s",
  1157. surface != NULL ? "(success)" : SDL_GetError());
  1158. SDL_DestroySurface(surface);
  1159. surface = SDL_CreateSurfaceFrom(6, 1, SDL_PIXELFORMAT_INDEX4MSB, buf, 3);
  1160. SDLTest_AssertCheck(surface != NULL, "6px * 4 bits per px fits in 3 bytes: %s",
  1161. surface != NULL ? "(success)" : SDL_GetError());
  1162. SDL_DestroySurface(surface);
  1163. surface = SDL_CreateSurfaceFrom(7, 1, SDL_PIXELFORMAT_INDEX4LSB, buf, 3);
  1164. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  1165. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1166. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1167. surface = SDL_CreateSurfaceFrom(7, 1, SDL_PIXELFORMAT_INDEX4MSB, buf, 3);
  1168. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  1169. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1170. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1171. surface = SDL_CreateSurfaceFrom(7, 1, SDL_PIXELFORMAT_INDEX4LSB, buf, 4);
  1172. SDLTest_AssertCheck(surface != NULL, "7px * 4 bits per px fits in 4 bytes: %s",
  1173. surface != NULL ? "(success)" : SDL_GetError());
  1174. SDL_DestroySurface(surface);
  1175. surface = SDL_CreateSurfaceFrom(7, 1, SDL_PIXELFORMAT_INDEX4MSB, buf, 4);
  1176. SDLTest_AssertCheck(surface != NULL, "7px * 4 bits per px fits in 4 bytes: %s",
  1177. surface != NULL ? "(success)" : SDL_GetError());
  1178. SDL_DestroySurface(surface);
  1179. /* SDL_PIXELFORMAT_INDEX2* needs 1 byte per 4 pixels. */
  1180. surface = SDL_CreateSurfaceFrom(12, 1, SDL_PIXELFORMAT_INDEX2LSB, buf, 3);
  1181. SDLTest_AssertCheck(surface != NULL, "12px * 2 bits per px fits in 3 bytes: %s",
  1182. surface != NULL ? "(success)" : SDL_GetError());
  1183. SDL_DestroySurface(surface);
  1184. surface = SDL_CreateSurfaceFrom(12, 1, SDL_PIXELFORMAT_INDEX2MSB, buf, 3);
  1185. SDLTest_AssertCheck(surface != NULL, "12px * 2 bits per px fits in 3 bytes: %s",
  1186. surface != NULL ? "(success)" : SDL_GetError());
  1187. SDL_DestroySurface(surface);
  1188. surface = SDL_CreateSurfaceFrom(13, 1, SDL_PIXELFORMAT_INDEX2LSB, buf, 3);
  1189. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp (%d)", surface ? surface->pitch : 0);
  1190. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1191. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1192. surface = SDL_CreateSurfaceFrom(13, 1, SDL_PIXELFORMAT_INDEX2MSB, buf, 3);
  1193. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  1194. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1195. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1196. surface = SDL_CreateSurfaceFrom(13, 1, SDL_PIXELFORMAT_INDEX2LSB, buf, 4);
  1197. SDLTest_AssertCheck(surface != NULL, "13px * 2 bits per px fits in 4 bytes: %s",
  1198. surface != NULL ? "(success)" : SDL_GetError());
  1199. SDL_DestroySurface(surface);
  1200. surface = SDL_CreateSurfaceFrom(13, 1, SDL_PIXELFORMAT_INDEX2MSB, buf, 4);
  1201. SDLTest_AssertCheck(surface != NULL, "13px * 2 bits per px fits in 4 bytes: %s",
  1202. surface != NULL ? "(success)" : SDL_GetError());
  1203. SDL_DestroySurface(surface);
  1204. /* SDL_PIXELFORMAT_INDEX1* needs 1 byte per 8 pixels. */
  1205. surface = SDL_CreateSurfaceFrom(16, 1, SDL_PIXELFORMAT_INDEX1LSB, buf, 2);
  1206. SDLTest_AssertCheck(surface != NULL, "16px * 1 bit per px fits in 2 bytes: %s",
  1207. surface != NULL ? "(success)" : SDL_GetError());
  1208. SDL_DestroySurface(surface);
  1209. surface = SDL_CreateSurfaceFrom(16, 1, SDL_PIXELFORMAT_INDEX1MSB, buf, 2);
  1210. SDLTest_AssertCheck(surface != NULL, "16px * 1 bit per px fits in 2 bytes: %s",
  1211. surface != NULL ? "(success)" : SDL_GetError());
  1212. SDL_DestroySurface(surface);
  1213. surface = SDL_CreateSurfaceFrom(17, 1, SDL_PIXELFORMAT_INDEX1LSB, buf, 2);
  1214. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  1215. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1216. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1217. surface = SDL_CreateSurfaceFrom(17, 1, SDL_PIXELFORMAT_INDEX1MSB, buf, 2);
  1218. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  1219. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1220. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1221. surface = SDL_CreateSurfaceFrom(17, 1, SDL_PIXELFORMAT_INDEX1LSB, buf, 3);
  1222. SDLTest_AssertCheck(surface != NULL, "17px * 1 bit per px fits in 3 bytes: %s",
  1223. surface != NULL ? "(success)" : SDL_GetError());
  1224. SDL_DestroySurface(surface);
  1225. surface = SDL_CreateSurfaceFrom(17, 1, SDL_PIXELFORMAT_INDEX1MSB, buf, 3);
  1226. SDLTest_AssertCheck(surface != NULL, "17px * 1 bit per px fits in 3 bytes: %s",
  1227. surface != NULL ? "(success)" : SDL_GetError());
  1228. SDL_DestroySurface(surface);
  1229. /* SDL_PIXELFORMAT_INDEX8 and SDL_PIXELFORMAT_RGB332 require 1 byte per pixel. */
  1230. surface = SDL_CreateSurfaceFrom(5, 1, SDL_PIXELFORMAT_RGB332, buf, 5);
  1231. SDLTest_AssertCheck(surface != NULL, "5px * 8 bits per px fits in 5 bytes: %s",
  1232. surface != NULL ? "(success)" : SDL_GetError());
  1233. SDL_DestroySurface(surface);
  1234. surface = SDL_CreateSurfaceFrom(5, 1, SDL_PIXELFORMAT_INDEX8, buf, 5);
  1235. SDLTest_AssertCheck(surface != NULL, "5px * 8 bits per px fits in 5 bytes: %s",
  1236. surface != NULL ? "(success)" : SDL_GetError());
  1237. SDL_DestroySurface(surface);
  1238. surface = SDL_CreateSurfaceFrom(6, 1, SDL_PIXELFORMAT_RGB332, buf, 5);
  1239. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  1240. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1241. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1242. surface = SDL_CreateSurfaceFrom(6, 1, SDL_PIXELFORMAT_INDEX8, buf, 5);
  1243. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  1244. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1245. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1246. /* Everything else requires more than 1 byte per pixel, and rounds up
  1247. * each pixel to an integer number of bytes (e.g. RGB555 is really
  1248. * XRGB1555, with 1 bit per pixel wasted). */
  1249. surface = SDL_CreateSurfaceFrom(3, 1, SDL_PIXELFORMAT_XRGB1555, buf, 6);
  1250. SDLTest_AssertCheck(surface != NULL, "3px * 15 (really 16) bits per px fits in 6 bytes: %s",
  1251. surface != NULL ? "(success)" : SDL_GetError());
  1252. SDL_DestroySurface(surface);
  1253. surface = SDL_CreateSurfaceFrom(3, 1, SDL_PIXELFORMAT_XRGB1555, buf, 6);
  1254. SDLTest_AssertCheck(surface != NULL, "5px * 15 (really 16) bits per px fits in 6 bytes: %s",
  1255. surface != NULL ? "(success)" : SDL_GetError());
  1256. SDL_DestroySurface(surface);
  1257. surface = SDL_CreateSurfaceFrom(4, 1, SDL_PIXELFORMAT_XRGB1555, buf, 6);
  1258. SDLTest_AssertCheck(surface == NULL, "4px * 15 (really 16) bits per px doesn't fit in 6 bytes");
  1259. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1260. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1261. surface = SDL_CreateSurfaceFrom(4, 1, SDL_PIXELFORMAT_XRGB1555, buf, 6);
  1262. SDLTest_AssertCheck(surface == NULL, "4px * 15 (really 16) bits per px doesn't fit in 6 bytes");
  1263. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1264. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1265. if (sizeof(size_t) == 4 && sizeof(int) >= 4) {
  1266. SDL_ClearError();
  1267. expectedError = "aligning pitch would overflow";
  1268. /* 0x5555'5555 * 3bpp = 0xffff'ffff which fits in size_t, but adding
  1269. * alignment padding makes it overflow */
  1270. surface = SDL_CreateSurface(0x55555555, 1, SDL_PIXELFORMAT_RGB24);
  1271. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in pitch + alignment");
  1272. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1273. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1274. SDL_ClearError();
  1275. expectedError = "width * bpp would overflow";
  1276. /* 0x4000'0000 * 4bpp = 0x1'0000'0000 which (just) overflows */
  1277. surface = SDL_CreateSurface(0x40000000, 1, SDL_PIXELFORMAT_ARGB8888);
  1278. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in width * bytes per pixel");
  1279. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1280. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1281. SDL_ClearError();
  1282. expectedError = "height * pitch would overflow";
  1283. surface = SDL_CreateSurface((1 << 29) - 1, (1 << 29) - 1, SDL_PIXELFORMAT_INDEX8);
  1284. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in width * height");
  1285. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1286. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1287. SDL_ClearError();
  1288. expectedError = "height * pitch would overflow";
  1289. surface = SDL_CreateSurface((1 << 15) + 1, (1 << 15) + 1, SDL_PIXELFORMAT_ARGB8888);
  1290. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in width * height * bytes per pixel");
  1291. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1292. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1293. } else {
  1294. SDLTest_Log("Can't easily overflow size_t on this platform");
  1295. }
  1296. return TEST_COMPLETED;
  1297. }
  1298. static int SDLCALL surface_testFlip(void *arg)
  1299. {
  1300. SDL_Surface *surface;
  1301. Uint8 *pixels;
  1302. int offset;
  1303. const char *expectedError;
  1304. surface = SDL_CreateSurface(3, 3, SDL_PIXELFORMAT_RGB24);
  1305. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  1306. SDL_ClearError();
  1307. expectedError = "Parameter 'surface' is invalid";
  1308. SDL_FlipSurface(NULL, SDL_FLIP_HORIZONTAL);
  1309. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1310. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1311. SDL_ClearError();
  1312. expectedError = "Parameter 'flip' is invalid";
  1313. SDL_FlipSurface(surface, SDL_FLIP_NONE);
  1314. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1315. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1316. pixels = (Uint8 *)surface->pixels;
  1317. *pixels = 0xFF;
  1318. offset = 0;
  1319. SDLTest_AssertPass("Call to SDL_FlipSurface(surface, SDL_FLIP_VERTICAL)");
  1320. CHECK_FUNC(SDL_FlipSurface, (surface, SDL_FLIP_VERTICAL));
  1321. SDLTest_AssertCheck(pixels[offset] == 0x00,
  1322. "Expected pixels[%d] == 0x00 got 0x%.2X", offset, pixels[offset]);
  1323. offset = 2 * surface->pitch;
  1324. SDLTest_AssertCheck(pixels[offset] == 0xFF,
  1325. "Expected pixels[%d] == 0xFF got 0x%.2X", offset, pixels[offset]);
  1326. SDLTest_AssertPass("Call to SDL_FlipSurface(surface, SDL_FLIP_HORIZONTAL)");
  1327. CHECK_FUNC(SDL_FlipSurface, (surface, SDL_FLIP_HORIZONTAL));
  1328. SDLTest_AssertCheck(pixels[offset] == 0x00,
  1329. "Expected pixels[%d] == 0x00 got 0x%.2X", offset, pixels[offset]);
  1330. offset += (surface->w - 1) * SDL_BYTESPERPIXEL(surface->format);
  1331. SDLTest_AssertCheck(pixels[offset] == 0xFF,
  1332. "Expected pixels[%d] == 0xFF got 0x%.2X", offset, pixels[offset]);
  1333. SDL_DestroySurface(surface);
  1334. return TEST_COMPLETED;
  1335. }
  1336. static int SDLCALL surface_testPalette(void *arg)
  1337. {
  1338. SDL_Surface *source, *surface, *output;
  1339. SDL_Palette *palette;
  1340. Uint8 *pixels;
  1341. palette = SDL_CreatePalette(2);
  1342. SDLTest_AssertCheck(palette != NULL, "SDL_CreatePalette()");
  1343. source = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_INDEX8);
  1344. SDLTest_AssertCheck(source != NULL, "SDL_CreateSurface()");
  1345. SDLTest_AssertCheck(SDL_GetSurfacePalette(source) == NULL, "SDL_GetSurfacePalette(source)");
  1346. surface = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_INDEX8);
  1347. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  1348. SDLTest_AssertCheck(SDL_GetSurfacePalette(surface) == NULL, "SDL_GetSurfacePalette(surface)");
  1349. pixels = (Uint8 *)surface->pixels;
  1350. SDLTest_AssertCheck(*pixels == 0, "Expected *pixels == 0 got %u", *pixels);
  1351. /* Identity copy between indexed surfaces without a palette */
  1352. *(Uint8 *)source->pixels = 1;
  1353. SDL_BlitSurface(source, NULL, surface, NULL);
  1354. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  1355. /* Identity copy between indexed surfaces where the source has a palette */
  1356. palette->colors[0].r = 0;
  1357. palette->colors[0].g = 0;
  1358. palette->colors[0].b = 0;
  1359. palette->colors[1].r = 0xFF;
  1360. palette->colors[1].g = 0;
  1361. palette->colors[1].b = 0;
  1362. SDL_SetSurfacePalette(source, palette);
  1363. *pixels = 0;
  1364. SDL_BlitSurface(source, NULL, surface, NULL);
  1365. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  1366. /* Identity copy between indexed surfaces where the destination has a palette */
  1367. palette->colors[0].r = 0;
  1368. palette->colors[0].g = 0;
  1369. palette->colors[0].b = 0;
  1370. palette->colors[1].r = 0xFF;
  1371. palette->colors[1].g = 0;
  1372. palette->colors[1].b = 0;
  1373. SDL_SetSurfacePalette(source, NULL);
  1374. SDL_SetSurfacePalette(surface, palette);
  1375. *pixels = 0;
  1376. SDL_BlitSurface(source, NULL, surface, NULL);
  1377. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  1378. /* Identity copy between indexed surfaces where the source and destination share a palette */
  1379. palette->colors[0].r = 0;
  1380. palette->colors[0].g = 0;
  1381. palette->colors[0].b = 0;
  1382. palette->colors[1].r = 0xFF;
  1383. palette->colors[1].g = 0;
  1384. palette->colors[1].b = 0;
  1385. SDL_SetSurfacePalette(source, palette);
  1386. SDL_SetSurfacePalette(surface, palette);
  1387. *pixels = 0;
  1388. SDL_BlitSurface(source, NULL, surface, NULL);
  1389. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  1390. output = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA32);
  1391. SDLTest_AssertCheck(output != NULL, "SDL_CreateSurface()");
  1392. pixels = (Uint8 *)output->pixels;
  1393. SDL_BlitSurface(surface, NULL, output, NULL);
  1394. SDLTest_AssertCheck(*pixels == 0xFF, "Expected *pixels == 0xFF got 0x%.2X", *pixels);
  1395. /* Set the palette color and blit again */
  1396. palette->colors[1].r = 0xAA;
  1397. SDL_SetSurfacePalette(surface, palette);
  1398. SDL_BlitSurface(surface, NULL, output, NULL);
  1399. SDLTest_AssertCheck(*pixels == 0xAA, "Expected *pixels == 0xAA got 0x%.2X", *pixels);
  1400. SDL_DestroyPalette(palette);
  1401. SDL_DestroySurface(source);
  1402. SDL_DestroySurface(surface);
  1403. SDL_DestroySurface(output);
  1404. return TEST_COMPLETED;
  1405. }
  1406. static int SDLCALL surface_testPalettization(void *arg)
  1407. {
  1408. const SDL_Color palette_colors[] = {
  1409. { 0x80, 0x00, 0x00, 0xff },
  1410. { 0x00, 0x80, 0x00, 0xff },
  1411. { 0x00, 0x00, 0x80, 0xff },
  1412. { 0x40, 0x00, 0x00, 0xff },
  1413. { 0x00, 0x40, 0x00, 0xff },
  1414. { 0x00, 0x00, 0x40, 0xff },
  1415. { 0x00, 0x00, 0x00, 0xff },
  1416. { 0xff, 0x00, 0x00, 0xff },
  1417. { 0x00, 0xff, 0x00, 0xff },
  1418. { 0x00, 0x00, 0xff, 0xff },
  1419. { 0xff, 0xff, 0x00, 0xff },
  1420. { 0x00, 0xff, 0xff, 0xff },
  1421. { 0xff, 0x00, 0xff, 0xff },
  1422. };
  1423. const struct {
  1424. SDL_Color c;
  1425. Uint8 e;
  1426. } colors[] = {
  1427. { { 0xff, 0x00, 0x00, 0xff }, 7 },
  1428. { { 0xfe, 0x00, 0x00, 0xff }, 7 },
  1429. { { 0xfd, 0x00, 0x00, 0xff }, 7 },
  1430. { { 0xf0, 0x00, 0x00, 0xff }, 7 },
  1431. { { 0xd0, 0x00, 0x00, 0xff }, 7 },
  1432. { { 0xb0, 0x00, 0x00, 0xff }, 0 },
  1433. { { 0xa0, 0x00, 0x00, 0xff }, 0 },
  1434. { { 0xff, 0x00, 0x00, 0x00 }, 7 },
  1435. { { 0x00, 0x10, 0x21, 0xff }, 5 },
  1436. { { 0x00, 0x10, 0x19, 0xff }, 6 },
  1437. { { 0x81, 0x00, 0x41, 0xff }, 0 },
  1438. { { 0x80, 0xf0, 0xf0, 0x7f }, 11 },
  1439. { { 0x00, 0x00, 0x00, 0xff }, 6 },
  1440. { { 0x00, 0x00, 0x00, 0x01 }, 6 },
  1441. };
  1442. int i;
  1443. int result;
  1444. SDL_Surface *source, *output;
  1445. SDL_Palette *palette;
  1446. Uint8 *pixels;
  1447. palette = SDL_CreatePalette(SDL_arraysize(palette_colors));
  1448. SDLTest_AssertCheck(palette != NULL, "SDL_CreatePalette()");
  1449. result = SDL_SetPaletteColors(palette, palette_colors, 0, SDL_arraysize(palette_colors));
  1450. SDLTest_AssertCheck(result, "SDL_SetPaletteColors()");
  1451. source = SDL_CreateSurface(SDL_arraysize(palette_colors) + SDL_arraysize(colors), 1, SDL_PIXELFORMAT_RGBA8888);
  1452. SDLTest_AssertCheck(source != NULL, "SDL_CreateSurface()");
  1453. 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);
  1454. SDLTest_AssertCheck(source->h == 1, "Expected source->h == %d, got %d", 1, source->h);
  1455. SDLTest_AssertCheck(source->format == SDL_PIXELFORMAT_RGBA8888, "Expected source->format == SDL_PIXELFORMAT_RGBA8888, got 0x%x (%s)", source->format, SDL_GetPixelFormatName(source->format));
  1456. for (i = 0; i < SDL_arraysize(colors); i++) {
  1457. result = SDL_WriteSurfacePixel(source, i, 0, colors[i].c.r, colors[i].c.g, colors[i].c.b, colors[i].c.a);
  1458. SDLTest_AssertCheck(result == true, "SDL_WriteSurfacePixel");
  1459. }
  1460. for (i = 0; i < SDL_arraysize(palette_colors); i++) {
  1461. 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);
  1462. SDLTest_AssertCheck(result == true, "SDL_WriteSurfacePixel");
  1463. }
  1464. output = SDL_ConvertSurfaceAndColorspace(source, SDL_PIXELFORMAT_INDEX8, palette, SDL_COLORSPACE_UNKNOWN, 0);
  1465. SDLTest_AssertCheck(output != NULL, "SDL_ConvertSurfaceAndColorspace()");
  1466. SDLTest_AssertCheck(output->w == source->w, "Expected output->w == %d, got %d", source->w, output->w);
  1467. SDLTest_AssertCheck(output->h == source->h, "Expected output->h == %d, got %d", source->h, output->h);
  1468. SDLTest_AssertCheck(output->format == SDL_PIXELFORMAT_INDEX8, "Expected output->format == SDL_PIXELFORMAT_INDEX8, got 0x%x (%s)", output->format, SDL_GetPixelFormatName(output->format));
  1469. pixels = output->pixels;
  1470. for (i = 0; i < SDL_arraysize(colors); i++) {
  1471. int idx = i;
  1472. Uint8 actual = pixels[idx];
  1473. Uint8 expected = colors[i].e;
  1474. SDLTest_AssertCheck(actual < SDL_arraysize(palette_colors), "output->pixels[%d] < %d", idx, (int)SDL_arraysize(palette_colors));
  1475. SDLTest_AssertCheck(actual == expected, "Expected output->pixels[%d] == %u, got %u", idx, expected, actual);
  1476. }
  1477. SDLTest_AssertPass("Check palette 1:1 mapping");
  1478. for (i = 0; i < SDL_arraysize(palette_colors); i++) {
  1479. int idx = SDL_arraysize(colors) + i;
  1480. Uint8 actual = pixels[idx];
  1481. Uint8 expected = i;
  1482. SDLTest_AssertCheck(actual < SDL_arraysize(palette_colors), "output->pixels[%d] < %d", idx, (int)SDL_arraysize(palette_colors));
  1483. SDLTest_AssertCheck(actual == expected, "Expected output->pixels[%d] == %u, got %u", idx, expected, actual);
  1484. }
  1485. SDL_DestroyPalette(palette);
  1486. SDL_DestroySurface(source);
  1487. SDL_DestroySurface(output);
  1488. return TEST_COMPLETED;
  1489. }
  1490. static int SDLCALL surface_testClearSurface(void *arg)
  1491. {
  1492. SDL_PixelFormat formats[] = {
  1493. SDL_PIXELFORMAT_ARGB8888, SDL_PIXELFORMAT_RGBA8888,
  1494. SDL_PIXELFORMAT_ARGB2101010, SDL_PIXELFORMAT_ABGR2101010,
  1495. SDL_PIXELFORMAT_ARGB64, SDL_PIXELFORMAT_RGBA64,
  1496. SDL_PIXELFORMAT_ARGB128_FLOAT, SDL_PIXELFORMAT_RGBA128_FLOAT,
  1497. SDL_PIXELFORMAT_YV12, SDL_PIXELFORMAT_UYVY, SDL_PIXELFORMAT_NV12
  1498. };
  1499. SDL_Surface *surface;
  1500. SDL_PixelFormat format;
  1501. const float MAXIMUM_ERROR_RGB = 0.0001f;
  1502. const float MAXIMUM_ERROR_YUV = 0.01f;
  1503. float srcR = 10 / 255.0f, srcG = 128 / 255.0f, srcB = 240 / 255.0f, srcA = 1.0f;
  1504. float actualR, actualG, actualB, actualA;
  1505. float deltaR, deltaG, deltaB, deltaA;
  1506. int i, ret;
  1507. for (i = 0; i < SDL_arraysize(formats); ++i) {
  1508. const float MAXIMUM_ERROR = SDL_ISPIXELFORMAT_FOURCC(formats[i]) ? MAXIMUM_ERROR_YUV : MAXIMUM_ERROR_RGB;
  1509. format = formats[i];
  1510. surface = SDL_CreateSurface(1, 1, format);
  1511. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  1512. ret = SDL_ClearSurface(surface, srcR, srcG, srcB, srcA);
  1513. SDLTest_AssertCheck(ret == true, "SDL_ClearSurface()");
  1514. ret = SDL_ReadSurfacePixelFloat(surface, 0, 0, &actualR, &actualG, &actualB, &actualA);
  1515. SDLTest_AssertCheck(ret == true, "SDL_ReadSurfacePixelFloat()");
  1516. deltaR = SDL_fabsf(actualR - srcR);
  1517. deltaG = SDL_fabsf(actualG - srcG);
  1518. deltaB = SDL_fabsf(actualB - srcB);
  1519. deltaA = SDL_fabsf(actualA - srcA);
  1520. SDLTest_AssertCheck(
  1521. deltaR <= MAXIMUM_ERROR &&
  1522. deltaG <= MAXIMUM_ERROR &&
  1523. deltaB <= MAXIMUM_ERROR &&
  1524. deltaA <= MAXIMUM_ERROR,
  1525. "Checking %s surface clear results, expected %.4f,%.4f,%.4f,%.4f, got %.4f,%.4f,%.4f,%.4f",
  1526. SDL_GetPixelFormatName(format),
  1527. srcR, srcG, srcB, srcA, actualR, actualG, actualB, actualA);
  1528. SDL_DestroySurface(surface);
  1529. }
  1530. return TEST_COMPLETED;
  1531. }
  1532. static int SDLCALL surface_testPremultiplyAlpha(void *arg)
  1533. {
  1534. SDL_PixelFormat formats[] = {
  1535. SDL_PIXELFORMAT_ARGB8888, SDL_PIXELFORMAT_RGBA8888,
  1536. SDL_PIXELFORMAT_ARGB2101010, SDL_PIXELFORMAT_ABGR2101010,
  1537. SDL_PIXELFORMAT_ARGB64, SDL_PIXELFORMAT_RGBA64,
  1538. SDL_PIXELFORMAT_ARGB128_FLOAT, SDL_PIXELFORMAT_RGBA128_FLOAT,
  1539. };
  1540. SDL_Surface *surface;
  1541. SDL_PixelFormat format;
  1542. const float MAXIMUM_ERROR_LOW_PRECISION = 1 / 255.0f;
  1543. const float MAXIMUM_ERROR_HIGH_PRECISION = 0.0001f;
  1544. float srcR = 10 / 255.0f, srcG = 128 / 255.0f, srcB = 240 / 255.0f, srcA = 170 / 255.0f;
  1545. float expectedR = srcR * srcA;
  1546. float expectedG = srcG * srcA;
  1547. float expectedB = srcB * srcA;
  1548. float actualR, actualG, actualB;
  1549. float deltaR, deltaG, deltaB;
  1550. int i, ret;
  1551. for (i = 0; i < SDL_arraysize(formats); ++i) {
  1552. const float MAXIMUM_ERROR = (SDL_BITSPERPIXEL(formats[i]) > 32) ? MAXIMUM_ERROR_HIGH_PRECISION : MAXIMUM_ERROR_LOW_PRECISION;
  1553. format = formats[i];
  1554. surface = SDL_CreateSurface(1, 1, format);
  1555. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  1556. ret = SDL_SetSurfaceColorspace(surface, SDL_COLORSPACE_SRGB);
  1557. SDLTest_AssertCheck(ret == true, "SDL_SetSurfaceColorspace()");
  1558. ret = SDL_ClearSurface(surface, srcR, srcG, srcB, srcA);
  1559. SDLTest_AssertCheck(ret == true, "SDL_ClearSurface()");
  1560. ret = SDL_PremultiplySurfaceAlpha(surface, false);
  1561. SDLTest_AssertCheck(ret == true, "SDL_PremultiplySurfaceAlpha()");
  1562. ret = SDL_ReadSurfacePixelFloat(surface, 0, 0, &actualR, &actualG, &actualB, NULL);
  1563. SDLTest_AssertCheck(ret == true, "SDL_ReadSurfacePixelFloat()");
  1564. deltaR = SDL_fabsf(actualR - expectedR);
  1565. deltaG = SDL_fabsf(actualG - expectedG);
  1566. deltaB = SDL_fabsf(actualB - expectedB);
  1567. SDLTest_AssertCheck(
  1568. deltaR <= MAXIMUM_ERROR &&
  1569. deltaG <= MAXIMUM_ERROR &&
  1570. deltaB <= MAXIMUM_ERROR,
  1571. "Checking %s alpha premultiply results, expected %.4f,%.4f,%.4f, got %.4f,%.4f,%.4f",
  1572. SDL_GetPixelFormatName(format),
  1573. expectedR, expectedG, expectedB, actualR, actualG, actualB);
  1574. SDL_DestroySurface(surface);
  1575. }
  1576. return TEST_COMPLETED;
  1577. }
  1578. static int SDLCALL surface_testScale(void *arg)
  1579. {
  1580. SDL_PixelFormat formats[] = {
  1581. SDL_PIXELFORMAT_ARGB8888, SDL_PIXELFORMAT_RGBA8888,
  1582. SDL_PIXELFORMAT_ARGB2101010, SDL_PIXELFORMAT_ABGR2101010,
  1583. SDL_PIXELFORMAT_ARGB64, SDL_PIXELFORMAT_RGBA64,
  1584. SDL_PIXELFORMAT_ARGB128_FLOAT, SDL_PIXELFORMAT_RGBA128_FLOAT,
  1585. };
  1586. SDL_ScaleMode modes[] = {
  1587. SDL_SCALEMODE_NEAREST, SDL_SCALEMODE_LINEAR, SDL_SCALEMODE_PIXELART
  1588. };
  1589. SDL_Surface *surface, *result;
  1590. SDL_PixelFormat format;
  1591. SDL_ScaleMode mode;
  1592. const float MAXIMUM_ERROR = 0.0001f;
  1593. float srcR = 10 / 255.0f, srcG = 128 / 255.0f, srcB = 240 / 255.0f, srcA = 170 / 255.0f;
  1594. float actualR, actualG, actualB, actualA;
  1595. float deltaR, deltaG, deltaB, deltaA;
  1596. int i, j, ret;
  1597. for (i = 0; i < SDL_arraysize(formats); ++i) {
  1598. for (j = 0; j < SDL_arraysize(modes); ++j) {
  1599. format = formats[i];
  1600. mode = modes[j];
  1601. surface = SDL_CreateSurface(1, 1, format);
  1602. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  1603. ret = SDL_SetSurfaceColorspace(surface, SDL_COLORSPACE_SRGB);
  1604. SDLTest_AssertCheck(ret == true, "SDL_SetSurfaceColorspace()");
  1605. ret = SDL_ClearSurface(surface, srcR, srcG, srcB, srcA);
  1606. SDLTest_AssertCheck(ret == true, "SDL_ClearSurface()");
  1607. result = SDL_ScaleSurface(surface, 2, 2, mode);
  1608. SDLTest_AssertCheck(ret == true, "SDL_PremultiplySurfaceAlpha()");
  1609. ret = SDL_ReadSurfacePixelFloat(result, 1, 1, &actualR, &actualG, &actualB, &actualA);
  1610. SDLTest_AssertCheck(ret == true, "SDL_ReadSurfacePixelFloat()");
  1611. deltaR = SDL_fabsf(actualR - srcR);
  1612. deltaG = SDL_fabsf(actualG - srcG);
  1613. deltaB = SDL_fabsf(actualB - srcB);
  1614. deltaA = SDL_fabsf(actualA - srcA);
  1615. SDLTest_AssertCheck(
  1616. deltaR <= MAXIMUM_ERROR &&
  1617. deltaG <= MAXIMUM_ERROR &&
  1618. deltaB <= MAXIMUM_ERROR &&
  1619. deltaA <= MAXIMUM_ERROR,
  1620. "Checking %s %s scaling results, expected %.4f,%.4f,%.4f,%.4f got %.4f,%.4f,%.4f,%.4f",
  1621. SDL_GetPixelFormatName(format),
  1622. mode == SDL_SCALEMODE_NEAREST ? "nearest" :
  1623. mode == SDL_SCALEMODE_LINEAR ? "linear" :
  1624. mode == SDL_SCALEMODE_PIXELART ? "pixelart" : "unknown",
  1625. srcR, srcG, srcB, srcA, actualR, actualG, actualB, actualA);
  1626. SDL_DestroySurface(surface);
  1627. SDL_DestroySurface(result);
  1628. }
  1629. }
  1630. return TEST_COMPLETED;
  1631. }
  1632. #define GENERATE_SHIFTS
  1633. static Uint32 Calculate(int v, int bits, int vmax, int shift)
  1634. {
  1635. #if defined(GENERATE_FLOOR)
  1636. return (Uint32)SDL_floor(v * 255.0f / vmax) << shift;
  1637. #elif defined(GENERATE_ROUND)
  1638. return (Uint32)SDL_roundf(v * 255.0f / vmax) << shift;
  1639. #elif defined(GENERATE_SHIFTS)
  1640. switch (bits) {
  1641. case 1:
  1642. v = (v << 7) | (v << 6) | (v << 5) | (v << 4) | (v << 3) | (v << 2) | (v << 1) | v;
  1643. break;
  1644. case 2:
  1645. v = (v << 6) | (v << 4) | (v << 2) | v;
  1646. break;
  1647. case 3:
  1648. v = (v << 5) | (v << 2) | (v >> 1);
  1649. break;
  1650. case 4:
  1651. v = (v << 4) | v;
  1652. break;
  1653. case 5:
  1654. v = (v << 3) | (v >> 2);
  1655. break;
  1656. case 6:
  1657. v = (v << 2) | (v >> 4);
  1658. break;
  1659. case 7:
  1660. v = (v << 1) | (v >> 6);
  1661. break;
  1662. case 8:
  1663. break;
  1664. }
  1665. return (Uint32)v << shift;
  1666. #endif
  1667. }
  1668. static Uint32 Calculate565toARGB(int v, const SDL_PixelFormatDetails *fmt)
  1669. {
  1670. Uint8 r = (v & 0xF800) >> 11;
  1671. Uint8 g = (v & 0x07E0) >> 5;
  1672. Uint8 b = (v & 0x001F);
  1673. return fmt->Amask |
  1674. Calculate(r, 5, 31, fmt->Rshift) |
  1675. Calculate(g, 6, 63, fmt->Gshift) |
  1676. Calculate(b, 5, 31, fmt->Bshift);
  1677. }
  1678. static int SDLCALL surface_test16BitTo32Bit(void *arg)
  1679. {
  1680. static const SDL_PixelFormat formats[] = {
  1681. SDL_PIXELFORMAT_ARGB8888,
  1682. SDL_PIXELFORMAT_ABGR8888,
  1683. SDL_PIXELFORMAT_RGBA8888,
  1684. SDL_PIXELFORMAT_BGRA8888
  1685. };
  1686. static Uint16 pixels[1 << 16];
  1687. static Uint32 expected[1 << 16];
  1688. int i, p, ret;
  1689. SDL_Surface *surface16;
  1690. SDL_Surface *surface32;
  1691. SDL_Surface *expected32;
  1692. for (p = 0; p < SDL_arraysize(pixels); ++p) {
  1693. pixels[p] = p;
  1694. }
  1695. surface16 = SDL_CreateSurfaceFrom(SDL_arraysize(pixels), 1, SDL_PIXELFORMAT_RGB565, pixels, sizeof(pixels));
  1696. for (i = 0; i < SDL_arraysize(formats); ++i) {
  1697. SDL_PixelFormat format = formats[i];
  1698. const SDL_PixelFormatDetails *fmt = SDL_GetPixelFormatDetails(format);
  1699. SDLTest_Log("Checking conversion from SDL_PIXELFORMAT_RGB565 to %s", SDL_GetPixelFormatName(format));
  1700. surface32 = SDL_ConvertSurface(surface16, format);
  1701. for (p = 0; p < SDL_arraysize(pixels); ++p) {
  1702. expected[p] = Calculate565toARGB(p, fmt);
  1703. }
  1704. expected32 = SDL_CreateSurfaceFrom(SDL_arraysize(expected), 1, format, expected, sizeof(expected));
  1705. ret = SDLTest_CompareSurfaces(surface32, expected32, 0);
  1706. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  1707. SDL_DestroySurface(surface32);
  1708. SDL_DestroySurface(expected32);
  1709. }
  1710. SDL_DestroySurface(surface16);
  1711. return TEST_COMPLETED;
  1712. }
  1713. /* ================= Test References ================== */
  1714. /* Surface test cases */
  1715. static const SDLTest_TestCaseReference surfaceTestInvalidFormat = {
  1716. surface_testInvalidFormat, "surface_testInvalidFormat", "Tests creating surface with invalid format", TEST_ENABLED
  1717. };
  1718. static const SDLTest_TestCaseReference surfaceTestSaveLoad = {
  1719. surface_testSaveLoad, "surface_testSaveLoad", "Tests sprite saving and loading.", TEST_ENABLED
  1720. };
  1721. static const SDLTest_TestCaseReference surfaceTestBlitZeroSource = {
  1722. surface_testBlitZeroSource, "surface_testBlitZeroSource", "Tests blitting from a zero sized source rectangle", TEST_ENABLED
  1723. };
  1724. static const SDLTest_TestCaseReference surfaceTestBlit = {
  1725. surface_testBlit, "surface_testBlit", "Tests basic blitting.", TEST_ENABLED
  1726. };
  1727. static const SDLTest_TestCaseReference surfaceTestBlitTiled = {
  1728. surface_testBlitTiled, "surface_testBlitTiled", "Tests tiled blitting.", TEST_ENABLED
  1729. };
  1730. static const SDLTest_TestCaseReference surfaceTestBlit9Grid = {
  1731. surface_testBlit9Grid, "surface_testBlit9Grid", "Tests 9-grid blitting.", TEST_ENABLED
  1732. };
  1733. static const SDLTest_TestCaseReference surfaceTestBlitMultiple = {
  1734. surface_testBlitMultiple, "surface_testBlitMultiple", "Tests blitting between multiple surfaces of the same format.", TEST_ENABLED
  1735. };
  1736. static const SDLTest_TestCaseReference surfaceTestLoadFailure = {
  1737. surface_testLoadFailure, "surface_testLoadFailure", "Tests sprite loading. A failure case.", TEST_ENABLED
  1738. };
  1739. static const SDLTest_TestCaseReference surfaceTestNULLPixels = {
  1740. surface_testSurfaceNULLPixels, "surface_testSurfaceNULLPixels", "Tests surface operations with NULL pixels.", TEST_ENABLED
  1741. };
  1742. static const SDLTest_TestCaseReference surfaceTestRLEPixels = {
  1743. surface_testSurfaceRLEPixels, "surface_testSurfaceRLEPixels", "Tests surface operations with RLE surfaces.", TEST_ENABLED
  1744. };
  1745. static const SDLTest_TestCaseReference surfaceTestSurfaceConversion = {
  1746. surface_testSurfaceConversion, "surface_testSurfaceConversion", "Tests surface conversion.", TEST_ENABLED
  1747. };
  1748. static const SDLTest_TestCaseReference surfaceTestCompleteSurfaceConversion = {
  1749. surface_testCompleteSurfaceConversion, "surface_testCompleteSurfaceConversion", "Tests surface conversion across all pixel formats", TEST_ENABLED
  1750. };
  1751. static const SDLTest_TestCaseReference surfaceTestBlitColorMod = {
  1752. surface_testBlitColorMod, "surface_testBlitColorMod", "Tests some blitting routines with color mod.", TEST_ENABLED
  1753. };
  1754. static const SDLTest_TestCaseReference surfaceTestBlitAlphaMod = {
  1755. surface_testBlitAlphaMod, "surface_testBlitAlphaMod", "Tests some blitting routines with alpha mod.", TEST_ENABLED
  1756. };
  1757. static const SDLTest_TestCaseReference surfaceTestBlitBlendBlend = {
  1758. surface_testBlitBlendBlend, "surface_testBlitBlendBlend", "Tests blitting routines with blend blending mode.", TEST_ENABLED
  1759. };
  1760. static const SDLTest_TestCaseReference surfaceTestBlitBlendPremultiplied = {
  1761. surface_testBlitBlendPremultiplied, "surface_testBlitBlendPremultiplied", "Tests blitting routines with premultiplied blending mode.", TEST_ENABLED
  1762. };
  1763. static const SDLTest_TestCaseReference surfaceTestBlitBlendAdd = {
  1764. surface_testBlitBlendAdd, "surface_testBlitBlendAdd", "Tests blitting routines with add blending mode.", TEST_ENABLED
  1765. };
  1766. static const SDLTest_TestCaseReference surfaceTestBlitBlendAddPremultiplied = {
  1767. surface_testBlitBlendAddPremultiplied, "surface_testBlitBlendAddPremultiplied", "Tests blitting routines with premultiplied add blending mode.", TEST_ENABLED
  1768. };
  1769. static const SDLTest_TestCaseReference surfaceTestBlitBlendMod = {
  1770. surface_testBlitBlendMod, "surface_testBlitBlendMod", "Tests blitting routines with mod blending mode.", TEST_ENABLED
  1771. };
  1772. static const SDLTest_TestCaseReference surfaceTestBlitBlendMul = {
  1773. surface_testBlitBlendMul, "surface_testBlitBlendMul", "Tests blitting routines with mul blending mode.", TEST_ENABLED
  1774. };
  1775. static const SDLTest_TestCaseReference surfaceTestBlitInvalid = {
  1776. surface_testBlitInvalid, "surface_testBlitInvalid", "Tests blitting routines with invalid surfaces.", TEST_ENABLED
  1777. };
  1778. static const SDLTest_TestCaseReference surfaceTestBlitsWithBadCoordinates = {
  1779. surface_testBlitsWithBadCoordinates, "surface_testBlitsWithBadCoordinates", "Test blitting routines with bad coordinates.", TEST_ENABLED
  1780. };
  1781. static const SDLTest_TestCaseReference surfaceTestOverflow = {
  1782. surface_testOverflow, "surface_testOverflow", "Test overflow detection.", TEST_ENABLED
  1783. };
  1784. static const SDLTest_TestCaseReference surfaceTestFlip = {
  1785. surface_testFlip, "surface_testFlip", "Test surface flipping.", TEST_ENABLED
  1786. };
  1787. static const SDLTest_TestCaseReference surfaceTestPalette = {
  1788. surface_testPalette, "surface_testPalette", "Test surface palette operations.", TEST_ENABLED
  1789. };
  1790. static const SDLTest_TestCaseReference surfaceTestPalettization = {
  1791. surface_testPalettization, "surface_testPalettization", "Test surface palettization.", TEST_ENABLED
  1792. };
  1793. static const SDLTest_TestCaseReference surfaceTestClearSurface = {
  1794. surface_testClearSurface, "surface_testClearSurface", "Test clear surface operations.", TEST_ENABLED
  1795. };
  1796. static const SDLTest_TestCaseReference surfaceTestPremultiplyAlpha = {
  1797. surface_testPremultiplyAlpha, "surface_testPremultiplyAlpha", "Test alpha premultiply operations.", TEST_ENABLED
  1798. };
  1799. static const SDLTest_TestCaseReference surfaceTestScale = {
  1800. surface_testScale, "surface_testScale", "Test scaling operations.", TEST_ENABLED
  1801. };
  1802. static const SDLTest_TestCaseReference surfaceTest16BitTo32Bit = {
  1803. surface_test16BitTo32Bit, "surface_test16BitTo32Bit", "Test conversion from 16-bit to 32-bit pixels.", TEST_ENABLED
  1804. };
  1805. /* Sequence of Surface test cases */
  1806. static const SDLTest_TestCaseReference *surfaceTests[] = {
  1807. &surfaceTestInvalidFormat,
  1808. &surfaceTestSaveLoad,
  1809. &surfaceTestBlitZeroSource,
  1810. &surfaceTestBlit,
  1811. &surfaceTestBlitTiled,
  1812. &surfaceTestBlit9Grid,
  1813. &surfaceTestBlitMultiple,
  1814. &surfaceTestLoadFailure,
  1815. &surfaceTestNULLPixels,
  1816. &surfaceTestRLEPixels,
  1817. &surfaceTestSurfaceConversion,
  1818. &surfaceTestCompleteSurfaceConversion,
  1819. &surfaceTestBlitColorMod,
  1820. &surfaceTestBlitAlphaMod,
  1821. &surfaceTestBlitBlendBlend,
  1822. &surfaceTestBlitBlendPremultiplied,
  1823. &surfaceTestBlitBlendAdd,
  1824. &surfaceTestBlitBlendAddPremultiplied,
  1825. &surfaceTestBlitBlendMod,
  1826. &surfaceTestBlitBlendMul,
  1827. &surfaceTestBlitInvalid,
  1828. &surfaceTestBlitsWithBadCoordinates,
  1829. &surfaceTestOverflow,
  1830. &surfaceTestFlip,
  1831. &surfaceTestPalette,
  1832. &surfaceTestPalettization,
  1833. &surfaceTestClearSurface,
  1834. &surfaceTestPremultiplyAlpha,
  1835. &surfaceTestScale,
  1836. &surfaceTest16BitTo32Bit,
  1837. NULL
  1838. };
  1839. /* Surface test suite (global) */
  1840. SDLTest_TestSuiteReference surfaceTestSuite = {
  1841. "Surface",
  1842. surfaceSetUp,
  1843. surfaceTests,
  1844. surfaceTearDown
  1845. };