Skip to content

Commit

Permalink
blurhash.Encode() now requires image.Image instead of *image.Image (f…
Browse files Browse the repository at this point in the history
…ixes #3)

It makes no sense to require a pointer to an interface type here,
as we don't make any special use of it.
  • Loading branch information
buckket committed Jul 26, 2020
1 parent af4fad1 commit abcf0b8
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 17 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
language: go

go:
- 1.11.x
- 1.12.x
- 1.13.x
- 1.14.x

install:
- go get -t -v ./...
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ BlurHash is written by [Dag Ågren](https://github.com/DagAgren) / [Wolt](https:

go-blurhash exports three functions:
```go
func blurhash.Encode(xComponents, yComponents int, rgba *image.Image) (string, error)
func blurhash.Encode(xComponents, yComponents int, rgba image.Image) (string, error)
func blurhash.Decode(hash string, width, height, punch int) (image.Image, error)
func blurhash.Components(hash string) (xComponents, yComponents int, err error)
```
Expand All @@ -54,7 +54,7 @@ func main() {
// Generate the BlurHash for a given image
imageFile, _ := os.Open("test.png")
loadedImage, err := png.Decode(imageFile)
str, _ := blurhash.Encode(4, 3, &loadedImage)
str, _ := blurhash.Encode(4, 3, loadedImage)
if err != nil {
// Handle errors
}
Expand Down Expand Up @@ -88,6 +88,7 @@ func main() {
- As mentioned [here](https://github.com/woltapp/blurhash#how-fast-is-encoding-decoding), it’s best to
generate very small images (~32x32px) via BlurHash and scale them up to the desired dimensions afterwards for optimal performance.
- Since [#2](https://github.com/buckket/go-blurhash/pull/2) we diverted from the reference implementation by memorizing sRGBtoLinear values, thus increasing encoding speed at the cost of higher memory usage.
- Starting with v1.1.0 the signature of blurhash.Encode() has changed slightly (see [#3](https://github.com/buckket/go-blurhash/issues/3)).

## License

Expand Down
10 changes: 5 additions & 5 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (e EncodingError) Error() string {
// Encode calculates the Blurhash for an image using the given x and y component counts.
// The x and y components have to be between 1 and 9 respectively.
// The image must be of image.Image type.
func Encode(xComponents int, yComponents int, rgba *image.Image) (string, error) {
func Encode(xComponents int, yComponents int, rgba image.Image) (string, error) {
if xComponents < 1 || xComponents > 9 {
return "", InvalidParameterError{xComponents, "x"}
}
Expand Down Expand Up @@ -106,9 +106,9 @@ func Encode(xComponents int, yComponents int, rgba *image.Image) (string, error)
return blurhash.String(), nil
}

func multiplyBasisFunction(rgba *image.Image, factors []float64, xComponents int, yComponents int) {
height := (*rgba).Bounds().Max.Y
width := (*rgba).Bounds().Max.X
func multiplyBasisFunction(rgba image.Image, factors []float64, xComponents int, yComponents int) {
height := rgba.Bounds().Max.Y
width := rgba.Bounds().Max.X

xvalues := make([][]float64, xComponents)
for xComponent := 0; xComponent < xComponents; xComponent++ {
Expand All @@ -128,7 +128,7 @@ func multiplyBasisFunction(rgba *image.Image, factors []float64, xComponents int

for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
rt, gt, bt, _ := (*rgba).At(x, y).RGBA()
rt, gt, bt, _ := rgba.At(x, y).RGBA()
lr := channelToLinear[rt>>8]
lg := channelToLinear[gt>>8]
lb := channelToLinear[bt>>8]
Expand Down
14 changes: 7 additions & 7 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestEncodeFile(t *testing.T) {
t.Fatal("could not decode test.png")
}

str, err := blurhash.Encode(4, 3, &loadedImage)
str, err := blurhash.Encode(4, 3, loadedImage)
if err != nil {
t.Fatal(err)
return
Expand Down Expand Up @@ -53,7 +53,7 @@ func TestEncodeWrongParameters(t *testing.T) {

for _, tc := range testCases {
t.Run(fmt.Sprintf("xComp:%d yComp:%d", tc.xComp, tc.yComp), func(t *testing.T) {
_, err := blurhash.Encode(tc.xComp, tc.yComp, &img)
_, err := blurhash.Encode(tc.xComp, tc.yComp, img)
if err == nil {
t.Fatal("should have failed")
}
Expand All @@ -70,7 +70,7 @@ func TestEncodeEmptyImage(t *testing.T) {
var img image.Image
img = image.NewNRGBA(image.Rect(0, 0, 100, 100))

str, err := blurhash.Encode(4, 3, &img)
str, err := blurhash.Encode(4, 3, img)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -98,7 +98,7 @@ func TestEncodeSizeFlag(t *testing.T) {

for _, tc := range testCases {
t.Run(fmt.Sprintf("xComp:%d yComp:%d", tc.xComp, tc.yComp), func(t *testing.T) {
str, err := blurhash.Encode(tc.xComp, tc.yComp, &img)
str, err := blurhash.Encode(tc.xComp, tc.yComp, img)
if err != nil {
t.Fatal(err)
}
Expand All @@ -119,7 +119,7 @@ func TestEncodeSingleColor(t *testing.T) {
draw.Draw(img, img.Bounds(), &image.Uniform{tcolor}, image.ZP, draw.Src)
var img2 image.Image = img

str, err := blurhash.Encode(1, 1, &img2)
str, err := blurhash.Encode(1, 1, img2)
if err != nil {
t.Fatal(err)
}
Expand All @@ -141,14 +141,14 @@ func BenchmarkEncode(b *testing.B) {
}

for i := 0; i < b.N; i++ {
_, _ = blurhash.Encode(4, 3, &loadedImage)
_, _ = blurhash.Encode(4, 3, loadedImage)
}
}

func ExampleEncode() {
imageFile, _ := os.Open("test.png")
loadedImage, _ := png.Decode(imageFile)
str, err := blurhash.Encode(4, 3, &loadedImage)
str, err := blurhash.Encode(4, 3, loadedImage)
if err != nil {
// Handle errors
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/buckket/go-blurhash

go 1.12
go 1.14

0 comments on commit abcf0b8

Please sign in to comment.