Home > Software design >  Convert RBGA image to RGB byte array in effcient way
Convert RBGA image to RGB byte array in effcient way

Time:01-14

I have a C library and function that expects a pointer to byte array that contains a 24 bit bitmap in RGB format. Alpha channel is not important and can be truncated. I've tried something like this:

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())

However, it seems that NRGBA is also built on 4 bytes per pixel. I could solve this probably by using GoCV but this seems like overkill for such simple task. Is there a way to do this in a simple and efficient manner in Go?

CodePudding user response:

There is no RGB image type in the standard library, but you can assemble your RGB array pretty easily:

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
    }
}

The img.Pix data holds the 4-byte RGBA values. The code above just copies the leading 3-byte RGB values of all pixels.

Since lines are continuous in the Pix array, you can improve the above code by only calling PixOffset onces per line, and advance by 4 bytes for every pixel. Also manually copying 3 bytes may be faster than calling copy() (benchmark if it matters to you):

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
    }
}
  • Related