diff --git a/resources/image_test.go b/resources/image_test.go index e39e88992..a92249132 100644 --- a/resources/image_test.go +++ b/resources/image_test.go @@ -28,6 +28,8 @@ import ( "testing" "time" + "github.com/gohugoio/hugo/resources/images/webp" + "github.com/gohugoio/hugo/common/paths" "github.com/spf13/afero" @@ -550,6 +552,47 @@ func goldenEqual(img1, img2 *image.NRGBA) bool { return true } +// Issue #8729 +func TestImageOperationsGoldenWebp(t *testing.T) { + if !webp.Supports() { + t.Skip("skip webp test") + } + c := qt.New(t) + c.Parallel() + + devMode := false + + testImages := []string{"fuzzy-cirlcle.png"} + + spec, workDir := newTestResourceOsFs(c) + defer func() { + if !devMode { + os.Remove(workDir) + } + }() + + if devMode { + fmt.Println(workDir) + } + + for _, imageName := range testImages { + image := fetchImageForSpec(spec, c, imageName) + imageWebp, err := image.Resize("200x webp") + c.Assert(err, qt.IsNil) + c.Assert(imageWebp.Width(), qt.Equals, 200) + } + + if devMode { + return + } + + dir1 := filepath.Join(workDir, "resources/_gen/images") + dir2 := filepath.FromSlash("testdata/golden_webp") + + assetGoldenDirs(c, dir1, dir2) + +} + func TestImageOperationsGolden(t *testing.T) { c := qt.New(t) c.Parallel() @@ -658,6 +701,12 @@ func TestImageOperationsGolden(t *testing.T) { dir1 := filepath.Join(workDir, "resources/_gen/images") dir2 := filepath.FromSlash("testdata/golden") + assetGoldenDirs(c, dir1, dir2) + +} + +func assetGoldenDirs(c *qt.C, dir1, dir2 string) { + // The two dirs above should now be the same. dirinfos1, err := ioutil.ReadDir(dir1) c.Assert(err, qt.IsNil) @@ -692,7 +741,7 @@ func TestImageOperationsGolden(t *testing.T) { "gohugoio8_hu7f72c00afdf7634587afaa5eff2a25b2_73538_300x200_fill_gaussian_smart1_2.png": c.Log("expectedly differs from golden due to dithering:", fi1.Name()) default: - t.Errorf("resulting image differs from golden: %s", fi1.Name()) + c.Errorf("resulting image differs from golden: %s", fi1.Name()) } } diff --git a/resources/images/image.go b/resources/images/image.go index db7d566a7..4cbdc895f 100644 --- a/resources/images/image.go +++ b/resources/images/image.go @@ -17,6 +17,7 @@ import ( "fmt" "image" "image/color" + "image/draw" "image/gif" "image/jpeg" "image/png" @@ -236,7 +237,18 @@ func (p *ImageProcessor) ApplyFiltersFromConfig(src image.Image, conf ImageConfi func (p *ImageProcessor) Filter(src image.Image, filters ...gift.Filter) (image.Image, error) { g := gift.New(filters...) - dst := image.NewRGBA(g.Bounds(src.Bounds())) + bounds := g.Bounds(src.Bounds()) + var dst draw.Image + switch src.(type) { + case *image.RGBA: + dst = image.NewRGBA(bounds) + case *image.NRGBA: + dst = image.NewNRGBA(bounds) + case *image.Gray: + dst = image.NewGray(bounds) + default: + dst = image.NewNRGBA(bounds) + } g.Draw(dst, src) return dst, nil } diff --git a/resources/images/webp/webp.go b/resources/images/webp/webp.go index d7443ff23..5393569da 100644 --- a/resources/images/webp/webp.go +++ b/resources/images/webp/webp.go @@ -28,3 +28,8 @@ import ( func Encode(w io.Writer, m image.Image, o webpoptions.EncodingOptions) error { return libwebp.Encode(w, m, o) } + +// Supports returns whether webp encoding is supported in this build. +func Supports() bool { + return true +} diff --git a/resources/images/webp/webp_notavailable.go b/resources/images/webp/webp_notavailable.go index 4209eb41a..0c03a1749 100644 --- a/resources/images/webp/webp_notavailable.go +++ b/resources/images/webp/webp_notavailable.go @@ -28,3 +28,8 @@ import ( func Encode(w io.Writer, m image.Image, o webpoptions.EncodingOptions) error { return herrors.ErrFeatureNotAvailable } + +// Supports returns whether webp encoding is supported in this build. +func Supports() bool { + return false +} diff --git a/resources/testdata/fuzzy-cirlcle.png b/resources/testdata/fuzzy-cirlcle.png new file mode 100644 index 000000000..95497d822 Binary files /dev/null and b/resources/testdata/fuzzy-cirlcle.png differ diff --git a/resources/testdata/golden/gohugoio24_huc57dd738f4724f4b341121e66fd85555_267952_300x200_fill_gaussian_smart1_2.png b/resources/testdata/golden/gohugoio24_huc57dd738f4724f4b341121e66fd85555_267952_300x200_fill_gaussian_smart1_2.png index 59ac93c1c..4ef633564 100644 Binary files a/resources/testdata/golden/gohugoio24_huc57dd738f4724f4b341121e66fd85555_267952_300x200_fill_gaussian_smart1_2.png and b/resources/testdata/golden/gohugoio24_huc57dd738f4724f4b341121e66fd85555_267952_300x200_fill_gaussian_smart1_2.png differ diff --git a/resources/testdata/golden/gohugoio24_huc57dd738f4724f4b341121e66fd85555_267952_3ad578dd67cd055b4382e4062918d0a2.png b/resources/testdata/golden/gohugoio24_huc57dd738f4724f4b341121e66fd85555_267952_3ad578dd67cd055b4382e4062918d0a2.png index 3a267fbdb..056648a74 100644 Binary files a/resources/testdata/golden/gohugoio24_huc57dd738f4724f4b341121e66fd85555_267952_3ad578dd67cd055b4382e4062918d0a2.png and b/resources/testdata/golden/gohugoio24_huc57dd738f4724f4b341121e66fd85555_267952_3ad578dd67cd055b4382e4062918d0a2.png differ diff --git a/resources/testdata/golden/gohugoio8_hu7f72c00afdf7634587afaa5eff2a25b2_73538_300x200_fill_gaussian_smart1_2.png b/resources/testdata/golden/gohugoio8_hu7f72c00afdf7634587afaa5eff2a25b2_73538_300x200_fill_gaussian_smart1_2.png index f62d093a0..162dc4ec9 100644 Binary files a/resources/testdata/golden/gohugoio8_hu7f72c00afdf7634587afaa5eff2a25b2_73538_300x200_fill_gaussian_smart1_2.png and b/resources/testdata/golden/gohugoio8_hu7f72c00afdf7634587afaa5eff2a25b2_73538_300x200_fill_gaussian_smart1_2.png differ diff --git a/resources/testdata/golden/gohugoio8_hu7f72c00afdf7634587afaa5eff2a25b2_73538_8544b956dc08b714975ae52d4dcfdd78.png b/resources/testdata/golden/gohugoio8_hu7f72c00afdf7634587afaa5eff2a25b2_73538_8544b956dc08b714975ae52d4dcfdd78.png index 5a27e2fad..0b914391c 100644 Binary files a/resources/testdata/golden/gohugoio8_hu7f72c00afdf7634587afaa5eff2a25b2_73538_8544b956dc08b714975ae52d4dcfdd78.png and b/resources/testdata/golden/gohugoio8_hu7f72c00afdf7634587afaa5eff2a25b2_73538_8544b956dc08b714975ae52d4dcfdd78.png differ diff --git a/resources/testdata/golden/gohugoio8_hu7f72c00afdf7634587afaa5eff2a25b2_73538_b4afd8d32218a87ed1f7e351368501c3.png b/resources/testdata/golden/gohugoio8_hu7f72c00afdf7634587afaa5eff2a25b2_73538_b4afd8d32218a87ed1f7e351368501c3.png index 86896ef01..09d991972 100644 Binary files a/resources/testdata/golden/gohugoio8_hu7f72c00afdf7634587afaa5eff2a25b2_73538_b4afd8d32218a87ed1f7e351368501c3.png and b/resources/testdata/golden/gohugoio8_hu7f72c00afdf7634587afaa5eff2a25b2_73538_b4afd8d32218a87ed1f7e351368501c3.png differ diff --git a/resources/testdata/golden/gopher-hero8_huaa0cd7d2cfc14ff32a57f171896f2285_13327_200x0_resize_bge3e615_box_2.png b/resources/testdata/golden/gopher-hero8_huaa0cd7d2cfc14ff32a57f171896f2285_13327_200x0_resize_bge3e615_box_2.png index 830ee906b..50c55c9eb 100644 Binary files a/resources/testdata/golden/gopher-hero8_huaa0cd7d2cfc14ff32a57f171896f2285_13327_200x0_resize_bge3e615_box_2.png and b/resources/testdata/golden/gopher-hero8_huaa0cd7d2cfc14ff32a57f171896f2285_13327_200x0_resize_bge3e615_box_2.png differ diff --git a/resources/testdata/golden/gopher-hero8_huaa0cd7d2cfc14ff32a57f171896f2285_13327_200x0_resize_q75_bge3e615_box_2.jpg b/resources/testdata/golden/gopher-hero8_huaa0cd7d2cfc14ff32a57f171896f2285_13327_200x0_resize_q75_bge3e615_box_2.jpg index 4ae6f5173..17fca6e6a 100644 Binary files a/resources/testdata/golden/gopher-hero8_huaa0cd7d2cfc14ff32a57f171896f2285_13327_200x0_resize_q75_bge3e615_box_2.jpg and b/resources/testdata/golden/gopher-hero8_huaa0cd7d2cfc14ff32a57f171896f2285_13327_200x0_resize_q75_bge3e615_box_2.jpg differ diff --git a/resources/testdata/golden/gopher-hero8_huaa0cd7d2cfc14ff32a57f171896f2285_13327_30x0_resize_box_2.png b/resources/testdata/golden/gopher-hero8_huaa0cd7d2cfc14ff32a57f171896f2285_13327_30x0_resize_box_2.png index 5b8748351..eb9f1170c 100644 Binary files a/resources/testdata/golden/gopher-hero8_huaa0cd7d2cfc14ff32a57f171896f2285_13327_30x0_resize_box_2.png and b/resources/testdata/golden/gopher-hero8_huaa0cd7d2cfc14ff32a57f171896f2285_13327_30x0_resize_box_2.png differ diff --git a/resources/testdata/golden/gradient-circle_huf3d35257a40a8d6f525263a856c5ecfd_20069_200x0_resize_bge3e615_box_2.png b/resources/testdata/golden/gradient-circle_huf3d35257a40a8d6f525263a856c5ecfd_20069_200x0_resize_bge3e615_box_2.png index 3c861e922..b01efee50 100644 Binary files a/resources/testdata/golden/gradient-circle_huf3d35257a40a8d6f525263a856c5ecfd_20069_200x0_resize_bge3e615_box_2.png and b/resources/testdata/golden/gradient-circle_huf3d35257a40a8d6f525263a856c5ecfd_20069_200x0_resize_bge3e615_box_2.png differ diff --git a/resources/testdata/golden/gradient-circle_huf3d35257a40a8d6f525263a856c5ecfd_20069_200x0_resize_q75_bge3e615_box_2.jpg b/resources/testdata/golden/gradient-circle_huf3d35257a40a8d6f525263a856c5ecfd_20069_200x0_resize_q75_bge3e615_box_2.jpg index beb80bb12..56642d7e1 100644 Binary files a/resources/testdata/golden/gradient-circle_huf3d35257a40a8d6f525263a856c5ecfd_20069_200x0_resize_q75_bge3e615_box_2.jpg and b/resources/testdata/golden/gradient-circle_huf3d35257a40a8d6f525263a856c5ecfd_20069_200x0_resize_q75_bge3e615_box_2.jpg differ diff --git a/resources/testdata/golden/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_da536a2a5436e387d0d482675e08ad48.jpg b/resources/testdata/golden/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_da536a2a5436e387d0d482675e08ad48.jpg index 15f2ef1c5..1857f8758 100644 Binary files a/resources/testdata/golden/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_da536a2a5436e387d0d482675e08ad48.jpg and b/resources/testdata/golden/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_da536a2a5436e387d0d482675e08ad48.jpg differ diff --git a/resources/testdata/golden_webp/fuzzy-cirlcle_hu525d1a6cf670e85f5e8f19890241399b_26792_200x0_resize_q75_h2_box_2.webp b/resources/testdata/golden_webp/fuzzy-cirlcle_hu525d1a6cf670e85f5e8f19890241399b_26792_200x0_resize_q75_h2_box_2.webp new file mode 100644 index 000000000..0b9e6752a Binary files /dev/null and b/resources/testdata/golden_webp/fuzzy-cirlcle_hu525d1a6cf670e85f5e8f19890241399b_26792_200x0_resize_q75_h2_box_2.webp differ