## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

239
Image Manipulation
11
The OpenGL computer graphics API is primarily intended for rendering 3D
synthetic scenes from geometric primitives, but some capabilities for manipu-
lating images were built into the system from the beginning. With the addition
of shader capabilities, OpenGL can now use texture access and manipulation
operations to carry out a number of new image functions. In this chapter, we
describe some of these functions. Our main tools will be the ability to get texels
directly from a texture and the ability to do arithmetic on texel values.
The general form of the GLIB le is as below, including a uniform slider
variable T, used in case you use a parameterized operation such as image
blending, and variables for the resolution of the image le. Each texture needs
to be assigned to a texture unit. Here we have set up the GLIB le for two
textures, because some of the later examples in this chapter operate on two
240
images. Of course there will be changes if we work on a single image (using
only one le,
sample.bmp), if we use a dierent image (replacing the name of
the image le), or if we include additional uniform variables to support other
computations.
##OpenGL GLIB
Ortho -1. 1. -1. 1.
Texture 5 sample1.bmp
Texture 6 sample2.bmp
Vertex sample.vert
Fragment sample.frag
Program Sample uT <0. 0. 5.> \
uImageUnit 5 uImage2Unit 6
This GLIB le puts the two texture images on
texture units 5 and 6. You can arbitrarily pick which
texture units to use, up to the total number sup-
The vertex shader is short and uses our familiar
conventions for variable names:
out vec2 vST;
void main( )
{
vST = aTexCoord0.st;
gl_Position = uModelViewProjectionMatrix * aVertex;
}
Throughout this chapter we will be looking at dierent image manipula-
tion functions that we can build into fragment shaders.
Basic Concepts
GLSL deals with images by treating them as textures and using texture access
and manipulation to set the color of each pixel in the color buer. This color
buer may then be displayed, leing you see the eect of your manipulation,
If you are using glman,
do not use texture units 2
and 3, because glman uses
those to hold its built-in
2D and 3D noise textures.
241
Single-Image Manipulation
or it may be saved as another texture or output as
a le.
In Figure 11.1, we see a texture image as
it might have been read from an image le. This
texture le may be treated as an image raster by
working with each texel individually. A built-in
GLSL function
textureSize( ) will tell you the
resolution of the texture, called ResS and ResT in
Figure 11.1.
There are two ways to access a single texel in
a texture. Since any OpenGL texture has texture
coordinates ranging from 0.0 to 1.0, the coordinates
of the center of the image are
vec2(0.5,0.5) and
you can increment texture coordinates by 1./ResS
or 1./ResT to move from one texel to another hori-
zontally or vertically, respectively. Alternately, if
you are working with GLSL 1.50 (OpenGL 3.2) or
higher, you can access any texel with the
texelFetch( ) function. We will use
these GLSL texture-access capabilities in the fragment shader to identify and
calculate colors for pixels in the color buer.
In order to be as general as possible, we will address and increment tex-
ture coordinates with real numbers rather than integers, in spite of the weak-
ness in this approach, since it can lead to some unintentional interpolations of
pixel values.
Single-Image Manipulation
In the next several sections, we work with an individual image and compute
the color of output pixels by using information contained in the image. This
is in contrast to some later sections in this chapter, when we use two dierent
images as textures loaded into dierent texture units in our computation.
Luminance
The luminance of a color is the overall brightness of the color, with no reference
to the color’s hue. Luminance is a more complex property than it might seem,
because our eyes respond to dierent primary colors dierently. Luminance
has been studied because of the need to give luminance cues to persons who
have decient color vision, as described in [15, Chapter 5], and because it was
Figure 11.1. A texture raster that could be
created from an image le.

## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

No credit card required