Esempio n. 1
1
// 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)
	}
}
Esempio n. 2
0
func (c *Context) RenderMultiline(txt []string, size float64, bg, fg color.Color) (*image.RGBA, error) {
	w, h := 0, 0
	imgs := []*image.RGBA{}

	for _, l := range txt {
		i, err := c.Render(l, size, fg)
		if err != nil {
			return nil, err
		}
		if i.Bounds().Dx() > w {
			w = i.Bounds().Dx()
		}
		h += i.Bounds().Dy()
		imgs = append(imgs, i)
	}

	dst := image.NewRGBA(image.Rect(0, 0, w, h))
	draw.Draw(dst, dst.Bounds(), image.NewUniform(bg), image.ZP, draw.Src)
	y := 0
	for _, src := range imgs {
		sr := src.Bounds()
		dp := image.Point{0, y}
		r := image.Rectangle{dp, dp.Add(sr.Size())}
		draw.Draw(dst, r, src, sr.Min, draw.Src)
		y += sr.Dy()
	}

	return dst, nil
}
Esempio n. 3
0
func parseImgBoundary(img image.Image) {
	minX, maxX := img.Bounds().Min.X, img.Bounds().Max.X
	minY, maxY := img.Bounds().Min.Y, img.Bounds().Max.Y
	for i := minX; i < maxX; i++ {
		for j := minY; j < maxY; j++ {
			_, _, _, a := img.At(i, j).RGBA()
			if a != 0 {
				if boundary.Empty() && boundary.Min.X == -1 {
					boundary = image.Rect(i, j, i, j)
				} else {
					_p := image.Point{i, j}
					if !_p.In(boundary) {
						boundary = boundary.Union(image.Rect(i, j, i, j))
					}
				}
			}
		}
	}

	// Should Make the midline of boundary and the img
	l := boundary.Min.X
	r := imageW - boundary.Max.X
	if l > r {
		boundary.Min.X = r
	} else if l < r {
		boundary.Max.X = imageW - l
	}
}
Esempio n. 4
0
// originTrans translates the origin with respect to the current image and the
// current canvas size. This makes sure we never incorrect position the image.
// (i.e., panning never goes too far, and whenever the canvas is bigger than
// the image, the origin is *always* (0, 0).
func originTrans(pt image.Point, win *window, img *vimage) image.Point {
	// If there's no valid image, then always return (0, 0).
	if img == nil {
		return image.Point{0, 0}
	}

	// Quick aliases.
	ww, wh := win.Geom.Width(), win.Geom.Height()
	dw := img.Bounds().Dx() - ww
	dh := img.Bounds().Dy() - wh

	// Set the allowable range of the origin point of the image.
	// i.e., never less than (0, 0) and never greater than the width/height
	// of the image that isn't viewable at any given point (which is determined
	// by the canvas size).
	pt.X = min(img.Bounds().Min.X+dw, max(pt.X, 0))
	pt.Y = min(img.Bounds().Min.Y+dh, max(pt.Y, 0))

	// Validate origin point. If the width/height of an image is smaller than
	// the canvas width/height, then the image origin cannot change in x/y
	// direction.
	if img.Bounds().Dx() < ww {
		pt.X = 0
	}
	if img.Bounds().Dy() < wh {
		pt.Y = 0
	}

	return pt
}
Esempio n. 5
0
func (f *Frame) _draw(pt image.Point) image.Point {
	for nb := 0; nb < f.nbox; nb++ {
		b := f.box[nb]
		f.cklinewrap0(&pt, b)
		if pt.Y == f.Rect.Max.Y {
			f.nchars -= f.strlen(nb)
			f.delbox(nb, f.nbox-1)
			break
		}

		if b.Nrune > 0 {
			n, fits := f.canfit(pt, b)
			if !fits {
				break
			}
			if n != b.Nrune {
				f.splitbox(uint64(nb), uint64(n))
				b = f.box[nb]
			}
			pt.X += b.Wid
		} else {
			if b.Bc == '\n' {
				pt.X = f.Rect.Min.X
				pt.Y += f.Font.Height
			} else {
				pt.X += f.newwid(pt, b)
			}
		}
	}
	return pt
}
Esempio n. 6
0
// Clones the image if resize is not necessary.
func resizeIfNec(size image.Point, im image.Image, interp resize.InterpolationFunction) image.Image {
	if size.Eq(im.Bounds().Size()) {
		return clone(im)
	}
	log.Printf("resize %v to %v", im.Bounds().Size(), size)
	return resize.Resize(uint(size.X), uint(size.Y), im, interp)
}
Esempio n. 7
0
func (d *dimensionChanger) At(x, y int) color.Color {
	p := image.Point{x, y}
	if !p.In(d.bounds) {
		return nil
	}
	return d.Image.At(x*d.pixel.Dx()+d.Image.Bounds().Canon().Min.X, y*d.pixel.Dy()+d.Image.Bounds().Canon().Min.Y)
}
Esempio n. 8
0
func main() {
	bounds := image.Rect(0, 0, 100, 100)
	im := image.NewGray(bounds)
	gBlack := color.Gray{0}
	gWhite := color.Gray{255}
	draw.Draw(im, bounds, image.NewUniform(gWhite), image.ZP, draw.Src)
	pos := image.Point{50, 50}
	dir := up
	for pos.In(bounds) {
		switch im.At(pos.X, pos.Y).(color.Gray).Y {
		case gBlack.Y:
			im.SetGray(pos.X, pos.Y, gWhite)
			dir--
		case gWhite.Y:
			im.SetGray(pos.X, pos.Y, gBlack)
			dir++
		}
		if dir&1 == 1 {
			pos.X += 1 - dir&2
		} else {
			pos.Y -= 1 - dir&2
		}
	}
	f, err := os.Create("ant.png")
	if err != nil {
		fmt.Println(err)
		return
	}
	if err = png.Encode(f, im); err != nil {
		fmt.Println(err)
	}
	if err = f.Close(); err != nil {
		fmt.Println(err)
	}
}
Esempio n. 9
0
func (self *Image) sourceRectTouchOriginalFromInside(width, height int, horAlign HorAlignment, verAlign VerAlignment) (r image.Rectangle) {
	var offset image.Point
	aspectRatio := float64(width) / float64(height)
	if aspectRatio > self.AspectRatio() {
		// Wider than original
		// so touchOriginalFromInside means
		// that the source rect is as wide as the original
		r.Max.X = self.Width()
		r.Max.Y = int(float64(self.Width()) / aspectRatio)
		switch verAlign {
		case VerCenter:
			offset.Y = (self.Height() - r.Max.Y) / 2
		case Bottom:
			offset.Y = self.Height() - r.Max.Y
		}
	} else {
		// Heigher than original,
		// so touchOriginalFromInside means
		// that the source rect is as high as the original
		r.Max.X = int(float64(self.Height()) * aspectRatio)
		r.Max.Y = self.Height()
		switch horAlign {
		case HorCenter:
			offset.X = (self.Width() - r.Max.X) / 2
		case Right:
			offset.X = self.Width() - r.Max.X
		}
	}
	return r.Add(offset)
}
Esempio n. 10
0
func (si *subimage) Set(x, y int, c color.Color) {
	p := image.Point{x + si.bounds.Min.X, y + si.bounds.Min.Y}
	if !p.In(si.Bounds()) {
		return
	}
	si.Image.Set(p.X, p.Y, c)
}
Esempio n. 11
0
func (b *bucket) addPixel(pixel image.Point) {
	b.group.wire.pixels = append(b.group.wire.pixels, pixel)
	b.group.wire.bounds = b.group.wire.bounds.Union(
		image.Rectangle{
			pixel,
			pixel.Add(image.Point{1, 1})})
}
Esempio n. 12
0
func (si *subimage) At(x, y int) color.Color {
	p := image.Point{x + si.bounds.Min.X, y + si.bounds.Min.Y}
	if !p.In(si.Bounds()) {
		return color.Black
	}
	return si.Image.At(p.X, p.Y)
}
Esempio n. 13
0
// Paste pastes the img image to the background image at the specified position and returns the combined image.
func Paste(background, img image.Image, pos image.Point) *image.NRGBA {
	src := toNRGBA(img)
	dst := Clone(background)                    // cloned image bounds start at (0, 0)
	startPt := pos.Sub(background.Bounds().Min) // so we should translate start point
	endPt := startPt.Add(src.Bounds().Size())
	pasteBounds := image.Rectangle{startPt, endPt}

	if dst.Bounds().Overlaps(pasteBounds) {
		intersectBounds := dst.Bounds().Intersect(pasteBounds)

		rowSize := intersectBounds.Dx() * 4
		numRows := intersectBounds.Dy()

		srcStartX := intersectBounds.Min.X - pasteBounds.Min.X
		srcStartY := intersectBounds.Min.Y - pasteBounds.Min.Y

		i0 := dst.PixOffset(intersectBounds.Min.X, intersectBounds.Min.Y)
		j0 := src.PixOffset(srcStartX, srcStartY)

		di := dst.Stride
		dj := src.Stride

		for row := 0; row < numRows; row++ {
			copy(dst.Pix[i0:i0+rowSize], src.Pix[j0:j0+rowSize])
			i0 += di
			j0 += dj
		}
	}

	return dst
}
Esempio n. 14
0
func (i *imageSlice) Set(x, y int, c color.Color) {
	p := image.Point{x, y}
	if p.In(i.r) {
		p = p.Add(i.p)
		i.img.Set(p.X, p.Y, c)
	}
}
Esempio n. 15
0
func canfit(p *Piece) bool {
	var dx = [...]int{0, -1, 1, -2, 2, -3, 3, 4, -4}
	j := N + 1
	if j >= 4 {
		j = p.sz.X
		if j < p.sz.Y {
			j = p.sz.Y
		}
		j = 2*j - 1
	}
	for i := 0; i < j; i++ {
		var z image.Point
		z.X = pos.X + dx[i]*pcsz
		z.Y = pos.Y
		if !collide(z, p) {
			z.Y = pos.Y + pcsz - 1
			if !collide(z, p) {
				undrawpiece()
				pos.X = z.X
				return true
			}
		}
	}
	return false
}
Esempio n. 16
0
func (si *subimage) At(x, y int) color.Color {
	p := image.Point{x, y}
	if !p.In(si.Bounds()) {
		return color.Black
	}
	return si.Image.At(x, y)
}
Esempio n. 17
0
func (si *subimage) Set(x, y int, c color.Color) {
	p := image.Point{x, y}
	if !p.In(si.Bounds()) {
		return
	}
	si.Image.Set(x, y, c)
}
Esempio n. 18
0
// Adjust adjusts the two images aligning ther centers. The background
// of the smaller image is filled with FillColor. The returned images
// are of the same size.
func (c Centerer) Adjust(img1, img2 image.Image) (image.Image, image.Image) {
	img1Rect := img1.Bounds()
	img2Rect := img2.Bounds()
	backgroundRect := img1Rect.Union(img2Rect)

	dstImg1 := image.NewRGBA(backgroundRect)
	dstImg2 := image.NewRGBA(backgroundRect)

	// Fill destination images with FillColor
	draw.Draw(dstImg1, dstImg1.Bounds(), &image.Uniform{c.FillColor}, image.ZP, draw.Src)
	draw.Draw(dstImg2, dstImg2.Bounds(), &image.Uniform{c.FillColor}, image.ZP, draw.Src)

	// Copy img1 to the center of dstImg1
	dp := image.Point{
		(backgroundRect.Max.X-backgroundRect.Min.X)/2 - (img1Rect.Max.X-img1Rect.Min.X)/2,
		(backgroundRect.Max.Y-backgroundRect.Min.Y)/2 - (img1Rect.Max.Y-img1Rect.Min.Y)/2,
	}
	r := image.Rectangle{dp, dp.Add(img1Rect.Size())}
	draw.Draw(dstImg1, r, img1, image.ZP, draw.Src)

	// Copy img2 to the center of dstImg2
	dp = image.Point{
		(backgroundRect.Max.X-backgroundRect.Min.X)/2 - (img2Rect.Max.X-img2Rect.Min.X)/2,
		(backgroundRect.Max.Y-backgroundRect.Min.Y)/2 - (img2Rect.Max.Y-img2Rect.Min.Y)/2,
	}
	r = image.Rectangle{dp, dp.Add(img2Rect.Size())}
	draw.Draw(dstImg2, r, img2, image.ZP, draw.Src)

	return dstImg1, dstImg2
}
Esempio n. 19
0
// 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
}
Esempio n. 20
0
// imgHandle serves images of the player's view.
func imgHandle(w http.ResponseWriter, r *http.Request) {
	quality, err := strconv.Atoi(r.FormValue("quality"))
	if err != nil || quality < 0 || quality > 100 {
		quality = 70
	}

	// Center Gopher in view if possible
	gpos := model.Gopher.Pos
	rect := image.Rect(0, 0, ViewWidth, ViewHeight).Add(image.Pt(int(gpos.X)-ViewWidth/2, int(gpos.Y)-ViewHeight/2))

	// But needs correction at the edges of the view (it can't be centered)
	corr := image.Point{}
	if rect.Min.X < 0 {
		corr.X = -rect.Min.X
	}
	if rect.Min.Y < 0 {
		corr.Y = -rect.Min.Y
	}
	if rect.Max.X > model.LabWidth {
		corr.X = model.LabWidth - rect.Max.X
	}
	if rect.Max.Y > model.LabHeight {
		corr.Y = model.LabHeight - rect.Max.Y
	}
	rect = rect.Add(corr)

	model.Mutex.Lock()
	jpeg.Encode(w, model.LabImg.SubImage(rect), &jpeg.Options{quality})
	model.Mutex.Unlock()

	// Store the new view's position:
	Pos = rect.Min
}
Esempio n. 21
0
func (d *dimensionChanger) Set(x, y int, c color.Color) {
	p := image.Point{x, y}
	if !p.In(d.bounds) {
		return
	}
	draw.Draw(d.Image, d.pixel.Add(image.Point{x * d.pixel.Dx(), y * d.pixel.Dy()}).Add(d.Image.Bounds().Canon().Min), &image.Uniform{c}, image.Point{0, 0}, draw.Over)
}
Esempio n. 22
0
func drawPoint(point Point, dst *image.RGBA, src image.Image) {
	p := image.Point{point.X, point.Y}
	srcRect := src.Bounds()
	size := srcRect.Size()
	rect := image.Rectangle{p, p.Add(size)}
	draw.Draw(dst, rect, src, srcRect.Min, draw.Src)
}
Esempio n. 23
0
func addPortal(p image.Point) {
	for _, portal := range portals {
		if p.In(portal) {
			return
		}
	}
	portals = append(portals, image.Rect(p.X-7, p.Y, p.X+9, p.Y+16))
}
Esempio n. 24
0
// (overwrite BBoxObject default implementation)
func (n *Node) SetPosition(pos image.Point) {
	shift := pos.Sub(n.Position())
	n.positioner.SetPosition(pos)
	n.BBoxDefaultSetPosition(pos)
	for _, p := range n.ports {
		p.SetPosition(p.Position().Add(shift))
	}
}
Esempio n. 25
0
File: gen.go Progetto: jvlmdr/go-cv
// ToImageRect converts a point in the feature pyramid to a rectangle in the image.
func (pyr *Generator) ToImageRect(level int, pt image.Point, interior image.Rectangle) image.Rectangle {
	// Translate interior by position (scaled by rate) and subtract margin offset.
	rate := pyr.Transform.Rate()
	offset := pyr.Pad.Margin.TopLeft()
	scale := pyr.Image.Scales[level]
	rect := interior.Add(pt.Mul(rate)).Sub(offset)
	return scaleRect(1/scale, rect)
}
Esempio n. 26
0
func (i *imageSlice) At(x, y int) color.Color {
	p := image.Point{x, y}
	if p.In(i.r) {
		p = p.Add(i.p)
		return i.img.At(p.X, p.Y)
	}
	return color.RGBA{0, 0, 0, 0}
}
Esempio n. 27
0
func ClickInner(root *Widget, p image.Point) {
	for _, w := range root.Widgets {
		q := p.Sub(root.Rect.Min)
		if q.In(w.Rect) {
			w.Click(q)
		}
	}
}
Esempio n. 28
0
func (f *Frame) advance(p *image.Point, b *frbox) {
	if b.Nrune < 0 && b.Bc == '\n' {
		p.X = f.Rect.Min.X
		p.Y += f.Font.Height
	} else {
		p.X += b.Wid
	}
}
Esempio n. 29
0
// 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 Copy(dst Image, dp image.Point, src image.Image, sr image.Rectangle, opts *Options) {
	mask, mp, op := image.Image(nil), image.Point{}, Over
	if opts != nil {
		// TODO: set mask, mp and op.
	}
	dr := sr.Add(dp.Sub(sr.Min))
	DrawMask(dst, dr, src, sr.Min, mask, mp, op)
}
Esempio n. 30
0
// 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 Copy(dst Image, dp image.Point, src image.Image, sr image.Rectangle, opts *Options) {
	var o Options
	if opts != nil {
		o = *opts
	}
	dr := sr.Add(dp.Sub(sr.Min))
	// TODO: honor o.DstMask and o.SrcMask.
	DrawMask(dst, dr, src, sr.Min, nil, image.Point{}, o.Op)
}