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

View File

@ -40,6 +40,18 @@ type imageImpl struct {
m sync.Mutex 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 { func (i *imageImpl) Fill(clr color.Color) error {
i.m.Lock() i.m.Lock()
defer i.m.Unlock() defer i.m.Unlock()

View File

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