在
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
方法中使用这些量化数据。
func Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point, op Op)
,您不需要 Quantizer
中的任何东西
draw.Image
实现以及其他数据。soniakeys/quant
中的示例,其中:
quantizers
满足标准库的draw.Quantizer接口,像这个。draw.Drawer
接口。esimov/colorquant
这样的替代方案。