feat(图像合成): 加速图像采样合成
build.yaml / build (push) Successful in 5m15s

This commit is contained in:
xiaoqidun committed 2026-10-15 16:12:06 +08:00
1 parent 43d0713488
commit 307e3a718a
6 files changed
+85 -11

No files matched your search

+26
View File
@@ -459,6 +459,11 @@ func GrayAt(img image.Image, x, y int) color.Gray {
// 返回: color.NRGBA NRGBA像素
func NRGBAAt(img image.Image, x, y int) color.NRGBA {
switch src := img.(type) {
case *image.NRGBA:
return src.NRGBAAt(x, y)
case *image.NRGBA64:
c := src.NRGBA64At(x, y)
return color.NRGBA{R: uint8(c.R >> 8), G: uint8(c.G >> 8), B: uint8(c.B >> 8), A: uint8(c.A >> 8)}
case interface{ NRGBAAt(int, int) color.NRGBA }:
return src.NRGBAAt(x, y)
case interface{ NRGBA64At(int, int) color.NRGBA64 }:
@@ -488,6 +493,27 @@ func NRGBAAt(img image.Image, x, y int) color.NRGBA {
// 入参: img 图像, x 横坐标, y 纵坐标
// 返回: uint32 十六位透明度
func AlphaAt(img image.Image, x, y int) uint32 {
switch source := img.(type) {
case *image.RGBA:
if !(image.Point{X: x, Y: y}).In(source.Rect) {
return 0
}
return uint32(source.Pix[source.PixOffset(x, y)+3]) * 257
case *image.RGBA64:
if !(image.Point{X: x, Y: y}).In(source.Rect) {
return 0
}
i := source.PixOffset(x, y) + 6
return uint32(source.Pix[i])<<8 | uint32(source.Pix[i+1])
case *image.NRGBA:
return uint32(source.NRGBAAt(x, y).A) * 257
case *image.NRGBA64:
return uint32(source.NRGBA64At(x, y).A)
case *image.Alpha:
return uint32(source.AlphaAt(x, y).A) * 257
case *image.Alpha16:
return uint32(source.Alpha16At(x, y).A)
}
if source, ok := img.(image.RGBA64Image); ok {
return uint32(source.RGBA64At(x, y).A)
}
+26 -6
View File
@@ -221,16 +221,36 @@ func rasterImageHorizontal(row []rasterImagePixel, src image.RGBA64Image, sample
// 入参: dst 目标图像, output 行像素缓冲, values 累积颜色, x 横向起点, y 目标行坐标, op 合成操作
func rasterImageWriteRow(dst draw.Image, output *image.RGBA64, values []rasterImagePixel, x, y int, op draw.Op) {
for i, value := range values {
output.SetRGBA64(i, 0, color.RGBA64{
R: uint16(min(max(min(value.r, value.a)+.5, 0), 65535)),
G: uint16(min(max(min(value.g, value.a)+.5, 0), 65535)),
B: uint16(min(max(min(value.b, value.a)+.5, 0), 65535)),
A: uint16(min(max(value.a+.5, 0), 65535)),
})
r := rasterImageComponent(value.r, value.a)
g := rasterImageComponent(value.g, value.a)
b := rasterImageComponent(value.b, value.a)
a := rasterImageComponent(value.a, value.a)
pixel := output.Pix[i*8 : i*8+8 : i*8+8]
pixel[0], pixel[1] = uint8(r>>8), uint8(r)
pixel[2], pixel[3] = uint8(g>>8), uint8(g)
pixel[4], pixel[5] = uint8(b>>8), uint8(b)
pixel[6], pixel[7] = uint8(a>>8), uint8(a)
}
draw.Draw(dst, image.Rect(x, y, x+len(values), y+1), output, image.Point{}, op)
}
// rasterImageComponent 按预乘透明度裁切并取整十六位分量
// 入参: value 采样分量, alpha 采样透明度
// 返回: uint16 输出分量
func rasterImageComponent(value, alpha float64) uint16 {
if value > alpha || math.IsNaN(alpha) {
value = alpha
}
value += .5
if value <= 0 {
return 0
}
if value >= 65535 {
return 65535
}
return uint16(value)
}
// DrawPixels 对轴向缩放使用可分离采样,其余变换保留通用路径
// 入参: dst 目标图像, src 源图像, m 按a、b、c、d、e、f排列的像素变换, op 合成操作
func DrawPixels(dst draw.Image, src image.Image, m [6]float64, op draw.Op) {