为什么 Go 的 draw.DrawMask 似乎忽略了我的黑白面具?

Why does Go's draw.DrawMask appear to ignore my black and white mask?

我正在尝试使用用户输入的数据和背景颜色在 Go 中呈现条形码,尽管条形码本身按预期生成为黑白,但我试图将它们用作 "image/draw" 的 draw.DrawMask 函数导致源图像完全通过,完全忽略遮罩。

这与 Go blog post on the image/draw package 中给出的示例非常相反。

我已将问题减少到一个相当小的例子,黑色背景上的一个简单的白色方块作为蒙版,源和目标颜色统一,并且行为继续。我显然无法理解这个函数的行为方式的某些元素,但是试图找到其他人遇到的类似问题似乎都以完全不同的方法解决问题(例如另一个库来完成这项工作),而不是理解draw.DrawMask.

的使用错误

我发布的代码包括将三个输出图像写入 BMP 文件的函数,但如果使用任何其他方法将 image.Image 数据保存到文件,则会重复此行为。

package main

import (
    "bytes"
    bmp "golang.org/x/image/bmp"
    "image"
    "image/color"
    "image/draw"
    "io/ioutil"
    "os"
)

func main() {
    //Use one rectange to make all new images
    bounds := image.Rect(0, 0, 100, 100)
    //Generate a 20px wide white square in the centre of a black background
    mask := image.NewNRGBA(bounds)
    draw.Draw(mask, bounds, image.NewUniform(color.Black), image.ZP, draw.Src)
    draw.Draw(mask, image.Rect(40, 40, 60, 60), image.NewUniform(color.White), image.ZP, draw.Src)
    //Generate a blue image of the right size - this is unnecessary, but shouldn't hurt
    blue := image.NewNRGBA(bounds)
    draw.Draw(blue, bounds, image.NewUniform(color.NRGBA{B: 255, A: 255}), image.ZP, draw.Src)
    //Copy the blue image into what is the desired output - also unnecessary, but will help to demonstrate each step is working independently
    result := image.NewNRGBA(bounds)
    draw.Draw(result, bounds, blue, image.ZP, draw.Src)
    //Use mask to draw green onto the blue - but only inside the 20px square (in theory)
    draw.DrawMask(result, bounds, image.NewUniform(color.NRGBA{G: 255, A: 255}), image.ZP, mask, image.ZP, draw.Over)

    writeImageToBMP(blue, "blue.bmp")
    writeImageToBMP(mask, "mask.bmp")
    writeImageToBMP(result, "result.bmp")
}

func writeImageToBMP(img image.Image, filename string) {
    //This part isn't relevant to the problem, I just don't know a better way to show content of an image
    var imgBytes bytes.Buffer
    bmp.Encode(&imgBytes, img)
    ioutil.WriteFile(filename, imgBytes.Bytes(), os.ModeExclusive)
}

我希望上面的代码生成三张图片:

  1. 蓝色正方形,100 像素 x 100 像素
  2. 一个 100 像素 x 100 像素的黑色正方形,中间有一个 20 像素 x 20 像素的白色正方形
  3. 一个 100 像素 x 100 像素的蓝色正方形,中间有一个 20 像素 x 20 像素的绿色正方形

前两个按预期出现,但第三个完全是绿色的。

TLDR:面具不应该是黑白的,这就是他们为了视觉效果而渲染它的方式。 遮罩在应该使用Src的地方应该是不透明的,在不应该使用Src的地方应该是透明的。

用以下代码替换我原来代码中的掩码生成,一切突然都按预期工作了。 (将黑色替换为透明,将白色替换为不透明):

mask := image.NewNRGBA(bounds)
draw.Draw(mask, bounds, image.NewUniform(color.Transparent), image.ZP, draw.Src)
draw.Draw(mask, image.Rect(40, 40, 60, 60), image.NewUniform(color.Opaque), image.ZP, draw.Src)

我用了整整一天半的脑袋撞墙,终于屈服了第一次发到SO,然后一想不通立马解决了自己的问题,像个白痴.