OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [image/] [png/] [reader_test.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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
        "image/color"
12
        "io"
13
        "os"
14
        "strings"
15
        "testing"
16
)
17
 
18
var filenames = []string{
19
        "basn0g01",
20
        "basn0g01-30",
21
        "basn0g02",
22
        "basn0g02-29",
23
        "basn0g04",
24
        "basn0g04-31",
25
        "basn0g08",
26
        "basn0g16",
27
        "basn2c08",
28
        "basn2c16",
29
        "basn3p01",
30
        "basn3p02",
31
        "basn3p04",
32
        "basn3p08",
33
        "basn3p08-trns",
34
        "basn4a08",
35
        "basn4a16",
36
        "basn6a08",
37
        "basn6a16",
38
}
39
 
40
var filenamesShort = []string{
41
        "basn0g01",
42
        "basn0g04-31",
43
        "basn6a16",
44
}
45
 
46
func readPNG(filename string) (image.Image, error) {
47
        f, err := os.Open(filename)
48
        if err != nil {
49
                return nil, err
50
        }
51
        defer f.Close()
52
        return Decode(f)
53
}
54
 
55
// An approximation of the sng command-line tool.
56
func sng(w io.WriteCloser, filename string, png image.Image) {
57
        defer w.Close()
58
        bounds := png.Bounds()
59
        cm := png.ColorModel()
60
        var bitdepth int
61
        switch cm {
62
        case color.RGBAModel, color.NRGBAModel, color.AlphaModel, color.GrayModel:
63
                bitdepth = 8
64
        default:
65
                bitdepth = 16
66
        }
67
        cpm, _ := cm.(color.Palette)
68
        var paletted *image.Paletted
69
        if cpm != nil {
70
                switch {
71
                case len(cpm) <= 2:
72
                        bitdepth = 1
73
                case len(cpm) <= 4:
74
                        bitdepth = 2
75
                case len(cpm) <= 16:
76
                        bitdepth = 4
77
                default:
78
                        bitdepth = 8
79
                }
80
                paletted = png.(*image.Paletted)
81
        }
82
 
83
        // Write the filename and IHDR.
84
        io.WriteString(w, "#SNG: from "+filename+".png\nIHDR {\n")
85
        fmt.Fprintf(w, "    width: %d; height: %d; bitdepth: %d;\n", bounds.Dx(), bounds.Dy(), bitdepth)
86
        switch {
87
        case cm == color.RGBAModel, cm == color.RGBA64Model:
88
                io.WriteString(w, "    using color;\n")
89
        case cm == color.NRGBAModel, cm == color.NRGBA64Model:
90
                io.WriteString(w, "    using color alpha;\n")
91
        case cm == color.GrayModel, cm == color.Gray16Model:
92
                io.WriteString(w, "    using grayscale;\n")
93
        case cpm != nil:
94
                io.WriteString(w, "    using color palette;\n")
95
        default:
96
                io.WriteString(w, "unknown PNG decoder color model\n")
97
        }
98
        io.WriteString(w, "}\n")
99
 
100
        // We fake a gAMA output. The test files have a gAMA chunk but the go PNG parser ignores it
101
        // (the PNG spec section 11.3 says "Ancillary chunks may be ignored by a decoder").
102
        io.WriteString(w, "gAMA {1.0000}\n")
103
 
104
        // Write the PLTE and tRNS (if applicable).
105
        if cpm != nil {
106
                lastAlpha := -1
107
                io.WriteString(w, "PLTE {\n")
108
                for i, c := range cpm {
109
                        r, g, b, a := c.RGBA()
110
                        if a != 0xffff {
111
                                lastAlpha = i
112
                        }
113
                        r >>= 8
114
                        g >>= 8
115
                        b >>= 8
116
                        fmt.Fprintf(w, "    (%3d,%3d,%3d)     # rgb = (0x%02x,0x%02x,0x%02x)\n", r, g, b, r, g, b)
117
                }
118
                io.WriteString(w, "}\n")
119
                if lastAlpha != -1 {
120
                        io.WriteString(w, "tRNS {\n")
121
                        for i := 0; i <= lastAlpha; i++ {
122
                                _, _, _, a := cpm[i].RGBA()
123
                                a >>= 8
124
                                fmt.Fprintf(w, " %d", a)
125
                        }
126
                        io.WriteString(w, "}\n")
127
                }
128
        }
129
 
130
        // Write the IMAGE.
131
        io.WriteString(w, "IMAGE {\n    pixels hex\n")
132
        for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
133
                switch {
134
                case cm == color.GrayModel:
135
                        for x := bounds.Min.X; x < bounds.Max.X; x++ {
136
                                gray := png.At(x, y).(color.Gray)
137
                                fmt.Fprintf(w, "%02x", gray.Y)
138
                        }
139
                case cm == color.Gray16Model:
140
                        for x := bounds.Min.X; x < bounds.Max.X; x++ {
141
                                gray16 := png.At(x, y).(color.Gray16)
142
                                fmt.Fprintf(w, "%04x ", gray16.Y)
143
                        }
144
                case cm == color.RGBAModel:
145
                        for x := bounds.Min.X; x < bounds.Max.X; x++ {
146
                                rgba := png.At(x, y).(color.RGBA)
147
                                fmt.Fprintf(w, "%02x%02x%02x ", rgba.R, rgba.G, rgba.B)
148
                        }
149
                case cm == color.RGBA64Model:
150
                        for x := bounds.Min.X; x < bounds.Max.X; x++ {
151
                                rgba64 := png.At(x, y).(color.RGBA64)
152
                                fmt.Fprintf(w, "%04x%04x%04x ", rgba64.R, rgba64.G, rgba64.B)
153
                        }
154
                case cm == color.NRGBAModel:
155
                        for x := bounds.Min.X; x < bounds.Max.X; x++ {
156
                                nrgba := png.At(x, y).(color.NRGBA)
157
                                fmt.Fprintf(w, "%02x%02x%02x%02x ", nrgba.R, nrgba.G, nrgba.B, nrgba.A)
158
                        }
159
                case cm == color.NRGBA64Model:
160
                        for x := bounds.Min.X; x < bounds.Max.X; x++ {
161
                                nrgba64 := png.At(x, y).(color.NRGBA64)
162
                                fmt.Fprintf(w, "%04x%04x%04x%04x ", nrgba64.R, nrgba64.G, nrgba64.B, nrgba64.A)
163
                        }
164
                case cpm != nil:
165
                        var b, c int
166
                        for x := bounds.Min.X; x < bounds.Max.X; x++ {
167
                                b = b<
168
                                c++
169
                                if c == 8/bitdepth {
170
                                        fmt.Fprintf(w, "%02x", b)
171
                                        b = 0
172
                                        c = 0
173
                                }
174
                        }
175
                }
176
                io.WriteString(w, "\n")
177
        }
178
        io.WriteString(w, "}\n")
179
}
180
 
181
func TestReader(t *testing.T) {
182
        names := filenames
183
        if testing.Short() {
184
                names = filenamesShort
185
        }
186
        for _, fn := range names {
187
                // Read the .png file.
188
                img, err := readPNG("testdata/pngsuite/" + fn + ".png")
189
                if err != nil {
190
                        t.Error(fn, err)
191
                        continue
192
                }
193
 
194
                if fn == "basn4a16" {
195
                        // basn4a16.sng is gray + alpha but sng() will produce true color + alpha
196
                        // so we just check a single random pixel.
197
                        c := img.At(2, 1).(color.NRGBA64)
198
                        if c.R != 0x11a7 || c.G != 0x11a7 || c.B != 0x11a7 || c.A != 0x1085 {
199
                                t.Error(fn, fmt.Errorf("wrong pixel value at (2, 1): %x", c))
200
                        }
201
                        continue
202
                }
203
 
204
                piper, pipew := io.Pipe()
205
                pb := bufio.NewReader(piper)
206
                go sng(pipew, fn, img)
207
                defer piper.Close()
208
 
209
                // Read the .sng file.
210
                sf, err := os.Open("testdata/pngsuite/" + fn + ".sng")
211
                if err != nil {
212
                        t.Error(fn, err)
213
                        continue
214
                }
215
                defer sf.Close()
216
                sb := bufio.NewReader(sf)
217
                if err != nil {
218
                        t.Error(fn, err)
219
                        continue
220
                }
221
 
222
                // Compare the two, in SNG format, line by line.
223
                for {
224
                        ps, perr := pb.ReadString('\n')
225
                        ss, serr := sb.ReadString('\n')
226
                        if perr == io.EOF && serr == io.EOF {
227
                                break
228
                        }
229
                        if perr != nil {
230
                                t.Error(fn, perr)
231
                                break
232
                        }
233
                        if serr != nil {
234
                                t.Error(fn, serr)
235
                                break
236
                        }
237
                        if ps != ss {
238
                                t.Errorf("%s: Mismatch\n%sversus\n%s\n", fn, ps, ss)
239
                                break
240
                        }
241
                }
242
        }
243
}
244
 
245
var readerErrors = []struct {
246
        file string
247
        err  string
248
}{
249
        {"invalid-zlib.png", "zlib checksum error"},
250
        {"invalid-crc32.png", "invalid checksum"},
251
        {"invalid-noend.png", "unexpected EOF"},
252
        {"invalid-trunc.png", "unexpected EOF"},
253
}
254
 
255
func TestReaderError(t *testing.T) {
256
        for _, tt := range readerErrors {
257
                img, err := readPNG("testdata/" + tt.file)
258
                if err == nil {
259
                        t.Errorf("decoding %s: missing error", tt.file)
260
                        continue
261
                }
262
                if !strings.Contains(err.Error(), tt.err) {
263
                        t.Errorf("decoding %s: %s, want %s", tt.file, err, tt.err)
264
                }
265
                if img != nil {
266
                        t.Errorf("decoding %s: have image + error", tt.file)
267
                }
268
        }
269
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.