10 extern std::string *movit_data_directory;
12 void hsv2rgb(float h, float s, float v, float *r, float *g, float *b)
18 float hp = (h * 180.0 / M_PI) / 60.0;
19 float x = c * (1 - fabs(fmod(hp, 2.0f) - 1.0f));
21 if (hp >= 0 && hp < 1) {
25 } else if (hp >= 1 && hp < 2) {
29 } else if (hp >= 2 && hp < 3) {
33 } else if (hp >= 3 && hp < 4) {
37 } else if (hp >= 4 && hp < 5) {
53 void hsv2rgb_normalized(float h, float s, float v, float *r, float *g, float *b)
55 float ref_r, ref_g, ref_b;
56 hsv2rgb(h, s, v, r, g, b);
57 hsv2rgb(h, 0.0f, v, &ref_r, &ref_g, &ref_b);
58 float lum = 0.2126 * *r + 0.7152 * *g + 0.0722 * *b;
59 float ref_lum = 0.2126 * ref_r + 0.7152 * ref_g + 0.0722 * ref_b;
61 float fac = ref_lum / lum;
68 std::string read_file(const std::string &filename)
70 const std::string full_pathname = *movit_data_directory + "/" + filename;
72 static char buf[131072];
73 FILE *fp = fopen(full_pathname.c_str(), "r");
75 perror(full_pathname.c_str());
79 int len = fread(buf, 1, sizeof(buf), fp);
82 return std::string(buf, len);
85 GLuint compile_shader(const std::string &shader_src, GLenum type)
87 GLuint obj = glCreateShader(type);
88 const GLchar* source[] = { shader_src.data() };
89 const GLint length[] = { (GLint)shader_src.size() };
90 glShaderSource(obj, 1, source, length);
93 GLchar info_log[4096];
94 GLsizei log_length = sizeof(info_log) - 1;
95 glGetShaderInfoLog(obj, log_length, &log_length, info_log);
96 info_log[log_length] = 0;
97 if (strlen(info_log) > 0) {
98 printf("shader compile log: %s\n", info_log);
102 glGetShaderiv(obj, GL_COMPILE_STATUS, &status);
103 if (status == GL_FALSE) {
110 void print_3x3_matrix(const Eigen::Matrix3d& m)
112 printf("%6.4f %6.4f %6.4f\n", m(0,0), m(0,1), m(0,2));
113 printf("%6.4f %6.4f %6.4f\n", m(1,0), m(1,1), m(1,2));
114 printf("%6.4f %6.4f %6.4f\n", m(2,0), m(2,1), m(2,2));
118 std::string output_glsl_mat3(const std::string &name, const Eigen::Matrix3d &m)
122 "const mat3 %s = mat3(\n"
123 " %.8f, %.8f, %.8f,\n"
124 " %.8f, %.8f, %.8f,\n"
125 " %.8f, %.8f, %.8f);\n\n",
127 m(0,0), m(1,0), m(2,0),
128 m(0,1), m(1,1), m(2,1),
129 m(0,2), m(1,2), m(2,2));
133 void combine_two_samples(float w1, float w2, float *offset, float *total_weight, float *sum_sq_error)
135 assert(movit_initialized);
136 assert(w1 * w2 >= 0.0f); // Should not have differing signs.
137 float z; // Just a shorter name for offset.
138 if (fabs(w1 + w2) < 1e-6) {
144 // Round to the minimum number of bits we have measured earlier.
145 // The card will do this for us anyway, but if we know what the real z
146 // is, we can pick a better total_weight below.
147 z = lrintf(z / movit_texel_subpixel_precision) * movit_texel_subpixel_precision;
149 // Choose total weight w so that we minimize total squared error
150 // for the effective weights:
152 // e = (w(1-z) - a)² + (wz - b)²
154 // Differentiating by w and setting equal to zero:
156 // 2(w(1-z) - a)(1-z) + 2(wz - b)z = 0
157 // w(1-z)² - a(1-z) + wz² - bz = 0
158 // w((1-z)² + z²) = a(1-z) + bz
159 // w = (a(1-z) + bz) / ((1-z)² + z²)
161 // If z had infinite precision, this would simply reduce to w = w1 + w2.
162 *total_weight = (w1 * (1 - z) + w2 * z) / (z * z + (1 - z) * (1 - z));
165 if (sum_sq_error != NULL) {
166 float err1 = *total_weight * (1 - z) - w1;
167 float err2 = *total_weight * z - w2;
168 *sum_sq_error = err1 * err1 + err2 * err2;
171 assert(*offset >= 0.0f);
172 assert(*offset <= 1.0f);