Add SubStitchedImage

This commit is contained in:
David Vogel 2023-12-23 01:16:25 +01:00
parent 6d028d4064
commit 915da73845
2 changed files with 46 additions and 7 deletions

View File

@ -128,12 +128,6 @@ func (si *StitchedImage) RGBAAt(x, y int) color.RGBA {
go si.cacheRows[newRowIndex].Regenerate()
}
// Invalidate old cache row.
oldRowIndex := si.oldCacheRowIndex
if oldRowIndex >= 0 && oldRowIndex < len(si.cacheRows) {
si.cacheRows[oldRowIndex].Invalidate()
}
// Invalidate all tiles that are above the next row.
si.tiles.InvalidateAboveY((rowIndex+1)*si.cacheRowHeight - si.cacheRowYOffset)
@ -146,7 +140,7 @@ func (si *StitchedImage) RGBAAt(x, y int) color.RGBA {
// Opaque returns whether the image is fully opaque.
//
// For more speed and smaller file size, StitchedImage will be marked as non-transparent.
// This will speed up image saving by 2x, as there is no need to iterate over the whole image to find a single non opaque pixel.
// This will speed up image saving by 2x, as there is no need to iterate over the whole image just to find a single non opaque pixel.
func (si *StitchedImage) Opaque() bool {
return true
}
@ -157,3 +151,12 @@ func (si *StitchedImage) Progress() (value, max int) {
return int(si.queryCounter.Load()), size.X * size.Y
}
// SubStitchedImage returns an image representing the portion of the image p visible through r.
// The returned image references to the original stitched image, and therefore reuses its cache.
func (si *StitchedImage) SubStitchedImage(r image.Rectangle) SubStitchedImage {
return SubStitchedImage{
StitchedImage: si,
bounds: si.Bounds().Intersect(r),
}
}

View File

@ -0,0 +1,36 @@
// Copyright (c) 2023 David Vogel
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
package main
import (
"image"
"image/color"
)
type SubStitchedImage struct {
*StitchedImage // The original stitched image.
bounds image.Rectangle // The new bounds of the cropped image.
}
// Bounds returns the domain for which At can return non-zero color.
// The bounds do not necessarily contain the point (0, 0).
func (s SubStitchedImage) Bounds() image.Rectangle {
return s.bounds
}
func (s SubStitchedImage) At(x, y int) color.Color {
return s.RGBAAt(x, y)
}
func (s SubStitchedImage) RGBAAt(x, y int) color.RGBA {
point := image.Point{X: x, Y: y}
if !point.In(s.bounds) {
return color.RGBA{}
}
return s.StitchedImage.RGBAAt(x, y)
}