CS 145 Lecture 21 – Image Manipulation
Agenda
- what ?s
- working with images
- program this
Program This
Grayscale images are two-dimensional grids of pixels. Each pixel is represented as an int in [0, 255], with 0 being black and 255 being white.
Imagine a grayscale image that can be generated with an algorithm. Many patterns and shapes can be produced by algorithms. Write pseudocode to generate this image.
Code
Image.java
package lecture1020;
import java.util.Random;
public class Image {
public static void main(String[] args) {
int width = 128;
int height = 256;
System.out.println("P2");
System.out.println(width + " " + height);
System.out.println(255);
makePacman(width, height);
}
public static void makeStatic(int width, int height) {
Random g = new Random();
for (int r = 0; r < height; ++r) {
for (int c = 0; c < width; ++c) {
System.out.println(g.nextInt(256));
}
}
}
public static void makeStripes(int width, int height) {
for (int r = 0; r < height; ++r) {
for (int c = 0; c < width; ++c) {
if (c % 10 <= 3) {
System.out.println(0);
} else {
System.out.println(255);
}
}
}
}
public static void makeDiagonals(int width, int height) {
for (int r = 0; r < height; ++r) {
for (int c = 0; c < width; ++c) {
if (c % 10 <= 3 && r != 0 && c / r == 1) {
System.out.println(0);
} else {
System.out.println(255);
}
}
}
}
public static void makePacman(int width, int height) {
int rCenter = height / 2;
int cCenter = width / 2;
for (int r = 0; r < height; ++r) {
int diffR = r - rCenter;
for (int c = 0; c < width; ++c) {
int diffC = c - cCenter;
double distance = Math.hypot(diffR, diffC);
if (distance < 14) {
System.out.println(0);
} else {
System.out.println(255);
}
}
}
}
}
Haiku
on Halloween strategy:
I send Candybot
I send Candybot
for each street; for each avenue
ringBell; collect; thank