13 extern string *movit_data_directory;
15 void hsv2rgb(float h, float s, float v, float *r, float *g, float *b)
21 float hp = (h * 180.0 / M_PI) / 60.0;
22 float x = c * (1 - fabs(fmod(hp, 2.0f) - 1.0f));
24 if (hp >= 0 && hp < 1) {
28 } else if (hp >= 1 && hp < 2) {
32 } else if (hp >= 2 && hp < 3) {
36 } else if (hp >= 3 && hp < 4) {
40 } else if (hp >= 4 && hp < 5) {
56 void hsv2rgb_normalized(float h, float s, float v, float *r, float *g, float *b)
58 float ref_r, ref_g, ref_b;
59 hsv2rgb(h, s, v, r, g, b);
60 hsv2rgb(h, 0.0f, v, &ref_r, &ref_g, &ref_b);
61 float lum = 0.2126 * *r + 0.7152 * *g + 0.0722 * *b;
62 float ref_lum = 0.2126 * ref_r + 0.7152 * ref_g + 0.0722 * ref_b;
64 float fac = ref_lum / lum;
71 string read_file(const string &filename)
73 const string full_pathname = *movit_data_directory + "/" + filename;
75 static char buf[131072];
76 FILE *fp = fopen(full_pathname.c_str(), "r");
78 perror(full_pathname.c_str());
82 int len = fread(buf, 1, sizeof(buf), fp);
85 return string(buf, len);
88 GLuint compile_shader(const string &shader_src, GLenum type)
90 GLuint obj = glCreateShader(type);
91 const GLchar* source[] = { shader_src.data() };
92 const GLint length[] = { (GLint)shader_src.size() };
93 glShaderSource(obj, 1, source, length);
96 GLchar info_log[4096];
97 GLsizei log_length = sizeof(info_log) - 1;
98 glGetShaderInfoLog(obj, log_length, &log_length, info_log);
99 info_log[log_length] = 0;
100 if (strlen(info_log) > 0) {
101 printf("shader compile log: %s\n", info_log);
105 glGetShaderiv(obj, GL_COMPILE_STATUS, &status);
106 if (status == GL_FALSE) {
113 void print_3x3_matrix(const Eigen::Matrix3d& m)
115 printf("%6.4f %6.4f %6.4f\n", m(0,0), m(0,1), m(0,2));
116 printf("%6.4f %6.4f %6.4f\n", m(1,0), m(1,1), m(1,2));
117 printf("%6.4f %6.4f %6.4f\n", m(2,0), m(2,1), m(2,2));
121 string output_glsl_mat3(const string &name, const Eigen::Matrix3d &m)
125 "const mat3 %s = mat3(\n"
126 " %.8f, %.8f, %.8f,\n"
127 " %.8f, %.8f, %.8f,\n"
128 " %.8f, %.8f, %.8f);\n\n",
130 m(0,0), m(1,0), m(2,0),
131 m(0,1), m(1,1), m(2,1),
132 m(0,2), m(1,2), m(2,2));
136 void combine_two_samples(float w1, float w2, float *offset, float *total_weight, float *sum_sq_error)
138 assert(movit_initialized);
139 assert(w1 * w2 >= 0.0f); // Should not have differing signs.
140 float z; // Just a shorter name for offset.
141 if (fabs(w1 + w2) < 1e-6) {
147 // Round to the minimum number of bits we have measured earlier.
148 // The card will do this for us anyway, but if we know what the real z
149 // is, we can pick a better total_weight below.
150 z = lrintf(z / movit_texel_subpixel_precision) * movit_texel_subpixel_precision;
152 // Choose total weight w so that we minimize total squared error
153 // for the effective weights:
155 // e = (w(1-z) - a)² + (wz - b)²
157 // Differentiating by w and setting equal to zero:
159 // 2(w(1-z) - a)(1-z) + 2(wz - b)z = 0
160 // w(1-z)² - a(1-z) + wz² - bz = 0
161 // w((1-z)² + z²) = a(1-z) + bz
162 // w = (a(1-z) + bz) / ((1-z)² + z²)
164 // If z had infinite precision, this would simply reduce to w = w1 + w2.
165 *total_weight = (w1 * (1 - z) + w2 * z) / (z * z + (1 - z) * (1 - z));
168 if (sum_sq_error != NULL) {
169 float err1 = *total_weight * (1 - z) - w1;
170 float err2 = *total_weight * z - w2;
171 *sum_sq_error = err1 * err1 + err2 * err2;
174 assert(*offset >= 0.0f);
175 assert(*offset <= 1.0f);