SDL_RLEaccel.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 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. #if SDL_HAVE_RLE
  20. /*
  21. * RLE encoding for software colorkey and alpha-channel acceleration
  22. *
  23. * Original version by Sam Lantinga
  24. *
  25. * Mattias Engdegård (Yorick): Rewrite. New encoding format, encoder and
  26. * decoder. Added per-surface alpha blitter. Added per-pixel alpha
  27. * format, encoder and blitter.
  28. *
  29. * Many thanks to Xark and johns for hints, benchmarks and useful comments
  30. * leading to this code.
  31. *
  32. * Welcome to Macro Mayhem.
  33. */
  34. /*
  35. * The encoding translates the image data to a stream of segments of the form
  36. *
  37. * <skip> <run> <data>
  38. *
  39. * where <skip> is the number of transparent pixels to skip,
  40. * <run> is the number of opaque pixels to blit,
  41. * and <data> are the pixels themselves.
  42. *
  43. * This basic structure is used both for colorkeyed surfaces, used for simple
  44. * binary transparency and for per-surface alpha blending, and for surfaces
  45. * with per-pixel alpha. The details differ, however:
  46. *
  47. * Encoding of colorkeyed surfaces:
  48. *
  49. * Encoded pixels always have the same format as the target surface.
  50. * <skip> and <run> are unsigned 8 bit integers, except for 32 bit depth
  51. * where they are 16 bit. This makes the pixel data aligned at all times.
  52. * Segments never wrap around from one scan line to the next.
  53. *
  54. * The end of the sequence is marked by a zero <skip>,<run> pair at the *
  55. * beginning of a line.
  56. *
  57. * Encoding of surfaces with per-pixel alpha:
  58. *
  59. * The sequence begins with an SDL_PixelFormat value describing the target
  60. * pixel format, to provide reliable un-encoding.
  61. *
  62. * Each scan line is encoded twice: First all completely opaque pixels,
  63. * encoded in the target format as described above, and then all
  64. * partially transparent (translucent) pixels (where 1 <= alpha <= 254),
  65. * in the following 32-bit format:
  66. *
  67. * For 32-bit targets, each pixel has the target RGB format but with
  68. * the alpha value occupying the highest 8 bits. The <skip> and <run>
  69. * counts are 16 bit.
  70. *
  71. * For 16-bit targets, each pixel has the target RGB format, but with
  72. * the middle component (usually green) shifted 16 steps to the left,
  73. * and the hole filled with the 5 most significant bits of the alpha value.
  74. * i.e. if the target has the format rrrrrggggggbbbbb,
  75. * the encoded pixel will be 00000gggggg00000rrrrr0aaaaabbbbb.
  76. * The <skip> and <run> counts are 8 bit for the opaque lines, 16 bit
  77. * for the translucent lines. Two padding bytes may be inserted
  78. * before each translucent line to keep them 32-bit aligned.
  79. *
  80. * The end of the sequence is marked by a zero <skip>,<run> pair at the
  81. * beginning of an opaque line.
  82. */
  83. #include "SDL_sysvideo.h"
  84. #include "SDL_blit.h"
  85. #include "SDL_RLEaccel_c.h"
  86. #define PIXEL_COPY(to, from, len, bpp) \
  87. SDL_memcpy(to, from, (size_t)(len) * (bpp))
  88. /*
  89. * Various colorkey blit methods, for opaque and per-surface alpha
  90. */
  91. #define OPAQUE_BLIT(to, from, length, bpp, alpha) \
  92. PIXEL_COPY(to, from, length, bpp)
  93. /*
  94. * For 32bpp pixels on the form 0x00rrggbb:
  95. * If we treat the middle component separately, we can process the two
  96. * remaining in parallel. This is safe to do because of the gap to the left
  97. * of each component, so the bits from the multiplication don't collide.
  98. * This can be used for any RGB permutation of course.
  99. */
  100. #define ALPHA_BLIT32_888(to, from, length, bpp, alpha) \
  101. do { \
  102. int i; \
  103. Uint32 *src = (Uint32 *)(from); \
  104. Uint32 *dst = (Uint32 *)(to); \
  105. for (i = 0; i < (int)(length); i++) { \
  106. Uint32 s = *src++; \
  107. Uint32 d = *dst; \
  108. Uint32 s1 = s & 0xff00ff; \
  109. Uint32 d1 = d & 0xff00ff; \
  110. d1 = (d1 + ((s1 - d1) * alpha >> 8)) & 0xff00ff; \
  111. s &= 0xff00; \
  112. d &= 0xff00; \
  113. d = (d + ((s - d) * alpha >> 8)) & 0xff00; \
  114. *dst++ = d1 | d; \
  115. } \
  116. } while (0)
  117. /*
  118. * For 16bpp pixels we can go a step further: put the middle component
  119. * in the high 16 bits of a 32 bit word, and process all three RGB
  120. * components at the same time. Since the smallest gap is here just
  121. * 5 bits, we have to scale alpha down to 5 bits as well.
  122. */
  123. #define ALPHA_BLIT16_565(to, from, length, bpp, alpha) \
  124. do { \
  125. int i; \
  126. Uint16 *src = (Uint16 *)(from); \
  127. Uint16 *dst = (Uint16 *)(to); \
  128. Uint32 ALPHA = alpha >> 3; \
  129. for (i = 0; i < (int)(length); i++) { \
  130. Uint32 s = *src++; \
  131. Uint32 d = *dst; \
  132. s = (s | s << 16) & 0x07e0f81f; \
  133. d = (d | d << 16) & 0x07e0f81f; \
  134. d += (s - d) * ALPHA >> 5; \
  135. d &= 0x07e0f81f; \
  136. *dst++ = (Uint16)(d | d >> 16); \
  137. } \
  138. } while (0)
  139. #define ALPHA_BLIT16_555(to, from, length, bpp, alpha) \
  140. do { \
  141. int i; \
  142. Uint16 *src = (Uint16 *)(from); \
  143. Uint16 *dst = (Uint16 *)(to); \
  144. Uint32 ALPHA = alpha >> 3; \
  145. for (i = 0; i < (int)(length); i++) { \
  146. Uint32 s = *src++; \
  147. Uint32 d = *dst; \
  148. s = (s | s << 16) & 0x03e07c1f; \
  149. d = (d | d << 16) & 0x03e07c1f; \
  150. d += (s - d) * ALPHA >> 5; \
  151. d &= 0x03e07c1f; \
  152. *dst++ = (Uint16)(d | d >> 16); \
  153. } \
  154. } while (0)
  155. /*
  156. * The general slow catch-all function, for remaining depths and formats
  157. */
  158. #define ALPHA_BLIT_ANY(to, from, length, bpp, alpha) \
  159. do { \
  160. int i; \
  161. Uint8 *src = from; \
  162. Uint8 *dst = to; \
  163. for (i = 0; i < (int)(length); i++) { \
  164. Uint32 s = 0, d = 0; \
  165. unsigned rs, gs, bs, rd, gd, bd; \
  166. switch (bpp) { \
  167. case 2: \
  168. s = *(Uint16 *)src; \
  169. d = *(Uint16 *)dst; \
  170. break; \
  171. case 3: \
  172. if (SDL_BYTEORDER == SDL_BIG_ENDIAN) { \
  173. s = (src[0] << 16) | (src[1] << 8) | src[2]; \
  174. d = (dst[0] << 16) | (dst[1] << 8) | dst[2]; \
  175. } else { \
  176. s = (src[2] << 16) | (src[1] << 8) | src[0]; \
  177. d = (dst[2] << 16) | (dst[1] << 8) | dst[0]; \
  178. } \
  179. break; \
  180. case 4: \
  181. s = *(Uint32 *)src; \
  182. d = *(Uint32 *)dst; \
  183. break; \
  184. } \
  185. RGB_FROM_PIXEL(s, fmt, rs, gs, bs); \
  186. RGB_FROM_PIXEL(d, fmt, rd, gd, bd); \
  187. rd += (rs - rd) * alpha >> 8; \
  188. gd += (gs - gd) * alpha >> 8; \
  189. bd += (bs - bd) * alpha >> 8; \
  190. PIXEL_FROM_RGB(d, fmt, rd, gd, bd); \
  191. switch (bpp) { \
  192. case 2: \
  193. *(Uint16 *)dst = (Uint16)d; \
  194. break; \
  195. case 3: \
  196. if (SDL_BYTEORDER == SDL_BIG_ENDIAN) { \
  197. dst[0] = (Uint8)(d >> 16); \
  198. dst[1] = (Uint8)(d >> 8); \
  199. dst[2] = (Uint8)(d); \
  200. } else { \
  201. dst[0] = (Uint8)d; \
  202. dst[1] = (Uint8)(d >> 8); \
  203. dst[2] = (Uint8)(d >> 16); \
  204. } \
  205. break; \
  206. case 4: \
  207. *(Uint32 *)dst = d; \
  208. break; \
  209. } \
  210. src += bpp; \
  211. dst += bpp; \
  212. } \
  213. } while (0)
  214. /*
  215. * Special case: 50% alpha (alpha=128)
  216. * This is treated specially because it can be optimized very well, and
  217. * since it is good for many cases of semi-translucency.
  218. * The theory is to do all three components at the same time:
  219. * First zero the lowest bit of each component, which gives us room to
  220. * add them. Then shift right and add the sum of the lowest bits.
  221. */
  222. #define ALPHA_BLIT32_888_50(to, from, length, bpp, alpha) \
  223. do { \
  224. int i; \
  225. Uint32 *src = (Uint32 *)(from); \
  226. Uint32 *dst = (Uint32 *)(to); \
  227. for (i = 0; i < (int)(length); i++) { \
  228. Uint32 s = *src++; \
  229. Uint32 d = *dst; \
  230. *dst++ = (((s & 0x00fefefe) + (d & 0x00fefefe)) >> 1) + (s & d & 0x00010101); \
  231. } \
  232. } while (0)
  233. /*
  234. * For 16bpp, we can actually blend two pixels in parallel, if we take
  235. * care to shift before we add, not after.
  236. */
  237. /* helper: blend a single 16 bit pixel at 50% */
  238. #define BLEND16_50(dst, src, mask) \
  239. do { \
  240. Uint32 s = *src++; \
  241. Uint32 d = *dst; \
  242. *dst++ = (Uint16)((((s & mask) + (d & mask)) >> 1) + \
  243. (s & d & (~mask & 0xffff))); \
  244. } while (0)
  245. /* basic 16bpp blender. mask is the pixels to keep when adding. */
  246. #define ALPHA_BLIT16_50(to, from, length, bpp, alpha, mask) \
  247. do { \
  248. unsigned n = (length); \
  249. Uint16 *src = (Uint16 *)(from); \
  250. Uint16 *dst = (Uint16 *)(to); \
  251. if (((uintptr_t)src ^ (uintptr_t)dst) & 3) { \
  252. /* source and destination not in phase, blit one by one */ \
  253. while (n--) \
  254. BLEND16_50(dst, src, mask); \
  255. } else { \
  256. if ((uintptr_t)src & 3) { \
  257. /* first odd pixel */ \
  258. BLEND16_50(dst, src, mask); \
  259. n--; \
  260. } \
  261. for (; n > 1; n -= 2) { \
  262. Uint32 s = *(Uint32 *)src; \
  263. Uint32 d = *(Uint32 *)dst; \
  264. *(Uint32 *)dst = ((s & (mask | mask << 16)) >> 1) + ((d & (mask | mask << 16)) >> 1) + (s & d & (~(mask | mask << 16))); \
  265. src += 2; \
  266. dst += 2; \
  267. } \
  268. if (n) \
  269. BLEND16_50(dst, src, mask); /* last odd pixel */ \
  270. } \
  271. } while (0)
  272. #define ALPHA_BLIT16_565_50(to, from, length, bpp, alpha) \
  273. ALPHA_BLIT16_50(to, from, length, bpp, alpha, 0xf7deU)
  274. #define ALPHA_BLIT16_555_50(to, from, length, bpp, alpha) \
  275. ALPHA_BLIT16_50(to, from, length, bpp, alpha, 0xfbdeU)
  276. #define CHOOSE_BLIT(blitter, alpha, fmt) \
  277. do { \
  278. if (alpha == 255) { \
  279. switch (fmt->bytes_per_pixel) { \
  280. case 1: \
  281. blitter(1, Uint8, OPAQUE_BLIT); \
  282. break; \
  283. case 2: \
  284. blitter(2, Uint8, OPAQUE_BLIT); \
  285. break; \
  286. case 3: \
  287. blitter(3, Uint8, OPAQUE_BLIT); \
  288. break; \
  289. case 4: \
  290. blitter(4, Uint16, OPAQUE_BLIT); \
  291. break; \
  292. } \
  293. } else { \
  294. switch (fmt->bytes_per_pixel) { \
  295. case 1: \
  296. /* No 8bpp alpha blitting */ \
  297. break; \
  298. \
  299. case 2: \
  300. switch (fmt->Rmask | fmt->Gmask | fmt->Bmask) { \
  301. case 0xffff: \
  302. if (fmt->Gmask == 0x07e0 || fmt->Rmask == 0x07e0 || fmt->Bmask == 0x07e0) { \
  303. if (alpha == 128) { \
  304. blitter(2, Uint8, ALPHA_BLIT16_565_50); \
  305. } else { \
  306. blitter(2, Uint8, ALPHA_BLIT16_565); \
  307. } \
  308. } else { \
  309. goto general16; \
  310. } \
  311. break; \
  312. \
  313. case 0x7fff: \
  314. if (fmt->Gmask == 0x03e0 || fmt->Rmask == 0x03e0 || fmt->Bmask == 0x03e0) { \
  315. if (alpha == 128) { \
  316. blitter(2, Uint8, ALPHA_BLIT16_555_50); \
  317. } else { \
  318. blitter(2, Uint8, ALPHA_BLIT16_555); \
  319. } \
  320. break; \
  321. } else { \
  322. goto general16; \
  323. } \
  324. break; \
  325. \
  326. default: \
  327. general16: \
  328. blitter(2, Uint8, ALPHA_BLIT_ANY); \
  329. } \
  330. break; \
  331. \
  332. case 3: \
  333. blitter(3, Uint8, ALPHA_BLIT_ANY); \
  334. break; \
  335. \
  336. case 4: \
  337. if ((fmt->Rmask | fmt->Gmask | fmt->Bmask) == 0x00ffffff && (fmt->Gmask == 0xff00 || fmt->Rmask == 0xff00 || fmt->Bmask == 0xff00)) { \
  338. if (alpha == 128) { \
  339. blitter(4, Uint16, ALPHA_BLIT32_888_50); \
  340. } else { \
  341. blitter(4, Uint16, ALPHA_BLIT32_888); \
  342. } \
  343. } else { \
  344. blitter(4, Uint16, ALPHA_BLIT_ANY); \
  345. } \
  346. break; \
  347. } \
  348. } \
  349. } while (0)
  350. /*
  351. * Set a pixel value using the given format, except that the alpha value is
  352. * placed in the top byte. This is the format used for RLE with alpha.
  353. */
  354. #define RLEPIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a) \
  355. { \
  356. Pixel = ((r >> (8 - fmt->Rbits)) << fmt->Rshift) | \
  357. ((g >> (8 - fmt->Gbits)) << fmt->Gshift) | \
  358. ((b >> (8 - fmt->Bbits)) << fmt->Bshift) | \
  359. (a << 24); \
  360. }
  361. /*
  362. * This takes care of the case when the surface is clipped on the left and/or
  363. * right. Top clipping has already been taken care of.
  364. */
  365. #define RLECLIPBLIT(bpp, Type, do_blit) \
  366. do { \
  367. int linecount = srcrect->h; \
  368. int ofs = 0; \
  369. int left = srcrect->x; \
  370. int right = left + srcrect->w; \
  371. dstbuf -= left * bpp; \
  372. for (;;) { \
  373. int run; \
  374. ofs += *(Type *)srcbuf; \
  375. run = ((Type *)srcbuf)[1]; \
  376. srcbuf += 2 * sizeof(Type); \
  377. if (run) { \
  378. /* clip to left and right borders */ \
  379. if (ofs < right) { \
  380. int start = 0; \
  381. int len = run; \
  382. int startcol; \
  383. if (left - ofs > 0) { \
  384. start = left - ofs; \
  385. len -= start; \
  386. if (len <= 0) \
  387. goto nocopy##bpp##do_blit; \
  388. } \
  389. startcol = ofs + start; \
  390. if (len > right - startcol) \
  391. len = right - startcol; \
  392. do_blit(dstbuf + startcol * bpp, srcbuf + start * bpp, \
  393. len, bpp, alpha); \
  394. } \
  395. nocopy##bpp##do_blit : srcbuf += run * bpp; \
  396. ofs += run; \
  397. } else if (!ofs) { \
  398. break; \
  399. } \
  400. \
  401. if (ofs == w) { \
  402. ofs = 0; \
  403. dstbuf += surf_dst->pitch; \
  404. if (!--linecount) { \
  405. break; \
  406. } \
  407. } \
  408. } \
  409. } while (0)
  410. static void RLEClipBlit(int w, Uint8 *srcbuf, SDL_Surface *surf_dst,
  411. Uint8 *dstbuf, const SDL_Rect *srcrect, unsigned alpha)
  412. {
  413. const SDL_PixelFormatDetails *fmt = surf_dst->internal->format;
  414. CHOOSE_BLIT(RLECLIPBLIT, alpha, fmt);
  415. }
  416. #undef RLECLIPBLIT
  417. /* blit a colorkeyed RLE surface */
  418. static int SDLCALL SDL_RLEBlit(SDL_Surface *surf_src, const SDL_Rect *srcrect,
  419. SDL_Surface *surf_dst, const SDL_Rect *dstrect)
  420. {
  421. Uint8 *dstbuf;
  422. Uint8 *srcbuf;
  423. int x, y;
  424. int w = surf_src->w;
  425. unsigned alpha;
  426. /* Lock the destination if necessary */
  427. if (SDL_MUSTLOCK(surf_dst)) {
  428. if (SDL_LockSurface(surf_dst) < 0) {
  429. return -1;
  430. }
  431. }
  432. /* Set up the source and destination pointers */
  433. x = dstrect->x;
  434. y = dstrect->y;
  435. dstbuf = (Uint8 *)surf_dst->pixels + y * surf_dst->pitch + x * surf_src->internal->format->bytes_per_pixel;
  436. srcbuf = (Uint8 *)surf_src->internal->map.data + sizeof(SDL_PixelFormat);
  437. {
  438. /* skip lines at the top if necessary */
  439. int vskip = srcrect->y;
  440. int ofs = 0;
  441. if (vskip) {
  442. #define RLESKIP(bpp, Type) \
  443. for (;;) { \
  444. int run; \
  445. ofs += *(Type *)srcbuf; \
  446. run = ((Type *)srcbuf)[1]; \
  447. srcbuf += sizeof(Type) * 2; \
  448. if (run) { \
  449. srcbuf += run * bpp; \
  450. ofs += run; \
  451. } else if (!ofs) \
  452. goto done; \
  453. if (ofs == w) { \
  454. ofs = 0; \
  455. if (!--vskip) \
  456. break; \
  457. } \
  458. }
  459. switch (surf_src->internal->format->bytes_per_pixel) {
  460. case 1:
  461. RLESKIP(1, Uint8);
  462. break;
  463. case 2:
  464. RLESKIP(2, Uint8);
  465. break;
  466. case 3:
  467. RLESKIP(3, Uint8);
  468. break;
  469. case 4:
  470. RLESKIP(4, Uint16);
  471. break;
  472. }
  473. #undef RLESKIP
  474. }
  475. }
  476. alpha = surf_src->internal->map.info.a;
  477. /* if left or right edge clipping needed, call clip blit */
  478. if (srcrect->x || srcrect->w != surf_src->w) {
  479. RLEClipBlit(w, srcbuf, surf_dst, dstbuf, srcrect, alpha);
  480. } else {
  481. const SDL_PixelFormatDetails *fmt = surf_src->internal->format;
  482. #define RLEBLIT(bpp, Type, do_blit) \
  483. do { \
  484. int linecount = srcrect->h; \
  485. int ofs = 0; \
  486. for (;;) { \
  487. unsigned run; \
  488. ofs += *(Type *)srcbuf; \
  489. run = ((Type *)srcbuf)[1]; \
  490. srcbuf += 2 * sizeof(Type); \
  491. if (run) { \
  492. do_blit(dstbuf + ofs * bpp, srcbuf, run, bpp, alpha); \
  493. srcbuf += run * bpp; \
  494. ofs += run; \
  495. } else if (!ofs) \
  496. break; \
  497. if (ofs == w) { \
  498. ofs = 0; \
  499. dstbuf += surf_dst->pitch; \
  500. if (!--linecount) \
  501. break; \
  502. } \
  503. } \
  504. } while (0)
  505. CHOOSE_BLIT(RLEBLIT, alpha, fmt);
  506. #undef RLEBLIT
  507. }
  508. done:
  509. /* Unlock the destination if necessary */
  510. if (SDL_MUSTLOCK(surf_dst)) {
  511. SDL_UnlockSurface(surf_dst);
  512. }
  513. return 0;
  514. }
  515. #undef OPAQUE_BLIT
  516. /*
  517. * Per-pixel blitting macros for translucent pixels:
  518. * These use the same techniques as the per-surface blitting macros
  519. */
  520. /*
  521. * For 32bpp pixels, we have made sure the alpha is stored in the top
  522. * 8 bits, so proceed as usual
  523. */
  524. #define BLIT_TRANSL_888(src, dst) \
  525. do { \
  526. Uint32 s = src; \
  527. Uint32 d = dst; \
  528. unsigned alpha = s >> 24; \
  529. Uint32 s1 = s & 0xff00ff; \
  530. Uint32 d1 = d & 0xff00ff; \
  531. d1 = (d1 + ((s1 - d1) * alpha >> 8)) & 0xff00ff; \
  532. s &= 0xff00; \
  533. d &= 0xff00; \
  534. d = (d + ((s - d) * alpha >> 8)) & 0xff00; \
  535. dst = d1 | d | 0xff000000; \
  536. } while (0)
  537. /*
  538. * For 16bpp pixels, we have stored the 5 most significant alpha bits in
  539. * bits 5-10. As before, we can process all 3 RGB components at the same time.
  540. */
  541. #define BLIT_TRANSL_565(src, dst) \
  542. do { \
  543. Uint32 s = src; \
  544. Uint32 d = dst; \
  545. unsigned alpha = (s & 0x3e0) >> 5; \
  546. s &= 0x07e0f81f; \
  547. d = (d | d << 16) & 0x07e0f81f; \
  548. d += (s - d) * alpha >> 5; \
  549. d &= 0x07e0f81f; \
  550. dst = (Uint16)(d | d >> 16); \
  551. } while (0)
  552. #define BLIT_TRANSL_555(src, dst) \
  553. do { \
  554. Uint32 s = src; \
  555. Uint32 d = dst; \
  556. unsigned alpha = (s & 0x3e0) >> 5; \
  557. s &= 0x03e07c1f; \
  558. d = (d | d << 16) & 0x03e07c1f; \
  559. d += (s - d) * alpha >> 5; \
  560. d &= 0x03e07c1f; \
  561. dst = (Uint16)(d | d >> 16); \
  562. } while (0)
  563. /* blit a pixel-alpha RLE surface clipped at the right and/or left edges */
  564. static void RLEAlphaClipBlit(int w, Uint8 *srcbuf, SDL_Surface *surf_dst,
  565. Uint8 *dstbuf, const SDL_Rect *srcrect)
  566. {
  567. const SDL_PixelFormatDetails *df = surf_dst->internal->format;
  568. /*
  569. * clipped blitter: Ptype is the destination pixel type,
  570. * Ctype the translucent count type, and do_blend the macro
  571. * to blend one pixel.
  572. */
  573. #define RLEALPHACLIPBLIT(Ptype, Ctype, do_blend) \
  574. do { \
  575. int linecount = srcrect->h; \
  576. int left = srcrect->x; \
  577. int right = left + srcrect->w; \
  578. dstbuf -= left * sizeof(Ptype); \
  579. do { \
  580. int ofs = 0; \
  581. /* blit opaque pixels on one line */ \
  582. do { \
  583. unsigned run; \
  584. ofs += ((Ctype *)srcbuf)[0]; \
  585. run = ((Ctype *)srcbuf)[1]; \
  586. srcbuf += 2 * sizeof(Ctype); \
  587. if (run) { \
  588. /* clip to left and right borders */ \
  589. int cofs = ofs; \
  590. int crun = run; \
  591. if (left - cofs > 0) { \
  592. crun -= left - cofs; \
  593. cofs = left; \
  594. } \
  595. if (crun > right - cofs) \
  596. crun = right - cofs; \
  597. if (crun > 0) \
  598. PIXEL_COPY(dstbuf + cofs * sizeof(Ptype), \
  599. srcbuf + (cofs - ofs) * sizeof(Ptype), \
  600. (unsigned)crun, sizeof(Ptype)); \
  601. srcbuf += run * sizeof(Ptype); \
  602. ofs += run; \
  603. } else if (!ofs) \
  604. return; \
  605. } while (ofs < w); \
  606. /* skip padding if necessary */ \
  607. if (sizeof(Ptype) == 2) \
  608. srcbuf += (uintptr_t)srcbuf & 2; \
  609. /* blit translucent pixels on the same line */ \
  610. ofs = 0; \
  611. do { \
  612. unsigned run; \
  613. ofs += ((Uint16 *)srcbuf)[0]; \
  614. run = ((Uint16 *)srcbuf)[1]; \
  615. srcbuf += 4; \
  616. if (run) { \
  617. /* clip to left and right borders */ \
  618. int cofs = ofs; \
  619. int crun = run; \
  620. if (left - cofs > 0) { \
  621. crun -= left - cofs; \
  622. cofs = left; \
  623. } \
  624. if (crun > right - cofs) \
  625. crun = right - cofs; \
  626. if (crun > 0) { \
  627. Ptype *dst = (Ptype *)dstbuf + cofs; \
  628. Uint32 *src = (Uint32 *)srcbuf + (cofs - ofs); \
  629. int i; \
  630. for (i = 0; i < crun; i++) \
  631. do_blend(src[i], dst[i]); \
  632. } \
  633. srcbuf += run * 4; \
  634. ofs += run; \
  635. } \
  636. } while (ofs < w); \
  637. dstbuf += surf_dst->pitch; \
  638. } while (--linecount); \
  639. } while (0)
  640. switch (df->bytes_per_pixel) {
  641. case 2:
  642. if (df->Gmask == 0x07e0 || df->Rmask == 0x07e0 || df->Bmask == 0x07e0) {
  643. RLEALPHACLIPBLIT(Uint16, Uint8, BLIT_TRANSL_565);
  644. } else {
  645. RLEALPHACLIPBLIT(Uint16, Uint8, BLIT_TRANSL_555);
  646. }
  647. break;
  648. case 4:
  649. RLEALPHACLIPBLIT(Uint32, Uint16, BLIT_TRANSL_888);
  650. break;
  651. }
  652. }
  653. /* blit a pixel-alpha RLE surface */
  654. static int SDLCALL SDL_RLEAlphaBlit(SDL_Surface *surf_src, const SDL_Rect *srcrect,
  655. SDL_Surface *surf_dst, const SDL_Rect *dstrect)
  656. {
  657. int x, y;
  658. int w = surf_src->w;
  659. Uint8 *srcbuf, *dstbuf;
  660. const SDL_PixelFormatDetails *df = surf_dst->internal->format;
  661. /* Lock the destination if necessary */
  662. if (SDL_MUSTLOCK(surf_dst)) {
  663. if (SDL_LockSurface(surf_dst) < 0) {
  664. return -1;
  665. }
  666. }
  667. x = dstrect->x;
  668. y = dstrect->y;
  669. dstbuf = (Uint8 *)surf_dst->pixels + y * surf_dst->pitch + x * df->bytes_per_pixel;
  670. srcbuf = (Uint8 *)surf_src->internal->map.data + sizeof(SDL_PixelFormat);
  671. {
  672. /* skip lines at the top if necessary */
  673. int vskip = srcrect->y;
  674. if (vskip) {
  675. int ofs;
  676. if (df->bytes_per_pixel == 2) {
  677. /* the 16/32 interleaved format */
  678. do {
  679. /* skip opaque line */
  680. ofs = 0;
  681. do {
  682. int run;
  683. ofs += srcbuf[0];
  684. run = srcbuf[1];
  685. srcbuf += 2;
  686. if (run) {
  687. srcbuf += 2 * run;
  688. ofs += run;
  689. } else if (ofs == 0) {
  690. goto done;
  691. }
  692. } while (ofs < w);
  693. /* skip padding */
  694. srcbuf += (uintptr_t)srcbuf & 2;
  695. /* skip translucent line */
  696. ofs = 0;
  697. do {
  698. int run;
  699. ofs += ((Uint16 *)srcbuf)[0];
  700. run = ((Uint16 *)srcbuf)[1];
  701. srcbuf += 4 * (run + 1);
  702. ofs += run;
  703. } while (ofs < w);
  704. } while (--vskip);
  705. } else {
  706. /* the 32/32 interleaved format */
  707. vskip <<= 1; /* opaque and translucent have same format */
  708. do {
  709. ofs = 0;
  710. do {
  711. int run;
  712. ofs += ((Uint16 *)srcbuf)[0];
  713. run = ((Uint16 *)srcbuf)[1];
  714. srcbuf += 4;
  715. if (run) {
  716. srcbuf += 4 * run;
  717. ofs += run;
  718. } else if (ofs == 0) {
  719. goto done;
  720. }
  721. } while (ofs < w);
  722. } while (--vskip);
  723. }
  724. }
  725. }
  726. /* if left or right edge clipping needed, call clip blit */
  727. if (srcrect->x || srcrect->w != surf_src->w) {
  728. RLEAlphaClipBlit(w, srcbuf, surf_dst, dstbuf, srcrect);
  729. } else {
  730. /*
  731. * non-clipped blitter. Ptype is the destination pixel type,
  732. * Ctype the translucent count type, and do_blend the
  733. * macro to blend one pixel.
  734. */
  735. #define RLEALPHABLIT(Ptype, Ctype, do_blend) \
  736. do { \
  737. int linecount = srcrect->h; \
  738. do { \
  739. int ofs = 0; \
  740. /* blit opaque pixels on one line */ \
  741. do { \
  742. unsigned run; \
  743. ofs += ((Ctype *)srcbuf)[0]; \
  744. run = ((Ctype *)srcbuf)[1]; \
  745. srcbuf += 2 * sizeof(Ctype); \
  746. if (run) { \
  747. PIXEL_COPY(dstbuf + ofs * sizeof(Ptype), srcbuf, \
  748. run, sizeof(Ptype)); \
  749. srcbuf += run * sizeof(Ptype); \
  750. ofs += run; \
  751. } else if (!ofs) \
  752. goto done; \
  753. } while (ofs < w); \
  754. /* skip padding if necessary */ \
  755. if (sizeof(Ptype) == 2) \
  756. srcbuf += (uintptr_t)srcbuf & 2; \
  757. /* blit translucent pixels on the same line */ \
  758. ofs = 0; \
  759. do { \
  760. unsigned run; \
  761. ofs += ((Uint16 *)srcbuf)[0]; \
  762. run = ((Uint16 *)srcbuf)[1]; \
  763. srcbuf += 4; \
  764. if (run) { \
  765. Ptype *dst = (Ptype *)dstbuf + ofs; \
  766. unsigned i; \
  767. for (i = 0; i < run; i++) { \
  768. Uint32 src = *(Uint32 *)srcbuf; \
  769. do_blend(src, *dst); \
  770. srcbuf += 4; \
  771. dst++; \
  772. } \
  773. ofs += run; \
  774. } \
  775. } while (ofs < w); \
  776. dstbuf += surf_dst->pitch; \
  777. } while (--linecount); \
  778. } while (0)
  779. switch (df->bytes_per_pixel) {
  780. case 2:
  781. if (df->Gmask == 0x07e0 || df->Rmask == 0x07e0 || df->Bmask == 0x07e0) {
  782. RLEALPHABLIT(Uint16, Uint8, BLIT_TRANSL_565);
  783. } else {
  784. RLEALPHABLIT(Uint16, Uint8, BLIT_TRANSL_555);
  785. }
  786. break;
  787. case 4:
  788. RLEALPHABLIT(Uint32, Uint16, BLIT_TRANSL_888);
  789. break;
  790. }
  791. }
  792. done:
  793. /* Unlock the destination if necessary */
  794. if (SDL_MUSTLOCK(surf_dst)) {
  795. SDL_UnlockSurface(surf_dst);
  796. }
  797. return 0;
  798. }
  799. /*
  800. * Auxiliary functions:
  801. * The encoding functions take 32bpp rgb + a, and
  802. * return the number of bytes copied to the destination.
  803. * The decoding functions copy to 32bpp rgb + a, and
  804. * return the number of bytes copied from the source.
  805. * These are only used in the encoder and un-RLE code and are therefore not
  806. * highly optimised.
  807. */
  808. /* encode 32bpp rgb + a into 16bpp rgb, losing alpha */
  809. static int copy_opaque_16(void *dst, const Uint32 *src, int n,
  810. const SDL_PixelFormatDetails *sfmt, const SDL_PixelFormatDetails *dfmt)
  811. {
  812. int i;
  813. Uint16 *d = (Uint16 *)dst;
  814. for (i = 0; i < n; i++) {
  815. unsigned r, g, b;
  816. RGB_FROM_PIXEL(*src, sfmt, r, g, b);
  817. PIXEL_FROM_RGB(*d, dfmt, r, g, b);
  818. src++;
  819. d++;
  820. }
  821. return n * 2;
  822. }
  823. /* decode opaque pixels from 16bpp to 32bpp rgb + a */
  824. static int uncopy_opaque_16(Uint32 *dst, const void *src, int n,
  825. const SDL_PixelFormatDetails *sfmt, const SDL_PixelFormatDetails *dfmt)
  826. {
  827. int i;
  828. const Uint16 *s = (const Uint16 *)src;
  829. unsigned alpha = dfmt->Amask ? 255 : 0;
  830. for (i = 0; i < n; i++) {
  831. unsigned r, g, b;
  832. RGB_FROM_PIXEL(*s, sfmt, r, g, b);
  833. PIXEL_FROM_RGBA(*dst, dfmt, r, g, b, alpha);
  834. s++;
  835. dst++;
  836. }
  837. return n * 2;
  838. }
  839. /* encode 32bpp rgb + a into 32bpp G0RAB format for blitting into 565 */
  840. static int copy_transl_565(void *dst, const Uint32 *src, int n,
  841. const SDL_PixelFormatDetails *sfmt, const SDL_PixelFormatDetails *dfmt)
  842. {
  843. int i;
  844. Uint32 *d = (Uint32 *)dst;
  845. for (i = 0; i < n; i++) {
  846. unsigned r, g, b, a;
  847. Uint16 pix;
  848. RGBA_FROM_8888(*src, sfmt, r, g, b, a);
  849. PIXEL_FROM_RGB(pix, dfmt, r, g, b);
  850. *d = ((pix & 0x7e0) << 16) | (pix & 0xf81f) | ((a << 2) & 0x7e0);
  851. src++;
  852. d++;
  853. }
  854. return n * 4;
  855. }
  856. /* encode 32bpp rgb + a into 32bpp G0RAB format for blitting into 555 */
  857. static int copy_transl_555(void *dst, const Uint32 *src, int n,
  858. const SDL_PixelFormatDetails *sfmt, const SDL_PixelFormatDetails *dfmt)
  859. {
  860. int i;
  861. Uint32 *d = (Uint32 *)dst;
  862. for (i = 0; i < n; i++) {
  863. unsigned r, g, b, a;
  864. Uint16 pix;
  865. RGBA_FROM_8888(*src, sfmt, r, g, b, a);
  866. PIXEL_FROM_RGB(pix, dfmt, r, g, b);
  867. *d = ((pix & 0x3e0) << 16) | (pix & 0xfc1f) | ((a << 2) & 0x3e0);
  868. src++;
  869. d++;
  870. }
  871. return n * 4;
  872. }
  873. /* decode translucent pixels from 32bpp GORAB to 32bpp rgb + a */
  874. static int uncopy_transl_16(Uint32 *dst, const void *src, int n,
  875. const SDL_PixelFormatDetails *sfmt, const SDL_PixelFormatDetails *dfmt)
  876. {
  877. int i;
  878. const Uint32 *s = (const Uint32 *)src;
  879. for (i = 0; i < n; i++) {
  880. unsigned r, g, b, a;
  881. Uint32 pix = *s++;
  882. a = (pix & 0x3e0) >> 2;
  883. pix = (pix & ~0x3e0) | pix >> 16;
  884. RGB_FROM_PIXEL(pix, sfmt, r, g, b);
  885. PIXEL_FROM_RGBA(*dst, dfmt, r, g, b, a);
  886. dst++;
  887. }
  888. return n * 4;
  889. }
  890. /* encode 32bpp rgba into 32bpp rgba, keeping alpha (dual purpose) */
  891. static int copy_32(void *dst, const Uint32 *src, int n,
  892. const SDL_PixelFormatDetails *sfmt, const SDL_PixelFormatDetails *dfmt)
  893. {
  894. int i;
  895. Uint32 *d = (Uint32 *)dst;
  896. for (i = 0; i < n; i++) {
  897. unsigned r, g, b, a;
  898. RGBA_FROM_8888(*src, sfmt, r, g, b, a);
  899. RLEPIXEL_FROM_RGBA(*d, dfmt, r, g, b, a);
  900. d++;
  901. src++;
  902. }
  903. return n * 4;
  904. }
  905. /* decode 32bpp rgba into 32bpp rgba, keeping alpha (dual purpose) */
  906. static int uncopy_32(Uint32 *dst, const void *src, int n,
  907. const SDL_PixelFormatDetails *sfmt, const SDL_PixelFormatDetails *dfmt)
  908. {
  909. int i;
  910. const Uint32 *s = (const Uint32 *)src;
  911. for (i = 0; i < n; i++) {
  912. unsigned r, g, b, a;
  913. Uint32 pixel = *s++;
  914. RGB_FROM_PIXEL(pixel, sfmt, r, g, b);
  915. a = pixel >> 24;
  916. PIXEL_FROM_RGBA(*dst, dfmt, r, g, b, a);
  917. dst++;
  918. }
  919. return n * 4;
  920. }
  921. #define ISOPAQUE(pixel, fmt) ((((pixel)&fmt->Amask) >> fmt->Ashift) == 255)
  922. #define ISTRANSL(pixel, fmt) \
  923. ((unsigned)((((pixel)&fmt->Amask) >> fmt->Ashift) - 1U) < 254U)
  924. /* convert surface to be quickly alpha-blittable onto dest, if possible */
  925. static int RLEAlphaSurface(SDL_Surface *surface)
  926. {
  927. SDL_Surface *dest;
  928. const SDL_PixelFormatDetails *df;
  929. int maxsize = 0;
  930. int max_opaque_run;
  931. int max_transl_run = 65535;
  932. unsigned masksum;
  933. Uint8 *rlebuf, *dst;
  934. int (*copy_opaque)(void *, const Uint32 *, int,
  935. const SDL_PixelFormatDetails *, const SDL_PixelFormatDetails *);
  936. int (*copy_transl)(void *, const Uint32 *, int,
  937. const SDL_PixelFormatDetails *, const SDL_PixelFormatDetails *);
  938. dest = surface->internal->map.dst;
  939. if (!dest) {
  940. return -1;
  941. }
  942. df = dest->internal->format;
  943. if (surface->internal->format->bits_per_pixel != 32) {
  944. return -1; /* only 32bpp source supported */
  945. }
  946. /* find out whether the destination is one we support,
  947. and determine the max size of the encoded result */
  948. masksum = df->Rmask | df->Gmask | df->Bmask;
  949. switch (df->bytes_per_pixel) {
  950. case 2:
  951. /* 16bpp: only support 565 and 555 formats */
  952. switch (masksum) {
  953. case 0xffff:
  954. if (df->Gmask == 0x07e0 || df->Rmask == 0x07e0 || df->Bmask == 0x07e0) {
  955. copy_opaque = copy_opaque_16;
  956. copy_transl = copy_transl_565;
  957. } else {
  958. return -1;
  959. }
  960. break;
  961. case 0x7fff:
  962. if (df->Gmask == 0x03e0 || df->Rmask == 0x03e0 || df->Bmask == 0x03e0) {
  963. copy_opaque = copy_opaque_16;
  964. copy_transl = copy_transl_555;
  965. } else {
  966. return -1;
  967. }
  968. break;
  969. default:
  970. return -1;
  971. }
  972. max_opaque_run = 255; /* runs stored as bytes */
  973. /* worst case is alternating opaque and translucent pixels,
  974. with room for alignment padding between lines */
  975. maxsize = surface->h * (2 + (4 + 2) * (surface->w + 1)) + 2;
  976. break;
  977. case 4:
  978. if (masksum != 0x00ffffff) {
  979. return -1; /* requires unused high byte */
  980. }
  981. copy_opaque = copy_32;
  982. copy_transl = copy_32;
  983. max_opaque_run = 255; /* runs stored as short ints */
  984. /* worst case is alternating opaque and translucent pixels */
  985. maxsize = surface->h * 2 * 4 * (surface->w + 1) + 4;
  986. break;
  987. default:
  988. return -1; /* anything else unsupported right now */
  989. }
  990. maxsize += sizeof(SDL_PixelFormat);
  991. rlebuf = (Uint8 *)SDL_malloc(maxsize);
  992. if (!rlebuf) {
  993. return -1;
  994. }
  995. /* save the destination format so we can undo the encoding later */
  996. *(SDL_PixelFormat *)rlebuf = df->format;
  997. dst = rlebuf + sizeof(SDL_PixelFormat);
  998. /* Do the actual encoding */
  999. {
  1000. int x, y;
  1001. int h = surface->h, w = surface->w;
  1002. const SDL_PixelFormatDetails *sf = surface->internal->format;
  1003. Uint32 *src = (Uint32 *)surface->pixels;
  1004. Uint8 *lastline = dst; /* end of last non-blank line */
  1005. /* opaque counts are 8 or 16 bits, depending on target depth */
  1006. #define ADD_OPAQUE_COUNTS(n, m) \
  1007. if (df->bytes_per_pixel == 4) { \
  1008. ((Uint16 *)dst)[0] = (Uint16)n; \
  1009. ((Uint16 *)dst)[1] = (Uint16)m; \
  1010. dst += 4; \
  1011. } else { \
  1012. dst[0] = (Uint8)n; \
  1013. dst[1] = (Uint8)m; \
  1014. dst += 2; \
  1015. }
  1016. /* translucent counts are always 16 bit */
  1017. #define ADD_TRANSL_COUNTS(n, m) \
  1018. (((Uint16 *)dst)[0] = (Uint16)n, ((Uint16 *)dst)[1] = (Uint16)m, dst += 4)
  1019. for (y = 0; y < h; y++) {
  1020. int runstart, skipstart;
  1021. int blankline = 0;
  1022. /* First encode all opaque pixels of a scan line */
  1023. x = 0;
  1024. do {
  1025. int run, skip, len;
  1026. skipstart = x;
  1027. while (x < w && !ISOPAQUE(src[x], sf)) {
  1028. x++;
  1029. }
  1030. runstart = x;
  1031. while (x < w && ISOPAQUE(src[x], sf)) {
  1032. x++;
  1033. }
  1034. skip = runstart - skipstart;
  1035. if (skip == w) {
  1036. blankline = 1;
  1037. }
  1038. run = x - runstart;
  1039. while (skip > max_opaque_run) {
  1040. ADD_OPAQUE_COUNTS(max_opaque_run, 0);
  1041. skip -= max_opaque_run;
  1042. }
  1043. len = SDL_min(run, max_opaque_run);
  1044. ADD_OPAQUE_COUNTS(skip, len);
  1045. dst += copy_opaque(dst, src + runstart, len, sf, df);
  1046. runstart += len;
  1047. run -= len;
  1048. while (run) {
  1049. len = SDL_min(run, max_opaque_run);
  1050. ADD_OPAQUE_COUNTS(0, len);
  1051. dst += copy_opaque(dst, src + runstart, len, sf, df);
  1052. runstart += len;
  1053. run -= len;
  1054. }
  1055. } while (x < w);
  1056. /* Make sure the next output address is 32-bit aligned */
  1057. dst += (uintptr_t)dst & 2;
  1058. /* Next, encode all translucent pixels of the same scan line */
  1059. x = 0;
  1060. do {
  1061. int run, skip, len;
  1062. skipstart = x;
  1063. while (x < w && !ISTRANSL(src[x], sf)) {
  1064. x++;
  1065. }
  1066. runstart = x;
  1067. while (x < w && ISTRANSL(src[x], sf)) {
  1068. x++;
  1069. }
  1070. skip = runstart - skipstart;
  1071. blankline &= (skip == w);
  1072. run = x - runstart;
  1073. while (skip > max_transl_run) {
  1074. ADD_TRANSL_COUNTS(max_transl_run, 0);
  1075. skip -= max_transl_run;
  1076. }
  1077. len = SDL_min(run, max_transl_run);
  1078. ADD_TRANSL_COUNTS(skip, len);
  1079. dst += copy_transl(dst, src + runstart, len, sf, df);
  1080. runstart += len;
  1081. run -= len;
  1082. while (run) {
  1083. len = SDL_min(run, max_transl_run);
  1084. ADD_TRANSL_COUNTS(0, len);
  1085. dst += copy_transl(dst, src + runstart, len, sf, df);
  1086. runstart += len;
  1087. run -= len;
  1088. }
  1089. if (!blankline) {
  1090. lastline = dst;
  1091. }
  1092. } while (x < w);
  1093. src += surface->pitch >> 2;
  1094. }
  1095. dst = lastline; /* back up past trailing blank lines */
  1096. ADD_OPAQUE_COUNTS(0, 0);
  1097. }
  1098. #undef ADD_OPAQUE_COUNTS
  1099. #undef ADD_TRANSL_COUNTS
  1100. /* Now that we have it encoded, release the original pixels */
  1101. if (!(surface->flags & SDL_SURFACE_PREALLOCATED)) {
  1102. if (surface->flags & SDL_SURFACE_SIMD_ALIGNED) {
  1103. SDL_aligned_free(surface->pixels);
  1104. surface->flags &= ~SDL_SURFACE_SIMD_ALIGNED;
  1105. } else {
  1106. SDL_free(surface->pixels);
  1107. }
  1108. surface->pixels = NULL;
  1109. }
  1110. /* reallocate the buffer to release unused memory */
  1111. {
  1112. Uint8 *p = (Uint8 *)SDL_realloc(rlebuf, dst - rlebuf);
  1113. if (!p) {
  1114. p = rlebuf;
  1115. }
  1116. surface->internal->map.data = p;
  1117. }
  1118. return 0;
  1119. }
  1120. static Uint32 getpix_8(const Uint8 *srcbuf)
  1121. {
  1122. return *srcbuf;
  1123. }
  1124. static Uint32 getpix_16(const Uint8 *srcbuf)
  1125. {
  1126. return *(const Uint16 *)srcbuf;
  1127. }
  1128. static Uint32 getpix_24(const Uint8 *srcbuf)
  1129. {
  1130. #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  1131. return srcbuf[0] + (srcbuf[1] << 8) + (srcbuf[2] << 16);
  1132. #else
  1133. return (srcbuf[0] << 16) + (srcbuf[1] << 8) + srcbuf[2];
  1134. #endif
  1135. }
  1136. static Uint32 getpix_32(const Uint8 *srcbuf)
  1137. {
  1138. return *(const Uint32 *)srcbuf;
  1139. }
  1140. typedef Uint32 (*getpix_func)(const Uint8 *);
  1141. static const getpix_func getpixes[4] = {
  1142. getpix_8, getpix_16, getpix_24, getpix_32
  1143. };
  1144. static int RLEColorkeySurface(SDL_Surface *surface)
  1145. {
  1146. Uint8 *rlebuf, *dst;
  1147. int maxn;
  1148. int y;
  1149. Uint8 *srcbuf, *lastline;
  1150. int maxsize = 0;
  1151. const int bpp = surface->internal->format->bytes_per_pixel;
  1152. getpix_func getpix;
  1153. Uint32 ckey, rgbmask;
  1154. int w, h;
  1155. /* calculate the worst case size for the compressed surface */
  1156. switch (bpp) {
  1157. case 1:
  1158. /* worst case is alternating opaque and transparent pixels,
  1159. starting with an opaque pixel */
  1160. maxsize = surface->h * 3 * (surface->w / 2 + 1) + 2;
  1161. break;
  1162. case 2:
  1163. case 3:
  1164. /* worst case is solid runs, at most 255 pixels wide */
  1165. maxsize = surface->h * (2 * (surface->w / 255 + 1) + surface->w * bpp) + 2;
  1166. break;
  1167. case 4:
  1168. /* worst case is solid runs, at most 65535 pixels wide */
  1169. maxsize = surface->h * (4 * (surface->w / 65535 + 1) + surface->w * 4) + 4;
  1170. break;
  1171. default:
  1172. return -1;
  1173. }
  1174. rlebuf = (Uint8 *)SDL_malloc(maxsize);
  1175. if (!rlebuf) {
  1176. return -1;
  1177. }
  1178. /* Set up the conversion */
  1179. srcbuf = (Uint8 *)surface->pixels;
  1180. maxn = bpp == 4 ? 65535 : 255;
  1181. dst = rlebuf;
  1182. rgbmask = ~surface->internal->format->Amask;
  1183. ckey = surface->internal->map.info.colorkey & rgbmask;
  1184. lastline = dst;
  1185. getpix = getpixes[bpp - 1];
  1186. w = surface->w;
  1187. h = surface->h;
  1188. #define ADD_COUNTS(n, m) \
  1189. if (bpp == 4) { \
  1190. ((Uint16 *)dst)[0] = (Uint16)n; \
  1191. ((Uint16 *)dst)[1] = (Uint16)m; \
  1192. dst += 4; \
  1193. } else { \
  1194. dst[0] = (Uint8)n; \
  1195. dst[1] = (Uint8)m; \
  1196. dst += 2; \
  1197. }
  1198. for (y = 0; y < h; y++) {
  1199. int x = 0;
  1200. int blankline = 0;
  1201. do {
  1202. int run, skip;
  1203. int len;
  1204. int runstart;
  1205. int skipstart = x;
  1206. /* find run of transparent, then opaque pixels */
  1207. while (x < w && (getpix(srcbuf + x * bpp) & rgbmask) == ckey) {
  1208. x++;
  1209. }
  1210. runstart = x;
  1211. while (x < w && (getpix(srcbuf + x * bpp) & rgbmask) != ckey) {
  1212. x++;
  1213. }
  1214. skip = runstart - skipstart;
  1215. if (skip == w) {
  1216. blankline = 1;
  1217. }
  1218. run = x - runstart;
  1219. /* encode segment */
  1220. while (skip > maxn) {
  1221. ADD_COUNTS(maxn, 0);
  1222. skip -= maxn;
  1223. }
  1224. len = SDL_min(run, maxn);
  1225. ADD_COUNTS(skip, len);
  1226. SDL_memcpy(dst, srcbuf + runstart * bpp, (size_t)len * bpp);
  1227. dst += len * bpp;
  1228. run -= len;
  1229. runstart += len;
  1230. while (run) {
  1231. len = SDL_min(run, maxn);
  1232. ADD_COUNTS(0, len);
  1233. SDL_memcpy(dst, srcbuf + runstart * bpp, (size_t)len * bpp);
  1234. dst += len * bpp;
  1235. runstart += len;
  1236. run -= len;
  1237. }
  1238. if (!blankline) {
  1239. lastline = dst;
  1240. }
  1241. } while (x < w);
  1242. srcbuf += surface->pitch;
  1243. }
  1244. dst = lastline; /* back up bast trailing blank lines */
  1245. ADD_COUNTS(0, 0);
  1246. #undef ADD_COUNTS
  1247. /* Now that we have it encoded, release the original pixels */
  1248. if (!(surface->flags & SDL_SURFACE_PREALLOCATED)) {
  1249. if (surface->flags & SDL_SURFACE_SIMD_ALIGNED) {
  1250. SDL_aligned_free(surface->pixels);
  1251. surface->flags &= ~SDL_SURFACE_SIMD_ALIGNED;
  1252. } else {
  1253. SDL_free(surface->pixels);
  1254. }
  1255. surface->pixels = NULL;
  1256. }
  1257. /* reallocate the buffer to release unused memory */
  1258. {
  1259. /* If SDL_realloc returns NULL, the original block is left intact */
  1260. Uint8 *p = (Uint8 *)SDL_realloc(rlebuf, dst - rlebuf);
  1261. if (!p) {
  1262. p = rlebuf;
  1263. }
  1264. surface->internal->map.data = p;
  1265. }
  1266. return 0;
  1267. }
  1268. int SDL_RLESurface(SDL_Surface *surface)
  1269. {
  1270. int flags;
  1271. /* Clear any previous RLE conversion */
  1272. if (surface->internal->flags & SDL_INTERNAL_SURFACE_RLEACCEL) {
  1273. SDL_UnRLESurface(surface, SDL_TRUE);
  1274. }
  1275. /* We don't support RLE encoding of bitmaps */
  1276. if (SDL_BITSPERPIXEL(surface->format) < 8) {
  1277. return -1;
  1278. }
  1279. /* Make sure the pixels are available */
  1280. if (!surface->pixels) {
  1281. return -1;
  1282. }
  1283. flags = surface->internal->map.info.flags;
  1284. if (flags & SDL_COPY_COLORKEY) {
  1285. /* ok */
  1286. } else if ((flags & SDL_COPY_BLEND) && SDL_ISPIXELFORMAT_ALPHA(surface->format)) {
  1287. /* ok */
  1288. } else {
  1289. /* If we don't have colorkey or blending, nothing to do... */
  1290. return -1;
  1291. }
  1292. /* Pass on combinations not supported */
  1293. if ((flags & SDL_COPY_MODULATE_COLOR) ||
  1294. ((flags & SDL_COPY_MODULATE_ALPHA) && SDL_ISPIXELFORMAT_ALPHA(surface->format)) ||
  1295. (flags & (SDL_COPY_BLEND_PREMULTIPLIED | SDL_COPY_ADD | SDL_COPY_ADD_PREMULTIPLIED | SDL_COPY_MOD | SDL_COPY_MUL)) ||
  1296. (flags & SDL_COPY_NEAREST)) {
  1297. return -1;
  1298. }
  1299. /* Encode and set up the blit */
  1300. if (!SDL_ISPIXELFORMAT_ALPHA(surface->format) || !(flags & SDL_COPY_BLEND)) {
  1301. if (!surface->internal->map.identity) {
  1302. return -1;
  1303. }
  1304. if (RLEColorkeySurface(surface) < 0) {
  1305. return -1;
  1306. }
  1307. surface->internal->map.blit = SDL_RLEBlit;
  1308. surface->internal->map.info.flags |= SDL_COPY_RLE_COLORKEY;
  1309. } else {
  1310. if (RLEAlphaSurface(surface) < 0) {
  1311. return -1;
  1312. }
  1313. surface->internal->map.blit = SDL_RLEAlphaBlit;
  1314. surface->internal->map.info.flags |= SDL_COPY_RLE_ALPHAKEY;
  1315. }
  1316. /* The surface is now accelerated */
  1317. surface->internal->flags |= SDL_INTERNAL_SURFACE_RLEACCEL;
  1318. return 0;
  1319. }
  1320. /*
  1321. * Un-RLE a surface with pixel alpha
  1322. * This may not give back exactly the image before RLE-encoding; all
  1323. * completely transparent pixels will be lost, and color and alpha depth
  1324. * may have been reduced (when encoding for 16bpp targets).
  1325. */
  1326. static SDL_bool UnRLEAlpha(SDL_Surface *surface)
  1327. {
  1328. Uint8 *srcbuf;
  1329. Uint32 *dst;
  1330. const SDL_PixelFormatDetails *sf = surface->internal->format;
  1331. const SDL_PixelFormatDetails *df = SDL_GetPixelFormatDetails(*(SDL_PixelFormat *)surface->internal->map.data);
  1332. int (*uncopy_opaque)(Uint32 *, const void *, int,
  1333. const SDL_PixelFormatDetails *, const SDL_PixelFormatDetails *);
  1334. int (*uncopy_transl)(Uint32 *, const void *, int,
  1335. const SDL_PixelFormatDetails *, const SDL_PixelFormatDetails *);
  1336. int w = surface->w;
  1337. int bpp = df->bytes_per_pixel;
  1338. size_t size;
  1339. if (bpp == 2) {
  1340. uncopy_opaque = uncopy_opaque_16;
  1341. uncopy_transl = uncopy_transl_16;
  1342. } else {
  1343. uncopy_opaque = uncopy_transl = uncopy_32;
  1344. }
  1345. if (SDL_size_mul_overflow(surface->h, surface->pitch, &size)) {
  1346. return SDL_FALSE;
  1347. }
  1348. surface->pixels = SDL_aligned_alloc(SDL_GetSIMDAlignment(), size);
  1349. if (!surface->pixels) {
  1350. return SDL_FALSE;
  1351. }
  1352. surface->flags |= SDL_SURFACE_SIMD_ALIGNED;
  1353. /* fill background with transparent pixels */
  1354. SDL_memset(surface->pixels, 0, (size_t)surface->h * surface->pitch);
  1355. dst = (Uint32 *)surface->pixels;
  1356. srcbuf = (Uint8 *)surface->internal->map.data + sizeof(SDL_PixelFormat);
  1357. for (;;) {
  1358. /* copy opaque pixels */
  1359. int ofs = 0;
  1360. do {
  1361. unsigned run;
  1362. if (bpp == 2) {
  1363. ofs += srcbuf[0];
  1364. run = srcbuf[1];
  1365. srcbuf += 2;
  1366. } else {
  1367. ofs += ((Uint16 *)srcbuf)[0];
  1368. run = ((Uint16 *)srcbuf)[1];
  1369. srcbuf += 4;
  1370. }
  1371. if (run) {
  1372. srcbuf += uncopy_opaque(dst + ofs, srcbuf, run, df, sf);
  1373. ofs += run;
  1374. } else if (!ofs) {
  1375. goto end_function;
  1376. }
  1377. } while (ofs < w);
  1378. /* skip padding if needed */
  1379. if (bpp == 2) {
  1380. srcbuf += (uintptr_t)srcbuf & 2;
  1381. }
  1382. /* copy translucent pixels */
  1383. ofs = 0;
  1384. do {
  1385. unsigned run;
  1386. ofs += ((Uint16 *)srcbuf)[0];
  1387. run = ((Uint16 *)srcbuf)[1];
  1388. srcbuf += 4;
  1389. if (run) {
  1390. srcbuf += uncopy_transl(dst + ofs, srcbuf, run, df, sf);
  1391. ofs += run;
  1392. }
  1393. } while (ofs < w);
  1394. dst += surface->pitch >> 2;
  1395. }
  1396. end_function:
  1397. return SDL_TRUE;
  1398. }
  1399. void SDL_UnRLESurface(SDL_Surface *surface, SDL_bool recode)
  1400. {
  1401. if (surface->internal->flags & SDL_INTERNAL_SURFACE_RLEACCEL) {
  1402. surface->internal->flags &= ~SDL_INTERNAL_SURFACE_RLEACCEL;
  1403. if (recode && !(surface->flags & SDL_SURFACE_PREALLOCATED)) {
  1404. if (surface->internal->map.info.flags & SDL_COPY_RLE_COLORKEY) {
  1405. SDL_Rect full;
  1406. size_t size;
  1407. /* re-create the original surface */
  1408. if (SDL_size_mul_overflow(surface->h, surface->pitch, &size)) {
  1409. /* Memory corruption? */
  1410. surface->internal->flags |= SDL_INTERNAL_SURFACE_RLEACCEL;
  1411. return;
  1412. }
  1413. surface->pixels = SDL_aligned_alloc(SDL_GetSIMDAlignment(), size);
  1414. if (!surface->pixels) {
  1415. /* Oh crap... */
  1416. surface->internal->flags |= SDL_INTERNAL_SURFACE_RLEACCEL;
  1417. return;
  1418. }
  1419. surface->flags |= SDL_SURFACE_SIMD_ALIGNED;
  1420. /* fill it with the background color */
  1421. SDL_FillSurfaceRect(surface, NULL, surface->internal->map.info.colorkey);
  1422. /* now render the encoded surface */
  1423. full.x = full.y = 0;
  1424. full.w = surface->w;
  1425. full.h = surface->h;
  1426. SDL_RLEBlit(surface, &full, surface, &full);
  1427. } else {
  1428. if (!UnRLEAlpha(surface)) {
  1429. /* Oh crap... */
  1430. surface->internal->flags |= SDL_INTERNAL_SURFACE_RLEACCEL;
  1431. return;
  1432. }
  1433. }
  1434. }
  1435. surface->internal->map.info.flags &=
  1436. ~(SDL_COPY_RLE_COLORKEY | SDL_COPY_RLE_ALPHAKEY);
  1437. SDL_free(surface->internal->map.data);
  1438. surface->internal->map.data = NULL;
  1439. }
  1440. }
  1441. #endif /* SDL_HAVE_RLE */