Files
2026-11-01 17:59:39 +03:00

129 lines
4.1 KiB
Go

package transform
import (
"context"
"image"
"image/color"
)
func fitContext(
ctx context.Context,
source image.Image,
maxWidth int,
maxHeight int,
upscale bool,
) (*image.NRGBA, error) {
bounds := source.Bounds()
width, height := fitDimensions(bounds.Dx(), bounds.Dy(), maxWidth, maxHeight, upscale)
return resizeContext(ctx, source, width, height)
}
func fitDimensions(width, height, maxWidth, maxHeight int, upscale bool) (int, int) {
if !upscale && width <= maxWidth && height <= maxHeight {
return width, height
}
if int64(width)*int64(maxHeight) > int64(height)*int64(maxWidth) {
newHeight := max(1, int(int64(height)*int64(maxWidth)/int64(width)))
return maxWidth, newHeight
}
newWidth := max(1, int(int64(width)*int64(maxHeight)/int64(height)))
return newWidth, maxHeight
}
func centeredSquare(source image.Image) image.Image {
bounds := source.Bounds()
size := min(bounds.Dx(), bounds.Dy())
left := bounds.Min.X + (bounds.Dx()-size)/2
top := bounds.Min.Y + (bounds.Dy()-size)/2
return croppedImage{source: source, bounds: image.Rect(0, 0, size, size), offset: image.Pt(left, top)}
}
type croppedImage struct {
source image.Image
bounds image.Rectangle
offset image.Point
}
func (i croppedImage) ColorModel() color.Model { return i.source.ColorModel() }
func (i croppedImage) Bounds() image.Rectangle { return i.bounds }
func (i croppedImage) At(x, y int) color.Color {
return i.source.At(i.offset.X+x, i.offset.Y+y)
}
func resizeContext(ctx context.Context, source image.Image, width, height int) (*image.NRGBA, error) {
bounds := source.Bounds()
if bounds.Dx() == width && bounds.Dy() == height {
return copyNRGBAContext(ctx, source)
}
output := image.NewNRGBA(image.Rect(0, 0, width, height))
for y := 0; y < height; y++ {
if err := contextError(ctx); err != nil {
return nil, err
}
y0, y1, fy := sampleCoordinate(y, height, bounds.Dy())
for x := 0; x < width; x++ {
if x&255 == 0 {
if err := contextError(ctx); err != nil {
return nil, err
}
}
x0, x1, fx := sampleCoordinate(x, width, bounds.Dx())
c00 := color.NRGBAModel.Convert(source.At(bounds.Min.X+x0, bounds.Min.Y+y0)).(color.NRGBA)
c10 := color.NRGBAModel.Convert(source.At(bounds.Min.X+x1, bounds.Min.Y+y0)).(color.NRGBA)
c01 := color.NRGBAModel.Convert(source.At(bounds.Min.X+x0, bounds.Min.Y+y1)).(color.NRGBA)
c11 := color.NRGBAModel.Convert(source.At(bounds.Min.X+x1, bounds.Min.Y+y1)).(color.NRGBA)
output.SetNRGBA(x, y, interpolate(c00, c10, c01, c11, fx, fy))
}
}
return output, nil
}
func sampleCoordinate(destination, destinationSize, sourceSize int) (int, int, uint32) {
coordinate := ((int64(2*destination+1)*int64(sourceSize))<<15)/int64(destinationSize) - 1<<15
if coordinate <= 0 {
return 0, min(1, sourceSize-1), 0
}
maximum := int64(sourceSize-1) << 16
if coordinate >= maximum {
return sourceSize - 1, sourceSize - 1, 0
}
first := int(coordinate >> 16)
return first, first + 1, uint32(coordinate & 0xffff)
}
func interpolate(c00, c10, c01, c11 color.NRGBA, fx, fy uint32) color.NRGBA {
return color.NRGBA{
R: interpolateChannel(c00.R, c10.R, c01.R, c11.R, fx, fy),
G: interpolateChannel(c00.G, c10.G, c01.G, c11.G, fx, fy),
B: interpolateChannel(c00.B, c10.B, c01.B, c11.B, fx, fy),
A: interpolateChannel(c00.A, c10.A, c01.A, c11.A, fx, fy),
}
}
func interpolateChannel(c00, c10, c01, c11 uint8, fx, fy uint32) uint8 {
const one uint64 = 1 << 16
top := uint64(c00)*(one-uint64(fx)) + uint64(c10)*uint64(fx)
bottom := uint64(c01)*(one-uint64(fx)) + uint64(c11)*uint64(fx)
value := top*(one-uint64(fy)) + bottom*uint64(fy)
return uint8((value + 1<<31) >> 32)
}
func copyNRGBAContext(ctx context.Context, source image.Image) (*image.NRGBA, error) {
bounds := source.Bounds()
output := image.NewNRGBA(image.Rect(0, 0, bounds.Dx(), bounds.Dy()))
for y := 0; y < bounds.Dy(); y++ {
if err := contextError(ctx); err != nil {
return nil, err
}
for x := 0; x < bounds.Dx(); x++ {
if x&255 == 0 {
if err := contextError(ctx); err != nil {
return nil, err
}
}
output.Set(x, y, source.At(bounds.Min.X+x, bounds.Min.Y+y))
}
}
return output, nil
}