Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

main.c 9.3 KiB

21 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * Atpdec
  3. * Copyright (c) 2004-2005 by Thierry Leconte (F4DWV)
  4. *
  5. * $Id$
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Library General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. *
  21. */
  22. #include <stdio.h>
  23. #ifdef WIN32
  24. #include "w32util.h"
  25. #else
  26. #include <libgen.h>
  27. #endif
  28. #include <string.h>
  29. #include <sndfile.h>
  30. #include <png.h>
  31. #include "version.h"
  32. extern int getpixelrow(float *pixelv);
  33. #define SYNC_WIDTH 39
  34. #define SPC_WIDTH 47
  35. #define TELE_WIDTH 45
  36. #define CH_WIDTH 909
  37. #define CH_OFFSET (SYNC_WIDTH+SPC_WIDTH+CH_WIDTH+TELE_WIDTH)
  38. #define IMG_WIDTH 2080
  39. static SNDFILE *inwav;
  40. static int initsnd(char *filename)
  41. {
  42. SF_INFO infwav;
  43. /* open wav input file */
  44. infwav.format = 0;
  45. inwav = sf_open(filename, SFM_READ, &infwav);
  46. if (inwav == NULL) {
  47. fprintf(stderr, "could not open %s\n", filename);
  48. return (1);
  49. }
  50. if (infwav.samplerate != 11025) {
  51. fprintf(stderr, "Bad Input File sample rate: %d. Must be 11025\n",
  52. infwav.samplerate);
  53. return (1);
  54. }
  55. if (infwav.channels != 1) {
  56. fprintf(stderr, "Too many channels in input file : %d\n",
  57. infwav.channels);
  58. return (1);
  59. }
  60. return (0);
  61. }
  62. int getsample(float *sample, int nb)
  63. {
  64. return (sf_read_float(inwav, sample, nb));
  65. }
  66. static png_text text_ptr[] = {
  67. {PNG_TEXT_COMPRESSION_NONE, "Software", version, sizeof(version)}
  68. ,
  69. {PNG_TEXT_COMPRESSION_NONE, "Channel", NULL, 0}
  70. ,
  71. {PNG_TEXT_COMPRESSION_NONE, "Description", "NOAA POES satellite Image",
  72. 25}
  73. };
  74. static int
  75. ImageOut(char *filename, char *chid, float **prow, int nrow,
  76. int width, int offset)
  77. {
  78. FILE *pngfile;
  79. png_infop info_ptr;
  80. png_structp png_ptr;
  81. int n;
  82. /* init png lib */
  83. png_ptr =
  84. png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  85. if (!png_ptr) {
  86. fprintf(stderr, "could not open create png_ptr\n");
  87. return (1);
  88. }
  89. info_ptr = png_create_info_struct(png_ptr);
  90. if (!info_ptr) {
  91. png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
  92. fprintf(stderr, "could not open create info_ptr\n");
  93. return (1);
  94. }
  95. png_set_IHDR(png_ptr, info_ptr, width, nrow,
  96. 8, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
  97. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  98. text_ptr[1].text = chid;
  99. text_ptr[1].text_length = strlen(chid);
  100. png_set_text(png_ptr, info_ptr, text_ptr, 3);
  101. png_set_pHYs(png_ptr, info_ptr, 4000, 4000, PNG_RESOLUTION_METER);
  102. printf("Writing %s ... ", filename);
  103. fflush(stdout);
  104. pngfile = fopen(filename, "wb");
  105. if (pngfile == NULL) {
  106. fprintf(stderr, "could not open %s\n", filename);
  107. return (1);
  108. }
  109. png_init_io(png_ptr, pngfile);
  110. png_write_info(png_ptr, info_ptr);
  111. for (n = 0; n < nrow; n++) {
  112. float *pixelv;
  113. png_byte pixel[2*IMG_WIDTH];
  114. int i;
  115. pixelv = prow[n];
  116. for (i = 0; i < width; i++) {
  117. pixel[i] = pixelv[i + offset];
  118. }
  119. png_write_row(png_ptr, pixel);
  120. }
  121. png_write_end(png_ptr, info_ptr);
  122. fclose(pngfile);
  123. printf("Done\n");
  124. png_destroy_write_struct(&png_ptr, &info_ptr);
  125. return (0);
  126. }
  127. int ImageColorOut(char *filename, float **prow, int nrow)
  128. {
  129. FILE *pngfile;
  130. png_infop info_ptr;
  131. png_structp png_ptr;
  132. int n;
  133. float *pixelc;
  134. extern void falsecolor(double v, double t, float *r, float *g,
  135. float *b);
  136. /* init png lib */
  137. png_ptr =
  138. png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  139. if (!png_ptr) {
  140. fprintf(stderr, "could not open create png_ptr\n");
  141. return (1);
  142. }
  143. info_ptr = png_create_info_struct(png_ptr);
  144. if (!info_ptr) {
  145. png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
  146. fprintf(stderr, "could not open create info_ptr\n");
  147. return (1);
  148. }
  149. png_set_IHDR(png_ptr, info_ptr, CH_WIDTH , nrow ,
  150. 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
  151. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  152. png_set_pHYs(png_ptr, info_ptr, 4000, 4000, PNG_RESOLUTION_METER);
  153. text_ptr[1].text = "False Colors";
  154. text_ptr[1].text_length = strlen(text_ptr[1].text);
  155. png_set_text(png_ptr, info_ptr, text_ptr, 3);
  156. printf("Computing False colors & writing : %s ...", filename);
  157. fflush(stdout);
  158. pngfile = fopen(filename, "wb");
  159. if (pngfile == NULL) {
  160. fprintf(stderr, "could not open %s\n", filename);
  161. return (1);
  162. }
  163. png_init_io(png_ptr, pngfile);
  164. png_write_info(png_ptr, info_ptr);
  165. for (n = 0; n < nrow ; n++) {
  166. png_color pix[CH_WIDTH];
  167. int i;
  168. pixelc = prow[n];
  169. for (i = 0; i < CH_WIDTH - 1; i++) {
  170. float v, t;
  171. float r, g, b;
  172. v = pixelc[i+SYNC_WIDTH + SPC_WIDTH];
  173. t = pixelc[i+SYNC_WIDTH + SPC_WIDTH + CH_OFFSET];
  174. falsecolor(v, t, &r, &g, &b);
  175. pix[i].red = 255.0 * r;
  176. pix[i].green = 255.0 * g;
  177. pix[i].blue = 255.0 * b;
  178. }
  179. png_write_row(png_ptr, (png_bytep) pix);
  180. }
  181. png_write_end(png_ptr, info_ptr);
  182. fclose(pngfile);
  183. printf("Done\n");
  184. png_destroy_write_struct(&png_ptr, &info_ptr);
  185. return (0);
  186. }
  187. extern int Calibrate(float **prow, int nrow, int offset);
  188. extern void Temperature(float **prow, int nrow, int ch, int offset);
  189. extern void readfconf(char *file);
  190. extern int optind, opterr;
  191. extern char *optarg;
  192. int satnum = 1;
  193. static void usage(void)
  194. {
  195. fprintf(stderr, "atpdec [options] soundfiles ...\n");
  196. fprintf(stderr,
  197. "options:\n-d <dir>\tDestination directory\n-i [r|a|b|c|t]\tOutput image type\n\t\t\tr: Raw\n\t\t\ta: A chan.\n\t\t\tb: B chan.\n\t\t\tc: False color\n\t\t\tt: Temperature\n-c <file>\tFalse color config file\n-s [15|16|17|18]\tSatellite number (for temperature and false color generation)\n");
  198. exit(1);
  199. }
  200. int main(int argc, char **argv)
  201. {
  202. char pngfilename[1024];
  203. char name[1024];
  204. char pngdirname[1024] = "";
  205. char imgopt[20] = "ac";
  206. float *prow[3000];
  207. char *chid[6] = { "1", "2", "3A", "4", "5", "3B" };
  208. int nrow;
  209. int ch;
  210. int c;
  211. printf("%s\n", version);
  212. opterr = 0;
  213. while ((c = getopt(argc, argv, "c:d:i:s:")) != EOF) {
  214. switch (c) {
  215. case 'd':
  216. strcpy(pngdirname, optarg);
  217. break;
  218. case 'c':
  219. readfconf(optarg);
  220. break;
  221. case 'i':
  222. strcpy(imgopt, optarg);
  223. break;
  224. case 's':
  225. satnum = atoi(optarg)-15;
  226. if (satnum < 0 || satnum > 3) {
  227. fprintf(stderr, "invalid satellite number : must be in [15-18]\n");
  228. exit(1);
  229. }
  230. break;
  231. default:
  232. usage();
  233. }
  234. }
  235. for (nrow = 0; nrow < 3000; nrow++)
  236. prow[nrow] = NULL;
  237. for (; optind < argc; optind++) {
  238. int a = 0, b = 0;
  239. strcpy(pngfilename, argv[optind]);
  240. strcpy(name, basename(pngfilename));
  241. strtok(name, ".");
  242. if (pngdirname[0] == '\0') {
  243. strcpy(pngfilename, argv[optind]);
  244. strcpy(pngdirname, dirname(pngfilename));
  245. }
  246. /* open snd input */
  247. if (initsnd(argv[optind]))
  248. exit(1);
  249. /* main loop */
  250. printf("Decoding: %s \n", argv[optind]);
  251. for (nrow = 0; nrow < 3000; nrow++) {
  252. if (prow[nrow] == NULL)
  253. prow[nrow] = (float *) malloc(sizeof(float) * 2150);
  254. if (getpixelrow(prow[nrow]) == 0)
  255. break;
  256. printf("%d\r", nrow);
  257. fflush(stdout);
  258. }
  259. printf("\nDone\n");
  260. sf_close(inwav);
  261. /* raw image */
  262. if (strchr(imgopt, (int) 'r') != NULL) {
  263. sprintf(pngfilename, "%s/%s-r.png", pngdirname, name);
  264. ImageOut(pngfilename, "raw", prow, nrow, IMG_WIDTH, 0);
  265. }
  266. /* Channel A */
  267. if (((strchr(imgopt, (int) 'a') != NULL)
  268. || (strchr(imgopt, (int) 'c') != NULL)
  269. || (strchr(imgopt, (int) 'd') != NULL))) {
  270. ch = Calibrate(prow, nrow, SYNC_WIDTH);
  271. if (ch >= 0) {
  272. if (strchr(imgopt, (int) 'a') != NULL) {
  273. sprintf(pngfilename, "%s/%s-%s.png", pngdirname, name,
  274. chid[ch]);
  275. ImageOut(pngfilename, chid[ch], prow, nrow,
  276. SPC_WIDTH + CH_WIDTH + TELE_WIDTH,
  277. SYNC_WIDTH);
  278. }
  279. }
  280. if (ch < 2)
  281. a = 1;
  282. }
  283. /* Channel B */
  284. if ((strchr(imgopt, (int) 'b') != NULL)
  285. || (strchr(imgopt, (int) 'c') != NULL)
  286. || (strchr(imgopt, (int) 't') != NULL)
  287. || (strchr(imgopt, (int) 'd') != NULL)) {
  288. ch = Calibrate(prow, nrow, CH_OFFSET + SYNC_WIDTH);
  289. if (ch >= 0) {
  290. if (strchr(imgopt, (int) 'b') != NULL) {
  291. sprintf(pngfilename, "%s/%s-%s.png", pngdirname, name,
  292. chid[ch]);
  293. ImageOut(pngfilename, chid[ch], prow, nrow,
  294. SPC_WIDTH + CH_WIDTH + TELE_WIDTH,
  295. CH_OFFSET + SYNC_WIDTH);
  296. }
  297. }
  298. if (ch > 2) {
  299. b = 1;
  300. Temperature(prow, nrow, ch, CH_OFFSET + SYNC_WIDTH);
  301. if (strchr(imgopt, (int) 't') != NULL) {
  302. sprintf(pngfilename, "%s/%s-t.png", pngdirname, name);
  303. ImageOut(pngfilename, "Temperature", prow, nrow,
  304. CH_WIDTH, CH_OFFSET + SYNC_WIDTH + SPC_WIDTH);
  305. }
  306. }
  307. }
  308. /* distribution */
  309. if (a && b && strchr(imgopt, (int) 'd') != NULL) {
  310. sprintf(pngfilename, "%s/%s-d.pnm", pngdirname, name);
  311. }
  312. /* color image */
  313. if (a && b && strchr(imgopt, (int) 'c') != NULL) {
  314. sprintf(pngfilename, "%s/%s-c.png", pngdirname, name);
  315. ImageColorOut(pngfilename, prow, nrow);
  316. }
  317. }
  318. exit(0);
  319. }