Allow slices in the image Filter funcs, not just varargs
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 4 Sep 2019 12:07:10 +0000 (14:07 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 4 Sep 2019 15:24:50 +0000 (17:24 +0200)
[ci skip]

See #6255

hugolib/image_test.go
resources/image.go
resources/image_test.go
resources/images/filters_test.go [new file with mode: 0644]
resources/images/image.go
resources/resource/resourcetypes.go
resources/transform.go
tpl/images/images.go

index 0cd781018e38f435f5c4c687359330688c26c615..4db57e4a5a5954430d5fee843ec6bedfbb008a43 100644 (file)
@@ -28,7 +28,7 @@ import (
 
 // We have many tests for the different resize operations etc. in the resource package,
 // this is an integration test.
-func TestImageResize(t *testing.T) {
+func TestImageOps(t *testing.T) {
        c := qt.New(t)
        // Make this a real as possible.
        workDir, clean, err := htesting.CreateTempDir(hugofs.Os, "image-resize")
@@ -69,6 +69,22 @@ title: "My bundle"
 {{ printf "Resized%d:" (add $i  1) }} {{ $r.Name }}|{{ $r.Width }}|{{ $r.Height }}|{{ $r.MediaType }}|{{ $r.RelPermalink }}|
 {{ end }}
 
+{{ $blurryGrayscale1 := $r | images.Filter images.Grayscale (images.GaussianBlur 8) }}
+BG1: {{ $blurryGrayscale1.RelPermalink }}/{{ $blurryGrayscale1.Width }}
+{{ $blurryGrayscale2 := $r.Filter images.Grayscale (images.GaussianBlur 8) }}
+BG2: {{ $blurryGrayscale2.RelPermalink }}/{{ $blurryGrayscale2.Width }}
+{{ $blurryGrayscale2_2 := $r.Filter images.Grayscale (images.GaussianBlur 8) }}
+BG2_2: {{ $blurryGrayscale2_2.RelPermalink }}/{{ $blurryGrayscale2_2.Width }}
+
+{{ $filters := slice images.Grayscale (images.GaussianBlur 9) }}
+{{ $blurryGrayscale3 := $r | images.Filter $filters }}
+BG3: {{ $blurryGrayscale3.RelPermalink }}/{{ $blurryGrayscale3.Width }}
+
+{{ $blurryGrayscale4 := $r.Filter $filters }}
+BG4: {{ $blurryGrayscale4.RelPermalink }}/{{ $blurryGrayscale4.Width }}
+
+
+
 `)
 
                return b
@@ -106,7 +122,10 @@ Resized3: sunset.jpg|345|678|image/jpg|/mybundle/sunset_hu59e56ffff1bc1d8d122b14
 Resized4: sunset.jpg|34|67|image/jpg|/mybundle/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_44d8c928664d7c5a67377c6ec58425ce.jpg|
 Resized5: images/sunset.jpg|456|789|image/jpg|/images/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_456x789_resize_q75_box.jpg|
 Resized6: images/sunset.jpg|350|219|image/jpg|/images/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_350x0_resize_q75_box.a86fe88d894e5db613f6aa8a80538fefc25b20fa24ba0d782c057adcef616f56.jpg|
-
+BG1: /images/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_2ae8bb993431ec1aec40fe59927b46b4.jpg/123
+BG2: /images/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_2ae8bb993431ec1aec40fe59927b46b4.jpg/123
+BG3: /images/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_ed7740a90b82802261c2fbdb98bc8082.jpg/123
+BG4: /images/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_ed7740a90b82802261c2fbdb98bc8082.jpg/123
 `
 
        b.AssertFileContent(filepath.Join(workDir, "public/index.html"), imgExpect)
@@ -171,6 +190,7 @@ No bundle for {{ $.Site.Language.Lang }}
 {{ $resized2 := $sunset2.Resize "123x234" }}
 SUNSET2: {{ $resized2.RelPermalink }}/{{ $resized2.Width }}/Lat: {{ $resized2.Exif.Lat }}
 
+
 `)
 
        b.Build(BuildCfg{})
index 8a9e6fde4a2a8de515fbc19305c99bff2937797c..26b9b8710b04cb4df0dd97f845ebcdbb5d7666dd 100644 (file)
@@ -28,6 +28,8 @@ import (
        "strings"
        "sync"
 
+       "github.com/disintegration/gift"
+
        "github.com/gohugoio/hugo/cache/filecache"
        "github.com/gohugoio/hugo/resources/images/exif"
 
@@ -37,7 +39,6 @@ import (
 
        _errors "github.com/pkg/errors"
 
-       "github.com/disintegration/gift"
        "github.com/gohugoio/hugo/helpers"
        "github.com/gohugoio/hugo/resources/images"
 
@@ -209,12 +210,19 @@ func (i *imageResource) Fill(spec string) (resource.Image, error) {
        })
 }
 
-func (i *imageResource) Filter(filters ...gift.Filter) (resource.Image, error) {
+func (i *imageResource) Filter(filters ...interface{}) (resource.Image, error) {
        conf := i.Proc.GetDefaultImageConfig("filter")
-       conf.Key = internal.HashString(filters)
+
+       var gfilters []gift.Filter
+
+       for _, f := range filters {
+               gfilters = append(gfilters, images.ToFilters(f)...)
+       }
+
+       conf.Key = internal.HashString(gfilters)
 
        return i.doWithImageConfig(conf, func(src image.Image) (image.Image, error) {
-               return i.Proc.Filter(src, filters...)
+               return i.Proc.Filter(src, gfilters...)
        })
 }
 
@@ -331,9 +339,6 @@ func (i *imageResource) getImageMetaCacheTargetPath() string {
 
 func (i *imageResource) relTargetPathFromConfig(conf images.ImageConfig) dirFile {
        p1, p2 := helpers.FileAndExt(i.getResourcePaths().relTargetDirFile.file)
-       if conf.Action == "trace" {
-               p2 = ".svg"
-       }
 
        h, _ := i.hash()
        idStr := fmt.Sprintf("_hu%s_%d", h, i.size())
index 0146e8bdd60a621a75dcc352418f42a3f5a55abb..59d6b7c9a2c029af9ebca57cee4a29c462dfbd4e 100644 (file)
@@ -512,7 +512,7 @@ func TestImageOperationsGolden(t *testing.T) {
                        c.Assert(rel, qt.Not(qt.Equals), "")
                }
 
-               resized, err = resized.Filter(filters[0:4]...)
+               resized, err = resized.Filter(filters[0:4])
                c.Assert(err, qt.IsNil)
                rel := resized.RelPermalink()
                c.Log("filter all", rel)
diff --git a/resources/images/filters_test.go b/resources/images/filters_test.go
new file mode 100644 (file)
index 0000000..658a9a4
--- /dev/null
@@ -0,0 +1,34 @@
+// Copyright 2019 The Hugo Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package images
+
+import (
+       "testing"
+
+       "github.com/gohugoio/hugo/resources/internal"
+
+       qt "github.com/frankban/quicktest"
+)
+
+func TestFilterHash(t *testing.T) {
+       c := qt.New(t)
+
+       f := &Filters{}
+
+       c.Assert(internal.HashString(f.Grayscale()), qt.Equals, internal.HashString(f.Grayscale()))
+       c.Assert(internal.HashString(f.Grayscale()), qt.Not(qt.Equals), internal.HashString(f.Invert()))
+       c.Assert(internal.HashString(f.Gamma(32)), qt.Not(qt.Equals), internal.HashString(f.Gamma(33)))
+       c.Assert(internal.HashString(f.Gamma(32)), qt.Equals, internal.HashString(f.Gamma(32)))
+
+}
index aa7d567aa7052d9fe5e5ee7e57a16658369870b7..e72d968370b1a4a14bf3946baff691fe8bfa2945 100644 (file)
@@ -14,6 +14,7 @@
 package images
 
 import (
+       "fmt"
        "image"
        "image/color"
        "image/gif"
@@ -259,3 +260,20 @@ func imageConfigFromImage(img image.Image) image.Config {
        b := img.Bounds()
        return image.Config{Width: b.Max.X, Height: b.Max.Y}
 }
+
+func ToFilters(in interface{}) []gift.Filter {
+       switch v := in.(type) {
+       case []gift.Filter:
+               return v
+       case []filter:
+               vv := make([]gift.Filter, len(v))
+               for i, f := range v {
+                       vv[i] = f
+               }
+               return vv
+       case gift.Filter:
+               return []gift.Filter{v}
+       default:
+               panic(fmt.Sprintf("%T is not an image filter", in))
+       }
+}
index f6b6d2af1acf26653a18888520c960c8ebe4c533..7a055b25dd3dee3ad41e2a63bf30c48df5f39e03 100644 (file)
@@ -14,7 +14,6 @@
 package resource
 
 import (
-       "github.com/disintegration/gift"
        "github.com/gohugoio/hugo/langs"
        "github.com/gohugoio/hugo/media"
        "github.com/gohugoio/hugo/resources/images/exif"
@@ -49,7 +48,7 @@ type ImageOps interface {
        Fill(spec string) (Image, error)
        Fit(spec string) (Image, error)
        Resize(spec string) (Image, error)
-       Filter(filters ...gift.Filter) (Image, error)
+       Filter(filters ...interface{}) (Image, error)
        Exif() (*exif.Exif, error)
 }
 
index eb282eab6f3a846d9cc4c535704de8928c70a51f..1fb3ed4da652e39bb2980532b4c2a72446f48d1e 100644 (file)
@@ -21,7 +21,6 @@ import (
        "strings"
        "sync"
 
-       "github.com/disintegration/gift"
        "github.com/gohugoio/hugo/resources/images/exif"
        "github.com/spf13/afero"
 
@@ -174,7 +173,7 @@ func (r *resourceAdapter) Fit(spec string) (resource.Image, error) {
        return r.getImageOps().Fit(spec)
 }
 
-func (r *resourceAdapter) Filter(filters ...gift.Filter) (resource.Image, error) {
+func (r *resourceAdapter) Filter(filters ...interface{}) (resource.Image, error) {
        return r.getImageOps().Filter(filters...)
 }
 
index b644568766f5c705821c8ff3f092a92c820d6a59..1d12aea725a1ee0d01cf0fb480d4210fda38ebdf 100644 (file)
@@ -18,8 +18,6 @@ import (
        "image"
        "sync"
 
-       "github.com/disintegration/gift"
-
        "github.com/pkg/errors"
 
        "github.com/gohugoio/hugo/resources/images"
@@ -101,10 +99,6 @@ func (ns *Namespace) Filter(args ...interface{}) (resource.Image, error) {
 
        img := args[len(args)-1].(resource.Image)
        filtersv := args[:len(args)-1]
-       filters := make([]gift.Filter, len(filtersv))
-       for i, f := range filtersv {
-               filters[i] = f.(gift.Filter)
-       }
 
-       return img.Filter(filters...)
+       return img.Filter(filtersv...)
 }