SDL_surface.c 33 KB

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