Graphics Reference
In-Depth Information
Image Flipping, Rotation, and Warping
In the previous examples of single-image manipulation, we worked with each
pixel in place. However, we can also compute the color for a pixel by manipu-
lating the coordinates of the pixel to get that pixel's color from another place
in the image.
While we create image warps to achieve particular effects on specific
images, it can be useful to have some kind of uniform benchmark image for
warps. There are many such benchmark images, depending on just what
effects you want to see, but in Figure 11.15 we see a simple rect-
angular grid that we will use to examine the details of changes in
the images.
We will work with images by treating them as texture maps,
as we have throughout this chapter so far. When we compute the
source address for a pixel, we are applying a function from the
pixel address space to itself. Since the texture space is the unit
square, [0, 1] × [0, 1] we are looking for functions that map that
space to itself. Sometimes, however, we may want to double the
size of that space and shift it so that the space we are working with
is [−1, 1] × [−1, 1] to make it convenient to apply familiar functions
(such as trigonometric functions) to the space. Examples and exer-
cises will help clarify what we mean by this.
One of the simplest kinds of address-based image manipulation is image
flipping, . There are two kinds of flipping: horizontal and vertical. In vertical
image lipping, you exchange the top pixels in the image with the botom
pixels, effectively mirroring the image around a horizontal line. In horizontal
image flipping, you exchange the left and right pixels in the image, effectively
mirroring it around a vertical line.
You can flip an image by a very simple calculation on the texture coor-
dinates. Since the texture coordinates are in the interval [0, 1], the function
t = 1 − t will reverse the order of the coordinate t in this interval. If this is
applied to the texture coordinates in the fragment shader (with the common
glman setup) as
vec2 st = vST;
st.t = 1. - st.t;
vec3 irgb = texture( uImageUnit, st ).rgb;
fFragColor = vec4( irgb, 1. );
Figure 11.15. The rectan-
gular grid image.
then the resulting image will be displayed “upside down” or flipped verti-
cally. It is quite easy to see how a horizontal flip could be implemented by
manipulating the s texture coordinate.
Search WWH ::




Custom Search