Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 

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