sidebar features
sidebar content

Go Back   Blender Artists Forums > General Forums > Python & Plugins

Closed Thread
 
Thread Tools
phlip's Avatar
phlip phlip is offline
Member
 
Join Date: Mar 2002
Location: Australia
Posts: 2,303
If you don't like reading long technical threads, you can skip right down to the pictures and the links at the bottom, you won't miss much.

The history:
Alpha transparancy is based on a generalised linear progression:
[size=5]x[/size][size=1]n[/size][size=5] = x[/size][size=1]1[/size][size=5]n + x[/size][size=1]0[/size][size=5](1 – n)[/size]
So, for example, if a pixel with a shade of 4 and an alpha of 0.25 is drawn over a pixel with a shade of 2, the result is
4*0.25 + 2*(1 – 0.25) = 2.5
which you'd expect.

However, if an image is drawn several times, it will always be calculating 4*0.25 for that pixel, regardless of what it's being drawn on - so if we pre-multiply this then we can save a multiplication for every pixel each time it's drawn. So we store a shade of 1 and an alpha of 0.25, and the calculation becomes:
1 + 2*(1 – 0.25) = 2.5

The problem:
Not all programs (in fact, none that I can think of) will read premultiplied images. Even textures in blender don't have that option. So when they load the premultiplied image, they do the normal calculation and get:
1*0.25 + 2*(1 – 0.25) = 1.75
Which is too dark. The result is that premultiplied images look much darker in transparent areas.

In Blender however, generating premultiplied images is easy - if you make the world black then the colour is already premultiplied and you just have to calculate the alpha. Thus Blender has the Premul button for generating premultiplied images, but has no facility to create the "normal" alpha images.

Example:
To show you what I mean, here is a blue halo, with the colour channel, the alpha channel, and the transparent image (not really transparent, but looks right on this forum):

The colour is blue everywhere because the halo really is blue everywhere, the alpha makes it fade off to the edges.
But here is the premultiplied image as given by blender:

The colour mask actually looks better on this one, because you can make out the halo, and this is what blender displays. However its results in the last picture are less engouraging. The fact is that, halfway out on the halo, the halo colour is a dark blue because of the premul, so it's drawn with a dark blue in the last frame!

The fix:
I have written a sequence plugin that will cancel out the premultiplication done by blender when it makes a premul picture, so that when it is loaded by another program that accepts the alpha channel, it looks correct.

I called it "PostMul" since it's the opposite of Premul, and I don't know what the technical term for non-premultiplied alpha is...

It also has a button to show the alpha channel as a greyscale image - I realise there's already a plugin to do that ("alphamatte") but I figure this is something that goes together with the un-premultiplying, and it is easier to click a button than to replace the plugin strip (I know a couple of things that want a seperate image and greyscale alpha mask, so I'll personally be using this button - and that's reason enough to put it there )

Download Windows build here: http://members.lycos.co.uk/phlipping/postmul.zip
Download Sourcecode here: http://members.lycos.co.uk/phlipping/postmul.c

If someone could make a linux build that would be great.

I've sent this off to the plugin repository, hopefully it will be listed soon.
............................................
<link rel="signature" type="text/hilarious" href="/funnysig.txt" />
#1   Old 21-Mar-05, 12:35   


broken's Avatar
broken broken is offline
Member
 
Join Date: Feb 2003
Location: Sydney, AU
Posts: 2,649
Hey there,

How is this different to Blender's 'Key' setting? Any details?

Cheers
............................................
web log
#2   Old 21-Mar-05, 12:57   
phlip's Avatar
phlip phlip is offline
Member
 
Join Date: Mar 2002
Location: Australia
Posts: 2,303
"Key" makes a picture with only two alpha values - on and off (unless you have transparent objcts in the scene) so you use it for places where these are your only options (GIF's for example).

"Sky" pictures are pretty much useless for compositing, even though you get an alpha channel the world gets in the way.

"Premul" pictures are the only real option for fractional alpha values in compositing, but have the problems with black effects, as I explained above.

I don't know of any way to get a "normal" alpha image out of blender other than this plugin.

[edit]
New Sample Pic:
Left: Colour channels, Centre: Alpha channel, Right: Transparent result
Rows are Sky, Premul, Key and Postmul respectively.

Key looks rather awful for this, it looks much better on things that don't have fractional transparancy. I don't really know why it looks as bad as it does though...
Notice that of the last column only the bottom picture doesn't have artefacts of some description.

BTW: If you care, the right image was made by taking the rendered output and putting it on a shadeless plane in a new scene, mapping it to Col and Alpha, with a world colour that matches elYsiun's background colour (R, G, B all 0.4)

BTW: This plugin is on the repository now: http://www-users.cs.umn.edu/~mein/bl...uence/postmul/ so you can get linux builds etc there.
............................................
<link rel="signature" type="text/hilarious" href="/funnysig.txt" />
#3   Old 21-Mar-05, 13:28   
Caronte's Avatar
Caronte Caronte is offline
Member
 
Join Date: Mar 2002
Location: Valencia-Spain-Europe
Posts: 503
I think that must be corrected inside Blender code, because IMHO is a long know bug.

Thanks anyway by your code
............................................
Caronte.
http://www.nicodigital.com
\"Some day, all will be digital\"
#4   Old 21-Mar-05, 18:47   
phlip's Avatar
phlip phlip is offline
Member
 
Join Date: Mar 2002
Location: Australia
Posts: 2,303
It's not a bug, the pictures are technically correct.
IMHO it's just a missing feature
............................................
<link rel="signature" type="text/hilarious" href="/funnysig.txt" />
#5   Old 22-Mar-05, 02:03   
DarkEcho's Avatar
DarkEcho DarkEcho is offline
 
Join Date: Jun 2004
Posts: 72
Still a little bit confused, but seems a quite usefull feature to have.
Had seen those black rings around halos and such before, but never really got why they appeared. Thanks for your plugin
............................................
Quantum mechanics: The dreams stuff is made of.
Yum.. Cells... http://www.blachford.info/computer/Cells/Cell0.html
Anything worth taking seriously is worth making fun of.
Quickly, I must hurry, for there go my people and I am their leader.
#6   Old 22-Mar-05, 02:45   
Caronte's Avatar
Caronte Caronte is offline
Member
 
Join Date: Mar 2002
Location: Valencia-Spain-Europe
Posts: 503
Quote:
Originally Posted by phlip
It's not a bug, the pictures are technically correct.
IMHO it's just a missing feature
Yes, you are right
............................................
Caronte.
http://www.nicodigital.com
\"Some day, all will be digital\"
#7   Old 22-Mar-05, 08:43   
broken's Avatar
broken broken is offline
Member
 
Join Date: Feb 2003
Location: Sydney, AU
Posts: 2,649
As far as I know, key is supposed to do the same thing as your postmul - it's certainly not meant to be GIF-like transparency.. Are you using 2.36 or a CVS build? I've run into some weirdness in CVS builds using Key, maybe this is related. It can also depend on what program you're using to import and composite. Photoshop tends to read Key alpha (well at least in 2.36) fine.

By the way, are you using unified renderer for those shots? Non-unified renderer is known to have problems with halo transparency.
............................................
web log
#8   Old 22-Mar-05, 12:19   
phlip's Avatar
phlip phlip is offline
Member
 
Join Date: Mar 2002
Location: Australia
Posts: 2,303
It's the same in 2.23 through 2.36, and I'd check earlier versions but I don't have them.

It matters not whether Unified Renderer is on or not, they end up exactly the same either way.

The examples in the first post was a halo, but the second picture is two planes with Sphere Blend textures mapped to Alpha. Halos with Key on have similar ugly effects though.

Correct me if I'm wrong, but doesn't Key just make it so objects aren't antialiased against the world colour, so that if that colour is chroma-keyed out there's no aliasing halo?

If you want to mess with it, this is the scene that I made all of the second set of pictures from: http://members.lycos.co.uk/phlipping/Postmultest.blend
............................................
<link rel="signature" type="text/hilarious" href="/funnysig.txt" />
#9   Old 22-Mar-05, 15:58   
sonix's Avatar
sonix sonix is offline
Member
 
Join Date: Oct 2002
Location: Leeds, UK
Posts: 2,660
I use Premul often, but haven't come across the problems you're describing.


My method.

Turn Premul on.
Select TGA output at 100 quality.
Turn on RGBA.

When opened in Photoshop, I have a transparent section where the Blender sky can be seen and the file is regarded as a layer, instead of as a background.

I don't understand why there is a problem, but perhaps I'm missing something.

Sonix.
............................................
'Please do not feed this monkey' |
Click here to find the Unofficial Car Material Library, and my Blender works.
For my most recent work, click here.
#10   Old 23-Mar-05, 04:21   
phlip's Avatar
phlip phlip is offline
Member
 
Join Date: Mar 2002
Location: Australia
Posts: 2,303
I tried that (including setting the quality to 100 even though tga is lossless).
I don't have photoshop, but I loaded it in the GIMP:

Notice the dark glow around the blends.

But maybe PS will load Premul images properly? Did you have to set any options for it to do it?
............................................
<link rel="signature" type="text/hilarious" href="/funnysig.txt" />
#11   Old 23-Mar-05, 05:45   
EnV's Avatar
EnV EnV is offline
Member
 
Join Date: Sep 2002
Location: Back to Verona, Italy
Posts: 1,062
AFAIK, the mentioned problem happens also in the Blender sequencer, as you try to comp blurred tgas. Ton made a fix for it a couples of bf ago, but I don't know if it's still working the correct way.

EnV
............................................
EnV
------------------------------------------------------
Time to start to change my signature and... err...
#12   Old 23-Mar-05, 08:50   
sonix's Avatar
sonix sonix is offline
Member
 
Join Date: Oct 2002
Location: Leeds, UK
Posts: 2,660
Ok I did some tests today.

1 plane
3 light setup (1 shadow)
material > Magic texture for colour > circular blend for alpha.
Premul on
RGBA on
Tga 100%


Yes I can notice the grey areas between the blends of transparency and opaque, when loaded into PS7.

Attempted with Z trans on with same result, didn't try ray trans though.

The reason I had never come across this problem is I hadn't used Premul & RGBA in this manner. I use it to composite a scene onto a new background.

I also noticed that the Blender render window and the output tga file have different alpha levels. That is the image looks brighter and there's more of it in the render window than there is in the tga.

So well done for sorting a solution.

Sonix.
............................................
'Please do not feed this monkey' |
Click here to find the Unofficial Car Material Library, and my Blender works.
For my most recent work, click here.
#13   Old 24-Mar-05, 00:25   
broken's Avatar
broken broken is offline
Member
 
Join Date: Feb 2003
Location: Sydney, AU
Posts: 2,649
Phlip, here is your example file, rendered with Key alpha, unified renderer off, from Blender 2.36, opened in Photoshop CS:


Side by side RGB / RGBA

Looks fine to me. It seems turning on the unified renderer gives the weird results as in your previously posted Key image.

By the way, as a general note, some apps expect alpha to be premultiplied, and compensate accordingly. For example, the window on the left is a premul image in Photoshop, and underneath is the same image in Shake (which expects premultiplied):

............................................
web log
#14   Old 24-Mar-05, 00:45   
sonix's Avatar
sonix sonix is offline
Member
 
Join Date: Oct 2002
Location: Leeds, UK
Posts: 2,660
Quote:
Originally Posted by broken
By the way, as a general note, some apps expect alpha to be premultiplied, and compensate accordingly. For example, the window on the left is a premul image in Photoshop, and underneath is the same image in Shake (which expects premultiplied):
Ahh, this would be why PS looks grey, any way to make PS expect a pre-mul image?


Sonix.
............................................
'Please do not feed this monkey' |
Click here to find the Unofficial Car Material Library, and my Blender works.
For my most recent work, click here.
#15   Old 24-Mar-05, 01:12   
broken's Avatar
broken broken is offline
Member
 
Join Date: Feb 2003
Location: Sydney, AU
Posts: 2,649
Not that I know of - try just using Key though
............................................
web log
#16   Old 24-Mar-05, 01:16   
phlip's Avatar
phlip phlip is offline
Member
 
Join Date: Mar 2002
Location: Australia
Posts: 2,303
OK, I copied the picture over to my linux box and rendered it key on there and it worked.

It seems its a bug with the windows version.
............................................
<link rel="signature" type="text/hilarious" href="/funnysig.txt" />
#17   Old 24-Mar-05, 03:53   
Closed Thread

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT. The time now is 12:41.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Logo and website design copyright © 2006 by froodee design bureau. All rights reserved.
Other Blender Sites
new icon Blender Homepage »
The official Blender homepage
new icon BlenderNation »
Fresh Blender News, Every Day
new icon Blenderart Magazine »
Blender articles, tutorials and images.
Blender Headlines
Featured Artwork
Short animation: Barrel by Phlopper
Woolly mammoth by sebastian_k
Photorealistic classic furniture by eMirage
Social BlenderArtists