...

Source file src/golang.org/x/image/bmp/reader_test.go

Documentation: golang.org/x/image/bmp

     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  	}
    50  
    51  	for _, tc := range testCases {
    52  		f0, err := os.Open(testdataDir + tc + ".png")
    53  		if err != nil {
    54  			t.Errorf("%s: Open PNG: %v", tc, err)
    55  			continue
    56  		}
    57  		defer f0.Close()
    58  		img0, _, err := image.Decode(f0)
    59  		if err != nil {
    60  			t.Errorf("%s: Decode PNG: %v", tc, err)
    61  			continue
    62  		}
    63  
    64  		f1, err := os.Open(testdataDir + tc + ".bmp")
    65  		if err != nil {
    66  			t.Errorf("%s: Open BMP: %v", tc, err)
    67  			continue
    68  		}
    69  		defer f1.Close()
    70  		img1, _, err := image.Decode(f1)
    71  		if err != nil {
    72  			t.Errorf("%s: Decode BMP: %v", tc, err)
    73  			continue
    74  		}
    75  
    76  		if err := compare(img0, img1); err != nil {
    77  			t.Errorf("%s: %v", tc, err)
    78  			continue
    79  		}
    80  	}
    81  }
    82  
    83  // TestEOF tests that decoding a BMP image returns io.ErrUnexpectedEOF
    84  // when there are no headers or data is empty
    85  func TestEOF(t *testing.T) {
    86  	_, err := Decode(bytes.NewReader(nil))
    87  	if err != io.ErrUnexpectedEOF {
    88  		t.Errorf("Error should be io.ErrUnexpectedEOF on nil but got %v", err)
    89  	}
    90  }
    91  

View as plain text