Skip to content

Commit

Permalink
Refactor drawing logic into separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanBaulch committed Mar 8, 2022
1 parent 8f8d556 commit b1ece1a
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 72 deletions.
66 changes: 66 additions & 0 deletions draw.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package main

import (
"image"
"image/color"

"github.com/StephaneBunel/bresenham"
"golang.org/x/image/font"
"golang.org/x/image/font/basicfont"
"golang.org/x/image/math/fixed"
)

func drawLine(im *image.Paletted, c uint8, x1, y1, x2, y2 int) {
setPixIfLower := func(x, y int, ci uint8) bool {
if (image.Point{X: x, Y: y}.In(im.Rect)) {
i := im.PixOffset(x, y)
if im.Pix[i] > ci {
im.Pix[i] = ci
return true
}
}
return false
}
setPix := func(x, y int, _ color.Color) {
if !setPixIfLower(x, y, c) {
return
}
if c < 0x80 {
c *= 2
setPixIfLower(x-1, y, c)
setPixIfLower(x, y-1, c)
setPixIfLower(x+1, y, c)
setPixIfLower(x, y+1, c)
}
if c < 0x80 {
c *= 2
setPixIfLower(x-1, y-1, c)
setPixIfLower(x-1, y+1, c)
setPixIfLower(x+1, y-1, c)
setPixIfLower(x+1, y+1, c)
}
}
bresenham.Bresenham(plotterFunc(setPix), x1, y1, x2, y2, nil)
}

type plotterFunc func(x, y int, c color.Color)

func (f plotterFunc) Set(x, y int, c color.Color) {
f(x, y, c)
}

func drawString(im *image.Paletted, c uint8, text string) {
d := &font.Drawer{
Dst: im,
Src: image.NewUniform(im.Palette[c]),
Face: basicfont.Face7x13,
}
b, _ := d.BoundString(text)
b = b.Sub(b.Min)
if b.In(fixed.R(0, 0, im.Bounds().Max.X-10, im.Rect.Max.Y-10)) {
d.Dot = fixed.P(im.Rect.Max.X, im.Rect.Max.Y).
Sub(b.Max.Sub(fixed.P(0, basicfont.Face7x13.Height))).
Sub(fixed.P(5, 5))
d.DrawString(text)
}
}
81 changes: 9 additions & 72 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,8 @@ import (
"strings"
"time"

"github.com/StephaneBunel/bresenham"
"github.com/kettek/apng"
"github.com/schollz/progressbar"
"golang.org/x/image/font"
"golang.org/x/image/font/basicfont"
"golang.org/x/image/math/fixed"
"golang.org/x/text/language"
"golang.org/x/text/message"
)
Expand Down Expand Up @@ -496,10 +492,6 @@ func render() error {
pal[i] = colors.GetColorAt(math.Sqrt(float64(i) / float64(0xff)))
}

watermark := "NathanBaulch/rainbow-roads"
if Version != "" {
watermark += " " + Version
}
bg := image.NewPaletted(image.Rect(0, 0, int(width), int(height)), pal)
for i := 0; i < len(bg.Pix); i += bg.Stride {
if i == 0 {
Expand All @@ -511,19 +503,11 @@ func render() error {
}
}
if !noWatermark {
d := &font.Drawer{
Dst: bg,
Src: image.NewUniform(pal[0x80]),
Face: basicfont.Face7x13,
}
b, _ := d.BoundString(watermark)
b = b.Sub(b.Min)
if b.In(fixed.R(0, 0, bg.Rect.Max.X-10, bg.Rect.Max.Y-10)) {
d.Dot = fixed.P(bg.Rect.Max.X, bg.Rect.Max.Y).
Sub(b.Max.Sub(fixed.P(0, basicfont.Face7x13.Height))).
Sub(fixed.P(5, 5))
d.DrawString(watermark)
text := "NathanBaulch/rainbow-roads"
if Version != "" {
text += " " + Version
}
drawString(bg, 0x80, text)
}

ims = make([]*image.Paletted, frames)
Expand All @@ -533,55 +517,14 @@ func render() error {
copy(im.Pix, bg.Pix)
fp := 1.2 * float64(f+1) / float64(frames)
for _, act := range activities {
var rprev *record
var rPrev *record
for _, r := range act.records {
pp := fp - r.p
if pp < 0 {
if pp := fp - r.p; pp < 0 {
break
} else if pp > 1 || (rprev != nil && r.x == rprev.x && r.y == rprev.y) {
rprev = r
continue
} else if pp < 1 && rPrev != nil && (r.x != rPrev.x || r.y != rPrev.y) {
drawLine(im, uint8(pp*0xff), rPrev.x, rPrev.y, r.x, r.y)
}
setPixIfLower := func(x, y int, ci uint8) bool {
if (image.Point{X: x, Y: y}.In(im.Rect)) {
i := im.PixOffset(x, y)
if im.Pix[i] > ci {
im.Pix[i] = ci
return true
}
}
return false
}
setPix := func(x, y int, _ color.Color) {
ci := uint8(pp * 0xff)
if !setPixIfLower(x, y, ci) {
return
}
if ci < 0x80 {
ci *= 2
setPixIfLower(x-1, y, ci)
setPixIfLower(x, y-1, ci)
setPixIfLower(x+1, y, ci)
setPixIfLower(x, y+1, ci)
}
if ci < 0x80 {
ci *= 2
setPixIfLower(x-1, y-1, ci)
setPixIfLower(x-1, y+1, ci)
setPixIfLower(x+1, y-1, ci)
setPixIfLower(x+1, y+1, ci)
}
}
if rprev != nil {
if dx, dy := r.x-rprev.x, r.y-rprev.y; dx < -1 || dx > 1 || dy < -1 || dy > 1 {
bresenham.Bresenham(plotterFunc(setPix), rprev.x, rprev.y, r.x, r.y, nil)
} else {
setPix(r.x, r.y, nil)
}
} else {
setPix(r.x, r.y, nil)
}
rprev = r
rPrev = r
}
}
ims[f] = im
Expand All @@ -590,12 +533,6 @@ func render() error {
return nil
}

type plotterFunc func(x, y int, c color.Color)

func (f plotterFunc) Set(x, y int, c color.Color) {
f(x, y, c)
}

func save() error {
if dir := filepath.Dir(output); dir != "." {
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
Expand Down

0 comments on commit b1ece1a

Please sign in to comment.