Skip to content

Commit

Permalink
Fix grayscale on sub-images (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
leizor authored Feb 10, 2024
1 parent f6a5e5b commit d5d9042
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
9 changes: 6 additions & 3 deletions grayscale/grayscale.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package grayscale

import (
"github.com/ernyoke/imger/utils"
"image"
"image/color"

"github.com/ernyoke/imger/utils"
)

// Grayscale takes an image on any type and returns the equivalent grayscale image represented on 8 bits.
func Grayscale(img image.Image) *image.Gray {
gray := image.NewGray(img.Bounds())
size := img.Bounds().Size()
offset := img.Bounds().Min
utils.ParallelForEachPixel(size, func(x, y int) {
gray.Set(x, y, color.GrayModel.Convert(img.At(x, y)))
gray.Set(x+offset.X, y+offset.Y, color.GrayModel.Convert(img.At(x+offset.X, y+offset.Y)))
})
return gray
}
Expand All @@ -20,8 +22,9 @@ func Grayscale(img image.Image) *image.Gray {
func Grayscale16(img image.Image) *image.Gray16 {
gray := image.NewGray16(img.Bounds())
size := img.Bounds().Size()
offset := img.Bounds().Min
utils.ParallelForEachPixel(size, func(x, y int) {
gray.Set(x, y, color.Gray16Model.Convert(img.At(x, y)))
gray.Set(x+offset.X, y+offset.Y, color.Gray16Model.Convert(img.At(x+offset.X, y+offset.Y)))
})
return gray
}
17 changes: 16 additions & 1 deletion grayscale/grayscale_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package grayscale

import (
"github.com/ernyoke/imger/imgio"
"image"
"testing"

"github.com/ernyoke/imger/imgio"
)

// -----------------------------Acceptance tests------------------------------------
Expand All @@ -29,10 +30,24 @@ func Test_Acceptance_GrayScale(t *testing.T) {
tearDownTestCase(t, gray, "../res/grayscale/gray.jpg")
}

func Test_Acceptance_GrayScaleCropped(t *testing.T) {
rgba := setupTestCaseRGBA(t)
cropped := rgba.SubImage(image.Rect(100, 100, rgba.Bounds().Max.X-100, rgba.Bounds().Max.Y-100))
gray := Grayscale(cropped)
tearDownTestCase(t, gray, "../res/grayscale/cropped_gray.jpg")
}

func Test_Acceptance_GrayScale16(t *testing.T) {
rgba := setupTestCaseRGBA(t)
gray := Grayscale16(rgba)
tearDownTestCase(t, gray, "../res/grayscale/gray16.jpg")
}

func Test_Acceptance_GrayScale16Cropped(t *testing.T) {
rgba := setupTestCaseRGBA(t)
cropped := rgba.SubImage(image.Rect(100, 100, rgba.Bounds().Max.X-100, rgba.Bounds().Max.Y-100))
gray := Grayscale16(cropped)
tearDownTestCase(t, gray, "../res/grayscale/cropped_gray16.jpg")
}

// ---------------------------------------------------------------------------------
Binary file added res/grayscale/cropped_gray.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/grayscale/cropped_gray16.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d5d9042

Please sign in to comment.