Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

main.c 9.2 KiB

21 år sedan
21 år sedan
21 år sedan
21 år sedan
21 år sedan
19 år sedan
21 år sedan
21 år sedan
21 år sedan
21 år sedan
4 år sedan
4 år sedan
21 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. #ifndef _MSC_VER
  23. #include <libgen.h>
  24. #else
  25. #include <windows.h>
  26. #endif
  27. #include <math.h>
  28. #include <sndfile.h>
  29. #include <errno.h>
  30. #include <time.h>
  31. #include "libs/argparse.h"
  32. #include "offsets.h"
  33. #include "common.h"
  34. #include "apt.h"
  35. #include "pngio.h"
  36. #include "image.h"
  37. #include "color.h"
  38. // Audio file
  39. static SNDFILE *audioFile;
  40. // Number of channels in audio file
  41. int channels = 1;
  42. // Function declarations
  43. static int initsnd(char *filename);
  44. int getsample(float *sample, int nb);
  45. static int processAudio(char *filename, options_t *opts);
  46. #ifdef _MSC_VER
  47. // Functions not supported by MSVC
  48. static char *dirname(char *path)
  49. {
  50. static char dir[MAX_PATH];
  51. _splitpath(path, NULL, dir, NULL, NULL);
  52. return dir;
  53. }
  54. static char *basename(char *path)
  55. {
  56. static char base[MAX_PATH];
  57. _splitpath(path, NULL, NULL, base, NULL);
  58. return base;
  59. }
  60. #endif
  61. int main(int argc, const char **argv) {
  62. options_t opts = { "r", "", 19, "", ".", 0, "", "", 1.0, 0 };
  63. static const char *const usages[] = {
  64. "aptdec [options] [[--] sources]",
  65. "aptdec [sources]",
  66. NULL,
  67. };
  68. struct argparse_option options[] = {
  69. OPT_HELP(),
  70. OPT_GROUP("Image options"),
  71. OPT_STRING('i', "image", &opts.type, "set output image type (see the README for a list)", NULL, 0, 0),
  72. OPT_STRING('e', "effect", &opts.effects, "add an effect (see the README for a list)", NULL, 0, 0),
  73. OPT_FLOAT('g', "gamma", &opts.gamma, "gamma adjustment (1.0 = off)", NULL, 0, 0),
  74. OPT_GROUP("Satellite options"),
  75. OPT_INTEGER('s', "satellite", &opts.satnum, "satellite ID, must be between 15 and 19", NULL, 0, 0),
  76. OPT_GROUP("Paths"),
  77. OPT_STRING('p', "palette", &opts.palette, "path to a palette", NULL, 0, 0),
  78. OPT_STRING('m', "map", &opts.map, "path to a WXtoImg map", NULL, 0, 0),
  79. OPT_STRING('o', "filename", &opts.filename, "filename of the output image", NULL, 0, 0),
  80. OPT_STRING('d', "output", &opts.path, "output directory (must exist first)", NULL, 0, 0),
  81. OPT_GROUP("Misc"),
  82. OPT_BOOLEAN('r', "realtime", &opts.realtime, "decode in realtime", NULL, 0, 0),
  83. OPT_INTEGER('k', "map-offset", &opts.mapOffset, "Map offset (in px, default 0)", NULL, 0, 0),
  84. OPT_END(),
  85. };
  86. struct argparse argparse;
  87. argparse_init(&argparse, options, usages, 0);
  88. argparse_describe(&argparse, "\nA lightweight FOSS NOAA APT satellite imagery decoder.", "\nSee `README.md` for a full description of command line arguments and `LICENSE` for licensing conditions.");
  89. argc = argparse_parse(&argparse, argc, argv);
  90. if(argc == 0){
  91. argparse_usage(&argparse);
  92. }
  93. // Actually decode the files
  94. for (int i = 0; i < argc; i++) {
  95. char *filename = strdup(argv[i]);
  96. processAudio(filename, &opts);
  97. }
  98. return 0;
  99. }
  100. static int processAudio(char *filename, options_t *opts){
  101. // Image info struct
  102. apt_image_t img;
  103. // Mapping between wedge value and channel ID
  104. static struct {
  105. char *id[7];
  106. char *name[7];
  107. } ch = {
  108. { "?", "1", "2", "3A", "4", "5", "3B" },
  109. { "unknown", "visble", "near-infrared", "mid-infrared", "thermal-infrared", "thermal-infrared", "mid-infrared" }
  110. };
  111. // Buffer for image channel
  112. char desc[60];
  113. // Parse file path
  114. char path[256], extension[32];
  115. strcpy(path, filename);
  116. strcpy(path, dirname(path));
  117. sscanf(basename(filename), "%255[^.].%31s", img.name, extension);
  118. if(opts->realtime){
  119. // Set output filename to current time when in realtime mode
  120. time_t t;
  121. time(&t);
  122. strncpy(img.name, ctime(&t), 24);
  123. // Init a row writer
  124. initWriter(opts, &img, IMG_WIDTH, APT_MAX_HEIGHT, "Unprocessed realtime image", "r");
  125. }
  126. if(strcmp(extension, "png") == 0){
  127. // Read PNG into image buffer
  128. printf("Reading %s\n", filename);
  129. if(readRawImage(filename, img.prow, &img.nrow) == 0){
  130. exit(EPERM);
  131. }
  132. }else{
  133. // Attempt to open the audio file
  134. if (initsnd(filename) == 0)
  135. exit(EPERM);
  136. // Build image
  137. // TODO: multithreading, would require some sort of input buffer
  138. for (img.nrow = 0; img.nrow < APT_MAX_HEIGHT; img.nrow++) {
  139. // Allocate memory for this row
  140. img.prow[img.nrow] = (float *) malloc(sizeof(float) * 2150);
  141. // Write into memory and break the loop when there are no more samples to read
  142. if (apt_getpixelrow(img.prow[img.nrow], img.nrow, &img.zenith, (img.nrow == 0), getsample) == 0)
  143. break;
  144. if(opts->realtime) pushRow(img.prow[img.nrow], IMG_WIDTH);
  145. fprintf(stderr, "Row: %d\r", img.nrow);
  146. fflush(stderr);
  147. }
  148. // Close stream
  149. sf_close(audioFile);
  150. }
  151. if(opts->realtime) closeWriter();
  152. printf("Total rows: %d\n", img.nrow);
  153. // Fallback for detecting the zenith
  154. // TODO: encode metadata in raw images
  155. if(opts->map != NULL && opts->map[0] != '\0' && img.zenith == 0){
  156. fprintf(stderr, "Guessing zenith in image, map will most likely be misaligned.\n");
  157. img.zenith = img.nrow / 2;
  158. }
  159. // Calibrate
  160. img.chA = apt_calibrate(img.prow, img.nrow, CHA_OFFSET, CH_WIDTH);
  161. img.chB = apt_calibrate(img.prow, img.nrow, CHB_OFFSET, CH_WIDTH);
  162. printf("Channel A: %s (%s)\n", ch.id[img.chA], ch.name[img.chA]);
  163. printf("Channel B: %s (%s)\n", ch.id[img.chB], ch.name[img.chB]);
  164. // Crop noise from start and end of image
  165. if(CONTAINS(opts->effects, Crop_Noise)){
  166. img.zenith -= apt_cropNoise(&img);
  167. }
  168. // Denoise
  169. if(CONTAINS(opts->effects, Denoise)){
  170. apt_denoise(img.prow, img.nrow, CHA_OFFSET, CH_WIDTH);
  171. apt_denoise(img.prow, img.nrow, CHB_OFFSET, CH_WIDTH);
  172. }
  173. // Flip, for northbound passes
  174. if(CONTAINS(opts->effects, Flip_Image)){
  175. apt_flipImage(&img, CH_WIDTH, CHA_OFFSET);
  176. apt_flipImage(&img, CH_WIDTH, CHB_OFFSET);
  177. }
  178. // Temperature
  179. if (CONTAINS(opts->type, Temperature) && img.chB >= 4) {
  180. // Create another buffer as to not modify the orignal
  181. apt_image_t tmpimg = img;
  182. for(int i = 0; i < img.nrow; i++){
  183. tmpimg.prow[i] = (float *) malloc(sizeof(float) * 2150);
  184. memcpy(tmpimg.prow[i], img.prow[i], sizeof(float) * 2150);
  185. }
  186. // Perform temperature calibration
  187. temperature(opts, &tmpimg, CHB_OFFSET, CH_WIDTH);
  188. ImageOut(opts, &tmpimg, CHB_OFFSET, CH_WIDTH, "Temperature", Temperature, (char *)apt_TempPalette);
  189. }
  190. // MCIR
  191. if (CONTAINS(opts->type, MCIR))
  192. ImageOut(opts, &img, CHA_OFFSET, CH_WIDTH, "MCIR", MCIR, NULL);
  193. // Linear equalise
  194. if(CONTAINS(opts->effects, Linear_Equalise)){
  195. apt_linearEnhance(img.prow, img.nrow, CHA_OFFSET, CH_WIDTH);
  196. apt_linearEnhance(img.prow, img.nrow, CHB_OFFSET, CH_WIDTH);
  197. }
  198. // Histogram equalise
  199. if(CONTAINS(opts->effects, Histogram_Equalise)){
  200. apt_histogramEqualise(img.prow, img.nrow, CHA_OFFSET, CH_WIDTH);
  201. apt_histogramEqualise(img.prow, img.nrow, CHB_OFFSET, CH_WIDTH);
  202. }
  203. // Raw image
  204. if (CONTAINS(opts->type, Raw_Image)) {
  205. sprintf(desc, "%s (%s) & %s (%s)", ch.id[img.chA], ch.name[img.chA], ch.id[img.chB], ch.name[img.chB]);
  206. ImageOut(opts, &img, 0, IMG_WIDTH, desc, Raw_Image, NULL);
  207. }
  208. // Palette image
  209. if (CONTAINS(opts->type, Palleted)) {
  210. img.palette = opts->palette;
  211. strcpy(desc, "Palette composite");
  212. ImageOut(opts, &img, CHA_OFFSET, 909, desc, Palleted, NULL);
  213. }
  214. // Channel A
  215. if (CONTAINS(opts->type, Channel_A)) {
  216. sprintf(desc, "%s (%s)", ch.id[img.chA], ch.name[img.chA]);
  217. ImageOut(opts, &img, CHA_OFFSET, CH_WIDTH, desc, Channel_A, NULL);
  218. }
  219. // Channel B
  220. if (CONTAINS(opts->type, Channel_B)) {
  221. sprintf(desc, "%s (%s)", ch.id[img.chB], ch.name[img.chB]);
  222. ImageOut(opts, &img, CHB_OFFSET, CH_WIDTH, desc, Channel_B, NULL);
  223. }
  224. return 1;
  225. }
  226. static int initsnd(char *filename) {
  227. SF_INFO infwav;
  228. int res;
  229. // Open audio file
  230. infwav.format = 0;
  231. audioFile = sf_open(filename, SFM_READ, &infwav);
  232. if (audioFile == NULL) {
  233. fprintf(stderr, "Could not open %s\n", filename);
  234. return 0;
  235. }
  236. res = apt_init(infwav.samplerate);
  237. printf("Input file: %s\n", filename);
  238. if(res < 0) {
  239. fprintf(stderr, "Input sample rate too low: %d\n", infwav.samplerate);
  240. return 0;
  241. }else if(res > 0) {
  242. fprintf(stderr, "Input sample rate too high: %d\n", infwav.samplerate);
  243. return 0;
  244. }
  245. printf("Input sample rate: %d\n", infwav.samplerate);
  246. channels = infwav.channels;
  247. return 1;
  248. }
  249. // Read samples from the audio file
  250. int getsample(float *sample, int nb) {
  251. if(channels == 1){
  252. return (int)sf_read_float(audioFile, sample, nb);
  253. }else{
  254. /* Multi channel audio is encoded such as:
  255. * Ch1,Ch2,Ch1,Ch2,Ch1,Ch2
  256. */
  257. float *buf = malloc(sizeof(float) * nb * channels); // Something like BLKIN*2 could also be used
  258. int samples = (int)sf_read_float(audioFile, buf, nb * channels);
  259. for(int i = 0; i < nb; i++) sample[i] = buf[i * channels];
  260. free(buf);
  261. return samples / channels;
  262. }
  263. }