ebiten/pixels.go

149 lines
3.7 KiB
Go
Raw Normal View History

2016-07-16 21:41:28 +02:00
// Copyright 2016 The Ebiten Authors
2016-07-13 19:35:20 +02:00
//
// 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
}
2016-07-15 17:11:37 +02:00
// basePixels and baseColor are exclusive.
type pixels struct {
2016-07-13 20:29:19 +02:00
basePixels []uint8
2016-07-13 19:35:20 +02:00
baseColor color.Color
drawImageHistory []*drawImageHistoryItem
}
func (p *pixels) resetWithPixels(pixels []uint8) {
2016-07-13 20:29:19 +02:00
if p.basePixels == nil {
p.basePixels = make([]uint8, len(pixels))
2016-07-13 19:35:20 +02:00
}
2016-07-13 20:29:19 +02:00
copy(p.basePixels, pixels)
p.baseColor = nil
p.drawImageHistory = nil
2016-07-13 19:35:20 +02:00
}
func (p *pixels) clear() {
2016-07-13 20:29:19 +02:00
p.basePixels = nil
p.baseColor = nil
p.drawImageHistory = nil
2016-07-13 19:35:20 +02:00
}
func (p *pixels) fill(clr color.Color) {
2016-07-13 20:29:19 +02:00
p.basePixels = 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) {
2016-07-13 20:29:19 +02:00
if p.basePixels == nil || p.drawImageHistory != nil {
2016-07-13 19:35:20 +02:00
var err error
2016-07-13 20:29:19 +02:00
p.basePixels, 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
}
2016-07-13 20:29:19 +02:00
r, g, b, a := p.basePixels[idx], p.basePixels[idx+1], p.basePixels[idx+2], p.basePixels[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:29:19 +02:00
p.basePixels, 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:29:19 +02:00
// restore restores the pixels using its history.
//
// restore is the only function that the pixel data is not present on GPU when this is called.
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))
2016-07-13 20:29:19 +02:00
if p.basePixels != nil {
2016-07-13 20:00:50 +02:00
for j := 0; j < height; j++ {
2016-07-13 20:29:19 +02:00
copy(img.Pix[j*img.Stride:], p.basePixels[j*width*4:(j+1)*width*4])
2016-07-13 19:35:20 +02:00
}
}
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
}
2016-07-15 17:11:37 +02:00
if p.baseColor != nil {
if p.basePixels != nil {
panic("not reach")
}
if err := gimg.Fill(p.baseColor); err != nil {
return nil, err
}
}
for _, c := range p.drawImageHistory {
2016-07-13 20:29:19 +02:00
// c.image.impl must be already restored.
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
}
}
2016-07-13 20:29:19 +02:00
p.basePixels, 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
}