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.
 
 
 
 
 

379 lines
9.7 KiB

  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 <stdlib.h>
  23. #include <stdio.h>
  24. #ifdef WIN32
  25. #include "w32util.h"
  26. #else
  27. #include <libgen.h>
  28. #endif
  29. #include <string.h>
  30. #include <sndfile.h>
  31. #include <png.h>
  32. #include "version.h"
  33. extern int getpixelrow(float *pixelv);
  34. extern int init_dsp(double F);;
  35. #define SYNC_WIDTH 39
  36. #define SPC_WIDTH 47
  37. #define TELE_WIDTH 45
  38. #define CH_WIDTH 909
  39. #define CH_OFFSET (SYNC_WIDTH+SPC_WIDTH+CH_WIDTH+TELE_WIDTH)
  40. #define IMG_WIDTH 2080
  41. static SNDFILE *inwav;
  42. static int initsnd(char *filename)
  43. {
  44. SF_INFO infwav;
  45. int res;
  46. /* open wav input file */
  47. infwav.format = 0;
  48. inwav = sf_open(filename, SFM_READ, &infwav);
  49. if (inwav == NULL) {
  50. fprintf(stderr, "could not open %s\n", filename);
  51. return (1);
  52. }
  53. res=init_dsp(infwav.samplerate);
  54. if(res<0) {
  55. fprintf(stderr, "Sample rate too low : %d\n", infwav.samplerate);
  56. return (1);
  57. }
  58. if(res>0) {
  59. fprintf(stderr, "Sample rate too hight : %d\n", infwav.samplerate);
  60. return (1);
  61. }
  62. fprintf(stderr, "Sample rate : %d\n", infwav.samplerate);
  63. if (infwav.channels != 1) {
  64. fprintf(stderr, "Too many channels in input file : %d\n", infwav.channels);
  65. return (1);
  66. }
  67. return (0);
  68. }
  69. int getsample(float *sample, int nb)
  70. {
  71. return (sf_read_float(inwav, sample, nb));
  72. }
  73. static png_text text_ptr[] = {
  74. {PNG_TEXT_COMPRESSION_NONE, "Software", version, sizeof(version)}
  75. ,
  76. {PNG_TEXT_COMPRESSION_NONE, "Channel", NULL, 0}
  77. ,
  78. {PNG_TEXT_COMPRESSION_NONE, "Description", "NOAA POES satellite Image",
  79. 25}
  80. };
  81. static int
  82. ImageOut(char *filename, char *chid, float **prow, int nrow,
  83. int width, int offset)
  84. {
  85. FILE *pngfile;
  86. png_infop info_ptr;
  87. png_structp png_ptr;
  88. int n;
  89. /* init png lib */
  90. png_ptr =
  91. png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  92. if (!png_ptr) {
  93. fprintf(stderr, "could not open create png_ptr\n");
  94. return (1);
  95. }
  96. info_ptr = png_create_info_struct(png_ptr);
  97. if (!info_ptr) {
  98. png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
  99. fprintf(stderr, "could not open create info_ptr\n");
  100. return (1);
  101. }
  102. png_set_IHDR(png_ptr, info_ptr, width, nrow,
  103. 8, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
  104. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  105. text_ptr[1].text = chid;
  106. text_ptr[1].text_length = strlen(chid);
  107. png_set_text(png_ptr, info_ptr, text_ptr, 3);
  108. png_set_pHYs(png_ptr, info_ptr, 4000, 4000, PNG_RESOLUTION_METER);
  109. printf("Writing %s ... ", filename);
  110. fflush(stdout);
  111. pngfile = fopen(filename, "wb");
  112. if (pngfile == NULL) {
  113. fprintf(stderr, "could not open %s\n", filename);
  114. return (1);
  115. }
  116. png_init_io(png_ptr, pngfile);
  117. png_write_info(png_ptr, info_ptr);
  118. for (n = 0; n < nrow; n++) {
  119. float *pixelv;
  120. png_byte pixel[2*IMG_WIDTH];
  121. int i;
  122. pixelv = prow[n];
  123. for (i = 0; i < width; i++) {
  124. pixel[i] = pixelv[i + offset];
  125. }
  126. png_write_row(png_ptr, pixel);
  127. }
  128. png_write_end(png_ptr, info_ptr);
  129. fclose(pngfile);
  130. printf("Done\n");
  131. png_destroy_write_struct(&png_ptr, &info_ptr);
  132. return (0);
  133. }
  134. int ImageColorOut(char *filename, float **prow, int nrow)
  135. {
  136. FILE *pngfile;
  137. png_infop info_ptr;
  138. png_structp png_ptr;
  139. int n;
  140. float *pixelc, *pixelp;
  141. extern void falsecolor(double v, double t, float *r, float *g,
  142. float *b);
  143. /* init png lib */
  144. png_ptr =
  145. png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  146. if (!png_ptr) {
  147. fprintf(stderr, "could not open create png_ptr\n");
  148. return (1);
  149. }
  150. info_ptr = png_create_info_struct(png_ptr);
  151. if (!info_ptr) {
  152. png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
  153. fprintf(stderr, "could not open create info_ptr\n");
  154. return (1);
  155. }
  156. png_set_IHDR(png_ptr, info_ptr, CH_WIDTH , nrow ,
  157. 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
  158. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  159. png_set_pHYs(png_ptr, info_ptr, 4000, 4000, PNG_RESOLUTION_METER);
  160. text_ptr[1].text = "False Colors";
  161. text_ptr[1].text_length = strlen(text_ptr[1].text);
  162. png_set_text(png_ptr, info_ptr, text_ptr, 3);
  163. printf("Computing False colors & writing : %s ...", filename);
  164. fflush(stdout);
  165. pngfile = fopen(filename, "wb");
  166. if (pngfile == NULL) {
  167. fprintf(stderr, "could not open %s\n", filename);
  168. return (1);
  169. }
  170. png_init_io(png_ptr, pngfile);
  171. png_write_info(png_ptr, info_ptr);
  172. pixelc=prow[0];
  173. for (n = 0; n < nrow ; n++) {
  174. png_color pix[CH_WIDTH];
  175. int i;
  176. pixelp=pixelc;
  177. pixelc = prow[n];
  178. for (i = 0; i < CH_WIDTH - 1; i++) {
  179. float v, t;
  180. float r, g, b;
  181. v = pixelc[i+SYNC_WIDTH + SPC_WIDTH];
  182. t = (2.0*pixelc[i+SYNC_WIDTH + SPC_WIDTH + CH_OFFSET]+pixelp[i+SYNC_WIDTH + SPC_WIDTH + CH_OFFSET])/3.0;
  183. falsecolor(v, t, &r, &g, &b);
  184. pix[i].red = 255.0 * r;
  185. pix[i].green = 255.0 * g;
  186. pix[i].blue = 255.0 * b;
  187. }
  188. png_write_row(png_ptr, (png_bytep) pix);
  189. }
  190. png_write_end(png_ptr, info_ptr);
  191. fclose(pngfile);
  192. printf("Done\n");
  193. png_destroy_write_struct(&png_ptr, &info_ptr);
  194. return (0);
  195. }
  196. extern int Calibrate(float **prow, int nrow, int offset);
  197. extern void Temperature(float **prow, int nrow, int ch, int offset);
  198. extern void readfconf(char *file);
  199. extern int optind, opterr;
  200. extern char *optarg;
  201. int satnum = 1;
  202. static void usage(void)
  203. {
  204. fprintf(stderr, "atpdec [options] soundfiles ...\n");
  205. fprintf(stderr,
  206. "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");
  207. exit(1);
  208. }
  209. int main(int argc, char **argv)
  210. {
  211. char pngfilename[1024];
  212. char name[1024];
  213. char pngdirname[1024] = "";
  214. char imgopt[20] = "ac";
  215. float *prow[3000];
  216. char *chid[6] = { "1", "2", "3A", "4", "5", "3B" };
  217. int nrow;
  218. int ch;
  219. int c;
  220. printf("%s\n", version);
  221. opterr = 0;
  222. while ((c = getopt(argc, argv, "c:d:i:s:")) != EOF) {
  223. switch (c) {
  224. case 'd':
  225. strcpy(pngdirname, optarg);
  226. break;
  227. case 'c':
  228. readfconf(optarg);
  229. break;
  230. case 'i':
  231. strcpy(imgopt, optarg);
  232. break;
  233. case 's':
  234. satnum = atoi(optarg)-15;
  235. if (satnum < 0 || satnum > 3) {
  236. fprintf(stderr, "invalid satellite number : must be in [15-18]\n");
  237. exit(1);
  238. }
  239. break;
  240. default:
  241. usage();
  242. }
  243. }
  244. for (nrow = 0; nrow < 3000; nrow++)
  245. prow[nrow] = NULL;
  246. for (; optind < argc; optind++) {
  247. int a = 0, b = 0;
  248. strcpy(pngfilename, argv[optind]);
  249. strcpy(name, basename(pngfilename));
  250. strtok(name, ".");
  251. if (pngdirname[0] == '\0') {
  252. strcpy(pngfilename, argv[optind]);
  253. strcpy(pngdirname, dirname(pngfilename));
  254. }
  255. /* open snd input */
  256. if (initsnd(argv[optind]))
  257. exit(1);
  258. /* main loop */
  259. printf("Decoding: %s \n", argv[optind]);
  260. for (nrow = 0; nrow < 3000; nrow++) {
  261. if (prow[nrow] == NULL)
  262. prow[nrow] = (float *) malloc(sizeof(float) * 2150);
  263. if (getpixelrow(prow[nrow]) == 0)
  264. break;
  265. printf("%d\r", nrow);
  266. fflush(stdout);
  267. }
  268. printf("\nDone\n");
  269. sf_close(inwav);
  270. /* raw image */
  271. if (strchr(imgopt, (int) 'r') != NULL) {
  272. sprintf(pngfilename, "%s/%s-r.png", pngdirname, name);
  273. ImageOut(pngfilename, "raw", prow, nrow, IMG_WIDTH, 0);
  274. }
  275. /* Channel A */
  276. if (((strchr(imgopt, (int) 'a') != NULL)
  277. || (strchr(imgopt, (int) 'c') != NULL)
  278. || (strchr(imgopt, (int) 'd') != NULL))) {
  279. ch = Calibrate(prow, nrow, SYNC_WIDTH);
  280. if (ch >= 0) {
  281. if (strchr(imgopt, (int) 'a') != NULL) {
  282. sprintf(pngfilename, "%s/%s-%s.png", pngdirname, name,
  283. chid[ch]);
  284. ImageOut(pngfilename, chid[ch], prow, nrow,
  285. SPC_WIDTH + CH_WIDTH + TELE_WIDTH,
  286. SYNC_WIDTH);
  287. }
  288. }
  289. if (ch < 2)
  290. a = 1;
  291. }
  292. /* Channel B */
  293. if ((strchr(imgopt, (int) 'b') != NULL)
  294. || (strchr(imgopt, (int) 'c') != NULL)
  295. || (strchr(imgopt, (int) 't') != NULL)
  296. || (strchr(imgopt, (int) 'd') != NULL)) {
  297. ch = Calibrate(prow, nrow, CH_OFFSET + SYNC_WIDTH);
  298. if (ch >= 0) {
  299. if (strchr(imgopt, (int) 'b') != NULL) {
  300. sprintf(pngfilename, "%s/%s-%s.png", pngdirname, name,
  301. chid[ch]);
  302. ImageOut(pngfilename, chid[ch], prow, nrow,
  303. SPC_WIDTH + CH_WIDTH + TELE_WIDTH,
  304. CH_OFFSET + SYNC_WIDTH);
  305. }
  306. }
  307. if (ch > 2) {
  308. b = 1;
  309. Temperature(prow, nrow, ch, CH_OFFSET + SYNC_WIDTH);
  310. if (strchr(imgopt, (int) 't') != NULL) {
  311. sprintf(pngfilename, "%s/%s-t.png", pngdirname, name);
  312. ImageOut(pngfilename, "Temperature", prow, nrow,
  313. CH_WIDTH, CH_OFFSET + SYNC_WIDTH + SPC_WIDTH);
  314. }
  315. }
  316. }
  317. /* distribution */
  318. if (a && b && strchr(imgopt, (int) 'd') != NULL) {
  319. sprintf(pngfilename, "%s/%s-d.pnm", pngdirname, name);
  320. }
  321. /* color image */
  322. if (a && b && strchr(imgopt, (int) 'c') != NULL) {
  323. sprintf(pngfilename, "%s/%s-c.png", pngdirname, name);
  324. ImageColorOut(pngfilename, prow, nrow);
  325. }
  326. }
  327. exit(0);
  328. }