本文整理匯總了Golang中image.Rect函數的典型用法代碼示例。如果您正苦於以下問題:Golang Rect函數的具體用法?Golang Rect怎麽用?Golang Rect使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Rect函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: to_img
func to_img(brot *[size][size]uint, max uint) {
gray := image.NewGray16(image.Rect(0, 0, size, size))
norm_gray := image.NewGray16(image.Rect(0, 0, size, size))
for x := 0; x < size; x++ {
for y := 0; y < size; y++ {
pix := brot[x][y]
norm := float64(brot[x][y]*4) / float64(max) * 65534
if norm > 65534 {
norm = 65534
}
if pix > 65534 {
pix = 65534
}
gray.SetGray16(
x, y,
color.Gray16{uint16(pix)})
norm_gray.SetGray16(
x, y,
color.Gray16{uint16(norm)})
}
}
w, _ := os.OpenFile("./brot.png", os.O_CREATE|os.O_WRONLY, 0666)
png.Encode(w, gray)
n, _ := os.OpenFile("./brot-norm.png", os.O_CREATE|os.O_WRONLY, 0666)
png.Encode(n, norm_gray)
}
示例2: 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
}
示例3: TestEncodeAllFramesOutOfBounds
func TestEncodeAllFramesOutOfBounds(t *testing.T) {
images := []*image.Paletted{
image.NewPaletted(image.Rect(0, 0, 5, 5), palette.Plan9),
image.NewPaletted(image.Rect(2, 2, 8, 8), palette.Plan9),
image.NewPaletted(image.Rect(3, 3, 4, 4), palette.Plan9),
}
for _, upperBound := range []int{6, 10} {
g := &GIF{
Image: images,
Delay: make([]int, len(images)),
Disposal: make([]byte, len(images)),
Config: image.Config{
Width: upperBound,
Height: upperBound,
},
}
err := EncodeAll(ioutil.Discard, g)
if upperBound >= 8 {
if err != nil {
t.Errorf("upperBound=%d: %v", upperBound, err)
}
} else {
if err == nil {
t.Errorf("upperBound=%d: got nil error, want non-nil", upperBound)
}
}
}
}
示例4: TestDrawOverlap
func TestDrawOverlap(t *testing.T) {
for _, op := range []Op{Over, 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.
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
}
}
}
}
}
}
}
示例5: rotate
func rotate(im image.Image, angle int) image.Image {
var rotated *image.NRGBA
// trigonometric (i.e counter clock-wise)
switch angle {
case 90:
newH, newW := im.Bounds().Dx(), im.Bounds().Dy()
rotated = image.NewNRGBA(image.Rect(0, 0, newW, newH))
for y := 0; y < newH; y++ {
for x := 0; x < newW; x++ {
rotated.Set(x, y, im.At(newH-1-y, x))
}
}
case -90:
newH, newW := im.Bounds().Dx(), im.Bounds().Dy()
rotated = image.NewNRGBA(image.Rect(0, 0, newW, newH))
for y := 0; y < newH; y++ {
for x := 0; x < newW; x++ {
rotated.Set(x, y, im.At(y, newW-1-x))
}
}
case 180, -180:
newW, newH := im.Bounds().Dx(), im.Bounds().Dy()
rotated = image.NewNRGBA(image.Rect(0, 0, newW, newH))
for y := 0; y < newH; y++ {
for x := 0; x < newW; x++ {
rotated.Set(x, y, im.At(newW-1-x, newH-1-y))
}
}
default:
return im
}
return rotated
}
示例6: TestLoadConfig
func TestLoadConfig(t *testing.T) {
var sampleFile = "../_template/furnace.toml"
var answer = Config{
Type: "furnace",
Image: "furnace.png",
Crop: image.Rect(7, 6, 168, 79),
Slot: []image.Point{
{56, 53},
{56, 17},
{116, 35},
},
Progress: []Progress{
{
Crop: image.Rect(176, 0, 189, 13),
Paste: image.Pt(57, 36),
},
{
Crop: image.Rect(176, 14, 24, 17),
Paste: image.Pt(199, 30),
},
},
}
sample := sampleFile
ans := answer
if !reflect.DeepEqual(LoadConfig(sample), ans) {
t.Error("toml convert error: not equal input and output \n", LoadConfig(sample), ans)
}
}
示例7: TestSrcMask
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)
}
}
示例8: parseImgBoundary
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
}
}
示例9: mergeImage
//------------------------------------------------------------------------------
// Image Functions
func mergeImage(imgs []image.Image) image.Image {
width := 0
height := 0
imgs[0] = RotateImageLeft(imgs[0])
for _, v := range imgs {
if v.Bounds().Size().X > width {
width = v.Bounds().Size().X
}
height += v.Bounds().Size().Y
}
m := image.NewRGBA(image.Rect(0, 0, width, height))
height = 0
for _, v := range imgs {
h := v.Bounds().Size().Y
draw.Draw(m,
image.Rect(0, height, v.Bounds().Size().X, h+height),
v,
v.Bounds().Min,
draw.Src)
height += h
}
return m
}
示例10: Crop
func Crop(img image.Image, width int, height int, padding int) (fimg draw.Image, err error) {
// For now assume top left is bg color
// future maybe avg outer rows of pixels
bgcolor := img.At(img.Bounds().Min.X, img.Bounds().Min.Y)
logoh, logow := height-2*padding, width-2*padding
logorat := float32(logow) / float32(logoh)
interior := findLogo(img, bgcolor)
interior.Max = interior.Max.Add(image.Pt(1, 1))
center := func(rect image.Rectangle) image.Point {
return image.Point{(rect.Max.X - rect.Min.X) / 2, (rect.Max.Y - rect.Min.Y) / 2}
}
fimg = image.NewRGBA(image.Rect(0, 0, width, height))
rc := center(fimg.Bounds())
origrat := float32(interior.Dx()) / float32(interior.Dy())
if logorat > origrat {
logow = int(origrat * float32(logoh))
} else {
logoh = int(float32(logow) / origrat)
}
logoimg := Resize(img, interior, logow, logoh)
logorect := image.Rect(0, 0, logoimg.Bounds().Dx(), logoimg.Bounds().Dy())
logorect = logorect.Add(rc.Sub(image.Pt(logorect.Dx()/2, logorect.Dy()/2)))
draw.Draw(fimg, fimg.Bounds(), &image.Uniform{bgcolor}, image.ZP, draw.Src)
draw.Draw(fimg, logorect, logoimg, image.ZP, draw.Src)
return
}
示例11: writeToJP
func writeToJP() {
imgRect := image.Rect(0, 0, gridSize*10, gridSize*10)
img := image.NewGray(imgRect)
draw.Draw(img, img.Bounds(), &image.Uniform{color.White}, image.ZP, draw.Src)
for x := 0; x < gridSize; x++ {
for y := 0; y < gridSize; y++ {
fill := &image.Uniform{color.White}
if Env[x][y] == -1 {
fill = &image.Uniform{color.Black}
} else if Env[x][y] > 1 {
c := color.Gray{uint8(Env[x][y] * 20)}
fill = &image.Uniform{c}
}
draw.Draw(img, image.Rect((x-1)*10, (y-1)*10, x*10, y*10), fill, image.ZP, draw.Src)
}
}
buf := bytes.Buffer{}
// ok, write out the data into the new JPEG file
err := gif.Encode(&buf, img, nil)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
tmpimg, err := gif.Decode(&buf)
if err != nil {
log.Printf("Skipping frame due to weird error reading the temporary gif :%s", err)
}
frames = append(frames, tmpimg.(*image.Paletted))
}
示例12: TestGMProcess
func TestGMProcess(t *testing.T) {
buffer := new(bytes.Buffer)
png.Encode(buffer, image.NewRGBA(image.Rect(0, 0, 200, 200)))
gm := NewGMProcessor()
params, _ := ParseParams("r_100x100,c_50x50,g_c,q_50")
img, err := gm.Process(params, "text.png", buffer)
assert.NoError(t, err)
assert.NotNil(t, img)
assert.True(t, img.Len() > 0)
buffer = new(bytes.Buffer)
png.Encode(buffer, image.NewRGBA(image.Rect(0, 0, 200, 200)))
params, _ = ParseParams("r_100x0,c_50x50,g_c,q_50")
img, err = gm.Process(params, "text.png", buffer)
assert.NoError(t, err)
assert.NotNil(t, img)
assert.True(t, img.Len() > 0)
buffer = new(bytes.Buffer)
png.Encode(buffer, image.NewRGBA(image.Rect(0, 0, 200, 200)))
params, _ = ParseParams("r_0x100,c_50x50,g_c,q_50")
img, err = gm.Process(params, "text.png", buffer)
assert.NoError(t, err)
assert.NotNil(t, img)
assert.True(t, img.Len() > 0)
}
示例13: TestRotate270
func TestRotate270(t *testing.T) {
td := []struct {
desc string
src image.Image
want *image.NRGBA
}{
{
"Rotate270 2x3",
&image.NRGBA{
Rect: image.Rect(-1, -1, 1, 2),
Stride: 2 * 4,
Pix: []uint8{
0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff,
0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00,
0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff,
},
},
&image.NRGBA{
Rect: image.Rect(0, 0, 3, 2),
Stride: 3 * 4,
Pix: []uint8{
0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x11, 0x22, 0x33,
0x00, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xcc, 0xdd, 0xee, 0xff,
},
},
},
}
for _, d := range td {
got := Rotate270(d.src)
want := d.want
if !compareNRGBA(got, want, 0) {
t.Errorf("test [%s] failed: %#v", d.desc, got)
}
}
}
示例14: joinGrid
// Join the matrix of images into a single image.
func (h Himawari) joinGrid(grid [][]image.Image) image.Image {
dst := image.NewNRGBA(image.Rect(0, 0, gridSize*h.Depth, gridSize*h.Depth))
var wg sync.WaitGroup
for i := range grid {
for j := range grid[i] {
if grid[i][j] == nil {
break
}
wg.Add(1)
go func(i, j int) {
defer wg.Done()
// Modification is performed pixel-by-pixel, so async
// should be fine, theoretically.
src := grid[i][j]
sdx, sdy := src.Bounds().Dx(), src.Bounds().Dy()
rect := image.Rect(
i*sdx, j*sdy, i*sdx+sdx, j*sdy+sdy,
)
draw.Draw(dst, rect, src, image.ZP, draw.Src)
}(i, j)
}
}
wg.Wait()
return dst
}
示例15: DrawText
// DrawText is a convenience function that will create a new image, render
// the provided text to it, paint the image to the provided window, and resize
// the window to fit the text snugly.
//
// An error can occur when rendering the text to an image.
func DrawText(win *xwindow.Window, font *truetype.Font, size float64,
fontClr, bgClr color.RGBA, text string) error {
// Over estimate the extents.
ew, eh := xgraphics.TextMaxExtents(font, size, text)
eh += misc.TextBreathe // <-- this is the bug
// Create an image using the over estimated extents.
img := xgraphics.New(win.X, image.Rect(0, 0, ew, eh))
xgraphics.BlendBgColor(img, bgClr)
// Now draw the text, grab the (x, y) position advanced by the text, and
// check for an error in rendering.
x, y, err := img.Text(0, 0, fontClr, size, font, text)
if err != nil {
return err
}
// Resize the window to the geometry determined by (x, y).
w, h := x, y+misc.TextBreathe // <-- also part of the bug
win.Resize(w, h)
// Now draw the image to the window and destroy it.
img.XSurfaceSet(win.Id)
subimg := img.SubImage(image.Rect(0, 0, w, h))
subimg.XDraw()
subimg.XPaint(win.Id)
img.Destroy()
return nil
}