登录
首页 >  Golang >  Go问答

快速转换RBGA图像为RGB字节数组

来源:stackoverflow

时间:2024-02-17 14:27:24 477浏览 收藏

哈喽!今天心血来潮给大家带来了《快速转换RBGA图像为RGB字节数组》,想必大家应该对Golang都不陌生吧,那么阅读本文就都不会很困难,以下内容主要涉及到,若是你正在学习Golang,千万别错过这篇文章~希望能帮助到你!

问题内容

我有一个 c 库和函数,需要一个指向包含 rgb 格式的 24 位位图的字节数组的指针。 alpha 通道并不重要,可以被截断。我尝试过这样的事情:

func load(filePath string) *image.RGBA {
    imgFile, err := os.Open(filePath)
    if err != nil {
        fmt.Printf("Cannot read file %v\n", err)
    }
    defer imgFile.Close()

    img, _, err := image.Decode(imgFile)
    if err != nil {
        fmt.Printf("Cannot decode file %v\n", err)
    }
    return img.(*image.RGBA)
}

    img := load("myimg.png")

    bounds := img.Bounds()
    width, height := bounds.Max.X, bounds.Max.Y

    // Convert to RGB? Probably not...
    newImg := image.NewNRGBA(image.Rect(0, 0, width, height))
    draw.Draw(newImg, newImg.Bounds(), img, bounds.Min, draw.Src)
    // Pass image pointer to C function.
    C.PaintOnImage(unsafe.Pointer(&newImg.Pix[0]), C.int(newImg.Bounds().Dy()), C.int(newImg.Bounds().Dx())

但是,nrgba 似乎也是建立在每个像素 4 个字节的基础上的。我可以通过使用 gocv 来解决这个问题,但这对于如此简单的任务来说似乎有点矫枉过正。有没有办法在 go 中以简单有效的方式做到这一点?


正确答案


标准库中没有 rgb 图像类型,但您可以非常轻松地组装 rgb 数组:

bounds := img.bounds()
rgb := make([]byte, bounds.dx()*bounds.dy()*3)
idx := 0
for y := bounds.min.y; y < bounds.max.y; y++ {
    for x := bounds.min.x; x < bounds.max.x; x++ {
        offs := img.pixoffset(x, y)
        copy(rgb[idx:], img.pix[offs:offs+3])
        idx += 3
    }
}

img.pix 数据保存 4 字节 rgba 值。上面的代码只是复制所有像素的前 3 字节 rgb 值。

由于 pix 数组中的行是连续的,因此可以通过每行仅调用 pixoffset 一次来改进上述代码,并且每个像素前进 4 个字节。另外,手动复制 3 个字节可能比调用 copy() 更快(如果对您很重要,则为基准):

bounds := img.Bounds()
rgb := make([]byte, bounds.Dx()*bounds.Dy()*3)
idx := 0

for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
    offs := img.PixOffset(bounds.Min.X, y)
    for x := bounds.Min.X; x < bounds.Max.X; x++ {
        rgb[idx+0] = img.Pix[offs+0]
        rgb[idx+1] = img.Pix[offs+1]
        rgb[idx+2] = img.Pix[offs+2]
        idx += 3
        offs += 4
    }
}

以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>