#include "SDL_internal.h"
#ifdef SDL_HAVE_RLE
#include "SDL_sysvideo.h"
#include "SDL_surface_c.h"
#include "SDL_pixels_c.h"
#include "SDL_RLEaccel_c.h"
#define PIXEL_COPY(to, from, len, bpp) \
SDL_memcpy(to, from, (size_t)(len) * (bpp))
#define OPAQUE_BLIT(to, from, length, bpp, alpha) \
PIXEL_COPY(to, from, length, bpp)
#define ALPHA_BLIT32_888(to, from, length, bpp, alpha) \
do { \
int i; \
Uint32 *src = (Uint32 *)(from); \
Uint32 *dst = (Uint32 *)(to); \
for (i = 0; i < (int)(length); i++) { \
Uint32 s = *src++; \
Uint32 d = *dst; \
Uint32 s1 = s & 0xff00ff; \
Uint32 d1 = d & 0xff00ff; \
d1 = (d1 + ((s1 - d1) * alpha >> 8)) & 0xff00ff; \
s &= 0xff00; \
d &= 0xff00; \
d = (d + ((s - d) * alpha >> 8)) & 0xff00; \
*dst++ = d1 | d; \
} \
} while (0)
#define ALPHA_BLIT16_565(to, from, length, bpp, alpha) \
do { \
int i; \
Uint16 *src = (Uint16 *)(from); \
Uint16 *dst = (Uint16 *)(to); \
Uint32 ALPHA = alpha >> 3; \
for (i = 0; i < (int)(length); i++) { \
Uint32 s = *src++; \
Uint32 d = *dst; \
s = (s | s << 16) & 0x07e0f81f; \
d = (d | d << 16) & 0x07e0f81f; \
d += (s - d) * ALPHA >> 5; \
d &= 0x07e0f81f; \
*dst++ = (Uint16)(d | d >> 16); \
} \
} while (0)
#define ALPHA_BLIT16_555(to, from, length, bpp, alpha) \
do { \
int i; \
Uint16 *src = (Uint16 *)(from); \
Uint16 *dst = (Uint16 *)(to); \
Uint32 ALPHA = alpha >> 3; \
for (i = 0; i < (int)(length); i++) { \
Uint32 s = *src++; \
Uint32 d = *dst; \
s = (s | s << 16) & 0x03e07c1f; \
d = (d | d << 16) & 0x03e07c1f; \
d += (s - d) * ALPHA >> 5; \
d &= 0x03e07c1f; \
*dst++ = (Uint16)(d | d >> 16); \
} \
} while (0)
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
#define SET_RGB24(dst, d) \
dst[0] = (Uint8)(d >> 16); \
dst[1] = (Uint8)(d >> 8); \
dst[2] = (Uint8)(d);
#else
#define SET_RGB24(dst, d) \
dst[0] = (Uint8)(d); \
dst[1] = (Uint8)(d >> 8); \
dst[2] = (Uint8)(d >> 16);
#endif
#define ALPHA_BLIT_ANY(to, from, length, bpp, alpha) \
do { \
int i; \
Uint8 *src = from; \
Uint8 *dst = to; \
for (i = 0; i < (int)(length); i++) { \
Uint32 s = 0, d = 0; \
unsigned rs, gs, bs, rd, gd, bd; \
switch (bpp) { \
case 2: \
s = *(Uint16 *)src; \
d = *(Uint16 *)dst; \
break; \
case 3: \
s = GET_RGB24(src); \
d = GET_RGB24(dst); \
break; \
case 4: \
s = *(Uint32 *)src; \
d = *(Uint32 *)dst; \
break; \
} \
RGB_FROM_PIXEL(s, fmt, rs, gs, bs); \
RGB_FROM_PIXEL(d, fmt, rd, gd, bd); \
rd += (rs - rd) * alpha >> 8; \
gd += (gs - gd) * alpha >> 8; \
bd += (bs - bd) * alpha >> 8; \
PIXEL_FROM_RGB(d, fmt, rd, gd, bd); \
switch (bpp) { \
case 2: \
*(Uint16 *)dst = (Uint16)d; \
break; \
case 3: \
SET_RGB24(dst, d); \
break; \
case 4: \
*(Uint32 *)dst = d; \
break; \
} \
src += bpp; \
dst += bpp; \
} \
} while (0)
#define ALPHA_BLIT32_888_50(to, from, length, bpp, alpha) \
do { \
int i; \
Uint32 *src = (Uint32 *)(from); \
Uint32 *dst = (Uint32 *)(to); \
for (i = 0; i < (int)(length); i++) { \
Uint32 s = *src++; \
Uint32 d = *dst; \
*dst++ = (((s & 0x00fefefe) + (d & 0x00fefefe)) >> 1) + (s & d & 0x00010101); \
} \
} while (0)
#define BLEND16_50(dst, src, mask) \
do { \
Uint32 s = *src++; \
Uint32 d = *dst; \
*dst++ = (Uint16)((((s & mask) + (d & mask)) >> 1) + \
(s & d & (~mask & 0xffff))); \
} while (0)
#define ALPHA_BLIT16_50(to, from, length, bpp, alpha, mask) \
do { \
unsigned n = (length); \
Uint16 *src = (Uint16 *)(from); \
Uint16 *dst = (Uint16 *)(to); \
if (((uintptr_t)src ^ (uintptr_t)dst) & 3) { \
\
while (n--) \
BLEND16_50(dst, src, mask); \
} else { \
if ((uintptr_t)src & 3) { \
\
BLEND16_50(dst, src, mask); \
n--; \
} \
for (; n > 1; n -= 2) { \
Uint32 s = *(Uint32 *)src; \
Uint32 d = *(Uint32 *)dst; \
*(Uint32 *)dst = ((s & (mask | mask << 16)) >> 1) + ((d & (mask | mask << 16)) >> 1) + (s & d & (~(mask | mask << 16))); \
src += 2; \
dst += 2; \
} \
if (n) \
BLEND16_50(dst, src, mask); \
} \
} while (0)
#define ALPHA_BLIT16_565_50(to, from, length, bpp, alpha) \
ALPHA_BLIT16_50(to, from, length, bpp, alpha, 0xf7deU)
#define ALPHA_BLIT16_555_50(to, from, length, bpp, alpha) \
ALPHA_BLIT16_50(to, from, length, bpp, alpha, 0xfbdeU)
#define CHOOSE_BLIT(blitter, alpha, fmt) \
do { \
if (alpha == 255) { \
switch (fmt->bytes_per_pixel) { \
case 1: \
blitter(1, Uint8, OPAQUE_BLIT); \
break; \
case 2: \
blitter(2, Uint8, OPAQUE_BLIT); \
break; \
case 3: \
blitter(3, Uint8, OPAQUE_BLIT); \
break; \
case 4: \
blitter(4, Uint16, OPAQUE_BLIT); \
break; \
} \
} else { \
switch (fmt->bytes_per_pixel) { \
case 1: \
\
break; \
\
case 2: \
switch (fmt->Rmask | fmt->Gmask | fmt->Bmask) { \
case 0xffff: \
if (fmt->Gmask == 0x07e0 || fmt->Rmask == 0x07e0 || fmt->Bmask == 0x07e0) { \
if (alpha == 128) { \
blitter(2, Uint8, ALPHA_BLIT16_565_50); \
} else { \
blitter(2, Uint8, ALPHA_BLIT16_565); \
} \
} else { \
goto general16; \
} \
break; \
\
case 0x7fff: \
if (fmt->Gmask == 0x03e0 || fmt->Rmask == 0x03e0 || fmt->Bmask == 0x03e0) { \
if (alpha == 128) { \
blitter(2, Uint8, ALPHA_BLIT16_555_50); \
} else { \
blitter(2, Uint8, ALPHA_BLIT16_555); \
} \
break; \
} else { \
goto general16; \
} \
break; \
\
default: \
general16: \
blitter(2, Uint8, ALPHA_BLIT_ANY); \
} \
break; \
\
case 3: \
blitter(3, Uint8, ALPHA_BLIT_ANY); \
break; \
\
case 4: \
if ((fmt->Rmask | fmt->Gmask | fmt->Bmask) == 0x00ffffff && (fmt->Gmask == 0xff00 || fmt->Rmask == 0xff00 || fmt->Bmask == 0xff00)) { \
if (alpha == 128) { \
blitter(4, Uint16, ALPHA_BLIT32_888_50); \
} else { \
blitter(4, Uint16, ALPHA_BLIT32_888); \
} \
} else { \
blitter(4, Uint16, ALPHA_BLIT_ANY); \
} \
break; \
} \
} \
} while (0)
#define RLEPIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a) \
{ \
Pixel = ((r >> (8 - fmt->Rbits)) << fmt->Rshift) | \
((g >> (8 - fmt->Gbits)) << fmt->Gshift) | \
((b >> (8 - fmt->Bbits)) << fmt->Bshift) | \
(a << 24); \
}
#define RLECLIPBLIT(bpp, Type, do_blit) \
do { \
int linecount = srcrect->h; \
int ofs = 0; \
int left = srcrect->x; \
int right = left + srcrect->w; \
dstbuf -= left * bpp; \
for (;;) { \
int run; \
ofs += *(Type *)srcbuf; \
run = ((Type *)srcbuf)[1]; \
srcbuf += 2 * sizeof(Type); \
if (run) { \
\
if (ofs < right) { \
int start = 0; \
int len = run; \
int startcol; \
if (left - ofs > 0) { \
start = left - ofs; \
len -= start; \
if (len <= 0) \
goto nocopy##bpp##do_blit; \
} \
startcol = ofs + start; \
if (len > right - startcol) \
len = right - startcol; \
do_blit(dstbuf + startcol * bpp, srcbuf + start * bpp, \
len, bpp, alpha); \
} \
nocopy##bpp##do_blit : srcbuf += run * bpp; \
ofs += run; \
} else if (!ofs) { \
break; \
} \
\
if (ofs == w) { \
ofs = 0; \
dstbuf += surf_dst->pitch; \
if (!--linecount) { \
break; \
} \
} \
} \
} while (0)
static void RLEClipBlit(int w, Uint8 *srcbuf, SDL_Surface *surf_dst,
Uint8 *dstbuf, const SDL_Rect *srcrect, unsigned alpha)
{
const SDL_PixelFormatDetails *fmt = surf_dst->fmt;
CHOOSE_BLIT(RLECLIPBLIT, alpha, fmt);
}
#undef RLECLIPBLIT
static bool SDLCALL SDL_RLEBlit(SDL_Surface *surf_src, const SDL_Rect *srcrect,
SDL_Surface *surf_dst, const SDL_Rect *dstrect)
{
Uint8 *dstbuf;
Uint8 *srcbuf;
int x, y;
int w = surf_src->w;
unsigned alpha;
if (SDL_MUSTLOCK(surf_dst)) {
if (!SDL_LockSurface(surf_dst)) {
return false;
}
}
x = dstrect->x;
y = dstrect->y;
dstbuf = (Uint8 *)surf_dst->pixels + y * surf_dst->pitch + x * surf_src->fmt->bytes_per_pixel;
srcbuf = (Uint8 *)surf_src->map.data + sizeof(SDL_PixelFormat);
{
int vskip = srcrect->y;
int ofs = 0;
if (vskip) {
#define RLESKIP(bpp, Type) \
for (;;) { \
int run; \
ofs += *(Type *)srcbuf; \
run = ((Type *)srcbuf)[1]; \
srcbuf += sizeof(Type) * 2; \
if (run) { \
srcbuf += run * bpp; \
ofs += run; \
} else if (!ofs) \
goto done; \
if (ofs == w) { \
ofs = 0; \
if (!--vskip) \
break; \
} \
}
switch (surf_src->fmt->bytes_per_pixel) {
case 1:
RLESKIP(1, Uint8);
break;
case 2:
RLESKIP(2, Uint8);
break;
case 3:
RLESKIP(3, Uint8);
break;
case 4:
RLESKIP(4, Uint16);
break;
}
#undef RLESKIP
}
}
alpha = surf_src->map.info.a;
if (srcrect->x || srcrect->w != surf_src->w) {
RLEClipBlit(w, srcbuf, surf_dst, dstbuf, srcrect, alpha);
} else {
const SDL_PixelFormatDetails *fmt = surf_src->fmt;
#define RLEBLIT(bpp, Type, do_blit) \
do { \
int linecount = srcrect->h; \
int ofs = 0; \
for (;;) { \
unsigned run; \
ofs += *(Type *)srcbuf; \
run = ((Type *)srcbuf)[1]; \
srcbuf += 2 * sizeof(Type); \
if (run) { \
do_blit(dstbuf + ofs * bpp, srcbuf, run, bpp, alpha); \
srcbuf += run * bpp; \
ofs += run; \
} else if (!ofs) \
break; \
if (ofs == w) { \
ofs = 0; \
dstbuf += surf_dst->pitch; \
if (!--linecount) \
break; \
} \
} \
} while (0)
CHOOSE_BLIT(RLEBLIT, alpha, fmt);
#undef RLEBLIT
}
done:
if (SDL_MUSTLOCK(surf_dst)) {
SDL_UnlockSurface(surf_dst);
}
return true;
}
#undef OPAQUE_BLIT
#define BLIT_TRANSL_888(src, dst) \
do { \
Uint32 s = src; \
Uint32 d = dst; \
unsigned alpha = s >> 24; \
Uint32 s1 = s & 0xff00ff; \
Uint32 d1 = d & 0xff00ff; \
d1 = (d1 + ((s1 - d1) * alpha >> 8)) & 0xff00ff; \
s &= 0xff00; \
d &= 0xff00; \
d = (d + ((s - d) * alpha >> 8)) & 0xff00; \
dst = d1 | d | 0xff000000; \
} while (0)
#define BLIT_TRANSL_565(src, dst) \
do { \
Uint32 s = src; \
Uint32 d = dst; \
unsigned alpha = (s & 0x3e0) >> 5; \
s &= 0x07e0f81f; \
d = (d | d << 16) & 0x07e0f81f; \
d += (s - d) * alpha >> 5; \
d &= 0x07e0f81f; \
dst = (Uint16)(d | d >> 16); \
} while (0)
#define BLIT_TRANSL_555(src, dst) \
do { \
Uint32 s = src; \
Uint32 d = dst; \
unsigned alpha = (s & 0x3e0) >> 5; \
s &= 0x03e07c1f; \
d = (d | d << 16) & 0x03e07c1f; \
d += (s - d) * alpha >> 5; \
d &= 0x03e07c1f; \
dst = (Uint16)(d | d >> 16); \
} while (0)
static void RLEAlphaClipBlit(int w, Uint8 *srcbuf, SDL_Surface *surf_dst,
Uint8 *dstbuf, const SDL_Rect *srcrect)
{
const SDL_PixelFormatDetails *df = surf_dst->fmt;
#define RLEALPHACLIPBLIT(Ptype, Ctype, do_blend) \
do { \
int linecount = srcrect->h; \
int left = srcrect->x; \
int right = left + srcrect->w; \
dstbuf -= left * sizeof(Ptype); \
do { \
int ofs = 0; \
\
do { \
unsigned run; \
ofs += ((Ctype *)srcbuf)[0]; \
run = ((Ctype *)srcbuf)[1]; \
srcbuf += 2 * sizeof(Ctype); \
if (run) { \
\
int cofs = ofs; \
int crun = run; \
if (left - cofs > 0) { \
crun -= left - cofs; \
cofs = left; \
} \
if (crun > right - cofs) \
crun = right - cofs; \
if (crun > 0) \
PIXEL_COPY(dstbuf + cofs * sizeof(Ptype), \
srcbuf + (cofs - ofs) * sizeof(Ptype), \
(unsigned)crun, sizeof(Ptype)); \
srcbuf += run * sizeof(Ptype); \
ofs += run; \
} else if (!ofs) \
return; \
} while (ofs < w); \
\
const size_t psize = sizeof(Ptype); \
if (psize == 2) \
srcbuf += (uintptr_t)srcbuf & 2; \
\
ofs = 0; \
do { \
unsigned run; \
ofs += ((Uint16 *)srcbuf)[0]; \
run = ((Uint16 *)srcbuf)[1]; \
srcbuf += 4; \
if (run) { \
\
int cofs = ofs; \
int crun = run; \
if (left - cofs > 0) { \
crun -= left - cofs; \
cofs = left; \
} \
if (crun > right - cofs) \
crun = right - cofs; \
if (crun > 0) { \
Ptype *dst = (Ptype *)dstbuf + cofs; \
Uint32 *src = (Uint32 *)srcbuf + (cofs - ofs); \
int i; \
for (i = 0; i < crun; i++) \
do_blend(src[i], dst[i]); \
} \
srcbuf += run * 4; \
ofs += run; \
} \
} while (ofs < w); \
dstbuf += surf_dst->pitch; \
} while (--linecount); \
} while (0)
switch (df->bytes_per_pixel) {
case 2:
if (df->Gmask == 0x07e0 || df->Rmask == 0x07e0 || df->Bmask == 0x07e0) {
RLEALPHACLIPBLIT(Uint16, Uint8, BLIT_TRANSL_565);
} else {
RLEALPHACLIPBLIT(Uint16, Uint8, BLIT_TRANSL_555);
}
break;
case 4:
RLEALPHACLIPBLIT(Uint32, Uint16, BLIT_TRANSL_888);
break;
}
}
static bool SDLCALL SDL_RLEAlphaBlit(SDL_Surface *surf_src, const SDL_Rect *srcrect,
SDL_Surface *surf_dst, const SDL_Rect *dstrect)
{
int x, y;
int w = surf_src->w;
Uint8 *srcbuf, *dstbuf;
const SDL_PixelFormatDetails *df = surf_dst->fmt;
if (SDL_MUSTLOCK(surf_dst)) {
if (!SDL_LockSurface(surf_dst)) {
return false;
}
}
x = dstrect->x;
y = dstrect->y;
dstbuf = (Uint8 *)surf_dst->pixels + y * surf_dst->pitch + x * df->bytes_per_pixel;
srcbuf = (Uint8 *)surf_src->map.data + sizeof(SDL_PixelFormat);
{
int vskip = srcrect->y;
if (vskip) {
int ofs;
if (df->bytes_per_pixel == 2) {
do {
ofs = 0;
do {
int run;
ofs += srcbuf[0];
run = srcbuf[1];
srcbuf += 2;
if (run) {
srcbuf += 2 * run;
ofs += run;
} else if (ofs == 0) {
goto done;
}
} while (ofs < w);
srcbuf += (uintptr_t)srcbuf & 2;
ofs = 0;
do {
int run;
ofs += ((Uint16 *)srcbuf)[0];
run = ((Uint16 *)srcbuf)[1];
srcbuf += 4 * (run + 1);
ofs += run;
} while (ofs < w);
} while (--vskip);
} else {
vskip <<= 1; do {
ofs = 0;
do {
int run;
ofs += ((Uint16 *)srcbuf)[0];
run = ((Uint16 *)srcbuf)[1];
srcbuf += 4;
if (run) {
srcbuf += 4 * run;
ofs += run;
} else if (ofs == 0) {
goto done;
}
} while (ofs < w);
} while (--vskip);
}
}
}
if (srcrect->x || srcrect->w != surf_src->w) {
RLEAlphaClipBlit(w, srcbuf, surf_dst, dstbuf, srcrect);
} else {
#define RLEALPHABLIT(Ptype, Ctype, do_blend) \
do { \
int linecount = srcrect->h; \
do { \
int ofs = 0; \
\
do { \
unsigned run; \
ofs += ((Ctype *)srcbuf)[0]; \
run = ((Ctype *)srcbuf)[1]; \
srcbuf += 2 * sizeof(Ctype); \
if (run) { \
PIXEL_COPY(dstbuf + ofs * sizeof(Ptype), srcbuf, \
run, sizeof(Ptype)); \
srcbuf += run * sizeof(Ptype); \
ofs += run; \
} else if (!ofs) \
goto done; \
} while (ofs < w); \
\
const size_t psize = sizeof(Ptype); \
if (psize == 2) \
srcbuf += (uintptr_t)srcbuf & 2; \
\
ofs = 0; \
do { \
unsigned run; \
ofs += ((Uint16 *)srcbuf)[0]; \
run = ((Uint16 *)srcbuf)[1]; \
srcbuf += 4; \
if (run) { \
Ptype *dst = (Ptype *)dstbuf + ofs; \
unsigned i; \
for (i = 0; i < run; i++) { \
Uint32 src = *(Uint32 *)srcbuf; \
do_blend(src, *dst); \
srcbuf += 4; \
dst++; \
} \
ofs += run; \
} \
} while (ofs < w); \
dstbuf += surf_dst->pitch; \
} while (--linecount); \
} while (0)
switch (df->bytes_per_pixel) {
case 2:
if (df->Gmask == 0x07e0 || df->Rmask == 0x07e0 || df->Bmask == 0x07e0) {
RLEALPHABLIT(Uint16, Uint8, BLIT_TRANSL_565);
} else {
RLEALPHABLIT(Uint16, Uint8, BLIT_TRANSL_555);
}
break;
case 4:
RLEALPHABLIT(Uint32, Uint16, BLIT_TRANSL_888);
break;
}
}
done:
if (SDL_MUSTLOCK(surf_dst)) {
SDL_UnlockSurface(surf_dst);
}
return true;
}
static int copy_opaque_16(void *dst, const Uint32 *src, int n,
const SDL_PixelFormatDetails *sfmt, const SDL_PixelFormatDetails *dfmt)
{
int i;
Uint16 *d = (Uint16 *)dst;
for (i = 0; i < n; i++) {
unsigned r, g, b;
RGB_FROM_PIXEL(*src, sfmt, r, g, b);
PIXEL_FROM_RGB(*d, dfmt, r, g, b);
src++;
d++;
}
return n * 2;
}
static int copy_transl_565(void *dst, const Uint32 *src, int n,
const SDL_PixelFormatDetails *sfmt, const SDL_PixelFormatDetails *dfmt)
{
int i;
Uint32 *d = (Uint32 *)dst;
for (i = 0; i < n; i++) {
unsigned r, g, b, a;
Uint16 pix;
RGBA_FROM_8888(*src, sfmt, r, g, b, a);
PIXEL_FROM_RGB(pix, dfmt, r, g, b);
*d = ((pix & 0x7e0) << 16) | (pix & 0xf81f) | ((a << 2) & 0x7e0);
src++;
d++;
}
return n * 4;
}
static int copy_transl_555(void *dst, const Uint32 *src, int n,
const SDL_PixelFormatDetails *sfmt, const SDL_PixelFormatDetails *dfmt)
{
int i;
Uint32 *d = (Uint32 *)dst;
for (i = 0; i < n; i++) {
unsigned r, g, b, a;
Uint16 pix;
RGBA_FROM_8888(*src, sfmt, r, g, b, a);
PIXEL_FROM_RGB(pix, dfmt, r, g, b);
*d = ((pix & 0x3e0) << 16) | (pix & 0xfc1f) | ((a << 2) & 0x3e0);
src++;
d++;
}
return n * 4;
}
static int copy_32(void *dst, const Uint32 *src, int n,
const SDL_PixelFormatDetails *sfmt, const SDL_PixelFormatDetails *dfmt)
{
int i;
Uint32 *d = (Uint32 *)dst;
for (i = 0; i < n; i++) {
unsigned r, g, b, a;
RGBA_FROM_8888(*src, sfmt, r, g, b, a);
RLEPIXEL_FROM_RGBA(*d, dfmt, r, g, b, a);
d++;
src++;
}
return n * 4;
}
#define ISOPAQUE(pixel, fmt) ((((pixel)&fmt->Amask) >> fmt->Ashift) == 255)
#define ISTRANSL(pixel, fmt) \
((unsigned)((((pixel)&fmt->Amask) >> fmt->Ashift) - 1U) < 254U)
static bool RLEAlphaSurface(SDL_Surface *surface)
{
SDL_Surface *dest;
const SDL_PixelFormatDetails *df;
int maxsize = 0;
int max_opaque_run;
int max_transl_run = 65535;
unsigned masksum;
Uint8 *rlebuf, *dst;
int (*copy_opaque)(void *, const Uint32 *, int,
const SDL_PixelFormatDetails *, const SDL_PixelFormatDetails *);
int (*copy_transl)(void *, const Uint32 *, int,
const SDL_PixelFormatDetails *, const SDL_PixelFormatDetails *);
dest = surface->map.info.dst_surface;
if (!dest) {
return false;
}
df = dest->fmt;
if (surface->fmt->bits_per_pixel != 32) {
return false; }
masksum = df->Rmask | df->Gmask | df->Bmask;
switch (df->bytes_per_pixel) {
case 2:
switch (masksum) {
case 0xffff:
if (df->Gmask == 0x07e0 || df->Rmask == 0x07e0 || df->Bmask == 0x07e0) {
copy_opaque = copy_opaque_16;
copy_transl = copy_transl_565;
} else {
return false;
}
break;
case 0x7fff:
if (df->Gmask == 0x03e0 || df->Rmask == 0x03e0 || df->Bmask == 0x03e0) {
copy_opaque = copy_opaque_16;
copy_transl = copy_transl_555;
} else {
return false;
}
break;
default:
return false;
}
max_opaque_run = 255;
maxsize = surface->h * (2 + (4 + 2) * (surface->w + 1)) + 2;
break;
case 4:
if (masksum != 0x00ffffff) {
return false; }
copy_opaque = copy_32;
copy_transl = copy_32;
max_opaque_run = 255;
maxsize = surface->h * 2 * 4 * (surface->w + 1) + 4;
break;
default:
return false; }
maxsize += sizeof(SDL_PixelFormat);
rlebuf = (Uint8 *)SDL_malloc(maxsize);
if (!rlebuf) {
return false;
}
*(SDL_PixelFormat *)rlebuf = dest->format;
dst = rlebuf + sizeof(SDL_PixelFormat);
{
int x, y;
int h = surface->h, w = surface->w;
const SDL_PixelFormatDetails *sf = surface->fmt;
Uint32 *src = (Uint32 *)surface->pixels;
Uint8 *lastline = dst;
#define ADD_OPAQUE_COUNTS(n, m) \
if (df->bytes_per_pixel == 4) { \
((Uint16 *)dst)[0] = (Uint16)n; \
((Uint16 *)dst)[1] = (Uint16)m; \
dst += 4; \
} else { \
dst[0] = (Uint8)n; \
dst[1] = (Uint8)m; \
dst += 2; \
}
#define ADD_TRANSL_COUNTS(n, m) \
(((Uint16 *)dst)[0] = (Uint16)n, ((Uint16 *)dst)[1] = (Uint16)m, dst += 4)
for (y = 0; y < h; y++) {
int runstart, skipstart;
int blankline = 0;
x = 0;
do {
int run, skip, len;
skipstart = x;
while (x < w && !ISOPAQUE(src[x], sf)) {
x++;
}
runstart = x;
while (x < w && ISOPAQUE(src[x], sf)) {
x++;
}
skip = runstart - skipstart;
if (skip == w) {
blankline = 1;
}
run = x - runstart;
while (skip > max_opaque_run) {
ADD_OPAQUE_COUNTS(max_opaque_run, 0);
skip -= max_opaque_run;
}
len = SDL_min(run, max_opaque_run);
ADD_OPAQUE_COUNTS(skip, len);
dst += copy_opaque(dst, src + runstart, len, sf, df);
runstart += len;
run -= len;
while (run) {
len = SDL_min(run, max_opaque_run);
ADD_OPAQUE_COUNTS(0, len);
dst += copy_opaque(dst, src + runstart, len, sf, df);
runstart += len;
run -= len;
}
} while (x < w);
dst += (uintptr_t)dst & 2;
x = 0;
do {
int run, skip, len;
skipstart = x;
while (x < w && !ISTRANSL(src[x], sf)) {
x++;
}
runstart = x;
while (x < w && ISTRANSL(src[x], sf)) {
x++;
}
skip = runstart - skipstart;
blankline &= (skip == w);
run = x - runstart;
while (skip > max_transl_run) {
ADD_TRANSL_COUNTS(max_transl_run, 0);
skip -= max_transl_run;
}
len = SDL_min(run, max_transl_run);
ADD_TRANSL_COUNTS(skip, len);
dst += copy_transl(dst, src + runstart, len, sf, df);
runstart += len;
run -= len;
while (run) {
len = SDL_min(run, max_transl_run);
ADD_TRANSL_COUNTS(0, len);
dst += copy_transl(dst, src + runstart, len, sf, df);
runstart += len;
run -= len;
}
if (!blankline) {
lastline = dst;
}
} while (x < w);
src += surface->pitch >> 2;
}
dst = lastline; ADD_OPAQUE_COUNTS(0, 0);
}
#undef ADD_OPAQUE_COUNTS
#undef ADD_TRANSL_COUNTS
{
Uint8 *p = (Uint8 *)SDL_realloc(rlebuf, dst - rlebuf);
if (!p) {
p = rlebuf;
}
surface->map.data = p;
}
return true;
}
static Uint32 getpix_8(const Uint8 *srcbuf)
{
return *srcbuf;
}
static Uint32 getpix_16(const Uint8 *srcbuf)
{
return *(const Uint16 *)srcbuf;
}
static Uint32 getpix_24(const Uint8 *srcbuf)
{
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
return srcbuf[0] + (srcbuf[1] << 8) + (srcbuf[2] << 16);
#else
return (srcbuf[0] << 16) + (srcbuf[1] << 8) + srcbuf[2];
#endif
}
static Uint32 getpix_32(const Uint8 *srcbuf)
{
return *(const Uint32 *)srcbuf;
}
typedef Uint32 (*getpix_func)(const Uint8 *);
static const getpix_func getpixes[4] = {
getpix_8, getpix_16, getpix_24, getpix_32
};
static bool RLEColorkeySurface(SDL_Surface *surface)
{
SDL_Surface *dest;
Uint8 *rlebuf, *dst;
int maxn;
int y;
Uint8 *srcbuf, *lastline;
int maxsize = 0;
const int bpp = surface->fmt->bytes_per_pixel;
getpix_func getpix;
Uint32 ckey, rgbmask;
int w, h;
dest = surface->map.info.dst_surface;
if (!dest) {
return false;
}
switch (bpp) {
case 1:
maxsize = surface->h * 3 * (surface->w / 2 + 1) + 2;
break;
case 2:
case 3:
maxsize = surface->h * (2 * (surface->w / 255 + 1) + surface->w * bpp) + 2;
break;
case 4:
maxsize = surface->h * (4 * (surface->w / 65535 + 1) + surface->w * 4) + 4;
break;
default:
return false;
}
maxsize += sizeof(SDL_PixelFormat);
rlebuf = (Uint8 *)SDL_malloc(maxsize);
if (!rlebuf) {
return false;
}
*(SDL_PixelFormat *)rlebuf = dest->format;
srcbuf = (Uint8 *)surface->pixels;
maxn = bpp == 4 ? 65535 : 255;
dst = rlebuf + sizeof(SDL_PixelFormat);
rgbmask = ~surface->fmt->Amask;
ckey = surface->map.info.colorkey & rgbmask;
lastline = dst;
getpix = getpixes[bpp - 1];
w = surface->w;
h = surface->h;
#define ADD_COUNTS(n, m) \
if (bpp == 4) { \
((Uint16 *)dst)[0] = (Uint16)n; \
((Uint16 *)dst)[1] = (Uint16)m; \
dst += 4; \
} else { \
dst[0] = (Uint8)n; \
dst[1] = (Uint8)m; \
dst += 2; \
}
for (y = 0; y < h; y++) {
int x = 0;
int blankline = 0;
do {
int run, skip;
int len;
int runstart;
int skipstart = x;
while (x < w && (getpix(srcbuf + x * bpp) & rgbmask) == ckey) {
x++;
}
runstart = x;
while (x < w && (getpix(srcbuf + x * bpp) & rgbmask) != ckey) {
x++;
}
skip = runstart - skipstart;
if (skip == w) {
blankline = 1;
}
run = x - runstart;
while (skip > maxn) {
ADD_COUNTS(maxn, 0);
skip -= maxn;
}
len = SDL_min(run, maxn);
ADD_COUNTS(skip, len);
SDL_memcpy(dst, srcbuf + runstart * bpp, (size_t)len * bpp);
dst += len * bpp;
run -= len;
runstart += len;
while (run) {
len = SDL_min(run, maxn);
ADD_COUNTS(0, len);
SDL_memcpy(dst, srcbuf + runstart * bpp, (size_t)len * bpp);
dst += len * bpp;
runstart += len;
run -= len;
}
if (!blankline) {
lastline = dst;
}
} while (x < w);
srcbuf += surface->pitch;
}
dst = lastline; ADD_COUNTS(0, 0);
#undef ADD_COUNTS
{
Uint8 *p = (Uint8 *)SDL_realloc(rlebuf, dst - rlebuf);
if (!p) {
p = rlebuf;
}
surface->map.data = p;
}
return true;
}
bool SDL_RLESurface(SDL_Surface *surface)
{
int flags;
if (surface->internal_flags & SDL_INTERNAL_SURFACE_RLEACCEL) {
SDL_UnRLESurface(surface);
}
if (SDL_BITSPERPIXEL(surface->format) < 8) {
return false;
}
if (!surface->pixels) {
return false;
}
flags = surface->map.info.flags;
if (flags & SDL_COPY_COLORKEY) {
} else if ((flags & SDL_COPY_BLEND) && SDL_ISPIXELFORMAT_ALPHA(surface->format)) {
} else {
return false;
}
if ((flags & SDL_COPY_MODULATE_COLOR) ||
((flags & SDL_COPY_MODULATE_ALPHA) && SDL_ISPIXELFORMAT_ALPHA(surface->format)) ||
(flags & (SDL_COPY_BLEND_PREMULTIPLIED | SDL_COPY_ADD | SDL_COPY_ADD_PREMULTIPLIED | SDL_COPY_MOD | SDL_COPY_MUL)) ||
(flags & SDL_COPY_NEAREST)) {
return false;
}
if (!SDL_ISPIXELFORMAT_ALPHA(surface->format) || !(flags & SDL_COPY_BLEND)) {
if (!surface->map.identity) {
return false;
}
if (!RLEColorkeySurface(surface)) {
return false;
}
surface->map.blit = SDL_RLEBlit;
surface->map.info.flags |= SDL_COPY_RLE_COLORKEY;
} else {
if (!RLEAlphaSurface(surface)) {
return false;
}
surface->map.blit = SDL_RLEAlphaBlit;
surface->map.info.flags |= SDL_COPY_RLE_ALPHAKEY;
}
if (!(surface->flags & SDL_SURFACE_PREALLOCATED)) {
surface->saved_pixels = surface->pixels;
surface->pixels = NULL;
}
surface->internal_flags |= SDL_INTERNAL_SURFACE_RLEACCEL;
SDL_UpdateSurfaceLockFlag(surface);
return true;
}
void SDL_UnRLESurface(SDL_Surface *surface)
{
if (surface->internal_flags & SDL_INTERNAL_SURFACE_RLEACCEL) {
surface->internal_flags &= ~SDL_INTERNAL_SURFACE_RLEACCEL;
surface->map.info.flags &= ~(SDL_COPY_RLE_COLORKEY | SDL_COPY_RLE_ALPHAKEY);
if (!(surface->flags & SDL_SURFACE_PREALLOCATED)) {
surface->pixels = surface->saved_pixels;
surface->saved_pixels = NULL;
}
SDL_free(surface->map.data);
surface->map.data = NULL;
SDL_InvalidateMap(&surface->map);
SDL_UpdateSurfaceLockFlag(surface);
}
}
#endif