10 void hsv2rgb(float h, float s, float v, float *r, float *g, float *b)
16 float hp = (h * 180.0 / M_PI) / 60.0;
17 float x = c * (1 - fabs(fmod(hp, 2.0f) - 1.0f));
19 if (hp >= 0 && hp < 1) {
23 } else if (hp >= 1 && hp < 2) {
27 } else if (hp >= 2 && hp < 3) {
31 } else if (hp >= 3 && hp < 4) {
35 } else if (hp >= 4 && hp < 5) {
51 void hsv2rgb_normalized(float h, float s, float v, float *r, float *g, float *b)
53 float ref_r, ref_g, ref_b;
54 hsv2rgb(h, s, v, r, g, b);
55 hsv2rgb(h, 0.0f, v, &ref_r, &ref_g, &ref_b);
56 float lum = 0.2126 * *r + 0.7152 * *g + 0.0722 * *b;
57 float ref_lum = 0.2126 * ref_r + 0.7152 * ref_g + 0.0722 * ref_b;
59 float fac = ref_lum / lum;
66 std::string read_file(const std::string &filename)
68 static char buf[131072];
69 FILE *fp = fopen(filename.c_str(), "r");
71 perror(filename.c_str());
75 int len = fread(buf, 1, sizeof(buf), fp);
78 return std::string(buf, len);
81 GLuint compile_shader(const std::string &shader_src, GLenum type)
83 GLuint obj = glCreateShader(type);
84 const GLchar* source[] = { shader_src.data() };
85 const GLint length[] = { (GLint)shader_src.size() };
86 glShaderSource(obj, 1, source, length);
89 GLchar info_log[4096];
90 GLsizei log_length = sizeof(info_log) - 1;
91 glGetShaderInfoLog(obj, log_length, &log_length, info_log);
92 info_log[log_length] = 0;
93 if (strlen(info_log) > 0) {
94 printf("shader compile log: %s\n", info_log);
98 glGetShaderiv(obj, GL_COMPILE_STATUS, &status);
99 if (status == GL_FALSE) {
106 void print_3x3_matrix(const Eigen::Matrix3d& m)
108 printf("%6.4f %6.4f %6.4f\n", m(0,0), m(0,1), m(0,2));
109 printf("%6.4f %6.4f %6.4f\n", m(1,0), m(1,1), m(1,2));
110 printf("%6.4f %6.4f %6.4f\n", m(2,0), m(2,1), m(2,2));
114 std::string output_glsl_mat3(const std::string &name, const Eigen::Matrix3d &m)
118 "const mat3 %s = mat3(\n"
119 " %.8f, %.8f, %.8f,\n"
120 " %.8f, %.8f, %.8f,\n"
121 " %.8f, %.8f, %.8f);\n\n",
123 m(0,0), m(1,0), m(2,0),
124 m(0,1), m(1,1), m(2,1),
125 m(0,2), m(1,2), m(2,2));
129 void combine_two_samples(float w1, float w2, float *offset, float *total_weight, float *sum_sq_error)
131 assert(movit_initialized);
132 assert(w1 * w2 >= 0.0f); // Should not have differing signs.
133 float z; // Just a shorter name for offset.
134 if (fabs(w1 + w2) < 1e-6) {
140 // Round to the minimum number of bits we have measured earlier.
141 // The card will do this for us anyway, but if we know what the real z
142 // is, we can pick a better total_weight below.
143 z = lrintf(z / movit_texel_subpixel_precision) * movit_texel_subpixel_precision;
145 // Choose total weight w so that we minimize total squared error
146 // for the effective weights:
148 // e = (w(1-z) - a)² + (wz - b)²
150 // Differentiating by w and setting equal to zero:
152 // 2(w(1-z) - a)(1-z) + 2(wz - b)z = 0
153 // w(1-z)² - a(1-z) + wz² - bz = 0
154 // w((1-z)² + z²) = a(1-z) + bz
155 // w = (a(1-z) + bz) / ((1-z)² + z²)
157 // If z had infinite precision, this would simply reduce to w = w1 + w2.
158 *total_weight = (w1 * (1 - z) + w2 * z) / (z * z + (1 - z) * (1 - z));
161 if (sum_sq_error != NULL) {
162 float err1 = *total_weight * (1 - z) - w1;
163 float err2 = *total_weight * z - w2;
164 *sum_sq_error = err1 * err1 + err2 * err2;
167 assert(*offset >= 0.0f);
168 assert(*offset <= 1.0f);