Added shaded renderer.

This commit is contained in:
Sascha L. Teichmann 2014-09-11 17:35:03 +02:00
parent 458fc5d1da
commit 9fdf06d671
2 changed files with 60 additions and 2 deletions

View File

@ -7,6 +7,7 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"image"
"image/color" "image/color"
"log" "log"
@ -21,6 +22,7 @@ func main() {
width, height, depth int width, height, depth int
colorsfile string colorsfile string
outfile string outfile string
shaded bool
) )
flag.IntVar(&port, "port", 6379, "port to of mtredisalize server") flag.IntVar(&port, "port", 6379, "port to of mtredisalize server")
@ -38,6 +40,7 @@ func main() {
flag.StringVar(&colorsfile, "colors", "colors.txt", "definition of colors") flag.StringVar(&colorsfile, "colors", "colors.txt", "definition of colors")
flag.StringVar(&outfile, "output", "out.png", "image file of result") flag.StringVar(&outfile, "output", "out.png", "image file of result")
flag.StringVar(&outfile, "o", "out.png", "image file of result (shorthand)") flag.StringVar(&outfile, "o", "out.png", "image file of result (shorthand)")
flag.BoolVar(&shaded, "shaded", true, "draw relief")
flag.Parse() flag.Parse()
@ -86,8 +89,15 @@ func main() {
} }
} }
image := renderer.CreateImage( var image image.Image
if shaded {
image = renderer.CreateShadedImage(
colors.Colors, color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff}) colors.Colors, color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff})
} else {
image = renderer.CreateImage(
colors.Colors, color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff})
}
if err = SaveAsPNG(outfile, image); err != nil { if err = SaveAsPNG(outfile, image); err != nil {
log.Fatalf("writing image failed: %s", err) log.Fatalf("writing image failed: %s", err)

View File

@ -125,3 +125,51 @@ func (r *Renderer) CreateImage(colors []color.RGBA, background color.RGBA) *imag
} }
return image return image
} }
func safeColor(x int32) uint8 {
if x < 0 {
x = 0
} else if x > 255 {
x = 255
}
return uint8(x)
}
func (r *Renderer) CreateShadedImage(colors []color.RGBA, background color.RGBA) *image.RGBA {
pw, ph := r.width<<4, r.height<<4
image := image.NewRGBA(image.Rect(0, 0, pw, ph))
ofs, numCols := 0, int32(len(colors))
for z := ph - 1; z >= 0; z-- {
for x := 0; x < pw; x++ {
colIdx := r.cBuffer[ofs]
if colIdx < 0 || colIdx >= numCols {
image.Set(x, z, background)
} else {
var y, y1, y2 int32
y = r.yBuffer[ofs]
if x == 0 {
y1 = y
} else {
y1 = r.yBuffer[ofs-1]
}
if z == 0 {
y2 = y
} else {
y2 = r.yBuffer[ofs+pw]
}
d := ((y - y1) + (y - y2)) * 12
if d > 36 {
d = 36
}
col := colors[colIdx]
image.Set(x, z, color.RGBA{
R: safeColor(int32(col.R) + d),
G: safeColor(int32(col.G) + d),
B: safeColor(int32(col.B) + d),
A: 0xff})
}
ofs++
}
}
return image
}