| 1 |
// Copyright 2019 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package images |
| 15 |
|
| 16 |
import ( |
| 17 |
"fmt" |
| 18 |
"strings" |
| 19 |
"testing" |
| 20 |
|
| 21 |
qt "github.com/frankban/quicktest" |
| 22 |
) |
| 23 |
|
| 24 |
func TestDecodeConfig(t *testing.T) { |
| 25 |
c := qt.New(t) |
| 26 |
m := map[string]any{ |
| 27 |
"quality": 42, |
| 28 |
"resampleFilter": "NearestNeighbor", |
| 29 |
"anchor": "topLeft", |
| 30 |
} |
| 31 |
|
| 32 |
imagingConfig, err := DecodeConfig(m) |
| 33 |
|
| 34 |
c.Assert(err, qt.IsNil) |
| 35 |
conf := imagingConfig.Config |
| 36 |
c.Assert(conf.Imaging.Quality, qt.Equals, 42) |
| 37 |
c.Assert(conf.Imaging.ResampleFilter, qt.Equals, "nearestneighbor") |
| 38 |
c.Assert(conf.Imaging.Anchor, qt.Equals, "topleft") |
| 39 |
|
| 40 |
m = map[string]any{} |
| 41 |
|
| 42 |
imagingConfig, err = DecodeConfig(m) |
| 43 |
c.Assert(err, qt.IsNil) |
| 44 |
conf = imagingConfig.Config |
| 45 |
c.Assert(conf.Imaging.ResampleFilter, qt.Equals, "box") |
| 46 |
c.Assert(conf.Imaging.Anchor, qt.Equals, "smart") |
| 47 |
|
| 48 |
_, err = DecodeConfig(map[string]any{ |
| 49 |
"quality": 123, |
| 50 |
}) |
| 51 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 52 |
|
| 53 |
_, err = DecodeConfig(map[string]any{ |
| 54 |
"resampleFilter": "asdf", |
| 55 |
}) |
| 56 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 57 |
|
| 58 |
_, err = DecodeConfig(map[string]any{ |
| 59 |
"anchor": "asdf", |
| 60 |
}) |
| 61 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 62 |
|
| 63 |
imagingConfig, err = DecodeConfig(map[string]any{ |
| 64 |
"anchor": "Smart", |
| 65 |
}) |
| 66 |
conf = imagingConfig.Config |
| 67 |
c.Assert(err, qt.IsNil) |
| 68 |
c.Assert(conf.Imaging.Anchor, qt.Equals, "smart") |
| 69 |
|
| 70 |
imagingConfig, err = DecodeConfig(map[string]any{ |
| 71 |
"exif": map[string]any{ |
| 72 |
"disableLatLong": true, |
| 73 |
}, |
| 74 |
}) |
| 75 |
c.Assert(err, qt.IsNil) |
| 76 |
conf = imagingConfig.Config |
| 77 |
c.Assert(conf.Imaging.Exif.DisableLatLong, qt.Equals, true) |
| 78 |
c.Assert(conf.Imaging.Exif.ExcludeFields, qt.Equals, "GPS|Exif|Exposure[M|P|B]|Contrast|Resolution|Sharp|JPEG|Metering|Sensing|Saturation|ColorSpace|Flash|WhiteBalance") |
| 79 |
|
| 80 |
// AVIF: default is speed 10. |
| 81 |
imagingConfig, err = DecodeConfig(map[string]any{}) |
| 82 |
c.Assert(err, qt.IsNil) |
| 83 |
c.Assert(imagingConfig.Config.Imaging.Avif.EncoderSpeed, qt.Equals, defaultAvifEncoderSpeed) |
| 84 |
|
| 85 |
// AVIF: override via config. |
| 86 |
imagingConfig, err = DecodeConfig(map[string]any{ |
| 87 |
"avif": map[string]any{"encoderSpeed": 5}, |
| 88 |
}) |
| 89 |
c.Assert(err, qt.IsNil) |
| 90 |
c.Assert(imagingConfig.Config.Imaging.Avif.EncoderSpeed, qt.Equals, 5) |
| 91 |
|
| 92 |
// AVIF: out-of-range rejected. |
| 93 |
_, err = DecodeConfig(map[string]any{ |
| 94 |
"avif": map[string]any{"encoderSpeed": 11}, |
| 95 |
}) |
| 96 |
c.Assert(err, qt.ErrorMatches, ".*encoderSpeed must be between.*") |
| 97 |
|
| 98 |
imagingConfig, err = DecodeConfig(map[string]any{ |
| 99 |
"avif": map[string]any{"encoderSpeed": 1}, |
| 100 |
}) |
| 101 |
c.Assert(err, qt.IsNil) |
| 102 |
c.Assert(imagingConfig.Config.Imaging.Avif.EncoderSpeed, qt.Equals, 1) |
| 103 |
} |
| 104 |
|
| 105 |
func TestImageConfigPerFormat(t *testing.T) { |
| 106 |
c := qt.New(t) |
| 107 |
|
| 108 |
cfg, err := DecodeConfig(map[string]any{ |
| 109 |
"quality": 80, |
| 110 |
"compression": "lossy", |
| 111 |
"hint": "text", |
| 112 |
"jpeg": map[string]any{"quality": 80}, |
| 113 |
"webp": map[string]any{"quality": 70, "hint": "picture", "compression": "lossless"}, |
| 114 |
"avif": map[string]any{"quality": 55}, |
| 115 |
}) |
| 116 |
c.Assert(err, qt.IsNil) |
| 117 |
|
| 118 |
conf := func(opts ...string) ImageConfig { |
| 119 |
conf, err := DecodeImageConfig(append([]string{"resize", "100x"}, opts...), cfg, JPEG) |
| 120 |
c.Assert(err, qt.IsNil) |
| 121 |
return conf |
| 122 |
} |
| 123 |
|
| 124 |
c.Assert(conf("jpg").Quality, qt.Equals, 80) |
| 125 |
c.Assert(conf("avif").Quality, qt.Equals, 55) |
| 126 |
c.Assert(conf("webp").Quality, qt.Equals, 70) |
| 127 |
|
| 128 |
c.Assert(conf("webp", "q33").Quality, qt.Equals, 33) |
| 129 |
c.Assert(conf("avif", "q33").Quality, qt.Equals, 33) |
| 130 |
|
| 131 |
c.Assert(conf("webp").Hint, qt.Equals, "picture") |
| 132 |
c.Assert(conf("avif").Hint, qt.Equals, "text") |
| 133 |
c.Assert(conf("jpeg").Hint, qt.Equals, "") |
| 134 |
|
| 135 |
c.Assert(conf("webp").Compression, qt.Equals, "lossless") |
| 136 |
c.Assert(conf("avif").Compression, qt.Equals, "lossy") |
| 137 |
c.Assert(conf("jpeg").Compression, qt.Equals, "") |
| 138 |
} |
| 139 |
|
| 140 |
func TestDecodeImageConfig(t *testing.T) { |
| 141 |
c := qt.New(t) |
| 142 |
|
| 143 |
for i, this := range []struct { |
| 144 |
action string |
| 145 |
in string |
| 146 |
expect any |
| 147 |
}{ |
| 148 |
{"resize", "300x400", newTestImageConfig("resize", 300, 400, 75, 0, "box", "smart", "")}, |
| 149 |
{"resize", "300x400 #fff", newTestImageConfig("resize", 300, 400, 75, 0, "box", "smart", "fff")}, |
| 150 |
{"resize", "100x200 bottomRight", newTestImageConfig("resize", 100, 200, 75, 0, "box", "BottomRight", "")}, |
| 151 |
{"resize", "10x20 topleft Lanczos", newTestImageConfig("resize", 10, 20, 75, 0, "Lanczos", "topleft", "")}, |
| 152 |
{"resize", "linear left 10x r180", newTestImageConfig("resize", 10, 0, 75, 180, "linear", "left", "")}, |
| 153 |
{"resize", "x20 riGht Cosine q95", newTestImageConfig("resize", 0, 20, 95, 0, "cosine", "right", "")}, |
| 154 |
{"crop", "300x400", newTestImageConfig("crop", 300, 400, 75, 0, "box", "smart", "")}, |
| 155 |
{"fill", "300x400", newTestImageConfig("fill", 300, 400, 75, 0, "box", "smart", "")}, |
| 156 |
{"fit", "300x400", newTestImageConfig("fit", 300, 400, 75, 0, "box", "smart", "")}, |
| 157 |
|
| 158 |
{"resize", "", false}, |
| 159 |
{"resize", "foo", false}, |
| 160 |
{"crop", "100x", false}, |
| 161 |
{"fill", "100x", false}, |
| 162 |
{"fit", "100x", false}, |
| 163 |
{"foo", "100x", false}, |
| 164 |
} { |
| 165 |
|
| 166 |
cfg, err := DecodeConfig(nil) |
| 167 |
if err != nil { |
| 168 |
t.Fatal(err) |
| 169 |
} |
| 170 |
options := append([]string{this.action}, strings.Fields(this.in)...) |
| 171 |
result, err := DecodeImageConfig(options, cfg, WEBP) |
| 172 |
if b, ok := this.expect.(bool); ok && !b { |
| 173 |
if err == nil { |
| 174 |
t.Errorf("[%d] parseImageConfig didn't return an expected error", i) |
| 175 |
} |
| 176 |
} else { |
| 177 |
if err != nil { |
| 178 |
t.Fatalf("[%d] err: %s", i, err) |
| 179 |
} |
| 180 |
expect := this.expect.(ImageConfig) |
| 181 |
result.Key = "" |
| 182 |
|
| 183 |
c.Assert(fmt.Sprint(result), qt.Equals, fmt.Sprint(expect)) |
| 184 |
|
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
func newTestImageConfig(action string, width, height, quality, rotate int, filter, anchor, bgColor string) ImageConfig { |
| 190 |
var c ImageConfig = newImageConfig() |
| 191 |
c.Action = action |
| 192 |
c.TargetFormat = WEBP |
| 193 |
c.Hint = defaultHint |
| 194 |
c.Compression = defaultCompression |
| 195 |
c.Width = width |
| 196 |
c.Height = height |
| 197 |
c.Quality = quality |
| 198 |
c.Rotate = rotate |
| 199 |
c.BgColor, _ = hexStringToColorGo(bgColor) |
| 200 |
c.Anchor = SmartCropAnchor |
| 201 |
c.Method = defaultWebpMethod |
| 202 |
|
| 203 |
if filter != "" { |
| 204 |
filter = strings.ToLower(filter) |
| 205 |
if v, ok := imageFilters[filter]; ok { |
| 206 |
c.Filter = v |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
if anchor != "" { |
| 211 |
anchor = strings.ToLower(anchor) |
| 212 |
if v, ok := anchorPositions[anchor]; ok { |
| 213 |
c.Anchor = v |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
return c |
| 218 |
} |