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.
20 // slowPaeth is a slow but simple implementation of the Paeth function.
21 // It is a straight port of the sample code in the PNG spec, section 9.4.
22 func slowPaeth(a, b, c uint8) uint8 {
23 p := int(a) + int(b) - int(c)
27 if pa <= pb && pa <= pc {
35 // slowFilterPaeth is a slow but simple implementation of func filterPaeth.
36 func slowFilterPaeth(cdat, pdat []byte, bytesPerPixel int) {
37 for i := 0; i < bytesPerPixel; i++ {
38 cdat[i] += paeth(0, pdat[i], 0)
40 for i := bytesPerPixel; i < len(cdat); i++ {
41 cdat[i] += paeth(cdat[i-bytesPerPixel], pdat[i], pdat[i-bytesPerPixel])
45 func TestPaeth(t *testing.T) {
46 for a := 0; a < 256; a += 15 {
47 for b := 0; b < 256; b += 15 {
48 for c := 0; c < 256; c += 15 {
49 got := paeth(uint8(a), uint8(b), uint8(c))
50 want := slowPaeth(uint8(a), uint8(b), uint8(c))
52 t.Errorf("a, b, c = %d, %d, %d: got %d, want %d", a, b, c, got, want)
59 func BenchmarkPaeth(b *testing.B) {
60 for i := 0; i < b.N; i++ {
61 paeth(uint8(i>>16), uint8(i>>8), uint8(i))
65 func TestPaethDecode(t *testing.T) {
66 pdat0 := make([]byte, 32)
67 pdat1 := make([]byte, 32)
68 pdat2 := make([]byte, 32)
69 cdat0 := make([]byte, 32)
70 cdat1 := make([]byte, 32)
71 cdat2 := make([]byte, 32)
72 r := rand.New(rand.NewSource(1))
73 for bytesPerPixel := 1; bytesPerPixel <= 8; bytesPerPixel++ {
74 for i := 0; i < 100; i++ {
75 for j := range pdat0 {
76 pdat0[j] = uint8(r.Uint32())
77 cdat0[j] = uint8(r.Uint32())
83 filterPaeth(cdat1, pdat1, bytesPerPixel)
84 slowFilterPaeth(cdat2, pdat2, bytesPerPixel)
85 if !bytes.Equal(cdat1, cdat2) {
86 t.Errorf("bytesPerPixel: %d\npdat0: % x\ncdat0: % x\ngot: % x\nwant: % x", bytesPerPixel, pdat0, cdat0, cdat1, cdat2)