From 590d870f56eaefb43c6cf08f72f3dcba2b4ccd20 Mon Sep 17 00:00:00 2001 From: Yongwei Xing Date: Mon, 5 Jul 2021 15:07:08 +0800 Subject: [PATCH] support multiple images generation with time lapse for domainwrap --- arts/domainwrap.go | 33 +++++++++++++++++++++++++++++++++ example/example_domainwrap2.go | 30 ++++++++++++++++++++++++++++++ generativeart.go | 8 ++++++-- 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 example/example_domainwrap2.go diff --git a/arts/domainwrap.go b/arts/domainwrap.go index a1196cd..63c81f6 100644 --- a/arts/domainwrap.go +++ b/arts/domainwrap.go @@ -1,9 +1,11 @@ package arts import ( + "fmt" "github.com/jdxyw/generativeart" "github.com/jdxyw/generativeart/common" "image/color" + "log" ) // ColorMapping maps some parameters to color space. @@ -15,6 +17,12 @@ type domainWrap struct { scale2 float64 xOffset, yOffset float64 fn ColorMapping + // How many images would be created in this generation. + numImages int + // Use these parameters to create images with time lapse. + xOffsetStep, yOffsetStep float64 + // The imagPath for generative images. + imgPath string } // NewDomainWrap returns a domainWrap object. @@ -29,9 +37,34 @@ func NewDomainWrap(scale, scale2, xOffset, yOffset float64, cmap ColorMapping) * } } +func (d *domainWrap) SetDynamicParameter(xstep, ystep float64, n int, path string) { + d.xOffsetStep = xstep + d.yOffsetStep = ystep + d.numImages = n + d.imgPath = path +} + // Generative draws a domain warp image. // Reference: https://www.iquilezles.org/www/articles/warp/warp.htm func (d *domainWrap) Generative(c *generativeart.Canva) { + if d.numImages == 0 && len(d.imgPath) == 0 { + d.generative(c) + return + } + + if d.numImages > 0 && len(d.imgPath) == 0 { + log.Fatal("Missing the parameters numImages or imgPath") + } + for i := 0; i < d.numImages; i++ { + imgfile := fmt.Sprintf("%v/domainwrap%03d.PNG", d.imgPath, i) + d.xOffset += d.xOffsetStep * float64(i) + d.yOffset += d.yOffsetStep * float64(i) + d.generative(c) + c.ToPNG(imgfile) + } +} + +func (d *domainWrap) generative(c *generativeart.Canva) { for h := 0.0; h < float64(c.Height()); h += 1.0 { for w := 0.0; w < float64(c.Width()); w += 1.0 { r, m1, m2 := d.pattern(w*d.scale, h*d.scale, d.xOffset, d.yOffset) diff --git a/example/example_domainwrap2.go b/example/example_domainwrap2.go new file mode 100644 index 0000000..c727a4d --- /dev/null +++ b/example/example_domainwrap2.go @@ -0,0 +1,30 @@ +package main + +import ( + "github.com/jdxyw/generativeart" + "github.com/jdxyw/generativeart/arts" + "github.com/jdxyw/generativeart/common" + "image/color" + "math/rand" + "time" +) + +func cmap(r, m1, m2 float64) color.RGBA { + rgb := color.RGBA{ + R: uint8(common.Constrain(m1*200*r, 0, 255)), + G: uint8(common.Constrain(r*200, 0, 255)), + B: uint8(common.Constrain(m2*255*r, 70, 255)), + A: 255, + } + return rgb +} + +func main() { + rand.Seed(time.Now().Unix()) + c := generativeart.NewCanva(500, 500) + c.SetBackground(common.Black) + c.FillBackground() + d := arts.NewDomainWrap(0.01, 4, 4, 20, cmap) + d.SetDynamicParameter(0.005, 0, 100, "./temp") + c.Draw(d) +} diff --git a/generativeart.go b/generativeart.go index 9fc30a8..c3145c4 100644 --- a/generativeart.go +++ b/generativeart.go @@ -131,6 +131,10 @@ func (c *Canva) Draw(e Engine) { e.Generative(c) } +func (c *Canva) DrawTimelapse(e Engine) { + +} + // FillBackground fills the background of the Canva. func (c *Canva) FillBackground() { draw.Draw(c.Img(), c.Img().Bounds(), &image.Uniform{c.Opts().Background()}, image.ZP, draw.Src) @@ -142,12 +146,12 @@ func (c *Canva) ToPNG(fpath string) error { if err != nil { return err } - if err := png.Encode(f, c.Img()); err != nil { + if err = png.Encode(f, c.Img()); err != nil { f.Close() return err } - if err := f.Close(); err != nil { + if err = f.Close(); err != nil { return err }