當前位置: 首頁>>代碼示例>>Golang>>正文


Golang draw.Image類代碼示例

本文整理匯總了Golang中image/draw.Image的典型用法代碼示例。如果您正苦於以下問題:Golang Image類的具體用法?Golang Image怎麽用?Golang Image使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Image類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: Draw

// Draw renders the given cpu cores on img.
func (app *App) Draw(img draw.Image, cpus []CPU) {
	rect := img.Bounds()
	bg := app.Background
	if bg == nil {
		bg = image.Black
	}
	draw.Draw(img, rect, bg, bg.Bounds().Min, draw.Over)

	if len(cpus) == 0 {
		return
	}

	cpuDx := rect.Dx() / len(cpus)
	ptIncr := image.Point{X: cpuDx}
	ptDelta := image.Point{}
	rectDx := image.Rectangle{
		Min: rect.Min,
		Max: rect.Max,
	}
	rectDx.Max.X = rect.Min.X + cpuDx
	for _, cpu := range cpus {
		irect := image.Rectangle{
			Min: rectDx.Min.Add(ptDelta),
			Max: rectDx.Max.Add(ptDelta),
		}
		subimg := SubImage(img, irect)
		app.renderCPU(subimg, cpu)

		ptDelta = ptDelta.Add(ptIncr)
	}
}
開發者ID:bmatsuo,項目名稱:dockapp-go,代碼行數:32,代碼來源:main.go

示例2: gennoise

func gennoise(screen draw.Image) {
	for y := 0; y < 240; y++ {
		for x := 0; x < 320; x++ {
			screen.Set(x, y, <-randcol)
		}
	}
}
開發者ID:travis1230,項目名稱:RosettaCodeData,代碼行數:7,代碼來源:image-noise-1.go

示例3: PasteAt

func (p *Drawer) PasteAt(img draw.Image, pt P) {
	flr := pt.Floor()
	bounds := img.Bounds()
	dp := image.Point{flr[0], flr[1]}
	rec := bounds.Sub(bounds.Min).Add(dp)
	draw.Draw(p.Img, rec, img, bounds.Min, draw.Over)
}
開發者ID:sore0159,項目名稱:golang-helpers,代碼行數:7,代碼來源:merge.go

示例4: Thumbnail

// Thumbnail scales and crops src so it fits in dst.
func Thumbnail(dst draw.Image, src image.Image) error {
	// Scale down src in the dimension that is closer to dst.
	sb := src.Bounds()
	db := dst.Bounds()
	rx := float64(sb.Dx()) / float64(db.Dx())
	ry := float64(sb.Dy()) / float64(db.Dy())
	var b image.Rectangle
	if rx < ry {
		b = image.Rect(0, 0, db.Dx(), int(float64(sb.Dy())/rx))
	} else {
		b = image.Rect(0, 0, int(float64(sb.Dx())/ry), db.Dy())
	}

	buf := image.NewRGBA(b)
	if err := Scale(buf, src); err != nil {
		return err
	}

	// Crop.
	// TODO(crawshaw): improve on center-alignment.
	var pt image.Point
	if rx < ry {
		pt.Y = (b.Dy() - db.Dy()) / 2
	} else {
		pt.X = (b.Dx() - db.Dx()) / 2
	}
	draw.Draw(dst, db, buf, pt, draw.Src)
	return nil
}
開發者ID:sandbreaker,項目名稱:graphics-go,代碼行數:30,代碼來源:thumbnail.go

示例5: line

func line(img draw.Image, a, b image.Point, c color.Color) {
	minx, maxx := minmax(a.X, b.X)
	miny, maxy := minmax(a.Y, b.Y)

	Δx := float64(b.X - a.X)
	Δy := float64(b.Y - a.Y)

	if maxx-minx > maxy-miny {
		d := 1
		if a.X > b.X {
			d = -1
		}
		for x := 0; x != b.X-a.X+d; x += d {
			y := int(float64(x) * Δy / Δx)
			img.Set(a.X+x, a.Y+y, c)
		}
	} else {
		d := 1
		if a.Y > b.Y {
			d = -1
		}
		for y := 0; y != b.Y-a.Y+d; y += d {
			x := int(float64(y) * Δx / Δy)
			img.Set(a.X+x, a.Y+y, c)
		}
	}
}
開發者ID:tstranex,項目名稱:carpcomm,代碼行數:27,代碼來源:map.go

示例6: line

// http://en.wikipedia.org/wiki/Bresenham's_line_algorithm#Simplification
func line(x0, y0, x1, y1 int, c color.Color, img draw.Image) {
	var dx = abs(x1 - x0)
	var dy = abs(y1 - y0)
	var err = dx - dy
	var sx, sy = 1, 1

	if x0 > x1 {
		sx = -1
	}
	if y0 > y1 {
		sy = -1
	}

	img.Set(x0, y0, c)
	for x0 != x1 || y0 != y1 {
		var e2 = 2 * err
		if e2 > -dy {
			err -= dy
			x0 += sx
		}
		if e2 < dx {
			err += dx
			y0 += sy
		}
		img.Set(x0, y0, c)
	}

}
開發者ID:rodolf0,項目名稱:bezier,代碼行數:29,代碼來源:bresenham.go

示例7: Bresenham

func Bresenham(img draw.Image, color color.Color, x0, y0, x1, y1 int) {
	dx := abs(x1 - x0)
	dy := abs(y1 - y0)
	var sx, sy int
	if x0 < x1 {
		sx = 1
	} else {
		sx = -1
	}
	if y0 < y1 {
		sy = 1
	} else {
		sy = -1
	}
	err := dx - dy

	var e2 int
	for {
		img.Set(x0, y0, color)
		if x0 == x1 && y0 == y1 {
			return
		}
		e2 = 2 * err
		if e2 > -dy {
			err = err - dy
			x0 = x0 + sx
		}
		if e2 < dx {
			err = err + dx
			y0 = y0 + sy
		}
	}
}
開發者ID:nolenroyalty,項目名稱:bangarang,代碼行數:33,代碼來源:line.go

示例8: NewImage

// NewImage returns a new image canvas
// that draws to the given image.  The
// minimum point of the given image
// should probably be 0,0.
func NewImage(img draw.Image, name string) (*Canvas, error) {
	w := float64(img.Bounds().Max.X - img.Bounds().Min.X)
	h := float64(img.Bounds().Max.Y - img.Bounds().Min.Y)

	X, err := xgbutil.NewConn()
	if err != nil {
		return nil, err
	}
	keybind.Initialize(X)
	ximg := xgraphics.New(X, image.Rect(0, 0, int(w), int(h)))
	err = ximg.CreatePixmap()
	if err != nil {
		return nil, err
	}
	painter := NewPainter(ximg)
	gc := draw2d.NewGraphicContextWithPainter(ximg, painter)
	gc.SetDPI(dpi)
	gc.Scale(1, -1)
	gc.Translate(0, -h)

	wid := ximg.XShowExtra(name, true)
	go func() {
		xevent.Main(X)
	}()

	c := &Canvas{
		Canvas: vgimg.NewWith(vgimg.UseImageWithContext(img, gc)),
		x:      X,
		ximg:   ximg,
		wid:    wid,
	}
	vg.Initialize(c)
	return c, nil
}
開發者ID:nolenroyalty,項目名稱:bangarang,代碼行數:38,代碼來源:canvas.go

示例9: decodeP6

// decodeP6 reads a binary pixmap
func decodeP6(r *bufio.Reader, img draw.Image, width, height uint) {
	var x, y, pix int

	mask := byte(readU(r))
	mul := 255 / mask
	data := make([]byte, width*height*3)

	space(r)

	_, err := r.Read(data)
	check(err)

	for y = 0; y < int(height); y++ {
		for x = 0; x < int(width); x++ {
			img.Set(x, y, color.RGBA{
				(data[pix] & mask) * mul,
				(data[pix+1] & mask) * mul,
				(data[pix+2] & mask) * mul,
				0xff,
			})

			pix += 3
		}
	}
}
開發者ID:jteeuwen,項目名稱:pnm,代碼行數:26,代碼來源:decode.go

示例10: GetImage

func (ff *FontFace) GetImage(text string) (img draw.Image, err error) {
	var (
		src image.Image
		bg  image.Image
		dst draw.Image
		pt  fixed.Point26_6
		w   int
		h   int
	)
	src = image.NewUniform(ff.fg)
	bg = image.NewUniform(ff.bg)
	w = int(float32(len(text)) * ff.charw)
	h = int(ff.charh)
	dst = image.NewRGBA(image.Rect(0, 0, w, h))
	draw.Draw(dst, dst.Bounds(), bg, image.ZP, draw.Src)
	ff.context.SetSrc(src)
	ff.context.SetDst(dst)
	ff.context.SetClip(dst.Bounds())
	pt = freetype.Pt(0, int(ff.charh+ff.offy))
	if pt, err = ff.context.DrawString(text, pt); err != nil {
		return
	}
	img = image.NewRGBA(image.Rect(0, 0, int(pt.X/64), int(pt.Y/64)))
	draw.Draw(img, img.Bounds(), dst, image.Pt(0, -int(ff.offy)), draw.Src)
	return
}
開發者ID:kurrik,項目名稱:opengl-benchmarks,代碼行數:26,代碼來源:fontface.go

示例11: decodeP4

// decodeP4 reads a binary bitmap
func decodeP4(r *bufio.Reader, img draw.Image, width, height uint) {
	var x, y, bit int

	bytes := int(math.Ceil((float64(width) / 8)))
	bits := newBitset(uint(bytes) * height * 8)
	pad := (bytes * 8) - int(width)

	space(r)

	_, err := r.Read(bits)
	check(err)

	for y = 0; y < int(height); y++ {
		for x = 0; x < int(width); x++ {
			if bits.Test(bit) {
				img.Set(x, y, color.Alpha{0xff})
			} else {
				img.Set(x, y, color.Alpha{0x00})
			}

			bit++
		}

		bit += pad
	}
}
開發者ID:jteeuwen,項目名稱:pnm,代碼行數:27,代碼來源:decode.go

示例12: Convolve

// Convolve produces dst by applying the convolution kernel k to src.
func Convolve(dst draw.Image, src image.Image, k Kernel) (err error) {
	if dst == nil || src == nil || k == nil {
		return nil
	}

	b := dst.Bounds()
	dstRgba, ok := dst.(*image.RGBA)
	if !ok {
		dstRgba = image.NewRGBA(b)
	}

	switch k := k.(type) {
	case *SeparableKernel:
		err = convolveRGBASep(dstRgba, src, k)
	default:
		err = convolveRGBA(dstRgba, src, k)
	}

	if err != nil {
		return err
	}

	if !ok {
		draw.Draw(dst, b, dstRgba, b.Min, draw.Src)
	}
	return nil
}
開發者ID:sandbreaker,項目名稱:graphics-go,代碼行數:28,代碼來源:convolve.go

示例13: NewFromImage

// NewFromImage uses the given image as the destination for all calls to Add.
// It is assumed to be empty at the beginning so all the available space will be
// used for sub-images.
func NewFromImage(atlas draw.Image) *Atlas {
	packer := binpacker.New(atlas.Bounds().Dx(), atlas.Bounds().Dy())
	return &Atlas{
		Image:  atlas,
		packer: packer,
	}
}
開發者ID:gonutz,項目名稱:atlas,代碼行數:10,代碼來源:atlas.go

示例14: Overturn

// 翻轉函數,要求兩個圖像大小契合,act&1 == 0則左右翻轉,否則垂直翻轉。
func Overturn(dst draw.Image, src image.Image, act int) error {
	var to func(int, int) (int, int)
	sr := src.Bounds()
	dr := dst.Bounds()
	W := dr.Max.X - dr.Min.X
	H := dr.Max.Y - dr.Min.Y
	if H <= 0 || W <= 0 {
		return errors.New("target image is empty or noncanonical")
	}
	if sr.Min.X >= sr.Max.X || sr.Min.Y >= sr.Max.Y {
		return errors.New("source image is empty or noncanonical")
	}
	if sr.Max.X-sr.Min.X != W || sr.Max.Y-sr.Min.Y != H {
		return errors.New("target and source must be same size!")
	}
	if act&1 == 0 {
		to = func(x, y int) (int, int) {
			return W - 1 - x, y
		}
	} else {
		to = func(x, y int) (int, int) {
			return x, H - 1 - y
		}
	}
	for i := 0; i < W; i++ {
		for j := 0; j < H; j++ {
			x, y := to(i, j)
			dst.Set(dr.Min.X+x, dr.Min.Y+y, src.At(sr.Min.X+i, sr.Min.Y+j))
		}
	}
	return nil
}
開發者ID:hydra13142,項目名稱:paint,代碼行數:33,代碼來源:spin.go

示例15: Transform

// Transform applies the affine transform to src and produces dst.
func (a Affine) Transform(dst draw.Image, src image.Image, i interp.Interp) error {
	if dst == nil {
		return errors.New("graphics: dst is nil")
	}
	if src == nil {
		return errors.New("graphics: src is nil")
	}

	// RGBA fast path.
	dstRGBA, dstOk := dst.(*image.RGBA)
	srcRGBA, srcOk := src.(*image.RGBA)
	interpRGBA, interpOk := i.(interp.RGBA)
	if dstOk && srcOk && interpOk {
		return a.transformRGBA(dstRGBA, srcRGBA, interpRGBA)
	}

	srcb := src.Bounds()
	b := dst.Bounds()
	for y := b.Min.Y; y < b.Max.Y; y++ {
		for x := b.Min.X; x < b.Max.X; x++ {
			sx, sy := a.pt(x, y)
			if inBounds(srcb, sx, sy) {
				dst.Set(x, y, i.Interp(src, sx, sy))
			}
		}
	}
	return nil
}
開發者ID:UssieApp,項目名稱:graphics-go,代碼行數:29,代碼來源:affine.go


注:本文中的image/draw.Image類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。