SDL_RLEaccel.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2026 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. #ifdef 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_surface_c.h"
  85. #include "SDL_pixels_c.h"
  86. #include "SDL_RLEaccel_c.h"
  87. #define PIXEL_COPY(to, from, len, bpp) \
  88. SDL_memcpy(to, from, (size_t)(len) * (bpp))
  89. /*
  90. * Various colorkey blit methods, for opaque and per-surface alpha
  91. */
  92. #define OPAQUE_BLIT(to, from, length, bpp, alpha) \
  93. PIXEL_COPY(to, from, length, bpp)
  94. /*
  95. * For 32bpp pixels on the form 0x00rrggbb:
  96. * If we treat the middle component separately, we can process the two
  97. * remaining in parallel. This is safe to do because of the gap to the left
  98. * of each component, so the bits from the multiplication don't collide.
  99. * This can be used for any RGB permutation of course.
  100. */
  101. #define ALPHA_BLIT32_888(to, from, length, bpp, alpha) \
  102. do { \
  103. int i; \
  104. Uint32 *src = (Uint32 *)(from); \
  105. Uint32 *dst = (Uint32 *)(to); \
  106. for (i = 0; i < (int)(length); i++) { \
  107. Uint32 s = *src++; \
  108. Uint32 d = *dst; \
  109. Uint32 s1 = s & 0xff00ff; \
  110. Uint32 d1 = d & 0xff00ff; \
  111. d1 = (d1 + ((s1 - d1) * alpha >> 8)) & 0xff00ff; \
  112. s &= 0xff00; \
  113. d &= 0xff00; \
  114. d = (d + ((s - d) * alpha >> 8)) & 0xff00; \
  115. *dst++ = d1 | d; \
  116. } \
  117. } while (0)
  118. /*
  119. * For 16bpp pixels we can go a step further: put the middle component
  120. * in the high 16 bits of a 32 bit word, and process all three RGB
  121. * components at the same time. Since the smallest gap is here just
  122. * 5 bits, we have to scale alpha down to 5 bits as well.
  123. */
  124. #define ALPHA_BLIT16_565(to, from, length, bpp, alpha) \
  125. do { \
  126. int i; \
  127. Uint16 *src = (Uint16 *)(from); \
  128. Uint16 *dst = (Uint16 *)(to); \
  129. Uint32 ALPHA = alpha >> 3; \
  130. for (i = 0; i < (int)(length); i++) { \
  131. Uint32 s = *src++; \
  132. Uint32 d = *dst; \
  133. s = (s | s << 16) & 0x07e0f81f; \
  134. d = (d | d << 16) & 0x07e0f81f; \
  135. d += (s - d) * ALPHA >> 5; \
  136. d &= 0x07e0f81f; \
  137. *dst++ = (Uint16)(d | d >> 16); \
  138. } \
  139. } while (0)
  140. #define ALPHA_BLIT16_555(to, from, length, bpp, alpha) \
  141. do { \
  142. int i; \
  143. Uint16 *src = (Uint16 *)(from); \
  144. Uint16 *dst = (Uint16 *)(to); \
  145. Uint32 ALPHA = alpha >> 3; \
  146. for (i = 0; i < (int)(length); i++) { \
  147. Uint32 s = *src++; \
  148. Uint32 d = *dst; \
  149. s = (s | s << 16) & 0x03e07c1f; \
  150. d = (d | d << 16) & 0x03e07c1f; \
  151. d += (s - d) * ALPHA >> 5; \
  152. d &= 0x03e07c1f; \
  153. *dst++ = (Uint16)(d | d >> 16); \
  154. } \
  155. } while (0)
  156. /*
  157. * The general slow catch-all function, for remaining depths and formats
  158. */
  159. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  160. #define SET_RGB24(dst, d) \
  161. dst[0] = (Uint8)(d >> 16); \
  162. dst[1] = (Uint8)(d >> 8); \
  163. dst[2] = (Uint8)(d);
  164. #else
  165. #define SET_RGB24(dst, d) \
  166. dst[0] = (Uint8)(d); \
  167. dst[1] = (Uint8)(d >> 8); \
  168. dst[2] = (Uint8)(d >> 16);
  169. #endif
  170. #define ALPHA_BLIT_ANY(to, from, length, bpp, alpha) \
  171. do { \
  172. int i; \
  173. Uint8 *src = from; \
  174. Uint8 *dst = to; \
  175. for (i = 0; i < (int)(length); i++) { \
  176. Uint32 s = 0, d = 0; \
  177. unsigned rs, gs, bs, rd, gd, bd; \
  178. switch (bpp) { \
  179. case 2: \
  180. s = *(Uint16 *)src; \
  181. d = *(Uint16 *)dst; \
  182. break; \
  183. case 3: \
  184. s = GET_RGB24(src); \
  185. d = GET_RGB24(dst); \
  186. break; \
  187. case 4: \
  188. s = *(Uint32 *)src; \
  189. d = *(Uint32 *)dst; \
  190. break; \
  191. } \
  192. RGB_FROM_PIXEL(s, fmt, rs, gs, bs); \
  193. RGB_FROM_PIXEL(d, fmt, rd, gd, bd); \
  194. rd += (rs - rd) * alpha >> 8; \
  195. gd += (gs - gd) * alpha >> 8; \
  196. bd += (bs - bd) * alpha >> 8; \
  197. PIXEL_FROM_RGB(d, fmt, rd, gd, bd); \
  198. switch (bpp) { \
  199. case 2: \
  200. *(Uint16 *)dst = (Uint16)d; \
  201. break; \
  202. case 3: \
  203. SET_RGB24(dst, d); \
  204. break; \
  205. case 4: \
  206. *(Uint32 *)dst = d; \
  207. break; \
  208. } \
  209. src += bpp; \
  210. dst += bpp; \
  211. } \
  212. } while (0)
  213. /*
  214. * Special case: 50% alpha (alpha=128)
  215. * This is treated specially because it can be optimized very well, and
  216. * since it is good for many cases of semi-translucency.
  217. * The theory is to do all three components at the same time:
  218. * First zero the lowest bit of each component, which gives us room to
  219. * add them. Then shift right and add the sum of the lowest bits.
  220. */
  221. #define ALPHA_BLIT32_888_50(to, from, length, bpp, alpha) \
  222. do { \
  223. int i; \
  224. Uint32 *src = (Uint32 *)(from); \
  225. Uint32 *dst = (Uint32 *)(to); \
  226. for (i = 0; i < (int)(length); i++) { \
  227. Uint32 s = *src++; \
  228. Uint32 d = *dst; \
  229. *dst++ = (((s & 0x00fefefe) + (d & 0x00fefefe)) >> 1) + (s & d & 0x00010101); \
  230. } \
  231. } while (0)
  232. /*
  233. * For 16bpp, we can actually blend two pixels in parallel, if we take
  234. * care to shift before we add, not after.
  235. */
  236. // helper: blend a single 16 bit pixel at 50%
  237. #define BLEND16_50(dst, src, mask) \
  238. do { \
  239. Uint32 s = *src++; \
  240. Uint32 d = *dst; \
  241. *dst++ = (Uint16)((((s & mask) + (d & mask)) >> 1) + \
  242. (s & d & (~mask & 0xffff))); \
  243. } while (0)
  244. // basic 16bpp blender. mask is the pixels to keep when adding.
  245. #define ALPHA_BLIT16_50(to, from, length, bpp, alpha, mask) \
  246. do { \
  247. unsigned n = (length); \
  248. Uint16 *src = (Uint16 *)(from); \
  249. Uint16 *dst = (Uint16 *)(to); \
  250. if (((uintptr_t)src ^ (uintptr_t)dst) & 3) { \
  251. /* source and destination not in phase, blit one by one */ \
  252. while (n--) \
  253. BLEND16_50(dst, src, mask); \
  254. } else { \
  255. if ((uintptr_t)src & 3) { \
  256. /* first odd pixel */ \
  257. BLEND16_50(dst, src, mask); \
  258. n--; \
  259. } \
  260. for (; n > 1; n -= 2) { \
  261. Uint32 s = *(Uint32 *)src; \
  262. Uint32 d = *(Uint32 *)dst; \
  263. *(Uint32 *)dst = ((s & (mask | mask << 16)) >> 1) + ((d & (mask | mask << 16)) >> 1) + (s & d & (~(mask | mask << 16))); \
  264. src += 2; \
  265. dst += 2; \
  266. } \
  267. if (n) \
  268. BLEND16_50(dst, src, mask); /* last odd pixel */ \
  269. } \
  270. } while (0)
  271. #define ALPHA_BLIT16_565_50(to, from, length, bpp, alpha) \
  272. ALPHA_BLIT16_50(to, from, length, bpp, alpha, 0xf7deU)
  273. #define ALPHA_BLIT16_555_50(to, from, length, bpp, alpha) \
  274. ALPHA_BLIT16_50(to, from, length, bpp, alpha, 0xfbdeU)
  275. #define CHOOSE_BLIT(blitter, alpha, fmt) \
  276. do { \
  277. if (alpha == 255) { \
  278. switch (fmt->bytes_per_pixel) { \
  279. case 1: \
  280. blitter(1, Uint8, OPAQUE_BLIT); \
  281. break; \
  282. case 2: \
  283. blitter(2, Uint8, OPAQUE_BLIT); \
  284. break; \
  285. case 3: \
  286. blitter(3, Uint8, OPAQUE_BLIT); \
  287. break; \
  288. case 4: \
  289. blitter(4, Uint16, OPAQUE_BLIT); \
  290. break; \
  291. } \
  292. } else { \
  293. switch (fmt->bytes_per_pixel) { \
  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 || fmt->Rmask == 0x07e0 || fmt->Bmask == 0x07e0) { \
  302. if (alpha == 128) { \
  303. blitter(2, Uint8, ALPHA_BLIT16_565_50); \
  304. } else { \
  305. blitter(2, Uint8, ALPHA_BLIT16_565); \
  306. } \
  307. } else { \
  308. goto general16; \
  309. } \
  310. break; \
  311. \
  312. case 0x7fff: \
  313. if (fmt->Gmask == 0x03e0 || fmt->Rmask == 0x03e0 || fmt->Bmask == 0x03e0) { \
  314. if (alpha == 128) { \
  315. blitter(2, Uint8, ALPHA_BLIT16_555_50); \
  316. } else { \
  317. blitter(2, Uint8, ALPHA_BLIT16_555); \
  318. } \
  319. break; \
  320. } else { \
  321. goto general16; \
  322. } \
  323. break; \
  324. \
  325. default: \
  326. general16: \
  327. blitter(2, Uint8, ALPHA_BLIT_ANY); \
  328. } \
  329. break; \
  330. \
  331. case 3: \
  332. blitter(3, Uint8, ALPHA_BLIT_ANY); \
  333. break; \
  334. \
  335. case 4: \
  336. if ((fmt->Rmask | fmt->Gmask | fmt->Bmask) == 0x00ffffff && (fmt->Gmask == 0xff00 || fmt->Rmask == 0xff00 || fmt->Bmask == 0xff00)) { \
  337. if (alpha == 128) { \
  338. blitter(4, Uint16, ALPHA_BLIT32_888_50); \
  339. } else { \
  340. blitter(4, Uint16, ALPHA_BLIT32_888); \
  341. } \
  342. } else { \
  343. blitter(4, Uint16, ALPHA_BLIT_ANY); \
  344. } \
  345. break; \
  346. } \
  347. } \
  348. } while (0)
  349. /*
  350. * Set a pixel value using the given format, except that the alpha value is
  351. * placed in the top byte. This is the format used for RLE with alpha.
  352. */
  353. #define RLEPIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a) \
  354. { \
  355. Pixel = ((r >> (8 - fmt->Rbits)) << fmt->Rshift) | \
  356. ((g >> (8 - fmt->Gbits)) << fmt->Gshift) | \
  357. ((b >> (8 - fmt->Bbits)) << fmt->Bshift) | \
  358. (a << 24); \
  359. }
  360. /*
  361. * This takes care of the case when the surface is clipped on the left and/or
  362. * right. Top clipping has already been taken care of.
  363. */
  364. #define RLECLIPBLIT(bpp, Type, do_blit) \
  365. do { \
  366. int linecount = srcrect->h; \
  367. int ofs = 0; \
  368. int left = srcrect->x; \
  369. int right = left + srcrect->w; \
  370. dstbuf -= left * bpp; \
  371. for (;;) { \
  372. int run; \
  373. ofs += *(Type *)srcbuf; \
  374. run = ((Type *)srcbuf)[1]; \
  375. srcbuf += 2 * sizeof(Type); \
  376. if (run) { \
  377. /* clip to left and right borders */ \
  378. if (ofs < right) { \
  379. int start = 0; \
  380. int len = run; \
  381. int startcol; \
  382. if (left - ofs > 0) { \
  383. start = left - ofs; \
  384. len -= start; \
  385. if (len <= 0) \
  386. goto nocopy##bpp##do_blit; \
  387. } \
  388. startcol = ofs + start; \
  389. if (len > right - startcol) \
  390. len = right - startcol; \
  391. do_blit(dstbuf + startcol * bpp, srcbuf + start * bpp, \
  392. len, bpp, alpha); \
  393. } \
  394. nocopy##bpp##do_blit : srcbuf += run * bpp; \
  395. ofs += run; \
  396. } else if (!ofs) { \
  397. break; \
  398. } \
  399. \
  400. if (ofs == w) { \
  401. ofs = 0; \
  402. dstbuf += surf_dst->pitch; \
  403. if (!--linecount) { \
  404. break; \
  405. } \
  406. } \
  407. } \
  408. } while (0)
  409. static void RLEClipBlit(int w, Uint8 *srcbuf, SDL_Surface *surf_dst,
  410. Uint8 *dstbuf, const SDL_Rect *srcrect, unsigned alpha)
  411. {
  412. const SDL_PixelFormatDetails *fmt = surf_dst->fmt;
  413. CHOOSE_BLIT(RLECLIPBLIT, alpha, fmt);
  414. }
  415. #undef RLECLIPBLIT
  416. // blit a colorkeyed RLE surface
  417. static bool SDLCALL SDL_RLEBlit(SDL_Surface *surf_src, const SDL_Rect *srcrect,
  418. SDL_Surface *surf_dst, const SDL_Rect *dstrect)
  419. {
  420. Uint8 *dstbuf;
  421. Uint8 *srcbuf;
  422. int x, y;
  423. int w = surf_src->w;
  424. unsigned alpha;
  425. // Lock the destination if necessary
  426. if (SDL_MUSTLOCK(surf_dst)) {
  427. if (!SDL_LockSurface(surf_dst)) {
  428. return false;
  429. }
  430. }
  431. // Set up the source and destination pointers
  432. x = dstrect->x;
  433. y = dstrect->y;
  434. dstbuf = (Uint8 *)surf_dst->pixels + y * surf_dst->pitch + x * surf_src->fmt->bytes_per_pixel;
  435. srcbuf = (Uint8 *)surf_src->map.data + sizeof(SDL_PixelFormat);
  436. {
  437. // skip lines at the top if necessary
  438. int vskip = srcrect->y;
  439. int ofs = 0;
  440. if (vskip) {
  441. #define RLESKIP(bpp, Type) \
  442. for (;;) { \
  443. int run; \
  444. ofs += *(Type *)srcbuf; \
  445. run = ((Type *)srcbuf)[1]; \
  446. srcbuf += sizeof(Type) * 2; \
  447. if (run) { \
  448. srcbuf += run * bpp; \
  449. ofs += run; \
  450. } else if (!ofs) \
  451. goto done; \
  452. if (ofs == w) { \
  453. ofs = 0; \
  454. if (!--vskip) \
  455. break; \
  456. } \
  457. }
  458. switch (surf_src->fmt->bytes_per_pixel) {
  459. case 1:
  460. RLESKIP(1, Uint8);
  461. break;
  462. case 2:
  463. RLESKIP(2, Uint8);
  464. break;
  465. case 3:
  466. RLESKIP(3, Uint8);
  467. break;
  468. case 4:
  469. RLESKIP(4, Uint16);
  470. break;
  471. }
  472. #undef RLESKIP
  473. }
  474. }
  475. alpha = surf_src->map.info.a;
  476. // if left or right edge clipping needed, call clip blit
  477. if (srcrect->x || srcrect->w != surf_src->w) {
  478. RLEClipBlit(w, srcbuf, surf_dst, dstbuf, srcrect, alpha);
  479. } else {
  480. const SDL_PixelFormatDetails *fmt = surf_src->fmt;
  481. #define RLEBLIT(bpp, Type, do_blit) \
  482. do { \
  483. int linecount = srcrect->h; \
  484. int ofs = 0; \
  485. for (;;) { \
  486. unsigned run; \
  487. ofs += *(Type *)srcbuf; \
  488. run = ((Type *)srcbuf)[1]; \
  489. srcbuf += 2 * sizeof(Type); \
  490. if (run) { \
  491. do_blit(dstbuf + ofs * bpp, srcbuf, run, bpp, alpha); \
  492. srcbuf += run * bpp; \
  493. ofs += run; \
  494. } else if (!ofs) \
  495. break; \
  496. if (ofs == w) { \
  497. ofs = 0; \
  498. dstbuf += surf_dst->pitch; \
  499. if (!--linecount) \
  500. break; \
  501. } \
  502. } \
  503. } while (0)
  504. CHOOSE_BLIT(RLEBLIT, alpha, fmt);
  505. #undef RLEBLIT
  506. }
  507. done:
  508. // Unlock the destination if necessary
  509. if (SDL_MUSTLOCK(surf_dst)) {
  510. SDL_UnlockSurface(surf_dst);
  511. }
  512. return true;
  513. }
  514. #undef OPAQUE_BLIT
  515. /*
  516. * Per-pixel blitting macros for translucent pixels:
  517. * These use the same techniques as the per-surface blitting macros
  518. */
  519. /*
  520. * For 32bpp pixels, we have made sure the alpha is stored in the top
  521. * 8 bits, so proceed as usual
  522. */
  523. #define BLIT_TRANSL_888(src, dst) \
  524. do { \
  525. Uint32 s = src; \
  526. Uint32 d = dst; \
  527. unsigned alpha = s >> 24; \
  528. Uint32 s1 = s & 0xff00ff; \
  529. Uint32 d1 = d & 0xff00ff; \
  530. d1 = (d1 + ((s1 - d1) * alpha >> 8)) & 0xff00ff; \
  531. s &= 0xff00; \
  532. d &= 0xff00; \
  533. d = (d + ((s - d) * alpha >> 8)) & 0xff00; \
  534. dst = d1 | d | 0xff000000; \
  535. } while (0)
  536. /*
  537. * For 16bpp pixels, we have stored the 5 most significant alpha bits in
  538. * bits 5-10. As before, we can process all 3 RGB components at the same time.
  539. */
  540. #define BLIT_TRANSL_565(src, dst) \
  541. do { \
  542. Uint32 s = src; \
  543. Uint32 d = dst; \
  544. unsigned alpha = (s & 0x3e0) >> 5; \
  545. s &= 0x07e0f81f; \
  546. d = (d | d << 16) & 0x07e0f81f; \
  547. d += (s - d) * alpha >> 5; \
  548. d &= 0x07e0f81f; \
  549. dst = (Uint16)(d | d >> 16); \
  550. } while (0)
  551. #define BLIT_TRANSL_555(src, dst) \
  552. do { \
  553. Uint32 s = src; \
  554. Uint32 d = dst; \
  555. unsigned alpha = (s & 0x3e0) >> 5; \
  556. s &= 0x03e07c1f; \
  557. d = (d | d << 16) & 0x03e07c1f; \
  558. d += (s - d) * alpha >> 5; \
  559. d &= 0x03e07c1f; \
  560. dst = (Uint16)(d | d >> 16); \
  561. } while (0)
  562. // blit a pixel-alpha RLE surface clipped at the right and/or left edges
  563. static void RLEAlphaClipBlit(int w, Uint8 *srcbuf, SDL_Surface *surf_dst,
  564. Uint8 *dstbuf, const SDL_Rect *srcrect)
  565. {
  566. const SDL_PixelFormatDetails *df = surf_dst->fmt;
  567. /*
  568. * clipped blitter: Ptype is the destination pixel type,
  569. * Ctype the translucent count type, and do_blend the macro
  570. * to blend one pixel.
  571. */
  572. #define RLEALPHACLIPBLIT(Ptype, Ctype, do_blend) \
  573. do { \
  574. int linecount = srcrect->h; \
  575. int left = srcrect->x; \
  576. int right = left + srcrect->w; \
  577. dstbuf -= left * sizeof(Ptype); \
  578. do { \
  579. int ofs = 0; \
  580. /* blit opaque pixels on one line */ \
  581. do { \
  582. unsigned run; \
  583. ofs += ((Ctype *)srcbuf)[0]; \
  584. run = ((Ctype *)srcbuf)[1]; \
  585. srcbuf += 2 * sizeof(Ctype); \
  586. if (run) { \
  587. /* clip to left and right borders */ \
  588. int cofs = ofs; \
  589. int crun = run; \
  590. if (left - cofs > 0) { \
  591. crun -= left - cofs; \
  592. cofs = left; \
  593. } \
  594. if (crun > right - cofs) \
  595. crun = right - cofs; \
  596. if (crun > 0) \
  597. PIXEL_COPY(dstbuf + cofs * sizeof(Ptype), \
  598. srcbuf + (cofs - ofs) * sizeof(Ptype), \
  599. (unsigned)crun, sizeof(Ptype)); \
  600. srcbuf += run * sizeof(Ptype); \
  601. ofs += run; \
  602. } else if (!ofs) \
  603. return; \
  604. } while (ofs < w); \
  605. /* skip padding if necessary */ \
  606. const size_t psize = sizeof(Ptype); \
  607. if (psize == 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 bool 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->fmt;
  661. // Lock the destination if necessary
  662. if (SDL_MUSTLOCK(surf_dst)) {
  663. if (!SDL_LockSurface(surf_dst)) {
  664. return false;
  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->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. const size_t psize = sizeof(Ptype); \
  756. if (psize == 2) \
  757. srcbuf += (uintptr_t)srcbuf & 2; \
  758. /* blit translucent pixels on the same line */ \
  759. ofs = 0; \
  760. do { \
  761. unsigned run; \
  762. ofs += ((Uint16 *)srcbuf)[0]; \
  763. run = ((Uint16 *)srcbuf)[1]; \
  764. srcbuf += 4; \
  765. if (run) { \
  766. Ptype *dst = (Ptype *)dstbuf + ofs; \
  767. unsigned i; \
  768. for (i = 0; i < run; i++) { \
  769. Uint32 src = *(Uint32 *)srcbuf; \
  770. do_blend(src, *dst); \
  771. srcbuf += 4; \
  772. dst++; \
  773. } \
  774. ofs += run; \
  775. } \
  776. } while (ofs < w); \
  777. dstbuf += surf_dst->pitch; \
  778. } while (--linecount); \
  779. } while (0)
  780. switch (df->bytes_per_pixel) {
  781. case 2:
  782. if (df->Gmask == 0x07e0 || df->Rmask == 0x07e0 || df->Bmask == 0x07e0) {
  783. RLEALPHABLIT(Uint16, Uint8, BLIT_TRANSL_565);
  784. } else {
  785. RLEALPHABLIT(Uint16, Uint8, BLIT_TRANSL_555);
  786. }
  787. break;
  788. case 4:
  789. RLEALPHABLIT(Uint32, Uint16, BLIT_TRANSL_888);
  790. break;
  791. }
  792. }
  793. done:
  794. // Unlock the destination if necessary
  795. if (SDL_MUSTLOCK(surf_dst)) {
  796. SDL_UnlockSurface(surf_dst);
  797. }
  798. return true;
  799. }
  800. /*
  801. * Auxiliary functions:
  802. * The encoding functions take 32bpp rgb + a, and
  803. * return the number of bytes copied to the destination.
  804. * The decoding functions copy to 32bpp rgb + a, and
  805. * return the number of bytes copied from the source.
  806. * These are only used in the encoder and un-RLE code and are therefore not
  807. * highly optimised.
  808. */
  809. // encode 32bpp rgb + a into 16bpp rgb, losing alpha
  810. static int copy_opaque_16(void *dst, const Uint32 *src, int n,
  811. const SDL_PixelFormatDetails *sfmt, const SDL_PixelFormatDetails *dfmt)
  812. {
  813. int i;
  814. Uint16 *d = (Uint16 *)dst;
  815. for (i = 0; i < n; i++) {
  816. unsigned r, g, b;
  817. RGB_FROM_PIXEL(*src, sfmt, r, g, b);
  818. PIXEL_FROM_RGB(*d, dfmt, r, g, b);
  819. src++;
  820. d++;
  821. }
  822. return n * 2;
  823. }
  824. // encode 32bpp rgb + a into 32bpp G0RAB format for blitting into 565
  825. static int copy_transl_565(void *dst, const Uint32 *src, int n,
  826. const SDL_PixelFormatDetails *sfmt, const SDL_PixelFormatDetails *dfmt)
  827. {
  828. int i;
  829. Uint32 *d = (Uint32 *)dst;
  830. for (i = 0; i < n; i++) {
  831. unsigned r, g, b, a;
  832. Uint16 pix;
  833. RGBA_FROM_8888(*src, sfmt, r, g, b, a);
  834. PIXEL_FROM_RGB(pix, dfmt, r, g, b);
  835. *d = ((pix & 0x7e0) << 16) | (pix & 0xf81f) | ((a << 2) & 0x7e0);
  836. src++;
  837. d++;
  838. }
  839. return n * 4;
  840. }
  841. // encode 32bpp rgb + a into 32bpp G0RAB format for blitting into 555
  842. static int copy_transl_555(void *dst, const Uint32 *src, int n,
  843. const SDL_PixelFormatDetails *sfmt, const SDL_PixelFormatDetails *dfmt)
  844. {
  845. int i;
  846. Uint32 *d = (Uint32 *)dst;
  847. for (i = 0; i < n; i++) {
  848. unsigned r, g, b, a;
  849. Uint16 pix;
  850. RGBA_FROM_8888(*src, sfmt, r, g, b, a);
  851. PIXEL_FROM_RGB(pix, dfmt, r, g, b);
  852. *d = ((pix & 0x3e0) << 16) | (pix & 0xfc1f) | ((a << 2) & 0x3e0);
  853. src++;
  854. d++;
  855. }
  856. return n * 4;
  857. }
  858. // encode 32bpp rgba into 32bpp rgba, keeping alpha (dual purpose)
  859. static int copy_32(void *dst, const Uint32 *src, int n,
  860. const SDL_PixelFormatDetails *sfmt, const SDL_PixelFormatDetails *dfmt)
  861. {
  862. int i;
  863. Uint32 *d = (Uint32 *)dst;
  864. for (i = 0; i < n; i++) {
  865. unsigned r, g, b, a;
  866. RGBA_FROM_8888(*src, sfmt, r, g, b, a);
  867. RLEPIXEL_FROM_RGBA(*d, dfmt, r, g, b, a);
  868. d++;
  869. src++;
  870. }
  871. return n * 4;
  872. }
  873. #define ISOPAQUE(pixel, fmt) ((((pixel)&fmt->Amask) >> fmt->Ashift) == 255)
  874. #define ISTRANSL(pixel, fmt) \
  875. ((unsigned)((((pixel)&fmt->Amask) >> fmt->Ashift) - 1U) < 254U)
  876. // convert surface to be quickly alpha-blittable onto dest, if possible
  877. static bool RLEAlphaSurface(SDL_Surface *surface)
  878. {
  879. SDL_Surface *dest;
  880. const SDL_PixelFormatDetails *df;
  881. int maxsize = 0;
  882. int max_opaque_run;
  883. int max_transl_run = 65535;
  884. unsigned masksum;
  885. Uint8 *rlebuf, *dst;
  886. int (*copy_opaque)(void *, const Uint32 *, int,
  887. const SDL_PixelFormatDetails *, const SDL_PixelFormatDetails *);
  888. int (*copy_transl)(void *, const Uint32 *, int,
  889. const SDL_PixelFormatDetails *, const SDL_PixelFormatDetails *);
  890. dest = surface->map.info.dst_surface;
  891. if (!dest) {
  892. return false;
  893. }
  894. df = dest->fmt;
  895. if (surface->fmt->bits_per_pixel != 32) {
  896. return false; // only 32bpp source supported
  897. }
  898. /* find out whether the destination is one we support,
  899. and determine the max size of the encoded result */
  900. masksum = df->Rmask | df->Gmask | df->Bmask;
  901. switch (df->bytes_per_pixel) {
  902. case 2:
  903. // 16bpp: only support 565 and 555 formats
  904. switch (masksum) {
  905. case 0xffff:
  906. if (df->Gmask == 0x07e0 || df->Rmask == 0x07e0 || df->Bmask == 0x07e0) {
  907. copy_opaque = copy_opaque_16;
  908. copy_transl = copy_transl_565;
  909. } else {
  910. return false;
  911. }
  912. break;
  913. case 0x7fff:
  914. if (df->Gmask == 0x03e0 || df->Rmask == 0x03e0 || df->Bmask == 0x03e0) {
  915. copy_opaque = copy_opaque_16;
  916. copy_transl = copy_transl_555;
  917. } else {
  918. return false;
  919. }
  920. break;
  921. default:
  922. return false;
  923. }
  924. max_opaque_run = 255; // runs stored as bytes
  925. /* worst case is alternating opaque and translucent pixels,
  926. with room for alignment padding between lines */
  927. maxsize = surface->h * (2 + (4 + 2) * (surface->w + 1)) + 2;
  928. break;
  929. case 4:
  930. if (masksum != 0x00ffffff) {
  931. return false; // requires unused high byte
  932. }
  933. copy_opaque = copy_32;
  934. copy_transl = copy_32;
  935. max_opaque_run = 255; // runs stored as short ints
  936. // worst case is alternating opaque and translucent pixels
  937. maxsize = surface->h * 2 * 4 * (surface->w + 1) + 4;
  938. break;
  939. default:
  940. return false; // anything else unsupported right now
  941. }
  942. maxsize += sizeof(SDL_PixelFormat);
  943. rlebuf = (Uint8 *)SDL_malloc(maxsize);
  944. if (!rlebuf) {
  945. return false;
  946. }
  947. // save the destination format so we can undo the encoding later
  948. *(SDL_PixelFormat *)rlebuf = dest->format;
  949. dst = rlebuf + sizeof(SDL_PixelFormat);
  950. // Do the actual encoding
  951. {
  952. int x, y;
  953. int h = surface->h, w = surface->w;
  954. const SDL_PixelFormatDetails *sf = surface->fmt;
  955. Uint32 *src = (Uint32 *)surface->pixels;
  956. Uint8 *lastline = dst; // end of last non-blank line
  957. // opaque counts are 8 or 16 bits, depending on target depth
  958. #define ADD_OPAQUE_COUNTS(n, m) \
  959. if (df->bytes_per_pixel == 4) { \
  960. ((Uint16 *)dst)[0] = (Uint16)n; \
  961. ((Uint16 *)dst)[1] = (Uint16)m; \
  962. dst += 4; \
  963. } else { \
  964. dst[0] = (Uint8)n; \
  965. dst[1] = (Uint8)m; \
  966. dst += 2; \
  967. }
  968. // translucent counts are always 16 bit
  969. #define ADD_TRANSL_COUNTS(n, m) \
  970. (((Uint16 *)dst)[0] = (Uint16)n, ((Uint16 *)dst)[1] = (Uint16)m, dst += 4)
  971. for (y = 0; y < h; y++) {
  972. int runstart, skipstart;
  973. int blankline = 0;
  974. // First encode all opaque pixels of a scan line
  975. x = 0;
  976. do {
  977. int run, skip, len;
  978. skipstart = x;
  979. while (x < w && !ISOPAQUE(src[x], sf)) {
  980. x++;
  981. }
  982. runstart = x;
  983. while (x < w && ISOPAQUE(src[x], sf)) {
  984. x++;
  985. }
  986. skip = runstart - skipstart;
  987. if (skip == w) {
  988. blankline = 1;
  989. }
  990. run = x - runstart;
  991. while (skip > max_opaque_run) {
  992. ADD_OPAQUE_COUNTS(max_opaque_run, 0);
  993. skip -= max_opaque_run;
  994. }
  995. len = SDL_min(run, max_opaque_run);
  996. ADD_OPAQUE_COUNTS(skip, len);
  997. dst += copy_opaque(dst, src + runstart, len, sf, df);
  998. runstart += len;
  999. run -= len;
  1000. while (run) {
  1001. len = SDL_min(run, max_opaque_run);
  1002. ADD_OPAQUE_COUNTS(0, len);
  1003. dst += copy_opaque(dst, src + runstart, len, sf, df);
  1004. runstart += len;
  1005. run -= len;
  1006. }
  1007. } while (x < w);
  1008. // Make sure the next output address is 32-bit aligned
  1009. dst += (uintptr_t)dst & 2;
  1010. // Next, encode all translucent pixels of the same scan line
  1011. x = 0;
  1012. do {
  1013. int run, skip, len;
  1014. skipstart = x;
  1015. while (x < w && !ISTRANSL(src[x], sf)) {
  1016. x++;
  1017. }
  1018. runstart = x;
  1019. while (x < w && ISTRANSL(src[x], sf)) {
  1020. x++;
  1021. }
  1022. skip = runstart - skipstart;
  1023. blankline &= (skip == w);
  1024. run = x - runstart;
  1025. while (skip > max_transl_run) {
  1026. ADD_TRANSL_COUNTS(max_transl_run, 0);
  1027. skip -= max_transl_run;
  1028. }
  1029. len = SDL_min(run, max_transl_run);
  1030. ADD_TRANSL_COUNTS(skip, len);
  1031. dst += copy_transl(dst, src + runstart, len, sf, df);
  1032. runstart += len;
  1033. run -= len;
  1034. while (run) {
  1035. len = SDL_min(run, max_transl_run);
  1036. ADD_TRANSL_COUNTS(0, len);
  1037. dst += copy_transl(dst, src + runstart, len, sf, df);
  1038. runstart += len;
  1039. run -= len;
  1040. }
  1041. if (!blankline) {
  1042. lastline = dst;
  1043. }
  1044. } while (x < w);
  1045. src += surface->pitch >> 2;
  1046. }
  1047. dst = lastline; // back up past trailing blank lines
  1048. ADD_OPAQUE_COUNTS(0, 0);
  1049. }
  1050. #undef ADD_OPAQUE_COUNTS
  1051. #undef ADD_TRANSL_COUNTS
  1052. // reallocate the buffer to release unused memory
  1053. {
  1054. Uint8 *p = (Uint8 *)SDL_realloc(rlebuf, dst - rlebuf);
  1055. if (!p) {
  1056. p = rlebuf;
  1057. }
  1058. surface->map.data = p;
  1059. }
  1060. return true;
  1061. }
  1062. static Uint32 getpix_8(const Uint8 *srcbuf)
  1063. {
  1064. return *srcbuf;
  1065. }
  1066. static Uint32 getpix_16(const Uint8 *srcbuf)
  1067. {
  1068. return *(const Uint16 *)srcbuf;
  1069. }
  1070. static Uint32 getpix_24(const Uint8 *srcbuf)
  1071. {
  1072. #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  1073. return srcbuf[0] + (srcbuf[1] << 8) + (srcbuf[2] << 16);
  1074. #else
  1075. return (srcbuf[0] << 16) + (srcbuf[1] << 8) + srcbuf[2];
  1076. #endif
  1077. }
  1078. static Uint32 getpix_32(const Uint8 *srcbuf)
  1079. {
  1080. return *(const Uint32 *)srcbuf;
  1081. }
  1082. typedef Uint32 (*getpix_func)(const Uint8 *);
  1083. static const getpix_func getpixes[4] = {
  1084. getpix_8, getpix_16, getpix_24, getpix_32
  1085. };
  1086. static bool RLEColorkeySurface(SDL_Surface *surface)
  1087. {
  1088. SDL_Surface *dest;
  1089. Uint8 *rlebuf, *dst;
  1090. int maxn;
  1091. int y;
  1092. Uint8 *srcbuf, *lastline;
  1093. int maxsize = 0;
  1094. const int bpp = surface->fmt->bytes_per_pixel;
  1095. getpix_func getpix;
  1096. Uint32 ckey, rgbmask;
  1097. int w, h;
  1098. dest = surface->map.info.dst_surface;
  1099. if (!dest) {
  1100. return false;
  1101. }
  1102. // calculate the worst case size for the compressed surface
  1103. switch (bpp) {
  1104. case 1:
  1105. /* worst case is alternating opaque and transparent pixels,
  1106. starting with an opaque pixel */
  1107. maxsize = surface->h * 3 * (surface->w / 2 + 1) + 2;
  1108. break;
  1109. case 2:
  1110. case 3:
  1111. // worst case is solid runs, at most 255 pixels wide
  1112. maxsize = surface->h * (2 * (surface->w / 255 + 1) + surface->w * bpp) + 2;
  1113. break;
  1114. case 4:
  1115. // worst case is solid runs, at most 65535 pixels wide
  1116. maxsize = surface->h * (4 * (surface->w / 65535 + 1) + surface->w * 4) + 4;
  1117. break;
  1118. default:
  1119. return false;
  1120. }
  1121. maxsize += sizeof(SDL_PixelFormat);
  1122. rlebuf = (Uint8 *)SDL_malloc(maxsize);
  1123. if (!rlebuf) {
  1124. return false;
  1125. }
  1126. // save the destination format so we can undo the encoding later
  1127. *(SDL_PixelFormat *)rlebuf = dest->format;
  1128. // Set up the conversion
  1129. srcbuf = (Uint8 *)surface->pixels;
  1130. maxn = bpp == 4 ? 65535 : 255;
  1131. dst = rlebuf + sizeof(SDL_PixelFormat);
  1132. rgbmask = ~surface->fmt->Amask;
  1133. ckey = surface->map.info.colorkey & rgbmask;
  1134. lastline = dst;
  1135. getpix = getpixes[bpp - 1];
  1136. w = surface->w;
  1137. h = surface->h;
  1138. #define ADD_COUNTS(n, m) \
  1139. if (bpp == 4) { \
  1140. ((Uint16 *)dst)[0] = (Uint16)n; \
  1141. ((Uint16 *)dst)[1] = (Uint16)m; \
  1142. dst += 4; \
  1143. } else { \
  1144. dst[0] = (Uint8)n; \
  1145. dst[1] = (Uint8)m; \
  1146. dst += 2; \
  1147. }
  1148. for (y = 0; y < h; y++) {
  1149. int x = 0;
  1150. int blankline = 0;
  1151. do {
  1152. int run, skip;
  1153. int len;
  1154. int runstart;
  1155. int skipstart = x;
  1156. // find run of transparent, then opaque pixels
  1157. while (x < w && (getpix(srcbuf + x * bpp) & rgbmask) == ckey) {
  1158. x++;
  1159. }
  1160. runstart = x;
  1161. while (x < w && (getpix(srcbuf + x * bpp) & rgbmask) != ckey) {
  1162. x++;
  1163. }
  1164. skip = runstart - skipstart;
  1165. if (skip == w) {
  1166. blankline = 1;
  1167. }
  1168. run = x - runstart;
  1169. // encode segment
  1170. while (skip > maxn) {
  1171. ADD_COUNTS(maxn, 0);
  1172. skip -= maxn;
  1173. }
  1174. len = SDL_min(run, maxn);
  1175. ADD_COUNTS(skip, len);
  1176. SDL_memcpy(dst, srcbuf + runstart * bpp, (size_t)len * bpp);
  1177. dst += len * bpp;
  1178. run -= len;
  1179. runstart += len;
  1180. while (run) {
  1181. len = SDL_min(run, maxn);
  1182. ADD_COUNTS(0, len);
  1183. SDL_memcpy(dst, srcbuf + runstart * bpp, (size_t)len * bpp);
  1184. dst += len * bpp;
  1185. runstart += len;
  1186. run -= len;
  1187. }
  1188. if (!blankline) {
  1189. lastline = dst;
  1190. }
  1191. } while (x < w);
  1192. srcbuf += surface->pitch;
  1193. }
  1194. dst = lastline; // back up bast trailing blank lines
  1195. ADD_COUNTS(0, 0);
  1196. #undef ADD_COUNTS
  1197. // reallocate the buffer to release unused memory
  1198. {
  1199. // If SDL_realloc returns NULL, the original block is left intact
  1200. Uint8 *p = (Uint8 *)SDL_realloc(rlebuf, dst - rlebuf);
  1201. if (!p) {
  1202. p = rlebuf;
  1203. }
  1204. surface->map.data = p;
  1205. }
  1206. return true;
  1207. }
  1208. bool SDL_RLESurface(SDL_Surface *surface)
  1209. {
  1210. int flags;
  1211. // Clear any previous RLE conversion
  1212. if (surface->internal_flags & SDL_INTERNAL_SURFACE_RLEACCEL) {
  1213. SDL_UnRLESurface(surface);
  1214. }
  1215. // We don't support RLE encoding of bitmaps
  1216. if (SDL_BITSPERPIXEL(surface->format) < 8) {
  1217. return false;
  1218. }
  1219. // Make sure the pixels are available
  1220. if (!surface->pixels) {
  1221. return false;
  1222. }
  1223. flags = surface->map.info.flags;
  1224. if (flags & SDL_COPY_COLORKEY) {
  1225. // ok
  1226. } else if ((flags & SDL_COPY_BLEND) && SDL_ISPIXELFORMAT_ALPHA(surface->format)) {
  1227. // ok
  1228. } else {
  1229. // If we don't have colorkey or blending, nothing to do...
  1230. return false;
  1231. }
  1232. // Pass on combinations not supported
  1233. if ((flags & SDL_COPY_MODULATE_COLOR) ||
  1234. ((flags & SDL_COPY_MODULATE_ALPHA) && SDL_ISPIXELFORMAT_ALPHA(surface->format)) ||
  1235. (flags & (SDL_COPY_BLEND_PREMULTIPLIED | SDL_COPY_ADD | SDL_COPY_ADD_PREMULTIPLIED | SDL_COPY_MOD | SDL_COPY_MUL)) ||
  1236. (flags & SDL_COPY_NEAREST)) {
  1237. return false;
  1238. }
  1239. // Encode and set up the blit
  1240. if (!SDL_ISPIXELFORMAT_ALPHA(surface->format) || !(flags & SDL_COPY_BLEND)) {
  1241. if (!surface->map.identity) {
  1242. return false;
  1243. }
  1244. if (!RLEColorkeySurface(surface)) {
  1245. return false;
  1246. }
  1247. surface->map.blit = SDL_RLEBlit;
  1248. surface->map.info.flags |= SDL_COPY_RLE_COLORKEY;
  1249. } else {
  1250. if (!RLEAlphaSurface(surface)) {
  1251. return false;
  1252. }
  1253. surface->map.blit = SDL_RLEAlphaBlit;
  1254. surface->map.info.flags |= SDL_COPY_RLE_ALPHAKEY;
  1255. }
  1256. if (!(surface->flags & SDL_SURFACE_PREALLOCATED)) {
  1257. surface->saved_pixels = surface->pixels;
  1258. surface->pixels = NULL;
  1259. }
  1260. // The surface is now accelerated
  1261. surface->internal_flags |= SDL_INTERNAL_SURFACE_RLEACCEL;
  1262. SDL_UpdateSurfaceLockFlag(surface);
  1263. return true;
  1264. }
  1265. void SDL_UnRLESurface(SDL_Surface *surface)
  1266. {
  1267. if (surface->internal_flags & SDL_INTERNAL_SURFACE_RLEACCEL) {
  1268. surface->internal_flags &= ~SDL_INTERNAL_SURFACE_RLEACCEL;
  1269. surface->map.info.flags &= ~(SDL_COPY_RLE_COLORKEY | SDL_COPY_RLE_ALPHAKEY);
  1270. if (!(surface->flags & SDL_SURFACE_PREALLOCATED)) {
  1271. surface->pixels = surface->saved_pixels;
  1272. surface->saved_pixels = NULL;
  1273. }
  1274. SDL_free(surface->map.data);
  1275. surface->map.data = NULL;
  1276. SDL_InvalidateMap(&surface->map);
  1277. SDL_UpdateSurfaceLockFlag(surface);
  1278. }
  1279. }
  1280. #endif // SDL_HAVE_RLE