| 1 |
747 |
jeremybenn |
// Copyright 2011 The Go Authors. All rights reserved.
|
| 2 |
|
|
// Use of this source code is governed by a BSD-style
|
| 3 |
|
|
// license that can be found in the LICENSE file.
|
| 4 |
|
|
|
| 5 |
|
|
package image_test
|
| 6 |
|
|
|
| 7 |
|
|
import (
|
| 8 |
|
|
. "image"
|
| 9 |
|
|
"image/color"
|
| 10 |
|
|
"testing"
|
| 11 |
|
|
)
|
| 12 |
|
|
|
| 13 |
|
|
type image interface {
|
| 14 |
|
|
Image
|
| 15 |
|
|
Opaque() bool
|
| 16 |
|
|
Set(int, int, color.Color)
|
| 17 |
|
|
SubImage(Rectangle) Image
|
| 18 |
|
|
}
|
| 19 |
|
|
|
| 20 |
|
|
func cmp(t *testing.T, cm color.Model, c0, c1 color.Color) bool {
|
| 21 |
|
|
r0, g0, b0, a0 := cm.Convert(c0).RGBA()
|
| 22 |
|
|
r1, g1, b1, a1 := cm.Convert(c1).RGBA()
|
| 23 |
|
|
return r0 == r1 && g0 == g1 && b0 == b1 && a0 == a1
|
| 24 |
|
|
}
|
| 25 |
|
|
|
| 26 |
|
|
func TestImage(t *testing.T) {
|
| 27 |
|
|
testImage := []image{
|
| 28 |
|
|
NewRGBA(Rect(0, 0, 10, 10)),
|
| 29 |
|
|
NewRGBA64(Rect(0, 0, 10, 10)),
|
| 30 |
|
|
NewNRGBA(Rect(0, 0, 10, 10)),
|
| 31 |
|
|
NewNRGBA64(Rect(0, 0, 10, 10)),
|
| 32 |
|
|
NewAlpha(Rect(0, 0, 10, 10)),
|
| 33 |
|
|
NewAlpha16(Rect(0, 0, 10, 10)),
|
| 34 |
|
|
NewGray(Rect(0, 0, 10, 10)),
|
| 35 |
|
|
NewGray16(Rect(0, 0, 10, 10)),
|
| 36 |
|
|
NewPaletted(Rect(0, 0, 10, 10), color.Palette{
|
| 37 |
|
|
Transparent,
|
| 38 |
|
|
Opaque,
|
| 39 |
|
|
}),
|
| 40 |
|
|
}
|
| 41 |
|
|
for _, m := range testImage {
|
| 42 |
|
|
if !Rect(0, 0, 10, 10).Eq(m.Bounds()) {
|
| 43 |
|
|
t.Errorf("%T: want bounds %v, got %v", m, Rect(0, 0, 10, 10), m.Bounds())
|
| 44 |
|
|
continue
|
| 45 |
|
|
}
|
| 46 |
|
|
if !cmp(t, m.ColorModel(), Transparent, m.At(6, 3)) {
|
| 47 |
|
|
t.Errorf("%T: at (6, 3), want a zero color, got %v", m, m.At(6, 3))
|
| 48 |
|
|
continue
|
| 49 |
|
|
}
|
| 50 |
|
|
m.Set(6, 3, Opaque)
|
| 51 |
|
|
if !cmp(t, m.ColorModel(), Opaque, m.At(6, 3)) {
|
| 52 |
|
|
t.Errorf("%T: at (6, 3), want a non-zero color, got %v", m, m.At(6, 3))
|
| 53 |
|
|
continue
|
| 54 |
|
|
}
|
| 55 |
|
|
if !m.SubImage(Rect(6, 3, 7, 4)).(image).Opaque() {
|
| 56 |
|
|
t.Errorf("%T: at (6, 3) was not opaque", m)
|
| 57 |
|
|
continue
|
| 58 |
|
|
}
|
| 59 |
|
|
m = m.SubImage(Rect(3, 2, 9, 8)).(image)
|
| 60 |
|
|
if !Rect(3, 2, 9, 8).Eq(m.Bounds()) {
|
| 61 |
|
|
t.Errorf("%T: sub-image want bounds %v, got %v", m, Rect(3, 2, 9, 8), m.Bounds())
|
| 62 |
|
|
continue
|
| 63 |
|
|
}
|
| 64 |
|
|
if !cmp(t, m.ColorModel(), Opaque, m.At(6, 3)) {
|
| 65 |
|
|
t.Errorf("%T: sub-image at (6, 3), want a non-zero color, got %v", m, m.At(6, 3))
|
| 66 |
|
|
continue
|
| 67 |
|
|
}
|
| 68 |
|
|
if !cmp(t, m.ColorModel(), Transparent, m.At(3, 3)) {
|
| 69 |
|
|
t.Errorf("%T: sub-image at (3, 3), want a zero color, got %v", m, m.At(3, 3))
|
| 70 |
|
|
continue
|
| 71 |
|
|
}
|
| 72 |
|
|
m.Set(3, 3, Opaque)
|
| 73 |
|
|
if !cmp(t, m.ColorModel(), Opaque, m.At(3, 3)) {
|
| 74 |
|
|
t.Errorf("%T: sub-image at (3, 3), want a non-zero color, got %v", m, m.At(3, 3))
|
| 75 |
|
|
continue
|
| 76 |
|
|
}
|
| 77 |
|
|
// Test that taking an empty sub-image starting at a corner does not panic.
|
| 78 |
|
|
m.SubImage(Rect(0, 0, 0, 0))
|
| 79 |
|
|
m.SubImage(Rect(10, 0, 10, 0))
|
| 80 |
|
|
m.SubImage(Rect(0, 10, 0, 10))
|
| 81 |
|
|
m.SubImage(Rect(10, 10, 10, 10))
|
| 82 |
|
|
}
|
| 83 |
|
|
}
|
| 84 |
|
|
|
| 85 |
|
|
func Test16BitsPerColorChannel(t *testing.T) {
|
| 86 |
|
|
testColorModel := []color.Model{
|
| 87 |
|
|
color.RGBA64Model,
|
| 88 |
|
|
color.NRGBA64Model,
|
| 89 |
|
|
color.Alpha16Model,
|
| 90 |
|
|
color.Gray16Model,
|
| 91 |
|
|
}
|
| 92 |
|
|
for _, cm := range testColorModel {
|
| 93 |
|
|
c := cm.Convert(color.RGBA64{0x1234, 0x1234, 0x1234, 0x1234}) // Premultiplied alpha.
|
| 94 |
|
|
r, _, _, _ := c.RGBA()
|
| 95 |
|
|
if r != 0x1234 {
|
| 96 |
|
|
t.Errorf("%T: want red value 0x%04x got 0x%04x", c, 0x1234, r)
|
| 97 |
|
|
continue
|
| 98 |
|
|
}
|
| 99 |
|
|
}
|
| 100 |
|
|
testImage := []image{
|
| 101 |
|
|
NewRGBA64(Rect(0, 0, 10, 10)),
|
| 102 |
|
|
NewNRGBA64(Rect(0, 0, 10, 10)),
|
| 103 |
|
|
NewAlpha16(Rect(0, 0, 10, 10)),
|
| 104 |
|
|
NewGray16(Rect(0, 0, 10, 10)),
|
| 105 |
|
|
}
|
| 106 |
|
|
for _, m := range testImage {
|
| 107 |
|
|
m.Set(1, 2, color.NRGBA64{0xffff, 0xffff, 0xffff, 0x1357}) // Non-premultiplied alpha.
|
| 108 |
|
|
r, _, _, _ := m.At(1, 2).RGBA()
|
| 109 |
|
|
if r != 0x1357 {
|
| 110 |
|
|
t.Errorf("%T: want red value 0x%04x got 0x%04x", m, 0x1357, r)
|
| 111 |
|
|
continue
|
| 112 |
|
|
}
|
| 113 |
|
|
}
|
| 114 |
|
|
}
|