SDL_surface.c 42 KB

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