Black Lives Matter. Support the Equal Justice Initiative.

Source file src/image/png/paeth_test.go

Documentation: image/png

     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 png
     6  
     7  import (
     8  	"bytes"
     9  	"math/rand"
    10  	"testing"
    11  )
    12  
    13  func slowAbs(x int) int {
    14  	if x < 0 {
    15  		return -x
    16  	}
    17  	return x
    18  }
    19  
    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)
    24  	pa := slowAbs(p - int(a))
    25  	pb := slowAbs(p - int(b))
    26  	pc := slowAbs(p - int(c))
    27  	if pa <= pb && pa <= pc {
    28  		return a
    29  	} else if pb <= pc {
    30  		return b
    31  	}
    32  	return c
    33  }
    34  
    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)
    39  	}
    40  	for i := bytesPerPixel; i < len(cdat); i++ {
    41  		cdat[i] += paeth(cdat[i-bytesPerPixel], pdat[i], pdat[i-bytesPerPixel])
    42  	}
    43  }
    44  
    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))
    51  				if got != want {
    52  					t.Errorf("a, b, c = %d, %d, %d: got %d, want %d", a, b, c, got, want)
    53  				}
    54  			}
    55  		}
    56  	}
    57  }
    58  
    59  func BenchmarkPaeth(b *testing.B) {
    60  	for i := 0; i < b.N; i++ {
    61  		paeth(uint8(i>>16), uint8(i>>8), uint8(i))
    62  	}
    63  }
    64  
    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())
    78  			}
    79  			copy(pdat1, pdat0)
    80  			copy(pdat2, pdat0)
    81  			copy(cdat1, cdat0)
    82  			copy(cdat2, cdat0)
    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)
    87  				break
    88  			}
    89  		}
    90  	}
    91  }
    92  

View as plain text