You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

389 lines
13 KiB

  1. /*
  2. * This file is part of Aptdec.
  3. * Copyright (c) 2004-2009 Thierry Leconte (F4DWV), Xerbo (xerbo@protonmail.com) 2019-2022
  4. *
  5. * Aptdec is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. *
  18. */
  19. #include "image.h"
  20. #include <math.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include "algebra.h"
  25. #include "apt.h"
  26. #include "util.h"
  27. static linear_t compute_regression(float *wedges) {
  28. // { 0.106, 0.215, 0.324, 0.433, 0.542, 0.652, 0.78, 0.87, 0.0 }
  29. const float teleramp[9] = {31.07, 63.02, 94.96, 126.9, 158.86, 191.1, 228.62, 255.0, 0.0};
  30. return linear_regression(wedges, teleramp, 9);
  31. }
  32. static float tele[16];
  33. static float Cs;
  34. void apt_histogramEqualise(float **prow, int nrow, int offset, int width) {
  35. // Plot histogram
  36. int histogram[256] = {0};
  37. for (int y = 0; y < nrow; y++)
  38. for (int x = 0; x < width; x++) histogram[(int)CLIP(prow[y][x + offset], 0, 255)]++;
  39. // Calculate cumulative frequency
  40. long sum = 0, cf[256] = {0};
  41. for (int i = 0; i < 255; i++) {
  42. sum += histogram[i];
  43. cf[i] = sum;
  44. }
  45. // Apply histogram
  46. int area = nrow * width;
  47. for (int y = 0; y < nrow; y++) {
  48. for (int x = 0; x < width; x++) {
  49. int k = (int)prow[y][x + offset];
  50. prow[y][x + offset] = (256.0f / area) * cf[k];
  51. }
  52. }
  53. }
  54. void apt_linearEnhance(float **prow, int nrow, int offset, int width) {
  55. // Plot histogram
  56. int histogram[256] = {0};
  57. for (int y = 0; y < nrow; y++)
  58. for (int x = 0; x < width; x++) histogram[(int)CLIP(prow[y][x + offset], 0, 255)]++;
  59. // Find min/max points
  60. int min = -1, max = -1;
  61. for (int i = 5; i < 250; i++) {
  62. if (histogram[i] / width / (nrow / 255.0) > 0.1) {
  63. if (min == -1) min = i;
  64. max = i;
  65. }
  66. }
  67. // Stretch the brightness into the new range
  68. for (int y = 0; y < nrow; y++) {
  69. for (int x = 0; x < width; x++) {
  70. prow[y][x + offset] = (prow[y][x + offset] - min) / (max - min) * 255.0f;
  71. prow[y][x + offset] = CLIP(prow[y][x + offset], 0.0f, 255.0f);
  72. }
  73. }
  74. }
  75. // Brightness calibrate, including telemetry
  76. void calibrateImage(float **prow, int nrow, int offset, int width, linear_t regr) {
  77. offset -= APT_SYNC_WIDTH + APT_SPC_WIDTH;
  78. for (int y = 0; y < nrow; y++) {
  79. for (int x = 0; x < width + APT_SYNC_WIDTH + APT_SPC_WIDTH + APT_TELE_WIDTH; x++) {
  80. float pv = linear_calc(prow[y][x + offset], regr);
  81. prow[y][x + offset] = CLIP(pv, 0, 255);
  82. }
  83. }
  84. }
  85. float teleNoise(float *wedges) {
  86. float pattern[9] = {31.07, 63.02, 94.96, 126.9, 158.86, 191.1, 228.62, 255.0, 0.0};
  87. float noise = 0;
  88. for (int i = 0; i < 9; i++) noise += fabs(wedges[i] - pattern[i]);
  89. return noise;
  90. }
  91. // Get telemetry data for thermal calibration
  92. apt_channel_t apt_calibrate(float **prow, int nrow, int offset, int width) {
  93. float teleline[APT_MAX_HEIGHT] = {0.0};
  94. float wedge[16];
  95. linear_t regr[APT_MAX_HEIGHT / APT_FRAME_LEN + 1];
  96. int telestart, mtelestart = 0;
  97. int channel = -1;
  98. // The minimum rows required to decode a full frame
  99. if (nrow < APT_CALIBRATION_ROWS) {
  100. error_noexit("Telemetry decoding error, not enough rows");
  101. return APT_CHANNEL_UNKNOWN;
  102. }
  103. // Calculate average of a row of telemetry
  104. for (int y = 0; y < nrow; y++) {
  105. for (int x = 3; x < 43; x++) teleline[y] += prow[y][x + offset + width];
  106. teleline[y] /= 40.0;
  107. }
  108. /* Wedge 7 is white and 8 is black, this will have the largest
  109. * difference in brightness, this can be used to find the current
  110. * position within the telemetry.
  111. */
  112. float max = 0.0f;
  113. for (int n = nrow / 3 - 64; n < 2 * nrow / 3 - 64; n++) {
  114. float df;
  115. // (sum 4px below) - (sum 4px above)
  116. df = (float)((teleline[n - 4] + teleline[n - 3] + teleline[n - 2] + teleline[n - 1]) -
  117. (teleline[n + 0] + teleline[n + 1] + teleline[n + 2] + teleline[n + 3]));
  118. // Find the maximum difference
  119. if (df > max) {
  120. mtelestart = n;
  121. max = df;
  122. }
  123. }
  124. telestart = (mtelestart + 64) % APT_FRAME_LEN;
  125. // Make sure that theres at least one full frame in the image
  126. if (nrow < telestart + APT_FRAME_LEN) {
  127. error_noexit("Telemetry decoding error, not enough rows");
  128. return APT_CHANNEL_UNKNOWN;
  129. }
  130. // Find the least noisy frame
  131. float minNoise = -1;
  132. int bestFrame = -1;
  133. for (int n = telestart, k = 0; n < nrow - APT_FRAME_LEN; n += APT_FRAME_LEN, k++) {
  134. int j;
  135. for (j = 0; j < 16; j++) {
  136. int i;
  137. wedge[j] = 0.0;
  138. for (i = 1; i < 7; i++) wedge[j] += teleline[n + j * 8 + i];
  139. wedge[j] /= 6;
  140. }
  141. float noise = teleNoise(wedge);
  142. if (noise < minNoise || minNoise == -1) {
  143. minNoise = noise;
  144. bestFrame = k;
  145. // Compute & apply regression on the wedges
  146. regr[k] = compute_regression(wedge);
  147. for (int j = 0; j < 16; j++) tele[j] = linear_calc(wedge[j], regr[k]);
  148. /* Compare the channel ID wedge to the reference
  149. * wedges, the wedge with the closest match will
  150. * be the channel ID
  151. */
  152. float min = -1;
  153. for (int j = 0; j < 6; j++) {
  154. float df = (float)(tele[15] - tele[j]);
  155. df *= df;
  156. if (df < min || min == -1) {
  157. channel = j;
  158. min = df;
  159. }
  160. }
  161. // Find the brightness of the minute marker, I don't really know what for
  162. Cs = 0.0;
  163. int i, j = n;
  164. for (i = 0, j = n; j < n + APT_FRAME_LEN; j++) {
  165. float csline = 0.0;
  166. for (int l = 3; l < 43; l++) csline += prow[n][l + offset - APT_SPC_WIDTH];
  167. csline /= 40.0;
  168. if (csline > 50.0) {
  169. Cs += csline;
  170. i++;
  171. }
  172. }
  173. Cs = linear_calc((Cs / i), regr[k]);
  174. }
  175. }
  176. if (bestFrame == -1) {
  177. error_noexit("Something has gone very wrong, please file a bug report");
  178. return APT_CHANNEL_UNKNOWN;
  179. }
  180. calibrateImage(prow, nrow, offset, width, regr[bestFrame]);
  181. return (apt_channel_t)(channel + 1);
  182. }
  183. extern float quick_select(float arr[], int n);
  184. // Biased median denoise, pretyt ugly
  185. #define TRIG_LEVEL 40
  186. void apt_denoise(float **prow, int nrow, int offset, int width) {
  187. for (int y = 2; y < nrow - 2; y++) {
  188. for (int x = offset + 1; x < offset + width - 1; x++) {
  189. if (prow[y][x + 1] - prow[y][x] > TRIG_LEVEL || prow[y][x - 1] - prow[y][x] > TRIG_LEVEL ||
  190. prow[y + 1][x] - prow[y][x] > TRIG_LEVEL || prow[y - 1][x] - prow[y][x] > TRIG_LEVEL) {
  191. prow[y][x] = quick_select((float[]){prow[y + 2][x - 1], prow[y + 2][x], prow[y + 2][x + 1], prow[y + 1][x - 1],
  192. prow[y + 1][x], prow[y + 1][x + 1], prow[y - 1][x - 1], prow[y - 1][x],
  193. prow[y - 1][x + 1], prow[y - 2][x - 1], prow[y - 2][x], prow[y - 2][x + 1]},
  194. 12);
  195. }
  196. }
  197. }
  198. }
  199. #undef TRIG_LEVEL
  200. // Flips a channel, for northbound passes
  201. void apt_flipImage(apt_image_t *img, int width, int offset) {
  202. for (int y = 1; y < img->nrow; y++) {
  203. for (int x = 1; x < ceil(width / 2.0); x++) {
  204. // Flip top-left & bottom-right
  205. float buffer = img->prow[img->nrow - y][offset + x];
  206. img->prow[img->nrow - y][offset + x] = img->prow[y][offset + (width - x)];
  207. img->prow[y][offset + (width - x)] = buffer;
  208. }
  209. }
  210. }
  211. // Calculate crop to reomve noise from the start and end of an image
  212. int apt_cropNoise(apt_image_t *img) {
  213. #define NOISE_THRESH 180.0
  214. // Average value of minute marker
  215. float spc_rows[APT_MAX_HEIGHT] = {0.0};
  216. int startCrop = 0;
  217. int endCrop = img->nrow;
  218. for (int y = 0; y < img->nrow; y++) {
  219. for (int x = 0; x < APT_SPC_WIDTH; x++) {
  220. spc_rows[y] += img->prow[y][x + (APT_CHB_OFFSET - APT_SPC_WIDTH)];
  221. }
  222. spc_rows[y] /= APT_SPC_WIDTH;
  223. // Skip minute markings
  224. if (spc_rows[y] < 10) {
  225. spc_rows[y] = spc_rows[y - 1];
  226. }
  227. }
  228. // 3 row average
  229. for (int y = 0; y < img->nrow; y++) {
  230. spc_rows[y] = (spc_rows[y + 1] + spc_rows[y + 2] + spc_rows[y + 3]) / 3;
  231. // img.prow[y][0] = spc_rows[y];
  232. }
  233. // Find ends
  234. for (int y = 0; y < img->nrow - 1; y++) {
  235. if (spc_rows[y] > NOISE_THRESH) {
  236. endCrop = y;
  237. }
  238. }
  239. for (int y = img->nrow; y > 0; y--) {
  240. if (spc_rows[y] > NOISE_THRESH) {
  241. startCrop = y;
  242. }
  243. }
  244. // printf("Crop rows: %i -> %i\n", startCrop, endCrop);
  245. // Remove the noisy rows at start
  246. for (int y = 0; y < img->nrow - startCrop; y++) {
  247. memmove(img->prow[y], img->prow[y + startCrop], sizeof(float) * APT_PROW_WIDTH);
  248. }
  249. // Ignore the noisy rows at the end
  250. img->nrow = (endCrop - startCrop);
  251. return startCrop;
  252. }
  253. // --- Visible and Temperature Calibration --- //
  254. #include "calibration.h"
  255. typedef struct {
  256. float Nbb;
  257. float Cs;
  258. float Cb;
  259. int ch;
  260. } tempparam_t;
  261. // IR channel temperature compensation
  262. tempparam_t tempcomp(float t[16], int ch, int satnum) {
  263. tempparam_t tpr;
  264. tpr.ch = ch - 4;
  265. const calibration_t calibration = get_calibration(satnum);
  266. const float Vc = calibration.rad[tpr.ch].vc;
  267. const float A = calibration.rad[tpr.ch].A;
  268. const float B = calibration.rad[tpr.ch].B;
  269. // Compute PRT temperature
  270. float T[4];
  271. for (size_t n = 0; n < 4; n++) {
  272. T[n] = quadratic_calc(t[n + 9] * 4.0, calibration.prt[n]);
  273. }
  274. float Tbb = (T[0] + T[1] + T[2] + T[3]) / 4.0; // Blackbody temperature
  275. float Tbbstar = A + Tbb * B; // Effective blackbody temperature
  276. tpr.Nbb = C1 * pow(Vc, 3) / (expf(C2 * Vc / Tbbstar) - 1.0f); // Blackbody radiance
  277. tpr.Cs = 246.4 * 4.0; // FIXME
  278. tpr.Cb = t[14] * 4.0;
  279. return tpr;
  280. }
  281. // IR channel temperature calibration
  282. static float tempcal(float Ce, int satnum, tempparam_t tpr) {
  283. const calibration_t calibration = get_calibration(satnum);
  284. const float Ns = calibration.cor[tpr.ch].Ns;
  285. const float Vc = calibration.rad[tpr.ch].vc;
  286. const float A = calibration.rad[tpr.ch].A;
  287. const float B = calibration.rad[tpr.ch].B;
  288. float Nl = Ns + (tpr.Nbb - Ns) * (tpr.Cs - Ce * 4.0) / (tpr.Cs - tpr.Cb); // Linear radiance estimate
  289. float Nc = quadratic_calc(Nl, calibration.cor[tpr.ch].quadratic); // Non-linear correction
  290. float Ne = Nl + Nc; // Corrected radiance
  291. float Testar = C2 * Vc / logf(C1 * powf(Vc, 3) / Ne + 1.0); // Equivlent black body temperature
  292. float Te = (Testar - A) / B; // Temperature (kelvin)
  293. // Convert to celsius
  294. Te -= 273.15;
  295. // Rescale to 0-255 for -100°C to +60°C
  296. return (Te + 100.0) / 160.0 * 255.0;
  297. }
  298. // Temperature calibration wrapper
  299. void apt_calibrate_thermal(int satnum, apt_image_t *img, int offset, int width) {
  300. tempparam_t temp = tempcomp(tele, img->chB, satnum);
  301. for (int y = 0; y < img->nrow; y++) {
  302. for (int x = 0; x < width; x++) {
  303. img->prow[y][x + offset] = (float)tempcal(img->prow[y][x + offset], satnum, temp);
  304. }
  305. }
  306. }
  307. float calibrate_pixel(float value, int channel, calibration_t cal) {
  308. if (value > cal.visible[channel].cutoff) {
  309. return linear_calc(value * 4.0f, cal.visible[channel].high) * 255.0f / 100.0f;
  310. } else {
  311. return linear_calc(value * 4.0f, cal.visible[channel].low) * 255.0f / 100.0f;
  312. }
  313. }
  314. void apt_calibrate_visible(int satnum, apt_image_t *img, int offset, int width) {
  315. const calibration_t calibration = get_calibration(satnum);
  316. int channel = img->chA - 1;
  317. for (int y = 0; y < img->nrow; y++) {
  318. for (int x = 0; x < width; x++) {
  319. img->prow[y][x + offset] = clamp(calibrate_pixel(img->prow[y][x + offset], channel, calibration), 255.0f, 0.0f);
  320. }
  321. }
  322. }