Created
January 31, 2012 15:11
-
-
Save hidsh/bad2ad0b5d4a6d7880fe to your computer and use it in GitHub Desktop.
look up table /w linear interpolation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
look up table /w linear interpolation | |
$ gcc -std=c99 test.c && a.out | |
$ gnuplot | |
gnuplot> plot "k.txt" notitle | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
typedef unsigned char u1; | |
#define X(x) (u1)((x)/(100.0/255)+0.5) /* unit: % */ | |
#define Y(y) (u1)((y)/(100.0/255)+0.5) /* unit: % */ | |
const u1 table[] = { | |
11, | |
X(0), X(10), X(20), X(30), X(40), X(50), X(60), X(70), X(80), X(90), X(100), | |
Y(0), Y(37), Y(55), Y(67), Y(74), Y(81), Y(85), Y(90), Y(94), Y(98), Y(100) | |
}; | |
u1 lookup_table(const u1 x, const u1 tbl[]) | |
{ | |
u1 n; | |
u1 *ex, *ey; | |
n = tbl[0]; /* number of element */ | |
ex = (u1 *)&tbl[1]; | |
ey = (u1 *)&tbl[1+n]; | |
if(x <= ex[0]) return ey[0]; | |
if(x >= ex[n-1]) return ey[n-1]; | |
for(u1 i=n-2;; i--) { | |
if(x < ex[i]) continue; | |
if(x == ex[i]) return ey[i]; | |
return ((ey[i+1] - ey[i]) * (x - ex[i])) / (ex[i+1] - ex[i]) + ey[i]; | |
} | |
} | |
int main(void) | |
{ | |
FILE *fp; | |
fp=fopen("k.txt", "w"); | |
for(int x=0; x<256; x++) { | |
int y = lookup_table(x, table); | |
fprintf(fp, "%d %d\n", x, y); | |
} | |
fclose(fp); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment