2022-03-19 17:24:47 +01:00
|
|
|
// Copyright 2022 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 ui
|
|
|
|
|
|
|
|
import (
|
2022-10-19 17:46:44 +02:00
|
|
|
"fmt"
|
2023-04-27 17:55:10 +02:00
|
|
|
"image"
|
2023-02-18 10:34:06 +01:00
|
|
|
"math"
|
2022-10-19 17:46:44 +02:00
|
|
|
|
2022-06-07 16:57:56 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/atlas"
|
2022-03-19 17:24:47 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/mipmap"
|
|
|
|
)
|
|
|
|
|
2022-03-20 08:51:23 +01:00
|
|
|
// panicOnErrorOnReadingPixels indicates whether reading pixels panics on an error or not.
|
2022-03-20 08:26:26 +01:00
|
|
|
// This value is set only on testing.
|
2022-03-20 08:51:23 +01:00
|
|
|
var panicOnErrorOnReadingPixels bool
|
2022-03-20 08:26:26 +01:00
|
|
|
|
2022-03-20 08:51:23 +01:00
|
|
|
func SetPanicOnErrorOnReadingPixelsForTesting(value bool) {
|
|
|
|
panicOnErrorOnReadingPixels = value
|
2022-03-20 08:26:26 +01:00
|
|
|
}
|
|
|
|
|
2022-10-20 20:11:13 +02:00
|
|
|
const bigOffscreenScale = 2
|
|
|
|
|
2022-03-19 17:24:47 +01:00
|
|
|
type Image struct {
|
2023-10-14 17:06:07 +02:00
|
|
|
ui *UserInterface
|
|
|
|
|
2022-10-19 17:46:44 +02:00
|
|
|
mipmap *mipmap.Mipmap
|
|
|
|
width int
|
|
|
|
height int
|
|
|
|
imageType atlas.ImageType
|
2022-09-28 16:44:27 +02:00
|
|
|
|
2023-10-20 18:24:46 +02:00
|
|
|
// lastBlend is the lastly-used blend for mipmap.Image.
|
|
|
|
lastBlend graphicsdriver.Blend
|
|
|
|
|
2022-10-19 17:46:44 +02:00
|
|
|
// bigOffscreenBuffer is a double-sized offscreen for anti-alias rendering.
|
2023-02-17 15:56:00 +01:00
|
|
|
bigOffscreenBuffer *bigOffscreenImage
|
2022-10-23 18:51:37 +02:00
|
|
|
|
2022-12-20 03:34:35 +01:00
|
|
|
// modifyCallback is a callback called when DrawTriangles or WritePixels is called.
|
|
|
|
// modifyCallback is useful to detect whether the image is manipulated or not after a certain time.
|
|
|
|
modifyCallback func()
|
2022-12-02 13:04:03 +01:00
|
|
|
|
2023-02-17 15:56:00 +01:00
|
|
|
tmpVerticesForFill []float32
|
2022-03-19 17:24:47 +01:00
|
|
|
}
|
|
|
|
|
2023-10-14 17:06:07 +02:00
|
|
|
func (u *UserInterface) NewImage(width, height int, imageType atlas.ImageType) *Image {
|
2022-03-19 17:24:47 +01:00
|
|
|
return &Image{
|
2023-10-14 17:06:07 +02:00
|
|
|
ui: u,
|
2022-10-19 17:46:44 +02:00
|
|
|
mipmap: mipmap.New(width, height, imageType),
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
imageType: imageType,
|
2023-10-20 18:24:46 +02:00
|
|
|
lastBlend: graphicsdriver.BlendSourceOver,
|
2022-03-19 17:24:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-03 07:06:22 +01:00
|
|
|
func (i *Image) Deallocate() {
|
2022-04-02 17:21:21 +02:00
|
|
|
if i.mipmap == nil {
|
|
|
|
return
|
|
|
|
}
|
2022-10-19 17:46:44 +02:00
|
|
|
if i.bigOffscreenBuffer != nil {
|
2023-11-03 07:06:22 +01:00
|
|
|
i.bigOffscreenBuffer.deallocate()
|
2022-10-19 17:46:44 +02:00
|
|
|
}
|
2023-11-03 07:06:22 +01:00
|
|
|
i.mipmap.Deallocate()
|
2022-03-19 17:24:47 +01:00
|
|
|
}
|
|
|
|
|
2024-06-09 19:02:47 +02:00
|
|
|
func (i *Image) DrawTriangles(srcs [graphics.ShaderSrcImageCount]*Image, vertices []float32, indices []uint32, blend graphicsdriver.Blend, dstRegion image.Rectangle, srcRegions [graphics.ShaderSrcImageCount]image.Rectangle, shader *Shader, uniforms []uint32, fillRule graphicsdriver.FillRule, canSkipMipmap bool, antialias bool) {
|
2022-12-20 03:34:35 +01:00
|
|
|
if i.modifyCallback != nil {
|
|
|
|
i.modifyCallback()
|
2022-10-28 12:07:33 +02:00
|
|
|
}
|
2022-10-23 18:51:37 +02:00
|
|
|
|
2023-10-20 18:24:46 +02:00
|
|
|
i.lastBlend = blend
|
|
|
|
|
2022-10-19 17:46:44 +02:00
|
|
|
if antialias {
|
|
|
|
if i.bigOffscreenBuffer == nil {
|
|
|
|
var imageType atlas.ImageType
|
|
|
|
switch i.imageType {
|
|
|
|
case atlas.ImageTypeRegular, atlas.ImageTypeUnmanaged:
|
|
|
|
imageType = atlas.ImageTypeUnmanaged
|
|
|
|
case atlas.ImageTypeScreen, atlas.ImageTypeVolatile:
|
|
|
|
imageType = atlas.ImageTypeVolatile
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("ui: unexpected image type: %d", imageType))
|
|
|
|
}
|
2023-10-14 17:06:07 +02:00
|
|
|
i.bigOffscreenBuffer = i.ui.newBigOffscreenImage(i, imageType)
|
2022-10-19 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
2024-05-03 17:26:37 +02:00
|
|
|
i.bigOffscreenBuffer.drawTriangles(srcs, vertices, indices, blend, dstRegion, srcRegions, shader, uniforms, fillRule, canSkipMipmap)
|
2022-10-19 17:46:44 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-16 12:47:00 +02:00
|
|
|
i.flushBufferIfNeeded()
|
2022-09-28 16:44:27 +02:00
|
|
|
|
2024-06-09 19:02:47 +02:00
|
|
|
var srcMipmaps [graphics.ShaderSrcImageCount]*mipmap.Mipmap
|
2022-03-19 17:24:47 +01:00
|
|
|
for i, src := range srcs {
|
|
|
|
if src == nil {
|
|
|
|
continue
|
|
|
|
}
|
2022-10-16 12:47:00 +02:00
|
|
|
src.flushBufferIfNeeded()
|
2022-03-19 17:24:47 +01:00
|
|
|
srcMipmaps[i] = src.mipmap
|
|
|
|
}
|
|
|
|
|
2023-11-04 10:09:47 +01:00
|
|
|
i.mipmap.DrawTriangles(srcMipmaps, vertices, indices, blend, dstRegion, srcRegions, shader.shader, uniforms, fillRule, canSkipMipmap)
|
2022-03-19 17:24:47 +01:00
|
|
|
}
|
|
|
|
|
2023-04-27 17:55:10 +02:00
|
|
|
func (i *Image) WritePixels(pix []byte, region image.Rectangle) {
|
2022-12-20 03:34:35 +01:00
|
|
|
if i.modifyCallback != nil {
|
|
|
|
i.modifyCallback()
|
2022-10-28 12:07:33 +02:00
|
|
|
}
|
2022-10-16 12:47:00 +02:00
|
|
|
i.flushBufferIfNeeded()
|
2023-04-27 17:55:10 +02:00
|
|
|
i.mipmap.WritePixels(pix, region)
|
2022-03-19 17:24:47 +01:00
|
|
|
}
|
|
|
|
|
2023-04-27 17:55:10 +02:00
|
|
|
func (i *Image) ReadPixels(pixels []byte, region image.Rectangle) {
|
2022-03-20 08:26:26 +01:00
|
|
|
// Check the error existence and avoid unnecessary calls.
|
2023-10-15 08:50:39 +02:00
|
|
|
if i.ui.error() != nil {
|
2022-08-07 19:48:23 +02:00
|
|
|
return
|
2022-03-20 08:26:26 +01:00
|
|
|
}
|
|
|
|
|
2022-10-19 17:46:44 +02:00
|
|
|
i.flushBigOffscreenBufferIfNeeded()
|
|
|
|
|
2023-10-14 17:06:07 +02:00
|
|
|
if err := i.ui.readPixels(i.mipmap, pixels, region); err != nil {
|
2022-03-20 08:51:23 +01:00
|
|
|
if panicOnErrorOnReadingPixels {
|
2022-03-20 08:26:26 +01:00
|
|
|
panic(err)
|
|
|
|
}
|
2023-10-15 08:50:39 +02:00
|
|
|
i.ui.setError(err)
|
2022-03-20 08:26:26 +01:00
|
|
|
}
|
2022-03-19 17:24:47 +01:00
|
|
|
}
|
|
|
|
|
2022-08-31 06:10:41 +02:00
|
|
|
func (i *Image) DumpScreenshot(name string, blackbg bool) (string, error) {
|
2022-10-22 07:19:24 +02:00
|
|
|
i.flushBufferIfNeeded()
|
2023-10-14 17:06:07 +02:00
|
|
|
return i.ui.dumpScreenshot(i.mipmap, name, blackbg)
|
2022-03-19 17:24:47 +01:00
|
|
|
}
|
|
|
|
|
2022-10-16 12:47:00 +02:00
|
|
|
func (i *Image) flushBufferIfNeeded() {
|
2022-10-19 17:46:44 +02:00
|
|
|
i.flushBigOffscreenBufferIfNeeded()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Image) flushBigOffscreenBufferIfNeeded() {
|
2023-02-17 15:56:00 +01:00
|
|
|
if i.bigOffscreenBuffer != nil {
|
|
|
|
i.bigOffscreenBuffer.flush()
|
2022-10-19 17:46:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-14 17:06:07 +02:00
|
|
|
func (u *UserInterface) DumpImages(dir string) (string, error) {
|
|
|
|
return u.dumpImages(dir)
|
2022-03-19 17:24:47 +01:00
|
|
|
}
|
2022-03-19 15:55:14 +01:00
|
|
|
|
2022-04-01 11:16:03 +02:00
|
|
|
func (i *Image) clear() {
|
2023-04-27 17:55:10 +02:00
|
|
|
i.Fill(0, 0, 0, 0, image.Rect(0, 0, i.width, i.height))
|
2022-04-01 11:16:03 +02:00
|
|
|
}
|
|
|
|
|
2023-04-27 17:55:10 +02:00
|
|
|
func (i *Image) Fill(r, g, b, a float32, region image.Rectangle) {
|
2022-12-02 13:04:03 +01:00
|
|
|
if len(i.tmpVerticesForFill) < 4*graphics.VertexFloatCount {
|
|
|
|
i.tmpVerticesForFill = make([]float32, 4*graphics.VertexFloatCount)
|
|
|
|
}
|
2023-02-17 15:56:00 +01:00
|
|
|
// i.tmpVerticesForFill can be reused as this is sent to DrawTriangles immediately.
|
2022-12-02 13:04:03 +01:00
|
|
|
graphics.QuadVertices(
|
|
|
|
i.tmpVerticesForFill,
|
2023-10-14 17:06:07 +02:00
|
|
|
1, 1, float32(i.ui.whiteImage.width-1), float32(i.ui.whiteImage.height-1),
|
2022-04-01 11:16:03 +02:00
|
|
|
float32(i.width), 0, 0, float32(i.height), 0, 0,
|
|
|
|
r, g, b, a)
|
|
|
|
is := graphics.QuadIndices()
|
|
|
|
|
2024-06-09 19:02:47 +02:00
|
|
|
srcs := [graphics.ShaderSrcImageCount]*Image{i.ui.whiteImage}
|
2022-04-01 11:16:03 +02:00
|
|
|
|
2023-10-20 18:24:46 +02:00
|
|
|
blend := graphicsdriver.BlendCopy
|
|
|
|
// If possible, use BlendSourceOver to encourage batching (#2817).
|
|
|
|
if a == 1 && i.lastBlend == graphicsdriver.BlendSourceOver {
|
|
|
|
blend = graphicsdriver.BlendSourceOver
|
|
|
|
}
|
|
|
|
// i.lastBlend is updated in DrawTriangles.
|
2024-06-09 19:02:47 +02:00
|
|
|
i.DrawTriangles(srcs, i.tmpVerticesForFill, is, blend, region, [graphics.ShaderSrcImageCount]image.Rectangle{}, NearestFilterShader, nil, graphicsdriver.FillRuleFillAll, true, false)
|
2022-03-19 15:55:14 +01:00
|
|
|
}
|
2023-02-17 15:56:00 +01:00
|
|
|
|
|
|
|
type bigOffscreenImage struct {
|
2023-10-14 17:06:07 +02:00
|
|
|
ui *UserInterface
|
|
|
|
|
2023-02-18 10:23:57 +01:00
|
|
|
orig *Image
|
|
|
|
imageType atlas.ImageType
|
|
|
|
|
2023-02-17 15:56:00 +01:00
|
|
|
image *Image
|
2023-09-28 07:29:55 +02:00
|
|
|
region image.Rectangle
|
2023-02-18 10:23:57 +01:00
|
|
|
|
|
|
|
blend graphicsdriver.Blend
|
|
|
|
dirty bool
|
2023-02-17 15:56:00 +01:00
|
|
|
|
|
|
|
tmpVerticesForFlushing []float32
|
|
|
|
tmpVerticesForCopying []float32
|
|
|
|
}
|
|
|
|
|
2023-10-14 17:06:07 +02:00
|
|
|
func (u *UserInterface) newBigOffscreenImage(orig *Image, imageType atlas.ImageType) *bigOffscreenImage {
|
2023-02-17 15:56:00 +01:00
|
|
|
return &bigOffscreenImage{
|
2023-10-14 17:06:07 +02:00
|
|
|
ui: u,
|
2023-02-18 10:23:57 +01:00
|
|
|
orig: orig,
|
|
|
|
imageType: imageType,
|
2023-02-17 15:56:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-03 07:06:22 +01:00
|
|
|
func (i *bigOffscreenImage) deallocate() {
|
2023-02-18 10:23:57 +01:00
|
|
|
if i.image != nil {
|
2023-11-03 07:06:22 +01:00
|
|
|
i.image.Deallocate()
|
2023-02-18 10:23:57 +01:00
|
|
|
}
|
2023-02-17 15:56:00 +01:00
|
|
|
i.dirty = false
|
|
|
|
}
|
|
|
|
|
2024-06-09 19:02:47 +02:00
|
|
|
func (i *bigOffscreenImage) drawTriangles(srcs [graphics.ShaderSrcImageCount]*Image, vertices []float32, indices []uint32, blend graphicsdriver.Blend, dstRegion image.Rectangle, srcRegions [graphics.ShaderSrcImageCount]image.Rectangle, shader *Shader, uniforms []uint32, fillRule graphicsdriver.FillRule, canSkipMipmap bool) {
|
2023-02-17 15:56:00 +01:00
|
|
|
if i.blend != blend {
|
|
|
|
i.flush()
|
|
|
|
}
|
|
|
|
i.blend = blend
|
|
|
|
|
2023-02-19 14:12:27 +01:00
|
|
|
// If the new region doesn't match with the current region, remove the buffer image and recreate it later.
|
2023-02-18 10:34:06 +01:00
|
|
|
if r := i.requiredRegion(vertices); i.region != r {
|
2023-02-19 14:12:27 +01:00
|
|
|
i.flush()
|
|
|
|
i.image = nil
|
2023-02-18 10:34:06 +01:00
|
|
|
i.region = r
|
|
|
|
}
|
|
|
|
|
2023-09-28 07:29:55 +02:00
|
|
|
if i.region.Empty() {
|
2023-03-03 14:34:40 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-18 10:34:06 +01:00
|
|
|
if i.image == nil {
|
2023-10-14 17:06:07 +02:00
|
|
|
i.image = i.ui.NewImage(i.region.Dx()*bigOffscreenScale, i.region.Dy()*bigOffscreenScale, i.imageType)
|
2023-02-18 10:34:06 +01:00
|
|
|
}
|
|
|
|
|
2023-02-17 15:56:00 +01:00
|
|
|
// Copy the current rendering result to get the correct blending result.
|
|
|
|
if blend != graphicsdriver.BlendSourceOver && !i.dirty {
|
2024-06-09 19:02:47 +02:00
|
|
|
srcs := [graphics.ShaderSrcImageCount]*Image{i.orig}
|
2023-02-17 15:56:00 +01:00
|
|
|
if len(i.tmpVerticesForCopying) < 4*graphics.VertexFloatCount {
|
|
|
|
i.tmpVerticesForCopying = make([]float32, 4*graphics.VertexFloatCount)
|
|
|
|
}
|
2023-12-17 14:42:34 +01:00
|
|
|
// i.tmpVerticesForCopying can be reused as this is sent to DrawTriangles immediately.
|
2023-02-17 15:56:00 +01:00
|
|
|
graphics.QuadVertices(
|
|
|
|
i.tmpVerticesForCopying,
|
2023-09-28 07:29:55 +02:00
|
|
|
float32(i.region.Min.X), float32(i.region.Min.Y), float32(i.region.Max.X), float32(i.region.Max.Y),
|
2023-02-17 15:56:00 +01:00
|
|
|
bigOffscreenScale, 0, 0, bigOffscreenScale, 0, 0,
|
|
|
|
1, 1, 1, 1)
|
|
|
|
is := graphics.QuadIndices()
|
2023-09-28 07:29:55 +02:00
|
|
|
dstRegion := image.Rect(0, 0, i.region.Dx()*bigOffscreenScale, i.region.Dy()*bigOffscreenScale)
|
2024-06-09 19:02:47 +02:00
|
|
|
i.image.DrawTriangles(srcs, i.tmpVerticesForCopying, is, graphicsdriver.BlendCopy, dstRegion, [graphics.ShaderSrcImageCount]image.Rectangle{}, NearestFilterShader, nil, graphicsdriver.FillRuleFillAll, true, false)
|
2023-02-17 15:56:00 +01:00
|
|
|
}
|
|
|
|
|
2023-02-18 10:34:06 +01:00
|
|
|
for idx := 0; idx < len(vertices); idx += graphics.VertexFloatCount {
|
2023-09-28 07:29:55 +02:00
|
|
|
vertices[idx] = (vertices[idx] - float32(i.region.Min.X)) * bigOffscreenScale
|
|
|
|
vertices[idx+1] = (vertices[idx+1] - float32(i.region.Min.Y)) * bigOffscreenScale
|
2023-02-17 15:56:00 +01:00
|
|
|
}
|
|
|
|
|
2023-06-23 19:25:19 +02:00
|
|
|
// Translate to i.region coordinate space, and clamp against region size.
|
2023-09-28 07:29:55 +02:00
|
|
|
dstRegion = dstRegion.Sub(i.region.Min)
|
|
|
|
dstRegion = dstRegion.Intersect(image.Rect(0, 0, i.region.Dx(), i.region.Dy()))
|
|
|
|
dstRegion.Min.X *= bigOffscreenScale
|
|
|
|
dstRegion.Min.Y *= bigOffscreenScale
|
|
|
|
dstRegion.Max.X *= bigOffscreenScale
|
|
|
|
dstRegion.Max.Y *= bigOffscreenScale
|
2023-02-17 15:56:00 +01:00
|
|
|
|
2023-11-04 10:09:47 +01:00
|
|
|
i.image.DrawTriangles(srcs, vertices, indices, blend, dstRegion, srcRegions, shader, uniforms, fillRule, canSkipMipmap, false)
|
2023-02-17 15:56:00 +01:00
|
|
|
i.dirty = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *bigOffscreenImage) flush() {
|
2023-02-18 10:23:57 +01:00
|
|
|
if i.image == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-17 15:56:00 +01:00
|
|
|
if !i.dirty {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-06 15:00:58 +01:00
|
|
|
// Mark the offscreen clean earlier to avoid recursive calls.
|
2023-02-17 15:56:00 +01:00
|
|
|
i.dirty = false
|
|
|
|
|
2024-06-09 19:02:47 +02:00
|
|
|
srcs := [graphics.ShaderSrcImageCount]*Image{i.image}
|
2023-02-17 15:56:00 +01:00
|
|
|
if len(i.tmpVerticesForFlushing) < 4*graphics.VertexFloatCount {
|
|
|
|
i.tmpVerticesForFlushing = make([]float32, 4*graphics.VertexFloatCount)
|
|
|
|
}
|
|
|
|
// i.tmpVerticesForFlushing can be reused as this is sent to DrawTriangles in this function.
|
|
|
|
graphics.QuadVertices(
|
|
|
|
i.tmpVerticesForFlushing,
|
2023-09-28 07:29:55 +02:00
|
|
|
0, 0, float32(i.region.Dx()*bigOffscreenScale), float32(i.region.Dy()*bigOffscreenScale),
|
|
|
|
1.0/bigOffscreenScale, 0, 0, 1.0/bigOffscreenScale, float32(i.region.Min.X), float32(i.region.Min.Y),
|
2023-02-17 15:56:00 +01:00
|
|
|
1, 1, 1, 1)
|
|
|
|
is := graphics.QuadIndices()
|
2023-02-18 10:34:06 +01:00
|
|
|
dstRegion := i.region
|
2023-02-17 15:56:00 +01:00
|
|
|
blend := graphicsdriver.BlendSourceOver
|
|
|
|
if i.blend != graphicsdriver.BlendSourceOver {
|
|
|
|
blend = graphicsdriver.BlendCopy
|
|
|
|
}
|
2024-06-09 19:02:47 +02:00
|
|
|
i.orig.DrawTriangles(srcs, i.tmpVerticesForFlushing, is, blend, dstRegion, [graphics.ShaderSrcImageCount]image.Rectangle{}, LinearFilterShader, nil, graphicsdriver.FillRuleFillAll, true, false)
|
2023-02-17 15:56:00 +01:00
|
|
|
|
|
|
|
i.image.clear()
|
|
|
|
i.dirty = false
|
|
|
|
}
|
2023-02-18 10:34:06 +01:00
|
|
|
|
2023-09-28 07:29:55 +02:00
|
|
|
func (i *bigOffscreenImage) requiredRegion(vertices []float32) image.Rectangle {
|
2023-02-18 10:34:06 +01:00
|
|
|
minX := float32(i.orig.width)
|
|
|
|
minY := float32(i.orig.height)
|
|
|
|
maxX := float32(0)
|
|
|
|
maxY := float32(0)
|
|
|
|
for i := 0; i < len(vertices); i += graphics.VertexFloatCount {
|
|
|
|
dstX := vertices[i]
|
|
|
|
dstY := vertices[i+1]
|
|
|
|
if minX > floor(dstX)-1 {
|
|
|
|
minX = floor(dstX) - 1
|
|
|
|
}
|
|
|
|
if minY > floor(dstY)-1 {
|
|
|
|
minY = floor(dstY) - 1
|
|
|
|
}
|
|
|
|
if maxX < ceil(dstX)+1 {
|
|
|
|
maxX = ceil(dstX) + 1
|
|
|
|
}
|
|
|
|
if maxY < ceil(dstY)+1 {
|
|
|
|
maxY = ceil(dstY) + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adjust the granularity of the rectangle.
|
2023-09-28 07:29:55 +02:00
|
|
|
r := image.Rect(
|
|
|
|
roundDown16(int(minX)),
|
|
|
|
roundDown16(int(minY)),
|
|
|
|
roundUp16(int(maxX)),
|
|
|
|
roundUp16(int(maxY)))
|
|
|
|
r = r.Intersect(image.Rect(0, 0, i.orig.width, i.orig.height))
|
|
|
|
|
|
|
|
// TODO: Is this check required?
|
|
|
|
if r.Dx() < 0 || r.Dy() < 0 {
|
2023-02-18 10:34:06 +01:00
|
|
|
return i.region
|
|
|
|
}
|
|
|
|
|
2023-09-28 07:29:55 +02:00
|
|
|
return r.Union(i.region)
|
2023-02-18 10:34:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func floor(x float32) float32 {
|
|
|
|
return float32(math.Floor(float64(x)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func ceil(x float32) float32 {
|
|
|
|
return float32(math.Ceil(float64(x)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func roundDown16(x int) int {
|
|
|
|
return x & ^(0xf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func roundUp16(x int) int {
|
|
|
|
return ((x - 1) & ^(0xf)) + 0x10
|
|
|
|
}
|