diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-16 19:23:18 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-16 19:23:18 +0000 |
commit | 43a123c1ae6613b3efeed291fa552ecd909d3acf (patch) | |
tree | fd92518b7024bc74031f78a1cf9e454b65e73665 /src/image/draw | |
parent | Initial commit. (diff) | |
download | golang-1.20-upstream.tar.xz golang-1.20-upstream.zip |
Adding upstream version 1.20.14.upstream/1.20.14upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/image/draw')
-rw-r--r-- | src/image/draw/bench_test.go | 275 | ||||
-rw-r--r-- | src/image/draw/clip_test.go | 205 | ||||
-rw-r--r-- | src/image/draw/draw.go | 1086 | ||||
-rw-r--r-- | src/image/draw/draw_test.go | 810 | ||||
-rw-r--r-- | src/image/draw/example_test.go | 48 |
5 files changed, 2424 insertions, 0 deletions
diff --git a/src/image/draw/bench_test.go b/src/image/draw/bench_test.go new file mode 100644 index 0000000..55d25b8 --- /dev/null +++ b/src/image/draw/bench_test.go @@ -0,0 +1,275 @@ +// Copyright 2011 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package draw + +import ( + "image" + "image/color" + "reflect" + "testing" +) + +const ( + dstw, dsth = 640, 480 + srcw, srch = 400, 300 +) + +var palette = color.Palette{ + color.Black, + color.White, +} + +// bench benchmarks drawing src and mask images onto a dst image with the +// given op and the color models to create those images from. +// The created images' pixels are initialized to non-zero values. +func bench(b *testing.B, dcm, scm, mcm color.Model, op Op) { + b.StopTimer() + + var dst Image + switch dcm { + case color.RGBAModel: + dst1 := image.NewRGBA(image.Rect(0, 0, dstw, dsth)) + for y := 0; y < dsth; y++ { + for x := 0; x < dstw; x++ { + dst1.SetRGBA(x, y, color.RGBA{ + uint8(5 * x % 0x100), + uint8(7 * y % 0x100), + uint8((7*x + 5*y) % 0x100), + 0xff, + }) + } + } + dst = dst1 + case color.RGBA64Model: + dst1 := image.NewRGBA64(image.Rect(0, 0, dstw, dsth)) + for y := 0; y < dsth; y++ { + for x := 0; x < dstw; x++ { + dst1.SetRGBA64(x, y, color.RGBA64{ + uint16(53 * x % 0x10000), + uint16(59 * y % 0x10000), + uint16((59*x + 53*y) % 0x10000), + 0xffff, + }) + } + } + dst = dst1 + default: + // The == operator isn't defined on a color.Palette (a slice), so we + // use reflection. + if reflect.DeepEqual(dcm, palette) { + dst1 := image.NewPaletted(image.Rect(0, 0, dstw, dsth), palette) + for y := 0; y < dsth; y++ { + for x := 0; x < dstw; x++ { + dst1.SetColorIndex(x, y, uint8(x^y)&1) + } + } + dst = dst1 + } else { + b.Fatal("unknown destination color model", dcm) + } + } + + var src image.Image + switch scm { + case nil: + src = &image.Uniform{C: color.RGBA{0x11, 0x22, 0x33, 0x44}} + case color.CMYKModel: + src1 := image.NewCMYK(image.Rect(0, 0, srcw, srch)) + for y := 0; y < srch; y++ { + for x := 0; x < srcw; x++ { + src1.SetCMYK(x, y, color.CMYK{ + uint8(13 * x % 0x100), + uint8(11 * y % 0x100), + uint8((11*x + 13*y) % 0x100), + uint8((31*x + 37*y) % 0x100), + }) + } + } + src = src1 + case color.GrayModel: + src1 := image.NewGray(image.Rect(0, 0, srcw, srch)) + for y := 0; y < srch; y++ { + for x := 0; x < srcw; x++ { + src1.SetGray(x, y, color.Gray{ + uint8((11*x + 13*y) % 0x100), + }) + } + } + src = src1 + case color.RGBAModel: + src1 := image.NewRGBA(image.Rect(0, 0, srcw, srch)) + for y := 0; y < srch; y++ { + for x := 0; x < srcw; x++ { + src1.SetRGBA(x, y, color.RGBA{ + uint8(13 * x % 0x80), + uint8(11 * y % 0x80), + uint8((11*x + 13*y) % 0x80), + 0x7f, + }) + } + } + src = src1 + case color.RGBA64Model: + src1 := image.NewRGBA64(image.Rect(0, 0, srcw, srch)) + for y := 0; y < srch; y++ { + for x := 0; x < srcw; x++ { + src1.SetRGBA64(x, y, color.RGBA64{ + uint16(103 * x % 0x8000), + uint16(101 * y % 0x8000), + uint16((101*x + 103*y) % 0x8000), + 0x7fff, + }) + } + } + src = src1 + case color.NRGBAModel: + src1 := image.NewNRGBA(image.Rect(0, 0, srcw, srch)) + for y := 0; y < srch; y++ { + for x := 0; x < srcw; x++ { + src1.SetNRGBA(x, y, color.NRGBA{ + uint8(13 * x % 0x100), + uint8(11 * y % 0x100), + uint8((11*x + 13*y) % 0x100), + 0x7f, + }) + } + } + src = src1 + case color.YCbCrModel: + yy := make([]uint8, srcw*srch) + cb := make([]uint8, srcw*srch) + cr := make([]uint8, srcw*srch) + for i := range yy { + yy[i] = uint8(3 * i % 0x100) + cb[i] = uint8(5 * i % 0x100) + cr[i] = uint8(7 * i % 0x100) + } + src = &image.YCbCr{ + Y: yy, + Cb: cb, + Cr: cr, + YStride: srcw, + CStride: srcw, + SubsampleRatio: image.YCbCrSubsampleRatio444, + Rect: image.Rect(0, 0, srcw, srch), + } + default: + b.Fatal("unknown source color model", scm) + } + + var mask image.Image + switch mcm { + case nil: + // No-op. + case color.AlphaModel: + mask1 := image.NewAlpha(image.Rect(0, 0, srcw, srch)) + for y := 0; y < srch; y++ { + for x := 0; x < srcw; x++ { + a := uint8((23*x + 29*y) % 0x100) + // Glyph masks are typically mostly zero, + // so we only set a quarter of mask1's pixels. + if a >= 0xc0 { + mask1.SetAlpha(x, y, color.Alpha{a}) + } + } + } + mask = mask1 + default: + b.Fatal("unknown mask color model", mcm) + } + + b.StartTimer() + for i := 0; i < b.N; i++ { + // Scatter the destination rectangle to draw into. + x := 3 * i % (dstw - srcw) + y := 7 * i % (dsth - srch) + + DrawMask(dst, dst.Bounds().Add(image.Pt(x, y)), src, image.ZP, mask, image.ZP, op) + } +} + +// The BenchmarkFoo functions exercise a drawFoo fast-path function in draw.go. + +func BenchmarkFillOver(b *testing.B) { + bench(b, color.RGBAModel, nil, nil, Over) +} + +func BenchmarkFillSrc(b *testing.B) { + bench(b, color.RGBAModel, nil, nil, Src) +} + +func BenchmarkCopyOver(b *testing.B) { + bench(b, color.RGBAModel, color.RGBAModel, nil, Over) +} + +func BenchmarkCopySrc(b *testing.B) { + bench(b, color.RGBAModel, color.RGBAModel, nil, Src) +} + +func BenchmarkNRGBAOver(b *testing.B) { + bench(b, color.RGBAModel, color.NRGBAModel, nil, Over) +} + +func BenchmarkNRGBASrc(b *testing.B) { + bench(b, color.RGBAModel, color.NRGBAModel, nil, Src) +} + +func BenchmarkYCbCr(b *testing.B) { + bench(b, color.RGBAModel, color.YCbCrModel, nil, Over) +} + +func BenchmarkGray(b *testing.B) { + bench(b, color.RGBAModel, color.GrayModel, nil, Over) +} + +func BenchmarkCMYK(b *testing.B) { + bench(b, color.RGBAModel, color.CMYKModel, nil, Over) +} + +func BenchmarkGlyphOver(b *testing.B) { + bench(b, color.RGBAModel, nil, color.AlphaModel, Over) +} + +func BenchmarkRGBAMaskOver(b *testing.B) { + bench(b, color.RGBAModel, color.RGBAModel, color.AlphaModel, Over) +} + +func BenchmarkGrayMaskOver(b *testing.B) { + bench(b, color.RGBAModel, color.GrayModel, color.AlphaModel, Over) +} + +func BenchmarkRGBA64ImageMaskOver(b *testing.B) { + bench(b, color.RGBAModel, color.RGBA64Model, color.AlphaModel, Over) +} + +func BenchmarkRGBA(b *testing.B) { + bench(b, color.RGBAModel, color.RGBA64Model, nil, Src) +} + +func BenchmarkPalettedFill(b *testing.B) { + bench(b, palette, nil, nil, Src) +} + +func BenchmarkPalettedRGBA(b *testing.B) { + bench(b, palette, color.RGBAModel, nil, Src) +} + +// The BenchmarkGenericFoo functions exercise the generic, slow-path code. + +func BenchmarkGenericOver(b *testing.B) { + bench(b, color.RGBA64Model, color.RGBA64Model, nil, Over) +} + +func BenchmarkGenericMaskOver(b *testing.B) { + bench(b, color.RGBA64Model, color.RGBA64Model, color.AlphaModel, Over) +} + +func BenchmarkGenericSrc(b *testing.B) { + bench(b, color.RGBA64Model, color.RGBA64Model, nil, Src) +} + +func BenchmarkGenericMaskSrc(b *testing.B) { + bench(b, color.RGBA64Model, color.RGBA64Model, color.AlphaModel, Src) +} diff --git a/src/image/draw/clip_test.go b/src/image/draw/clip_test.go new file mode 100644 index 0000000..0abf53e --- /dev/null +++ b/src/image/draw/clip_test.go @@ -0,0 +1,205 @@ +// Copyright 2011 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package draw + +import ( + "image" + "testing" +) + +type clipTest struct { + desc string + r, dr, sr, mr image.Rectangle + sp, mp image.Point + nilMask bool + r0 image.Rectangle + sp0, mp0 image.Point +} + +var clipTests = []clipTest{ + // The following tests all have a nil mask. + { + "basic", + image.Rect(0, 0, 100, 100), + image.Rect(0, 0, 100, 100), + image.Rect(0, 0, 100, 100), + image.ZR, + image.ZP, + image.ZP, + true, + image.Rect(0, 0, 100, 100), + image.ZP, + image.ZP, + }, + { + "clip dr", + image.Rect(0, 0, 100, 100), + image.Rect(40, 40, 60, 60), + image.Rect(0, 0, 100, 100), + image.ZR, + image.ZP, + image.ZP, + true, + image.Rect(40, 40, 60, 60), + image.Pt(40, 40), + image.ZP, + }, + { + "clip sr", + image.Rect(0, 0, 100, 100), + image.Rect(0, 0, 100, 100), + image.Rect(20, 20, 80, 80), + image.ZR, + image.ZP, + image.ZP, + true, + image.Rect(20, 20, 80, 80), + image.Pt(20, 20), + image.ZP, + }, + { + "clip dr and sr", + image.Rect(0, 0, 100, 100), + image.Rect(0, 0, 50, 100), + image.Rect(20, 20, 80, 80), + image.ZR, + image.ZP, + image.ZP, + true, + image.Rect(20, 20, 50, 80), + image.Pt(20, 20), + image.ZP, + }, + { + "clip dr and sr, sp outside sr (top-left)", + image.Rect(0, 0, 100, 100), + image.Rect(0, 0, 50, 100), + image.Rect(20, 20, 80, 80), + image.ZR, + image.Pt(15, 8), + image.ZP, + true, + image.Rect(5, 12, 50, 72), + image.Pt(20, 20), + image.ZP, + }, + { + "clip dr and sr, sp outside sr (middle-left)", + image.Rect(0, 0, 100, 100), + image.Rect(0, 0, 50, 100), + image.Rect(20, 20, 80, 80), + image.ZR, + image.Pt(15, 66), + image.ZP, + true, + image.Rect(5, 0, 50, 14), + image.Pt(20, 66), + image.ZP, + }, + { + "clip dr and sr, sp outside sr (bottom-left)", + image.Rect(0, 0, 100, 100), + image.Rect(0, 0, 50, 100), + image.Rect(20, 20, 80, 80), + image.ZR, + image.Pt(15, 91), + image.ZP, + true, + image.ZR, + image.Pt(15, 91), + image.ZP, + }, + { + "clip dr and sr, sp inside sr", + image.Rect(0, 0, 100, 100), + image.Rect(0, 0, 50, 100), + image.Rect(20, 20, 80, 80), + image.ZR, + image.Pt(44, 33), + image.ZP, + true, + image.Rect(0, 0, 36, 47), + image.Pt(44, 33), + image.ZP, + }, + + // The following tests all have a non-nil mask. + { + "basic mask", + image.Rect(0, 0, 80, 80), + image.Rect(20, 0, 100, 80), + image.Rect(0, 0, 50, 49), + image.Rect(0, 0, 46, 47), + image.ZP, + image.ZP, + false, + image.Rect(20, 0, 46, 47), + image.Pt(20, 0), + image.Pt(20, 0), + }, + { + "clip sr and mr", + image.Rect(0, 0, 100, 100), + image.Rect(0, 0, 100, 100), + image.Rect(23, 23, 55, 86), + image.Rect(44, 44, 87, 58), + image.Pt(10, 10), + image.Pt(11, 11), + false, + image.Rect(33, 33, 45, 47), + image.Pt(43, 43), + image.Pt(44, 44), + }, +} + +func TestClip(t *testing.T) { + dst0 := image.NewRGBA(image.Rect(0, 0, 100, 100)) + src0 := image.NewRGBA(image.Rect(0, 0, 100, 100)) + mask0 := image.NewRGBA(image.Rect(0, 0, 100, 100)) + for _, c := range clipTests { + dst := dst0.SubImage(c.dr).(*image.RGBA) + src := src0.SubImage(c.sr).(*image.RGBA) + r, sp, mp := c.r, c.sp, c.mp + if c.nilMask { + clip(dst, &r, src, &sp, nil, nil) + } else { + clip(dst, &r, src, &sp, mask0.SubImage(c.mr), &mp) + } + + // Check that the actual results equal the expected results. + if !c.r0.Eq(r) { + t.Errorf("%s: clip rectangle want %v got %v", c.desc, c.r0, r) + continue + } + if !c.sp0.Eq(sp) { + t.Errorf("%s: sp want %v got %v", c.desc, c.sp0, sp) + continue + } + if !c.nilMask { + if !c.mp0.Eq(mp) { + t.Errorf("%s: mp want %v got %v", c.desc, c.mp0, mp) + continue + } + } + + // Check that the clipped rectangle is contained by the dst / src / mask + // rectangles, in their respective coordinate spaces. + if !r.In(c.dr) { + t.Errorf("%s: c.dr %v does not contain r %v", c.desc, c.dr, r) + } + // sr is r translated into src's coordinate space. + sr := r.Add(c.sp.Sub(c.dr.Min)) + if !sr.In(c.sr) { + t.Errorf("%s: c.sr %v does not contain sr %v", c.desc, c.sr, sr) + } + if !c.nilMask { + // mr is r translated into mask's coordinate space. + mr := r.Add(c.mp.Sub(c.dr.Min)) + if !mr.In(c.mr) { + t.Errorf("%s: c.mr %v does not contain mr %v", c.desc, c.mr, mr) + } + } + } +} diff --git a/src/image/draw/draw.go b/src/image/draw/draw.go new file mode 100644 index 0000000..920ebb9 --- /dev/null +++ b/src/image/draw/draw.go @@ -0,0 +1,1086 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package draw provides image composition functions. +// +// See "The Go image/draw package" for an introduction to this package: +// https://golang.org/doc/articles/image_draw.html +package draw + +import ( + "image" + "image/color" + "image/internal/imageutil" +) + +// m is the maximum color value returned by image.Color.RGBA. +const m = 1<<16 - 1 + +// 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) +} + +// RGBA64Image extends both the Image and image.RGBA64Image interfaces with a +// SetRGBA64 method to change a single pixel. SetRGBA64 is equivalent to +// calling Set, but it can avoid allocations from converting concrete color +// types to the color.Color interface type. +type RGBA64Image interface { + image.RGBA64Image + Set(x, y int, c color.Color) + SetRGBA64(x, y int, c color.RGBA64) +} + +// Quantizer produces a palette for an image. +type Quantizer interface { + // Quantize appends up to cap(p) - len(p) colors to p and returns the + // updated palette suitable for converting m to a paletted image. + Quantize(p color.Palette, m image.Image) color.Palette +} + +// Op is a Porter-Duff compositing operator. +type Op int + +const ( + // Over specifies ``(src in mask) over dst''. + Over Op = iota + // Src specifies ``src in mask''. + Src +) + +// 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) +} + +// 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 +// destination image's coordinate space) and shifts the points sp and mp by +// the same amount as the change in r.Min. +func clip(dst Image, r *image.Rectangle, src image.Image, sp *image.Point, mask image.Image, mp *image.Point) { + orig := r.Min + *r = r.Intersect(dst.Bounds()) + *r = r.Intersect(src.Bounds().Add(orig.Sub(*sp))) + if mask != nil { + *r = r.Intersect(mask.Bounds().Add(orig.Sub(*mp))) + } + dx := r.Min.X - orig.X + dy := r.Min.Y - orig.Y + if dx == 0 && dy == 0 { + return + } + sp.X += dx + sp.Y += dy + if mp != nil { + mp.X += dx + mp.Y += dy + } +} + +func processBackward(dst image.Image, r image.Rectangle, src image.Image, sp image.Point) bool { + return 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) { + clip(dst, &r, src, &sp, mask, &mp) + if r.Empty() { + return + } + + // Fast paths for special cases. If none of them apply, then we fall back + // to general but slower implementations. + // + // For NRGBA and NRGBA64 image types, the code paths aren't just faster. + // They also avoid the information loss that would otherwise occur from + // converting non-alpha-premultiplied color to and from alpha-premultiplied + // color. See TestDrawSrcNonpremultiplied. + switch dst0 := dst.(type) { + case *image.RGBA: + if op == Over { + if mask == nil { + switch src0 := src.(type) { + case *image.Uniform: + sr, sg, sb, sa := src0.RGBA() + if sa == 0xffff { + drawFillSrc(dst0, r, sr, sg, sb, sa) + } else { + drawFillOver(dst0, r, sr, sg, sb, sa) + } + return + case *image.RGBA: + drawCopyOver(dst0, r, src0, sp) + return + case *image.NRGBA: + drawNRGBAOver(dst0, r, src0, sp) + return + case *image.YCbCr: + // An image.YCbCr is always fully opaque, and so if the + // mask is nil (i.e. fully opaque) then the op is + // effectively always Src. Similarly for image.Gray and + // image.CMYK. + if imageutil.DrawYCbCr(dst0, r, src0, sp) { + return + } + case *image.Gray: + drawGray(dst0, r, src0, sp) + return + case *image.CMYK: + drawCMYK(dst0, r, src0, sp) + return + } + } else if mask0, ok := mask.(*image.Alpha); ok { + switch src0 := src.(type) { + case *image.Uniform: + drawGlyphOver(dst0, r, src0, mask0, mp) + return + case *image.RGBA: + drawRGBAMaskOver(dst0, r, src0, sp, mask0, mp) + return + case *image.Gray: + drawGrayMaskOver(dst0, r, src0, sp, mask0, mp) + return + // Case order matters. The next case (image.RGBA64Image) is an + // interface type that the concrete types above also implement. + case image.RGBA64Image: + drawRGBA64ImageMaskOver(dst0, r, src0, sp, mask0, mp) + return + } + } + } else { + if mask == nil { + switch src0 := src.(type) { + case *image.Uniform: + sr, sg, sb, sa := src0.RGBA() + drawFillSrc(dst0, r, sr, sg, sb, sa) + return + case *image.RGBA: + d0 := dst0.PixOffset(r.Min.X, r.Min.Y) + s0 := src0.PixOffset(sp.X, sp.Y) + drawCopySrc( + dst0.Pix[d0:], dst0.Stride, r, src0.Pix[s0:], src0.Stride, sp, 4*r.Dx()) + return + case *image.NRGBA: + drawNRGBASrc(dst0, r, src0, sp) + return + case *image.YCbCr: + if imageutil.DrawYCbCr(dst0, r, src0, sp) { + return + } + case *image.Gray: + drawGray(dst0, r, src0, sp) + return + case *image.CMYK: + drawCMYK(dst0, r, src0, sp) + return + } + } + } + drawRGBA(dst0, r, src, sp, mask, mp, op) + return + case *image.Paletted: + if op == Src && mask == nil { + if src0, ok := src.(*image.Uniform); ok { + colorIndex := uint8(dst0.Palette.Index(src0.C)) + i0 := dst0.PixOffset(r.Min.X, r.Min.Y) + i1 := i0 + r.Dx() + for i := i0; i < i1; i++ { + dst0.Pix[i] = colorIndex + } + firstRow := dst0.Pix[i0:i1] + for y := r.Min.Y + 1; y < r.Max.Y; y++ { + i0 += dst0.Stride + i1 += dst0.Stride + copy(dst0.Pix[i0:i1], firstRow) + } + return + } else if !processBackward(dst, r, src, sp) { + drawPaletted(dst0, r, src, sp, false) + return + } + } + case *image.NRGBA: + if op == Src && mask == nil { + if src0, ok := src.(*image.NRGBA); ok { + d0 := dst0.PixOffset(r.Min.X, r.Min.Y) + s0 := src0.PixOffset(sp.X, sp.Y) + drawCopySrc( + dst0.Pix[d0:], dst0.Stride, r, src0.Pix[s0:], src0.Stride, sp, 4*r.Dx()) + return + } + } + case *image.NRGBA64: + if op == Src && mask == nil { + if src0, ok := src.(*image.NRGBA64); ok { + d0 := dst0.PixOffset(r.Min.X, r.Min.Y) + s0 := src0.PixOffset(sp.X, sp.Y) + drawCopySrc( + dst0.Pix[d0:], dst0.Stride, r, src0.Pix[s0:], src0.Stride, sp, 8*r.Dx()) + return + } + } + } + + x0, x1, dx := r.Min.X, r.Max.X, 1 + y0, y1, dy := r.Min.Y, r.Max.Y, 1 + if processBackward(dst, r, src, sp) { + x0, x1, dx = x1-1, x0-1, -1 + y0, y1, dy = y1-1, y0-1, -1 + } + + // FALLBACK1.17 + // + // Try the draw.RGBA64Image and image.RGBA64Image interfaces, part of the + // standard library since Go 1.17. These are like the draw.Image and + // image.Image interfaces but they can avoid allocations from converting + // concrete color types to the color.Color interface type. + + if dst0, _ := dst.(RGBA64Image); dst0 != nil { + if src0, _ := src.(image.RGBA64Image); src0 != nil { + if mask == nil { + sy := sp.Y + y0 - r.Min.Y + my := mp.Y + y0 - r.Min.Y + for y := y0; y != y1; y, sy, my = y+dy, sy+dy, my+dy { + sx := sp.X + x0 - r.Min.X + mx := mp.X + x0 - r.Min.X + for x := x0; x != x1; x, sx, mx = x+dx, sx+dx, mx+dx { + if op == Src { + dst0.SetRGBA64(x, y, src0.RGBA64At(sx, sy)) + } else { + srgba := src0.RGBA64At(sx, sy) + a := m - uint32(srgba.A) + drgba := dst0.RGBA64At(x, y) + dst0.SetRGBA64(x, y, color.RGBA64{ + R: uint16((uint32(drgba.R)*a)/m) + srgba.R, + G: uint16((uint32(drgba.G)*a)/m) + srgba.G, + B: uint16((uint32(drgba.B)*a)/m) + srgba.B, + A: uint16((uint32(drgba.A)*a)/m) + srgba.A, + }) + } + } + } + return + + } else if mask0, _ := mask.(image.RGBA64Image); mask0 != nil { + sy := sp.Y + y0 - r.Min.Y + my := mp.Y + y0 - r.Min.Y + for y := y0; y != y1; y, sy, my = y+dy, sy+dy, my+dy { + sx := sp.X + x0 - r.Min.X + mx := mp.X + x0 - r.Min.X + for x := x0; x != x1; x, sx, mx = x+dx, sx+dx, mx+dx { + ma := uint32(mask0.RGBA64At(mx, my).A) + switch { + case ma == 0: + if op == Over { + // No-op. + } else { + dst0.SetRGBA64(x, y, color.RGBA64{}) + } + case ma == m && op == Src: + dst0.SetRGBA64(x, y, src0.RGBA64At(sx, sy)) + default: + srgba := src0.RGBA64At(sx, sy) + if op == Over { + drgba := dst0.RGBA64At(x, y) + a := m - (uint32(srgba.A) * ma / m) + dst0.SetRGBA64(x, y, color.RGBA64{ + R: uint16((uint32(drgba.R)*a + uint32(srgba.R)*ma) / m), + G: uint16((uint32(drgba.G)*a + uint32(srgba.G)*ma) / m), + B: uint16((uint32(drgba.B)*a + uint32(srgba.B)*ma) / m), + A: uint16((uint32(drgba.A)*a + uint32(srgba.A)*ma) / m), + }) + } else { + dst0.SetRGBA64(x, y, color.RGBA64{ + R: uint16(uint32(srgba.R) * ma / m), + G: uint16(uint32(srgba.G) * ma / m), + B: uint16(uint32(srgba.B) * ma / m), + A: uint16(uint32(srgba.A) * ma / m), + }) + } + } + } + } + return + } + } + } + + // FALLBACK1.0 + // + // If none of the faster code paths above apply, use the draw.Image and + // image.Image interfaces, part of the standard library since Go 1.0. + + var out color.RGBA64 + sy := sp.Y + y0 - r.Min.Y + my := mp.Y + y0 - r.Min.Y + for y := y0; y != y1; y, sy, my = y+dy, sy+dy, my+dy { + sx := sp.X + x0 - r.Min.X + mx := mp.X + x0 - r.Min.X + for x := x0; x != x1; x, sx, mx = x+dx, sx+dx, mx+dx { + ma := uint32(m) + if mask != nil { + _, _, _, ma = mask.At(mx, my).RGBA() + } + switch { + case ma == 0: + if op == Over { + // No-op. + } else { + dst.Set(x, y, color.Transparent) + } + case ma == m && op == Src: + dst.Set(x, y, src.At(sx, sy)) + default: + sr, sg, sb, sa := src.At(sx, sy).RGBA() + if op == Over { + dr, dg, db, da := dst.At(x, y).RGBA() + a := m - (sa * ma / m) + out.R = uint16((dr*a + sr*ma) / m) + out.G = uint16((dg*a + sg*ma) / m) + out.B = uint16((db*a + sb*ma) / m) + out.A = uint16((da*a + sa*ma) / m) + } else { + out.R = uint16(sr * ma / m) + out.G = uint16(sg * ma / m) + out.B = uint16(sb * ma / m) + out.A = uint16(sa * ma / m) + } + // The third argument is &out instead of out (and out is + // declared outside of the inner loop) to avoid the implicit + // conversion to color.Color here allocating memory in the + // inner loop if sizeof(color.RGBA64) > sizeof(uintptr). + dst.Set(x, y, &out) + } + } + } +} + +func drawFillOver(dst *image.RGBA, r image.Rectangle, sr, sg, sb, sa uint32) { + // The 0x101 is here for the same reason as in drawRGBA. + a := (m - sa) * 0x101 + i0 := dst.PixOffset(r.Min.X, r.Min.Y) + i1 := i0 + r.Dx()*4 + for y := r.Min.Y; y != r.Max.Y; y++ { + for i := i0; i < i1; i += 4 { + dr := &dst.Pix[i+0] + dg := &dst.Pix[i+1] + db := &dst.Pix[i+2] + da := &dst.Pix[i+3] + + *dr = uint8((uint32(*dr)*a/m + sr) >> 8) + *dg = uint8((uint32(*dg)*a/m + sg) >> 8) + *db = uint8((uint32(*db)*a/m + sb) >> 8) + *da = uint8((uint32(*da)*a/m + sa) >> 8) + } + i0 += dst.Stride + i1 += dst.Stride + } +} + +func drawFillSrc(dst *image.RGBA, r image.Rectangle, sr, sg, sb, sa uint32) { + sr8 := uint8(sr >> 8) + sg8 := uint8(sg >> 8) + sb8 := uint8(sb >> 8) + sa8 := uint8(sa >> 8) + // The built-in copy function is faster than a straightforward for loop to fill the destination with + // the color, but copy requires a slice source. We therefore use a for loop to fill the first row, and + // then use the first row as the slice source for the remaining rows. + i0 := dst.PixOffset(r.Min.X, r.Min.Y) + i1 := i0 + r.Dx()*4 + for i := i0; i < i1; i += 4 { + dst.Pix[i+0] = sr8 + dst.Pix[i+1] = sg8 + dst.Pix[i+2] = sb8 + dst.Pix[i+3] = sa8 + } + firstRow := dst.Pix[i0:i1] + for y := r.Min.Y + 1; y < r.Max.Y; y++ { + i0 += dst.Stride + i1 += dst.Stride + copy(dst.Pix[i0:i1], firstRow) + } +} + +func drawCopyOver(dst *image.RGBA, r image.Rectangle, src *image.RGBA, sp image.Point) { + dx, dy := r.Dx(), r.Dy() + d0 := dst.PixOffset(r.Min.X, r.Min.Y) + s0 := src.PixOffset(sp.X, sp.Y) + var ( + ddelta, sdelta int + i0, i1, idelta int + ) + if r.Min.Y < sp.Y || r.Min.Y == sp.Y && r.Min.X <= sp.X { + ddelta = dst.Stride + sdelta = src.Stride + i0, i1, idelta = 0, dx*4, +4 + } else { + // If the source start point is higher than the destination start point, or equal height but to the left, + // then we compose the rows in right-to-left, bottom-up order instead of left-to-right, top-down. + d0 += (dy - 1) * dst.Stride + s0 += (dy - 1) * src.Stride + ddelta = -dst.Stride + sdelta = -src.Stride + i0, i1, idelta = (dx-1)*4, -4, -4 + } + for ; dy > 0; dy-- { + dpix := dst.Pix[d0:] + spix := src.Pix[s0:] + for i := i0; i != i1; i += idelta { + s := spix[i : i+4 : i+4] // Small cap improves performance, see https://golang.org/issue/27857 + sr := uint32(s[0]) * 0x101 + sg := uint32(s[1]) * 0x101 + sb := uint32(s[2]) * 0x101 + sa := uint32(s[3]) * 0x101 + + // The 0x101 is here for the same reason as in drawRGBA. + a := (m - sa) * 0x101 + + d := dpix[i : i+4 : i+4] // Small cap improves performance, see https://golang.org/issue/27857 + d[0] = uint8((uint32(d[0])*a/m + sr) >> 8) + d[1] = uint8((uint32(d[1])*a/m + sg) >> 8) + d[2] = uint8((uint32(d[2])*a/m + sb) >> 8) + d[3] = uint8((uint32(d[3])*a/m + sa) >> 8) + } + d0 += ddelta + s0 += sdelta + } +} + +// drawCopySrc copies bytes to dstPix from srcPix. These arguments roughly +// correspond to the Pix fields of the image package's concrete image.Image +// implementations, but are offset (dstPix is dst.Pix[dpOffset:] not dst.Pix). +func drawCopySrc( + dstPix []byte, dstStride int, r image.Rectangle, + srcPix []byte, srcStride int, sp image.Point, + bytesPerRow int) { + + d0, s0, ddelta, sdelta, dy := 0, 0, dstStride, srcStride, r.Dy() + if r.Min.Y > sp.Y { + // If the source start point is higher than the destination start + // point, then we compose the rows in bottom-up order instead of + // top-down. Unlike the drawCopyOver function, we don't have to check + // the x coordinates because the built-in copy function can handle + // overlapping slices. + d0 = (dy - 1) * dstStride + s0 = (dy - 1) * srcStride + ddelta = -dstStride + sdelta = -srcStride + } + for ; dy > 0; dy-- { + copy(dstPix[d0:d0+bytesPerRow], srcPix[s0:s0+bytesPerRow]) + d0 += ddelta + s0 += sdelta + } +} + +func drawNRGBAOver(dst *image.RGBA, r image.Rectangle, src *image.NRGBA, sp image.Point) { + i0 := (r.Min.X - dst.Rect.Min.X) * 4 + i1 := (r.Max.X - dst.Rect.Min.X) * 4 + si0 := (sp.X - src.Rect.Min.X) * 4 + yMax := r.Max.Y - dst.Rect.Min.Y + + y := r.Min.Y - dst.Rect.Min.Y + sy := sp.Y - src.Rect.Min.Y + for ; y != yMax; y, sy = y+1, sy+1 { + dpix := dst.Pix[y*dst.Stride:] + spix := src.Pix[sy*src.Stride:] + + for i, si := i0, si0; i < i1; i, si = i+4, si+4 { + // Convert from non-premultiplied color to pre-multiplied color. + s := spix[si : si+4 : si+4] // Small cap improves performance, see https://golang.org/issue/27857 + sa := uint32(s[3]) * 0x101 + sr := uint32(s[0]) * sa / 0xff + sg := uint32(s[1]) * sa / 0xff + sb := uint32(s[2]) * sa / 0xff + + d := dpix[i : i+4 : i+4] // Small cap improves performance, see https://golang.org/issue/27857 + dr := uint32(d[0]) + dg := uint32(d[1]) + db := uint32(d[2]) + da := uint32(d[3]) + + // The 0x101 is here for the same reason as in drawRGBA. + a := (m - sa) * 0x101 + + d[0] = uint8((dr*a/m + sr) >> 8) + d[1] = uint8((dg*a/m + sg) >> 8) + d[2] = uint8((db*a/m + sb) >> 8) + d[3] = uint8((da*a/m + sa) >> 8) + } + } +} + +func drawNRGBASrc(dst *image.RGBA, r image.Rectangle, src *image.NRGBA, sp image.Point) { + i0 := (r.Min.X - dst.Rect.Min.X) * 4 + i1 := (r.Max.X - dst.Rect.Min.X) * 4 + si0 := (sp.X - src.Rect.Min.X) * 4 + yMax := r.Max.Y - dst.Rect.Min.Y + + y := r.Min.Y - dst.Rect.Min.Y + sy := sp.Y - src.Rect.Min.Y + for ; y != yMax; y, sy = y+1, sy+1 { + dpix := dst.Pix[y*dst.Stride:] + spix := src.Pix[sy*src.Stride:] + + for i, si := i0, si0; i < i1; i, si = i+4, si+4 { + // Convert from non-premultiplied color to pre-multiplied color. + s := spix[si : si+4 : si+4] // Small cap improves performance, see https://golang.org/issue/27857 + sa := uint32(s[3]) * 0x101 + sr := uint32(s[0]) * sa / 0xff + sg := uint32(s[1]) * sa / 0xff + sb := uint32(s[2]) * sa / 0xff + + d := dpix[i : i+4 : i+4] // Small cap improves performance, see https://golang.org/issue/27857 + d[0] = uint8(sr >> 8) + d[1] = uint8(sg >> 8) + d[2] = uint8(sb >> 8) + d[3] = uint8(sa >> 8) + } + } +} + +func drawGray(dst *image.RGBA, r image.Rectangle, src *image.Gray, sp image.Point) { + i0 := (r.Min.X - dst.Rect.Min.X) * 4 + i1 := (r.Max.X - dst.Rect.Min.X) * 4 + si0 := (sp.X - src.Rect.Min.X) * 1 + yMax := r.Max.Y - dst.Rect.Min.Y + + y := r.Min.Y - dst.Rect.Min.Y + sy := sp.Y - src.Rect.Min.Y + for ; y != yMax; y, sy = y+1, sy+1 { + dpix := dst.Pix[y*dst.Stride:] + spix := src.Pix[sy*src.Stride:] + + for i, si := i0, si0; i < i1; i, si = i+4, si+1 { + p := spix[si] + d := dpix[i : i+4 : i+4] // Small cap improves performance, see https://golang.org/issue/27857 + d[0] = p + d[1] = p + d[2] = p + d[3] = 255 + } + } +} + +func drawCMYK(dst *image.RGBA, r image.Rectangle, src *image.CMYK, sp image.Point) { + i0 := (r.Min.X - dst.Rect.Min.X) * 4 + i1 := (r.Max.X - dst.Rect.Min.X) * 4 + si0 := (sp.X - src.Rect.Min.X) * 4 + yMax := r.Max.Y - dst.Rect.Min.Y + + y := r.Min.Y - dst.Rect.Min.Y + sy := sp.Y - src.Rect.Min.Y + for ; y != yMax; y, sy = y+1, sy+1 { + dpix := dst.Pix[y*dst.Stride:] + spix := src.Pix[sy*src.Stride:] + + for i, si := i0, si0; i < i1; i, si = i+4, si+4 { + s := spix[si : si+4 : si+4] // Small cap improves performance, see https://golang.org/issue/27857 + d := dpix[i : i+4 : i+4] + d[0], d[1], d[2] = color.CMYKToRGB(s[0], s[1], s[2], s[3]) + d[3] = 255 + } + } +} + +func drawGlyphOver(dst *image.RGBA, r image.Rectangle, src *image.Uniform, mask *image.Alpha, mp image.Point) { + i0 := dst.PixOffset(r.Min.X, r.Min.Y) + i1 := i0 + r.Dx()*4 + mi0 := mask.PixOffset(mp.X, mp.Y) + sr, sg, sb, sa := src.RGBA() + for y, my := r.Min.Y, mp.Y; y != r.Max.Y; y, my = y+1, my+1 { + for i, mi := i0, mi0; i < i1; i, mi = i+4, mi+1 { + ma := uint32(mask.Pix[mi]) + if ma == 0 { + continue + } + ma |= ma << 8 + + // The 0x101 is here for the same reason as in drawRGBA. + a := (m - (sa * ma / m)) * 0x101 + + d := dst.Pix[i : i+4 : i+4] // Small cap improves performance, see https://golang.org/issue/27857 + d[0] = uint8((uint32(d[0])*a + sr*ma) / m >> 8) + d[1] = uint8((uint32(d[1])*a + sg*ma) / m >> 8) + d[2] = uint8((uint32(d[2])*a + sb*ma) / m >> 8) + d[3] = uint8((uint32(d[3])*a + sa*ma) / m >> 8) + } + i0 += dst.Stride + i1 += dst.Stride + mi0 += mask.Stride + } +} + +func drawGrayMaskOver(dst *image.RGBA, r image.Rectangle, src *image.Gray, sp image.Point, mask *image.Alpha, mp image.Point) { + x0, x1, dx := r.Min.X, r.Max.X, 1 + y0, y1, dy := r.Min.Y, r.Max.Y, 1 + if r.Overlaps(r.Add(sp.Sub(r.Min))) { + if sp.Y < r.Min.Y || sp.Y == r.Min.Y && sp.X < r.Min.X { + x0, x1, dx = x1-1, x0-1, -1 + y0, y1, dy = y1-1, y0-1, -1 + } + } + + sy := sp.Y + y0 - r.Min.Y + my := mp.Y + y0 - r.Min.Y + sx0 := sp.X + x0 - r.Min.X + mx0 := mp.X + x0 - r.Min.X + sx1 := sx0 + (x1 - x0) + i0 := dst.PixOffset(x0, y0) + di := dx * 4 + for y := y0; y != y1; y, sy, my = y+dy, sy+dy, my+dy { + for i, sx, mx := i0, sx0, mx0; sx != sx1; i, sx, mx = i+di, sx+dx, mx+dx { + mi := mask.PixOffset(mx, my) + ma := uint32(mask.Pix[mi]) + ma |= ma << 8 + si := src.PixOffset(sx, sy) + sy := uint32(src.Pix[si]) + sy |= sy << 8 + sa := uint32(0xffff) + + d := dst.Pix[i : i+4 : i+4] // Small cap improves performance, see https://golang.org/issue/27857 + dr := uint32(d[0]) + dg := uint32(d[1]) + db := uint32(d[2]) + da := uint32(d[3]) + + // dr, dg, db and da are all 8-bit color at the moment, ranging in [0,255]. + // We work in 16-bit color, and so would normally do: + // dr |= dr << 8 + // and similarly for dg, db and da, but instead we multiply a + // (which is a 16-bit color, ranging in [0,65535]) by 0x101. + // This yields the same result, but is fewer arithmetic operations. + a := (m - (sa * ma / m)) * 0x101 + + d[0] = uint8((dr*a + sy*ma) / m >> 8) + d[1] = uint8((dg*a + sy*ma) / m >> 8) + d[2] = uint8((db*a + sy*ma) / m >> 8) + d[3] = uint8((da*a + sa*ma) / m >> 8) + } + i0 += dy * dst.Stride + } +} + +func drawRGBAMaskOver(dst *image.RGBA, r image.Rectangle, src *image.RGBA, sp image.Point, mask *image.Alpha, mp image.Point) { + x0, x1, dx := r.Min.X, r.Max.X, 1 + y0, y1, dy := r.Min.Y, r.Max.Y, 1 + if dst == src && r.Overlaps(r.Add(sp.Sub(r.Min))) { + if sp.Y < r.Min.Y || sp.Y == r.Min.Y && sp.X < r.Min.X { + x0, x1, dx = x1-1, x0-1, -1 + y0, y1, dy = y1-1, y0-1, -1 + } + } + + sy := sp.Y + y0 - r.Min.Y + my := mp.Y + y0 - r.Min.Y + sx0 := sp.X + x0 - r.Min.X + mx0 := mp.X + x0 - r.Min.X + sx1 := sx0 + (x1 - x0) + i0 := dst.PixOffset(x0, y0) + di := dx * 4 + for y := y0; y != y1; y, sy, my = y+dy, sy+dy, my+dy { + for i, sx, mx := i0, sx0, mx0; sx != sx1; i, sx, mx = i+di, sx+dx, mx+dx { + mi := mask.PixOffset(mx, my) + ma := uint32(mask.Pix[mi]) + ma |= ma << 8 + si := src.PixOffset(sx, sy) + sr := uint32(src.Pix[si+0]) + sg := uint32(src.Pix[si+1]) + sb := uint32(src.Pix[si+2]) + sa := uint32(src.Pix[si+3]) + sr |= sr << 8 + sg |= sg << 8 + sb |= sb << 8 + sa |= sa << 8 + d := dst.Pix[i : i+4 : i+4] // Small cap improves performance, see https://golang.org/issue/27857 + dr := uint32(d[0]) + dg := uint32(d[1]) + db := uint32(d[2]) + da := uint32(d[3]) + + // dr, dg, db and da are all 8-bit color at the moment, ranging in [0,255]. + // We work in 16-bit color, and so would normally do: + // dr |= dr << 8 + // and similarly for dg, db and da, but instead we multiply a + // (which is a 16-bit color, ranging in [0,65535]) by 0x101. + // This yields the same result, but is fewer arithmetic operations. + a := (m - (sa * ma / m)) * 0x101 + + d[0] = uint8((dr*a + sr*ma) / m >> 8) + d[1] = uint8((dg*a + sg*ma) / m >> 8) + d[2] = uint8((db*a + sb*ma) / m >> 8) + d[3] = uint8((da*a + sa*ma) / m >> 8) + } + i0 += dy * dst.Stride + } +} + +func drawRGBA64ImageMaskOver(dst *image.RGBA, r image.Rectangle, src image.RGBA64Image, sp image.Point, mask *image.Alpha, mp image.Point) { + x0, x1, dx := r.Min.X, r.Max.X, 1 + y0, y1, dy := r.Min.Y, r.Max.Y, 1 + if image.Image(dst) == src && r.Overlaps(r.Add(sp.Sub(r.Min))) { + if sp.Y < r.Min.Y || sp.Y == r.Min.Y && sp.X < r.Min.X { + x0, x1, dx = x1-1, x0-1, -1 + y0, y1, dy = y1-1, y0-1, -1 + } + } + + sy := sp.Y + y0 - r.Min.Y + my := mp.Y + y0 - r.Min.Y + sx0 := sp.X + x0 - r.Min.X + mx0 := mp.X + x0 - r.Min.X + sx1 := sx0 + (x1 - x0) + i0 := dst.PixOffset(x0, y0) + di := dx * 4 + for y := y0; y != y1; y, sy, my = y+dy, sy+dy, my+dy { + for i, sx, mx := i0, sx0, mx0; sx != sx1; i, sx, mx = i+di, sx+dx, mx+dx { + mi := mask.PixOffset(mx, my) + ma := uint32(mask.Pix[mi]) + ma |= ma << 8 + srgba := src.RGBA64At(sx, sy) + d := dst.Pix[i : i+4 : i+4] // Small cap improves performance, see https://golang.org/issue/27857 + dr := uint32(d[0]) + dg := uint32(d[1]) + db := uint32(d[2]) + da := uint32(d[3]) + + // dr, dg, db and da are all 8-bit color at the moment, ranging in [0,255]. + // We work in 16-bit color, and so would normally do: + // dr |= dr << 8 + // and similarly for dg, db and da, but instead we multiply a + // (which is a 16-bit color, ranging in [0,65535]) by 0x101. + // This yields the same result, but is fewer arithmetic operations. + a := (m - (uint32(srgba.A) * ma / m)) * 0x101 + + d[0] = uint8((dr*a + uint32(srgba.R)*ma) / m >> 8) + d[1] = uint8((dg*a + uint32(srgba.G)*ma) / m >> 8) + d[2] = uint8((db*a + uint32(srgba.B)*ma) / m >> 8) + d[3] = uint8((da*a + uint32(srgba.A)*ma) / m >> 8) + } + i0 += dy * dst.Stride + } +} + +func drawRGBA(dst *image.RGBA, r image.Rectangle, src image.Image, sp image.Point, mask image.Image, mp image.Point, op Op) { + x0, x1, dx := r.Min.X, r.Max.X, 1 + y0, y1, dy := r.Min.Y, r.Max.Y, 1 + if image.Image(dst) == src && r.Overlaps(r.Add(sp.Sub(r.Min))) { + if sp.Y < r.Min.Y || sp.Y == r.Min.Y && sp.X < r.Min.X { + x0, x1, dx = x1-1, x0-1, -1 + y0, y1, dy = y1-1, y0-1, -1 + } + } + + sy := sp.Y + y0 - r.Min.Y + my := mp.Y + y0 - r.Min.Y + sx0 := sp.X + x0 - r.Min.X + mx0 := mp.X + x0 - r.Min.X + sx1 := sx0 + (x1 - x0) + i0 := dst.PixOffset(x0, y0) + di := dx * 4 + + // Try the image.RGBA64Image interface, part of the standard library since + // Go 1.17. + // + // This optimization is similar to how FALLBACK1.17 optimizes FALLBACK1.0 + // in DrawMask, except here the concrete type of dst is known to be + // *image.RGBA. + if src0, _ := src.(image.RGBA64Image); src0 != nil { + if mask == nil { + if op == Over { + for y := y0; y != y1; y, sy, my = y+dy, sy+dy, my+dy { + for i, sx, mx := i0, sx0, mx0; sx != sx1; i, sx, mx = i+di, sx+dx, mx+dx { + srgba := src0.RGBA64At(sx, sy) + d := dst.Pix[i : i+4 : i+4] + dr := uint32(d[0]) + dg := uint32(d[1]) + db := uint32(d[2]) + da := uint32(d[3]) + a := (m - uint32(srgba.A)) * 0x101 + d[0] = uint8((dr*a/m + uint32(srgba.R)) >> 8) + d[1] = uint8((dg*a/m + uint32(srgba.G)) >> 8) + d[2] = uint8((db*a/m + uint32(srgba.B)) >> 8) + d[3] = uint8((da*a/m + uint32(srgba.A)) >> 8) + } + i0 += dy * dst.Stride + } + } else { + for y := y0; y != y1; y, sy, my = y+dy, sy+dy, my+dy { + for i, sx, mx := i0, sx0, mx0; sx != sx1; i, sx, mx = i+di, sx+dx, mx+dx { + srgba := src0.RGBA64At(sx, sy) + d := dst.Pix[i : i+4 : i+4] + d[0] = uint8(srgba.R >> 8) + d[1] = uint8(srgba.G >> 8) + d[2] = uint8(srgba.B >> 8) + d[3] = uint8(srgba.A >> 8) + } + i0 += dy * dst.Stride + } + } + return + + } else if mask0, _ := mask.(image.RGBA64Image); mask0 != nil { + if op == Over { + for y := y0; y != y1; y, sy, my = y+dy, sy+dy, my+dy { + for i, sx, mx := i0, sx0, mx0; sx != sx1; i, sx, mx = i+di, sx+dx, mx+dx { + ma := uint32(mask0.RGBA64At(mx, my).A) + srgba := src0.RGBA64At(sx, sy) + d := dst.Pix[i : i+4 : i+4] + dr := uint32(d[0]) + dg := uint32(d[1]) + db := uint32(d[2]) + da := uint32(d[3]) + a := (m - (uint32(srgba.A) * ma / m)) * 0x101 + d[0] = uint8((dr*a + uint32(srgba.R)*ma) / m >> 8) + d[1] = uint8((dg*a + uint32(srgba.G)*ma) / m >> 8) + d[2] = uint8((db*a + uint32(srgba.B)*ma) / m >> 8) + d[3] = uint8((da*a + uint32(srgba.A)*ma) / m >> 8) + } + i0 += dy * dst.Stride + } + } else { + for y := y0; y != y1; y, sy, my = y+dy, sy+dy, my+dy { + for i, sx, mx := i0, sx0, mx0; sx != sx1; i, sx, mx = i+di, sx+dx, mx+dx { + ma := uint32(mask0.RGBA64At(mx, my).A) + srgba := src0.RGBA64At(sx, sy) + d := dst.Pix[i : i+4 : i+4] + d[0] = uint8(uint32(srgba.R) * ma / m >> 8) + d[1] = uint8(uint32(srgba.G) * ma / m >> 8) + d[2] = uint8(uint32(srgba.B) * ma / m >> 8) + d[3] = uint8(uint32(srgba.A) * ma / m >> 8) + } + i0 += dy * dst.Stride + } + } + return + } + } + + // Use the image.Image interface, part of the standard library since Go + // 1.0. + // + // This is similar to FALLBACK1.0 in DrawMask, except here the concrete + // type of dst is known to be *image.RGBA. + for y := y0; y != y1; y, sy, my = y+dy, sy+dy, my+dy { + for i, sx, mx := i0, sx0, mx0; sx != sx1; i, sx, mx = i+di, sx+dx, mx+dx { + ma := uint32(m) + if mask != nil { + _, _, _, ma = mask.At(mx, my).RGBA() + } + sr, sg, sb, sa := src.At(sx, sy).RGBA() + d := dst.Pix[i : i+4 : i+4] // Small cap improves performance, see https://golang.org/issue/27857 + if op == Over { + dr := uint32(d[0]) + dg := uint32(d[1]) + db := uint32(d[2]) + da := uint32(d[3]) + + // dr, dg, db and da are all 8-bit color at the moment, ranging in [0,255]. + // We work in 16-bit color, and so would normally do: + // dr |= dr << 8 + // and similarly for dg, db and da, but instead we multiply a + // (which is a 16-bit color, ranging in [0,65535]) by 0x101. + // This yields the same result, but is fewer arithmetic operations. + a := (m - (sa * ma / m)) * 0x101 + + d[0] = uint8((dr*a + sr*ma) / m >> 8) + d[1] = uint8((dg*a + sg*ma) / m >> 8) + d[2] = uint8((db*a + sb*ma) / m >> 8) + d[3] = uint8((da*a + sa*ma) / m >> 8) + + } else { + d[0] = uint8(sr * ma / m >> 8) + d[1] = uint8(sg * ma / m >> 8) + d[2] = uint8(sb * ma / m >> 8) + d[3] = uint8(sa * ma / m >> 8) + } + } + i0 += dy * dst.Stride + } +} + +// clamp clamps i to the interval [0, 0xffff]. +func clamp(i int32) int32 { + if i < 0 { + return 0 + } + if i > 0xffff { + return 0xffff + } + return i +} + +// sqDiff returns the squared-difference of x and y, shifted by 2 so that +// adding four of those won't overflow a uint32. +// +// x and y are both assumed to be in the range [0, 0xffff]. +func sqDiff(x, y int32) uint32 { + // This is an optimized code relying on the overflow/wrap around + // properties of unsigned integers operations guaranteed by the language + // spec. See sqDiff from the image/color package for more details. + d := uint32(x - y) + return (d * d) >> 2 +} + +func drawPaletted(dst Image, r image.Rectangle, src image.Image, sp image.Point, floydSteinberg bool) { + // TODO(nigeltao): handle the case where the dst and src overlap. + // Does it even make sense to try and do Floyd-Steinberg whilst + // walking the image backward (right-to-left bottom-to-top)? + + // If dst is an *image.Paletted, we have a fast path for dst.Set and + // dst.At. The dst.Set equivalent is a batch version of the algorithm + // used by color.Palette's Index method in image/color/color.go, plus + // optional Floyd-Steinberg error diffusion. + palette, pix, stride := [][4]int32(nil), []byte(nil), 0 + if p, ok := dst.(*image.Paletted); ok { + palette = make([][4]int32, len(p.Palette)) + for i, col := range p.Palette { + r, g, b, a := col.RGBA() + palette[i][0] = int32(r) + palette[i][1] = int32(g) + palette[i][2] = int32(b) + palette[i][3] = int32(a) + } + pix, stride = p.Pix[p.PixOffset(r.Min.X, r.Min.Y):], p.Stride + } + + // quantErrorCurr and quantErrorNext are the Floyd-Steinberg quantization + // errors that have been propagated to the pixels in the current and next + // rows. The +2 simplifies calculation near the edges. + var quantErrorCurr, quantErrorNext [][4]int32 + if floydSteinberg { + quantErrorCurr = make([][4]int32, r.Dx()+2) + quantErrorNext = make([][4]int32, r.Dx()+2) + } + pxRGBA := func(x, y int) (r, g, b, a uint32) { return src.At(x, y).RGBA() } + // Fast paths for special cases to avoid excessive use of the color.Color + // interface which escapes to the heap but need to be discovered for + // each pixel on r. See also https://golang.org/issues/15759. + switch src0 := src.(type) { + case *image.RGBA: + pxRGBA = func(x, y int) (r, g, b, a uint32) { return src0.RGBAAt(x, y).RGBA() } + case *image.NRGBA: + pxRGBA = func(x, y int) (r, g, b, a uint32) { return src0.NRGBAAt(x, y).RGBA() } + case *image.YCbCr: + pxRGBA = func(x, y int) (r, g, b, a uint32) { return src0.YCbCrAt(x, y).RGBA() } + } + + // Loop over each source pixel. + out := color.RGBA64{A: 0xffff} + for y := 0; y != r.Dy(); y++ { + for x := 0; x != r.Dx(); x++ { + // er, eg and eb are the pixel's R,G,B values plus the + // optional Floyd-Steinberg error. + sr, sg, sb, sa := pxRGBA(sp.X+x, sp.Y+y) + er, eg, eb, ea := int32(sr), int32(sg), int32(sb), int32(sa) + if floydSteinberg { + er = clamp(er + quantErrorCurr[x+1][0]/16) + eg = clamp(eg + quantErrorCurr[x+1][1]/16) + eb = clamp(eb + quantErrorCurr[x+1][2]/16) + ea = clamp(ea + quantErrorCurr[x+1][3]/16) + } + + if palette != nil { + // Find the closest palette color in Euclidean R,G,B,A space: + // the one that minimizes sum-squared-difference. + // TODO(nigeltao): consider smarter algorithms. + bestIndex, bestSum := 0, uint32(1<<32-1) + for index, p := range palette { + sum := sqDiff(er, p[0]) + sqDiff(eg, p[1]) + sqDiff(eb, p[2]) + sqDiff(ea, p[3]) + if sum < bestSum { + bestIndex, bestSum = index, sum + if sum == 0 { + break + } + } + } + pix[y*stride+x] = byte(bestIndex) + + if !floydSteinberg { + continue + } + er -= palette[bestIndex][0] + eg -= palette[bestIndex][1] + eb -= palette[bestIndex][2] + ea -= palette[bestIndex][3] + + } else { + out.R = uint16(er) + out.G = uint16(eg) + out.B = uint16(eb) + out.A = uint16(ea) + // The third argument is &out instead of out (and out is + // declared outside of the inner loop) to avoid the implicit + // conversion to color.Color here allocating memory in the + // inner loop if sizeof(color.RGBA64) > sizeof(uintptr). + dst.Set(r.Min.X+x, r.Min.Y+y, &out) + + if !floydSteinberg { + continue + } + sr, sg, sb, sa = dst.At(r.Min.X+x, r.Min.Y+y).RGBA() + er -= int32(sr) + eg -= int32(sg) + eb -= int32(sb) + ea -= int32(sa) + } + + // Propagate the Floyd-Steinberg quantization error. + quantErrorNext[x+0][0] += er * 3 + quantErrorNext[x+0][1] += eg * 3 + quantErrorNext[x+0][2] += eb * 3 + quantErrorNext[x+0][3] += ea * 3 + quantErrorNext[x+1][0] += er * 5 + quantErrorNext[x+1][1] += eg * 5 + quantErrorNext[x+1][2] += eb * 5 + quantErrorNext[x+1][3] += ea * 5 + quantErrorNext[x+2][0] += er * 1 + quantErrorNext[x+2][1] += eg * 1 + quantErrorNext[x+2][2] += eb * 1 + quantErrorNext[x+2][3] += ea * 1 + quantErrorCurr[x+2][0] += er * 7 + quantErrorCurr[x+2][1] += eg * 7 + quantErrorCurr[x+2][2] += eb * 7 + quantErrorCurr[x+2][3] += ea * 7 + } + + // Recycle the quantization error buffers. + if floydSteinberg { + quantErrorCurr, quantErrorNext = quantErrorNext, quantErrorCurr + for i := range quantErrorNext { + quantErrorNext[i] = [4]int32{} + } + } + } +} diff --git a/src/image/draw/draw_test.go b/src/image/draw/draw_test.go new file mode 100644 index 0000000..a34d1c3 --- /dev/null +++ b/src/image/draw/draw_test.go @@ -0,0 +1,810 @@ +// Copyright 2010 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package draw + +import ( + "image" + "image/color" + "image/png" + "os" + "testing" + "testing/quick" +) + +// slowestRGBA is a draw.Image like image.RGBA but it is a different type and +// therefore does not trigger the draw.go fastest code paths. +// +// Unlike slowerRGBA, it does not implement the draw.RGBA64Image interface. +type slowestRGBA struct { + Pix []uint8 + Stride int + Rect image.Rectangle +} + +func (p *slowestRGBA) ColorModel() color.Model { return color.RGBAModel } + +func (p *slowestRGBA) Bounds() image.Rectangle { return p.Rect } + +func (p *slowestRGBA) At(x, y int) color.Color { + return p.RGBA64At(x, y) +} + +func (p *slowestRGBA) RGBA64At(x, y int) color.RGBA64 { + if !(image.Point{x, y}.In(p.Rect)) { + return color.RGBA64{} + } + i := p.PixOffset(x, y) + s := p.Pix[i : i+4 : i+4] // Small cap improves performance, see https://golang.org/issue/27857 + r := uint16(s[0]) + g := uint16(s[1]) + b := uint16(s[2]) + a := uint16(s[3]) + return color.RGBA64{ + (r << 8) | r, + (g << 8) | g, + (b << 8) | b, + (a << 8) | a, + } +} + +func (p *slowestRGBA) Set(x, y int, c color.Color) { + if !(image.Point{x, y}.In(p.Rect)) { + return + } + i := p.PixOffset(x, y) + c1 := color.RGBAModel.Convert(c).(color.RGBA) + s := p.Pix[i : i+4 : i+4] // Small cap improves performance, see https://golang.org/issue/27857 + s[0] = c1.R + s[1] = c1.G + s[2] = c1.B + s[3] = c1.A +} + +func (p *slowestRGBA) PixOffset(x, y int) int { + return (y-p.Rect.Min.Y)*p.Stride + (x-p.Rect.Min.X)*4 +} + +func convertToSlowestRGBA(m image.Image) *slowestRGBA { + if rgba, ok := m.(*image.RGBA); ok { + return &slowestRGBA{ + Pix: append([]byte(nil), rgba.Pix...), + Stride: rgba.Stride, + Rect: rgba.Rect, + } + } + rgba := image.NewRGBA(m.Bounds()) + Draw(rgba, rgba.Bounds(), m, m.Bounds().Min, Src) + return &slowestRGBA{ + Pix: rgba.Pix, + Stride: rgba.Stride, + Rect: rgba.Rect, + } +} + +func init() { + var p any = (*slowestRGBA)(nil) + if _, ok := p.(RGBA64Image); ok { + panic("slowestRGBA should not be an RGBA64Image") + } +} + +// slowerRGBA is a draw.Image like image.RGBA but it is a different type and +// therefore does not trigger the draw.go fastest code paths. +// +// Unlike slowestRGBA, it still implements the draw.RGBA64Image interface. +type slowerRGBA struct { + Pix []uint8 + Stride int + Rect image.Rectangle +} + +func (p *slowerRGBA) ColorModel() color.Model { return color.RGBAModel } + +func (p *slowerRGBA) Bounds() image.Rectangle { return p.Rect } + +func (p *slowerRGBA) At(x, y int) color.Color { + return p.RGBA64At(x, y) +} + +func (p *slowerRGBA) RGBA64At(x, y int) color.RGBA64 { + if !(image.Point{x, y}.In(p.Rect)) { + return color.RGBA64{} + } + i := p.PixOffset(x, y) + s := p.Pix[i : i+4 : i+4] // Small cap improves performance, see https://golang.org/issue/27857 + r := uint16(s[0]) + g := uint16(s[1]) + b := uint16(s[2]) + a := uint16(s[3]) + return color.RGBA64{ + (r << 8) | r, + (g << 8) | g, + (b << 8) | b, + (a << 8) | a, + } +} + +func (p *slowerRGBA) Set(x, y int, c color.Color) { + if !(image.Point{x, y}.In(p.Rect)) { + return + } + i := p.PixOffset(x, y) + c1 := color.RGBAModel.Convert(c).(color.RGBA) + s := p.Pix[i : i+4 : i+4] // Small cap improves performance, see https://golang.org/issue/27857 + s[0] = c1.R + s[1] = c1.G + s[2] = c1.B + s[3] = c1.A +} + +func (p *slowerRGBA) SetRGBA64(x, y int, c color.RGBA64) { + if !(image.Point{x, y}.In(p.Rect)) { + return + } + i := p.PixOffset(x, y) + s := p.Pix[i : i+4 : i+4] // Small cap improves performance, see https://golang.org/issue/27857 + s[0] = uint8(c.R >> 8) + s[1] = uint8(c.G >> 8) + s[2] = uint8(c.B >> 8) + s[3] = uint8(c.A >> 8) +} + +func (p *slowerRGBA) PixOffset(x, y int) int { + return (y-p.Rect.Min.Y)*p.Stride + (x-p.Rect.Min.X)*4 +} + +func convertToSlowerRGBA(m image.Image) *slowerRGBA { + if rgba, ok := m.(*image.RGBA); ok { + return &slowerRGBA{ + Pix: append([]byte(nil), rgba.Pix...), + Stride: rgba.Stride, + Rect: rgba.Rect, + } + } + rgba := image.NewRGBA(m.Bounds()) + Draw(rgba, rgba.Bounds(), m, m.Bounds().Min, Src) + return &slowerRGBA{ + Pix: rgba.Pix, + Stride: rgba.Stride, + Rect: rgba.Rect, + } +} + +func init() { + var p any = (*slowerRGBA)(nil) + if _, ok := p.(RGBA64Image); !ok { + panic("slowerRGBA should be an RGBA64Image") + } +} + +func eq(c0, c1 color.Color) bool { + r0, g0, b0, a0 := c0.RGBA() + r1, g1, b1, a1 := c1.RGBA() + return r0 == r1 && g0 == g1 && b0 == b1 && a0 == a1 +} + +func fillBlue(alpha int) image.Image { + return image.NewUniform(color.RGBA{0, 0, uint8(alpha), uint8(alpha)}) +} + +func fillAlpha(alpha int) image.Image { + return image.NewUniform(color.Alpha{uint8(alpha)}) +} + +func vgradGreen(alpha int) image.Image { + m := image.NewRGBA(image.Rect(0, 0, 16, 16)) + for y := 0; y < 16; y++ { + for x := 0; x < 16; x++ { + m.Set(x, y, color.RGBA{0, uint8(y * alpha / 15), 0, uint8(alpha)}) + } + } + return m +} + +func vgradAlpha(alpha int) image.Image { + m := image.NewAlpha(image.Rect(0, 0, 16, 16)) + for y := 0; y < 16; y++ { + for x := 0; x < 16; x++ { + m.Set(x, y, color.Alpha{uint8(y * alpha / 15)}) + } + } + return m +} + +func vgradGreenNRGBA(alpha int) image.Image { + m := image.NewNRGBA(image.Rect(0, 0, 16, 16)) + for y := 0; y < 16; y++ { + for x := 0; x < 16; x++ { + m.Set(x, y, color.RGBA{0, uint8(y * 0x11), 0, uint8(alpha)}) + } + } + return m +} + +func vgradCr() image.Image { + m := &image.YCbCr{ + Y: make([]byte, 16*16), + Cb: make([]byte, 16*16), + Cr: make([]byte, 16*16), + YStride: 16, + CStride: 16, + SubsampleRatio: image.YCbCrSubsampleRatio444, + Rect: image.Rect(0, 0, 16, 16), + } + for y := 0; y < 16; y++ { + for x := 0; x < 16; x++ { + m.Cr[y*m.CStride+x] = uint8(y * 0x11) + } + } + return m +} + +func vgradGray() image.Image { + m := image.NewGray(image.Rect(0, 0, 16, 16)) + for y := 0; y < 16; y++ { + for x := 0; x < 16; x++ { + m.Set(x, y, color.Gray{uint8(y * 0x11)}) + } + } + return m +} + +func vgradMagenta() image.Image { + m := image.NewCMYK(image.Rect(0, 0, 16, 16)) + for y := 0; y < 16; y++ { + for x := 0; x < 16; x++ { + m.Set(x, y, color.CMYK{0, uint8(y * 0x11), 0, 0x3f}) + } + } + return m +} + +func hgradRed(alpha int) Image { + m := image.NewRGBA(image.Rect(0, 0, 16, 16)) + for y := 0; y < 16; y++ { + for x := 0; x < 16; x++ { + m.Set(x, y, color.RGBA{uint8(x * alpha / 15), 0, 0, uint8(alpha)}) + } + } + return m +} + +func gradYellow(alpha int) Image { + m := image.NewRGBA(image.Rect(0, 0, 16, 16)) + for y := 0; y < 16; y++ { + for x := 0; x < 16; x++ { + m.Set(x, y, color.RGBA{uint8(x * alpha / 15), uint8(y * alpha / 15), 0, uint8(alpha)}) + } + } + return m +} + +type drawTest struct { + desc string + src image.Image + mask image.Image + op Op + expected color.Color +} + +var drawTests = []drawTest{ + // Uniform mask (0% opaque). + {"nop", vgradGreen(255), fillAlpha(0), Over, color.RGBA{136, 0, 0, 255}}, + {"clear", vgradGreen(255), fillAlpha(0), Src, color.RGBA{0, 0, 0, 0}}, + // Uniform mask (100%, 75%, nil) and uniform source. + // At (x, y) == (8, 8): + // The destination pixel is {136, 0, 0, 255}. + // The source pixel is {0, 0, 90, 90}. + {"fill", fillBlue(90), fillAlpha(255), Over, color.RGBA{88, 0, 90, 255}}, + {"fillSrc", fillBlue(90), fillAlpha(255), Src, color.RGBA{0, 0, 90, 90}}, + {"fillAlpha", fillBlue(90), fillAlpha(192), Over, color.RGBA{100, 0, 68, 255}}, + {"fillAlphaSrc", fillBlue(90), fillAlpha(192), Src, color.RGBA{0, 0, 68, 68}}, + {"fillNil", fillBlue(90), nil, Over, color.RGBA{88, 0, 90, 255}}, + {"fillNilSrc", fillBlue(90), nil, Src, color.RGBA{0, 0, 90, 90}}, + // Uniform mask (100%, 75%, nil) and variable source. + // At (x, y) == (8, 8): + // The destination pixel is {136, 0, 0, 255}. + // The source pixel is {0, 48, 0, 90}. + {"copy", vgradGreen(90), fillAlpha(255), Over, color.RGBA{88, 48, 0, 255}}, + {"copySrc", vgradGreen(90), fillAlpha(255), Src, color.RGBA{0, 48, 0, 90}}, + {"copyAlpha", vgradGreen(90), fillAlpha(192), Over, color.RGBA{100, 36, 0, 255}}, + {"copyAlphaSrc", vgradGreen(90), fillAlpha(192), Src, color.RGBA{0, 36, 0, 68}}, + {"copyNil", vgradGreen(90), nil, Over, color.RGBA{88, 48, 0, 255}}, + {"copyNilSrc", vgradGreen(90), nil, Src, color.RGBA{0, 48, 0, 90}}, + // Uniform mask (100%, 75%, nil) and variable NRGBA source. + // At (x, y) == (8, 8): + // The destination pixel is {136, 0, 0, 255}. + // The source pixel is {0, 136, 0, 90} in NRGBA-space, which is {0, 48, 0, 90} in RGBA-space. + // The result pixel is different than in the "copy*" test cases because of rounding errors. + {"nrgba", vgradGreenNRGBA(90), fillAlpha(255), Over, color.RGBA{88, 46, 0, 255}}, + {"nrgbaSrc", vgradGreenNRGBA(90), fillAlpha(255), Src, color.RGBA{0, 46, 0, 90}}, + {"nrgbaAlpha", vgradGreenNRGBA(90), fillAlpha(192), Over, color.RGBA{100, 34, 0, 255}}, + {"nrgbaAlphaSrc", vgradGreenNRGBA(90), fillAlpha(192), Src, color.RGBA{0, 34, 0, 68}}, + {"nrgbaNil", vgradGreenNRGBA(90), nil, Over, color.RGBA{88, 46, 0, 255}}, + {"nrgbaNilSrc", vgradGreenNRGBA(90), nil, Src, color.RGBA{0, 46, 0, 90}}, + // Uniform mask (100%, 75%, nil) and variable YCbCr source. + // At (x, y) == (8, 8): + // The destination pixel is {136, 0, 0, 255}. + // The source pixel is {0, 0, 136} in YCbCr-space, which is {11, 38, 0, 255} in RGB-space. + {"ycbcr", vgradCr(), fillAlpha(255), Over, color.RGBA{11, 38, 0, 255}}, + {"ycbcrSrc", vgradCr(), fillAlpha(255), Src, color.RGBA{11, 38, 0, 255}}, + {"ycbcrAlpha", vgradCr(), fillAlpha(192), Over, color.RGBA{42, 28, 0, 255}}, + {"ycbcrAlphaSrc", vgradCr(), fillAlpha(192), Src, color.RGBA{8, 28, 0, 192}}, + {"ycbcrNil", vgradCr(), nil, Over, color.RGBA{11, 38, 0, 255}}, + {"ycbcrNilSrc", vgradCr(), nil, Src, color.RGBA{11, 38, 0, 255}}, + // Uniform mask (100%, 75%, nil) and variable Gray source. + // At (x, y) == (8, 8): + // The destination pixel is {136, 0, 0, 255}. + // The source pixel is {136} in Gray-space, which is {136, 136, 136, 255} in RGBA-space. + {"gray", vgradGray(), fillAlpha(255), Over, color.RGBA{136, 136, 136, 255}}, + {"graySrc", vgradGray(), fillAlpha(255), Src, color.RGBA{136, 136, 136, 255}}, + {"grayAlpha", vgradGray(), fillAlpha(192), Over, color.RGBA{136, 102, 102, 255}}, + {"grayAlphaSrc", vgradGray(), fillAlpha(192), Src, color.RGBA{102, 102, 102, 192}}, + {"grayNil", vgradGray(), nil, Over, color.RGBA{136, 136, 136, 255}}, + {"grayNilSrc", vgradGray(), nil, Src, color.RGBA{136, 136, 136, 255}}, + // Same again, but with a slowerRGBA source. + {"graySlower", convertToSlowerRGBA(vgradGray()), fillAlpha(255), + Over, color.RGBA{136, 136, 136, 255}}, + {"graySrcSlower", convertToSlowerRGBA(vgradGray()), fillAlpha(255), + Src, color.RGBA{136, 136, 136, 255}}, + {"grayAlphaSlower", convertToSlowerRGBA(vgradGray()), fillAlpha(192), + Over, color.RGBA{136, 102, 102, 255}}, + {"grayAlphaSrcSlower", convertToSlowerRGBA(vgradGray()), fillAlpha(192), + Src, color.RGBA{102, 102, 102, 192}}, + {"grayNilSlower", convertToSlowerRGBA(vgradGray()), nil, + Over, color.RGBA{136, 136, 136, 255}}, + {"grayNilSrcSlower", convertToSlowerRGBA(vgradGray()), nil, + Src, color.RGBA{136, 136, 136, 255}}, + // Same again, but with a slowestRGBA source. + {"graySlowest", convertToSlowestRGBA(vgradGray()), fillAlpha(255), + Over, color.RGBA{136, 136, 136, 255}}, + {"graySrcSlowest", convertToSlowestRGBA(vgradGray()), fillAlpha(255), + Src, color.RGBA{136, 136, 136, 255}}, + {"grayAlphaSlowest", convertToSlowestRGBA(vgradGray()), fillAlpha(192), + Over, color.RGBA{136, 102, 102, 255}}, + {"grayAlphaSrcSlowest", convertToSlowestRGBA(vgradGray()), fillAlpha(192), + Src, color.RGBA{102, 102, 102, 192}}, + {"grayNilSlowest", convertToSlowestRGBA(vgradGray()), nil, + Over, color.RGBA{136, 136, 136, 255}}, + {"grayNilSrcSlowest", convertToSlowestRGBA(vgradGray()), nil, + Src, color.RGBA{136, 136, 136, 255}}, + // Uniform mask (100%, 75%, nil) and variable CMYK source. + // At (x, y) == (8, 8): + // The destination pixel is {136, 0, 0, 255}. + // The source pixel is {0, 136, 0, 63} in CMYK-space, which is {192, 89, 192} in RGB-space. + {"cmyk", vgradMagenta(), fillAlpha(255), Over, color.RGBA{192, 89, 192, 255}}, + {"cmykSrc", vgradMagenta(), fillAlpha(255), Src, color.RGBA{192, 89, 192, 255}}, + {"cmykAlpha", vgradMagenta(), fillAlpha(192), Over, color.RGBA{178, 67, 145, 255}}, + {"cmykAlphaSrc", vgradMagenta(), fillAlpha(192), Src, color.RGBA{145, 67, 145, 192}}, + {"cmykNil", vgradMagenta(), nil, Over, color.RGBA{192, 89, 192, 255}}, + {"cmykNilSrc", vgradMagenta(), nil, Src, color.RGBA{192, 89, 192, 255}}, + // Variable mask and uniform source. + // At (x, y) == (8, 8): + // The destination pixel is {136, 0, 0, 255}. + // The source pixel is {0, 0, 255, 255}. + // The mask pixel's alpha is 102, or 40%. + {"generic", fillBlue(255), vgradAlpha(192), Over, color.RGBA{81, 0, 102, 255}}, + {"genericSrc", fillBlue(255), vgradAlpha(192), Src, color.RGBA{0, 0, 102, 102}}, + // Same again, but with a slowerRGBA mask. + {"genericSlower", fillBlue(255), convertToSlowerRGBA(vgradAlpha(192)), + Over, color.RGBA{81, 0, 102, 255}}, + {"genericSrcSlower", fillBlue(255), convertToSlowerRGBA(vgradAlpha(192)), + Src, color.RGBA{0, 0, 102, 102}}, + // Same again, but with a slowestRGBA mask. + {"genericSlowest", fillBlue(255), convertToSlowestRGBA(vgradAlpha(192)), + Over, color.RGBA{81, 0, 102, 255}}, + {"genericSrcSlowest", fillBlue(255), convertToSlowestRGBA(vgradAlpha(192)), + Src, color.RGBA{0, 0, 102, 102}}, + // Variable mask and variable source. + // At (x, y) == (8, 8): + // The destination pixel is {136, 0, 0, 255}. + // The source pixel is: + // - {0, 48, 0, 90}. + // - {136} in Gray-space, which is {136, 136, 136, 255} in RGBA-space. + // The mask pixel's alpha is 102, or 40%. + {"rgbaVariableMaskOver", vgradGreen(90), vgradAlpha(192), Over, color.RGBA{117, 19, 0, 255}}, + {"grayVariableMaskOver", vgradGray(), vgradAlpha(192), Over, color.RGBA{136, 54, 54, 255}}, +} + +func makeGolden(dst image.Image, r image.Rectangle, src image.Image, sp image.Point, mask image.Image, mp image.Point, op Op) image.Image { + // Since golden is a newly allocated image, we don't have to check if the + // input source and mask images and the output golden image overlap. + b := dst.Bounds() + sb := src.Bounds() + mb := image.Rect(-1e9, -1e9, 1e9, 1e9) + if mask != nil { + mb = mask.Bounds() + } + golden := image.NewRGBA(image.Rect(0, 0, b.Max.X, b.Max.Y)) + for y := r.Min.Y; y < r.Max.Y; y++ { + sy := y + sp.Y - r.Min.Y + my := y + mp.Y - r.Min.Y + for x := r.Min.X; x < r.Max.X; x++ { + if !(image.Pt(x, y).In(b)) { + continue + } + sx := x + sp.X - r.Min.X + if !(image.Pt(sx, sy).In(sb)) { + continue + } + mx := x + mp.X - r.Min.X + if !(image.Pt(mx, my).In(mb)) { + continue + } + + const M = 1<<16 - 1 + var dr, dg, db, da uint32 + if op == Over { + dr, dg, db, da = dst.At(x, y).RGBA() + } + sr, sg, sb, sa := src.At(sx, sy).RGBA() + ma := uint32(M) + if mask != nil { + _, _, _, ma = mask.At(mx, my).RGBA() + } + a := M - (sa * ma / M) + golden.Set(x, y, color.RGBA64{ + uint16((dr*a + sr*ma) / M), + uint16((dg*a + sg*ma) / M), + uint16((db*a + sb*ma) / M), + uint16((da*a + sa*ma) / M), + }) + } + } + return golden.SubImage(b) +} + +func TestDraw(t *testing.T) { + rr := []image.Rectangle{ + image.Rect(0, 0, 0, 0), + image.Rect(0, 0, 16, 16), + image.Rect(3, 5, 12, 10), + image.Rect(0, 0, 9, 9), + image.Rect(8, 8, 16, 16), + image.Rect(8, 0, 9, 16), + image.Rect(0, 8, 16, 9), + image.Rect(8, 8, 9, 9), + image.Rect(8, 8, 8, 8), + } + for _, r := range rr { + loop: + for _, test := range drawTests { + for i := 0; i < 3; i++ { + dst := hgradRed(255).(*image.RGBA).SubImage(r).(Image) + // For i != 0, substitute a different-typed dst that will take + // us off the fastest code paths. We should still get the same + // result, in terms of final pixel RGBA values. + switch i { + case 1: + dst = convertToSlowerRGBA(dst) + case 2: + dst = convertToSlowestRGBA(dst) + } + + // Draw the (src, mask, op) onto a copy of dst using a slow but obviously correct implementation. + golden := makeGolden(dst, image.Rect(0, 0, 16, 16), test.src, image.ZP, test.mask, image.ZP, test.op) + b := dst.Bounds() + if !b.Eq(golden.Bounds()) { + t.Errorf("draw %v %s on %T: bounds %v versus %v", + r, test.desc, dst, dst.Bounds(), golden.Bounds()) + continue + } + // Draw the same combination onto the actual dst using the optimized DrawMask implementation. + DrawMask(dst, image.Rect(0, 0, 16, 16), test.src, image.ZP, test.mask, image.ZP, test.op) + if image.Pt(8, 8).In(r) { + // Check that the resultant pixel at (8, 8) matches what we expect + // (the expected value can be verified by hand). + if !eq(dst.At(8, 8), test.expected) { + t.Errorf("draw %v %s on %T: at (8, 8) %v versus %v", + r, test.desc, dst, dst.At(8, 8), test.expected) + continue + } + } + // Check that the resultant dst image matches the golden output. + for y := b.Min.Y; y < b.Max.Y; y++ { + for x := b.Min.X; x < b.Max.X; x++ { + if !eq(dst.At(x, y), golden.At(x, y)) { + t.Errorf("draw %v %s on %T: at (%d, %d), %v versus golden %v", + r, test.desc, dst, x, y, dst.At(x, y), golden.At(x, y)) + continue loop + } + } + } + } + } + } +} + +func TestDrawOverlap(t *testing.T) { + for _, op := range []Op{Over, Src} { + for yoff := -2; yoff <= 2; yoff++ { + loop: + for xoff := -2; xoff <= 2; xoff++ { + m := gradYellow(127).(*image.RGBA) + dst := m.SubImage(image.Rect(5, 5, 10, 10)).(*image.RGBA) + src := m.SubImage(image.Rect(5+xoff, 5+yoff, 10+xoff, 10+yoff)).(*image.RGBA) + b := dst.Bounds() + // Draw the (src, mask, op) onto a copy of dst using a slow but obviously correct implementation. + golden := makeGolden(dst, b, src, src.Bounds().Min, nil, image.ZP, op) + if !b.Eq(golden.Bounds()) { + t.Errorf("drawOverlap xoff=%d,yoff=%d: bounds %v versus %v", xoff, yoff, dst.Bounds(), golden.Bounds()) + continue + } + // Draw the same combination onto the actual dst using the optimized DrawMask implementation. + DrawMask(dst, b, src, src.Bounds().Min, nil, image.ZP, op) + // Check that the resultant dst image matches the golden output. + for y := b.Min.Y; y < b.Max.Y; y++ { + for x := b.Min.X; x < b.Max.X; x++ { + if !eq(dst.At(x, y), golden.At(x, y)) { + t.Errorf("drawOverlap xoff=%d,yoff=%d: at (%d, %d), %v versus golden %v", xoff, yoff, x, y, dst.At(x, y), golden.At(x, y)) + continue loop + } + } + } + } + } + } +} + +// TestNonZeroSrcPt checks drawing with a non-zero src point parameter. +func TestNonZeroSrcPt(t *testing.T) { + a := image.NewRGBA(image.Rect(0, 0, 1, 1)) + b := image.NewRGBA(image.Rect(0, 0, 2, 2)) + b.Set(0, 0, color.RGBA{0, 0, 0, 5}) + b.Set(1, 0, color.RGBA{0, 0, 5, 5}) + b.Set(0, 1, color.RGBA{0, 5, 0, 5}) + b.Set(1, 1, color.RGBA{5, 0, 0, 5}) + Draw(a, image.Rect(0, 0, 1, 1), b, image.Pt(1, 1), Over) + if !eq(color.RGBA{5, 0, 0, 5}, a.At(0, 0)) { + t.Errorf("non-zero src pt: want %v got %v", color.RGBA{5, 0, 0, 5}, a.At(0, 0)) + } +} + +func TestFill(t *testing.T) { + rr := []image.Rectangle{ + image.Rect(0, 0, 0, 0), + image.Rect(0, 0, 40, 30), + image.Rect(10, 0, 40, 30), + image.Rect(0, 20, 40, 30), + image.Rect(10, 20, 40, 30), + image.Rect(10, 20, 15, 25), + image.Rect(10, 0, 35, 30), + image.Rect(0, 15, 40, 16), + image.Rect(24, 24, 25, 25), + image.Rect(23, 23, 26, 26), + image.Rect(22, 22, 27, 27), + image.Rect(21, 21, 28, 28), + image.Rect(20, 20, 29, 29), + } + for _, r := range rr { + m := image.NewRGBA(image.Rect(0, 0, 40, 30)).SubImage(r).(*image.RGBA) + b := m.Bounds() + c := color.RGBA{11, 0, 0, 255} + src := &image.Uniform{C: c} + check := func(desc string) { + for y := b.Min.Y; y < b.Max.Y; y++ { + for x := b.Min.X; x < b.Max.X; x++ { + if !eq(c, m.At(x, y)) { + t.Errorf("%s fill: at (%d, %d), sub-image bounds=%v: want %v got %v", desc, x, y, r, c, m.At(x, y)) + return + } + } + } + } + // Draw 1 pixel at a time. + for y := b.Min.Y; y < b.Max.Y; y++ { + for x := b.Min.X; x < b.Max.X; x++ { + DrawMask(m, image.Rect(x, y, x+1, y+1), src, image.ZP, nil, image.ZP, Src) + } + } + check("pixel") + // Draw 1 row at a time. + c = color.RGBA{0, 22, 0, 255} + src = &image.Uniform{C: c} + for y := b.Min.Y; y < b.Max.Y; y++ { + DrawMask(m, image.Rect(b.Min.X, y, b.Max.X, y+1), src, image.ZP, nil, image.ZP, Src) + } + check("row") + // Draw 1 column at a time. + c = color.RGBA{0, 0, 33, 255} + src = &image.Uniform{C: c} + for x := b.Min.X; x < b.Max.X; x++ { + DrawMask(m, image.Rect(x, b.Min.Y, x+1, b.Max.Y), src, image.ZP, nil, image.ZP, Src) + } + check("column") + // Draw the whole image at once. + c = color.RGBA{44, 55, 66, 77} + src = &image.Uniform{C: c} + DrawMask(m, b, src, image.ZP, nil, image.ZP, Src) + check("whole") + } +} + +func TestDrawSrcNonpremultiplied(t *testing.T) { + var ( + opaqueGray = color.NRGBA{0x99, 0x99, 0x99, 0xff} + transparentBlue = color.NRGBA{0x00, 0x00, 0xff, 0x00} + transparentGreen = color.NRGBA{0x00, 0xff, 0x00, 0x00} + transparentRed = color.NRGBA{0xff, 0x00, 0x00, 0x00} + + opaqueGray64 = color.NRGBA64{0x9999, 0x9999, 0x9999, 0xffff} + transparentPurple64 = color.NRGBA64{0xfedc, 0x0000, 0x7654, 0x0000} + ) + + // dst and src are 1x3 images but the dr rectangle (and hence the overlap) + // is only 1x2. The Draw call should affect dst's pixels at (1, 10) and (2, + // 10) but the pixel at (0, 10) should be untouched. + // + // The src image is entirely transparent (and the Draw operator is Src) so + // the two touched pixels should be set to transparent colors. + // + // In general, Go's color.Color type (and specifically the Color.RGBA + // method) works in premultiplied alpha, where there's no difference + // between "transparent blue" and "transparent red". It's all "just + // transparent" and canonically "transparent black" (all zeroes). + // + // However, since the operator is Src (so the pixels are 'copied', not + // 'blended') and both dst and src images are *image.NRGBA (N stands for + // Non-premultiplied alpha which *does* distinguish "transparent blue" and + // "transparent red"), we prefer that this distinction carries through and + // dst's touched pixels should be transparent blue and transparent green, + // not just transparent black. + { + dst := image.NewNRGBA(image.Rect(0, 10, 3, 11)) + dst.SetNRGBA(0, 10, opaqueGray) + src := image.NewNRGBA(image.Rect(1, 20, 4, 21)) + src.SetNRGBA(1, 20, transparentBlue) + src.SetNRGBA(2, 20, transparentGreen) + src.SetNRGBA(3, 20, transparentRed) + + dr := image.Rect(1, 10, 3, 11) + Draw(dst, dr, src, image.Point{1, 20}, Src) + + if got, want := dst.At(0, 10), opaqueGray; got != want { + t.Errorf("At(0, 10):\ngot %#v\nwant %#v", got, want) + } + if got, want := dst.At(1, 10), transparentBlue; got != want { + t.Errorf("At(1, 10):\ngot %#v\nwant %#v", got, want) + } + if got, want := dst.At(2, 10), transparentGreen; got != want { + t.Errorf("At(2, 10):\ngot %#v\nwant %#v", got, want) + } + } + + // Check image.NRGBA64 (not image.NRGBA) similarly. + { + dst := image.NewNRGBA64(image.Rect(0, 0, 1, 1)) + dst.SetNRGBA64(0, 0, opaqueGray64) + src := image.NewNRGBA64(image.Rect(0, 0, 1, 1)) + src.SetNRGBA64(0, 0, transparentPurple64) + Draw(dst, dst.Bounds(), src, image.Point{0, 0}, Src) + if got, want := dst.At(0, 0), transparentPurple64; got != want { + t.Errorf("At(0, 0):\ngot %#v\nwant %#v", got, want) + } + } +} + +// TestFloydSteinbergCheckerboard tests that the result of Floyd-Steinberg +// error diffusion of a uniform 50% gray source image with a black-and-white +// palette is a checkerboard pattern. +func TestFloydSteinbergCheckerboard(t *testing.T) { + b := image.Rect(0, 0, 640, 480) + // We can't represent 50% exactly, but 0x7fff / 0xffff is close enough. + src := &image.Uniform{color.Gray16{0x7fff}} + dst := image.NewPaletted(b, color.Palette{color.Black, color.White}) + FloydSteinberg.Draw(dst, b, src, image.Point{}) + nErr := 0 + for y := b.Min.Y; y < b.Max.Y; y++ { + for x := b.Min.X; x < b.Max.X; x++ { + got := dst.Pix[dst.PixOffset(x, y)] + want := uint8(x+y) % 2 + if got != want { + t.Errorf("at (%d, %d): got %d, want %d", x, y, got, want) + if nErr++; nErr == 10 { + t.Fatal("there may be more errors") + } + } + } + } +} + +// embeddedPaletted is an Image that behaves like an *image.Paletted but whose +// type is not *image.Paletted. +type embeddedPaletted struct { + *image.Paletted +} + +// TestPaletted tests that the drawPaletted function behaves the same +// regardless of whether dst is an *image.Paletted. +func TestPaletted(t *testing.T) { + f, err := os.Open("../testdata/video-001.png") + if err != nil { + t.Fatalf("open: %v", err) + } + defer f.Close() + video001, err := png.Decode(f) + if err != nil { + t.Fatalf("decode: %v", err) + } + b := video001.Bounds() + + cgaPalette := color.Palette{ + color.RGBA{0x00, 0x00, 0x00, 0xff}, + color.RGBA{0x55, 0xff, 0xff, 0xff}, + color.RGBA{0xff, 0x55, 0xff, 0xff}, + color.RGBA{0xff, 0xff, 0xff, 0xff}, + } + drawers := map[string]Drawer{ + "src": Src, + "floyd-steinberg": FloydSteinberg, + } + sources := map[string]image.Image{ + "uniform": &image.Uniform{color.RGBA{0xff, 0x7f, 0xff, 0xff}}, + "video001": video001, + } + + for dName, d := range drawers { + loop: + for sName, src := range sources { + dst0 := image.NewPaletted(b, cgaPalette) + dst1 := image.NewPaletted(b, cgaPalette) + d.Draw(dst0, b, src, image.Point{}) + d.Draw(embeddedPaletted{dst1}, b, src, image.Point{}) + for y := b.Min.Y; y < b.Max.Y; y++ { + for x := b.Min.X; x < b.Max.X; x++ { + if !eq(dst0.At(x, y), dst1.At(x, y)) { + t.Errorf("%s / %s: at (%d, %d), %v versus %v", + dName, sName, x, y, dst0.At(x, y), dst1.At(x, y)) + continue loop + } + } + } + } + } +} + +func TestSqDiff(t *testing.T) { + // This test is similar to the one from the image/color package, but + // sqDiff in this package accepts int32 instead of uint32, so test it + // for appropriate input. + + // canonical sqDiff implementation + orig := func(x, y int32) uint32 { + var d uint32 + if x > y { + d = uint32(x - y) + } else { + d = uint32(y - x) + } + return (d * d) >> 2 + } + testCases := []int32{ + 0, + 1, + 2, + 0x0fffd, + 0x0fffe, + 0x0ffff, + 0x10000, + 0x10001, + 0x10002, + 0x7ffffffd, + 0x7ffffffe, + 0x7fffffff, + -0x7ffffffd, + -0x7ffffffe, + -0x80000000, + } + for _, x := range testCases { + for _, y := range testCases { + if got, want := sqDiff(x, y), orig(x, y); got != want { + t.Fatalf("sqDiff(%#x, %#x): got %d, want %d", x, y, got, want) + } + } + } + if err := quick.CheckEqual(orig, sqDiff, &quick.Config{MaxCountScale: 10}); err != nil { + t.Fatal(err) + } +} diff --git a/src/image/draw/example_test.go b/src/image/draw/example_test.go new file mode 100644 index 0000000..2ccc2f4 --- /dev/null +++ b/src/image/draw/example_test.go @@ -0,0 +1,48 @@ +// Copyright 2016 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package draw_test + +import ( + "fmt" + "image" + "image/color" + "image/draw" + "math" +) + +func ExampleDrawer_floydSteinberg() { + const width = 130 + const height = 50 + + im := image.NewGray(image.Rectangle{Max: image.Point{X: width, Y: height}}) + for x := 0; x < width; x++ { + for y := 0; y < height; y++ { + dist := math.Sqrt(math.Pow(float64(x-width/2), 2)/3+math.Pow(float64(y-height/2), 2)) / (height / 1.5) * 255 + var gray uint8 + if dist > 255 { + gray = 255 + } else { + gray = uint8(dist) + } + im.SetGray(x, y, color.Gray{Y: 255 - gray}) + } + } + pi := image.NewPaletted(im.Bounds(), []color.Color{ + color.Gray{Y: 255}, + color.Gray{Y: 160}, + color.Gray{Y: 70}, + color.Gray{Y: 35}, + color.Gray{Y: 0}, + }) + + draw.FloydSteinberg.Draw(pi, im.Bounds(), im, image.ZP) + shade := []string{" ", "░", "▒", "▓", "█"} + for i, p := range pi.Pix { + fmt.Print(shade[p]) + if (i+1)%width == 0 { + fmt.Print("\n") + } + } +} |