Image Expressions

In this section, we'll look how to use expressions to greatly expand what we can do in a loop. With expressions, we'll be able to write real image manipulation code, and in particular solve some puzzles based on images.

Expressions 1 + 1

We have seen function calls where we pass in a value within the parenthesis, such as the value 42 passed in to the print function below.


Instead of a plain number like 42, an "expression" written in the code like 11 + 13 computes the value to use. For example you could write something like this:

  print(11 + 31);

When that line runs, the computer first computes the expression 11 + 31, yielding 42. Then in effect it calls print(42), passing in the computed value. Anywhere in the code where we have used a fixed number like 0 or 255 of whatever, we can instead write an expression, letting the code compute a value when that line runs.

pixel.getRed() / pixel.getGreen() / pixel.getBlue()

We have not used them until now, but there are three pixel functions that retrieve the red, green or blue value out of a pixel -- these will be very handy to use in expressions.

Combine pixel.getRed() and pixel.setRed()

  // Doubles the pixel's red value
  old = pixel.getRed();
  pixel.setRed(old * 2);

The pixel.getRed() can be combined with pixel.setRed(number) to operate on a pixel. For example, the above code snippet doubles the pixel's red value. The first line retrieves the red value from the pixel and stores that value in an variable named old. Say in this case that the red value is 50. The second line computes old * 2 (100 in this case), and sets that value back into the pixel as its new red value. The net effect is to double the red value of the pixel from 50 to 100.

  pixel.setRed(pixel.getRed() * 2);

The code pixel.getRed() * 2 is an expression, which is whatever the old red value was multiplied by 2. This expression is evaluated first, resulting a number such as 240. Then in effect pixel.setRed(240); is called. The setRed() etc. functions automatically limit the value set to the range 0..255. If setRed() is called with a value a greater than 255, it just uses 255, and likewise if a value less than 0 is passed in, it just uses the value 0.

Loops With Expressions

Before we could only express ideas like "set the red value to 200". Now we can express what new value we want in terms of the old value, like "triple the red value", or "set the red value to 75% of what it was".

Image Expression Example 1

Solution code:

  // your code here
  pixel.setGreen(pixel.getGreen() * 0.75);

Image Expression Example 2

Solution code:

  // your code here
  pixel.setRed(pixel.getRed() * 0.75);
  pixel.setGreen(pixel.getGreen() * 0.75);
  pixel.setBlue(pixel.getBlue() * 0.75);
  // Then try multipliers of 0.5 and 0.25
  // The effect is to make the image darker (towards 0)

Image Expression Example 3

Solution code:

  // your code here
  pixel.setRed(pixel.getRed() * 1.3);
  pixel.setGreen(pixel.getGreen() * 0.75);
  // The effect is to change the yellow flowers to light orange

5-10-20 Image Puzzles

5-10-20 Banana Puzzle

Solution code:

  // your code here
  pixel.setRed(pixel.getRed() * 20);
  pixel.setGreen(pixel.getGreen() * 5);
  pixel.setBlue(pixel.getBlue() * 10);

In the solution image, you will see some "banding" of the yellow of the banana. This is because the red and green channels were scaled down to a range as small as just 0, 1, 2, .. 12. With so few values, the image only represent a few different shades of yellow, and those are the bands we see if we look carefully at the banana. We will talk about "banding" more when we talk about digital media formats.

> exercises