packages/initscripts/353/gamma.c

Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <math.h>
00003 #include <sys/types.h>
00004 #include <sys/stat.h>
00005 #include <fcntl.h>
00006 #include <unistd.h>
00007 
00008 #define GAMMA_FN        "gamma.dat"
00009 
00010 /*
00011  * write in resulting file table in format:
00012  *
00013  * count: unsigned short
00014  *
00015  * gamma value [1]
00016  * scale value [256]
00017  * ...
00018  * value in format 0.00 - 1.0
00019  * as 0x0000 - 0xFFFF in fixed point format 0.16
00020  */
00021 
00022 inline unsigned short float_to_fp(unsigned char *fp, float val) {
00023         unsigned long v;
00024         unsigned short r;
00025 
00026         if(val <= 0.0) {
00027                 fp[0] = 0x00;
00028                 fp[1] = 0x00;
00029                 return 0x0000;
00030         }
00031         if(val >= 1.0) {
00032                 fp[0] = 0xFF;
00033                 fp[1] = 0xFF;
00034                 return 0xFFFF;
00035         }
00036         float vv = val;
00037         val *= 0x10000;
00038         r = (unsigned short)val;
00039         fp[0] = (unsigned char)(r >> 8);
00040         fp[1] = (unsigned char)(r & 0xFF);
00041         return r;
00042 }
00043 
00044 int main() {
00045         int i, j;
00046         float g;
00047         unsigned char v[2];
00048 //      unsigned short c = htons(100);
00049         float gamma;
00050         int fout;
00051         unsigned short t;
00052         
00053         fout = open(GAMMA_FN, O_RDWR | O_CREAT, S_IROTH | S_IRGRP | S_IRUSR | S_IWUSR);
00054         if(fout < 0) {
00055                 printf("failed to open \"%s\" file\n", GAMMA_FN);
00056                 return -1;
00057         }
00058         for(j = 1; j <= 200; j++) {
00059                 gamma = j;
00060                 if(gamma > 100)
00061                         gamma = 100;
00062                 gamma /= 100.0;
00063                 t = j;
00064                 write(fout, &t, 2);
00065                 for(i = 0; i < 257; i++) {
00066                         g = i;
00067                         g /= 256.0;
00068                         g = powf(g, gamma);
00069                         t = float_to_fp(v, g);
00070                         write(fout, v, 2);
00071                 }
00072         }
00073         close(fout);
00074         return 0;
00075 }

Generated on Fri Nov 28 00:06:24 2008 for elphel by  doxygen 1.5.1