A Gallery of Ways Not to Resize Images

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

  1. (First off, here’s the original, full size image. Click for a larger view. Each attempt below was to make it 2/3 size.)
    Original image
  2. 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):
    Less-than vs Less-than-or-equal
  3. Use an unsigned char instead of an int to store your y coordinate index. (unsigned char‘s can only hold the numbers 0 to 255, whereas ints can hold +/- 2 billion):
    Use char instead of int
  4. Cast float to unsigned 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 an unsigned char, you get overflow and the output becomes “2”.)
    Amplifying Filter + direct cast to char