support multiple images generation with time lapse for domainwrap

This commit is contained in:
Yongwei Xing 2021-07-05 15:07:08 +08:00
parent b3ee1f8c84
commit 590d870f56
3 changed files with 69 additions and 2 deletions

View file

@ -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)

View file

@ -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)
}

View file

@ -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
}