blob: dee9ee99143d30dd48d22c2c67198c7bc93c0836 [file] [log] [blame]
mtklein65e58242016-01-13 12:57:57 -08001/*
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"
mtkleina1159422016-01-15 05:46:54 -08009#include <stdlib.h>
mtklein65e58242016-01-13 12:57:57 -080010
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
14static 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
26static 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
36DEF_FUZZ(Paeth, fuzz) {
mtklein24a22c72016-01-14 04:59:42 -080037 auto a = fuzz->nextB(),
38 b = fuzz->nextB(),
39 c = fuzz->nextB();
mtkleina1159422016-01-15 05:46:54 -080040 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 }
mtklein65e58242016-01-13 12:57:57 -080049}