Files
sunny9898/task7/wk4_example_05.1/wk4_example_05.1.pde
louiscklaw 5637fbf94f update,
2025-02-01 02:07:58 +08:00

98 lines
2.7 KiB
Plaintext

PImage color_source; // Source image
PImage bw_source; // Source image, black and white copy
PImage sine_waved; // Store processed sine-waved image
PImage drawn; // Store the area brushed over
PImage destination; // Canvas
int r = 25;
float a=0; // Phase
float da=PI/30; // Phase increment: 180/30 = 6 degrees
float amp=8; // Amplitude for the sin wave
int w, h;
void setup() {
// Initialize sketch window size
size(515, 800);
// Load input images and convert the second one to grayscale using filter function
color_source = loadImage("lisa.jpg");
bw_source = loadImage("lisa.jpg");
bw_source.filter(GRAY);
// Create blank output images
sine_waved = createImage(color_source.width, color_source.height, RGB);
drawn = createImage(color_source.width, color_source.height, RGB);
destination = createImage(color_source.width, color_source.height, RGB);
// We are going to look at both image's pixels
// Prepare for working with each pixel individually by loading all image data into memory
color_source.loadPixels();
bw_source.loadPixels();
sine_waved.loadPixels();
drawn.loadPixels();
destination.loadPixels();
w=color_source.width;
h=color_source.height;
a = 0;
for (int x = 0; x < w; x++ ) {
for (int y = 0; y < h; y++ ) {
int loc = x + y*w;
int newloc = x + int(y+sin(a)*amp)*w;
newloc=constrain(newloc, 0, w*h-1); // Image 1D array: from index "0" to "w*h-1"
//if (newloc>=0 && newloc<=w*h-1)
sine_waved.pixels[newloc]=color_source.pixels[loc]; //Copy the pixel from "source" to "sine_waved"
}
a=a+da;
}
}
void draw() {
// Display the final result
image(destination, 0, 0);
// Draw red circle around current mouse cursor position
stroke(255, 0, 0);
noFill();
circle(mouseX, mouseY, r*2);
for (int x = 0; x < w; x++ ) {
for (int y = 0; y < h; y++ ) {
float d = dist(mouseX, mouseY, x, y);
if (d <= r) {
if (mousePressed && mouseButton == LEFT) {
drawn.pixels[y * width + x] = color(255);
}
}
// If this pixel was painted recently
if (drawn.pixels[y * width + x] == color(255)) {
// replace the background pixel with the corresponding pixel from the sine-waved image
destination.pixels[y * width + x] = sine_waved.pixels[y * width + x];
}else{
destination.pixels[y * width + x] = bw_source.pixels[y * width + x];
}
}
}
// Update the content of the drawings to reflect changes made to them
drawn.updatePixels();
destination.updatePixels();
}
void keyPressed() {
if (keyCode == UP) {
r += 10;
} else if (keyCode == DOWN) {
r -= 10;
}
if (key == ENTER) {
setup();
}
}