Atlas - testautomation_surface.c

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