mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "Fuzz.h" |
mtklein | a115942 | 2016-01-15 05:46:54 -0800 | [diff] [blame] | 9 | #include <stdlib.h> |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 10 | |
| 11 | // This really is just an example Fuzz*.cpp file. |
| 12 | // It tests that two different ways of calculating the Paeth predictor function are equivalent. |
| 13 | |
| 14 | static uint8_t paeth_std(uint8_t a, uint8_t b, uint8_t c) { |
| 15 | int p = a+b-c; |
| 16 | |
| 17 | int pa = abs(p-a), |
| 18 | pb = abs(p-b), |
| 19 | pc = abs(p-c); |
| 20 | |
| 21 | if (pb < pa) { pa = pb; a = b; } |
| 22 | if (pc < pa) { a = c; } |
| 23 | return a; |
| 24 | } |
| 25 | |
| 26 | static uint8_t paeth_alt(uint8_t a, uint8_t b, uint8_t c) { |
| 27 | int min = SkTMin(a,b), |
| 28 | max = SkTMax(a,b); |
| 29 | int delta = (max-min)/3; |
| 30 | |
| 31 | if (c <= min+delta) return max; |
| 32 | if (c >= max-delta) return min; |
| 33 | return c; |
| 34 | } |
| 35 | |
| 36 | DEF_FUZZ(Paeth, fuzz) { |
mtklein | 24a22c7 | 2016-01-14 04:59:42 -0800 | [diff] [blame] | 37 | auto a = fuzz->nextB(), |
| 38 | b = fuzz->nextB(), |
| 39 | c = fuzz->nextB(); |
mtklein | a115942 | 2016-01-15 05:46:54 -0800 | [diff] [blame] | 40 | SkDebugf("Paeth(%d,%d,%d)\n", a,b,c); |
| 41 | |
| 42 | if (a == b && b == c) { |
| 43 | fuzz->signalBoring(); // Not really boring, just demoing signalBoring(). |
| 44 | } |
| 45 | |
| 46 | if (paeth_alt(a,b,c) != paeth_std(a,b,c)) { |
| 47 | fuzz->signalBug(); |
| 48 | } |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 49 | } |