No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 

470 líneas
13 KiB

  1. /*
  2. * Atpdec
  3. * Copyright (c) 2003 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 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
  41. initsnd (char *filename)
  42. {
  43. SF_INFO infwav;
  44. /* open wav input file */
  45. infwav.format = 0;
  46. inwav = sf_open (filename, SFM_READ, &infwav);
  47. if (inwav == NULL) {
  48. fprintf (stderr, "could not open %s\n", filename);
  49. return (1);
  50. }
  51. if (infwav.samplerate != 11025) {
  52. fprintf (stderr, "Bad Input File sample rate: %d. Must be 11025\n",
  53. infwav.samplerate);
  54. return (1);
  55. }
  56. if (infwav.channels != 1) {
  57. fprintf (stderr, "Too many channels in input file : %d\n",
  58. infwav.channels);
  59. return (1);
  60. }
  61. return (0);
  62. }
  63. int
  64. getsample (float *sample, int nb)
  65. {
  66. return (sf_read_float (inwav, sample, nb));
  67. }
  68. static png_text text_ptr[] = {
  69. {PNG_TEXT_COMPRESSION_NONE, "Software", version, sizeof (version)}
  70. ,
  71. {PNG_TEXT_COMPRESSION_NONE, "Channel", NULL, 0}
  72. ,
  73. {PNG_TEXT_COMPRESSION_NONE, "Description", "NOAA POES satellite Image", 25}
  74. };
  75. static int
  76. ImageOut (char *filename, char *chid, float **prow, int nrow, int depth,
  77. int width, int offset)
  78. {
  79. FILE *pngfile;
  80. png_infop info_ptr;
  81. png_structp png_ptr;
  82. int n;
  83. /* init png lib */
  84. png_ptr = 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. depth, 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, "w");
  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. float pv;
  118. pv = pixelv[i + offset];
  119. switch (depth) {
  120. case 8:
  121. pixel[i] = floor (pv);
  122. break;
  123. case 16:
  124. ((unsigned short *) pixel)[i] =
  125. htons ((unsigned short) floor (pv * 255.0));
  126. break;
  127. }
  128. }
  129. png_write_row (png_ptr, pixel);
  130. }
  131. png_write_end (png_ptr, info_ptr);
  132. fclose (pngfile);
  133. printf ("Done\n");
  134. png_destroy_write_struct (&png_ptr, &info_ptr);
  135. return (0);
  136. }
  137. int
  138. ImageColorOut (char *filename, float **prow, int nrow)
  139. {
  140. FILE *pngfile;
  141. png_infop info_ptr;
  142. png_structp png_ptr;
  143. int n;
  144. float *pixelc,*pixeln,*pixelp;
  145. float pv[CH_WIDTH];
  146. float pt[CH_WIDTH];
  147. extern void false_color (double v, double t, float *r, float *g, float *b);
  148. extern void dres(
  149. float av, float at, float bv, float bt,
  150. float cv, float ct, float dv, float dt,
  151. float *v , float *t);
  152. /* init png lib */
  153. png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  154. if (!png_ptr) {
  155. fprintf (stderr, "could not open create png_ptr\n");
  156. return (1);
  157. }
  158. info_ptr = png_create_info_struct (png_ptr);
  159. if (!info_ptr) {
  160. png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
  161. fprintf (stderr, "could not open create info_ptr\n");
  162. return (1);
  163. }
  164. png_set_IHDR (png_ptr, info_ptr, 2*CH_WIDTH-3, 2*nrow-3,
  165. 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
  166. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  167. text_ptr[1].text = "False Colors";
  168. png_set_pHYs (png_ptr, info_ptr, 2000, 2000, PNG_RESOLUTION_METER);
  169. text_ptr[1].text_length = strlen (text_ptr[1].text);
  170. png_set_text (png_ptr, info_ptr, text_ptr, 3);
  171. printf ("Computing False colors & writing : %s ...", filename);
  172. fflush (stdout);
  173. pngfile = fopen (filename, "w");
  174. if (pngfile == NULL) {
  175. fprintf (stderr, "could not open %s\n", filename);
  176. return (1);
  177. }
  178. png_init_io (png_ptr, pngfile);
  179. png_write_info (png_ptr, info_ptr);
  180. pixelc=NULL;pixeln=prow[0];
  181. for (n = 0; n < nrow-1; n++) {
  182. png_color pixa[2*CH_WIDTH];
  183. png_color pixb[2*CH_WIDTH];
  184. int i;
  185. pixelp=pixelc;pixelc=pixeln;pixeln=prow[n+1];
  186. for (i = 0; i < CH_WIDTH-1; i++) {
  187. float av,bv,cv,dv;
  188. float at,bt,ct,dt;
  189. float pvc,ptc;
  190. float v,t;
  191. float r, g, b;
  192. av = pixelc[i+SYNC_WIDTH + SPC_WIDTH];
  193. bv = pixeln[i+SYNC_WIDTH + SPC_WIDTH];
  194. cv = pixelc[i+1+SYNC_WIDTH + SPC_WIDTH];
  195. dv = pixeln[i+1+SYNC_WIDTH + SPC_WIDTH];
  196. if(n!=0) {
  197. at = 0.667*pixelc[i+SYNC_WIDTH + SPC_WIDTH + CH_OFFSET]
  198. + 0.333*pixelp[i+SYNC_WIDTH + SPC_WIDTH + CH_OFFSET];
  199. bt = 0.667*pixeln[i+SYNC_WIDTH + SPC_WIDTH + CH_OFFSET]
  200. + 0.333*pixelc[i+SYNC_WIDTH + SPC_WIDTH + CH_OFFSET];
  201. ct = 0.667*pixelc[i+1+SYNC_WIDTH + SPC_WIDTH + CH_OFFSET]
  202. + 0.333*pixelp[i+1+SYNC_WIDTH + SPC_WIDTH + CH_OFFSET];
  203. dt = 0.667*pixeln[i+1+SYNC_WIDTH + SPC_WIDTH + CH_OFFSET]
  204. + 0.333*pixelc[i+1+SYNC_WIDTH + SPC_WIDTH + CH_OFFSET];
  205. } else {
  206. at = pixelc[i+SYNC_WIDTH + SPC_WIDTH + CH_OFFSET];
  207. bt = pixeln[i+SYNC_WIDTH + SPC_WIDTH + CH_OFFSET];
  208. ct = pixelc[i+1+SYNC_WIDTH + SPC_WIDTH + CH_OFFSET];
  209. dt = pixeln[i+1+SYNC_WIDTH + SPC_WIDTH + CH_OFFSET];
  210. }
  211. dres (av,at,bv,bt,cv,ct,dv,dt,&v,&t);
  212. falsecolor (v, t, &r, &g, &b);
  213. pixb[2*i].red = (unsigned int) (255.0 * r);
  214. pixb[2*i].green = (unsigned int) (255.0 * g);
  215. pixb[2*i].blue = (unsigned int) (255.0 * b);
  216. pvc=v;ptc=t;
  217. if(n!=0) {
  218. av = pv[i];
  219. bv = pixelc[i+SYNC_WIDTH + SPC_WIDTH];
  220. cv = pixelc[i+1+SYNC_WIDTH + SPC_WIDTH];
  221. dv = pvc;
  222. at = pt[i];
  223. bt = 0.667*pixelc[i+SYNC_WIDTH + SPC_WIDTH + CH_OFFSET]
  224. + 0.333*pixelp[i+SYNC_WIDTH + SPC_WIDTH + CH_OFFSET];
  225. ct = 0.667*pixelc[i+1+SYNC_WIDTH + SPC_WIDTH + CH_OFFSET]
  226. + 0.333*pixelp[i+1+SYNC_WIDTH + SPC_WIDTH + CH_OFFSET];
  227. dt = ptc;
  228. dres (av,at,bv,bt,cv,ct,dv,dt,&v,&t);
  229. falsecolor (v, t, &r, &g, &b);
  230. pixa[2*i].red = (unsigned int) (255.0 * r);
  231. pixa[2*i].green = (unsigned int) (255.0 * g);
  232. pixa[2*i].blue = (unsigned int) (255.0 * b);
  233. }
  234. pv[i]=pvc;pt[i]=ptc;
  235. if (i!=0) {
  236. if (n!=0) {
  237. v = pixelc[i+SYNC_WIDTH + SPC_WIDTH];
  238. t = 0.667*pixelc[i+SYNC_WIDTH + SPC_WIDTH + CH_OFFSET]
  239. + 0.333*pixelp[i+SYNC_WIDTH + SPC_WIDTH + CH_OFFSET];
  240. falsecolor (v, t, &r, &g, &b);
  241. pixa[2*i-1].red = (unsigned int) (255.0 * r);
  242. pixa[2*i-1].green = (unsigned int) (255.0 * g);
  243. pixa[2*i-1].blue = (unsigned int) (255.0 * b);
  244. }
  245. av = pixelc[i+SYNC_WIDTH + SPC_WIDTH];
  246. bv = pv[i-1];
  247. cv = pvc;
  248. dv = pixeln[i+SYNC_WIDTH + SPC_WIDTH];
  249. if(n!=0) {
  250. at = 0.667*pixelc[i+SYNC_WIDTH + SPC_WIDTH + CH_OFFSET]
  251. + 0.333*pixelp[i+SYNC_WIDTH + SPC_WIDTH + CH_OFFSET];
  252. dt = 0.667*pixeln[i+SYNC_WIDTH + SPC_WIDTH + CH_OFFSET]
  253. + 0.333*pixelc[i+SYNC_WIDTH + SPC_WIDTH + CH_OFFSET];
  254. } else {
  255. at = pixelc[i+SYNC_WIDTH + SPC_WIDTH + CH_OFFSET];
  256. dt = pixeln[i+SYNC_WIDTH + SPC_WIDTH + CH_OFFSET];
  257. }
  258. bt = pt[i-1];
  259. ct = ptc;
  260. dres (av,at,bv,bt,cv,ct,dv,dt,&v,&t);
  261. falsecolor (v, t, &r, &g, &b);
  262. pixb[2*i-1].red = (unsigned int) (255.0 * r);
  263. pixb[2*i-1].green = (unsigned int) (255.0 * g);
  264. pixb[2*i-1].blue = (unsigned int) (255.0 * b);
  265. }
  266. }
  267. if(n!=0) png_write_row (png_ptr, (png_bytep) pixa);
  268. png_write_row (png_ptr, (png_bytep) pixb);
  269. }
  270. png_write_end (png_ptr, info_ptr);
  271. fclose (pngfile);
  272. printf ("Done\n");
  273. png_destroy_write_struct (&png_ptr, &info_ptr);
  274. return (0);
  275. }
  276. extern int Calibrate (float **prow, int nrow, int offset);
  277. extern int Temperature (float **prow, int nrow, int ch, int offset);
  278. extern void readfconf (char *file);
  279. extern int optind, opterr;
  280. extern char *optarg;
  281. int satnum = 1;
  282. static void
  283. usage (void)
  284. {
  285. fprintf (stderr, "atpdec [options] soundfiles ...\n");
  286. fprintf (stderr,
  287. "options:\n-d <dir>\tDestination directory\n-i [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-p\t\t16bits output\n-s [0|1]\tSatellite id (for temperature and false color generation)\n\t\t\t0:NOAA15\n\t\t\t1:NOAA17\n");
  288. }
  289. main (int argc, char **argv)
  290. {
  291. char pngfilename[1024];
  292. char name[1024];
  293. char pngdirname[1024] = "";
  294. char imgopt[20] = "ac";
  295. float *prow[3000];
  296. char *chid[6] = { "1", "2", "3A", "4", "5", "3B" };
  297. int depth = 8;
  298. int n, nrow;
  299. int ch;
  300. int c;
  301. printf ("%s\n", version);
  302. opterr = 0;
  303. while ((c = getopt (argc, argv, "c:d:i:ps:")) != EOF) {
  304. switch (c) {
  305. case 'd':
  306. strcpy (pngdirname, optarg);
  307. break;
  308. case 'c':
  309. readfconf (optarg);
  310. break;
  311. case 'i':
  312. strcpy (imgopt, optarg);
  313. break;
  314. case 'p':
  315. depth = 16;
  316. break;
  317. case 's':
  318. satnum = atoi (optarg);
  319. if (satnum < 0 || satnum > 1) {
  320. fprintf (stderr, "invalid satellite\n");
  321. exit (1);
  322. }
  323. break;
  324. default:
  325. usage ();
  326. }
  327. }
  328. for (nrow = 0; nrow < 3000; nrow++)
  329. prow[nrow] = NULL;
  330. for (; optind < argc; optind++) {
  331. int a = 0, b = 0;
  332. strcpy (pngfilename, argv[optind]);
  333. strcpy (name, basename (pngfilename));
  334. strtok (name, ".");
  335. if (pngdirname[0] == '\0') {
  336. strcpy (pngfilename, argv[optind]);
  337. strcpy (pngdirname, dirname (pngfilename));
  338. }
  339. /* open snd input */
  340. if (initsnd (argv[optind]))
  341. exit (1);
  342. /* main loop */
  343. printf ("Decoding: %s \n", argv[optind]);
  344. for (nrow = 0; nrow < 3000; nrow++) {
  345. if (prow[nrow] == NULL)
  346. prow[nrow] = (float *) malloc (sizeof (float) * 2150);
  347. if (getpixelrow (prow[nrow]) == 0)
  348. break;
  349. printf ("%d\r", nrow);
  350. fflush (stdout);
  351. }
  352. printf ("\nDone\n", nrow);
  353. sf_close (inwav);
  354. /* raw image */
  355. if (strchr (imgopt, (int) 'r') != NULL) {
  356. sprintf (pngfilename, "%s/%s-r.png", pngdirname, name);
  357. ImageOut (pngfilename, "raw", prow, nrow, depth, IMG_WIDTH, 0);
  358. }
  359. /* Channel A */
  360. if (((strchr (imgopt, (int) 'a') != NULL)
  361. || (strchr (imgopt, (int) 'c') != NULL)
  362. || (strchr (imgopt, (int) 'd') != NULL))) {
  363. ch = Calibrate (prow, nrow, SYNC_WIDTH);
  364. if (ch >= 0) {
  365. if (strchr (imgopt, (int) 'a') != NULL) {
  366. sprintf (pngfilename, "%s/%s-%s.png", pngdirname, name, chid[ch]);
  367. ImageOut (pngfilename, chid[ch], prow, nrow, depth,
  368. SPC_WIDTH + CH_WIDTH + TELE_WIDTH, SYNC_WIDTH);
  369. }
  370. }
  371. if (ch < 2)
  372. a = 1;
  373. }
  374. /* Channel B */
  375. if ((strchr (imgopt, (int) 'b') != NULL)
  376. || (strchr (imgopt, (int) 'c') != NULL)
  377. || (strchr (imgopt, (int) 't') != NULL)
  378. || (strchr (imgopt, (int) 'd') != NULL)) {
  379. ch = Calibrate (prow, nrow, CH_OFFSET + SYNC_WIDTH);
  380. if (ch >= 0) {
  381. if (strchr (imgopt, (int) 'b') != NULL) {
  382. sprintf (pngfilename, "%s/%s-%s.png", pngdirname, name, chid[ch]);
  383. ImageOut (pngfilename, chid[ch], prow, nrow, depth,
  384. SPC_WIDTH + CH_WIDTH + TELE_WIDTH,
  385. CH_OFFSET + SYNC_WIDTH);
  386. }
  387. }
  388. if (ch > 2) {
  389. b = 1;
  390. Temperature (prow, nrow, ch, CH_OFFSET + SYNC_WIDTH);
  391. if (strchr (imgopt, (int) 't') != NULL) {
  392. sprintf (pngfilename, "%s/%s-t.png", pngdirname, name);
  393. ImageOut (pngfilename, "Temperature", prow, nrow, depth,
  394. CH_WIDTH, CH_OFFSET + SYNC_WIDTH + SPC_WIDTH);
  395. }
  396. }
  397. }
  398. /* distribution */
  399. if (a && b && strchr (imgopt, (int) 'd') != NULL) {
  400. sprintf (pngfilename, "%s/%s-d.pnm", pngdirname, name);
  401. }
  402. /* color image */
  403. if (a && b && strchr (imgopt, (int) 'c') != NULL) {
  404. sprintf (pngfilename, "%s/%s-c.png", pngdirname, name);
  405. ImageColorOut (pngfilename, prow, nrow);
  406. }
  407. }
  408. exit (0);
  409. }