SDL_surface.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  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. if (SDL_ISPIXELFORMAT_FOURCC(format) || SDL_BITSPERPIXEL(format) >= 8) {
  37. pitch = (width * SDL_BYTESPERPIXEL(format));
  38. } else {
  39. pitch = ((width * SDL_BITSPERPIXEL(format)) + 7) / 8;
  40. }
  41. pitch = (pitch + 3) & ~3; /* 4-byte aligning for speed */
  42. return pitch;
  43. }
  44. /*
  45. * Create an empty RGB surface of the appropriate depth using the given
  46. * enum SDL_PIXELFORMAT_* format
  47. */
  48. SDL_Surface *
  49. SDL_CreateRGBSurfaceWithFormat(Uint32 flags, int width, int height, int depth,
  50. Uint32 format)
  51. {
  52. SDL_Surface *surface;
  53. /* The flags are no longer used, make the compiler happy */
  54. (void)flags;
  55. /* Allocate the surface */
  56. surface = (SDL_Surface *) SDL_calloc(1, sizeof(*surface));
  57. if (surface == NULL) {
  58. SDL_OutOfMemory();
  59. return NULL;
  60. }
  61. surface->format = SDL_AllocFormat(format);
  62. if (!surface->format) {
  63. SDL_FreeSurface(surface);
  64. return NULL;
  65. }
  66. surface->w = width;
  67. surface->h = height;
  68. surface->pitch = SDL_CalculatePitch(format, width);
  69. SDL_SetClipRect(surface, NULL);
  70. if (SDL_ISPIXELFORMAT_INDEXED(surface->format->format)) {
  71. SDL_Palette *palette =
  72. SDL_AllocPalette((1 << surface->format->BitsPerPixel));
  73. if (!palette) {
  74. SDL_FreeSurface(surface);
  75. return NULL;
  76. }
  77. if (palette->ncolors == 2) {
  78. /* Create a black and white bitmap palette */
  79. palette->colors[0].r = 0xFF;
  80. palette->colors[0].g = 0xFF;
  81. palette->colors[0].b = 0xFF;
  82. palette->colors[1].r = 0x00;
  83. palette->colors[1].g = 0x00;
  84. palette->colors[1].b = 0x00;
  85. }
  86. SDL_SetSurfacePalette(surface, palette);
  87. SDL_FreePalette(palette);
  88. }
  89. /* Get the pixels */
  90. if (surface->w && surface->h) {
  91. /* Assumptions checked in surface_size_assumptions assert above */
  92. Sint64 size = ((Sint64)surface->h * surface->pitch);
  93. if (size < 0 || size > SDL_MAX_SINT32) {
  94. /* Overflow... */
  95. SDL_FreeSurface(surface);
  96. SDL_OutOfMemory();
  97. return NULL;
  98. }
  99. surface->pixels = SDL_SIMDAlloc((size_t)size);
  100. if (!surface->pixels) {
  101. SDL_FreeSurface(surface);
  102. SDL_OutOfMemory();
  103. return NULL;
  104. }
  105. surface->flags |= SDL_SIMD_ALIGNED;
  106. /* This is important for bitmaps */
  107. SDL_memset(surface->pixels, 0, surface->h * surface->pitch);
  108. }
  109. /* Allocate an empty mapping */
  110. surface->map = SDL_AllocBlitMap();
  111. if (!surface->map) {
  112. SDL_FreeSurface(surface);
  113. return NULL;
  114. }
  115. /* By default surface with an alpha mask are set up for blending */
  116. if (surface->format->Amask) {
  117. SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
  118. }
  119. /* The surface is ready to go */
  120. surface->refcount = 1;
  121. return surface;
  122. }
  123. /*
  124. * Create an empty RGB surface of the appropriate depth
  125. */
  126. SDL_Surface *
  127. SDL_CreateRGBSurface(Uint32 flags,
  128. int width, int height, int depth,
  129. Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
  130. {
  131. Uint32 format;
  132. /* Get the pixel format */
  133. format = SDL_MasksToPixelFormatEnum(depth, Rmask, Gmask, Bmask, Amask);
  134. if (format == SDL_PIXELFORMAT_UNKNOWN) {
  135. SDL_SetError("Unknown pixel format");
  136. return NULL;
  137. }
  138. return SDL_CreateRGBSurfaceWithFormat(flags, width, height, depth, format);
  139. }
  140. /*
  141. * Create an RGB surface from an existing memory buffer
  142. */
  143. SDL_Surface *
  144. SDL_CreateRGBSurfaceFrom(void *pixels,
  145. int width, int height, int depth, int pitch,
  146. Uint32 Rmask, Uint32 Gmask, Uint32 Bmask,
  147. Uint32 Amask)
  148. {
  149. SDL_Surface *surface;
  150. surface = SDL_CreateRGBSurface(0, 0, 0, depth, Rmask, Gmask, Bmask, Amask);
  151. if (surface != NULL) {
  152. surface->flags |= SDL_PREALLOC;
  153. surface->pixels = pixels;
  154. surface->w = width;
  155. surface->h = height;
  156. surface->pitch = pitch;
  157. SDL_SetClipRect(surface, NULL);
  158. }
  159. return surface;
  160. }
  161. /*
  162. * Create an RGB surface from an existing memory buffer using the given given
  163. * enum SDL_PIXELFORMAT_* format
  164. */
  165. SDL_Surface *
  166. SDL_CreateRGBSurfaceWithFormatFrom(void *pixels,
  167. int width, int height, int depth, int pitch,
  168. Uint32 format)
  169. {
  170. SDL_Surface *surface;
  171. surface = SDL_CreateRGBSurfaceWithFormat(0, 0, 0, depth, format);
  172. if (surface != NULL) {
  173. surface->flags |= SDL_PREALLOC;
  174. surface->pixels = pixels;
  175. surface->w = width;
  176. surface->h = height;
  177. surface->pitch = pitch;
  178. SDL_SetClipRect(surface, NULL);
  179. }
  180. return surface;
  181. }
  182. int
  183. SDL_SetSurfacePalette(SDL_Surface * surface, SDL_Palette * palette)
  184. {
  185. if (!surface) {
  186. return SDL_SetError("SDL_SetSurfacePalette() passed a NULL surface");
  187. }
  188. if (SDL_SetPixelFormatPalette(surface->format, palette) < 0) {
  189. return -1;
  190. }
  191. SDL_InvalidateMap(surface->map);
  192. return 0;
  193. }
  194. int
  195. SDL_SetSurfaceRLE(SDL_Surface * surface, int flag)
  196. {
  197. int flags;
  198. if (!surface) {
  199. return -1;
  200. }
  201. flags = surface->map->info.flags;
  202. if (flag) {
  203. surface->map->info.flags |= SDL_COPY_RLE_DESIRED;
  204. } else {
  205. surface->map->info.flags &= ~SDL_COPY_RLE_DESIRED;
  206. }
  207. if (surface->map->info.flags != flags) {
  208. SDL_InvalidateMap(surface->map);
  209. }
  210. return 0;
  211. }
  212. int
  213. SDL_SetColorKey(SDL_Surface * surface, int flag, Uint32 key)
  214. {
  215. int flags;
  216. if (!surface) {
  217. return SDL_InvalidParamError("surface");
  218. }
  219. if (surface->format->palette && key >= ((Uint32) surface->format->palette->ncolors)) {
  220. return SDL_InvalidParamError("key");
  221. }
  222. if (flag & SDL_RLEACCEL) {
  223. SDL_SetSurfaceRLE(surface, 1);
  224. }
  225. flags = surface->map->info.flags;
  226. if (flag) {
  227. surface->map->info.flags |= SDL_COPY_COLORKEY;
  228. surface->map->info.colorkey = key;
  229. } else {
  230. surface->map->info.flags &= ~SDL_COPY_COLORKEY;
  231. }
  232. if (surface->map->info.flags != flags) {
  233. SDL_InvalidateMap(surface->map);
  234. }
  235. return 0;
  236. }
  237. SDL_bool
  238. SDL_HasColorKey(SDL_Surface * surface)
  239. {
  240. if (!surface) {
  241. return SDL_FALSE;
  242. }
  243. if (!(surface->map->info.flags & SDL_COPY_COLORKEY)) {
  244. return SDL_FALSE;
  245. }
  246. return SDL_TRUE;
  247. }
  248. int
  249. SDL_GetColorKey(SDL_Surface * surface, Uint32 * key)
  250. {
  251. if (!surface) {
  252. return SDL_InvalidParamError("surface");
  253. }
  254. if (!(surface->map->info.flags & SDL_COPY_COLORKEY)) {
  255. return SDL_SetError("Surface doesn't have a colorkey");
  256. }
  257. if (key) {
  258. *key = surface->map->info.colorkey;
  259. }
  260. return 0;
  261. }
  262. /* This is a fairly slow function to switch from colorkey to alpha */
  263. static void
  264. SDL_ConvertColorkeyToAlpha(SDL_Surface * surface, SDL_bool ignore_alpha)
  265. {
  266. int x, y;
  267. if (!surface) {
  268. return;
  269. }
  270. if (!(surface->map->info.flags & SDL_COPY_COLORKEY) ||
  271. !surface->format->Amask) {
  272. return;
  273. }
  274. SDL_LockSurface(surface);
  275. switch (surface->format->BytesPerPixel) {
  276. case 2:
  277. {
  278. Uint16 *row, *spot;
  279. Uint16 ckey = (Uint16) surface->map->info.colorkey;
  280. Uint16 mask = (Uint16) (~surface->format->Amask);
  281. /* Ignore, or not, alpha in colorkey comparison */
  282. if (ignore_alpha) {
  283. ckey &= mask;
  284. row = (Uint16 *) surface->pixels;
  285. for (y = surface->h; y--;) {
  286. spot = row;
  287. for (x = surface->w; x--;) {
  288. if ((*spot & mask) == ckey) {
  289. *spot &= mask;
  290. }
  291. ++spot;
  292. }
  293. row += surface->pitch / 2;
  294. }
  295. } else {
  296. row = (Uint16 *) surface->pixels;
  297. for (y = surface->h; y--;) {
  298. spot = row;
  299. for (x = surface->w; x--;) {
  300. if (*spot == ckey) {
  301. *spot &= mask;
  302. }
  303. ++spot;
  304. }
  305. row += surface->pitch / 2;
  306. }
  307. }
  308. }
  309. break;
  310. case 3:
  311. /* FIXME */
  312. break;
  313. case 4:
  314. {
  315. Uint32 *row, *spot;
  316. Uint32 ckey = surface->map->info.colorkey;
  317. Uint32 mask = ~surface->format->Amask;
  318. /* Ignore, or not, alpha in colorkey comparison */
  319. if (ignore_alpha) {
  320. ckey &= mask;
  321. row = (Uint32 *) surface->pixels;
  322. for (y = surface->h; y--;) {
  323. spot = row;
  324. for (x = surface->w; x--;) {
  325. if ((*spot & mask) == ckey) {
  326. *spot &= mask;
  327. }
  328. ++spot;
  329. }
  330. row += surface->pitch / 4;
  331. }
  332. } else {
  333. row = (Uint32 *) surface->pixels;
  334. for (y = surface->h; y--;) {
  335. spot = row;
  336. for (x = surface->w; x--;) {
  337. if (*spot == ckey) {
  338. *spot &= mask;
  339. }
  340. ++spot;
  341. }
  342. row += surface->pitch / 4;
  343. }
  344. }
  345. }
  346. break;
  347. }
  348. SDL_UnlockSurface(surface);
  349. SDL_SetColorKey(surface, 0, 0);
  350. SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
  351. }
  352. int
  353. SDL_SetSurfaceColorMod(SDL_Surface * surface, Uint8 r, Uint8 g, Uint8 b)
  354. {
  355. int flags;
  356. if (!surface) {
  357. return -1;
  358. }
  359. surface->map->info.r = r;
  360. surface->map->info.g = g;
  361. surface->map->info.b = b;
  362. flags = surface->map->info.flags;
  363. if (r != 0xFF || g != 0xFF || b != 0xFF) {
  364. surface->map->info.flags |= SDL_COPY_MODULATE_COLOR;
  365. } else {
  366. surface->map->info.flags &= ~SDL_COPY_MODULATE_COLOR;
  367. }
  368. if (surface->map->info.flags != flags) {
  369. SDL_InvalidateMap(surface->map);
  370. }
  371. return 0;
  372. }
  373. int
  374. SDL_GetSurfaceColorMod(SDL_Surface * surface, Uint8 * r, Uint8 * g, Uint8 * b)
  375. {
  376. if (!surface) {
  377. return -1;
  378. }
  379. if (r) {
  380. *r = surface->map->info.r;
  381. }
  382. if (g) {
  383. *g = surface->map->info.g;
  384. }
  385. if (b) {
  386. *b = surface->map->info.b;
  387. }
  388. return 0;
  389. }
  390. int
  391. SDL_SetSurfaceAlphaMod(SDL_Surface * surface, Uint8 alpha)
  392. {
  393. int flags;
  394. if (!surface) {
  395. return -1;
  396. }
  397. surface->map->info.a = alpha;
  398. flags = surface->map->info.flags;
  399. if (alpha != 0xFF) {
  400. surface->map->info.flags |= SDL_COPY_MODULATE_ALPHA;
  401. } else {
  402. surface->map->info.flags &= ~SDL_COPY_MODULATE_ALPHA;
  403. }
  404. if (surface->map->info.flags != flags) {
  405. SDL_InvalidateMap(surface->map);
  406. }
  407. return 0;
  408. }
  409. int
  410. SDL_GetSurfaceAlphaMod(SDL_Surface * surface, Uint8 * alpha)
  411. {
  412. if (!surface) {
  413. return -1;
  414. }
  415. if (alpha) {
  416. *alpha = surface->map->info.a;
  417. }
  418. return 0;
  419. }
  420. int
  421. SDL_SetSurfaceBlendMode(SDL_Surface * surface, SDL_BlendMode blendMode)
  422. {
  423. int flags, status;
  424. if (!surface) {
  425. return -1;
  426. }
  427. status = 0;
  428. flags = surface->map->info.flags;
  429. surface->map->info.flags &=
  430. ~(SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD | SDL_COPY_MUL);
  431. switch (blendMode) {
  432. case SDL_BLENDMODE_NONE:
  433. break;
  434. case SDL_BLENDMODE_BLEND:
  435. surface->map->info.flags |= SDL_COPY_BLEND;
  436. break;
  437. case SDL_BLENDMODE_ADD:
  438. surface->map->info.flags |= SDL_COPY_ADD;
  439. break;
  440. case SDL_BLENDMODE_MOD:
  441. surface->map->info.flags |= SDL_COPY_MOD;
  442. break;
  443. case SDL_BLENDMODE_MUL:
  444. surface->map->info.flags |= SDL_COPY_MUL;
  445. break;
  446. default:
  447. status = SDL_Unsupported();
  448. break;
  449. }
  450. if (surface->map->info.flags != flags) {
  451. SDL_InvalidateMap(surface->map);
  452. }
  453. return status;
  454. }
  455. int
  456. SDL_GetSurfaceBlendMode(SDL_Surface * surface, SDL_BlendMode *blendMode)
  457. {
  458. if (!surface) {
  459. return -1;
  460. }
  461. if (!blendMode) {
  462. return 0;
  463. }
  464. switch (surface->map->
  465. info.flags & (SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD | SDL_COPY_MUL)) {
  466. case SDL_COPY_BLEND:
  467. *blendMode = SDL_BLENDMODE_BLEND;
  468. break;
  469. case SDL_COPY_ADD:
  470. *blendMode = SDL_BLENDMODE_ADD;
  471. break;
  472. case SDL_COPY_MOD:
  473. *blendMode = SDL_BLENDMODE_MOD;
  474. break;
  475. case SDL_COPY_MUL:
  476. *blendMode = SDL_BLENDMODE_MUL;
  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 | SDL_COPY_MUL |
  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. int palette_ck_transform = 0;
  834. int palette_ck_value = 0;
  835. if (!surface) {
  836. SDL_InvalidParamError("surface");
  837. return NULL;
  838. }
  839. if (!format) {
  840. SDL_InvalidParamError("format");
  841. return NULL;
  842. }
  843. /* Check for empty destination palette! (results in empty image) */
  844. if (format->palette != NULL) {
  845. int i;
  846. for (i = 0; i < format->palette->ncolors; ++i) {
  847. if ((format->palette->colors[i].r != 0xFF) ||
  848. (format->palette->colors[i].g != 0xFF) ||
  849. (format->palette->colors[i].b != 0xFF))
  850. break;
  851. }
  852. if (i == format->palette->ncolors) {
  853. SDL_SetError("Empty destination palette");
  854. return (NULL);
  855. }
  856. }
  857. /* Create a new surface with the desired format */
  858. convert = SDL_CreateRGBSurface(flags, surface->w, surface->h,
  859. format->BitsPerPixel, format->Rmask,
  860. format->Gmask, format->Bmask,
  861. format->Amask);
  862. if (convert == NULL) {
  863. return (NULL);
  864. }
  865. /* Copy the palette if any */
  866. if (format->palette && convert->format->palette) {
  867. SDL_memcpy(convert->format->palette->colors,
  868. format->palette->colors,
  869. format->palette->ncolors * sizeof(SDL_Color));
  870. convert->format->palette->ncolors = format->palette->ncolors;
  871. }
  872. /* Save the original copy flags */
  873. copy_flags = surface->map->info.flags;
  874. copy_color.r = surface->map->info.r;
  875. copy_color.g = surface->map->info.g;
  876. copy_color.b = surface->map->info.b;
  877. copy_color.a = surface->map->info.a;
  878. surface->map->info.r = 0xFF;
  879. surface->map->info.g = 0xFF;
  880. surface->map->info.b = 0xFF;
  881. surface->map->info.a = 0xFF;
  882. surface->map->info.flags = 0;
  883. SDL_InvalidateMap(surface->map);
  884. /* Copy over the image data */
  885. bounds.x = 0;
  886. bounds.y = 0;
  887. bounds.w = surface->w;
  888. bounds.h = surface->h;
  889. /* Transform colorkey to alpha. for cases where source palette has duplicate values, and colorkey is one of them */
  890. if (copy_flags & SDL_COPY_COLORKEY) {
  891. if (surface->format->palette && !format->palette) {
  892. palette_ck_transform = 1;
  893. palette_ck_value = surface->format->palette->colors[surface->map->info.colorkey].a;
  894. surface->format->palette->colors[surface->map->info.colorkey].a = SDL_ALPHA_TRANSPARENT;
  895. }
  896. }
  897. ret = SDL_LowerBlit(surface, &bounds, convert, &bounds);
  898. /* Restore value */
  899. if (palette_ck_transform) {
  900. surface->format->palette->colors[surface->map->info.colorkey].a = palette_ck_value;
  901. }
  902. /* Clean up the original surface, and update converted surface */
  903. convert->map->info.r = copy_color.r;
  904. convert->map->info.g = copy_color.g;
  905. convert->map->info.b = copy_color.b;
  906. convert->map->info.a = copy_color.a;
  907. convert->map->info.flags =
  908. (copy_flags &
  909. ~(SDL_COPY_COLORKEY | SDL_COPY_BLEND
  910. | SDL_COPY_RLE_DESIRED | SDL_COPY_RLE_COLORKEY |
  911. SDL_COPY_RLE_ALPHAKEY));
  912. surface->map->info.r = copy_color.r;
  913. surface->map->info.g = copy_color.g;
  914. surface->map->info.b = copy_color.b;
  915. surface->map->info.a = copy_color.a;
  916. surface->map->info.flags = copy_flags;
  917. SDL_InvalidateMap(surface->map);
  918. /* SDL_LowerBlit failed, and so the conversion */
  919. if (ret < 0) {
  920. SDL_FreeSurface(convert);
  921. return NULL;
  922. }
  923. if (copy_flags & SDL_COPY_COLORKEY) {
  924. SDL_bool set_colorkey_by_color = SDL_FALSE;
  925. SDL_bool ignore_alpha = SDL_TRUE; /* Ignore, or not, alpha in colorkey comparison */
  926. if (surface->format->palette) {
  927. if (format->palette &&
  928. surface->format->palette->ncolors <= format->palette->ncolors &&
  929. (SDL_memcmp(surface->format->palette->colors, format->palette->colors,
  930. surface->format->palette->ncolors * sizeof(SDL_Color)) == 0)) {
  931. /* The palette is identical, just set the same colorkey */
  932. SDL_SetColorKey(convert, 1, surface->map->info.colorkey);
  933. } else if (!format->palette) {
  934. /* Was done by 'palette_ck_transform' */
  935. }else if (format->Amask) {
  936. set_colorkey_by_color = SDL_TRUE;
  937. ignore_alpha = SDL_FALSE;
  938. } else {
  939. set_colorkey_by_color = SDL_TRUE;
  940. }
  941. } else {
  942. set_colorkey_by_color = SDL_TRUE;
  943. }
  944. if (set_colorkey_by_color) {
  945. SDL_Surface *tmp;
  946. SDL_Surface *tmp2;
  947. int converted_colorkey = 0;
  948. /* Create a dummy surface to get the colorkey converted */
  949. tmp = SDL_CreateRGBSurface(0, 1, 1,
  950. surface->format->BitsPerPixel, surface->format->Rmask,
  951. surface->format->Gmask, surface->format->Bmask,
  952. surface->format->Amask);
  953. /* Share the palette, if any */
  954. if (surface->format->palette) {
  955. SDL_SetSurfacePalette(tmp, surface->format->palette);
  956. }
  957. SDL_FillRect(tmp, NULL, surface->map->info.colorkey);
  958. tmp->map->info.flags &= ~SDL_COPY_COLORKEY;
  959. /* Convertion of the colorkey */
  960. tmp2 = SDL_ConvertSurface(tmp, format, 0);
  961. /* Get the converted colorkey */
  962. SDL_memcpy(&converted_colorkey, tmp2->pixels, tmp2->format->BytesPerPixel);
  963. SDL_FreeSurface(tmp);
  964. SDL_FreeSurface(tmp2);
  965. /* Set the converted colorkey on the new surface */
  966. SDL_SetColorKey(convert, 1, converted_colorkey);
  967. /* This is needed when converting for 3D texture upload */
  968. SDL_ConvertColorkeyToAlpha(convert, ignore_alpha);
  969. }
  970. }
  971. SDL_SetClipRect(convert, &surface->clip_rect);
  972. /* Enable alpha blending by default if the new surface has an
  973. * alpha channel or alpha modulation */
  974. if ((surface->format->Amask && format->Amask) ||
  975. (copy_flags & SDL_COPY_MODULATE_ALPHA)) {
  976. SDL_SetSurfaceBlendMode(convert, SDL_BLENDMODE_BLEND);
  977. }
  978. if ((copy_flags & SDL_COPY_RLE_DESIRED) || (flags & SDL_RLEACCEL)) {
  979. SDL_SetSurfaceRLE(convert, SDL_RLEACCEL);
  980. }
  981. /* We're ready to go! */
  982. return (convert);
  983. }
  984. SDL_Surface *
  985. SDL_ConvertSurfaceFormat(SDL_Surface * surface, Uint32 pixel_format,
  986. Uint32 flags)
  987. {
  988. SDL_PixelFormat *fmt;
  989. SDL_Surface *convert = NULL;
  990. fmt = SDL_AllocFormat(pixel_format);
  991. if (fmt) {
  992. convert = SDL_ConvertSurface(surface, fmt, flags);
  993. SDL_FreeFormat(fmt);
  994. }
  995. return convert;
  996. }
  997. /*
  998. * Create a surface on the stack for quick blit operations
  999. */
  1000. static SDL_INLINE SDL_bool
  1001. SDL_CreateSurfaceOnStack(int width, int height, Uint32 pixel_format,
  1002. void * pixels, int pitch, SDL_Surface * surface,
  1003. SDL_PixelFormat * format, SDL_BlitMap * blitmap)
  1004. {
  1005. if (SDL_ISPIXELFORMAT_INDEXED(pixel_format)) {
  1006. SDL_SetError("Indexed pixel formats not supported");
  1007. return SDL_FALSE;
  1008. }
  1009. if (SDL_InitFormat(format, pixel_format) < 0) {
  1010. return SDL_FALSE;
  1011. }
  1012. SDL_zerop(surface);
  1013. surface->flags = SDL_PREALLOC;
  1014. surface->format = format;
  1015. surface->pixels = pixels;
  1016. surface->w = width;
  1017. surface->h = height;
  1018. surface->pitch = pitch;
  1019. /* We don't actually need to set up the clip rect for our purposes */
  1020. /* SDL_SetClipRect(surface, NULL); */
  1021. /* Allocate an empty mapping */
  1022. SDL_zerop(blitmap);
  1023. blitmap->info.r = 0xFF;
  1024. blitmap->info.g = 0xFF;
  1025. blitmap->info.b = 0xFF;
  1026. blitmap->info.a = 0xFF;
  1027. surface->map = blitmap;
  1028. /* The surface is ready to go */
  1029. surface->refcount = 1;
  1030. return SDL_TRUE;
  1031. }
  1032. /*
  1033. * Copy a block of pixels of one format to another format
  1034. */
  1035. int SDL_ConvertPixels(int width, int height,
  1036. Uint32 src_format, const void * src, int src_pitch,
  1037. Uint32 dst_format, void * dst, int dst_pitch)
  1038. {
  1039. SDL_Surface src_surface, dst_surface;
  1040. SDL_PixelFormat src_fmt, dst_fmt;
  1041. SDL_BlitMap src_blitmap, dst_blitmap;
  1042. SDL_Rect rect;
  1043. void *nonconst_src = (void *) src;
  1044. /* Check to make sure we are blitting somewhere, so we don't crash */
  1045. if (!dst) {
  1046. return SDL_InvalidParamError("dst");
  1047. }
  1048. if (!dst_pitch) {
  1049. return SDL_InvalidParamError("dst_pitch");
  1050. }
  1051. if (SDL_ISPIXELFORMAT_FOURCC(src_format) && SDL_ISPIXELFORMAT_FOURCC(dst_format)) {
  1052. return SDL_ConvertPixels_YUV_to_YUV(width, height, src_format, src, src_pitch, dst_format, dst, dst_pitch);
  1053. } else if (SDL_ISPIXELFORMAT_FOURCC(src_format)) {
  1054. return SDL_ConvertPixels_YUV_to_RGB(width, height, src_format, src, src_pitch, dst_format, dst, dst_pitch);
  1055. } else if (SDL_ISPIXELFORMAT_FOURCC(dst_format)) {
  1056. return SDL_ConvertPixels_RGB_to_YUV(width, height, src_format, src, src_pitch, dst_format, dst, dst_pitch);
  1057. }
  1058. /* Fast path for same format copy */
  1059. if (src_format == dst_format) {
  1060. int i;
  1061. const int bpp = SDL_BYTESPERPIXEL(src_format);
  1062. width *= bpp;
  1063. for (i = height; i--;) {
  1064. SDL_memcpy(dst, src, width);
  1065. src = (const Uint8*)src + src_pitch;
  1066. dst = (Uint8*)dst + dst_pitch;
  1067. }
  1068. return 0;
  1069. }
  1070. if (!SDL_CreateSurfaceOnStack(width, height, src_format, nonconst_src,
  1071. src_pitch,
  1072. &src_surface, &src_fmt, &src_blitmap)) {
  1073. return -1;
  1074. }
  1075. if (!SDL_CreateSurfaceOnStack(width, height, dst_format, dst, dst_pitch,
  1076. &dst_surface, &dst_fmt, &dst_blitmap)) {
  1077. return -1;
  1078. }
  1079. /* Set up the rect and go! */
  1080. rect.x = 0;
  1081. rect.y = 0;
  1082. rect.w = width;
  1083. rect.h = height;
  1084. return SDL_LowerBlit(&src_surface, &rect, &dst_surface, &rect);
  1085. }
  1086. /*
  1087. * Free a surface created by the above function.
  1088. */
  1089. void
  1090. SDL_FreeSurface(SDL_Surface * surface)
  1091. {
  1092. if (surface == NULL) {
  1093. return;
  1094. }
  1095. if (surface->flags & SDL_DONTFREE) {
  1096. return;
  1097. }
  1098. SDL_InvalidateMap(surface->map);
  1099. if (--surface->refcount > 0) {
  1100. return;
  1101. }
  1102. while (surface->locked > 0) {
  1103. SDL_UnlockSurface(surface);
  1104. }
  1105. if (surface->flags & SDL_RLEACCEL) {
  1106. SDL_UnRLESurface(surface, 0);
  1107. }
  1108. if (surface->format) {
  1109. SDL_SetSurfacePalette(surface, NULL);
  1110. SDL_FreeFormat(surface->format);
  1111. surface->format = NULL;
  1112. }
  1113. if (surface->flags & SDL_PREALLOC) {
  1114. /* Don't free */
  1115. } else if (surface->flags & SDL_SIMD_ALIGNED) {
  1116. /* Free aligned */
  1117. SDL_SIMDFree(surface->pixels);
  1118. } else {
  1119. /* Normal */
  1120. SDL_free(surface->pixels);
  1121. }
  1122. if (surface->map) {
  1123. SDL_FreeBlitMap(surface->map);
  1124. }
  1125. SDL_free(surface);
  1126. }
  1127. /* vi: set ts=4 sw=4 expandtab: */