11 extern std::string *movit_data_directory;
13 void hsv2rgb(float h, float s, float v, float *r, float *g, float *b)
19 float hp = (h * 180.0 / M_PI) / 60.0;
20 float x = c * (1 - fabs(fmod(hp, 2.0f) - 1.0f));
22 if (hp >= 0 && hp < 1) {
26 } else if (hp >= 1 && hp < 2) {
30 } else if (hp >= 2 && hp < 3) {
34 } else if (hp >= 3 && hp < 4) {
38 } else if (hp >= 4 && hp < 5) {
54 void hsv2rgb_normalized(float h, float s, float v, float *r, float *g, float *b)
56 float ref_r, ref_g, ref_b;
57 hsv2rgb(h, s, v, r, g, b);
58 hsv2rgb(h, 0.0f, v, &ref_r, &ref_g, &ref_b);
59 float lum = 0.2126 * *r + 0.7152 * *g + 0.0722 * *b;
60 float ref_lum = 0.2126 * ref_r + 0.7152 * ref_g + 0.0722 * ref_b;
62 float fac = ref_lum / lum;
69 std::string read_file(const std::string &filename)
71 const std::string full_pathname = *movit_data_directory + "/" + filename;
73 static char buf[131072];
74 FILE *fp = fopen(full_pathname.c_str(), "r");
76 perror(full_pathname.c_str());
80 int len = fread(buf, 1, sizeof(buf), fp);
83 return std::string(buf, len);
86 GLuint compile_shader(const std::string &shader_src, GLenum type)
88 GLuint obj = glCreateShader(type);
89 const GLchar* source[] = { shader_src.data() };
90 const GLint length[] = { (GLint)shader_src.size() };
91 glShaderSource(obj, 1, source, length);
94 GLchar info_log[4096];
95 GLsizei log_length = sizeof(info_log) - 1;
96 glGetShaderInfoLog(obj, log_length, &log_length, info_log);
97 info_log[log_length] = 0;
98 if (strlen(info_log) > 0) {
99 printf("shader compile log: %s\n", info_log);
103 glGetShaderiv(obj, GL_COMPILE_STATUS, &status);
104 if (status == GL_FALSE) {
111 void print_3x3_matrix(const Eigen::Matrix3d& m)
113 printf("%6.4f %6.4f %6.4f\n", m(0,0), m(0,1), m(0,2));
114 printf("%6.4f %6.4f %6.4f\n", m(1,0), m(1,1), m(1,2));
115 printf("%6.4f %6.4f %6.4f\n", m(2,0), m(2,1), m(2,2));
119 std::string output_glsl_mat3(const std::string &name, const Eigen::Matrix3d &m)
123 "const mat3 %s = mat3(\n"
124 " %.8f, %.8f, %.8f,\n"
125 " %.8f, %.8f, %.8f,\n"
126 " %.8f, %.8f, %.8f);\n\n",
128 m(0,0), m(1,0), m(2,0),
129 m(0,1), m(1,1), m(2,1),
130 m(0,2), m(1,2), m(2,2));
134 void combine_two_samples(float w1, float w2, float *offset, float *total_weight, float *sum_sq_error)
136 assert(movit_initialized);
137 assert(w1 * w2 >= 0.0f); // Should not have differing signs.
138 float z; // Just a shorter name for offset.
139 if (fabs(w1 + w2) < 1e-6) {
145 // Round to the minimum number of bits we have measured earlier.
146 // The card will do this for us anyway, but if we know what the real z
147 // is, we can pick a better total_weight below.
148 z = lrintf(z / movit_texel_subpixel_precision) * movit_texel_subpixel_precision;
150 // Choose total weight w so that we minimize total squared error
151 // for the effective weights:
153 // e = (w(1-z) - a)² + (wz - b)²
155 // Differentiating by w and setting equal to zero:
157 // 2(w(1-z) - a)(1-z) + 2(wz - b)z = 0
158 // w(1-z)² - a(1-z) + wz² - bz = 0
159 // w((1-z)² + z²) = a(1-z) + bz
160 // w = (a(1-z) + bz) / ((1-z)² + z²)
162 // If z had infinite precision, this would simply reduce to w = w1 + w2.
163 *total_weight = (w1 * (1 - z) + w2 * z) / (z * z + (1 - z) * (1 - z));
166 if (sum_sq_error != NULL) {
167 float err1 = *total_weight * (1 - z) - w1;
168 float err2 = *total_weight * z - w2;
169 *sum_sq_error = err1 * err1 + err2 * err2;
172 assert(*offset >= 0.0f);
173 assert(*offset <= 1.0f);