Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Small input images should be used (like 256x256px). You don't need the detail an
| `s` | 1024 | output image size |
| `a` | 128 | color alpha (use `0` to let the algorithm choose alpha for each shape) |
| `bg` | avg | starting background color (hex) |
| `blur` | 0 | making SVG blured, adds blur filter witn N deviation. 0 - disabled. |
| `j` | 0 | number of parallel workers (default uses all cores) |
| `v` | off | verbose output |
| `vv` | off | very verbose output |
Expand Down
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import (
"strings"
"time"

"github.com/fogleman/primitive/primitive"
"github.com/ramainen/primitive/primitive"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for my intervention, but it looks like there's a typo.

"github.com/nfnt/resize"
)

var (
Input string
Outputs flagArray
BlurFilter int
Background string
Configs shapeConfigArray
Alpha int
Expand Down Expand Up @@ -65,6 +66,7 @@ func init() {
flag.StringVar(&Input, "i", "", "input image path")
flag.Var(&Outputs, "o", "output image path")
flag.Var(&Configs, "n", "number of primitives")
flag.IntVar(&BlurFilter, "blur", 0, "make N blur (for svg, 0 - disabled)")
flag.StringVar(&Background, "bg", "", "background color (hex)")
flag.IntVar(&Alpha, "a", 128, "alpha value")
flag.IntVar(&InputSize, "r", 256, "resize large input images to this size")
Expand Down Expand Up @@ -153,7 +155,7 @@ func main() {
}

// run algorithm
model := primitive.NewModel(input, bg, OutputSize, Workers)
model := primitive.NewModel(input, bg, OutputSize, Workers, BlurFilter)
primitive.Log(1, "%d: t=%.3f, score=%.6f\n", 0, 0.0, model.Score)
start := time.Now()
frame := 0
Expand Down
14 changes: 12 additions & 2 deletions primitive/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Model struct {
Sw, Sh int
Scale float64
Background Color
BlurFilter int
Target *image.RGBA
Current *image.RGBA
Context *gg.Context
Expand All @@ -22,7 +23,7 @@ type Model struct {
Workers []*Worker
}

func NewModel(target image.Image, background Color, size, numWorkers int) *Model {
func NewModel(target image.Image, background Color, size, numWorkers int, blurFilter int) *Model {
w := target.Bounds().Size().X
h := target.Bounds().Size().Y
aspect := float64(w) / float64(h)
Expand All @@ -43,6 +44,7 @@ func NewModel(target image.Image, background Color, size, numWorkers int) *Model
model.Sh = sh
model.Scale = scale
model.Background = background
model.BlurFilter = blurFilter
model.Target = imageToRGBA(target)
model.Current = uniformRGBA(target.Bounds(), background.NRGBA())
model.Score = differenceFull(model.Target, model.Current)
Expand Down Expand Up @@ -87,8 +89,16 @@ func (model *Model) SVG() string {
bg := model.Background
var lines []string
lines = append(lines, fmt.Sprintf("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"%d\" height=\"%d\">", model.Sw, model.Sh))
if model.BlurFilter != 0 {
lines = append(lines, fmt.Sprintf("<defs><filter id=\"f1\" x=\"0\" y=\"0\"><feGaussianBlur in=\"SourceGraphic\" stdDeviation=\"%d\" /></filter></defs>", model.BlurFilter))
}
lines = append(lines, fmt.Sprintf("<rect x=\"0\" y=\"0\" width=\"%d\" height=\"%d\" fill=\"#%02x%02x%02x\" />", model.Sw, model.Sh, bg.R, bg.G, bg.B))
lines = append(lines, fmt.Sprintf("<g transform=\"scale(%f) translate(0.5 0.5)\">", model.Scale))
if model.BlurFilter != 0 {
lines = append(lines, fmt.Sprintf("<g transform=\"scale(%f) translate(0.5 0.5)\" filter=\"url(#f1)\">", model.Scale))
} else {
lines = append(lines, fmt.Sprintf("<g transform=\"scale(%f) translate(0.5 0.5)\">", model.Scale))
}

for i, shape := range model.Shapes {
c := model.Colors[i]
attrs := "fill=\"#%02x%02x%02x\" fill-opacity=\"%f\""
Expand Down