Image.At returns nil

vityavv :

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?

Tim H. :

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related