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.
 
 
 
 
 

344 lines
9.4 KiB

  1. /*
  2. * This file is part of Aptdec.
  3. * Copyright (c) 2004-2009 Thierry Leconte (F4DWV), Xerbo (xerbo@protonmail.com) 2019-2020
  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 <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <getopt.h>
  23. #include <libgen.h>
  24. #include <math.h>
  25. #include <sndfile.h>
  26. #include <errno.h>
  27. #include <time.h>
  28. #include "common.h"
  29. #include "offsets.h"
  30. // DSP
  31. extern int init_dsp(double F);
  32. extern int getpixelrow(float *pixelv, int nrow, int *zenith, int reset);
  33. // I/O
  34. extern int readRawImage(char *filename, float **prow, int *nrow);
  35. extern int ImageOut(options_t *opts, image_t *img, int offset, int width, char *desc, char *chid, char *palette);
  36. extern int initWriter(options_t *opts, image_t *img, int width, int height, char *desc, char *chid);
  37. extern void pushRow(float *row, int width);
  38. extern void closeWriter();
  39. // Image functions
  40. extern int calibrate(float **prow, int nrow, int offset, int width);
  41. extern void histogramEqualise(float **prow, int nrow, int offset, int width);
  42. extern void linearEnhance(float **prow, int nrow, int offset, int width);
  43. extern void temperature(options_t *opts, image_t *img, int offset, int width);
  44. extern void denoise(float **prow, int nrow, int offset, int width);
  45. extern void distrib(options_t *opts, image_t *img, char *chid);
  46. extern void flipImage(image_t *img, int width, int offset);
  47. // Palettes
  48. extern char GviPalette[256*3];
  49. extern char TempPalette[256*3];
  50. // Row where the satellite is closest to the observer
  51. int zenith = 0;
  52. // Audio file
  53. static SNDFILE *audioFile;
  54. // Number of channels in audio file
  55. int channels = 1;
  56. // Function declarations
  57. static int initsnd(char *filename);
  58. int getsample(float *sample, int nb);
  59. static int processAudio(char *filename, options_t *opts);
  60. static void usage(void);
  61. int main(int argc, char **argv) {
  62. fprintf(stderr, VERSION"\n");
  63. // Check if there are actually any input files
  64. if(argc == optind || argc == 1){
  65. fprintf(stderr, "No input files provided.\n");
  66. usage();
  67. }
  68. options_t opts = { "r", "", 19, "", ".", 0, "" };
  69. // Parse arguments
  70. int opt;
  71. while ((opt = getopt(argc, argv, "o:m:d:i:s:e:rp:")) != EOF) {
  72. switch (opt) {
  73. case 'd':
  74. opts.path = optarg;
  75. break;
  76. case 'm':
  77. opts.map = optarg;
  78. break;
  79. case 'i':
  80. opts.type = optarg;
  81. break;
  82. case 's':
  83. opts.satnum = atoi(optarg);
  84. if(opts.satnum < 15 || opts.satnum > 19){
  85. fprintf(stderr, "Invalid satellite number, it must be the range 15-19\n");
  86. exit(EPERM);
  87. }
  88. break;
  89. case 'e':
  90. opts.effects = optarg;
  91. break;
  92. case 'r':
  93. opts.realtime = 1;
  94. break;
  95. case 'o':
  96. opts.filename = optarg;
  97. break;
  98. case 'p':
  99. opts.palette = optarg;
  100. break;
  101. default:
  102. usage();
  103. }
  104. }
  105. // Process the files
  106. for (; optind < argc; optind++) {
  107. processAudio(argv[optind], &opts);
  108. }
  109. exit(0);
  110. }
  111. static int processAudio(char *filename, options_t *opts){
  112. // Image info struct
  113. image_t img;
  114. // Mapping between wedge value and channel ID
  115. static struct {
  116. char *id[7];
  117. char *name[7];
  118. } ch = {
  119. { "?", "1", "2", "3A", "4", "5", "3B" },
  120. { "unknown", "visble", "near-infrared", "mid-infrared", "thermal-infrared", "thermal-infrared", "mid-infrared" }
  121. };
  122. // Buffer for image channel
  123. char desc[60];
  124. // Parse file path
  125. char path[256], extension[32];
  126. strcpy(path, filename);
  127. strcpy(path, dirname(path));
  128. sscanf(basename(filename), "%[^.].%s", img.name, extension);
  129. if(opts->realtime){
  130. // Set output filename to current time when in realtime mode
  131. time_t t;
  132. time(&t);
  133. strncpy(img.name, ctime(&t), 24);
  134. // Init a row writer
  135. initWriter(opts, &img, IMG_WIDTH, MAX_HEIGHT, "Unprocessed realtime image", "r");
  136. }
  137. if(strcmp(extension, "png") == 0){
  138. // Read PNG into image buffer
  139. printf("Reading %s\n", filename);
  140. if(readRawImage(filename, img.prow, &img.nrow) == 0){
  141. fprintf(stderr, "Skipping %s\n", img.name);
  142. return 0;
  143. }
  144. }else{
  145. // Attempt to open the audio file
  146. if (initsnd(filename) == 0)
  147. exit(EPERM);
  148. // Build image
  149. // TODO: multithreading, would require some sort of input buffer
  150. for (img.nrow = 0; img.nrow < MAX_HEIGHT; img.nrow++) {
  151. // Allocate memory for this row
  152. img.prow[img.nrow] = (float *) malloc(sizeof(float) * 2150);
  153. // Write into memory and break the loop when there are no more samples to read
  154. if (getpixelrow(img.prow[img.nrow], img.nrow, &zenith, (img.nrow == 0)) == 0)
  155. break;
  156. if(opts->realtime) pushRow(img.prow[img.nrow], IMG_WIDTH);
  157. fprintf(stderr, "Row: %d\r", img.nrow);
  158. fflush(stderr);
  159. }
  160. // Close stream
  161. sf_close(audioFile);
  162. }
  163. if(opts->realtime) closeWriter();
  164. printf("Total rows: %d\n", img.nrow);
  165. // Fallback for detecting the zenith
  166. // TODO: encode metadata in raw images
  167. if(opts->map != NULL && opts->map[0] != '\0' && zenith == 0){
  168. fprintf(stderr, "Guessing zenith in image, map will most likely be misaligned.\n");
  169. zenith = img.nrow / 2;
  170. }
  171. // Calibrate
  172. img.chA = calibrate(img.prow, img.nrow, CHA_OFFSET, CH_WIDTH);
  173. img.chB = calibrate(img.prow, img.nrow, CHB_OFFSET, CH_WIDTH);
  174. printf("Channel A: %s (%s)\n", ch.id[img.chA], ch.name[img.chA]);
  175. printf("Channel B: %s (%s)\n", ch.id[img.chB], ch.name[img.chB]);
  176. // Denoise
  177. if(CONTAINS(opts->effects, 'd')){
  178. denoise(img.prow, img.nrow, CHA_OFFSET, CH_WIDTH);
  179. denoise(img.prow, img.nrow, CHB_OFFSET, CH_WIDTH);
  180. }
  181. // Flip, for southbound passes
  182. if(CONTAINS(opts->effects, 'f')){
  183. flipImage(&img, CH_WIDTH, CHA_OFFSET);
  184. flipImage(&img, CH_WIDTH, CHB_OFFSET);
  185. }
  186. // Temperature
  187. if (CONTAINS(opts->type, 't') && img.chB >= 4) {
  188. temperature(opts, &img, CHB_OFFSET, CH_WIDTH);
  189. ImageOut(opts, &img, CHB_OFFSET, CH_WIDTH, "Temperature", "t", (char *)TempPalette);
  190. }
  191. // MCIR
  192. if (CONTAINS(opts->type, 'm'))
  193. ImageOut(opts, &img, CHA_OFFSET, CH_WIDTH, "MCIR", "m", NULL);
  194. // Linear equalise
  195. if(CONTAINS(opts->effects, 'l')){
  196. linearEnhance(img.prow, img.nrow, CHA_OFFSET, CH_WIDTH);
  197. linearEnhance(img.prow, img.nrow, CHB_OFFSET, CH_WIDTH);
  198. }
  199. // Histogram equalise
  200. if(CONTAINS(opts->effects, 'h')){
  201. histogramEqualise(img.prow, img.nrow, CHA_OFFSET, CH_WIDTH);
  202. histogramEqualise(img.prow, img.nrow, CHB_OFFSET, CH_WIDTH);
  203. }
  204. // Raw image
  205. if (CONTAINS(opts->type, 'r')) {
  206. sprintf(desc, "%s (%s) & %s (%s)", ch.id[img.chA], ch.name[img.chA], ch.id[img.chB], ch.name[img.chB]);
  207. ImageOut(opts, &img, 0, IMG_WIDTH, desc, "r", NULL);
  208. }
  209. // Palette image
  210. if (CONTAINS(opts->type, 'p')) {
  211. img.palette = opts->palette;
  212. strcpy(desc, "Palette composite");
  213. ImageOut(opts, &img, 0, 909, desc, "p", NULL);
  214. }
  215. // Channel A
  216. if (CONTAINS(opts->type, 'a')) {
  217. sprintf(desc, "%s (%s)", ch.id[img.chA], ch.name[img.chA]);
  218. ImageOut(opts, &img, CHA_OFFSET, CH_WIDTH, desc, ch.id[img.chA], NULL);
  219. }
  220. // Channel B
  221. if (CONTAINS(opts->type, 'b')) {
  222. sprintf(desc, "%s (%s)", ch.id[img.chB], ch.name[img.chB]);
  223. ImageOut(opts, &img, CHB_OFFSET, CH_WIDTH, desc, ch.id[img.chB], NULL);
  224. }
  225. // Value distribution image
  226. if (CONTAINS(opts->type, 'd'))
  227. distrib(opts, &img, "d");
  228. return 1;
  229. }
  230. static int initsnd(char *filename) {
  231. SF_INFO infwav;
  232. int res;
  233. // Open audio file
  234. infwav.format = 0;
  235. audioFile = sf_open(filename, SFM_READ, &infwav);
  236. if (audioFile == NULL) {
  237. fprintf(stderr, "Could not open %s for reading\n", filename);
  238. return 0;
  239. }
  240. res = init_dsp(infwav.samplerate);
  241. printf("Input file: %s\n", filename);
  242. if(res < 0) {
  243. fprintf(stderr, "Input sample rate too low: %d\n", infwav.samplerate);
  244. return 0;
  245. }else if(res > 0) {
  246. fprintf(stderr, "Input sample rate too high: %d\n", infwav.samplerate);
  247. return 0;
  248. }
  249. printf("Input sample rate: %d\n", infwav.samplerate);
  250. channels = infwav.channels;
  251. return 1;
  252. }
  253. // Read samples from the wave file
  254. int getsample(float *sample, int nb) {
  255. if(channels == 1){
  256. return sf_read_float(audioFile, sample, nb);
  257. }else{
  258. /* Multi channel audio is encoded such as:
  259. * Ch1,Ch2,Ch1,Ch2,Ch1,Ch2
  260. */
  261. float buf[nb * channels]; // Something like BLKIN*2 could also be used
  262. int samples = sf_read_float(audioFile, buf, nb * channels);
  263. for(int i = 0; i < nb; i++) sample[i] = buf[i * channels];
  264. return samples / channels;
  265. }
  266. }
  267. static void usage(void) {
  268. fprintf(stderr,
  269. "Aptdec [options] audio files ...\n"
  270. "Options:\n"
  271. " -e [t|h|d|p|f|l] Effects\n"
  272. " t: Crop telemetry\n"
  273. " h: Histogram equalise\n"
  274. " d: Denoise\n"
  275. " p: Precipitation\n"
  276. " f: Flip image\n"
  277. " l: Linear equalise\n"
  278. " -i [r|a|b|c|t|m|p] Output image\n"
  279. " r: Raw\n"
  280. " a: Channel A\n"
  281. " b: Channel B\n"
  282. " t: Temperature\n"
  283. " m: MCIR\n"
  284. " p: Paletted image\n"
  285. " -d <dir> Image destination directory.\n"
  286. " -o <name> Output filename\n"
  287. " -s [15-19] Satellite number\n"
  288. " -m <file> Map file\n"
  289. " -r Realtime decode\n"
  290. " -p Path to palette\n"
  291. "\nRefer to the README for more infomation\n");
  292. exit(EINVAL);
  293. }