| 1 |
package gift |
| 2 |
|
| 3 |
import ( |
| 4 |
"image" |
| 5 |
"image/color" |
| 6 |
"image/draw" |
| 7 |
"math" |
| 8 |
"testing" |
| 9 |
) |
| 10 |
|
| 11 |
func TestNewPixelGetter(t *testing.T) { |
| 12 |
var img image.Image |
| 13 |
var pg *pixelGetter |
| 14 |
img = image.NewNRGBA(image.Rect(0, 0, 1, 1)) |
| 15 |
pg = newPixelGetter(img) |
| 16 |
if pg.it != itNRGBA || pg.nrgba == nil || !img.Bounds().Eq(pg.bounds) { |
| 17 |
t.Error("newPixelGetter NRGBA") |
| 18 |
} |
| 19 |
img = image.NewNRGBA64(image.Rect(0, 0, 1, 1)) |
| 20 |
pg = newPixelGetter(img) |
| 21 |
if pg.it != itNRGBA64 || pg.nrgba64 == nil || !img.Bounds().Eq(pg.bounds) { |
| 22 |
t.Error("newPixelGetter NRGBA64") |
| 23 |
} |
| 24 |
img = image.NewRGBA(image.Rect(0, 0, 1, 1)) |
| 25 |
pg = newPixelGetter(img) |
| 26 |
if pg.it != itRGBA || pg.rgba == nil || !img.Bounds().Eq(pg.bounds) { |
| 27 |
t.Error("newPixelGetter RGBA") |
| 28 |
} |
| 29 |
img = image.NewRGBA64(image.Rect(0, 0, 1, 1)) |
| 30 |
pg = newPixelGetter(img) |
| 31 |
if pg.it != itRGBA64 || pg.rgba64 == nil || !img.Bounds().Eq(pg.bounds) { |
| 32 |
t.Error("newPixelGetter RGBA64") |
| 33 |
} |
| 34 |
img = image.NewGray(image.Rect(0, 0, 1, 1)) |
| 35 |
pg = newPixelGetter(img) |
| 36 |
if pg.it != itGray || pg.gray == nil || !img.Bounds().Eq(pg.bounds) { |
| 37 |
t.Error("newPixelGetter Gray") |
| 38 |
} |
| 39 |
img = image.NewGray16(image.Rect(0, 0, 1, 1)) |
| 40 |
pg = newPixelGetter(img) |
| 41 |
if pg.it != itGray16 || pg.gray16 == nil || !img.Bounds().Eq(pg.bounds) { |
| 42 |
t.Error("newPixelGetter Gray16") |
| 43 |
} |
| 44 |
img = image.NewYCbCr(image.Rect(0, 0, 1, 1), image.YCbCrSubsampleRatio422) |
| 45 |
pg = newPixelGetter(img) |
| 46 |
if pg.it != itYCbCr || pg.ycbcr == nil || !img.Bounds().Eq(pg.bounds) { |
| 47 |
t.Error("newPixelGetter YCbCr") |
| 48 |
} |
| 49 |
img = image.NewUniform(color.NRGBA64{0, 0, 0, 0}) |
| 50 |
pg = newPixelGetter(img) |
| 51 |
if pg.it != itGeneric || pg.image == nil || !img.Bounds().Eq(pg.bounds) { |
| 52 |
t.Error("newPixelGetter Generic(Uniform)") |
| 53 |
} |
| 54 |
img = image.NewAlpha(image.Rect(0, 0, 1, 1)) |
| 55 |
pg = newPixelGetter(img) |
| 56 |
if pg.it != itGeneric || pg.image == nil || !img.Bounds().Eq(pg.bounds) { |
| 57 |
t.Error("newPixelGetter Generic(Alpha)") |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
func comparePixels(px1, px2 pixel, dif float64) bool { |
| 62 |
if math.Abs(float64(px1.r)-float64(px2.r)) > dif { |
| 63 |
return false |
| 64 |
} |
| 65 |
if math.Abs(float64(px1.g)-float64(px2.g)) > dif { |
| 66 |
return false |
| 67 |
} |
| 68 |
if math.Abs(float64(px1.b)-float64(px2.b)) > dif { |
| 69 |
return false |
| 70 |
} |
| 71 |
if math.Abs(float64(px1.a)-float64(px2.a)) > dif { |
| 72 |
return false |
| 73 |
} |
| 74 |
return true |
| 75 |
} |
| 76 |
|
| 77 |
func compareColorsNRGBA(c1, c2 color.NRGBA, dif int) bool { |
| 78 |
if math.Abs(float64(c1.R)-float64(c2.R)) > float64(dif) { |
| 79 |
return false |
| 80 |
} |
| 81 |
if math.Abs(float64(c1.G)-float64(c2.G)) > float64(dif) { |
| 82 |
return false |
| 83 |
} |
| 84 |
if math.Abs(float64(c1.B)-float64(c2.B)) > float64(dif) { |
| 85 |
return false |
| 86 |
} |
| 87 |
if math.Abs(float64(c1.A)-float64(c2.A)) > float64(dif) { |
| 88 |
return false |
| 89 |
} |
| 90 |
return true |
| 91 |
} |
| 92 |
|
| 93 |
func TestGetPixel(t *testing.T) { |
| 94 |
var pg *pixelGetter |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
palette := []color.Color{ |
| 99 |
color.NRGBA{0, 0, 0, 0}, |
| 100 |
color.NRGBA{255, 255, 255, 255}, |
| 101 |
color.NRGBA{50, 100, 150, 255}, |
| 102 |
color.NRGBA{150, 100, 50, 200}, |
| 103 |
} |
| 104 |
|
| 105 |
images1 := []draw.Image{ |
| 106 |
image.NewRGBA(image.Rect(-1, -2, 3, 4)), |
| 107 |
image.NewRGBA64(image.Rect(-1, -2, 3, 4)), |
| 108 |
image.NewNRGBA(image.Rect(-1, -2, 3, 4)), |
| 109 |
image.NewNRGBA64(image.Rect(-1, -2, 3, 4)), |
| 110 |
image.NewPaletted(image.Rect(-1, -2, 3, 4), palette), |
| 111 |
} |
| 112 |
|
| 113 |
colors1 := []struct { |
| 114 |
c color.NRGBA |
| 115 |
px pixel |
| 116 |
}{ |
| 117 |
{color.NRGBA{0, 0, 0, 0}, pixel{0, 0, 0, 0}}, |
| 118 |
{color.NRGBA{255, 255, 255, 255}, pixel{1, 1, 1, 1}}, |
| 119 |
{color.NRGBA{50, 100, 150, 255}, pixel{0.196, 0.392, 0.588, 1}}, |
| 120 |
{color.NRGBA{150, 100, 50, 200}, pixel{0.588, 0.392, 0.196, 0.784}}, |
| 121 |
} |
| 122 |
|
| 123 |
for _, img := range images1 { |
| 124 |
pg = newPixelGetter(img) |
| 125 |
for _, k := range colors1 { |
| 126 |
for _, x := range []int{-1, 0, 2} { |
| 127 |
for _, y := range []int{-2, 0, 3} { |
| 128 |
img.Set(x, y, k.c) |
| 129 |
px := pg.getPixel(x, y) |
| 130 |
if !comparePixels(k.px, px, 0.005) { |
| 131 |
t.Errorf("getPixel %T %v %dx%d %v %v", img, k.c, x, y, k.px, px) |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
for _, k := range colors1 { |
| 141 |
img := image.NewUniform(k.c) |
| 142 |
pg = newPixelGetter(img) |
| 143 |
for _, x := range []int{-1, 0, 2} { |
| 144 |
for _, y := range []int{-2, 0, 3} { |
| 145 |
px := pg.getPixel(x, y) |
| 146 |
if !comparePixels(k.px, px, 0.005) { |
| 147 |
t.Errorf("getPixel %T %v %dx%d %v %v", img, k.c, x, y, k.px, px) |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
colors2 := []struct { |
| 156 |
c color.NRGBA |
| 157 |
px pixel |
| 158 |
}{ |
| 159 |
{color.NRGBA{0, 0, 0, 255}, pixel{0, 0, 0, 1}}, |
| 160 |
{color.NRGBA{255, 255, 255, 255}, pixel{1, 1, 1, 1}}, |
| 161 |
{color.NRGBA{50, 100, 150, 255}, pixel{0.196, 0.392, 0.588, 1}}, |
| 162 |
{color.NRGBA{150, 100, 50, 255}, pixel{0.588, 0.392, 0.196, 1}}, |
| 163 |
} |
| 164 |
|
| 165 |
for _, k := range colors2 { |
| 166 |
for _, sr := range []image.YCbCrSubsampleRatio{ |
| 167 |
image.YCbCrSubsampleRatio444, |
| 168 |
image.YCbCrSubsampleRatio422, |
| 169 |
image.YCbCrSubsampleRatio420, |
| 170 |
image.YCbCrSubsampleRatio440, |
| 171 |
image.YCbCrSubsampleRatio410, |
| 172 |
image.YCbCrSubsampleRatio411, |
| 173 |
} { |
| 174 |
img := image.NewYCbCr(image.Rect(-1, -2, 3, 4), sr) |
| 175 |
pg = newPixelGetter(img) |
| 176 |
for _, x := range []int{-1, 0, 2} { |
| 177 |
for _, y := range []int{-2, 0, 3} { |
| 178 |
iy := img.YOffset(x, y) |
| 179 |
ic := img.COffset(x, y) |
| 180 |
img.Y[iy], img.Cb[ic], img.Cr[ic] = color.RGBToYCbCr(k.c.R, k.c.G, k.c.B) |
| 181 |
px := pg.getPixel(x, y) |
| 182 |
if !comparePixels(k.px, px, 0.005) { |
| 183 |
t.Errorf("getPixel %T %v %dx%d %v %v", img, k.c, x, y, k.px, px) |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
images2 := []draw.Image{ |
| 193 |
image.NewGray(image.Rect(-1, -2, 3, 4)), |
| 194 |
image.NewGray16(image.Rect(-1, -2, 3, 4)), |
| 195 |
} |
| 196 |
|
| 197 |
colors3 := []struct { |
| 198 |
c color.NRGBA |
| 199 |
px pixel |
| 200 |
}{ |
| 201 |
{color.NRGBA{0, 0, 0, 0}, pixel{0, 0, 0, 1}}, |
| 202 |
{color.NRGBA{255, 255, 255, 255}, pixel{1, 1, 1, 1}}, |
| 203 |
{color.NRGBA{50, 100, 150, 255}, pixel{0.356, 0.356, 0.356, 1}}, |
| 204 |
{color.NRGBA{150, 100, 50, 200}, pixel{0.337, 0.337, 0.337, 1}}, |
| 205 |
} |
| 206 |
|
| 207 |
for _, img := range images2 { |
| 208 |
pg = newPixelGetter(img) |
| 209 |
for _, k := range colors3 { |
| 210 |
for _, x := range []int{-1, 0, 2} { |
| 211 |
for _, y := range []int{-2, 0, 3} { |
| 212 |
img.Set(x, y, k.c) |
| 213 |
px := pg.getPixel(x, y) |
| 214 |
if !comparePixels(k.px, px, 0.005) { |
| 215 |
t.Errorf("getPixel %T %v %dx%d %v %v", img, k.c, x, y, k.px, px) |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
func comparePixelSlices(s1, s2 []pixel, dif float64) bool { |
| 224 |
if len(s1) != len(s2) { |
| 225 |
return false |
| 226 |
} |
| 227 |
for i := 1; i < len(s1); i++ { |
| 228 |
if !comparePixels(s1[i], s2[i], dif) { |
| 229 |
return false |
| 230 |
} |
| 231 |
} |
| 232 |
return true |
| 233 |
} |
| 234 |
|
| 235 |
func TestGetPixelRow(t *testing.T) { |
| 236 |
colors := []color.NRGBA{ |
| 237 |
{0, 0, 0, 0}, |
| 238 |
{255, 255, 255, 255}, |
| 239 |
{50, 100, 150, 255}, |
| 240 |
{150, 100, 50, 200}, |
| 241 |
} |
| 242 |
pixels := []pixel{ |
| 243 |
{0, 0, 0, 0}, |
| 244 |
{1, 1, 1, 1}, |
| 245 |
{0.196, 0.392, 0.588, 1}, |
| 246 |
{0.588, 0.392, 0.196, 0.784}, |
| 247 |
} |
| 248 |
|
| 249 |
img := image.NewNRGBA(image.Rect(-1, -2, 3, 2)) |
| 250 |
pg := newPixelGetter(img) |
| 251 |
var row []pixel |
| 252 |
for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ { |
| 253 |
for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ { |
| 254 |
img.Set(x, y, colors[x-img.Bounds().Min.X]) |
| 255 |
} |
| 256 |
pg.getPixelRow(y, &row) |
| 257 |
if !comparePixelSlices(row, pixels, 0.005) { |
| 258 |
t.Errorf("getPixelRow y=%d %v %v", y, row, pixels) |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
func TestGetPixelColumn(t *testing.T) { |
| 264 |
colors := []color.NRGBA{ |
| 265 |
{0, 0, 0, 0}, |
| 266 |
{255, 255, 255, 255}, |
| 267 |
{50, 100, 150, 255}, |
| 268 |
{150, 100, 50, 200}, |
| 269 |
} |
| 270 |
pixels := []pixel{ |
| 271 |
{0, 0, 0, 0}, |
| 272 |
{1, 1, 1, 1}, |
| 273 |
{0.196, 0.392, 0.588, 1}, |
| 274 |
{0.588, 0.392, 0.196, 0.784}, |
| 275 |
} |
| 276 |
|
| 277 |
img := image.NewNRGBA(image.Rect(-1, -2, 3, 2)) |
| 278 |
pg := newPixelGetter(img) |
| 279 |
var column []pixel |
| 280 |
for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ { |
| 281 |
for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ { |
| 282 |
img.Set(x, y, colors[y-img.Bounds().Min.Y]) |
| 283 |
} |
| 284 |
pg.getPixelColumn(x, &column) |
| 285 |
if !comparePixelSlices(column, pixels, 0.005) { |
| 286 |
t.Errorf("getPixelColumn x=%d %v %v", x, column, pixels) |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
func TestF32u8(t *testing.T) { |
| 292 |
testData := []struct { |
| 293 |
x float32 |
| 294 |
y uint8 |
| 295 |
}{ |
| 296 |
{-1, 0}, |
| 297 |
{0, 0}, |
| 298 |
{100, 100}, |
| 299 |
{255, 255}, |
| 300 |
{256, 255}, |
| 301 |
} |
| 302 |
for _, p := range testData { |
| 303 |
v := f32u8(p.x) |
| 304 |
if v != p.y { |
| 305 |
t.Errorf("f32u8(%f) != %d: %d", p.x, p.y, v) |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
func TestF32u16(t *testing.T) { |
| 311 |
testData := []struct { |
| 312 |
x float32 |
| 313 |
y uint16 |
| 314 |
}{ |
| 315 |
{-1, 0}, |
| 316 |
{0, 0}, |
| 317 |
{1, 1}, |
| 318 |
{10000, 10000}, |
| 319 |
{65535, 65535}, |
| 320 |
{65536, 65535}, |
| 321 |
} |
| 322 |
for _, p := range testData { |
| 323 |
v := f32u16(p.x) |
| 324 |
if v != p.y { |
| 325 |
t.Errorf("f32u16(%f) != %d: %d", p.x, p.y, v) |
| 326 |
} |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
func TestClampi32(t *testing.T) { |
| 331 |
testData := []struct { |
| 332 |
x int32 |
| 333 |
y int32 |
| 334 |
}{ |
| 335 |
{-1, 0}, |
| 336 |
{0, 0}, |
| 337 |
{1, 1}, |
| 338 |
{99, 99}, |
| 339 |
{100, 100}, |
| 340 |
{101, 100}, |
| 341 |
} |
| 342 |
for _, p := range testData { |
| 343 |
v := clampi32(p.x, 0, 100) |
| 344 |
if v != p.y { |
| 345 |
t.Errorf("clampi32(%d) != %d: %d", p.x, p.y, v) |
| 346 |
} |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
func TestNewPixelSetter(t *testing.T) { |
| 351 |
var img draw.Image |
| 352 |
var pg *pixelSetter |
| 353 |
img = image.NewNRGBA(image.Rect(0, 0, 1, 1)) |
| 354 |
pg = newPixelSetter(img) |
| 355 |
if pg.it != itNRGBA || pg.nrgba == nil || !img.Bounds().Eq(pg.bounds) { |
| 356 |
t.Error("newPixelSetter NRGBA") |
| 357 |
} |
| 358 |
img = image.NewNRGBA64(image.Rect(0, 0, 1, 1)) |
| 359 |
pg = newPixelSetter(img) |
| 360 |
if pg.it != itNRGBA64 || pg.nrgba64 == nil || !img.Bounds().Eq(pg.bounds) { |
| 361 |
t.Error("newPixelSetter NRGBA64") |
| 362 |
} |
| 363 |
img = image.NewRGBA(image.Rect(0, 0, 1, 1)) |
| 364 |
pg = newPixelSetter(img) |
| 365 |
if pg.it != itRGBA || pg.rgba == nil || !img.Bounds().Eq(pg.bounds) { |
| 366 |
t.Error("newPixelSetter RGBA") |
| 367 |
} |
| 368 |
img = image.NewRGBA64(image.Rect(0, 0, 1, 1)) |
| 369 |
pg = newPixelSetter(img) |
| 370 |
if pg.it != itRGBA64 || pg.rgba64 == nil || !img.Bounds().Eq(pg.bounds) { |
| 371 |
t.Error("newPixelSetter RGBA64") |
| 372 |
} |
| 373 |
img = image.NewGray(image.Rect(0, 0, 1, 1)) |
| 374 |
pg = newPixelSetter(img) |
| 375 |
if pg.it != itGray || pg.gray == nil || !img.Bounds().Eq(pg.bounds) { |
| 376 |
t.Error("newPixelSetter Gray") |
| 377 |
} |
| 378 |
img = image.NewGray16(image.Rect(0, 0, 1, 1)) |
| 379 |
pg = newPixelSetter(img) |
| 380 |
if pg.it != itGray16 || pg.gray16 == nil || !img.Bounds().Eq(pg.bounds) { |
| 381 |
t.Error("newPixelSetter Gray16") |
| 382 |
} |
| 383 |
img = image.NewPaletted(image.Rect(0, 0, 1, 1), color.Palette{}) |
| 384 |
pg = newPixelSetter(img) |
| 385 |
if pg.it != itPaletted || pg.paletted == nil || !img.Bounds().Eq(pg.bounds) { |
| 386 |
t.Error("newPixelSetter Paletted") |
| 387 |
} |
| 388 |
img = image.NewAlpha(image.Rect(0, 0, 1, 1)) |
| 389 |
pg = newPixelSetter(img) |
| 390 |
if pg.it != itGeneric || pg.image == nil || !img.Bounds().Eq(pg.bounds) { |
| 391 |
t.Error("newPixelSetter Generic(Alpha)") |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
func TestSetPixel(t *testing.T) { |
| 396 |
var ps *pixelSetter |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
images1 := []draw.Image{ |
| 401 |
image.NewRGBA(image.Rect(-1, -2, 3, 4)), |
| 402 |
image.NewRGBA64(image.Rect(-1, -2, 3, 4)), |
| 403 |
image.NewNRGBA(image.Rect(-1, -2, 3, 4)), |
| 404 |
image.NewNRGBA64(image.Rect(-1, -2, 3, 4)), |
| 405 |
} |
| 406 |
|
| 407 |
colors1 := []struct { |
| 408 |
c color.NRGBA |
| 409 |
px pixel |
| 410 |
}{ |
| 411 |
{color.NRGBA{0, 0, 0, 0}, pixel{0, 0, 0, 0}}, |
| 412 |
{color.NRGBA{0, 0, 0, 255}, pixel{0, 0, 0, 1}}, |
| 413 |
{color.NRGBA{255, 255, 255, 255}, pixel{1, 1, 1, 1}}, |
| 414 |
{color.NRGBA{50, 100, 150, 255}, pixel{0.196, 0.392, 0.588, 1}}, |
| 415 |
{color.NRGBA{150, 100, 50, 200}, pixel{0.588, 0.392, 0.196, 0.784}}, |
| 416 |
} |
| 417 |
|
| 418 |
for _, img := range images1 { |
| 419 |
ps = newPixelSetter(img) |
| 420 |
for _, k := range colors1 { |
| 421 |
for _, x := range []int{-1, 0, 2} { |
| 422 |
for _, y := range []int{-2, 0, 3} { |
| 423 |
ps.setPixel(x, y, k.px) |
| 424 |
c := color.NRGBAModel.Convert(img.At(x, y)).(color.NRGBA) |
| 425 |
if !compareColorsNRGBA(c, k.c, 1) { |
| 426 |
t.Errorf("setPixel %T %v %dx%d %v %v", img, k.px, x, y, k.c, c) |
| 427 |
} |
| 428 |
} |
| 429 |
} |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
images2 := []draw.Image{ |
| 436 |
image.NewGray(image.Rect(-1, -2, 3, 4)), |
| 437 |
image.NewGray16(image.Rect(-1, -2, 3, 4)), |
| 438 |
} |
| 439 |
|
| 440 |
colors2 := []struct { |
| 441 |
c color.NRGBA |
| 442 |
px pixel |
| 443 |
}{ |
| 444 |
{color.NRGBA{0, 0, 0, 255}, pixel{0, 0, 0, 1}}, |
| 445 |
{color.NRGBA{255, 255, 255, 255}, pixel{1, 1, 1, 1}}, |
| 446 |
{color.NRGBA{110, 110, 110, 255}, pixel{0.2, 0.5, 0.7, 1}}, |
| 447 |
{color.NRGBA{55, 55, 55, 255}, pixel{0.2, 0.5, 0.7, 0.5}}, |
| 448 |
} |
| 449 |
|
| 450 |
for _, img := range images2 { |
| 451 |
ps = newPixelSetter(img) |
| 452 |
for _, k := range colors2 { |
| 453 |
for _, x := range []int{-1, 0, 2} { |
| 454 |
for _, y := range []int{-2, 0, 3} { |
| 455 |
ps.setPixel(x, y, k.px) |
| 456 |
c := color.NRGBAModel.Convert(img.At(x, y)).(color.NRGBA) |
| 457 |
if !compareColorsNRGBA(c, k.c, 1) { |
| 458 |
t.Errorf("setPixel %T %v %dx%d %v %v", img, k.px, x, y, k.c, c) |
| 459 |
} |
| 460 |
} |
| 461 |
} |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
colors3 := []struct { |
| 468 |
c color.NRGBA |
| 469 |
px pixel |
| 470 |
}{ |
| 471 |
{color.NRGBA{255, 255, 255, 255}, pixel{0, 0, 0, 1}}, |
| 472 |
{color.NRGBA{255, 255, 255, 127}, pixel{0.2, 0.5, 0.7, 0.5}}, |
| 473 |
{color.NRGBA{255, 255, 255, 63}, pixel{0.1, 0.2, 0.3, 0.25}}, |
| 474 |
} |
| 475 |
|
| 476 |
img := image.NewAlpha(image.Rect(-1, -2, 3, 4)) |
| 477 |
ps = newPixelSetter(img) |
| 478 |
for _, k := range colors3 { |
| 479 |
for _, x := range []int{-1, 0, 2} { |
| 480 |
for _, y := range []int{-2, 0, 3} { |
| 481 |
ps.setPixel(x, y, k.px) |
| 482 |
c := color.NRGBAModel.Convert(img.At(x, y)).(color.NRGBA) |
| 483 |
if !compareColorsNRGBA(c, k.c, 1) { |
| 484 |
t.Errorf("setPixel %T %v %dx%d %v %v", img, k.px, x, y, k.c, c) |
| 485 |
} |
| 486 |
} |
| 487 |
} |
| 488 |
} |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
images4 := []draw.Image{ |
| 493 |
image.NewPaletted( |
| 494 |
image.Rect(-1, -2, 3, 4), |
| 495 |
color.Palette{ |
| 496 |
color.NRGBA{0, 0, 0, 0}, |
| 497 |
color.NRGBA{0, 0, 0, 255}, |
| 498 |
color.NRGBA{255, 255, 255, 255}, |
| 499 |
color.NRGBA{50, 100, 150, 255}, |
| 500 |
color.NRGBA{150, 100, 50, 200}, |
| 501 |
color.NRGBA{1, 255, 255, 255}, |
| 502 |
color.NRGBA{2, 255, 255, 255}, |
| 503 |
color.NRGBA{3, 255, 255, 255}, |
| 504 |
}, |
| 505 |
), |
| 506 |
} |
| 507 |
|
| 508 |
colors4 := []struct { |
| 509 |
c color.NRGBA |
| 510 |
px pixel |
| 511 |
}{ |
| 512 |
{color.NRGBA{0, 0, 0, 0}, pixel{0, 0, 0, 0}}, |
| 513 |
{color.NRGBA{0, 0, 0, 255}, pixel{0, 0, 0, 1}}, |
| 514 |
{color.NRGBA{255, 255, 255, 255}, pixel{1, 1, 1, 1}}, |
| 515 |
{color.NRGBA{50, 100, 150, 255}, pixel{0.196, 0.392, 0.588, 1}}, |
| 516 |
{color.NRGBA{150, 100, 50, 200}, pixel{0.588, 0.392, 0.196, 0.784}}, |
| 517 |
{color.NRGBA{0, 0, 0, 0}, pixel{0.1, 0.01, 0.001, 0.1}}, |
| 518 |
{color.NRGBA{0, 0, 0, 255}, pixel{0, 0, 0, 0.9}}, |
| 519 |
{color.NRGBA{255, 255, 255, 255}, pixel{1, 0.9, 1, 0.9}}, |
| 520 |
{color.NRGBA{1, 255, 255, 255}, pixel{0.001 / 255, 1, 1, 1}}, |
| 521 |
{color.NRGBA{1, 255, 255, 255}, pixel{1.001 / 255, 1, 1, 1}}, |
| 522 |
{color.NRGBA{2, 255, 255, 255}, pixel{2.001 / 255, 1, 1, 1}}, |
| 523 |
{color.NRGBA{3, 255, 255, 255}, pixel{3.001 / 255, 1, 1, 1}}, |
| 524 |
{color.NRGBA{3, 255, 255, 255}, pixel{4.001 / 255, 1, 1, 1}}, |
| 525 |
} |
| 526 |
|
| 527 |
for _, img := range images4 { |
| 528 |
ps = newPixelSetter(img) |
| 529 |
for _, k := range colors4 { |
| 530 |
for _, x := range []int{-1, 0, 2} { |
| 531 |
for _, y := range []int{-2, 0, 3} { |
| 532 |
ps.setPixel(x, y, k.px) |
| 533 |
c := color.NRGBAModel.Convert(img.At(x, y)).(color.NRGBA) |
| 534 |
if !compareColorsNRGBA(c, k.c, 0) { |
| 535 |
t.Errorf("setPixel %T %v %dx%d %v %v", img, k.px, x, y, k.c, c) |
| 536 |
} |
| 537 |
} |
| 538 |
} |
| 539 |
} |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
func TestSetPixelRow(t *testing.T) { |
| 544 |
colors := []color.NRGBA{ |
| 545 |
{0, 0, 0, 0}, |
| 546 |
{255, 255, 255, 255}, |
| 547 |
{50, 100, 150, 255}, |
| 548 |
{150, 100, 50, 200}, |
| 549 |
} |
| 550 |
pixels := []pixel{ |
| 551 |
{0, 0, 0, 0}, |
| 552 |
{1, 1, 1, 1}, |
| 553 |
{0.196, 0.392, 0.588, 1}, |
| 554 |
{0.588, 0.392, 0.196, 0.784}, |
| 555 |
} |
| 556 |
|
| 557 |
img := image.NewNRGBA(image.Rect(-1, -2, 3, 2)) |
| 558 |
ps := newPixelSetter(img) |
| 559 |
for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ { |
| 560 |
ps.setPixelRow(y, pixels) |
| 561 |
for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ { |
| 562 |
c := img.At(x, y).(color.NRGBA) |
| 563 |
wantedColor := colors[x-img.Bounds().Min.X] |
| 564 |
if !compareColorsNRGBA(wantedColor, c, 1) { |
| 565 |
t.Errorf("setPixelRow y=%d x=%d %v %v", y, x, wantedColor, c) |
| 566 |
} |
| 567 |
} |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
func TestSetPixelColumn(t *testing.T) { |
| 572 |
colors := []color.NRGBA{ |
| 573 |
{0, 0, 0, 0}, |
| 574 |
{255, 255, 255, 255}, |
| 575 |
{50, 100, 150, 255}, |
| 576 |
{150, 100, 50, 200}, |
| 577 |
} |
| 578 |
pixels := []pixel{ |
| 579 |
{0, 0, 0, 0}, |
| 580 |
{1, 1, 1, 1}, |
| 581 |
{0.196, 0.392, 0.588, 1}, |
| 582 |
{0.588, 0.392, 0.196, 0.784}, |
| 583 |
} |
| 584 |
|
| 585 |
img := image.NewNRGBA(image.Rect(-1, -2, 3, 2)) |
| 586 |
ps := newPixelSetter(img) |
| 587 |
for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ { |
| 588 |
ps.setPixelColumn(x, pixels) |
| 589 |
for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ { |
| 590 |
c := img.At(x, y).(color.NRGBA) |
| 591 |
wantedColor := colors[y-img.Bounds().Min.Y] |
| 592 |
if !compareColorsNRGBA(wantedColor, c, 1) { |
| 593 |
t.Errorf("setPixelColumn x=%d y=%d %v %v", x, y, wantedColor, c) |
| 594 |
} |
| 595 |
} |
| 596 |
} |
| 597 |
} |