~john-koepi/ubuntu/trusty/golang/default

« back to all changes in this revision

Viewing changes to src/pkg/image/png/reader_test.go

  • Committer: Bazaar Package Importer
  • Author(s): Ondřej Surý
  • Date: 2011-04-20 17:36:48 UTC
  • Revision ID: james.westby@ubuntu.com-20110420173648-ifergoxyrm832trd
Tags: upstream-2011.03.07.1
Import upstream version 2011.03.07.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2009 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 png
 
6
 
 
7
import (
 
8
        "bufio"
 
9
        "fmt"
 
10
        "image"
 
11
        "io"
 
12
        "os"
 
13
        "testing"
 
14
)
 
15
 
 
16
var filenames = []string{
 
17
        "basn0g01",
 
18
        "basn0g01-30",
 
19
        "basn0g02",
 
20
        "basn0g02-29",
 
21
        "basn0g04",
 
22
        "basn0g04-31",
 
23
        "basn0g08",
 
24
        "basn0g16",
 
25
        "basn2c08",
 
26
        "basn2c16",
 
27
        "basn3p01",
 
28
        "basn3p02",
 
29
        "basn3p04",
 
30
        "basn3p08",
 
31
        "basn4a08",
 
32
        "basn4a16",
 
33
        "basn6a08",
 
34
        "basn6a16",
 
35
}
 
36
 
 
37
func readPng(filename string) (image.Image, os.Error) {
 
38
        f, err := os.Open(filename, os.O_RDONLY, 0444)
 
39
        if err != nil {
 
40
                return nil, err
 
41
        }
 
42
        defer f.Close()
 
43
        return Decode(f)
 
44
}
 
45
 
 
46
// An approximation of the sng command-line tool.
 
47
func sng(w io.WriteCloser, filename string, png image.Image) {
 
48
        defer w.Close()
 
49
        bounds := png.Bounds()
 
50
        cm := png.ColorModel()
 
51
        var bitdepth int
 
52
        switch cm {
 
53
        case image.RGBAColorModel, image.NRGBAColorModel, image.AlphaColorModel, image.GrayColorModel:
 
54
                bitdepth = 8
 
55
        default:
 
56
                bitdepth = 16
 
57
        }
 
58
        cpm, _ := cm.(image.PalettedColorModel)
 
59
        var paletted *image.Paletted
 
60
        if cpm != nil {
 
61
                switch {
 
62
                case len(cpm) <= 2:
 
63
                        bitdepth = 1
 
64
                case len(cpm) <= 4:
 
65
                        bitdepth = 2
 
66
                case len(cpm) <= 16:
 
67
                        bitdepth = 4
 
68
                default:
 
69
                        bitdepth = 8
 
70
                }
 
71
                paletted = png.(*image.Paletted)
 
72
        }
 
73
 
 
74
        // Write the filename and IHDR.
 
75
        io.WriteString(w, "#SNG: from "+filename+".png\nIHDR {\n")
 
76
        fmt.Fprintf(w, "    width: %d; height: %d; bitdepth: %d;\n", bounds.Dx(), bounds.Dy(), bitdepth)
 
77
        switch {
 
78
        case cm == image.RGBAColorModel, cm == image.RGBA64ColorModel:
 
79
                io.WriteString(w, "    using color;\n")
 
80
        case cm == image.NRGBAColorModel, cm == image.NRGBA64ColorModel:
 
81
                io.WriteString(w, "    using color alpha;\n")
 
82
        case cm == image.GrayColorModel, cm == image.Gray16ColorModel:
 
83
                io.WriteString(w, "    using grayscale;\n")
 
84
        case cpm != nil:
 
85
                io.WriteString(w, "    using color palette;\n")
 
86
        default:
 
87
                io.WriteString(w, "unknown PNG decoder color model\n")
 
88
        }
 
89
        io.WriteString(w, "}\n")
 
90
 
 
91
        // We fake a gAMA output. The test files have a gAMA chunk but the go PNG parser ignores it
 
92
        // (the PNG spec section 11.3 says "Ancillary chunks may be ignored by a decoder").
 
93
        io.WriteString(w, "gAMA {1.0000}\n")
 
94
 
 
95
        // Write the PLTE (if applicable).
 
96
        if cpm != nil {
 
97
                io.WriteString(w, "PLTE {\n")
 
98
                for i := 0; i < len(cpm); i++ {
 
99
                        r, g, b, _ := cpm[i].RGBA()
 
100
                        r >>= 8
 
101
                        g >>= 8
 
102
                        b >>= 8
 
103
                        fmt.Fprintf(w, "    (%3d,%3d,%3d)     # rgb = (0x%02x,0x%02x,0x%02x)\n", r, g, b, r, g, b)
 
104
                }
 
105
                io.WriteString(w, "}\n")
 
106
        }
 
107
 
 
108
        // Write the IMAGE.
 
109
        io.WriteString(w, "IMAGE {\n    pixels hex\n")
 
110
        for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
 
111
                switch {
 
112
                case cm == image.GrayColorModel:
 
113
                        for x := bounds.Min.X; x < bounds.Max.X; x++ {
 
114
                                gray := png.At(x, y).(image.GrayColor)
 
115
                                fmt.Fprintf(w, "%02x", gray.Y)
 
116
                        }
 
117
                case cm == image.Gray16ColorModel:
 
118
                        for x := bounds.Min.X; x < bounds.Max.X; x++ {
 
119
                                gray16 := png.At(x, y).(image.Gray16Color)
 
120
                                fmt.Fprintf(w, "%04x ", gray16.Y)
 
121
                        }
 
122
                case cm == image.RGBAColorModel:
 
123
                        for x := bounds.Min.X; x < bounds.Max.X; x++ {
 
124
                                rgba := png.At(x, y).(image.RGBAColor)
 
125
                                fmt.Fprintf(w, "%02x%02x%02x ", rgba.R, rgba.G, rgba.B)
 
126
                        }
 
127
                case cm == image.RGBA64ColorModel:
 
128
                        for x := bounds.Min.X; x < bounds.Max.X; x++ {
 
129
                                rgba64 := png.At(x, y).(image.RGBA64Color)
 
130
                                fmt.Fprintf(w, "%04x%04x%04x ", rgba64.R, rgba64.G, rgba64.B)
 
131
                        }
 
132
                case cm == image.NRGBAColorModel:
 
133
                        for x := bounds.Min.X; x < bounds.Max.X; x++ {
 
134
                                nrgba := png.At(x, y).(image.NRGBAColor)
 
135
                                fmt.Fprintf(w, "%02x%02x%02x%02x ", nrgba.R, nrgba.G, nrgba.B, nrgba.A)
 
136
                        }
 
137
                case cm == image.NRGBA64ColorModel:
 
138
                        for x := bounds.Min.X; x < bounds.Max.X; x++ {
 
139
                                nrgba64 := png.At(x, y).(image.NRGBA64Color)
 
140
                                fmt.Fprintf(w, "%04x%04x%04x%04x ", nrgba64.R, nrgba64.G, nrgba64.B, nrgba64.A)
 
141
                        }
 
142
                case cpm != nil:
 
143
                        var b, c int
 
144
                        for x := bounds.Min.X; x < bounds.Max.X; x++ {
 
145
                                b = b<<uint(bitdepth) | int(paletted.ColorIndexAt(x, y))
 
146
                                c++
 
147
                                if c == 8/bitdepth {
 
148
                                        fmt.Fprintf(w, "%02x", b)
 
149
                                        b = 0
 
150
                                        c = 0
 
151
                                }
 
152
                        }
 
153
                }
 
154
                io.WriteString(w, "\n")
 
155
        }
 
156
        io.WriteString(w, "}\n")
 
157
}
 
158
 
 
159
func TestReader(t *testing.T) {
 
160
        for _, fn := range filenames {
 
161
                // Read the .png file.
 
162
                img, err := readPng("testdata/pngsuite/" + fn + ".png")
 
163
                if err != nil {
 
164
                        t.Error(fn, err)
 
165
                        continue
 
166
                }
 
167
 
 
168
                if fn == "basn4a16" {
 
169
                        // basn4a16.sng is gray + alpha but sng() will produce true color + alpha
 
170
                        // so we just check a single random pixel.
 
171
                        c := img.At(2, 1).(image.NRGBA64Color)
 
172
                        if c.R != 0x11a7 || c.G != 0x11a7 || c.B != 0x11a7 || c.A != 0x1085 {
 
173
                                t.Error(fn, fmt.Errorf("wrong pixel value at (2, 1): %x", c))
 
174
                        }
 
175
                        continue
 
176
                }
 
177
 
 
178
                piper, pipew := io.Pipe()
 
179
                pb := bufio.NewReader(piper)
 
180
                go sng(pipew, fn, img)
 
181
                defer piper.Close()
 
182
 
 
183
                // Read the .sng file.
 
184
                sf, err := os.Open("testdata/pngsuite/"+fn+".sng", os.O_RDONLY, 0444)
 
185
                if err != nil {
 
186
                        t.Error(fn, err)
 
187
                        continue
 
188
                }
 
189
                defer sf.Close()
 
190
                sb := bufio.NewReader(sf)
 
191
                if err != nil {
 
192
                        t.Error(fn, err)
 
193
                        continue
 
194
                }
 
195
 
 
196
                // Compare the two, in SNG format, line by line.
 
197
                for {
 
198
                        ps, perr := pb.ReadString('\n')
 
199
                        ss, serr := sb.ReadString('\n')
 
200
                        if perr == os.EOF && serr == os.EOF {
 
201
                                break
 
202
                        }
 
203
                        if perr != nil {
 
204
                                t.Error(fn, perr)
 
205
                                break
 
206
                        }
 
207
                        if serr != nil {
 
208
                                t.Error(fn, serr)
 
209
                                break
 
210
                        }
 
211
                        if ps != ss {
 
212
                                t.Errorf("%s: Mismatch\n%sversus\n%s\n", fn, ps, ss)
 
213
                                break
 
214
                        }
 
215
                }
 
216
        }
 
217
}