Image-4 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


image-expression-1

 

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

  print(42);

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 get the red, green or blue value out of a pixel. These will be very handy to use in expressions.

Here is an example of getRed() - get the red number out of a pixel, store it in a variable named "red".

  red = pixel.getRed();

Notice that calling, say, pixel.getRed() has the pair of parenthesis () after it . The syntax requires that calling a function includes the parenthesis, even if there's nothing inside the parenthesis. A good example of the inflexible nature of computers.

Set/Get Pattern: combine pixel.setRed() and pixel.getRed()

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 red. Say in this case that the red value is 50. The second line computes red * 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.

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 - Set/Get Patterns

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


image-expression-2

 

Solution code:

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

Image Expression Example 2 - You Try It


image-expression-3

 

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 (optional)


image-expression-4

 

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


image-expression-banana

 

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.