Skip to content
Myr

test_png_paeth.c

This file

Path

tests/test_png_paeth.c

47 lines · 1.0 KiB · C

Last changed by

Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d

History of this file

Take it

1#include <stdio.h>2#include <stdlib.h>34// Reference Paeth filter as per PNG spec5static int ref_paeth(int a, int b, int c)6{7   int p = a + b - c;8   int pa = abs(p-a);9   int pb = abs(p-b);10   int pc = abs(p-c);11   if (pa <= pb && pa <= pc) return a;12   if (pb <= pc) return b;13   return c;14}1516// Optimized Paeth filter17static int opt_paeth(int a, int b, int c)18{19   int thresh = c*3 - (a + b);20   int lo = a < b ? a : b;21   int hi = a < b ? b : a;22   int t0 = (hi <= thresh) ? lo : c;23   int t1 = (thresh <= lo) ? hi : t0;24   return t1;25}2627int main()28{29   // Exhaustively test the functions match for all byte inputs a, b,c in [0,255]30   for (int i = 0; i < (1 << 24); ++i) {31      int a = i & 0xff;32      int b = (i >> 8) & 0xff;33      int c = (i >> 16) & 0xff;3435      int ref = ref_paeth(a, b, c);36      int opt = opt_paeth(a, b, c);37      if (ref != opt) {38         fprintf(stderr, "mismatch at a=%3d b=%3d c=%3d: ref=%3d opt=%3d\n", a, b, c, ref, opt);39         return 1;40      }41   }4243   printf("all ok!\n");44   return 0;45}4647// vim:sw=3:sts=3:et