Skip to content

Instantly share code, notes, and snippets.

@clausecker
Created August 24, 2012 15:04
Show Gist options
  • Save clausecker/3451755 to your computer and use it in GitHub Desktop.
Save clausecker/3451755 to your computer and use it in GitHub Desktop.
Improved Paeth-transformation
#include <stdlib.h>
int naive_paeth(int a, int b, int c) {
int
p = a + b - c,
pa = abs(p - a),
pb = abs(p - b),
pc = abs(p - c);
if (pa <= pb && pa <= pc) return a;
if (pb <= pc) return b;
return c;
}
int libpng_paeth(int a, int b, int c) {
int pa, pb, pc, p;
p = b - c;
pc = a - c;
pa = abs(p);
pb = abs(pc);
pc = abs(p + pc);
/* Find the best predictor, the least of pa, pb, pc favoring the earlier
* ones in the case of a tie.
*/
if (pb < pa) pa = pb, a = b;
if (pc < pa) a = c;
return a;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment