Skip to content

Commit d946534

Browse files
authored
Merge pull request #1359 from Acha0203/fix/indentation-of-source-code-for-example-05-image
Change indentation and format of source code for some examples/en/05_Image/.js files
2 parents 5596ead + 3ebd9cd commit d946534

File tree

5 files changed

+103
-98
lines changed

5 files changed

+103
-98
lines changed

src/data/examples/en/05_Image/06_Blur.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,21 @@
1010
// v is the normalized value
1111
let v = 1.0 / 9.0;
1212
// kernel is the 3x3 matrix of normalized values
13-
let kernel = [[ v, v, v ], [ v, v, v ], [ v, v, v ]];
13+
let kernel = [
14+
[v, v, v],
15+
[v, v, v],
16+
[v, v, v]
17+
];
1418

1519
// preload() runs once, before setup()
1620
// loadImage() needs to occur here instead of setup()
17-
// if loadImage() is called in setup(), the image won't appear
21+
// if loadImage() is called in setup(), the image won't appear
1822
// since noLoop() restricts draw() to execute only once
1923
// (one execution of draw() is not enough time for the image to load),
2024
// preload() makes sure image is loaded before anything else occurs
2125
function preload() {
2226
// load the original image
23-
img = loadImage("assets/rover.png");
27+
img = loadImage('assets/rover.png');
2428
}
2529

2630
// setup() runs once after preload
@@ -31,7 +35,6 @@ function setup() {
3135
noLoop();
3236
}
3337

34-
3538
// draw() runs after setup(), normally on a loop
3639
// in this case it runs only once, because of noDraw()
3740
function draw() {
@@ -43,7 +46,7 @@ function draw() {
4346

4447
// load its pixels
4548
edgeImg.loadPixels();
46-
49+
4750
// two for() loops, to iterate in x axis and y axis
4851
// since the kernel assumes that the pixel
4952
// has pixels above, under, left, and right
@@ -53,18 +56,18 @@ function draw() {
5356
for (let x = 1; x < img.width; x++) {
5457
for (let y = 1; y < img.height; y++) {
5558
// kernel sum for the current pixel starts as 0
56-
let sum = 0;
59+
let sum = 0;
5760

5861
// kx, ky variables for iterating over the kernel
5962
// kx, ky have three different values: -1, 0, 1
6063
for (kx = -1; kx <= 1; kx++) {
6164
for (ky = -1; ky <= 1; ky++) {
6265
let xpos = x + kx;
6366
let ypos = y + ky;
64-
65-
// since our image is grayscale,
67+
68+
// since our image is grayscale,
6669
// RGB values are identical
67-
// we retrieve the red value for this example
70+
// we retrieve the red value for this example
6871
// (green and blue work as well)
6972
let val = red(img.get(xpos, ypos));
7073

@@ -74,7 +77,7 @@ function draw() {
7477
// if we add 1 to kx and ky, we get 0, 1, 2
7578
// with that we can use it to iterate over kernel
7679
// and calculate the accumulated sum
77-
sum += kernel[kx+1][ky+1] * val;
80+
sum += kernel[kx + 1][ky + 1] * val;
7881
}
7982
}
8083

@@ -84,7 +87,7 @@ function draw() {
8487
}
8588
// updatePixels() to write the changes on edgeImg
8689
edgeImg.updatePixels();
87-
90+
8891
// draw edgeImg at the right of the original image
8992
image(edgeImg, img.width, 0);
90-
}
93+
}
Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* @name Edge Detection
33
* @arialabel Astronaut rendered in black and white on the left and a highly sharpened version of the image on the right
4-
* @description A high-pass filter sharpens an image. This program analyzes every pixel in an image in relation to the neighboring pixels to sharpen the image.
4+
* @description A high-pass filter sharpens an image. This program analyzes every pixel in an image in relation to the neighboring pixels to sharpen the image.
55
* <br><br><span class="small"><em>This example is ported from the <a href="https://processing.org/examples/edgedetection.html">Edge Detection example</a>
66
* on the Processing website</em></span>
77
*/
@@ -12,17 +12,21 @@
1212
// to consider all neighboring pixels we use a 3x3 array
1313
// and normalize these values
1414
// kernel is the 3x3 matrix of normalized values
15-
let kernel = [[-1, -1, -1 ], [ -1, 9, -1 ], [-1, -1, -1 ]];
15+
let kernel = [
16+
[-1, -1, -1],
17+
[-1, 9, -1],
18+
[-1, -1, -1]
19+
];
1620

1721
// preload() runs once, before setup()
1822
// loadImage() needs to occur here instead of setup()
19-
// if loadImage() is called in setup(), the image won't appear
23+
// if loadImage() is called in setup(), the image won't appear
2024
// since noLoop() restricts draw() to execute only once
2125
// (one execution of draw() is not enough time for the image to load),
2226
// preload() makes sure image is loaded before anything else occurs
2327
function preload() {
2428
// load the original image
25-
img = loadImage("assets/rover.png");
29+
img = loadImage('assets/rover.png');
2630
}
2731

2832
// setup() runs after preload, once()
@@ -36,17 +40,15 @@ function setup() {
3640
// draw() runs after setup(), normally on a loop
3741
// in this case it runs only once, because of noDraw()
3842
function draw() {
39-
4043
// place the original image on the upper left corner
4144
image(img, 0, 0);
4245

4346
// create a new image, same dimensions as img
4447
edgeImg = createImage(img.width, img.height);
45-
48+
4649
// load its pixels
4750
edgeImg.loadPixels();
4851

49-
5052
// two for() loops, to iterate in x axis and y axis
5153
// since the kernel assumes that the pixel
5254
// has pixels above, under, left, and right
@@ -57,17 +59,16 @@ function draw() {
5759
for (let x = 1; x < img.width - 1; x++) {
5860
for (let y = 1; y < img.height - 1; y++) {
5961
// kernel sum for the current pixel starts as 0
60-
let sum = 0;
61-
62+
let sum = 0;
63+
6264
// kx, ky variables for iterating over the kernel
6365
// kx, ky have three different values: -1, 0, 1
6466
for (kx = -1; kx <= 1; kx++) {
6567
for (ky = -1; ky <= 1; ky++) {
66-
6768
let xpos = x + kx;
6869
let ypos = y + ky;
69-
let pos = (y + ky)*img.width + (x + kx);
70-
// since our image is grayscale,
70+
let pos = (y + ky) * img.width + (x + kx);
71+
// since our image is grayscale,
7172
// RGB values are identical
7273
// we retrieve the red value for this example
7374
let val = red(img.get(xpos, ypos));
@@ -77,18 +78,18 @@ function draw() {
7778
// if we add 1 to kx and ky, we get 0, 1, 2
7879
// with that we can use it to iterate over kernel
7980
// and calculate the accumulated sum
80-
sum += kernel[ky+1][kx+1] * val;
81+
sum += kernel[ky + 1][kx + 1] * val;
8182
}
8283
}
83-
84-
// set the pixel value of the edgeImg
84+
85+
// set the pixel value of the edgeImg
8586
edgeImg.set(x, y, color(sum, sum, sum));
8687
}
8788
}
88-
89+
8990
// updatePixels() to write the changes on edgeImg
9091
edgeImg.updatePixels();
91-
92+
9293
// draw edgeImg at the right of the original image
9394
image(edgeImg, img.width, 0);
94-
}
95+
}

src/data/examples/en/05_Image/08_Brightness.js

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
* <br><br><span class="small"><em>This example is ported from the <a href="https://processing.org/examples/brightness.html">Brightness example</a>
66
* on the Processing website</em></span>
77
*/
8-
// This program adjusts the brightness
9-
// of a part of the image by
10-
// calculating the distance of
8+
// This program adjusts the brightness
9+
// of a part of the image by
10+
// calculating the distance of
1111
// each pixel to the mouse.
1212
let img;
1313
// preload() runs once, before setup()
1414
// loadImage() needs to occur here instead of setup()
1515
// preload() makes sure image is loaded before anything else occurs
1616
function preload() {
1717
// load the original image
18-
img = loadImage("assets/rover_wide.jpg");
18+
img = loadImage('assets/rover_wide.jpg');
1919
}
2020
// setup() runs after preload, once()
2121
function setup() {
@@ -25,40 +25,40 @@ function setup() {
2525
}
2626

2727
function draw() {
28-
image(img,0,0);
29-
// Only need to load the pixels[] array once, because we're only
30-
// manipulating pixels[] inside draw(), not drawing shapes.
31-
loadPixels();
32-
// We must also call loadPixels() on the PImage since we are going to read its pixels.
33-
img.loadPixels();
34-
for (let x = 0; x < img.width; x++) {
35-
for (let y = 0; y < img.height; y++ ) {
36-
// Calculate the 1D location from a 2D grid
37-
let loc = (x + y*img.width)*4;
38-
// Get the R,G,B values from image
39-
let r,g,b;
40-
r = img.pixels[loc];
41-
// g = img.pixels[loc+1];
42-
// b = img.pixels[loc+2];
43-
// Calculate an amount to change brightness based on proximity to the mouse
44-
// The closer the pixel is to the mouse, the lower the value of "distance"
45-
let maxdist = 50;//dist(0,0,width,height);
46-
let d = dist(x, y, mouseX, mouseY);
47-
let adjustbrightness = 255*(maxdist-d)/maxdist;
48-
r += adjustbrightness;
49-
// g += adjustbrightness;
50-
// b += adjustbrightness;
51-
// Constrain RGB to make sure they are within 0-255 color range
52-
r = constrain(r, 0, 255);
53-
// g = constrain(g, 0, 255);
54-
// b = constrain(b, 0, 255);
55-
// Make a new color and set pixel in the window
56-
let pixloc = (y*width + x)*4;
57-
pixels[pixloc] = r;
58-
pixels[pixloc+1] = r;
59-
pixels[pixloc+2] = r;
60-
pixels[pixloc+3] = 255; // Always have to set alpha
61-
}
28+
image(img, 0, 0);
29+
// Only need to load the pixels[] array once, because we're only
30+
// manipulating pixels[] inside draw(), not drawing shapes.
31+
loadPixels();
32+
// We must also call loadPixels() on the PImage since we are going to read its pixels.
33+
img.loadPixels();
34+
for (let x = 0; x < img.width; x++) {
35+
for (let y = 0; y < img.height; y++) {
36+
// Calculate the 1D location from a 2D grid
37+
let loc = (x + y * img.width) * 4;
38+
// Get the R,G,B values from image
39+
let r, g, b;
40+
r = img.pixels[loc];
41+
// g = img.pixels[loc+1];
42+
// b = img.pixels[loc+2];
43+
// Calculate an amount to change brightness based on proximity to the mouse
44+
// The closer the pixel is to the mouse, the lower the value of "distance"
45+
let maxdist = 50; //dist(0,0,width,height);
46+
let d = dist(x, y, mouseX, mouseY);
47+
let adjustbrightness = (255 * (maxdist - d)) / maxdist;
48+
r += adjustbrightness;
49+
// g += adjustbrightness;
50+
// b += adjustbrightness;
51+
// Constrain RGB to make sure they are within 0-255 color range
52+
r = constrain(r, 0, 255);
53+
// g = constrain(g, 0, 255);
54+
// b = constrain(b, 0, 255);
55+
// Make a new color and set pixel in the window
56+
let pixloc = (y * width + x) * 4;
57+
pixels[pixloc] = r;
58+
pixels[pixloc + 1] = r;
59+
pixels[pixloc + 2] = r;
60+
pixels[pixloc + 3] = 255; // Always have to set alpha
6261
}
63-
updatePixels();
64-
}
62+
}
63+
updatePixels();
64+
}

src/data/examples/en/05_Image/09_Convolution.js

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@
66
* image file, and a running <a href="https://github.com/processing/p5.js/wiki/Local-server">
77
* local server</a>.</span></em></p>
88
*/
9-
9+
1010
let img;
1111
let w = 80;
1212

13-
// It's possible to convolve the image with many different
14-
// matrices to produce different effects. This is a high-pass
15-
// filter; it accentuates the edges.
16-
const matrix = [ [ -1, -1, -1 ],
17-
[ -1, 9, -1 ],
18-
[ -1, -1, -1 ] ];
13+
// It's possible to convolve the image with many different
14+
// matrices to produce different effects. This is a high-pass
15+
// filter; it accentuates the edges.
16+
const matrix = [
17+
[-1, -1, -1],
18+
[-1, 9, -1],
19+
[-1, -1, -1]
20+
];
1921

2022
function preload() {
2123
img = loadImage('assets/moonwalk.jpg');
@@ -36,20 +38,20 @@ function draw() {
3638
background(img);
3739

3840
// Calculate the small rectangle we will process
39-
const xstart = constrain(mouseX - w/2, 0, img.width);
40-
const ystart = constrain(mouseY - w/2, 0, img.height);
41-
const xend = constrain(mouseX + w/2, 0, img.width);
42-
const yend = constrain(mouseY + w/2, 0, img.height);
41+
const xstart = constrain(mouseX - w / 2, 0, img.width);
42+
const ystart = constrain(mouseY - w / 2, 0, img.height);
43+
const xend = constrain(mouseX + w / 2, 0, img.width);
44+
const yend = constrain(mouseY + w / 2, 0, img.height);
4345
const matrixsize = 3;
4446

4547
loadPixels();
4648
// Begin our loop for every pixel in the smaller image
4749
for (let x = xstart; x < xend; x++) {
48-
for (let y = ystart; y < yend; y++ ) {
50+
for (let y = ystart; y < yend; y++) {
4951
let c = convolution(x, y, matrix, matrixsize, img);
50-
52+
5153
// retrieve the RGBA values from c and update pixels()
52-
let loc = (x + y*img.width) * 4;
54+
let loc = (x + y * img.width) * 4;
5355
pixels[loc] = red(c);
5456
pixels[loc + 1] = green(c);
5557
pixels[loc + 2] = blue(c);
@@ -64,29 +66,28 @@ function convolution(x, y, matrix, matrixsize, img) {
6466
let gtotal = 0.0;
6567
let btotal = 0.0;
6668
const offset = Math.floor(matrixsize / 2);
67-
for (let i = 0; i < matrixsize; i++){
68-
for (let j = 0; j < matrixsize; j++){
69-
69+
for (let i = 0; i < matrixsize; i++) {
70+
for (let j = 0; j < matrixsize; j++) {
7071
// What pixel are we testing
71-
const xloc = (x + i - offset);
72-
const yloc = (y + j - offset);
72+
const xloc = x + i - offset;
73+
const yloc = y + j - offset;
7374
let loc = (xloc + img.width * yloc) * 4;
7475

7576
// Make sure we haven't walked off our image, we could do better here
76-
loc = constrain(loc, 0 , img.pixels.length - 1);
77+
loc = constrain(loc, 0, img.pixels.length - 1);
7778

7879
// Calculate the convolution
7980
// retrieve RGB values
80-
rtotal += (img.pixels[loc]) * matrix[i][j];
81-
gtotal += (img.pixels[loc + 1]) * matrix[i][j];
82-
btotal += (img.pixels[loc + 2]) * matrix[i][j];
81+
rtotal += img.pixels[loc] * matrix[i][j];
82+
gtotal += img.pixels[loc + 1] * matrix[i][j];
83+
btotal += img.pixels[loc + 2] * matrix[i][j];
8384
}
8485
}
8586
// Make sure RGB is within range
8687
rtotal = constrain(rtotal, 0, 255);
8788
gtotal = constrain(gtotal, 0, 255);
8889
btotal = constrain(btotal, 0, 255);
89-
90+
9091
// Return the resulting color
9192
return color(rtotal, gtotal, btotal);
92-
}
93+
}

src/data/examples/en/05_Image/10_Copy_Method.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
*/
77
let draft, ready;
88
function preload() {
9-
ready = loadImage("assets/parrot-color.png");
10-
draft = loadImage("assets/parrot-bw.png");
9+
ready = loadImage('assets/parrot-color.png');
10+
draft = loadImage('assets/parrot-bw.png');
1111
}
1212
function setup() {
1313
createCanvas(600, 400);
1414
noCursor();
15-
cursor("assets/brush.png", 20, -10);
15+
cursor('assets/brush.png', 20, -10);
1616
image(ready, 0, 0);
1717
image(draft, 0, 0);
1818
}

0 commit comments

Comments
 (0)