git.lucas.co / go_mono
git clone https://git.lucas.co/go_mono.git

bmp/reader_test.go (2K)

 1 // Copyright 2012 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 bmp
 6 
 7 import (
 8 	"bytes"
 9 	"fmt"
10 	"image"
11 	"io"
12 	"os"
13 	"testing"
14 
15 	_ "image/png"
16 )
17 
18 const testdataDir = "../testdata/"
19 
20 func compare(img0, img1 image.Image) error {
21 	b := img1.Bounds()
22 	if !b.Eq(img0.Bounds()) {
23 		return fmt.Errorf("wrong image size: want %s, got %s", img0.Bounds(), b)
24 	}
25 	for y := b.Min.Y; y < b.Max.Y; y++ {
26 		for x := b.Min.X; x < b.Max.X; x++ {
27 			c0 := img0.At(x, y)
28 			c1 := img1.At(x, y)
29 			r0, g0, b0, a0 := c0.RGBA()
30 			r1, g1, b1, a1 := c1.RGBA()
31 			if r0 != r1 || g0 != g1 || b0 != b1 || a0 != a1 {
32 				return fmt.Errorf("pixel at (%d, %d) has wrong color: want %v, got %v", x, y, c0, c1)
33 			}
34 		}
35 	}
36 	return nil
37 }
38 
39 // TestDecode tests that decoding a PNG image and a BMP image result in the
40 // same pixel data.
41 func TestDecode(t *testing.T) {
42 	testCases := []string{
43 		"colormap",
44 		"colormap-0",
45 		"colormap-251",
46 		"video-001",
47 		"yellow_rose-small",
48 		"yellow_rose-small-v5",
49 		"bmp_1bpp",
50 		"bmp_4bpp",
51 		"bmp_8bpp",
52 	}
53 
54 	for _, tc := range testCases {
55 		f0, err := os.Open(testdataDir + tc + ".png")
56 		if err != nil {
57 			t.Errorf("%s: Open PNG: %v", tc, err)
58 			continue
59 		}
60 		defer f0.Close()
61 		img0, _, err := image.Decode(f0)
62 		if err != nil {
63 			t.Errorf("%s: Decode PNG: %v", tc, err)
64 			continue
65 		}
66 
67 		f1, err := os.Open(testdataDir + tc + ".bmp")
68 		if err != nil {
69 			t.Errorf("%s: Open BMP: %v", tc, err)
70 			continue
71 		}
72 		defer f1.Close()
73 		img1, _, err := image.Decode(f1)
74 		if err != nil {
75 			t.Errorf("%s: Decode BMP: %v", tc, err)
76 			continue
77 		}
78 
79 		if err := compare(img0, img1); err != nil {
80 			t.Errorf("%s: %v", tc, err)
81 			continue
82 		}
83 	}
84 }
85 
86 // TestEOF tests that decoding a BMP image returns io.ErrUnexpectedEOF
87 // when there are no headers or data is empty
88 func TestEOF(t *testing.T) {
89 	_, err := Decode(bytes.NewReader(nil))
90 	if err != io.ErrUnexpectedEOF {
91 		t.Errorf("Error should be io.ErrUnexpectedEOF on nil but got %v", err)
92 	}
93 }