| 94 | } |
| 95 | |
| 96 | void |
| 97 | display_depth_image(const float* depth_buffer, int width, int height) |
| 98 | { |
| 99 | int npixels = width * height; |
| 100 | auto* depth_img = new std::uint8_t[npixels * 3]; |
| 101 | |
| 102 | float min_depth = depth_buffer[0]; |
| 103 | float max_depth = depth_buffer[0]; |
| 104 | for (int i = 1; i < npixels; ++i) { |
| 105 | if (depth_buffer[i] < min_depth) |
| 106 | min_depth = depth_buffer[i]; |
| 107 | if (depth_buffer[i] > max_depth) |
| 108 | max_depth = depth_buffer[i]; |
| 109 | } |
| 110 | |
| 111 | for (int i = 0; i < npixels; ++i) { |
| 112 | float zn = 0.7f; |
| 113 | float zf = 20.0f; |
| 114 | float d = depth_buffer[i]; |
| 115 | float z = -zf * zn / ((zf - zn) * (d - zf / (zf - zn))); |
| 116 | float b = 0.075f; |
| 117 | float f = 580.0f; |
| 118 | int kd = static_cast<int>(1090 - b * f / z * 8); |
| 119 | if (kd < 0) |
| 120 | kd = 0; |
| 121 | else if (kd > 2047) |
| 122 | kd = 2047; |
| 123 | |
| 124 | int pval = t_gamma[kd]; |
| 125 | auto lb = static_cast<std::uint8_t>(pval & 0xff); |
| 126 | switch (pval >> 8) { |
| 127 | case 0: |
| 128 | depth_img[3 * i + 0] = 255; |
| 129 | depth_img[3 * i + 1] = static_cast<std::uint8_t>(255 - lb); |
| 130 | depth_img[3 * i + 2] = static_cast<std::uint8_t>(255 - lb); |
| 131 | break; |
| 132 | case 1: |
| 133 | depth_img[3 * i + 0] = 255; |
| 134 | depth_img[3 * i + 1] = lb; |
| 135 | depth_img[3 * i + 2] = 0; |
| 136 | break; |
| 137 | case 2: |
| 138 | depth_img[3 * i + 0] = static_cast<std::uint8_t>(255 - lb); |
| 139 | depth_img[3 * i + 1] = 255; |
| 140 | depth_img[3 * i + 2] = 0; |
| 141 | break; |
| 142 | case 3: |
| 143 | depth_img[3 * i + 0] = 0; |
| 144 | depth_img[3 * i + 1] = 255; |
| 145 | depth_img[3 * i + 2] = lb; |
| 146 | break; |
| 147 | case 4: |
| 148 | depth_img[3 * i + 0] = 0; |
| 149 | depth_img[3 * i + 1] = static_cast<std::uint8_t>(255 - lb); |
| 150 | depth_img[3 * i + 2] = 255; |
| 151 | break; |
| 152 | case 5: |
| 153 | depth_img[3 * i + 0] = 0; |