Skip to content

Commit

Permalink
Merge pull request #77 from yangwenmai/improve_adjust_apply
Browse files Browse the repository at this point in the history
feat: improve adjust apply
  • Loading branch information
anthonynsimon authored Feb 4, 2020
2 parents 05cf37d + bb01dfa commit c7da305
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
21 changes: 13 additions & 8 deletions adjust/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,22 @@ func Apply(img image.Image, fn func(color.RGBA) color.RGBA) *image.RGBA {

c := color.RGBA{}

c.R = dst.Pix[dstPos+0]
c.G = dst.Pix[dstPos+1]
c.B = dst.Pix[dstPos+2]
c.A = dst.Pix[dstPos+3]
dr := &dst.Pix[dstPos+0]
dg := &dst.Pix[dstPos+1]
db := &dst.Pix[dstPos+2]
da := &dst.Pix[dstPos+3]

c.R = *dr
c.G = *dg
c.B = *db
c.A = *da

c = fn(c)

dst.Pix[dstPos+0] = c.R
dst.Pix[dstPos+1] = c.G
dst.Pix[dstPos+2] = c.B
dst.Pix[dstPos+3] = c.A
*dr = c.R
*dg = c.G
*db = c.B
*da = c.A
}
}
})
Expand Down
18 changes: 18 additions & 0 deletions adjust/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,21 @@ func TestClampFloat64(t *testing.T) {
}
}
}

func BenchmarkApply(b *testing.B) {
val := &image.RGBA{
Rect: image.Rect(0, 0, 2, 2),
Stride: 2 * 4,
Pix: []uint8{
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
},
}
fn := func(c color.RGBA) color.RGBA {
return color.RGBA{c.R - 64, c.G - 64, c.B - 64, c.A - 64}
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
Apply(val, fn)
}
}

0 comments on commit c7da305

Please sign in to comment.