Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Create alpha mask from B/W image  (Read 6172 times)

0 Members and 1 Guest are viewing this topic.

Shump

  • Newbie
  • *
  • Posts: 8
    • View Profile
Create alpha mask from B/W image
« 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?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Create alpha mask from B/W image
« Reply #1 on: May 14, 2008, 03:49:15 pm »
It's not possible to use a separate alpha mask. But you should just put it in the alpha channel of your image.
Laurent Gomila - SFML developer

workmad3

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
Create alpha mask from B/W image
« Reply #2 on: May 14, 2008, 04:31:56 pm »
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:
Code: [Select]

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 :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Create alpha mask from B/W image
« Reply #3 on: May 14, 2008, 04:43:54 pm »
Quote
sf::Image image(), mask();

You should remove the parenthesis, otherwise the compiler will think you're declaring functions.

Quote
c.a = m.r*0.3 + m.g*0.4 + m.b*0.3;

Not very accurate ;)
Try this :
Code: [Select]
c.a = static_cast<sf::Uint8>(m.r*0.39 + m.g*0.50 + m.b*0.11);
Laurent Gomila - SFML developer

workmad3

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
Create alpha mask from B/W image
« Reply #4 on: May 14, 2008, 05:54:37 pm »
Quote from: "Laurent"
Quote
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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Create alpha mask from B/W image
« Reply #5 on: May 14, 2008, 06:10:01 pm »
Quote
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 ? ;)
Laurent Gomila - SFML developer

workmad3

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
Create alpha mask from B/W image
« Reply #6 on: May 15, 2008, 09:28:24 am »
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:

Shump

  • Newbie
  • *
  • Posts: 8
    • View Profile
Create alpha mask from B/W image
« Reply #7 on: May 15, 2008, 10:57:08 pm »
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!

 

anything