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.
 
 
 
 
 

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