graphics: Add newImageImpl

This commit is contained in:
Hajime Hoshi 2016-07-05 00:24:06 +09:00
parent 006f918b8e
commit 085336d50f
3 changed files with 45 additions and 28 deletions

View File

@ -198,21 +198,18 @@ type DrawImageOptions struct {
//
// This function is concurrent-safe.
func NewImage(width, height int, filter Filter) (*Image, error) {
image := &imageImpl{
width: width,
height: height,
filter: filter,
}
var err error
image.image, err = graphics.NewImage(width, height, glFilter(filter))
i, err := graphics.NewImage(width, height, glFilter(filter))
if err != nil {
return nil, err
}
runtime.SetFinalizer(image, (*imageImpl).Dispose)
if err := image.image.Fill(color.Transparent); err != nil {
img, err := newImageImpl(i, filter)
if err != nil {
return nil, err
}
eimg, err := theImages.add(image)
if err := img.image.Fill(color.Transparent); err != nil {
return nil, err
}
eimg, err := theImages.add(img)
if err != nil {
return nil, err
}
@ -240,19 +237,13 @@ func NewImageFromImage(source image.Image, filter Filter) (*Image, error) {
for j := 0; j < h; j++ {
copy(pixels[j*w*4:(j+1)*w*4], rgbaImg.Pix[j*rgbaImg.Stride:])
}
img := &imageImpl{
width: w,
height: h,
filter: filter,
pixels: pixels,
}
var err error
img.image, err = graphics.NewImageFromImage(rgbaImg, glFilter(filter))
i, err := graphics.NewImageFromImage(rgbaImg, glFilter(filter))
if err != nil {
// TODO: texture should be removed here?
return nil, err
}
runtime.SetFinalizer(img, (*imageImpl).Dispose)
img, err := newImageImpl(i, filter)
img.pixels = pixels
eimg, err := theImages.add(img)
if err != nil {
return nil, err
@ -265,13 +256,11 @@ func newImageWithScreenFramebuffer(width, height int) (*Image, error) {
if err != nil {
return nil, err
}
img := &imageImpl{
image: i,
width: width,
height: height,
screen: true,
img, err := newImageImpl(i, FilterNearest)
if err != nil {
return nil, err
}
runtime.SetFinalizer(img, (*imageImpl).Dispose)
img.screen = true
eimg, err := theImages.add(img)
if err != nil {
return nil, err

View File

@ -40,6 +40,18 @@ type imageImpl struct {
m sync.Mutex
}
func newImageImpl(image *graphics.Image, filter Filter) (*imageImpl, error) {
w, h := image.Size()
i := &imageImpl{
image: image,
width: w,
height: h,
filter: filter,
}
runtime.SetFinalizer(i, (*imageImpl).Dispose)
return i, nil
}
func (i *imageImpl) Fill(clr color.Color) error {
i.m.Lock()
defer i.m.Unlock()

View File

@ -24,10 +24,15 @@ import (
type Image struct {
texture *texture
framebuffer *framebuffer
width int
height int
}
func NewImage(width, height int, filter opengl.Filter) (*Image, error) {
i := &Image{}
i := &Image{
width: width,
height: height,
}
c := &newImageCommand{
result: i,
width: width,
@ -39,7 +44,11 @@ func NewImage(width, height int, filter opengl.Filter) (*Image, error) {
}
func NewImageFromImage(img *image.RGBA, filter opengl.Filter) (*Image, error) {
i := &Image{}
s := img.Bounds().Size()
i := &Image{
width: s.X,
height: s.Y,
}
c := &newImageFromImageCommand{
result: i,
img: img,
@ -50,7 +59,10 @@ func NewImageFromImage(img *image.RGBA, filter opengl.Filter) (*Image, error) {
}
func NewScreenFramebufferImage(width, height int) (*Image, error) {
i := &Image{}
i := &Image{
width: width,
height: height,
}
c := &newScreenFramebufferImageCommand{
result: i,
width: width,
@ -68,6 +80,10 @@ func (i *Image) Dispose() error {
return nil
}
func (i *Image) Size() (int, int) {
return i.width, i.height
}
func (i *Image) Fill(clr color.Color) error {
c := &fillCommand{
dst: i,