14 uint32_t width, height;
15 std::unique_ptr<Vec2[]> flow;
18 Flow read_flow(const char *filename);
20 // du and dv are in pixels.
21 inline void flow2rgb(float du, float dv, uint8_t *rr, uint8_t *gg, uint8_t *bb)
23 float angle = atan2(dv, du);
24 float magnitude = std::min(hypot(du, dv) / 20.0, 1.0);
26 // HSV to RGB (from Wikipedia). Saturation is 1.
28 float h = (angle + M_PI) * 6.0 / (2.0 * M_PI);
29 float X = c * (1.0 - fabs(fmod(h, 2.0) - 1.0));
30 float r = 0.0f, g = 0.0f, b = 0.0f;
34 } else if (h <= 2.0f) {
37 } else if (h <= 3.0f) {
40 } else if (h <= 4.0f) {
43 } else if (h <= 5.0f) {
46 } else if (h <= 6.0f) {
50 // h is NaN, so black is fine.
52 float m = magnitude - c;
56 r = std::max(std::min(r, 1.0f), 0.0f);
57 g = std::max(std::min(g, 1.0f), 0.0f);
58 b = std::max(std::min(b, 1.0f), 0.0f);
59 *rr = lrintf(r * 255.0f);
60 *gg = lrintf(g * 255.0f);
61 *bb = lrintf(b * 255.0f);
64 #endif // !defined(_UTIL_H)