└── primer └── prime.go /primer/prime.go: -------------------------------------------------------------------------------- 1 | package primer 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | "path/filepath" 7 | "strings" 8 | "time" 9 | 10 | "github.com/fogleman/primitive/primitive" 11 | "github.com/nfnt/resize" 12 | ) 13 | 14 | type OnIterationDone interface { 15 | Do(int) 16 | } 17 | type JsonDummy struct { 18 | hello string 19 | meaning int 20 | } 21 | 22 | func Bench() { 23 | for i := 0; i < 10000000; i++ { 24 | _, _ = json.Marshal(JsonDummy{hello: "world", meaning: 42}) 25 | } 26 | fmt.Print("Done\n") 27 | } 28 | 29 | func Process(name string, iters int, size int, workers int, mode int, iterdone OnIterationDone) string { 30 | out := "" 31 | //files, err := ioutil.ReadDir(name) 32 | //if err != nil { 33 | //log.Fatal(err) 34 | //} 35 | 36 | //for _, file := range files { 37 | //println(file.Name()) 38 | //out = out + file.Name() 39 | //} 40 | if workers <= 0 { 41 | workers = 1 42 | } 43 | if size <= 0 { 44 | size = 250 45 | } 46 | if mode <= 0 { 47 | mode = 1 48 | } 49 | 50 | OutputSize := 1024 51 | 52 | Outputs := []string{name + ".out.png"} 53 | Alpha := 128 54 | Number := iters 55 | 56 | name = strings.Replace(name, "file://", "", -1) 57 | input, err := primitive.LoadImage(name) 58 | if err != nil { 59 | fmt.Printf("Error: %v", err) 60 | iterdone.Do(-1) 61 | } 62 | 63 | input = resize.Thumbnail(uint(size), uint(size), input, resize.Bilinear) 64 | 65 | if err != nil { 66 | fmt.Printf("Error: %v", err) 67 | } 68 | bg := primitive.MakeColor(primitive.AverageImageColor(input)) 69 | fmt.Printf("Input: ok") 70 | 71 | model := primitive.NewModel(input, bg, OutputSize, workers) 72 | fmt.Printf("iteration %d, time %.3f, score %.6f\n", 0, 0.0, model.Score) 73 | start := time.Now() 74 | for i := 1; i <= Number; i++ { 75 | // find optimal shape and add it to the model 76 | model.Step(primitive.ShapeType(mode), Alpha, 1) 77 | elapsed := time.Since(start).Seconds() 78 | fmt.Printf("iteration %d, time %.3f, score %.6f\n", i, elapsed, model.Score) 79 | iterdone.Do(i) 80 | 81 | // write output image(s) 82 | for _, output := range Outputs { 83 | ext := strings.ToLower(filepath.Ext(output)) 84 | saveFrames := strings.Contains(output, "%") && ext != ".gif" 85 | if saveFrames || i == Number { 86 | path := output 87 | if saveFrames { 88 | path = fmt.Sprintf(output, i) 89 | } 90 | fmt.Printf("writing %s\n", path) 91 | switch ext { 92 | default: 93 | fmt.Errorf("unrecognized file extension: %s", ext) 94 | case ".png": 95 | fmt.Printf("saving: %s", path) 96 | primitive.SavePNG(path, model.Context.Image()) 97 | //case ".jpg", ".jpeg": 98 | //check(primitive.SaveJPG(path, model.Context.Image(), 95)) 99 | //case ".svg": 100 | //check(primitive.SaveFile(path, model.SVG())) 101 | //case ".gif": 102 | //frames := model.Frames(0.001) 103 | //check(primitive.SaveGIFImageMagick(path, frames, 50, 250)) 104 | //} 105 | } 106 | } 107 | } 108 | } 109 | 110 | return out 111 | } 112 | --------------------------------------------------------------------------------