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.
 
 
 
 
 

337 rivejä
9.2 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);
  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 predeclarations
  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, "m:d:i:s:e:r")) != 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. default:
  96. usage();
  97. }
  98. }
  99. // Process the files
  100. for (; optind < argc; optind++) {
  101. processAudio(argv[optind], &opts);
  102. }
  103. exit(0);
  104. }
  105. static int processAudio(char *filename, options_t *opts){
  106. // Image info struct
  107. image_t img;
  108. // Mapping between wedge value and channel ID
  109. static struct {
  110. char *id[7];
  111. char *name[7];
  112. } ch = {
  113. { "?", "1", "2", "3A", "4", "5", "3B" },
  114. { "unknown", "visble", "near-infrared", "mid-infrared", "thermal-infrared", "thermal-infrared", "mid-infrared" }
  115. };
  116. // Buffer for image channel
  117. char desc[60];
  118. // Parse file path
  119. char path[256], extension[32];
  120. strcpy(path, filename);
  121. strcpy(path, dirname(path));
  122. sscanf(basename(filename), "%[^.].%s", img.name, extension);
  123. // Set output filename to current time when in realtime mode
  124. if(opts->realtime){
  125. time_t t;
  126. time(&t);
  127. strncpy(img.name, ctime(&t), 24);
  128. }
  129. if(opts->realtime) initWriter(opts, &img, IMG_WIDTH, MAX_HEIGHT, "Unprocessed realtime image", "r");
  130. if(strcmp(extension, "png") == 0){
  131. // Read PNG into image buffer
  132. printf("Reading %s\n", filename);
  133. if(readRawImage(filename, img.prow, &img.nrow) == 0){
  134. fprintf(stderr, "Skipping %s; see above.\n", img.name);
  135. return 0;
  136. }
  137. }else{
  138. // Attempt to open the audio file
  139. if (initsnd(filename) == 0)
  140. exit(EPERM);
  141. // Build image
  142. // TODO: multithreading, would require some sort of input buffer
  143. for (img.nrow = 0; img.nrow < MAX_HEIGHT; img.nrow++) {
  144. // Allocate memory for this row
  145. img.prow[img.nrow] = (float *) malloc(sizeof(float) * 2150);
  146. // Write into memory and break the loop when there are no more samples to read
  147. if (getpixelrow(img.prow[img.nrow], img.nrow, &zenith) == 0)
  148. break;
  149. if(opts->realtime) pushRow(img.prow[img.nrow], IMG_WIDTH);
  150. fprintf(stderr, "Row: %d\r", img.nrow);
  151. fflush(stderr);
  152. }
  153. // Close stream
  154. sf_close(audioFile);
  155. }
  156. if(opts->realtime) closeWriter();
  157. printf("Total rows: %d\n", img.nrow);
  158. // Fallback for detecting the zenith
  159. // TODO: encode zenith in raw images
  160. if(opts->map != NULL && opts->map[0] != '\0' && zenith == 0){
  161. fprintf(stderr, "Guessing zenith in image, map will most likely be misaligned.\n");
  162. zenith = img.nrow / 2;
  163. }
  164. // Calibrate
  165. img.chA = calibrate(img.prow, img.nrow, CHA_OFFSET, CH_WIDTH);
  166. img.chB = calibrate(img.prow, img.nrow, CHB_OFFSET, CH_WIDTH);
  167. printf("Channel A: %s (%s)\n", ch.id[img.chA], ch.name[img.chA]);
  168. printf("Channel B: %s (%s)\n", ch.id[img.chB], ch.name[img.chB]);
  169. // Denoise
  170. if(CONTAINS(opts->effects, 'd')){
  171. denoise(img.prow, img.nrow, CHA_OFFSET, CH_WIDTH);
  172. denoise(img.prow, img.nrow, CHB_OFFSET, CH_WIDTH);
  173. }
  174. // Flip, for southbound passes
  175. if(CONTAINS(opts->effects, 'f')){
  176. flipImage(&img, CH_WIDTH, CHA_OFFSET);
  177. flipImage(&img, CH_WIDTH, CHB_OFFSET);
  178. }
  179. // Temperature
  180. if (CONTAINS(opts->type, 't') && img.chB >= 4) {
  181. temperature(opts, &img, CHB_OFFSET, CH_WIDTH);
  182. ImageOut(opts, &img, CHB_OFFSET, CH_WIDTH, "Temperature", "t", (char *)TempPalette);
  183. }
  184. // MCIR
  185. if (CONTAINS(opts->type, 'm'))
  186. ImageOut(opts, &img, CHA_OFFSET, CH_WIDTH, "MCIR", "m", NULL);
  187. // Linear equalise
  188. if(CONTAINS(opts->effects, 'l')){
  189. linearEnhance(img.prow, img.nrow, CHA_OFFSET, CH_WIDTH);
  190. linearEnhance(img.prow, img.nrow, CHB_OFFSET, CH_WIDTH);
  191. }
  192. // Histogram equalise
  193. if(CONTAINS(opts->effects, 'h')){
  194. histogramEqualise(img.prow, img.nrow, CHA_OFFSET, CH_WIDTH);
  195. histogramEqualise(img.prow, img.nrow, CHB_OFFSET, CH_WIDTH);
  196. }
  197. // False color
  198. if(CONTAINS(opts->type, 'c')){
  199. if(img.chA == 2 && img.chB >= 4){
  200. ImageOut(opts, &img, 0, CH_WIDTH, "False Color", "c", NULL);
  201. }else{
  202. fprintf(stderr, "Lacking channels required for false color computation\n");
  203. }
  204. }
  205. // Raw image
  206. if (CONTAINS(opts->type, 'r')) {
  207. sprintf(desc, "%s (%s) & %s (%s)", ch.id[img.chA], ch.name[img.chA], ch.id[img.chB], ch.name[img.chB]);
  208. ImageOut(opts, &img, 0, IMG_WIDTH, desc, "r", NULL);
  209. }
  210. // Channel A
  211. if (CONTAINS(opts->type, 'a')) {
  212. sprintf(desc, "%s (%s)", ch.id[img.chA], ch.name[img.chA]);
  213. ImageOut(opts, &img, CHA_OFFSET, CH_WIDTH, desc, ch.id[img.chA], NULL);
  214. }
  215. // Channel B
  216. if (CONTAINS(opts->type, 'b')) {
  217. sprintf(desc, "%s (%s)", ch.id[img.chB], ch.name[img.chB]);
  218. ImageOut(opts, &img, CHB_OFFSET, CH_WIDTH, desc, ch.id[img.chB], NULL);
  219. }
  220. // Distribution image
  221. if (CONTAINS(opts->type, 'd'))
  222. distrib(opts, &img, "d");
  223. return 1;
  224. }
  225. static int initsnd(char *filename) {
  226. SF_INFO infwav;
  227. int res;
  228. // Open audio file
  229. infwav.format = 0;
  230. audioFile = sf_open(filename, SFM_READ, &infwav);
  231. if (audioFile == NULL) {
  232. fprintf(stderr, "Could not open %s for reading\n", filename);
  233. return 0;
  234. }
  235. res = init_dsp(infwav.samplerate);
  236. printf("Input file: %s\n", filename);
  237. if(res < 0) {
  238. fprintf(stderr, "Input sample rate too low: %d\n", infwav.samplerate);
  239. return 0;
  240. }else if(res > 0) {
  241. fprintf(stderr, "Input sample rate too high: %d\n", infwav.samplerate);
  242. return 0;
  243. }
  244. printf("Input sample rate: %d\n", infwav.samplerate);
  245. channels = infwav.channels;
  246. return 1;
  247. }
  248. // Read samples from the wave file
  249. int getsample(float *sample, int nb) {
  250. if(channels == 1){
  251. return sf_read_float(audioFile, sample, nb);
  252. }else{
  253. /* Multi channel audio is encoded such as:
  254. * Ch1,Ch2,Ch1,Ch2,Ch1,Ch2
  255. */
  256. float buf[nb * channels]; // Something like BLKIN*2 could also be used
  257. int samples = sf_read_float(audioFile, buf, nb * channels);
  258. for(int i = 0; i < nb; i++) sample[i] = buf[i * channels];
  259. return samples / channels;
  260. }
  261. }
  262. static void usage(void) {
  263. fprintf(stderr,
  264. "Aptdec [options] audio files ...\n"
  265. "Options:\n"
  266. " -e [t|h|d|p|f|l] Effects\n"
  267. " t: Crop telemetry\n"
  268. " h: Histogram equalise\n"
  269. " d: Denoise\n"
  270. " p: Precipitation\n"
  271. " f: Flip image\n"
  272. " l: Linear equalise\n"
  273. " -i [r|a|b|c|t|m] Output image\n"
  274. " r: Raw\n"
  275. " a: Channel A\n"
  276. " b: Channel B\n"
  277. " c: False color\n"
  278. " t: Temperature\n"
  279. " m: MCIR\n"
  280. " -d <dir> Image destination directory.\n"
  281. " -s [15-19] Satellite number\n"
  282. " -m <file> Map file\n"
  283. " -r Realtime decode\n"
  284. "\nRefer to the README for more infomation\n");
  285. exit(EINVAL);
  286. }