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.
 
 
 
 
 

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