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.
 
 
 
 
 

330 lines
9.5 KiB

  1. /*
  2. * This file is part of Aptdec.
  3. * Copyright (c) 2004-2009 Thierry Leconte (F4DWV), Xerbo (xerbo@protonmail.com) 2019-2022
  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 "argparse/argparse.h"
  32. #include "common.h"
  33. #include "apt.h"
  34. #include "pngio.h"
  35. #include "image.h"
  36. #include "color.h"
  37. #include "util.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 getsamples(void *context, float *samples, 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 = {
  63. .type = "r",
  64. .effects = "",
  65. .satnum = 19,
  66. .path = ".",
  67. .realtime = 0,
  68. .filename = "",
  69. .palette = "",
  70. .gamma = 1.0
  71. };
  72. static const char *const usages[] = {
  73. "aptdec [options] [[--] sources]",
  74. "aptdec [sources]",
  75. NULL,
  76. };
  77. struct argparse_option options[] = {
  78. OPT_HELP(),
  79. OPT_GROUP("Image options"),
  80. OPT_STRING('i', "image", &opts.type, "set output image type (see the README for a list)", NULL, 0, 0),
  81. OPT_STRING('e', "effect", &opts.effects, "add an effect (see the README for a list)", NULL, 0, 0),
  82. OPT_FLOAT('g', "gamma", &opts.gamma, "gamma adjustment (1.0 = off)", NULL, 0, 0),
  83. OPT_GROUP("Satellite options"),
  84. OPT_INTEGER('s', "satellite", &opts.satnum, "satellite ID, must be between 15 and 19", NULL, 0, 0),
  85. OPT_GROUP("Paths"),
  86. OPT_STRING('p', "palette", &opts.palette, "path to a palette", NULL, 0, 0),
  87. OPT_STRING('o', "filename", &opts.filename, "filename of the output image", NULL, 0, 0),
  88. OPT_STRING('d', "output", &opts.path, "output directory (must exist first)", NULL, 0, 0),
  89. OPT_GROUP("Misc"),
  90. OPT_BOOLEAN('r', "realtime", &opts.realtime, "decode in realtime", NULL, 0, 0),
  91. OPT_END(),
  92. };
  93. struct argparse argparse;
  94. argparse_init(&argparse, options, usages, 0);
  95. 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.");
  96. argc = argparse_parse(&argparse, argc, argv);
  97. if(argc == 0){
  98. argparse_usage(&argparse);
  99. }
  100. // Actually decode the files
  101. for (int i = 0; i < argc; i++) {
  102. char *filename = strdup(argv[i]);
  103. processAudio(filename, &opts);
  104. }
  105. return 0;
  106. }
  107. static int processAudio(char *filename, options_t *opts){
  108. // Image info struct
  109. apt_image_t img;
  110. // Mapping between wedge value and channel ID
  111. static struct {
  112. char *id[7];
  113. char *name[7];
  114. } ch = {
  115. { "?", "1", "2", "3A", "4", "5", "3B" },
  116. { "unknown", "visble", "near-infrared", "near-infrared", "thermal-infrared", "thermal-infrared", "mid-infrared" }
  117. };
  118. // Buffer for image channel
  119. char desc[60];
  120. // Parse file path
  121. char path[256], extension[32];
  122. strcpy(path, filename);
  123. strcpy(path, dirname(path));
  124. sscanf(basename(filename), "%255[^.].%31s", img.name, extension);
  125. if(opts->realtime){
  126. // Set output filename to current time when in realtime mode
  127. time_t t;
  128. time(&t);
  129. strncpy(img.name, ctime(&t), 24);
  130. // Init a row writer
  131. initWriter(opts, &img, APT_IMG_WIDTH, APT_MAX_HEIGHT, "Unprocessed realtime image", "r");
  132. }
  133. if(strcmp(extension, "png") == 0){
  134. // Read PNG into image buffer
  135. printf("Reading %s\n", filename);
  136. if(readRawImage(filename, img.prow, &img.nrow) == 0){
  137. exit(EPERM);
  138. }
  139. }else{
  140. // Attempt to open the audio file
  141. if (initsnd(filename) == 0)
  142. exit(EPERM);
  143. // Build image
  144. // TODO: multithreading, would require some sort of input buffer
  145. for (img.nrow = 0; img.nrow < APT_MAX_HEIGHT; img.nrow++) {
  146. // Allocate memory for this row
  147. img.prow[img.nrow] = (float *) malloc(sizeof(float) * APT_PROW_WIDTH);
  148. // Write into memory and break the loop when there are no more samples to read
  149. if (apt_getpixelrow(img.prow[img.nrow], img.nrow, &img.zenith, (img.nrow == 0), getsamples, NULL) == 0)
  150. break;
  151. if(opts->realtime) pushRow(img.prow[img.nrow], APT_IMG_WIDTH);
  152. fprintf(stderr, "Row: %d\r", img.nrow);
  153. fflush(stderr);
  154. }
  155. // Close stream
  156. sf_close(audioFile);
  157. }
  158. if(opts->realtime) closeWriter();
  159. printf("Total rows: %d\n", img.nrow);
  160. // Calibrate
  161. img.chA = apt_calibrate(img.prow, img.nrow, APT_CHA_OFFSET, APT_CH_WIDTH);
  162. img.chB = apt_calibrate(img.prow, img.nrow, APT_CHB_OFFSET, APT_CH_WIDTH);
  163. printf("Channel A: %s (%s)\n", ch.id[img.chA], ch.name[img.chA]);
  164. printf("Channel B: %s (%s)\n", ch.id[img.chB], ch.name[img.chB]);
  165. // Crop noise from start and end of image
  166. if(CONTAINS(opts->effects, Crop_Noise)){
  167. img.zenith -= apt_cropNoise(&img);
  168. }
  169. // Denoise
  170. if(CONTAINS(opts->effects, Denoise)){
  171. apt_denoise(img.prow, img.nrow, APT_CHA_OFFSET, APT_CH_WIDTH);
  172. apt_denoise(img.prow, img.nrow, APT_CHB_OFFSET, APT_CH_WIDTH);
  173. }
  174. // Flip, for northbound passes
  175. if(CONTAINS(opts->effects, Flip_Image)){
  176. apt_flipImage(&img, APT_CH_WIDTH, APT_CHA_OFFSET);
  177. apt_flipImage(&img, APT_CH_WIDTH, APT_CHB_OFFSET);
  178. }
  179. // Temperature
  180. if (CONTAINS(opts->type, Temperature) && img.chB >= 4) {
  181. // Create another buffer as to not modify the orignal
  182. apt_image_t tmpimg = img;
  183. for(int i = 0; i < img.nrow; i++){
  184. tmpimg.prow[i] = (float *) malloc(sizeof(float) * APT_PROW_WIDTH);
  185. memcpy(tmpimg.prow[i], img.prow[i], sizeof(float) * APT_PROW_WIDTH);
  186. }
  187. // Perform temperature calibration
  188. apt_calibrate_thermal(opts->satnum, &tmpimg, APT_CHB_OFFSET, APT_CH_WIDTH);
  189. ImageOut(opts, &tmpimg, APT_CHB_OFFSET, APT_CH_WIDTH, "Temperature", Temperature, (char *)apt_TempPalette);
  190. }
  191. // Temperature
  192. if (CONTAINS(opts->type, Visible) && img.chA <= 2) {
  193. // Create another buffer as to not modify the orignal
  194. apt_image_t tmpimg = img;
  195. for(int i = 0; i < img.nrow; i++){
  196. tmpimg.prow[i] = (float *) malloc(sizeof(float) * APT_PROW_WIDTH);
  197. memcpy(tmpimg.prow[i], img.prow[i], sizeof(float) * APT_PROW_WIDTH);
  198. }
  199. // Perform visible calibration
  200. apt_calibrate_visible(opts->satnum, &tmpimg, APT_CHA_OFFSET, APT_CH_WIDTH);
  201. ImageOut(opts, &tmpimg, APT_CHA_OFFSET, APT_CH_WIDTH, "Visible", Visible, (char *)apt_TempPalette);
  202. }
  203. // Linear equalise
  204. if(CONTAINS(opts->effects, Linear_Equalise)){
  205. apt_linearEnhance(img.prow, img.nrow, APT_CHA_OFFSET, APT_CH_WIDTH);
  206. apt_linearEnhance(img.prow, img.nrow, APT_CHB_OFFSET, APT_CH_WIDTH);
  207. }
  208. // Histogram equalise
  209. if(CONTAINS(opts->effects, Histogram_Equalise)){
  210. apt_histogramEqualise(img.prow, img.nrow, APT_CHA_OFFSET, APT_CH_WIDTH);
  211. apt_histogramEqualise(img.prow, img.nrow, APT_CHB_OFFSET, APT_CH_WIDTH);
  212. }
  213. // Raw image
  214. if (CONTAINS(opts->type, Raw_Image)) {
  215. sprintf(desc, "%s (%s) & %s (%s)", ch.id[img.chA], ch.name[img.chA], ch.id[img.chB], ch.name[img.chB]);
  216. ImageOut(opts, &img, 0, APT_IMG_WIDTH, desc, Raw_Image, NULL);
  217. }
  218. // Palette image
  219. if (CONTAINS(opts->type, Palleted)) {
  220. img.palette = opts->palette;
  221. strcpy(desc, "Palette composite");
  222. ImageOut(opts, &img, APT_CHA_OFFSET, APT_CH_WIDTH, desc, Palleted, NULL);
  223. }
  224. // Channel A
  225. if (CONTAINS(opts->type, Channel_A)) {
  226. sprintf(desc, "%s (%s)", ch.id[img.chA], ch.name[img.chA]);
  227. ImageOut(opts, &img, APT_CHA_OFFSET, APT_CH_WIDTH, desc, Channel_A, NULL);
  228. }
  229. // Channel B
  230. if (CONTAINS(opts->type, Channel_B)) {
  231. sprintf(desc, "%s (%s)", ch.id[img.chB], ch.name[img.chB]);
  232. ImageOut(opts, &img, APT_CHB_OFFSET, APT_CH_WIDTH, desc, Channel_B, NULL);
  233. }
  234. return 1;
  235. }
  236. float *samplebuf;
  237. static int initsnd(char *filename) {
  238. SF_INFO infwav;
  239. int res;
  240. // Open audio file
  241. infwav.format = 0;
  242. audioFile = sf_open(filename, SFM_READ, &infwav);
  243. if (audioFile == NULL) {
  244. error_noexit("Could not file");
  245. return 0;
  246. }
  247. res = apt_init(infwav.samplerate);
  248. printf("Input file: %s\n", filename);
  249. if(res < 0) {
  250. error_noexit("Input sample rate too low");
  251. return 0;
  252. }else if(res > 0) {
  253. error_noexit("Input sample rate too high");
  254. return 0;
  255. }
  256. printf("Input sample rate: %d\n", infwav.samplerate);
  257. channels = infwav.channels;
  258. samplebuf = (float *)malloc(sizeof(float) * 32768 * channels);
  259. return 1;
  260. }
  261. // Read samples from the audio file
  262. int getsamples(void *context, float *samples, int nb) {
  263. (void) context;
  264. if (channels == 1){
  265. return (int)sf_read_float(audioFile, samples, nb);
  266. } else if (channels == 2) {
  267. // Stereo channels are interleaved
  268. int samplesRead = (int)sf_read_float(audioFile, samplebuf, nb * channels);
  269. for(int i = 0; i < nb; i++) {
  270. samples[i] = samplebuf[i * channels];
  271. }
  272. return samplesRead / channels;
  273. } else {
  274. printf("Only mono and stereo input files are supported\n");
  275. exit(1);
  276. }
  277. }