graphics: Refactoring

This commit is contained in:
Hajime Hoshi 2018-10-28 01:39:12 +09:00
parent 5f04f05cb1
commit 170f0fcaa9

View File

@ -53,7 +53,7 @@ func (m *mipmap) original() *shareable.Image {
func (m *mipmap) level(r image.Rectangle, level int) *shareable.Image { func (m *mipmap) level(r image.Rectangle, level int) *shareable.Image {
if level == 0 { if level == 0 {
return m.orig panic("not reached")
} }
imgs, ok := m.imgs[r] imgs, ok := m.imgs[r]
@ -70,7 +70,13 @@ func (m *mipmap) level(r image.Rectangle, level int) *shareable.Image {
} }
for len(imgs) < idx+1 { for len(imgs) < idx+1 {
src := m.level(r, len(imgs)) l := len(imgs)
var src *shareable.Image
if l > 0 {
src = m.level(r, l)
} else {
src = m.orig
}
w2 := w / 2 w2 := w / 2
h2 := h / 2 h2 := h / 2
if w2 == 0 || h2 == 0 { if w2 == 0 || h2 == 0 {
@ -83,7 +89,7 @@ func (m *mipmap) level(r image.Rectangle, level int) *shareable.Image {
s = shareable.NewImage(w2, h2) s = shareable.NewImage(w2, h2)
} }
var vs []float32 var vs []float32
if len(imgs) == 0 { if l == 0 {
vs = src.QuadVertices(r.Min.X, r.Min.Y, r.Max.X, r.Max.Y, 0.5, 0, 0, 0.5, 0, 0, 1, 1, 1, 1) vs = src.QuadVertices(r.Min.X, r.Min.Y, r.Max.X, r.Max.Y, 0.5, 0, 0, 0.5, 0, 0, 1, 1, 1, 1)
} else { } else {
vs = src.QuadVertices(0, 0, w, h, 0.5, 0, 0, 0.5, 0, 0, 1, 1, 1, 1) vs = src.QuadVertices(0, 0, w, h, 0.5, 0, 0, 0.5, 0, 0, 1, 1, 1, 1)
@ -364,37 +370,32 @@ func (i *Image) drawImage(img *Image, options *DrawImageOptions) {
level = 6 level = 6
} }
// TODO: Move this logic to mipmap? // TODO: Add (*mipmap).drawImage and move the below code.
if src := img.mipmap.level(image.Rect(sx0, sy0, sx1, sy1), level); src != nil { colorm := options.ColorM.impl
colorm := options.ColorM.impl cr, cg, cb, ca := float32(1), float32(1), float32(1), float32(1)
cr, cg, cb, ca := float32(1), float32(1), float32(1), float32(1) if colorm.ScaleOnly() {
if colorm.ScaleOnly() { body, _ := colorm.UnsafeElements()
body, _ := colorm.UnsafeElements() cr = body[0]
cr = body[0] cg = body[5]
cg = body[5] cb = body[10]
cb = body[10] ca = body[15]
ca = body[15] colorm = nil
} }
var vs []float32
// TODO: This is too tricky: src is a whole image when level == 0, if level == 0 {
// or a part of the image when level > 0. Do refactoring. src := img.mipmap.original()
if level == 0 { vs := src.QuadVertices(sx0, sy0, sx1, sy1, a, b, c, d, tx, ty, cr, cg, cb, ca)
vs = src.QuadVertices(sx0, sy0, sx1, sy1, a, b, c, d, tx, ty, cr, cg, cb, ca) is := graphicsutil.QuadIndices()
} else { i.mipmap.original().DrawImage(src, vs, is, colorm, mode, filter)
w, h := src.Size() } else if src := img.mipmap.level(image.Rect(sx0, sy0, sx1, sy1), level); src != nil {
s := 1 << uint(level) w, h := src.Size()
a *= float32(s) s := 1 << uint(level)
b *= float32(s) a *= float32(s)
c *= float32(s) b *= float32(s)
d *= float32(s) c *= float32(s)
vs = src.QuadVertices(0, 0, w, h, a, b, c, d, tx, ty, cr, cg, cb, ca) d *= float32(s)
} vs := src.QuadVertices(0, 0, w, h, a, b, c, d, tx, ty, cr, cg, cb, ca)
is := graphicsutil.QuadIndices() is := graphicsutil.QuadIndices()
if colorm.ScaleOnly() {
colorm = nil
}
i.mipmap.original().DrawImage(src, vs, is, colorm, mode, filter) i.mipmap.original().DrawImage(src, vs, is, colorm, mode, filter)
} }
i.disposeMipmaps() i.disposeMipmaps()