New to SDL and have an alpha blitting question

Hello All,
I have recently started working with SDL and have had little to no
problems until recently. I am trying to create a transparent text
overlay. So, what I want to do is blit per-pixel alpha blended text
onto a transparent surface, so that I can overlay this surface on
different backgrounds. My problem is that I cannot figure out how to do
this without the entire surface either becoming fully opaque or fully
transparent. My biggest problem is I need to find out how to just copy
the per-pixel alpha information from a character font onto a transparent
surface, and then blit that surface. I have tried creating a surface,
then filling it with the color 0xff000000 where FF is the alpha value.
Then setting the alpha with SDL_SetAlpha to no SDL_SRCALPHA and no per
surface alpha. Then performing the blitsurface of the character onto
that surface. Then blitting that surface onto the background. This
didn’t work. So, I tried just creating the surface in the GIMP, as just
a .png with a fully transparent background, and using this surface in
the same way, only to discover the same result. I know I am just doing
something stupid, can someone please help me out?
Robert

Robert,

To copy every pixels with color different than FF you need to
set the colorkey, setting the alpha value won’t make a color
disappear when blitting. So all you need is to set ff as the
COLORKEY and an alpha value. I have done similar things and
had no problem, if you can’t solve it, send the code :).

Also it’s good that the background color on
your font, if a bitmap, is the same color as the intermediate
surface colorkey, or you will have to set a colorkey on it also.

Paulo>

From: Robert Diel
Date: 2002/10/31 Thu AM 10:30:43 EST
To: sdl at libsdl.org
Subject: [SDL] New to SDL and have an alpha blitting question

Hello All,
I have recently started working with SDL and have had little to
no
problems until recently. I am trying to create a transparent text
overlay. So, what I want to do is blit per-pixel alpha blended
text
onto a transparent surface, so that I can overlay this surface
on
different backgrounds. My problem is that I cannot figure out
how to do
this without the entire surface either becoming fully opaque or
fully
transparent. My biggest problem is I need to find out how to
just copy
the per-pixel alpha information from a character font onto a
transparent
surface, and then blit that surface. I have tried creating a
surface,
then filling it with the color 0xff000000 where FF is the alpha
value.
Then setting the alpha with SDL_SetAlpha to no
SDL_SRCALPHA and no per
surface alpha. Then performing the blitsurface of the character
onto
that surface. Then blitting that surface onto the background.
This
didn’t work. So, I tried just creating the surface in the GIMP, as
just
a .png with a fully transparent background, and using this
surface in
the same way, only to discover the same result. I know I am
just doing
something stupid, can someone please help me out?
Robert


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

The problem with colorkey blitting is that it only supports 1-bit alpha
(any given pixel is either opaque or transparant)… The original poster
specified “alpha blended text”, which means he probably wants
anti-aliased fonts… and therefore multi-bit alpah…

The function you probably want is:

SDL_SetAlpha(pSourceSurface, SDL_SRCALPHA, 0);

FYI, when pSourceSurface has an alpha channel
(pSourceSurface->format->Amask != 0) then the third argument is
ignored…

This essentialy enables the alpha channel on the surface

I hope this helps,

-LorenOn Thu, 2002-10-31 at 08:31, pvwr at sympatico.ca wrote:

Robert,

To copy every pixels with color different than FF you need to
set the colorkey, setting the alpha value won’t make a color
disappear when blitting. So all you need is to set ff as the
COLORKEY and an alpha value. I have done similar things and
had no problem, if you can’t solve it, send the code :).

Also it’s good that the background color on
your font, if a bitmap, is the same color as the intermediate
surface colorkey, or you will have to set a colorkey on it also.

Paulo

From: Robert Diel
Date: 2002/10/31 Thu AM 10:30:43 EST
To: sdl at libsdl.org
Subject: [SDL] New to SDL and have an alpha blitting question

Hello All,
I have recently started working with SDL and have had little to
no
problems until recently. I am trying to create a transparent text
overlay. So, what I want to do is blit per-pixel alpha blended
text
onto a transparent surface, so that I can overlay this surface
on
different backgrounds. My problem is that I cannot figure out
how to do
this without the entire surface either becoming fully opaque or
fully
transparent. My biggest problem is I need to find out how to
just copy
the per-pixel alpha information from a character font onto a
transparent
surface, and then blit that surface. I have tried creating a
surface,
then filling it with the color 0xff000000 where FF is the alpha
value.
Then setting the alpha with SDL_SetAlpha to no
SDL_SRCALPHA and no per
surface alpha. Then performing the blitsurface of the character
onto
that surface. Then blitting that surface onto the background.
This
didn’t work. So, I tried just creating the surface in the GIMP, as
just
a .png with a fully transparent background, and using this
surface in
the same way, only to discover the same result. I know I am
just doing
something stupid, can someone please help me out?
Robert


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

I attempted this and the color key solution to no avail. It seems
almost like when I am blitting my anti-aliased text to my ,per-pixel not
per-surface, alpha transparent background, it treats the background as
non-alpha and blends the text (which is black with alpha anti-aliasing),
with the background which has pixel value R:0 G:0 B:0 A:0. I assume
this pixel value means that, if treated correctly, this surface is
transparent. My assumption is that if alpha blending is done correctly
then an opaque pixel from my font like R:0 G:0 B:0 A:255 would blend
correctly with the background value of R:0 G:0 B:0 A:0 and produce an
opaque pixel that is R:0 G:0 B:0 A:255.
I have assured that the font blitting is correct to this surface when
the surface is opaque, by filling it with white and blitting it to the
screen. So, overall I have to assume it has something to do with the
process of blitting these aliased fonts to the alpha transparent
background.
Thank you for you help so far, and any further assistance would be
greatly appreciated.
RobertOn Thu, 2002-10-31 at 12:59, Loren Osborn wrote:

The problem with colorkey blitting is that it only supports 1-bit alpha
(any given pixel is either opaque or transparant)… The original poster
specified “alpha blended text”, which means he probably wants
anti-aliased fonts… and therefore multi-bit alpah…

The function you probably want is:

SDL_SetAlpha(pSourceSurface, SDL_SRCALPHA, 0);

FYI, when pSourceSurface has an alpha channel
(pSourceSurface->format->Amask != 0) then the third argument is
ignored…

This essentialy enables the alpha channel on the surface

I hope this helps,

-Loren

On Thu, 2002-10-31 at 08:31, pvwr at sympatico.ca wrote:

Robert,

To copy every pixels with color different than FF you need to
set the colorkey, setting the alpha value won’t make a color
disappear when blitting. So all you need is to set ff as the
COLORKEY and an alpha value. I have done similar things and
had no problem, if you can’t solve it, send the code :).

Also it’s good that the background color on
your font, if a bitmap, is the same color as the intermediate
surface colorkey, or you will have to set a colorkey on it also.

Paulo

From: Robert Diel <@Robert_Diel>
Date: 2002/10/31 Thu AM 10:30:43 EST
To: sdl at libsdl.org
Subject: [SDL] New to SDL and have an alpha blitting question

Hello All,
I have recently started working with SDL and have had little to
no
problems until recently. I am trying to create a transparent text
overlay. So, what I want to do is blit per-pixel alpha blended
text
onto a transparent surface, so that I can overlay this surface
on
different backgrounds. My problem is that I cannot figure out
how to do
this without the entire surface either becoming fully opaque or
fully
transparent. My biggest problem is I need to find out how to
just copy
the per-pixel alpha information from a character font onto a
transparent
surface, and then blit that surface. I have tried creating a
surface,
then filling it with the color 0xff000000 where FF is the alpha
value.
Then setting the alpha with SDL_SetAlpha to no
SDL_SRCALPHA and no per
surface alpha. Then performing the blitsurface of the character
onto
that surface. Then blitting that surface onto the background.
This
didn’t work. So, I tried just creating the surface in the GIMP, as
just
a .png with a fully transparent background, and using this
surface in
the same way, only to discover the same result. I know I am
just doing
something stupid, can someone please help me out?
Robert


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

The problem is in static void BlitRGBtoRGBPixelAlpha(SDL_BlitInfo *info) in
SDL_blit_A.c

As documented in http://sdldoc.csn.ul.ie/sdlsetalpha.php :
“RGBA->RGBA with SDL_SRCALPHA The source is alpha-blended with the
destination using the source alpha channel. The alpha channel in the
destination surface is left untouched. SDL_SRCCOLORKEY is ignored.”

It seems you expected that blitting an opaque pixel on a transparent pixel
would yield an opaque pixel.
I agree that that seems more natural. I think this has been discussed on
this mailing list before, but I never understood why blitting is implemented
as it is (and as it is documented).

The code below approximates what seems much more natural to me. When you
paste this code in SDL_blit_A.c and blit two images with an alphachannel
after each other onto a surface that is completely transparent to start
with, the resulting image is approximately the combination of the two in the
way that photoshop calls “normal blending”.
The last else clause however is only an approximation. It calculates the
alphachannel correctly but the calculation of the color components is not
exact.

/* fast ARGB888->(A)RGB888 blending with pixel alpha */
static void BlitRGBtoRGBPixelAlpha(SDL_BlitInfo *info)
{
int width = info->d_width;
int height = info->d_height;
Uint32 *srcp = (Uint32 *)info->s_pixels;
int srcskip = info->s_skip >> 2;
Uint32 *dstp = (Uint32 *)info->d_pixels;
int dstskip = info->d_skip >> 2;
while(height–) {
DUFFS_LOOP4({
Uint32 dalpha;
Uint32 d = *dstp;
Uint32 s1;
Uint32 d1;
Uint32 s = *srcp;
Uint32 alpha = s >> 24;
Uint32 alphad = d >> 24;
if (alpha == SDL_ALPHA_OPAQUE) {
*dstp = s;
} else if (alphad == SDL_ALPHA_OPAQUE) {
s1 = s & 0xff00ff;
d1 = d & 0xff00ff;
d1 = (d1 + ((s1 - d1) * alpha >> 8)) & 0xff00ff;
s &= 0xff00;
d &= 0xff00;
d = (d + ((s - d) * alpha >> 8)) & 0xff00;
*dstp = d1 | d | 0xff000000;
} else {
// dalpha = d & 0xff000000;
// dalpha = (dalpha + (((s & 0xff000000) - dalpha)>>8)alpha) &
0xff000000; // hji
dalpha = 0xff000000 - (((256-alpha)
(255-alphad)) << 16) & 0xff000000;
s1 = s & 0xff00ff;
d1 = d & 0xff00ff;
d1 = (d1 + ((s1 - d1) * alpha >> 8)) & 0xff00ff;
s &= 0xff00;
d &= 0xff00;
d = (d + ((s - d) * alpha >> 8)) & 0xff00;
*dstp = d1 | d | dalpha;
}
++srcp;
++dstp;
}, width);
srcp += srcskip;
dstp += dstskip;
}

}

Huib-Jan Imbens> ----- Original Message -----

From: robert@littlebitlost.com (Robert Diel)
To:
Sent: Thursday, October 31, 2002 8:56 PM
Subject: Re: [SDL] New to SDL and have an alpha blitting question

I attempted this and the color key solution to no avail. It seems
almost like when I am blitting my anti-aliased text to my ,per-pixel not
per-surface, alpha transparent background, it treats the background as
non-alpha and blends the text (which is black with alpha anti-aliasing),
with the background which has pixel value R:0 G:0 B:0 A:0. I assume
this pixel value means that, if treated correctly, this surface is
transparent. My assumption is that if alpha blending is done correctly
then an opaque pixel from my font like R:0 G:0 B:0 A:255 would blend
correctly with the background value of R:0 G:0 B:0 A:0 and produce an
opaque pixel that is R:0 G:0 B:0 A:255.
I have assured that the font blitting is correct to this surface when
the surface is opaque, by filling it with white and blitting it to the
screen. So, overall I have to assume it has something to do with the
process of blitting these aliased fonts to the alpha transparent
background.
Thank you for you help so far, and any further assistance would be
greatly appreciated.
Robert

On Thu, 2002-10-31 at 12:59, Loren Osborn wrote:

The problem with colorkey blitting is that it only supports 1-bit alpha
(any given pixel is either opaque or transparant)… The original poster
specified “alpha blended text”, which means he probably wants
anti-aliased fonts… and therefore multi-bit alpah…

The function you probably want is:

SDL_SetAlpha(pSourceSurface, SDL_SRCALPHA, 0);

FYI, when pSourceSurface has an alpha channel
(pSourceSurface->format->Amask != 0) then the third argument is
ignored…

This essentialy enables the alpha channel on the surface

I hope this helps,

-Loren

On Thu, 2002-10-31 at 08:31, pvwr at sympatico.ca wrote:

Robert,

To copy every pixels with color different than FF you need to
set the colorkey, setting the alpha value won’t make a color
disappear when blitting. So all you need is to set ff as the
COLORKEY and an alpha value. I have done similar things and
had no problem, if you can’t solve it, send the code :).

Also it’s good that the background color on
your font, if a bitmap, is the same color as the intermediate
surface colorkey, or you will have to set a colorkey on it also.

Paulo

From: Robert Diel
Date: 2002/10/31 Thu AM 10:30:43 EST
To: sdl at libsdl.org
Subject: [SDL] New to SDL and have an alpha blitting question

Hello All,
I have recently started working with SDL and have had little to
no
problems until recently. I am trying to create a transparent text
overlay. So, what I want to do is blit per-pixel alpha blended
text
onto a transparent surface, so that I can overlay this surface
on
different backgrounds. My problem is that I cannot figure out
how to do
this without the entire surface either becoming fully opaque or
fully
transparent. My biggest problem is I need to find out how to
just copy
the per-pixel alpha information from a character font onto a
transparent
surface, and then blit that surface. I have tried creating a
surface,
then filling it with the color 0xff000000 where FF is the alpha
value.
Then setting the alpha with SDL_SetAlpha to no
SDL_SRCALPHA and no per
surface alpha. Then performing the blitsurface of the character
onto
that surface. Then blitting that surface onto the background.
This
didn’t work. So, I tried just creating the surface in the GIMP, as
just
a .png with a fully transparent background, and using this
surface in
the same way, only to discover the same result. I know I am
just doing
something stupid, can someone please help me out?
Robert


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Thank you for the answer. I will try this solution immediately. In
rereading the mentioned section I see that the destination alpha is
maintained and you are correct this doesn’t seem natural. I would
believe that this would work more like a color key. In which you would
blit a surface with a color key opaquely into a back buffer of some
kind, blit images ontop of that, then blit the result transparently,
removing the color key, onto the final surface.
Thank you again for the help,
RobertOn Thu, 2002-10-31 at 15:30, Huib-Jan Imbens wrote:

The problem is in static void BlitRGBtoRGBPixelAlpha(SDL_BlitInfo *info) in
SDL_blit_A.c

As documented in http://sdldoc.csn.ul.ie/sdlsetalpha.php :
“RGBA->RGBA with SDL_SRCALPHA The source is alpha-blended with the
destination using the source alpha channel. The alpha channel in the
destination surface is left untouched. SDL_SRCCOLORKEY is ignored.”

It seems you expected that blitting an opaque pixel on a transparent pixel
would yield an opaque pixel.
I agree that that seems more natural. I think this has been discussed on
this mailing list before, but I never understood why blitting is implemented
as it is (and as it is documented).

The code below approximates what seems much more natural to me. When you
paste this code in SDL_blit_A.c and blit two images with an alphachannel
after each other onto a surface that is completely transparent to start
with, the resulting image is approximately the combination of the two in the
way that photoshop calls “normal blending”.
The last else clause however is only an approximation. It calculates the
alphachannel correctly but the calculation of the color components is not
exact.

/* fast ARGB888->(A)RGB888 blending with pixel alpha */
static void BlitRGBtoRGBPixelAlpha(SDL_BlitInfo *info)
{
int width = info->d_width;
int height = info->d_height;
Uint32 *srcp = (Uint32 *)info->s_pixels;
int srcskip = info->s_skip >> 2;
Uint32 *dstp = (Uint32 *)info->d_pixels;
int dstskip = info->d_skip >> 2;
while(height–) {
DUFFS_LOOP4({
Uint32 dalpha;
Uint32 d = *dstp;
Uint32 s1;
Uint32 d1;
Uint32 s = *srcp;
Uint32 alpha = s >> 24;
Uint32 alphad = d >> 24;
if (alpha == SDL_ALPHA_OPAQUE) {
*dstp = s;
} else if (alphad == SDL_ALPHA_OPAQUE) {
s1 = s & 0xff00ff;
d1 = d & 0xff00ff;
d1 = (d1 + ((s1 - d1) * alpha >> 8)) & 0xff00ff;
s &= 0xff00;
d &= 0xff00;
d = (d + ((s - d) * alpha >> 8)) & 0xff00;
*dstp = d1 | d | 0xff000000;
} else {
// dalpha = d & 0xff000000;
// dalpha = (dalpha + (((s & 0xff000000) - dalpha)>>8)alpha) &
0xff000000; // hji
dalpha = 0xff000000 - (((256-alpha)
(255-alphad)) << 16) & 0xff000000;
s1 = s & 0xff00ff;
d1 = d & 0xff00ff;
d1 = (d1 + ((s1 - d1) * alpha >> 8)) & 0xff00ff;
s &= 0xff00;
d &= 0xff00;
d = (d + ((s - d) * alpha >> 8)) & 0xff00;
*dstp = d1 | d | dalpha;
}
++srcp;
++dstp;
}, width);
srcp += srcskip;
dstp += dstskip;
}

}

Huib-Jan Imbens

----- Original Message -----
From: Robert Diel <@Robert_Diel>
To:
Sent: Thursday, October 31, 2002 8:56 PM
Subject: Re: [SDL] New to SDL and have an alpha blitting question

I attempted this and the color key solution to no avail. It seems
almost like when I am blitting my anti-aliased text to my ,per-pixel not
per-surface, alpha transparent background, it treats the background as
non-alpha and blends the text (which is black with alpha anti-aliasing),
with the background which has pixel value R:0 G:0 B:0 A:0. I assume
this pixel value means that, if treated correctly, this surface is
transparent. My assumption is that if alpha blending is done correctly
then an opaque pixel from my font like R:0 G:0 B:0 A:255 would blend
correctly with the background value of R:0 G:0 B:0 A:0 and produce an
opaque pixel that is R:0 G:0 B:0 A:255.
I have assured that the font blitting is correct to this surface when
the surface is opaque, by filling it with white and blitting it to the
screen. So, overall I have to assume it has something to do with the
process of blitting these aliased fonts to the alpha transparent
background.
Thank you for you help so far, and any further assistance would be
greatly appreciated.
Robert

On Thu, 2002-10-31 at 12:59, Loren Osborn wrote:

The problem with colorkey blitting is that it only supports 1-bit alpha
(any given pixel is either opaque or transparant)… The original poster
specified “alpha blended text”, which means he probably wants
anti-aliased fonts… and therefore multi-bit alpah…

The function you probably want is:

SDL_SetAlpha(pSourceSurface, SDL_SRCALPHA, 0);

FYI, when pSourceSurface has an alpha channel
(pSourceSurface->format->Amask != 0) then the third argument is
ignored…

This essentialy enables the alpha channel on the surface

I hope this helps,

-Loren

On Thu, 2002-10-31 at 08:31, pvwr at sympatico.ca wrote:

Robert,

To copy every pixels with color different than FF you need to
set the colorkey, setting the alpha value won’t make a color
disappear when blitting. So all you need is to set ff as the
COLORKEY and an alpha value. I have done similar things and
had no problem, if you can’t solve it, send the code :).

Also it’s good that the background color on
your font, if a bitmap, is the same color as the intermediate
surface colorkey, or you will have to set a colorkey on it also.

Paulo

From: Robert Diel <@Robert_Diel>
Date: 2002/10/31 Thu AM 10:30:43 EST
To: sdl at libsdl.org
Subject: [SDL] New to SDL and have an alpha blitting question

Hello All,
I have recently started working with SDL and have had little to
no
problems until recently. I am trying to create a transparent text
overlay. So, what I want to do is blit per-pixel alpha blended
text
onto a transparent surface, so that I can overlay this surface
on
different backgrounds. My problem is that I cannot figure out
how to do
this without the entire surface either becoming fully opaque or
fully
transparent. My biggest problem is I need to find out how to
just copy
the per-pixel alpha information from a character font onto a
transparent
surface, and then blit that surface. I have tried creating a
surface,
then filling it with the color 0xff000000 where FF is the alpha
value.
Then setting the alpha with SDL_SetAlpha to no
SDL_SRCALPHA and no per
surface alpha. Then performing the blitsurface of the character
onto
that surface. Then blitting that surface onto the background.
This
didn’t work. So, I tried just creating the surface in the GIMP, as
just
a .png with a fully transparent background, and using this
surface in
the same way, only to discover the same result. I know I am
just doing
something stupid, can someone please help me out?
Robert


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl