go-libwebp

Experimental translation from libwebp to Go source.
Log | Files | Refs | README | LICENSE

smoke_test.go (1100B)


      1 package webp
      2 
      3 import (
      4 	"bytes"
      5 	"image"
      6 	"image/color"
      7 	"testing"
      8 )
      9 
     10 func TestRoundTrip(t *testing.T) {
     11 	m := image.NewNRGBA(image.Rect(0, 0, 64, 48))
     12 	for y := 0; y < 48; y++ {
     13 		for x := 0; x < 64; x++ {
     14 			m.SetNRGBA(x, y, color.NRGBA{R: uint8(x * 4), G: uint8(y * 5), B: uint8(x ^ y), A: 255})
     15 		}
     16 	}
     17 	var buf bytes.Buffer
     18 	if err := EncodeImpl(&buf, m, 1.0); err != nil {
     19 		t.Fatalf("lossless encode: %v", err)
     20 	}
     21 	t.Logf("lossless encoded %d bytes", buf.Len())
     22 
     23 	got, err := DecodeImpl(buf.Bytes())
     24 	if err != nil {
     25 		t.Fatalf("decode: %v", err)
     26 	}
     27 	if got.Bounds() != m.Bounds() {
     28 		t.Fatalf("bounds: got %v want %v", got.Bounds(), m.Bounds())
     29 	}
     30 	for y := 0; y < 48; y++ {
     31 		for x := 0; x < 64; x++ {
     32 			if got.At(x, y) != m.At(x, y) {
     33 				t.Fatalf("pixel (%d,%d): got %v want %v", x, y, got.At(x, y), m.At(x, y))
     34 			}
     35 		}
     36 	}
     37 
     38 	var lossy bytes.Buffer
     39 	if err := EncodeImpl(&lossy, m, 0.75); err != nil {
     40 		t.Fatalf("lossy encode: %v", err)
     41 	}
     42 	t.Logf("lossy(0.75) encoded %d bytes", lossy.Len())
     43 	if _, err := DecodeImpl(lossy.Bytes()); err != nil {
     44 		t.Fatalf("decode lossy: %v", err)
     45 	}
     46 }