File: ScaleBiasAndGradientShader.frag.txt

package info (click to toggle)
psychtoolbox-3 3.0.19.14.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 86,796 kB
  • sloc: ansic: 176,245; cpp: 20,103; objc: 5,393; sh: 2,753; python: 1,397; php: 384; makefile: 193; java: 113
file content (54 lines) | stat: -rw-r--r-- 2,200 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* Shader for applying a bias and scale factor to a rectangle texture during drawing.
 * Can be used to apply f(g)=(g + prescaleoffset)*scalefactor + postscaleoffset;
 * After remapping, the gradient image of the rescaled image is computed by taking
 * central derivatives in horizontal and vertical direction.
 *
 * Input:  Intensity image in RED channel of texture.
 * Output: RED channel   = gradient dx.
 *         GREEN channel = gradient dy.
 *         BLUE  channel = Remapped intensity image whose gradient was computed.
 *
 * (w)2006 by Mario Kleiner. Licensed under MIT license.
*/

#extension GL_ARB_texture_rectangle : enable

/* We currently only support rectangular textures: */
uniform sampler2DRect Image;

/* Input parameter constants to specify pre- post-scale bias and scaling factor:*/
uniform float prescaleoffset;
uniform float postscaleoffset;
uniform float scalefactor;

void main()
{
    float v1, v2;
    vec2 incolor;
    /* Need to lookup two texture values for output position dx(x,y) == (s,t): */
    v1 = texture2DRect(Image, gl_TexCoord[0].st + vec2(-1,0)).r;
    v2 = texture2DRect(Image, gl_TexCoord[0].st + vec2(+1,0)).r;
    /* Remap: */
    v1 = ((v1 + prescaleoffset) * scalefactor) + postscaleoffset;
    v2 = ((v2 + prescaleoffset) * scalefactor) + postscaleoffset;
    /* Derivative dx into RED channel: */
    gl_FragColor.r = 0.5 * (v2 - v1);

    /* Need to lookup two texture values for output position dy(x,y) == (s,t): */
    v1 = texture2DRect(Image, gl_TexCoord[0].st + vec2(0,-1)).r;
    v2 = texture2DRect(Image, gl_TexCoord[0].st + vec2(0,+1)).r;
    /* Remap: */
    v1 = ((v1 + prescaleoffset) * scalefactor) + postscaleoffset;
    v2 = ((v2 + prescaleoffset) * scalefactor) + postscaleoffset;
    /* Derivative dy into GREEN channel: */
    gl_FragColor.g = 0.5 * (v2 - v1);

    /* Remapped input intensity into BLUE channel: */
    incolor.rg = texture2DRect(Image, gl_TexCoord[0].st).ra;
    gl_FragColor.b = ((incolor.r + prescaleoffset) * scalefactor) + postscaleoffset;

    /* Just pass-through the alpha value, do not modify it: */
    gl_FragColor.a = incolor.g;

    /* Done. */
}