OLD | NEW |
(Empty) | |
| 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 "testing" |
| 9 ) |
| 10 |
| 11 func abs(x int) int { |
| 12 if x < 0 { |
| 13 return -x |
| 14 } |
| 15 return x |
| 16 } |
| 17 |
| 18 // slowPaeth is a slow but simple implementation of the Paeth function. |
| 19 // It is a straight port of the sample code in the PNG spec, section 9.4. |
| 20 func slowPaeth(a, b, c uint8) uint8 { |
| 21 p := int(a) + int(b) - int(c) |
| 22 pa := abs(p - int(a)) |
| 23 pb := abs(p - int(b)) |
| 24 pc := abs(p - int(c)) |
| 25 if pa <= pb && pa <= pc { |
| 26 return a |
| 27 } else if pb <= pc { |
| 28 return b |
| 29 } |
| 30 return c |
| 31 } |
| 32 |
| 33 func TestPaeth(t *testing.T) { |
| 34 for a := 0; a < 256; a += 15 { |
| 35 for b := 0; b < 256; b += 15 { |
| 36 for c := 0; c < 256; c += 15 { |
| 37 got := paeth(uint8(a), uint8(b), uint8(c)) |
| 38 want := slowPaeth(uint8(a), uint8(b), uint8(c)) |
| 39 if got != want { |
| 40 t.Errorf("a, b, c = %d, %d, %d: got %d,
want %d", a, b, c, got, want) |
| 41 } |
| 42 } |
| 43 } |
| 44 } |
| 45 } |
| 46 |
| 47 func BenchmarkPaeth(b *testing.B) { |
| 48 for i := 0; i < b.N; i++ { |
| 49 paeth(uint8(i>>16), uint8(i>>8), uint8(i)) |
| 50 } |
| 51 } |
OLD | NEW |