最新公告
  • 欢迎您光临码农资源网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!加入我们
  • 高效地将RBGA图像转换为RGB字节数组

    高效地将rbga图像转换为rgb字节数组

    问题内容

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

    func load(filePath string) *image.RGBA {
        imgFile, err := os.Open(filePath)
        if err != nil {
            fmt.Printf("Cannot read file %vn", err)
        }
        defer imgFile.Close()
    
        img, _, err := image.Decode(imgFile)
        if err != nil {
            fmt.Printf("Cannot decode file %vn", 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
        }
    }
    想要了解更多内容,请持续关注码农资源网,一起探索发现编程世界的无限可能!
    本站部分资源来源于网络,仅限用于学习和研究目的,请勿用于其他用途。
    如有侵权请发送邮件至1943759704@qq.com删除

    码农资源网 » 高效地将RBGA图像转换为RGB字节数组
    • 20会员总数(位)
    • 16193资源总数(个)
    • 1013本周发布(个)
    • 22 今日发布(个)
    • 115稳定运行(天)

    提供最优质的资源集合

    立即查看 了解详情