Session 4

Pictures as Java Objects

Computer Science I: Media Computation

Opening Exercise

a turtle draws a house

Write the sequence of instructions for a particular Turtle to draw a house:

    World  w = new World();
    Turtle t = new Turtle(w);
    // ... fill in the blank

If that seems easy enough, you might:

We have now defined and named some simple processes.

Where Were We?

Last time we talked about the three kinds of things every programming language has: primitive values, ways to combine values into larger units, and ways to abstract details away. The primary mechanism we saw for abstraction was the ability to give something a name. The Turtle that you just played with was both a combination and an abstraction.

A picture consists of a combination of other objects: a two-dimensional grid of pixels. A pixel itself consists of three integers, one each for the red, green, and blue values in the element. Both are combinations of other data, which encode an idea in a way that people and program can manipulate. But each is also an abstraction, because it allows us to talk about a higher-level idea as a single unit, irrespective of the underlying encoding. The objects we use and create in Java are usually both combinations and abstractions.

Today, we will dive head-long into manipulating media objects.

Objects in Java

... stored in a Java variable -- a name for use in the program

... variable is initially null -- no object

... constructor to create an object

... notion of a binding between a variable and an object -- one at a time, "orphan" object

... interact by "sending messages", using the '.' notation

Names Redux

We use lots of different kinds of names:

And then there are file names, which reside at the level of your operating system and which, inside our programs, take the form of String objects...

Classes in Java

... a class defines a kind of object, an encoding in terms of other values

... a method define an operation on the object

... (a constructor is a special kind of method)

... when we send a message to an object, it uses the corresponding method to respond to our request

Quick Exercise

Write a method for a Picture that converts it to a black-and-white negative.

Wheaties Eugene as black-and-white negative

Reusing existing methods is often the simplest way to implement an operation:

    public void blackAndWhiteNegative()

Of course, this is not as efficient as we might be... We process all of the pixels in the image twice, whereas we could make a single pass through the pixels. (If you have some programming experience, you might give this a try at home!)

Features of Classes and Objects

... messages sent to objects -- most of what we do

... messages sent to classes -- FileChooser.pickAFile()

... another example, a class variable: Integer.MAX_VALUE

... class variables and method are coded as static in Java

More on Picture Encodings

How we encode pictures in a computer draws on how humans perceive light and color. Humans are able to perceive light that has wavelengths between 370 nanometers (nm) and 730 nm. These wavelengths are continuous values, that is, we do not perceive different values as discrete values. But we do perceive light with three different color sensors that focus at lengths of 425 nm (blue), 550 nm (green), and 560 nm (red).

The way that we distinguish different objects in the world is by their boundaries, motion, and depth, and we perceive these features by their apparent luminance -- not the amount of light present, but our perception of the amount of light. And most of how we perceive luminance is by comparison to backgrounds, not by their raw values.

When we represent a picture in a computer, we digitize it, that is, by representing them as a set of many, many tiny dots. (Computers -- at least all the ones you and I use -- are discrete, not continuous.) Yet the image looks continuous to the human eye because the human eye is limited:

Each dot -- pixel -- encodes a color for someone (something) to perceive. So a big part of how we encode pictures is how we encode colors.

Citation: The images I used in class are courtesy of some ColorSync documentation at Apple's developer website.

Some encodings of color match well how humans perceive colors, and others match better what computers do. Two human-centered ways are HSV and HLS:

It is much simpler for computers to represent colors using RGB, where each color is a combination of red, green, and blue light. We can think of an RGB color as a point in three-dimensional space. One neat result of this representation is that we have a natural way of determining how "close" two colors are to one another: the distance between two points!

Each component is represented by an 8-bit value, which means that each color value has 28 = 256 possible values, from 0 to 255. With three 8-bit values, we can thus represent 2563 = 224 = 16,777,216 colors. This is just about right, as human eyes can distinguish millions of colors. (Unfortunately, not all computer monitors can project that many different colors well...)

Next time, we move forward into to using this knowledge to manipulate images within our Java programs.

Wrap Up

To re-create some of what we did today, use this annotated script of my Interactions pane, and the resulting versions of and If it helps, you can follow along with my slides (PDF).

For next time, read Chapter 4.

Next session, I will assign Homework 1.

Eugene Wallingford ..... ..... August 31, 2006