实现GIF的'Quantizer'和'Drawer'接口时遇到麻烦

问题描述 投票:0回答:1

image/draw
中,
Quantizer
Drawer
定义如下:

type Quantizer interface { 
    Quantize(p color.Palette, m image.Image) color.Palette 
}
type Drawer interface {
    Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point)
}

gif.Encode(w io.Writer, m image.Image, o *Options)
中有这样的代码:

if opts.Quantizer != nil { 
    pm.Palette = opts.Quantizer.Quantize(make(color.Palette, 0, opts.NumColors), m) 
} 
opts.Drawer.Draw(pm, b, m, b.Min)

当我想自己写一个图像量化算法时,我需要实现

draw.Quantizer
draw.Drawer

如您所见,

opts.Quantizer.Quantize
返回
Palette
。但实际上,在调用
opts.Drawer.Draw
时,我不仅需要
Palette
,还需要
Quantize
的一些其他数据。

是否可以让量化数据可以使用?


12月25日编辑

例如,我在

quantize
时获得索引图。当我
draw
时,我需要这个索引图来使我的算法更快。我该怎么做才能将此索引图传递到
Drawer


编辑于2024年4月10日

type quantizer struct {
    indexingMap *IndexingMap
}
func (q *quantizer) Quantize(_ color.Palette, img image.Image) color.Palette {
    // do sth
    // q.indexingMap = sth
}
type drawer struct {
    q *quantizer
}
func (d drawer) Draw(dstOri draw.Image, rect image.Rectangle, src image.Image, sp image.Point) {
    // do sth with d.q.indexingMap
}
func Opt() *gif.Options {
    q := &quantizer{}
    return &gif.Options{
        NumColors: 256,
        Quantizer: q,
        Drawer:    drawer{q: q},
    }
}

然后我可以在

Draw
方法中使用这些量化数据。

image go gif
1个回答
0
投票

看看

func Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point, op Op)
,您不需要
Quantizer

中的任何东西

您需要提供

draw.Image
实现以及其他数据。
请参阅
soniakeys/quant
中的示例,其中:

如果这仍然不起作用,您需要像

esimov/colorquant
这样的替代方案。

© www.soinside.com 2019 - 2024. All rights reserved.