github.com/astrogo/fitsio@v0.3.0/fltimg/image_test.go (about)

     1  // Copyright 2016 The astrogo 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 fltimg
     6  
     7  import (
     8  	"bytes"
     9  	"encoding/binary"
    10  	"image"
    11  	"image/color"
    12  	"math/rand"
    13  	"testing"
    14  )
    15  
    16  func TestGray32Model(t *testing.T) {
    17  	for i, c := range []color.Color{
    18  		color.RGBAModel.Convert(f32Gray(0)),
    19  		color.RGBAModel.Convert(f32Gray(0.25)),
    20  		color.RGBAModel.Convert(f32Gray(0.5)),
    21  		color.RGBAModel.Convert(f32Gray(0.99)),
    22  		color.RGBAModel.Convert(f32Gray(1)),
    23  
    24  		color.RGBAModel.Convert(f64Gray(0)),
    25  		color.RGBAModel.Convert(f64Gray(0.25)),
    26  		color.RGBAModel.Convert(f64Gray(0.5)),
    27  		color.RGBAModel.Convert(f64Gray(0.99)),
    28  		color.RGBAModel.Convert(f64Gray(1)),
    29  
    30  		f32Gray(0),
    31  		f32Gray(0.5),
    32  		f32Gray(1),
    33  	} {
    34  		r, g, b, a := Gray32Model.Convert(c).RGBA()
    35  		got := [4]uint32{r, g, b, a}
    36  		r, g, b, a = c.RGBA()
    37  		want := [4]uint32{r, g, b, a}
    38  
    39  		if got != want {
    40  			t.Errorf("test[%d]: got=%v want=%v\n", i, got, want)
    41  		}
    42  	}
    43  }
    44  
    45  func TestGray64Model(t *testing.T) {
    46  	for i, c := range []color.Color{
    47  		color.RGBAModel.Convert(f32Gray(0)),
    48  		color.RGBAModel.Convert(f32Gray(0.25)),
    49  		color.RGBAModel.Convert(f32Gray(0.5)),
    50  		color.RGBAModel.Convert(f32Gray(0.99)),
    51  		color.RGBAModel.Convert(f32Gray(1)),
    52  
    53  		color.RGBAModel.Convert(f64Gray(0)),
    54  		color.RGBAModel.Convert(f64Gray(0.25)),
    55  		color.RGBAModel.Convert(f64Gray(0.5)),
    56  		color.RGBAModel.Convert(f64Gray(0.99)),
    57  		color.RGBAModel.Convert(f64Gray(1)),
    58  
    59  		f64Gray(0),
    60  		f64Gray(0.5),
    61  		f64Gray(1),
    62  	} {
    63  		r, g, b, a := Gray64Model.Convert(c).RGBA()
    64  		got := [4]uint32{r, g, b, a}
    65  		r, g, b, a = c.RGBA()
    66  		want := [4]uint32{r, g, b, a}
    67  
    68  		if got != want {
    69  			t.Errorf("test[%d]: got=%v want=%v\n", i, got, want)
    70  		}
    71  	}
    72  }
    73  
    74  func TestGray32(t *testing.T) {
    75  	rect := image.Rect(0, 0, 50, 50)
    76  	pix := make([]float32, rect.Dx()*rect.Dy())
    77  
    78  	for i := range pix {
    79  		v := rand.Float32()
    80  		pix[i] = v
    81  	}
    82  	buf := new(bytes.Buffer)
    83  	binary.Write(buf, binary.BigEndian, pix)
    84  	raw := buf.Bytes()
    85  
    86  	img := NewGray32(rect, raw)
    87  
    88  	nfail := 0
    89  	for i := 0; i < len(raw); i += 4 {
    90  		want := pix[i/4]
    91  		v1 := img.at(i)
    92  		if v1 != want {
    93  			t.Errorf("img[%d]: got=%v want=%v\n", i, v1, want)
    94  			nfail++
    95  		}
    96  
    97  		img.setf(i, want+0.5)
    98  		v2 := img.at(i)
    99  		if v2 != want+0.5 {
   100  			t.Errorf("img[%d]: got=%v want=%v\n", i, v2, want+0.5)
   101  			nfail++
   102  		}
   103  		img.setf(i, want)
   104  
   105  		v3 := img.at(i)
   106  		if v3 != want {
   107  			t.Errorf("img[%d]: got=%v want=%v\n", i, v3, want)
   108  			nfail++
   109  		}
   110  
   111  		if nfail > 20 {
   112  			t.Fatalf("too many failures")
   113  		}
   114  	}
   115  }
   116  
   117  func TestGray64(t *testing.T) {
   118  	rect := image.Rect(0, 0, 50, 50)
   119  	pix := make([]float64, rect.Dx()*rect.Dy())
   120  
   121  	for i := range pix {
   122  		v := rand.Float64()
   123  		pix[i] = v
   124  	}
   125  	buf := new(bytes.Buffer)
   126  	binary.Write(buf, binary.BigEndian, pix)
   127  	raw := buf.Bytes()
   128  
   129  	img := NewGray64(rect, raw)
   130  
   131  	nfail := 0
   132  	for i := 0; i < len(raw); i += 8 {
   133  		want := pix[i/8]
   134  		v1 := img.at(i)
   135  		if v1 != want {
   136  			t.Errorf("img[%d]: got=%v want=%v\n", i, v1, want)
   137  			nfail++
   138  		}
   139  
   140  		img.setf(i, want+0.5)
   141  		v2 := img.at(i)
   142  		if v2 != want+0.5 {
   143  			t.Errorf("img[%d]: got=%v want=%v\n", i, v2, want+0.5)
   144  			nfail++
   145  		}
   146  		img.setf(i, want)
   147  
   148  		v3 := img.at(i)
   149  		if v3 != want {
   150  			t.Errorf("img[%d]: got=%v want=%v\n", i, v3, want)
   151  			nfail++
   152  		}
   153  
   154  		if nfail > 20 {
   155  			t.Fatalf("too many failures")
   156  		}
   157  	}
   158  }