testautomation_surface.c 50 KB

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