Commit e974fb94 authored by Nigel Tao's avatar Nigel Tao

When making images, allocate one big buffer instead of many small ones.

R=rsc, r
CC=golang-dev
https://golang.org/cl/1267041
parent 72fd5c80
...@@ -38,11 +38,12 @@ func (p *RGBA) Set(x, y int, c Color) { p.Pixel[y][x] = toRGBAColor(c).(RGBAColo ...@@ -38,11 +38,12 @@ func (p *RGBA) Set(x, y int, c Color) { p.Pixel[y][x] = toRGBAColor(c).(RGBAColo
// NewRGBA returns a new RGBA with the given width and height. // NewRGBA returns a new RGBA with the given width and height.
func NewRGBA(w, h int) *RGBA { func NewRGBA(w, h int) *RGBA {
pixel := make([][]RGBAColor, h) buf := make([]RGBAColor, w*h)
for y := 0; y < h; y++ { pix := make([][]RGBAColor, h)
pixel[y] = make([]RGBAColor, w) for y := range pix {
pix[y] = buf[w*y : w*(y+1)]
} }
return &RGBA{pixel} return &RGBA{pix}
} }
// An RGBA64 is an in-memory image backed by a 2-D slice of RGBA64Color values. // An RGBA64 is an in-memory image backed by a 2-D slice of RGBA64Color values.
...@@ -68,11 +69,12 @@ func (p *RGBA64) Set(x, y int, c Color) { p.Pixel[y][x] = toRGBA64Color(c).(RGBA ...@@ -68,11 +69,12 @@ func (p *RGBA64) Set(x, y int, c Color) { p.Pixel[y][x] = toRGBA64Color(c).(RGBA
// NewRGBA64 returns a new RGBA64 with the given width and height. // NewRGBA64 returns a new RGBA64 with the given width and height.
func NewRGBA64(w, h int) *RGBA64 { func NewRGBA64(w, h int) *RGBA64 {
pixel := make([][]RGBA64Color, h) buf := make([]RGBA64Color, w*h)
for y := 0; y < h; y++ { pix := make([][]RGBA64Color, h)
pixel[y] = make([]RGBA64Color, w) for y := range pix {
pix[y] = buf[w*y : w*(y+1)]
} }
return &RGBA64{pixel} return &RGBA64{pix}
} }
// A NRGBA is an in-memory image backed by a 2-D slice of NRGBAColor values. // A NRGBA is an in-memory image backed by a 2-D slice of NRGBAColor values.
...@@ -98,11 +100,12 @@ func (p *NRGBA) Set(x, y int, c Color) { p.Pixel[y][x] = toNRGBAColor(c).(NRGBAC ...@@ -98,11 +100,12 @@ func (p *NRGBA) Set(x, y int, c Color) { p.Pixel[y][x] = toNRGBAColor(c).(NRGBAC
// NewNRGBA returns a new NRGBA with the given width and height. // NewNRGBA returns a new NRGBA with the given width and height.
func NewNRGBA(w, h int) *NRGBA { func NewNRGBA(w, h int) *NRGBA {
pixel := make([][]NRGBAColor, h) buf := make([]NRGBAColor, w*h)
for y := 0; y < h; y++ { pix := make([][]NRGBAColor, h)
pixel[y] = make([]NRGBAColor, w) for y := range pix {
pix[y] = buf[w*y : w*(y+1)]
} }
return &NRGBA{pixel} return &NRGBA{pix}
} }
// A NRGBA64 is an in-memory image backed by a 2-D slice of NRGBA64Color values. // A NRGBA64 is an in-memory image backed by a 2-D slice of NRGBA64Color values.
...@@ -128,11 +131,12 @@ func (p *NRGBA64) Set(x, y int, c Color) { p.Pixel[y][x] = toNRGBA64Color(c).(NR ...@@ -128,11 +131,12 @@ func (p *NRGBA64) Set(x, y int, c Color) { p.Pixel[y][x] = toNRGBA64Color(c).(NR
// NewNRGBA64 returns a new NRGBA64 with the given width and height. // NewNRGBA64 returns a new NRGBA64 with the given width and height.
func NewNRGBA64(w, h int) *NRGBA64 { func NewNRGBA64(w, h int) *NRGBA64 {
pixel := make([][]NRGBA64Color, h) buf := make([]NRGBA64Color, w*h)
for y := 0; y < h; y++ { pix := make([][]NRGBA64Color, h)
pixel[y] = make([]NRGBA64Color, w) for y := range pix {
pix[y] = buf[w*y : w*(y+1)]
} }
return &NRGBA64{pixel} return &NRGBA64{pix}
} }
// An Alpha is an in-memory image backed by a 2-D slice of AlphaColor values. // An Alpha is an in-memory image backed by a 2-D slice of AlphaColor values.
...@@ -158,11 +162,12 @@ func (p *Alpha) Set(x, y int, c Color) { p.Pixel[y][x] = toAlphaColor(c).(AlphaC ...@@ -158,11 +162,12 @@ func (p *Alpha) Set(x, y int, c Color) { p.Pixel[y][x] = toAlphaColor(c).(AlphaC
// NewAlpha returns a new Alpha with the given width and height. // NewAlpha returns a new Alpha with the given width and height.
func NewAlpha(w, h int) *Alpha { func NewAlpha(w, h int) *Alpha {
pixel := make([][]AlphaColor, h) buf := make([]AlphaColor, w*h)
for y := 0; y < h; y++ { pix := make([][]AlphaColor, h)
pixel[y] = make([]AlphaColor, w) for y := range pix {
pix[y] = buf[w*y : w*(y+1)]
} }
return &Alpha{pixel} return &Alpha{pix}
} }
// A PalettedColorModel represents a fixed palette of colors. // A PalettedColorModel represents a fixed palette of colors.
...@@ -235,9 +240,10 @@ func (p *Paletted) SetColorIndex(x, y int, index uint8) { ...@@ -235,9 +240,10 @@ func (p *Paletted) SetColorIndex(x, y int, index uint8) {
// NewPaletted returns a new Paletted with the given width, height and palette. // NewPaletted returns a new Paletted with the given width, height and palette.
func NewPaletted(w, h int, m PalettedColorModel) *Paletted { func NewPaletted(w, h int, m PalettedColorModel) *Paletted {
pixel := make([][]uint8, h) buf := make([]uint8, w*h)
for y := 0; y < h; y++ { pix := make([][]uint8, h)
pixel[y] = make([]uint8, w) for y := range pix {
pix[y] = buf[w*y : w*(y+1)]
} }
return &Paletted{pixel, m} return &Paletted{pix, m}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment