| 1 |
package gift |
| 2 |
|
| 3 |
import ( |
| 4 |
"image" |
| 5 |
"image/color" |
| 6 |
"image/draw" |
| 7 |
"math" |
| 8 |
"testing" |
| 9 |
) |
| 10 |
|
| 11 |
func TestLut(t *testing.T) { |
| 12 |
fn := func(v float32) float32 { |
| 13 |
return v |
| 14 |
} |
| 15 |
for _, size := range []int{10, 100, 1000} { |
| 16 |
lut := prepareLut(size, fn) |
| 17 |
l := len(lut) |
| 18 |
if l != size { |
| 19 |
t.Errorf("LUT bad size: expected %v got %v", size, l) |
| 20 |
} |
| 21 |
if lut[0] != 0 { |
| 22 |
t.Errorf("LUT bad start value: expected 0 got %v", lut[0]) |
| 23 |
} |
| 24 |
if lut[l-1] != 1 { |
| 25 |
t.Errorf("LUT bad end value: expected 1 got %v", lut[l-1]) |
| 26 |
} |
| 27 |
} |
| 28 |
lut := prepareLut(10000, fn) |
| 29 |
for _, u := range []float32{0, 0.0001, 0.5555, 0.9999, 1} { |
| 30 |
v := getFromLut(lut, u) |
| 31 |
if math.Abs(float64(v-u)) > 0.0001 { |
| 32 |
t.Errorf("LUT bad value: expected %v got %v", u, v) |
| 33 |
} |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
func TestInvert(t *testing.T) { |
| 38 |
src := image.NewGray(image.Rect(0, 0, 256, 1)) |
| 39 |
for i := 0; i <= 255; i++ { |
| 40 |
src.Pix[i] = uint8(i) |
| 41 |
} |
| 42 |
g := New(Invert()) |
| 43 |
dst := image.NewGray(g.Bounds(src.Bounds())) |
| 44 |
g.Draw(dst, src) |
| 45 |
|
| 46 |
for i := 0; i <= 255; i++ { |
| 47 |
if dst.Pix[i] != 255-src.Pix[i] { |
| 48 |
t.Errorf("InvertColors: index %d: expected %d got %d", i, 255-src.Pix[i], dst.Pix[i]) |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
func TestColorspaceSRGBToLinear(t *testing.T) { |
| 54 |
vals := []float32{ |
| 55 |
0.00000, |
| 56 |
0.01002, |
| 57 |
0.03310, |
| 58 |
0.07324, |
| 59 |
0.13287, |
| 60 |
0.21404, |
| 61 |
0.31855, |
| 62 |
0.44799, |
| 63 |
0.60383, |
| 64 |
0.78741, |
| 65 |
1.00000, |
| 66 |
} |
| 67 |
|
| 68 |
imgs := []draw.Image{ |
| 69 |
image.NewGray(image.Rect(0, 0, 11, 11)), |
| 70 |
image.NewGray(image.Rect(0, 0, 111, 111)), |
| 71 |
image.NewGray16(image.Rect(0, 0, 11, 11)), |
| 72 |
image.NewGray16(image.Rect(0, 0, 1111, 1111)), |
| 73 |
} |
| 74 |
for _, img := range imgs { |
| 75 |
for i := 0; i <= 10; i++ { |
| 76 |
img.Set(i, 0, color.Gray{uint8(255 * i / 10.0)}) |
| 77 |
} |
| 78 |
img2 := image.NewGray(img.Bounds()) |
| 79 |
New(ColorspaceSRGBToLinear()).Draw(img2, img) |
| 80 |
if !img2.Bounds().Size().Eq(img.Bounds().Size()) { |
| 81 |
t.Errorf("ColorspaceSRGBToLinear bad result size: expected %v got %v", img.Bounds().Size(), img2.Bounds().Size()) |
| 82 |
} |
| 83 |
for i := 0; i <= 10; i++ { |
| 84 |
expected := uint8(vals[i]*255 + 0.5) |
| 85 |
c := img2.At(i, 0).(color.Gray) |
| 86 |
if math.Abs(float64(c.Y)-float64(expected)) > 1 { |
| 87 |
t.Errorf("ColorspaceSRGBToLinear bad color value at index %v expected %v got %v", i, expected, c.Y) |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
func TestColorspaceLinearToSRGB(t *testing.T) { |
| 94 |
vals := []float32{ |
| 95 |
0.00000, |
| 96 |
0.34919, |
| 97 |
0.48453, |
| 98 |
0.58383, |
| 99 |
0.66519, |
| 100 |
0.73536, |
| 101 |
0.79774, |
| 102 |
0.85431, |
| 103 |
0.90633, |
| 104 |
0.95469, |
| 105 |
1.00000, |
| 106 |
} |
| 107 |
|
| 108 |
imgs := []draw.Image{ |
| 109 |
image.NewGray(image.Rect(0, 0, 11, 11)), |
| 110 |
image.NewGray(image.Rect(0, 0, 111, 111)), |
| 111 |
image.NewGray16(image.Rect(0, 0, 11, 11)), |
| 112 |
image.NewGray16(image.Rect(0, 0, 1111, 1111)), |
| 113 |
} |
| 114 |
for _, img := range imgs { |
| 115 |
for i := 0; i <= 10; i++ { |
| 116 |
img.Set(i, 0, color.Gray{uint8(255 * i / 10.0)}) |
| 117 |
} |
| 118 |
img2 := image.NewGray(img.Bounds()) |
| 119 |
New(ColorspaceLinearToSRGB()).Draw(img2, img) |
| 120 |
if !img2.Bounds().Size().Eq(img.Bounds().Size()) { |
| 121 |
t.Errorf("ColorspaceLinearRGBToSRGB bad result size: expected %v got %v", img.Bounds().Size(), img2.Bounds().Size()) |
| 122 |
} |
| 123 |
for i := 0; i <= 10; i++ { |
| 124 |
expected := uint8(vals[i]*255 + 0.5) |
| 125 |
c := img2.At(i, 0).(color.Gray) |
| 126 |
if math.Abs(float64(c.Y)-float64(expected)) > 1 { |
| 127 |
t.Errorf("ColorspaceLinearRGBToSRGB bad color value at index %v expected %v got %v", i, expected, c.Y) |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
func TestAdjustGamma(t *testing.T) { |
| 134 |
src := image.NewGray(image.Rect(0, 0, 256, 1)) |
| 135 |
dst := image.NewGray(image.Rect(0, 0, 256, 1)) |
| 136 |
for i := 0; i <= 255; i++ { |
| 137 |
src.Pix[i] = uint8(i) |
| 138 |
} |
| 139 |
ag := Gamma(2) |
| 140 |
ag.Draw(dst, src, nil) |
| 141 |
|
| 142 |
for i := 100; i <= 150; i++ { |
| 143 |
if dst.Pix[i] <= src.Pix[i] { |
| 144 |
t.Errorf("Gamma unexpected color") |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
ag = Gamma(0.5) |
| 149 |
ag.Draw(dst, src, nil) |
| 150 |
|
| 151 |
for i := 100; i <= 150; i++ { |
| 152 |
if dst.Pix[i] >= src.Pix[i] { |
| 153 |
t.Errorf("Gamma unexpected color") |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
ag = Gamma(1) |
| 158 |
ag.Draw(dst, src, nil) |
| 159 |
|
| 160 |
for i := 100; i <= 150; i++ { |
| 161 |
if dst.Pix[i] != src.Pix[i] { |
| 162 |
t.Errorf("Gamma unexpected color") |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
func TestContrast(t *testing.T) { |
| 168 |
testData := []struct { |
| 169 |
desc string |
| 170 |
p float32 |
| 171 |
srcb, dstb image.Rectangle |
| 172 |
srcPix, dstPix []uint8 |
| 173 |
}{ |
| 174 |
{ |
| 175 |
"contrast (0)", |
| 176 |
0, |
| 177 |
image.Rect(-1, -1, 4, 2), |
| 178 |
image.Rect(0, 0, 5, 3), |
| 179 |
[]uint8{ |
| 180 |
0x00, 0x40, 0x00, 0x40, 0x00, |
| 181 |
0x60, 0xB0, 0xA0, 0xB0, 0x60, |
| 182 |
0x00, 0x80, 0x00, 0x80, 0x00, |
| 183 |
}, |
| 184 |
[]uint8{ |
| 185 |
0x00, 0x40, 0x00, 0x40, 0x00, |
| 186 |
0x60, 0xB0, 0xA0, 0xB0, 0x60, |
| 187 |
0x00, 0x80, 0x00, 0x80, 0x00, |
| 188 |
}, |
| 189 |
}, |
| 190 |
{ |
| 191 |
"contrast (30)", |
| 192 |
30, |
| 193 |
image.Rect(-1, -1, 4, 2), |
| 194 |
image.Rect(0, 0, 5, 3), |
| 195 |
[]uint8{ |
| 196 |
0x00, 0x40, 0x00, 0x40, 0x00, |
| 197 |
0x60, 0xB0, 0xA0, 0xB0, 0x60, |
| 198 |
0x00, 0x80, 0x00, 0x80, 0x00, |
| 199 |
}, |
| 200 |
[]uint8{ |
| 201 |
0x00, 0x25, 0x00, 0x25, 0x00, |
| 202 |
0x53, 0xC5, 0xAE, 0xC5, 0x53, |
| 203 |
0x00, 0x80, 0x00, 0x80, 0x00, |
| 204 |
}, |
| 205 |
}, |
| 206 |
{ |
| 207 |
"contrast (-30)", |
| 208 |
-30, |
| 209 |
image.Rect(-1, -1, 4, 2), |
| 210 |
image.Rect(0, 0, 5, 3), |
| 211 |
[]uint8{ |
| 212 |
0x00, 0x40, 0x00, 0x40, 0x00, |
| 213 |
0x60, 0xB0, 0xA0, 0xB0, 0x60, |
| 214 |
0x00, 0x80, 0x00, 0x80, 0x00, |
| 215 |
}, |
| 216 |
[]uint8{ |
| 217 |
0x26, 0x53, 0x26, 0x53, 0x26, |
| 218 |
0x69, 0xA1, 0x96, 0xA1, 0x69, |
| 219 |
0x26, 0x80, 0x26, 0x80, 0x26, |
| 220 |
}, |
| 221 |
}, |
| 222 |
{ |
| 223 |
"contrast (100)", |
| 224 |
100, |
| 225 |
image.Rect(-1, -1, 4, 2), |
| 226 |
image.Rect(0, 0, 5, 3), |
| 227 |
[]uint8{ |
| 228 |
0x00, 0x40, 0x00, 0x40, 0x00, |
| 229 |
0x60, 0xB0, 0xA0, 0xB0, 0x60, |
| 230 |
0x00, 0x80, 0x00, 0x80, 0x00, |
| 231 |
}, |
| 232 |
[]uint8{ |
| 233 |
0x00, 0x00, 0x00, 0x00, 0x00, |
| 234 |
0x00, 0xFF, 0xFF, 0xFF, 0x00, |
| 235 |
0x00, 0xFF, 0x00, 0xFF, 0x00, |
| 236 |
}, |
| 237 |
}, |
| 238 |
{ |
| 239 |
"contrast (200)", |
| 240 |
200, |
| 241 |
image.Rect(-1, -1, 4, 2), |
| 242 |
image.Rect(0, 0, 5, 3), |
| 243 |
[]uint8{ |
| 244 |
0x00, 0x40, 0x00, 0x40, 0x00, |
| 245 |
0x60, 0xB0, 0xA0, 0xB0, 0x60, |
| 246 |
0x00, 0x80, 0x00, 0x80, 0x00, |
| 247 |
}, |
| 248 |
[]uint8{ |
| 249 |
0x00, 0x00, 0x00, 0x00, 0x00, |
| 250 |
0x00, 0xFF, 0xFF, 0xFF, 0x00, |
| 251 |
0x00, 0xFF, 0x00, 0xFF, 0x00, |
| 252 |
}, |
| 253 |
}, |
| 254 |
{ |
| 255 |
"contrast (-100)", |
| 256 |
-100, |
| 257 |
image.Rect(-1, -1, 4, 2), |
| 258 |
image.Rect(0, 0, 5, 3), |
| 259 |
[]uint8{ |
| 260 |
0x00, 0x40, 0x00, 0x40, 0x00, |
| 261 |
0x60, 0xB0, 0xA0, 0xB0, 0x60, |
| 262 |
0x00, 0x80, 0x00, 0x80, 0x00, |
| 263 |
}, |
| 264 |
[]uint8{ |
| 265 |
0x80, 0x80, 0x80, 0x80, 0x80, |
| 266 |
0x80, 0x80, 0x80, 0x80, 0x80, |
| 267 |
0x80, 0x80, 0x80, 0x80, 0x80, |
| 268 |
}, |
| 269 |
}, |
| 270 |
{ |
| 271 |
"contrast (-200)", |
| 272 |
-200, |
| 273 |
image.Rect(-1, -1, 4, 2), |
| 274 |
image.Rect(0, 0, 5, 3), |
| 275 |
[]uint8{ |
| 276 |
0x00, 0x40, 0x00, 0x40, 0x00, |
| 277 |
0x60, 0xB0, 0xA0, 0xB0, 0x60, |
| 278 |
0x00, 0x80, 0x00, 0x80, 0x00, |
| 279 |
}, |
| 280 |
[]uint8{ |
| 281 |
0x80, 0x80, 0x80, 0x80, 0x80, |
| 282 |
0x80, 0x80, 0x80, 0x80, 0x80, |
| 283 |
0x80, 0x80, 0x80, 0x80, 0x80, |
| 284 |
}, |
| 285 |
}, |
| 286 |
} |
| 287 |
|
| 288 |
for _, d := range testData { |
| 289 |
src := image.NewGray(d.srcb) |
| 290 |
src.Pix = d.srcPix |
| 291 |
|
| 292 |
f := Contrast(d.p) |
| 293 |
dst := image.NewGray(f.Bounds(src.Bounds())) |
| 294 |
f.Draw(dst, src, nil) |
| 295 |
|
| 296 |
if !checkBoundsAndPix(dst.Bounds(), d.dstb, dst.Pix, d.dstPix) { |
| 297 |
t.Errorf("test [%s] failed: %#v, %#v", d.desc, dst.Bounds(), dst.Pix) |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
func TestBrightness(t *testing.T) { |
| 303 |
testData := []struct { |
| 304 |
desc string |
| 305 |
p float32 |
| 306 |
srcb, dstb image.Rectangle |
| 307 |
srcPix, dstPix []uint8 |
| 308 |
}{ |
| 309 |
{ |
| 310 |
"brightness (0)", |
| 311 |
0, |
| 312 |
image.Rect(-1, -1, 4, 2), |
| 313 |
image.Rect(0, 0, 5, 3), |
| 314 |
[]uint8{ |
| 315 |
0x00, 0x40, 0x00, 0x40, 0x00, |
| 316 |
0x60, 0xB0, 0xA0, 0xB0, 0x60, |
| 317 |
0x00, 0x80, 0x00, 0x80, 0x00, |
| 318 |
}, |
| 319 |
[]uint8{ |
| 320 |
0x00, 0x40, 0x00, 0x40, 0x00, |
| 321 |
0x60, 0xB0, 0xA0, 0xB0, 0x60, |
| 322 |
0x00, 0x80, 0x00, 0x80, 0x00, |
| 323 |
}, |
| 324 |
}, |
| 325 |
{ |
| 326 |
"brightness (30)", |
| 327 |
30, |
| 328 |
image.Rect(-1, -1, 4, 2), |
| 329 |
image.Rect(0, 0, 5, 3), |
| 330 |
[]uint8{ |
| 331 |
0x00, 0x40, 0x00, 0x40, 0x00, |
| 332 |
0x60, 0xB0, 0xA0, 0xB0, 0x60, |
| 333 |
0x00, 0x80, 0x00, 0x80, 0x00, |
| 334 |
}, |
| 335 |
[]uint8{ |
| 336 |
0x4D, 0x8D, 0x4D, 0x8D, 0x4D, |
| 337 |
0xAD, 0xFD, 0xED, 0xFD, 0xAD, |
| 338 |
0x4D, 0xCD, 0x4D, 0xCD, 0x4D, |
| 339 |
}, |
| 340 |
}, |
| 341 |
{ |
| 342 |
"brightness (-30)", |
| 343 |
-30, |
| 344 |
image.Rect(-1, -1, 4, 2), |
| 345 |
image.Rect(0, 0, 5, 3), |
| 346 |
[]uint8{ |
| 347 |
0x00, 0x40, 0x00, 0x40, 0x00, |
| 348 |
0x60, 0xB0, 0xA1, 0xB0, 0x60, |
| 349 |
0x00, 0x80, 0x00, 0x80, 0x00, |
| 350 |
}, |
| 351 |
[]uint8{ |
| 352 |
0x00, 0x00, 0x00, 0x00, 0x00, |
| 353 |
0x14, 0x64, 0x55, 0x64, 0x14, |
| 354 |
0x00, 0x34, 0x00, 0x34, 0x00, |
| 355 |
}, |
| 356 |
}, |
| 357 |
{ |
| 358 |
"brightness (100)", |
| 359 |
100, |
| 360 |
image.Rect(-1, -1, 4, 2), |
| 361 |
image.Rect(0, 0, 5, 3), |
| 362 |
[]uint8{ |
| 363 |
0x00, 0x40, 0x00, 0x40, 0x00, |
| 364 |
0x60, 0xB0, 0xA0, 0xB0, 0x60, |
| 365 |
0x00, 0x80, 0x00, 0x80, 0x00, |
| 366 |
}, |
| 367 |
[]uint8{ |
| 368 |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 369 |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 370 |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 371 |
}, |
| 372 |
}, |
| 373 |
{ |
| 374 |
"brightness (200)", |
| 375 |
200, |
| 376 |
image.Rect(-1, -1, 4, 2), |
| 377 |
image.Rect(0, 0, 5, 3), |
| 378 |
[]uint8{ |
| 379 |
0x00, 0x40, 0x00, 0x40, 0x00, |
| 380 |
0x60, 0xB0, 0xA0, 0xB0, 0x60, |
| 381 |
0x00, 0x80, 0x00, 0x80, 0x00, |
| 382 |
}, |
| 383 |
[]uint8{ |
| 384 |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 385 |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 386 |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 387 |
}, |
| 388 |
}, |
| 389 |
{ |
| 390 |
"brightness (-100)", |
| 391 |
-100, |
| 392 |
image.Rect(-1, -1, 4, 2), |
| 393 |
image.Rect(0, 0, 5, 3), |
| 394 |
[]uint8{ |
| 395 |
0x00, 0x40, 0x00, 0x40, 0x00, |
| 396 |
0x60, 0xB0, 0xA0, 0xB0, 0x60, |
| 397 |
0x00, 0x80, 0x00, 0x80, 0x00, |
| 398 |
}, |
| 399 |
[]uint8{ |
| 400 |
0x00, 0x00, 0x00, 0x00, 0x00, |
| 401 |
0x00, 0x00, 0x00, 0x00, 0x00, |
| 402 |
0x00, 0x00, 0x00, 0x00, 0x00, |
| 403 |
}, |
| 404 |
}, |
| 405 |
{ |
| 406 |
"brightness (-200)", |
| 407 |
-200, |
| 408 |
image.Rect(-1, -1, 4, 2), |
| 409 |
image.Rect(0, 0, 5, 3), |
| 410 |
[]uint8{ |
| 411 |
0x00, 0x40, 0x00, 0x40, 0x00, |
| 412 |
0x60, 0xB0, 0xA0, 0xB0, 0x60, |
| 413 |
0x00, 0x80, 0x00, 0x80, 0x00, |
| 414 |
}, |
| 415 |
[]uint8{ |
| 416 |
0x00, 0x00, 0x00, 0x00, 0x00, |
| 417 |
0x00, 0x00, 0x00, 0x00, 0x00, |
| 418 |
0x00, 0x00, 0x00, 0x00, 0x00, |
| 419 |
}, |
| 420 |
}, |
| 421 |
} |
| 422 |
|
| 423 |
for _, d := range testData { |
| 424 |
src := image.NewGray(d.srcb) |
| 425 |
src.Pix = d.srcPix |
| 426 |
|
| 427 |
f := Brightness(d.p) |
| 428 |
dst := image.NewGray(f.Bounds(src.Bounds())) |
| 429 |
f.Draw(dst, src, nil) |
| 430 |
|
| 431 |
if !checkBoundsAndPix(dst.Bounds(), d.dstb, dst.Pix, d.dstPix) { |
| 432 |
t.Errorf("test [%s] failed: %#v, %#v", d.desc, dst.Bounds(), dst.Pix) |
| 433 |
} |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
func TestSigmoid(t *testing.T) { |
| 438 |
testData := []struct { |
| 439 |
desc string |
| 440 |
midpoint, factor float32 |
| 441 |
srcb, dstb image.Rectangle |
| 442 |
srcPix, dstPix []uint8 |
| 443 |
}{ |
| 444 |
{ |
| 445 |
"sigmoid (0.5, 0)", |
| 446 |
0.5, 0, |
| 447 |
image.Rect(-1, -1, 4, 2), |
| 448 |
image.Rect(0, 0, 5, 3), |
| 449 |
[]uint8{ |
| 450 |
0x00, 0x40, 0x00, 0x40, 0x00, |
| 451 |
0x60, 0xB0, 0xA0, 0xB0, 0x60, |
| 452 |
0x00, 0x80, 0x00, 0x80, 0x00, |
| 453 |
}, |
| 454 |
[]uint8{ |
| 455 |
0x00, 0x40, 0x00, 0x40, 0x00, |
| 456 |
0x60, 0xB0, 0xA0, 0xB0, 0x60, |
| 457 |
0x00, 0x80, 0x00, 0x80, 0x00, |
| 458 |
}, |
| 459 |
}, |
| 460 |
{ |
| 461 |
"sigmoid (0.5, 3)", |
| 462 |
0.5, 3, |
| 463 |
image.Rect(-1, -1, 4, 2), |
| 464 |
image.Rect(0, 0, 5, 3), |
| 465 |
[]uint8{ |
| 466 |
0x00, 0x40, 0x00, 0x40, 0x00, |
| 467 |
0x60, 0xB0, 0xA0, 0xB0, 0x60, |
| 468 |
0x00, 0x80, 0x00, 0x80, 0x00, |
| 469 |
}, |
| 470 |
[]uint8{ |
| 471 |
0x00, 0x38, 0x00, 0x38, 0x00, |
| 472 |
0x5B, 0xB7, 0xA5, 0xB7, 0x5B, |
| 473 |
0x00, 0x80, 0x00, 0x80, 0x00, |
| 474 |
}, |
| 475 |
}, |
| 476 |
{ |
| 477 |
"sigmoid (0.5, -3)", |
| 478 |
0.5, -3, |
| 479 |
image.Rect(-1, -1, 4, 2), |
| 480 |
image.Rect(0, 0, 5, 3), |
| 481 |
[]uint8{ |
| 482 |
0x00, 0x40, 0x00, 0x40, 0x00, |
| 483 |
0x60, 0xB0, 0xA0, 0xB0, 0x60, |
| 484 |
0x00, 0x80, 0x00, 0x80, 0x00, |
| 485 |
}, |
| 486 |
[]uint8{ |
| 487 |
0x00, 0x48, 0x00, 0x48, 0x00, |
| 488 |
0x65, 0xA9, 0x9B, 0xA9, 0x65, |
| 489 |
0x00, 0x80, 0x00, 0x80, 0x00, |
| 490 |
}, |
| 491 |
}, |
| 492 |
} |
| 493 |
|
| 494 |
for _, d := range testData { |
| 495 |
src := image.NewGray(d.srcb) |
| 496 |
src.Pix = d.srcPix |
| 497 |
|
| 498 |
f := Sigmoid(d.midpoint, d.factor) |
| 499 |
dst := image.NewGray(f.Bounds(src.Bounds())) |
| 500 |
f.Draw(dst, src, nil) |
| 501 |
|
| 502 |
if !checkBoundsAndPix(dst.Bounds(), d.dstb, dst.Pix, d.dstPix) { |
| 503 |
t.Errorf("test [%s] failed: %#v, %#v", d.desc, dst.Bounds(), dst.Pix) |
| 504 |
} |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
func TestGrayscale(t *testing.T) { |
| 509 |
testData := []struct { |
| 510 |
desc string |
| 511 |
srcb, dstb image.Rectangle |
| 512 |
srcPix, dstPix []uint8 |
| 513 |
}{ |
| 514 |
{ |
| 515 |
"grayscale 0x0", |
| 516 |
image.Rect(0, 0, 0, 0), |
| 517 |
image.Rect(0, 0, 0, 0), |
| 518 |
[]uint8{}, |
| 519 |
[]uint8{}, |
| 520 |
}, |
| 521 |
{ |
| 522 |
"grayscale 2x3", |
| 523 |
image.Rect(-1, -1, 1, 2), |
| 524 |
image.Rect(0, 0, 2, 3), |
| 525 |
[]uint8{ |
| 526 |
0x00, 0x10, 0x20, 0x30, 0xFF, 0x00, 0x88, 0xFF, |
| 527 |
0xF0, 0xE0, 0xD0, 0xC0, 0x11, 0x66, 0xBB, 0x00, |
| 528 |
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, |
| 529 |
}, |
| 530 |
[]uint8{ |
| 531 |
0x0D, 0x0D, 0x0D, 0x30, 0x5C, 0x5C, 0x5C, 0xFF, |
| 532 |
0xE3, 0xE3, 0xE3, 0xC0, 0x56, 0x56, 0x56, 0x00, |
| 533 |
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, |
| 534 |
}, |
| 535 |
}, |
| 536 |
} |
| 537 |
|
| 538 |
for _, d := range testData { |
| 539 |
src := image.NewNRGBA(d.srcb) |
| 540 |
src.Pix = d.srcPix |
| 541 |
|
| 542 |
f := Grayscale() |
| 543 |
dst := image.NewNRGBA(f.Bounds(src.Bounds())) |
| 544 |
f.Draw(dst, src, nil) |
| 545 |
|
| 546 |
if !checkBoundsAndPix(dst.Bounds(), d.dstb, dst.Pix, d.dstPix) { |
| 547 |
t.Errorf("test [%s] failed: %#v, %#v", d.desc, dst.Bounds(), dst.Pix) |
| 548 |
} |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
func TestSepia(t *testing.T) { |
| 553 |
testData := []struct { |
| 554 |
desc string |
| 555 |
p float32 |
| 556 |
srcb, dstb image.Rectangle |
| 557 |
srcPix, dstPix []uint8 |
| 558 |
}{ |
| 559 |
{ |
| 560 |
"sepia 100 0x0", |
| 561 |
100, |
| 562 |
image.Rect(0, 0, 0, 0), |
| 563 |
image.Rect(0, 0, 0, 0), |
| 564 |
[]uint8{}, |
| 565 |
[]uint8{}, |
| 566 |
}, |
| 567 |
{ |
| 568 |
"sepia 0 2x3", |
| 569 |
0, |
| 570 |
image.Rect(-1, -1, 1, 2), |
| 571 |
image.Rect(0, 0, 2, 3), |
| 572 |
[]uint8{ |
| 573 |
0x00, 0x10, 0x20, 0x30, 0xFF, 0x00, 0x88, 0xFF, |
| 574 |
0xF0, 0xE0, 0xD0, 0xC0, 0x11, 0x66, 0xBB, 0x00, |
| 575 |
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, |
| 576 |
}, |
| 577 |
[]uint8{ |
| 578 |
0x00, 0x10, 0x20, 0x30, 0xFF, 0x00, 0x88, 0xFF, |
| 579 |
0xF0, 0xE0, 0xD0, 0xC0, 0x11, 0x66, 0xBB, 0x00, |
| 580 |
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, |
| 581 |
}, |
| 582 |
}, |
| 583 |
{ |
| 584 |
"sepia 100 2x3", |
| 585 |
100, |
| 586 |
image.Rect(-1, -1, 1, 2), |
| 587 |
image.Rect(0, 0, 2, 3), |
| 588 |
[]uint8{ |
| 589 |
0x00, 0x10, 0x20, 0x30, 0xFF, 0x00, 0x88, 0xFF, |
| 590 |
0xF0, 0xE0, 0xD0, 0xC0, 0x11, 0x66, 0xBB, 0x00, |
| 591 |
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, |
| 592 |
}, |
| 593 |
[]uint8{ |
| 594 |
0x12, 0x10, 0x0D, 0x30, 0x7E, 0x70, 0x57, 0xFF, |
| 595 |
0xFF, 0xFF, 0xD4, 0xC0, 0x78, 0x6B, 0x54, 0x00, |
| 596 |
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xEF, 0xFF, |
| 597 |
}, |
| 598 |
}, |
| 599 |
} |
| 600 |
|
| 601 |
for _, d := range testData { |
| 602 |
src := image.NewNRGBA(d.srcb) |
| 603 |
src.Pix = d.srcPix |
| 604 |
|
| 605 |
f := Sepia(d.p) |
| 606 |
dst := image.NewNRGBA(f.Bounds(src.Bounds())) |
| 607 |
f.Draw(dst, src, nil) |
| 608 |
|
| 609 |
if !checkBoundsAndPix(dst.Bounds(), d.dstb, dst.Pix, d.dstPix) { |
| 610 |
t.Errorf("test [%s] failed: %#v, %#v", d.desc, dst.Bounds(), dst.Pix) |
| 611 |
} |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
func TestNormalizeHue(t *testing.T) { |
| 616 |
testData := []struct { |
| 617 |
h0, h1 float32 |
| 618 |
}{ |
| 619 |
{0, 0}, |
| 620 |
{0.1, 0.1}, |
| 621 |
{0.5, 0.5}, |
| 622 |
{0.9, 0.9}, |
| 623 |
{1.1, 0.1}, |
| 624 |
{3.0, 0.0}, |
| 625 |
{5.7, 0.7}, |
| 626 |
{-0.1, 0.9}, |
| 627 |
{-0.5, 0.5}, |
| 628 |
{-0.9, 0.1}, |
| 629 |
{-3.0, 0.0}, |
| 630 |
{-5.7, 0.3}, |
| 631 |
} |
| 632 |
|
| 633 |
for _, d := range testData { |
| 634 |
h := normalizeHue(d.h0) |
| 635 |
if absf32(h-d.h1) > 0.00001 { |
| 636 |
t.Errorf("normalizeHue(%#v) failed: %#v expected: %#v", d.h0, h, d.h1) |
| 637 |
} |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
func TestHue(t *testing.T) { |
| 642 |
testData := []struct { |
| 643 |
desc string |
| 644 |
p float32 |
| 645 |
srcb, dstb image.Rectangle |
| 646 |
srcPix, dstPix []uint8 |
| 647 |
}{ |
| 648 |
{ |
| 649 |
"hue 0 0x0", |
| 650 |
0, |
| 651 |
image.Rect(0, 0, 0, 0), |
| 652 |
image.Rect(0, 0, 0, 0), |
| 653 |
[]uint8{}, |
| 654 |
[]uint8{}, |
| 655 |
}, |
| 656 |
{ |
| 657 |
"hue 0", |
| 658 |
0, |
| 659 |
image.Rect(-1, -1, 1, 3), |
| 660 |
image.Rect(0, 0, 2, 4), |
| 661 |
[]uint8{ |
| 662 |
0x00, 0x10, 0x20, 0x30, 0x99, 0x66, 0x33, 0xFF, |
| 663 |
0xF0, 0xE0, 0xD0, 0xC0, 0x11, 0x66, 0xBB, 0x00, |
| 664 |
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, |
| 665 |
0xEE, 0x50, 0xA0, 0x77, 0xFD, 0xFE, 0xFD, 0xFD, |
| 666 |
}, |
| 667 |
[]uint8{ |
| 668 |
0x00, 0x10, 0x20, 0x30, 0x99, 0x66, 0x33, 0xFF, |
| 669 |
0xF0, 0xE0, 0xD0, 0xC0, 0x11, 0x66, 0xBB, 0x00, |
| 670 |
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, |
| 671 |
0xEE, 0x50, 0xA0, 0x77, 0xFD, 0xFE, 0xFD, 0xFD, |
| 672 |
}, |
| 673 |
}, |
| 674 |
{ |
| 675 |
"hue -720", |
| 676 |
-720, |
| 677 |
image.Rect(-1, -1, 1, 3), |
| 678 |
image.Rect(0, 0, 2, 4), |
| 679 |
[]uint8{ |
| 680 |
0x00, 0x10, 0x20, 0x30, 0x99, 0x66, 0x33, 0xFF, |
| 681 |
0xF0, 0xE0, 0xD0, 0xC0, 0x11, 0x66, 0xBB, 0x00, |
| 682 |
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, |
| 683 |
0xEE, 0x50, 0xA0, 0x77, 0xFD, 0xFE, 0xFD, 0xFD, |
| 684 |
}, |
| 685 |
[]uint8{ |
| 686 |
0x00, 0x10, 0x20, 0x30, 0x99, 0x66, 0x33, 0xFF, |
| 687 |
0xF0, 0xE0, 0xD0, 0xC0, 0x11, 0x66, 0xBB, 0x00, |
| 688 |
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, |
| 689 |
0xEE, 0x50, 0xA0, 0x77, 0xFD, 0xFE, 0xFD, 0xFD, |
| 690 |
}, |
| 691 |
}, |
| 692 |
{ |
| 693 |
"hue -90", |
| 694 |
-90, |
| 695 |
image.Rect(-1, -1, 1, 3), |
| 696 |
image.Rect(0, 0, 2, 4), |
| 697 |
[]uint8{ |
| 698 |
0x00, 0x10, 0x20, 0x30, 0x99, 0x66, 0x33, 0xFF, |
| 699 |
0xF0, 0xE0, 0xD0, 0xC0, 0x11, 0x66, 0xBB, 0x00, |
| 700 |
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, |
| 701 |
0xEE, 0x50, 0xA0, 0x77, 0xFD, 0xFE, 0xFD, 0xFD, |
| 702 |
}, |
| 703 |
[]uint8{ |
| 704 |
0x00, 0x20, 0x00, 0x30, 0x99, 0x33, 0x99, 0xFF, |
| 705 |
0xF0, 0xD0, 0xF0, 0xC0, 0x11, 0xBB, 0x11, 0x00, |
| 706 |
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, |
| 707 |
0x50, 0x51, 0xEE, 0x77, 0xFE, 0xFE, 0xFD, 0xFD, |
| 708 |
}, |
| 709 |
}, |
| 710 |
{ |
| 711 |
"hue 630", |
| 712 |
630, |
| 713 |
image.Rect(-1, -1, 1, 3), |
| 714 |
image.Rect(0, 0, 2, 4), |
| 715 |
[]uint8{ |
| 716 |
0x00, 0x10, 0x20, 0x30, 0x99, 0x66, 0x33, 0xFF, |
| 717 |
0xF0, 0xE0, 0xD0, 0xC0, 0x11, 0x66, 0xBB, 0x00, |
| 718 |
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, |
| 719 |
0xEE, 0x50, 0xA0, 0x77, 0xFD, 0xFE, 0xFD, 0xFD, |
| 720 |
}, |
| 721 |
[]uint8{ |
| 722 |
0x00, 0x20, 0x00, 0x30, 0x99, 0x33, 0x99, 0xFF, |
| 723 |
0xF0, 0xD0, 0xF0, 0xC0, 0x11, 0xBB, 0x11, 0x00, |
| 724 |
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, |
| 725 |
0x50, 0x51, 0xEE, 0x77, 0xFE, 0xFE, 0xFD, 0xFD, |
| 726 |
}, |
| 727 |
}, |
| 728 |
{ |
| 729 |
"hue 90", |
| 730 |
90, |
| 731 |
image.Rect(-1, -1, 1, 3), |
| 732 |
image.Rect(0, 0, 2, 4), |
| 733 |
[]uint8{ |
| 734 |
0x00, 0x10, 0x20, 0x30, 0x99, 0x66, 0x33, 0xFF, |
| 735 |
0xF0, 0xE0, 0xD0, 0xC0, 0x11, 0x66, 0xBB, 0x00, |
| 736 |
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, |
| 737 |
0xEE, 0x50, 0xA0, 0x77, 0xFD, 0xFE, 0xFD, 0xFD, |
| 738 |
}, |
| 739 |
[]uint8{ |
| 740 |
0x20, 0x00, 0x20, 0x30, 0x33, 0x99, 0x33, 0xFF, |
| 741 |
0xD0, 0xF0, 0xD0, 0xC0, 0xBB, 0x11, 0xBB, 0x00, |
| 742 |
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, |
| 743 |
0xEE, 0xED, 0x50, 0x77, 0xFD, 0xFE, 0xFE, 0xFD, |
| 744 |
}, |
| 745 |
}, |
| 746 |
{ |
| 747 |
"hue 3690", |
| 748 |
3690, |
| 749 |
image.Rect(-1, -1, 1, 3), |
| 750 |
image.Rect(0, 0, 2, 4), |
| 751 |
[]uint8{ |
| 752 |
0x00, 0x10, 0x20, 0x30, 0x99, 0x66, 0x33, 0xFF, |
| 753 |
0xF0, 0xE0, 0xD0, 0xC0, 0x11, 0x66, 0xBB, 0x00, |
| 754 |
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, |
| 755 |
0xEE, 0x50, 0xA0, 0x77, 0xFD, 0xFE, 0xFD, 0xFD, |
| 756 |
}, |
| 757 |
[]uint8{ |
| 758 |
0x20, 0x00, 0x20, 0x30, 0x33, 0x99, 0x33, 0xFF, |
| 759 |
0xD0, 0xF0, 0xD0, 0xC0, 0xBB, 0x11, 0xBB, 0x00, |
| 760 |
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, |
| 761 |
0xEE, 0xED, 0x50, 0x77, 0xFD, 0xFE, 0xFE, 0xFD, |
| 762 |
}, |
| 763 |
}, |
| 764 |
} |
| 765 |
|
| 766 |
for _, d := range testData { |
| 767 |
src := image.NewNRGBA(d.srcb) |
| 768 |
src.Pix = d.srcPix |
| 769 |
|
| 770 |
f := Hue(d.p) |
| 771 |
dst := image.NewNRGBA(f.Bounds(src.Bounds())) |
| 772 |
f.Draw(dst, src, nil) |
| 773 |
|
| 774 |
if !checkBoundsAndPix(dst.Bounds(), d.dstb, dst.Pix, d.dstPix) { |
| 775 |
t.Errorf("test [%s] failed: %#v, %#v", d.desc, dst.Bounds(), dst.Pix) |
| 776 |
} |
| 777 |
} |
| 778 |
} |
| 779 |
|
| 780 |
func TestSaturation(t *testing.T) { |
| 781 |
testData := []struct { |
| 782 |
desc string |
| 783 |
p float32 |
| 784 |
srcb, dstb image.Rectangle |
| 785 |
srcPix, dstPix []uint8 |
| 786 |
}{ |
| 787 |
{ |
| 788 |
"saturation 0 0x0", |
| 789 |
0, |
| 790 |
image.Rect(0, 0, 0, 0), |
| 791 |
image.Rect(0, 0, 0, 0), |
| 792 |
[]uint8{}, |
| 793 |
[]uint8{}, |
| 794 |
}, |
| 795 |
{ |
| 796 |
"saturation 0", |
| 797 |
0, |
| 798 |
image.Rect(-1, -1, 1, 3), |
| 799 |
image.Rect(0, 0, 2, 4), |
| 800 |
[]uint8{ |
| 801 |
0x00, 0x10, 0x20, 0x30, 0x99, 0x66, 0x33, 0xFF, |
| 802 |
0xF0, 0xE0, 0xD0, 0xC0, 0x11, 0x66, 0xBB, 0x00, |
| 803 |
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, |
| 804 |
0xE0, 0x50, 0xA0, 0x77, 0xEE, 0xFE, 0xEE, 0xFD, |
| 805 |
}, |
| 806 |
[]uint8{ |
| 807 |
0x00, 0x10, 0x20, 0x30, 0x99, 0x66, 0x33, 0xFF, |
| 808 |
0xF0, 0xE0, 0xD0, 0xC0, 0x11, 0x66, 0xBB, 0x00, |
| 809 |
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, |
| 810 |
0xE0, 0x50, 0xA0, 0x77, 0xEE, 0xFE, 0xEE, 0xFD, |
| 811 |
}, |
| 812 |
}, |
| 813 |
{ |
| 814 |
"saturation -50", |
| 815 |
-50, |
| 816 |
image.Rect(-1, -1, 1, 3), |
| 817 |
image.Rect(0, 0, 2, 4), |
| 818 |
[]uint8{ |
| 819 |
0x00, 0x10, 0x20, 0x30, 0x99, 0x66, 0x33, 0xFF, |
| 820 |
0xF0, 0xE0, 0xD0, 0xC0, 0x11, 0x66, 0xBB, 0x00, |
| 821 |
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, |
| 822 |
0xE0, 0x50, 0xA0, 0x77, 0xEE, 0xFE, 0xEE, 0xFD, |
| 823 |
}, |
| 824 |
[]uint8{ |
| 825 |
0x08, 0x10, 0x18, 0x30, 0x80, 0x66, 0x4D, 0xFF, |
| 826 |
0xE8, 0xE0, 0xD8, 0xC0, 0x3B, 0x66, 0x91, 0x00, |
| 827 |
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, |
| 828 |
0xBC, 0x74, 0x9C, 0x77, 0xF2, 0xFA, 0xF2, 0xFD, |
| 829 |
}, |
| 830 |
}, |
| 831 |
{ |
| 832 |
"saturation 100", |
| 833 |
100, |
| 834 |
image.Rect(-1, -1, 1, 3), |
| 835 |
image.Rect(0, 0, 2, 4), |
| 836 |
[]uint8{ |
| 837 |
0x00, 0x10, 0x20, 0x30, 0x99, 0x66, 0x33, 0xFF, |
| 838 |
0xF0, 0xE0, 0xD0, 0xC0, 0x11, 0x66, 0xBB, 0x00, |
| 839 |
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, |
| 840 |
0xE0, 0x50, 0xA0, 0x77, 0xEE, 0xFE, 0xEE, 0xFD, |
| 841 |
}, |
| 842 |
[]uint8{ |
| 843 |
0x00, 0x10, 0x20, 0x30, 0xCC, 0x66, 0x00, 0xFF, |
| 844 |
0xFF, 0xE0, 0xC1, 0xC0, 0x00, 0x66, 0xCC, 0x00, |
| 845 |
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, |
| 846 |
0xFF, 0x31, 0xA3, 0x77, 0xED, 0xFF, 0xED, 0xFD, |
| 847 |
}, |
| 848 |
}, |
| 849 |
} |
| 850 |
|
| 851 |
for _, d := range testData { |
| 852 |
src := image.NewNRGBA(d.srcb) |
| 853 |
src.Pix = d.srcPix |
| 854 |
|
| 855 |
f := Saturation(d.p) |
| 856 |
dst := image.NewNRGBA(f.Bounds(src.Bounds())) |
| 857 |
f.Draw(dst, src, nil) |
| 858 |
|
| 859 |
if !checkBoundsAndPix(dst.Bounds(), d.dstb, dst.Pix, d.dstPix) { |
| 860 |
t.Errorf("test [%s] failed: %#v, %#v", d.desc, dst.Bounds(), dst.Pix) |
| 861 |
} |
| 862 |
} |
| 863 |
} |
| 864 |
|
| 865 |
func TestColorize(t *testing.T) { |
| 866 |
testData := []struct { |
| 867 |
desc string |
| 868 |
h, s, p float32 |
| 869 |
srcb, dstb image.Rectangle |
| 870 |
srcPix, dstPix []uint8 |
| 871 |
}{ |
| 872 |
{ |
| 873 |
"colorize 0, 0, 0, 0x0", |
| 874 |
0, 0, 0, |
| 875 |
image.Rect(0, 0, 0, 0), |
| 876 |
image.Rect(0, 0, 0, 0), |
| 877 |
[]uint8{}, |
| 878 |
[]uint8{}, |
| 879 |
}, |
| 880 |
{ |
| 881 |
"colorize 0, 100, 0", |
| 882 |
0, 100, 0, |
| 883 |
image.Rect(-1, -1, 1, 3), |
| 884 |
image.Rect(0, 0, 2, 4), |
| 885 |
[]uint8{ |
| 886 |
0x00, 0x10, 0x20, 0x30, 0x99, 0x66, 0x33, 0xFF, |
| 887 |
0xF0, 0xE0, 0xD0, 0xC0, 0x11, 0x66, 0xBB, 0x00, |
| 888 |
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, |
| 889 |
0xE0, 0x50, 0xA0, 0x77, 0xEE, 0xFE, 0xEE, 0xFD, |
| 890 |
}, |
| 891 |
[]uint8{ |
| 892 |
0x00, 0x10, 0x20, 0x30, 0x99, 0x66, 0x33, 0xFF, |
| 893 |
0xF0, 0xE0, 0xD0, 0xC0, 0x11, 0x66, 0xBB, 0x00, |
| 894 |
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, |
| 895 |
0xE0, 0x50, 0xA0, 0x77, 0xEE, 0xFE, 0xEE, 0xFD, |
| 896 |
}, |
| 897 |
}, |
| 898 |
|
| 899 |
{ |
| 900 |
"colorize 0, 100, 100", |
| 901 |
0, 100, 100, |
| 902 |
image.Rect(-1, -1, 1, 3), |
| 903 |
image.Rect(0, 0, 2, 4), |
| 904 |
[]uint8{ |
| 905 |
0x00, 0x10, 0x20, 0x30, 0x99, 0x66, 0x33, 0xFF, |
| 906 |
0xF0, 0xE0, 0xD0, 0xC0, 0x11, 0x66, 0xBB, 0x00, |
| 907 |
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, |
| 908 |
0xE0, 0x50, 0xA0, 0x77, 0xEE, 0xFE, 0xEE, 0xFD, |
| 909 |
}, |
| 910 |
[]uint8{ |
| 911 |
0x20, 0x00, 0x00, 0x30, 0xCC, 0x00, 0x00, 0xFF, |
| 912 |
0xFF, 0xC1, 0xC1, 0xC0, 0xCC, 0x00, 0x00, 0x00, |
| 913 |
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, |
| 914 |
0xFF, 0x31, 0x31, 0x77, 0xFF, 0xED, 0xED, 0xFD, |
| 915 |
}, |
| 916 |
}, |
| 917 |
} |
| 918 |
|
| 919 |
for _, d := range testData { |
| 920 |
src := image.NewNRGBA(d.srcb) |
| 921 |
src.Pix = d.srcPix |
| 922 |
|
| 923 |
f := Colorize(d.h, d.s, d.p) |
| 924 |
dst := image.NewNRGBA(f.Bounds(src.Bounds())) |
| 925 |
f.Draw(dst, src, nil) |
| 926 |
|
| 927 |
if !checkBoundsAndPix(dst.Bounds(), d.dstb, dst.Pix, d.dstPix) { |
| 928 |
t.Errorf("test [%s] failed: %#v, %#v", d.desc, dst.Bounds(), dst.Pix) |
| 929 |
} |
| 930 |
} |
| 931 |
} |
| 932 |
|
| 933 |
func TestColorBalance(t *testing.T) { |
| 934 |
testData := []struct { |
| 935 |
desc string |
| 936 |
r, g, b float32 |
| 937 |
srcb, dstb image.Rectangle |
| 938 |
srcPix, dstPix []uint8 |
| 939 |
}{ |
| 940 |
{ |
| 941 |
"color balance 0, 0, 0, 0x0", |
| 942 |
0, 0, 0, |
| 943 |
image.Rect(0, 0, 0, 0), |
| 944 |
image.Rect(0, 0, 0, 0), |
| 945 |
[]uint8{}, |
| 946 |
[]uint8{}, |
| 947 |
}, |
| 948 |
{ |
| 949 |
"color balance 0, 0, 0", |
| 950 |
0, 0, 0, |
| 951 |
image.Rect(-1, -1, 1, 3), |
| 952 |
image.Rect(0, 0, 2, 4), |
| 953 |
[]uint8{ |
| 954 |
0x00, 0x10, 0x20, 0x30, 0x99, 0x66, 0x33, 0xFF, |
| 955 |
0xF0, 0xE0, 0xD0, 0xC0, 0x11, 0x66, 0xBB, 0x00, |
| 956 |
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, |
| 957 |
0xE0, 0x50, 0xA0, 0x77, 0xEE, 0xFE, 0xEE, 0xFD, |
| 958 |
}, |
| 959 |
[]uint8{ |
| 960 |
0x00, 0x10, 0x20, 0x30, 0x99, 0x66, 0x33, 0xFF, |
| 961 |
0xF0, 0xE0, 0xD0, 0xC0, 0x11, 0x66, 0xBB, 0x00, |
| 962 |
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, |
| 963 |
0xE0, 0x50, 0xA0, 0x77, 0xEE, 0xFE, 0xEE, 0xFD, |
| 964 |
}, |
| 965 |
}, |
| 966 |
|
| 967 |
{ |
| 968 |
"color balance 10, -20, 200", |
| 969 |
10, -20, 200, |
| 970 |
image.Rect(-1, -1, 1, 3), |
| 971 |
image.Rect(0, 0, 2, 4), |
| 972 |
[]uint8{ |
| 973 |
0x00, 0x10, 0x20, 0x30, 0x99, 0x66, 0x33, 0xFF, |
| 974 |
0xF0, 0xE0, 0xD0, 0xC0, 0x11, 0x66, 0xBB, 0x00, |
| 975 |
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, |
| 976 |
0xE0, 0x50, 0xA0, 0x77, 0xEE, 0xFE, 0xEE, 0xFD, |
| 977 |
}, |
| 978 |
[]uint8{ |
| 979 |
0x00, 0x0D, 0x60, 0x30, 0xA8, 0x52, 0x99, 0xFF, |
| 980 |
0xFF, 0xB3, 0xFF, 0xC0, 0x13, 0x52, 0xFF, 0x00, |
| 981 |
0x00, 0x00, 0x00, 0x00, 0xFF, 0xCC, 0xFF, 0xFF, |
| 982 |
0xF6, 0x40, 0xFF, 0x77, 0xFF, 0xCB, 0xFF, 0xFD, |
| 983 |
}, |
| 984 |
}, |
| 985 |
} |
| 986 |
|
| 987 |
for _, d := range testData { |
| 988 |
src := image.NewNRGBA(d.srcb) |
| 989 |
src.Pix = d.srcPix |
| 990 |
|
| 991 |
f := ColorBalance(d.r, d.g, d.b) |
| 992 |
dst := image.NewNRGBA(f.Bounds(src.Bounds())) |
| 993 |
f.Draw(dst, src, nil) |
| 994 |
|
| 995 |
if !checkBoundsAndPix(dst.Bounds(), d.dstb, dst.Pix, d.dstPix) { |
| 996 |
t.Errorf("test [%s] failed: %#v, %#v", d.desc, dst.Bounds(), dst.Pix) |
| 997 |
} |
| 998 |
} |
| 999 |
} |
| 1000 |
|
| 1001 |
func TestThreshold(t *testing.T) { |
| 1002 |
testData := []struct { |
| 1003 |
desc string |
| 1004 |
percentage float32 |
| 1005 |
srcb, dstb image.Rectangle |
| 1006 |
srcPix, dstPix []uint8 |
| 1007 |
}{ |
| 1008 |
{ |
| 1009 |
"threshold -1", |
| 1010 |
-1, |
| 1011 |
image.Rect(-1, -1, 5, 0), |
| 1012 |
image.Rect(0, 0, 6, 1), |
| 1013 |
[]uint8{0x00, 0x33, 0x66, 0x99, 0xcc, 0xff}, |
| 1014 |
[]uint8{0x00, 0xff, 0xff, 0xff, 0xff, 0xff}, |
| 1015 |
}, |
| 1016 |
{ |
| 1017 |
"threshold 0", |
| 1018 |
0, |
| 1019 |
image.Rect(-1, -1, 5, 0), |
| 1020 |
image.Rect(0, 0, 6, 1), |
| 1021 |
[]uint8{0x00, 0x33, 0x66, 0x99, 0xcc, 0xff}, |
| 1022 |
[]uint8{0x00, 0xff, 0xff, 0xff, 0xff, 0xff}, |
| 1023 |
}, |
| 1024 |
{ |
| 1025 |
"threshold 30", |
| 1026 |
30, |
| 1027 |
image.Rect(-1, -1, 5, 0), |
| 1028 |
image.Rect(0, 0, 6, 1), |
| 1029 |
[]uint8{0x00, 0x33, 0x66, 0x99, 0xcc, 0xff}, |
| 1030 |
[]uint8{0x00, 0x00, 0xff, 0xff, 0xff, 0xff}, |
| 1031 |
}, |
| 1032 |
{ |
| 1033 |
"threshold 50", |
| 1034 |
50, |
| 1035 |
image.Rect(-1, -1, 5, 0), |
| 1036 |
image.Rect(0, 0, 6, 1), |
| 1037 |
[]uint8{0x00, 0x33, 0x66, 0x99, 0xcc, 0xff}, |
| 1038 |
[]uint8{0x00, 0x00, 0x00, 0xff, 0xff, 0xff}, |
| 1039 |
}, |
| 1040 |
{ |
| 1041 |
"threshold 90", |
| 1042 |
90, |
| 1043 |
image.Rect(-1, -1, 5, 0), |
| 1044 |
image.Rect(0, 0, 6, 1), |
| 1045 |
[]uint8{0x00, 0x33, 0x66, 0x99, 0xcc, 0xff}, |
| 1046 |
[]uint8{0x00, 0x00, 0x00, 0x00, 0x00, 0xff}, |
| 1047 |
}, |
| 1048 |
{ |
| 1049 |
"threshold 100", |
| 1050 |
100, |
| 1051 |
image.Rect(-1, -1, 5, 0), |
| 1052 |
image.Rect(0, 0, 6, 1), |
| 1053 |
[]uint8{0x00, 0x33, 0x66, 0x99, 0xcc, 0xff}, |
| 1054 |
[]uint8{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, |
| 1055 |
}, |
| 1056 |
{ |
| 1057 |
"threshold 101", |
| 1058 |
101, |
| 1059 |
image.Rect(-1, -1, 5, 0), |
| 1060 |
image.Rect(0, 0, 6, 1), |
| 1061 |
[]uint8{0x00, 0x33, 0x66, 0x99, 0xcc, 0xff}, |
| 1062 |
[]uint8{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, |
| 1063 |
}, |
| 1064 |
} |
| 1065 |
|
| 1066 |
for _, d := range testData { |
| 1067 |
src := image.NewGray(d.srcb) |
| 1068 |
src.Pix = d.srcPix |
| 1069 |
|
| 1070 |
f := Threshold(d.percentage) |
| 1071 |
dst := image.NewGray(f.Bounds(src.Bounds())) |
| 1072 |
f.Draw(dst, src, nil) |
| 1073 |
|
| 1074 |
if !checkBoundsAndPix(dst.Bounds(), d.dstb, dst.Pix, d.dstPix) { |
| 1075 |
t.Errorf("test [%s] failed: %#v, %#v", d.desc, dst.Bounds(), dst.Pix) |
| 1076 |
} |
| 1077 |
} |
| 1078 |
} |
| 1079 |
|
| 1080 |
func TestColorFunc(t *testing.T) { |
| 1081 |
testData := []struct { |
| 1082 |
desc string |
| 1083 |
fn func(r0, g0, b0, a0 float32) (r, g, b, a float32) |
| 1084 |
srcb, dstb image.Rectangle |
| 1085 |
srcPix, dstPix []uint8 |
| 1086 |
}{ |
| 1087 |
{ |
| 1088 |
"color func 0x0", |
| 1089 |
func(r0, g0, b0, a0 float32) (r, g, b, a float32) { |
| 1090 |
return r0, g0, b0, a0 |
| 1091 |
}, |
| 1092 |
image.Rect(0, 0, 0, 0), |
| 1093 |
image.Rect(0, 0, 0, 0), |
| 1094 |
[]uint8{}, |
| 1095 |
[]uint8{}, |
| 1096 |
}, |
| 1097 |
{ |
| 1098 |
"color func swap channels", |
| 1099 |
func(r0, g0, b0, a0 float32) (r, g, b, a float32) { |
| 1100 |
return a0, b0, g0, r0 |
| 1101 |
}, |
| 1102 |
image.Rect(-1, -1, 1, 3), |
| 1103 |
image.Rect(0, 0, 2, 4), |
| 1104 |
[]uint8{ |
| 1105 |
0x00, 0x10, 0x20, 0x30, 0x99, 0x66, 0x33, 0xFF, |
| 1106 |
0xF0, 0xE0, 0xD0, 0xC0, 0x11, 0x66, 0xBB, 0x00, |
| 1107 |
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, |
| 1108 |
0xE0, 0x50, 0xA0, 0x77, 0xEE, 0xFE, 0xEE, 0xFD, |
| 1109 |
}, |
| 1110 |
[]uint8{ |
| 1111 |
0x30, 0x20, 0x10, 0x00, 0xFF, 0x33, 0x66, 0x99, |
| 1112 |
0xC0, 0xD0, 0xE0, 0xF0, 0x00, 0xBB, 0x66, 0x11, |
| 1113 |
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, |
| 1114 |
0x77, 0xA0, 0x50, 0xE0, 0xFD, 0xEE, 0xFE, 0xEE, |
| 1115 |
}, |
| 1116 |
}, |
| 1117 |
|
| 1118 |
{ |
| 1119 |
"color func invert all", |
| 1120 |
func(r0, g0, b0, a0 float32) (r, g, b, a float32) { |
| 1121 |
return 1 - r0, 1 - g0, 1 - b0, 1 - a0 |
| 1122 |
}, |
| 1123 |
image.Rect(-1, -1, 1, 3), |
| 1124 |
image.Rect(0, 0, 2, 4), |
| 1125 |
[]uint8{ |
| 1126 |
0x00, 0x10, 0x20, 0x30, 0x99, 0x66, 0x33, 0xFF, |
| 1127 |
0xF0, 0xE0, 0xD0, 0xC0, 0x11, 0x66, 0xBB, 0x00, |
| 1128 |
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, |
| 1129 |
0xE0, 0x50, 0xA0, 0x77, 0xEE, 0xFE, 0xEE, 0xFD, |
| 1130 |
}, |
| 1131 |
[]uint8{ |
| 1132 |
0xFF, 0xEF, 0xDF, 0xCF, 0x66, 0x99, 0xCC, 0x00, |
| 1133 |
0x0F, 0x1F, 0x2F, 0x3F, 0xEE, 0x99, 0x44, 0xFF, |
| 1134 |
0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, |
| 1135 |
0x1F, 0xAF, 0x5F, 0x88, 0x11, 0x01, 0x11, 0x02, |
| 1136 |
}, |
| 1137 |
}, |
| 1138 |
} |
| 1139 |
|
| 1140 |
for _, d := range testData { |
| 1141 |
src := image.NewNRGBA(d.srcb) |
| 1142 |
src.Pix = d.srcPix |
| 1143 |
|
| 1144 |
f := ColorFunc(d.fn) |
| 1145 |
dst := image.NewNRGBA(f.Bounds(src.Bounds())) |
| 1146 |
f.Draw(dst, src, nil) |
| 1147 |
|
| 1148 |
if !checkBoundsAndPix(dst.Bounds(), d.dstb, dst.Pix, d.dstPix) { |
| 1149 |
t.Errorf("test [%s] failed: %#v, %#v", d.desc, dst.Bounds(), dst.Pix) |
| 1150 |
} |
| 1151 |
} |
| 1152 |
} |