2011-09-16 17:47:21 +02:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2013-02-10 07:02:38 +01:00
|
|
|
package image
|
2011-09-16 17:47:21 +02:00
|
|
|
|
|
|
|
import (
|
2011-10-27 01:57:58 +02:00
|
|
|
"image/color"
|
2011-09-16 17:47:21 +02:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2013-02-10 07:02:38 +01:00
|
|
|
type image interface {
|
2011-09-16 17:47:21 +02:00
|
|
|
Image
|
|
|
|
Opaque() bool
|
2011-10-27 01:57:58 +02:00
|
|
|
Set(int, int, color.Color)
|
2011-09-16 17:47:21 +02:00
|
|
|
SubImage(Rectangle) Image
|
|
|
|
}
|
|
|
|
|
2011-10-27 01:57:58 +02:00
|
|
|
func cmp(t *testing.T, cm color.Model, c0, c1 color.Color) bool {
|
2011-09-16 17:47:21 +02:00
|
|
|
r0, g0, b0, a0 := cm.Convert(c0).RGBA()
|
|
|
|
r1, g1, b1, a1 := cm.Convert(c1).RGBA()
|
|
|
|
return r0 == r1 && g0 == g1 && b0 == b1 && a0 == a1
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestImage(t *testing.T) {
|
2013-02-10 07:02:38 +01:00
|
|
|
testImage := []image{
|
2011-10-27 01:57:58 +02:00
|
|
|
NewRGBA(Rect(0, 0, 10, 10)),
|
|
|
|
NewRGBA64(Rect(0, 0, 10, 10)),
|
|
|
|
NewNRGBA(Rect(0, 0, 10, 10)),
|
|
|
|
NewNRGBA64(Rect(0, 0, 10, 10)),
|
|
|
|
NewAlpha(Rect(0, 0, 10, 10)),
|
|
|
|
NewAlpha16(Rect(0, 0, 10, 10)),
|
|
|
|
NewGray(Rect(0, 0, 10, 10)),
|
|
|
|
NewGray16(Rect(0, 0, 10, 10)),
|
|
|
|
NewPaletted(Rect(0, 0, 10, 10), color.Palette{
|
2011-09-16 17:47:21 +02:00
|
|
|
Transparent,
|
|
|
|
Opaque,
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
for _, m := range testImage {
|
|
|
|
if !Rect(0, 0, 10, 10).Eq(m.Bounds()) {
|
|
|
|
t.Errorf("%T: want bounds %v, got %v", m, Rect(0, 0, 10, 10), m.Bounds())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !cmp(t, m.ColorModel(), Transparent, m.At(6, 3)) {
|
|
|
|
t.Errorf("%T: at (6, 3), want a zero color, got %v", m, m.At(6, 3))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
m.Set(6, 3, Opaque)
|
|
|
|
if !cmp(t, m.ColorModel(), Opaque, m.At(6, 3)) {
|
|
|
|
t.Errorf("%T: at (6, 3), want a non-zero color, got %v", m, m.At(6, 3))
|
|
|
|
continue
|
|
|
|
}
|
2013-02-10 07:02:38 +01:00
|
|
|
if !m.SubImage(Rect(6, 3, 7, 4)).(image).Opaque() {
|
2011-09-16 17:47:21 +02:00
|
|
|
t.Errorf("%T: at (6, 3) was not opaque", m)
|
|
|
|
continue
|
|
|
|
}
|
2013-02-10 07:02:38 +01:00
|
|
|
m = m.SubImage(Rect(3, 2, 9, 8)).(image)
|
2011-09-16 17:47:21 +02:00
|
|
|
if !Rect(3, 2, 9, 8).Eq(m.Bounds()) {
|
|
|
|
t.Errorf("%T: sub-image want bounds %v, got %v", m, Rect(3, 2, 9, 8), m.Bounds())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !cmp(t, m.ColorModel(), Opaque, m.At(6, 3)) {
|
|
|
|
t.Errorf("%T: sub-image at (6, 3), want a non-zero color, got %v", m, m.At(6, 3))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !cmp(t, m.ColorModel(), Transparent, m.At(3, 3)) {
|
|
|
|
t.Errorf("%T: sub-image at (3, 3), want a zero color, got %v", m, m.At(3, 3))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
m.Set(3, 3, Opaque)
|
|
|
|
if !cmp(t, m.ColorModel(), Opaque, m.At(3, 3)) {
|
|
|
|
t.Errorf("%T: sub-image at (3, 3), want a non-zero color, got %v", m, m.At(3, 3))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Test that taking an empty sub-image starting at a corner does not panic.
|
|
|
|
m.SubImage(Rect(0, 0, 0, 0))
|
|
|
|
m.SubImage(Rect(10, 0, 10, 0))
|
|
|
|
m.SubImage(Rect(0, 10, 0, 10))
|
|
|
|
m.SubImage(Rect(10, 10, 10, 10))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test16BitsPerColorChannel(t *testing.T) {
|
2011-10-27 01:57:58 +02:00
|
|
|
testColorModel := []color.Model{
|
|
|
|
color.RGBA64Model,
|
|
|
|
color.NRGBA64Model,
|
|
|
|
color.Alpha16Model,
|
|
|
|
color.Gray16Model,
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
for _, cm := range testColorModel {
|
2011-10-27 01:57:58 +02:00
|
|
|
c := cm.Convert(color.RGBA64{0x1234, 0x1234, 0x1234, 0x1234}) // Premultiplied alpha.
|
2011-09-16 17:47:21 +02:00
|
|
|
r, _, _, _ := c.RGBA()
|
|
|
|
if r != 0x1234 {
|
|
|
|
t.Errorf("%T: want red value 0x%04x got 0x%04x", c, 0x1234, r)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2013-02-10 07:02:38 +01:00
|
|
|
testImage := []image{
|
2011-10-27 01:57:58 +02:00
|
|
|
NewRGBA64(Rect(0, 0, 10, 10)),
|
|
|
|
NewNRGBA64(Rect(0, 0, 10, 10)),
|
|
|
|
NewAlpha16(Rect(0, 0, 10, 10)),
|
|
|
|
NewGray16(Rect(0, 0, 10, 10)),
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
for _, m := range testImage {
|
2011-10-27 01:57:58 +02:00
|
|
|
m.Set(1, 2, color.NRGBA64{0xffff, 0xffff, 0xffff, 0x1357}) // Non-premultiplied alpha.
|
2011-09-16 17:47:21 +02:00
|
|
|
r, _, _, _ := m.At(1, 2).RGBA()
|
|
|
|
if r != 0x1357 {
|
|
|
|
t.Errorf("%T: want red value 0x%04x got 0x%04x", m, 0x1357, r)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|