Hello all at java2d,
Hello Chris,

after bouncing my head for some time over OpenGL, alpha compositing,
accumulated and premultiplied alpha I decided (curiosity) to take a look at
the j2se6 source code just to see what happens behind the scenes.

All I discovered (surprise ?) is that the OpenGL codepath of java2d has to
deal with premultiplied alpha pixels. That's probably because the Porter and
Duff compositing rules "work better" with premultiplied data ;)

But premultiplying and unpremultiplying is done in software loops, so this
generates some overhead, expecially when using java2d for creating offscreen
graphics with unpremultiplied images (me).

So I decided to implement my very first OpenGL fragment shader that does a
SRC_OVER composite on unpremultiplied data. I'm a true beginer, so please
forgive me.

Here's the code

-----

uniform sampler2D texture;

void main()
{
 vec4 s = texture2D(texture,gl_TexCoord[0].st);
 vec4 d = gl_FragColor;

 float extra_alpha = 0.1;

 s.a = s.a * extra_alpha;

if (s.a == 0.0) gl_FragColor = d;
else if (s.a == 1.0) gl_FragColor = s;
else if (d.a == 0.0) {gl_FragColor = s;}
else
{
    float a = s.a + d.a - s.a * d.a;
 gl_FragColor.a = a;
 gl_FragColor.r = (s.a*s.r+d.a*d.r-s.a*d.a*d.r)/a;
 gl_FragColor.g = (s.a*s.g+d.a*d.g-s.a*d.a*d.g)/a;
 gl_FragColor.b = (s.a*s.b+d.a*d.b-s.a*d.a*d.b)/a;
}
}

-----

I works very well and, apart that compiler warning (!), the results are 1:1
the SRC_OVER I get from the Java2D software loops.

So the question: why don't implement the entire OpenGL java2d Composite with
fragment shaders and avoid premultiplied data ?

Cheers!

Mik
============================================================================
ClassX Development Italy  Via Francesca, 368/I I-56030 S.M. a Monte (PI) <
Tel.(+39)-0587-705153  Fax.(+39)-0587-705153  WEB: http://www.classx.it  <
============================================================================

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to