~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 14:06:23 UTC
  • mfrom: (14.1.23 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130820140623-b414jfxi3m0qkmrq
Tags: 2:1.1.2-2ubuntu1
* Merge from Debian unstable (LP: #1211749, #1202027). Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - d/control,control.cross: Update Breaks/Replaces for Ubuntu
    versions to ensure smooth upgrades, regenerate control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
        "image"
11
11
        "image/color"
12
12
        "io"
 
13
        "io/ioutil"
13
14
        "os"
14
15
        "strings"
15
16
        "testing"
37
38
        "basn6a16",
38
39
}
39
40
 
 
41
var filenamesPaletted = []string{
 
42
        "basn3p01",
 
43
        "basn3p02",
 
44
        "basn3p04",
 
45
        "basn3p08",
 
46
        "basn3p08-trns",
 
47
}
 
48
 
40
49
var filenamesShort = []string{
41
50
        "basn0g01",
42
51
        "basn0g04-31",
106
115
                lastAlpha := -1
107
116
                io.WriteString(w, "PLTE {\n")
108
117
                for i, c := range cpm {
109
 
                        r, g, b, a := c.RGBA()
110
 
                        if a != 0xffff {
 
118
                        var r, g, b, a uint8
 
119
                        switch c := c.(type) {
 
120
                        case color.RGBA:
 
121
                                r, g, b, a = c.R, c.G, c.B, 0xff
 
122
                        case color.NRGBA:
 
123
                                r, g, b, a = c.R, c.G, c.B, c.A
 
124
                        default:
 
125
                                panic("unknown palette color type")
 
126
                        }
 
127
                        if a != 0xff {
111
128
                                lastAlpha = i
112
129
                        }
113
 
                        r >>= 8
114
 
                        g >>= 8
115
 
                        b >>= 8
116
130
                        fmt.Fprintf(w, "    (%3d,%3d,%3d)     # rgb = (0x%02x,0x%02x,0x%02x)\n", r, g, b, r, g, b)
117
131
                }
118
132
                io.WriteString(w, "}\n")
202
216
                }
203
217
 
204
218
                piper, pipew := io.Pipe()
205
 
                pb := bufio.NewReader(piper)
 
219
                pb := bufio.NewScanner(piper)
206
220
                go sng(pipew, fn, img)
207
221
                defer piper.Close()
208
222
 
213
227
                        continue
214
228
                }
215
229
                defer sf.Close()
216
 
                sb := bufio.NewReader(sf)
 
230
                sb := bufio.NewScanner(sf)
217
231
                if err != nil {
218
232
                        t.Error(fn, err)
219
233
                        continue
221
235
 
222
236
                // Compare the two, in SNG format, line by line.
223
237
                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
 
                        }
 
238
                        pdone := pb.Scan()
 
239
                        sdone := sb.Scan()
 
240
                        if pdone && sdone {
 
241
                                break
 
242
                        }
 
243
                        if pdone || sdone {
 
244
                                t.Errorf("%s: Different sizes", fn)
 
245
                                break
 
246
                        }
 
247
                        ps := pb.Text()
 
248
                        ss := sb.Text()
237
249
                        if ps != ss {
238
250
                                t.Errorf("%s: Mismatch\n%sversus\n%s\n", fn, ps, ss)
239
251
                                break
240
252
                        }
241
253
                }
 
254
                if pb.Err() != nil {
 
255
                        t.Error(fn, pb.Err())
 
256
                }
 
257
                if sb.Err() != nil {
 
258
                        t.Error(fn, sb.Err())
 
259
                }
242
260
        }
243
261
}
244
262
 
267
285
                }
268
286
        }
269
287
}
 
288
 
 
289
func TestPalettedDecodeConfig(t *testing.T) {
 
290
        for _, fn := range filenamesPaletted {
 
291
                f, err := os.Open("testdata/pngsuite/" + fn + ".png")
 
292
                if err != nil {
 
293
                        t.Errorf("%s: open failed: %v", fn, err)
 
294
                        continue
 
295
                }
 
296
                defer f.Close()
 
297
                cfg, err := DecodeConfig(f)
 
298
                if err != nil {
 
299
                        t.Errorf("%s: %v", fn, err)
 
300
                        continue
 
301
                }
 
302
                pal, ok := cfg.ColorModel.(color.Palette)
 
303
                if !ok {
 
304
                        t.Errorf("%s: expected paletted color model", fn)
 
305
                        continue
 
306
                }
 
307
                if pal == nil {
 
308
                        t.Errorf("%s: palette not initialized", fn)
 
309
                        continue
 
310
                }
 
311
        }
 
312
}
 
313
 
 
314
func benchmarkDecode(b *testing.B, filename string, bytesPerPixel int) {
 
315
        b.StopTimer()
 
316
        data, err := ioutil.ReadFile(filename)
 
317
        if err != nil {
 
318
                b.Fatal(err)
 
319
        }
 
320
        s := string(data)
 
321
        cfg, err := DecodeConfig(strings.NewReader(s))
 
322
        if err != nil {
 
323
                b.Fatal(err)
 
324
        }
 
325
        b.SetBytes(int64(cfg.Width * cfg.Height * bytesPerPixel))
 
326
        b.StartTimer()
 
327
        for i := 0; i < b.N; i++ {
 
328
                Decode(strings.NewReader(s))
 
329
        }
 
330
}
 
331
 
 
332
func BenchmarkDecodeGray(b *testing.B) {
 
333
        benchmarkDecode(b, "testdata/benchGray.png", 1)
 
334
}
 
335
 
 
336
func BenchmarkDecodeNRGBAGradient(b *testing.B) {
 
337
        benchmarkDecode(b, "testdata/benchNRGBA-gradient.png", 4)
 
338
}
 
339
 
 
340
func BenchmarkDecodeNRGBAOpaque(b *testing.B) {
 
341
        benchmarkDecode(b, "testdata/benchNRGBA-opaque.png", 4)
 
342
}
 
343
 
 
344
func BenchmarkDecodePaletted(b *testing.B) {
 
345
        benchmarkDecode(b, "testdata/benchPaletted.png", 1)
 
346
}
 
347
 
 
348
func BenchmarkDecodeRGB(b *testing.B) {
 
349
        benchmarkDecode(b, "testdata/benchRGB.png", 4)
 
350
}