ebiten/internal/pixels/pixels.go

178 lines
4.3 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.
2016-07-24 19:28:59 +02:00
package pixels
2016-07-13 19:35:20 +02:00
import (
"image"
"image/color"
"github.com/hajimehoshi/ebiten/internal/graphics"
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
)
type drawImageHistoryItem struct {
image *graphics.Image
2016-07-13 19:35:20 +02:00
vertices []int16
geom graphics.Matrix
colorm graphics.Matrix
2016-07-13 19:35:20 +02:00
mode opengl.CompositeMode
}
2016-07-15 17:11:37 +02:00
// basePixels and baseColor are exclusive.
2016-07-24 19:28:59 +02:00
type Pixels struct {
2016-07-24 19:07:39 +02:00
image *graphics.Image
2016-07-13 20:29:19 +02:00
basePixels []uint8
2016-07-13 19:35:20 +02:00
baseColor color.Color
drawImageHistory []*drawImageHistoryItem
}
2016-07-24 19:28:59 +02:00
func NewPixels(image *graphics.Image) *Pixels {
return &Pixels{
image: image,
}
}
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
}
2016-07-24 19:28:59 +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
}
2016-07-24 19:28:59 +02:00
func (p *Pixels) IsCleared() bool {
2016-07-19 18:21:40 +02:00
return p.basePixels == nil && p.baseColor == nil && p.drawImageHistory == nil
}
2016-07-24 19:28:59 +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
}
2016-07-24 19:28:59 +02:00
func (p *Pixels) AppendDrawImageHistory(image *graphics.Image, vertices []int16, geom graphics.Matrix, colorm graphics.Matrix, mode opengl.CompositeMode) {
item := &drawImageHistoryItem{
image: image,
vertices: vertices,
geom: geom,
colorm: colorm,
mode: mode,
}
p.drawImageHistory = append(p.drawImageHistory, item)
2016-07-13 19:35:20 +02:00
}
2016-07-24 19:28:59 +02:00
func (p *Pixels) At(idx int, context *opengl.Context) (color.Color, error) {
2016-07-25 02:01:00 +02:00
if p.basePixels == nil || p.drawImageHistory != nil {
2016-07-13 19:35:20 +02:00
var err error
2016-07-23 18:50:22 +02:00
p.basePixels, err = p.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
}
2016-07-24 19:28:59 +02:00
func (p *Pixels) hasHistoryWith(target *graphics.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-25 02:10:51 +02:00
func (p *Pixels) Reset(context *opengl.Context) error {
var err error
2016-07-23 18:50:22 +02:00
p.basePixels, err = p.image.Pixels(context)
if err != nil {
return err
}
p.baseColor = nil
p.drawImageHistory = nil
return nil
}
2016-07-25 02:10:51 +02:00
func (p *Pixels) ResetIfNeeded(target *graphics.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-23 18:50:22 +02:00
p.basePixels, err = p.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
}
2016-07-24 19:28:59 +02:00
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-24 19:28:59 +02:00
func (p *Pixels) Restore(context *opengl.Context, width, height int, filter opengl.Filter) (*graphics.Image, error) {
2016-07-13 20:00:50 +02:00
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-24 19:28:59 +02:00
gimg, err := graphics.NewImageFromImage(img, 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.
/*if c.image.impl.hasHistory() {
2016-07-13 19:35:20 +02:00
panic("not reach")
}*/
if err := gimg.DrawImage(c.image, c.vertices, c.geom, c.colorm, c.mode); err != nil {
2016-07-13 19:35:20 +02:00
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
}