mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 18:58:54 +01:00
graphics: Make copying images faster
This commit is contained in:
parent
8cb1af138e
commit
6b4801ac7a
@ -19,7 +19,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"image"
|
"image"
|
||||||
"image/color"
|
"image/color"
|
||||||
"image/draw"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
@ -77,10 +76,7 @@ func newImageImplFromImage(source image.Image, filter Filter) (*imageImpl, error
|
|||||||
// It is necessary to copy the source image since the actual construction of
|
// It is necessary to copy the source image since the actual construction of
|
||||||
// an image is delayed and we can't expect the source image is not modified
|
// an image is delayed and we can't expect the source image is not modified
|
||||||
// until the construction.
|
// until the construction.
|
||||||
origImg := source
|
rgbaImg := graphics.CopyImage(source)
|
||||||
newImg := image.NewRGBA(image.Rect(0, 0, w, h))
|
|
||||||
draw.Draw(newImg, newImg.Bounds(), origImg, origImg.Bounds().Min, draw.Src)
|
|
||||||
rgbaImg := newImg
|
|
||||||
p := make([]uint8, 4*w*h)
|
p := make([]uint8, 4*w*h)
|
||||||
for j := 0; j < h; j++ {
|
for j := 0; j < h; j++ {
|
||||||
copy(p[j*w*4:(j+1)*w*4], rgbaImg.Pix[j*rgbaImg.Stride:])
|
copy(p[j*w*4:(j+1)*w*4], rgbaImg.Pix[j*rgbaImg.Stride:])
|
||||||
|
@ -17,11 +17,51 @@ package graphics
|
|||||||
import (
|
import (
|
||||||
"image"
|
"image"
|
||||||
"image/color"
|
"image/color"
|
||||||
|
"image/draw"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/internal/affine"
|
"github.com/hajimehoshi/ebiten/internal/affine"
|
||||||
"github.com/hajimehoshi/ebiten/internal/opengl"
|
"github.com/hajimehoshi/ebiten/internal/opengl"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func CopyImage(origImg image.Image) *image.RGBA {
|
||||||
|
size := origImg.Bounds().Size()
|
||||||
|
w, h := size.X, size.Y
|
||||||
|
newImg := image.NewRGBA(image.Rect(0, 0, w, h))
|
||||||
|
switch origImg := origImg.(type) {
|
||||||
|
case *image.Paletted:
|
||||||
|
b := origImg.Bounds()
|
||||||
|
x0 := b.Min.X
|
||||||
|
y0 := b.Min.Y
|
||||||
|
x1 := b.Max.X
|
||||||
|
y1 := b.Max.Y
|
||||||
|
palette := make([]color.RGBA, len(origImg.Palette))
|
||||||
|
for i, c := range origImg.Palette {
|
||||||
|
palette[i] = color.RGBAModel.Convert(c).(color.RGBA)
|
||||||
|
}
|
||||||
|
index0 := y0*origImg.Stride + x0
|
||||||
|
index1 := 0
|
||||||
|
d0 := origImg.Stride - (x1 - x0)
|
||||||
|
d1 := newImg.Stride - (x1-x0)*4
|
||||||
|
for j := 0; j < y1-y0; j++ {
|
||||||
|
for i := 0; i < x1-x0; i++ {
|
||||||
|
p := origImg.Pix[index0]
|
||||||
|
c := palette[p]
|
||||||
|
newImg.Pix[index1] = c.R
|
||||||
|
newImg.Pix[index1+1] = c.G
|
||||||
|
newImg.Pix[index1+2] = c.B
|
||||||
|
newImg.Pix[index1+3] = c.A
|
||||||
|
index0++
|
||||||
|
index1 += 4
|
||||||
|
}
|
||||||
|
index0 += d0
|
||||||
|
index1 += d1
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
draw.Draw(newImg, newImg.Bounds(), origImg, origImg.Bounds().Min, draw.Src)
|
||||||
|
}
|
||||||
|
return newImg
|
||||||
|
}
|
||||||
|
|
||||||
type Image struct {
|
type Image struct {
|
||||||
texture *texture
|
texture *texture
|
||||||
framebuffer *framebuffer
|
framebuffer *framebuffer
|
||||||
|
92
internal/graphics/image_test.go
Normal file
92
internal/graphics/image_test.go
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
// Copyright 2016 The Ebiten Authors
|
||||||
|
//
|
||||||
|
// 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 graphics_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image"
|
||||||
|
"image/color"
|
||||||
|
"image/color/palette"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
. "github.com/hajimehoshi/ebiten/internal/graphics"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCopyImage(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
In image.Image
|
||||||
|
Out *image.RGBA
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
In: &image.Paletted{
|
||||||
|
Pix: []uint8{0, 1, 1, 0},
|
||||||
|
Stride: 2,
|
||||||
|
Rect: image.Rect(0, 0, 2, 2),
|
||||||
|
Palette: color.Palette([]color.Color{
|
||||||
|
color.Transparent, color.White,
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
Out: &image.RGBA{
|
||||||
|
Pix: []uint8{0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0},
|
||||||
|
Stride: 8,
|
||||||
|
Rect: image.Rect(0, 0, 2, 2),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
In: &image.RGBA{
|
||||||
|
Pix: []uint8{0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0},
|
||||||
|
Stride: 8,
|
||||||
|
Rect: image.Rect(0, 0, 2, 2),
|
||||||
|
},
|
||||||
|
Out: &image.RGBA{
|
||||||
|
Pix: []uint8{0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0},
|
||||||
|
Stride: 8,
|
||||||
|
Rect: image.Rect(0, 0, 2, 2),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, c := range cases {
|
||||||
|
got := CopyImage(c.In)
|
||||||
|
if got.Rect != c.Out.Rect {
|
||||||
|
t.Errorf("Rect: %v, want: %v", got.Rect, c.Out.Rect)
|
||||||
|
}
|
||||||
|
size := got.Rect.Size()
|
||||||
|
w, h := size.X, size.Y
|
||||||
|
for j := 0; j < h; j++ {
|
||||||
|
for i := 0; i < w; i++ {
|
||||||
|
got := got.At(i, j)
|
||||||
|
want := c.Out.At(i, j)
|
||||||
|
if got != want {
|
||||||
|
t.Errorf("At(%d, %d): %v, want: %v", i, j, got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkCopyImageRGBA(b *testing.B) {
|
||||||
|
img := image.NewRGBA(image.Rect(0, 0, 4096, 4096))
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
CopyImage(img)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkCopyImagePaletted(b *testing.B) {
|
||||||
|
img := image.NewPaletted(image.Rect(0, 0, 4096, 4096), palette.Plan9)
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
CopyImage(img)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user