### CS51 - Fall 2009 - Lecture 28

• TicTacToe one last time
- Given
LEFT // x value of the left of the board
TOP // y value of the top of the board
CELL_SIZE // the size of the an individual cell in the board
that the user clicked at x, y, which we know is in the board
- How do we figure out what cell/board space the user clicked in?
x = x - LEFT;
y = y - TOP;
int col = (int)(x/CELL_SIZE);
int row = (int)(y/CELL_SIZE);

• Lab attendance!

• How is an image represented/stored in a computer?
- a common approach is a matrix of Colors (often represented by RGB values)
- How would we do this?
- Color[][] image

• show ImageEdit demo
- we have two window areas
- the one on the left is our image
- the one on the right is our modified image
- two variables
- Color[][] source
- Color[][] target
- other private variables
- private int height; // number of rows
- private int width; // number of cols
- grayscale
- modifies one pixel at a time
- What makes a color gray?
- gray colors have identical RGB values
- a measure of brightness
- horizontal reversal
- to do this we'll have two for loops and fill in "target"
for( int row = 0; row < height; row++ ){
for( int col = 0; col < width; col++ ){
- let's look at a diagram - where does pixel row, col come from?
- row stays the same
- col is the distance from the col to the end of the image
- target[row][col] = source[row][width-col-1]
- vertical inversion
- again, where does pixel row, col come from?
- column stays the same
- row is the distance from the row to the end of the image
- target[row][col] = source[height - row - 1][col]
- Is there a more efficient way of doing this?
for( int row = 0; row < height; row++ ){
target[row] = source[height-row-1]
- rectangular averaging
- target is the average of the pixels in the source in a rectangular range
- private int range; // defines how far from the pixel we look in any direction
- let's start by not worrying about the boundary cases
for( int row = 0; row < height; row++ ){
for( int col = 0; col < width; col++ ){
int redSum = 0;
int greenSum = 0;
int blueSum = 0;
int count = 0;

for( int i = row-range; i < row+range+1; i++ ){
for( int j = col-range; j < col+range+1; j++ ){
redSum += source[i][j].getRed();
greenSum += source[i][j].getGreen();
blueSum += source[i][j].getBlue();
count++;
}
}

// average the values
target[row][col] = new Color(redSum/count, greenSum/count, blueSum/count);
}
}
- What do we need to add to account for the boundaries?
- Some useful functions
- Math.min
- Math.max
for( int i = Math.max(O,row-range); i < Math.min(width, row+range+1); i++ ){
for( int j = Math.max(0,col-range); j < Math.min(hieght, col+range+1); j++ ){
• extensible arrays
- how would you do it?
- java.util.ArrayList
- get(index) // returns value at index
- set(index, value) // sets value at index to be value