version: 1.10

package draw

import "image/draw"

Overview

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

Index

Examples

Package files

draw.go

func Draw

  1. func Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point, op Op)

Draw calls DrawMask with a nil mask.

func DrawMask

  1. func DrawMask(dst Image, r image.Rectangle, src image.Image, sp image.Point, mask image.Image, mp image.Point, op 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.

type Drawer

  1. type Drawer interface {
  2. // Draw aligns r.Min in dst with sp in src and then replaces the
  3. // rectangle r in dst with the result of drawing src on dst.
  4. Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point)
  5. }

Drawer contains the Draw method.

  1. var FloydSteinberg Drawer = floydSteinberg{}

FloydSteinberg is a Drawer that is the Src Op with Floyd-Steinberg error diffusion.

Example:

  1. const width = 130
  2. const height = 50
  3. im := image.NewGray(image.Rectangle{Max: image.Point{X: width, Y: height}})
  4. for x := 0; x < width; x++ {
  5. for y := 0; y < height; y++ {
  6. dist := math.Sqrt(math.Pow(float64(x-width/2), 2)/3+math.Pow(float64(y-height/2), 2)) / (height / 1.5) * 255
  7. var gray uint8
  8. if dist > 255 {
  9. gray = 255
  10. } else {
  11. gray = uint8(dist)
  12. }
  13. im.SetGray(x, y, color.Gray{Y: 255 - gray})
  14. }
  15. }
  16. pi := image.NewPaletted(im.Bounds(), []color.Color{
  17. color.Gray{Y: 255},
  18. color.Gray{Y: 160},
  19. color.Gray{Y: 70},
  20. color.Gray{Y: 35},
  21. color.Gray{Y: 0},
  22. })
  23. draw.FloydSteinberg.Draw(pi, im.Bounds(), im, image.ZP)
  24. shade := []string{" ", "░", "▒", "▓", "█"}
  25. for i, p := range pi.Pix {
  26. fmt.Print(shade[p])
  27. if (i+1)%width == 0 {
  28. fmt.Print("\n")
  29. }
  30. }

type Image

  1. type Image interface {
  2. image.Image
  3. Set(x, y int, c color.Color)
  4. }

Image is an image.Image with a Set method to change a single pixel.

type Op

  1. type Op int

Op is a Porter-Duff compositing operator.

  1. const (
  2. // Over specifies ``(src in mask) over dst''.
  3. Over Op = iota
  4. // Src specifies ``src in mask''.
  5. Src
  6. )

func (Op) Draw

  1. func (op Op) Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point)

Draw implements the Drawer interface by calling the Draw function with this Op.

type Quantizer

  1. type Quantizer interface {
  2. // Quantize appends up to cap(p) - len(p) colors to p and returns the
  3. // updated palette suitable for converting m to a paletted image.
  4. Quantize(p color.Palette, m image.Image) color.Palette
  5. }

Quantizer produces a palette for an image.