SDL_surface.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../SDL_internal.h"
  19. #include "SDL_video.h"
  20. #include "SDL_sysvideo.h"
  21. #include "SDL_blit.h"
  22. #include "SDL_RLEaccel_c.h"
  23. #include "SDL_pixels_c.h"
  24. #include "SDL_yuv_c.h"
  25. /* Check to make sure we can safely check multiplication of surface w and pitch and it won't overflow size_t */
  26. SDL_COMPILE_TIME_ASSERT(surface_size_assumptions,
  27. sizeof(int) == sizeof(Sint32) && sizeof(size_t) >= sizeof(Sint32));
  28. /* Public routines */
  29. /*
  30. * Calculate the pad-aligned scanline width of a surface
  31. */
  32. static int
  33. SDL_CalculatePitch(Uint32 format, int width)
  34. {
  35. int pitch;
  36. /* Surface should be 4-byte aligned for speed */
  37. pitch = width * SDL_BYTESPERPIXEL(format);
  38. switch (SDL_BITSPERPIXEL(format)) {
  39. case 1:
  40. pitch = (pitch + 7) / 8;
  41. break;
  42. case 4:
  43. pitch = (pitch + 1) / 2;
  44. break;
  45. default:
  46. break;
  47. }
  48. pitch = (pitch + 3) & ~3; /* 4-byte aligning */
  49. return pitch;
  50. }
  51. /*
  52. * Create an empty RGB surface of the appropriate depth using the given
  53. * enum SDL_PIXELFORMAT_* format
  54. */
  55. SDL_Surface *
  56. SDL_CreateRGBSurfaceWithFormat(Uint32 flags, int width, int height, int depth,
  57. Uint32 format)
  58. {
  59. SDL_Surface *surface;
  60. /* The flags are no longer used, make the compiler happy */
  61. (void)flags;
  62. /* Allocate the surface */
  63. surface = (SDL_Surface *) SDL_calloc(1, sizeof(*surface));
  64. if (surface == NULL) {
  65. SDL_OutOfMemory();
  66. return NULL;
  67. }
  68. surface->format = SDL_AllocFormat(format);
  69. if (!surface->format) {
  70. SDL_FreeSurface(surface);
  71. return NULL;
  72. }
  73. surface->w = width;
  74. surface->h = height;
  75. surface->pitch = SDL_CalculatePitch(format, width);
  76. SDL_SetClipRect(surface, NULL);
  77. if (SDL_ISPIXELFORMAT_INDEXED(surface->format->format)) {
  78. SDL_Palette *palette =
  79. SDL_AllocPalette((1 << surface->format->BitsPerPixel));
  80. if (!palette) {
  81. SDL_FreeSurface(surface);
  82. return NULL;
  83. }
  84. if (palette->ncolors == 2) {
  85. /* Create a black and white bitmap palette */
  86. palette->colors[0].r = 0xFF;
  87. palette->colors[0].g = 0xFF;
  88. palette->colors[0].b = 0xFF;
  89. palette->colors[1].r = 0x00;
  90. palette->colors[1].g = 0x00;
  91. palette->colors[1].b = 0x00;
  92. }
  93. SDL_SetSurfacePalette(surface, palette);
  94. SDL_FreePalette(palette);
  95. }
  96. /* Get the pixels */
  97. if (surface->w && surface->h) {
  98. /* Assumptions checked in surface_size_assumptions assert above */
  99. Sint64 size = ((Sint64)surface->h * surface->pitch);
  100. if (size < 0 || size > SDL_MAX_SINT32) {
  101. /* Overflow... */
  102. SDL_FreeSurface(surface);
  103. SDL_OutOfMemory();
  104. return NULL;
  105. }
  106. surface->pixels = SDL_malloc((size_t)size);
  107. if (!surface->pixels) {
  108. SDL_FreeSurface(surface);
  109. SDL_OutOfMemory();
  110. return NULL;
  111. }
  112. /* This is important for bitmaps */
  113. SDL_memset(surface->pixels, 0, surface->h * surface->pitch);
  114. }
  115. /* Allocate an empty mapping */
  116. surface->map = SDL_AllocBlitMap();
  117. if (!surface->map) {
  118. SDL_FreeSurface(surface);
  119. return NULL;
  120. }
  121. /* By default surface with an alpha mask are set up for blending */
  122. if (surface->format->Amask) {
  123. SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
  124. }
  125. /* The surface is ready to go */
  126. surface->refcount = 1;
  127. return surface;
  128. }
  129. /*
  130. * Create an empty RGB surface of the appropriate depth
  131. */
  132. SDL_Surface *
  133. SDL_CreateRGBSurface(Uint32 flags,
  134. int width, int height, int depth,
  135. Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
  136. {
  137. Uint32 format;
  138. /* Get the pixel format */
  139. format = SDL_MasksToPixelFormatEnum(depth, Rmask, Gmask, Bmask, Amask);
  140. if (format == SDL_PIXELFORMAT_UNKNOWN) {
  141. SDL_SetError("Unknown pixel format");
  142. return NULL;
  143. }
  144. return SDL_CreateRGBSurfaceWithFormat(flags, width, height, depth, format);
  145. }
  146. /*
  147. * Create an RGB surface from an existing memory buffer
  148. */
  149. SDL_Surface *
  150. SDL_CreateRGBSurfaceFrom(void *pixels,
  151. int width, int height, int depth, int pitch,
  152. Uint32 Rmask, Uint32 Gmask, Uint32 Bmask,
  153. Uint32 Amask)
  154. {
  155. SDL_Surface *surface;
  156. surface = SDL_CreateRGBSurface(0, 0, 0, depth, Rmask, Gmask, Bmask, Amask);
  157. if (surface != NULL) {
  158. surface->flags |= SDL_PREALLOC;
  159. surface->pixels = pixels;
  160. surface->w = width;
  161. surface->h = height;
  162. surface->pitch = pitch;
  163. SDL_SetClipRect(surface, NULL);
  164. }
  165. return surface;
  166. }
  167. /*
  168. * Create an RGB surface from an existing memory buffer using the given given
  169. * enum SDL_PIXELFORMAT_* format
  170. */
  171. SDL_Surface *
  172. SDL_CreateRGBSurfaceWithFormatFrom(void *pixels,
  173. int width, int height, int depth, int pitch,
  174. Uint32 format)
  175. {
  176. SDL_Surface *surface;
  177. surface = SDL_CreateRGBSurfaceWithFormat(0, 0, 0, depth, format);
  178. if (surface != NULL) {
  179. surface->flags |= SDL_PREALLOC;
  180. surface->pixels = pixels;
  181. surface->w = width;
  182. surface->h = height;
  183. surface->pitch = pitch;
  184. SDL_SetClipRect(surface, NULL);
  185. }
  186. return surface;
  187. }
  188. int
  189. SDL_SetSurfacePalette(SDL_Surface * surface, SDL_Palette * palette)
  190. {
  191. if (!surface) {
  192. return SDL_SetError("SDL_SetSurfacePalette() passed a NULL surface");
  193. }
  194. if (SDL_SetPixelFormatPalette(surface->format, palette) < 0) {
  195. return -1;
  196. }
  197. SDL_InvalidateMap(surface->map);
  198. return 0;
  199. }
  200. int
  201. SDL_SetSurfaceRLE(SDL_Surface * surface, int flag)
  202. {
  203. int flags;
  204. if (!surface) {
  205. return -1;
  206. }
  207. flags = surface->map->info.flags;
  208. if (flag) {
  209. surface->map->info.flags |= SDL_COPY_RLE_DESIRED;
  210. } else {
  211. surface->map->info.flags &= ~SDL_COPY_RLE_DESIRED;
  212. }
  213. if (surface->map->info.flags != flags) {
  214. SDL_InvalidateMap(surface->map);
  215. }
  216. return 0;
  217. }
  218. int
  219. SDL_SetColorKey(SDL_Surface * surface, int flag, Uint32 key)
  220. {
  221. int flags;
  222. if (!surface) {
  223. return SDL_InvalidParamError("surface");
  224. }
  225. if (surface->format->palette && key >= ((Uint32) surface->format->palette->ncolors)) {
  226. return SDL_InvalidParamError("key");
  227. }
  228. if (flag & SDL_RLEACCEL) {
  229. SDL_SetSurfaceRLE(surface, 1);
  230. }
  231. flags = surface->map->info.flags;
  232. if (flag) {
  233. surface->map->info.flags |= SDL_COPY_COLORKEY;
  234. surface->map->info.colorkey = key;
  235. } else {
  236. surface->map->info.flags &= ~SDL_COPY_COLORKEY;
  237. }
  238. if (surface->map->info.flags != flags) {
  239. SDL_InvalidateMap(surface->map);
  240. }
  241. return 0;
  242. }
  243. SDL_bool
  244. SDL_HasColorKey(SDL_Surface * surface)
  245. {
  246. if (!surface) {
  247. return SDL_FALSE;
  248. }
  249. if (!(surface->map->info.flags & SDL_COPY_COLORKEY)) {
  250. return SDL_FALSE;
  251. }
  252. return SDL_TRUE;
  253. }
  254. int
  255. SDL_GetColorKey(SDL_Surface * surface, Uint32 * key)
  256. {
  257. if (!surface) {
  258. return SDL_InvalidParamError("surface");
  259. }
  260. if (!(surface->map->info.flags & SDL_COPY_COLORKEY)) {
  261. return SDL_SetError("Surface doesn't have a colorkey");
  262. }
  263. if (key) {
  264. *key = surface->map->info.colorkey;
  265. }
  266. return 0;
  267. }
  268. /* This is a fairly slow function to switch from colorkey to alpha */
  269. static void
  270. SDL_ConvertColorkeyToAlpha(SDL_Surface * surface, SDL_bool ignore_alpha)
  271. {
  272. int x, y;
  273. if (!surface) {
  274. return;
  275. }
  276. if (!(surface->map->info.flags & SDL_COPY_COLORKEY) ||
  277. !surface->format->Amask) {
  278. return;
  279. }
  280. SDL_LockSurface(surface);
  281. switch (surface->format->BytesPerPixel) {
  282. case 2:
  283. {
  284. Uint16 *row, *spot;
  285. Uint16 ckey = (Uint16) surface->map->info.colorkey;
  286. Uint16 mask = (Uint16) (~surface->format->Amask);
  287. /* Ignore, or not, alpha in colorkey comparison */
  288. if (ignore_alpha) {
  289. ckey &= mask;
  290. row = (Uint16 *) surface->pixels;
  291. for (y = surface->h; y--;) {
  292. spot = row;
  293. for (x = surface->w; x--;) {
  294. if ((*spot & mask) == ckey) {
  295. *spot &= mask;
  296. }
  297. ++spot;
  298. }
  299. row += surface->pitch / 2;
  300. }
  301. } else {
  302. row = (Uint16 *) surface->pixels;
  303. for (y = surface->h; y--;) {
  304. spot = row;
  305. for (x = surface->w; x--;) {
  306. if (*spot == ckey) {
  307. *spot &= mask;
  308. }
  309. ++spot;
  310. }
  311. row += surface->pitch / 2;
  312. }
  313. }
  314. }
  315. break;
  316. case 3:
  317. /* FIXME */
  318. break;
  319. case 4:
  320. {
  321. Uint32 *row, *spot;
  322. Uint32 ckey = surface->map->info.colorkey;
  323. Uint32 mask = ~surface->format->Amask;
  324. /* Ignore, or not, alpha in colorkey comparison */
  325. if (ignore_alpha) {
  326. ckey &= mask;
  327. row = (Uint32 *) surface->pixels;
  328. for (y = surface->h; y--;) {
  329. spot = row;
  330. for (x = surface->w; x--;) {
  331. if ((*spot & mask) == ckey) {
  332. *spot &= mask;
  333. }
  334. ++spot;
  335. }
  336. row += surface->pitch / 4;
  337. }
  338. } else {
  339. row = (Uint32 *) surface->pixels;
  340. for (y = surface->h; y--;) {
  341. spot = row;
  342. for (x = surface->w; x--;) {
  343. if (*spot == ckey) {
  344. *spot &= mask;
  345. }
  346. ++spot;
  347. }
  348. row += surface->pitch / 4;
  349. }
  350. }
  351. }
  352. break;
  353. }
  354. SDL_UnlockSurface(surface);
  355. SDL_SetColorKey(surface, 0, 0);
  356. SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
  357. }
  358. int
  359. SDL_SetSurfaceColorMod(SDL_Surface * surface, Uint8 r, Uint8 g, Uint8 b)
  360. {
  361. int flags;
  362. if (!surface) {
  363. return -1;
  364. }
  365. surface->map->info.r = r;
  366. surface->map->info.g = g;
  367. surface->map->info.b = b;
  368. flags = surface->map->info.flags;
  369. if (r != 0xFF || g != 0xFF || b != 0xFF) {
  370. surface->map->info.flags |= SDL_COPY_MODULATE_COLOR;
  371. } else {
  372. surface->map->info.flags &= ~SDL_COPY_MODULATE_COLOR;
  373. }
  374. if (surface->map->info.flags != flags) {
  375. SDL_InvalidateMap(surface->map);
  376. }
  377. return 0;
  378. }
  379. int
  380. SDL_GetSurfaceColorMod(SDL_Surface * surface, Uint8 * r, Uint8 * g, Uint8 * b)
  381. {
  382. if (!surface) {
  383. return -1;
  384. }
  385. if (r) {
  386. *r = surface->map->info.r;
  387. }
  388. if (g) {
  389. *g = surface->map->info.g;
  390. }
  391. if (b) {
  392. *b = surface->map->info.b;
  393. }
  394. return 0;
  395. }
  396. int
  397. SDL_SetSurfaceAlphaMod(SDL_Surface * surface, Uint8 alpha)
  398. {
  399. int flags;
  400. if (!surface) {
  401. return -1;
  402. }
  403. surface->map->info.a = alpha;
  404. flags = surface->map->info.flags;
  405. if (alpha != 0xFF) {
  406. surface->map->info.flags |= SDL_COPY_MODULATE_ALPHA;
  407. } else {
  408. surface->map->info.flags &= ~SDL_COPY_MODULATE_ALPHA;
  409. }
  410. if (surface->map->info.flags != flags) {
  411. SDL_InvalidateMap(surface->map);
  412. }
  413. return 0;
  414. }
  415. int
  416. SDL_GetSurfaceAlphaMod(SDL_Surface * surface, Uint8 * alpha)
  417. {
  418. if (!surface) {
  419. return -1;
  420. }
  421. if (alpha) {
  422. *alpha = surface->map->info.a;
  423. }
  424. return 0;
  425. }
  426. int
  427. SDL_SetSurfaceBlendMode(SDL_Surface * surface, SDL_BlendMode blendMode)
  428. {
  429. int flags, status;
  430. if (!surface) {
  431. return -1;
  432. }
  433. status = 0;
  434. flags = surface->map->info.flags;
  435. surface->map->info.flags &=
  436. ~(SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD);
  437. switch (blendMode) {
  438. case SDL_BLENDMODE_NONE:
  439. break;
  440. case SDL_BLENDMODE_BLEND:
  441. surface->map->info.flags |= SDL_COPY_BLEND;
  442. break;
  443. case SDL_BLENDMODE_ADD:
  444. surface->map->info.flags |= SDL_COPY_ADD;
  445. break;
  446. case SDL_BLENDMODE_MOD:
  447. surface->map->info.flags |= SDL_COPY_MOD;
  448. break;
  449. default:
  450. status = SDL_Unsupported();
  451. break;
  452. }
  453. if (surface->map->info.flags != flags) {
  454. SDL_InvalidateMap(surface->map);
  455. }
  456. return status;
  457. }
  458. int
  459. SDL_GetSurfaceBlendMode(SDL_Surface * surface, SDL_BlendMode *blendMode)
  460. {
  461. if (!surface) {
  462. return -1;
  463. }
  464. if (!blendMode) {
  465. return 0;
  466. }
  467. switch (surface->map->
  468. info.flags & (SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD)) {
  469. case SDL_COPY_BLEND:
  470. *blendMode = SDL_BLENDMODE_BLEND;
  471. break;
  472. case SDL_COPY_ADD:
  473. *blendMode = SDL_BLENDMODE_ADD;
  474. break;
  475. case SDL_COPY_MOD:
  476. *blendMode = SDL_BLENDMODE_MOD;
  477. break;
  478. default:
  479. *blendMode = SDL_BLENDMODE_NONE;
  480. break;
  481. }
  482. return 0;
  483. }
  484. SDL_bool
  485. SDL_SetClipRect(SDL_Surface * surface, const SDL_Rect * rect)
  486. {
  487. SDL_Rect full_rect;
  488. /* Don't do anything if there's no surface to act on */
  489. if (!surface) {
  490. return SDL_FALSE;
  491. }
  492. /* Set up the full surface rectangle */
  493. full_rect.x = 0;
  494. full_rect.y = 0;
  495. full_rect.w = surface->w;
  496. full_rect.h = surface->h;
  497. /* Set the clipping rectangle */
  498. if (!rect) {
  499. surface->clip_rect = full_rect;
  500. return SDL_TRUE;
  501. }
  502. return SDL_IntersectRect(rect, &full_rect, &surface->clip_rect);
  503. }
  504. void
  505. SDL_GetClipRect(SDL_Surface * surface, SDL_Rect * rect)
  506. {
  507. if (surface && rect) {
  508. *rect = surface->clip_rect;
  509. }
  510. }
  511. /*
  512. * Set up a blit between two surfaces -- split into three parts:
  513. * The upper part, SDL_UpperBlit(), performs clipping and rectangle
  514. * verification. The lower part is a pointer to a low level
  515. * accelerated blitting function.
  516. *
  517. * These parts are separated out and each used internally by this
  518. * library in the optimimum places. They are exported so that if
  519. * you know exactly what you are doing, you can optimize your code
  520. * by calling the one(s) you need.
  521. */
  522. int
  523. SDL_LowerBlit(SDL_Surface * src, SDL_Rect * srcrect,
  524. SDL_Surface * dst, SDL_Rect * dstrect)
  525. {
  526. /* Check to make sure the blit mapping is valid */
  527. if ((src->map->dst != dst) ||
  528. (dst->format->palette &&
  529. src->map->dst_palette_version != dst->format->palette->version) ||
  530. (src->format->palette &&
  531. src->map->src_palette_version != src->format->palette->version)) {
  532. if (SDL_MapSurface(src, dst) < 0) {
  533. return (-1);
  534. }
  535. /* just here for debugging */
  536. /* printf */
  537. /* ("src = 0x%08X src->flags = %08X src->map->info.flags = %08x\ndst = 0x%08X dst->flags = %08X dst->map->info.flags = %08X\nsrc->map->blit = 0x%08x\n", */
  538. /* src, dst->flags, src->map->info.flags, dst, dst->flags, */
  539. /* dst->map->info.flags, src->map->blit); */
  540. }
  541. return (src->map->blit(src, srcrect, dst, dstrect));
  542. }
  543. int
  544. SDL_UpperBlit(SDL_Surface * src, const SDL_Rect * srcrect,
  545. SDL_Surface * dst, SDL_Rect * dstrect)
  546. {
  547. SDL_Rect fulldst;
  548. int srcx, srcy, w, h;
  549. /* Make sure the surfaces aren't locked */
  550. if (!src || !dst) {
  551. return SDL_SetError("SDL_UpperBlit: passed a NULL surface");
  552. }
  553. if (src->locked || dst->locked) {
  554. return SDL_SetError("Surfaces must not be locked during blit");
  555. }
  556. /* If the destination rectangle is NULL, use the entire dest surface */
  557. if (dstrect == NULL) {
  558. fulldst.x = fulldst.y = 0;
  559. fulldst.w = dst->w;
  560. fulldst.h = dst->h;
  561. dstrect = &fulldst;
  562. }
  563. /* clip the source rectangle to the source surface */
  564. if (srcrect) {
  565. int maxw, maxh;
  566. srcx = srcrect->x;
  567. w = srcrect->w;
  568. if (srcx < 0) {
  569. w += srcx;
  570. dstrect->x -= srcx;
  571. srcx = 0;
  572. }
  573. maxw = src->w - srcx;
  574. if (maxw < w)
  575. w = maxw;
  576. srcy = srcrect->y;
  577. h = srcrect->h;
  578. if (srcy < 0) {
  579. h += srcy;
  580. dstrect->y -= srcy;
  581. srcy = 0;
  582. }
  583. maxh = src->h - srcy;
  584. if (maxh < h)
  585. h = maxh;
  586. } else {
  587. srcx = srcy = 0;
  588. w = src->w;
  589. h = src->h;
  590. }
  591. /* clip the destination rectangle against the clip rectangle */
  592. {
  593. SDL_Rect *clip = &dst->clip_rect;
  594. int dx, dy;
  595. dx = clip->x - dstrect->x;
  596. if (dx > 0) {
  597. w -= dx;
  598. dstrect->x += dx;
  599. srcx += dx;
  600. }
  601. dx = dstrect->x + w - clip->x - clip->w;
  602. if (dx > 0)
  603. w -= dx;
  604. dy = clip->y - dstrect->y;
  605. if (dy > 0) {
  606. h -= dy;
  607. dstrect->y += dy;
  608. srcy += dy;
  609. }
  610. dy = dstrect->y + h - clip->y - clip->h;
  611. if (dy > 0)
  612. h -= dy;
  613. }
  614. /* Switch back to a fast blit if we were previously stretching */
  615. if (src->map->info.flags & SDL_COPY_NEAREST) {
  616. src->map->info.flags &= ~SDL_COPY_NEAREST;
  617. SDL_InvalidateMap(src->map);
  618. }
  619. if (w > 0 && h > 0) {
  620. SDL_Rect sr;
  621. sr.x = srcx;
  622. sr.y = srcy;
  623. sr.w = dstrect->w = w;
  624. sr.h = dstrect->h = h;
  625. return SDL_LowerBlit(src, &sr, dst, dstrect);
  626. }
  627. dstrect->w = dstrect->h = 0;
  628. return 0;
  629. }
  630. int
  631. SDL_UpperBlitScaled(SDL_Surface * src, const SDL_Rect * srcrect,
  632. SDL_Surface * dst, SDL_Rect * dstrect)
  633. {
  634. double src_x0, src_y0, src_x1, src_y1;
  635. double dst_x0, dst_y0, dst_x1, dst_y1;
  636. SDL_Rect final_src, final_dst;
  637. double scaling_w, scaling_h;
  638. int src_w, src_h;
  639. int dst_w, dst_h;
  640. /* Make sure the surfaces aren't locked */
  641. if (!src || !dst) {
  642. return SDL_SetError("SDL_UpperBlitScaled: passed a NULL surface");
  643. }
  644. if (src->locked || dst->locked) {
  645. return SDL_SetError("Surfaces must not be locked during blit");
  646. }
  647. if (NULL == srcrect) {
  648. src_w = src->w;
  649. src_h = src->h;
  650. } else {
  651. src_w = srcrect->w;
  652. src_h = srcrect->h;
  653. }
  654. if (NULL == dstrect) {
  655. dst_w = dst->w;
  656. dst_h = dst->h;
  657. } else {
  658. dst_w = dstrect->w;
  659. dst_h = dstrect->h;
  660. }
  661. if (dst_w == src_w && dst_h == src_h) {
  662. /* No scaling, defer to regular blit */
  663. return SDL_BlitSurface(src, srcrect, dst, dstrect);
  664. }
  665. scaling_w = (double)dst_w / src_w;
  666. scaling_h = (double)dst_h / src_h;
  667. if (NULL == dstrect) {
  668. dst_x0 = 0;
  669. dst_y0 = 0;
  670. dst_x1 = dst_w - 1;
  671. dst_y1 = dst_h - 1;
  672. } else {
  673. dst_x0 = dstrect->x;
  674. dst_y0 = dstrect->y;
  675. dst_x1 = dst_x0 + dst_w - 1;
  676. dst_y1 = dst_y0 + dst_h - 1;
  677. }
  678. if (NULL == srcrect) {
  679. src_x0 = 0;
  680. src_y0 = 0;
  681. src_x1 = src_w - 1;
  682. src_y1 = src_h - 1;
  683. } else {
  684. src_x0 = srcrect->x;
  685. src_y0 = srcrect->y;
  686. src_x1 = src_x0 + src_w - 1;
  687. src_y1 = src_y0 + src_h - 1;
  688. /* Clip source rectangle to the source surface */
  689. if (src_x0 < 0) {
  690. dst_x0 -= src_x0 * scaling_w;
  691. src_x0 = 0;
  692. }
  693. if (src_x1 >= src->w) {
  694. dst_x1 -= (src_x1 - src->w + 1) * scaling_w;
  695. src_x1 = src->w - 1;
  696. }
  697. if (src_y0 < 0) {
  698. dst_y0 -= src_y0 * scaling_h;
  699. src_y0 = 0;
  700. }
  701. if (src_y1 >= src->h) {
  702. dst_y1 -= (src_y1 - src->h + 1) * scaling_h;
  703. src_y1 = src->h - 1;
  704. }
  705. }
  706. /* Clip destination rectangle to the clip rectangle */
  707. /* Translate to clip space for easier calculations */
  708. dst_x0 -= dst->clip_rect.x;
  709. dst_x1 -= dst->clip_rect.x;
  710. dst_y0 -= dst->clip_rect.y;
  711. dst_y1 -= dst->clip_rect.y;
  712. if (dst_x0 < 0) {
  713. src_x0 -= dst_x0 / scaling_w;
  714. dst_x0 = 0;
  715. }
  716. if (dst_x1 >= dst->clip_rect.w) {
  717. src_x1 -= (dst_x1 - dst->clip_rect.w + 1) / scaling_w;
  718. dst_x1 = dst->clip_rect.w - 1;
  719. }
  720. if (dst_y0 < 0) {
  721. src_y0 -= dst_y0 / scaling_h;
  722. dst_y0 = 0;
  723. }
  724. if (dst_y1 >= dst->clip_rect.h) {
  725. src_y1 -= (dst_y1 - dst->clip_rect.h + 1) / scaling_h;
  726. dst_y1 = dst->clip_rect.h - 1;
  727. }
  728. /* Translate back to surface coordinates */
  729. dst_x0 += dst->clip_rect.x;
  730. dst_x1 += dst->clip_rect.x;
  731. dst_y0 += dst->clip_rect.y;
  732. dst_y1 += dst->clip_rect.y;
  733. final_src.x = (int)SDL_floor(src_x0 + 0.5);
  734. final_src.y = (int)SDL_floor(src_y0 + 0.5);
  735. final_src.w = (int)SDL_floor(src_x1 + 1 + 0.5) - (int)SDL_floor(src_x0 + 0.5);
  736. final_src.h = (int)SDL_floor(src_y1 + 1 + 0.5) - (int)SDL_floor(src_y0 + 0.5);
  737. final_dst.x = (int)SDL_floor(dst_x0 + 0.5);
  738. final_dst.y = (int)SDL_floor(dst_y0 + 0.5);
  739. final_dst.w = (int)SDL_floor(dst_x1 - dst_x0 + 1.5);
  740. final_dst.h = (int)SDL_floor(dst_y1 - dst_y0 + 1.5);
  741. if (final_dst.w < 0)
  742. final_dst.w = 0;
  743. if (final_dst.h < 0)
  744. final_dst.h = 0;
  745. if (dstrect)
  746. *dstrect = final_dst;
  747. if (final_dst.w == 0 || final_dst.h == 0 ||
  748. final_src.w <= 0 || final_src.h <= 0) {
  749. /* No-op. */
  750. return 0;
  751. }
  752. return SDL_LowerBlitScaled(src, &final_src, dst, &final_dst);
  753. }
  754. /**
  755. * This is a semi-private blit function and it performs low-level surface
  756. * scaled blitting only.
  757. */
  758. int
  759. SDL_LowerBlitScaled(SDL_Surface * src, SDL_Rect * srcrect,
  760. SDL_Surface * dst, SDL_Rect * dstrect)
  761. {
  762. static const Uint32 complex_copy_flags = (
  763. SDL_COPY_MODULATE_COLOR | SDL_COPY_MODULATE_ALPHA |
  764. SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD |
  765. SDL_COPY_COLORKEY
  766. );
  767. if (!(src->map->info.flags & SDL_COPY_NEAREST)) {
  768. src->map->info.flags |= SDL_COPY_NEAREST;
  769. SDL_InvalidateMap(src->map);
  770. }
  771. if ( !(src->map->info.flags & complex_copy_flags) &&
  772. src->format->format == dst->format->format &&
  773. !SDL_ISPIXELFORMAT_INDEXED(src->format->format) ) {
  774. return SDL_SoftStretch( src, srcrect, dst, dstrect );
  775. } else {
  776. return SDL_LowerBlit( src, srcrect, dst, dstrect );
  777. }
  778. }
  779. /*
  780. * Lock a surface to directly access the pixels
  781. */
  782. int
  783. SDL_LockSurface(SDL_Surface * surface)
  784. {
  785. if (!surface->locked) {
  786. /* Perform the lock */
  787. if (surface->flags & SDL_RLEACCEL) {
  788. SDL_UnRLESurface(surface, 1);
  789. surface->flags |= SDL_RLEACCEL; /* save accel'd state */
  790. }
  791. }
  792. /* Increment the surface lock count, for recursive locks */
  793. ++surface->locked;
  794. /* Ready to go.. */
  795. return (0);
  796. }
  797. /*
  798. * Unlock a previously locked surface
  799. */
  800. void
  801. SDL_UnlockSurface(SDL_Surface * surface)
  802. {
  803. /* Only perform an unlock if we are locked */
  804. if (!surface->locked || (--surface->locked > 0)) {
  805. return;
  806. }
  807. /* Update RLE encoded surface with new data */
  808. if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
  809. surface->flags &= ~SDL_RLEACCEL; /* stop lying */
  810. SDL_RLESurface(surface);
  811. }
  812. }
  813. /*
  814. * Creates a new surface identical to the existing surface
  815. */
  816. SDL_Surface *
  817. SDL_DuplicateSurface(SDL_Surface * surface)
  818. {
  819. return SDL_ConvertSurface(surface, surface->format, surface->flags);
  820. }
  821. /*
  822. * Convert a surface into the specified pixel format.
  823. */
  824. SDL_Surface *
  825. SDL_ConvertSurface(SDL_Surface * surface, const SDL_PixelFormat * format,
  826. Uint32 flags)
  827. {
  828. SDL_Surface *convert;
  829. Uint32 copy_flags;
  830. SDL_Color copy_color;
  831. SDL_Rect bounds;
  832. int ret;
  833. if (!surface) {
  834. SDL_InvalidParamError("surface");
  835. return NULL;
  836. }
  837. if (!format) {
  838. SDL_InvalidParamError("format");
  839. return NULL;
  840. }
  841. /* Check for empty destination palette! (results in empty image) */
  842. if (format->palette != NULL) {
  843. int i;
  844. for (i = 0; i < format->palette->ncolors; ++i) {
  845. if ((format->palette->colors[i].r != 0xFF) ||
  846. (format->palette->colors[i].g != 0xFF) ||
  847. (format->palette->colors[i].b != 0xFF))
  848. break;
  849. }
  850. if (i == format->palette->ncolors) {
  851. SDL_SetError("Empty destination palette");
  852. return (NULL);
  853. }
  854. }
  855. /* Create a new surface with the desired format */
  856. convert = SDL_CreateRGBSurface(flags, surface->w, surface->h,
  857. format->BitsPerPixel, format->Rmask,
  858. format->Gmask, format->Bmask,
  859. format->Amask);
  860. if (convert == NULL) {
  861. return (NULL);
  862. }
  863. /* Copy the palette if any */
  864. if (format->palette && convert->format->palette) {
  865. SDL_memcpy(convert->format->palette->colors,
  866. format->palette->colors,
  867. format->palette->ncolors * sizeof(SDL_Color));
  868. convert->format->palette->ncolors = format->palette->ncolors;
  869. }
  870. /* Save the original copy flags */
  871. copy_flags = surface->map->info.flags;
  872. copy_color.r = surface->map->info.r;
  873. copy_color.g = surface->map->info.g;
  874. copy_color.b = surface->map->info.b;
  875. copy_color.a = surface->map->info.a;
  876. surface->map->info.r = 0xFF;
  877. surface->map->info.g = 0xFF;
  878. surface->map->info.b = 0xFF;
  879. surface->map->info.a = 0xFF;
  880. surface->map->info.flags = 0;
  881. SDL_InvalidateMap(surface->map);
  882. /* Copy over the image data */
  883. bounds.x = 0;
  884. bounds.y = 0;
  885. bounds.w = surface->w;
  886. bounds.h = surface->h;
  887. ret = SDL_LowerBlit(surface, &bounds, convert, &bounds);
  888. /* Clean up the original surface, and update converted surface */
  889. convert->map->info.r = copy_color.r;
  890. convert->map->info.g = copy_color.g;
  891. convert->map->info.b = copy_color.b;
  892. convert->map->info.a = copy_color.a;
  893. convert->map->info.flags =
  894. (copy_flags &
  895. ~(SDL_COPY_COLORKEY | SDL_COPY_BLEND
  896. | SDL_COPY_RLE_DESIRED | SDL_COPY_RLE_COLORKEY |
  897. SDL_COPY_RLE_ALPHAKEY));
  898. surface->map->info.r = copy_color.r;
  899. surface->map->info.g = copy_color.g;
  900. surface->map->info.b = copy_color.b;
  901. surface->map->info.a = copy_color.a;
  902. surface->map->info.flags = copy_flags;
  903. SDL_InvalidateMap(surface->map);
  904. /* SDL_LowerBlit failed, and so the conversion */
  905. if (ret < 0) {
  906. SDL_FreeSurface(convert);
  907. return NULL;
  908. }
  909. if (copy_flags & SDL_COPY_COLORKEY) {
  910. SDL_bool set_colorkey_by_color = SDL_FALSE;
  911. SDL_bool ignore_alpha = SDL_TRUE; /* Ignore, or not, alpha in colorkey comparison */
  912. if (surface->format->palette) {
  913. if (format->palette &&
  914. surface->format->palette->ncolors <= format->palette->ncolors &&
  915. (SDL_memcmp(surface->format->palette->colors, format->palette->colors,
  916. surface->format->palette->ncolors * sizeof(SDL_Color)) == 0)) {
  917. /* The palette is identical, just set the same colorkey */
  918. SDL_SetColorKey(convert, 1, surface->map->info.colorkey);
  919. } else if (format->Amask) {
  920. set_colorkey_by_color = SDL_TRUE;
  921. ignore_alpha = SDL_FALSE;
  922. } else {
  923. set_colorkey_by_color = SDL_TRUE;
  924. }
  925. } else {
  926. set_colorkey_by_color = SDL_TRUE;
  927. }
  928. if (set_colorkey_by_color) {
  929. SDL_Surface *tmp;
  930. SDL_Surface *tmp2;
  931. int converted_colorkey = 0;
  932. /* Create a dummy surface to get the colorkey converted */
  933. tmp = SDL_CreateRGBSurface(0, 1, 1,
  934. surface->format->BitsPerPixel, surface->format->Rmask,
  935. surface->format->Gmask, surface->format->Bmask,
  936. surface->format->Amask);
  937. /* Share the palette, if any */
  938. if (surface->format->palette) {
  939. SDL_SetSurfacePalette(tmp, surface->format->palette);
  940. }
  941. SDL_FillRect(tmp, NULL, surface->map->info.colorkey);
  942. tmp->map->info.flags &= ~SDL_COPY_COLORKEY;
  943. /* Convertion of the colorkey */
  944. tmp2 = SDL_ConvertSurface(tmp, format, 0);
  945. /* Get the converted colorkey */
  946. SDL_memcpy(&converted_colorkey, tmp2->pixels, tmp2->format->BytesPerPixel);
  947. SDL_FreeSurface(tmp);
  948. SDL_FreeSurface(tmp2);
  949. /* Set the converted colorkey on the new surface */
  950. SDL_SetColorKey(convert, 1, converted_colorkey);
  951. /* This is needed when converting for 3D texture upload */
  952. SDL_ConvertColorkeyToAlpha(convert, ignore_alpha);
  953. }
  954. }
  955. SDL_SetClipRect(convert, &surface->clip_rect);
  956. /* Enable alpha blending by default if the new surface has an
  957. * alpha channel or alpha modulation */
  958. if ((surface->format->Amask && format->Amask) ||
  959. (copy_flags & SDL_COPY_MODULATE_ALPHA)) {
  960. SDL_SetSurfaceBlendMode(convert, SDL_BLENDMODE_BLEND);
  961. }
  962. if ((copy_flags & SDL_COPY_RLE_DESIRED) || (flags & SDL_RLEACCEL)) {
  963. SDL_SetSurfaceRLE(convert, SDL_RLEACCEL);
  964. }
  965. /* We're ready to go! */
  966. return (convert);
  967. }
  968. SDL_Surface *
  969. SDL_ConvertSurfaceFormat(SDL_Surface * surface, Uint32 pixel_format,
  970. Uint32 flags)
  971. {
  972. SDL_PixelFormat *fmt;
  973. SDL_Surface *convert = NULL;
  974. fmt = SDL_AllocFormat(pixel_format);
  975. if (fmt) {
  976. convert = SDL_ConvertSurface(surface, fmt, flags);
  977. SDL_FreeFormat(fmt);
  978. }
  979. return convert;
  980. }
  981. /*
  982. * Create a surface on the stack for quick blit operations
  983. */
  984. static SDL_INLINE SDL_bool
  985. SDL_CreateSurfaceOnStack(int width, int height, Uint32 pixel_format,
  986. void * pixels, int pitch, SDL_Surface * surface,
  987. SDL_PixelFormat * format, SDL_BlitMap * blitmap)
  988. {
  989. if (SDL_ISPIXELFORMAT_INDEXED(pixel_format)) {
  990. SDL_SetError("Indexed pixel formats not supported");
  991. return SDL_FALSE;
  992. }
  993. if (SDL_InitFormat(format, pixel_format) < 0) {
  994. return SDL_FALSE;
  995. }
  996. SDL_zerop(surface);
  997. surface->flags = SDL_PREALLOC;
  998. surface->format = format;
  999. surface->pixels = pixels;
  1000. surface->w = width;
  1001. surface->h = height;
  1002. surface->pitch = pitch;
  1003. /* We don't actually need to set up the clip rect for our purposes */
  1004. /* SDL_SetClipRect(surface, NULL); */
  1005. /* Allocate an empty mapping */
  1006. SDL_zerop(blitmap);
  1007. blitmap->info.r = 0xFF;
  1008. blitmap->info.g = 0xFF;
  1009. blitmap->info.b = 0xFF;
  1010. blitmap->info.a = 0xFF;
  1011. surface->map = blitmap;
  1012. /* The surface is ready to go */
  1013. surface->refcount = 1;
  1014. return SDL_TRUE;
  1015. }
  1016. /*
  1017. * Copy a block of pixels of one format to another format
  1018. */
  1019. int SDL_ConvertPixels(int width, int height,
  1020. Uint32 src_format, const void * src, int src_pitch,
  1021. Uint32 dst_format, void * dst, int dst_pitch)
  1022. {
  1023. SDL_Surface src_surface, dst_surface;
  1024. SDL_PixelFormat src_fmt, dst_fmt;
  1025. SDL_BlitMap src_blitmap, dst_blitmap;
  1026. SDL_Rect rect;
  1027. void *nonconst_src = (void *) src;
  1028. /* Check to make sure we are blitting somewhere, so we don't crash */
  1029. if (!dst) {
  1030. return SDL_InvalidParamError("dst");
  1031. }
  1032. if (!dst_pitch) {
  1033. return SDL_InvalidParamError("dst_pitch");
  1034. }
  1035. if (SDL_ISPIXELFORMAT_FOURCC(src_format) && SDL_ISPIXELFORMAT_FOURCC(dst_format)) {
  1036. return SDL_ConvertPixels_YUV_to_YUV(width, height, src_format, src, src_pitch, dst_format, dst, dst_pitch);
  1037. } else if (SDL_ISPIXELFORMAT_FOURCC(src_format)) {
  1038. return SDL_ConvertPixels_YUV_to_RGB(width, height, src_format, src, src_pitch, dst_format, dst, dst_pitch);
  1039. } else if (SDL_ISPIXELFORMAT_FOURCC(dst_format)) {
  1040. return SDL_ConvertPixels_RGB_to_YUV(width, height, src_format, src, src_pitch, dst_format, dst, dst_pitch);
  1041. }
  1042. /* Fast path for same format copy */
  1043. if (src_format == dst_format) {
  1044. int i;
  1045. const int bpp = SDL_BYTESPERPIXEL(src_format);
  1046. width *= bpp;
  1047. for (i = height; i--;) {
  1048. SDL_memcpy(dst, src, width);
  1049. src = (const Uint8*)src + src_pitch;
  1050. dst = (Uint8*)dst + dst_pitch;
  1051. }
  1052. return 0;
  1053. }
  1054. if (!SDL_CreateSurfaceOnStack(width, height, src_format, nonconst_src,
  1055. src_pitch,
  1056. &src_surface, &src_fmt, &src_blitmap)) {
  1057. return -1;
  1058. }
  1059. if (!SDL_CreateSurfaceOnStack(width, height, dst_format, dst, dst_pitch,
  1060. &dst_surface, &dst_fmt, &dst_blitmap)) {
  1061. return -1;
  1062. }
  1063. /* Set up the rect and go! */
  1064. rect.x = 0;
  1065. rect.y = 0;
  1066. rect.w = width;
  1067. rect.h = height;
  1068. return SDL_LowerBlit(&src_surface, &rect, &dst_surface, &rect);
  1069. }
  1070. /*
  1071. * Free a surface created by the above function.
  1072. */
  1073. void
  1074. SDL_FreeSurface(SDL_Surface * surface)
  1075. {
  1076. if (surface == NULL) {
  1077. return;
  1078. }
  1079. if (surface->flags & SDL_DONTFREE) {
  1080. return;
  1081. }
  1082. SDL_InvalidateMap(surface->map);
  1083. if (--surface->refcount > 0) {
  1084. return;
  1085. }
  1086. while (surface->locked > 0) {
  1087. SDL_UnlockSurface(surface);
  1088. }
  1089. if (surface->flags & SDL_RLEACCEL) {
  1090. SDL_UnRLESurface(surface, 0);
  1091. }
  1092. if (surface->format) {
  1093. SDL_SetSurfacePalette(surface, NULL);
  1094. SDL_FreeFormat(surface->format);
  1095. surface->format = NULL;
  1096. }
  1097. if (!(surface->flags & SDL_PREALLOC)) {
  1098. SDL_free(surface->pixels);
  1099. }
  1100. if (surface->map) {
  1101. SDL_FreeBlitMap(surface->map);
  1102. }
  1103. SDL_free(surface);
  1104. }
  1105. /* vi: set ts=4 sw=4 expandtab: */