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.

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