2015-07-26 16:44:51 +02:00
|
|
|
// Copyright 2014, 2015 by Sascha L. Teichmann
|
2014-09-10 01:21:55 +02:00
|
|
|
// Use of this source code is governed by the MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
2014-09-12 20:22:34 +02:00
|
|
|
package common
|
2014-09-10 01:21:55 +02:00
|
|
|
|
|
|
|
import (
|
2014-09-12 12:37:27 +02:00
|
|
|
"container/heap"
|
2014-09-10 18:58:12 +02:00
|
|
|
"image"
|
|
|
|
"image/color"
|
2015-07-22 01:11:14 +02:00
|
|
|
"image/draw"
|
2014-09-10 01:21:55 +02:00
|
|
|
"math"
|
|
|
|
)
|
|
|
|
|
2014-09-14 11:41:25 +02:00
|
|
|
type Area struct {
|
|
|
|
X1, Z1 int16
|
|
|
|
X2, Z2 int16
|
|
|
|
}
|
|
|
|
|
2014-09-10 01:21:55 +02:00
|
|
|
type Renderer struct {
|
2014-10-26 17:09:23 +01:00
|
|
|
width int
|
|
|
|
height int
|
|
|
|
xOfs int16
|
|
|
|
zOfs int16
|
|
|
|
yBuffer []int32
|
|
|
|
yMin []int32
|
|
|
|
cBuffer []int32
|
|
|
|
RejectedBlocks int
|
|
|
|
SolidBlocks int
|
|
|
|
TransparentBlocks int
|
2014-10-26 18:36:47 +01:00
|
|
|
spans *SpanPool
|
|
|
|
tBuffer []*Span
|
2014-09-10 01:21:55 +02:00
|
|
|
}
|
|
|
|
|
2014-09-12 12:37:27 +02:00
|
|
|
type YOrder struct {
|
|
|
|
Renderer *Renderer
|
2014-09-12 20:22:34 +02:00
|
|
|
blocks []*Block
|
2014-09-12 12:37:27 +02:00
|
|
|
capacity int
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewYOrder(renderer *Renderer, capacity int) *YOrder {
|
|
|
|
return &YOrder{
|
|
|
|
Renderer: renderer,
|
2014-09-12 20:22:34 +02:00
|
|
|
blocks: make([]*Block, 0, capacity),
|
2014-09-12 12:37:27 +02:00
|
|
|
capacity: capacity}
|
|
|
|
}
|
|
|
|
|
2014-09-14 14:57:49 +02:00
|
|
|
func (yo *YOrder) Reset() {
|
|
|
|
yo.blocks = yo.blocks[0:0]
|
|
|
|
}
|
|
|
|
|
2014-09-12 12:37:27 +02:00
|
|
|
func copyData(data []byte) []byte {
|
|
|
|
l := len(data)
|
2015-07-26 12:32:59 +02:00
|
|
|
ndata := make([]byte, l, Max(l, 8*1024))
|
2014-09-12 12:37:27 +02:00
|
|
|
copy(ndata, data)
|
|
|
|
return ndata
|
|
|
|
}
|
|
|
|
|
2014-10-19 21:05:38 +02:00
|
|
|
func (yo *YOrder) RenderBlock(block *Block, colors *Colors) (err error) {
|
2014-09-12 20:22:34 +02:00
|
|
|
var nblock *Block
|
2014-09-12 12:37:27 +02:00
|
|
|
if len(yo.blocks) == yo.capacity {
|
|
|
|
oblock := yo.blocks[0]
|
|
|
|
if oblock.Coord.Y < block.Coord.Y {
|
|
|
|
// New one is above highest old. Directly render new.
|
2014-10-19 21:05:38 +02:00
|
|
|
err = yo.Renderer.RenderBlock(block, colors)
|
2014-09-12 12:37:27 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Render old one. Store copy of new in heap.
|
|
|
|
heap.Pop(yo)
|
2014-10-19 21:05:38 +02:00
|
|
|
err = yo.Renderer.RenderBlock(oblock, colors)
|
2014-09-12 12:37:27 +02:00
|
|
|
l := len(block.Data)
|
|
|
|
if cap(oblock.Data) < l {
|
2015-07-26 12:32:59 +02:00
|
|
|
oblock.Data = make([]byte, l, Max(l, 8*1024))
|
2014-09-12 12:37:27 +02:00
|
|
|
} else {
|
|
|
|
oblock.Data = oblock.Data[0:l]
|
|
|
|
}
|
|
|
|
copy(oblock.Data, block.Data)
|
|
|
|
oblock.Coord = block.Coord
|
|
|
|
nblock = oblock
|
|
|
|
} else {
|
2014-09-12 20:22:34 +02:00
|
|
|
nblock = &Block{Coord: block.Coord, Data: copyData(block.Data)}
|
2014-09-12 12:37:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
heap.Push(yo, nblock)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-10-19 21:05:38 +02:00
|
|
|
func (yo *YOrder) Drain(colors *Colors) (err error) {
|
2014-09-12 12:37:27 +02:00
|
|
|
for len(yo.blocks) > 0 {
|
2014-10-19 21:05:38 +02:00
|
|
|
if err = yo.Renderer.RenderBlock(heap.Pop(yo).(*Block), colors); err != nil {
|
2014-09-12 12:37:27 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (yo *YOrder) Len() int {
|
|
|
|
return len(yo.blocks)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (yo *YOrder) Swap(i, j int) {
|
|
|
|
yo.blocks[i], yo.blocks[j] = yo.blocks[j], yo.blocks[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (yo *YOrder) Less(i, j int) bool {
|
|
|
|
// Reverse order intented.
|
|
|
|
return yo.blocks[i].Coord.Y > yo.blocks[j].Coord.Y
|
|
|
|
}
|
|
|
|
|
|
|
|
func (yo *YOrder) Push(x interface{}) {
|
2014-09-12 20:22:34 +02:00
|
|
|
yo.blocks = append(yo.blocks, x.(*Block))
|
2014-09-12 12:37:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (yo *YOrder) Pop() (x interface{}) {
|
|
|
|
l := len(yo.blocks)
|
|
|
|
x = yo.blocks[l-1]
|
|
|
|
yo.blocks = yo.blocks[0 : l-1]
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
2014-10-26 18:36:47 +01:00
|
|
|
func NewRenderer(width, height int, transparent bool) (renderer *Renderer) {
|
2014-09-10 01:21:55 +02:00
|
|
|
dim := width * height
|
|
|
|
pixSize := dim * 16 * 16
|
|
|
|
yBuffer := make([]int32, pixSize)
|
|
|
|
cBuffer := make([]int32, pixSize)
|
2014-09-11 22:20:04 +02:00
|
|
|
yMin := make([]int32, dim)
|
2014-09-10 01:21:55 +02:00
|
|
|
|
2014-10-26 18:36:47 +01:00
|
|
|
var tBuffer []*Span
|
|
|
|
var spans *SpanPool
|
|
|
|
|
|
|
|
if transparent {
|
|
|
|
tBuffer = make([]*Span, pixSize)
|
|
|
|
spans = NewSpanPool()
|
|
|
|
}
|
|
|
|
|
2014-09-10 01:21:55 +02:00
|
|
|
renderer = &Renderer{
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
yBuffer: yBuffer,
|
2014-09-11 22:38:01 +02:00
|
|
|
cBuffer: cBuffer,
|
2014-10-26 18:36:47 +01:00
|
|
|
yMin: yMin,
|
|
|
|
tBuffer: tBuffer,
|
|
|
|
spans: spans}
|
2014-09-14 14:57:49 +02:00
|
|
|
|
|
|
|
renderer.Reset()
|
2014-09-10 01:21:55 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-14 14:57:49 +02:00
|
|
|
func (r *Renderer) SetPos(xOfs, zOfs int16) {
|
|
|
|
r.xOfs = xOfs
|
|
|
|
r.zOfs = zOfs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Renderer) Reset() {
|
|
|
|
for i, n := 0, len(r.yBuffer); i < n; i++ {
|
|
|
|
r.yBuffer[i] = math.MinInt32
|
|
|
|
r.cBuffer[i] = -1
|
|
|
|
}
|
|
|
|
for i, n := 0, len(r.yMin); i < n; i++ {
|
|
|
|
r.yMin[i] = math.MinInt32
|
|
|
|
}
|
2014-10-26 18:36:47 +01:00
|
|
|
|
2014-10-26 22:36:57 +01:00
|
|
|
if r.TransparentBlocks > 0 {
|
|
|
|
r.TransparentBlocks = 0
|
2014-10-26 18:36:47 +01:00
|
|
|
for i, t := range r.tBuffer {
|
|
|
|
if t != nil {
|
|
|
|
r.spans.FreeAll(t)
|
|
|
|
r.tBuffer[i] = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-10-26 22:36:57 +01:00
|
|
|
r.SolidBlocks = 0
|
|
|
|
r.RejectedBlocks = 0
|
2014-09-14 14:57:49 +02:00
|
|
|
}
|
|
|
|
|
2014-09-11 02:28:09 +02:00
|
|
|
func (r *Renderer) IsFilled() bool {
|
2014-09-14 11:41:25 +02:00
|
|
|
for _, y := range r.yMin {
|
|
|
|
if y == math.MinInt32 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
2014-09-11 02:28:09 +02:00
|
|
|
}
|
|
|
|
|
2015-07-22 01:11:14 +02:00
|
|
|
func (r *Renderer) IsEmpty() bool {
|
|
|
|
return r.SolidBlocks == 0 && r.TransparentBlocks == 0
|
|
|
|
}
|
|
|
|
|
2014-10-19 21:05:38 +02:00
|
|
|
func (r *Renderer) RenderBlock(block *Block, colors *Colors) (err error) {
|
2014-09-10 23:49:27 +02:00
|
|
|
|
2014-09-10 18:58:12 +02:00
|
|
|
bx := block.Coord.X - r.xOfs
|
|
|
|
bz := block.Coord.Z - r.zOfs
|
2014-09-11 22:20:04 +02:00
|
|
|
|
2014-09-10 01:21:55 +02:00
|
|
|
// We do not need to render the block if the whole 16x16 area
|
|
|
|
// is already filled and the block is strictly below.
|
2014-09-10 23:49:27 +02:00
|
|
|
blockY := int32(block.Coord.Y) << 4
|
2014-09-11 22:20:04 +02:00
|
|
|
pos := int(bz)*r.width + int(bx)
|
|
|
|
if blockY < r.yMin[pos] {
|
2014-10-26 17:09:23 +01:00
|
|
|
r.RejectedBlocks++
|
2014-09-10 01:21:55 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-11 22:20:04 +02:00
|
|
|
// Decoding is pretty expensive so do it that late.
|
2014-09-10 01:21:55 +02:00
|
|
|
var db *DecodedBlock
|
2014-10-19 21:05:38 +02:00
|
|
|
if db, err = NewDecodedBlock(block.Data, colors); err != nil {
|
2014-09-10 01:21:55 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-07-20 13:27:46 +02:00
|
|
|
if db.AirOnly() {
|
2014-10-26 17:09:23 +01:00
|
|
|
r.RejectedBlocks++
|
2014-10-19 17:15:33 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-11 22:38:01 +02:00
|
|
|
w := r.width << 4
|
|
|
|
ofs := int(bz)*w<<4 + int(bx)<<4
|
2014-09-14 11:41:25 +02:00
|
|
|
yB := r.yBuffer
|
2014-09-11 22:20:04 +02:00
|
|
|
yMin := int32(math.MaxInt32)
|
2014-10-26 18:36:47 +01:00
|
|
|
|
|
|
|
if db.Transparent && r.tBuffer != nil {
|
|
|
|
r.TransparentBlocks++
|
|
|
|
|
|
|
|
for z := 0; z < 16; z++ {
|
|
|
|
for x := 0; x < 16; x++ {
|
|
|
|
currentY := yB[ofs]
|
|
|
|
if currentY < blockY {
|
|
|
|
for y := 15; y >= 0; y-- {
|
|
|
|
if c, ok := db.Content(x, y, z); ok {
|
2014-10-26 20:39:53 +01:00
|
|
|
cY := blockY + int32(y)
|
2014-10-26 18:36:47 +01:00
|
|
|
|
|
|
|
if colors.IsTransparent(c) {
|
2014-10-26 20:39:53 +01:00
|
|
|
r.tBuffer[ofs] = r.spans.Insert(r.tBuffer[ofs], cY, c)
|
2014-10-26 18:36:47 +01:00
|
|
|
} else {
|
|
|
|
r.cBuffer[ofs] = c
|
2014-10-26 20:39:53 +01:00
|
|
|
currentY = cY
|
2014-10-26 18:36:47 +01:00
|
|
|
yB[ofs] = currentY
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2014-09-10 23:49:27 +02:00
|
|
|
}
|
|
|
|
}
|
2014-10-26 18:36:47 +01:00
|
|
|
if currentY < yMin {
|
|
|
|
yMin = currentY
|
|
|
|
}
|
|
|
|
ofs++
|
2014-09-10 01:21:55 +02:00
|
|
|
}
|
2014-10-26 18:36:47 +01:00
|
|
|
ofs += w - 16
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
r.SolidBlocks++
|
|
|
|
for z := 0; z < 16; z++ {
|
|
|
|
for x := 0; x < 16; x++ {
|
|
|
|
currentY := yB[ofs]
|
|
|
|
if currentY < blockY {
|
|
|
|
for y := 15; y >= 0; y-- {
|
|
|
|
if c, ok := db.Content(x, y, z); ok {
|
|
|
|
r.cBuffer[ofs] = c
|
|
|
|
currentY = blockY + int32(y)
|
|
|
|
yB[ofs] = currentY
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if currentY < yMin {
|
|
|
|
yMin = currentY
|
|
|
|
}
|
|
|
|
ofs++
|
2014-09-11 22:20:04 +02:00
|
|
|
}
|
2014-10-26 18:36:47 +01:00
|
|
|
ofs += w - 16
|
2014-09-10 01:21:55 +02:00
|
|
|
}
|
|
|
|
}
|
2014-10-26 18:36:47 +01:00
|
|
|
|
2014-09-11 22:20:04 +02:00
|
|
|
r.yMin[pos] = yMin
|
2014-09-10 01:21:55 +02:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2014-09-10 18:58:12 +02:00
|
|
|
|
2014-09-14 11:41:25 +02:00
|
|
|
func (a Area) Contains(x, z int16) bool {
|
|
|
|
return x >= a.X1 && x <= a.X2 && z >= a.Z1 && z <= a.Z2
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a Area) IsHigher() bool {
|
|
|
|
return a.Z2-a.Z1 > a.X2-a.X1
|
|
|
|
}
|
|
|
|
|
|
|
|
func areasContain(areas []Area, x, z int16) bool {
|
|
|
|
for _, r := range areas {
|
|
|
|
if r.Contains(x, z) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Greedy algorithm to figure out a list of disjunct areas
|
|
|
|
// of free regions in the domain to the (x, z) block plane.
|
|
|
|
// oldAreas are searched and found free areas are appended
|
|
|
|
// to newAreas which ist return.
|
|
|
|
// This is useful to spatial query only blocks from db
|
|
|
|
// that are not below already rendered blocks.
|
|
|
|
func (r *Renderer) UncoveredAreas(newAreas, oldAreas []Area) []Area {
|
|
|
|
yM := r.yMin
|
|
|
|
|
|
|
|
// Scan old areas.
|
|
|
|
for _, oldArea := range oldAreas {
|
|
|
|
for z := oldArea.Z1; z <= oldArea.Z2; z++ {
|
|
|
|
row := z * int16(r.width)
|
|
|
|
for x := oldArea.X1; x <= oldArea.X2; x++ {
|
|
|
|
// Uncovered and not in list of new areas?
|
|
|
|
if yM[row+x] > math.MinInt32 || areasContain(newAreas, x, z) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
area := Area{X1: x, Z1: z, X2: x, Z2: z}
|
|
|
|
// Try to extend the area in x and/or z till no further extension is possible.
|
|
|
|
for extendDirs := 1 | 2; extendDirs != 0; {
|
|
|
|
var xFirst bool
|
|
|
|
// Try to extend in the direction with most gain
|
|
|
|
// of blocks.
|
|
|
|
if area.IsHigher() { // Higher means to win more blocks in x direction.
|
|
|
|
xFirst = true
|
|
|
|
}
|
|
|
|
dirs:
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
if xFirst {
|
|
|
|
// Extension in x possible?
|
|
|
|
if extendDirs&1 == 1 {
|
|
|
|
nx := area.X2 + 1
|
|
|
|
if nx >= int16(r.width) {
|
|
|
|
extendDirs &= ^1
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Scan line below current area if its fully free.
|
|
|
|
for nz := area.Z1; nz <= area.Z2; nz++ {
|
|
|
|
if yM[nz*int16(r.width)+nx] > math.MinInt32 || areasContain(newAreas, nx, nz) {
|
|
|
|
extendDirs &= ^1
|
|
|
|
continue dirs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// free -> extend
|
|
|
|
area.X2 = nx
|
|
|
|
}
|
|
|
|
} else if extendDirs&2 == 2 {
|
|
|
|
// Symmetric case in z direction
|
|
|
|
nz := area.Z2 + 1
|
|
|
|
if nz >= int16(r.height) {
|
|
|
|
extendDirs &= ^2
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Scan line right beside the area if its free.
|
|
|
|
row2 := nz * int16(r.width)
|
|
|
|
for nx := area.X1; nx <= area.X2; nx++ {
|
|
|
|
if yM[row2+nx] > math.MinInt32 || areasContain(newAreas, nx, nz) {
|
|
|
|
extendDirs &= ^2
|
|
|
|
continue dirs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
area.Z2 = nz
|
|
|
|
}
|
|
|
|
// Switch to other search direction (x -> z or z -> x)
|
|
|
|
xFirst = !xFirst
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// At this point the area is extended to max.
|
|
|
|
newAreas = append(newAreas, area)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newAreas
|
|
|
|
}
|
|
|
|
|
2014-09-10 18:58:12 +02:00
|
|
|
func (r *Renderer) CreateImage(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))
|
2014-09-10 23:49:27 +02:00
|
|
|
for z := ph - 1; z >= 0; z-- {
|
2014-09-10 18:58:12 +02:00
|
|
|
for x := 0; x < pw; x++ {
|
|
|
|
colIdx := r.cBuffer[ofs]
|
|
|
|
if colIdx >= 0 && colIdx < numCols {
|
|
|
|
image.Set(x, z, colors[colIdx])
|
|
|
|
} else {
|
|
|
|
image.Set(x, z, background)
|
|
|
|
}
|
|
|
|
ofs++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return image
|
|
|
|
}
|
2014-09-11 17:35:03 +02:00
|
|
|
|
|
|
|
func safeColor(x int32) uint8 {
|
2014-09-12 10:45:36 +02:00
|
|
|
switch {
|
|
|
|
case x < 0:
|
|
|
|
return 0
|
|
|
|
case x > 255:
|
|
|
|
return 255
|
|
|
|
default:
|
|
|
|
return uint8(x)
|
2014-09-11 17:35:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-22 01:11:14 +02:00
|
|
|
func BackgroundImage(width, height int, bg color.RGBA) *image.RGBA {
|
|
|
|
m := image.NewRGBA(image.Rect(0, 0, width, height))
|
|
|
|
draw.Draw(m, m.Bounds(), &image.Uniform{bg}, image.ZP, draw.Src)
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2014-09-12 17:10:05 +02:00
|
|
|
func (r *Renderer) CreateShadedImage(
|
|
|
|
xOfs, zOfs, width, height int,
|
2014-10-26 18:36:47 +01:00
|
|
|
cols *Colors, background color.RGBA) *image.RGBA {
|
2014-09-12 17:10:05 +02:00
|
|
|
|
|
|
|
image := image.NewRGBA(image.Rect(0, 0, width, height))
|
|
|
|
|
|
|
|
pw := r.width << 4
|
|
|
|
|
2014-10-26 18:36:47 +01:00
|
|
|
cs := cols.Colors
|
|
|
|
|
|
|
|
ofs, numCols := zOfs*pw+xOfs, int32(len(cs))
|
2014-09-12 17:10:05 +02:00
|
|
|
|
|
|
|
stride := pw - width
|
|
|
|
|
2014-09-16 00:08:31 +02:00
|
|
|
istride := image.Stride + 4*width
|
|
|
|
|
|
|
|
iofs := image.PixOffset(0, height-1)
|
|
|
|
|
|
|
|
pix := image.Pix
|
|
|
|
|
2014-10-26 22:36:57 +01:00
|
|
|
if r.TransparentBlocks > 0 { // Fast path for transparent images.
|
2014-10-26 18:36:47 +01:00
|
|
|
for z := height - 1; z >= 0; z-- {
|
|
|
|
for x := 0; x < width; x++ {
|
|
|
|
colIdx := r.cBuffer[ofs]
|
|
|
|
if colIdx < 0 || colIdx >= numCols {
|
|
|
|
pix[iofs] = background.R
|
|
|
|
pix[iofs+1] = background.G
|
|
|
|
pix[iofs+2] = background.B
|
|
|
|
pix[iofs+3] = 0xff
|
2014-09-11 17:35:03 +02:00
|
|
|
} else {
|
2014-10-28 11:53:18 +01:00
|
|
|
y := r.yBuffer[ofs]
|
|
|
|
t := r.tBuffer[ofs]
|
|
|
|
|
|
|
|
opaque := t == nil || t.Top() < y
|
|
|
|
|
|
|
|
var y1, y2 int32
|
|
|
|
|
2014-10-26 18:36:47 +01:00
|
|
|
if x == 0 {
|
|
|
|
y1 = y
|
|
|
|
} else {
|
|
|
|
y1 = r.yBuffer[ofs-1]
|
2014-10-28 11:53:18 +01:00
|
|
|
if opaque {
|
|
|
|
if s := r.tBuffer[ofs-1]; s != nil {
|
|
|
|
y1 = max32(y1, s.Top())
|
|
|
|
}
|
|
|
|
}
|
2014-10-26 18:36:47 +01:00
|
|
|
}
|
|
|
|
if z == 0 {
|
|
|
|
y2 = y
|
|
|
|
} else {
|
|
|
|
y2 = r.yBuffer[ofs+pw]
|
2014-10-28 11:53:18 +01:00
|
|
|
if opaque {
|
|
|
|
if s := r.tBuffer[ofs+pw]; s != nil {
|
|
|
|
y1 = max32(y1, s.Top())
|
|
|
|
}
|
|
|
|
}
|
2014-10-26 18:36:47 +01:00
|
|
|
}
|
|
|
|
d := ((y - y1) + (y - y2)) * 12
|
|
|
|
if d > 36 {
|
|
|
|
d = 36
|
|
|
|
}
|
|
|
|
col := cs[colIdx]
|
|
|
|
col = color.RGBA{
|
|
|
|
R: safeColor(int32(col.R) + d),
|
|
|
|
G: safeColor(int32(col.G) + d),
|
|
|
|
B: safeColor(int32(col.B) + d),
|
|
|
|
A: 0xff}
|
2014-10-28 11:53:18 +01:00
|
|
|
if !opaque {
|
|
|
|
col = cols.BlendColors(t, col, y)
|
2014-10-26 18:36:47 +01:00
|
|
|
}
|
|
|
|
pix[iofs] = col.R
|
|
|
|
pix[iofs+1] = col.G
|
|
|
|
pix[iofs+2] = col.B
|
|
|
|
pix[iofs+3] = col.A
|
2014-09-11 17:35:03 +02:00
|
|
|
}
|
2014-10-26 18:36:47 +01:00
|
|
|
iofs += 4
|
|
|
|
ofs++
|
|
|
|
}
|
|
|
|
ofs += stride
|
|
|
|
iofs -= istride
|
|
|
|
}
|
|
|
|
|
|
|
|
} else { // Solid images.
|
|
|
|
for z := height - 1; z >= 0; z-- {
|
|
|
|
for x := 0; x < width; x++ {
|
|
|
|
colIdx := r.cBuffer[ofs]
|
|
|
|
if colIdx < 0 || colIdx >= numCols {
|
|
|
|
pix[iofs] = background.R
|
|
|
|
pix[iofs+1] = background.G
|
|
|
|
pix[iofs+2] = background.B
|
|
|
|
pix[iofs+3] = 0xff
|
2014-09-11 17:35:03 +02:00
|
|
|
} else {
|
2014-10-26 18:36:47 +01:00
|
|
|
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 := cs[colIdx]
|
|
|
|
pix[iofs] = safeColor(int32(col.R) + d)
|
|
|
|
pix[iofs+1] = safeColor(int32(col.G) + d)
|
|
|
|
pix[iofs+2] = safeColor(int32(col.B) + d)
|
|
|
|
pix[iofs+3] = 0xff
|
2014-09-11 17:35:03 +02:00
|
|
|
}
|
2014-10-26 18:36:47 +01:00
|
|
|
iofs += 4
|
|
|
|
ofs++
|
2014-09-11 17:35:03 +02:00
|
|
|
}
|
2014-10-26 18:36:47 +01:00
|
|
|
ofs += stride
|
|
|
|
iofs -= istride
|
2014-09-11 17:35:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return image
|
|
|
|
}
|