Skip to content

Commit

Permalink
support multiple images generation with time lapse for domainwrap
Browse files Browse the repository at this point in the history
  • Loading branch information
Yongwei Xing committed Jul 5, 2021
1 parent b3ee1f8 commit 590d870
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
33 changes: 33 additions & 0 deletions arts/domainwrap.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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.
Expand All @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions example/example_domainwrap2.go
Original file line number Diff line number Diff line change
@@ -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)
}
8 changes: 6 additions & 2 deletions generativeart.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}

Expand Down

0 comments on commit 590d870

Please sign in to comment.