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

Author Topic: Pre-multiplied Alpha (blend mode + convert on load)  (Read 5057 times)

0 Members and 1 Guest are viewing this topic.

NightCabbage

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
Pre-multiplied Alpha (blend mode + convert on load)
« on: December 01, 2012, 04:45:40 am »
Hi all

My first feature request is something I'm a little surprised isn't already here :)

I'd love to see pre-multiplied alpha in SFML.

Pre-multiplied alpha makes life a lot easier. Not only can you so alpha and additive blending in one pass, but it also makes objects with transparent backgrounds look better (no ugly black borders, etc.)

For example, XNA uses pre-multiplied alpha by default. Very handy, and after seeing how good it is, I can't really see any reason to NOT use pre-multiplied alpha.

There is an older thread on this subject, but I didn't want to necro it.
http://en.sfml-dev.org/forums/index.php?topic=5026.0

It would be great if not only was this added as a blend mode, but if there was also an option that SFML would automatically convert an image to pre-multiplied alpha when it was loaded (by simply multiplying the colour by the alpha).

Thanks!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Pre-multiplied Alpha (blend mode + convert on load)
« Reply #1 on: December 01, 2012, 08:41:44 am »
Quote
There is an older thread on this subject, but I didn't want to necro it
Why not? This is exactly the same request, so it's more convenient for everyone if all the discussions are gathered in a single thread.

Never mind, I'll add a link to this topic in the corresponding task on the tracker.
Laurent Gomila - SFML developer

NightCabbage

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
Re: Pre-multiplied Alpha (blend mode + convert on load)
« Reply #2 on: December 01, 2012, 04:16:53 pm »
Thanks Laurent

Sorry, most forums look down on resurrecting year old threads :)

Very much looking forward to this feature, but I imagine I can just modify it myself for now without too many problems?

I'd  also like to take the opportunity to thank you for SFML, you've done so much great work and I'm really grateful!

I'm currently swapping over from XNA :)

I look forward to posting more about my upcoming game! 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Pre-multiplied Alpha (blend mode + convert on load)
« Reply #3 on: December 01, 2012, 04:24:29 pm »
Quote
I imagine I can just modify it myself for now without too many problems?
Yep, should be easy to do :)
Laurent Gomila - SFML developer

NightCabbage

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
Re: Pre-multiplied Alpha (blend mode + convert on load)
« Reply #4 on: December 02, 2012, 03:55:34 am »
Hmmmm, this might be a dumb question... but where can I find the source code fot the .Net version of the SFML 2.0 Release Candidate?

(so I can modify it to add pre-multiplied alpha)

Also will the full version 2 release be within the next few months? :D

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10390
    • View Profile
    • development blog
    • Email
AW: Re: Pre-multiplied Alpha (blend mode + convert on load)
« Reply #5 on: December 02, 2012, 07:31:08 am »
Hmmmm, this might be a dumb question... but where can I find the source code fot the .Net version of the SFML 2.0 Release Candidate?

(so I can modify it to add pre-multiplied alpha)
It's on GitHub next to the C++ and CSFML repositories.

Also will the full version 2 release be within the next few months? :D
Everyone hopes so, but I doubt it. The offical statement is, as always: 'soon'. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://dev.my-gate.net/

NightCabbage

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
Re: Pre-multiplied Alpha (blend mode + convert on load)
« Reply #6 on: December 02, 2012, 09:08:21 am »
Cool thanks for the info :D

So if I'm making a game right now, am I better off using 2.0 or 1.6?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6278
  • Thor Developer
    • View Profile
    • Bromeon
Re: Pre-multiplied Alpha (blend mode + convert on load)
« Reply #7 on: December 02, 2012, 10:33:31 am »
You should use SFML 2.0, it has more features and less bugs. SFML 1.6 has not been maintained for years.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

NightCabbage

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
Re: Pre-multiplied Alpha (blend mode + convert on load)
« Reply #8 on: December 02, 2012, 02:18:34 pm »
Oh cool thanks for the info - I'm only just getting started with SFML so it's great to know WHERE to start :D

2.0 it is!

-EDIT-

About the source code:

Ah, I see - most of the .net stuff is just a wrapper for the normal dll files. So if I want to modify it (to add pre-multiplied alpha) I still need to compile the c++ version?
(or actually the c version? they seem to reference "csfml-window", etc.)
« Last Edit: December 02, 2012, 02:26:58 pm by NightCabbage »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10390
    • View Profile
    • development blog
    • Email
AW: Re: Pre-multiplied Alpha (blend mode + convert on load)
« Reply #9 on: December 02, 2012, 02:30:48 pm »
Quote from: NightCabbage
Ah, I see - most of the .net stuff is just a wrapper for the normal dll files. So if I want to modify it (to add pre-multiplied alpha) I still need to compile the c++ version?
(or actually the c version? they seem to reference "csfml-window", etc.)
Yes, iirc SFML.NET uses CSFML (like most of the other bindings). ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://dev.my-gate.net/

NightCabbage

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
Re: Pre-multiplied Alpha (blend mode + convert on load)
« Reply #10 on: December 04, 2012, 05:09:39 am »
-EDIT-

Ok, because this post isn't relevant to pre-multiplied alpha, I've made a new topic for all my silly questions :)

http://en.sfml-dev.org/forums/index.php?topic=9878.0

And I have decided to write a guide when I know what I'm doing a bit better (which will be thanks to your help answering my questions).

-FINISH EDIT-
« Last Edit: December 04, 2012, 07:55:37 am by Laurent »

Skwint

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Pre-multiplied Alpha (blend mode + convert on load)
« Reply #11 on: May 23, 2013, 03:50:27 pm »
*Bump*
+1 vote in favour of this feature request.

To quote TomF "What we think of as conventional alpha-blending is basically wrong"
It's been obviously wrong since atleast 1984.  It was wrong before then, but it would be understandable if people didn't realise it :P

So just so you know, I'm another guy off to implement premultiplied himself because I need something that works  :-\



cire

  • Full Member
  • ***
  • Posts: 138
    • View Profile

Skwint

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Pre-multiplied Alpha (blend mode + convert on load)
« Reply #13 on: May 23, 2013, 09:34:56 pm »
That's reassuring.  Thanks.

Since I found this thread, instead of finding github, I'll stick this here for any that want it:
void Image::convertToPremultipliedAlpha()
{
        if (!m_pixels.empty())
        {
                Uint8* source = &m_pixels[0];
                for (unsigned int y = 0; y < m_size.y; ++y)
                {
                        for (unsigned int x = 0; x < m_size.x; ++x)
                        {
                                Uint32 alpha = *(source + 3);
                                *(source    ) = Uint8((Uint32(*(source    )) * alpha) / 255);
                                *(source + 1) = Uint8((Uint32(*(source + 1)) * alpha) / 255);
                                *(source + 2) = Uint8((Uint32(*(source + 2)) * alpha) / 255);
                                source += 4;
                        }
                }
        }
}
 
optimizations exist for this I'm sure, but this works, and I don't have a profiler to hand.

 

anything