]> git.sesse.net Git - movit/blob - util.cpp
Add missing format LUMINANCE8.
[movit] / util.cpp
1 #include <GL/glew.h>
2 #include <assert.h>
3 #include <math.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <Eigen/Core>
7
8 #include "init.h"
9 #include "util.h"
10
11 extern std::string *movit_data_directory;
12
13 void hsv2rgb(float h, float s, float v, float *r, float *g, float *b)
14 {
15         if (h < 0.0f) {
16                 h += 2.0f * M_PI;
17         }
18         float c = v * s;
19         float hp = (h * 180.0 / M_PI) / 60.0;
20         float x = c * (1 - fabs(fmod(hp, 2.0f) - 1.0f));
21
22         if (hp >= 0 && hp < 1) {
23                 *r = c;
24                 *g = x;
25                 *b = 0.0f;
26         } else if (hp >= 1 && hp < 2) {
27                 *r = x;
28                 *g = c;
29                 *b = 0.0f;
30         } else if (hp >= 2 && hp < 3) {
31                 *r = 0.0f;
32                 *g = c;
33                 *b = x;
34         } else if (hp >= 3 && hp < 4) {
35                 *r = 0.0f;
36                 *g = x;
37                 *b = c;
38         } else if (hp >= 4 && hp < 5) {
39                 *r = x;
40                 *g = 0.0f;
41                 *b = c;
42         } else {
43                 *r = c;
44                 *g = 0.0f;
45                 *b = x;
46         }
47
48         float m = v - c;
49         *r += m;
50         *g += m;
51         *b += m;
52 }
53
54 void hsv2rgb_normalized(float h, float s, float v, float *r, float *g, float *b)
55 {
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;
61         if (lum > 1e-3) {
62                 float fac = ref_lum / lum;
63                 *r *= fac;
64                 *g *= fac;
65                 *b *= fac;
66         }
67 }
68
69 std::string read_file(const std::string &filename)
70 {
71         const std::string full_pathname = *movit_data_directory + "/" + filename;
72
73         static char buf[131072];
74         FILE *fp = fopen(full_pathname.c_str(), "r");
75         if (fp == NULL) {
76                 perror(full_pathname.c_str());
77                 exit(1);
78         }
79
80         int len = fread(buf, 1, sizeof(buf), fp);
81         fclose(fp);
82
83         return std::string(buf, len);
84 }
85
86 GLuint compile_shader(const std::string &shader_src, GLenum type)
87 {
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);
92         glCompileShader(obj);
93
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);
100         }
101
102         GLint status;
103         glGetShaderiv(obj, GL_COMPILE_STATUS, &status);
104         if (status == GL_FALSE) {
105                 exit(1);
106         }
107
108         return obj;
109 }
110
111 void print_3x3_matrix(const Eigen::Matrix3d& m)
112 {
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));
116         printf("\n");
117 }
118
119 std::string output_glsl_mat3(const std::string &name, const Eigen::Matrix3d &m)
120 {
121         char buf[1024];
122         sprintf(buf,
123                 "const mat3 %s = mat3(\n"
124                 "    %.8f, %.8f, %.8f,\n"
125                 "    %.8f, %.8f, %.8f,\n"
126                 "    %.8f, %.8f, %.8f);\n\n",
127                 name.c_str(),
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));
131         return buf;
132 }
133
134 void combine_two_samples(float w1, float w2, float *offset, float *total_weight, float *sum_sq_error)
135 {
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) {
140                 z = 0.5f;
141         } else {
142                 z = w2 / (w1 + w2);
143         }
144
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;
149         
150         // Choose total weight w so that we minimize total squared error
151         // for the effective weights:
152         //
153         //   e = (w(1-z) - a)² + (wz - b)²
154         //
155         // Differentiating by w and setting equal to zero:
156         //
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²)
161         //
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));
164         *offset = z;
165
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;
170         }
171
172         assert(*offset >= 0.0f);
173         assert(*offset <= 1.0f);
174 }