1 // 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.
12 type image
interface {
15 Set(int, int, color
.Color
)
16 SubImage(Rectangle
) Image
19 func cmp(cm color
.Model
, c0
, c1 color
.Color
) bool {
20 r0
, g0
, b0
, a0
:= cm
.Convert(c0
).RGBA()
21 r1
, g1
, b1
, a1
:= cm
.Convert(c1
).RGBA()
22 return r0
== r1
&& g0
== g1
&& b0
== b1
&& a0
== a1
25 func TestImage(t
*testing
.T
) {
27 NewRGBA(Rect(0, 0, 10, 10)),
28 NewRGBA64(Rect(0, 0, 10, 10)),
29 NewNRGBA(Rect(0, 0, 10, 10)),
30 NewNRGBA64(Rect(0, 0, 10, 10)),
31 NewAlpha(Rect(0, 0, 10, 10)),
32 NewAlpha16(Rect(0, 0, 10, 10)),
33 NewGray(Rect(0, 0, 10, 10)),
34 NewGray16(Rect(0, 0, 10, 10)),
35 NewPaletted(Rect(0, 0, 10, 10), color
.Palette
{
40 for _
, m
:= range testImage
{
41 if !Rect(0, 0, 10, 10).Eq(m
.Bounds()) {
42 t
.Errorf("%T: want bounds %v, got %v", m
, Rect(0, 0, 10, 10), m
.Bounds())
45 if !cmp(m
.ColorModel(), Transparent
, m
.At(6, 3)) {
46 t
.Errorf("%T: at (6, 3), want a zero color, got %v", m
, m
.At(6, 3))
50 if !cmp(m
.ColorModel(), Opaque
, m
.At(6, 3)) {
51 t
.Errorf("%T: at (6, 3), want a non-zero color, got %v", m
, m
.At(6, 3))
54 if !m
.SubImage(Rect(6, 3, 7, 4)).(image
).Opaque() {
55 t
.Errorf("%T: at (6, 3) was not opaque", m
)
58 m
= m
.SubImage(Rect(3, 2, 9, 8)).(image
)
59 if !Rect(3, 2, 9, 8).Eq(m
.Bounds()) {
60 t
.Errorf("%T: sub-image want bounds %v, got %v", m
, Rect(3, 2, 9, 8), m
.Bounds())
63 if !cmp(m
.ColorModel(), Opaque
, m
.At(6, 3)) {
64 t
.Errorf("%T: sub-image at (6, 3), want a non-zero color, got %v", m
, m
.At(6, 3))
67 if !cmp(m
.ColorModel(), Transparent
, m
.At(3, 3)) {
68 t
.Errorf("%T: sub-image at (3, 3), want a zero color, got %v", m
, m
.At(3, 3))
72 if !cmp(m
.ColorModel(), Opaque
, m
.At(3, 3)) {
73 t
.Errorf("%T: sub-image at (3, 3), want a non-zero color, got %v", m
, m
.At(3, 3))
76 // Test that taking an empty sub-image starting at a corner does not panic.
77 m
.SubImage(Rect(0, 0, 0, 0))
78 m
.SubImage(Rect(10, 0, 10, 0))
79 m
.SubImage(Rect(0, 10, 0, 10))
80 m
.SubImage(Rect(10, 10, 10, 10))
84 func Test16BitsPerColorChannel(t
*testing
.T
) {
85 testColorModel
:= []color
.Model
{
91 for _
, cm
:= range testColorModel
{
92 c
:= cm
.Convert(color
.RGBA64
{0x1234, 0x1234, 0x1234, 0x1234}) // Premultiplied alpha.
93 r
, _
, _
, _
:= c
.RGBA()
95 t
.Errorf("%T: want red value 0x%04x got 0x%04x", c
, 0x1234, r
)
100 NewRGBA64(Rect(0, 0, 10, 10)),
101 NewNRGBA64(Rect(0, 0, 10, 10)),
102 NewAlpha16(Rect(0, 0, 10, 10)),
103 NewGray16(Rect(0, 0, 10, 10)),
105 for _
, m
:= range testImage
{
106 m
.Set(1, 2, color
.NRGBA64
{0xffff, 0xffff, 0xffff, 0x1357}) // Non-premultiplied alpha.
107 r
, _
, _
, _
:= m
.At(1, 2).RGBA()
109 t
.Errorf("%T: want red value 0x%04x got 0x%04x", m
, 0x1357, r
)