So for CS 559: Computer Graphics, our first project is to implement a program that can do various things to images. For instance, we need to turn color images to gray scale (or part way there), resize them, rotate them, etc.
As it turns out, the mathematics behind some of these things that we take for granted in Photoshop or the GIMP are actually fairly complex, though I don’t want to get into them now. What I do want to show you are several ways that one (say, me) could try to implement a simple “resize” operation and fail, due to tiny little bugs:
How to Screw Up the “Resize” Function, in Pictures
-
(First off, here’s the original, full size image. Click for a larger view. Each attempt below was to make it 2/3 size.)
- Use
<
where you meant<=
to define your resampling filter. (Causes the filter not to include certain values that are exactly on the edge, like, say, all the exact integer multiples):
- Use an
unsigned char
instead of anint
to store your y coordinate index. (unsigned char
‘s can only hold the numbers 0 to 255, whereasint
s can hold +/- 2 billion):
- Cast
float
tounsigned char
after using a filter not guaranteed not to amplify values. This one is pretty subtle, but you can see red & yellow splotches appear on the shirt (e.g., dots on my left shoulder), and my right ear has holes in it. Very annoying. (If the filter amplifies values, then it can output, say “257” from inputs ranging 0-255. When you try to fit “257” in anunsigned char
, you get overflow and the output becomes “2”.)