本文整理匯總了Golang中image/draw.DrawMask函數的典型用法代碼示例。如果您正苦於以下問題:Golang DrawMask函數的具體用法?Golang DrawMask怎麽用?Golang DrawMask使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了DrawMask函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestFill
func TestFill(t *testing.T) {
rr := []image.Rectangle{
image.Rect(0, 0, 0, 0),
image.Rect(0, 0, 40, 30),
image.Rect(10, 0, 40, 30),
image.Rect(0, 20, 40, 30),
image.Rect(10, 20, 40, 30),
image.Rect(10, 20, 15, 25),
image.Rect(10, 0, 35, 30),
image.Rect(0, 15, 40, 16),
image.Rect(24, 24, 25, 25),
image.Rect(23, 23, 26, 26),
image.Rect(22, 22, 27, 27),
image.Rect(21, 21, 28, 28),
image.Rect(20, 20, 29, 29),
}
for _, r := range rr {
m := image.NewRGBA(image.Rect(0, 0, 40, 30)).SubImage(r).(*image.RGBA)
b := m.Bounds()
c := color.RGBA{11, 0, 0, 255}
src := &image.Uniform{C: c}
check := func(desc string) {
for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; x < b.Max.X; x++ {
if !eq(c, m.At(x, y)) {
t.Errorf("%s fill: at (%d, %d), sub-image bounds=%v: want %v got %v", desc, x, y, r, c, m.At(x, y))
return
}
}
}
}
// Draw 1 pixel at a time.
for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; x < b.Max.X; x++ {
draw.DrawMask(m, image.Rect(x, y, x+1, y+1), src, image.ZP, nil, image.ZP, draw.Src)
}
}
check("pixel")
// Draw 1 row at a time.
c = color.RGBA{0, 22, 0, 255}
src = &image.Uniform{C: c}
for y := b.Min.Y; y < b.Max.Y; y++ {
draw.DrawMask(m, image.Rect(b.Min.X, y, b.Max.X, y+1), src, image.ZP, nil, image.ZP, draw.Src)
}
check("row")
// Draw 1 column at a time.
c = color.RGBA{0, 0, 33, 255}
src = &image.Uniform{C: c}
for x := b.Min.X; x < b.Max.X; x++ {
draw.DrawMask(m, image.Rect(x, b.Min.Y, x+1, b.Max.Y), src, image.ZP, nil, image.ZP, draw.Src)
}
check("column")
// Draw the whole image at once.
c = color.RGBA{44, 55, 66, 77}
src = &image.Uniform{C: c}
draw.DrawMask(m, b, src, image.ZP, nil, image.ZP, draw.Src)
check("whole")
}
}
示例2: main
func main() {
fd, _ := os.OpenFile("log.txt", os.O_RDWR|os.O_CREATE, 0666)
defer fd.Close()
log.SetOutput(fd)
// Draw a rounded corner that is one pixel wide.
r := ft.NewRast(50, 50)
r.Start(p(5, 5))
r.Add1(p(5, 25))
r.Add2(p(5, 45), p(25, 45))
r.Add1(p(45, 45))
r.Add1(p(45, 44))
r.Add1(p(26, 44))
r.Add2(p(6, 44), p(6, 24))
r.Add1(p(6, 5))
r.Add1(p(5, 5))
// Rasterize that curve multiple times at different gammas.
const (
w = 600
h = 200
)
rgba := image.NewRGBA(image.Rect(0, 0, w, h))
draw.Draw(rgba, image.Rect(0, 0, w, h/2), image.Black, image.ZP, draw.Src)
draw.Draw(rgba, image.Rect(0, h/2, w, h), image.White, image.ZP, draw.Src)
mask := image.NewAlpha(image.Rect(0, 0, 50, 50))
drawer := ft.NewAlphaSrcDrawer(mask)
gammas := []float64{1.0 / 10.0, 1.0 / 3.0, 1.0 / 2.0, 2.0 / 3.0, 4.0 / 5.0, 1.0, 5.0 / 4.0, 3.0 / 2.0, 2.0, 3.0, 10.0}
for i, g := range gammas {
draw.Draw(mask, mask.Bounds(), image.Transparent, image.ZP, draw.Src)
r.Rast(ft.NewGammaCorrectionDrawer(drawer, g))
x, y := 50*i+25, 25
draw.DrawMask(rgba, image.Rect(x, y, x+50, y+50), image.White, image.ZP, mask, image.ZP, draw.Over)
y += 100
draw.DrawMask(rgba, image.Rect(x, y, x+50, y+50), image.Black, image.ZP, mask, image.ZP, draw.Over)
}
// Save that RGBA image to disk.
f, err := os.Create("out.png")
if err != nil {
log.Println(err)
os.Exit(1)
}
defer f.Close()
b := bufio.NewWriter(f)
err = png.Encode(b, rgba)
if err != nil {
log.Println(err)
os.Exit(1)
}
err = b.Flush()
if err != nil {
log.Println(err)
os.Exit(1)
}
fmt.Println("Wrote out.png OK.")
}
示例3: BuildImage
// BuildImage builds a new image with 4 square images and overlays
// numbers on top (translucent). It sends to S3 and returns the public
// URL.
func (c *Challenge) BuildImage(profileURLs []string) ([]byte, error) {
var profileImgs []image.Image
for _, url := range profileURLs {
resp, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("Couldn't get avatar: %s", err)
}
defer resp.Body.Close()
cnt, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("couldn't read avatar body: %s", err)
}
img, _, err := image.Decode(bytes.NewBuffer(cnt))
if err != nil {
return nil, fmt.Errorf("decoding image: %s", err)
}
profileImgs = append(profileImgs, img)
}
numbersImage, _, _ := image.Decode(bytes.NewBuffer(numbersPNG))
// for 4 images
finalImage := image.NewRGBA(image.Rect(0, 0, 192+192, 192+192))
mask := &image.Uniform{color.RGBA{0, 0, 100, 100}}
// First
draw.Draw(finalImage, image.Rect(0, 0, 192, 192), profileImgs[0], image.Point{0, 0}, draw.Over)
draw.DrawMask(finalImage, image.Rect(0, 0, 64, 64), numbersImage, numbersPositions[1].Min, mask, image.Point{0, 0}, draw.Over)
// Second
draw.Draw(finalImage, image.Rect(192, 0, 192*2, 192), profileImgs[1], image.Point{0, 0}, draw.Over)
draw.DrawMask(finalImage, image.Rect(192, 0, 192+64, 64), numbersImage, numbersPositions[2].Min, mask, image.Point{0, 0}, draw.Over)
// Third
draw.Draw(finalImage, image.Rect(0, 192, 192, 192*2), profileImgs[2], image.Point{0, 0}, draw.Over)
draw.DrawMask(finalImage, image.Rect(0, 192, 64, 192+64), numbersImage, numbersPositions[3].Min, mask, image.Point{0, 0}, draw.Over)
// Fourth
draw.Draw(finalImage, image.Rect(192, 192, 192*2, 192*2), profileImgs[3], image.Point{0, 0}, draw.Over)
draw.DrawMask(finalImage, image.Rect(192, 192, 192+64, 192+64), numbersImage, numbersPositions[4].Min, mask, image.Point{0, 0}, draw.Over)
buf := &bytes.Buffer{}
png.Encode(buf, finalImage)
return buf.Bytes(), nil
//return "https://avatars.slack-edge.com/2014-12-08/3167931031_42ef453717f47b15aa3b_192.jpg", nil
}
示例4: OnElite
func (ec *myEliteConsumer) OnElite(g *ga.IGenome) {
bits := (*g).GetBits()
newImage := createImageFromBitset(bits)
// Output elite
outputImageFile, _ := os.Create("elite.png")
png.Encode(outputImageFile, newImage)
outputImageFile.Close()
// Output elite with input image blended over the top
outputImageFileAlphaBlended, _ := os.Create("elite_with_original.png")
draw.DrawMask(newImage, newImage.Bounds(),
inputImage, image.ZP,
&image.Uniform{color.RGBA{0, 0, 0, 255 / 4}}, image.ZP,
draw.Over)
png.Encode(outputImageFileAlphaBlended, newImage)
outputImageFileAlphaBlended.Close()
ec.currentIter++
fitness := (*g).GetFitness()
fmt.Println(ec.currentIter, "\t", fitness, "\t", fitness-ec.previousFitness)
ec.previousFitness = fitness
time.Sleep(10 * time.Millisecond)
}
示例5: DrawString
// DrawString draws s at p and returns p advanced by the text extent. The text
// is placed so that the left edge of the em square of the first character of s
// and the baseline intersect at p. The majority of the affected pixels will be
// above and to the right of the point, but some may be below or to the left.
// For example, drawing a string that starts with a 'J' in an italic font may
// affect pixels below and left of the point.
//
// p is a fixed.Point26_6 and can therefore represent sub-pixel positions.
func (c *Context) DrawString(s string, p fixed.Point26_6) (fixed.Point26_6, error) {
if c.f == nil {
return fixed.Point26_6{}, errors.New("freetype: DrawText called with a nil font")
}
prev, hasPrev := truetype.Index(0), false
for _, rune := range s {
index := c.f.Index(rune)
if hasPrev {
kern := c.f.Kern(c.scale, prev, index)
if c.hinting != font.HintingNone {
kern = (kern + 32) &^ 63
}
p.X += kern
}
advanceWidth, mask, offset, err := c.glyph(index, p)
if err != nil {
return fixed.Point26_6{}, err
}
p.X += advanceWidth
glyphRect := mask.Bounds().Add(offset)
dr := c.clip.Intersect(glyphRect)
if !dr.Empty() {
mp := image.Point{0, dr.Min.Y - glyphRect.Min.Y}
draw.DrawMask(c.dst, dr, c.src, image.ZP, mask, mp, draw.Over)
}
prev, hasPrev = index, true
}
return p, nil
}
示例6: DrawString
// DrawString draws s at p and returns p advanced by the text extent. The text
// is placed so that the left edge of the em square of the first character of s
// and the baseline intersect at p. The majority of the affected pixels will be
// above and to the right of the point, but some may be below or to the left.
// For example, drawing a string that starts with a 'J' in an italic font may
// affect pixels below and left of the point.
// p is a raster.Point and can therefore represent sub-pixel positions.
func (c *Context) DrawString(s string, p raster.Point) (raster.Point, error) {
if c.font == nil {
return raster.Point{}, errors.New("freetype: DrawText called with a nil font")
}
prev, hasPrev := truetype.Index(0), false
for _, rune := range s {
index := c.font.Index(rune)
if hasPrev {
p.X += c.FUnitToFix32(int(c.font.Kerning(prev, index)))
}
mask, offset, err := c.glyph(index, p)
if err != nil {
return raster.Point{}, err
}
p.X += c.FUnitToFix32(int(c.font.HMetric(index).AdvanceWidth))
glyphRect := mask.Bounds().Add(offset)
dr := c.clip.Intersect(glyphRect)
if !dr.Empty() {
mp := image.Point{0, dr.Min.Y - glyphRect.Min.Y}
draw.DrawMask(c.dst, dr, c.src, image.ZP, mask, mp, draw.Over)
}
prev, hasPrev = index, true
}
return p, nil
}
示例7: TestDrawOverlap
func TestDrawOverlap(t *testing.T) {
for _, op := range []draw.Op{draw.Over, draw.Src} {
for yoff := -2; yoff <= 2; yoff++ {
loop:
for xoff := -2; xoff <= 2; xoff++ {
m := gradYellow(127).(*image.RGBA)
dst := m.SubImage(image.Rect(5, 5, 10, 10)).(*image.RGBA)
src := m.SubImage(image.Rect(5+xoff, 5+yoff, 10+xoff, 10+yoff)).(*image.RGBA)
b := dst.Bounds()
// Draw the (src, mask, op) onto a copy of dst using a slow but obviously correct implementation.
golden := makeGolden(dst, b, src, src.Bounds().Min, nil, image.ZP, op)
if !b.Eq(golden.Bounds()) {
t.Errorf("drawOverlap xoff=%d,yoff=%d: bounds %v versus %v", xoff, yoff, dst.Bounds(), golden.Bounds())
continue
}
// Draw the same combination onto the actual dst using the optimized DrawMask implementation.
draw.DrawMask(dst, b, src, src.Bounds().Min, nil, image.ZP, op)
// Check that the resultant dst image matches the golden output.
for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; x < b.Max.X; x++ {
if !eq(dst.At(x, y), golden.At(x, y)) {
t.Errorf("drawOverlap xoff=%d,yoff=%d: at (%d, %d), %v versus golden %v", xoff, yoff, x, y, dst.At(x, y), golden.At(x, y))
continue loop
}
}
}
}
}
}
}
示例8: DrawString
// DrawString draws s at p and returns p advanced by the text extent. The text
// is placed so that the left edge of the em square of the first character of s
// and the baseline intersect at p. The majority of the affected pixels will be
// above and to the right of the point, but some may be below or to the left.
// For example, drawing a string that starts with a 'J' in an italic font may
// affect pixels below and left of the point.
// p is a raster.Point and can therefore represent sub-pixel positions.
func (c *Context) DrawString(s string, p raster.Point) (raster.Point, error) {
if c.font == nil {
return raster.Point{}, errors.New("freetype: DrawText called with a nil font")
}
prev, hasPrev := truetype.Index(0), false
for _, rune := range s {
index := c.font.Index(rune)
if hasPrev {
kern := raster.Fix32(c.font.Kerning(c.scale, prev, index)) << 2
if c.hinting != NoHinting {
kern = (kern + 128) &^ 255
}
p.X += kern
}
advanceWidth, mask, offset, err := c.glyph(index, p)
if err != nil {
return raster.Point{}, err
}
p.X += advanceWidth
glyphRect := mask.Bounds().Add(offset)
dr := c.clip.Intersect(glyphRect)
if !dr.Empty() {
mp := image.Point{0, dr.Min.Y - glyphRect.Min.Y}
draw.DrawMask(c.dst, dr, c.src, image.ZP, mask, mp, draw.Over)
}
prev, hasPrev = index, true
}
return p, nil
}
示例9: main
func main() {
m := image.NewRGBA(image.Rect(0, 0, 500, 500))
blue := color.RGBA{59, 148, 217, 255}
white := color.RGBA{255, 255, 255, 255}
black := color.RGBA{0, 0, 0, 255}
draw.Draw(m, m.Bounds(), &image.Uniform{blue}, image.ZP, draw.Src)
filename := "egg_mask.png"
f, err := os.Open(filename)
if err != nil {
panic(err)
}
defer f.Close()
src, err := png.Decode(f)
if err != nil {
panic(err)
}
mask := image.NewRGBA(image.Rect(0, 0, 500, 500))
draw.Draw(mask, mask.Bounds(), &image.Uniform{white}, image.ZP, draw.Src)
draw.DrawMask(m, m.Bounds(), src, image.ZP, mask, image.ZP, draw.Over)
for i := m.Bounds().Min.X; i < m.Bounds().Max.X; i++ {
m.Set(i, m.Bounds().Max.Y/2, black) // to change a single pixel
}
w, _ := os.Create("defaultegg.png")
defer w.Close()
png.Encode(w, m)
}
示例10: Draw
func Draw(r io.RuneReader, f font.Face, dst draw.Image, src image.Image, bounds fixed.Rectangle26_6, dir Direction) (err error) {
var prev rune
var char rune
var origin = dir.Origin(f, bounds)
var dot = origin
var next fixed.Point26_6
for {
if char, _, err = r.ReadRune(); err != nil {
if err == io.EOF {
err = nil
}
return
}
dot, next = dir.Advance(f, origin, dot, prev, char)
if char != '\n' {
dr, mask, maskp := glyph(f, char, dot)
draw.DrawMask(dst, dr, src, dr.Min, mask, maskp, draw.Over)
}
dot, prev = next, char
}
}
示例11: DrawString
func (c *Context) DrawString(str string, pt RastPoint) (RastPoint, error) {
if c.font == nil {
return RastPoint{}, errors.New("freetype DrawString called with nil font.")
}
prev, has_prev := uint16(0), false
for _, rune := range str {
idx := c.font.Index(rune)
if has_prev {
pt.X += Fix32(c.font.Kerning(c.scale, prev, idx)) << 2
}
mask, offset, err := c.glyph_at(idx, pt)
if err != nil {
return RastPoint{}, err
}
pt.X += Fix32(c.font.HMetric(c.scale, idx).AdvanceWidth) << 2
glyph_rect := mask.Bounds().Add(offset)
dr := c.clip.Intersect(glyph_rect)
if !dr.Empty() {
mp := image.Point{0, dr.Min.Y - glyph_rect.Min.Y}
draw.DrawMask(c.dst, dr, c.src, image.ZP, mask, mp, draw.Over)
}
prev, has_prev = idx, true
}
return pt, nil
}
示例12: watermark
func (h *proxyHandler) watermark(m image.Image) (image.Image, error) {
if h.watermarkImage == nil {
return m, nil
}
mRect := m.Bounds()
if mRect.Dx()*mRect.Dy() <= 90000 {
return m, nil
}
wWidth := int(float32(mRect.Dx()) * 0.05)
if wWidth > h.watermarkImage.Bounds().Dx() {
wWidth = h.watermarkImage.Bounds().Dx()
}
scaledWatermark := resize.Resize(uint(wWidth), 0, h.watermarkImage, resize.MitchellNetravali)
b := m.Bounds()
r := image.NewRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
draw.Draw(r, r.Bounds(), m, b.Min, draw.Src)
margin := int(float32(mRect.Dx()) * 0.03)
location := image.Rect(r.Bounds().Dx()-margin-scaledWatermark.Bounds().Dx(), r.Bounds().Dy()-margin-scaledWatermark.Bounds().Dy(), r.Bounds().Dx()-margin, r.Bounds().Dy()-margin)
draw.DrawMask(r, location, scaledWatermark, image.ZP, nil, image.ZP, draw.Over)
return r, nil
}
示例13: makePngShield
func makePngShield(w io.Writer, d Data) {
// render text to determine how wide the image has to be
// we leave 6 pixels at the start and end, and 3 for each in the middle
v, vw := renderString(d.Vendor, c)
s, sw := renderString(d.Status, c)
imageWidth := op + vw + ip*2 + sw + op
img := image.NewRGBA(image.Rect(0, 0, imageWidth, h))
draw.Draw(img, img.Bounds(), &image.Uniform{C: d.Color}, image.ZP, draw.Src)
rect := image.Rect(0, 0, op+vw+ip, h)
draw.Draw(img, rect, &image.Uniform{C: Grey}, image.ZP, draw.Src)
dst := image.NewRGBA(image.Rect(0, 0, imageWidth, h))
mask := image.NewRGBA(image.Rect(0, 0, imageWidth, h))
buildMask(mask, imageWidth, edge, draw.Src)
draw.DrawMask(dst, dst.Bounds(), img, image.ZP, mask, image.ZP, draw.Over)
buildMask(dst, imageWidth, gradient, draw.Over)
draw.Draw(dst, dst.Bounds(), v, image.Point{-op, 0}, draw.Over)
draw.Draw(dst, dst.Bounds(), s, image.Point{-(op + vw + ip*2), 0}, draw.Over)
png.Encode(w, dst)
}
示例14: drawRune
func drawRune(dst draw.Image, r rune, color *image.Uniform, size, x, y int) int {
if r == ' ' {
return x + [3]int{24, 12, 8}[size]
}
rect := glyphRect[r][size]
draw.DrawMask(dst, rect.Sub(rect.Min).Add(image.Pt(x, y)), color, image.ZP, text, rect.Min, draw.Over)
return x + rect.Dx()
}
示例15: RenderCPU
// RenderCPU implements the Renderer interface.
func (b *Border) RenderCPU(img draw.Image, cpu CPU) {
rect := img.Bounds()
interior := geometry.Contract(rect, b.Size)
mask := MaskInside(interior)
draw.DrawMask(img, rect, image.NewUniform(b.Color), image.ZP, mask, rect.Min, draw.Over)
sub := SubImage(img, interior)
b.Renderer.RenderCPU(sub, cpu)
}