-
Notifications
You must be signed in to change notification settings - Fork 0
/
PixelSort.go
1430 lines (1185 loc) · 32.6 KB
/
PixelSort.go
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
package main
import (
"flag"
"fmt"
"image"
"image/color"
"image/draw"
"image/jpeg" // register the JPG format with the image package
"image/png" // register the PNG format with the image package
"math"
"math/cmplx"
"math/rand"
"os"
"path/filepath"
"sort"
"strings"
"sync"
"github.com/disintegration/imaging"
"github.com/mjibson/go-dsp/fft"
)
const (
VALUE_CONST uint = 15
)
const (
BLOB_SHAPE int = 0
BOX_SHAPE int = 1
SQUARE_SHAPE int = 2
)
type ArgsIn_t struct {
help bool
file string
outfile string
delta int
rev bool
randgroup bool
angle float64
scale float64
effect string
chanhue string
chandeg int
chanjoin bool
dftsat float64
}
var ArgsIn ArgsIn_t
func init() {
const (
default_file string = ""
usage_file string = "(required) Source image (.jpg or .png)"
usage_outfile string = "(required) Output image (.jpg or .png)"
default_delta int = 450
usage_delta string = "Sensitivity to edges for sorting effects"
usage_rev string = "swap the direction" // TODO change this into a angle direction for the grad
usage_rand string = "randomize in sorted group"
default_scale float64 = 1.0
usage_scale string = "sample every xth pixel (e.g. 1.5 will downsample to 2/3)"
default_angle float64 = 0.0
usage_angle string = "Angle for the applied the effect"
default_effect string = "linesort"
usage_effect string = "Selected effect (linesort|blocksort|floodsort|dft|idft|hdft|hidft)"
default_chanhue string = "FF0000"
default_chandeg int = 360
usage_chanhue string = "Hue to isolate. Use (r|g|b), sets deg automatically"
usage_chandeg string = "Size of hue to isolate (As degrees, eg 120 for just 1/3 of color space)"
usage_chanjoin string = "Don't join isolated channel after effect"
usage_dftsat string = "Value for log scaling half dft"
default_dftsat float64 = 65537.0
)
flag.BoolVar(&ArgsIn.help, "h", false, "Prints the usage")
flag.StringVar(&ArgsIn.file, "image", default_file, usage_file)
flag.StringVar(&ArgsIn.file, "i", default_file, usage_file+" (shorthand)")
flag.StringVar(&ArgsIn.outfile, "output", default_file, usage_outfile)
flag.StringVar(&ArgsIn.outfile, "o", default_file, usage_outfile+" (shorthand)")
flag.IntVar(&ArgsIn.delta, "delta", default_delta, usage_delta)
flag.IntVar(&ArgsIn.delta, "d", default_delta, usage_delta+" (shorthand)")
flag.BoolVar(&ArgsIn.rev, "r", false, usage_rev)
flag.BoolVar(&ArgsIn.randgroup, "rnd", false, usage_rand)
flag.Float64Var(&ArgsIn.angle, "ang", default_angle, usage_angle)
flag.Float64Var(&ArgsIn.scale, "smp", default_scale, usage_scale)
flag.StringVar(&ArgsIn.effect, "effect", default_effect, usage_effect)
flag.StringVar(&ArgsIn.effect, "e", default_effect, usage_effect+" (shorthand)")
flag.StringVar(&ArgsIn.chanhue, "ch", default_chanhue, usage_chanhue)
flag.IntVar(&ArgsIn.chandeg, "cd", default_chandeg, usage_chandeg)
flag.BoolVar(&ArgsIn.chanjoin, "cj", false, usage_chanjoin)
flag.Float64Var(&ArgsIn.dftsat, "dftsat", default_dftsat, usage_dftsat)
//TODO add a delta mask input option
//TODO add a dry/wet percentage option
}
func main() {
// parse the input
flag.Parse()
if ArgsIn.help || len(ArgsIn.file) == 0 || len(ArgsIn.outfile) == 0 {
fmt.Printf("Usage:\n")
flag.PrintDefaults()
os.Exit(0)
}
infile, err := os.Open(ArgsIn.file)
if err != nil {
fmt.Printf("Unknown file %s\n", ArgsIn.file)
flag.PrintDefaults()
os.Exit(1)
}
defer infile.Close()
ftype := filepath.Ext(ArgsIn.outfile)
if ftype != ".jpg" && ftype != ".jpeg" && ftype != ".png" {
fmt.Printf("Unknown output filetype %s\n", ftype)
os.Exit(1)
}
fmt.Printf("Reading %s\n", ArgsIn.file)
// decode our image
img, _, err := image.Decode(infile)
if err != nil {
fmt.Printf("Unknown file type %s\n", ArgsIn.file)
flag.PrintDefaults()
os.Exit(1)
}
// put it in a NRGBA type
var rgbaimg *image.NRGBA
if iimg, ok := img.(*image.NRGBA); ok {
rgbaimg = iimg
} else {
// it isn't already RGBA, so we have to convert it
rct := img.Bounds()
rgbaimg = image.NewNRGBA(rct)
draw.Draw(rgbaimg, rct, img, rct.Min, draw.Src)
}
// Apply isolation steps (separate channel, downrez, rotate)
if ArgsIn.scale > 1.0 {
//TODO scale img down
obound := rgbaimg.Bounds()
nw := float64(obound.Dx()) / ArgsIn.scale
nh := float64(obound.Dy()) / ArgsIn.scale
nbound := image.Rect(0, 0, int(nw), int(nh))
newimg := image.NewNRGBA(nbound)
for y := 0; y < nbound.Max.Y; y++ {
new_yi := y * newimg.Stride
old_yi := int(float64(y)*ArgsIn.scale) * rgbaimg.Stride
for x := 0; x < nbound.Max.X; x++ {
new_i := new_yi + (x * 4)
old_i := old_yi + (int(float64(x)*ArgsIn.scale) * 4)
copy(newimg.Pix[new_i:new_i+4], rgbaimg.Pix[old_i:old_i+4])
}
}
fmt.Printf("Scaled image down to %vx%v\n", int(nw), int(nh))
rgbaimg = newimg
}
// do rotation
origbounds := rgbaimg.Bounds()
if ArgsIn.angle != 0 {
rgbaimg = imaging.Rotate(rgbaimg, ArgsIn.angle, color.Transparent)
}
// isolate selected channel
var otherchans *image.NRGBA = nil
if ArgsIn.chandeg < 0 || ArgsIn.chandeg > 360 {
fmt.Println("Please specify a channel spread from 0 to 360")
os.Exit(1)
}
if ArgsIn.chanhue == "r" {
ArgsIn.chanhue = "FF0000"
ArgsIn.chandeg = 120
} else if ArgsIn.chanhue == "g" {
ArgsIn.chanhue = "00FF00"
ArgsIn.chandeg = 120
} else if ArgsIn.chanhue == "b" {
ArgsIn.chanhue = "0000FF"
ArgsIn.chandeg = 120
}
if ArgsIn.chandeg != 360 {
c, err := hex2col(ArgsIn.chanhue)
if err != nil {
fmt.Printf("Unrecognized hex color %q", ArgsIn.chanhue)
os.Exit(1)
}
if c[3] != 0xff {
fmt.Printf("Warning, ignoring non-opaque alpha component of channel color")
}
if c[0] == 0 && c[1] == 0 && c[2] == 0 {
fmt.Println("Channel color must not be black")
os.Exit(1)
}
if c[0] == 0xff && c[1] == 0xff && c[2] == 0xff {
fmt.Println("Channel color must not be white")
os.Exit(1)
}
if ArgsIn.chandeg == 120 {
if (c[0] == 0xff || c[0] == 0) &&
(c[1] == 0xff || c[1] == 0) &&
(c[2] == 0xff || c[2] == 0) {
// fast mask path
rgbaimg, otherchans = separateSimpleChannels(rgbaimg, c)
}
}
if otherchans == nil {
//TODO
fmt.Println("TODO separate out non-simple RGB channels")
os.Exit(1)
}
}
if ArgsIn.rev && ArgsIn.randgroup {
fmt.Printf("Warning, randgroup overrides reverse option")
}
// apply selected effect
fmt.Println("Applying Effect")
var outimg *image.NRGBA
if strings.HasPrefix(ArgsIn.effect, "line") || strings.HasPrefix(ArgsIn.effect, "pix") {
outimg, err = PixelSort(rgbaimg, uint32(ArgsIn.delta), ArgsIn.rev, ArgsIn.randgroup)
if err != nil {
fmt.Printf("Unable to pixel sort due to error : %v\n", err)
os.Exit(1)
}
} else if strings.HasPrefix(ArgsIn.effect, "flood") {
outimg, err = FloodSort(rgbaimg, uint32(ArgsIn.delta), ArgsIn.rev, ArgsIn.randgroup, BLOB_SHAPE)
if err != nil {
fmt.Printf("Unable to flood sort due to error : %v\n", err)
os.Exit(1)
}
} else if strings.HasPrefix(ArgsIn.effect, "block") || strings.HasPrefix(ArgsIn.effect, "box") {
outimg, err = FloodSort(rgbaimg, uint32(ArgsIn.delta), ArgsIn.rev, ArgsIn.randgroup, BOX_SHAPE)
if err != nil {
fmt.Printf("Unable to block sort due to error : %v\n", err)
os.Exit(1)
}
} else if strings.HasPrefix(ArgsIn.effect, "square") {
outimg, err = FloodSort(rgbaimg, uint32(ArgsIn.delta), ArgsIn.rev, ArgsIn.randgroup, SQUARE_SHAPE)
if err != nil {
fmt.Printf("Unable to square sort due to error : %v\n", err)
os.Exit(1)
}
} else if strings.HasPrefix(ArgsIn.effect, "hdft") {
outimg, err = FftHalf(rgbaimg, ArgsIn.dftsat, false)
if err != nil {
fmt.Printf("Unable to use fft due to error : %v\n", err)
os.Exit(1)
}
} else if strings.HasPrefix(ArgsIn.effect, "hidft") {
outimg, err = IFftHalf(rgbaimg, ArgsIn.dftsat, false)
if err != nil {
fmt.Printf("Unable to use fft due to error : %v\n", err)
os.Exit(1)
}
} else if strings.HasPrefix(ArgsIn.effect, "dft") {
outimg, err = FftHalf(rgbaimg, ArgsIn.dftsat, true)
if err != nil {
fmt.Printf("Unable to use fft due to error : %v\n", err)
os.Exit(1)
}
} else if strings.HasPrefix(ArgsIn.effect, "idft") {
outimg, err = IFftHalf(rgbaimg, ArgsIn.dftsat, true)
if err != nil {
fmt.Printf("Unable to use fft due to error : %v\n", err)
os.Exit(1)
}
} else {
fmt.Printf("Unknown effect %v\n", ArgsIn.effect)
os.Exit(1)
}
if otherchans != nil && !ArgsIn.chanjoin {
// join back with the other channels
outimg = addImages(outimg, otherchans)
}
// rotate back
if ArgsIn.angle != 0 {
outimg = imaging.Rotate(rgbaimg, float64(-ArgsIn.angle), color.Transparent)
// cut back to original size
newbounds := outimg.Bounds()
hdx := (newbounds.Dx() - origbounds.Dx()) / 2
hdy := (newbounds.Dy() - origbounds.Dy()) / 2
bnd := origbounds.Add(image.Point{X: hdx, Y: hdy})
outimg = outimg.SubImage(bnd).(*image.NRGBA)
}
// output the image
fmt.Printf("Writing to %s\n", ArgsIn.outfile)
outfile, err := os.Create(ArgsIn.outfile)
if err != nil {
fmt.Printf("Could not create file %s\n", ArgsIn.outfile)
os.Exit(1)
}
defer outfile.Close()
if ftype == ".jpg" || ftype == ".jpeg" {
jpeg.Encode(outfile, outimg, nil)
} else if ftype == ".png" {
png.Encode(outfile, outimg)
}
os.Exit(0)
}
func PixelSort(img *image.NRGBA, delta uint32, reverse bool, randgroup bool) (*image.NRGBA, error) {
if delta <= 0 {
return img, nil
}
var wg sync.WaitGroup
// iterate across the image
bounds := img.Bounds()
fmt.Printf("Processing %d rows...", bounds.Max.Y-bounds.Min.Y)
for row := bounds.Min.Y; row < bounds.Max.Y; row++ {
yi := img.Stride * row
// find areas to sort, within delta
wg.Add(1)
go func() {
defer wg.Done()
var back, front int
back = bounds.Min.X
inclear := true
for front = back + 1; front < bounds.Max.X; front++ {
i2 := yi + (front * 4)
i1 := i2 - 4
// first get to a spot past 0 alpha segment
if img.Pix[i1+3] == 0 {
back = front
continue
}
inclear = false
p1 := img.Pix[i1 : i1+4] // front -1
p2 := img.Pix[i2 : i2+4] // front
// go until we detect an edge, then sort the area
dosort := false
// check if we hit a 0 alpha
if img.Pix[i2+3] == 0 {
inclear = true
dosort = true
}
// check against delta
if !dosort {
pd := colorDist2(p1, p2)
if delta < pd {
dosort = true
}
}
if !dosort {
continue
}
// sort and step
if !randgroup {
SortLineArea(img, (back*4)+yi, (front*4)+yi, reverse)
} else {
RandomizeLineArea(img, (back*4)+yi, (front*4)+yi)
}
back = front
}
// sort the last bit of the row as long as it isn't 0 alpha
if !inclear {
if !randgroup {
SortLineArea(img, (back*4)+yi, (front*4)+yi, reverse)
} else {
RandomizeLineArea(img, (back*4)+yi, (front*4)+yi)
}
}
}()
}
wg.Wait()
fmt.Printf("\n")
return img, nil
}
func SortLineArea(img *image.NRGBA, iback, ifrontin int, reverse bool) {
if iback+4 >= ifrontin {
return
}
// bubble pixels
for ifront := ifrontin; ifront > iback; ifront = ifront - 4 {
for i := iback; i < ifront-4; i = i + 4 {
i1 := i
i2 := i1 + 4
p1 := img.Pix[i1 : i1+4]
p2 := img.Pix[i2 : i2+4]
sum1 := colorSum(p1)
sum2 := colorSum(p2)
// swap the points if brighter on left
if (reverse && sum1 < sum2) || (!reverse && sum1 > sum2) {
t := []uint8{0, 0, 0}
t[0] = p2[0]
t[1] = p2[1]
t[2] = p2[2]
img.Pix[i2] = p1[0]
img.Pix[i2+1] = p1[1]
img.Pix[i2+2] = p1[2]
img.Pix[i1] = t[0]
img.Pix[i1+1] = t[1]
img.Pix[i1+2] = t[2]
}
}
}
}
func RandomizeLineArea(img *image.NRGBA, iback, ifront int) {
rand.Shuffle((ifront-iback)/4, func(i, j int) {
i1 := iback + (i * 4)
i2 := iback + (j * 4)
img.Pix[i1], img.Pix[i2] = img.Pix[i2], img.Pix[i1]
img.Pix[i1+1], img.Pix[i2+1] = img.Pix[i2+1], img.Pix[i1+1]
img.Pix[i1+2], img.Pix[i2+2] = img.Pix[i2+2], img.Pix[i1+2]
})
}
type pxpt struct {
x int
y int
}
func FloodSort(img *image.NRGBA, delta uint32, reverse, randgroup bool, shape int) (*image.NRGBA, error) {
if delta <= 0 {
return img, nil
}
var wg sync.WaitGroup
// iterate across the image
bounds := img.Bounds()
w := bounds.Max.X - bounds.Min.X
h := bounds.Max.Y - bounds.Min.Y
if w <= 0 || h <= 0 {
return img, nil
}
var checked []bool = make([]bool, w*h)
var wave []pxpt = make([]pxpt, 0, w*3)
// start at min
wave = append(wave, pxpt{bounds.Min.X, bounds.Min.Y})
prevcap := cap(wave) // TODO tune this? actually using the biggest is a big mistake though
debuggroupcount := 0
//var shapefunc func(startpt pxpt, group *[]pxpt, wave *[]pxpt, img *image.NRGBA, checked []bool, delta uint32)
var shapefunc func(pxpt, *[]pxpt, *[]pxpt, *image.NRGBA, []bool, uint32, int) = nil
switch shape {
case BLOB_SHAPE:
shapefunc = FloodBlob
case SQUARE_SHAPE:
shapefunc = FloodBox
case BOX_SHAPE:
shapefunc = FloodBox
default:
panic("Unrecognized shape for floodsort")
}
// have a processing wave going out, goroutines will sort flooded areas
for len(wave) > 0 {
var pt pxpt
pt, wave = wave[0], wave[1:]
// if this is already checked, continue
if checked[(pt.x-bounds.Min.X)+((pt.y-bounds.Min.Y)*w)] {
continue
}
checked[(pt.x-bounds.Min.X)+((pt.y-bounds.Min.Y)*w)] = true
// if this is 0alpha, then add unchecked neighbors to the wave and continue
i1 := (pt.y * img.Stride) + (pt.x * 4)
if img.Pix[i1+3] == 0 {
for _, npt := range [4]pxpt{{0, 1}, {0, -1}, {1, 0}, {-1, 0}} {
y := pt.y + npt.y
x := pt.x + npt.x
iy := y - bounds.Min.Y
ix := x - bounds.Min.X
if ix < 0 || ix >= w || iy < 0 || iy >= h {
continue
}
if checked[ix+(iy*w)] {
continue
}
wave = append(wave, pxpt{x: x, y: y})
}
continue
}
// flood out a group
var group []pxpt = make([]pxpt, 0, prevcap)
debuggroupcount += 1
shapefunc(pt, &group, &wave, img, checked, delta, shape)
if len(group) <= 1 {
continue
}
// sort the group in a goroutine
//fmt.Printf("DEBUG Start %v group for %v (wave at %v) %v/%v\n", debuggroupcount, len(group), len(wave), debugcheckcount, debugfullcheckcount)
wg.Add(1)
go func(group []pxpt, img *image.NRGBA, rev bool) {
defer wg.Done()
// we should be soul owners of the pixels in the group, so no sync needed beyond waitgroup
// gather colors
colors := make([][4]uint8, len(group))
for i, pt := range group {
i1 := (pt.y * img.Stride) + (pt.x * 4)
p1 := img.Pix[i1 : i1+4]
copy(colors[i][:], p1[:4])
}
if !randgroup {
// sort points by direction
sort.Slice(group, func(i, j int) bool {
//DEBUG
res := group[i].x < group[j].x
if rev {
return !res
}
return res
})
// sort colors
sort.Slice(colors, func(i, j int) bool {
sum1 := colorSum(colors[i][:])
sum2 := colorSum(colors[j][:])
return sum1 < sum2
})
} else {
// randomize points
rand.Shuffle(len(group), func(i, j int) {
group[i], group[j] = group[j], group[i]
})
}
// put sorted colors in the points
for i := 0; i < len(group); i++ {
i1 := (group[i].y * img.Stride) + (group[i].x * 4)
copy(img.Pix[i1:i1+4], colors[i][:])
}
}(group, img, reverse)
}
fmt.Printf("Waiting on %v sorters\n", debuggroupcount)
wg.Wait()
return img, nil
}
func FloodBlob(startpt pxpt, group *[]pxpt, wave *[]pxpt, img *image.NRGBA, checked []bool, delta uint32, _ int) {
bounds := img.Bounds()
w := bounds.Dx()
h := bounds.Dy()
var groupwave []pxpt = make([]pxpt, 0, cap(*group))
groupwave = append(groupwave, startpt)
for len(groupwave) > 0 {
var gpt pxpt
gpt, groupwave = groupwave[0], groupwave[1:]
// add this to the group
*group = append(*group, gpt)
// check this is checked
ix := gpt.x - bounds.Min.X
iy := gpt.y - bounds.Min.Y
if !checked[ix+(iy*w)] {
panic("Px in groupwave that was not already checked")
}
i1 := (gpt.y * img.Stride) + (gpt.x * 4)
p1 := img.Pix[i1 : i1+4]
// if neighbors are unchecked, see if they are within delta
// add to group, else add to wave
for dy := -1; dy <= 1; dy++ {
if dy == 0 || iy+dy < 0 || iy+dy >= h {
continue
}
if checked[ix+((iy+dy)*w)] {
continue
}
i2 := ((gpt.y + dy) * img.Stride) + (gpt.x * 4)
p2 := img.Pix[i2 : i2+4]
// if this is 0alpha add to wave
addtogroup := true
if p2[3] == 0 {
addtogroup = false
}
if addtogroup {
// check if within delta
pd := colorDist2(p1, p2)
if delta < pd {
addtogroup = false
}
}
if !addtogroup {
// add to wave
*wave = append(*wave, pxpt{x: gpt.x, y: gpt.y + dy})
} else {
// add to group and mark checked
groupwave = append(groupwave, pxpt{x: gpt.x, y: gpt.y + dy})
checked[ix+((iy+dy)*w)] = true
}
}
for dx := -1; dx <= 1; dx++ {
if dx == 0 || ix+dx < 0 || ix+dx >= w {
continue
}
if checked[(ix+dx)+(iy*w)] {
continue
}
i2 := (gpt.y * img.Stride) + ((gpt.x + dx) * 4)
p2 := img.Pix[i2 : i2+4]
// if this is 0alpha add to wave
addtogroup := true
if p2[3] == 0 {
addtogroup = false
}
if addtogroup {
// check if within delta
pd := colorDist2(p1, p2)
if delta < pd {
addtogroup = false
}
}
if !addtogroup {
// add to wave
*wave = append(*wave, pxpt{x: gpt.x + dx, y: gpt.y})
} else {
// add to group
groupwave = append(groupwave, pxpt{x: gpt.x + dx, y: gpt.y})
checked[(ix+dx)+(iy*w)] = true
}
}
}
}
func FloodBox(startpt pxpt, group *[]pxpt, wave *[]pxpt, img *image.NRGBA, checked []bool, delta uint32, shape int) {
// from starting point keep expanding out until we hit something checked, 0alpha, or past delta
*group = append(*group, startpt)
bounds := img.Bounds()
w := bounds.Dx()
up, down, left, right := 0, 0, 0, 0
blockup, blockdown, blockleft, blockright := false, false, false, false
for {
if shape == SQUARE_SHAPE && ((blockup && blockdown) || (blockleft && blockright)) {
break
} else if blockup && blockdown && blockright && blockleft {
break
}
// UP
if !blockup {
y := startpt.y - (up + 1)
if y < bounds.Min.Y {
blockup = true
} else {
for x := startpt.x - left; x <= (startpt.x + right); x++ {
ix := x - bounds.Min.X
iy := y - bounds.Min.Y
if checked[ix+(iy*w)] {
blockup = true
break
}
i1 := (y * img.Stride) + (x * 4)
p1 := img.Pix[i1 : i1+4]
// check 0alpha
if p1[3] == 0 {
blockup = true
break
}
i2 := ((y + 1) * img.Stride) + (x * 4)
p2 := img.Pix[i2 : i2+4]
pd := colorDist2(p1, p2)
if pd > delta {
blockup = true
break
}
}
if !blockup {
// add those to the group
for x := startpt.x - left; x <= (startpt.x + right); x++ {
ix := x - bounds.Min.X
iy := y - bounds.Min.Y
checked[ix+(iy*w)] = true
*group = append(*group, pxpt{x, y})
}
up += 1
}
}
}
// LEFT
if !blockleft {
x := startpt.x - (left + 1)
if x < bounds.Min.X {
blockleft = true
} else {
for y := startpt.y - up; y <= (startpt.y + down); y++ {
ix := x - bounds.Min.X
iy := y - bounds.Min.Y
if checked[ix+(iy*w)] {
blockleft = true
break
}
i1 := (y * img.Stride) + (x * 4)
p1 := img.Pix[i1 : i1+4]
// check 0alpha
if p1[3] == 0 {
blockleft = true
break
}
i2 := (y * img.Stride) + ((x + 1) * 4)
p2 := img.Pix[i2 : i2+4]
pd := colorDist2(p1, p2)
if pd > delta {
blockleft = true
break
}
}
if !blockleft {
// add those to the group
for y := startpt.y - up; y <= (startpt.y + down); y++ {
ix := x - bounds.Min.X
iy := y - bounds.Min.Y
checked[ix+(iy*w)] = true
*group = append(*group, pxpt{x, y})
}
left += 1
}
}
}
// DOWN
if !blockdown {
y := startpt.y + (down + 1)
if y >= bounds.Max.Y {
blockdown = true
} else {
for x := startpt.x - left; x <= (startpt.x + right); x++ {
ix := x - bounds.Min.X
iy := y - bounds.Min.Y
if checked[ix+(iy*w)] {
blockdown = true
break
}
i1 := (y * img.Stride) + (x * 4)
p1 := img.Pix[i1 : i1+4]
// check 0alpha
if p1[3] == 0 {
blockdown = true
break
}
i2 := ((y - 1) * img.Stride) + (x * 4)
p2 := img.Pix[i2 : i2+4]
pd := colorDist2(p1, p2)
if pd > delta {
blockdown = true
break
}
}
if !blockdown {
// add those to the group
for x := startpt.x - left; x <= (startpt.x + right); x++ {
ix := x - bounds.Min.X
iy := y - bounds.Min.Y
checked[ix+(iy*w)] = true
*group = append(*group, pxpt{x, y})
}
down += 1
}
}
}
// RIGHT
if !blockright {
x := startpt.x + (right + 1)
if x >= bounds.Max.X {
blockright = true
} else {
for y := startpt.y - up; y <= (startpt.y + down); y++ {
ix := x - bounds.Min.X
iy := y - bounds.Min.Y
if checked[ix+(iy*w)] {
blockright = true
break
}
i1 := (y * img.Stride) + (x * 4)
p1 := img.Pix[i1 : i1+4]
// check 0alpha
if p1[3] == 0 {
blockright = true
break
}
i2 := (y * img.Stride) + ((x - 1) * 4)
p2 := img.Pix[i2 : i2+4]
pd := colorDist2(p1, p2)
if pd > delta {
blockright = true
break
}
}
if !blockright {
// add those to the group
for y := startpt.y - up; y <= (startpt.y + down); y++ {
ix := x - bounds.Min.X
iy := y - bounds.Min.Y
checked[ix+(iy*w)] = true
*group = append(*group, pxpt{x, y})
}
right += 1
}
}
}
}
// add all our nonchecked edges to the wave
//up & down
for x := startpt.x - (left + 1); x <= (startpt.x + (right + 1)); x++ {
if x < bounds.Min.X || x >= bounds.Max.X {
continue
}
ix := x - bounds.Min.X
y := startpt.y - (up + 1)
if y >= bounds.Min.Y && y < bounds.Max.Y {
iy := y - bounds.Min.Y
if !checked[ix+(iy*w)] {
*wave = append(*wave, pxpt{x, y})
}
}
y = startpt.y + (down + 1)
if y >= bounds.Min.Y && y < bounds.Max.Y {
iy := y - bounds.Min.Y
if !checked[ix+(iy*w)] {
*wave = append(*wave, pxpt{x, y})
}
}
}
//left & right
for y := startpt.y - (up + 1); y <= (startpt.y + (down + 1)); y++ {
if y < bounds.Min.Y || y >= bounds.Max.Y {
continue
}
iy := y - bounds.Min.Y
x := startpt.x - (left + 1)
if x >= bounds.Min.X && x < bounds.Max.X {
ix := x - bounds.Min.X
if !checked[ix+(iy*w)] {
*wave = append(*wave, pxpt{x, y})
}
}
x = startpt.x + (right + 1)
if x >= bounds.Min.X && x < bounds.Max.X {
ix := x - bounds.Min.X
if !checked[ix+(iy*w)] {
*wave = append(*wave, pxpt{x, y})
}
}
}
}
func FftHalf(img *image.NRGBA, satval float64, twod bool) (*image.NRGBA, error) {
fmt.Printf("Floatizing\n")
// just an image in
r_real, g_real, b_real := floatize_chans(img, false, satval)
// parallelize
fft.SetWorkerPoolSize(0)
var r_cplx [][]complex128
var g_cplx [][]complex128
var b_cplx [][]complex128
fmt.Printf("fft r\n")
if twod {
r_cplx = fft.FFT2Real(r_real)
} else {
r_cplx = FftOneChan(r_real)
}
fmt.Printf("fft g\n")
if twod {
g_cplx = fft.FFT2Real(g_real)
} else {
g_cplx = FftOneChan(g_real)
}
fmt.Printf("fft b\n")
if twod {
b_cplx = fft.FFT2Real(b_real)
} else {
b_cplx = FftOneChan(b_real)
}
fmt.Printf("together\n")
// get an image with the mag on the left and the phase on the right
img = from_complex(r_cplx, g_cplx, b_cplx, true, satval, true)
return img, nil
}
func FftOneChan(real [][]float64) [][]complex128 {
cplx := make([][]complex128, len(real))