refactor(色彩管理): 优化图像色彩转换性能
build.yaml / build (push) Successful in 53s

This commit is contained in:
xiaoqidun committed 2026-10-16 00:18:59 +08:00
1 parent e1bf215e6a
commit e64575ec16
1 file changed
+20 -1
+20 -1
View File
@@ -78,7 +78,7 @@ func applyTIFFColorProfile(ctx context.Context, source image.Image, profile *pdf
return applyImageColorProfile(ctx, source, profile, false)
}
// applyImageColorProfile 按源配置转换显示像素,缺省传递函数在浮点域解码
// applyImageColorProfile 按源配置转换显示像素,精确复用连续同色结果
// 入参: ctx 取消上下文, source 源像素, profile 颜色空间, linearSRGB 是否先解码sRGB传递函数
// 返回: image.Image 十六位显示图像, error 变换或取消错误
func applyImageColorProfile(ctx context.Context, source image.Image, profile *pdfgo.ColorSpace, linearSRGB bool) (image.Image, error) {
@@ -100,7 +100,10 @@ func applyImageColorProfile(ctx context.Context, source image.Image, profile *pd
premultiplied := model == color.RGBAModel || model == color.RGBA64Model
table := newTIFFProfileTable(source)
result := image.NewNRGBA64(bounds)
var previous [4]float64
var previousPixel color.NRGBA64
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
misses := 0
for x := bounds.Min.X; x < bounds.Max.X; x++ {
if (x-bounds.Min.X)&255 == 0 {
if err := ctx.Err(); err != nil {
@@ -151,6 +154,17 @@ func applyImageColorProfile(ctx context.Context, source image.Image, profile *pd
alpha = pixel.A
values = [4]float64{float64(pixel.R) / 65535, float64(pixel.G) / 65535, float64(pixel.B) / 65535}
}
if misses < 32 {
if previousPixel.A != 0 && values == previous {
pixel := previousPixel
pixel.A = alpha
result.SetNRGBA64(x, y, pixel)
misses = 0
continue
}
previous = values
misses++
}
if linearSRGB {
for i := range 3 {
if values[i] <= 0.04045 {
@@ -168,6 +182,11 @@ func applyImageColorProfile(ctx context.Context, source image.Image, profile *pd
for i, value := range rgb {
channels[i] = uint16(math.Round(max(0, min(1, value)) * 65535))
}
if misses < 32 {
previousPixel = color.NRGBA64{R: channels[0], G: channels[1], B: channels[2], A: 65535}
} else {
previousPixel.A = 0
}
if slot >= 0 {
table[slot] = color.NRGBA64{R: channels[0], G: channels[1], B: channels[2], A: 65535}
}