示例#1
0
文件: image.go 项目: ajstarks/chart
// textBox renders t into a tight fitting image
func (ig *ImageGraphics) textBox(t string, size int) image.Image {
	// Initialize the context.
	fg := image.NewUniform(color.Alpha{0xff})
	bg := image.NewUniform(color.Alpha{0x00})
	canvas := image.NewAlpha(image.Rect(0, 0, 400, 2*size))
	draw.Draw(canvas, canvas.Bounds(), bg, image.ZP, draw.Src)

	c := freetype.NewContext()
	c.SetDPI(dpi)
	c.SetFont(ig.font)
	c.SetFontSize(float64(size))
	c.SetClip(canvas.Bounds())
	c.SetDst(canvas)
	c.SetSrc(fg)

	// Draw the text.
	h := c.FUnitToPixelRU(ig.font.UnitsPerEm())
	pt := freetype.Pt(0, h)
	extent, err := c.DrawString(t, pt)
	if err != nil {
		log.Println(err)
		return nil
	}
	// log.Printf("text %q, extent: %v", t, extent)
	return canvas.SubImage(image.Rect(0, 0, int(extent.X/256), h*5/4))
}
示例#2
0
func TestSrcMask(t *testing.T) {
	srcMask := image.NewRGBA(image.Rect(0, 0, 23, 1))
	srcMask.SetRGBA(19, 0, color.RGBA{0x00, 0x00, 0x00, 0x7f})
	srcMask.SetRGBA(20, 0, color.RGBA{0x00, 0x00, 0x00, 0xff})
	srcMask.SetRGBA(21, 0, color.RGBA{0x00, 0x00, 0x00, 0x3f})
	srcMask.SetRGBA(22, 0, color.RGBA{0x00, 0x00, 0x00, 0x00})
	red := image.NewUniform(color.RGBA{0xff, 0x00, 0x00, 0xff})
	blue := image.NewUniform(color.RGBA{0x00, 0x00, 0xff, 0xff})
	dst := image.NewRGBA(image.Rect(0, 0, 6, 1))
	Copy(dst, image.Point{}, blue, dst.Bounds(), Src, nil)
	NearestNeighbor.Scale(dst, dst.Bounds(), red, image.Rect(0, 0, 3, 1), Over, &Options{
		SrcMask:  srcMask,
		SrcMaskP: image.Point{20, 0},
	})
	got := [6]color.RGBA{
		dst.RGBAAt(0, 0),
		dst.RGBAAt(1, 0),
		dst.RGBAAt(2, 0),
		dst.RGBAAt(3, 0),
		dst.RGBAAt(4, 0),
		dst.RGBAAt(5, 0),
	}
	want := [6]color.RGBA{
		{0xff, 0x00, 0x00, 0xff},
		{0xff, 0x00, 0x00, 0xff},
		{0x3f, 0x00, 0xc0, 0xff},
		{0x3f, 0x00, 0xc0, 0xff},
		{0x00, 0x00, 0xff, 0xff},
		{0x00, 0x00, 0xff, 0xff},
	}
	if got != want {
		t.Errorf("\ngot  %v\nwant %v", got, want)
	}
}
示例#3
0
// textBox renders t into a tight fitting image
func (ig *ImageGraphics) textBox(t string, font chart.Font) image.Image {
	// Initialize the context.
	fg := image.NewUniform(color.Alpha{0xff})
	bg := image.NewUniform(color.Alpha{0x00})
	width := ig.TextLen(t, font)
	size := ig.relFontsizeToPixel(font.Size)
	canvas := image.NewAlpha(image.Rect(0, 0, width, int(1.5*size+0.5)))
	draw.Draw(canvas, canvas.Bounds(), bg, image.ZP, draw.Src)

	c := freetype.NewContext()
	c.SetDPI(dpi)
	c.SetFont(ig.font)
	c.SetFontSize(size)
	c.SetClip(canvas.Bounds())
	c.SetDst(canvas)
	c.SetSrc(fg)

	// Draw the text.
	h := c.FUnitToPixelRU(ig.font.UnitsPerEm())
	pt := freetype.Pt(0, h)
	extent, err := c.DrawString(t, pt)
	if err != nil {
		log.Println(err)
		return nil
	}
	// log.Printf("text %q, extent: %v", t, extent)
	return canvas.SubImage(image.Rect(0, 0, int((extent.X+127)/256), h*5/4))
}
示例#4
0
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
}
示例#5
0
文件: font.go 项目: Kunde21/engi
func (f *Font) Render(text string) *Texture {
	width, height, yBearing := f.TextDimensions(text)
	font := f.ttf
	size := f.Size

	// Colors
	fg := image.NewUniform(color.NRGBA{f.FG.R, f.FG.G, f.FG.B, f.FG.A})
	bg := image.NewUniform(color.NRGBA{f.BG.R, f.BG.G, f.BG.B, f.BG.A})

	// Create the font context
	c := freetype.NewContext()

	nrgba := image.NewNRGBA(image.Rect(0, 0, width, height))
	draw.Draw(nrgba, nrgba.Bounds(), bg, image.ZP, draw.Src)

	c.SetDPI(dpi)
	c.SetFont(font)
	c.SetFontSize(size)
	c.SetClip(nrgba.Bounds())
	c.SetDst(nrgba)
	c.SetSrc(fg)

	// Draw the text.
	pt := freetype.Pt(0, int(yBearing))
	_, err := c.DrawString(text, pt)
	if err != nil {
		log.Println(err)
		return nil
	}

	// Create texture
	imObj := &ImageObject{nrgba}
	return NewTexture(imObj)

}
示例#6
0
func split(img image.Image) (textImg, bgImg image.Image) {
	size := img.Bounds().Max
	text := image.NewGray(img.Bounds())
	bg := image.NewRGBA(img.Bounds())
	// copy image to background
	draw.Src.Draw(bg, img.Bounds(), img, img.Bounds().Min)
	// make text white
	draw.Src.Draw(text, img.Bounds(), image.NewUniform(color.White), image.Point{})
	for x := 0; x+32 <= size.X; x += 16 {
		for y := 0; y+32 <= size.Y; y += 16 {
			pt := image.Point{X: x, Y: y}
			pt2 := image.Point{X: x + 32, Y: y + 32}
			col, ok := isBitonal(img, pt)
			if ok {
				// fill uniform
				draw.Src.Draw(bg, image.Rectangle{Min: pt, Max: pt2},
					image.NewUniform(col), image.Point{})
				// extract text
				ref := asYCbCr(col)
				for i := 0; i < 32; i++ {
					for j := 0; j < 32; j++ {
						col := asYCbCr(img.At(x+i, y+j))
						val := float64(col.Y) / float64(ref.Y) * 256
						if val >= 256 {
							val = 255
						}
						text.SetGray(x+i, y+j, color.Gray{uint8(val)})
					}
				}
			}
		}
	}
	return text, bg
}
示例#7
0
文件: text.go 项目: farhaven/universe
func (c *Context) Render(txt string, size float64, col color.Color) (*image.RGBA, error) {
	bnd := c.fnt.Bounds(fixed.I(int(size + 0.5)))
	lh := int26_6ToFloat64(bnd.Max.Y) - int26_6ToFloat64(bnd.Min.Y) - 0.5

	c.ft.SetSrc(image.NewUniform(col))
	c.ft.SetFontSize(size)

	/* Render image to temporary buffer to determine final size */
	tmp := nullImage{}
	c.ft.SetDst(tmp)
	c.ft.SetClip(tmp.Bounds())
	p, err := c.ft.DrawString(txt, fixed.P(0, int(lh)))
	if err != nil {
		return nil, err
	}

	dst := image.NewRGBA(image.Rect(0, 0, int(int26_6ToFloat64(p.X)+0.5), int(lh)))
	draw.Draw(dst, dst.Bounds(), image.NewUniform(color.RGBA{}), image.ZP, draw.Src)
	c.ft.SetDst(dst)
	c.ft.SetClip(dst.Bounds())

	p, err = c.ft.DrawString(txt, fixed.P(0, int(size)))
	if err != nil {
		return nil, err
	}

	return dst, nil
}
示例#8
0
文件: text.go 项目: pikkpoiss/twodee
func (ff *FontFace) GetText(text string) (t *Texture, err error) {
	var (
		src       image.Image
		bg        image.Image
		dst       draw.Image
		shortened 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))
	if pt, err = ff.context.DrawString(text, pt); err != nil {
		return
	}
	// if err = WritePNG("hello.png", dst); err != nil {
	// 	return
	// }
	shortened = image.NewRGBA(image.Rect(0, 0, int(pt.X/64), h))
	draw.Draw(shortened, shortened.Bounds(), dst, image.ZP, draw.Src)
	t, err = GetTexture(shortened, gl.NEAREST)
	return
}
示例#9
0
func TestDrawView(t *testing.T) {
	const str = "Hello World!\nHow are you doing?"

	f1 := truetype.NewFace(clearSans, &truetype.Options{DPI: 144})
	f2 := truetype.NewFace(clearSansBoldItalic, &truetype.Options{DPI: 144})

	red := image.NewUniform(color.RGBA{255, 0, 0, 255})
	yellow := image.NewUniform(color.RGBA{255, 255, 0, 255})

	view, _ := Render(
		NewReader(
			strings.NewReader("Hello World!\nHow are you doing?"),
			Style{Offset: 0, Face: f1, Foreground: image.Black, Background: yellow},
			Style{Offset: 10, Face: f2, Foreground: red, Background: yellow},
			Style{Offset: 20, Face: f1, Foreground: image.Black, Background: image.White},
		),
		NewNaturalLayout(fixed.P(0, 0)),
	)

	size := view.Bounds.Max.Sub(view.Bounds.Min)

	for _, a := range []Alignment{Left, Right, Center, Justify} {
		dst := image.NewRGBA(image.Rect(0, 0, int(size.X>>6)+1, int(size.Y>>6)+1))
		view.Align(a)
		view.Draw(dst, LeftToRight)
		saveTest(t, dst, "text.View.Draw_"+a.(fmt.Stringer).String()+".png")
	}
}
示例#10
0
func Example() {
	// As usual in examples, this ignores all errors. Don't do this in your program.

	// setup and find start point for centering
	s := "Hello, World!"
	size := image.Rect(0, 0, 120, 20)
	dst := image.NewRGBA(size)
	c := freetype.NewContext()
	c.SetFont(font)
	c.SetFontSize(14.0)
	c.SetSrc(image.NewUniform(color.Black))
	c.SetDst(dst)
	start, _ := fontutil.CenterX(c, s, size) // CenterX calls c.SetClip(size)

	// perform draw at start.X + y 16
	c.DrawString(s, start.Add(freetype.Pt(0, 16)))

	// write the image out to a file
	// out, _ := os.Create("helloworld.png")
	// defer out.Close()

	// write image to hash for testing purposes
	out := fnv.New64()
	_ = png.Encode(out, dst)
	fmt.Printf("Hash of compressed image: %x", out.Sum64())
	// Output: Hash of compressed image: fa83a1b8d8abf5f2
}
示例#11
0
文件: gui.go 项目: mewmew/playground
// Render renders the grid each time a value is sent on the render channel.
func Render(win wde.Window, g *grid.Grid, render chan bool) {
	screen := win.Screen()
	bounds := screen.Bounds()
	grey := image.NewUniform(color.RGBA{R: 0xF9, G: 0xF9, B: 0xF9}) /// ### todo ### A: 0xFF?
	for {
		<-render

		// Draw grid background.
		draw.Draw(screen, bounds, grey, image.ZP, draw.Src)
		draw.Draw(screen, bounds, imgGrid, image.ZP, draw.Over)

		// Draw grid markers.
		var img image.Image
		for col := 0; col < g.Width(); col++ {
			for row := 0; row < g.Height(); row++ {
				switch g[col][row] {
				case grid.MarkO:
					img = imgO
				case grid.MarkX:
					img = imgX
				default:
					continue
				}
				pt := getCellPt(col, row, g[col][row])
				rect := image.Rect(0, 0, img.Bounds().Dx(), img.Bounds().Dy())
				draw.Draw(screen, rect.Add(pt), img, image.ZP, draw.Over)
			}
		}

		// Flush screen updates.
		win.FlushImage(bounds)
	}
}
func main() {
	const order = 8
	const width = 1 << order
	const margin = 10
	bounds := image.Rect(-margin, -margin, width+2*margin, width+2*margin)
	im := image.NewGray(bounds)
	gBlack := color.Gray{0}
	gWhite := color.Gray{255}
	draw.Draw(im, bounds, image.NewUniform(gWhite), image.ZP, draw.Src)

	for y := 0; y < width; y++ {
		for x := 0; x < width; x++ {
			if x&y == 0 {
				im.SetGray(x, y, gBlack)
			}
		}
	}
	f, err := os.Create("sierpinski.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)
	}
}
func main() {
	scale := width / (rMax - rMin)
	height := int(scale * (iMax - iMin))
	bounds := image.Rect(0, 0, width, height)
	b := image.NewNRGBA(bounds)
	draw.Draw(b, bounds, image.NewUniform(color.Black), image.ZP, draw.Src)
	for x := 0; x < width; x++ {
		for y := 0; y < height; y++ {
			fEsc := mandelbrot(complex(
				float64(x)/scale+rMin,
				float64(y)/scale+iMin))
			b.Set(x, y, color.NRGBA{uint8(red * fEsc),
				uint8(green * fEsc), uint8(blue * fEsc), 255})

		}
	}
	f, err := os.Create("mandelbrot.png")
	if err != nil {
		fmt.Println(err)
		return
	}
	if err = png.Encode(f, b); err != nil {
		fmt.Println(err)
	}
	if err = f.Close(); err != nil {
		fmt.Println(err)
	}
}
示例#14
0
func createSwatch(col color.Color, size int) image.Image {
	log.Printf("Creating swatch for %v\n", col)
	bounds := image.Rect(0, 0, size, size)
	img := image.NewNRGBA(bounds)
	draw.Draw(img, img.Bounds(), image.NewUniform(col), img.Bounds().Min, draw.Src)
	return img
}
示例#15
0
func draw_image(filename string, plot_map map[Key]Point, width int, height int, gradient string) {
	build_gradient(gradient)
	fill_palette()

	bounds := image.Rect(0, 0, width, height)

	b := image.NewNRGBA(bounds)
	draw.Draw(b, bounds, image.NewUniform(color.Black), image.ZP, draw.Src)

	for x := 0; x < width; x += 1 {
		for y := 0; y < height; y += 1 {
			var p = plot_map[Key{x, y}]
			b.Set(p.X, p.Y, get_colour(p.Escape))
		}
	}
	file, err := os.Create(filename)
	if err != nil {
		fmt.Println(err)
	}

	if err = jpeg.Encode(file, b, &jpeg.Options{jpeg.DefaultQuality}); err != nil {
		fmt.Println(err)
	}

	if err = file.Close(); err != nil {
		fmt.Println(err)
	}
}
示例#16
0
func (w *Window) DrawFrame(frame int) {
	var f Frame
	var err os.Error

	if frame >= w.FrameCount {
		return
	}

	f = w.Frames[frame]

	if f.img == nil {
		f.img, err = getImage(f.Path)
		if err != nil {
			return
		}
	}
	draw.Draw(w.Screen, f.img.Bounds(), f.img, image.Point{0, 0}, draw.Over)

	if w.State == WORKING {
		draw.Draw(w.Screen, image.Rectangle{image.Point{0, 0}, image.Point{20, 20}},
			image.NewUniform(color.RGBA{0, 255, 0, 255}), image.Point{0, 0}, draw.Src)
	}

	DrawCircle(w.Screen, f.Centre.Centre, f.Centre.Radius, color.RGBA{0, 255, 255, 255})
	w.Window.FlushImage()
}
示例#17
0
文件: text.go 项目: farhaven/universe
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
}
示例#18
0
文件: collage.go 项目: loov/sketchure
func main() {
	flag.Parse()

	dir := flag.Arg(0)
	if dir == "" {
		fmt.Fprintf(os.Stderr, "Images not specified.")
		flag.Usage()
		os.Exit(1)
	}

	files, err := ioutil.ReadDir(dir)
	check(err)

	w, h := *width, *height
	collage := image.NewRGBA(image.Rect(0, 0, w*2, h*len(files)))
	draw.Draw(collage, collage.Bounds(), image.NewUniform(color.RGBA{0xFF, 0xFF, 0xFF, 0xFF}), image.Point{}, draw.Src)

	for i, file := range files {
		fmt.Println("Processing ", file.Name())
		m, err := ycbcr.FromFile(filepath.Join(dir, file.Name()))
		check(err)

		drawcell(collage, m, 0, i, w, h)
		process(m)
		drawcell(collage, m, 1, i, w, h)
	}

	check(ycbcr.ToFile(*output, collage))
}
示例#19
0
func drawBarcode(img *image.Gray, unitSize, xPos, yPos float64, num int) {
	drawSO(img, unitSize, xPos, yPos)
	x := xPos + unitSize*4.0

	// Draw reference bar
	x += drawBar(img, unitSize, x, yPos, 0) + unitSize

	// Print the form number below the barcode
	ftContext.SetFontSize(7)
	ftContext.SetSrc(image.NewUniform(color.Black))
	ftContext.DrawString(fmt.Sprintf("%d", num), freetype.Pt(int(x), int(yPos+unitSize*7)))

	// Draw bars
	ds := toBase4(num, 9)
	for i := len(ds) - 1; i >= 0; i-- {
		x += drawBar(img, unitSize, x, yPos, ds[i]) + unitSize
	}

	// Draw checksum bars
	ckBase10 := csumGen(num)
	fmt.Println("ckBase10 = ", ckBase10)
	ck := toBase4(ckBase10, 2)
	fmt.Println("ck = ", ck)
	for i := len(ck) - 1; i >= 0; i-- {
		x += drawBar(img, unitSize, x, yPos, ck[i]) + unitSize
	}

}
示例#20
0
func main() {
	fmt.Println("Criando arquivo")
	file, err := os.Create("imagem1.png")

	if err != nil {
		fmt.Println("Impossivel abrir ou criar arquivo")
		fmt.Println(err)
		return
	}

	defer file.Close()

	fmt.Println("Sem erros")

	imagem := image.NewRGBA(image.Rect(0, 0, 513, 257))

	draw.Draw(imagem, imagem.Bounds(), image.NewUniform(color.RGBA{100, 150, 255, 255}), image.ZP, draw.Src)

	create(imagem)

	err = png.Encode(file, imagem)

	if err != nil {
		fmt.Println(err)
	}

}
示例#21
0
文件: text.go 项目: velovix/paunch
// NewText creates a new Text object. The x and y positions represent the left
// and bottom of text without tails.
func NewText(x, y float64, font *Font, fontSize float64, message string) (*Text, error) {

	text := &Text{}

	text.x = x
	text.y = y - fontSize

	text.fontColor = image.NewUniform(color.RGBA{0, 0, 0, 255})

	text.context = freetype.NewContext()
	text.context.SetDPI(dpi)
	text.context.SetFont(font.font)
	text.context.SetFontSize(fontSize)
	text.context.SetSrc(text.fontColor)
	text.context.SetHinting(freetype.FullHinting)

	text.fontSize = fontSize

	err := text.SetMessage(message)
	if err != nil {
		return text, err
	}

	return text, nil
}
示例#22
0
func DrawString(dst draw.Image, s string, color color.Color, size, x, y int) int {
	c := image.NewUniform(color)
	for _, r := range s {
		x = drawRune(dst, r, c, size, x, y)
	}
	return x
}
示例#23
0
func (gc *ImageGraphicContext) FillString(text string) (cursor float64) {
	gc.freetype.SetSrc(image.NewUniform(gc.Current.StrokeColor))
	// Draw the text.
	x, y := gc.Current.Path.LastPoint()
	gc.Current.Tr.Transform(&x, &y)
	x0, fontSize := 0.0, gc.Current.FontSize
	gc.Current.Tr.VectorTransform(&x0, &fontSize)
	font := GetFont(gc.Current.FontData)
	if font == nil {
		font = GetFont(defaultFontData)
	}
	if font == nil {
		return 0
	}
	gc.freetype.SetFont(font)
	gc.freetype.SetFontSize(fontSize)
	pt := freetype.Pt(int(x), int(y))
	p, err := gc.freetype.DrawString(text, pt)
	if err != nil {
		log.Println(err)
	}
	x1, _ := gc.Current.Path.LastPoint()
	x2, y2 := float64(p.X)/256, float64(p.Y)/256
	gc.Current.Tr.InverseTransform(&x2, &y2)
	width := x2 - x1
	return width
}
示例#24
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)
	}
}
示例#25
0
文件: glyph.go 项目: runningwild/glop
// Render draws rune r front the specified font at the specified dpi and scale.  It returns a
// grayscale image that is just large enough to contain the rune.
func Render(font *truetype.Font, r rune, dpi, scale float64) (*image.Gray, error) {
	glyph := truetype.NewGlyphBuf()
	index := font.Index(r)
	glyph.Load(font, font.FUnitsPerEm(), index, truetype.FullHinting)
	ctx := freetype.NewContext()
	boxer := makeBoundingBoxer()
	ctx.SetSrc(image.NewUniform(color.White))
	ctx.SetDst(boxer)
	ctx.SetClip(boxer.largeBounds)
	ctx.SetFontSize(250)
	ctx.SetDPI(dpi)
	ctx.SetFont(font)
	if err := glyph.Load(font, font.FUnitsPerEm(), font.Index(r), truetype.FullHinting); err != nil {
		return nil, fmt.Errorf("Unable to load glyph: %v\n", err)
	}
	var rp raster.Point
	rp.X = ctx.PointToFix32(0)
	rp.Y = ctx.PointToFix32(100)
	ctx.DrawString(string(r), rp)
	boxer.complete()

	g := gift.New(
		gift.Resize(int(float64(boxer.Bounds().Dx())*scale+0.5), int(float64(boxer.Bounds().Dy())*scale+0.5), gift.CubicResampling),
	)
	dst := image.NewGray(g.Bounds(boxer.Bounds()))
	g.Draw(dst, boxer)
	return dst, nil
}
示例#26
0
// Pad reads a photosphere image and writes a padded 360 degree x 180 degree image to a given writer.
func Pad(w io.Writer, ir io.Reader) (pano *PanoOpts, err error) {
	d, err := ioutil.ReadAll(ir)
	if err != nil {
		return nil, err
	}

	r := bufio.NewReader(bytes.NewReader(d))
	for {
		s, err := NextSection(r, APP1)
		if err != nil {
			return nil, err
		}
		if s == nil {
			break
		}

		if IsXMP(s) {
			meta := new(XMPMeta)
			err := xml.Unmarshal(ExtractXMP(s), meta)
			if err != nil {
				// Not XMP?
				continue
			}
			if meta.PanoOpts.NS != "http://ns.google.com/photos/1.0/panorama/" {
				// Different XMP
				continue
			}
			pano = meta.PanoOpts
			break
		}
	}

	if pano == nil {
		return nil, errors.New("image provided had no photo sphere metadata")
	}

	src, err := jpeg.Decode(bytes.NewReader(d))
	if err != nil {
		return nil, err
	}
	srcB := src.Bounds()

	// Sometimes the height in the metadata doesn't match the actual height.
	// If that's the case, scale all of the other parameters.
	if srcB.Dy() != pano.Height {
		scale := float64(srcB.Dy()) / float64(pano.Height)
		pano.TotalHeight = int(float64(pano.TotalHeight) * scale)
		pano.TotalWidth = int(float64(pano.TotalWidth) * scale)
		pano.Top = int(float64(pano.Top) * scale)
		pano.Left = int(float64(pano.Left) * scale)
	}

	dst := image.NewRGBA(image.Rect(0, 0, pano.TotalWidth, pano.TotalHeight))
	draw.Draw(dst, dst.Bounds(), image.NewUniform(color.Black), image.ZP, draw.Src)
	compositeBounds := image.Rect(pano.Left, pano.Top, pano.Left+srcB.Dx(), pano.Top+srcB.Dy())
	draw.Draw(dst, compositeBounds, src, image.ZP, draw.Src)

	err = jpeg.Encode(w, dst, nil)
	return pano, err
}
func hough(im image.Image, ntx, mry int) draw.Image {
	nimx := im.Bounds().Max.X
	mimy := im.Bounds().Max.Y
	mry = int(mry/2) * 2
	him := image.NewGray(image.Rect(0, 0, ntx, mry))
	draw.Draw(him, him.Bounds(), image.NewUniform(color.White),
		image.ZP, draw.Src)

	rmax := math.Hypot(float64(nimx), float64(mimy))
	dr := rmax / float64(mry/2)
	dth := math.Pi / float64(ntx)

	for jx := 0; jx < nimx; jx++ {
		for iy := 0; iy < mimy; iy++ {
			col := color.GrayModel.Convert(im.At(jx, iy)).(color.Gray)
			if col.Y == 255 {
				continue
			}
			for jtx := 0; jtx < ntx; jtx++ {
				th := dth * float64(jtx)
				r := float64(jx)*math.Cos(th) + float64(iy)*math.Sin(th)
				iry := mry/2 - int(math.Floor(r/dr+.5))
				col = him.At(jtx, iry).(color.Gray)
				if col.Y > 0 {
					col.Y--
					him.SetGray(jtx, iry, col)
				}
			}
		}
	}
	return him
}
示例#28
0
// Color in HEX format: FAFAFA
func (g *Gummy) DrawText(text, textColor string, fontSize, xPosition, yPosition int) error {

	// Get black or white depending on the background
	if textColor == "" {
		c := (*g.Color).(color.RGBA)
		if blackWithBackground(float64(c.R), float64(c.G), float64(c.B)) {
			textColor = "000000"
		} else {
			textColor = "FFFFFF"
		}
	}

	fc := freetype.NewContext()
	fc.SetDst(g.Img)
	fc.SetFont(g.Font)
	fc.SetClip(g.Img.Bounds())

	// Color parsing
	cr, _ := strconv.ParseUint(string(textColor[:2]), 16, 64)
	cg, _ := strconv.ParseUint(string(textColor[2:4]), 16, 64)
	cb, _ := strconv.ParseUint(string(textColor[4:]), 16, 64)
	c := image.NewUniform(color.RGBA{R: uint8(cr), G: uint8(cg), B: uint8(cb), A: 255})

	fc.SetSrc(c)
	fc.SetFontSize(float64(fontSize))

	_, err := fc.DrawString(text, freetype.Pt(xPosition, yPosition))

	return err
}
示例#29
0
func main() {
	file, err := os.Create("circle.png")

	if err != nil {
		fmt.Println(err)
	}

	defer file.Close()

	imagem := image.NewRGBA(image.Rect(-lado, -lado, lado, lado))

	draw.Draw(imagem, imagem.Bounds(), image.NewUniform(color.RGBA{40, 125, 80, 255}), image.ZP, draw.Src)

	for raio := 65; raio > 0; raio-- {
		Circle(imagem, cor, float64(raio))
		cor.R += 6
		cor.G += 6
		cor.B += 6
		//fmt.Println(cor)
	}

	err = png.Encode(file, imagem)

	if err != nil {
		fmt.Println(err)
	}
}
示例#30
-1
文件: font.go 项目: anthonyrego/gosmf
func (font *Font) updateTexture(texture uint32, text string, width int, height int, size float64, dpi float64, rgba color.Color) (int, int) {
	context := freetype.NewContext()
	context.SetFont(font.ttf)

	img := image.NewRGBA(image.Rect(0, 0, width, height))
	r, g, b, _ := rgba.RGBA()
	draw.Draw(img, img.Bounds(), image.NewUniform(color.RGBA{uint8(r), uint8(g), uint8(b), 0}), image.ZP, draw.Src)

	context.SetDst(img)
	context.SetClip(img.Bounds())
	context.SetSrc(image.NewUniform(rgba))
	context.SetFontSize(size)
	context.SetDPI(dpi)
	pixelBounds, _ := context.DrawString(text, freetype.Pt(0, height/2))

	gl.ActiveTexture(gl.TEXTURE0)
	gl.BindTexture(gl.TEXTURE_2D, texture)

	gl.TexSubImage2D(
		gl.TEXTURE_2D,
		0,
		0,
		0,
		int32(img.Rect.Size().X),
		int32(img.Rect.Size().Y),
		gl.RGBA,
		gl.UNSIGNED_BYTE,
		gl.Ptr(img.Pix))

	return int26_6Ceiling(pixelBounds.X + 0x3f), int26_6Ceiling(pixelBounds.Y + 0x3f)
}