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.
 
 
 
 
 

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