向图像调色板添加颜色

Adding color to an image palette

我正在 go 中创建一个实用程序,它将加载一个图像(PNG 或 GIF)并将其与另一个图像叠加,以帮助调试这个问题我写了一些代码来突出显示边框红色的透明度,所以红色 color.Color{R:255,G:0,B:0,A:255} 需要添加到调色板中,如果它还没有的话,所有这些都使用与图像叠加过程相同的代码,但更容易理解,所以我会在此处共享该代码。

除了一个方面,我的这个工作正常,那就是我需要将叠加层的调色板添加到源图像。

我能够提取并附加到源图像的调色板,并创建一个新的 image.Paletted,其中包含来自源图像的所有 image.Image.Pix 数据,以及 image.Paletted.Palette 包含新颜色,但是当使用 image.Paletted.SetColorIndex() 时(在使用 image.Paletted.Palette.Index() 获得正确的索引并将结果保存到文件后,没有任何变化。

奇怪的是,如果我注释掉所有调色板代码,叠加图像将写入文件,但颜色会调整到源图像中最近的邻居(根据 go 文档)。

我不确定为什么修改调色板会阻止我写入图像。

这是循环遍历边缘像素并将其绘制为红色的代码。

func ApplyAttachment(img image.Image, augment *augment.Augment, attachment *augment.Attachment, debug bool) (appliedImage image.Image, err error) {
    edgePoints := FindEdge(img, EdgeFromString(attachment.Name))
    if debug {
        img, err = AddToPallete(img, red)
        if err != nil {
            logger.Warn("error adding to pallete: %s", err)
        }
        for _, pt := range edgePoints {
            if err = SetImagePixel(&img, pt.X, pt.Y, red); err != nil {
                logger.Warn("error setting image pixel: %s", err)
            }
        }
    } 
    ...

这是 AddToPalette 的代码

// AddToPallete adds a color to the palette of an image.
func AddToPallete(addTo image.Image, c color.Color) (output image.Image, err error) {
    srcPallete := extractPallete(addTo)
    if !palleteContains(srcPallete, c) {
        srcPallete = append(srcPallete, c)
    }

    pImg := image.NewPaletted(addTo.Bounds(), srcPallete)
    ipImg := pImg.SubImage(pImg.Bounds())
    output, err = ImageOntoImage(&ipImg, &addTo, augment.Point{X: 0, Y: 0})
    return
}

这是 ImageOntoImage 的代码(将一个 image.Image 写入另一个)

// ImageOntoImage does exactly that
func ImageOntoImage(
    source *image.Image,
    overlay *image.Image,
    startAt augment.Point,
) (output image.Image, err error) {
    output = *source
    for curY := startAt.Y; curY < startAt.Y+(*overlay).Bounds().Max.Y; curY++ {
        for curX := startAt.X; curX < startAt.X+(*overlay).Bounds().Max.X; curX++ {
            c := output.At(curX, curY)
            oc := (*overlay).At(curX-startAt.X, curY-startAt.Y)
            cc := combineColors(c, oc)
            if err = SetImagePixel(&output, curX, curY, cc); err != nil {
                err = errors.New("Cannot modify image")
                return
            }
        }
    }
    return
}

// SetImagePixel sets a single pixel of an image to another color
func SetImagePixel(
    source *image.Image,
    x int,
    y int,
    color color.Color,
) (err error) {
    if poutput, ok := (*source).(*image.Paletted); ok {
        cindex := poutput.Palette.Index(color)
        poutput.SetColorIndex(x, y, uint8(cindex))
        asImg := image.Image(poutput)
        source = &asImg
    } else if coutput, ok := (*source).(Changeable); ok {
        coutput.Set(x, y, color)
    } else {
        err = errors.New("Cannot modify image")
        return
    }
    return
}


总而言之,当我不扩展调色板时,我可以在图像上设置像素,但像素会更改为最接近的颜色,当我扩展调色板时,像素不会设置, (或者可能,最近的颜色以某种方式近似于透明?)

所以我觉得有点尴尬。 我所有的调色板代码都运行良好。 我被指针灼伤了。 通过创建一个新的调色板图像,我失去了与指向原始图像的指针的连接,因此设置像素在 go 的作用域中丢失了。 这就是为什么注释掉调色板代码修复它,设置新像素时仍在使用原始指针,当我创建调色板图像时,我 out-scoped 原始指针并且在正确设置像素时,保存图像功能仍然引用原始加载的图像,因此它基本上是在创建该图像的副本。