draw

package
v0.0.0-...-d16ac53 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 13, 2016 License: Apache-2.0, BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package draw provides image composition functions.

See "The Go image/draw package" for an introduction to this package: http://golang.org/doc/articles/image_draw.html

This package is a superset of and a drop-in replacement for the image/draw package in the standard library.

Index

Constants

This section is empty.

Variables

View Source
var (
	// NearestNeighbor is the nearest neighbor interpolator. It is very fast,
	// but usually gives very low quality results. When scaling up, the result
	// will look 'blocky'.
	NearestNeighbor = Interpolator(nnInterpolator{})

	// ApproxBiLinear is a mixture of the nearest neighbor and bi-linear
	// interpolators. It is fast, but usually gives medium quality results.
	//
	// It implements bi-linear interpolation when upscaling and a bi-linear
	// blend of the 4 nearest neighbor pixels when downscaling. This yields
	// nicer quality than nearest neighbor interpolation when upscaling, but
	// the time taken is independent of the number of source pixels, unlike the
	// bi-linear interpolator. When downscaling a large image, the performance
	// difference can be significant.
	ApproxBiLinear = Interpolator(ablInterpolator{})

	// BiLinear is the tent kernel. It is slow, but usually gives high quality
	// results.
	BiLinear = &Kernel{1, func(t float64) float64 {
		return 1 - t
	}}

	// CatmullRom is the Catmull-Rom kernel. It is very slow, but usually gives
	// very high quality results.
	//
	// It is an instance of the more general cubic BC-spline kernel with parameters
	// B=0 and C=0.5. See Mitchell and Netravali, "Reconstruction Filters in
	// Computer Graphics", Computer Graphics, Vol. 22, No. 4, pp. 221-228.
	CatmullRom = &Kernel{2, func(t float64) float64 {
		if t < 1 {
			return (1.5*t-2.5)*t*t + 1
		}
		return ((-0.5*t+2.5)*t-4)*t + 2
	}}
)

Functions

func Copy

func Copy(dst Image, dp image.Point, src image.Image, sr image.Rectangle, opts *Options)

Copy copies the part of the source image defined by src and sr and writes to the part of the destination image defined by dst and the translation of sr so that sr.Min translates to dp.

func Draw

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

Draw calls DrawMask with a nil mask.

func DrawMask

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.

Types

type 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)
}

Drawer contains the Draw method.

var FloydSteinberg Drawer = floydSteinberg{}

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

type Image

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

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

type Interpolator

type Interpolator interface {
	Scaler
	Transformer
}

Interpolator is an interpolation algorithm, when dst and src pixels don't have a 1:1 correspondence.

Of the interpolators provided by this package:

  • NearestNeighbor is fast but usually looks worst.
  • CatmullRom is slow but usually looks best.
  • ApproxBiLinear has reasonable speed and quality.

The time taken depends on the size of dr. For kernel interpolators, the speed also depends on the size of sr, and so are often slower than non-kernel interpolators, especially when scaling down.

type Kernel

type Kernel struct {
	// Support is the kernel support and must be >= 0. At(t) is assumed to be
	// zero when t >= Support.
	Support float64
	// At is the kernel function. It will only be called with t in the
	// range [0, Support).
	At func(t float64) float64
}

Kernel is an interpolator that blends source pixels weighted by a symmetric kernel function.

func (*Kernel) NewScaler

func (q *Kernel) NewScaler(dw, dh, sw, sh int) Scaler

NewScaler returns a Scaler that is optimized for scaling multiple times with the same fixed destination and source width and height.

func (*Kernel) Scale

func (q *Kernel) Scale(dst Image, dr image.Rectangle, src image.Image, sr image.Rectangle, opts *Options)

Scale implements the Scaler interface.

func (*Kernel) Transform

func (q *Kernel) Transform(dst Image, s2d *f64.Aff3, src image.Image, sr image.Rectangle, opts *Options)

type Op

type Op int

Op is a Porter-Duff compositing operator.

const (
	// Over specifies “(src in mask) over dst”.
	Over Op = Op(draw.Over)
	// Src specifies “src in mask”.
	Src Op = Op(draw.Src)
)

func (Op) Draw

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 Options

type Options struct {
	// Op is the compositing operator. The default value is Over.
	Op Op

	// Masks limit what parts of the dst image are drawn to and what parts of
	// the src image are drawn from.
	//
	// A dst or src mask image having a zero alpha (transparent) pixel value in
	// the respective coordinate space means that that dst pixel is entirely
	// unaffected or that src pixel is considered transparent black. A full
	// alpha (opaque) value means that the dst pixel is maximally affected or
	// the src pixel contributes maximally. The default values, nil, are
	// equivalent to fully opaque, infinitely large mask images.
	//
	// The DstMask is otherwise known as a clip mask, and its pixels map 1:1 to
	// the dst image's pixels. DstMaskP in DstMask space corresponds to
	// image.Point{X:0, Y:0} in dst space. For example, when limiting
	// repainting to a 'dirty rectangle', use that image.Rectangle and a zero
	// image.Point as the DstMask and DstMaskP.
	//
	// The SrcMask's pixels map 1:1 to the src image's pixels. SrcMaskP in
	// SrcMask space corresponds to image.Point{X:0, Y:0} in src space. For
	// example, when drawing font glyphs in a uniform color, use an
	// *image.Uniform as the src, and use the glyph atlas image and the
	// per-glyph offset as SrcMask and SrcMaskP:
	//	Copy(dst, dp, image.NewUniform(color), image.Rect(0, 0, glyphWidth, glyphHeight), &Options{
	//		SrcMask:  glyphAtlas,
	//		SrcMaskP: glyphOffset,
	//	})
	DstMask  image.Image
	DstMaskP image.Point
	SrcMask  image.Image
	SrcMaskP image.Point
}

Options are optional parameters to Copy, Scale and Transform.

A nil *Options means to use the default (zero) values of each field.

type Quantizer

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
}

Quantizer produces a palette for an image.

type Scaler

type Scaler interface {
	Scale(dst Image, dr image.Rectangle, src image.Image, sr image.Rectangle, opts *Options)
}

Scaler scales the part of the source image defined by src and sr and writes to the part of the destination image defined by dst and dr.

A Scaler is safe to use concurrently.

type Transformer

type Transformer interface {
	Transform(dst Image, m *f64.Aff3, src image.Image, sr image.Rectangle, opts *Options)
}

Transformer transforms the part of the source image defined by src and sr and writes to the part of the destination image defined by dst and the affine transform m applied to sr.

For example, if m is the matrix

m00 m01 m02 m10 m11 m12

then the src-space point (sx, sy) maps to the dst-space point (m00*sx + m01*sy + m02, m10*sx + m11*sy + m12).

A Transformer is safe to use concurrently.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL