mirror of
https://github.com/encounter/SDL.git
synced 2025-12-17 00:47:15 +00:00
Added SDL_PremultiplyAlpha() to premultiply alpha on a block of SDL_PIXELFORMAT_ARGB8888 pixels
This commit is contained in:
@@ -1227,33 +1227,4 @@ 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) {
|
||||
Uint32 *src_px = (Uint32*)((Uint8 *)src->pixels + (y * src->pitch));
|
||||
for (x = 0; x < src->w; ++x) {
|
||||
/* Component bytes extraction. */
|
||||
SDL_GetRGBA(*src_px++, src->format, &R, &G, &B, &A);
|
||||
|
||||
/* Alpha pre-multiplication of each component. */
|
||||
R = ((Uint32)A * R) / 255;
|
||||
G = ((Uint32)A * G) / 255;
|
||||
B = ((Uint32)A * B) / 255;
|
||||
|
||||
/* ARGB8888 pixel recomposition. */
|
||||
*dst++ = (((Uint32)A << 24) | ((Uint32)R << 16) | ((Uint32)G << 8) | (B << 0));
|
||||
}
|
||||
}
|
||||
if (SDL_MUSTLOCK(src))
|
||||
SDL_UnlockSurface(src);
|
||||
}
|
||||
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
||||
|
||||
@@ -43,7 +43,6 @@ 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_ */
|
||||
|
||||
|
||||
@@ -1381,7 +1381,12 @@ int SDL_ConvertPixels(int width, int height,
|
||||
void *nonconst_src = (void *) src;
|
||||
int ret;
|
||||
|
||||
/* Check to make sure we are blitting somewhere, so we don't crash */
|
||||
if (!src) {
|
||||
return SDL_InvalidParamError("src");
|
||||
}
|
||||
if (!src_pitch) {
|
||||
return SDL_InvalidParamError("src_pitch");
|
||||
}
|
||||
if (!dst) {
|
||||
return SDL_InvalidParamError("dst");
|
||||
}
|
||||
@@ -1440,6 +1445,68 @@ int SDL_ConvertPixels(int width, int height,
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Premultiply the alpha on a block of pixels
|
||||
*
|
||||
* This is currently only implemented for SDL_PIXELFORMAT_ARGB8888
|
||||
*
|
||||
* Here are some ideas for optimization:
|
||||
* https://github.com/Wizermil/premultiply_alpha/tree/master/premultiply_alpha
|
||||
* https://developer.arm.com/documentation/101964/0201/Pre-multiplied-alpha-channel-data
|
||||
*/
|
||||
int SDL_PremultiplyAlpha(int width, int height,
|
||||
Uint32 src_format, const void * src, int src_pitch,
|
||||
Uint32 dst_format, void * dst, int dst_pitch)
|
||||
{
|
||||
int c;
|
||||
Uint32 srcpixel;
|
||||
Uint32 srcR, srcG, srcB, srcA;
|
||||
Uint32 dstpixel;
|
||||
Uint32 dstR, dstG, dstB, dstA;
|
||||
|
||||
if (!src) {
|
||||
return SDL_InvalidParamError("src");
|
||||
}
|
||||
if (!src_pitch) {
|
||||
return SDL_InvalidParamError("src_pitch");
|
||||
}
|
||||
if (!dst) {
|
||||
return SDL_InvalidParamError("dst");
|
||||
}
|
||||
if (!dst_pitch) {
|
||||
return SDL_InvalidParamError("dst_pitch");
|
||||
}
|
||||
if (src_format != SDL_PIXELFORMAT_ARGB8888) {
|
||||
return SDL_InvalidParamError("src_format");
|
||||
}
|
||||
if (dst_format != SDL_PIXELFORMAT_ARGB8888) {
|
||||
return SDL_InvalidParamError("dst_format");
|
||||
}
|
||||
|
||||
while (height--) {
|
||||
const Uint32 *src_px = (const Uint32 *)src;
|
||||
Uint32 *dst_px = (Uint32 *)dst;
|
||||
for (c = width; c; --c) {
|
||||
/* Component bytes extraction. */
|
||||
srcpixel = *src_px++;
|
||||
RGBA_FROM_ARGB8888(srcpixel, srcR, srcG, srcB, srcA);
|
||||
|
||||
/* Alpha pre-multiplication of each component. */
|
||||
dstA = srcA;
|
||||
dstR = (srcA * srcR) / 255;
|
||||
dstG = (srcA * srcG) / 255;
|
||||
dstB = (srcA * srcB) / 255;
|
||||
|
||||
/* ARGB8888 pixel recomposition. */
|
||||
ARGB8888_FROM_RGBA(dstpixel, dstR, dstG, dstB, dstA);
|
||||
*dst_px++ = dstpixel;
|
||||
}
|
||||
src = (const Uint8 *)src + src_pitch;
|
||||
dst = (Uint8 *)dst + dst_pitch;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Free a surface created by the above function.
|
||||
*/
|
||||
|
||||
@@ -278,7 +278,9 @@ KMSDRM_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
|
||||
like other backends do. Also, the GBM BO pixels have to be
|
||||
alpha-premultiplied, but the SDL surface we receive has
|
||||
straight-alpha pixels, so we always have to convert. */
|
||||
SDL_PremultiplySurfaceAlphaToARGB8888(surface, curdata->buffer);
|
||||
SDL_PremultiplyAlpha(surface->w, surface->h,
|
||||
surface->format->format, surface->pixels, surface->pitch,
|
||||
SDL_PIXELFORMAT_ARGB8888, curdata->buffer, surface->w * 4);
|
||||
|
||||
cursor->driverdata = curdata;
|
||||
|
||||
|
||||
@@ -280,7 +280,9 @@ Wayland_CreateCursor(SDL_Surface *surface, int hot_x, int hot_y)
|
||||
}
|
||||
|
||||
/* Wayland requires premultiplied alpha for its surfaces. */
|
||||
SDL_PremultiplySurfaceAlphaToARGB8888(surface, data->shm_data);
|
||||
SDL_PremultiplyAlpha(surface->w, surface->h,
|
||||
surface->format->format, surface->pixels, surface->pitch,
|
||||
SDL_PIXELFORMAT_ARGB8888, data->shm_data, surface->w * 4);
|
||||
|
||||
data->surface = wl_compositor_create_surface(wd->compositor);
|
||||
wl_surface_set_user_data(data->surface, NULL);
|
||||
@@ -447,4 +449,7 @@ Wayland_FiniMouse(SDL_VideoData *data)
|
||||
}
|
||||
SDL_free(data->cursor_themes);
|
||||
}
|
||||
|
||||
#endif /* SDL_VIDEO_DRIVER_WAYLAND */
|
||||
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
||||
|
||||
Reference in New Issue
Block a user