Skip to content

Commit

Permalink
Update processor grayscale logic
Browse files Browse the repository at this point in the history
  • Loading branch information
albertus.raharja authored and ajatprabha committed Jul 1, 2019
1 parent 312118a commit dcbdd3b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
File renamed without changes
File renamed without changes
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 21 additions & 3 deletions pkg/processor/native/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package native
import (
"bytes"
"github.com/anthonynsimon/bild/clone"
"github.com/anthonynsimon/bild/effect"
"github.com/anthonynsimon/bild/parallel"
"github.com/anthonynsimon/bild/transform"
"image"
"image/color"
Expand Down Expand Up @@ -98,12 +98,30 @@ func (bp *BildProcessor) GrayScale(input []byte) ([]byte, error) {
if err != nil {
return nil, err
}

img = effect.Grayscale(img)
img = grayScale(img)

return bp.encode(img, f)
}

func grayScale(img image.Image) image.Image {
src := clone.AsRGBA(img)
bounds := src.Bounds()
if bounds.Empty() {
return &image.RGBA{}
}
dst := image.NewRGBA(bounds)
parallel.Line(bounds.Dy(), func(start, end int) {
for y := start; y < end; y++ {
for x := 0; x < bounds.Dx(); x++ {
srcPix := src.At(x, y).(color.RGBA)
g := color.GrayModel.Convert(srcPix).(color.Gray).Y
dst.Set(x, y, color.RGBA{R: g, G: g, B: g, A: srcPix.A})
}
}
})
return dst
}

func (bp *BildProcessor) decode(data []byte) (image.Image, string, error) {
t := time.Now()
img, f, err := image.Decode(bytes.NewReader(data))
Expand Down
16 changes: 11 additions & 5 deletions pkg/processor/native/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ type BildProcessorSuite struct {

func (s *BildProcessorSuite) SetupSuite() {
s.processor = NewBildProcessor()
s.srcData, _ = ioutil.ReadFile("test.png")
s.watermarkData, _ = ioutil.ReadFile("overlay.png")
s.srcData, _ = ioutil.ReadFile("_testdata/test.png")
s.watermarkData, _ = ioutil.ReadFile("_testdata/overlay.png")
s.badData = []byte("badImage.ext")
}

Expand Down Expand Up @@ -52,11 +52,17 @@ func (s *BildProcessorSuite) TestBildProcessor_Crop() {
}

func (s *BildProcessorSuite) TestBildProcessor_Grayscale() {
output, err := s.processor.GrayScale(s.srcData)
var actual, expected []byte
var err error
actual, err = s.processor.GrayScale(s.srcData)
assert.NotNil(s.T(), actual)
assert.Nil(s.T(), err)

assert.NotNil(s.T(), output)
expected, err = ioutil.ReadFile("_testdata/test_grayscaled.png")
assert.NotNil(s.T(), expected)
assert.Nil(s.T(), err)
assert.NotEqual(s.T(), s.srcData, output)

assert.EqualValues(s.T(), actual, expected)
}

func (s *BildProcessorSuite) TestBildProcessor_Watermark() {
Expand Down

0 comments on commit dcbdd3b

Please sign in to comment.