From f056204a7cc34cda79cfcb9d489c6acc5ff1db9f Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 12 Jun 2016 01:45:40 +0900 Subject: [PATCH] graphics: Add image.go --- internal/graphics/command.go | 23 ++++++ internal/graphics/framebuffer.go | 63 ---------------- internal/graphics/image.go | 119 +++++++++++++++++++++++++++++++ internal/graphics/texture.go | 61 ---------------- 4 files changed, 142 insertions(+), 124 deletions(-) create mode 100644 internal/graphics/image.go diff --git a/internal/graphics/command.go b/internal/graphics/command.go index e822e7576..0485f7ee8 100644 --- a/internal/graphics/command.go +++ b/internal/graphics/command.go @@ -19,6 +19,7 @@ import ( "fmt" "image" "image/color" + "image/draw" "math" "github.com/hajimehoshi/ebiten/internal/graphics/opengl" @@ -168,6 +169,28 @@ type newImageFromImageCommand struct { filter opengl.Filter } +func adjustImageForTexture(img *image.RGBA) *image.RGBA { + width, height := img.Bounds().Size().X, img.Bounds().Size().Y + adjustedImageBounds := image.Rectangle{ + image.ZP, + image.Point{ + int(NextPowerOf2Int32(int32(width))), + int(NextPowerOf2Int32(int32(height))), + }, + } + if img.Bounds() == adjustedImageBounds { + return img + } + + adjustedImage := image.NewRGBA(adjustedImageBounds) + dstBounds := image.Rectangle{ + image.ZP, + img.Bounds().Size(), + } + draw.Draw(adjustedImage, dstBounds, img, img.Bounds().Min, draw.Src) + return adjustedImage +} + func (c *newImageFromImageCommand) Exec(context *opengl.Context) error { origSize := c.img.Bounds().Size() if origSize.X < 4 { diff --git a/internal/graphics/framebuffer.go b/internal/graphics/framebuffer.go index 06e779653..de20cf6a3 100644 --- a/internal/graphics/framebuffer.go +++ b/internal/graphics/framebuffer.go @@ -15,8 +15,6 @@ package graphics import ( - "image/color" - "github.com/hajimehoshi/ebiten/internal/graphics/opengl" ) @@ -42,17 +40,6 @@ type framebuffer struct { proMatrix *[4][4]float64 } -func NewZeroFramebufferImage(width, height int) (*Image, error) { - f := &framebuffer{ - width: width, - height: height, - flipY: true, - } - return &Image{ - framebuffer: f, - }, nil -} - func (f *framebuffer) initFromTexture(context *opengl.Context, texture *texture) error { native, err := context.NewFramebuffer(opengl.Texture(texture.native)) if err != nil { @@ -64,15 +51,6 @@ func (f *framebuffer) initFromTexture(context *opengl.Context, texture *texture) return nil } -func (i *Image) Dispose() error { - c := &disposeCommand{ - framebuffer: i.framebuffer, - texture: i.texture, - } - theCommandQueue.Enqueue(c) - return nil -} - const viewportSize = 4096 func (f *framebuffer) setAsViewport(c *opengl.Context) error { @@ -95,44 +73,3 @@ func (f *framebuffer) projectionMatrix() *[4][4]float64 { f.proMatrix = m return f.proMatrix } - -func (i *Image) Fill(clr color.Color) error { - c := &fillCommand{ - dst: i.framebuffer, - color: clr, - } - theCommandQueue.Enqueue(c) - return nil -} - -func (i *Image) DrawImage(src *Image, vertices []int16, geo, clr Matrix, mode opengl.CompositeMode) error { - c := &drawImageCommand{ - dst: i.framebuffer, - src: src.texture, - vertices: vertices, - geo: geo, - color: clr, - mode: mode, - } - theCommandQueue.Enqueue(c) - return nil -} - -func (i *Image) Pixels(context *opengl.Context) ([]uint8, error) { - // Flush the enqueued commands so that pixels are certainly read. - if err := theCommandQueue.Flush(context); err != nil { - return nil, err - } - f := i.framebuffer - return context.FramebufferPixels(f.native, f.width, f.height) -} - -func (i *Image) ReplacePixels(p []uint8) error { - c := &replacePixelsCommand{ - dst: i.framebuffer, - texture: i.texture, - pixels: p, - } - theCommandQueue.Enqueue(c) - return nil -} diff --git a/internal/graphics/image.go b/internal/graphics/image.go new file mode 100644 index 000000000..d14f9af68 --- /dev/null +++ b/internal/graphics/image.go @@ -0,0 +1,119 @@ +// Copyright 2016 Hajime Hoshi +// +// 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 + +import ( + "image" + "image/color" + + "github.com/hajimehoshi/ebiten/internal/graphics/opengl" +) + +type Image struct { + texture *texture + framebuffer *framebuffer +} + +func NewImage(width, height int, filter opengl.Filter) (*Image, error) { + i := &Image{ + texture: &texture{}, + framebuffer: &framebuffer{}, + } + c := &newImageCommand{ + texture: i.texture, + framebuffer: i.framebuffer, + width: width, + height: height, + filter: filter, + } + theCommandQueue.Enqueue(c) + return i, nil +} + +func NewImageFromImage(img *image.RGBA, filter opengl.Filter) (*Image, error) { + i := &Image{ + texture: &texture{}, + framebuffer: &framebuffer{}, + } + c := &newImageFromImageCommand{ + texture: i.texture, + framebuffer: i.framebuffer, + img: img, + filter: filter, + } + theCommandQueue.Enqueue(c) + return i, nil +} + +func NewZeroFramebufferImage(width, height int) (*Image, error) { + f := &framebuffer{ + width: width, + height: height, + flipY: true, + } + return &Image{ + framebuffer: f, + }, nil +} + +func (i *Image) Dispose() error { + c := &disposeCommand{ + framebuffer: i.framebuffer, + texture: i.texture, + } + theCommandQueue.Enqueue(c) + return nil +} + +func (i *Image) Fill(clr color.Color) error { + c := &fillCommand{ + dst: i.framebuffer, + color: clr, + } + theCommandQueue.Enqueue(c) + return nil +} + +func (i *Image) DrawImage(src *Image, vertices []int16, geo, clr Matrix, mode opengl.CompositeMode) error { + c := &drawImageCommand{ + dst: i.framebuffer, + src: src.texture, + vertices: vertices, + geo: geo, + color: clr, + mode: mode, + } + theCommandQueue.Enqueue(c) + return nil +} + +func (i *Image) Pixels(context *opengl.Context) ([]uint8, error) { + // Flush the enqueued commands so that pixels are certainly read. + if err := theCommandQueue.Flush(context); err != nil { + return nil, err + } + f := i.framebuffer + return context.FramebufferPixels(f.native, f.width, f.height) +} + +func (i *Image) ReplacePixels(p []uint8) error { + c := &replacePixelsCommand{ + dst: i.framebuffer, + texture: i.texture, + pixels: p, + } + theCommandQueue.Enqueue(c) + return nil +} diff --git a/internal/graphics/texture.go b/internal/graphics/texture.go index 4a1472380..9e644a421 100644 --- a/internal/graphics/texture.go +++ b/internal/graphics/texture.go @@ -15,72 +15,11 @@ package graphics import ( - "image" - "image/draw" - "github.com/hajimehoshi/ebiten/internal/graphics/opengl" ) -func adjustImageForTexture(img *image.RGBA) *image.RGBA { - width, height := img.Bounds().Size().X, img.Bounds().Size().Y - adjustedImageBounds := image.Rectangle{ - image.ZP, - image.Point{ - int(NextPowerOf2Int32(int32(width))), - int(NextPowerOf2Int32(int32(height))), - }, - } - if img.Bounds() == adjustedImageBounds { - return img - } - - adjustedImage := image.NewRGBA(adjustedImageBounds) - dstBounds := image.Rectangle{ - image.ZP, - img.Bounds().Size(), - } - draw.Draw(adjustedImage, dstBounds, img, img.Bounds().Min, draw.Src) - return adjustedImage -} - -type Image struct { - texture *texture - framebuffer *framebuffer -} - type texture struct { native opengl.Texture width int height int } - -func NewImage(width, height int, filter opengl.Filter) (*Image, error) { - i := &Image{ - texture: &texture{}, - framebuffer: &framebuffer{}, - } - c := &newImageCommand{ - texture: i.texture, - framebuffer: i.framebuffer, - width: width, - height: height, - filter: filter, - } - theCommandQueue.Enqueue(c) - return i, nil -} - -func NewImageFromImage(img *image.RGBA, filter opengl.Filter) (*Image, error) { - i := &Image{ - texture: &texture{}, - framebuffer: &framebuffer{}, - } - c := &newImageFromImageCommand{ - texture: i.texture, - framebuffer: i.framebuffer, - img: img, - filter: filter, - } - theCommandQueue.Enqueue(c) - return i, nil -}