From: paul Date: Sat, 29 Oct 2011 17:00:51 +0000 (+0000) Subject: use function pointers X-Git-Url: https://git.plutz.net/?a=commitdiff_plain;h=4d44ba8fdad35505412082d2a066e77b953c8bd8;p=stereofy use function pointers svn path=/trunk/; revision=3 --- diff --git a/anaglyph.c b/anaglyph.c index 3a325be..4aa84ff 100644 --- a/anaglyph.c +++ b/anaglyph.c @@ -4,8 +4,10 @@ int error(int code); int mm(int v, int min, int max); -int anaglyph(FILE *in1, FILE *in2, FILE *out, char *alg); -unsigned char* colorize(unsigned char *out, const unsigned char *left, const unsigned char *right, const char *alg); +int anaglyph(FILE *in1, FILE *in2, FILE *out, + unsigned char* (*alg)(unsigned char*, const unsigned char*, const unsigned char*) ); +unsigned char* (*parse_func(const char *arg))(unsigned char*, const unsigned char*, const unsigned char*); + unsigned char* full_gm(unsigned char *out, const unsigned char *l, const unsigned char *r); unsigned char* half_gm(unsigned char *out, const unsigned char *l, const unsigned char *r); unsigned char* dubois_gm(unsigned char *out, const unsigned char *l, const unsigned char *r); @@ -17,12 +19,12 @@ unsigned char* dubois_mc(unsigned char *out, const unsigned char *l, const unsig unsigned char* dubois_ab(unsigned char *out, const unsigned char *l, const unsigned char *r); int main(int argc, char *argv[]){ - FILE *in1, *in2, *out; int cnt; char *alg = malloc(5); - in1 = in2 = out = NULL; alg = strcpy(alg, "-dgm"); + FILE *in1, *in2, *out; int cnt; in1 = in2 = out = NULL; + unsigned char* (*alg)(unsigned char[], const unsigned char[], const unsigned char[]) = &dubois_rc; if (argc < 4 || argc > 5) return error(1); for (cnt = 1; cnt < argc; cnt++) { - if (argv[cnt][0] == '-') alg = strcpy(alg, argv[cnt]); + if (argv[cnt][0] == '-') alg = parse_func(argv[cnt]); else if (!in1){ if ((in1 = fopen(argv[cnt], "r")) == NULL) return error(2);} else if (!in2){ if ((in2 = fopen(argv[cnt], "r")) == NULL) return error(2);} else if (!out){ if ((out = fopen(argv[cnt], "w")) == NULL) return error(3);} @@ -142,26 +144,27 @@ unsigned char* dubois_ab(unsigned char *out, const unsigned char *l, const unsig return out; } -unsigned char* colorize(unsigned char *out, const unsigned char *left, const unsigned char *right, const char *alg){ - if (!strcmp(alg, "-fgm")) return full_gm(out, left, right); - if (!strcmp(alg, "-hgm")) return half_gm(out, left, right); - if (!strcmp(alg, "-dgm")) return dubois_gm(out, left, right); +unsigned char* (*parse_func(const char *arg))(unsigned char*, const unsigned char*, const unsigned char*){ + if (!strcmp(arg, "-fgm")) return &full_gm; + if (!strcmp(arg, "-hgm")) return &half_gm; + if (!strcmp(arg, "-dgm")) return &dubois_gm; - if (!strcmp(alg, "-frc")) return full_rc(out, left, right); - if (!strcmp(alg, "-hrc")) return half_rc(out, left, right); - if (!strcmp(alg, "-drc")) return dubois_rc(out, left, right); + if (!strcmp(arg, "-frc")) return &full_rc; + if (!strcmp(arg, "-hrc")) return &half_rc; + if (!strcmp(arg, "-drc")) return &dubois_rc; - if (!strcmp(alg, "-fmc")) return full_mc(out, left, right); - if (!strcmp(alg, "-dmc")) return dubois_mc(out, left, right); - if (!strcmp(alg, "-dab")) return dubois_ab(out, left, right); - exit(error(1)); return out; + if (!strcmp(arg, "-fmc")) return &full_mc; + if (!strcmp(arg, "-dmc")) return &dubois_mc; + if (!strcmp(arg, "-dab")) return &dubois_ab; + exit(error(1)); return &dubois_rc; } -int anaglyph(FILE *in1, FILE *in2, FILE *out, char *alg){ +int anaglyph(FILE *in1, FILE *in2, FILE *out, + unsigned char* (*alg)(unsigned char*, const unsigned char*, const unsigned char*) ){ unsigned char *i1 = malloc(3), *i2 = malloc(3), *o = malloc(3); while (fread( i1, 1, 3, in1) && fread( i2, 1, 3, in2)){ - if (!fwrite(colorize( o, i1, i2, alg), 1, 3, out)) return error(5); + if (!fwrite(alg( o, i1, i2), 1, 3, out)) return error(5); } return 0; }