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. 4 5 package image 6 7 import ( 8 "image/color" 9 "testing" 10 ) 11 12 type image interface { 13 Image 14 Opaque() bool 15 Set(int, int, color.Color) 16 SubImage(Rectangle) Image 17 } 18 19 func cmp(t *testing.T, 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 23 } 24 25 func TestImage(t *testing.T) { 26 testImage := []image{ 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{ 36 Transparent, 37 Opaque, 38 }), 39 } 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()) 43 continue 44 } 45 if !cmp(t, 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)) 47 continue 48 } 49 m.Set(6, 3, Opaque) 50 if !cmp(t, 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)) 52 continue 53 } 54 if !m.SubImage(Rect(6, 3, 7, 4)).(image).Opaque() { 55 t.Errorf("%T: at (6, 3) was not opaque", m) 56 continue 57 } 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()) 61 continue 62 } 63 if !cmp(t, 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)) 65 continue 66 } 67 if !cmp(t, 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)) 69 continue 70 } 71 m.Set(3, 3, Opaque) 72 if !cmp(t, 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)) 74 continue 75 } 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)) 81 } 82 } 83 84 func Test16BitsPerColorChannel(t *testing.T) { 85 testColorModel := []color.Model{ 86 color.RGBA64Model, 87 color.NRGBA64Model, 88 color.Alpha16Model, 89 color.Gray16Model, 90 } 91 for _, cm := range testColorModel { 92 c := cm.Convert(color.RGBA64{0x1234, 0x1234, 0x1234, 0x1234}) // Premultiplied alpha. 93 r, _, _, _ := c.RGBA() 94 if r != 0x1234 { 95 t.Errorf("%T: want red value 0x%04x got 0x%04x", c, 0x1234, r) 96 continue 97 } 98 } 99 testImage := []image{ 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)), 104 } 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() 108 if r != 0x1357 { 109 t.Errorf("%T: want red value 0x%04x got 0x%04x", m, 0x1357, r) 110 continue 111 } 112 } 113 } 114