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.

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

报告相同问题?

悬赏问题

  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵
  • ¥15 cfx离心泵非稳态计算