github.com/jmigpin/editor@v1.6.0/util/imageutil/bgra.go (about)

     1  package imageutil
     2  
     3  import (
     4  	"image"
     5  	"image/color"
     6  	"image/draw"
     7  )
     8  
     9  type BGRA struct {
    10  	image.RGBA
    11  }
    12  
    13  func NewBGRA(r *image.Rectangle) *BGRA {
    14  	u := image.NewRGBA(*r)
    15  	return &BGRA{*u}
    16  }
    17  
    18  func NewBGRAFromBuffer(buf []byte, r *image.Rectangle) *BGRA {
    19  	rgba := image.RGBA{Pix: buf, Stride: 4 * r.Dx(), Rect: *r}
    20  	return &BGRA{RGBA: rgba}
    21  }
    22  func BGRASize(r *image.Rectangle) int {
    23  	return r.Dx() * r.Dy() * 4
    24  }
    25  
    26  func (img *BGRA) ColorModel() color.Model {
    27  	panic("!")
    28  }
    29  
    30  func (img *BGRA) Set(x, y int, c color.Color) {
    31  	u := RgbaColor(c)
    32  	img.SetRGBA(x, y, u)
    33  }
    34  
    35  // Allows fast lane if detected.
    36  func (img *BGRA) SetRGBA(x, y int, c color.RGBA) {
    37  	c.R, c.B = c.B, c.R // flip to keep Bgra
    38  	img.RGBA.SetRGBA(x, y, c)
    39  }
    40  func (img *BGRA) At(x, y int) color.Color {
    41  	c := img.RGBA.RGBAAt(x, y)
    42  	c.R, c.B = c.B, c.R // flip to return Rgba
    43  	return c
    44  }
    45  
    46  func (img *BGRA) SubImage(r image.Rectangle) draw.Image {
    47  	u := img.RGBA.SubImage(r).(*image.RGBA)
    48  	return &BGRA{*u}
    49  }
    50  
    51  //----------
    52  
    53  func BgraColor(c color.Color) color.RGBA {
    54  	c2 := RgbaColor(c)
    55  	c2.R, c2.B = c2.B, c2.R // convert to BGR
    56  	return c2
    57  }