dousong1926 2019-03-16 01:34
浏览 47
已采纳

Image.At返回nil

I'm having an issue. Here is my code:

package main

import (
    "math/rand"
    "image/draw"
    "image/png"
    "image/color"
    "strconv"
    "os"
    "time"
    "fmt"
)

func genSites(width, height int) ([][]int) {
    rand.Seed(time.Now().Unix())
    l, err := strconv.Atoi(os.Args[len(os.Args)-2])
    if err != nil {
        panic(err)
    }
    sites := make([][]int, l)
    for i := range sites {
        sites[i] = make([]int, 2)
        sites[i][0] = rand.Intn(width)
        sites[i][1] = rand.Intn(height)
    }
    return sites
}

func main() {
    inputF, err := os.Open(os.Args[len(os.Args)-3])
    if err != nil {
        panic(err)
    }
    defer inputF.Close()

    inputR, err := png.Decode(inputF)
    if err != nil {
        panic(err)
    }
    input := inputR.(draw.Image)
    minx, miny := input.Bounds().Min.X, input.Bounds().Min.Y
    maxx, maxy := input.Bounds().Max.X-1, input.Bounds().Max.Y-1
    sites := genSites(maxx-minx, maxy-miny)
    siteColors := make([][]color.Color, len(sites))
    //todo: figure out something better than this
    for i := range siteColors {
        siteColors[i] = make([]color.Color, (maxx-minx)*(maxy-miny))
    }
    siteBelongs := make([][]int, maxx - minx)
    for x := range siteBelongs {
        siteBelongs[x] = make([]int, maxy - miny)
        for y := range siteBelongs[x] {
            dmin := (maxx-minx)*(maxx-minx) + (maxy-miny)*(maxy-miny)
            var smin int
            for i, s := range sites {
                d := (s[0]-x)*(s[0]-x) + (s[1]-y)*(s[1]-y)
                if d > dmin {
                    smin = i
                    dmin = d
                }
            }
            siteBelongs[x][y] = smin
            siteColors[smin] = append(siteColors[smin], input.At(x+minx, y+miny))
        }
    }
    siteAvgColors := make([]color.Color, len(sites))
    for i := range siteAvgColors {
        var sR, sG, sB, sA int
        for _, val := range siteColors[i] {
            fmt.Println(val)
            r, g, b, a := val.RGBA()
            sR += int(r)
            sG += int(g)
            sB += int(b)
            sA += int(a)
        }
        siteAvgColors[i] = color.RGBA{
            uint8(sR/len(siteColors[i])),
            uint8(sG/len(siteColors[i])),
            uint8(sB/len(siteColors[i])),
            uint8(sA/len(siteColors[i]))}
    }
    for x := range siteBelongs {
        for y := range siteBelongs[x] {
            input.Set(minx + x, miny + y, siteAvgColors[siteBelongs[x][y]])
        }
    }
    output, err := os.Create(os.Args[len(os.Args)-1])
    if err != nil {
        panic(err)
    }
    defer output.Close()
    err = png.Encode(output, input)
    if err != nil {
        panic(err)
    }
}

The error is this:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x4b46e1]

goroutine 1 [running]:
main.main()
    /home/victor/programs/goprograms/src/v/imagerandvornoi/main.go:71 +0x7a1

Line 71 is the one that says r, g, b, a := val.RGBA(). That val is inserted at line 63, or siteColors[smin] = append(siteColors[smin], input.At(x+minx, y+miny)), which means that input.At is returning nil. Why? How can I fix this?

  • 写回答

1条回答 默认 最新

  • drh37116 2019-03-16 01:51
    关注

    This has to do with the semantics of the make() builtin within Go. make is special in that it can take two or three arguments:

    make(Type, length) or make(Type, length, capacity). For the former, the length and capacity are set to the same value. If you're going to be assigning values to the slice using append, you're going to want to use the latter form. This is because the two argument form (make(Type, 10)) builds a slice with 10 elements in it already with the zero value, when you use append() it becomes the 11th item.

    You're hitting a nil derefernece here because you're using the two-argument version of make with append, and so the first item in the slice is nil.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改