SFML community forums
Help => Graphics => Topic started by: Shump on May 14, 2008, 01:31:01 pm
-
Is it possible to create an alpha mask from a black and white image source? Does it have anything with sprite's blendmode to do?
-
It's not possible to use a separate alpha mask. But you should just put it in the alpha channel of your image.
-
you could also write a function that takes a B&W 'alpha' image and your other image, and constructs the alpha-channel for the first image.
A very basic version would be:
sf::Image CreateAlphaImageWithMask(std::string aImage, std::string aMask)
{
sf::Image image(), mask();
if (image.LoadFromFile(aImage) && mask.LoadFromFile(aMask))
{
for (int i = 0; i < image.GetWidth(); ++i)
{
for(int j = 0; j < image.GetHeight(); ++j)
{
Color c(image.GetPixel(i,j));
Color m(mask.GetPixel(i,j));
c.a = m.r*0.3 + m.g*0.4 + m.b*0.3;
image.SetPixel(i,j,c);
}
}
}
return image;
}
this is untested(written straight into the browser) has no bounds checking (so could overflow and cause issues) and uses what I think is the standard method of producing a greyscale for the alpha of 0.3r + 0.4g + 0.3b. It also has the potential to just return an empty image or the original image with no alpha if either image fails to load. It should be a good starting point though :)
-
sf::Image image(), mask();
You should remove the parenthesis, otherwise the compiler will think you're declaring functions.
c.a = m.r*0.3 + m.g*0.4 + m.b*0.3;
Not very accurate ;)
Try this :
c.a = static_cast<sf::Uint8>(m.r*0.39 + m.g*0.50 + m.b*0.11);
-
sf::Image image(), mask();
You should remove the parenthesis, otherwise the compiler will think you're declaring functions.
I think sure in this case, it will just call the default constructor, a kind of reinforcement of the default behaviour. You need to do much more than that to confuse the compiler (there is a great one you can do with the standard library though :)). I may be wrong though.
and yeah, I was trying to remember the standard conversion to greyscale :) I guess I was horribly wrong.
-
I think sure in this case, it will just call the default constructor, a kind of reinforcement of the default behaviour
Not really....
Just answer this question : how would you declare a function named image, taking no parameter and returning a sf::Image, and which doesn't look like the variable declaration you wrote previously ? ;)
-
doh... ok I give up :) and in future I'll try not to argue C++ syntax when I've spent the entire day coding in perl :roll:
-
So in other words, if I load an image that supports an alpha channel there's no need to create any alpha mask. That sounds OK to me (: Thanks!