forked from krummrey/SpiralFromImage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpiralFromImage.pde
1064 lines (926 loc) · 29.5 KB
/
SpiralFromImage.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SpiralfromImage
// Copyright Jan Krummrey 2016
//
// Forked version
// (C) 2021 Michiyasu Odaki
//
// Idea taken from Norwegian Creations Drawbot
// http://www.norwegiancreations.com/2012/04/drawing-machine-part-2/
//
// The sketch takes an image and turns it into a modulated spiral.
// Dark parts of the image have larger amplitudes.
// The result is being writen to a PDF for refinement in Illustrator/Inkscape
//
// Version
// 1.0 Buggy PDF export
// 1.1 added SVG export and flag to swith off PDF export
// 1.2 removed PDF export
// added and reworked CP5 gui (taken from max_bol's fork)
// fixed wrong SVG header
//
// Forked version
// 1.3 support live preview
// support PDF export
// choose centerpoint with mouse or numeric box
// 1.4 support transparency
// remove mask color function
// check to see if the image format is supported on open
// automatically calculate ampScale
// 1.5 rename clear display button to view original
// fixed the spiral data could be out of the display size range
// support drawing in white on a black canvas
// draw a guide frame around the original image
// draw a checkered pattern as a canvas to make the transparent image easier to see
// 1.6 added color mode
// support for inkscape layered SVG
// 1.7 fixed a problem where the save path was always the sketch folder
//
// SpiralfromImage is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SpiralfromImage. If not, see <http://www.gnu.org/licenses/>.
//
// http://jan.krummrey.de
import java.io.File; // Required for file path operations
import processing.svg.*;
import processing.pdf.*;
import controlP5.*; // CP5 for gui
ControlP5 cp5;
Textarea feedbackText;
static final int INTERNAL_IMAGE_SIZE = 1200;
static final int DISPLAY_IMAGE_SIZE = 600;
static final float SCALE_RATIO = float(DISPLAY_IMAGE_SIZE) / float(INTERNAL_IMAGE_SIZE);
String sourceImgPath = ""; // Source image absolute location
boolean isLoaded = false; // Whether the source image has been loaded or not
PImage sourceImg; // Source image for conversion
PImage displayImg; // Image to use as display
float distance = 5; // Distance between rings
float density = 36; // Density
int centerPointX = INTERNAL_IMAGE_SIZE / 2; // Center point of spiral
int centerPointY = INTERNAL_IMAGE_SIZE / 2; // Center point of spiral
float endRadius = INTERNAL_IMAGE_SIZE / 2; // Largest value the spiral needs to cover the image
PShape outputSpiral = null; // Spriral shape to draw
PShape outputSpiralC = null;
PShape outputSpiralM = null;
PShape outputSpiralY = null;
static final int PENCOLORMODE_BLACK = 0;
static final int PENCOLORMODE_WHITE = 1;
static final int PENCOLORMODE_COLORS = 2;
int penColorMode = PENCOLORMODE_BLACK;
boolean useCircleShape = false;
boolean usePreview = true;
color canvasColor = 255;
color guideFrameColor = color(0x00, 0x33, 0x68);
// internal state variables
boolean needToUpdatePreview = false;
boolean needToDrawOriginalImage = false;
static final int CANVAS_ORIGIN_X = 187;
static final int CANVAS_ORIGIN_Y = 85;
static final int GUI_BORDER = 12;
int canvasWidth = DISPLAY_IMAGE_SIZE;
int canvasHeight = DISPLAY_IMAGE_SIZE;
void settings() {
size(174 + DISPLAY_IMAGE_SIZE + 25 * 2, 75 + DISPLAY_IMAGE_SIZE + 25 * 2);
}
void setup() {
drawBackground();
outputSpiral = createShape(GROUP);
setupGUI();
}
void draw() {
if (needToDrawOriginalImage) {
needToDrawOriginalImage = false;
clearCanvas();
drawOriginalImage();
drawFrame();
}
if (needToUpdatePreview) {
needToUpdatePreview = false;
updateOutputSpiral();
clearCanvas();
drawSpiral();
drawFrame();
}
}
void setupGUI() {
cp5 = new ControlP5(this);
final int x0 = 37; // parts align x
final int y0 = 37; // parts align y
final int h0 = 19; // parts height
final int w0 = 100; // parts width
final int s0 = 6; // parts spacing
final int t0 = 12; // label height
int xx = x0;
int yy = y0;
// Create a new button with name 'openFileButton'
cp5.addButton("openFileButton")
.setLabel("Open File")
.setBroadcast(false)
.setPosition(xx, yy)
.setSize(w0, h0)
.setBroadcast(true)
;
yy += (h0 + s0);
yy += t0; // some space for grouping
// Create a new button with name 'generateSpiralButton'
cp5.addButton("generateSpiralButton")
.setLabel("Generate Spiral")
.setBroadcast(false)
.setPosition(xx, yy)
.setSize(w0, h0)
.setBroadcast(true)
;
yy += (h0 + s0);
// Create a new button with name 'viewOriginalButton'
cp5.addButton("viewOriginalButton")
.setLabel("View Original")
.setBroadcast(false)
.setPosition(xx, yy)
.setSize(w0, h0)
.setBroadcast(true)
;
yy += (h0 + s0);
yy += t0; // some space for grouping
// Create a radio button to select color mode: default is black pen
cp5.addRadioButton("penColorRadiobutton")
.setLabel("Pen color")
.setPosition(xx, yy)
.setColorLabel(color(127))
.setSize(h0, h0)
.setItemsPerRow(1)
.setLabelPadding(10,10)
.addItem("Black Pen", PENCOLORMODE_BLACK)
.addItem("White Pen", PENCOLORMODE_WHITE)
.addItem("Color Pens", PENCOLORMODE_COLORS)
.activate(penColorMode)
.setNoneSelectedAllowed(false) // always have 1 item selected
;
yy += (h0 + s0) * 3;
// Create a new slider to set distance between rings: default value is 5
yy += t0; // need spece for the label
cp5.addSlider("distanceSlider")
.setBroadcast(false)
.setLabel("Distance between rings")
.setRange(5, 10)
.setValue(5)
.setNumberOfTickMarks(6)
.setPosition(xx, yy)
.setSize(w0, h0)
.setSliderMode(Slider.FLEXIBLE)
.setBroadcast(true)
;
yy += (h0 + s0);
// Reposition the Label for controller 'slider'
cp5.getController("distanceSlider").getCaptionLabel().align(ControlP5.LEFT, ControlP5.TOP_OUTSIDE).setPaddingX(0).setColor(color(128));
// Create a new slider to set density: default value is 75
yy += t0; // need spece for the label
cp5.addSlider("densitySlider")
.setBroadcast(false)
.setLabel("Density")
.setRange(36, 180)
.setValue(density)
.setPosition(xx, yy)
.setSize(w0, h0)
.setSliderMode(Slider.FLEXIBLE)
.setBroadcast(true)
;
yy += (h0 + s0);
// Reposition the Label for controller 'slider'
cp5.getController("densitySlider").getCaptionLabel().align(ControlP5.LEFT, ControlP5.TOP_OUTSIDE).setPaddingX(0).setColor(color(128));
yy += t0; // some space for grouping
// Create a numberbox to set centerpoint
cp5.addNumberbox("cernterPointXNumberbox")
.setLabel("Center X")
.setBroadcast(false)
.setRange(0, INTERNAL_IMAGE_SIZE - 1)
.setPosition(xx, yy)
.setSize(w0 / 2, h0)
.setScrollSensitivity(1.1)
.setDirection(Controller.HORIZONTAL) // change the control direction to left/right
.setValue(centerPointX)
.setBroadcast(true)
;
yy += (h0 + s0);
// Reposition the Label for controller 'slider'
cp5.getController("cernterPointXNumberbox").getCaptionLabel().align(ControlP5.RIGHT_OUTSIDE, ControlP5.CENTER).setPaddingX(10).setColor(color(128));
// Create a numberbox to set centerpoint
cp5.addNumberbox("cernterPointYNumberbox")
.setLabel("Center Y")
.setBroadcast(false)
.setRange(0, INTERNAL_IMAGE_SIZE - 1)
.setPosition(xx, yy)
.setSize(w0 / 2, h0)
.setScrollSensitivity(1.1)
.setDirection(Controller.HORIZONTAL) // change the control direction to left/right
.setValue(centerPointY)
.setBroadcast(true)
;
yy += (h0 + s0);
// Reposition the Label for controller 'slider'
cp5.getController("cernterPointYNumberbox").getCaptionLabel().align(ControlP5.RIGHT_OUTSIDE, ControlP5.CENTER).setPaddingX(10).setColor(color(128));
// Create a toggle to enable/disable live preview: default is false
cp5.addToggle("useCircleSwitch")
.setLabel("Circle Shape")
.setBroadcast(false)
.setPosition(xx, yy)
.setSize(h0, h0)
.setValue(useCircleShape)
.setBroadcast(true)
;
yy += (h0 + s0);
// Reposition the Label for controller 'toggle'
cp5.getController("useCircleSwitch").getCaptionLabel().align(ControlP5.RIGHT_OUTSIDE, ControlP5.CENTER).setPaddingX(10).setColor(color(128));
yy += t0; // some space for grouping
// Create a toggle to enable/disable live preview: default is true
cp5.addToggle("previewSwitch")
.setLabel("Live Preview")
.setBroadcast(false)
.setPosition(xx, yy)
.setSize(h0, h0)
.setValue(usePreview)
.setBroadcast(true)
;
yy += (h0 + s0);
// Reposition the Label for controller 'toggle'
cp5.getController("previewSwitch").getCaptionLabel().align(ControlP5.RIGHT_OUTSIDE, ControlP5.CENTER).setPaddingX(10).setColor(color(128));
// Skip
yy += (h0 + s0);
// Create a new button with name 'saveAsSVGButton'
cp5.addButton("saveAsSVGButton")
.setLabel("Save As SVG")
.setBroadcast(false)
.setPosition(xx, yy)
.setSize(w0, h0)
.setBroadcast(true)
;
yy += (h0 + s0);
// Create a new button with name 'saveAsPDFButton'
cp5.addButton("saveAsPDFButton")
.setLabel("Save As PDF")
.setBroadcast(false)
.setPosition(xx, yy)
.setSize(w0, h0)
.setBroadcast(true)
;
yy += (h0 + s0);
// Reset position for next raw
yy = y0;
xx = x0 + 150;
// Create a new text field to show feedback from the controller
feedbackText = cp5.addTextarea("feedback")
.setSize(canvasWidth, h0 * 2)
.setText("Load image to start")
//.setFont(createFont("arial", 12))
.setLineHeight(14)
.setColor(color(128))
.setColorBackground(color(235, 100))
.setColorForeground(color(245, 100))
.setPosition(xx, yy)
;
}
// Button control event handler
public void controlEvent(ControlEvent theEvent) {
//println(theEvent.getController().getName());
}
// Button Event - Open: Open image file dialogue
public void openFileButton(int theValue) {
selectInput("Select a file to process:", "fileSelected");
}
// Opens input file selection window and draws selected image to screen
void fileSelected(File selection) {
if (selection == null) {
return;
}
String locImg = selection.getAbsolutePath();
// Check to see if the format is supported
// https://processing.org/reference/loadImage_.html
String ext = locImg.substring(locImg.lastIndexOf(".") + 1).toLowerCase();
if (!ext.equals("gif")
&& !ext.equals("jpg") && !ext.equals("jpeg")
&& !ext.equals("tga")
&& !ext.equals("png")) {
feedbackText.setText(locImg + " is not supported format");
feedbackText.update();
return;
}
sourceImg = loadImage(locImg);
feedbackText.setText(locImg + " was succesfully opened");
feedbackText.update();
resizeImg();
displayImg = loadImage(locImg);
resizedisplayImg();
centerPointX = sourceImg.width / 2;
centerPointY = sourceImg.height / 2;
updateEndRadius();
// update GUI parts
cp5.getController("cernterPointXNumberbox").setValue(centerPointX);
cp5.getController("cernterPointXNumberbox").setMax(float(sourceImg.width - 1));
cp5.getController("cernterPointYNumberbox").setValue(centerPointY);
cp5.getController("cernterPointYNumberbox").setMax(float(sourceImg.height - 1));
// Everything went well.
sourceImgPath = locImg;
isLoaded = true;
if (usePreview) {
needToUpdatePreview = true;
} else {
needToDrawOriginalImage = true;
}
}
// Button Event - generateSpiral: Convert image file to SVG
public void generateSpiralButton(int theValue) {
if (!isLoaded) {
return;
}
needToUpdatePreview = true;
}
// Display loaded images
public void viewOriginalButton(int theValue) {
if (!isLoaded) {
return;
}
needToDrawOriginalImage = true;
}
public void penColorRadiobutton(int theValue) {
if (theValue == penColorMode) {
return;
}
penColorMode = theValue;
updateCanvasColor();
if (isLoaded && usePreview) {
needToUpdatePreview = true;
}
}
// Recieve wave distance value from slider
public void distanceSlider(int theValue) {
distance = theValue;
if (isLoaded && usePreview) {
needToUpdatePreview = true;
}
}
// Recieve density value from slider
public void densitySlider(int theValue) {
density = theValue;
if (isLoaded && usePreview) {
needToUpdatePreview = true;
}
}
// Recieve center X value from numberbox
public void cernterPointXNumberbox(int theValue) {
centerPointX = theValue;
updateEndRadius();
if (isLoaded && usePreview) {
needToUpdatePreview = true;
}
}
// Recieve center Y value from numberbox
public void cernterPointYNumberbox(int theValue) {
centerPointY = theValue;
updateEndRadius();
if (isLoaded && usePreview) {
needToUpdatePreview = true;
}
}
// Whether to make the data shape a circle or not
public void useCircleSwitch(boolean theValue) {
useCircleShape = theValue;
if (!isLoaded) {
return;
}
updateEndRadius();
if (usePreview) {
needToUpdatePreview = true;
}
}
// Change preview mode
public void previewSwitch(boolean theValue) {
usePreview = theValue;
if (!isLoaded) {
return;
}
if (usePreview) {
needToUpdatePreview = true;
}
}
// File path utils
String createOutputFilename(String basePath, String ext) {
// Get the filename of the image and remove the extension
// No check if extension exists
File file = new File(basePath);
String imageName = file.getPath();
imageName = imageName.substring(0, imageName.lastIndexOf("."));
return imageName + "." + ext;
}
// Save the spiral in the specified format
void saveAs(String format) {
if (!isLoaded) {
feedbackText.setText("no image file is currently open!");
feedbackText.update();
return;
}
// Construct filename
String ext = "";
if (format.equals(PDF)) {
ext = "pdf";
} else if (format.equals(SVG)) {
ext = "svg";
} else {
feedbackText.setText("format \"" + format + "\"" + " is not supported!");
feedbackText.update();
return;
}
String fileName = createOutputFilename(sourceImgPath, ext);
needToUpdatePreview = false;
// Update spiral by current parameter
updateOutputSpiral();
// Prepare
int w = sourceImg.width;
int h = sourceImg.height;
if (useCircleShape) {
w = int(endRadius * 2) - 1;
h = int(endRadius * 2) - 1;
}
// Draw it!
PGraphics pg = createGraphics(w, h, format, fileName);
pg.beginDraw();
pg.noStroke();
pg.fill(canvasColor);
if (useCircleShape) {
pg.translate(endRadius - centerPointX, endRadius - centerPointY);
pg.circle(centerPointX, centerPointY, w);
} else {
pg.rect(0, 0, w, h);
}
if (penColorMode == PENCOLORMODE_COLORS) {
if (format.equals(PDF)) {
// PDFwriter does not support blendMode(MULTIPLY).
// write the split image to the individual pages.
pg.shape(outputSpiralC);
((PGraphicsPDF)pg).nextPage();
pg.shape(outputSpiralM);
((PGraphicsPDF)pg).nextPage();
pg.shape(outputSpiralY);
} else {
pg.blendMode(MULTIPLY); // It doesn't work with SVG, but I'll give it a try.
pg.shape(outputSpiralC);
pg.shape(outputSpiralM);
pg.shape(outputSpiralY);
pg.blendMode(BLEND);
}
} else {
pg.shape(outputSpiral);
}
pg.dispose();
pg.endDraw();
// Done.
feedbackText.setText("saved as " + sketchPath(fileName));
feedbackText.update();
needToUpdatePreview = true;
}
// Save As SVG
public void saveAsSVGButton(int theValue) {
//saveAs(SVG);
saveAsInkscapeSVG();
}
// Save As PDF
public void saveAsPDFButton(int theValue) {
saveAs(PDF);
}
// Update Canvas Color
void updateCanvasColor() {
if (penColorMode == PENCOLORMODE_WHITE) {
canvasColor = color(0);
} else {
canvasColor = color(255);
}
}
// Redraw background elements
void drawBackground() {
noStroke();
background(235);
fill(245);
rect(25, 25, 100 + GUI_BORDER * 2, 25 + DISPLAY_IMAGE_SIZE + 25 * 2);
fill(245);
rect(175, 25, DISPLAY_IMAGE_SIZE + GUI_BORDER * 2, 25 + DISPLAY_IMAGE_SIZE + 25 * 2);
clearCanvas();
}
void clearCanvas() {
// Draw a checkered pattern
final int gridWidth = 10;
int c[] = {
// checker colors
color(0xe4, 0xe4, 0xf0), // dark
color(0xec, 0xec, 0xf0) // light
};
noStroke();
int base = 0;
for (int y = 0; y < canvasHeight; y += gridWidth) {
int n = base;
for (int x = 0; x < canvasWidth; x += gridWidth) {
fill(c[n]);
rect(CANVAS_ORIGIN_X + x, CANVAS_ORIGIN_Y + y, gridWidth, gridWidth);
n ^= 1;
}
base ^= 1;
}
}
void drawFrame() {
if (!isLoaded) {
return;
}
// Draw guide frame around the original image
noFill();
stroke(guideFrameColor);
rect(CANVAS_ORIGIN_X, CANVAS_ORIGIN_Y, displayImg.width - 1, displayImg.height - 1); // -1 needed
}
// Utility functions
PShape startSpiralStroke(color c) {
PShape s = createShape();
s.setFill(false);
s.beginShape();
s.stroke(c);
return s;
}
void drawSpiralStroke(PShape s, float xa, float ya, float xb, float yb) {
s.vertex(xa, ya);
s.vertex(xb, yb);
}
void endSpiralStroke(PShape s, PShape parent) {
s.endShape();
parent.addChild(s);
}
// Callback interface for various brightness converters
public interface CalcBrightness {
// Convert color value to brightness
float calc(color c);
}
//
// Function to create spiral shape from loaded image file - Transparency zero work as a mask colour
//
PShape createSpiral(CalcBrightness brightnessCallback, color drawColor) {
if (!isLoaded) {
return null;
}
// Calculates the first point
float delta;
float degree = density * 2 / (distance / 2);
float radius = distance / (360 / degree);
float rad = radians(degree);
PShape parent = createShape(GROUP);
parent.setFill(false);
parent.setStroke(true);
parent.setStrokeJoin(ROUND);
float halfDistance = (float)Math.ceil(distance / 2);
PShape s = null;
boolean shapeOn = false; // Keeps track of a shape is open or closed
while ((radius + halfDistance) < endRadius) { // Have we reached the far corner of the image?
float x = radius * cos(rad) + centerPointX;
float y = -radius * sin(rad) + centerPointY;
// Get the color and brightness of the sampled pixel
color c = sourceImg.get(int(x), int(y)); // Sampled color
float a = alpha(c); // Sampled alpha (transparency 0 .. 255)
// Are we within the the image?
// If so check if the shape is open. If not, open it
if ((a != 0.0)
&& (x > halfDistance) && ((x + halfDistance) < sourceImg.width)
&& (y > halfDistance) && ((y + halfDistance) < sourceImg.height)) {
float b = brightnessCallback.calc(c);
// Move up according to sampled brightness
float aradius = radius + b; // Radius with brighness applied up
float xa = aradius * cos(rad) + centerPointX;
float ya = -aradius * sin(rad) + centerPointY;
// Move down according to sampled brightness
delta = density / radius;
degree += delta;
radius += distance / (360 / delta);
rad = radians(degree);
float bradius = radius - b; // Radius with brighness applied down
float xb = bradius * cos(rad) + centerPointX;
float yb = -bradius * sin(rad) + centerPointY;
// Add vertices to shape
if (shapeOn == false) {
s = startSpiralStroke(drawColor);
shapeOn = true;
}
// Draw lines (from previous (xb, yb) to (xa, ya), then from (xa, ya) to (xb, yb)
drawSpiralStroke(s, xa, ya, xb, yb);
} else {
// We are outside of the image or transparency is zero, so close the shape if it is open
if (shapeOn) {
endSpiralStroke(s, parent);
shapeOn = false;
}
}
// Next
delta = density / radius;
degree += delta;
radius += distance / (360 / delta);
rad = radians(degree);
}
// end of loop
if (shapeOn) {
endSpiralStroke(s, parent);
}
return parent;
}
void updateOutputSpiral() {
if (!isLoaded) {
return;
}
if (penColorMode == PENCOLORMODE_BLACK) {
// draw with black stroke (it is managed by public variable)
outputSpiral = createSpiral((c) -> map(brightness(c), 0, 255, distance / 2, 0), color(0));
} else if (penColorMode == PENCOLORMODE_WHITE) {
// draw with white stroke (it is managed by public variable)
outputSpiral = createSpiral((c) -> map(brightness(c), 0, 255, 0, distance / 2), color(255));
} else if (penColorMode == PENCOLORMODE_COLORS) {
// draw with CMY strokes (these are managed by public variables)
outputSpiralC = createSpiral((c) -> map(red(c), 0, 255, distance / 2, 0), color(0,255,255)); // no red
outputSpiralM = createSpiral((c) -> map(green(c), 0, 255, distance / 2, 0), color(255,0,255)); // no green
outputSpiralY = createSpiral((c) -> map(blue(c), 0, 255, distance / 2, 0), color(255,255,0)); // no blue
PShape parent = createShape(GROUP);
parent.addChild(outputSpiralC);
parent.addChild(outputSpiralM);
parent.addChild(outputSpiralY);
// replace
outputSpiral = parent;
} else {
; // invalid mode
}
}
void drawSpiral() {
if (!isLoaded) {
return;
}
if (penColorMode != PENCOLORMODE_BLACK && penColorMode != PENCOLORMODE_WHITE && penColorMode != PENCOLORMODE_COLORS) {
return;
}
// Draw
pushMatrix();
// Scaling
translate(CANVAS_ORIGIN_X, CANVAS_ORIGIN_Y);
scale(SCALE_RATIO);
// Draw background shape
noStroke();
fill(canvasColor);
if (useCircleShape) {
circle(centerPointX, centerPointY, int(endRadius * 2) - 1);
} else {
rect(0, 0, sourceImg.width, sourceImg.height);
}
// Draw spiral shape
if (penColorMode == PENCOLORMODE_COLORS) {
blendMode(MULTIPLY);
shape(outputSpiralC);
shape(outputSpiralM);
shape(outputSpiralY);
blendMode(BLEND);
} else if (penColorMode == PENCOLORMODE_BLACK || penColorMode == PENCOLORMODE_WHITE) {
shape(outputSpiral);
} else {
; // invalid mode
}
popMatrix();
}
void resizeImg() {
if (sourceImg.width > sourceImg.height) {
sourceImg.resize(INTERNAL_IMAGE_SIZE, 0);
} else {
sourceImg.resize(0, INTERNAL_IMAGE_SIZE);
}
}
void resizedisplayImg() {
if (displayImg.width > displayImg.height) {
displayImg.resize(canvasWidth, 0);
} else {
displayImg.resize(0, canvasHeight);
}
}
void drawOriginalImage() {
image(displayImg, CANVAS_ORIGIN_X, CANVAS_ORIGIN_Y);
}
//
// Centerpoint functions
//
void updateEndRadius() {
if (useCircleShape) {
endRadius = getMinRadius();
} else {
endRadius = getMaxRadius();
}
}
float getMaxRadius() {
// Search the far corner of the image
//
// r0 | r1
//----+----
// r2 | r3
float r0 = sqrt(pow(centerPointX, 2) + pow(centerPointY, 2));
float r1 = sqrt(pow(sourceImg.width - 1 - centerPointX, 2) + pow(centerPointY, 2));
float r2 = sqrt(pow(centerPointX, 2) + pow(sourceImg.height - 1 - centerPointY, 2));
float r3 = sqrt(pow(sourceImg.width - 1 - centerPointX, 2) + pow(sourceImg.height - 1 - centerPointY, 2));
return (float)Math.floor(max(max(r0, r1), max(r2, r3)));
}
float getMinRadius() {
// Search the nearest edge of the image
float r0 = centerPointX;
float r1 = centerPointY;
float r2 = sourceImg.height - 1 - centerPointY;
float r3 = sourceImg.width - 1 - centerPointX;
return (float)Math.floor(min(min(r0, r1), min(r2, r3)));
}
//
// Process mouse events
//
boolean inCanvas() {
return(mouseX >= CANVAS_ORIGIN_X && mouseX < (CANVAS_ORIGIN_X + displayImg.width) &&
mouseY >= CANVAS_ORIGIN_Y && mouseY < (CANVAS_ORIGIN_Y + displayImg.height));
}
boolean mouseLocked = false;
void mousePressed() {
if (!isLoaded) {
return;
}
if (mouseButton == LEFT) {
if (inCanvas()) {
mouseLocked = true;
centerPointX = int(float(mouseX - CANVAS_ORIGIN_X) / SCALE_RATIO);
centerPointY = int(float(mouseY - CANVAS_ORIGIN_Y) / SCALE_RATIO);
// update GUI parts
cp5.getController("cernterPointXNumberbox").setValue(centerPointX);
cp5.getController("cernterPointYNumberbox").setValue(centerPointY);
updateEndRadius();
if (usePreview) {
needToUpdatePreview = true;
}
return;
} else {
mouseLocked = false;
}
}
}
void mouseReleased() {
if (mouseButton == LEFT) {
mouseLocked = false;
}
}
void writeSVGPolygonBody(PrintWriter output, PShape s, String style, int translateX, int translateY) {
int vertexcodecount = s.getVertexCodeCount();
int [] vertexcodes = s.getVertexCodes();
PVector vec = s.getVertex(0);
output.printf("<path style=\"" + style + "\" d=\"M%.4f %.4f", vec.x + translateX, vec.y + translateY);
for (int i = 1; i < vertexcodecount; i++) {
int code = vertexcodes[i];
if (code == VERTEX) {
vec = s.getVertex(i);
output.printf(" L%.4f %.4f", vec.x + translateX, vec.y + translateY);
}
}
output.println("\" />");
}
void writeSVGGroupBody(PrintWriter output, PShape parent, String style, int translateX, int translateY) {
output.println("<g>");
PShape [] children = parent.getChildren();
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
PShape s = children[i];
int k = s.getKind();
if (k == GROUP) {
writeSVGGroupBody(output, s, style, translateX, translateY);
} else if (k == POLYGON) {
writeSVGPolygonBody(output, s, style, translateX, translateY);
}
}
output.println("</g>");
}
void writeSVGShapeBody(PrintWriter output, PShape s, String style, int translateX, int translateY) {
int k = s.getKind();
if (k == GROUP) {
writeSVGGroupBody(output, s, style, translateX, translateY);
} else if (k == POLYGON) {
writeSVGPolygonBody(output, s, style, translateX, translateY);
}
}
void writeSVGHeader(PrintWriter output, int w, int h) {
output.println(
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>"
+"\n"+ "<svg"
+"\n"+ " width=\"" + w + "\""
+"\n"+ " height=\"" + h + "\""
+"\n"+ " version=\"1.1\""
+"\n"+ " xmlns:inkscape=\"http://www.inkscape.org/namespaces/inkscape\""
+"\n"+ " xmlns:sodipodi=\"http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd\""
+"\n"+ " xmlns=\"http://www.w3.org/2000/svg\""
+"\n"+ " xmlns:svg=\"http://www.w3.org/2000/svg\">"
);
}
void writeSVGHooter(PrintWriter output) {
output.println("</svg>");
}
void writeSVGLayerHeader(PrintWriter output, String name, String style) {
output.println(
" <g"
+"\n"+ " inkscape:groupmode=\"layer\""
+"\n"+ " inkscape:label=\"" + name + "\""
+"\n"+ " style=\"" + style + "\""
+"\n"+ " >"
);
}
void writeSVGLayerHooter(PrintWriter output) {
output.println(" </g>");
}
void writeSVGRectBody(PrintWriter output, int w, int h, String style) {
output.println(
" <rect"
+"\n"+ " x=\"0\""
+"\n"+ " width=\"" + w + "\""
+"\n"+ " height=\"" + h + "\""
+"\n"+ " y=\"0\""
+"\n"+ " style=\"" + style + "\""
+"\n"+ " />"
);
}
void writeSVGCircleBody(PrintWriter output, int r, int cx, int cy, String style) {
output.println(
" <circle"
+"\n"+ " r=\"" + r + "\""
+"\n"+ " style=\"" + style + "\""
+"\n"+ " cx=\"" + cx + "\""
+"\n"+ " cy=\"" + cx + "\""
+"\n"+ " />"
);
}
void saveAsInkscapeSVG() {
if (!isLoaded) {
feedbackText.setText("no image file is currently open!");
feedbackText.update();
return;
}
// Construct filename
String fileName = createOutputFilename(sourceImgPath, "svg");
needToUpdatePreview = false;
// Update spiral by current parameter
updateOutputSpiral();
// Prepare
int r = 1;
int w = sourceImg.width;
int h = sourceImg.height;
int tx = 0;
int ty = 0;