-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPictureSection.pde
More file actions
163 lines (134 loc) · 3.48 KB
/
Copy pathPictureSection.pde
File metadata and controls
163 lines (134 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/****************************************************
* PictureSection class
* By: Chris Jimenez
*
*****************************************************/
class PictureSection {
int xPos;
int yPos;
float xMove;
float yMove;
int psWidth = 50;
int psHeight = 50;
int filterCount;
boolean invert = false;
PImage primaryImage;
PImage sectionImage;
/**
* CONSTRUCTOR
*/
PictureSection(PImage i) {
primaryImage = i;//initialize primary image
xPos = (int)random(30, width-psWidth);
yPos = (int)random(30, height-psHeight);
xMove = random(1, 3);
yMove = random(1, 3);
sectionImage = new PImage(psWidth, psHeight);
// load pixels for primary image and section image
// for manipulation later...
primaryImage.loadPixels();
sectionImage.loadPixels();
filterCount = (int)random(0, 5.4);
}
/**
* Displays the picture section/filter
*/
void display(PImage i) {
primaryImage = i;
sectionImage = new PImage(psWidth, psHeight);
updatePicSection();
updatePosition();
isPressed();//if pressed invert
if (keyPressed && key == ' ') {
invert = false;
}
if (invert) {
displayFilter(primaryImage);
image(primaryImage, 0, 0);
}
stroke(200);
rect(xPos, yPos, psWidth, psHeight);//border around seciton image
displayFilter(sectionImage);
image(sectionImage, xPos, yPos);
}
/**
* Updates the picture section
*/
void updatePosition() {
xPos += xMove;
yPos += yMove;
xPos = constrain(xPos, 0, width - psWidth);
yPos = constrain(yPos, 0, height - psHeight);
// do we need to bounce the rect?
if (xPos >= width - psWidth || xPos <= 0) {
xMove = xMove * -1;
if (!invert) filterCount++;
}
if (yPos >= height-psHeight || yPos <= 0) {
yMove = yMove * -1;
if (!invert) filterCount++;
}
}
/**
* using algorithm to capture section of image...
*/
void updatePicSection() {
// initilize location of x,y position of section
int location = xPos + yPos*primaryImage.width;
for (int i = 0; i < psWidth; i++) {//for each row of section
for (int j = 0; j < psHeight; j++) {//for each pixel at row
sectionImage.pixels[i+ j*psWidth] = primaryImage.pixels[location + i +j*width];
}
}
}
/**
* Applies filter to the section
*/
void displayFilter(PImage i) {
if (filterCount >5) filterCount = 0;
if (filterCount == 0) i.filter(THRESHOLD);
if (filterCount == 1) i.filter(GRAY);
if (filterCount == 2) i.filter(POSTERIZE, 2);
if (filterCount == 3) i.filter(BLUR);
if (filterCount == 4) i.filter(ERODE);
if (filterCount == 5) i.filter(DILATE);
}
/**
* determiness if picture section is pressed
* if so, then the whole pic will have that filter..
*/
void isPressed() {
if (mousePressed) {
if (((mouseX >= xPos) && (mouseX <= xPos+psWidth)) &&
((mouseY >= yPos) && (mouseY <=yPos+psHeight))) {
invert = true;
}
}
}
/**
* increase and decrease width and height of each section
*/
void increaseWidthAndHeight() {
psWidth++;
psHeight++;
}
void decreaseWidthAndHeight() {
psWidth--;
psHeight--;
}
/**
* increase and decrease speed of each section
*/
void increaseSpeed() {
if (xMove < 4 && yMove < 4) {//set speed limit
xMove += 0.2;
yMove += 0.2;
}
}
void decreaseSpeed() {
if (xMove > 1 && yMove > 1) {
xMove -= 0.2;
yMove -= 0.2;
}
}
}