github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/image/bmp/reader_test.go (about)

     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  	"fmt"
     9  	"image"
    10  	"os"
    11  	"testing"
    12  
    13  	_ "image/png"
    14  )
    15  
    16  const testdataDir = "../testdata/"
    17  
    18  func compare(t *testing.T, img0, img1 image.Image) error {
    19  	b := img1.Bounds()
    20  	if !b.Eq(img0.Bounds()) {
    21  		return fmt.Errorf("wrong image size: want %s, got %s", img0.Bounds(), b)
    22  	}
    23  	for y := b.Min.Y; y < b.Max.Y; y++ {
    24  		for x := b.Min.X; x < b.Max.X; x++ {
    25  			c0 := img0.At(x, y)
    26  			c1 := img1.At(x, y)
    27  			r0, g0, b0, a0 := c0.RGBA()
    28  			r1, g1, b1, a1 := c1.RGBA()
    29  			if r0 != r1 || g0 != g1 || b0 != b1 || a0 != a1 {
    30  				return fmt.Errorf("pixel at (%d, %d) has wrong color: want %v, got %v", x, y, c0, c1)
    31  			}
    32  		}
    33  	}
    34  	return nil
    35  }
    36  
    37  // TestDecode tests that decoding a PNG image and a BMP image result in the
    38  // same pixel data.
    39  func TestDecode(t *testing.T) {
    40  	testCases := []string{
    41  		"video-001",
    42  		"yellow_rose-small",
    43  	}
    44  
    45  	for _, tc := range testCases {
    46  		f0, err := os.Open(testdataDir + tc + ".png")
    47  		if err != nil {
    48  			t.Errorf("%s: Open PNG: %v", tc, err)
    49  			continue
    50  		}
    51  		defer f0.Close()
    52  		img0, _, err := image.Decode(f0)
    53  		if err != nil {
    54  			t.Errorf("%s: Decode PNG: %v", tc, err)
    55  			continue
    56  		}
    57  
    58  		f1, err := os.Open(testdataDir + tc + ".bmp")
    59  		if err != nil {
    60  			t.Errorf("%s: Open BMP: %v", tc, err)
    61  			continue
    62  		}
    63  		defer f1.Close()
    64  		img1, _, err := image.Decode(f1)
    65  		if err != nil {
    66  			t.Errorf("%s: Decode BMP: %v", tc, err)
    67  			continue
    68  		}
    69  
    70  		if err := compare(t, img0, img1); err != nil {
    71  			t.Errorf("%s: %v", tc, err)
    72  			continue
    73  		}
    74  	}
    75  }