CS 051 Fall 2012

Lecture 27

Image Processing

A number of programs that deal with images and sounds depend on
putting important information in an array and then building a new
array by making changes. A good example of this is a program that can
modify digital music. It is fairly easy to change the tempo or even
the octave of a tune by holding it in a one-dimensional array and then
copying while modifying the sounds.

In class we finished our example program that helped the
user process images. Operations available were transforming to
grayscale, flipping the image either horizontally or vertically,
blurring the image, or rotating it. One unusual feature of the
program is that we decided to have the first coordinate represent the
column rather than the row, so keep that in mind while reading it.

More Scribbles

Recall that we talked about two implementations of scribbles - one as
recursive structures and one represented as arrays. We can do the
same thing with collections of scribbles. Here a ScribbleCollection
is represented as an array of Scribbles, each of which is represented as an array.

Ragged Two-Dimensional Arrays

As mentioned earlier, we can create two-dimensional arrays where each
row has a different length. Calendars (see details in book) provide
a good example. Each row of the array will represent a month. We
can declare it as:

    private String[][] dailyEvent;

We can create 12 months by creating just slots for each month:

   dailyEvent = new String[12][];

Because we have not filled in the other dimension (size of the rows),
we will have to create each month separately. If somewhere else we
wrote a method getDays(int n) that returns the number of days in
month n (where 1 represents January), then we can create all of the
months as follows:

    for (int month = 0; month < 12; month++)
       dailyEvent[month] = new String[getDays(month+1)];
    }

Why do we need to write month+1 in the call to getDays?

Multi-Dimensional Arrays - The Final Word

We are not limited to this, of course. If we can create an array of any type,
and that includes creating an array of arrays, then we should be able to
create and array of arrays of arrays. In fact, we do this in the obvious way:

    private int [][][] a = new int[2][3][4];
Here, the expression a.length evaluates to 2, a[0].length
evaluates to 3, and a[0][0].length evaluates to 4.

This approach can be expanded to as many dimensions as we need, assuming
the computer can hold the data.