-
Notifications
You must be signed in to change notification settings - Fork 0
/
PFM_original.pde
202 lines (174 loc) · 8.23 KB
/
PFM_original.pde
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
///////////////////////////////////////////////////////////////////////////////////////////////////////
// This path finding module is the basis for nearly all drawings.
// Find the darkest average line away from my current location and move there.
///////////////////////////////////////////////////////////////////////////////////////////////////////
class PFM_original implements pfm {
final int squiggle_length = 500; // How often to lift the pen
final int adjustbrightness = 10; // How fast it moves from dark to light, over-draw
final float desired_brightness = 250; // How long to process. You can always stop early with "s" key
final int squiggles_till_first_change = 190;
int tests = 50; // Reasonable values: 13 for development, 720 for final
int line_length = int(random(3, 40)); // Reasonable values: 3 through 100
int squiggle_count;
int darkest_x;
int darkest_y;
float darkest_value;
float darkest_neighbor = 256;
/////////////////////////////////////////////////////////////////////////////////////////////////////
public void pre_processing() {
image_crop();
image_scale(int(image_size_x / pen_width));
//image_sharpen(img);
//image_blurr(img);
//image_unsharpen(img, 5);
//image_unsharpen(img, 4);
//image_unsharpen(img, 3);
//image_unsharpen(img, 2);
//image_unsharpen(img, 1);
//image_motion_blur(img);
//image_outline(img);
//image_edge_detect(img);
//image_sobel(img, 1.0, 0);
//image_posterize(6);
//image_erode();
//image_dilate();
//image_invert();
//image_blur(2);
//image_border("b1.png", 0, 0);
//image_border("b11.png", 0, 0);
//image_desaturate();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
public void find_path() {
find_squiggle();
if (avg_imgage_brightness() > desired_brightness) {
state++;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
private void find_squiggle() {
int x, y;
find_darkest();
//find_darkest_area();
x = darkest_x;
y = darkest_y;
squiggle_count++;
pen_color = 0;
find_darkest_neighbor(x, y);
move_abs(0, darkest_x, darkest_y);
pen_down();
for (int s = 0; s < squiggle_length; s++) {
find_darkest_neighbor(x, y);
bresenham_lighten(x, y, darkest_x, darkest_y, adjustbrightness);
move_abs(0, darkest_x, darkest_y);
x = darkest_x;
y = darkest_y;
}
pen_up();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
private void find_darkest() {
darkest_value = 257;
int darkest_loc = 0;
for (int loc = 0; loc < img.width * img.height; loc++) {
float r = brightness(img.pixels[loc]);
if (r < darkest_value) {
darkest_value = r + random(1);
darkest_loc = loc;
}
}
darkest_x = darkest_loc % img.width;
darkest_y = (darkest_loc - darkest_x) / img.width;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
private void find_darkest_area() {
// Warning, Experimental:
// Finds the darkest square area by down sampling the img into a much smaller area then finding
// the darkest pixel within that. It returns a random pixel within that darkest area.
int area_size = 10;
darkest_value = 999;
int darkest_loc = 1;
PImage img2;
img2= createImage(img.width / area_size, img.height / area_size, RGB);
img2.copy(img, 0, 0, img.width, img.height, 0, 0, img2.width, img2.height);
for (int loc = 0; loc < img2.width * img2.height; loc++) {
float r = brightness(img2.pixels[loc]);
if (r < darkest_value) {
darkest_value = r + random(1);
darkest_loc = loc;
}
}
darkest_x = darkest_loc % img2.width;
darkest_y = (darkest_loc - darkest_x) / img2.width;
darkest_x = darkest_x * area_size + int(random(area_size));
darkest_y = darkest_y * area_size + int(random(area_size));
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
private void find_darkest_neighbor(int start_x, int start_y) {
darkest_neighbor = 257;
float delta_angle;
float start_angle;
//start_angle = random(-35, -15) + cos(radians(start_x/4+(start_y/6)))*30;
//start_angle = random(-95, -75) + cos(radians(start_y/15))*90;
//start_angle = 36 + degrees( ( sin(radians(start_x/9+46)) + cos(radians(start_y/26+26)) ));
//start_angle = 34 + degrees( ( sin(radians(start_x/9+46)) + cos(radians(start_y/-7+26)) ));
//if(squiggle_count <220) { tests = 20; } else { tests = 2; }
//start_angle = random(20, 1); // Cuba 1
//start_angle = random(-72, -52); // Spitfire
//start_angle = random(-120, -140); // skier
start_angle = random( - 360, -1); // gradiant magic
//start_angle = squiggle_count % 360;
//start_angle += squiggle_count/4;
//start_angle = -45;
//start_angle = (squiggle_count * 37) % 360;
//delta_angle = 180 + 10 / (float)tests;
//delta_angle = 360.0 / (float)tests;
if (squiggle_count < squiggles_till_first_change) {
//line_length = int(random(3, 60));
delta_angle = 360.0 / (float)tests;
} else {
//start_angle = degrees(atan2(img.height/2.0 - start_y -470, img.width/2.0 - start_x+130) )-10+90; // wierd spiral
//start_angle = degrees(atan2(img.height/2.0 - start_y +145, img.width/2.0 - start_x+45) )-10+90; //cuba car
//start_angle = degrees(atan2(img.height/2.0 - start_y +210, img.width/2.0 - start_x-100) )-10; // italy
delta_angle = 180 + 7 / (float)tests;
}
for (int d = 0; d < tests; d++) {
float b = bresenham_avg_brightness(start_x, start_y, line_length,(delta_angle * d) + start_angle);
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
float bresenham_avg_brightness(int x0, int y0, float distance, float degree) {
int x1, y1;
int sum_brightness = 0;
int count_brightness = 0;
ArrayList <intPoint> pnts;
x1 =int(cos(radians(degree)) * distance) + x0;
y1 =int(sin(radians(degree)) * distance) + y0;
x0 =constrain(x0, 0, img.width - 1);
y0 =constrain(y0, 0, img.height - 1);
x1 =constrain(x1, 0, img.width - 1);
y1 =constrain(y1, 0, img.height - 1);
pnts= bresenham(x0, y0, x1, y1);
for (intPoint p : pnts) {
int loc = p.x + p.y * img.width;
sum_brightness += brightness(img.pixels[loc]);
count_brightness++;
if (sum_brightness / count_brightness < darkest_neighbor) {
darkest_x = p.x;
darkest_y = p.y;
darkest_neighbor = (float)sum_brightness / (float)count_brightness;
}
//println(x0+","+y0+" "+p.x+","+p.y+" brightness:"+sum_brightness / count_brightness+" darkest:"+darkest_neighbor+" "+darkest_x+","+darkest_y);
}
//println();
return(sum_brightness / count_brightness);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
public void post_processing() {
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
public void output_parameters() {
code_comment("adjustbrightness: " + adjustbrightness);
code_comment("squiggle_length: " + squiggle_length);
}
}