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") } }
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.") }
// 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 }
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) }
// 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 }
// 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 }
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 } } } } } } }
// 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 }
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) }
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 } }
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 }
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 }
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) }
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() }
// 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) }
func (obj *ImageItem) Draw(dst draw.Image, clip image.Rectangle) { dr := obj.R.Intersect(clip) sp := dr.Min.Sub(obj.R.Min) op := draw.Over if obj.IsOpaque { op = draw.Src } draw.DrawMask(dst, dr, obj.Image, sp, nil, image.ZP, op) }
func GetPieceImage(src, mask image.Image, copyPoint image.Point) (draw.Image, error) { maskBounds := mask.Bounds() // Create a new image with mask bounds for final move block copy := copySrc(mask) // Get the part image in src image with the mask-bounds draw.DrawMask(copy, maskBounds, src, copyPoint, mask, maskBounds.Min, draw.Over) return copy, nil }
func pngHandler(w http.ResponseWriter, r *http.Request) { m := image.NewRGBA(image.Rect(0, 0, 128, 128)) p := image.Point{64, 64} radius := 64 draw.DrawMask(m, m.Bounds(), &image.Uniform{getRandomColor()}, image.ZP, &circle{p, radius}, image.ZP, draw.Over) var img image.Image = m writeImage(w, &img) }
// Box creates a rectangular image of the given size, filled with the given colour, // with a border-size border of colour borderCol. // func Box(width, height int, col image.Image, border int, borderCol image.Image) image.Image { img := image.NewRGBA(image.Rect(0, 0, width, height)) if border < 0 { border = 0 } r := image.Rect(0, 0, width, height) draw.DrawMask(img, r.Inset(border), col, image.ZP, nil, image.ZP, draw.Src) BorderOp(img, r, border, borderCol, image.ZP, draw.Src) return img }
func (b *Background) flush() { if !b.flushrect.Empty() { draw.DrawMask(b.img, b.flushrect, b.bg, b.flushrect.Min, nil, image.ZP, draw.Src) b.item.Draw(b.img, b.flushrect) if b.imgflush != nil { b.imgflush(b.flushrect) } b.flushrect = image.ZR } }
func (p *Drawer) NoAddMerge(img draw.Image) { dest := p.Img srcBounds := img.Bounds() destB := dest.Bounds() destRect := srcBounds.Sub(srcBounds.Min).Add(destB.Min) source := img sourcePt := srcBounds.Min maskPt := destRect.Min mask := p.Img draw.DrawMask(dest, destRect, source, sourcePt, mask, maskPt, draw.Over) }
func (g *genericImagePainter) Paint(ss []raster.Span, done bool) { for _, s := range ss { draw.DrawMask(g.image, image.Rect(s.X0, s.Y, s.X1, s.Y+1), g.src, image.Point{s.X0, s.X1}, alphaColorImage(uint16(s.A)), image.ZP, g.op) } }
func DrawCharacter(dst draw.Image, x, y int, ch byte, c color.Color) { if ch < 32 || ch > 128 { return } cx := int((ch-32)%16) * 16 cy := int((ch-32)/16) * 16 r := image.Rect(x, y, x+16, y+16) src := &image.Uniform{c} sp := image.Pt(cx, cy) draw.DrawMask(dst, r, src, sp, fontMask, sp, draw.Over) }
func Glyph() { p := image.Point{100, 100} dst := image.NewRGBA(image.Rect(0, 0, 640, 480)) glyphIndex := 42 // GLYPH OMIT src := &image.Uniform{color.RGBA{0, 0, 255, 255}} mask := theGlyphImageForAFont() mr := theBoundsFor(glyphIndex) draw.DrawMask(dst, mr.Sub(mr.Min).Add(p), src, image.ZP, mask, mr.Min, draw.Over) // STOP OMIT }
func render() { for { crcl := &circle{image.Point{mousex, mousey}, radius} draw.DrawMask(dw.Screen(), dw.Screen().Bounds(), &image.Uniform{color.RGBA{r, g, b, a}}, image.ZP, crcl, image.ZP, draw.Over) dw.FlushImage(crcl.Bounds()) // Render only the needed portions to get more FPS select { case <-done: return default: } } }
func (d *decoder) decode(r io.Reader) (err error) { if err = d.decodeHeader(r); err != nil { return err } if err = d.decodeEntries(r); err != nil { return err } d.images = make([]image.Image, d.head.Number) for i, _ := range d.entries { e := &(d.entries[i]) data := make([]byte, e.Size+14) n, err := io.ReadFull(r, data[14:]) if err != nil && err != io.ErrUnexpectedEOF { return err } data = data[:14+n] if n > 8 && bytes.Compare(data[14:22], pngHeader) == 0 { // decode as PNG if d.images[i], err = png.Decode(bytes.NewReader(data[14:])); err != nil { return err } } else { // decode as BMP maskData := d.forgeBMPHead(data, e) if maskData != nil { data = data[:n+14-len(maskData)] } if d.images[i], err = bmp.Decode(bytes.NewReader(data)); err != nil { return err } bounds := d.images[i].Bounds() mask := image.NewAlpha(image.Rect(0, 0, bounds.Dx(), bounds.Dy())) masked := image.NewNRGBA(image.Rect(0, 0, bounds.Dx(), bounds.Dy())) for row := 0; row < int(e.Height); row++ { for col := 0; col < int(e.Width); col++ { if maskData != nil { rowSize := (int(e.Width) + 31) / 32 * 4 if (maskData[row*rowSize+col/8]>>(7-uint(col)%8))&0x01 != 1 { mask.SetAlpha(col, int(e.Height)-row-1, color.Alpha{255}) } } else { // 32-Bit rowSize := (int(e.Width)*32 + 31) / 32 * 4 offset := int(binary.LittleEndian.Uint32(data[10:14])) mask.SetAlpha(col, int(e.Height)-row-1, color.Alpha{data[offset+row*rowSize+col*4+3]}) } } } draw.DrawMask(masked, masked.Bounds(), d.images[i], bounds.Min, mask, bounds.Min, draw.Src) d.images[i] = masked } } return nil }
func GetWallImage(src, mask image.Image, copyPoint image.Point) (draw.Image, error) { srcBounds := src.Bounds() maskBounds := mask.Bounds() white := image.Uniform{color.RGBA{255, 255, 255, 255}} whiteImg := image.NewRGBA(maskBounds) draw.Draw(whiteImg, maskBounds, &white, image.ZP, draw.Over) // Create a new image with src bounds for final background copy := copySrc(src) // Get the src image without mask-bounds-region draw.DrawMask(copy, srcBounds.Add(copyPoint), whiteImg, image.ZP, mask, maskBounds.Min, draw.Over) return copy, nil }
func (i *imageSlice) DrawMask(r image.Rectangle, src image.Image, sp image.Point, mask image.Image, mp image.Point, op draw.Op) bool { //debugp("imageslice draw %v; sp %v\n", r, sp) dr := r.Intersect(i.r) if dr.Empty() { //debugp("-> clipped empty (r %v)\n", i.r) return true } delta := dr.Min.Sub(r.Min) // realignment because of clipping. sp = sp.Add(delta) mp = mp.Add(delta) dr = dr.Sub(i.p) //debugp("-> draw %v; sp %v\n", dr, sp) draw.DrawMask(i.img, dr, src, sp, mask, mp, op) return true }
func (app *App) drawBattery(img draw.Image, metrics *battery.Metrics) { var zeropt image.Point // shrink the rectangle in which energy is drawn to account for thickness // and make the visible percentage more accurate. after adjustment reduce // the energy rect to account for the account of energy drained. energyRect := app.Layout.battRect energyRect.Min.X = app.minEnergy energyRect.Max.X = app.maxEnergy energySize := energyRect.Size() drain := 1 - metrics.Fraction drainSize := int(drain * float64(energySize.X)) energyRect.Min.X += drainSize colorfn := app.EnergyColor if colorfn == nil { colorfn = DefaultEnergyColor } energyColor := colorfn(metrics) // draw the energy first and overlay the battery shell/border. draw.DrawMask(img, energyRect, image.NewUniform(energyColor), zeropt, app.maskEnergy, energyRect.Min, draw.Over) draw.DrawMask(img, app.Layout.battRect, image.NewUniform(app.BatteryColor), zeropt, app.maskBattery, app.Layout.battRect.Min, draw.Over) }
func maskImage(dst draw.Image, filename string) { infile, err := os.Open(filename) defer infile.Close() if err != nil { term.OutputError(fmt.Sprintf("Unable to open overlay image - %s", err.Error())) } else { overlay, _, err := image.Decode(infile) if err != nil { term.OutputError(fmt.Sprintf("Unable to decode image file - %s", err.Error())) } else { mask := image.NewUniform(color.Alpha{128}) draw.DrawMask(dst, dst.Bounds(), imaging.Thumbnail(overlay, 480, 480, imaging.CatmullRom), image.Pt(0, 0), mask, image.Pt(0, 0), draw.Over) } } }