[インデックス 16727] ファイルの概要
このコミットは、Go言語の標準ライブラリ image/draw
パッケージに、新しい画像描画機能と、特にパレット画像に対する高品質なディザリング手法であるFloyd-Steinberg誤差拡散を追加するものです。これにより、image/draw
パッケージの柔軟性と機能性が向上し、より高度な画像処理が可能になります。
コミット
commit e430eb8bd76da47edc3c615162ab2ef6e57cfbbe
Author: Nigel Tao <nigeltao@golang.org>
Date: Thu Jul 11 08:47:29 2013 +1000
image/draw: add Drawer, FloydSteinberg and the op.Draw method.
R=r, andybons
CC=andybons, golang-dev
https://golang.org/cl/10977043
GitHub上でのコミットページへのリンク
https://github.com/golang/go/commit/e430eb8bd76da47edc3c615162ab2ef6e57cfbbe
元コミット内容
image/draw
パッケージに、Drawer
インターフェース、FloydSteinberg
誤差拡散アルゴリズム、およびop.Draw
メソッドを追加する。
変更の背景
Go言語のimage
パッケージは、基本的な画像操作機能を提供していますが、より高度な描画や画像変換のニーズに応えるためには、機能の拡充が必要でした。特に、限られた色数しか持たないパレット画像に対して、元の画像の見た目を損なわずに色数を減らす(量子化する)際には、ディザリングという手法が不可欠です。Floyd-Steinberg誤差拡散は、このディザリングにおいて広く使われている高品質なアルゴリズムであり、これを標準ライブラリに組み込むことで、開発者がより簡単に高品質な画像処理を行えるようにすることが目的でした。
また、描画操作をより抽象化し、異なる描画ロジックを統一的に扱えるようにするために、Drawer
インターフェースが導入されました。これにより、Op
(Porter-Duff合成演算子)のような既存の描画モードだけでなく、Floyd-Steinbergのような新しい描画ロジックも、同じインターフェースを通じて利用できるようになります。
前提知識の解説
Go言語のimage
パッケージ
Go言語の標準ライブラリには、画像処理のためのimage
パッケージとimage/color
、image/draw
などのサブパッケージがあります。
image.Image
: 画像データを表すインターフェースで、At(x, y color.Color)
メソッドやBounds() image.Rectangle
メソッドを持ちます。image.Rectangle
: 画像の矩形領域を表し、Min
とMax
のimage.Point
で定義されます。image.Point
: 2次元の座標を表します。color.Color
: 色を表すインターフェースで、RGBA()
メソッドを通じてR, G, B, A(赤、緑、青、アルファ)の各成分を16ビットのuint32
値で取得できます。image.Paletted
: パレット画像を表す構造体で、色のインデックスの配列と、そのインデックスが参照するcolor.Palette
(色の配列)を持ちます。
Porter-Duff合成演算子
画像合成の分野で用いられる概念で、2つの画像(ソース画像とデスティネーション画像)をどのように合成するかを定義する一連の規則です。image/draw
パッケージのOp
型は、これらの演算子(例: Over
, Src
)を表現します。
Over
: ソース画像をデスティネーション画像の上に重ねる一般的な合成方法です。ソースのアルファ値に基づいて、デスティネーションのピクセルがブレンドされます。Src
: ソース画像がデスティネーション画像を完全に置き換えます。
Floyd-Steinberg誤差拡散
ディザリング(Dithering)とは、限られた色数(特にパレット画像)で画像を表現する際に、元の画像の見た目をできるだけ忠実に再現するために、利用可能な色を混ぜて錯覚的に中間色を表現する技術です。Floyd-Steinberg誤差拡散は、このディザリングアルゴリズムの一種で、量子化誤差(元のピクセルと量子化されたピクセルの色の差)を周囲のピクセルに拡散させることで、より滑らかで自然な見た目を実現します。
アルゴリズムの基本的な考え方は以下の通りです。
- 現在のピクセルを最も近いパレット色に量子化します。
- 元のピクセルと量子化されたピクセルの色の差(誤差)を計算します。
- この誤差を、特定の比率で周囲の未処理のピクセルに分配します。一般的なFloyd-Steinbergの誤差拡散率は以下のようになります(右、下、左下、右下)。
- 右のピクセル: 7/16
- 下のピクセル: 5/16
- 左下のピクセル: 3/16
- 右下のピクセル: 1/16
この誤差拡散により、人間の目が色の違いを平均化して認識するため、限られた色数でもグラデーションが滑らかに見える効果が得られます。
技術的詳細
このコミットは、主にimage/draw
パッケージに以下の機能を追加・変更しています。
-
Drawer
インターフェースの導入:type Drawer interface { // Draw aligns r.Min in dst with sp in src and then replaces the // rectangle r in dst with the result of drawing src on dst. Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point) }
これは、任意の描画ロジックをカプセル化するための新しいインターフェースです。これにより、
Op
(Porter-Duff合成)だけでなく、Floyd-Steinbergのような異なる描画戦略も統一的に扱えるようになります。 -
Op.Draw
メソッドの追加:func (op Op) Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point) { DrawMask(dst, r, src, sp, nil, image.Point{}, op) }
既存の
Op
型がDrawer
インターフェースを満たすように、Draw
メソッドが追加されました。これにより、draw.Over.Draw(...)
のように、合成演算子を直接描画操作として利用できるようになります。 -
FloydSteinberg
誤差拡散の実装:var FloydSteinberg Drawer = floydSteinberg{} type floydSteinberg struct{} func (floydSteinberg) Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point) { clip(dst, &r, src, &sp, nil, nil) if r.Empty() { return } drawPaletted(dst, r, src, sp, true) }
FloydSteinberg
というグローバル変数としてDrawer
インターフェースを実装したfloydSteinberg
型が追加されました。このDraw
メソッドは、内部的にdrawPaletted
関数を呼び出し、Floyd-Steinberg誤差拡散を適用します。 -
drawPaletted
関数の追加: この関数は、パレット画像への描画を最適化し、Floyd-Steinberg誤差拡散を適用する主要なロジックを含んでいます。dst
が*image.Paletted
型である場合に、高速パスを提供します。- ソースピクセルをパレット色に量子化し、その誤差を周囲のピクセルに拡散させます。
- 誤差拡散のために
quantErrorCurr
とquantErrorNext
という2つのバッファを使用し、現在の行と次の行の誤差を管理します。 clamp
関数(int32
値を[0, 0xffff]
の範囲に制限)が追加され、色成分のオーバーフローを防ぎます。
-
image/color/color.go
の変更:Palette.Index
メソッドにおける色の差分計算ロジックが変更されました。以前はdiff
関数を使用していましたが、uint32
のオーバーフローを避けるために、int32
にキャストして差分を計算し、その後に2でシフト(実質的に1/2にする)して二乗和差(SSD)を計算するようになりました。これは、drawPaletted
関数でのバッチ処理と整合性を取るための変更です。 -
DrawMask
関数の改善:DrawMask
関数内で、dst
の型に応じた高速パスが追加されました。特に*image.Paletted
型の場合に、drawPaletted
関数を呼び出すようになりました。これにより、パレット画像に対する描画性能が向上します。 -
テストの追加:
TestFloydSteinbergCheckerboard
: 50%グレーの画像を白黒パレットにFloyd-Steinberg誤差拡散で描画した際に、チェッカーボードパターンが生成されることを検証します。これは、Floyd-Steinbergアルゴリズムが正しく機能していることを示す典型的なテストケースです。TestPaletted
:dst
が*image.Paletted
型であるかどうかにかかわらず、drawPaletted
関数が同じ振る舞いをすることを確認します。これは、インターフェースを介した描画と具体的な型を介した描画の一貫性を保証するためのテストです。
コアとなるコードの変更箇所
src/pkg/image/color/color.go
--- a/src/pkg/image/color/color.go
+++ b/src/pkg/image/color/color.go
@@ -253,13 +253,6 @@ func gray16Model(c Color) Color {
// Palette is a palette of colors.
type Palette []Color
-func diff(a, b uint32) uint32 {
- if a > b {
- return a - b
- }
- return b - a
-}
-
// Convert returns the palette color closest to c in Euclidean R,G,B space.
func (p Palette) Convert(c Color) Color {
if len(p) == 0 {
@@ -271,19 +264,20 @@ func (p Palette) Convert(c Color) Color {
// Index returns the index of the palette color closest to c in Euclidean
// R,G,B space.
func (p Palette) Index(c Color) int {
+\t// A batch version of this computation is in image/draw/draw.go.
+\n \tcr, cg, cb, _ := c.RGBA()
-\t// Shift by 1 bit to avoid potential uint32 overflow in sum-squared-difference.
-\tcr >>= 1
-\tcg >>= 1
-\tcb >>= 1
\tret, bestSSD := 0, uint32(1<<32-1)
\tfor i, v := range p {
\t\tvr, vg, vb, _ := v.RGBA()
-\t\tvr >>= 1
-\t\tvg >>= 1
-\t\tvb >>= 1
-\t\tdr, dg, db := diff(cr, vr), diff(cg, vg), diff(cb, vb)
-\t\tssd := (dr * dr) + (dg * dg) + (db * db)
+\t\t// We shift by 1 bit to avoid potential uint32 overflow in
+\t\t// sum-squared-difference.
+\t\tdelta := (int32(cr) - int32(vr)) >> 1
+\t\tssd := uint32(delta * delta)
+\t\tdelta = (int32(cg) - int32(vg)) >> 1
+\t\tssd += uint32(delta * delta)
+\t\tdelta = (int32(cb) - int32(vb)) >> 1
+\t\tssd += uint32(delta * delta)
\t\tif ssd < bestSSD {
\t\t\tif ssd == 0 {
\t\t\t\treturn i
src/pkg/image/draw/draw.go
--- a/src/pkg/image/draw/draw.go
+++ b/src/pkg/image/draw/draw.go
@@ -16,6 +16,12 @@ import (
// m is the maximum color value returned by image.Color.RGBA.
const m = 1<<16 - 1
+// A draw.Image is an image.Image with a Set method to change a single pixel.
+type Image interface {
+ image.Image
+ Set(x, y int, c color.Color)
+}
+
// Op is a Porter-Duff compositing operator.
type Op int
@@ -26,15 +32,31 @@ const (
Src
)
-// A draw.Image is an image.Image with a Set method to change a single pixel.
-type Image interface {
- image.Image
- Set(x, y int, c color.Color)
+// Draw implements the Drawer interface by calling the Draw function with this
+// Op.
+func (op Op) Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point) {
+ DrawMask(dst, r, src, sp, nil, image.Point{}, op)
}
-// Draw calls DrawMask with a nil mask.\n-func Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point, op Op) {
-\tDrawMask(dst, r, src, sp, nil, image.ZP, op)
+// Drawer contains the Draw method.
+type Drawer interface {
+ // Draw aligns r.Min in dst with sp in src and then replaces the
+ // rectangle r in dst with the result of drawing src on dst.
+ Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point)
}
+// FloydSteinberg is a Drawer that is the Src Op with Floyd-Steinberg error
+// diffusion.
+var FloydSteinberg Drawer = floydSteinberg{}
+
+type floydSteinberg struct{}
+
+func (floydSteinberg) Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point) {
+ clip(dst, &r, src, &sp, nil, nil)
+ if r.Empty() {
+ return
+ }
+ drawPaletted(dst, r, src, sp, true)
}
// clip clips r against each image's bounds (after translating into the
@@ -58,6 +80,17 @@ func clip(dst Image, r *image.Rectangle, src *image.Image, sp *image.Point, mask
(*mp).Y += dy
}
+func processBackward(dst Image, r image.Rectangle, src image.Image, sp image.Point) bool {
+ return image.Image(dst) == src &&
+ r.Overlaps(r.Add(sp.Sub(r.Min))) &&
+ (sp.Y < r.Min.Y || (sp.Y == r.Min.Y && sp.X < r.Min.X))
+}
+
+// Draw calls DrawMask with a nil mask.
+func Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point, op Op) {
+ DrawMask(dst, r, src, sp, nil, image.Point{}, op)
+}
+
// DrawMask aligns r.Min in dst with sp in src and mp in mask and then replaces the rectangle r
// in dst with the result of a Porter-Duff composition. A nil mask is treated as opaque.
func DrawMask(dst Image, r image.Rectangle, src image.Image, sp image.Point, mask image.Image, mp image.Point, op Op) {
@@ -67,7 +100,8 @@ func DrawMask(dst Image, r *image.Rectangle, src image.Image, sp image.Point, mas
}
// Fast paths for special cases. If none of them apply, then we fall back to a general but slow implementation.
-\tif dst0, ok := dst.(*image.RGBA); ok {
+\tswitch dst0 := dst.(type) {
+\tcase *image.RGBA:
\t\tif op == Over {
\t\t\tif mask == nil {
\t\t\t\tswitch src0 := src.(type) {
@@ -113,19 +147,20 @@ func DrawMask(dst Image, r image.Rectangle, src image.Image, sp image.Point, mas
\t\t}\n \t\tdrawRGBA(dst0, r, src, sp, mask, mp, op)\n \t\treturn\n+\tcase *image.Paletted:\n+\t\tif op == Src && mask == nil && !processBackward(dst, r, src, sp) {\n+\t\t\tdrawPaletted(dst0, r, src, sp, false)\n+\t\t}\n \t}\n
\tx0, x1, dx := r.Min.X, r.Max.X, 1
\ty0, y1, dy := r.Min.Y, r.Max.Y, 1
-\tif image.Image(dst) == src && r.Overlaps(r.Add(sp.Sub(r.Min))) {
-\t\t// Rectangles overlap: process backward?\n-\t\tif sp.Y < r.Min.Y || sp.Y == r.Min.Y && sp.X < r.Min.X {
-\t\t\tx0, x1, dx = x1-1, x0-1, -1\n-\t\t\ty0, y1, dy = y1-1, y0-1, -1\n-\t\t}\n+\tif processBackward(dst, r, src, sp) {
+\t\tx0, x1, dx = x1-1, x0-1, -1\n+\t\ty0, y1, dy = y1-1, y0-1, -1\n \t}\n
-\tvar out *color.RGBA64\n+\tvar out color.RGBA64
\tsy := sp.Y + y0 - r.Min.Y
\tmy := mp.Y + y0 - r.Min.Y
\tfor y := y0; y != y1; y, sy, my = y+dy, sy+dy, my+dy {
@@ -147,9 +182,6 @@ func DrawMask(dst Image, r image.Rectangle, src image.Image, sp image.Point, mas
\t\t\t\tdst.Set(x, y, src.At(sx, sy))\n \t\t\tdefault:\n \t\t\t\tsr, sg, sb, sa := src.At(sx, sy).RGBA()\n-\t\t\t\tif out == nil {\n-\t\t\t\t\tout = new(color.RGBA64)\n-\t\t\t\t}\n \t\t\t\tif op == Over {\n \t\t\t\t\tdr, dg, db, da := dst.At(x, y).RGBA()\n \t\t\t\t\ta := m - (sa * ma / m)\n@@ -163,7 +195,11 @@ func DrawMask(dst Image, r image.Rectangle, src image.Image, sp image.Point, mas
\t\t\t\t\tout.B = uint16(sb * ma / m)\n \t\t\t\t\tout.A = uint16(sa * ma / m)\n \t\t\t\t}\n-\t\t\t\tdst.Set(x, y, out)\n+\t\t\t\t// The third argument is &out instead of out (and out is\n+\t\t\t\t// declared outside of the inner loop) to avoid the implicit\n+\t\t\t\t// conversion to color.Color here allocating memory in the\n+\t\t\t\t// inner loop if sizeof(color.RGBA64) > sizeof(uintptr).\n+\t\t\t\tdst.Set(x, y, &out)\n \t\t\t}\n \t\t}\n \t}\n@@ -500,3 +536,131 @@ func drawRGBA(dst *image.RGBA, r image.Rectangle, src image.Image, sp image.Poin\n \t\ti0 += dy * dst.Stride\n \t}\n }\n+\n+// clamp clamps i to the interval [0, 0xffff].\n+func clamp(i int32) int32 {\n+\tif i < 0 {\n+\t\treturn 0\n+\t}\n+\tif i > 0xffff {\n+\t\treturn 0xffff\n+\t}\n+\treturn i\n+}\n+\n+func drawPaletted(dst Image, r image.Rectangle, src image.Image, sp image.Point, floydSteinberg bool) {\n+\t// TODO(nigeltao): handle the case where the dst and src overlap.\n+\t// Does it even make sense to try and do Floyd-Steinberg whilst\n+\t// walking the image backward (right-to-left bottom-to-top)?\n+\n+\t// If dst is an *image.Paletted, we have a fast path for dst.Set and\n+\t// dst.At. The dst.Set equivalent is a batch version of the algorithm\n+\t// used by color.Palette's Index method in image/color/color.go, plus\n+\t// optional Floyd-Steinberg error diffusion.\n+\tpalette, pix, stride := [][3]int32(nil), []byte(nil), 0\n+\tif p, ok := dst.(*image.Paletted); ok {\n+\t\tpalette = make([][3]int32, len(p.Palette))\n+\t\tfor i, col := range p.Palette {\n+\t\t\tr, g, b, _ := col.RGBA()\n+\t\t\tpalette[i][0] = int32(r)\n+\t\t\tpalette[i][1] = int32(g)\n+\t\t\tpalette[i][2] = int32(b)\n+\t\t}\n+\t\tpix, stride = p.Pix[p.PixOffset(r.Min.X, r.Min.Y):], p.Stride\n+\t}\n+\n+\t// quantErrorCurr and quantErrorNext are the Floyd-Steinberg quantization\n+\t// errors that have been propagated to the pixels in the current and next\n+\t// rows. The +2 simplifies calculation near the edges.\n+\tvar quantErrorCurr, quantErrorNext [][3]int32\n+\tif floydSteinberg {\n+\t\tquantErrorCurr = make([][3]int32, r.Dx()+2)\n+\t\tquantErrorNext = make([][3]int32, r.Dx()+2)\n+\t}\n+\n+\t// Loop over each source pixel.\n+\tout := color.RGBA64{A: 0xffff}\n+\tfor y := 0; y != r.Dy(); y++ {\n+\t\tfor x := 0; x != r.Dx(); x++ {\n+\t\t\t// er, eg and eb are the pixel's R,G,B values plus the\n+\t\t\t// optional Floyd-Steinberg error.\n+\t\t\tsr, sg, sb, _ := src.At(sp.X+x, sp.Y+y).RGBA()\n+\t\t\ter, eg, eb := int32(sr), int32(sg), int32(sb)\n+\t\t\tif floydSteinberg {\n+\t\t\t\ter = clamp(er + quantErrorCurr[x+1][0]/16)\n+\t\t\t\teg = clamp(eg + quantErrorCurr[x+1][1]/16)\n+\t\t\t\teb = clamp(eb + quantErrorCurr[x+1][2]/16)\n+\t\t\t}\n+\n+\t\t\tif palette != nil {\n+\t\t\t\t// Find the closest palette color in Euclidean R,G,B space: the\n+\t\t\t\t// one that minimizes sum-squared-difference. We shift by 1 bit\n+\t\t\t\t// to avoid potential uint32 overflow in sum-squared-difference.\n+\t\t\t\t// TODO(nigeltao): consider smarter algorithms.\n+\t\t\t\tbestIndex, bestSSD := 0, uint32(1<<32-1)\n+\t\t\t\tfor index, p := range palette {\n+\t\t\t\t\tdelta := (er - p[0]) >> 1\n+\t\t\t\t\tssd := uint32(delta * delta)\n+\t\t\t\t\tdelta = (eg - p[1]) >> 1\n+\t\t\t\t\tssd += uint32(delta * delta)\n+\t\t\t\t\tdelta = (eb - p[2]) >> 1\n+\t\t\t\t\tssd += uint32(delta * delta)\n+\t\t\t\t\tif ssd < bestSSD {\n+\t\t\t\t\t\tbestIndex, bestSSD = index, ssd\n+\t\t\t\t\t\tif ssd == 0 {\n+\t\t\t\t\t\t\tbreak\n+\t\t\t\t\t\t}\n+\t\t\t\t\t}\n+\t\t\t\t}\n+\t\t\t\tpix[y*stride+x] = byte(bestIndex)\n+\n+\t\t\t\tif !floydSteinberg {\n+\t\t\t\t\tcontinue\n+\t\t\t\t}\n+\t\t\t\ter -= int32(palette[bestIndex][0])\n+\t\t\t\teg -= int32(palette[bestIndex][1])\n+\t\t\t\teb -= int32(palette[bestIndex][2])\n+\n+\t\t\t} else {\n+\t\t\t\tout.R = uint16(er)\n+\t\t\t\tout.G = uint16(eg)\n+\t\t\t\tout.B = uint16(eb)\n+\t\t\t\t// The third argument is &out instead of out (and out is\n+\t\t\t\t// declared outside of the inner loop) to avoid the implicit\n+\t\t\t\t// conversion to color.Color here allocating memory in the\n+\t\t\t\t// inner loop if sizeof(color.RGBA64) > sizeof(uintptr).\n+\t\t\t\tdst.Set(r.Min.X+x, r.Min.Y+y, &out)\n+\n+\t\t\t\tif !floydSteinberg {\n+\t\t\t\t\tcontinue\n+\t\t\t\t}\n+\t\t\t\tsr, sg, sb, _ = dst.At(r.Min.X+x, r.Min.Y+y).RGBA()\n+\t\t\t\ter -= int32(sr)\n+\t\t\t\teg -= int32(sg)\n+\t\t\t\teb -= int32(sb)\n+\t\t\t}\n+\n+\t\t\t// Propagate the Floyd-Steinberg quantization error.\n+\t\t\tquantErrorNext[x+0][0] += er * 3\n+\t\t\tquantErrorNext[x+0][1] += eg * 3\n+\t\t\tquantErrorNext[x+0][2] += eb * 3\n+\t\t\tquantErrorNext[x+1][0] += er * 5\n+\t\t\tquantErrorNext[x+1][1] += eg * 5\n+\t\t\tquantErrorNext[x+1][2] += eb * 5\n+\t\t\tquantErrorNext[x+2][0] += er * 1\n+\t\t\tquantErrorNext[x+2][1] += eg * 1\n+\t\t\tquantErrorNext[x+2][2] += eb * 1\n+\t\t\tquantErrorCurr[x+2][0] += er * 7\n+\t\t\tquantErrorCurr[x+2][1] += eg * 7\n+\t\t\tquantErrorCurr[x+2][2] += eb * 7\n+\t\t}\n+\n+\t\t// Recycle the quantization error buffers.\n+\t\tif floydSteinberg {\n+\t\t\tquantErrorCurr, quantErrorNext = quantErrorNext, quantErrorCurr\n+\t\t\tfor i := range quantErrorNext {\n+\t\t\t\tquantErrorNext[i] = [3]int32{}\n+\t\t\t}\n+\t\t}\n+\t}\n+}\n```
## コアとなるコードの解説
### `image/color/color.go`の変更点
* `diff`関数の削除: 以前は`diff`関数で2つの`uint32`値の絶対差を計算していましたが、これが削除されました。
* `Palette.Index`メソッドの変更:
* `cr, cg, cb, _ := c.RGBA()`: 入力色`c`のRGBA値を`uint32`で取得します。
* `delta := (int32(cr) - int32(vr)) >> 1`: ここが重要な変更点です。`uint32`のまま差分を計算するとオーバーフローの可能性があるため、まず`int32`にキャストして差分を計算します。その後、`>> 1`(1ビット右シフト)を行うことで、値を半分にします。これは、後続の二乗和差(SSD)計算で`uint32`のオーバーフローを防ぐための工夫です。
* `ssd := uint32(delta * delta)`: `delta`の二乗を`uint32`にキャストして`ssd`に加算します。この計算をR, G, Bの各成分に対して行い、最もSSDが小さいパレット色を見つけます。この変更は、`image/draw/draw.go`で実装されるバッチ処理と整合性を保つためのものです。
### `image/draw/draw.go`の変更点
* **`Image`インターフェースの再定義**:
`type Image interface { image.Image; Set(x, y int, c color.Color) }`
このインターフェースは、`image.Image`の機能に加えて、特定のピクセルを設定する`Set`メソッドを持つことを保証します。これは、描画操作のデスティネーション(描画先)となる画像が、ピクセル単位で変更可能であることを示すために必要です。
* **`Op.Draw`メソッド**:
`func (op Op) Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point)`
`Op`型に`Draw`メソッドが追加されたことで、`Op`が`Drawer`インターフェースを満たすようになりました。これにより、`draw.Over.Draw(dst, r, src, sp)`のように、Porter-Duff合成演算子を直接描画操作として使用できるようになります。内部的には、既存の`DrawMask`関数を呼び出しています。
* **`Drawer`インターフェース**:
`type Drawer interface { Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point) }`
これは、任意の描画ロジックを抽象化するための新しいインターフェースです。これにより、`Op`だけでなく、Floyd-Steinbergのような異なる描画アルゴリズムも、この統一されたインターフェースを通じて利用できるようになります。
* **`FloydSteinberg`の実装**:
`var FloydSteinberg Drawer = floydSteinberg{}`
`FloydSteinberg`というグローバル変数として、`Drawer`インターフェースを実装した`floydSteinberg`型のインスタンスが提供されます。この`floydSteinberg`型の`Draw`メソッドは、Floyd-Steinberg誤差拡散を実行するためのエントリポイントとなります。
* **`floydSteinberg.Draw`メソッド**:
このメソッドは、`clip`関数で描画領域をクリップした後、`drawPaletted`関数を呼び出します。`drawPaletted`の最後の引数`true`は、Floyd-Steinberg誤差拡散を適用することを示します。
* **`clamp`関数**:
`func clamp(i int32) int32 { ... }`
このヘルパー関数は、`int32`の値を`[0, 0xffff]`(0から65535)の範囲に制限します。これは、色成分がこの範囲を超えることを防ぎ、有効な色値に収めるために使用されます。
* **`drawPaletted`関数**:
この関数は、パレット画像への描画とFloyd-Steinberg誤差拡散の核心部分です。
* **パレットの準備**: `dst`が`*image.Paletted`型の場合、そのパレットを`[][3]int32`形式に変換して`palette`変数に格納します。これにより、パレット色のR, G, B成分を`int32`として効率的に扱えます。
* **誤差バッファの初期化**: `floydSteinberg`が`true`の場合、`quantErrorCurr`と`quantErrorNext`という2つの`[][3]int32`スライスを初期化します。これらは、現在の行と次の行に伝播される誤差を保持するためのバッファです。`r.Dx()+2`のサイズは、境界条件の計算を簡素化するためです。
* **ピクセルループ**: `y`と`x`の2重ループで、描画領域内の各ピクセルを処理します。
* **誤差の適用**: `sr, sg, sb, _ := src.At(sp.X+x, sp.Y+y).RGBA()`でソースピクセルのRGBA値を取得し、`er, eg, eb`に格納します。`floydSteinberg`が`true`の場合、`quantErrorCurr[x+1]`に格納されている誤差を現在のピクセルに加算し、`clamp`関数で値を正規化します。
* **パレットへの量子化**: `palette != nil`の場合(つまり、デスティネーションがパレット画像の場合)、現在のピクセル`er, eg, eb`に最も近いパレット色を探索します。`image/color/color.go`の`Palette.Index`と同様に、`int32`にキャストして1ビット右シフトした差分の二乗和差(SSD)を計算し、最適なパレット色を見つけます。見つかったパレット色のインデックスを`dst.Pix`に書き込みます。
* **誤差の計算と拡散**: パレットに量子化された色と元のピクセル(誤差が適用された後の`er, eg, eb`)との差を計算し、その誤差をFloyd-Steinbergの比率(7/16, 3/16, 5/16, 1/16)に従って、周囲のピクセル(右、下、左下、右下)の誤差バッファ`quantErrorNext`と`quantErrorCurr`に加算します。
* **誤差バッファの交換**: 各行の処理が終わると、`quantErrorCurr`と`quantErrorNext`を交換し、`quantErrorNext`をリセットして、次の行の処理に備えます。
* **`DrawMask`関数の高速パス**:
`DrawMask`関数内で、`dst`の型を`switch`文でチェックするようになりました。
* `case *image.RGBA:`: 既存のRGBA画像に対する高速パスを維持します。
* `case *image.Paletted:`: `op == Src`かつ`mask == nil`の場合に、新しく追加された`drawPaletted`関数を呼び出す高速パスが追加されました。これにより、パレット画像への直接描画が最適化されます。
* **`processBackward`関数の追加**:
`func processBackward(dst Image, r image.Rectangle, src image.Image, sp image.Point) bool`
この関数は、描画元と描画先が重なっている場合に、描画を逆方向(右から左、下から上)に行う必要があるかどうかを判断します。これは、画像の一部を自分自身にコピーするような操作で、正しい結果を得るために重要です。
## 関連リンク
* Go言語 `image` パッケージのドキュメント: [https://pkg.go.dev/image](https://pkg.go.dev/image)
* Go言語 `image/draw` パッケージのドキュメント: [https://pkg.go.dev/image/draw](https://pkg.go.dev/image/draw)
* Go言語 `image/color` パッケージのドキュメント: [https://pkg.go.dev/image/color](https://pkg.go.dev/image/color)
* Porter-Duff Compositing Operations (Wikipedia): [https://en.wikipedia.org/wiki/Porter%E2%80%93Duff_compositing](https://en.wikipedia.org/wiki/Porter%E2%80%93Duff_compositing)
* Floyd–Steinberg dithering (Wikipedia): [https://en.wikipedia.org/wiki/Floyd%E2%80%93Steinberg_dithering](https://en.wikipedia.org/wiki/Floyd%E2%80%93Steinberg_dithering)
## 参考にした情報源リンク
* Go言語の公式ドキュメント (上記「関連リンク」に記載)
* Wikipedia (上記「関連リンク」に記載)
* コミットメッセージと差分情報 (`./commit_data/16727.txt`)