SDL_RLEaccel.c 59 KB

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