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.
 
 
 
 
 

172 regels
5.6 KiB

  1. /*
  2. * aptdec - A lightweight FOSS (NOAA) APT decoder
  3. * Copyright (C) 2004-2009 Thierry Leconte (F4DWV) 2019-2023 Xerbo (xerbo@protonmail.com)
  4. * Copyright (C) 2021 Jon Beniston (M7RCE)
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #ifndef APTDEC_H_
  20. #define APTDEC_H_
  21. #include <stddef.h>
  22. #include <stdint.h>
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. #if defined(__GNUC__) && (__GNUC__ >= 4)
  27. # define APTDEC_API __attribute__((visibility("default")))
  28. #elif defined(_MSC_VER)
  29. # ifdef APTDEC_API_EXPORT
  30. # define APTDEC_API __declspec(dllexport)
  31. # else
  32. # define APTDEC_API __declspec(dllimport)
  33. # endif
  34. #else
  35. # define APTDEC_API
  36. #endif
  37. // Height of a single telemetry wedge
  38. #define APT_WEDGE_HEIGHT 8
  39. // Numbers of wedges in a frame
  40. #define APT_FRAME_WEDGES 16
  41. // Height of a telemetry frame
  42. #define APT_FRAME_LEN (APT_WEDGE_HEIGHT * APT_FRAME_WEDGES)
  43. // Width of the overall image
  44. #define APT_IMG_WIDTH 2080
  45. // Width of sync marker
  46. #define APT_SYNC_WIDTH 39
  47. // Width of space view
  48. #define APT_SPC_WIDTH 47
  49. // Width of telemetry
  50. #define APT_TELEMETRY_WIDTH 45
  51. // Width of a single video channel
  52. #define APT_CH_WIDTH 909
  53. // Offset to channel A video data
  54. #define APT_CHA_OFFSET (APT_SYNC_WIDTH + APT_SPC_WIDTH)
  55. // Offset to channel B video data
  56. #define APT_CHB_OFFSET (APT_SYNC_WIDTH + APT_SPC_WIDTH + APT_CH_WIDTH + APT_TELEMETRY_WIDTH + APT_SYNC_WIDTH + APT_SPC_WIDTH)
  57. // Number of rows needed for apt_normalize to (reliably) work
  58. #define APTDEC_NORMALIZE_ROWS (APT_FRAME_LEN * 2)
  59. // Maximum amount of samples that will be requested from aptdec_callback_t
  60. #define APTDEC_BUFFER_SIZE 16384
  61. // Channel 1: visible (0.58-0.68 um)
  62. // Channel 2: near-IR (0.725-1.0 um)
  63. // Channel 3A: near-IR (1.58-1.64 um)
  64. // Channel 3B: mid-infrared (3.55-3.93 um)
  65. // Channel 4: thermal-infrared (10.3-11.3 um)
  66. // Channel 5: thermal-infrared (11.5-12.5 um)
  67. typedef enum apt_channel {
  68. AVHRR_CHANNEL_UNKNOWN,
  69. AVHRR_CHANNEL_1,
  70. AVHRR_CHANNEL_2,
  71. AVHRR_CHANNEL_3A,
  72. AVHRR_CHANNEL_4,
  73. AVHRR_CHANNEL_5,
  74. AVHRR_CHANNEL_3B
  75. } avhrr_channel_t;
  76. typedef enum apt_satellite {
  77. NOAA15,
  78. NOAA18,
  79. NOAA19
  80. } apt_satellite_t;
  81. typedef struct {
  82. uint8_t *data; // Image data
  83. size_t rows; // Number of rows
  84. // Telemetry
  85. apt_satellite_t satellite;
  86. avhrr_channel_t ch[2];
  87. float space_view[2];
  88. float telemetry[2][16];
  89. } apt_image_t;
  90. typedef struct {
  91. uint8_t r, g, b;
  92. } apt_rgb_t;
  93. typedef struct {
  94. size_t offset;
  95. size_t width;
  96. } apt_region_t;
  97. typedef struct aptdec_t aptdec_t;
  98. // Callback function to get samples
  99. // `context` is the same as passed to aptdec_getrow
  100. typedef size_t (*aptdec_callback_t)(float *samples, size_t count, void *context);
  101. // Clone an apt_image_t struct
  102. // Useful for calibration
  103. APTDEC_API apt_image_t apt_image_clone(apt_image_t img);
  104. // Returns version of libaptdec in git tag format
  105. // i.e. v2.0.0 or v2.0.0-1-xxxxxx
  106. APTDEC_API char *aptdec_get_version(void);
  107. // Create and destroy libaptdec instances
  108. // If aptdec_init fails it will return NULL
  109. APTDEC_API aptdec_t *aptdec_init(float sample_rate);
  110. APTDEC_API void aptdec_free(aptdec_t *apt);
  111. // Normalize and quantize raw image data
  112. // Data is arranged so that each row starts at APT_IMG_WIDTH*y
  113. APTDEC_API apt_image_t apt_normalize(const float *data, size_t rows, apt_satellite_t satellite, int *error);
  114. // Get an entire row of pixels
  115. // Requires that `row` has enough space to store APT_IMG_WIDTH*2
  116. // Returns 0 when `callback` return value != count
  117. APTDEC_API int aptdec_getrow(aptdec_t *apt, float *row, aptdec_callback_t callback, void *context);
  118. // Calibrate channels
  119. APTDEC_API int apt_calibrate_thermal(apt_image_t *img, apt_region_t region);
  120. APTDEC_API int apt_calibrate_visible(apt_image_t *img, apt_region_t region);
  121. APTDEC_API void apt_denoise (apt_image_t *img, apt_region_t region);
  122. APTDEC_API void apt_flip (apt_image_t *img, apt_region_t region);
  123. APTDEC_API void apt_stretch (apt_image_t *img, apt_region_t region);
  124. APTDEC_API void apt_equalize(apt_image_t *img, apt_region_t region);
  125. APTDEC_API int apt_crop (apt_image_t *img);
  126. // Composite two RGB values as layers, in most cases bottom_a will be 1.0f
  127. APTDEC_API apt_rgb_t apt_composite_rgb(apt_rgb_t top, float top_a, apt_rgb_t bottom, float bottom_a);
  128. // Apply a gradient such as temperature_gradient
  129. // If gradient is less than 256 elements it is the callers responsibility
  130. // that `val` does not exceed the length of the gradient
  131. APTDEC_API apt_rgb_t apt_gradient(const uint32_t *gradient, uint8_t val);
  132. static const apt_region_t APT_REGION_CHA = { APT_CHA_OFFSET, APT_CH_WIDTH };
  133. static const apt_region_t APT_REGION_CHB = { APT_CHB_OFFSET, APT_CH_WIDTH };
  134. static const apt_region_t APT_REGION_CHA_FULL = { 0, APT_IMG_WIDTH/2 };
  135. static const apt_region_t APT_REGION_CHB_FULL = { APT_IMG_WIDTH/2, APT_IMG_WIDTH/2 };
  136. static const apt_region_t APT_REGION_FULL = { 0, APT_IMG_WIDTH };
  137. APTDEC_API extern const uint32_t temperature_gradient[256];
  138. APTDEC_API extern const uint32_t precipitation_gradient[58];
  139. #ifdef __cplusplus
  140. }
  141. #endif
  142. #endif