Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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