diff --git a/src/video/SDL_pixels.c b/src/video/SDL_pixels.c index ccb024dcb..c7b4d3291 100644 --- a/src/video/SDL_pixels.c +++ b/src/video/SDL_pixels.c @@ -1227,4 +1227,34 @@ SDL_CalculateGammaRamp(float gamma, Uint16 * ramp) } } +/* Creates a copy of an ARGB8888-format surface's pixels with premultiplied alpha */ +void +SDL_PremultiplySurfaceAlphaToARGB8888(SDL_Surface *src, Uint32 *dst) +{ + Uint8 A, R, G, B; + int x, y; + + if (SDL_MUSTLOCK(src)) + SDL_LockSurface(src); + + for (y = 0; y < src->h; ++y) { + Uint8 *src_px = (Uint8*)(src->pixels) + (y * src->pitch); + for (x = 0; x < src->w; ++x) { + /* Component bytes extraction. */ + SDL_GetRGBA(*(Uint32*)src_px, src->format, &R, &G, &B, &A); + src_px += src->format->BytesPerPixel; + + /* Alpha pre-multiplication of each component. */ + R = (float)A * ((float)R /255); + G = (float)A * ((float)G /255); + B = (float)A * ((float)B /255); + + /* ARGB8888 pixel recomposition. */ + (*dst++) = (((Uint32)A << 24) | ((Uint32)R << 16) | ((Uint32)G << 8)) | ((Uint32)B << 0); + } + } + if (SDL_MUSTLOCK(src)) + SDL_UnlockSurface(src); +} + /* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/video/SDL_pixels_c.h b/src/video/SDL_pixels_c.h index 1974797fc..91db5b446 100644 --- a/src/video/SDL_pixels_c.h +++ b/src/video/SDL_pixels_c.h @@ -43,6 +43,7 @@ extern void SDL_InvalidateAllBlitMap(SDL_Surface *surface); extern void SDL_DitherColors(SDL_Color * colors, int bpp); extern Uint8 SDL_FindColor(SDL_Palette * pal, Uint8 r, Uint8 g, Uint8 b, Uint8 a); extern void SDL_DetectPalette(SDL_Palette *pal, SDL_bool *is_opaque, SDL_bool *has_alpha_channel); +extern void SDL_PremultiplySurfaceAlphaToARGB8888(SDL_Surface *src, Uint32 *dst); #endif /* SDL_pixels_c_h_ */