ebiten/pixels.go

144 lines
3.5 KiB
Go
Raw Normal View History

2016-07-13 19:35:20 +02:00
// Copyright 2016 Hajime Hoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ebiten
import (
"image"
"image/color"
"github.com/hajimehoshi/ebiten/internal/graphics"
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
)
type drawImageHistoryItem struct {
image *Image
vertices []int16
geom GeoM
colorm ColorM
mode opengl.CompositeMode
}
type pixels struct {
2016-07-13 19:35:20 +02:00
pixels []uint8
baseColor color.Color
drawImageHistory []*drawImageHistoryItem
}
func (p *pixels) resetWithPixels(pixels []uint8) {
if p.pixels == nil {
p.pixels = make([]uint8, len(pixels))
2016-07-13 19:35:20 +02:00
}
copy(p.pixels, pixels)
p.baseColor = nil
p.drawImageHistory = nil
2016-07-13 19:35:20 +02:00
}
func (p *pixels) clear() {
p.pixels = nil
p.baseColor = nil
p.drawImageHistory = nil
2016-07-13 19:35:20 +02:00
}
func (p *pixels) fill(clr color.Color) {
p.pixels = nil
p.baseColor = clr
p.drawImageHistory = nil
2016-07-13 19:35:20 +02:00
}
func (p *pixels) appendDrawImageHistory(item *drawImageHistoryItem) {
p.drawImageHistory = append(p.drawImageHistory, item)
2016-07-13 19:35:20 +02:00
}
2016-07-13 20:00:50 +02:00
func (p *pixels) at(image *graphics.Image, idx int, context *opengl.Context) (color.Color, error) {
if p.pixels == nil || p.drawImageHistory != nil {
2016-07-13 19:35:20 +02:00
var err error
2016-07-13 20:00:50 +02:00
p.pixels, err = image.Pixels(context)
2016-07-13 19:35:20 +02:00
if err != nil {
return nil, err
}
p.baseColor = nil
p.drawImageHistory = nil
2016-07-13 19:35:20 +02:00
}
r, g, b, a := p.pixels[idx], p.pixels[idx+1], p.pixels[idx+2], p.pixels[idx+3]
2016-07-13 19:35:20 +02:00
return color.RGBA{r, g, b, a}, nil
}
func (p *pixels) hasHistoryWith(target *Image) bool {
for _, c := range p.drawImageHistory {
2016-07-13 19:35:20 +02:00
if c.image == target {
return true
}
}
return false
}
2016-07-13 20:00:50 +02:00
func (p *pixels) resetHistoryIfNeeded(image *graphics.Image, target *Image, context *opengl.Context) error {
if p.drawImageHistory == nil {
2016-07-13 19:35:20 +02:00
return nil
}
if !p.hasHistoryWith(target) {
2016-07-13 19:35:20 +02:00
return nil
}
var err error
2016-07-13 20:00:50 +02:00
p.pixels, err = image.Pixels(context)
2016-07-13 19:35:20 +02:00
if err != nil {
return err
}
p.baseColor = nil
p.drawImageHistory = nil
2016-07-13 19:35:20 +02:00
return nil
}
func (p *pixels) hasHistory() bool {
return p.drawImageHistory != nil
2016-07-13 19:35:20 +02:00
}
2016-07-13 20:00:50 +02:00
func (p *pixels) restore(context *opengl.Context, width, height int, filter Filter) (*graphics.Image, error) {
img := image.NewRGBA(image.Rect(0, 0, width, height))
if p.pixels != nil {
2016-07-13 20:00:50 +02:00
for j := 0; j < height; j++ {
copy(img.Pix[j*img.Stride:], p.pixels[j*width*4:(j+1)*width*4])
2016-07-13 19:35:20 +02:00
}
} else if p.baseColor != nil {
r32, g32, b32, a32 := p.baseColor.RGBA()
2016-07-13 19:35:20 +02:00
r, g, b, a := uint8(r32), uint8(g32), uint8(b32), uint8(a32)
for idx := 0; idx < len(img.Pix)/4; idx++ {
img.Pix[4*idx] = r
img.Pix[4*idx+1] = g
img.Pix[4*idx+2] = b
img.Pix[4*idx+3] = a
}
}
2016-07-13 20:00:50 +02:00
gimg, err := graphics.NewImageFromImage(img, glFilter(filter))
2016-07-13 19:35:20 +02:00
if err != nil {
return nil, err
}
for _, c := range p.drawImageHistory {
2016-07-13 19:35:20 +02:00
if c.image.impl.hasHistory() {
panic("not reach")
}
if err := gimg.DrawImage(c.image.impl.image, c.vertices, &c.geom, &c.colorm, c.mode); err != nil {
return nil, err
}
}
p.pixels, err = gimg.Pixels(context)
2016-07-13 19:35:20 +02:00
if err != nil {
return nil, err
}
p.baseColor = nil
p.drawImageHistory = nil
2016-07-13 19:35:20 +02:00
return gimg, nil
}