From 1672dc364424addbac0907db7922079646bbe591 Mon Sep 17 00:00:00 2001 From: Sergey Makinen Date: Fri, 18 Feb 2022 03:39:53 +0300 Subject: [PATCH] Improve Gray/Gray16 color model coverage --- writer_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/writer_test.go b/writer_test.go index 1b10b90..0e7c2b2 100644 --- a/writer_test.go +++ b/writer_test.go @@ -35,15 +35,52 @@ func TestEncode(t *testing.T) { t.Fatalf("Decode() = _, %v; want nil", err) } compare(t, img, img2) + if file == "testdata/pal8gs.bmp" { + t.Run("Gray", func(t *testing.T) { + gray := image.NewGray(img.Bounds()) + for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ { + for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ { + gray.Set(x, y, img.At(x, y)) + } + } + buf.Reset() + if err = Encode(&buf, gray); err != nil { + t.Fatalf("Encode() = %v; want nil", err) + } + img2, err = Decode(bytes.NewReader(buf.Bytes())) + if err != nil { + t.Fatalf("Decode() = _, %v; want nil", err) + } + compare(t, img, img2) + }) + t.Run("Gray16", func(t *testing.T) { + gray := image.NewGray16(img.Bounds()) + for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ { + for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ { + gray.Set(x, y, img.At(x, y)) + } + } + buf.Reset() + if err = Encode(&buf, gray); err != nil { + t.Fatalf("Encode() = %v; want nil", err) + } + img2, err = Decode(bytes.NewReader(buf.Bytes())) + if err != nil { + t.Fatalf("Decode() = _, %v; want nil", err) + } + compare(t, img, img2) + }) + } switch img := img.(type) { case *image.RGBA: t.Run("RGBA;Transparent", func(t *testing.T) { img.Set(0, 0, color.Transparent) + img.Set(1, 0, color.RGBA{10, 10, 10, 10}) buf.Reset() - if err := Encode(&buf, img); err != nil { + if err = Encode(&buf, img); err != nil { t.Fatalf("Encode() = %v; want nil", err) } - img2, err := Decode(bytes.NewReader(buf.Bytes())) + img2, err = Decode(bytes.NewReader(buf.Bytes())) if err != nil { t.Fatalf("Decode() = _, %v; want nil", err) } @@ -52,11 +89,12 @@ func TestEncode(t *testing.T) { case *image.NRGBA: t.Run("NRGBA;Transparent", func(t *testing.T) { img.Set(0, 0, color.Transparent) + img.Set(1, 0, color.NRGBA{10, 10, 10, 10}) buf.Reset() - if err := Encode(&buf, img); err != nil { + if err = Encode(&buf, img); err != nil { t.Fatalf("Encode() = %v; want nil", err) } - img2, err := Decode(bytes.NewReader(buf.Bytes())) + img2, err = Decode(bytes.NewReader(buf.Bytes())) if err != nil { t.Fatalf("Decode() = _, %v; want nil", err) }